Add a command to gradually glitch text in a message

This commit is contained in:
Loren Burkholder 2024-02-27 13:13:08 -05:00
parent 99bbe26609
commit 21d51d9739
No known key found for this signature in database
GPG key ID: AB62CB312CEC2BBC
5 changed files with 57 additions and 8 deletions

View file

@ -93,6 +93,8 @@ CommandCompleter::data(const QModelIndex &index, int role) const
return QStringLiteral("/msgtype "); return QStringLiteral("/msgtype ");
case Glitch: case Glitch:
return QStringLiteral("/glitch "); return QStringLiteral("/glitch ");
case GradualGlitch:
return QStringLiteral("/gradualglitch ");
case Goto: case Goto:
return QStringLiteral("/goto "); return QStringLiteral("/goto ");
case ConvertToDm: case ConvertToDm:
@ -174,6 +176,8 @@ CommandCompleter::data(const QModelIndex &index, int role) const
return tr("/goto <message reference>"); return tr("/goto <message reference>");
case Glitch: case Glitch:
return tr("/glitch <message>"); return tr("/glitch <message>");
case GradualGlitch:
return tr("/gradualglitch <message>");
case ConvertToDm: case ConvertToDm:
return QStringLiteral("/converttodm"); return QStringLiteral("/converttodm");
case ConvertToRoom: case ConvertToRoom:
@ -251,6 +255,8 @@ CommandCompleter::data(const QModelIndex &index, int role) const
return tr("Send a message with a custom message type."); return tr("Send a message with a custom message type.");
case Glitch: case Glitch:
return tr("Send a message with a glitch effect."); return tr("Send a message with a glitch effect.");
case GradualGlitch:
return tr("Send a message that gradually glitches.");
case Goto: case Goto:
return tr("Go to a specific message using an event id, index or matrix: link"); return tr("Go to a specific message using an event id, index or matrix: link");
case ConvertToDm: case ConvertToDm:

View file

@ -49,6 +49,7 @@ public:
Rainfall, Rainfall,
Msgtype, Msgtype,
Glitch, Glitch,
GradualGlitch,
Goto, Goto,
ConvertToDm, ConvertToDm,
ConvertToRoom, ConvertToRoom,

View file

@ -2014,24 +2014,60 @@ utils::removeExpiredEvents()
ApplyEventExpiration::next(std::move(asus)); ApplyEventExpiration::next(std::move(asus));
} }
namespace {
static const QList<QChar> diacritics = []() {
QList<QChar> ret;
for (wchar_t c = u'\u0300'; c <= u'\u036f'; ++c)
ret.append(QChar(c));
return ret;
}();
}
QString QString
utils::glitchText(const QString &text) utils::glitchText(const QString &text)
{ {
static const QList<QChar> diacritics = []() {
QList<QChar> ret;
for (wchar_t c = u'\u0300'; c <= u'\u036f'; ++c)
ret.append(QChar(c));
return ret;
}();
QString result; QString result;
for (int i = 0; i < text.size(); ++i) { for (int i = 0; i < text.size(); ++i) {
result.append(text.at(i)); result.append(text.at(i));
if (QRandomGenerator64::global()->bounded(0, 100) >= 25) if (QRandomGenerator64::global()->bounded(0, 100) >= 25)
result.append( result.append(
diacritics.at(QRandomGenerator64::global()->bounded(0, diacritics.size()))); diacritics.at(QRandomGenerator64::global()->bounded(0, diacritics.size())));
} }
return result;
}
QString
utils::graduallyGlitchText(const QString &text)
{
QString result;
const int noGlitch = text.size() * 0.5;
const int someGlitch = text.size() * 0.8;
const int lotsOfGlitch = text.size() * 0.95;
for (int i = 0; i < text.size(); ++i) {
result.append(text.at(i));
if (i < noGlitch) // first 40% of text is normal
continue;
else if (i < someGlitch) // next 25% is progressively glitchier
{
if (QRandomGenerator64::global()->bounded(noGlitch, someGlitch) <
noGlitch + (i - noGlitch) * 0.05)
result.append(
diacritics.at(QRandomGenerator64::global()->bounded(0, diacritics.size())));
} else if (i < lotsOfGlitch) { // oh no, it's spreading!
if (QRandomGenerator64::global()->bounded(someGlitch, lotsOfGlitch) < i)
result.append(
diacritics.at(QRandomGenerator64::global()->bounded(0, diacritics.size())));
} else { // just give up, your computer is cursed now
do {
if (QRandomGenerator64::global()->bounded(text.size() / 5, text.size()) < i)
result.append(
diacritics.at(QRandomGenerator64::global()->bounded(0, diacritics.size())));
} while (QRandomGenerator64::global()->bounded(0, 100) < 35);
}
}
return result; return result;
} }

View file

@ -208,4 +208,7 @@ removeExpiredEvents();
QString QString
glitchText(const QString &text); glitchText(const QString &text);
QString
graduallyGlitchText(const QString &text);
} }

View file

@ -239,6 +239,7 @@ InputBar::updateTextContentProperties(const QString &t)
QStringLiteral("rainfall"), QStringLiteral("rainfall"),
QStringLiteral("msgtype"), QStringLiteral("msgtype"),
QStringLiteral("glitch"), QStringLiteral("glitch"),
QStringLiteral("gradualglitch"),
QStringLiteral("goto"), QStringLiteral("goto"),
QStringLiteral("converttodm"), QStringLiteral("converttodm"),
QStringLiteral("converttoroom"), QStringLiteral("converttoroom"),
@ -921,6 +922,8 @@ InputBar::command(const QString &command, QString args)
customMsgtype(args.section(' ', 0, 0), args.section(' ', 1, -1)); customMsgtype(args.section(' ', 0, 0), args.section(' ', 1, -1));
} else if (command == QLatin1String("glitch")) { } else if (command == QLatin1String("glitch")) {
message(utils::glitchText(args)); message(utils::glitchText(args));
} else if (command == QLatin1String("gradualglitch")) {
message(utils::graduallyGlitchText(args));
} else if (command == QLatin1String("goto")) { } else if (command == QLatin1String("goto")) {
// Goto has three different modes: // Goto has three different modes:
// 1 - Going directly to a given event ID // 1 - Going directly to a given event ID