Skip to content

Commit

Permalink
fix TextLabel issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul-Dempsey committed Sep 15, 2023
1 parent ec3beb5 commit d9a0e69
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/colors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ extern const NVGcolor no_light;
extern const NVGcolor preset_name_color;

#define IS_SAME_COLOR(p,q) (((p).r == (q).r) && ((p).g == (q).g) && ((p).b == (q).b) && ((p).a == (q).a))
inline NVGcolor Overlay(NVGcolor color) { return nvgTransRGBAf(color, 0.2f); }
inline NVGcolor Overlay(NVGcolor color, float trans = 0.2f) { return nvgTransRGBAf(color, trans); }
inline NVGcolor Gray(float L) {
NVGcolor color;
color.r = color.b = color.g = L;
Expand Down
2 changes: 1 addition & 1 deletion src/preset_widget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct PresetWidget : TipWidget
addChild(symbol);
text_label = createStaticTextLabel(Vec(2.5f, 1.5f), box.size.x - 4.f, "", TextAlignment::Left, 12.f, false);
addChild(text_label);
text_code = createStaticTextLabel(Vec(box.size.x - 20, box.size.y - 14.f), 16.f, "", TextAlignment::Right, 9.f, false, GetStockColor(StockColor::pachde_blue_light));
text_code = createStaticTextLabel(Vec(box.size.x - 20, box.size.y - 12.f), 16.f, "", TextAlignment::Right, 9.f, false, GetStockColor(StockColor::pachde_blue_light));
addChild(text_code);
}

Expand Down
30 changes: 15 additions & 15 deletions src/text.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ struct BasicTextLabel: Widget
_bold(true)
{
}
std::string getText() { return _text; }
void setPos(Vec pos) { box.pos = pos; }
void setSize(Vec size) { box.size = size; }
void text(std::string text) { _text = text; }
void text_height(float height) { _text_height = height; box.size.y = height; }
void style(float height, bool bold = true, TextAlignment alignment = TextAlignment::Left)
Expand All @@ -62,7 +64,6 @@ struct BasicTextLabel: Widget
_align = alignment;
}
void color(const NVGcolor &new_color) { _color = new_color; }

void render(const DrawArgs& args)
{
if (_text.empty()) return;
Expand Down Expand Up @@ -103,6 +104,8 @@ struct DynamicTextLabel : BasicTextLabel
bool _lazy = false;
bool _dirt = false;

void bright(bool lit = true) { _bright = lit; }

std::function<std::string ()> _getText;
void setFetch(std::function<std::string ()> get)
{
Expand All @@ -111,18 +114,15 @@ struct DynamicTextLabel : BasicTextLabel
void setLazy() {
_dirt = _lazy = true;
}

void bright(bool lit = true) { _bright = lit; }

void modified(bool changed = true)
{
_dirt = changed;
}
void setText(std::string text)
{
BasicTextLabel::text(text);
_dirt = true;
}

void refresh()
{
if (_getText) {
Expand All @@ -132,15 +132,13 @@ struct DynamicTextLabel : BasicTextLabel
}
}
}

void drawLayer(const DrawArgs& args, int layer) override
{
Widget::drawLayer(args, layer);
if (1 != layer || !_bright) return;
refresh();
BasicTextLabel::render(args);
}

void draw(const DrawArgs& args) override
{
Widget::draw(args);
Expand Down Expand Up @@ -181,12 +179,10 @@ struct StaticTextLabel: Widget
FramebufferWidget* _fb = nullptr;
BasicTextLabel* _label = nullptr;

StaticTextLabel() {
StaticTextLabel()
{
_label = new BasicTextLabel();
box.size.y = _label->box.size.y;
_fb = new widget::FramebufferWidget;
_fb->box.pos = Vec();
_fb->box.size = Vec(150.f, _label->box.size.y);
_fb->addChild(_label);
addChild(_fb);
dirty();
Expand All @@ -195,9 +191,10 @@ struct StaticTextLabel: Widget
return _label ? _label->_text : "";
}
void setPos(Vec pos) {
_fb->box.pos = pos;
box.pos = pos;
}
void setSize(Vec size) {
box.size = size;
_fb->box.size = size;
_label->box.size = size;
}
Expand Down Expand Up @@ -225,6 +222,9 @@ struct StaticTextLabel: Widget
void draw(const DrawArgs& args) override
{
Widget::draw(args);
#if defined VISIBLE_STATICTEXTLABEL_BOUNDS
FillRect(args.vg, _fb->box.pos.x, _fb->box.pos.y, _fb->box.size.x, _fb->box.size.y, Overlay(GetStockColor(StockColor::Yellow), .20f));
#endif
}
};

Expand Down Expand Up @@ -252,7 +252,7 @@ template<typename TWidget = StaticTextLabel>
TWidget* createStaticTextLabel(
math::Vec pos,
float width,
const char * text,
std::string text,
TextAlignment alignment = TextAlignment::Center,
float text_height = 12.f,
bool bold = true,
Expand All @@ -262,9 +262,9 @@ TWidget* createStaticTextLabel(
TWidget* w = createWidget<TWidget>(pos);
w->setSize(Vec(width, text_height));
if (alignment == TextAlignment::Center) {
w->setPos(Vec(w->box.pos.x -= width *.5f, w->box.pos.y));
w->setPos(Vec(w->box.pos.x - width*.5f, w->box.pos.y));
}
w->text(std::string(text));
w->text(text);
w->style(text_height, bold, alignment);
w->color(color);
return w;
Expand Down

0 comments on commit d9a0e69

Please sign in to comment.