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 WrapLayout for grid-like widget arrangement
esenginef7595c1e9d65
diff --git a/src/esengine/ui/layout/WrapLayout.cpp b/src/esengine/ui/layout/WrapLayout.cppnew file mode 100644index 000000000..621a7992f--- /dev/null+++ b/src/esengine/ui/layout/WrapLayout.cpp@@ -0,0 +1,100 @@+/**+ * @file WrapLayout.cpp+ * @brief Wrap layout implementation+ *+ * @author ESEngine Team+ * @date 2026+ *+ * @copyright Copyright (c) 2026 ESEngine Team+ * Licensed under the MIT License.+ */++#include "WrapLayout.hpp"+#include "../widgets/Widget.hpp"++#include <algorithm>++namespace esengine::ui {++// =============================================================================+// Constructor+// =============================================================================++WrapLayout::WrapLayout(f32 hSpacing, f32 vSpacing)+ : hSpacing_(hSpacing), vSpacing_(vSpacing) {}++// =============================================================================+// Measure+// =============================================================================++glm::vec2 WrapLayout::measure(Widget& container, f32 availableWidth, f32 availableHeight) {+ const auto& children = container.getChildren();++ f32 x = 0.0f;+ f32 y = 0.0f;+ f32 rowHeight = 0.0f;+ f32 maxWidth = 0.0f;++ f32 effectiveWidth = availableWidth;+ if (effectiveWidth > 10000.0f || effectiveWidth <= 0.0f) {+ effectiveWidth = 800.0f;+ }++ for (const auto& child : children) {+ if (!child->isVisible()) continue;++ auto childSize = child->measure(effectiveWidth, availableHeight);++ if (x + childSize.x > effectiveWidth && x > 0) {+ x = 0;+ y += rowHeight + vSpacing_;+ rowHeight = 0;+ }++ x += childSize.x + hSpacing_;+ rowHeight = std::max(rowHeight, childSize.y);+ maxWidth = std::max(maxWidth, x - hSpacing_);+ }++ f32 totalHeight = y + rowHeight;+ f32 totalWidth = maxWidth;++ return {totalWidth, totalHeight};+}++// =============================================================================+// Layout+// =============================================================================++void WrapLayout::layout(Widget& container, const Rect& bounds) {+ const auto& children = container.getChildren();++ if (children.empty()) return;++ f32 startX = bounds.x;+ f32 startY = bounds.y;+ f32 contentWidth = bounds.width;++ f32 x = startX;+ f32 y = startY;+ f32 rowHeight = 0.0f;++ for (const auto& child : children) {+ if (!child->isVisible()) continue;++ auto childSize = child->measure(contentWidth, bounds.height);++ if (x - startX + childSize.x > contentWidth && x > startX) {+ x = startX;+ y += rowHeight + vSpacing_;+ rowHeight = 0;+ }++ child->layout(Rect{x, y, childSize.x, childSize.y});++ x += childSize.x + hSpacing_;+ rowHeight = std::max(rowHeight, childSize.y);+ }+}++} // namespace esengine::uidiff --git a/src/esengine/ui/layout/WrapLayout.hpp b/src/esengine/ui/layout/WrapLayout.hppnew file mode 100644index 000000000..72de541b5--- /dev/null+++ b/src/esengine/ui/layout/WrapLayout.hpp@@ -0,0 +1,78 @@+/**+ * @file WrapLayout.hpp+ * @brief Flow/wrap layout for grid-like arrangements+ * @details Arranges children in rows that wrap to the next line when+ * the container width is exceeded.+ *+ * @author ESEngine Team+ * @date 2026+ *+ * @copyright Copyright (c) 2026 ESEngine Team+ * Licensed under the MIT License.+ */+#pragma once++// =============================================================================+// Includes+// =============================================================================++#include "Layout.hpp"++namespace esengine::ui {++// =============================================================================+// WrapLayout Class+// =============================================================================++/**+ * @brief Arranges children in a wrapping flow layout+ *+ * @details Children are laid out horizontally and wrap to the next row+ * when they exceed the container width. Useful for grid-like+ * displays such as asset browsers.+ *+ * @code+ * auto panel = makeUnique<Panel>("panel");+ * panel->setLayout(makeUnique<WrapLayout>(8.0f, 8.0f));+ * panel->addChild(makeUnique<AssetItem>("item1"));+ * panel->addChild(makeUnique<AssetItem>("item2"));+ * @endcode+ */+class WrapLayout : public Layout {+public:+ /**+ * @brief Creates a wrap layout+ * @param hSpacing Horizontal space between items in pixels+ * @param vSpacing Vertical space between rows in pixels+ */+ explicit WrapLayout(f32 hSpacing = 4.0f, f32 vSpacing = 4.0f);++ // =========================================================================+ // Configuration+ // =========================================================================++ /** @brief Sets the horizontal spacing between items */+ void setHorizontalSpacing(f32 spacing) { hSpacing_ = spacing; }++ /** @brief Gets the horizontal spacing */+ f32 getHorizontalSpacing() const { return hSpacing_; }++ /** @brief Sets the vertical spacing between rows */+ void setVerticalSpacing(f32 spacing) { vSpacing_ = spacing; }++ /** @brief Gets the vertical spacing */+ f32 getVerticalSpacing() const { return vSpacing_; }++ // =========================================================================+ // Layout Interface+ // =========================================================================++ glm::vec2 measure(Widget& container, f32 availableWidth, f32 availableHeight) override;+ void layout(Widget& container, const Rect& bounds) override;++private:+ f32 hSpacing_ = 4.0f;+ f32 vSpacing_ = 4.0f;+};++} // namespace esengine::ui