HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
mouse_event.hpp
1// Copyright Take Vos 2020.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "mouse_buttons.hpp"
8#include "../geometry/point.hpp"
9#include "../geometry/vector.hpp"
10#include "../geometry/transform.hpp"
11#include <chrono>
12
13namespace hi::inline v1 {
14
16 enum class Type { None, Entered, Exited, Move, Drag, ButtonDown, ButtonUp, Wheel };
17
18 Type type;
19
20 std::chrono::utc_time<std::chrono::nanoseconds> timePoint;
21
24
27
30
33
36
39
40 mouse_event() noexcept : type(Type::None), position(), cause(), down(), clickCount(0) {}
41
42 [[nodiscard]] bool empty() const noexcept
43 {
44 return type == Type::None;
45 }
46
47 operator bool() const noexcept
48 {
49 return not empty();
50 }
51
52 static mouse_event entered(point2 position = {}) noexcept
53 {
54 mouse_event event;
55 event.position = position;
56 event.type = mouse_event::Type::Entered;
57 return event;
58 }
59
60 static mouse_event exited() noexcept
61 {
62 // Position far away from the left/bottom corner, but where even
63 // after translations will not cause the position to be infinite.
64 constexpr float far_ = std::numeric_limits<float>::max() * -0.5f;
65
66 mouse_event event;
67 event.position = point2{far_, far_};
68 event.type = mouse_event::Type::Exited;
69 return event;
70 }
71
74 [[nodiscard]] bool is_left_button_up(aarectangle active_area) const noexcept
75 {
76 return type == Type::ButtonUp and cause.leftButton and active_area.contains(position);
77 }
78
81 [[nodiscard]] vector2 delta() const noexcept
82 {
83 return type == Type::Drag ? position - downPosition : vector2{};
84 }
85
86 [[nodiscard]] friend mouse_event operator*(geo::transformer auto const &transform, mouse_event const &rhs) noexcept
87 {
88 auto r = rhs;
89 r.position = point2{transform * rhs.position};
90 r.downPosition = point2{transform * rhs.downPosition};
91 r.wheelDelta = vector2{transform * rhs.wheelDelta};
92 return r;
93 }
94
95 friend std::string to_string(mouse_event const &rhs) noexcept
96 {
97 char const *type_s;
98 switch (rhs.type) {
99 using enum mouse_event::Type;
100 case None: type_s = "none"; break;
101 case Entered: type_s = "entered"; break;
102 case Exited: type_s = "exited"; break;
103 case Move: type_s = "move"; break;
104 case Drag: type_s = "drag"; break;
105 case ButtonDown: type_s = "down"; break;
106 case ButtonUp: type_s = "up"; break;
107 case Wheel: type_s = "wheel"; break;
108 default: hi_no_default();
109 }
110
111 return std::format("<mouse {} {}>", type_s, rhs.position);
112 }
113
114 friend std::ostream &operator<<(std::ostream &lhs, mouse_event const &rhs)
115 {
116 return lhs << to_string(rhs);
117 }
118};
119
120} // namespace hi::inline v1
Class which represents an axis-aligned rectangle.
Definition axis_aligned_rectangle.hpp:20
Definition mouse_buttons.hpp:11
Definition mouse_event.hpp:15
mouse_buttons cause
Buttons which has caused this event.
Definition mouse_event.hpp:32
vector2 wheelDelta
Change in wheel rotation, in pixels.
Definition mouse_event.hpp:29
bool is_left_button_up(aarectangle active_area) const noexcept
Check if this event is for a left-button-up event while the mouse pointer is in the given area.
Definition mouse_event.hpp:74
int clickCount
Number of clicks from the last button clicked.
Definition mouse_event.hpp:38
point2 position
The current position of the mouse pointer.
Definition mouse_event.hpp:23
point2 downPosition
The position the last time a button was pressed.
Definition mouse_event.hpp:26
mouse_buttons down
Buttons that are pressed/held down.
Definition mouse_event.hpp:35
vector2 delta() const noexcept
Get the location of the mouse relative to the start of a drag.
Definition mouse_event.hpp:81
Definition transform.hpp:82
T max(T... args)
T to_string(T... args)
T transform(T... args)