add gui.set_syntax_styles

This commit is contained in:
MihailRis
2025-11-23 18:10:06 +03:00
parent 1676c47093
commit 50f6c48771
11 changed files with 149 additions and 43 deletions
+37
View File
@@ -0,0 +1,37 @@
#include "FontStyle.hpp"
#include "data/dv.hpp"
#include "data/dv_util.hpp"
#include "devtools/SyntaxProcessor.hpp"
FontStyle FontStyle::parse(const dv::value& src) {
FontStyle style {};
src.at("bold").get(style.bold);
src.at("italic").get(style.italic);
src.at("strikethrough").get(style.strikethrough);
src.at("underline").get(style.underline);
dv::get_vec(src, "color", style.color);
return style;
}
static void parse_style(
const dv::value& src,
FontStylesScheme& scheme,
const std::string& name,
devtools::SyntaxStyles tag
) {
if (src.has(name)) {
scheme.palette[static_cast<int>(tag)] = FontStyle::parse(src[name]);
}
}
FontStylesScheme FontStylesScheme::parse(const dv::value& src) {
FontStylesScheme scheme {};
scheme.palette.resize(8);
parse_style(src, scheme, "default", devtools::SyntaxStyles::DEFAULT);
parse_style(src, scheme, "keyword", devtools::SyntaxStyles::KEYWORD);
parse_style(src, scheme, "literal", devtools::SyntaxStyles::LITERAL);
parse_style(src, scheme, "comment", devtools::SyntaxStyles::COMMENT);
parse_style(src, scheme, "error", devtools::SyntaxStyles::ERROR);
return scheme;
}
+39
View File
@@ -0,0 +1,39 @@
#pragma once
#include "data/dv_fwd.hpp"
#include <vector>
#include <glm/glm.hpp>
struct FontStyle {
bool bold = false;
bool italic = false;
bool strikethrough = false;
bool underline = false;
glm::vec4 color {1, 1, 1, 1};
FontStyle() = default;
FontStyle(
bool bold,
bool italic,
bool strikethrough,
bool underline,
glm::vec4 color
)
: bold(bold),
italic(italic),
strikethrough(strikethrough),
underline(underline),
color(std::move(color)) {
}
static FontStyle parse(const dv::value& src);
};
struct FontStylesScheme {
std::vector<FontStyle> palette;
std::vector<unsigned char> map;
static FontStylesScheme parse(const dv::value& src);
};
+2 -29
View File
@@ -7,41 +7,14 @@
#include "typedefs.hpp"
#include "FontMetics.hpp"
#include "data/dv_fwd.hpp"
#include "../commons/FontStyle.hpp"
class Texture;
class Batch2D;
class Batch3D;
class Camera;
struct FontStyle {
bool bold = false;
bool italic = false;
bool strikethrough = false;
bool underline = false;
glm::vec4 color {1, 1, 1, 1};
FontStyle() = default;
FontStyle(
bool bold,
bool italic,
bool strikethrough,
bool underline,
glm::vec4 color
)
: bold(bold),
italic(italic),
strikethrough(strikethrough),
underline(underline),
color(std::move(color)) {
}
};
struct FontStylesScheme {
std::vector<FontStyle> palette;
std::vector<ubyte> map;
};
class Font {
int lineHeight;
int yoffset;
+8
View File
@@ -80,6 +80,14 @@ std::shared_ptr<Menu> GUI::getMenu() {
return menu;
}
void GUI::setSyntaxColorScheme(std::unique_ptr<FontStylesScheme> scheme) {
syntaxColorScheme = std::move(scheme);
}
FontStylesScheme* GUI::getSyntaxColorScheme() const {
return syntaxColorScheme.get();
}
void GUI::onAssetsLoad(Assets* assets) {
rootDocument->rebuildIndices();
assets->store(rootDocument, "core:root");
+5
View File
@@ -17,6 +17,7 @@ struct CursorState;
class Engine;
class Input;
class Window;
struct FontStylesScheme;
namespace devtools {
class Editor;
@@ -73,6 +74,7 @@ namespace gui {
std::shared_ptr<UINode> focus;
std::shared_ptr<UINode> tooltip;
std::shared_ptr<UiDocument> rootDocument;
std::unique_ptr<FontStylesScheme> syntaxColorScheme;
std::unordered_map<std::string, std::shared_ptr<UINode>> storage;
std::unique_ptr<Camera> uicamera;
@@ -157,6 +159,9 @@ namespace gui {
/// @deprecated
std::shared_ptr<Container> getContainer() const;
void setSyntaxColorScheme(std::unique_ptr<FontStylesScheme> scheme);
FontStylesScheme* getSyntaxColorScheme() const;
void onAssetsLoad(Assets* assets);
void postRunnable(const runnable& callback);
+3 -1
View File
@@ -918,7 +918,9 @@ void TextBox::onTab(bool shiftPressed) {
void TextBox::refreshSyntax() {
if (!syntax.empty()) {
const auto& processor = gui.getEditor().getSyntaxProcessor();
if (auto styles = processor.highlight(syntax, input)) {
auto scheme = gui.getSyntaxColorScheme();
if (auto styles =
processor.highlight(scheme ? *scheme : FontStylesScheme {}, syntax, input)) {
label->setStyles(std::move(styles));
}
}