Skip to content

Commit

Permalink
Add formatting to FooScript (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
ludouzi committed Mar 23, 2024
1 parent bf37aca commit db48ffc
Show file tree
Hide file tree
Showing 22 changed files with 1,050 additions and 603 deletions.
12 changes: 10 additions & 2 deletions include/core/scripting/scriptscanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ class FYCORE_EXPORT ScriptScanner
TokRightParen = ')',
TokLeftSquare = '[',
TokRightSquare = ']',
TokSlash = '/',
TokColon = ':',
TokEquals = '=',
TokEscape = '\\',
TokEos = '\0',
TokError,
Expand All @@ -47,15 +50,17 @@ class FYCORE_EXPORT ScriptScanner

struct Token
{
TokenType type;
TokenType type{TokError};
QStringView value;
int position{0};
};

void setup(const QString& input);
Token scanNext();
Token next();
Token peekNext(int delta = 1);

private:
Token scanNext();
[[nodiscard]] Token makeToken(TokenType type) const;
Token literal();

Expand All @@ -66,5 +71,8 @@ class FYCORE_EXPORT ScriptScanner
QStringView m_input;
const QChar* m_start;
const QChar* m_current;

std::vector<Token> m_tokens;
int m_currentTokenIndex;
};
} // namespace Fooyin
71 changes: 71 additions & 0 deletions include/gui/scripting/richtext.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Fooyin
* Copyright © 2024, Luke Taylor <LukeT1@proton.me>
*
* Fooyin is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Fooyin is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Fooyin. If not, see <http://www.gnu.org/licenses/>.
*
*/

#pragma once

#include <QFont>
#include <QColor>

namespace Fooyin {
struct RichFormatting
{
QFont font;
QColor colour;

bool operator==(const RichFormatting& other) const
{
return std::tie(font, colour) == std::tie(other.font, other.colour);
};
};

struct RichTextBlock
{
QString text;
RichFormatting format;

bool operator==(const RichTextBlock& other) const
{
return std::tie(text, format) == std::tie(other.text, other.format);
};
};
using RichText = std::vector<RichTextBlock>;

struct RichScript
{
QString script;
RichText text;

bool operator==(const RichScript& other) const
{
return std::tie(script, text) == std::tie(other.script, other.text);
};

friend QDataStream& operator<<(QDataStream& stream, const RichScript& richScript)
{
stream << richScript.script;
return stream;
}

friend QDataStream& operator>>(QDataStream& stream, RichScript& richScript)
{
stream >> richScript.script;
return stream;
}
};
} // namespace Fooyin
44 changes: 44 additions & 0 deletions include/gui/scripting/scriptformatter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Fooyin
* Copyright © 2024, Luke Taylor <LukeT1@proton.me>
*
* Fooyin is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Fooyin is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Fooyin. If not, see <http://www.gnu.org/licenses/>.
*
*/

#pragma once

#include "fygui_export.h"

#include <gui/scripting/richtext.h>

#include <QColor>
#include <QFont>

namespace Fooyin {
class ScriptFormatterRegistry;

class FYGUI_EXPORT ScriptFormatter
{
public:
ScriptFormatter();
~ScriptFormatter();

RichText evaluate(const QString& input);

private:
struct Private;
std::unique_ptr<Private> p;
};
} // namespace Fooyin
43 changes: 43 additions & 0 deletions include/gui/scripting/scriptformatterregistry.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Fooyin
* Copyright © 2024, Luke Taylor <LukeT1@proton.me>
*
* Fooyin is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Fooyin is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Fooyin. If not, see <http://www.gnu.org/licenses/>.
*
*/

#pragma once

#include "fygui_export.h"

#include <core/scripting/scriptregistry.h>
#include <gui/scripting/richtext.h>

namespace Fooyin {
struct FormattedTextBlock;

class FYGUI_EXPORT ScriptFormatterRegistry
{
public:
ScriptFormatterRegistry();
~ScriptFormatterRegistry();

bool isFormatFunc(const QString& option) const;
void format(RichFormatting& formatting, const QString& func, const QString& option = {}) const;

private:
struct Private;
std::unique_ptr<Private> p;
};
} // namespace Fooyin
Loading

0 comments on commit db48ffc

Please sign in to comment.