feat(ui): add int slider support and fix update_text_scale calls

This commit is contained in:
2026-07-15 11:47:45 +08:00
parent e1818cccfb
commit 36b537365b
2 changed files with 51 additions and 14 deletions

View File

@@ -5,12 +5,18 @@
#include "Cubed/ui/widget.hpp" #include "Cubed/ui/widget.hpp"
namespace Cubed { namespace Cubed {
enum class ValueType {
FLOAT,
INT,
};
class Slider : public Widget { class Slider : public Widget {
public: public:
Slider(Widget* parent); Slider(Widget* parent);
Slider& set_slider(float* value, const float& min, const float& max); Slider& set_slider(float* value, const float& min, const float& max);
Slider& set_slider(int* value, const int& min, const int& max);
float width() const override; float width() const override;
float height() const override; float height() const override;
@@ -33,16 +39,19 @@ private:
std::unique_ptr<Image> m_track; std::unique_ptr<Image> m_track;
std::unique_ptr<Image> m_thumb; std::unique_ptr<Image> m_thumb;
std::unique_ptr<Label> m_label; std::unique_ptr<Label> m_label;
ValueType m_type = ValueType::FLOAT;
float m_scale = DEFAULT_SCALE; float m_scale = DEFAULT_SCALE;
float m_width = NORMAL_SLIDER_WIDTH; float m_width = NORMAL_SLIDER_WIDTH;
float m_height = NORMAL_SLIDER_HEIGHT; float m_height = NORMAL_SLIDER_HEIGHT;
float m_xpos = 0.0f; float m_xpos = 0.0f;
bool m_dragging = false; bool m_dragging = false;
bool m_inside = false; bool m_inside = false;
float* m_value = nullptr; float* m_float_value = nullptr;
int* m_int_value = nullptr;
float m_min = 0; float m_min = 0;
float m_max = 0; float m_max = 0;
std::string m_text; std::string m_text;
void init_track(); void init_track();
void init_thumb(); void init_thumb();

View File

@@ -12,13 +12,24 @@ Slider::Slider(Widget* parent) : Widget(parent) {
m_label = std::make_unique<Label>(this); m_label = std::make_unique<Label>(this);
m_label->set_anchor(Anchor::CENTER); m_label->set_anchor(Anchor::CENTER);
void update_text_scale(); update_text_scale();
} }
Slider& Slider::set_slider(float* value, const float& min, const float& max) { Slider& Slider::set_slider(float* value, const float& min, const float& max) {
m_value = value; m_float_value = value;
m_int_value = nullptr;
m_min = min; m_min = min;
m_max = max; m_max = max;
m_type = ValueType::FLOAT;
return *this;
}
Slider& Slider::set_slider(int* value, const int& min, const int& max) {
m_int_value = value;
m_float_value = nullptr;
m_min = static_cast<float>(min);
m_max = static_cast<float>(max);
m_type = ValueType::INT;
return *this; return *this;
} }
@@ -50,7 +61,7 @@ Slider& Slider::set_scale(float scale) {
m_track->set_width(width()).set_height(height()); m_track->set_width(width()).set_height(height());
m_thumb->set_height(height()); m_thumb->set_height(height());
m_thumb->set_width(height() / 2.0f); m_thumb->set_width(height() / 2.0f);
void update_text_scale(); update_text_scale();
return *this; return *this;
} }
@@ -61,7 +72,7 @@ Slider& Slider::set_width(float width) {
return *this; return *this;
} }
m_width = width; m_width = width;
void update_text_scale(); update_text_scale();
return *this; return *this;
} }
@@ -70,7 +81,7 @@ Slider& Slider::set_height(float h) {
ASSERT_MSG(m_thumb, "Thumb is nullptr !"); ASSERT_MSG(m_thumb, "Thumb is nullptr !");
m_thumb->set_height(height()); m_thumb->set_height(height());
m_thumb->set_width(height() / 2.0f); m_thumb->set_width(height() / 2.0f);
void update_text_scale(); update_text_scale();
return *this; return *this;
} }
Slider& Slider::set_text(const std::string& text) { Slider& Slider::set_text(const std::string& text) {
@@ -97,7 +108,7 @@ bool Slider::handle_mouse_move_event(const MouseMoveEvent& e) {
} else { } else {
m_inside = false; m_inside = false;
} }
if (m_dragging && m_value) { if (m_dragging) {
update_value(e.xpos); update_value(e.xpos);
return true; return true;
} }
@@ -128,12 +139,17 @@ void Slider::update_value(float mouse_x) {
return; return;
} }
float t = std::clamp((mouse_x - pos().x) / width(), 0.0f, 1.0f); float t = std::clamp((mouse_x - pos().x) / width(), 0.0f, 1.0f);
float value = m_min + t * (m_max - m_min);
*m_value = m_min + t * (m_max - m_min); if (m_type == ValueType::FLOAT && m_float_value) {
*m_float_value = value;
}
if (m_type == ValueType::INT && m_int_value) {
*m_int_value = static_cast<int>(std::round(value));
}
} }
void Slider::on_update(float dt) { void Slider::on_update(float dt) {
if (!m_value || !m_thumb || !m_track) { if (!m_thumb || !m_track) {
Logger::error("Please Set value and thumb and track"); Logger::error("Please Set value and thumb and track");
ASSERT(false); ASSERT(false);
return; return;
@@ -144,16 +160,28 @@ void Slider::on_update(float dt) {
ASSERT(false); ASSERT(false);
return; return;
} }
float t = 0.0f;
if (m_type == ValueType::FLOAT && m_float_value) {
t = (*m_float_value - m_min) / range;
}
float t = (*m_value - m_min) / range; if (m_type == ValueType::INT && m_int_value) {
t = (static_cast<float>(*m_int_value) - m_min) / range;
}
t = std::clamp(t, 0.0f, 1.0f); t = std::clamp(t, 0.0f, 1.0f);
float offset = t * (width() - m_thumb->width()); float offset = t * (width() - m_thumb->width());
m_thumb->set_offset({offset, 0}); m_thumb->set_offset({offset, 0});
if (m_label && m_value) { if (m_label) {
m_label->set_text(std::format("{}: {:.2f}", m_text, *m_value)); if (m_type == ValueType::FLOAT && m_float_value) {
m_label->set_text(
std::format("{}: {:.2f}", m_text, *m_float_value));
}
if (m_type == ValueType::INT && m_int_value) {
m_label->set_text(std::format("{}: {}", m_text, *m_int_value));
}
} }
if (m_track) { if (m_track) {
m_track->update(dt); m_track->update(dt);