Added font size modifier to edit GUI
This commit is contained in:
parent
4857b40af3
commit
bc8bd93e3b
8 changed files with 80 additions and 56 deletions
|
|
@ -21,7 +21,7 @@ DrawTextBox::DrawTextBox():
|
|||
textBox->set_initial_text_style_modifier(colorMod);
|
||||
|
||||
auto textSizeMod = std::make_shared<SizeTextStyleModifier>();
|
||||
textSizeMod->size = 16.0f;
|
||||
textSizeMod->size = 18.0f;
|
||||
textBox->set_initial_text_style_modifier(textSizeMod);
|
||||
|
||||
auto fontFamilyMod = std::make_shared<FontFamiliesTextStyleModifier>();
|
||||
|
|
|
|||
|
|
@ -31,21 +31,13 @@ bool TextBoxEditTool::edit_gui(const std::shared_ptr<DrawComponent>& comp) {
|
|||
italicMod->set_slant(isItalic ? SkFontStyle::Slant::kUpright_Slant : SkFontStyle::Slant::kItalic_Slant);
|
||||
a->textBox->set_text_style_modifier_between(a->cursor->selectionBeginPos, a->cursor->selectionEndPos, italicMod);
|
||||
}
|
||||
if(t.gui.text_button_wide("Red button", "Red")) {
|
||||
auto redMod = std::make_shared<ColorTextStyleModifier>();
|
||||
redMod->color = {1.0f, 0.0f, 0.0f, 1.0f};
|
||||
a->textBox->set_text_style_modifier_between(a->cursor->selectionBeginPos, a->cursor->selectionEndPos, redMod);
|
||||
}
|
||||
if(t.gui.text_button_wide("Green button", "Green")) {
|
||||
auto redMod = std::make_shared<ColorTextStyleModifier>();
|
||||
redMod->color = {0.0f, 1.0f, 0.0f, 1.0f};
|
||||
a->textBox->set_text_style_modifier_between(a->cursor->selectionBeginPos, a->cursor->selectionEndPos, redMod);
|
||||
}
|
||||
if(t.gui.text_button_wide("Blue button", "Blue")) {
|
||||
auto redMod = std::make_shared<ColorTextStyleModifier>();
|
||||
redMod->color = {0.0f, 0.0f, 1.0f, 1.0f};
|
||||
a->textBox->set_text_style_modifier_between(a->cursor->selectionBeginPos, a->cursor->selectionEndPos, redMod);
|
||||
newFontSize = std::static_pointer_cast<SizeTextStyleModifier>(modsAtStartOfSelection[TextStyleModifier::ModifierType::SIZE])->size;
|
||||
if(t.gui.slider_scalar_field<uint32_t>("Font Size Slider", "Font Size", &newFontSize, 3, 100)) {
|
||||
auto sizeMod = std::make_shared<SizeTextStyleModifier>();
|
||||
sizeMod->size = newFontSize;
|
||||
a->textBox->set_text_style_modifier_between(a->cursor->selectionBeginPos, a->cursor->selectionEndPos, sizeMod);
|
||||
}
|
||||
|
||||
//t.gui.left_to_right_line_layout([&]() {
|
||||
// CLAY({.layout = {.sizing = {.width = CLAY_SIZING_FIXED(40), .height = CLAY_SIZING_FIXED(40)}}}) {
|
||||
// if(t.gui.color_button("Text Color", &a->d.textColor, &a->d.textColor == t.colorRight))
|
||||
|
|
@ -56,7 +48,7 @@ bool TextBoxEditTool::edit_gui(const std::shared_ptr<DrawComponent>& comp) {
|
|||
t.gui.pop_id();
|
||||
|
||||
if(a->textBox->inputChangedTextBox)
|
||||
modsAtStartOfSelection = a->textBox->get_mods_used_at_pos(std::min(a->cursor->selectionBeginPos, a->cursor->selectionEndPos));
|
||||
set_mods_at_selection(a);
|
||||
|
||||
// NOTE: There should be a system that periodically sends updates even if no changes are made, since unreliable channels can drop update data
|
||||
bool oldInputChangedTextBox = a->textBox->inputChangedTextBox;
|
||||
|
|
@ -64,6 +56,15 @@ bool TextBoxEditTool::edit_gui(const std::shared_ptr<DrawComponent>& comp) {
|
|||
return oldInputChangedTextBox;
|
||||
}
|
||||
|
||||
void TextBoxEditTool::set_mods_at_selection(const std::shared_ptr<DrawTextBox>& a) {
|
||||
RichTextBox::TextPosition start = std::min(a->cursor->selectionBeginPos, a->cursor->selectionEndPos);
|
||||
RichTextBox::TextPosition end = std::max(a->cursor->selectionBeginPos, a->cursor->selectionEndPos);
|
||||
if(start == end)
|
||||
modsAtStartOfSelection = a->textBox->get_mods_used_at_pos(a->textBox->move(RichTextBox::Movement::LEFT, start));
|
||||
else
|
||||
modsAtStartOfSelection = a->textBox->get_mods_used_at_pos(start);
|
||||
}
|
||||
|
||||
void TextBoxEditTool::commit_edit_updates(const std::shared_ptr<DrawComponent>& comp, std::any& prevData) {
|
||||
std::shared_ptr<DrawTextBox> a = std::static_pointer_cast<DrawTextBox>(comp);
|
||||
|
||||
|
|
@ -112,7 +113,7 @@ void TextBoxEditTool::edit_start(EditTool& editTool, const std::shared_ptr<DrawC
|
|||
a->commit_update(drawP);
|
||||
a->client_send_update(drawP, false);
|
||||
|
||||
modsAtStartOfSelection = a->textBox->get_mods_used_at_pos(std::min(a->cursor->selectionBeginPos, a->cursor->selectionEndPos));
|
||||
set_mods_at_selection(a);
|
||||
|
||||
editTool.add_point_handle({&a->d.p1, nullptr, &a->d.p2});
|
||||
editTool.add_point_handle({&a->d.p2, &a->d.p1, nullptr});
|
||||
|
|
|
|||
|
|
@ -19,7 +19,9 @@ class TextBoxEditTool : public DrawingProgramEditToolBase {
|
|||
DrawTextBox::Data textboxData;
|
||||
RichTextBox::RichTextData richText;
|
||||
};
|
||||
uint32_t newFontSize;
|
||||
|
||||
void set_mods_at_selection(const std::shared_ptr<DrawTextBox>& a);
|
||||
TextStyleModifier::ModifierMap modsAtStartOfSelection;
|
||||
|
||||
TextBoxEditToolAllData get_all_data(const std::shared_ptr<DrawTextBox>& a);
|
||||
|
|
|
|||
|
|
@ -5,8 +5,9 @@ namespace GUIStuff {
|
|||
|
||||
template <typename T> class NumberSlider : public Element {
|
||||
public:
|
||||
void update(UpdateInputData& io, T* dataPtr, T minNew, T maxNew, const std::function<void()>& elemUpdate) {
|
||||
data = dataPtr;
|
||||
bool update(UpdateInputData& io, T* data, T minNew, T maxNew, const std::function<void()>& elemUpdate) {
|
||||
bool isUpdating = false;
|
||||
|
||||
min = minNew;
|
||||
max = maxNew;
|
||||
|
||||
|
|
@ -19,17 +20,22 @@ template <typename T> class NumberSlider : public Element {
|
|||
selection.update(Clay_Hovered(), io.mouse.leftClick, io.mouse.leftHeld);
|
||||
if(selection.held && data) {
|
||||
float fracPosOnSlider = (io.mouse.pos.x() - bb.min.x()) / bb.width();
|
||||
*data = std::clamp<T>(std::lerp<double>(min, max, fracPosOnSlider), min, max);
|
||||
*data = static_cast<T>(std::clamp<double>(std::lerp<double>(min, max, fracPosOnSlider), min, max)); // Clamp as double then cast so that unsigned types dont wrap on clamp
|
||||
isUpdating = true;
|
||||
}
|
||||
if(elemUpdate)
|
||||
elemUpdate();
|
||||
}
|
||||
|
||||
if(data)
|
||||
cachedData = *data;
|
||||
else
|
||||
cachedData = 0.0;
|
||||
|
||||
return isUpdating;
|
||||
}
|
||||
|
||||
virtual void clay_draw(SkCanvas* canvas, UpdateInputData& io, Clay_RenderCommand* command) override {
|
||||
if(!data)
|
||||
return;
|
||||
|
||||
bb = get_bb(command);
|
||||
|
||||
canvas->save();
|
||||
|
|
@ -47,7 +53,7 @@ template <typename T> class NumberSlider : public Element {
|
|||
|
||||
const float yChange = bb.height() * 0.5f - holderRadius * 0.5f;
|
||||
|
||||
float holderPos = lerp_time<float>(*data, max, min) * bb.width();
|
||||
float holderPos = lerp_time<float>(cachedData, max, min) * bb.width();
|
||||
|
||||
SkRect barFull = SkRect::MakeXYWH(0.0f, yChange, holderPos, holderRadius);
|
||||
SkRect barEmpty = SkRect::MakeXYWH(holderPos, yChange, bb.width() - holderPos, holderRadius);
|
||||
|
|
@ -80,9 +86,9 @@ template <typename T> class NumberSlider : public Element {
|
|||
SCollision::AABB<float> bb;
|
||||
SelectionHelper selection;
|
||||
|
||||
T* data = nullptr;
|
||||
T min = 0.0;
|
||||
T max = 1.0;
|
||||
T cachedData = 0.0;
|
||||
|
||||
float hoverAnimation = 0.0;
|
||||
float holdAnimation = 0.0;
|
||||
|
|
|
|||
|
|
@ -8,7 +8,9 @@ namespace GUIStuff {
|
|||
|
||||
template <typename T> class TextBox : public Element {
|
||||
public:
|
||||
void update(UpdateInputData& io, T* newData, const std::function<std::optional<T>(const std::string&)>& newFromStr, const std::function<std::string(const T&)> newToStr, bool newSingleLine, bool updateEveryEdit, const std::function<void(SelectionHelper&)>& elemUpdate) {
|
||||
bool update(UpdateInputData& io, T* newData, const std::function<std::optional<T>(const std::string&)>& newFromStr, const std::function<std::string(const T&)> newToStr, bool newSingleLine, bool updateEveryEdit, const std::function<void(SelectionHelper&)>& elemUpdate) {
|
||||
bool isUpdating = false;
|
||||
|
||||
if(!textbox || !cur)
|
||||
force_update_textbox(io, false);
|
||||
|
||||
|
|
@ -30,17 +32,23 @@ template <typename T> class TextBox : public Element {
|
|||
if(data && selection.selected) {
|
||||
io.richTextBoxEdit(textbox, cur);
|
||||
textbox->process_mouse_left_button(*cur, io.mouse.pos - bb.min, io.mouse.leftClick, io.mouse.leftHeld, io.key.leftShift);
|
||||
update_on_edit(updateEveryEdit);
|
||||
if(updateEveryEdit || io.key.enter) {
|
||||
update_on_edit();
|
||||
isUpdating = true;
|
||||
}
|
||||
}
|
||||
if(data && selection.justUnselected) {
|
||||
std::optional<T> dataToAssign = fromStr(textbox->get_string());
|
||||
if(dataToAssign)
|
||||
*data = dataToAssign.value();
|
||||
force_update_textbox(io, true);
|
||||
isUpdating = true;
|
||||
}
|
||||
if(elemUpdate)
|
||||
elemUpdate(selection);
|
||||
}
|
||||
|
||||
return isUpdating;
|
||||
}
|
||||
|
||||
virtual void clay_draw(SkCanvas* canvas, UpdateInputData& io, Clay_RenderCommand* command) override {
|
||||
|
|
@ -98,13 +106,11 @@ template <typename T> class TextBox : public Element {
|
|||
}
|
||||
}
|
||||
|
||||
void update_on_edit(bool updateEveryEdit) {
|
||||
if(updateEveryEdit) {
|
||||
std::optional<T> dataToAssign = fromStr(textbox->get_string());
|
||||
if(dataToAssign) {
|
||||
*data = dataToAssign.value();
|
||||
oldData = *data;
|
||||
}
|
||||
void update_on_edit() {
|
||||
std::optional<T> dataToAssign = fromStr(textbox->get_string());
|
||||
if(dataToAssign) {
|
||||
*data = dataToAssign.value();
|
||||
oldData = *data;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -496,9 +496,10 @@ bool GUIManager::text_button_left_transparent(const std::string& id, const std::
|
|||
return toRet;
|
||||
}
|
||||
|
||||
void GUIManager::input_scalar(const std::string& id, uint8_t* val, uint8_t min, uint8_t max, int decimalPrecision, const std::function<void(SelectionHelper&)>& elemUpdate) {
|
||||
bool GUIManager::input_scalar(const std::string& id, uint8_t* val, uint8_t min, uint8_t max, int decimalPrecision, const std::function<void(SelectionHelper&)>& elemUpdate) {
|
||||
bool isUpdating = false;
|
||||
push_id(id);
|
||||
insert_element<TextBox<uint8_t>>()->update(*io, val,
|
||||
isUpdating = insert_element<TextBox<uint8_t>>()->update(*io, val,
|
||||
[&](const std::string& a) {
|
||||
if(a.empty())
|
||||
return min;
|
||||
|
|
@ -514,6 +515,7 @@ void GUIManager::input_scalar(const std::string& id, uint8_t* val, uint8_t min,
|
|||
return std::to_string(static_cast<uint32_t>(a));
|
||||
}, true, false, elemUpdate);
|
||||
pop_id();
|
||||
return isUpdating;
|
||||
}
|
||||
|
||||
void GUIManager::input_path(const std::string& id, std::filesystem::path* val, std::filesystem::file_type fileTypeRestriction, const std::function<void(SelectionHelper&)>& elemUpdate) {
|
||||
|
|
|
|||
|
|
@ -127,14 +127,16 @@ class GUIManager {
|
|||
return toRet;
|
||||
}
|
||||
|
||||
template <typename T> void input_generic(const std::string& id, T* val, const std::function<std::optional<T>(const std::string&)>& fromStr, const std::function<std::string(const T&)>& toStr, bool singleLine, bool updateEveryEdit, const std::function<void(SelectionHelper&)>& elemUpdate = nullptr) {
|
||||
template <typename T> bool input_generic(const std::string& id, T* val, const std::function<std::optional<T>(const std::string&)>& fromStr, const std::function<std::string(const T&)>& toStr, bool singleLine, bool updateEveryEdit, const std::function<void(SelectionHelper&)>& elemUpdate = nullptr) {
|
||||
bool isUpdating = false;
|
||||
push_id(id);
|
||||
insert_element<TextBox<T>>()->update(*io, val, fromStr, toStr, singleLine, updateEveryEdit, elemUpdate);
|
||||
isUpdating = insert_element<TextBox<T>>()->update(*io, val, fromStr, toStr, singleLine, updateEveryEdit, elemUpdate);
|
||||
pop_id();
|
||||
return isUpdating;
|
||||
}
|
||||
|
||||
template <typename T> void input_scalar(const std::string& id, T* val, T min, T max, int decimalPrecision = 0, const std::function<void(SelectionHelper&)>& elemUpdate = nullptr) {
|
||||
input_generic<T>(id, val,
|
||||
template <typename T> bool input_scalar(const std::string& id, T* val, T min, T max, int decimalPrecision = 0, const std::function<void(SelectionHelper&)>& elemUpdate = nullptr) {
|
||||
return input_generic<T>(id, val,
|
||||
[&](const std::string& a) {
|
||||
if(a.empty())
|
||||
return min;
|
||||
|
|
@ -158,7 +160,7 @@ class GUIManager {
|
|||
}, true, false, elemUpdate);
|
||||
}
|
||||
|
||||
void input_scalar(const std::string& id, uint8_t* val, uint8_t min, uint8_t max, int decimalPrecision, const std::function<void(SelectionHelper&)>& elemUpdate);
|
||||
bool input_scalar(const std::string& id, uint8_t* val, uint8_t min, uint8_t max, int decimalPrecision, const std::function<void(SelectionHelper&)>& elemUpdate);
|
||||
|
||||
template <typename TContainer, typename T> void input_scalar_fields(const std::string& id, const std::string& name, TContainer* val, size_t elemCount, T min, T max, int decimalPrecision = 0, const std::function<void(SelectionHelper&)>& elemUpdate = nullptr) {
|
||||
push_id(id);
|
||||
|
|
@ -213,27 +215,32 @@ class GUIManager {
|
|||
pop_id();
|
||||
}
|
||||
|
||||
template <typename T> void slider_scalar(const std::string& id, T* val, T min, T max, const std::function<void()>& elemUpdate = nullptr) {
|
||||
template <typename T> bool slider_scalar(const std::string& id, T* val, T min, T max, const std::function<void()>& elemUpdate = nullptr) {
|
||||
push_id(id);
|
||||
insert_element<NumberSlider<T>>()->update(*io, val, min, max, elemUpdate);
|
||||
bool isUpdating = insert_element<NumberSlider<T>>()->update(*io, val, min, max, elemUpdate);
|
||||
pop_id();
|
||||
return isUpdating;
|
||||
}
|
||||
|
||||
template <typename T> void input_scalar_field(const std::string& id, const std::string& name, T* val, T min, T max, int decimalPrecision = 0, const std::function<void(SelectionHelper&)>& elemUpdate = nullptr) {
|
||||
template <typename T> bool input_scalar_field(const std::string& id, const std::string& name, T* val, T min, T max, int decimalPrecision = 0, const std::function<void(SelectionHelper&)>& elemUpdate = nullptr) {
|
||||
bool isUpdating = false;
|
||||
left_to_right_line_layout([&]() {
|
||||
text_label(name);
|
||||
input_scalar(id, val, min, max, decimalPrecision, elemUpdate);
|
||||
isUpdating = input_scalar(id, val, min, max, decimalPrecision, elemUpdate);
|
||||
});
|
||||
return isUpdating;
|
||||
}
|
||||
|
||||
template <typename T> void slider_scalar_field(const std::string& id, const std::string& name, T* val, T min, T max, int decimalPrecision = 0, const std::function<void()>& elemUpdate = nullptr) {
|
||||
template <typename T> bool slider_scalar_field(const std::string& id, const std::string& name, T* val, T min, T max, int decimalPrecision = 0, const std::function<void()>& elemUpdate = nullptr) {
|
||||
bool isUpdating = false;
|
||||
push_id(id);
|
||||
left_to_right_line_layout([&]() {
|
||||
text_label(name);
|
||||
input_scalar("0", val, min, max, decimalPrecision, nullptr);
|
||||
isUpdating |= input_scalar("0", val, min, max, decimalPrecision, nullptr);
|
||||
});
|
||||
slider_scalar("1", val, min, max, elemUpdate);
|
||||
isUpdating |= slider_scalar("1", val, min, max, elemUpdate);
|
||||
pop_id();
|
||||
return isUpdating;
|
||||
}
|
||||
|
||||
template <typename T> bool big_color_button(const std::string& id, T* val, bool isSelected = false, const std::function<void()>& elemUpdate = nullptr) {
|
||||
|
|
|
|||
|
|
@ -675,9 +675,9 @@ void RichTextBox::rebuild() {
|
|||
heightOffset += pData.p->getHeight();
|
||||
}
|
||||
|
||||
std::cout << "size: " << tStyleMods.size() << std::endl;
|
||||
for(auto& s : tStyleMods)
|
||||
std::cout << s.pos.fParagraphIndex << " " << s.pos.fTextByteIndex << std::endl;
|
||||
//std::cout << "size: " << tStyleMods.size() << std::endl;
|
||||
//for(auto& s : tStyleMods)
|
||||
// std::cout << s.pos.fParagraphIndex << " " << s.pos.fTextByteIndex << std::endl;
|
||||
|
||||
needsRebuild = false;
|
||||
}
|
||||
|
|
@ -816,12 +816,12 @@ SkRect RichTextBox::get_cursor_rect(TextPosition pos) {
|
|||
height = pData.p->getHeight();
|
||||
}
|
||||
else {
|
||||
if(pos.fTextByteIndex == pData.text.size()) {
|
||||
if(pos.fTextByteIndex == 0) {
|
||||
skia::textlayout::Paragraph::GlyphClusterInfo glyphInfo;
|
||||
bool exists = pData.p->getGlyphClusterAt(prev_grapheme(pData.text, pos.fTextByteIndex), &glyphInfo);
|
||||
bool exists = pData.p->getGlyphClusterAt(pos.fTextByteIndex, &glyphInfo);
|
||||
if(exists) {
|
||||
bool isLtr = glyphInfo.fGlyphClusterPosition == skia::textlayout::TextDirection::kLtr;
|
||||
topPoint = (isLtr ? glyphInfo.fBounds.TR() : glyphInfo.fBounds.TL()) + SkPoint{0.0f, pData.heightOffset};
|
||||
topPoint = (isLtr ? glyphInfo.fBounds.TL() : glyphInfo.fBounds.TR()) + SkPoint{0.0f, pData.heightOffset};
|
||||
height = glyphInfo.fBounds.height();
|
||||
}
|
||||
else {
|
||||
|
|
@ -831,10 +831,10 @@ SkRect RichTextBox::get_cursor_rect(TextPosition pos) {
|
|||
}
|
||||
else {
|
||||
skia::textlayout::Paragraph::GlyphClusterInfo glyphInfo;
|
||||
bool exists = pData.p->getGlyphClusterAt(pos.fTextByteIndex, &glyphInfo);
|
||||
bool exists = pData.p->getGlyphClusterAt(prev_grapheme(pData.text, pos.fTextByteIndex), &glyphInfo);
|
||||
if(exists) {
|
||||
bool isLtr = glyphInfo.fGlyphClusterPosition == skia::textlayout::TextDirection::kLtr;
|
||||
topPoint = (isLtr ? glyphInfo.fBounds.TL() : glyphInfo.fBounds.TR()) + SkPoint{0.0f, pData.heightOffset};
|
||||
topPoint = (isLtr ? glyphInfo.fBounds.TR() : glyphInfo.fBounds.TL()) + SkPoint{0.0f, pData.heightOffset};
|
||||
height = glyphInfo.fBounds.height();
|
||||
}
|
||||
else {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue