From 12bd750d73cc61cde86c25bca5c70f28bbb3fbf6 Mon Sep 17 00:00:00 2001 From: Zack Buhman Date: Thu, 3 Jul 2025 22:07:34 -0500 Subject: [PATCH] widget: new label widget --- src/widget/button_label.hpp | 11 ----------- src/widget/container.hpp | 4 ++-- src/widget/label.cpp | 32 ++++++++++++++++++++++++++++++++ src/widget/label.hpp | 23 +++++++++++++++++++++++ src/widget/left_aligned.hpp | 4 ++-- src/widget/top_aligned.hpp | 4 ++-- src/widget/widget.hpp | 14 ++++++++++++++ 7 files changed, 75 insertions(+), 17 deletions(-) create mode 100644 src/widget/label.cpp create mode 100644 src/widget/label.hpp diff --git a/src/widget/button_label.hpp b/src/widget/button_label.hpp index 907bf71..ddb0f5b 100644 --- a/src/widget/button_label.hpp +++ b/src/widget/button_label.hpp @@ -1,20 +1,9 @@ #pragma once #include "holly/ta_parameter.hpp" -#include "ta_multiwriter.hpp" #include "widget/button.hpp" namespace widget { - - constexpr inline int str_length(const char * s) - { - int l = 0; - while (*s++) { - l += 1; - } - return l; - } - struct button_label : button { const char * const label; const int label_length; diff --git a/src/widget/container.hpp b/src/widget/container.hpp index b35af4c..f37d04a 100644 --- a/src/widget/container.hpp +++ b/src/widget/container.hpp @@ -32,8 +32,8 @@ namespace widget { inline widget * pick(float _x, float _y) { - if (!inside(_x, _y)) - return nullptr; + //if (!inside(_x, _y)) + //return nullptr; for (int i = 0; i < length; i++) { widget * w = children[i]->pick(_x, _y); diff --git a/src/widget/label.cpp b/src/widget/label.cpp new file mode 100644 index 0000000..bb9f397 --- /dev/null +++ b/src/widget/label.cpp @@ -0,0 +1,32 @@ +#include "widget/label.hpp" + +#include "ta_parameter.hpp" +#include "graphics_primitive.hpp" + +namespace widget { + + const static float label_depth = 1.0 / 5.0; + const static int label_color = 0xa7a7a7; + + const static float label_shadow_depth = 1.0 / 6.0; + const static int label_shadow_color = 0x000000; + + void label::draw_label(ta_parameter_writer& writer) const + { + float y_offset = 0; + + float cx = x() + width / 2 - (glyph::hori_advance * value_length) / 2; + float cy = y() + height / 2 - glyph::vert_advance / 2 + y_offset; + + transfer_string(writer, value, cx, cy, label_depth, label_color); + } + + void label::draw(ta_multiwriter& multi) + { + transfer_global_polygon_glyph(multi.pt); + + draw_label(multi.pt); + + widget::draw(multi); + } +} diff --git a/src/widget/label.hpp b/src/widget/label.hpp new file mode 100644 index 0000000..50c31dc --- /dev/null +++ b/src/widget/label.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include "holly/ta_parameter.hpp" +#include "ta_multiwriter.hpp" +#include "widget/widget.hpp" + +namespace widget { + struct label : widget { + const char * const value; + const int value_length; + + inline label(float _width, float _height, const char * value) + : widget(0, 0, _width, _height), value(value), value_length(str_length(value)) + { } + + inline label(float _x, float _y, float _width, float _height, const char * value) + : widget(_x, _y, _width, _height), value(value), value_length(str_length(value)) + { } + + void draw_label(ta_parameter_writer& writer) const; + void draw(ta_multiwriter& multi) override; + }; +} diff --git a/src/widget/left_aligned.hpp b/src/widget/left_aligned.hpp index 4933f91..9cc26e3 100644 --- a/src/widget/left_aligned.hpp +++ b/src/widget/left_aligned.hpp @@ -11,8 +11,8 @@ namespace widget { { float xi = 0; for (int i = 0; i < length; i++) { - children[i]->_x = xi; - children[i]->_y = 0; + children[i]->_x += xi; + children[i]->_y += 0; xi += children[i]->width + gap; if (children[i]->height > height) height = children[i]->height; diff --git a/src/widget/top_aligned.hpp b/src/widget/top_aligned.hpp index 5d4b6c6..d23b0ec 100644 --- a/src/widget/top_aligned.hpp +++ b/src/widget/top_aligned.hpp @@ -11,8 +11,8 @@ namespace widget { { float yi = 0; for (int i = 0; i < length; i++) { - children[i]->_x = 0; - children[i]->_y = yi; + children[i]->_x += 0; + children[i]->_y += yi; yi += children[i]->height + gap; if (children[i]->width > width) width = children[i]->width; diff --git a/src/widget/widget.hpp b/src/widget/widget.hpp index f6fc10c..fc737c7 100644 --- a/src/widget/widget.hpp +++ b/src/widget/widget.hpp @@ -1,9 +1,19 @@ +#pragma once + #include "widget/bounding_box.hpp" #include "ta_multiwriter.hpp" #include "printf/printf.h" namespace widget { + constexpr inline int str_length(const char * s) + { + int l = 0; + while (*s++) { + l += 1; + } + return l; + } enum class click_type { press, @@ -16,6 +26,10 @@ namespace widget { click_type click_state; public: + inline widget(float _width, float _height) + : bounding_box(0, 0, _width, _height), click_state(click_type::release) + { } + inline widget(float _x, float _y, float _width, float _height) : bounding_box(_x, _y, _width, _height), click_state(click_type::release) { }