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 "../numeric_array.hpp"
9#include "../hires_utc_clock.hpp"
10
11namespace tt {
12
14 enum class Type { None, Entered, Exited, Move, Drag, ButtonDown, ButtonUp, Wheel };
15
16 Type type;
17
19
22
25
28
31
34
37
38 mouse_event() noexcept :
39 type(Type::None),
40 position(),
41 cause(),
42 down(),
43 clickCount(0)
44 {
45 }
46
49 [[nodiscard]] f32x4 delta() const noexcept {
50 return type == Type::Drag ? position - downPosition : f32x4{};
51 }
52
53 static mouse_event entered(f32x4 position=f32x4::point({0.0f, 0.0f})) noexcept {
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 // Position far away from the left/bottom corner, but where even
62 // after translations will not cause the position to be infinite.
63 constexpr float far_ = std::numeric_limits<float>::max() * -0.5f;
64
65 mouse_event event;
66 event.position = f32x4{far_, far_};
67 event.type = mouse_event::Type::Exited;
68 return event;
69 }
70
71 friend std::string to_string(mouse_event const &rhs) noexcept {
72 char const *type_s;
73 switch (rhs.type) {
74 using enum mouse_event::Type;
75 case None: type_s = "none"; break;
76 case Entered: type_s = "entered"; break;
77 case Exited: type_s = "exited"; break;
78 case Move: type_s = "move"; break;
79 case Drag: type_s = "drag"; break;
80 case ButtonDown: type_s = "down"; break;
81 case ButtonUp: type_s = "up"; break;
82 case Wheel: type_s = "wheel"; break;
83 default: tt_no_default();
84 }
85
86 return fmt::format("<mouse {} {}>", type_s, rhs.position);
87 }
88
89 friend std::ostream &operator<<(std::ostream &lhs, mouse_event const &rhs) {
90 return lhs << to_string(rhs);
91 }
92};
93
94
95
96}
Definition mouse_buttons.hpp:11
Definition mouse_event.hpp:13
f32x4 downPosition
The position the last time a button was pressed.
Definition mouse_event.hpp:24
f32x4 delta() const noexcept
Get the location of the mouse relative to the start of a drag.
Definition mouse_event.hpp:49
int clickCount
Number of clicks from the last button clicked.
Definition mouse_event.hpp:36
f32x4 wheelDelta
Change in wheel rotation, in pixels.
Definition mouse_event.hpp:27
f32x4 position
The current position of the mouse pointer.
Definition mouse_event.hpp:21
mouse_buttons down
Buttons that are pressed/held down.
Definition mouse_event.hpp:33
mouse_buttons cause
Buttons which has caused this event.
Definition mouse_event.hpp:30
static constexpr numeric_array point() noexcept
Get a point at the origin.
Definition numeric_array.hpp:90
T max(T... args)