VisuTwin Canvas
C++ 3D Engine — Metal Backend
Loading...
Searching...
No Matches
transformGizmo.h
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2025-2026 Arnis Lektauers
3#pragma once
4
5#include <algorithm>
6#include <array>
7#include <memory>
8#include <vector>
9
10#include <SDL3/SDL_events.h>
11
12#include "core/math/color.h"
15#include "framework/entity.h"
16
17namespace visutwin::canvas
18{
19 class Engine;
20 class RenderComponent;
21 class StandardMaterial;
22
24 {
25 public:
26 enum class Mode
27 {
31 };
32
33 enum class Axis
34 {
40 };
41
42 TransformGizmo(Engine* engine, CameraComponent* camera);
43 ~TransformGizmo() = default;
44
45 void attach(Entity* target);
46
47 void setMode(Mode mode);
48 Mode mode() const { return _mode; }
49
50 void setSnap(bool enabled) { _snap = enabled; }
51 bool snap() const { return _snap; }
52
53 void setTranslateSnapIncrement(float value) { _translateSnapIncrement = std::max(0.001f, value); }
54 void setRotateSnapIncrement(float value) { _rotateSnapIncrement = std::max(0.1f, value); }
55 void setScaleSnapIncrement(float value) { _scaleSnapIncrement = std::max(0.001f, value); }
56
57 bool handleEvent(const SDL_Event& event, int windowWidth, int windowHeight);
58 void update();
59
60 private:
61 struct Handle
62 {
63 Axis axis = Axis::None;
64 Entity* entity = nullptr;
65 RenderComponent* render = nullptr;
66 StandardMaterial* material = nullptr;
67 Color baseColor = Color(1.0f, 1.0f, 1.0f, 1.0f);
68 Vector3 worldPosition = Vector3(0.0f, 0.0f, 0.0f);
69 };
70
71 Entity* createHandleEntity(const char* primitiveType, const Color& color);
72 void updateHandleTransforms();
73 void updateHandleColors();
74
75 Axis pickAxis(float mouseX, float mouseY) const;
76 bool worldToScreen(const Vector3& world, float& outX, float& outY) const;
77 Vector3 axisDirection(Axis axis) const;
78 Vector3 cameraRight() const;
79 Vector3 cameraUp() const;
80 Vector3 cameraForward() const;
81
82 void beginDrag(Axis axis, float mouseX, float mouseY);
83 void applyDrag(float mouseX, float mouseY);
84 void endDrag();
85
86 float unitsPerPixelAtTarget() const;
87
88 Engine* _engine = nullptr;
89 CameraComponent* _camera = nullptr;
90 Entity* _target = nullptr;
91
92 Entity* _root = nullptr;
93 Handle _handleX;
94 Handle _handleY;
95 Handle _handleZ;
96 Handle _handleCenter;
97 Handle _shaftX;
98 Handle _shaftY;
99 Handle _shaftZ;
100
101 std::vector<std::shared_ptr<StandardMaterial>> _materials;
102
103 Mode _mode = Mode::Translate;
104 Axis _hoveredAxis = Axis::None;
105 Axis _activeAxis = Axis::None;
106 bool _dragging = false;
107
108 float _windowWidth = 1.0f;
109 float _windowHeight = 1.0f;
110
111 float _dragStartMouseX = 0.0f;
112 float _dragStartMouseY = 0.0f;
113
114 Vector3 _targetStartPosition = Vector3(0.0f, 0.0f, 0.0f);
115 Quaternion _targetStartRotation;
116 Vector3 _targetStartScale = Vector3(1.0f, 1.0f, 1.0f);
117
118 float _gizmoSize = 1.8f;
119
120 bool _snap = false;
121 float _translateSnapIncrement = 0.5f;
122 float _rotateSnapIncrement = 15.0f;
123 float _scaleSnapIncrement = 0.1f;
124 };
125}
Central application orchestrator managing scenes, rendering, input, and resource loading.
Definition engine.h:38
ECS entity — a GraphNode that hosts components defining its behavior.
Definition entity.h:32
Full PBR material with metalness/roughness workflow and advanced surface features.
void setRotateSnapIncrement(float value)
void setScaleSnapIncrement(float value)
TransformGizmo(Engine *engine, CameraComponent *camera)
bool handleEvent(const SDL_Event &event, int windowWidth, int windowHeight)
void setTranslateSnapIncrement(float value)
RGBA color with floating-point components in [0, 1].
Definition color.h:18
3D vector for positions, directions, and normals with multi-backend SIMD acceleration.
Definition vector3.h:29