Skip to content

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.

fix: Resolve editor rendering and input coordinate mismatch

esenginef130860ab6ba
diff --git a/src/esengine/editor/EditorApplication.cpp b/src/esengine/editor/EditorApplication.cpp
index 205e84e3e..0ea9f8895 100644
--- a/src/esengine/editor/EditorApplication.cpp
+++ b/src/esengine/editor/EditorApplication.cpp
@@ -57,6 +57,7 @@ void EditorApplication::onInit() {
uiContext_ = makeUnique<ui::UIContext>(getRenderContext(), dispatcher_);
uiContext_->init();
uiContext_->setViewport(getWidth(), getHeight());
+ uiContext_->setDevicePixelRatio(getPlatform().getDevicePixelRatio());
// Load default font (try Windows system fonts)
const char* fontPaths[] = {
@@ -183,6 +184,7 @@ void EditorApplication::onResize(u32 width, u32 height) {
if (uiContext_) {
uiContext_->setViewport(width, height);
+ uiContext_->setDevicePixelRatio(getPlatform().getDevicePixelRatio());
}
}
diff --git a/src/esengine/editor/panels/SceneViewPanel.cpp b/src/esengine/editor/panels/SceneViewPanel.cpp
index 0558f825a..848328066 100644
--- a/src/esengine/editor/panels/SceneViewPanel.cpp
+++ b/src/esengine/editor/panels/SceneViewPanel.cpp
@@ -15,6 +15,10 @@
#include "../../ecs/components/Transform.hpp"
#include "../../ecs/components/Sprite.hpp"
#include "../../core/Log.hpp"
+#include <glad/glad.h>
+#ifndef GL_VIEWPORT
+#define GL_VIEWPORT 0x0BA2
+#endif
namespace esengine::editor {
@@ -114,6 +118,10 @@ void SceneViewPanel::renderSceneToTexture() {
return;
}
+ // Save current viewport
+ GLint savedViewport[4];
+ glGetIntegerv(GL_VIEWPORT, savedViewport);
+
framebuffer_->bind();
RenderCommand::setViewport(0, 0, viewportWidth_, viewportHeight_);
@@ -123,6 +131,9 @@ void SceneViewPanel::renderSceneToTexture() {
renderSceneContent();
framebuffer_->unbind();
+
+ // Restore viewport
+ glViewport(savedViewport[0], savedViewport[1], savedViewport[2], savedViewport[3]);
}
void SceneViewPanel::renderSceneContent() {
diff --git a/src/esengine/platform/native/NativePlatform.cpp b/src/esengine/platform/native/NativePlatform.cpp
index b6cced88f..9c93e44ce 100644
--- a/src/esengine/platform/native/NativePlatform.cpp
+++ b/src/esengine/platform/native/NativePlatform.cpp
@@ -92,9 +92,11 @@ bool NativePlatform::initialize(u32 width, u32 height) {
return false;
}
- // Store window dimensions
- windowWidth_ = width;
- windowHeight_ = height;
+ // Get actual window size (may differ from requested due to DPI scaling)
+ int actualWidth, actualHeight;
+ glfwGetWindowSize(window_, &actualWidth, &actualHeight);
+ windowWidth_ = static_cast<u32>(actualWidth);
+ windowHeight_ = static_cast<u32>(actualHeight);
// Get framebuffer size (for high-DPI)
int fbWidth, fbHeight;
@@ -102,7 +104,7 @@ bool NativePlatform::initialize(u32 width, u32 height) {
framebufferWidth_ = static_cast<u32>(fbWidth);
framebufferHeight_ = static_cast<u32>(fbHeight);
- ES_LOG_DEBUG("Window size: {}x{}, Framebuffer size: {}x{}",
+ ES_LOG_DEBUG("Window: {}x{}, Framebuffer: {}x{}",
windowWidth_, windowHeight_, framebufferWidth_, framebufferHeight_);
// Make OpenGL context current
diff --git a/src/esengine/ui/UIContext.cpp b/src/esengine/ui/UIContext.cpp
index c546bf5e6..9119abb87 100644
--- a/src/esengine/ui/UIContext.cpp
+++ b/src/esengine/ui/UIContext.cpp
@@ -153,7 +153,10 @@ void UIContext::render() {
return;
}
- RenderCommand::setViewport(0, 0, viewportWidth_, viewportHeight_);
+ u32 physicalWidth = static_cast<u32>(viewportWidth_ * devicePixelRatio_);
+ u32 physicalHeight = static_cast<u32>(viewportHeight_ * devicePixelRatio_);
+
+ RenderCommand::setViewport(0, 0, physicalWidth, physicalHeight);
glm::mat4 projection = glm::ortho(0.0f, static_cast<f32>(viewportWidth_),
static_cast<f32>(viewportHeight_), 0.0f, -1.0f, 1.0f);
@@ -174,6 +177,10 @@ void UIContext::setViewport(u32 width, u32 height) {
}
}
+void UIContext::setDevicePixelRatio(f32 ratio) {
+ devicePixelRatio_ = ratio > 0.0f ? ratio : 1.0f;
+}
+
void UIContext::doLayout() {
if (!root_) return;
diff --git a/src/esengine/ui/UIContext.hpp b/src/esengine/ui/UIContext.hpp
index 34fa6461b..32edfd924 100644
--- a/src/esengine/ui/UIContext.hpp
+++ b/src/esengine/ui/UIContext.hpp
@@ -180,16 +180,32 @@ public:
/**
* @brief Sets the viewport size
- * @param width Viewport width in pixels
- * @param height Viewport height in pixels
+ * @param width Viewport width in logical pixels
+ * @param height Viewport height in logical pixels
*/
void setViewport(u32 width, u32 height);
+ /**
+ * @brief Sets the device pixel ratio for high-DPI displays
+ * @param ratio Pixel ratio (1.0 for standard, 2.0 for retina, etc.)
+ */
+ void setDevicePixelRatio(f32 ratio);
+
+ /**
+ * @brief Gets the device pixel ratio
+ */
+ f32 getDevicePixelRatio() const { return devicePixelRatio_; }
+
/**
* @brief Gets the viewport size
*/
glm::vec2 getViewportSize() const { return {viewportWidth_, viewportHeight_}; }
+ /**
+ * @brief Gets the current mouse position
+ */
+ glm::vec2 getMousePosition() const { return {lastMouseX_, lastMouseY_}; }
+
// =========================================================================
// Input Processing
// =========================================================================
@@ -321,6 +337,7 @@ private:
u32 viewportWidth_ = 0;
u32 viewportHeight_ = 0;
+ f32 devicePixelRatio_ = 1.0f;
Widget* focusedWidget_ = nullptr;
Widget* hoveredWidget_ = nullptr;
diff --git a/src/esengine/ui/docking/DockArea.cpp b/src/esengine/ui/docking/DockArea.cpp
index 76c8028bd..d25a4ade7 100644
--- a/src/esengine/ui/docking/DockArea.cpp
+++ b/src/esengine/ui/docking/DockArea.cpp
@@ -294,6 +294,14 @@ void DockArea::render(UIBatchRenderer& renderer) {
zoneDetector_.render(renderer);
}
+Widget* DockArea::hitTest(f32 x, f32 y) {
+ if (hitTestSplitter(x, y)) {
+ return this;
+ }
+
+ return Widget::hitTest(x, y);
+}
+
void DockArea::renderNode(UIBatchRenderer& renderer, DockNode* node) {
if (!node) return;
@@ -321,8 +329,9 @@ void DockArea::renderSplitter(UIBatchRenderer& renderer, DockNode* node) {
if (!ctx) return;
Rect splitterBounds = node->getSplitterBounds(splitterThickness_);
+ glm::vec2 mousePos = ctx->getMousePosition();
bool hovered = (draggedSplitter_ == node) ||
- splitterBounds.contains({lastMouseX_, lastMouseY_});
+ splitterBounds.contains({mousePos.x, mousePos.y});
glm::vec4 color = hovered
? ctx->getTheme().colors.accent
diff --git a/src/esengine/ui/docking/DockArea.hpp b/src/esengine/ui/docking/DockArea.hpp
index 38799d08c..6bf2f6d9c 100644
--- a/src/esengine/ui/docking/DockArea.hpp
+++ b/src/esengine/ui/docking/DockArea.hpp
@@ -207,6 +207,7 @@ public:
glm::vec2 measure(f32 availableWidth, f32 availableHeight) override;
void render(UIBatchRenderer& renderer) override;
+ Widget* hitTest(f32 x, f32 y) override;
bool onMouseDown(const MouseButtonEvent& event) override;
bool onMouseUp(const MouseButtonEvent& event) override;
bool onMouseMove(const MouseMoveEvent& event) override;