mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-08 17:57:02 +08:00
feat(ui): add int slider support and fix update_text_scale calls
This commit is contained in:
@@ -5,12 +5,18 @@
|
||||
#include "Cubed/ui/widget.hpp"
|
||||
|
||||
namespace Cubed {
|
||||
|
||||
enum class ValueType {
|
||||
FLOAT,
|
||||
INT,
|
||||
};
|
||||
|
||||
class Slider : public Widget {
|
||||
public:
|
||||
Slider(Widget* parent);
|
||||
|
||||
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 height() const override;
|
||||
|
||||
@@ -33,16 +39,19 @@ private:
|
||||
std::unique_ptr<Image> m_track;
|
||||
std::unique_ptr<Image> m_thumb;
|
||||
std::unique_ptr<Label> m_label;
|
||||
ValueType m_type = ValueType::FLOAT;
|
||||
float m_scale = DEFAULT_SCALE;
|
||||
float m_width = NORMAL_SLIDER_WIDTH;
|
||||
float m_height = NORMAL_SLIDER_HEIGHT;
|
||||
float m_xpos = 0.0f;
|
||||
bool m_dragging = 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_max = 0;
|
||||
std::string m_text;
|
||||
|
||||
void init_track();
|
||||
void init_thumb();
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user