hokusai-zero 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/Dockerfile +26 -0
- data/Gemfile +15 -0
- data/Gemfile.lock +91 -0
- data/LICENSE +21 -0
- data/README.md +28 -0
- data/ast/genheader +3 -0
- data/ast/include/hashmap.c +1151 -0
- data/ast/include/hashmap.c.license +20 -0
- data/ast/include/hashmap.h +54 -0
- data/ast/src/core/ast.c +448 -0
- data/ast/src/core/ast.h +259 -0
- data/ast/src/core/common.h +24 -0
- data/ast/src/core/component.c +85 -0
- data/ast/src/core/component.h +35 -0
- data/ast/src/core/hml.c +665 -0
- data/ast/src/core/hml.h +11 -0
- data/ast/src/core/input.c +458 -0
- data/ast/src/core/input.h +118 -0
- data/ast/src/core/style.c +101 -0
- data/ast/src/core/style.h +41 -0
- data/ast/src/core/text.c +784 -0
- data/ast/src/core/text.h +93 -0
- data/ast/src/core/util.c +140 -0
- data/ast/src/core/util.h +48 -0
- data/ast/src/hokusai.c +6 -0
- data/ast/src/hokusai.h +6 -0
- data/ast/test/fixtures/test.ui +13 -0
- data/ast/test/greatest.h +1266 -0
- data/ast/test/hokusai.c +14 -0
- data/ast/test/parser.c +234 -0
- data/ast/test/text.c +116 -0
- data/ext/extconf.rb +27 -0
- data/grammar/Cargo.lock +80 -0
- data/grammar/Cargo.toml +26 -0
- data/grammar/binding.gyp +20 -0
- data/grammar/bindings/node/binding.cc +28 -0
- data/grammar/bindings/node/index.js +19 -0
- data/grammar/bindings/rust/build.rs +40 -0
- data/grammar/bindings/rust/lib.rs +52 -0
- data/grammar/corpus/1_document.txt +131 -0
- data/grammar/corpus/2_selectors.txt +58 -0
- data/grammar/corpus/3_spaces.txt +69 -0
- data/grammar/corpus/4_errors.txt +10 -0
- data/grammar/corpus/5_macros.txt +175 -0
- data/grammar/corpus/6_styles.txt +81 -0
- data/grammar/grammar.js +275 -0
- data/grammar/package-lock.json +34 -0
- data/grammar/package.json +33 -0
- data/grammar/src/grammar.json +1269 -0
- data/grammar/src/node-types.json +474 -0
- data/grammar/src/parser.c +5772 -0
- data/grammar/src/scanner.c +258 -0
- data/grammar/src/tree_sitter/parser.h +230 -0
- data/grammar/src/tree_sitter/scanner.h +12 -0
- data/grammar/test.nml +10 -0
- data/hokusai.gemspec +19 -0
- data/ui/examples/assets/DigitalDisplay.ttf +0 -0
- data/ui/examples/assets/OpenSans-Regular.ttf +0 -0
- data/ui/examples/assets/addy.png +0 -0
- data/ui/examples/assets/baby_sean.png +0 -0
- data/ui/examples/assets/football-troll.png +0 -0
- data/ui/examples/assets/gear.png +0 -0
- data/ui/examples/assets/icecold.ttf +0 -0
- data/ui/examples/assets/science-troll.png +0 -0
- data/ui/examples/buddy.rb +31 -0
- data/ui/examples/clock.rb +58 -0
- data/ui/examples/counter.rb +123 -0
- data/ui/examples/dynamic.rb +147 -0
- data/ui/examples/foobar.rb +236 -0
- data/ui/examples/stock.rb +115 -0
- data/ui/examples/stock_decider/option.rb +74 -0
- data/ui/examples/tic_tac_toe.rb +246 -0
- data/ui/lib/lib_hokusai.rb +425 -0
- data/ui/spec/hokusai/ast_spec.rb +88 -0
- data/ui/spec/hokusai/automation/keys_transcoder_spec.rb +50 -0
- data/ui/spec/hokusai/automation/selector_spec.rb +68 -0
- data/ui/spec/hokusai/block_spec.rb +126 -0
- data/ui/spec/hokusai/directives_spec.rb +327 -0
- data/ui/spec/hokusai/e2e/client_spec.rb +58 -0
- data/ui/spec/hokusai/e2e/meta_spec.rb +42 -0
- data/ui/spec/hokusai/publisher_spec.rb +38 -0
- data/ui/spec/hokusai/slots_spec.rb +150 -0
- data/ui/spec/hokusai/util/piece_table_spec.rb +90 -0
- data/ui/spec/hokusai_spec.rb +0 -0
- data/ui/spec/spec_helper.rb +30 -0
- data/ui/src/hokusai/ast.rb +446 -0
- data/ui/src/hokusai/automation/client.rb +167 -0
- data/ui/src/hokusai/automation/constants.rb +98 -0
- data/ui/src/hokusai/automation/converters/selector_converter.rb +61 -0
- data/ui/src/hokusai/automation/driver.rb +54 -0
- data/ui/src/hokusai/automation/driver_command_queue.rb +50 -0
- data/ui/src/hokusai/automation/driver_commands/base.rb +79 -0
- data/ui/src/hokusai/automation/driver_commands/get_attribute.rb +41 -0
- data/ui/src/hokusai/automation/driver_commands/invoke.rb +33 -0
- data/ui/src/hokusai/automation/driver_commands/locate.rb +48 -0
- data/ui/src/hokusai/automation/driver_commands/trigger_keyboard.rb +94 -0
- data/ui/src/hokusai/automation/driver_commands/trigger_mouse.rb +213 -0
- data/ui/src/hokusai/automation/keys_transcoder.rb +128 -0
- data/ui/src/hokusai/automation/selector.rb +39 -0
- data/ui/src/hokusai/automation/server.rb +114 -0
- data/ui/src/hokusai/automation.rb +3 -0
- data/ui/src/hokusai/backends/raylib/config.rb +47 -0
- data/ui/src/hokusai/backends/raylib/font.rb +113 -0
- data/ui/src/hokusai/backends/raylib/keys.rb +124 -0
- data/ui/src/hokusai/backends/raylib.rb +449 -0
- data/ui/src/hokusai/backends/sdl2/Monaco.ttf +0 -0
- data/ui/src/hokusai/backends/sdl2/color.rb +12 -0
- data/ui/src/hokusai/backends/sdl2/config.rb +31 -0
- data/ui/src/hokusai/backends/sdl2/font.rb +127 -0
- data/ui/src/hokusai/backends/sdl2/keys.rb +119 -0
- data/ui/src/hokusai/backends/sdl2.rb +529 -0
- data/ui/src/hokusai/block.rb +237 -0
- data/ui/src/hokusai/blocks/button.rb +100 -0
- data/ui/src/hokusai/blocks/checkbox.rb +51 -0
- data/ui/src/hokusai/blocks/circle.rb +28 -0
- data/ui/src/hokusai/blocks/clipped.rb +23 -0
- data/ui/src/hokusai/blocks/cursor.rb +49 -0
- data/ui/src/hokusai/blocks/dynamic.rb +37 -0
- data/ui/src/hokusai/blocks/empty.rb +10 -0
- data/ui/src/hokusai/blocks/hblock.rb +35 -0
- data/ui/src/hokusai/blocks/image.rb +18 -0
- data/ui/src/hokusai/blocks/input.rb +200 -0
- data/ui/src/hokusai/blocks/label.rb +39 -0
- data/ui/src/hokusai/blocks/panel.rb +126 -0
- data/ui/src/hokusai/blocks/rect.rb +24 -0
- data/ui/src/hokusai/blocks/scissor_begin.rb +18 -0
- data/ui/src/hokusai/blocks/scissor_end.rb +12 -0
- data/ui/src/hokusai/blocks/scrollbar.rb +103 -0
- data/ui/src/hokusai/blocks/selectable.rb +77 -0
- data/ui/src/hokusai/blocks/svg.rb +20 -0
- data/ui/src/hokusai/blocks/text.rb +214 -0
- data/ui/src/hokusai/blocks/titlebar/osx.rb +145 -0
- data/ui/src/hokusai/blocks/toggle.rb +55 -0
- data/ui/src/hokusai/blocks/vblock.rb +35 -0
- data/ui/src/hokusai/commands/base.rb +22 -0
- data/ui/src/hokusai/commands/circle.rb +47 -0
- data/ui/src/hokusai/commands/image.rb +45 -0
- data/ui/src/hokusai/commands/rect.rb +158 -0
- data/ui/src/hokusai/commands/scissor.rb +22 -0
- data/ui/src/hokusai/commands/text.rb +92 -0
- data/ui/src/hokusai/commands.rb +87 -0
- data/ui/src/hokusai/diff.rb +124 -0
- data/ui/src/hokusai/error.rb +3 -0
- data/ui/src/hokusai/event.rb +54 -0
- data/ui/src/hokusai/events/keyboard.rb +84 -0
- data/ui/src/hokusai/events/mouse.rb +172 -0
- data/ui/src/hokusai/font.rb +280 -0
- data/ui/src/hokusai/meta.rb +152 -0
- data/ui/src/hokusai/mounting/loop_entry.rb +230 -0
- data/ui/src/hokusai/mounting/mount_entry.rb +74 -0
- data/ui/src/hokusai/mounting/update_entry.rb +101 -0
- data/ui/src/hokusai/node.rb +98 -0
- data/ui/src/hokusai/node_mounter.rb +102 -0
- data/ui/src/hokusai/painter.rb +214 -0
- data/ui/src/hokusai/publisher.rb +32 -0
- data/ui/src/hokusai/style.rb +72 -0
- data/ui/src/hokusai/types.rb +266 -0
- data/ui/src/hokusai/util/clamping_iterator.rb +202 -0
- data/ui/src/hokusai/util/piece_table.rb +111 -0
- data/ui/src/hokusai/util/selection.rb +145 -0
- data/ui/src/hokusai.rb +120 -0
- data/ui/vendor/.gitkeep +0 -0
- data/vendor/.gitkeep +0 -0
- data/xmake.lua +192 -0
- metadata +222 -0
@@ -0,0 +1,458 @@
|
|
1
|
+
#ifndef HOKU_CORE_INPUT
|
2
|
+
#define HOKU_CORE_INPUT
|
3
|
+
|
4
|
+
#include "input.h"
|
5
|
+
#include <stdio.h>
|
6
|
+
|
7
|
+
static int hoku_keycodes[HOKU_KEY_MAX] = {
|
8
|
+
0,39,44,45,46,47,48,49,50,51,52,53,54,55,56,57,59,61,65,66,67,68,69,
|
9
|
+
70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,
|
10
|
+
93,96,32,256,257,258,259,260,261,262,263,264,265,266,267,268,269,280,
|
11
|
+
281,282,283,284,290,291,292,293,294,295,296,297,298,299,300,301,340,341,
|
12
|
+
342,343,344,345,346,347,348,320,321,322,323,324,325,326,327,328,329,330,
|
13
|
+
331,332,333,334,335,336,4,5,24,25
|
14
|
+
};
|
15
|
+
|
16
|
+
void hoku_input_mouse_free(hoku_input_mouse* mouse)
|
17
|
+
{
|
18
|
+
free(mouse);
|
19
|
+
}
|
20
|
+
|
21
|
+
int hoku_input_mouse_init(hoku_input_mouse** mouse)
|
22
|
+
{
|
23
|
+
hoku_input_mouse* init = malloc(sizeof(hoku_input_mouse));
|
24
|
+
if (init == NULL) return -1;
|
25
|
+
|
26
|
+
hoku_vec2 zero = (hoku_vec2){ 0.0f, 0.0f };
|
27
|
+
hoku_dvec2 zerod = (hoku_dvec2){ 0.0f, 0.0f };
|
28
|
+
hoku_rect zero_rect = (hoku_rect){ .x=-1.0f, .y=-1.0f, .w=-1.0f, .h=-1.0f };
|
29
|
+
hoku_input_mouse_button zero_button = (hoku_input_mouse_button){ .up=0, .down=0, .clicked=0, .released=0 };
|
30
|
+
|
31
|
+
init->pos = malloc(sizeof(hoku_vec2));
|
32
|
+
if (init->pos == NULL) return -1;
|
33
|
+
init->pos->x = 0.0f;
|
34
|
+
init->pos->y = 0.0f;
|
35
|
+
|
36
|
+
init->delta = zerod;
|
37
|
+
init->scroll = 0.0f;
|
38
|
+
init->scroll_delta = 0.0f;
|
39
|
+
init->selection = zero_rect;
|
40
|
+
init->selection_type = HOKU_SELECT_NONE;
|
41
|
+
init->left = malloc(sizeof(hoku_input_mouse_button));
|
42
|
+
if (init->left == NULL) return -1;
|
43
|
+
init->left->up = 0;
|
44
|
+
init->left->down = 0;
|
45
|
+
init->left->clicked = 0;
|
46
|
+
init->left->released = 0;
|
47
|
+
|
48
|
+
init->middle = malloc(sizeof(hoku_input_mouse_button));
|
49
|
+
if (init->middle == NULL) return -1;
|
50
|
+
init->middle->up = 0;
|
51
|
+
init->middle->down = 0;
|
52
|
+
init->middle->clicked = 0;
|
53
|
+
init->middle->released = 0;
|
54
|
+
|
55
|
+
init->right = malloc(sizeof(hoku_input_mouse_button));
|
56
|
+
if (init->right == NULL) return -1;
|
57
|
+
init->right->up = 0;
|
58
|
+
init->right->down = 0;
|
59
|
+
init->right->clicked = 0;
|
60
|
+
init->right->released = 0;
|
61
|
+
|
62
|
+
*mouse = init;
|
63
|
+
return 0;
|
64
|
+
}
|
65
|
+
|
66
|
+
int hoku_input_keyboard_populate_keys(hoku_input_keyboard* keyboard)
|
67
|
+
{
|
68
|
+
hoku_input_keyboard_key* keys = malloc(sizeof(hoku_input_keyboard_key) * HOKU_KEY_MAX);
|
69
|
+
if (keys == NULL) return -1;
|
70
|
+
|
71
|
+
for (int i=0; i<HOKU_KEY_MAX; i++)
|
72
|
+
{
|
73
|
+
keys[i] = (hoku_input_keyboard_key){.code=hoku_keycodes[i], .key=((hoku_input_key) i), .up=true, .down=false, .released=false, .pressed=false};
|
74
|
+
}
|
75
|
+
|
76
|
+
keyboard->keys = keys;
|
77
|
+
return 0;
|
78
|
+
}
|
79
|
+
|
80
|
+
void hoku_input_keyboard_free(hoku_input_keyboard* keyboard)
|
81
|
+
{
|
82
|
+
free(keyboard->keys);
|
83
|
+
free(keyboard->pressed);
|
84
|
+
free(keyboard->released);
|
85
|
+
free(keyboard);
|
86
|
+
}
|
87
|
+
|
88
|
+
int hoku_input_keyboard_init(hoku_input_keyboard** keyboard)
|
89
|
+
{
|
90
|
+
hoku_input_keyboard* init = malloc(sizeof(hoku_input_keyboard));
|
91
|
+
if (init == NULL) return -1;
|
92
|
+
|
93
|
+
init->pressed = malloc(sizeof(hoku_input_keyboard_key_with_char) * 20);
|
94
|
+
if (init->pressed == NULL)
|
95
|
+
{
|
96
|
+
free(init);
|
97
|
+
return -1;
|
98
|
+
}
|
99
|
+
init->released = malloc(sizeof(hoku_input_keyboard_key_with_char) * 20);
|
100
|
+
if (init->released == NULL)
|
101
|
+
{
|
102
|
+
free(init->pressed);
|
103
|
+
free(init);
|
104
|
+
return -1;
|
105
|
+
}
|
106
|
+
|
107
|
+
if (hoku_input_keyboard_populate_keys(init) == -1)
|
108
|
+
{
|
109
|
+
free(init->pressed);
|
110
|
+
free(init->released);
|
111
|
+
free(init);
|
112
|
+
return -1;
|
113
|
+
}
|
114
|
+
|
115
|
+
init->allocated_len = 20;
|
116
|
+
init->pressed_len = 0;
|
117
|
+
init->released_len = 0;
|
118
|
+
init->shift = false;
|
119
|
+
init->ctrl = false;
|
120
|
+
init->alt = false;
|
121
|
+
init->super = false;
|
122
|
+
init->collecting = false;
|
123
|
+
|
124
|
+
*keyboard = init;
|
125
|
+
return 0;
|
126
|
+
}
|
127
|
+
|
128
|
+
void hoku_input_keyboard_start(hoku_input* input)
|
129
|
+
{
|
130
|
+
input->keyboard->collecting = true;
|
131
|
+
// reset any pressed or released keys
|
132
|
+
input->keyboard->pressed_len = 0;
|
133
|
+
input->keyboard->released_len = 0;
|
134
|
+
// reset modifiers
|
135
|
+
input->keyboard->shift = false;
|
136
|
+
input->keyboard->ctrl = false;
|
137
|
+
input->keyboard->alt = false;
|
138
|
+
input->keyboard->super = false;
|
139
|
+
}
|
140
|
+
|
141
|
+
void hoku_input_keyboard_stop(hoku_input* input)
|
142
|
+
{
|
143
|
+
input->keyboard->collecting = false;
|
144
|
+
}
|
145
|
+
|
146
|
+
bool hoku_input_key_is_letter(hoku_input_key key)
|
147
|
+
{
|
148
|
+
return (
|
149
|
+
key == HOKU_KEY_A ||
|
150
|
+
key == HOKU_KEY_B ||
|
151
|
+
key == HOKU_KEY_C ||
|
152
|
+
key == HOKU_KEY_D ||
|
153
|
+
key == HOKU_KEY_E ||
|
154
|
+
key == HOKU_KEY_F ||
|
155
|
+
key == HOKU_KEY_G ||
|
156
|
+
key == HOKU_KEY_H ||
|
157
|
+
key == HOKU_KEY_I ||
|
158
|
+
key == HOKU_KEY_J ||
|
159
|
+
key == HOKU_KEY_K ||
|
160
|
+
key == HOKU_KEY_L ||
|
161
|
+
key == HOKU_KEY_M ||
|
162
|
+
key == HOKU_KEY_N ||
|
163
|
+
key == HOKU_KEY_O ||
|
164
|
+
key == HOKU_KEY_P ||
|
165
|
+
key == HOKU_KEY_Q ||
|
166
|
+
key == HOKU_KEY_R ||
|
167
|
+
key == HOKU_KEY_S ||
|
168
|
+
key == HOKU_KEY_T ||
|
169
|
+
key == HOKU_KEY_U ||
|
170
|
+
key == HOKU_KEY_V ||
|
171
|
+
key == HOKU_KEY_W ||
|
172
|
+
key == HOKU_KEY_X ||
|
173
|
+
key == HOKU_KEY_Y ||
|
174
|
+
key == HOKU_KEY_Z
|
175
|
+
);
|
176
|
+
}
|
177
|
+
|
178
|
+
int hoku_get_code_from_key(hoku_input_key key, int source, bool shift)
|
179
|
+
{
|
180
|
+
int code = source;
|
181
|
+
if (!shift && hoku_input_key_is_letter(key)) { code = source + 32; }
|
182
|
+
else if (shift && key == HOKU_KEY_APOSTROPHE) { code = 34; }
|
183
|
+
else if (shift && key == HOKU_KEY_COMMA) { code = 60; }
|
184
|
+
else if (shift && key == HOKU_KEY_MINUS) { code = 95; }
|
185
|
+
else if (shift && key == HOKU_KEY_PERIOD) { code = 62; }
|
186
|
+
else if (shift && key == HOKU_KEY_SLASH) { code = 63; }
|
187
|
+
else if (shift && key == HOKU_KEY_ZERO) { code = 41; }
|
188
|
+
else if (shift && key == HOKU_KEY_ONE) { code = 33; }
|
189
|
+
else if (shift && key == HOKU_KEY_TWO) { code = 64; }
|
190
|
+
else if (shift && key == HOKU_KEY_THREE) { code = 35; }
|
191
|
+
else if (shift && key == HOKU_KEY_FOUR) { code = 36; }
|
192
|
+
else if (shift && key == HOKU_KEY_FIVE) { code = 37; }
|
193
|
+
else if (shift && key == HOKU_KEY_SIX) { code = 94; }
|
194
|
+
else if (shift && key == HOKU_KEY_SEVEN) { code = 38; }
|
195
|
+
else if (shift && key == HOKU_KEY_EIGHT) { code = 42; }
|
196
|
+
else if (shift && key == HOKU_KEY_NINE) { code = 40; }
|
197
|
+
else if (shift && key == HOKU_KEY_SEMICOLON) { code = 58; }
|
198
|
+
else if (shift && key == HOKU_KEY_EQUAL) { code = 43; }
|
199
|
+
else if (shift && key == HOKU_KEY_LEFT_BRACKET) { code = 123;}
|
200
|
+
else if (shift && key == HOKU_KEY_BACKSLASH) { code = 124; }
|
201
|
+
else if (shift && key == HOKU_KEY_RIGHT_BRACKET) { code = 125; }
|
202
|
+
else if (shift && key == HOKU_KEY_GRAVE) { code = 126; }
|
203
|
+
return code;
|
204
|
+
}
|
205
|
+
|
206
|
+
bool hoku_input_key_is_modifier(hoku_input_key key)
|
207
|
+
{
|
208
|
+
return (
|
209
|
+
key == HOKU_KEY_LEFT_SHIFT ||
|
210
|
+
key == HOKU_KEY_LEFT_CONTROL ||
|
211
|
+
key == HOKU_KEY_LEFT_ALT ||
|
212
|
+
key == HOKU_KEY_LEFT_SUPER ||
|
213
|
+
key == HOKU_KEY_RIGHT_SHIFT ||
|
214
|
+
key == HOKU_KEY_RIGHT_CONTROL ||
|
215
|
+
key == HOKU_KEY_RIGHT_ALT ||
|
216
|
+
key == HOKU_KEY_RIGHT_SUPER
|
217
|
+
);
|
218
|
+
}
|
219
|
+
|
220
|
+
void hoku_input_keyboard_set_key(hoku_input* input, hoku_input_key key, bool down)
|
221
|
+
{
|
222
|
+
if (hoku_input_key_is_modifier(key) && down && (key == HOKU_KEY_LEFT_SHIFT || key == HOKU_KEY_RIGHT_SHIFT)) input->keyboard->shift = true;
|
223
|
+
if (hoku_input_key_is_modifier(key) && down && (key == HOKU_KEY_LEFT_CONTROL || key == HOKU_KEY_RIGHT_CONTROL)) input->keyboard->ctrl = true;
|
224
|
+
if (hoku_input_key_is_modifier(key) && down && (key == HOKU_KEY_LEFT_ALT || key == HOKU_KEY_RIGHT_ALT)) input->keyboard->alt = true;
|
225
|
+
if (hoku_input_key_is_modifier(key) && down && (key == HOKU_KEY_LEFT_SUPER || key == HOKU_KEY_RIGHT_SUPER)) input->keyboard->super = true;
|
226
|
+
|
227
|
+
bool shift = input->keyboard->shift;
|
228
|
+
hoku_input_keyboard_key keyboard_key = hoku_input_keyboard_get_key(input, key);
|
229
|
+
|
230
|
+
if (down && input->keyboard->keys[key].up)
|
231
|
+
{
|
232
|
+
// pressed condition
|
233
|
+
input->keyboard->keys[key].pressed = true;
|
234
|
+
input->keyboard->keys[key].released = false;
|
235
|
+
|
236
|
+
if (input->keyboard->pressed_len < 20 && !hoku_input_key_is_modifier(key))
|
237
|
+
{
|
238
|
+
int code = hoku_get_code_from_key(key, keyboard_key.code, shift);
|
239
|
+
input->keyboard->pressed[input->keyboard->pressed_len] = (hoku_input_keyboard_key_with_char) {.key=keyboard_key, .char_code=code};
|
240
|
+
input->keyboard->pressed_len++;
|
241
|
+
}
|
242
|
+
}
|
243
|
+
else if (!down && input->keyboard->keys[key].down)
|
244
|
+
{
|
245
|
+
// released condition
|
246
|
+
input->keyboard->keys[key].pressed = false;
|
247
|
+
input->keyboard->keys[key].released = true;
|
248
|
+
|
249
|
+
if (input->keyboard->released_len < 20 && !hoku_input_key_is_modifier(key))
|
250
|
+
{
|
251
|
+
int code = hoku_get_code_from_key(key, keyboard_key.code, shift);
|
252
|
+
input->keyboard->released[input->keyboard->released_len] = (hoku_input_keyboard_key_with_char) {.key=keyboard_key, .char_code=code};
|
253
|
+
input->keyboard->released_len++;
|
254
|
+
}
|
255
|
+
}
|
256
|
+
else
|
257
|
+
{
|
258
|
+
input->keyboard->keys[key].pressed = false;
|
259
|
+
input->keyboard->keys[key].released = false;
|
260
|
+
}
|
261
|
+
|
262
|
+
input->keyboard->keys[key].down = down;
|
263
|
+
input->keyboard->keys[key].up = !down;
|
264
|
+
|
265
|
+
}
|
266
|
+
|
267
|
+
hoku_input_keyboard_key hoku_input_keyboard_get_key(hoku_input* input, hoku_input_key key)
|
268
|
+
{
|
269
|
+
return input->keyboard->keys[key];
|
270
|
+
}
|
271
|
+
|
272
|
+
void hoku_input_free(hoku_input* input)
|
273
|
+
{
|
274
|
+
hoku_input_keyboard_free(input->keyboard);
|
275
|
+
hoku_input_mouse_free(input->mouse);
|
276
|
+
free(input);
|
277
|
+
}
|
278
|
+
|
279
|
+
int hoku_input_init(hoku_input** input)
|
280
|
+
{
|
281
|
+
|
282
|
+
hoku_input* init = malloc(sizeof(hoku_input));
|
283
|
+
if (init == NULL) return -1;
|
284
|
+
|
285
|
+
hoku_input_mouse* mouse;
|
286
|
+
if (hoku_input_mouse_init(&mouse) == -1)
|
287
|
+
{
|
288
|
+
free(init);
|
289
|
+
return -1;
|
290
|
+
}
|
291
|
+
init->mouse = mouse;
|
292
|
+
|
293
|
+
hoku_input_keyboard* keyboard;
|
294
|
+
if (hoku_input_keyboard_init(&keyboard) == -1)
|
295
|
+
{
|
296
|
+
free(init->mouse);
|
297
|
+
free(init);
|
298
|
+
return -1;
|
299
|
+
}
|
300
|
+
init->keyboard = keyboard;
|
301
|
+
|
302
|
+
*input = init;
|
303
|
+
return 0;
|
304
|
+
}
|
305
|
+
|
306
|
+
void hoku_input_set_mouse_position(hoku_input* head, hoku_vec2* pos)
|
307
|
+
{
|
308
|
+
hoku_vec2* last = head->mouse->pos;
|
309
|
+
|
310
|
+
double new_x = (double) (pos->x - last->x);
|
311
|
+
double new_y = (double) (pos->y - last->y);
|
312
|
+
|
313
|
+
head->mouse->delta = (hoku_dvec2){ new_x, new_y };
|
314
|
+
head->mouse->pos->x = pos->x;
|
315
|
+
head->mouse->pos->y = pos->y;
|
316
|
+
}
|
317
|
+
|
318
|
+
void hoku_input_mouse_set_scroll(hoku_input* head, float scroll_y)
|
319
|
+
{
|
320
|
+
float last = head->mouse->scroll;
|
321
|
+
|
322
|
+
float new_y = (last >= scroll_y) ? last - scroll_y : scroll_y - last;
|
323
|
+
head->mouse->scroll_delta = new_y;
|
324
|
+
head->mouse->scroll = scroll_y;
|
325
|
+
}
|
326
|
+
|
327
|
+
void hoku_input_mouse_set_selection(hoku_input** input, hoku_input_mouse_button current)
|
328
|
+
{
|
329
|
+
/** select logic
|
330
|
+
* if prev up && current click
|
331
|
+
* set selection.x && selection.y
|
332
|
+
* if prev->selection.x && prev->selection.y && current mousedown
|
333
|
+
* set selection.w && selection.y
|
334
|
+
* if selection filled && click
|
335
|
+
* unset selection
|
336
|
+
*/
|
337
|
+
|
338
|
+
// printf("current down? %d\n", current.down);
|
339
|
+
hoku_input* head = *input;
|
340
|
+
hoku_input_mouse* mouse = head->mouse;
|
341
|
+
|
342
|
+
// freeze
|
343
|
+
if(mouse->left->up && mouse->selection_type & (HOKU_SELECT_ACTIVE))
|
344
|
+
{
|
345
|
+
mouse->selection_type = HOKU_SELECT_FROZEN;
|
346
|
+
}
|
347
|
+
// unfreeze select -> populate sx and sy but not ex/ey (sets cursor); set to selecting
|
348
|
+
else if (current.down && mouse->selection_type & (HOKU_SELECT_NONE | HOKU_SELECT_FROZEN))
|
349
|
+
{
|
350
|
+
mouse->selection.x = mouse->pos->x;
|
351
|
+
mouse->selection.y = mouse->pos->y;
|
352
|
+
mouse->selection.w = -1.0f;
|
353
|
+
mouse->selection.h = -1.0f;
|
354
|
+
mouse->selection_type = HOKU_SELECT_ACTIVE;
|
355
|
+
}
|
356
|
+
// start updating w/h
|
357
|
+
else if(current.down && mouse->left->down && mouse->selection_type & (HOKU_SELECT_ACTIVE))
|
358
|
+
{
|
359
|
+
mouse->selection.w = mouse->pos->x;
|
360
|
+
mouse->selection.h = mouse->pos->y;
|
361
|
+
}
|
362
|
+
|
363
|
+
*input = head;
|
364
|
+
}
|
365
|
+
|
366
|
+
void hoku_input_mouse_set_button(hoku_input* head, hoku_input_mouse_button* button, int type)
|
367
|
+
{
|
368
|
+
switch(type)
|
369
|
+
{
|
370
|
+
case 0:
|
371
|
+
{
|
372
|
+
head->mouse->left->down = button->down;
|
373
|
+
head->mouse->left->up = button->up;
|
374
|
+
head->mouse->left->clicked = button->clicked;
|
375
|
+
head->mouse->left->released = button->released;
|
376
|
+
|
377
|
+
break;
|
378
|
+
}
|
379
|
+
case 1:
|
380
|
+
{
|
381
|
+
head->mouse->middle->down = button->down;
|
382
|
+
head->mouse->middle->up = button->up;
|
383
|
+
head->mouse->middle->clicked = button->clicked;
|
384
|
+
head->mouse->middle->released = button->released;
|
385
|
+
break;
|
386
|
+
}
|
387
|
+
case 2:
|
388
|
+
{
|
389
|
+
head->mouse->right->down = button->down;
|
390
|
+
head->mouse->right->up = button->up;
|
391
|
+
head->mouse->right->clicked = button->clicked;
|
392
|
+
head->mouse->right->released = button->released;
|
393
|
+
break;
|
394
|
+
}
|
395
|
+
default:
|
396
|
+
{
|
397
|
+
break;
|
398
|
+
}
|
399
|
+
}
|
400
|
+
}
|
401
|
+
|
402
|
+
void hoku_input_mouse_set_left_button(hoku_input* input, hoku_input_mouse_button* button)
|
403
|
+
{
|
404
|
+
hoku_input_mouse_set_button(input, button, 0);
|
405
|
+
}
|
406
|
+
|
407
|
+
void hoku_input_mouse_set_middle_button(hoku_input* input, hoku_input_mouse_button* button)
|
408
|
+
{
|
409
|
+
hoku_input_mouse_set_button(input, button, 1);
|
410
|
+
}
|
411
|
+
|
412
|
+
void hoku_input_mouse_set_right_button(hoku_input* input, hoku_input_mouse_button* button)
|
413
|
+
{
|
414
|
+
hoku_input_mouse_set_button(input, button, 2);
|
415
|
+
}
|
416
|
+
|
417
|
+
bool hoku_input_is_hovered(hoku_input* input, hoku_rect* rect)
|
418
|
+
{
|
419
|
+
hoku_vec2* pos = input->mouse->pos;
|
420
|
+
return pos->x >= rect->x && pos->x <= rect->x + rect->w && pos->y >= rect->y && pos->y <= rect->y + rect->h;
|
421
|
+
}
|
422
|
+
|
423
|
+
|
424
|
+
bool hoku_input_is_any_clicked(hoku_input* input, hoku_rect* rect, int which)
|
425
|
+
{
|
426
|
+
|
427
|
+
bool clicked = false;
|
428
|
+
switch(which)
|
429
|
+
{
|
430
|
+
case 0:
|
431
|
+
{
|
432
|
+
clicked = input->mouse->left->clicked;
|
433
|
+
break;
|
434
|
+
}
|
435
|
+
case 1:
|
436
|
+
{
|
437
|
+
clicked = input->mouse->middle->clicked;
|
438
|
+
break;
|
439
|
+
}
|
440
|
+
case 2:
|
441
|
+
{
|
442
|
+
clicked = input->mouse->right->clicked;
|
443
|
+
break;
|
444
|
+
}
|
445
|
+
default:
|
446
|
+
{
|
447
|
+
break;
|
448
|
+
}
|
449
|
+
}
|
450
|
+
|
451
|
+
return clicked && hoku_input_is_hovered(input, rect);
|
452
|
+
}
|
453
|
+
|
454
|
+
bool hoku_input_is_clicked(hoku_input* input, hoku_rect* rect)
|
455
|
+
{
|
456
|
+
return hoku_input_is_any_clicked(input, rect, 0);
|
457
|
+
}
|
458
|
+
#endif
|
@@ -0,0 +1,118 @@
|
|
1
|
+
#ifndef HOKU_CORE_INPUT_H
|
2
|
+
#define HOKU_CORE_INPUT_H
|
3
|
+
|
4
|
+
#include "common.h"
|
5
|
+
#include <stdbool.h>
|
6
|
+
#include <stdlib.h>
|
7
|
+
|
8
|
+
typedef struct HmlInputMouseButton
|
9
|
+
{
|
10
|
+
bool down;
|
11
|
+
bool up;
|
12
|
+
bool clicked;
|
13
|
+
bool released;
|
14
|
+
} hoku_input_mouse_button;
|
15
|
+
|
16
|
+
typedef enum HmlInputSelectType {
|
17
|
+
HOKU_SELECT_ACTIVE = 1 << 1,
|
18
|
+
HOKU_SELECT_FROZEN = 1 << 2,
|
19
|
+
HOKU_SELECT_NONE = 1 << 3
|
20
|
+
} hoku_input_select_type;
|
21
|
+
|
22
|
+
typedef struct HmlInputMouse
|
23
|
+
{
|
24
|
+
hoku_vec2* pos;
|
25
|
+
hoku_dvec2 delta;
|
26
|
+
float scroll;
|
27
|
+
float scroll_delta;
|
28
|
+
hoku_rect selection;
|
29
|
+
hoku_input_select_type selection_type;
|
30
|
+
hoku_input_mouse_button* left;
|
31
|
+
hoku_input_mouse_button* middle;
|
32
|
+
hoku_input_mouse_button* right;
|
33
|
+
} hoku_input_mouse;
|
34
|
+
|
35
|
+
|
36
|
+
typedef enum HmlInputKey
|
37
|
+
{
|
38
|
+
HOKU_KEY_NULL, HOKU_KEY_APOSTROPHE, HOKU_KEY_COMMA, HOKU_KEY_MINUS, HOKU_KEY_PERIOD,
|
39
|
+
HOKU_KEY_SLASH, HOKU_KEY_ZERO, HOKU_KEY_ONE, HOKU_KEY_TWO, HOKU_KEY_THREE, HOKU_KEY_FOUR, HOKU_KEY_FIVE,
|
40
|
+
HOKU_KEY_SIX, HOKU_KEY_SEVEN, HOKU_KEY_EIGHT, HOKU_KEY_NINE, HOKU_KEY_SEMICOLON,
|
41
|
+
HOKU_KEY_EQUAL, HOKU_KEY_A, HOKU_KEY_B, HOKU_KEY_C, HOKU_KEY_D, HOKU_KEY_E, HOKU_KEY_F,
|
42
|
+
HOKU_KEY_G, HOKU_KEY_H, HOKU_KEY_I, HOKU_KEY_J, HOKU_KEY_K, HOKU_KEY_L,
|
43
|
+
HOKU_KEY_M, HOKU_KEY_N, HOKU_KEY_O, HOKU_KEY_P, HOKU_KEY_Q, HOKU_KEY_R, HOKU_KEY_S,
|
44
|
+
HOKU_KEY_T, HOKU_KEY_U, HOKU_KEY_V, HOKU_KEY_W, HOKU_KEY_X,
|
45
|
+
HOKU_KEY_Y, HOKU_KEY_Z, HOKU_KEY_LEFT_BRACKET, HOKU_KEY_BACKSLASH,
|
46
|
+
HOKU_KEY_RIGHT_BRACKET, HOKU_KEY_GRAVE, HOKU_KEY_SPACE, HOKU_KEY_ESCAPE,
|
47
|
+
HOKU_KEY_ENTER, HOKU_KEY_TAB, HOKU_KEY_BACKSPACE, HOKU_KEY_INSERT, HOKU_KEY_DELETE, HOKU_KEY_RIGHT,
|
48
|
+
HOKU_KEY_LEFT, HOKU_KEY_DOWN, HOKU_KEY_UP, HOKU_KEY_PAGE_UP, HOKU_KEY_PAGE_DOWN, HOKU_KEY_HOME,
|
49
|
+
HOKU_KEY_END, HOKU_KEY_CAPS_LOCK, HOKU_KEY_SCROLL_LOCK, HOKU_KEY_NUM_LOCK, HOKU_KEY_PRINT_SCREEN, HOKU_KEY_PAUSE,
|
50
|
+
HOKU_KEY_F1, HOKU_KEY_F2, HOKU_KEY_F3, HOKU_KEY_F4,
|
51
|
+
HOKU_KEY_F5, HOKU_KEY_F6, HOKU_KEY_F7, HOKU_KEY_F8, HOKU_KEY_F9, HOKU_KEY_F10, HOKU_KEY_F11, HOKU_KEY_F12,
|
52
|
+
HOKU_KEY_LEFT_SHIFT, HOKU_KEY_LEFT_CONTROL, HOKU_KEY_LEFT_ALT, HOKU_KEY_LEFT_SUPER, HOKU_KEY_RIGHT_SHIFT,
|
53
|
+
HOKU_KEY_RIGHT_CONTROL, HOKU_KEY_RIGHT_ALT, HOKU_KEY_RIGHT_SUPER, HOKU_KEY_KB_MENU, HOKU_KEY_KP_0, HOKU_KEY_KP_1, HOKU_KEY_KP_2,
|
54
|
+
HOKU_KEY_KP_3, HOKU_KEY_KP_4, HOKU_KEY_KP_5, HOKU_KEY_KP_6, HOKU_KEY_KP_7, HOKU_KEY_KP_8, HOKU_KEY_KP_9,
|
55
|
+
HOKU_KEY_KP_DECIMAL, HOKU_KEY_KP_DIVIDE, HOKU_KEY_KP_MULTIPLY, HOKU_KEY_KP_SUBTRACT, HOKU_KEY_KP_ADD, HOKU_KEY_KP_ENTER, HOKU_KEY_KP_EQUAL,
|
56
|
+
HOKU_KEY_BACK, HOKU_KEY_MENU, HOKU_KEY_VOLUME_UP, HOKU_KEY_VOLUME_DOWN, HOKU_KEY_MAX
|
57
|
+
} hoku_input_key;
|
58
|
+
|
59
|
+
typedef struct HmlInputKeyboardKey
|
60
|
+
{
|
61
|
+
int code; // some code, not super useful...
|
62
|
+
bool down; // is key down
|
63
|
+
bool up; // is key up
|
64
|
+
bool released; // was key down and now up
|
65
|
+
bool pressed; // was key up and now down
|
66
|
+
hoku_input_key key;
|
67
|
+
} hoku_input_keyboard_key;
|
68
|
+
|
69
|
+
typedef struct HmlInputKeyboardKeyWithChar
|
70
|
+
{
|
71
|
+
hoku_input_keyboard_key key; // the keyboard key
|
72
|
+
int char_code; // the resolved character
|
73
|
+
} hoku_input_keyboard_key_with_char;
|
74
|
+
|
75
|
+
typedef struct HmlInputKeyboard
|
76
|
+
{
|
77
|
+
int allocated_len;
|
78
|
+
hoku_input_keyboard_key_with_char* pressed;
|
79
|
+
int pressed_len;
|
80
|
+
hoku_input_keyboard_key_with_char* released;
|
81
|
+
int released_len;
|
82
|
+
bool shift;
|
83
|
+
bool super;
|
84
|
+
bool ctrl;
|
85
|
+
bool alt;
|
86
|
+
hoku_input_keyboard_key* keys;
|
87
|
+
bool collecting;
|
88
|
+
} hoku_input_keyboard;
|
89
|
+
|
90
|
+
typedef struct HmlInput
|
91
|
+
{
|
92
|
+
hoku_input_keyboard* keyboard;
|
93
|
+
hoku_input_mouse* mouse;
|
94
|
+
} hoku_input;
|
95
|
+
|
96
|
+
|
97
|
+
int hoku_input_keyboard_init(hoku_input_keyboard** keyboard);
|
98
|
+
int hoku_input_mouse_init(hoku_input_mouse** mouse);
|
99
|
+
int hoku_input_init(hoku_input** input);
|
100
|
+
void hoku_input_free(hoku_input* input);
|
101
|
+
void hoku_input_set_mouse_position(hoku_input* input, hoku_vec2* pos);
|
102
|
+
void hoku_input_mouse_set_button(hoku_input* input, hoku_input_mouse_button* button, int type);
|
103
|
+
void hoku_input_mouse_set_left_button(hoku_input* input, hoku_input_mouse_button* button);
|
104
|
+
void hoku_input_mouse_set_middle_button(hoku_input* input, hoku_input_mouse_button* button);
|
105
|
+
void hoku_input_mouse_set_right_button(hoku_input* input, hoku_input_mouse_button* button);
|
106
|
+
void hoku_input_mouse_set_selection(hoku_input** input, hoku_input_mouse_button current);
|
107
|
+
|
108
|
+
void hoku_input_mouse_set_scroll(hoku_input* input, float scroll_y);
|
109
|
+
|
110
|
+
void hoku_input_keyboard_start(hoku_input* input);
|
111
|
+
void hoku_input_keyboard_set_key(hoku_input* input, hoku_input_key key, bool down);
|
112
|
+
hoku_input_keyboard_key hoku_input_keyboard_get_key(hoku_input* input, hoku_input_key key);
|
113
|
+
void hoku_input_keyboard_stop(hoku_input* input);
|
114
|
+
|
115
|
+
bool hoku_input_is_clicked(hoku_input* input, hoku_rect* rect);
|
116
|
+
bool hoku_input_is_any_clicked(hoku_input* input, hoku_rect* rect, int which);
|
117
|
+
bool hoku_input_is_hovered(hoku_input* input, hoku_rect* rect);
|
118
|
+
#endif
|
@@ -0,0 +1,101 @@
|
|
1
|
+
#ifndef HOKU_CORE_STYLE
|
2
|
+
#define HOKU_CORE_STYLE
|
3
|
+
|
4
|
+
#include "style.h"
|
5
|
+
|
6
|
+
int hoku_style_attribute_init(hoku_style_attribute** attribute, char* name, char* value, enum HOKU_STYLE_TYPE type)
|
7
|
+
{
|
8
|
+
hoku_style_attribute* init = malloc(sizeof(hoku_style_attribute));
|
9
|
+
if (init == NULL) return -1;
|
10
|
+
|
11
|
+
init->name = strdup(name);
|
12
|
+
if (init->name == NULL) return -1;
|
13
|
+
|
14
|
+
init->value = strdup(value);
|
15
|
+
if (init->value == NULL) return -1;
|
16
|
+
|
17
|
+
init->type = type;
|
18
|
+
init->function_name = NULL;
|
19
|
+
init->next = NULL;
|
20
|
+
|
21
|
+
*attribute = init;
|
22
|
+
|
23
|
+
return 0;
|
24
|
+
}
|
25
|
+
|
26
|
+
int hoku_style_init(hoku_style** style, char* name)
|
27
|
+
{
|
28
|
+
hoku_style* init = malloc(sizeof(hoku_style));
|
29
|
+
if (init == NULL) return -1;
|
30
|
+
|
31
|
+
init->name = strdup(name);
|
32
|
+
if (init->name == NULL) return -1;
|
33
|
+
|
34
|
+
init->attributes = NULL;
|
35
|
+
init->next = NULL;
|
36
|
+
|
37
|
+
*style = init;
|
38
|
+
|
39
|
+
return 0;
|
40
|
+
}
|
41
|
+
|
42
|
+
|
43
|
+
void hoku_style_append(hoku_style* style, hoku_style* next)
|
44
|
+
{
|
45
|
+
hoku_style* head = style;
|
46
|
+
|
47
|
+
while (head->next)
|
48
|
+
{
|
49
|
+
head = head->next;
|
50
|
+
}
|
51
|
+
|
52
|
+
head->next = next;
|
53
|
+
}
|
54
|
+
|
55
|
+
void hoku_style_attribute_append(hoku_style_attribute* attribute, hoku_style_attribute* next)
|
56
|
+
{
|
57
|
+
hoku_style_attribute* head = attribute;
|
58
|
+
|
59
|
+
while (head->next)
|
60
|
+
{
|
61
|
+
head = head->next;
|
62
|
+
}
|
63
|
+
|
64
|
+
head->next = next;
|
65
|
+
head->next->next = NULL;
|
66
|
+
}
|
67
|
+
|
68
|
+
void hoku_style_attribute_free(hoku_style_attribute* attribute)
|
69
|
+
{
|
70
|
+
hoku_style_attribute* head = attribute;
|
71
|
+
hoku_style_attribute* current = NULL;
|
72
|
+
|
73
|
+
while (head)
|
74
|
+
{
|
75
|
+
current = head;
|
76
|
+
head = head->next;
|
77
|
+
|
78
|
+
free(current->name);
|
79
|
+
free(current->value);
|
80
|
+
if (current->function_name) free(current->function_name);
|
81
|
+
free(current);
|
82
|
+
}
|
83
|
+
}
|
84
|
+
|
85
|
+
void hoku_style_free(hoku_style* style)
|
86
|
+
{
|
87
|
+
hoku_style* head = style;
|
88
|
+
hoku_style* current = NULL;
|
89
|
+
|
90
|
+
while (head)
|
91
|
+
{
|
92
|
+
current = head;
|
93
|
+
head = head->next;
|
94
|
+
|
95
|
+
free(current->name);
|
96
|
+
hoku_style_attribute_free(current->attributes);
|
97
|
+
free(current);
|
98
|
+
}
|
99
|
+
}
|
100
|
+
|
101
|
+
#endif
|
@@ -0,0 +1,41 @@
|
|
1
|
+
#ifndef HOKU_CORE_STYLE_H
|
2
|
+
#define HOKU_CORE_STYLE_H
|
3
|
+
|
4
|
+
#include <string.h>
|
5
|
+
#include <stdlib.h>
|
6
|
+
#include <stdio.h>
|
7
|
+
|
8
|
+
enum HOKU_STYLE_TYPE
|
9
|
+
{
|
10
|
+
HOKU_STYLE_TYPE_INT,
|
11
|
+
HOKU_STYLE_TYPE_FLOAT,
|
12
|
+
HOKU_STYLE_TYPE_BOOL,
|
13
|
+
HOKU_STYLE_TYPE_STRING,
|
14
|
+
HOKU_STYLE_TYPE_FUNC
|
15
|
+
};
|
16
|
+
|
17
|
+
typedef struct HmlStyleAttribute
|
18
|
+
{
|
19
|
+
char* name;
|
20
|
+
char* function_name;
|
21
|
+
char* value;
|
22
|
+
enum HOKU_STYLE_TYPE type;
|
23
|
+
struct HmlStyleAttribute* next;
|
24
|
+
|
25
|
+
} hoku_style_attribute;
|
26
|
+
|
27
|
+
typedef struct HmlStyle
|
28
|
+
{
|
29
|
+
char* name;
|
30
|
+
struct HmlStyleAttribute* attributes;
|
31
|
+
struct HmlStyle* next;
|
32
|
+
} hoku_style;
|
33
|
+
|
34
|
+
int hoku_style_init(hoku_style** style, char* name);
|
35
|
+
int hoku_style_attribute_init(hoku_style_attribute** attribute, char* name, char* value, enum HOKU_STYLE_TYPE type);
|
36
|
+
void hoku_style_append(hoku_style* style, hoku_style* next);
|
37
|
+
void hoku_style_attribute_append(hoku_style_attribute* attribute, hoku_style_attribute* next);
|
38
|
+
void hoku_style_attribute_free(hoku_style_attribute* attribute);
|
39
|
+
void hoku_style_free(hoku_style* style);
|
40
|
+
|
41
|
+
#endif
|