ZenitUI
Retained-mode C++20 UI framework
Loading...
Searching...
No Matches
ScrollState.hpp
Go to the documentation of this file.
1#pragma once
2
12
13#include "CoreTypes.hpp"
14
15namespace ZenitUI
16{
17
21
34 {
35 Vec2 offset{0.0f, 0.0f};
36 Vec2 maxScroll{0.0f, 0.0f};
37 Vec2 velocity{0.0f, 0.0f};
38
39 float dragStartMouseY{0.0f};
40 float dragStartOffsetY{0.0f};
41 float dragStartMouseX{0.0f};
42 float dragStartOffsetX{0.0f};
43
44 static constexpr float WHEEL_IMPULSE = 400.0f;
45 static constexpr float DECAY = 0.90f;
46 static constexpr float VELOCITY_MIN = 5.0f;
47
49 void clamp()
50 {
51 offset.x = std::clamp(offset.x, 0.0f, maxScroll.x);
52 offset.y = std::clamp(offset.y, 0.0f, maxScroll.y);
53 }
54
56 void reset()
57 {
58 offset = {0.0f, 0.0f};
59 maxScroll = {0.0f, 0.0f};
60 velocity = {0.0f, 0.0f};
61 }
62
71 void tickInertia(float dt)
72 {
73 if (std::abs(velocity.x) > VELOCITY_MIN)
74 {
75 offset.x += velocity.x * dt;
76 velocity.x *= std::pow(DECAY, dt * 60.0f);
77 }
78 else
79 {
80 velocity.x = 0.0f;
81 }
82
83 if (std::abs(velocity.y) > VELOCITY_MIN)
84 {
85 offset.y += velocity.y * dt;
86 velocity.y *= std::pow(DECAY, dt * 60.0f);
87 }
88 else
89 {
90 velocity.y = 0.0f;
91 }
92
93 clamp();
94 }
95 };
96
98} // namespace ZenitUI
Small value types shared by the layout, rendering and input layers.
Offset, bounds, velocity and drag anchors for one scroll container.
Definition ScrollState.hpp:34
void clamp()
Clamp offset to [0, maxScroll] on both axes.
Definition ScrollState.hpp:49
void reset()
Reset offset, bounds and velocity to zero.
Definition ScrollState.hpp:56
void tickInertia(float dt)
Advance the inertial offset by dt seconds.
Definition ScrollState.hpp:71
2D vector with component-wise arithmetic.
Definition CoreTypes.hpp:25