mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-09 02:07:04 +08:00
feat(ui): add label and image support to slider
- Add `set_text()`, `set_track_image()`, `set_thumb_image()` methods. - Include label rendering and auto-scaling based on slider dimensions. - Replace static `THUMB_WIDTH` with dynamic thumb sizing. - Add slider thumb and track texture assets.
This commit is contained in:
@@ -9,6 +9,10 @@ namespace Cubed {
|
||||
Slider::Slider(Widget* parent) : Widget(parent) {
|
||||
init_thumb();
|
||||
init_track();
|
||||
|
||||
m_label = std::make_unique<Label>(this);
|
||||
m_label->set_anchor(Anchor::CENTER);
|
||||
void update_text_scale();
|
||||
}
|
||||
|
||||
Slider& Slider::set_slider(float* value, const float& min, const float& max) {
|
||||
@@ -18,9 +22,6 @@ Slider& Slider::set_slider(float* value, const float& min, const float& max) {
|
||||
return *this;
|
||||
}
|
||||
|
||||
Image& Slider::get_track() { return *m_track; }
|
||||
Image& Slider::get_thumb() { return *m_thumb; }
|
||||
|
||||
void Slider::init_track() {
|
||||
m_track = std::make_unique<Image>(this);
|
||||
|
||||
@@ -34,7 +35,7 @@ void Slider::init_thumb() {
|
||||
|
||||
m_thumb->set_height(height());
|
||||
|
||||
m_thumb->set_width(THUMB_WIDTH);
|
||||
m_thumb->set_width(height() / 2.0f);
|
||||
|
||||
m_thumb->set_anchor(Anchor::TOP_LEFT);
|
||||
|
||||
@@ -48,6 +49,8 @@ Slider& Slider::set_scale(float scale) {
|
||||
m_scale = 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();
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -58,6 +61,7 @@ Slider& Slider::set_width(float width) {
|
||||
return *this;
|
||||
}
|
||||
m_width = width;
|
||||
void update_text_scale();
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -65,6 +69,22 @@ Slider& Slider::set_height(float h) {
|
||||
m_height = h;
|
||||
ASSERT_MSG(m_thumb, "Thumb is nullptr !");
|
||||
m_thumb->set_height(height());
|
||||
m_thumb->set_width(height() / 2.0f);
|
||||
void update_text_scale();
|
||||
return *this;
|
||||
}
|
||||
Slider& Slider::set_text(const std::string& text) {
|
||||
m_text = text;
|
||||
return *this;
|
||||
}
|
||||
Slider& Slider::set_track_image(const std::string& path,
|
||||
TextureManager& texture_manager) {
|
||||
m_track->set_image(path, texture_manager);
|
||||
return *this;
|
||||
}
|
||||
Slider& Slider::set_thumb_image(const std::string& path,
|
||||
TextureManager& texture_manager) {
|
||||
m_thumb->set_image(path, texture_manager);
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -112,7 +132,7 @@ void Slider::update_value(float mouse_x) {
|
||||
*m_value = m_min + t * (m_max - m_min);
|
||||
}
|
||||
|
||||
void Slider::on_update(float) {
|
||||
void Slider::on_update(float dt) {
|
||||
if (!m_value || !m_thumb || !m_track) {
|
||||
Logger::error("Please Set value and thumb and track");
|
||||
ASSERT(false);
|
||||
@@ -132,6 +152,18 @@ void Slider::on_update(float) {
|
||||
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_track) {
|
||||
m_track->update(dt);
|
||||
}
|
||||
if (m_thumb) {
|
||||
m_thumb->update(dt);
|
||||
}
|
||||
if (m_label) {
|
||||
m_label->update(dt);
|
||||
}
|
||||
}
|
||||
|
||||
void Slider::on_render(Renderer& renderer) {
|
||||
@@ -141,6 +173,27 @@ void Slider::on_render(Renderer& renderer) {
|
||||
if (m_thumb) {
|
||||
m_thumb->render(renderer);
|
||||
}
|
||||
if (m_label) {
|
||||
m_label->render(renderer);
|
||||
}
|
||||
}
|
||||
|
||||
void Slider::update_text_scale() {
|
||||
float text_w = m_label->real_width();
|
||||
float text_h = m_label->real_height();
|
||||
|
||||
if (text_w <= 0.0f || text_h <= 0.0f)
|
||||
return;
|
||||
|
||||
float available_w = std::max(0.0f, width() - 2.0f * PADDING * m_scale);
|
||||
float available_h = std::max(0.0f, height() - 2.0f * PADDING * m_scale);
|
||||
|
||||
float scale_x = available_w / text_w;
|
||||
float scale_y = available_h / text_h;
|
||||
|
||||
float scale = std::max(0.0f, std::min(scale_x, scale_y));
|
||||
scale = std::clamp(scale, 0.01f, 100.0f);
|
||||
m_label->set_scale(scale);
|
||||
}
|
||||
|
||||
} // namespace Cubed
|
||||
Reference in New Issue
Block a user