forked from Dovahseod/Bongobs-Cat-Plugin-Edition-D
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventManager.cpp
170 lines (138 loc) · 4.16 KB
/
EventManager.cpp
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**
* Copyright(c) Live2D Inc. All rights reserved.
*
* Use of this source code is governed by the Live2D Open Software license
* that can be found at
* https://www.live2d.com/eula/live2d-open-software-license-agreement_en.html.
*/
#include "EventManager.hpp"
#include <Windows.h>
#include <math.h>
#include <algorithm>
EventManager::EventManager()
: _startY(0.0f),
_startX(0.0f),
_lastX(0.0f),
_lastY(0.0f),
_lastX1(0.0f),
_lastY1(0.0f),
_lastX2(0.0f),
_lastY2(0.0f),
_deltaX(0.0f),
_deltaY(0.0f),
_scale(1.0f),
_relativemouseX(0),
_relativemouseY(0),
_leftButton(false),
_rightButton(false) {
for (int i = 0; i < KEYAMOUT; i++) _keyEvent[i].KeyBoardSignal = false;
}
EventManager::~EventManager() {}
void EventManager::MouseEventBegan(float deviceX, float deviceY) {
_lastX = deviceX;
_lastY = deviceY;
_startX = deviceX;
_startY = deviceY;
}
void EventManager::MouseEventMoved(float deviceX, float deviceY) {
_lastX = deviceX;
_lastY = deviceY;
}
void EventManager::MouseEventMoved(float deviceX1, float deviceY1,
float deviceX2, float deviceY2) {
float distance = CalculateDistance(deviceX1, deviceY1, deviceX2, deviceY2);
float centerX = (deviceX1 + deviceX2) * 0.5f;
float centerY = (deviceY1 + deviceY2) * 0.5f;
_lastX = centerX;
_lastY = centerY;
_lastX1 = deviceX1;
_lastY1 = deviceY1;
_lastX2 = deviceX2;
_lastY2 = deviceY2;
}
void EventManager::MouseEventMoved(int width, int height, float deviceX,
float deviceY) {
if (deviceX - _startX > 0) {
if (deviceX - _startX > width / 2) {
_startX += deviceX - _startX - width / 2;
}
_lastX = deviceX;
} else {
if (abs(deviceX - _startX) > width / 2) {
_startX -= abs(deviceX - _startX) - width / 2;
}
_lastX = deviceX;
}
if (deviceY - _startY > 0) {
if (deviceY - _startY > height / 2) {
_startY += deviceY - _startY - height / 2;
}
_lastY = deviceY;
} else {
if (abs(deviceY - _startY) > height / 2) {
_startY -= abs(deviceY - _startY) - height / 2;
}
_lastY = deviceY;
}
}
void EventManager::KeyEventDown(int key) {
std::lock_guard lock(mutex_);
_keyEvent[key].KeyBoardSignal = true;
if (std::remove(pressed_keys_.begin(), pressed_keys_.end(), key) ==
pressed_keys_.end()) {
pressed_keys_.push_back(key);
}
}
void EventManager::KeyEventUp(int key) {
std::lock_guard lock(mutex_);
_keyEvent[key].KeyBoardSignal = false;
pressed_keys_.erase(
std::remove(pressed_keys_.begin(), pressed_keys_.end(), key),
pressed_keys_.end());
}
void EventManager::AllKeysUp() {
std::lock_guard lock(mutex_);
for (int i = 0; i < sizeof(_keyEvent) / sizeof(_keyEvent[0]); ++i) {
_keyEvent[i].KeyBoardSignal = false;
}
pressed_keys_.clear();
}
void EventManager::LeftButtonDown() { _leftButton = true; }
void EventManager::LeftButtonUp() { _leftButton = false; }
void EventManager::RightButtonDown() { _rightButton = true; }
void EventManager::RightButtonUp() { _rightButton = false; }
void EventManager::SetRelativeMouse(int _rx, int _ry) {
_relativemouseX += _rx;
_relativemouseY += _ry;
}
void EventManager::GetRelativeMouse(int &_rx, int &_ry) {
_rx = _relativemouseX;
_ry = _relativemouseY;
}
void EventManager::GetCurrentMousePosition(int &_x, int &_y) {
POINT p;
GetCursorPos(&p);
_x = p.x;
_y = p.y;
}
float EventManager::GetFlickDistance() const {
return CalculateDistance(_startX, _startY, _lastX, _lastY);
}
std::vector<int> EventManager::GetPressedKeys() {
std::lock_guard lock(mutex_);
return pressed_keys_;
}
float EventManager::CalculateDistance(float x1, float y1, float x2,
float y2) const {
return sqrtf((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
float EventManager::CalculateMovingAmount(float v1, float v2) {
if ((v1 > 0.0f) != (v2 > 0.0f)) {
return 0.0f;
}
float sign = v1 > 0.0f ? 1.0f : -1.0f;
float absoluteValue1 = fabsf(v1);
float absoluteValue2 = fabsf(v2);
return sign *
((absoluteValue1 < absoluteValue2) ? absoluteValue1 : absoluteValue2);
}