forked from 4coder-archive/4coder_fleury
-
Notifications
You must be signed in to change notification settings - Fork 0
/
4coder_fleury_lego.cpp
159 lines (146 loc) · 5.36 KB
/
4coder_fleury_lego.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
#define F4_MAX_LEGOS 12
global F4_Lego f4_legos[F4_MAX_LEGOS];
function F4_Lego *
F4_LegoFromIndex(int index)
{
F4_Lego *result = 0;
if(index >= 0 && index < F4_MAX_LEGOS)
{
result = f4_legos + index;
}
return result;
}
function void
_F4_Lego_Initialize(F4_Lego *lego)
{
if(lego == 0)
{
return;
}
if(lego->initialized == 0)
{
memset(lego, 0, sizeof(*lego));
lego->initialized = 1;
lego->arena = make_arena(get_base_allocator_system());
}
}
function void
F4_Lego_Store(F4_Lego *lego, F4_LegoKind kind, String8 string)
{
_F4_Lego_Initialize(lego);
linalloc_clear(&lego->arena);
lego->kind = kind;
lego->string = push_string_copy(&lego->arena, string);
}
function F4_Lego *
F4_LegoFromUserInput(User_Input in)
{
F4_Lego *lego = 0;
Input_Event event = in.event;
if(event.kind == InputEventKind_KeyStroke &&
event.key.code >= KeyCode_F1 && event.key.code <= KeyCode_F24)
{
int index = event.key.code - KeyCode_F1;
index = index % 4;
lego = F4_LegoFromIndex(index);
}
return lego;
}
function void
F4_Lego_BufferPlace(Application_Links *app, View_ID view, Buffer_ID buffer, i64 pos, F4_Lego *lego)
{
switch(lego->kind)
{
case F4_LegoKind_String:
{
buffer_replace_range(app, buffer, Ii64(pos, pos), lego->string);
view_set_mark(app, view, seek_pos(pos));
view_set_cursor_and_preferred_x(app, view, seek_pos(pos + (i32)lego->string.size));
F4_PushFlash(app, buffer, Ii64(pos, pos+lego->string.size), fcolor_resolve(fcolor_id(fleury_color_lego_splat)), 0.8f);
}break;
default: break;
}
}
CUSTOM_COMMAND_SIG(f4_lego_buffer_place)
CUSTOM_DOC("Will place the lego, determined by the pressed F-key, at the cursor in the active buffer.")
{
F4_Lego *lego = F4_LegoFromUserInput(get_current_input(app));
View_ID view = get_active_view(app, Access_Write);
Buffer_ID buffer = view_get_buffer(app, view, Access_Write);
if(buffer)
{
F4_Lego_BufferPlace(app, view, buffer, view_get_cursor_pos(app, view), lego);
}
}
CUSTOM_COMMAND_SIG(f4_lego_store_token)
CUSTOM_DOC("Will store the token under the cursor into the lego determined by the associated F-key.")
{
Scratch_Block scratch(app);
F4_Lego *lego = F4_LegoFromUserInput(get_current_input(app));
if(lego)
{
View_ID view = get_active_view(app, Access_Always);
Buffer_ID buffer = view_get_buffer(app, view, Access_Always);
Token *token = get_token_from_pos(app, buffer, view_get_cursor_pos(app, view));
if(token != 0)
{
F4_Lego_Store(lego, F4_LegoKind_String, push_token_lexeme(app, scratch, buffer, token));
F4_PushFlash(app, buffer, Ii64(token), fcolor_resolve(fcolor_id(fleury_color_lego_grab)), 0.8f);
}
}
}
CUSTOM_COMMAND_SIG(f4_lego_store_range)
CUSTOM_DOC("Will store the selected range into the lego determined by the associated F-key.")
{
Scratch_Block scratch(app);
F4_Lego *lego = F4_LegoFromUserInput(get_current_input(app));
if(lego)
{
View_ID view = get_active_view(app, Access_Always);
Buffer_ID buffer = view_get_buffer(app, view, Access_Always);
Range_i64 range = Ii64(view_get_cursor_pos(app, view), view_get_mark_pos(app, view));
F4_Lego_Store(lego, F4_LegoKind_String, push_buffer_range(app, scratch, buffer, range));
F4_PushFlash(app, buffer, range, fcolor_resolve(fcolor_id(fleury_color_lego_grab)), 0.8f);
}
}
CUSTOM_COMMAND_SIG(f4_lego_store_line)
CUSTOM_DOC("Will store the selected range into the lego determined by the associated F-key.")
{
Scratch_Block scratch(app);
F4_Lego *lego = F4_LegoFromUserInput(get_current_input(app));
if(lego)
{
View_ID view = get_active_view(app, Access_Always);
Buffer_ID buffer = view_get_buffer(app, view, Access_Always);
i64 cursor_pos = view_get_cursor_pos(app, view);
i64 line_num = get_line_number_from_pos(app, buffer, cursor_pos);
Range_i64 range = get_line_pos_range(app, buffer, line_num);
F4_Lego_Store(lego, F4_LegoKind_String, push_buffer_range(app, scratch, buffer, range));
F4_PushFlash(app, buffer, range, fcolor_resolve(fcolor_id(fleury_color_lego_grab)), 0.8f);
}
}
function void
F4_Lego_StoreClickedToken(Application_Links *app, F4_Lego *lego)
{
Scratch_Block scratch(app);
View_ID view = get_active_view(app, Access_Always);
Buffer_ID buffer = view_get_buffer(app, view, Access_Always);
Mouse_State mouse = get_mouse_state(app);
i64 pos = view_pos_from_xy(app, view, V2f32(mouse.p));
Token *token = get_token_from_pos(app, buffer, pos);
if(token != 0)
{
F4_Lego_Store(lego, F4_LegoKind_String, push_token_lexeme(app, scratch, buffer, token));
F4_PushFlash(app, buffer, Ii64(token), fcolor_resolve(fcolor_id(fleury_color_lego_grab)), 0.8f);
}
}
CUSTOM_COMMAND_SIG(f4_lego_click_store_token_1)
CUSTOM_DOC("Sets the cursor to the clicked position, and then stores the token under that position into the F1 slot.")
{
F4_Lego_StoreClickedToken(app, F4_LegoFromIndex(0));
}
CUSTOM_COMMAND_SIG(f4_lego_click_store_token_2)
CUSTOM_DOC("Sets the cursor to the clicked position, and then stores the token under that position into the F2 slot.")
{
F4_Lego_StoreClickedToken(app, F4_LegoFromIndex(1));
}