slider值改为在鼠标放置时显示

This commit is contained in:
Nanako 2024-04-02 14:12:08 +08:00
parent 9aa8f357f4
commit 9e9795e822

View File

@ -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<T, float>) {
std::ostringstream ss;
ss << std::fixed << std::setprecision(2) << *value;
return ss.str();
} else if constexpr (std::is_same_v<T, int>) {
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<T>(0);
max_value = static_cast<T>(1);
value = static_cast<T>(0.5);
_internal_value = *value;
_mode = in_mode;
_value_txt = std::to_string(*value);
_value_text = std::make_unique<hi::text_widget>(this, _value_txt, hi::alignment::top_flush());
_value_txt = value_to_string();
_value_text = std::make_unique<hi::text_widget>(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<void(T)> _value_changed;
hi::callback<void(T)> _max_value_changed;
hi::callback<void(T)> _min_value_changed;
std::unique_ptr<hi::text_widget> _value_text;
hi::observer<hi::txt> _value_txt;
bool _mouse_hoverd = false;
};