TachyonUI 1.1.0
High-performance UI for building tools.
Loading...
Searching...
No Matches
gui_transition_manager.h
Go to the documentation of this file.
1#pragma once
2
4#include <gui_core/gui_id.h>
6
7#include <array>
8#include <optional>
9#include <stdint.h>
10#include <unordered_map>
11#include <vector>
12
13namespace tachyon
14{
17
18 enum class ElementState : uint8_t;
19 enum class VisualBoxStyleIndex : uint16_t;
20
23 enum class TransitionChannel : uint8_t { Hover, Active, Focused, Selected, Invalid };
24 inline constexpr int kTransitionChannelCount = 5;
25
29 {
31 uint32_t current = 0;
32 };
33
36 {
37 float duration = 0.0f;
39
40 bool operator==(const TransitionTiming&) const = default;
41 };
42
46
53 {
54 public:
55 // Each kind of change has its own timing; the ones not listed (press, selection,
56 // disabled) are instant.
57 static constexpr TransitionTiming kHoverIn { 0.070f, Easing::EaseOut };
58 static constexpr TransitionTiming kHoverOut { 0.140f, Easing::EaseOut };
59 static constexpr TransitionTiming kRelease { 0.120f, Easing::EaseOut };
60 static constexpr TransitionTiming kFocus { 0.100f, Easing::EaseOut };
61 static constexpr TransitionTiming kToggle { 0.120f, Easing::EaseInOut };
62 static constexpr TransitionTiming kInvalid { 0.150f, Easing::EaseOut };
63
67
70 void OnStateChange(ID id, ElementState fromState, ElementState toState, float elapsedSecs);
71
76 void OnStateSeen(ID id, ElementState state, ElementState watched, float elapsedSecs);
78 bool IsWatched(ID id) const { return !m_seen.empty() && m_seen.contains(id); }
79
81 std::optional<float> Value(ID id, TransitionChannel channel, float elapsedSecs) const;
83 bool IsRunning(ID id) const { return m_index.contains(id); }
85 bool HasRunning() const { return !m_slots.empty(); }
86
91 ElementState currentState, float elapsedSecs) const;
92
95 void Purge(float elapsedSecs);
96
98 void Clear() { m_slots.clear(); m_index.clear(); m_seen.clear(); }
99
102 void SetEnabled(bool enabled) { m_enabled = enabled; if (!enabled) Clear(); }
104 bool IsEnabled() const { return m_enabled; }
105
107 void SetReducedMotion(bool reduced) { m_reducedMotion = reduced; if (reduced) { m_slots.clear(); m_index.clear(); } }
108 bool IsReducedMotion() const { return m_reducedMotion; }
109
110 private:
112 enum class Timing : uint8_t { HoverIn, HoverOut, Release, Focus, Toggle, Invalid };
113 static const TransitionTiming& TimingOf(Timing timing);
114
117 struct Slot
118 {
119 ID id;
120 uint8_t running = 0;
121 uint8_t on = 0;
122 std::array<Timing, kTransitionChannelCount> timing {};
123 std::array<float, kTransitionChannelCount> start {};
124 };
125 struct Seen
126 {
127 ElementState state;
128 uint32_t frame;
129 };
130
132 static float ValueOf(const Slot& slot, int channel, float elapsedSecs);
134 void Run(ID id, int channel, bool on, Timing timing, bool was, float elapsedSecs);
136 void Remove(size_t index);
137
138 std::vector<Slot> m_slots;
139 std::unordered_map<ID, uint32_t> m_index;
140 std::unordered_map<ID, Seen> m_seen;
141 uint32_t m_frame = 0;
142 bool m_enabled = true;
143 bool m_reducedMotion = false;
144 };
145
147} // namespace tachyon
Tracks the running transitions of the elements' state channels.
Definition gui_transition_manager.h:53
bool IsEnabled() const
True unless SetEnabled(false) was called.
Definition gui_transition_manager.h:104
void Clear()
Removes all active transitions, for instance after a reorder invalidates ID mappings.
Definition gui_transition_manager.h:98
static TransitionTiming TimingFor(TransitionChannel channel, bool on)
The timing of channel turning on, or off.
bool HasRunning() const
True while any element's channel runs.
Definition gui_transition_manager.h:85
void Purge(float elapsedSecs)
Once a frame, after the build: lets go of the channels that have ended, and of the elements without o...
void SetReducedMotion(bool reduced)
Reduced motion: every change is instant. Turning it on drops the active transitions.
Definition gui_transition_manager.h:107
bool IsWatched(ID id) const
True when OnStateSeen saw the element the frame before or this one.
Definition gui_transition_manager.h:78
static constexpr TransitionTiming kHoverOut
Definition gui_transition_manager.h:58
static constexpr TransitionTiming kFocus
Definition gui_transition_manager.h:60
void OnStateSeen(ID id, ElementState state, ElementState watched, float elapsedSecs)
For an element whose watched bits change between frames rather than with input (Selected for ElementF...
static constexpr TransitionTiming kRelease
Definition gui_transition_manager.h:59
static constexpr TransitionTiming kHoverIn
Definition gui_transition_manager.h:57
TransitionState Resolve(ID id, VisualBoxStyleIndex base, ElementState currentState, float elapsedSecs) const
The element's box base in currentState: the styles of the states its running channels are between,...
bool IsReducedMotion() const
Definition gui_transition_manager.h:108
void SetEnabled(bool enabled)
Test/debug seam: when disabled, states snap instantly (deterministic rendering).
Definition gui_transition_manager.h:102
static constexpr TransitionTiming kInvalid
the invalid halo growing in
Definition gui_transition_manager.h:62
bool IsRunning(ID id) const
True while one of the element's channels runs.
Definition gui_transition_manager.h:83
std::optional< float > Value(ID id, TransitionChannel channel, float elapsedSecs) const
How far on (0 to 1) the element's channel is while it runs; nothing when it doesn't.
void OnStateChange(ID id, ElementState fromState, ElementState toState, float elapsedSecs)
The element's state went from fromState to toState: each channel that changed starts toward it,...
static constexpr TransitionTiming kToggle
a switch, check box or radio turning on or off
Definition gui_transition_manager.h:61
TransitionChannel
The parts of an element's state that change over time, each on its own: hover, press,...
Definition gui_transition_manager.h:23
uint32_t ComputeGPUStyleIndex(VisualBoxStyleIndex base, ElementState state)
Flat GPU style index for a base pool index and state.
constexpr int kTransitionChannelCount
Definition gui_transition_manager.h:24
@ Invalid
Definition gui_image.h:99
VisualBoxStyleIndex
Index into StyleManager's visual box style pool; Invalid (0) = none.
Definition gui_style_ref.h:17
ElementState
Interaction state bits of an element; they pick the Style::states variant it draws with.
Definition gui_style.h:42
@ Active
being pressed or dragged (CSS: :active)
Definition gui_style.h:44
@ Selected
selected item (CSS: :selected)
Definition gui_style.h:47
@ Focused
has keyboard focus (CSS: :focus)
Definition gui_style.h:46
@ Hover
under the mouse (CSS: :hover)
Definition gui_style.h:45
constexpr CubicBezier EaseInOut
slow at both ends
Definition gui_easing.h:66
constexpr CubicBezier EaseOut
fast start, long settle
Definition gui_easing.h:65
Definition gui_logging.h:24
A CSS cubic-bezier(x1, y1, x2, y2) timing curve: maps the share of a transition's time that has passe...
Definition gui_easing.h:11
A 64-bit element ID whose bit layout depends on its IDMode.
Definition gui_id.h:23
A box's look as a mix of up to four of its state styles (flat GPU style indices, as GUIRenderer::Styl...
Definition gui_renderer.h:47
A box's look for its state now: the state styles mixed while channels run (styles),...
Definition gui_transition_manager.h:29
uint32_t current
Definition gui_transition_manager.h:31
StyleBlend styles
Definition gui_transition_manager.h:30
How long a change of state takes, and how it eases. A duration of 0 is instant.
Definition gui_transition_manager.h:36
bool operator==(const TransitionTiming &) const =default
CubicBezier easing
Definition gui_transition_manager.h:38
float duration
seconds
Definition gui_transition_manager.h:37