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 ScrollView widget for scrollable content
esenginef274ad47fe74
diff --git a/CMakeLists.txt b/CMakeLists.txtindex 6fe7ba86d..5c39cff64 100644--- a/CMakeLists.txt+++ b/CMakeLists.txt@@ -45,6 +45,7 @@ set(ESENGINE_SOURCESsrc/esengine/ui/widgets/Panel.cppsrc/esengine/ui/widgets/Label.cppsrc/esengine/ui/widgets/Button.cpp+ src/esengine/ui/widgets/ScrollView.cpp# Docking Systemsrc/esengine/ui/docking/DockNode.cppsrc/esengine/ui/docking/DockPanel.cpp@@ -107,6 +108,7 @@ set(ESENGINE_HEADERSsrc/esengine/ui/widgets/Panel.hppsrc/esengine/ui/widgets/Label.hppsrc/esengine/ui/widgets/Button.hpp+ src/esengine/ui/widgets/ScrollView.hpp# Docking Systemsrc/esengine/ui/docking/DockTypes.hppsrc/esengine/ui/docking/DockNode.hppdiff --git a/src/esengine/ui/core/Theme.hpp b/src/esengine/ui/core/Theme.hppindex c57b8fd74..2028c6114 100644--- a/src/esengine/ui/core/Theme.hpp+++ b/src/esengine/ui/core/Theme.hpp@@ -180,6 +180,11 @@ struct WidgetStyle {glm::vec4 backgroundColorPressed{0.0f, 0.0f, 0.0f, 0.0f};glm::vec4 backgroundColorDisabled{0.0f, 0.0f, 0.0f, 0.0f};+ glm::vec4 foregroundColor{1.0f, 1.0f, 1.0f, 1.0f};+ glm::vec4 foregroundColorHover{1.0f, 1.0f, 1.0f, 1.0f};+ glm::vec4 foregroundColorPressed{1.0f, 1.0f, 1.0f, 1.0f};+ glm::vec4 foregroundColorDisabled{0.5f, 0.5f, 0.5f, 1.0f};+glm::vec4 textColor{1.0f, 1.0f, 1.0f, 1.0f};glm::vec4 textColorDisabled{0.5f, 0.5f, 0.5f, 1.0f};@@ -206,6 +211,20 @@ struct WidgetStyle {return backgroundColor;}+ /** @brief Gets the foreground color based on widget state */+ glm::vec4 getForegroundColor(const WidgetState& state) const {+ if (state.disabled) {+ return foregroundColorDisabled;+ }+ if (state.pressed) {+ return foregroundColorPressed;+ }+ if (state.hovered) {+ return foregroundColorHover;+ }+ return foregroundColor;+ }+/** @brief Gets the text color based on widget state */glm::vec4 getTextColor(const WidgetState& state) const {return state.disabled ? textColorDisabled : textColor;@@ -295,6 +314,16 @@ public:return style;}+ /** @brief Gets the style for a scrollbar widget */+ WidgetStyle getScrollbarStyle() const {+ WidgetStyle style;+ style.backgroundColor = colors.scrollbarTrack;+ style.backgroundColorHover = colors.scrollbarTrack;+ style.foregroundColor = colors.scrollbarThumb;+ style.foregroundColorHover = colors.scrollbarThumbHover;+ return style;+ }+/** @brief Gets the style for a text input widget */WidgetStyle getTextInputStyle() const {WidgetStyle style;diff --git a/src/esengine/ui/widgets/ScrollView.cpp b/src/esengine/ui/widgets/ScrollView.cppnew file mode 100644index 000000000..a2cd9e58d--- /dev/null+++ b/src/esengine/ui/widgets/ScrollView.cpp@@ -0,0 +1,450 @@+/**+ * @file ScrollView.cpp+ * @brief Scrollable container widget implementation+ *+ * @author ESEngine Team+ * @date 2026+ *+ * @copyright Copyright (c) 2026 ESEngine Team+ * Licensed under the MIT License.+ */++#include "ScrollView.hpp"+#include "../UIContext.hpp"+#include "../rendering/UIBatchRenderer.hpp"+#include "../../math/Math.hpp"++namespace esengine::ui {++// =============================================================================+// Constructor+// =============================================================================++ScrollView::ScrollView(const WidgetId& id) : Widget(id) {}++// =============================================================================+// Content Management+// =============================================================================++void ScrollView::setContent(Unique<Widget> content) {+ if (content_) {+ removeChild(content_);+ }++ if (content) {+ content_ = content.get();+ addChild(std::move(content));+ invalidateLayout();+ } else {+ content_ = nullptr;+ }+}++// =============================================================================+// Scroll Position+// =============================================================================++void ScrollView::setScrollOffset(const glm::vec2& offset) {+ scrollOffset_ = offset;+ clampScrollOffset();+ updateContentLayout();+}++void ScrollView::scrollBy(const glm::vec2& delta) {+ setScrollOffset(scrollOffset_ + delta);+}++void ScrollView::scrollToWidget(const Widget* widget) {+ if (!widget || !content_) return;++ const Rect& widgetBounds = widget->getBounds();+ const Rect& viewBounds = getBounds();++ glm::vec2 newOffset = scrollOffset_;++ if (scrollDirection_ & ScrollDirection::Vertical) {+ f32 widgetTop = widgetBounds.y;+ f32 widgetBottom = widgetBounds.y + widgetBounds.height;+ f32 viewTop = viewBounds.y + scrollOffset_.y;+ f32 viewBottom = viewTop + viewportSize_.y;++ if (widgetTop < viewTop) {+ newOffset.y = widgetTop - viewBounds.y;+ } else if (widgetBottom > viewBottom) {+ newOffset.y = widgetBottom - viewBounds.y - viewportSize_.y;+ }+ }++ if (scrollDirection_ & ScrollDirection::Horizontal) {+ f32 widgetLeft = widgetBounds.x;+ f32 widgetRight = widgetBounds.x + widgetBounds.width;+ f32 viewLeft = viewBounds.x + scrollOffset_.x;+ f32 viewRight = viewLeft + viewportSize_.x;++ if (widgetLeft < viewLeft) {+ newOffset.x = widgetLeft - viewBounds.x;+ } else if (widgetRight > viewRight) {+ newOffset.x = widgetRight - viewBounds.x - viewportSize_.x;+ }+ }++ setScrollOffset(newOffset);+}++glm::vec2 ScrollView::getMaxScrollOffset() const {+ glm::vec2 maxOffset{0.0f, 0.0f};++ if (scrollDirection_ & ScrollDirection::Vertical) {+ maxOffset.y = glm::max(0.0f, contentSize_.y - viewportSize_.y);+ }++ if (scrollDirection_ & ScrollDirection::Horizontal) {+ maxOffset.x = glm::max(0.0f, contentSize_.x - viewportSize_.x);+ }++ return maxOffset;+}++// =============================================================================+// Layout+// =============================================================================++glm::vec2 ScrollView::measure(f32 availableWidth, f32 availableHeight) {+ glm::vec2 size = Widget::measure(availableWidth, availableHeight);++ if (content_) {+ f32 contentAvailableWidth = availableWidth;+ f32 contentAvailableHeight = availableHeight;++ if (showScrollbars_) {+ if (scrollDirection_ & ScrollDirection::Vertical) {+ contentAvailableWidth -= scrollbarWidth_;+ }+ if (scrollDirection_ & ScrollDirection::Horizontal) {+ contentAvailableHeight -= scrollbarWidth_;+ }+ }++ contentSize_ = content_->measure(std::numeric_limits<f32>::infinity(),+ std::numeric_limits<f32>::infinity());+ }++ return size;+}++void ScrollView::layout(const Rect& bounds) {+ Widget::layout(bounds);++ const Insets& padding = getPadding();+ viewportSize_ = glm::vec2(+ bounds.width - padding.left - padding.right,+ bounds.height - padding.top - padding.bottom+ );++ if (showScrollbars_) {+ if (scrollDirection_ & ScrollDirection::Vertical) {+ viewportSize_.x -= scrollbarWidth_;+ }+ if (scrollDirection_ & ScrollDirection::Horizontal) {+ viewportSize_.y -= scrollbarWidth_;+ }+ }++ clampScrollOffset();+ updateContentLayout();+}++// =============================================================================+// Rendering+// =============================================================================++void ScrollView::render(UIBatchRenderer& renderer) {+ if (!content_) return;++ const Rect& bounds = getBounds();+ const Insets& padding = getPadding();++ Rect viewportRect{+ bounds.x + padding.left,+ bounds.y + padding.top,+ viewportSize_.x,+ viewportSize_.y+ };++ renderer.pushClipRect(viewportRect);+ content_->renderTree(renderer);+ renderer.popClipRect();++ if (showScrollbars_) {+ if (canScrollVertically()) {+ renderVerticalScrollbar(renderer);+ }+ if (canScrollHorizontally()) {+ renderHorizontalScrollbar(renderer);+ }+ }+}++Widget* ScrollView::hitTest(f32 x, f32 y) {+ if (!containsPoint(x, y)) {+ return nullptr;+ }++ if (showScrollbars_) {+ if (canScrollVertically()) {+ Rect vScrollbarBounds = getVerticalScrollbarBounds();+ if (vScrollbarBounds.contains(x, y)) {+ return this;+ }+ }++ if (canScrollHorizontally()) {+ Rect hScrollbarBounds = getHorizontalScrollbarBounds();+ if (hScrollbarBounds.contains(x, y)) {+ return this;+ }+ }+ }++ if (content_) {+ Widget* hit = content_->hitTest(x, y);+ if (hit) {+ return hit;+ }+ }++ return this;+}++// =============================================================================+// Event Handling+// =============================================================================++bool ScrollView::onScroll(const ScrollEvent& event) {+ glm::vec2 delta{0.0f, 0.0f};++ if (scrollDirection_ & ScrollDirection::Vertical) {+ delta.y = -event.deltaY * scrollSpeed_;+ }++ if (scrollDirection_ & ScrollDirection::Horizontal) {+ delta.x = -event.deltaX * scrollSpeed_;+ }++ if (glm::length(delta) > 0.0f) {+ scrollBy(delta);+ return true;+ }++ return false;+}++bool ScrollView::onMouseDown(const MouseButtonEvent& event) {+ if (event.button != MouseButton::Left) {+ return false;+ }++ if (showScrollbars_ && canScrollVertically()) {+ Rect thumbBounds = getVerticalScrollbarThumbBounds();+ if (thumbBounds.contains(event.x, event.y)) {+ draggingVerticalScrollbar_ = true;+ dragStartMousePos_ = glm::vec2(event.x, event.y);+ dragStartScrollOffset_ = scrollOffset_;+ return true;+ }+ }++ if (showScrollbars_ && canScrollHorizontally()) {+ Rect thumbBounds = getHorizontalScrollbarThumbBounds();+ if (thumbBounds.contains(event.x, event.y)) {+ draggingHorizontalScrollbar_ = true;+ dragStartMousePos_ = glm::vec2(event.x, event.y);+ dragStartScrollOffset_ = scrollOffset_;+ return true;+ }+ }++ return false;+}++bool ScrollView::onMouseUp(const MouseButtonEvent& event) {+ if (event.button != MouseButton::Left) {+ return false;+ }++ bool wasDragging = draggingVerticalScrollbar_ || draggingHorizontalScrollbar_;+ draggingVerticalScrollbar_ = false;+ draggingHorizontalScrollbar_ = false;++ return wasDragging;+}++bool ScrollView::onMouseMove(const MouseMoveEvent& event) {+ if (draggingVerticalScrollbar_) {+ f32 mouseDelta = event.y - dragStartMousePos_.y;+ f32 trackHeight = viewportSize_.y;+ f32 maxScroll = getMaxScrollOffset().y;++ if (trackHeight > 0.0f) {+ f32 scrollRatio = maxScroll / trackHeight;+ f32 scrollDelta = mouseDelta * scrollRatio;++ glm::vec2 newOffset = dragStartScrollOffset_;+ newOffset.y += scrollDelta;+ setScrollOffset(newOffset);+ }++ return true;+ }++ if (draggingHorizontalScrollbar_) {+ f32 mouseDelta = event.x - dragStartMousePos_.x;+ f32 trackWidth = viewportSize_.x;+ f32 maxScroll = getMaxScrollOffset().x;++ if (trackWidth > 0.0f) {+ f32 scrollRatio = maxScroll / trackWidth;+ f32 scrollDelta = mouseDelta * scrollRatio;++ glm::vec2 newOffset = dragStartScrollOffset_;+ newOffset.x += scrollDelta;+ setScrollOffset(newOffset);+ }++ return true;+ }++ return false;+}++// =============================================================================+// Private Methods+// =============================================================================++void ScrollView::clampScrollOffset() {+ glm::vec2 maxOffset = getMaxScrollOffset();+ scrollOffset_.x = glm::clamp(scrollOffset_.x, 0.0f, maxOffset.x);+ scrollOffset_.y = glm::clamp(scrollOffset_.y, 0.0f, maxOffset.y);+}++void ScrollView::updateContentLayout() {+ if (!content_) return;++ const Rect& bounds = getBounds();+ const Insets& padding = getPadding();++ Rect contentBounds{+ bounds.x + padding.left - scrollOffset_.x,+ bounds.y + padding.top - scrollOffset_.y,+ contentSize_.x,+ contentSize_.y+ };++ content_->layout(contentBounds);+}++void ScrollView::renderVerticalScrollbar(UIBatchRenderer& renderer) {+ Rect trackBounds = getVerticalScrollbarBounds();+ Rect thumbBounds = getVerticalScrollbarThumbBounds();++ WidgetStyle style;+ if (getContext()) {+ style = getContext()->getTheme().getScrollbarStyle();+ }++ glm::vec4 trackColor = style.getBackgroundColor(getState());+ glm::vec4 thumbColor = style.getForegroundColor(getState());++ renderer.drawRect(trackBounds, trackColor);+ renderer.drawRoundedRect(thumbBounds, thumbColor, CornerRadii::all(4.0f));+}++void ScrollView::renderHorizontalScrollbar(UIBatchRenderer& renderer) {+ Rect trackBounds = getHorizontalScrollbarBounds();+ Rect thumbBounds = getHorizontalScrollbarThumbBounds();++ WidgetStyle style;+ if (getContext()) {+ style = getContext()->getTheme().getScrollbarStyle();+ }++ glm::vec4 trackColor = style.getBackgroundColor(getState());+ glm::vec4 thumbColor = style.getForegroundColor(getState());++ renderer.drawRect(trackBounds, trackColor);+ renderer.drawRoundedRect(thumbBounds, thumbColor, CornerRadii::all(4.0f));+}++Rect ScrollView::getVerticalScrollbarBounds() const {+ const Rect& bounds = getBounds();+ return Rect{+ bounds.x + bounds.width - scrollbarWidth_,+ bounds.y,+ scrollbarWidth_,+ viewportSize_.y+ };+}++Rect ScrollView::getHorizontalScrollbarBounds() const {+ const Rect& bounds = getBounds();+ return Rect{+ bounds.x,+ bounds.y + bounds.height - scrollbarWidth_,+ viewportSize_.x,+ scrollbarWidth_+ };+}++Rect ScrollView::getVerticalScrollbarThumbBounds() const {+ Rect trackBounds = getVerticalScrollbarBounds();+ f32 maxScroll = getMaxScrollOffset().y;++ if (maxScroll <= 0.0f) {+ return trackBounds;+ }++ f32 visibleRatio = viewportSize_.y / contentSize_.y;+ f32 thumbHeight = glm::max(20.0f, trackBounds.height * visibleRatio);+ f32 scrollRatio = scrollOffset_.y / maxScroll;+ f32 thumbY = trackBounds.y + scrollRatio * (trackBounds.height - thumbHeight);++ return Rect{+ trackBounds.x + 2.0f,+ thumbY,+ scrollbarWidth_ - 4.0f,+ thumbHeight+ };+}++Rect ScrollView::getHorizontalScrollbarThumbBounds() const {+ Rect trackBounds = getHorizontalScrollbarBounds();+ f32 maxScroll = getMaxScrollOffset().x;++ if (maxScroll <= 0.0f) {+ return trackBounds;+ }++ f32 visibleRatio = viewportSize_.x / contentSize_.x;+ f32 thumbWidth = glm::max(20.0f, trackBounds.width * visibleRatio);+ f32 scrollRatio = scrollOffset_.x / maxScroll;+ f32 thumbX = trackBounds.x + scrollRatio * (trackBounds.width - thumbWidth);++ return Rect{+ thumbX,+ trackBounds.y + 2.0f,+ thumbWidth,+ scrollbarWidth_ - 4.0f+ };+}++bool ScrollView::canScrollVertically() const {+ return (scrollDirection_ & ScrollDirection::Vertical) &&+ contentSize_.y > viewportSize_.y;+}++bool ScrollView::canScrollHorizontally() const {+ return (scrollDirection_ & ScrollDirection::Horizontal) &&+ contentSize_.x > viewportSize_.x;+}++} // namespace esengine::uidiff --git a/src/esengine/ui/widgets/ScrollView.hpp b/src/esengine/ui/widgets/ScrollView.hppnew file mode 100644index 000000000..eac99aaed--- /dev/null+++ b/src/esengine/ui/widgets/ScrollView.hpp@@ -0,0 +1,260 @@+/**+ * @file ScrollView.hpp+ * @brief Scrollable container widget+ * @details A container widget that provides scrolling for content larger+ * than its visible area. Supports vertical and horizontal scrolling+ * with optional scrollbars.+ *+ * @author ESEngine Team+ * @date 2026+ *+ * @copyright Copyright (c) 2026 ESEngine Team+ * Licensed under the MIT License.+ */+#pragma once++// =============================================================================+// Includes+// =============================================================================++#include "Widget.hpp"++namespace esengine::ui {++// =============================================================================+// ScrollDirection Enum+// =============================================================================++/**+ * @brief Scroll direction flags+ */+enum class ScrollDirection : u8 {+ None = 0,+ Vertical = 1 << 0,+ Horizontal = 1 << 1,+ Both = Vertical | Horizontal+};++inline ScrollDirection operator|(ScrollDirection a, ScrollDirection b) {+ return static_cast<ScrollDirection>(static_cast<u8>(a) | static_cast<u8>(b));+}++inline bool operator&(ScrollDirection a, ScrollDirection b) {+ return (static_cast<u8>(a) & static_cast<u8>(b)) != 0;+}++// =============================================================================+// ScrollView Class+// =============================================================================++/**+ * @brief Scrollable container widget+ *+ * @details ScrollView provides a scrollable viewport for content that exceeds+ * the available space. Features:+ * - Vertical and/or horizontal scrolling+ * - Mouse wheel support+ * - Optional scrollbar rendering+ * - Smooth scrolling with configurable speed+ *+ * @code+ * auto scroll = makeUnique<ScrollView>(WidgetId("scroll"));+ * scroll->setScrollDirection(ScrollDirection::Vertical);+ * scroll->setShowScrollbars(true);+ *+ * auto content = makeUnique<Panel>(WidgetId("content"));+ * content->setHeight(SizeValue::pixels(2000.0f)); // Tall content+ * scroll->setContent(std::move(content));+ * @endcode+ */+class ScrollView : public Widget {+public:+ /**+ * @brief Creates a scroll view+ * @param id Widget identifier+ */+ explicit ScrollView(const WidgetId& id);++ // =========================================================================+ // Configuration+ // =========================================================================++ /**+ * @brief Sets the scroll direction+ * @param direction Vertical, Horizontal, or Both+ */+ void setScrollDirection(ScrollDirection direction) { scrollDirection_ = direction; }++ /**+ * @brief Gets the scroll direction+ */+ ScrollDirection getScrollDirection() const { return scrollDirection_; }++ /**+ * @brief Sets whether to show scrollbars+ * @param show True to show scrollbars+ */+ void setShowScrollbars(bool show) { showScrollbars_ = show; }++ /**+ * @brief Returns true if scrollbars are shown+ */+ bool isShowingScrollbars() const { return showScrollbars_; }++ /**+ * @brief Sets the scrollbar width+ * @param width Scrollbar width in pixels+ */+ void setScrollbarWidth(f32 width) { scrollbarWidth_ = width; }++ /**+ * @brief Gets the scrollbar width+ */+ f32 getScrollbarWidth() const { return scrollbarWidth_; }++ /**+ * @brief Sets the scroll speed multiplier+ * @param speed Scroll speed (default 1.0)+ */+ void setScrollSpeed(f32 speed) { scrollSpeed_ = speed; }++ /**+ * @brief Gets the scroll speed+ */+ f32 getScrollSpeed() const { return scrollSpeed_; }++ // =========================================================================+ // Content+ // =========================================================================++ /**+ * @brief Sets the scrollable content+ * @param content Widget to scroll (ownership transferred)+ */+ void setContent(Unique<Widget> content);++ /**+ * @brief Gets the content widget+ */+ Widget* getContent() const { return content_; }++ // =========================================================================+ // Scroll Position+ // =========================================================================++ /**+ * @brief Gets the current scroll offset+ * @return Scroll offset (x, y)+ */+ glm::vec2 getScrollOffset() const { return scrollOffset_; }++ /**+ * @brief Sets the scroll offset+ * @param offset New scroll offset (will be clamped)+ */+ void setScrollOffset(const glm::vec2& offset);++ /**+ * @brief Scrolls by a delta amount+ * @param delta Amount to scroll (x, y)+ */+ void scrollBy(const glm::vec2& delta);++ /**+ * @brief Scrolls to make a widget visible+ * @param widget Widget to scroll to+ */+ void scrollToWidget(const Widget* widget);++ /**+ * @brief Gets the maximum scroll offset+ */+ glm::vec2 getMaxScrollOffset() const;++ // =========================================================================+ // Widget Overrides+ // =========================================================================++ glm::vec2 measure(f32 availableWidth, f32 availableHeight) override;+ void layout(const Rect& bounds) override;+ void render(UIBatchRenderer& renderer) override;+ Widget* hitTest(f32 x, f32 y) override;++ bool onScroll(const ScrollEvent& event) override;+ bool onMouseDown(const MouseButtonEvent& event) override;+ bool onMouseUp(const MouseButtonEvent& event) override;+ bool onMouseMove(const MouseMoveEvent& event) override;++private:+ // =========================================================================+ // Private Methods+ // =========================================================================++ /**+ * @brief Clamps scroll offset to valid range+ */+ void clampScrollOffset();++ /**+ * @brief Updates content layout based on scroll offset+ */+ void updateContentLayout();++ /**+ * @brief Renders the vertical scrollbar+ */+ void renderVerticalScrollbar(UIBatchRenderer& renderer);++ /**+ * @brief Renders the horizontal scrollbar+ */+ void renderHorizontalScrollbar(UIBatchRenderer& renderer);++ /**+ * @brief Gets the vertical scrollbar bounds+ */+ Rect getVerticalScrollbarBounds() const;++ /**+ * @brief Gets the horizontal scrollbar bounds+ */+ Rect getHorizontalScrollbarBounds() const;++ /**+ * @brief Gets the vertical scrollbar thumb bounds+ */+ Rect getVerticalScrollbarThumbBounds() const;++ /**+ * @brief Gets the horizontal scrollbar thumb bounds+ */+ Rect getHorizontalScrollbarThumbBounds() const;++ /**+ * @brief Checks if scrolling is possible in a direction+ */+ bool canScrollVertically() const;+ bool canScrollHorizontally() const;++ // =========================================================================+ // Member Variables+ // =========================================================================++ Widget* content_ = nullptr; ///< Scrollable content+ ScrollDirection scrollDirection_ = ScrollDirection::Vertical;+ bool showScrollbars_ = true;+ f32 scrollbarWidth_ = 12.0f;+ f32 scrollSpeed_ = 20.0f;++ glm::vec2 scrollOffset_{0.0f, 0.0f}; ///< Current scroll position+ glm::vec2 contentSize_{0.0f, 0.0f}; ///< Size of content+ glm::vec2 viewportSize_{0.0f, 0.0f}; ///< Size of visible area++ // Scrollbar dragging state+ bool draggingVerticalScrollbar_ = false;+ bool draggingHorizontalScrollbar_ = false;+ glm::vec2 dragStartMousePos_{0.0f, 0.0f};+ glm::vec2 dragStartScrollOffset_{0.0f, 0.0f};+};++} // namespace esengine::ui