blob: 1a88b8a65b71b63d3979f659f4b280d9e49a73a8 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
#pragma once
#include "control.hpp"
#include <cstdlib>
#include <forward_list>
#include <optional>
namespace cru::ui {
class ClickEventArgs : Object {
public:
ClickEventArgs(Control* sender, const Point& down_point,
const Point& up_point, MouseButton button)
: sender_(sender),
down_point_(down_point),
up_point_(up_point),
button_(button) {}
CRU_DEFAULT_COPY(ClickEventArgs)
CRU_DEFAULT_MOVE(ClickEventArgs)
~ClickEventArgs() override = default;
Control* GetSender() const { return sender_; }
Point GetDownPoint() const { return down_point_; }
Point GetUpPoint() const { return up_point_; }
MouseButton GetButton() const { return button_; }
private:
Control* sender_;
Point down_point_;
Point up_point_;
MouseButton button_;
};
class ClickDetector : public Object {
public:
explicit ClickDetector(Control* control);
ClickDetector(const ClickDetector& other) = delete;
ClickDetector& operator=(const ClickDetector& other) = delete;
ClickDetector(ClickDetector&& other) = delete;
ClickDetector& operator=(ClickDetector&& other) = delete;
~ClickDetector() override = default;
Control* GetControl() const { return control_; }
// Return a union of buttons being pressed. Return 0 if no button is being
// pressed.
MouseButton GetPressingButton() const {
unsigned result = 0;
if (click_map_.left.has_value()) result |= MouseButton::Left;
if (click_map_.middle.has_value()) result |= MouseButton::Middle;
if (click_map_.right.has_value()) result |= MouseButton::Right;
return static_cast<MouseButton>(result);
}
IEvent<ClickEventArgs>* ClickEvent() { return &event_; }
IEvent<MouseButton>* ClickBeginEvent() { return &begin_event_; }
IEvent<MouseButton>* ClickEndEvent() { return &end_event_; }
private:
std::optional<Point>& FromButton(MouseButton button) {
switch (button) {
case MouseButton::Left:
return click_map_.left;
case MouseButton::Middle:
return click_map_.middle;
case MouseButton::Right:
return click_map_.right;
default:
UnreachableCode();
}
}
private:
Control* control_;
Event<ClickEventArgs> event_;
Event<MouseButton> begin_event_;
Event<MouseButton> end_event_;
std::forward_list<EventRevokerGuard> event_rovoker_guards_;
struct {
std::optional<Point> left = std::nullopt;
std::optional<Point> middle = std::nullopt;
std::optional<Point> right = std::nullopt;
} click_map_;
};
} // namespace cru::ui
|