AUI Framework  master
Cross-platform module-based framework for developing C++20 desktop applications
APointerIndex.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#include <compare>
15#include <AUI/Platform/AInput.h>
16
22public:
23 constexpr APointerIndex() noexcept: mValue(AInput::LBUTTON) {}
24
25 static constexpr APointerIndex button(AInput::Key button) {
26 assert((button == AInput::LBUTTON ||
27 button == AInput::RBUTTON ||
28 button == AInput::CBUTTON));
29 return APointerIndex((int)button);
30 }
31
32 static constexpr APointerIndex finger(int fingerIndex) {
33 return APointerIndex(fingerIndex + MAX_BUTTON_VALUE + 1);
34 }
35
36
40 [[nodiscard]]
41 bool isButton() const noexcept {
42 return mValue <= MAX_BUTTON_VALUE;
43 }
44
48 [[nodiscard]]
49 bool isFinger() const noexcept {
50 return mValue > MAX_BUTTON_VALUE;
51 }
52
56 AOptional<AInput::Key> button() const noexcept {
57 if (!isButton()) {
58 return std::nullopt;
59 }
60 return (AInput::Key)mValue;
61 }
62
66 AOptional<int> finger() const noexcept {
67 if (!isFinger()) {
68 return std::nullopt;
69 }
70 return mValue - MAX_BUTTON_VALUE - 1;
71 }
72
73 [[nodiscard]]
74 auto operator<=>(const APointerIndex& rhs) const noexcept = default;
75
76 [[nodiscard]]
77 constexpr int rawValue() const noexcept {
78 return mValue;
79 }
80
81private:
82 int mValue;
83 static constexpr int MAX_BUTTON_VALUE = AInput::RBUTTON;
84
85 explicit constexpr APointerIndex(int value) : mValue(value) {}
86};
87
88
89inline std::ostream& operator<<(std::ostream& o, const APointerIndex& color) {
90 o << "APointerIndex{";
91 if (auto b = color.button()) {
92 o << "button=";
93 switch (*b) {
94 case AInput::LBUTTON:
95 o << "l";
96 break;
97 case AInput::RBUTTON:
98 o << "r";
99 break;
100 case AInput::CBUTTON:
101 o << "c";
102 break;
103 default:
104 o << (int)*b;
105 break;
106 }
107 } else if (auto b = color.finger()) {
108 o << "finger=" << *b;
109 }
110 o << "}";
111 return o;
112}
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
bool isFinger() const noexcept
Definition: APointerIndex.h:49
AOptional< int > finger() const noexcept
Definition: APointerIndex.h:66
AOptional< AInput::Key > button() const noexcept
Definition: APointerIndex.h:56
bool isButton() const noexcept
Definition: APointerIndex.h:41