Merge pull request #698 from MihailRis/editor-related
editor-related changes
This commit is contained in:
commit
2af7b23b2e
@ -30,4 +30,7 @@ utf8.lower(text: str) -> str
|
||||
|
||||
-- Escapes a string
|
||||
utf8.escape(text: str) -> str
|
||||
|
||||
-- Escapes special XML characters
|
||||
utf8.escape_xml(text: str) -> str
|
||||
```
|
||||
|
||||
@ -30,4 +30,7 @@ utf8.lower(text: str) -> str
|
||||
|
||||
-- Экранирует строку
|
||||
utf8.escape(text: str) -> str
|
||||
|
||||
-- Экранирует спец-символы XML
|
||||
utf8.escape_xml(text: str) -> str
|
||||
```
|
||||
|
||||
@ -187,6 +187,12 @@ string.escape(str: string) -> string
|
||||
|
||||
Экранирует строку. Является псевдонимом `utf8.escape`.
|
||||
|
||||
```lua
|
||||
string.escape_xml(text: str) -> str
|
||||
```
|
||||
|
||||
Экранирует спец-символы XML. Является псевдонимом `utf8.escape_xml`.
|
||||
|
||||
```lua
|
||||
string.pad(str: string, size: number, [опционально] char: string) -> string
|
||||
```
|
||||
|
||||
19
res/devtools/default_syntax_scheme.toml
Normal file
19
res/devtools/default_syntax_scheme.toml
Normal file
@ -0,0 +1,19 @@
|
||||
[default]
|
||||
bold = false
|
||||
italic = false
|
||||
strikethrough = false
|
||||
underline = false
|
||||
color = [0.8, 0.8, 0.8, 1]
|
||||
|
||||
[keyword]
|
||||
bold = true
|
||||
color = [0.9, 0.6, 0.4, 1]
|
||||
|
||||
[literal]
|
||||
color = [0.4, 0.8, 0.5, 1]
|
||||
|
||||
[comment]
|
||||
color = [0.5, 0.7, 1.0, 1]
|
||||
|
||||
[error]
|
||||
color = [1.0, 0.2, 0.1, 1]
|
||||
@ -105,6 +105,7 @@ end
|
||||
string.lower = utf8.lower
|
||||
string.upper = utf8.upper
|
||||
string.escape = utf8.escape
|
||||
string.escape_xml = utf8.escape_xml
|
||||
|
||||
local meta = getmetatable("")
|
||||
|
||||
|
||||
@ -3,8 +3,8 @@ local gui_util = {
|
||||
}
|
||||
|
||||
--- Parse `pagename?arg1=value1&arg2=value2` queries
|
||||
--- @param query page query string
|
||||
--- @return page_name, args_table
|
||||
--- @param query string page query string
|
||||
--- @return string, string -> page_name, args_table
|
||||
function gui_util.parse_query(query)
|
||||
local args = {}
|
||||
local name
|
||||
@ -23,8 +23,7 @@ function gui_util.parse_query(query)
|
||||
return name, args
|
||||
end
|
||||
|
||||
--- @param query page query string
|
||||
--- @return document_id
|
||||
--- @param query string page query string
|
||||
function gui_util.load_page(query)
|
||||
local name, args = gui_util.parse_query(query)
|
||||
for i = #gui_util.local_dispatchers, 1, -1 do
|
||||
@ -116,4 +115,52 @@ gui_util.Document = Document
|
||||
gui_util.Element = Element
|
||||
gui_util.RadioGroup = RadioGroup
|
||||
|
||||
function gui.show_message(text, actual_callback)
|
||||
local id = "dialog_"..random.uuid()
|
||||
|
||||
local callback = function()
|
||||
gui.root[id]:destruct()
|
||||
if actual_callback then
|
||||
actual_callback()
|
||||
end
|
||||
end
|
||||
gui.root.root:add(string.format([[
|
||||
<container id='%s' color='#00000080' size-func='-1,-1' z-index='10'>
|
||||
<panel color='#507090E0' size='300' padding='16'
|
||||
gravity='center-center' interval='4'>
|
||||
<label>%s</label>
|
||||
<button onclick='DATA.callback()'>@OK</button>
|
||||
</panel>
|
||||
</container>
|
||||
]], id, string.escape_xml(text)), {callback=callback})
|
||||
input.add_callback("key:escape", callback, gui.root[id])
|
||||
end
|
||||
|
||||
function gui.ask(text, on_yes, on_no)
|
||||
on_yes = on_yes or function() end
|
||||
on_no = on_no or function() end
|
||||
|
||||
local id = "dialog_"..random.uuid()
|
||||
|
||||
local yes_callback = function()
|
||||
gui.root[id]:destruct()
|
||||
on_yes()
|
||||
end
|
||||
local no_callback = function()
|
||||
gui.root[id]:destruct()
|
||||
on_no()
|
||||
end
|
||||
gui.root.root:add(string.format([[
|
||||
<container id='%s' color='#00000080' size-func='-1,-1' z-index='10'>
|
||||
<panel color='#507090E0' size='300' padding='16'
|
||||
gravity='center-center' interval='4'>
|
||||
<label margin='4'>%s</label>
|
||||
<button onclick='DATA.on_yes()'>@Yes</button>
|
||||
<button onclick='DATA.on_no()'>@No</button>
|
||||
</panel>
|
||||
</container>
|
||||
]], id, string.escape_xml(text)), {on_yes=yes_callback, on_no=no_callback})
|
||||
input.add_callback("key:escape", no_callback, gui.root[id])
|
||||
end
|
||||
|
||||
return gui_util
|
||||
|
||||
@ -150,6 +150,18 @@ _MENU = _GUI_ROOT.menu
|
||||
menu = _MENU
|
||||
gui.root = _GUI_ROOT
|
||||
|
||||
do
|
||||
local status, err = pcall(function()
|
||||
local default_styles = toml.parse(file.read(
|
||||
"res:devtools/default_syntax_scheme.toml"
|
||||
))
|
||||
gui.set_syntax_styles(default_styles)
|
||||
end)
|
||||
if not status then
|
||||
debug.error("could not to load default syntax scheme: "..err)
|
||||
end
|
||||
end
|
||||
|
||||
--- Console library extension ---
|
||||
console.cheats = {}
|
||||
|
||||
|
||||
@ -7,19 +7,15 @@
|
||||
using namespace devtools;
|
||||
|
||||
static std::unique_ptr<FontStylesScheme> build_styles(
|
||||
const FontStylesScheme& colorScheme,
|
||||
const std::vector<devtools::Token>& tokens
|
||||
) {
|
||||
using devtools::TokenTag;
|
||||
FontStylesScheme styles {
|
||||
{
|
||||
{false, false, false, false, glm::vec4(0.8f, 0.8f, 0.8f, 1)}, // default
|
||||
{true, false, false, false, glm::vec4(0.9, 0.6f, 0.4f, 1)}, // keyword
|
||||
{false, false, false, false, glm::vec4(0.4, 0.8f, 0.5f, 1)}, // string
|
||||
{false, false, false, false, glm::vec4(0.3, 0.3f, 0.3f, 1)}, // comment
|
||||
{true, false, false, false, glm::vec4(1.0f, 0.2f, 0.1f, 1)}, // unexpected
|
||||
},
|
||||
{}
|
||||
};
|
||||
FontStylesScheme styles {colorScheme.palette, {}};
|
||||
if (styles.palette.empty()) {
|
||||
styles.palette.push_back(FontStyle {
|
||||
false, false, false, false, glm::vec4(0.8f, 0.8f, 0.8f, 1)});
|
||||
}
|
||||
size_t offset = 0;
|
||||
for (int i = 0; i < tokens.size(); i++) {
|
||||
const auto& token = tokens.at(i);
|
||||
@ -47,6 +43,9 @@ static std::unique_ptr<FontStylesScheme> build_styles(
|
||||
styleIndex = 0;
|
||||
break;
|
||||
}
|
||||
if (styleIndex >= styles.palette.size()) {
|
||||
styleIndex = 0;
|
||||
}
|
||||
styles.map.insert(
|
||||
styles.map.end(), token.end.pos - token.start.pos, styleIndex
|
||||
);
|
||||
@ -67,7 +66,9 @@ void SyntaxProcessor::addSyntax(
|
||||
}
|
||||
|
||||
std::unique_ptr<FontStylesScheme> SyntaxProcessor::highlight(
|
||||
const std::string& ext, std::wstring_view source
|
||||
const FontStylesScheme& colorScheme,
|
||||
const std::string& ext,
|
||||
std::wstring_view source
|
||||
) const {
|
||||
const auto& found = langsExtensions.find(ext);
|
||||
if (found == langsExtensions.end()) {
|
||||
@ -76,7 +77,7 @@ std::unique_ptr<FontStylesScheme> SyntaxProcessor::highlight(
|
||||
const auto& syntax = *found->second;
|
||||
try {
|
||||
auto tokens = tokenize(syntax, "<string>", source);
|
||||
return build_styles(tokens);
|
||||
return build_styles(colorScheme, tokens);
|
||||
} catch (const parsing_error& err) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@ -18,7 +18,9 @@ namespace devtools {
|
||||
class SyntaxProcessor {
|
||||
public:
|
||||
std::unique_ptr<FontStylesScheme> highlight(
|
||||
const std::string& ext, std::wstring_view source
|
||||
const FontStylesScheme& colorScheme,
|
||||
const std::string& ext,
|
||||
std::wstring_view source
|
||||
) const;
|
||||
|
||||
void addSyntax(std::unique_ptr<Syntax> syntax);
|
||||
|
||||
@ -23,11 +23,12 @@ void UiDocument::rebuildIndices() {
|
||||
map["root"] = root;
|
||||
}
|
||||
|
||||
const UINodesMap& UiDocument::getMap() const {
|
||||
return map;
|
||||
void UiDocument::pushIndices(const std::shared_ptr<gui::UINode>& node) {
|
||||
gui::UINode::getIndices(node, map);
|
||||
map["root"] = root;
|
||||
}
|
||||
|
||||
UINodesMap& UiDocument::getMapWriteable() {
|
||||
const UINodesMap& UiDocument::getMap() const {
|
||||
return map;
|
||||
}
|
||||
|
||||
|
||||
@ -36,10 +36,10 @@ public:
|
||||
);
|
||||
|
||||
void rebuildIndices();
|
||||
void pushIndices(const std::shared_ptr<gui::UINode>& node);
|
||||
|
||||
const std::string& getId() const;
|
||||
const UINodesMap& getMap() const;
|
||||
UINodesMap& getMapWriteable();
|
||||
std::shared_ptr<gui::UINode> getRoot() const;
|
||||
std::shared_ptr<gui::UINode> get(const std::string& id) const;
|
||||
const uidocscript& getScript() const;
|
||||
|
||||
37
src/graphics/commons/FontStyle.cpp
Normal file
37
src/graphics/commons/FontStyle.cpp
Normal 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
src/graphics/commons/FontStyle.hpp
Normal file
39
src/graphics/commons/FontStyle.hpp
Normal 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);
|
||||
};
|
||||
@ -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;
|
||||
|
||||
@ -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");
|
||||
@ -302,7 +310,7 @@ bool GUI::isFocusCaught() const {
|
||||
}
|
||||
|
||||
void GUI::add(std::shared_ptr<UINode> node) {
|
||||
UINode::getIndices(node, rootDocument->getMapWriteable());
|
||||
rootDocument->pushIndices(node);
|
||||
container->add(std::move(node));
|
||||
}
|
||||
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
@ -105,7 +105,7 @@ static int l_container_add(lua::State* L) {
|
||||
auto subnode = guiutil::create(
|
||||
engine->getGUI(), xmlsrc, std::move(env)
|
||||
);
|
||||
UINode::getIndices(subnode, docnode.document->getMapWriteable());
|
||||
docnode.document->pushIndices(subnode);
|
||||
node->add(std::move(subnode));
|
||||
} catch (const std::exception& err) {
|
||||
throw std::runtime_error("container:add(...): " + std::string(err.what()));
|
||||
@ -410,9 +410,7 @@ static const std::string& request_node_id(const DocumentNode& docnode) {
|
||||
reinterpret_cast<std::ptrdiff_t>(docnode.node.get()));
|
||||
}
|
||||
docnode.node->setId(std::move(id));
|
||||
UINode::getIndices(
|
||||
docnode.node, docnode.document->getMapWriteable()
|
||||
);
|
||||
docnode.document->pushIndices(docnode.node);
|
||||
return docnode.node->getId();
|
||||
}
|
||||
|
||||
@ -1089,6 +1087,16 @@ static int l_gui_load_document(lua::State* L) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_set_syntax_styles(lua::State* L) {
|
||||
if (engine->isHeadless()) {
|
||||
return 0;
|
||||
}
|
||||
engine->getGUI().setSyntaxColorScheme(std::make_unique<FontStylesScheme>(
|
||||
FontStylesScheme::parse(lua::tovalue(L, 1))
|
||||
));
|
||||
return 0;
|
||||
}
|
||||
|
||||
const luaL_Reg guilib[] = {
|
||||
{"get_viewport", lua::wrap<l_gui_getviewport>},
|
||||
{"getattr", lua::wrap<l_gui_getattr>},
|
||||
@ -1101,6 +1109,7 @@ const luaL_Reg guilib[] = {
|
||||
{"confirm", lua::wrap<l_gui_confirm>},
|
||||
{"alert", lua::wrap<l_gui_alert>},
|
||||
{"load_document", lua::wrap<l_gui_load_document>},
|
||||
{"set_syntax_styles", lua::wrap<l_set_syntax_styles>},
|
||||
{"__reindex", lua::wrap<l_gui_reindex>},
|
||||
{nullptr, nullptr}
|
||||
};
|
||||
|
||||
@ -88,6 +88,11 @@ static int l_escape(lua::State* L) {
|
||||
return lua::pushstring(L, util::escape(string));
|
||||
}
|
||||
|
||||
static int l_escape_xml(lua::State* L) {
|
||||
auto string = lua::require_wstring(L, 1);
|
||||
return lua::pushwstring(L, util::escape_xml(string));
|
||||
}
|
||||
|
||||
const luaL_Reg utf8lib[] = {
|
||||
{"tobytes", lua::wrap<l_tobytes>},
|
||||
{"tostring", lua::wrap<l_tostring>},
|
||||
@ -98,5 +103,6 @@ const luaL_Reg utf8lib[] = {
|
||||
{"lower", lua::wrap<l_lower>},
|
||||
{"encode", lua::wrap<l_encode>},
|
||||
{"escape", lua::wrap<l_escape>},
|
||||
{"escape_xml", lua::wrap<l_escape_xml>},
|
||||
{nullptr, nullptr}
|
||||
};
|
||||
|
||||
@ -60,6 +60,33 @@ std::string util::escape(std::string_view s, bool escapeUnicode) {
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
std::wstring util::escape_xml(std::wstring_view s) {
|
||||
std::wstringstream ss;
|
||||
for (wchar_t c : s) {
|
||||
switch (c) {
|
||||
case L'&':
|
||||
ss << L"&";
|
||||
break;
|
||||
case L'<':
|
||||
ss << L"<";
|
||||
break;
|
||||
case L'>':
|
||||
ss << L">";
|
||||
break;
|
||||
case '"':
|
||||
ss << L""";
|
||||
break;
|
||||
case '\'':
|
||||
ss << L"'";
|
||||
break;
|
||||
default:
|
||||
ss << c;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
std::string util::quote(const std::string& s) {
|
||||
return escape(s, false);
|
||||
}
|
||||
|
||||
@ -10,6 +10,9 @@ namespace util {
|
||||
/// @brief Function used for string serialization in text formats
|
||||
std::string escape(std::string_view s, bool escapeUnicode=true);
|
||||
|
||||
/// @brief Escape all special XML characters
|
||||
std::wstring escape_xml(std::wstring_view s);
|
||||
|
||||
/// @brief Function used for error messages
|
||||
std::string quote(const std::string& s);
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user