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
fix: Add mouse button support and fix UI layout issues
esenginedca4ee85d9e9
diff --git a/src/esengine/editor/EditorApplication.cpp b/src/esengine/editor/EditorApplication.cppindex cea817f6e..4d5f6da4d 100644--- a/src/esengine/editor/EditorApplication.cpp+++ b/src/esengine/editor/EditorApplication.cpp@@ -126,6 +126,18 @@ void EditorApplication::onInit() {}});+ // Wire up mouse button events (for right-click context menus, etc.)+ getPlatform().setMouseButtonCallback([this](MouseButton button, bool pressed, f32 x, f32 y) {+ if (!uiContext_) return;++ auto uiButton = static_cast<ui::MouseButton>(static_cast<u8>(button));+ if (pressed) {+ uiContext_->processMouseDown(uiButton, x, y);+ } else {+ uiContext_->processMouseUp(uiButton, x, y);+ }+ });+// Wire up text input events to UI systemgetPlatform().setTextInputCallback([this](const std::string& text) {if (uiContext_) {@@ -278,19 +290,13 @@ void EditorApplication::handleRedo() {}void EditorApplication::onTouch(TouchType type, const TouchPoint& point) {- if (!uiContext_) {- return;- }-glm::vec2 pos(point.x, point.y);switch (type) {case TouchType::Begin:- uiContext_->processMouseDown(ui::MouseButton::Left, point.x, point.y);break;case TouchType::Move:- uiContext_->processMouseMove(point.x, point.y);dragDropManager_.updateDrag(pos);break;@@ -299,7 +305,6 @@ void EditorApplication::onTouch(TouchType type, const TouchPoint& point) {if (dragDropManager_.isDragging()) {dragDropManager_.endDrag(pos);}- uiContext_->processMouseUp(ui::MouseButton::Left, point.x, point.y);break;}}diff --git a/src/esengine/editor/panels/HierarchyPanel.cpp b/src/esengine/editor/panels/HierarchyPanel.cppindex 948173030..42e1d95fa 100644--- a/src/esengine/editor/panels/HierarchyPanel.cpp+++ b/src/esengine/editor/panels/HierarchyPanel.cpp@@ -266,13 +266,7 @@ void HierarchyPanel::onSearchTextChanged(const std::string& text) {// =============================================================================void HierarchyPanel::render(ui::UIBatchRenderer& renderer) {- if (rootPanel_) {- if (rootPanel_->getContext() != getContext()) {- rootPanel_->setContext(getContext());- }- rootPanel_->layout(getBounds());- rootPanel_->renderTree(renderer);- }+ DockPanel::render(renderer);}bool HierarchyPanel::onMouseDown(const ui::MouseButtonEvent& event) {@@ -507,8 +501,11 @@ void HierarchyPanel::onNodeDoubleClicked(ui::TreeNodeId nodeId) {}void HierarchyPanel::onNodeRightClicked(ui::TreeNodeId nodeId, f32 x, f32 y) {+ ES_LOG_DEBUG("HierarchyPanel::onNodeRightClicked nodeId={} x={} y={}", nodeId, x, y);+Entity entity = getEntityForNode(nodeId);if (entity == INVALID_ENTITY) {+ ES_LOG_DEBUG(" -> entity not found for node");return;}diff --git a/src/esengine/platform/Platform.hpp b/src/esengine/platform/Platform.hppindex 397a22aa0..01998d691 100644--- a/src/esengine/platform/Platform.hpp+++ b/src/esengine/platform/Platform.hpp@@ -146,6 +146,24 @@ using TextInputCallback = std::function<void(const std::string& text)>;*/using MouseMoveCallback = std::function<void(f32 x, f32 y)>;+/**+ * @brief Mouse button identifiers+ */+enum class MouseButton : u8 {+ Left = 0,+ Right = 1,+ Middle = 2+};++/**+ * @brief Callback type for mouse button events+ * @param button The mouse button+ * @param pressed True if pressed, false if released+ * @param x Mouse X position+ * @param y Mouse Y position+ */+using MouseButtonCallback = std::function<void(MouseButton button, bool pressed, f32 x, f32 y)>;+// =============================================================================// Platform Class// =============================================================================@@ -315,6 +333,12 @@ public:*/virtual void setMouseMoveCallback(MouseMoveCallback callback) = 0;+ /**+ * @brief Sets the mouse button callback+ * @param callback Function to call on mouse button events+ */+ virtual void setMouseButtonCallback(MouseButtonCallback callback) = 0;+// =========================================================================// Factory// =========================================================================diff --git a/src/esengine/platform/native/NativePlatform.cpp b/src/esengine/platform/native/NativePlatform.cppindex ed55fdf0a..96b617314 100644--- a/src/esengine/platform/native/NativePlatform.cpp+++ b/src/esengine/platform/native/NativePlatform.cpp@@ -268,6 +268,10 @@ void NativePlatform::setMouseMoveCallback(MouseMoveCallback callback) {mouseMoveCallback_ = std::move(callback);}+void NativePlatform::setMouseButtonCallback(MouseButtonCallback callback) {+ mouseButtonCallback_ = std::move(callback);+}+// =============================================================================// GLFW Callbacks// =============================================================================@@ -296,19 +300,34 @@ void NativePlatform::glfwMouseButtonCallback(GLFWwindow* window, int button,(void)mods;auto* platform = static_cast<NativePlatform*>(glfwGetWindowUserPointer(window));- if (!platform || !platform->touchCallback_) return;+ if (!platform) return;- // Only handle left mouse button (emulate as touch)- if (button == GLFW_MOUSE_BUTTON_LEFT) {+ f32 x = static_cast<f32>(platform->mouseX_);+ f32 y = static_cast<f32>(platform->mouseY_);+ bool pressed = (action == GLFW_PRESS);++ MouseButton mouseBtn;+ switch (button) {+ case GLFW_MOUSE_BUTTON_LEFT: mouseBtn = MouseButton::Left; break;+ case GLFW_MOUSE_BUTTON_RIGHT: mouseBtn = MouseButton::Right; break;+ case GLFW_MOUSE_BUTTON_MIDDLE: mouseBtn = MouseButton::Middle; break;+ default: return;+ }++ if (platform->mouseButtonCallback_) {+ platform->mouseButtonCallback_(mouseBtn, pressed, x, y);+ }++ if (button == GLFW_MOUSE_BUTTON_LEFT && platform->touchCallback_) {TouchPoint point;- point.id = 0; // Mouse is always touch ID 0- point.x = static_cast<f32>(platform->mouseX_);- point.y = static_cast<f32>(platform->mouseY_);+ point.id = 0;+ point.x = x;+ point.y = y;- if (action == GLFW_PRESS) {+ if (pressed) {platform->mousePressed_ = true;platform->touchCallback_(TouchType::Begin, point);- } else if (action == GLFW_RELEASE) {+ } else {platform->mousePressed_ = false;platform->touchCallback_(TouchType::End, point);}diff --git a/src/esengine/platform/native/NativePlatform.hpp b/src/esengine/platform/native/NativePlatform.hppindex 7d39dcb14..0e290b343 100644--- a/src/esengine/platform/native/NativePlatform.hpp+++ b/src/esengine/platform/native/NativePlatform.hpp@@ -181,6 +181,12 @@ public:*/void setMouseMoveCallback(MouseMoveCallback callback) override;+ /**+ * @brief Sets mouse button callback+ * @param callback Function to call on mouse button events+ */+ void setMouseButtonCallback(MouseButtonCallback callback) override;+private:// =========================================================================// GLFW Callbacks@@ -224,14 +230,15 @@ private:f64 mouseX_ = 0.0; ///< Current mouse X positionf64 mouseY_ = 0.0; ///< Current mouse Y position- TouchCallback touchCallback_; ///< Touch/mouse event callback- KeyCallback keyCallback_; ///< Keyboard event callback- ResizeCallback resizeCallback_; ///< Window resize callback- ScrollCallback scrollCallback_; ///< Scroll event callback- TextInputCallback textInputCallback_; ///< Text input callback- MouseMoveCallback mouseMoveCallback_; ///< Mouse move callback+ TouchCallback touchCallback_; ///< Touch/mouse event callback+ KeyCallback keyCallback_; ///< Keyboard event callback+ ResizeCallback resizeCallback_; ///< Window resize callback+ ScrollCallback scrollCallback_; ///< Scroll event callback+ TextInputCallback textInputCallback_; ///< Text input callback+ MouseMoveCallback mouseMoveCallback_; ///< Mouse move callback+ MouseButtonCallback mouseButtonCallback_; ///< Mouse button callback- static NativePlatform* instance_; ///< Singleton instance for callbacks+ static NativePlatform* instance_; ///< Singleton instance for callbacks};} // namespace esenginediff --git a/src/esengine/ui/docking/DockArea.cpp b/src/esengine/ui/docking/DockArea.cppindex 2b3fda771..5c388f179 100644--- a/src/esengine/ui/docking/DockArea.cpp+++ b/src/esengine/ui/docking/DockArea.cpp@@ -291,14 +291,30 @@ glm::vec2 DockArea::measure(f32 availableWidth, f32 availableHeight) {return {availableWidth, availableHeight};}+void DockArea::layout(const Rect& bounds) {+ Widget::layout(bounds);++ if (rootNode_) {+ rootNode_->setBounds(getBounds());+ rootNode_->layout(splitterThickness_, tabBarHeight_);++ // Propagate context to all panels+ UIContext* ctx = getContext();+ rootNode_->forEachLeaf([ctx](DockNode& node) {+ for (const auto& panel : node.getPanels()) {+ if (panel && panel->getContext() != ctx) {+ panel->setContext(ctx);+ }+ }+ });+ }+}+void DockArea::render(UIBatchRenderer& renderer) {if (!rootNode_) {return;}- rootNode_->setBounds(getBounds());- rootNode_->layout(splitterThickness_, tabBarHeight_);-renderNode(renderer, rootNode_.get());zoneDetector_.render(renderer);@@ -530,6 +546,7 @@ void DockArea::handleSplitterDrag(f32 x, f32 y) {}draggedSplitter_->setSplitRatio(newRatio);+ invalidateLayout();}void DockArea::setNodeArea(DockNode* node) {diff --git a/src/esengine/ui/docking/DockArea.hpp b/src/esengine/ui/docking/DockArea.hppindex 6bf2f6d9c..ea432fe1a 100644--- a/src/esengine/ui/docking/DockArea.hpp+++ b/src/esengine/ui/docking/DockArea.hpp@@ -205,6 +205,7 @@ public:// =========================================================================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;diff --git a/src/esengine/ui/docking/DockPanel.cpp b/src/esengine/ui/docking/DockPanel.cppindex 5057e083e..1b4b67321 100644--- a/src/esengine/ui/docking/DockPanel.cpp+++ b/src/esengine/ui/docking/DockPanel.cpp@@ -92,6 +92,14 @@ glm::vec2 DockPanel::measure(f32 availableWidth, f32 availableHeight) {};}+void DockPanel::layout(const Rect& bounds) {+ Widget::layout(bounds);++ if (contentWidget_) {+ contentWidget_->layout(getContentBounds());+ }+}+void DockPanel::render(UIBatchRenderer& renderer) {UIContext* ctx = getContext();if (!ctx) return;diff --git a/src/esengine/ui/docking/DockPanel.hpp b/src/esengine/ui/docking/DockPanel.hppindex 7747fe51b..ad3d97fa4 100644--- a/src/esengine/ui/docking/DockPanel.hpp+++ b/src/esengine/ui/docking/DockPanel.hpp@@ -150,6 +150,7 @@ public:// =========================================================================glm::vec2 measure(f32 availableWidth, f32 availableHeight) override;+ void layout(const Rect& bounds) override;void render(UIBatchRenderer& renderer) override;protected:diff --git a/src/esengine/ui/widgets/ContextMenu.cpp b/src/esengine/ui/widgets/ContextMenu.cppindex 0e5914473..9582b7c4e 100644--- a/src/esengine/ui/widgets/ContextMenu.cpp+++ b/src/esengine/ui/widgets/ContextMenu.cpp@@ -195,7 +195,20 @@ bool ContextMenu::onMouseDown(const MouseButtonEvent& event) {if (!isOpen_) return false;glm::vec2 size = measure(0.0f, 0.0f);- Rect menuBounds{menuX_, menuY_, size.x, size.y};++ UIContext* ctx = getContext();+ glm::vec2 viewportSize = ctx ? ctx->getViewportSize() : glm::vec2(9999.0f);++ f32 x = menuX_;+ f32 y = menuY_;+ if (x + size.x > viewportSize.x) {+ x = viewportSize.x - size.x - 4.0f;+ }+ if (y + size.y > viewportSize.y) {+ y = viewportSize.y - size.y - 4.0f;+ }++ Rect menuBounds{x, y, size.x, size.y};if (!menuBounds.contains(event.x, event.y)) {hide();@@ -227,7 +240,12 @@ bool ContextMenu::onMouseMove(const MouseMoveEvent& event) {y = viewportSize.y - size.y - 4.0f;}- hoveredIndex_ = getItemAtY(event.y - y);+ Rect menuBounds{x, y, size.x, size.y};+ if (menuBounds.contains(event.x, event.y)) {+ hoveredIndex_ = getItemAtY(event.y - y);+ } else {+ hoveredIndex_ = -1;+ }return false;}@@ -271,27 +289,10 @@ bool ContextMenu::onKeyDown(const KeyEvent& event) {}Widget* ContextMenu::hitTest(f32 x, f32 y) {- if (!isOpen_) return nullptr;-- glm::vec2 size = measure(0.0f, 0.0f);-- UIContext* ctx = getContext();- glm::vec2 viewportSize = ctx ? ctx->getViewportSize() : glm::vec2(9999.0f);-- f32 menuX = menuX_;- f32 menuY = menuY_;- if (menuX + size.x > viewportSize.x) {- menuX = viewportSize.x - size.x - 4.0f;- }- if (menuY + size.y > viewportSize.y) {- menuY = viewportSize.y - size.y - 4.0f;- }+ (void)x;+ (void)y;- Rect menuBounds{menuX, menuY, size.x, size.y};-- if (menuBounds.contains(x, y)) {- return this;- }+ if (!isOpen_) return nullptr;return this;}diff --git a/src/esengine/ui/widgets/ScrollView.cpp b/src/esengine/ui/widgets/ScrollView.cppindex ddea1cfdd..cf312da3f 100644--- a/src/esengine/ui/widgets/ScrollView.cpp+++ b/src/esengine/ui/widgets/ScrollView.cpp@@ -190,10 +190,25 @@ void ScrollView::render(UIBatchRenderer& renderer) {viewportSize_.y};+ // Save original content bounds for hitTest (not offset)+ Rect originalBounds = content_->getBounds();++ // Temporarily offset content bounds for rendering+ Rect renderBounds{+ originalBounds.x - scrollOffset_.x,+ originalBounds.y - scrollOffset_.y,+ originalBounds.width,+ originalBounds.height+ };+ content_->layout(renderBounds);+renderer.pushClipRect(viewportRect);content_->renderTree(renderer);renderer.popClipRect();+ // Restore original bounds for hitTest+ content_->layout(originalBounds);+if (showScrollbars_) {if (canScrollVertically()) {renderVerticalScrollbar(renderer);@@ -226,9 +241,29 @@ Widget* ScrollView::hitTest(f32 x, f32 y) {}if (content_) {- Widget* hit = content_->hitTest(x, y);- if (hit) {- return hit;+ // Content bounds are not offset (offset is applied only during render).+ // We need to check if the click is within the viewport, then transform+ // coordinates to content's local coordinate system.+ const Rect& bounds = getBounds();+ const Insets& padding = getPadding();++ Rect viewportRect{+ bounds.x + padding.left,+ bounds.y + padding.top,+ viewportSize_.x,+ viewportSize_.y+ };++ if (viewportRect.contains(x, y)) {+ // Transform screen coordinates to content coordinates+ // by adding scroll offset+ f32 contentX = x + scrollOffset_.x;+ f32 contentY = y + scrollOffset_.y;++ Widget* hit = content_->hitTest(contentX, contentY);+ if (hit) {+ return hit;+ }}}@@ -374,9 +409,11 @@ void ScrollView::updateContentLayout() {contentHeight = viewportSize_.y;}+ // Content bounds are NOT offset by scrollOffset_ for correct hitTest.+ // The scroll offset is applied during rendering only.Rect contentBounds{- bounds.x + padding.left - scrollOffset_.x,- bounds.y + padding.top - scrollOffset_.y,+ bounds.x + padding.left,+ bounds.y + padding.top,contentWidth,contentHeight};