A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
feat: Add Slider widget with horizontal and vertical support
esengine9a24af69c1d5
diff --git a/CMakeLists.txt b/CMakeLists.txtindex be315f085..337417c9a 100644--- a/CMakeLists.txt+++ b/CMakeLists.txt@@ -49,6 +49,7 @@ set(ESENGINE_SOURCESsrc/esengine/ui/widgets/TreeView.cppsrc/esengine/ui/widgets/TextField.cppsrc/esengine/ui/widgets/Checkbox.cpp+ src/esengine/ui/widgets/Slider.cpp# Docking Systemsrc/esengine/ui/docking/DockNode.cppsrc/esengine/ui/docking/DockPanel.cpp@@ -115,6 +116,7 @@ set(ESENGINE_HEADERSsrc/esengine/ui/widgets/TreeView.hppsrc/esengine/ui/widgets/TextField.hppsrc/esengine/ui/widgets/Checkbox.hpp+ src/esengine/ui/widgets/Slider.hpp# Docking Systemsrc/esengine/ui/docking/DockTypes.hppsrc/esengine/ui/docking/DockNode.hppdiff --git a/src/esengine/ui/widgets/Slider.cpp b/src/esengine/ui/widgets/Slider.cppnew file mode 100644index 000000000..6c541ffaf--- /dev/null+++ b/src/esengine/ui/widgets/Slider.cpp@@ -0,0 +1,253 @@+/**+ * @file Slider.cpp+ * @brief Slider widget implementation+ *+ * @author ESEngine Team+ * @date 2026+ *+ * @copyright Copyright (c) 2026 ESEngine Team+ * Licensed under the MIT License.+ */++#include "Slider.hpp"+#include "../UIContext.hpp"+#include "../rendering/UIBatchRenderer.hpp"+#include "../../math/Math.hpp"++#include <cmath>++namespace esengine::ui {++// =============================================================================+// Constructor+// =============================================================================++Slider::Slider(const WidgetId& id, SliderOrientation orientation)+ : Widget(id), orientation_(orientation) {}++// =============================================================================+// Value Management+// =============================================================================++void Slider::setValue(f32 value) {+ f32 clampedValue = glm::clamp(value, min_, max_);+ clampedValue = quantizeValue(clampedValue);++ if (value_ == clampedValue) {+ return;+ }++ value_ = clampedValue;+ onValueChanged.publish(value_);+}++void Slider::setRange(f32 min, f32 max) {+ min_ = min;+ max_ = max;++ if (max_ < min_) {+ std::swap(min_, max_);+ }++ setValue(value_);+}++void Slider::setStep(f32 step) {+ step_ = glm::max(0.0f, step);+ setValue(value_);+}++// =============================================================================+// Orientation+// =============================================================================++void Slider::setOrientation(SliderOrientation orientation) {+ if (orientation_ == orientation) {+ return;+ }++ orientation_ = orientation;+ invalidateLayout();+}++// =============================================================================+// Appearance+// =============================================================================++void Slider::setTrackThickness(f32 thickness) {+ if (trackThickness_ == thickness) {+ return;+ }++ trackThickness_ = thickness;+ invalidateLayout();+}++void Slider::setThumbSize(f32 size) {+ if (thumbSize_ == size) {+ return;+ }++ thumbSize_ = size;+ invalidateLayout();+}++// =============================================================================+// Widget Interface+// =============================================================================++glm::vec2 Slider::measure(f32 availableWidth, f32 availableHeight) {+ f32 width = 0.0f;+ f32 height = 0.0f;++ if (orientation_ == SliderOrientation::Horizontal) {+ width = availableWidth;+ height = thumbSize_;+ } else {+ width = thumbSize_;+ height = availableHeight;+ }++ const SizeConstraints& constraints = getConstraints();+ width = glm::clamp(width, constraints.minWidth, constraints.maxWidth);+ height = glm::clamp(height, constraints.minHeight, constraints.maxHeight);++ return glm::vec2(width, height);+}++void Slider::render(UIBatchRenderer& renderer) {+ const Rect& bounds = getBounds();+ const Insets& padding = getPadding();++ WidgetStyle trackStyle;+ WidgetStyle thumbStyle;+ if (getContext()) {+ trackStyle = getContext()->getTheme().getSliderTrackStyle();+ thumbStyle = getContext()->getTheme().getSliderThumbStyle();+ }++ WidgetState state{+ .hovered = isHovered(),+ .pressed = isPressed(),+ .focused = false,+ .disabled = !isEnabled(),+ .visible = true+ };++ f32 normalizedValue = getNormalizedValue();++ if (orientation_ == SliderOrientation::Horizontal) {+ f32 trackWidth = bounds.width - padding.left - padding.right - thumbSize_;+ f32 trackHeight = trackThickness_;+ f32 trackX = bounds.x + padding.left + thumbSize_ * 0.5f;+ f32 trackY = bounds.y + (bounds.height - trackHeight) * 0.5f;++ Rect trackBounds{trackX, trackY, trackWidth, trackHeight};+ glm::vec4 trackColor = trackStyle.getBackgroundColor(state);+ renderer.drawRoundedRect(trackBounds, trackColor, trackStyle.cornerRadii);++ f32 thumbX = trackX + normalizedValue * trackWidth - thumbSize_ * 0.5f;+ f32 thumbY = bounds.y + (bounds.height - thumbSize_) * 0.5f;++ Rect thumbBounds{thumbX, thumbY, thumbSize_, thumbSize_};+ glm::vec4 thumbColor = thumbStyle.getBackgroundColor(state);+ renderer.drawRoundedRect(thumbBounds, thumbColor, thumbStyle.cornerRadii);+ } else {+ f32 trackWidth = trackThickness_;+ f32 trackHeight = bounds.height - padding.top - padding.bottom - thumbSize_;+ f32 trackX = bounds.x + (bounds.width - trackWidth) * 0.5f;+ f32 trackY = bounds.y + padding.top + thumbSize_ * 0.5f;++ Rect trackBounds{trackX, trackY, trackWidth, trackHeight};+ glm::vec4 trackColor = trackStyle.getBackgroundColor(state);+ renderer.drawRoundedRect(trackBounds, trackColor, trackStyle.cornerRadii);++ f32 thumbX = bounds.x + (bounds.width - thumbSize_) * 0.5f;+ f32 thumbY = trackY + (1.0f - normalizedValue) * trackHeight - thumbSize_ * 0.5f;++ Rect thumbBounds{thumbX, thumbY, thumbSize_, thumbSize_};+ glm::vec4 thumbColor = thumbStyle.getBackgroundColor(state);+ renderer.drawRoundedRect(thumbBounds, thumbColor, thumbStyle.cornerRadii);+ }+}++bool Slider::onMouseDown(const MouseButtonEvent& event) {+ if (event.button != MouseButton::Left) {+ return false;+ }++ const Rect& bounds = getBounds();+ if (!bounds.contains(event.x, event.y)) {+ return false;+ }++ dragging_ = true;+ updateValueFromPosition(event.x, event.y);++ return true;+}++bool Slider::onMouseMove(const MouseMoveEvent& event) {+ if (!dragging_) {+ return false;+ }++ updateValueFromPosition(event.x, event.y);+ return true;+}++bool Slider::onMouseUp(const MouseButtonEvent& event) {+ if (event.button != MouseButton::Left) {+ return false;+ }++ dragging_ = false;+ return true;+}++// =============================================================================+// Private Methods+// =============================================================================++void Slider::updateValueFromPosition(f32 x, f32 y) {+ const Rect& bounds = getBounds();+ const Insets& padding = getPadding();++ f32 normalizedValue = 0.0f;++ if (orientation_ == SliderOrientation::Horizontal) {+ f32 trackWidth = bounds.width - padding.left - padding.right - thumbSize_;+ f32 trackX = bounds.x + padding.left + thumbSize_ * 0.5f;++ f32 relativeX = x - trackX;+ normalizedValue = glm::clamp(relativeX / trackWidth, 0.0f, 1.0f);+ } else {+ f32 trackHeight = bounds.height - padding.top - padding.bottom - thumbSize_;+ f32 trackY = bounds.y + padding.top + thumbSize_ * 0.5f;++ f32 relativeY = y - trackY;+ normalizedValue = 1.0f - glm::clamp(relativeY / trackHeight, 0.0f, 1.0f);+ }++ f32 newValue = min_ + normalizedValue * (max_ - min_);+ setValue(newValue);+}++f32 Slider::quantizeValue(f32 value) const {+ if (step_ <= 0.0f) {+ return value;+ }++ f32 steps = std::round((value - min_) / step_);+ return min_ + steps * step_;+}++f32 Slider::getNormalizedValue() const {+ if (max_ <= min_) {+ return 0.0f;+ }++ return (value_ - min_) / (max_ - min_);+}++} // namespace esengine::uidiff --git a/src/esengine/ui/widgets/Slider.hpp b/src/esengine/ui/widgets/Slider.hppnew file mode 100644index 000000000..ab69f8fa7--- /dev/null+++ b/src/esengine/ui/widgets/Slider.hpp@@ -0,0 +1,212 @@+/**+ * @file Slider.hpp+ * @brief Slider widget for numeric input+ * @details Provides a draggable slider for selecting values within a range.+ * Supports both horizontal and vertical orientations with optional+ * step quantization.+ *+ * @author ESEngine Team+ * @date 2026+ *+ * @copyright Copyright (c) 2026 ESEngine Team+ * Licensed under the MIT License.+ */+#pragma once++// =============================================================================+// Includes+// =============================================================================++#include "Widget.hpp"+#include "../../events/Signal.hpp"++namespace esengine::ui {++// =============================================================================+// Slider Orientation+// =============================================================================++/**+ * @brief Slider orientation+ */+enum class SliderOrientation : u8 {+ Horizontal,+ Vertical+};++// =============================================================================+// Slider Class+// =============================================================================++/**+ * @brief Slider widget for numeric value selection+ *+ * @details Renders a track with a draggable thumb. Clicking the track+ * jumps the value to that position. Dragging adjusts continuously.+ *+ * @code+ * auto slider = makeUnique<Slider>(WidgetId("volume"));+ * slider->setRange(0.0f, 1.0f);+ * slider->setValue(0.5f);+ * slider->onValueChanged.connect([](f32 value) {+ * ES_LOG_INFO("Volume: {}", value);+ * });+ * @endcode+ */+class Slider : public Widget {+public:+ /**+ * @brief Constructs a slider widget+ * @param id Unique widget identifier+ * @param orientation Slider orientation (default horizontal)+ */+ explicit Slider(const WidgetId& id, SliderOrientation orientation = SliderOrientation::Horizontal);++ ~Slider() override = default;++ // =========================================================================+ // Value Management+ // =========================================================================++ /**+ * @brief Sets the current value+ * @param value New value (clamped to range)+ */+ void setValue(f32 value);++ /**+ * @brief Gets the current value+ * @return Current value+ */+ f32 getValue() const { return value_; }++ /**+ * @brief Sets the value range+ * @param min Minimum value+ * @param max Maximum value+ */+ void setRange(f32 min, f32 max);++ /**+ * @brief Gets the minimum value+ * @return Minimum value+ */+ f32 getMin() const { return min_; }++ /**+ * @brief Gets the maximum value+ * @return Maximum value+ */+ f32 getMax() const { return max_; }++ /**+ * @brief Sets the step size for quantization+ * @param step Step size (0 for continuous)+ */+ void setStep(f32 step);++ /**+ * @brief Gets the step size+ * @return Step size+ */+ f32 getStep() const { return step_; }++ // =========================================================================+ // Orientation+ // =========================================================================++ /**+ * @brief Sets the slider orientation+ * @param orientation Horizontal or Vertical+ */+ void setOrientation(SliderOrientation orientation);++ /**+ * @brief Gets the slider orientation+ * @return Current orientation+ */+ SliderOrientation getOrientation() const { return orientation_; }++ // =========================================================================+ // Appearance+ // =========================================================================++ /**+ * @brief Sets the track thickness+ * @param thickness Track thickness in pixels (default 4.0f)+ */+ void setTrackThickness(f32 thickness);++ /**+ * @brief Gets the track thickness+ * @return Track thickness in pixels+ */+ f32 getTrackThickness() const { return trackThickness_; }++ /**+ * @brief Sets the thumb size+ * @param size Thumb diameter in pixels (default 16.0f)+ */+ void setThumbSize(f32 size);++ /**+ * @brief Gets the thumb size+ * @return Thumb diameter in pixels+ */+ f32 getThumbSize() const { return thumbSize_; }++ // =========================================================================+ // Widget Interface+ // =========================================================================++ glm::vec2 measure(f32 availableWidth, f32 availableHeight) override;+ void render(class UIBatchRenderer& renderer) override;++ bool onMouseDown(const MouseButtonEvent& event) override;+ bool onMouseMove(const MouseMoveEvent& event) override;+ bool onMouseUp(const MouseButtonEvent& event) override;++ // =========================================================================+ // Signals+ // =========================================================================++ /**+ * @brief Emitted when the value changes+ * @param value New value+ */+ Signal<void(f32)> onValueChanged;++private:+ /**+ * @brief Updates value from mouse position+ * @param x Mouse X coordinate+ * @param y Mouse Y coordinate+ */+ void updateValueFromPosition(f32 x, f32 y);++ /**+ * @brief Quantizes value to step size+ * @param value Raw value+ * @return Quantized value+ */+ f32 quantizeValue(f32 value) const;++ /**+ * @brief Gets the normalized value (0-1)+ * @return Normalized value+ */+ f32 getNormalizedValue() const;++ f32 value_ = 0.0f;+ f32 min_ = 0.0f;+ f32 max_ = 1.0f;+ f32 step_ = 0.0f;++ SliderOrientation orientation_ = SliderOrientation::Horizontal;+ f32 trackThickness_ = 4.0f;+ f32 thumbSize_ = 16.0f;++ bool dragging_ = false;+};++} // namespace esengine::ui