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.

feat: Add EditorCamera with orbit, pan, and zoom controls

esengine0e07c1290637
diff --git a/src/esengine/editor/CMakeLists.txt b/src/esengine/editor/CMakeLists.txt
index d34486ed6..3ec44b89a 100644
--- a/src/esengine/editor/CMakeLists.txt
+++ b/src/esengine/editor/CMakeLists.txt
@@ -9,6 +9,9 @@ set(EDITOR_SOURCES
property/editors/FloatEditor.cpp
property/editors/Vector3Editor.cpp
+ # Camera
+ camera/EditorCamera.cpp
+
# Panels
panels/HierarchyPanel.cpp
panels/InspectorPanel.cpp
@@ -33,6 +36,9 @@ set(EDITOR_HEADERS
property/editors/FloatEditor.hpp
property/editors/Vector3Editor.hpp
+ # Camera
+ camera/EditorCamera.hpp
+
# Panels
panels/HierarchyPanel.hpp
panels/InspectorPanel.hpp
diff --git a/src/esengine/editor/camera/EditorCamera.cpp b/src/esengine/editor/camera/EditorCamera.cpp
new file mode 100644
index 000000000..b0e7de558
--- /dev/null
+++ b/src/esengine/editor/camera/EditorCamera.cpp
@@ -0,0 +1,177 @@
+/**
+ * @file EditorCamera.cpp
+ * @brief Editor camera implementation
+ *
+ * @author ESEngine Team
+ * @date 2026
+ *
+ * @copyright Copyright (c) 2026 ESEngine Team
+ * Licensed under the MIT License.
+ */
+
+#include "EditorCamera.hpp"
+#include "../../platform/Platform.hpp"
+
+namespace esengine::editor {
+
+// =============================================================================
+// Constructor
+// =============================================================================
+
+EditorCamera::EditorCamera() {
+ updateView();
+}
+
+// =============================================================================
+// Configuration
+// =============================================================================
+
+void EditorCamera::setViewportSize(f32 width, f32 height) {
+ viewportWidth_ = width;
+ viewportHeight_ = height;
+ aspectRatio_ = width / height;
+}
+
+void EditorCamera::setFocalPoint(const glm::vec3& point) {
+ focalPoint_ = point;
+ updateView();
+}
+
+void EditorCamera::setDistance(f32 distance) {
+ distance_ = glm::clamp(distance, 0.1f, 1000.0f);
+ updateView();
+}
+
+void EditorCamera::setFieldOfView(f32 fov) {
+ fov_ = glm::clamp(fov, 10.0f, 120.0f);
+}
+
+void EditorCamera::setClipPlanes(f32 near, f32 far) {
+ nearClip_ = near;
+ farClip_ = far;
+}
+
+// =============================================================================
+// Input Handling
+// =============================================================================
+
+bool EditorCamera::onMouseDown(const ui::MouseButtonEvent& event) {
+ lastMousePos_ = glm::vec2(event.x, event.y);
+
+ if (event.button == ui::MouseButton::Left && event.alt) {
+ isOrbiting_ = true;
+ return true;
+ }
+
+ if (event.button == ui::MouseButton::Middle ||
+ (event.button == ui::MouseButton::Left && event.shift)) {
+ isPanning_ = true;
+ return true;
+ }
+
+ return false;
+}
+
+bool EditorCamera::onMouseUp(const ui::MouseButtonEvent& event) {
+ if (event.button == ui::MouseButton::Left) {
+ isOrbiting_ = false;
+ }
+ if (event.button == ui::MouseButton::Middle) {
+ isPanning_ = false;
+ }
+
+ return isOrbiting_ || isPanning_;
+}
+
+bool EditorCamera::onMouseMove(const ui::MouseMoveEvent& event) {
+ glm::vec2 mousePos(event.x, event.y);
+ glm::vec2 delta = mousePos - lastMousePos_;
+ lastMousePos_ = mousePos;
+
+ if (isOrbiting_) {
+ mouseRotate(delta.x, delta.y);
+ return true;
+ }
+
+ if (isPanning_) {
+ mousePan(delta.x, delta.y);
+ return true;
+ }
+
+ return false;
+}
+
+bool EditorCamera::onMouseScroll(const ui::ScrollEvent& event) {
+ mouseZoom(event.deltaY);
+ return true;
+}
+
+// =============================================================================
+// Matrix Access
+// =============================================================================
+
+glm::mat4 EditorCamera::getViewMatrix() const {
+ return viewMatrix_;
+}
+
+glm::mat4 EditorCamera::getProjectionMatrix() const {
+ return math::perspective(math::toRadians(fov_), aspectRatio_, nearClip_, farClip_);
+}
+
+glm::mat4 EditorCamera::getViewProjectionMatrix() const {
+ return getProjectionMatrix() * getViewMatrix();
+}
+
+glm::vec3 EditorCamera::getPosition() const {
+ return focalPoint_ - getForwardDirection() * distance_;
+}
+
+glm::vec3 EditorCamera::getForwardDirection() const {
+ return glm::rotate(getOrientation(), glm::vec3(0.0f, 0.0f, -1.0f));
+}
+
+glm::vec3 EditorCamera::getUpDirection() const {
+ return glm::rotate(getOrientation(), glm::vec3(0.0f, 1.0f, 0.0f));
+}
+
+glm::vec3 EditorCamera::getRightDirection() const {
+ return glm::rotate(getOrientation(), glm::vec3(1.0f, 0.0f, 0.0f));
+}
+
+// =============================================================================
+// Private Methods
+// =============================================================================
+
+void EditorCamera::updateView() {
+ glm::vec3 position = getPosition();
+ viewMatrix_ = glm::lookAt(position, focalPoint_, glm::vec3(0.0f, 1.0f, 0.0f));
+}
+
+glm::quat EditorCamera::getOrientation() const {
+ return glm::quat(glm::vec3(-pitch_, -yaw_, 0.0f));
+}
+
+void EditorCamera::mousePan(f32 deltaX, f32 deltaY) {
+ f32 speed = 0.001f * distance_;
+ focalPoint_ += getRightDirection() * (-deltaX * speed);
+ focalPoint_ += getUpDirection() * (deltaY * speed);
+ updateView();
+}
+
+void EditorCamera::mouseRotate(f32 deltaX, f32 deltaY) {
+ f32 yawSign = getUpDirection().y < 0.0f ? -1.0f : 1.0f;
+ yaw_ += yawSign * deltaX * 0.003f;
+ pitch_ += deltaY * 0.003f;
+
+ pitch_ = glm::clamp(pitch_, -glm::half_pi<f32>() + 0.01f, glm::half_pi<f32>() - 0.01f);
+
+ updateView();
+}
+
+void EditorCamera::mouseZoom(f32 delta) {
+ distance_ -= delta * 0.5f;
+ distance_ = glm::clamp(distance_, 0.1f, 1000.0f);
+ updateView();
+}
+
+} // namespace esengine::editor
diff --git a/src/esengine/editor/camera/EditorCamera.hpp b/src/esengine/editor/camera/EditorCamera.hpp
new file mode 100644
index 000000000..1343d0f0f
--- /dev/null
+++ b/src/esengine/editor/camera/EditorCamera.hpp
@@ -0,0 +1,186 @@
+/**
+ * @file EditorCamera.hpp
+ * @brief Editor camera with orbit, pan, and zoom controls
+ * @details Provides intuitive camera controls for 3D scene editing including
+ * mouse orbit, pan, and scroll wheel zoom.
+ *
+ * @author ESEngine Team
+ * @date 2026
+ *
+ * @copyright Copyright (c) 2026 ESEngine Team
+ * Licensed under the MIT License.
+ */
+#pragma once
+
+// =============================================================================
+// Includes
+// =============================================================================
+
+#include "../../core/Types.hpp"
+#include "../../math/Math.hpp"
+#include "../../ui/events/UIEvent.hpp"
+
+namespace esengine::editor {
+
+// =============================================================================
+// EditorCamera Class
+// =============================================================================
+
+/**
+ * @brief 3D camera for scene editing
+ *
+ * @details Implements orbit camera controls common in 3D editors:
+ * - Alt+LMB: Orbit around focal point
+ * - Alt+MMB / MMB: Pan camera
+ * - Scroll wheel: Zoom in/out
+ *
+ * @code
+ * EditorCamera camera;
+ * camera.setViewportSize(800, 600);
+ * camera.setFocalPoint(glm::vec3(0.0f));
+ * camera.setDistance(10.0f);
+ *
+ * // Handle mouse events
+ * camera.onMouseDown(mouseEvent);
+ * camera.onMouseMove(mouseEvent);
+ * camera.onMouseScroll(scrollEvent);
+ *
+ * // Get matrices for rendering
+ * glm::mat4 view = camera.getViewMatrix();
+ * glm::mat4 proj = camera.getProjectionMatrix();
+ * @endcode
+ */
+class EditorCamera {
+public:
+ EditorCamera();
+ ~EditorCamera() = default;
+
+ // =========================================================================
+ // Configuration
+ // =========================================================================
+
+ /**
+ * @brief Sets the viewport dimensions
+ * @param width Viewport width in pixels
+ * @param height Viewport height in pixels
+ */
+ void setViewportSize(f32 width, f32 height);
+
+ /**
+ * @brief Sets the camera focal point
+ * @param point World position to orbit around
+ */
+ void setFocalPoint(const glm::vec3& point);
+
+ /**
+ * @brief Sets the distance from focal point
+ * @param distance Distance in world units
+ */
+ void setDistance(f32 distance);
+
+ /**
+ * @brief Sets the field of view
+ * @param fov Field of view in degrees
+ */
+ void setFieldOfView(f32 fov);
+
+ /**
+ * @brief Sets near and far clip planes
+ * @param near Near plane distance
+ * @param far Far plane distance
+ */
+ void setClipPlanes(f32 near, f32 far);
+
+ // =========================================================================
+ // Input Handling
+ // =========================================================================
+
+ /**
+ * @brief Handles mouse button down events
+ * @param event Mouse button event
+ * @return True if event was handled
+ */
+ bool onMouseDown(const ui::MouseButtonEvent& event);
+
+ /**
+ * @brief Handles mouse button up events
+ * @param event Mouse button event
+ * @return True if event was handled
+ */
+ bool onMouseUp(const ui::MouseButtonEvent& event);
+
+ /**
+ * @brief Handles mouse move events
+ * @param event Mouse move event
+ * @return True if event was handled
+ */
+ bool onMouseMove(const ui::MouseMoveEvent& event);
+
+ /**
+ * @brief Handles mouse scroll events
+ * @param event Mouse scroll event
+ * @return True if event was handled
+ */
+ bool onMouseScroll(const ui::ScrollEvent& event);
+
+ // =========================================================================
+ // Matrix Access
+ // =========================================================================
+
+ /** @brief Gets the view matrix */
+ glm::mat4 getViewMatrix() const;
+
+ /** @brief Gets the projection matrix */
+ glm::mat4 getProjectionMatrix() const;
+
+ /** @brief Gets the view-projection matrix */
+ glm::mat4 getViewProjectionMatrix() const;
+
+ /** @brief Gets the camera position in world space */
+ glm::vec3 getPosition() const;
+
+ /** @brief Gets the camera forward direction */
+ glm::vec3 getForwardDirection() const;
+
+ /** @brief Gets the camera up direction */
+ glm::vec3 getUpDirection() const;
+
+ /** @brief Gets the camera right direction */
+ glm::vec3 getRightDirection() const;
+
+ /** @brief Gets the focal point */
+ const glm::vec3& getFocalPoint() const { return focalPoint_; }
+
+ /** @brief Gets the distance from focal point */
+ f32 getDistance() const { return distance_; }
+
+private:
+ void updateView();
+ glm::quat getOrientation() const;
+
+ void mousePan(f32 deltaX, f32 deltaY);
+ void mouseRotate(f32 deltaX, f32 deltaY);
+ void mouseZoom(f32 delta);
+
+ glm::vec3 focalPoint_{0.0f};
+ f32 distance_ = 10.0f;
+ f32 pitch_ = 0.0f;
+ f32 yaw_ = 0.0f;
+
+ f32 fov_ = 45.0f;
+ f32 aspectRatio_ = 16.0f / 9.0f;
+ f32 nearClip_ = 0.1f;
+ f32 farClip_ = 1000.0f;
+
+ f32 viewportWidth_ = 1280.0f;
+ f32 viewportHeight_ = 720.0f;
+
+ glm::vec2 lastMousePos_{0.0f};
+ bool isDragging_ = false;
+ bool isPanning_ = false;
+ bool isOrbiting_ = false;
+
+ glm::mat4 viewMatrix_{1.0f};
+};
+
+} // namespace esengine::editor