-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4coder_qol_draw.cpp
More file actions
366 lines (317 loc) · 15.4 KB
/
4coder_qol_draw.cpp
File metadata and controls
366 lines (317 loc) · 15.4 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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
function void
qol_draw_hex_color(Application_Links *app, View_ID view, Buffer_ID buffer, Text_Layout_ID text_layout_id){
Scratch_Block scratch(app);
Range_i64 visible_range = text_layout_get_visible_range(app, text_layout_id);
String_Const_u8 buffer_string = push_buffer_range(app, scratch, buffer, visible_range);
for (i64 i = 0; i+9 < range_size(visible_range); i += 1){
u8 *str = buffer_string.str+i;
bool s0 = str[0] != '0';
bool s1 = str[1] != 'x';
if (s0 || s1){ continue; }
b32 all_hex = true;
for (i64 j = 0; j < 8; j += 1){
u8 c = str[j+2];
bool is_digit = '0' <= c && c <= '9';
bool is_lower = 'a' <= c && c <= 'f';
bool is_upper = 'A' <= c && c <= 'F';
if (!(is_digit || is_lower || is_upper)) { all_hex=false; break; }
}
if (!all_hex){ continue; }
i64 pos = visible_range.min + i;
Rect_f32 r0 = text_layout_character_on_screen(app, text_layout_id, pos+0);
Rect_f32 r1 = text_layout_character_on_screen(app, text_layout_id, pos+9);
Rect_f32 rect = rect_inner(rect_union(r0, r1), -1.f);
ARGB_Color color = ARGB_Color(string_to_integer(SCu8(str+2, 8), 16));
u32 sum = ((color >> 16) & 0xFF) + ((color >> 8) & 0xFF) + (color & 0xFF);
ARGB_Color contrast = ARGB_Color(0xFF000000 | (i32(sum > 330)-1));
// draw_rectangle_outline(app, rect_inner(rect, -1.f), 4.f, 2.f, contrast);
draw_rectangle(app, rect, 4.f, color);
paint_text_color(app, text_layout_id, Ii64_size(pos, 10), contrast);
i += 9;
}
}
function void
qol_draw_cursor_mark(Application_Links *app, View_ID view_id, b32 is_active_view,
Buffer_ID buffer, Text_Layout_ID text_layout_id,
f32 roundness, f32 outline_thickness){
b32 has_highlight_range = draw_highlight_range(app, view_id, buffer, text_layout_id, roundness);
i64 cursor_pos = view_get_cursor_pos(app, view_id);
i64 mark_pos = view_get_mark_pos(app, view_id);
Rect_f32 nxt_cursor_rect = text_layout_character_on_screen(app, text_layout_id, cursor_pos);
Rect_f32 cur_cursor_rect = Rf32_xy_wh(qol_cur_cursor_pos, rect_dim(nxt_cursor_rect));
if (is_active_view && nxt_cursor_rect.x1 > 0.f)
qol_nxt_cursor_pos = nxt_cursor_rect.p0;
if (!has_highlight_range){
ARGB_Color cl_cursor = fcolor_resolve(fcolor_id(defcolor_cursor, default_cursor_sub_id()));
if (is_active_view && rect_overlap(nxt_cursor_rect, cur_cursor_rect)){
// NOTE: Only paint once cursor is overlapping (from Jack Punter)
paint_text_color_pos(app, text_layout_id, cursor_pos, fcolor_id(defcolor_at_cursor));
} else if (!is_active_view) {
draw_rectangle_outline(app, nxt_cursor_rect, roundness, outline_thickness, cl_cursor);
}
draw_character_wire_frame(app, text_layout_id, mark_pos, roundness, outline_thickness, fcolor_id(defcolor_mark));
draw_rectangle(app, cur_cursor_rect, roundness, cl_cursor);
}
}
function void
qol_draw_comments(Application_Links *app, Buffer_ID buffer, Text_Layout_ID text_layout_id, Token_Array *array, Rect_f32 rect){
Scratch_Block scratch(app);
ARGB_Color cl_annotate = finalize_color(defcolor_comment_pop, 4);
b32 use_comment_annotations = def_get_config_b32(vars_save_string_lit("use_comment_annotations"));
Range_i64 visible_range = text_layout_get_visible_range(app, text_layout_id);
i64 first_index = token_index_from_pos(array, visible_range.first);
Token_Iterator_Array it = token_iterator_index(buffer, array, first_index);
for (;;){
Temp_Memory_Block temp(scratch);
Token *token = token_it_read(&it);
if (token->pos >= visible_range.one_past_last)
break;
String_Const_u8 tail = {};
if (token_it_check_and_get_lexeme(app, scratch, &it, TokenBaseKind_Comment, &tail)){
// paint @annotations in comments
if (use_comment_annotations){
for (i64 i = 1; i < token->size; i += 1){
if (tail.str[i] != '@' || !character_is_whitespace(tail.str[i-1])){ continue; }
Range_i64 range = Ii64(i,i+1);
for (i64 j = i+1; j < token->size; j += 1) {
char ch = tail.str[j];
if (character_is_whitespace(ch) || !character_is_alpha_numeric(ch))
break;
range.max++;
}
if (range_size(range) > 1){
paint_text_color(app, text_layout_id, range + token->pos, cl_annotate);
}
}
}
}
if (!token_it_inc_non_whitespace(&it))
break;
}
}
function void
qol_draw_function_tooltip_inner(Application_Links *app, Arena *arena, Buffer_ID buffer, Code_Index_Note *note, Range_f32 x_range, i64 depth, Range_i64 range){
Range_i64 def_range = note->pos;
find_nest_side(app, note->file->buffer, def_range.end+1, FindNest_Paren|FindNest_Balanced|FindNest_EndOfToken, Scan_Forward, NestDelim_Close, &def_range.end);
String_Const_u8 sig = push_buffer_range(app, arena, note->file->buffer, def_range);
sig = string_condense_whitespace(arena, sig);
Face_Metrics metrics = get_face_metrics(app, qol_small_face);
f32 wid = 2.f;
f32 pad = 2.f;
Rect_f32 rect = {};
rect.p0 = qol_cur_cursor_pos + V2f32(0, 2.f + depth*(metrics.line_height + pad + 2.f*wid));
rect.p1 = rect.p0 + V2f32(sig.size*metrics.normal_advance, wid + metrics.line_height);
f32 x_offset = ((rect.x1 > x_range.max)*(rect.x1 - x_range.max) -
(rect.x0 < x_range.min)*(rect.x0 - x_range.max));
rect.x0 -= x_offset;
rect.x1 -= x_offset;
draw_rectangle_fcolor(app, rect, 3.f, fcolor_id(defcolor_back));
rect.x0 -= pad;
rect.x1 += pad;
draw_rectangle_outline_fcolor(app, rect, 3.f, wid, fcolor_id(defcolor_ghost_character));
draw_string(app, qol_small_face, sig, rect.p0 + (pad + wid)*V2f32(1,1), fcolor_id(defcolor_ghost_character));
}
function void
qol_draw_function_tooltip(Application_Links *app, Buffer_ID buffer, Range_f32 x_range, i64 pos){
Token_Array tokens = get_token_array_from_buffer(app, buffer);
if (tokens.tokens == 0){ return; }
Token_Iterator_Array it = token_iterator_pos(0, &tokens, pos);
Token *token = token_it_read(&it);
if (token->kind == TokenBaseKind_ParentheticalOpen){
pos = token->pos + token->size;
} else if (token_it_dec_all(&it)){
token = token_it_read(&it);
if (token->kind == TokenBaseKind_ParentheticalClose && pos == token->pos + token->size){
pos = token->pos;
}
}
i64 count = 0;
Scratch_Block scratch(app);
Range_i64_Array ranges = get_enclosure_ranges(app, scratch, buffer, pos, FindNest_Paren);
code_index_lock();
for (i64 i=0; i < ranges.count; i += 1){
Scratch_Block temp(app, scratch);
Range_i64 range = ranges.ranges[i];
i64 f_pos = range.min-1;
Token_Iterator_Array cur_it = token_iterator_pos(0, &tokens, f_pos);
token = token_it_read(&cur_it);
if (token->kind == TokenBaseKind_Identifier){
String_Const_u8 lexeme = push_token_lexeme(app, temp, buffer, token);
Code_Index_Note *note = code_index_note_from_string(lexeme);
if (note == NULL){ continue; }
if (note->note_kind == CodeIndexNote_Function ||
note->note_kind == CodeIndexNote_Macro)
{
qol_draw_function_tooltip_inner(app, temp, buffer, note, x_range, ++count, range);
}
}
}
code_index_unlock();
}
function void
qol_draw_compile_errors(Application_Links *app, Buffer_ID buffer, Text_Layout_ID text_layout_id, Buffer_ID jump_buffer){
if (jump_buffer == 0){ return; }
Scratch_Block scratch(app);
Range_i64 visible_range = text_layout_get_visible_range(app, text_layout_id);
FColor cl_error = fcolor_blend(fcolor_id(defcolor_highlight_junk), 0.6f, fcolor_id(defcolor_text_default));
Managed_Scope scopes[2];
scopes[0] = buffer_get_managed_scope(app, jump_buffer);
scopes[1] = buffer_get_managed_scope(app, buffer);
Managed_Scope comp_scope = get_managed_scope_with_multiple_dependencies(app, scopes, ArrayCount(scopes));
Managed_Object *markers_object = scope_attachment(app, comp_scope, sticky_jump_marker_handle, Managed_Object);
i32 count = managed_object_get_item_count(app, *markers_object);
Marker *markers = push_array(scratch, Marker, count);
managed_object_load_data(app, *markers_object, 0, count, markers);
for (i32 i = 0; i < count; i += 1){
i64 line_number = get_line_number_from_pos(app, buffer, markers[i].pos);
Range_i64 line_range = get_line_pos_range(app, buffer, line_number);
if (!range_overlap(visible_range, line_range)){ continue; }
String_Const_u8 comp_line_string = push_buffer_line(app, scratch, jump_buffer, markers[i].line);
Parsed_Jump jump = parse_jump_location(comp_line_string);
if (!jump.success){ continue; }
i64 end_pos = get_line_end_pos(app, buffer, line_number)-1;
Rect_f32 end_rect = text_layout_character_on_screen(app, text_layout_id, end_pos);
Vec2_f32 p0 = V2f32(end_rect.x1, end_rect.y0 + 4.f);
String_Const_u8 error_string = string_skip(comp_line_string, jump.colon_position + 2);
draw_line_highlight(app, text_layout_id, line_number, fcolor_id(defcolor_highlight_junk));
draw_string(app, qol_small_face, error_string, p0, cl_error);
}
}
function Rect_f32
qol_draw_query_bars(Application_Links *app, Rect_f32 region, View_ID view_id, Face_ID face_id){
Face_Metrics face_metrics = get_face_metrics(app, face_id);
f32 line_height = face_metrics.line_height;
Query_Bar *space[32];
Query_Bar_Ptr_Array query_bars = {};
query_bars.ptrs = space;
if (get_active_query_bars(app, view_id, ArrayCount(space), &query_bars)){
for (i32 i = 0; i < query_bars.count; i += 1){
Rect_f32_Pair pair = layout_query_bar_on_bot(region, line_height, 1);
Query_Bar *query_bar = query_bars.ptrs[i];
Rect_f32 bar_rect = pair.max;
Scratch_Block scratch(app);
Fancy_Line list = {};
push_fancy_string(scratch, &list, fcolor_id(defcolor_pop1), query_bar->prompt);
push_fancy_string(scratch, &list, fcolor_id(defcolor_text_default), query_bar->string);
Vec2_f32 p = bar_rect.p0 + V2f32(2.f, 2.f);
p = draw_fancy_line(app, face_id, fcolor_zero(), &list, p);
if (i == 0){
draw_rectangle_fcolor(app, Rf32_xy_wh(p.x, p.y, 2.f, face_metrics.line_height), 0.f, fcolor_id(defcolor_cursor, 0));
}
region = pair.min;
}
}
return(region);
}
function void
qol_render_caller(Application_Links *app, Frame_Info frame_info, View_ID view_id){
ProfileScope(app, "qol render caller");
View_ID active_view = get_active_view(app, Access_Always);
b32 is_active_view = (active_view == view_id);
Rect_f32 region = draw_background_and_margin(app, view_id, is_active_view);
Rect_f32 prev_clip = draw_set_clip(app, region);
Buffer_ID buffer = view_get_buffer(app, view_id, Access_Always);
Face_ID face_id = get_face_id(app, buffer);
Face_Metrics face_metrics = get_face_metrics(app, face_id);
f32 line_height = face_metrics.line_height;
f32 normal_advance = face_metrics.normal_advance;
f32 digit_advance = face_metrics.decimal_digit_advance;
// NOTE(allen): query bars
region = qol_draw_query_bars(app, region, view_id, face_id);
// NOTE(allen): file bar
b64 showing_file_bar = false;
if (view_get_setting(app, view_id, ViewSetting_ShowFileBar, &showing_file_bar) && showing_file_bar){
b32 on_top = def_get_config_b32(vars_save_string_lit("filebar_on_top"));
Rect_f32_Pair pair = (on_top ?
layout_file_bar_on_top(region, line_height) :
layout_file_bar_on_bot(region, line_height));
draw_file_bar(app, view_id, buffer, face_id, pair.e[1-on_top]);
region = pair.e[on_top];
}
f32 char_count = def_get_config_f32(app, vars_save_string_lit("scroll_margin_x"));
f32 line_count = def_get_config_f32(app, vars_save_string_lit("scroll_margin_y"));
Vec2_f32 margin = V2f32(char_count*normal_advance, line_count*line_height);
view_set_camera_bounds(app, view_id, margin, V2f32(1,1));
Buffer_Scroll scroll = view_get_buffer_scroll(app, view_id);
Buffer_Point_Delta_Result delta = delta_apply(app, view_id, frame_info.animation_dt, scroll);
if (!block_match_struct(&scroll.position, &delta.point)){
if (is_active_view){
qol_cur_cursor_pos -= view_point_difference(app, view_id, delta.point, scroll.position);
}
block_copy_struct(&scroll.position, &delta.point);
view_set_buffer_scroll(app, view_id, scroll, SetBufferScroll_NoCursorChange);
}
if (delta.still_animating){
animate_in_n_milliseconds(app, 0);
}
// NOTE(allen): FPS hud
if (show_fps_hud){
Rect_f32_Pair pair = layout_fps_hud_on_bottom(region, line_height);
draw_fps_hud(app, frame_info, face_id, pair.max);
region = pair.min;
animate_in_n_milliseconds(app, 1000);
}
// NOTE(allen): layout line numbers
b32 show_line_number_margins = def_get_config_b32(vars_save_string_lit("show_line_number_margins"));
Rect_f32 line_number_rect = {};
if (show_line_number_margins){
Rect_f32_Pair pair = layout_line_number_margin(app, buffer, region, digit_advance);
line_number_rect = pair.min;
region = pair.max;
}
region = rect_split_left_right(region, 4.f).max;
// NOTE(allen): begin buffer render
Buffer_Point buffer_point = scroll.position;
Text_Layout_ID text_layout_id = text_layout_create(app, buffer, region, buffer_point);
// NOTE(allen): draw line numbers
if (show_line_number_margins){
draw_line_number_margin(app, view_id, buffer, face_id, text_layout_id, line_number_rect);
}
// NOTE(allen): draw the buffer
default_render_buffer(app, view_id, face_id, buffer, text_layout_id, region);
text_layout_free(app, text_layout_id);
draw_set_clip(app, prev_clip);
}
function Rect_f32
qol_buffer_region(Application_Links *app, View_ID view_id, Rect_f32 region){
Buffer_ID buffer = view_get_buffer(app, view_id, Access_Always);
Face_ID face_id = get_face_id(app, buffer);
Face_Metrics metrics = get_face_metrics(app, face_id);
f32 line_height = metrics.line_height;
f32 digit_advance = metrics.decimal_digit_advance;
// NOTE(allen): margins
region = rect_inner(region, 3.f);
// NOTE(allen): file bar
b64 showing_file_bar = false;
if (view_get_setting(app, view_id, ViewSetting_ShowFileBar, &showing_file_bar) &&
showing_file_bar){
b32 on_top = def_get_config_b32(vars_save_string_lit("filebar_on_top"));
Rect_f32_Pair pair = (on_top ?
layout_file_bar_on_top(region, line_height) :
layout_file_bar_on_bot(region, line_height));
region = pair.e[on_top];
}
// NOTE(allen): query bars
{
Query_Bar *space[32];
Query_Bar_Ptr_Array query_bars = {};
query_bars.ptrs = space;
if (get_active_query_bars(app, view_id, ArrayCount(space), &query_bars)){
Rect_f32_Pair pair = layout_query_bar_on_top(region, line_height, query_bars.count);
region = pair.max;
}
}
// NOTE(allen): FPS hud
if (show_fps_hud){
Rect_f32_Pair pair = layout_fps_hud_on_bottom(region, line_height);
region = pair.min;
}
// NOTE(allen): line numbers
b32 show_line_number_margins = def_get_config_b32(vars_save_string_lit("show_line_number_margins"));
if (show_line_number_margins){
Rect_f32_Pair pair = layout_line_number_margin(app, buffer, region, digit_advance);
region = pair.max;
}
region = rect_split_left_right(region, 4.f).max;
return(region);
}