add syntax highlighting (WIP)
This commit is contained in:
@@ -194,9 +194,9 @@ void SlotView::draw(const DrawContext* pctx, Assets* assets) {
|
||||
int y = pos.y+slotSize-16;
|
||||
|
||||
batch->setColor({0, 0, 0, 1.0f});
|
||||
font->draw(*batch, text, x+1, y+1, nullptr);
|
||||
font->draw(*batch, text, x+1, y+1, nullptr, 0);
|
||||
batch->setColor(glm::vec4(1.0f));
|
||||
font->draw(*batch, text, x, y, nullptr);
|
||||
font->draw(*batch, text, x, y, nullptr, 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -203,10 +203,10 @@ void Label::draw(const DrawContext* pctx, Assets* assets) {
|
||||
if (i < cache.lines.size()-1) {
|
||||
view = std::wstring_view(text.c_str()+offset, cache.lines.at(i+1).offset-offset);
|
||||
}
|
||||
font->draw(*batch, view, pos.x, pos.y + i * totalLineHeight, styles.get());
|
||||
font->draw(*batch, view, pos.x, pos.y + i * totalLineHeight, styles.get(), offset);
|
||||
}
|
||||
} else {
|
||||
font->draw(*batch, text, pos.x, pos.y, styles.get());
|
||||
font->draw(*batch, text, pos.x, pos.y, styles.get(), 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -52,7 +52,8 @@ void Plotter::draw(const DrawContext* pctx, Assets* assets) {
|
||||
string,
|
||||
pos.x + dmwidth + 2,
|
||||
pos.y + dmheight - y - labelsInterval,
|
||||
nullptr
|
||||
nullptr,
|
||||
0
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <algorithm>
|
||||
|
||||
#include "Label.hpp"
|
||||
#include "devtools/syntax_highlighting.hpp"
|
||||
#include "graphics/core/DrawContext.hpp"
|
||||
#include "graphics/core/Batch2D.hpp"
|
||||
#include "graphics/core/Font.hpp"
|
||||
@@ -65,11 +66,10 @@ void TextBox::draw(const DrawContext* pctx, Assets* assets) {
|
||||
lcoord.y -= 2;
|
||||
auto batch = pctx->getBatch2D();
|
||||
batch->texture(nullptr);
|
||||
batch->setColor(glm::vec4(1.0f));
|
||||
if (editable && int((Window::time() - caretLastMove) * 2) % 2 == 0) {
|
||||
uint line = label->getLineByTextIndex(caret);
|
||||
uint lcaret = caret - label->getTextLineOffset(line);
|
||||
batch->setColor(glm::vec4(1.0f));
|
||||
|
||||
int width = font->calcWidth(input, lcaret);
|
||||
batch->rect(lcoord.x + width, lcoord.y+label->getLineYOffset(line), 2, lineHeight);
|
||||
}
|
||||
@@ -529,10 +529,21 @@ void TextBox::stepDefaultUp(bool shiftPressed, bool breakSelection) {
|
||||
}
|
||||
}
|
||||
|
||||
void TextBox::refreshSyntax() {
|
||||
if (!syntax.empty()) {
|
||||
if (auto styles = devtools::syntax_highlight(
|
||||
syntax, util::wstr2str_utf8(input)
|
||||
)) {
|
||||
label->setStyles(std::move(styles));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void TextBox::onInput() {
|
||||
if (subconsumer) {
|
||||
subconsumer(input);
|
||||
}
|
||||
refreshSyntax();
|
||||
}
|
||||
|
||||
void TextBox::performEditingKeyboardEvents(keycode key) {
|
||||
@@ -710,6 +721,7 @@ const std::wstring& TextBox::getText() const {
|
||||
void TextBox::setText(const std::wstring& value) {
|
||||
this->input = value;
|
||||
input.erase(std::remove(input.begin(), input.end(), '\r'), input.end());
|
||||
refreshSyntax();
|
||||
}
|
||||
|
||||
const std::wstring& TextBox::getPlaceholder() const {
|
||||
@@ -789,3 +801,12 @@ void TextBox::setShowLineNumbers(bool flag) {
|
||||
bool TextBox::isShowLineNumbers() const {
|
||||
return showLineNumbers;
|
||||
}
|
||||
|
||||
void TextBox::setSyntax(const std::string& lang) {
|
||||
syntax = lang;
|
||||
if (syntax.empty()) {
|
||||
label->setStyles(nullptr);
|
||||
} else {
|
||||
refreshSyntax();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,6 +57,8 @@ namespace gui {
|
||||
bool autoresize = false;
|
||||
bool showLineNumbers = false;
|
||||
|
||||
std::string syntax;
|
||||
|
||||
void stepLeft(bool shiftPressed, bool breakSelection);
|
||||
void stepRight(bool shiftPressed, bool breakSelection);
|
||||
void stepDefaultDown(bool shiftPressed, bool breakSelection);
|
||||
@@ -84,6 +86,8 @@ namespace gui {
|
||||
void refreshLabel();
|
||||
|
||||
void onInput();
|
||||
|
||||
void refreshSyntax();
|
||||
public:
|
||||
TextBox(
|
||||
std::wstring placeholder,
|
||||
@@ -219,5 +223,7 @@ namespace gui {
|
||||
virtual std::shared_ptr<UINode> getAt(glm::vec2 pos, std::shared_ptr<UINode> self) override;
|
||||
virtual void setOnUpPressed(const runnable &callback);
|
||||
virtual void setOnDownPressed(const runnable &callback);
|
||||
|
||||
virtual void setSyntax(const std::string& lang);
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user