TachyonUI 1.1.0
High-performance UI for building tools.
Loading...
Searching...
No Matches
gui_navigation.h
Go to the documentation of this file.
1#pragma once
2
3// How the pointer moves around a canvas that pans and zooms (the node view, the curve and
4// timeline editors, a plot): the same buttons and wheel everywhere.
5
7
8#include <cmath>
9
10namespace tachyon
11{
14
18 enum class WheelMode : uint8_t
19 {
22 };
23
27 {
28#if defined(__APPLE__)
29 return WheelMode::Pan;
30#else
31 return WheelMode::Zoom;
32#endif
33 }
34
37 {
38 float zoom = 1.0f;
40 bool Any() const { return zoom != 1.0f || pan.x != 0.0f || pan.y != 0.0f; }
41 };
42
46 inline WheelNavigation NavigateByWheel(const InputState& s, WheelMode mode, float zoomStep)
47 {
49 if (s.pinch != 1.0f && std::isfinite(s.pinch) && s.pinch > 0.0f)
50 {
51 nav.zoom = s.pinch;
52 return nav;
53 }
54 const bool ctrl = (s.modifiers & KeyModifier::Control) != KeyModifier::None;
55 const bool shift = (s.modifiers & KeyModifier::Shift) != KeyModifier::None;
56 const bool zooms = mode == WheelMode::Zoom ? !shift : ctrl;
57 if (zooms)
58 {
59 if (s.scroll.y != 0.0f)
60 nav.zoom = std::pow(zoomStep, s.scroll.y);
61 nav.pan.x = s.scroll.x;
62 return nav;
63 }
64 // Away from the user scrolls up; with Shift, the turn scrolls sideways.
65 nav.pan = shift ? float2{ s.scroll.x - s.scroll.y, 0.0f } : float2{ s.scroll.x, -s.scroll.y };
66 return nav;
67 }
68
72 {
73 if (s.MiddlePressed())
76 return MouseButtons::Left;
77 return MouseButtons::None;
78 }
79
81}
WheelMode
What the wheel, and a trackpad's two-finger scroll, does over such a canvas.
Definition gui_navigation.h:19
constexpr WheelMode DefaultWheelMode()
The mode a platform's users expect: Pan on macOS, where most pointers are trackpads; Zoom elsewhere.
Definition gui_navigation.h:26
MouseButtons PanDragButton(const InputState &s)
The button a press drags the view with: the middle one, or the left with Alt for a mouse or trackpad ...
Definition gui_navigation.h:71
MouseButtons
Mouse buttons, combined with |.
Definition gui_input_enums.h:38
WheelNavigation NavigateByWheel(const InputState &s, WheelMode mode, float zoomStep)
A wheel (or pinch) event's zoom or scroll by mode; zoomStep is the zoom of one notch.
Definition gui_navigation.h:46
@ Zoom
the wheel zooms round the pointer; Shift+wheel scrolls sideways
Definition gui_navigation.h:20
@ Pan
the wheel scrolls the view (Shift: sideways); Ctrl+wheel zooms
Definition gui_navigation.h:21
@ None
Definition gui_input_enums.h:27
@ Shift
Definition gui_input_enums.h:28
@ Control
Definition gui_input_enums.h:29
@ Alt
Definition gui_input_enums.h:30
@ None
Definition gui_input_enums.h:39
@ Left
Definition gui_input_enums.h:40
@ Middle
Definition gui_input_enums.h:41
Definition gui_logging.h:24
Snapshot of input state for a single recorded OS event.
Definition gui_input_state.h:75
bool LeftPressed() const
Definition gui_input_state.h:120
bool MiddlePressed() const
Definition gui_input_state.h:128
float pinch
A trackpad pinch (a MouseScroll event): how far the fingers spread since the last one,...
Definition gui_input_state.h:88
tachyon::KeyModifier modifiers
Definition gui_input_state.h:83
tachyon::float2 scroll
wheel notches (fractional on hi-res devices); label drags: pixels
Definition gui_input_state.h:85
What a wheel or pinch event asks of a canvas.
Definition gui_navigation.h:37
bool Any() const
Definition gui_navigation.h:40
float zoom
by how much to zoom round the pointer; above 1 is in, 1 is none
Definition gui_navigation.h:38
float2 pan
how far to scroll, in wheel notches: x right, y down
Definition gui_navigation.h:39
Definition gui_base_types.h:120
float y
Definition gui_base_types.h:121
float x
Definition gui_base_types.h:121