add markdown dialect (WIP) & add strikethrough and underline font styles

This commit is contained in:
MihailRis
2024-12-06 17:35:03 +03:00
parent 605d7e7897
commit 80e809f97f
15 changed files with 296 additions and 58 deletions
+17 -2
View File
@@ -6,6 +6,7 @@
#include "graphics/core/Font.hpp"
#include "assets/Assets.hpp"
#include "util/stringutil.hpp"
#include "../markdown.hpp"
using namespace gui;
@@ -80,11 +81,16 @@ glm::vec2 Label::calcSize() {
);
}
void Label::setText(const std::wstring& text) {
void Label::setText(std::wstring text) {
if (isMarkdown()) {
auto [processedText, styles] = markdown::process(text, true);
text = std::move(processedText);
setStyles(std::move(styles));
}
if (text == this->text && !cache.resetFlag) {
return;
}
this->text = text;
this->text = std::move(text);
cache.update(this->text, multiline, textWrap);
if (cache.font && autoresize) {
@@ -242,6 +248,15 @@ bool Label::isTextWrapping() const {
return textWrap;
}
void Label::setMarkdown(bool flag) {
markdown = flag;
setText(text);
}
bool Label::isMarkdown() const {
return markdown;
}
void Label::setStyles(std::unique_ptr<FontStylesScheme> styles) {
this->styles = std::move(styles);
}
+7 -1
View File
@@ -53,6 +53,9 @@ namespace gui {
/// @brief Auto resize label to fit text
bool autoresize = false;
/// @brief Enable text markdown
bool markdown = false;
std::unique_ptr<FontStylesScheme> styles;
public:
Label(const std::string& text, std::string fontName="normal");
@@ -60,7 +63,7 @@ namespace gui {
virtual ~Label();
virtual void setText(const std::wstring& text);
virtual void setText(std::wstring text);
const std::wstring& getText() const;
virtual void setFontName(std::string name);
@@ -113,6 +116,9 @@ namespace gui {
virtual void setTextWrapping(bool flag);
virtual bool isTextWrapping() const;
virtual void setMarkdown(bool flag);
virtual bool isMarkdown() const;
virtual void setStyles(std::unique_ptr<FontStylesScheme> styles);
};
}
+21 -1
View File
@@ -13,6 +13,7 @@
#include "util/stringutil.hpp"
#include "window/Events.hpp"
#include "window/Window.hpp"
#include "../markdown.hpp"
using namespace gui;
@@ -191,7 +192,18 @@ void TextBox::drawBackground(const DrawContext& pctx, const Assets&) {
void TextBox::refreshLabel() {
label->setColor(textColor * glm::vec4(input.empty() ? 0.5f : 1.0f));
label->setText(input.empty() && !hint.empty() ? hint : getText());
const auto& displayText = input.empty() && !hint.empty() ? hint : getText();
if (markdown) {
auto [processedText, styles] = markdown::process(displayText, !focused);
label->setText(std::move(processedText));
label->setStyles(std::move(styles));
} else {
label->setText(displayText);
if (syntax.empty()) {
label->setStyles(nullptr);
}
}
if (showLineNumbers) {
if (lineNumbersLabel->getLinesNumber() != label->getLinesNumber()) {
@@ -846,3 +858,11 @@ void TextBox::setSyntax(const std::string& lang) {
refreshSyntax();
}
}
void TextBox::setMarkdown(bool flag) {
markdown = flag;
}
bool TextBox::isMarkdown() const {
return markdown;
}
+4
View File
@@ -56,6 +56,7 @@ namespace gui {
bool editable = true;
bool autoresize = false;
bool showLineNumbers = false;
bool markdown = false;
std::string syntax;
@@ -227,5 +228,8 @@ namespace gui {
virtual void setOnDownPressed(const runnable &callback);
virtual void setSyntax(const std::string& lang);
virtual void setMarkdown(bool flag);
virtual bool isMarkdown() const;
};
}