diff --git a/Arona/src/widget/misc/slider.h b/Arona/src/widget/misc/slider.h index 898bda0..56ff757 100644 --- a/Arona/src/widget/misc/slider.h +++ b/Arona/src/widget/misc/slider.h @@ -18,19 +18,31 @@ public: const float bar_width = 20; // 当水平时,值为bar的宽度 slider_mode _mode; + std::string value_to_string() + { + if constexpr (std::is_same_v) { + std::ostringstream ss; + ss << std::fixed << std::setprecision(2) << *value; + return ss.str(); + } else if constexpr (std::is_same_v) { + return std::to_string(*value); + } else { + return "Unknown type"; + } + } slider(hi::widget const* parent, bool horizontal, slider_mode in_mode) noexcept : hi::widget(parent), _horizontal(horizontal) { - min_value = (T)0; - max_value = (T)1; - value = (T)0.5; + min_value = static_cast(0); + max_value = static_cast(1); + value = static_cast(0.5); _internal_value = *value; _mode = in_mode; - _value_txt = std::to_string(*value); - _value_text = std::make_unique(this, _value_txt, hi::alignment::top_flush()); + _value_txt = value_to_string(); + _value_text = std::make_unique(this, _value_txt, hi::alignment::middle_center()); _value_changed = value.subscribe([this](T in_value) { _internal_value = in_value; - _value_txt = std::to_string(in_value); + _value_txt = value_to_string(); request_redraw(); }); _max_value_changed = max_value.subscribe([this](T in_value) { @@ -145,10 +157,14 @@ public: return true; } break; + case hi::gui_event_type::mouse_enter: + _mouse_hoverd = true; + break; case hi::gui_event_type::mouse_exit: case hi::gui_event_type::mouse_exit_window: case hi::gui_event_type::keyboard_exit: _micro_controls = false; + _mouse_hoverd = false; default:; } @@ -184,7 +200,8 @@ public: } } // Draw value text - _value_text->draw(context); + if (_mouse_hoverd) + _value_text->draw(context); } void set_layout(hi::widget_layout const &context) noexcept override { @@ -204,6 +221,8 @@ private: hi::callback _value_changed; hi::callback _max_value_changed; hi::callback _min_value_changed; + std::unique_ptr _value_text; hi::observer _value_txt; + bool _mouse_hoverd = false; };