/
allevents.cpp
241 lines (203 loc) · 6.92 KB
/
allevents.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// LAF Library
// Copyright (c) 2019-2020 Igara Studio S.A.
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#include "base/clamp.h"
#include "gfx/hsv.h"
#include "gfx/rgb.h"
#include "os/os.h"
#include <cstdarg>
#include <cstdlib>
#include <string>
#include <vector>
class LogWindow {
public:
LogWindow(os::System* system)
: m_display(system->makeDisplay(800, 600)) {
m_display->setNativeMouseCursor(os::kArrowCursor);
m_display->setTitle("All Events");
recalcMaxLines();
logLine("-- Events Log --");
}
bool processEvent(const os::Event& ev) {
switch (ev.type()) {
case os::Event::CloseDisplay:
return false;
case os::Event::ResizeDisplay:
logLine("ResizeDisplay size=%d,%d",
m_display->width(),
m_display->height());
recalcMaxLines();
break;
case os::Event::DropFiles:
logLine("DropFiles files={");
for (const auto& file : ev.files()) {
logLine(" \"%s\"", file.c_str());
}
logLine("}");
break;
case os::Event::MouseEnter: logMouseEvent(ev, "MouseEnter"); break;
case os::Event::MouseLeave: logMouseEvent(ev, "MouseLeave"); break;
case os::Event::MouseMove: logMouseEvent(ev, "MouseMove"); break;
case os::Event::MouseDown: logMouseEvent(ev, "MouseDown"); break;
case os::Event::MouseUp: logMouseEvent(ev, "MouseUp"); break;
case os::Event::MouseDoubleClick: logMouseEvent(ev, "MouseDoubleClick"); break;
case os::Event::MouseWheel:
m_mousePos = ev.position();
logLine("MouseWheel pos=%d,%d %s=%d,%d%s",
ev.position().x,
ev.position().y,
ev.preciseWheel() ? " preciseWheel": "wheel",
ev.wheelDelta().x,
ev.wheelDelta().y,
modifiersToString(ev.modifiers()).c_str());
m_hue += double(ev.wheelDelta().x + ev.wheelDelta().y);
break;
case os::Event::KeyDown:
if (ev.scancode() == os::kKeyEsc) {
if (m_nextEscCloses)
return false;
else
m_nextEscCloses = true;
logLine("-- Next KeyDown with kKeyEsc will close the window --");
}
else {
m_nextEscCloses = false;
}
//[[fallthrough]];
case os::Event::KeyUp: {
wchar_t wideUnicode[2] = { ev.unicodeChar(), 0 };
logLine("%s scancode=%d unicode=%d (%s)%s",
(ev.type() == os::Event::KeyDown ? "KeyDown": "KeyUp"),
ev.scancode(),
ev.unicodeChar(),
base::to_utf8(wideUnicode).c_str(),
modifiersToString(ev.modifiers()).c_str());
break;
}
case os::Event::TouchMagnify:
logLine("TouchMagnify %.4g",
ev.magnification());
m_brushSize += 32*ev.magnification();
m_brushSize = base::clamp(m_brushSize, 1.0, 500.0);
break;
default:
// Do nothing
break;
}
return true;
}
void flush() {
if (m_oldLogSize != m_textLog.size()) {
int newlines = m_textLog.size() - m_oldLogSize;
while (m_textLog.size() > m_maxlines)
m_textLog.erase(m_textLog.begin());
scrollAndDrawLog(newlines);
m_oldLogSize = m_textLog.size();
}
}
private:
void recalcMaxLines() {
m_maxlines = (m_display->height() - m_lineHeight) / m_lineHeight;
}
void scrollAndDrawLog(const int newlines) {
os::Surface* surface = m_display->surface();
os::SurfaceLock lock(surface);
const gfx::Rect rc = surface->bounds();
os::Paint p;
p.style(os::Paint::Fill);
p.color(gfx::rgba(0, 0, 0, 8));
// Scroll old lines
int i;
if (m_textLog.size() >= m_maxlines) {
int h = m_lineHeight*newlines;
surface->scrollTo(rc, 0, -h);
surface->drawRect(gfx::Rect(rc.x, rc.y, rc.w, rc.h-h), p);
p.color(gfx::rgba(0, 0, 0));
surface->drawRect(gfx::Rect(rc.x, rc.y+rc.h-h, rc.w, h), p);
i = (m_textLog.size()-newlines);
}
// First lines without scroll
else {
i = m_oldLogSize;
surface->drawRect(gfx::Rect(rc.x, rc.y, rc.w, i*m_lineHeight), p);
}
os::Paint paint;
paint.color(gfx::rgba(255, 255, 255));
for (; i<m_textLog.size(); ++i)
os::draw_text(surface, nullptr, m_textLog[i],
gfx::Point(0, (1+i)*m_lineHeight), &paint);
gfx::Rgb rgb(gfx::Hsv(m_hue, 1.0, 1.0));
paint.color(gfx::rgba(rgb.red(), rgb.green(), rgb.blue()));
paint.antialias(true);
surface->drawCircle(m_mousePos.x, m_mousePos.y, m_brushSize, paint);
// Invalidates the whole display to show it on the screen.
m_display->invalidateRegion(gfx::Region(rc));
}
void logMouseEvent(const os::Event& ev, const char* eventName) {
const os::Event::MouseButton mb = ev.button();
const os::PointerType pt = ev.pointerType();
m_mousePos = ev.position();
logLine("%s pos=%d,%d%s%s%s",
eventName,
ev.position().x,
ev.position().y,
(mb == os::Event::LeftButton ? " LeftButton":
mb == os::Event::RightButton ? " RightButton":
mb == os::Event::MiddleButton ? " MiddleButton":
mb == os::Event::X1Button ? " X1Button":
mb == os::Event::X2Button ? " X2Button": ""),
(pt == os::PointerType::Mouse ? " Mouse":
pt == os::PointerType::Touchpad ? " Touchpad":
pt == os::PointerType::Touch ? " Touch":
pt == os::PointerType::Pen ? " Pen":
pt == os::PointerType::Cursor ? " Cursor":
pt == os::PointerType::Eraser ? " Eraser": ""),
modifiersToString(ev.modifiers()).c_str());
}
void logLine(const char* str, ...) {
va_list ap;
va_start(ap, str);
char buf[4096];
vsprintf(buf, str, ap);
va_end(ap);
m_textLog.push_back(buf);
}
static std::string modifiersToString(os::KeyModifiers mods) {
std::string s;
if (mods & os::kKeyShiftModifier) s += " Shift";
if (mods & os::kKeyCtrlModifier ) s += " Ctrl";
if (mods & os::kKeyAltModifier ) s += " Alt";
if (mods & os::kKeyCmdModifier ) s += " Command";
if (mods & os::kKeySpaceModifier) s += " Space";
if (mods & os::kKeyWinModifier ) s += " Win";
return s;
}
os::DisplayRef m_display;
std::vector<std::string> m_textLog;
size_t m_oldLogSize = 0;
int m_lineHeight = 12;
int m_maxlines = 0;
gfx::Point m_mousePos;
double m_brushSize = 4;
double m_hue = 0.0;
bool m_nextEscCloses = false;
};
int app_main(int argc, char* argv[])
{
auto system = os::make_system();
system->setAppMode(os::AppMode::GUI);
LogWindow window(system.get());
system->finishLaunching();
system->activateApp();
os::EventQueue* queue = system->eventQueue();
while (true) {
window.flush();
os::Event ev;
queue->getEvent(ev, true);
if (!window.processEvent(ev))
break;
}
return 0;
}