AUI Framework  master
Cross-platform module-based framework for developing C++20 desktop applications
ATouchScroller.h
1/*
2 * AUI Framework - Declarative UI toolkit for modern C++20
3 * Copyright (C) 2020-2024 Alex2772 and Contributors
4 *
5 * SPDX-License-Identifier: MPL-2.0
6 *
7 * This Source Code Form is subject to the terms of the Mozilla Public
8 * License, v. 2.0. If a copy of the MPL was not distributed with this
9 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 */
11
12#pragma once
13
14
15#include "AUI/Common/AObject.h"
16#include "AUI/Event/APointerPressedEvent.h"
17#include "AUI/Event/APointerReleasedEvent.h"
18#include "AMetric.h"
19#include "AUI/Event/AScrollEvent.h"
20#include "AUI/Animator/Curves/bezier.h"
21
29class API_AUI_VIEWS ATouchScroller {
30public:
31 /*
32 * @brief Deceleration for kinetic scroll
33 * @note Multiply by dpi ratio for proper work of kinetic scroll on all devices
34 */
35 static constexpr float BASE_DECELERATION = 1600.f;
36
40 static constexpr float INITIAL_ACCELERATION_COEFFICIENT = 60.f;
41
42 static float deceleration();
43
47 static constexpr AMetric THRESHOLD = 8_dp;
48
49 ATouchScroller() = default;
50
54 void handlePointerPressed(const APointerPressedEvent& e);
55
59 void handlePointerReleased(const APointerReleasedEvent& e);
60
66 glm::ivec2 handlePointerMove(glm::vec2 pos);
67
68 [[nodiscard]]
69 glm::ivec2 origin() const noexcept;
70
77 [[nodiscard]]
78 AOptional<glm::ivec2> gatherKineticScrollValue();
79
80private:
81 struct WaitingForThresholdState {
82 APointerIndex pointer;
83 glm::vec2 origin;
84 };
85 struct ScrollingState {
86 APointerIndex pointer;
87 glm::vec2 origin;
88 glm::vec2 previousPosition = {0.f, 0.f}; // to calculate velocity
89 glm::vec2 currentVelocity = {0.f, 0.f};
90 glm::vec2 prevVelocity = {0.f, 0.f};
91 glm::vec2 prevPrevVelocity = {0.f, 0.f};
92 std::chrono::microseconds timeBetweenFrames;
94 };
95
96 struct KineticScrollingState {
97 APointerIndex pointer;
98 glm::vec2 origin;
99 float velocity;
100 float averageTimeDelta;
101 glm::vec2 direction;
102
103 std::chrono::high_resolution_clock::time_point lastFrameTime = std::chrono::high_resolution_clock::now();
104 };
105
106 std::variant<std::nullopt_t, WaitingForThresholdState, ScrollingState, KineticScrollingState> mState = std::nullopt;
107};
Stores dimensions in scalable units (dp, pt, etc...).
Definition: AMetric.h:75
Utility wrapper implementing the stack-allocated (fast) optional idiom.
Definition: AOptional.h:32
Wrapper class that stores either mouse button index or finger index.
Definition: APointerIndex.h:21
Utility object that helps with touchscreen scroll events.
Definition: ATouchScroller.h:29
Pointing method press event.
Definition: APointerPressedEvent.h:21
Pointing method press event.
Definition: APointerReleasedEvent.h:19