Avara3D 0.2.0
C++ API reference
TurntableCameraController.h
1//
2// TurntableCameraController.h
3// avara3d
4//
5// Created by Morgan Davis on 8/9/26.
6// Copyright © 2026 Morgan K Davis. All rights reserved.
7//
8
9#ifndef AVARA3D_EXTENSION_CAMERA_TURNTABLECAMERACONTROLLER_H
10#define AVARA3D_EXTENSION_CAMERA_TURNTABLECAMERACONTROLLER_H
11
12#include <memory>
13#include <optional>
14#include <variant>
15
16#include "a3d/Math.h"
17#include "a3d/input/DesktopInputContext.h"
18
19namespace a3d {
20
21 class Node;
22
23}
24
25namespace a3d::ext {
26
37
38 public:
39 // [Public Types]
40
42 struct NodeTarget {
43
44 std::weak_ptr<Node> node;
46 };
47
49 using Target = std::variant<math::vec3, NodeTarget>;
50
52 struct View {
53
55 float yaw {0.0f};
56 float pitch {0.0f};
57 float distance {10.0f};
58 };
59
61 struct Controls {
62
63 DesktopInputContext::MouseButton orbitButton {DesktopInputContext::MouseButton::One};
64 DesktopInputContext::MouseButton panButton {DesktopInputContext::MouseButton::Two};
65 DesktopInputContext::Key panModifier {DesktopInputContext::Key::LeftShift};
66 DesktopInputContext::Key dollyModifier {DesktopInputContext::Key::LeftControl};
67 };
68
70 struct PointerClick {
71
73 };
74
76 struct Config {
77
78 float minPitch {-math::radians(85.0f)};
79 float maxPitch {math::radians(85.0f)};
80
81 float minDistance {0.01f};
82 float maxDistance {math::F32_MAX};
83
84 float orbitSensitivity {0.004f};
85 float dollySensitivity {0.01f};
86 float scrollDollySensitivity {0.15f};
87 float dragThreshold {4.0f};
88 bool invertPitch {false};
89
91 };
92
94 struct UpdateResult {
95
96 bool cameraChanged {false};
97 bool pointerDragging {false};
98 std::optional<PointerClick> orbitButtonClick;
99 std::optional<PointerClick> panButtonClick;
100 };
101
102 // [Public Lifecycle Functions]
103
106
113
114 // [Public Member Functions]
115
117 const Config& config() const;
118
124 void config(const Config& config);
125
127 const View& view() const;
128
136 void view(const View& view);
137
141 void target(const math::vec3& worldPosition);
142
150 void target(const std::shared_ptr<Node>& node);
151
159 void target(const std::shared_ptr<Node>& node, const math::vec3& localPosition);
160
176 UpdateResult update(DesktopInputContext& input, float vFov, float viewportHeight);
177
184 void apply(Node& pov);
185
186 private:
187 // [Private Types]
188
189 enum class DragMode {
190 Orbit,
191 Pan,
192 Dolly
193 };
194
195 // [Private Member Functions]
196
197 math::vec3 resolveTarget();
198 void translateTarget(const math::vec3& worldTranslation);
199
200 // [Private Member Variables]
201
202 Config _config;
203 View _view;
204 math::vec3 _resolvedTarget;
205 bool _orbitButtonActive;
206 bool _orbitButtonDragging;
207 DragMode _orbitButtonDragMode;
208 math::vec2 _orbitButtonPressPosition;
209 bool _panButtonActive;
210 bool _panButtonDragging;
211 math::vec2 _panButtonPressPosition;
212 };
213
214}
215
216#endif // AVARA3D_EXTENSION_CAMERA_TURNTABLECAMERACONTROLLER_H
Exposes keyboard, pointer-button, pointer-motion, and scroll state.
MouseButton
Identifies one of up to eight pointer buttons.
Key
Identifies keyboard keys reported by desktop-style input contexts.
A transformable object in a Scene hierarchy.
Definition: Node.h:45
Provides orbit, pan, and dolly control around a world-space or Node-relative target.
const Config & config() const
Returns the current controller configuration.
void target(const std::shared_ptr< Node > &node)
Sets the orbit target to the local origin of node.
std::variant< math::vec3, NodeTarget > Target
Orbit target represented by either a fixed world position or a Node-relative point.
const View & view() const
Returns the current orbit View.
void view(const View &view)
Replaces the orbit View, clamping pitch and distance to the configured limits.
TurntableCameraController()
Creates a controller with the default configuration and View.
void target(const std::shared_ptr< Node > &node, const math::vec3 &localPosition)
Sets the orbit target to localPosition in node coordinates.
void target(const math::vec3 &worldPosition)
Sets a fixed world-space orbit target.
void config(const Config &config)
Replaces the controller configuration and clamps the current View to its new limits.
UpdateResult update(DesktopInputContext &input, float vFov, float viewportHeight)
Updates the View from pointer-button, pointer-motion, and scroll input.
void apply(Node &pov)
Applies the current View to pov so it looks at the resolved target.
TurntableCameraController(const Config &config)
Creates a controller with config and the default View.
Limits, sensitivities, and input bindings used by the controller.
float dollySensitivity
Exponential dolly sensitivity for pointer dragging.
float orbitSensitivity
Orbit radians per logical pointer unit.
float dragThreshold
Pointer displacement required to convert a click candidate into a drag.
float maxDistance
Maximum target distance in scene units.
Controls controls
Pointer-button and modifier bindings.
float scrollDollySensitivity
Exponential dolly sensitivity per scroll unit.
float minDistance
Minimum positive target distance in scene units.
float maxPitch
Maximum pitch in radians; must remain below 90 degrees.
bool invertPitch
Reverses vertical orbit direction when true.
float minPitch
Minimum pitch in radians; must remain above -90 degrees.
Pointer buttons and modifier keys used for camera manipulation.
DesktopInputContext::Key dollyModifier
Makes orbit-button dragging dolly.
DesktopInputContext::MouseButton panButton
Dedicated pan/click button.
DesktopInputContext::MouseButton orbitButton
Primary orbit/click button.
DesktopInputContext::Key panModifier
Makes orbit-button dragging pan.
Target point expressed in the local coordinates of a weakly referenced Node.
std::weak_ptr< Node > node
Node whose transform supplies the target coordinate space.
math::vec3 localPosition
Target point in the Node's local coordinates.
Pointer click reported when a configured button is released without becoming a drag.
math::vec2 position
Release position in logical viewport coordinates.
std::optional< PointerClick > panButtonClick
Pan-button click released without crossing the drag threshold.
bool pointerDragging
Whether an orbit or pan button is currently dragging.
bool cameraChanged
Whether update() changed the View.
std::optional< PointerClick > orbitButtonClick
Orbit-button click released without crossing the drag threshold.
float distance
Distance from the target in scene units.
float yaw
Horizontal orbit angle in radians.
float pitch
Vertical orbit angle in radians.
Target target
Point around which the camera orbits.