TachyonUI 1.1.0
High-performance UI for building tools.
Loading...
Searching...
No Matches
gui_viewport_types.h
Go to the documentation of this file.
1#pragma once
2
3// Viewport: a 3D view the app renders, with a camera to move round it and gizmos to move,
4// rotate and scale the selected object.
5
11
12#include <functional>
13
14namespace tachyon
15{
18
20 enum class ViewportTool : uint8_t
21 {
26 };
27
30 {
32 float distance = 8.0f;
33 float yaw = 0.6f;
34 float pitch = -0.45f;
35 float fovY = 0.8727f;
36 bool orthographic = false;
37 float nearZ = 0.05f, farZ = 1000.0f;
38
40 float3 Forward() const;
41 float3 Position() const { return target - Forward() * distance; }
42 float3 Right() const;
43 float3 Up() const { return Cross(Right(), Forward()); }
44 float4x4 View() const { return LookAt(Position(), target, { 0, 1, 0 }); }
47 float4x4 Projection(float aspect) const;
49 float WorldPerPixel(float3 at, float heightPixels) const;
50 };
51
53 Ray ViewportRay(const ViewportCamera& camera, float2 pixel, float2 size);
55 bool ViewportProject(const ViewportCamera& camera, float2 size, float3 point, float2& pixel);
56
59 {
62 float3 scale { 1, 1, 1 };
63
64 bool operator==(const ViewportTransform&) const = default;
65 float4x4 Matrix() const { return TRS(position, rotation, scale); }
66 };
67
70 {
72 bool empty() const { return from == to; }
73 };
74
76 enum class GizmoPart : uint8_t
77 {
79 X, Y, Z,
80 XY, YZ, XZ,
82 };
83
97
100 {
105 bool showTools = true;
107 bool hasSelection = false;
109 float selectionRadius = 1.0f;
112 float flySpeed = 4.0f;
114 color4f background { 0.07f, 0.08f, 0.09f, 1.0f };
115
117 std::function<void(const ViewportRenderContext&)> onRender;
119 std::function<void(const ViewportEdit&)> onEdit;
121 std::function<void(const Ray& ray, KeyModifier modifiers)> onClick;
122
130 enum class CameraMove : uint8_t { None, Pan, Orbit, Look, Dolly } cameraMove = CameraMove::None;
132 uint8_t flyKeys = 0;
133 bool flyFast = false;
135 uint32_t revision = 0, revisionSeen = 0;
137 };
138
141
143 constexpr float kGizmoPixels = 90.0f;
144
147 GizmoPart HitGizmo(const ViewportView& view, float2 pixel, float2 size);
150 const ViewportCamera& camera, float2 size, float2 from, float2 to);
151
153 void OrbitCamera(ViewportCamera& camera, float2 delta);
155 void LookCamera(ViewportCamera& camera, float2 delta);
158 void PanCamera(ViewportCamera& camera, float2 delta, float heightPixels);
160 void DollyCamera(ViewportCamera& camera, float factor);
162 void AlignCamera(ViewportCamera& camera, int axis, bool fromNegative);
167 void FlyCamera(ViewportView& view, float seconds);
168
170 enum FlyKey : uint8_t { FlyForward = 1, FlyBack = 2, FlyLeft = 4, FlyRight = 8, FlyDown = 16, FlyUp = 32 };
171
173 void ApplyViewportEdit(ViewportView& view, const ViewportEdit& edit, bool undo = false);
177
179}
What GraphicsContext::RenderScene's record gets: the backend's recording state, as the struct of the ...
Definition gui_graphics_api.h:41
ViewportTransform DragGizmo(ViewportTool tool, GizmoPart part, const ViewportTransform &before, const ViewportCamera &camera, float2 size, float2 from, float2 to)
The selection a drag of part from from to to (view pixels) makes of before.
void PanCamera(ViewportCamera &camera, float2 delta, float heightPixels)
Slides the camera and its target so what was under the pointer follows a drag of delta pixels,...
bool ViewportProject(const ViewportCamera &camera, float2 size, float3 point, float2 &pixel)
Where point shows in the view, from its top left; false when it's behind the camera.
void OrbitCamera(ViewportCamera &camera, float2 delta)
Turns the camera round its target by a drag of delta pixels.
ViewportTool
What a drag on the view does to the selected object.
Definition gui_viewport_types.h:21
void FlyCamera(ViewportView &view, float seconds)
Moves the camera by the fly keys held (ViewportView::flyKeys) for seconds: forward,...
void AlignCamera(ViewportCamera &camera, int axis, bool fromNegative)
Looks along axis (0 x, 1 y, 2 z) toward its negative end, or its positive with fromBelow.
void FrameSelection(ViewportView &view)
Centers the selection, near enough to see all of it.
Ray ViewportRay(const ViewportCamera &camera, float2 pixel, float2 size)
The ray from the camera through pixel (from the view's top left) of a view size pixels.
GizmoPart
A part of the gizmo.
Definition gui_viewport_types.h:77
FlyKey
The fly keys' bits in ViewportView::flyKeys.
Definition gui_viewport_types.h:170
void CommitViewportEdit(ViewportView &view, const ViewportEdit &edit)
Applies edit and hands it to view.onEdit; does nothing for an empty one.
void ApplyViewportEdit(ViewportView &view, const ViewportEdit &edit, bool undo=false)
Does (or with undo, undoes) edit on the selection.
void LookCamera(ViewportCamera &camera, float2 delta)
Turns the camera where it stands (looks round) by a drag of delta pixels.
constexpr float kGizmoPixels
The gizmo's arrows, rings and handles are this many pixels long.
Definition gui_viewport_types.h:143
void DollyCamera(ViewportCamera &camera, float factor)
Moves the camera toward its target by factor (above 1: closer).
GizmoPart HitGizmo(const ViewportView &view, float2 pixel, float2 size)
The part of view's gizmo under pixel in a view size pixels, by its tool; None without a selection or ...
@ Move
Definition gui_viewport_types.h:23
@ Rotate
Definition gui_viewport_types.h:24
@ Z
an arrow, a ring or a scale handle
Definition gui_viewport_types.h:79
@ XZ
a plane square (move)
Definition gui_viewport_types.h:80
@ None
Definition gui_viewport_types.h:78
@ XY
Definition gui_viewport_types.h:80
@ YZ
Definition gui_viewport_types.h:80
@ FlyBack
Definition gui_viewport_types.h:170
@ FlyLeft
Definition gui_viewport_types.h:170
@ FlyForward
Definition gui_viewport_types.h:170
@ FlyRight
Definition gui_viewport_types.h:170
@ FlyDown
Definition gui_viewport_types.h:170
@ FlyUp
Definition gui_viewport_types.h:170
@ None
Definition gui_element_flags.h:18
@ X
Definition gui_scroll_management.h:24
@ Y
Definition gui_scroll_management.h:25
KeyModifier
Modifier keys held, combined with |.
Definition gui_input_enums.h:26
@ Select
Definition gui_input_enums.h:290
@ Pan
the wheel scrolls the view (Shift: sideways); Ctrl+wheel zooms
Definition gui_navigation.h:21
@ Move
it's at screenPos
Definition gui_input_enums.h:67
float4x4 TRS(float3 position, quat rotation, float3 scale)
Scale, then rotate, then move: an object's transform in the world.
Definition gui_math3d.h:93
float4x4 LookAt(float3 eye, float3 target, float3 up)
The view matrix of a camera at eye looking at target, up roughly up.
Definition gui_math3d.h:103
float3 Scale(float3 a, float3 b)
Definition gui_math3d.h:29
float3 Cross(float3 a, float3 b)
Definition gui_math3d.h:21
@ Center
Center on cross axis.
Definition gui_style.h:185
Definition gui_logging.h:24
One press that may become a drag, with what every control does around it: the focus and the capture o...
Definition gui_gesture.h:60
The part of a drawn control under the pointer (a key, a handle, a clip's end), for its hover look.
Definition gui_gesture.h:115
Definition gui_base_types.h:406
A ray: from origin, along direction (length 1).
Definition gui_math3d.h:145
A camera orbiting target, distance away.
Definition gui_viewport_types.h:30
float distance
Definition gui_viewport_types.h:32
float nearZ
Definition gui_viewport_types.h:37
float3 Right() const
float3 Position() const
Definition gui_viewport_types.h:41
float farZ
Definition gui_viewport_types.h:37
float3 Forward() const
The way it looks, length 1.
float4x4 View() const
Definition gui_viewport_types.h:44
float3 Up() const
Definition gui_viewport_types.h:43
float yaw
radians round y; 0 looks down -z, positive turns left
Definition gui_viewport_types.h:33
bool orthographic
Definition gui_viewport_types.h:36
float4x4 Projection(float aspect) const
For a view aspect wide over high.
float fovY
radians, top to bottom (50 degrees)
Definition gui_viewport_types.h:35
float3 target
Definition gui_viewport_types.h:31
float pitch
radians; negative looks down
Definition gui_viewport_types.h:34
float WorldPerPixel(float3 at, float heightPixels) const
How many world units a pixel is at at, in a view heightPixels high.
A gizmo's drag: the selection as it was and is.
Definition gui_viewport_types.h:70
ViewportTransform from
Definition gui_viewport_types.h:71
ViewportTransform to
Definition gui_viewport_types.h:71
bool empty() const
Definition gui_viewport_types.h:72
What the app's onRender gets each frame: the target's size and the camera's matrices,...
Definition gui_viewport_types.h:91
ScenePayload scene
Definition gui_viewport_types.h:95
uint2 size
pixels
Definition gui_viewport_types.h:92
float4x4 projection
Definition gui_viewport_types.h:94
float4x4 view
Definition gui_viewport_types.h:93
The selected object's place in the world.
Definition gui_viewport_types.h:59
float4x4 Matrix() const
Definition gui_viewport_types.h:65
float3 scale
Definition gui_viewport_types.h:62
bool operator==(const ViewportTransform &) const =default
float3 position
Definition gui_viewport_types.h:60
quat rotation
Definition gui_viewport_types.h:61
A viewport: what it shows and how. Keep it from frame to frame, as a CurveView.
Definition gui_viewport_types.h:100
ViewportTransform selection
Definition gui_viewport_types.h:108
bool hasSelection
The selected object, which the gizmo moves; none, no gizmo.
Definition gui_viewport_types.h:107
std::function< void(const ViewportRenderContext &)> onRender
Records the app's drawing into the view each frame (see ViewportRenderContext).
Definition gui_viewport_types.h:117
IntRect viewRect
the view last frame, in screen pixels
Definition gui_viewport_types.h:134
ViewportCamera camera
Definition gui_viewport_types.h:101
uint32_t revision
a gizmo part dragged; the selection as it began
Definition gui_viewport_types.h:135
color4f background
Behind what onRender draws; and all there is without it.
Definition gui_viewport_types.h:114
uint32_t revisionSeen
a gizmo part dragged; the selection as it began
Definition gui_viewport_types.h:135
Gesture< ViewportCamera > cameraDrag
the camera moved by a drag; as it was, for Escape
Definition gui_viewport_types.h:129
Gesture< ViewportTransform > gizmoDrag
a gizmo part dragged; the selection as it began
Definition gui_viewport_types.h:125
HoverPart< GizmoPart > hover
the part under the pointer
Definition gui_viewport_types.h:127
ViewportTool tool
Definition gui_viewport_types.h:102
bool showTools
The tools' buttons down the view's left.
Definition gui_viewport_types.h:105
float flySpeed
How fast W, A, S, D, Q and E move the camera while the right button looks round, in world units a sec...
Definition gui_viewport_types.h:112
std::function< void(const Ray &ray, KeyModifier modifiers)> onClick
A click that didn't drag, off the gizmo: the ray through it, to pick with.
Definition gui_viewport_types.h:121
float2 dragFrom
the press, in view pixels
Definition gui_viewport_types.h:128
enum tachyon::ViewportView::CameraMove cameraMove
a gizmo part dragged; the selection as it began
uint8_t flyKeys
the fly keys held, by FlyKey bit
Definition gui_viewport_types.h:132
std::function< void(const ViewportEdit &)> onEdit
A gizmo's drag ended (already applied): put it in the undo history.
Definition gui_viewport_types.h:119
Gesture click
a left press off the gizmo: a pick if it doesn't move
Definition gui_viewport_types.h:131
CameraMove
a gizmo part dragged; the selection as it began
Definition gui_viewport_types.h:130
@ None
Definition gui_viewport_types.h:130
float selectionRadius
how big it is, for framing it (F)
Definition gui_viewport_types.h:109
bool flyFast
Shift held while flying.
Definition gui_viewport_types.h:133
GizmoPart activePart
the part dragged
Definition gui_viewport_types.h:126
Definition gui_base_types.h:394
Definition gui_base_types.h:120
Definition gui_base_types.h:149
Definition gui_base_types.h:258
A rotation, as a unit quaternion.
Definition gui_math3d.h:33
Definition gui_base_types.h:74