AUI Framework  develop
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
ATouchScroller.h
    1/*
    2 * AUI Framework - Declarative UI toolkit for modern C++20
    3 * Copyright (C) 2020-2025 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
   55
   60
   66    glm::ivec2 handlePointerMove(glm::vec2 pos);
   67
   68    [[nodiscard]]
   69    glm::ivec2 origin() const noexcept;
   70
   77    [[nodiscard]]
   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;
   93        AOptional<std::chrono::high_resolution_clock::time_point> lastFrameTime;
   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:33
Wrapper class that stores either mouse button index or finger index.
Definition APointerIndex.h:21
static constexpr float INITIAL_ACCELERATION_COEFFICIENT
Initial acceleration after pointer released is defined by direction of pointer moving and this coeffi...
Definition ATouchScroller.h:40
static constexpr AMetric THRESHOLD
Distance that pointer have to pass in order to treat pointer move events as scroll events.
Definition ATouchScroller.h:47
AOptional< glm::ivec2 > gatherKineticScrollValue()
void handlePointerPressed(const APointerPressedEvent &e)
Handles pointer pressed events.
void handlePointerReleased(const APointerReleasedEvent &e)
Handles pointer released events.
glm::ivec2 handlePointerMove(glm::vec2 pos)
Handles pointer move events.
Pointing method press event.
Definition APointerPressedEvent.h:21
Pointing method press event.
Definition APointerReleasedEvent.h:19