2021-01-27 15:52:39 +03:00
|
|
|
// Copyright (c) 2020-2021 Raúl Hernández. All rights reserved.
|
2020-11-26 02:28:57 +03:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
2020-11-12 14:12:51 +03:00
|
|
|
module ui
|
|
|
|
|
|
|
|
pub enum KeyCode {
|
|
|
|
null = 0
|
|
|
|
tab = 9
|
|
|
|
enter = 10
|
|
|
|
escape = 27
|
|
|
|
space = 32
|
|
|
|
backspace = 127
|
|
|
|
|
|
|
|
exclamation = 33
|
|
|
|
double_quote = 34
|
|
|
|
hashtag = 35
|
|
|
|
dollar = 36
|
|
|
|
percent = 37
|
|
|
|
ampersand = 38
|
|
|
|
single_quote = 39
|
|
|
|
left_paren = 40
|
|
|
|
right_paren = 41
|
|
|
|
asterisk = 42
|
|
|
|
plus = 43
|
|
|
|
comma = 44
|
|
|
|
minus = 45
|
|
|
|
period = 46
|
|
|
|
slash = 47
|
|
|
|
|
|
|
|
_0 = 48
|
|
|
|
_1 = 49
|
|
|
|
_2 = 50
|
|
|
|
_3 = 51
|
|
|
|
_4 = 52
|
|
|
|
_5 = 53
|
|
|
|
_6 = 54
|
|
|
|
_7 = 55
|
|
|
|
_8 = 56
|
|
|
|
_9 = 57
|
|
|
|
|
|
|
|
colon = 58
|
|
|
|
semicolon = 59
|
|
|
|
less_than = 60
|
|
|
|
equal = 61
|
|
|
|
greater_than = 62
|
|
|
|
question_mark = 63
|
|
|
|
at = 64
|
|
|
|
|
|
|
|
a = 97
|
|
|
|
b = 98
|
|
|
|
c = 99
|
|
|
|
d = 100
|
|
|
|
e = 101
|
|
|
|
f = 102
|
|
|
|
g = 103
|
|
|
|
h = 104
|
|
|
|
i = 105
|
|
|
|
j = 106
|
|
|
|
k = 107
|
|
|
|
l = 108
|
|
|
|
m = 109
|
|
|
|
n = 110
|
|
|
|
o = 111
|
|
|
|
p = 112
|
|
|
|
q = 113
|
|
|
|
r = 114
|
|
|
|
s = 115
|
|
|
|
t = 116
|
|
|
|
u = 117
|
|
|
|
v = 118
|
|
|
|
w = 119
|
|
|
|
x = 120
|
|
|
|
y = 121
|
|
|
|
z = 122
|
|
|
|
|
|
|
|
left_square_bracket = 91
|
|
|
|
backslash = 92
|
|
|
|
right_square_bracket = 93
|
|
|
|
caret = 94
|
|
|
|
underscore = 95
|
|
|
|
backtick = 96
|
|
|
|
|
|
|
|
left_curly_bracket = 123
|
|
|
|
vertical_bar = 124
|
|
|
|
right_curly_bracket = 125
|
|
|
|
tilde = 126
|
|
|
|
|
|
|
|
insert = 260
|
|
|
|
delete = 261
|
|
|
|
up = 262
|
|
|
|
down = 263
|
|
|
|
right = 264
|
|
|
|
left = 265
|
|
|
|
page_up = 266
|
|
|
|
page_down = 267
|
|
|
|
home = 268
|
|
|
|
end = 269
|
|
|
|
|
|
|
|
f1 = 290
|
|
|
|
f2 = 291
|
|
|
|
f3 = 292
|
|
|
|
f4 = 293
|
|
|
|
f5 = 294
|
|
|
|
f6 = 295
|
|
|
|
f7 = 296
|
|
|
|
f8 = 297
|
|
|
|
f9 = 298
|
|
|
|
f10 = 299
|
|
|
|
f11 = 300
|
|
|
|
f12 = 301
|
2021-01-27 15:52:39 +03:00
|
|
|
f13 = 302
|
|
|
|
f14 = 303
|
|
|
|
f15 = 304
|
|
|
|
f16 = 305
|
|
|
|
f17 = 306
|
|
|
|
f18 = 307
|
|
|
|
f19 = 308
|
|
|
|
f20 = 309
|
|
|
|
f21 = 310
|
|
|
|
f22 = 311
|
|
|
|
f23 = 312
|
|
|
|
f24 = 313
|
2020-11-12 14:12:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub enum Direction {
|
|
|
|
unknown
|
|
|
|
up
|
|
|
|
down
|
|
|
|
left
|
|
|
|
right
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum MouseButton {
|
|
|
|
unknown
|
2020-11-14 10:14:54 +03:00
|
|
|
left
|
|
|
|
middle
|
|
|
|
right
|
2020-11-12 14:12:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub enum EventType {
|
|
|
|
unknown
|
|
|
|
mouse_down
|
|
|
|
mouse_up
|
|
|
|
mouse_move
|
|
|
|
mouse_drag
|
|
|
|
mouse_scroll
|
|
|
|
key_down
|
|
|
|
resized
|
|
|
|
}
|
|
|
|
|
2021-02-21 17:07:49 +03:00
|
|
|
[flag]
|
|
|
|
pub enum Modifiers {
|
|
|
|
ctrl
|
|
|
|
shift
|
|
|
|
alt
|
|
|
|
}
|
|
|
|
|
2020-11-12 14:12:51 +03:00
|
|
|
pub struct Event {
|
|
|
|
pub:
|
|
|
|
typ EventType
|
|
|
|
|
|
|
|
// Mouse event info
|
|
|
|
x int
|
|
|
|
y int
|
|
|
|
button MouseButton
|
|
|
|
direction Direction
|
|
|
|
|
|
|
|
// Keyboard event info
|
|
|
|
code KeyCode
|
2021-02-21 17:07:49 +03:00
|
|
|
modifiers Modifiers
|
2020-11-12 14:12:51 +03:00
|
|
|
ascii byte
|
|
|
|
utf8 string
|
|
|
|
|
|
|
|
// Resized event info
|
|
|
|
width int
|
|
|
|
height int
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Context {
|
2021-01-27 15:52:39 +03:00
|
|
|
ExtraContext // contains fields specific to an implementation
|
2020-11-12 14:12:51 +03:00
|
|
|
pub:
|
2021-05-08 13:32:29 +03:00
|
|
|
cfg Config // adsasdas
|
2020-11-12 14:12:51 +03:00
|
|
|
mut:
|
|
|
|
print_buf []byte
|
2020-11-14 22:05:22 +03:00
|
|
|
paused bool
|
2020-11-16 15:48:08 +03:00
|
|
|
enable_su bool
|
2020-11-26 02:28:57 +03:00
|
|
|
enable_rgb bool
|
2020-11-12 14:12:51 +03:00
|
|
|
pub mut:
|
|
|
|
frame_count u64
|
|
|
|
window_width int
|
|
|
|
window_height int
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Config {
|
2020-11-26 02:28:57 +03:00
|
|
|
user_data voidptr
|
|
|
|
init_fn fn(voidptr)
|
|
|
|
frame_fn fn(voidptr)
|
|
|
|
cleanup_fn fn(voidptr)
|
|
|
|
event_fn fn(&Event, voidptr)
|
|
|
|
fail_fn fn(string)
|
|
|
|
|
|
|
|
buffer_size int = 256
|
|
|
|
frame_rate int = 30
|
|
|
|
use_x11 bool
|
|
|
|
|
|
|
|
window_title string
|
|
|
|
hide_cursor bool
|
|
|
|
capture_events bool
|
2020-11-14 22:05:22 +03:00
|
|
|
use_alternate_buffer bool = true
|
2020-11-26 02:28:57 +03:00
|
|
|
skip_init_checks bool
|
|
|
|
// All kill signals to set up exit listeners on
|
|
|
|
reset []int = [1, 2, 3, 4, 6, 7, 8, 9, 11, 13, 14, 15, 19]
|
2020-11-12 14:12:51 +03:00
|
|
|
}
|
2021-01-27 15:52:39 +03:00
|
|
|
|
|
|
|
[inline]
|
|
|
|
fn (ctx &Context) init() {
|
|
|
|
if ctx.cfg.init_fn != voidptr(0) {
|
|
|
|
ctx.cfg.init_fn(ctx.cfg.user_data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
|
|
|
fn (ctx &Context) frame() {
|
|
|
|
if ctx.cfg.frame_fn != voidptr(0) {
|
|
|
|
ctx.cfg.frame_fn(ctx.cfg.user_data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
|
|
|
fn (ctx &Context) cleanup() {
|
|
|
|
if ctx.cfg.cleanup_fn != voidptr(0) {
|
|
|
|
ctx.cfg.cleanup_fn(ctx.cfg.user_data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
|
|
|
fn (ctx &Context) fail(error string) {
|
|
|
|
if ctx.cfg.fail_fn != voidptr(0) {
|
|
|
|
ctx.cfg.fail_fn(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
|
|
|
fn (ctx &Context) event(event &Event) {
|
|
|
|
if ctx.cfg.event_fn != voidptr(0) {
|
|
|
|
ctx.cfg.event_fn(event, ctx.cfg.user_data)
|
|
|
|
}
|
|
|
|
}
|