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

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