-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp-Events.cppm
More file actions
98 lines (87 loc) · 3.51 KB
/
App-Events.cppm
File metadata and controls
98 lines (87 loc) · 3.51 KB
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
// This project is licensed under the GPL-3.0 License. See the LICENSE file for details, or check out <gnu.org>.
// Copyright (C) 2026 mxreal64
module;
#include <SDL3/SDL.h>
#include <SDL3_ttf/SDL_ttf.h>
export module delta.app:events;
import delta.types;
import delta.engine;
import :buffer; // Internal import of the sibling partition slice
import std;
export class EventController {
public:
EventController() noexcept = default;
[[nodiscard]] bool process_input(
const SDL_Event& event,
DocumentBuffer& doc,
selection_span& sel,
TTF_Font* font,
const std::vector<LayoutLine>& layout,
bool& app_running
) noexcept {
bool changed = false;
if (event.type == SDL_EVENT_QUIT) {
app_running = false;
return false;
}
if (event.type == SDL_EVENT_TEXT_INPUT) {
if (sel.start != sel.end) {
doc.erase_range(sel.start, sel.end - sel.start);
sel.end = sel.start;
}
doc.insert_str(sel.start, event.text.text);
sel.start += std::strlen(event.text.text);
sel.end = sel.start;
changed = true;
}
if (event.type == SDL_EVENT_MOUSE_BUTTON_DOWN && event.button.button == SDL_BUTTON_LEFT) {
sel.active_drag = true;
sel.start = pixel_coords_to_buffer_index(font, layout, doc.get_raw(), event.button.x, event.button.y);
sel.end = sel.start;
}
if (event.type == SDL_EVENT_MOUSE_MOTION && sel.active_drag) {
sel.end = pixel_coords_to_buffer_index(font, layout, doc.get_raw(), event.motion.x, event.motion.y);
}
if (event.type == SDL_EVENT_MOUSE_BUTTON_UP && event.button.button == SDL_BUTTON_LEFT) {
sel.active_drag = false;
if (sel.start > sel.end) std::swap(sel.start, sel.end);
}
if (event.type == SDL_EVENT_KEY_DOWN) {
if (event.key.key == SDLK_ESCAPE) app_running = false;
if (event.key.key == SDLK_RETURN) {
if (sel.start != sel.end) {
doc.erase_range(sel.start, sel.end - sel.start);
sel.end = sel.start;
}
doc.insert_str(sel.start, "\n");
sel.start++; sel.end = sel.start;
changed = true;
}
if (event.key.key == SDLK_BACKSPACE) {
if (sel.start != sel.end) {
doc.erase_range(sel.start, sel.end - sel.start);
sel.end = sel.start;
changed = true;
} else if (sel.start > 0) {
char target = doc[sel.start - 1];
if (target == '\x02' || target == '\x03') {
if (sel.start > 1) {
doc.erase_range(sel.start - 2, 2); sel.start -= 2;
} else {
doc.erase_range(sel.start - 1, 1); sel.start--;
}
} else {
doc.erase_range(sel.start - 1, 1); sel.start--;
}
sel.end = sel.start;
changed = true;
}
}
if (event.key.mod & SDL_KMOD_CTRL) {
if (event.key.key == SDLK_B) { doc.inject_style(sel, '\x02'); changed = true; }
if (event.key.key == SDLK_I) { doc.inject_style(sel, '\x03'); changed = true; }
}
}
return changed;
}
};