forked from 4coder-archive/4coder_fleury
-
Notifications
You must be signed in to change notification settings - Fork 0
/
4coder_fleury_cursor.cpp
455 lines (387 loc) · 15.3 KB
/
4coder_fleury_cursor.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
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
//~ NOTE(rjf): Cursor rendering
global int global_cursor_count = 1;
global i64 global_cursor_positions[16] = {0};
global i64 global_mark_positions[16] = {0};
global int global_hide_region_boundary = 0;
enum Cursor_Type
{
cursor_none,
cursor_insert,
cursor_open_range,
cursor_close_range,
};
function void
C4_RenderCursorSymbolThingy(Application_Links *app, Rect_f32 rect,
f32 roundness, f32 thickness,
ARGB_Color color, Cursor_Type type)
{
f32 line_height = rect.y1 - rect.y0;
f32 bracket_width = 0.5f*line_height;
if(type == cursor_open_range)
{
Rect_f32 start_top, start_side, start_bottom;
Vec2_f32 start_p = {rect.x0, rect.y0};
start_top.x0 = start_p.x + thickness;
start_top.x1 = start_p.x + bracket_width;
start_top.y0 = start_p.y;
start_top.y1 = start_p.y + thickness;
start_bottom.x0 = start_top.x0;
start_bottom.x1 = start_top.x1;
start_bottom.y1 = start_p.y + line_height;
start_bottom.y0 = start_bottom.y1 - thickness;
start_side.x0 = start_p.x;
start_side.x1 = start_p.x + thickness;
start_side.y0 = start_top.y0;
start_side.y1 = start_bottom.y1;
draw_rectangle(app, start_top, roundness, color);
draw_rectangle(app, start_side, roundness, color);
}
else if(type == cursor_close_range)
{
Rect_f32 end_top, end_side, end_bottom;
Vec2_f32 end_p = {rect.x0, rect.y0};
end_top.x0 = end_p.x;
end_top.x1 = end_p.x - bracket_width;
end_top.y0 = end_p.y;
end_top.y1 = end_p.y + thickness;
end_side.x1 = end_p.x;
end_side.x0 = end_p.x + thickness;
end_side.y0 = end_p.y;
end_side.y1 = end_p.y + line_height;
end_bottom.x0 = end_top.x0;
end_bottom.x1 = end_top.x1;
end_bottom.y1 = end_p.y + line_height;
end_bottom.y0 = end_bottom.y1 - thickness;
draw_rectangle(app, end_bottom, roundness, color);
draw_rectangle(app, end_side, roundness, color);
}
else if(type == cursor_insert)
{
Rect_f32 side;
side.x0 = rect.x0;
side.x1 = rect.x0 + thickness;
side.y0 = rect.y0;
side.y1 = rect.y1;
draw_rectangle(app, side, roundness, color);
}
}
function void
DoTheCursorInterpolation(Application_Links *app, Frame_Info frame_info,
Rect_f32 *rect, Rect_f32 *last_rect, Rect_f32 target)
{
*last_rect = *rect;
float x_change = target.x0 - rect->x0;
float y_change = target.y0 - rect->y0;
b32 should_animate_cursor = !global_battery_saver && !def_get_config_b32(vars_save_string_lit("f4_disable_cursor_trails"));
if(should_animate_cursor)
{
if(fabs(x_change) > 1.f || fabs(y_change) > 1.f)
{
animate_in_n_milliseconds(app, 0);
}
}
else
{
*rect = *last_rect = target;
}
if(should_animate_cursor)
{
float mul = 1.0f - powf(1e-9f, frame_info.animation_dt);
rect->x0 += (target.x0 - rect->x0) * mul;
rect->y0 += (target.y0 - rect->y0) * mul;
rect->x1 += (target.x1 - rect->x1) * mul;
rect->y1 += (target.y1 - rect->y1) * mul;
}
if(target.y0 > last_rect->y0)
{
if(rect->y0 < last_rect->y0)
{
rect->y0 = last_rect->y0;
}
}
else
{
if(rect->y1 > last_rect->y1)
{
rect->y1 = last_rect->y1;
}
}
}
function void
F4_Cursor_RenderEmacsStyle(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, Frame_Info frame_info)
{
Rect_f32 view_rect = view_get_screen_rect(app, view_id);
Rect_f32 clip = draw_set_clip(app, view_rect);
Range_i64 visible_range = text_layout_get_visible_range(app, text_layout_id);
b32 has_highlight_range = draw_highlight_range(app, view_id, buffer, text_layout_id, roundness);
ColorFlags flags = 0;
flags |= !!global_keyboard_macro_is_recording * ColorFlag_Macro;
ARGB_Color cursor_color = F4_GetColor(app, ColorCtx_Cursor(flags, GlobalKeybindingMode));
ARGB_Color mark_color = cursor_color;
ARGB_Color inactive_cursor_color = F4_ARGBFromID(active_color_table, fleury_color_cursor_inactive, 0);
if(!F4_ARGBIsValid(inactive_cursor_color))
{
inactive_cursor_color = cursor_color;
}
if(is_active_view == 0)
{
cursor_color = inactive_cursor_color;
mark_color = inactive_cursor_color;
}
// TODO(rjf): REMOVE THIS
{
i64 cursor_pos = view_get_cursor_pos(app, view_id);
i64 mark_pos = view_get_mark_pos(app, view_id);
global_cursor_positions[0] = cursor_pos;
global_mark_positions[0] = mark_pos;
}
if(!has_highlight_range)
{
for(int i = 0; i < 1/*global_cursor_count*/; ++i)
{
i64 cursor_pos = global_cursor_positions[0];
i64 mark_pos = global_mark_positions[0];
Cursor_Type cursor_type = cursor_none;
Cursor_Type mark_type = cursor_none;
//- NOTE(jack): insert cursor unless cursor == mark
if (cursor_pos == mark_pos)
{
cursor_type = cursor_open_range;
mark_type = cursor_close_range;
}
else if(cursor_pos < mark_pos)
{
cursor_type = cursor_insert;
mark_type = cursor_close_range;
}
else
{
cursor_type = cursor_insert;
mark_type = cursor_open_range;
}
//-
if(global_hide_region_boundary)
{
cursor_type = cursor_insert;
mark_type = cursor_none;
}
Rect_f32 target_cursor = text_layout_character_on_screen(app, text_layout_id, cursor_pos);
Rect_f32 target_mark = text_layout_character_on_screen(app, text_layout_id, mark_pos);
// NOTE(rjf): Draw cursor.
{
if(is_active_view)
{
if(cursor_pos < visible_range.start || cursor_pos > visible_range.end)
{
f32 width = target_cursor.x1 - target_cursor.x0;
target_cursor.x0 = view_rect.x0;
target_cursor.x1 = target_cursor.x0 + width;
}
DoTheCursorInterpolation(app, frame_info, &global_cursor_rect,
&global_last_cursor_rect, target_cursor);
if(mark_pos > visible_range.end)
{
target_mark.x0 = 0;
target_mark.y0 = view_rect.y1;
target_mark.y1 = view_rect.y1;
}
if(mark_pos < visible_range.start || mark_pos > visible_range.end)
{
f32 width = target_mark.x1 - target_mark.x0;
target_mark.x0 = view_rect.x0;
target_mark.x1 = target_mark.x0 + width;
}
DoTheCursorInterpolation(app, frame_info, &global_mark_rect, &global_last_mark_rect,
target_mark);
}
// NOTE(rjf): Draw main cursor.
{
C4_RenderCursorSymbolThingy(app, global_cursor_rect, roundness, 2.f, cursor_color, cursor_type);
C4_RenderCursorSymbolThingy(app, target_cursor, roundness, 2.f, cursor_color, cursor_type);
}
}
C4_RenderCursorSymbolThingy(app, global_mark_rect, roundness, 2.f,
fcolor_resolve(fcolor_change_alpha(fcolor_argb(mark_color), 0.5f)), mark_type);
C4_RenderCursorSymbolThingy(app, target_mark, roundness, 2.f,
fcolor_resolve(fcolor_change_alpha(fcolor_argb(mark_color), 0.75f)), mark_type);
}
}
draw_set_clip(app, clip);
}
internal b32
F4_Cursor_DrawHighlightRange(Application_Links *app, View_ID view_id,
Buffer_ID buffer, Text_Layout_ID text_layout_id,
f32 roundness)
{
b32 has_highlight_range = false;
Managed_Scope scope = view_get_managed_scope(app, view_id);
Buffer_ID *highlight_buffer = scope_attachment(app, scope, view_highlight_buffer, Buffer_ID);
if (*highlight_buffer != 0){
if (*highlight_buffer != buffer){
view_disable_highlight_range(app, view_id);
}
else{
has_highlight_range = true;
Managed_Object *highlight = scope_attachment(app, scope, view_highlight_range, Managed_Object);
Marker marker_range[2];
if (managed_object_load_data(app, *highlight, 0, 2, marker_range)){
Range_i64 range = Ii64(marker_range[0].pos, marker_range[1].pos);
draw_character_block(app, text_layout_id, range, roundness,
fcolor_id(defcolor_highlight));
}
}
}
return(has_highlight_range);
}
function void
F4_Cursor_RenderNotepadStyle(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, Frame_Info frame_info)
{
Rect_f32 view_rect = view_get_screen_rect(app, view_id);
b32 has_highlight_range = draw_highlight_range(app, view_id, buffer, text_layout_id, roundness);
if(!has_highlight_range)
{
i64 cursor_pos = view_get_cursor_pos(app, view_id);
i64 mark_pos = view_get_mark_pos(app, view_id);
if (cursor_pos != mark_pos)
{
Range_i64 range = Ii64(cursor_pos, mark_pos);
draw_character_block(app, text_layout_id, range, roundness, fcolor_id(defcolor_highlight));
}
// NOTE(rjf): Draw cursor
{
ARGB_Color cursor_color = F4_GetColor(app, ColorCtx_Cursor(0, GlobalKeybindingMode));
ARGB_Color ghost_color = fcolor_resolve(fcolor_change_alpha(fcolor_argb(cursor_color), 0.5f));
Rect_f32 rect = text_layout_character_on_screen(app, text_layout_id, cursor_pos);
rect.x1 = rect.x0 + outline_thickness;
if(rect.x0 < view_rect.x0)
{
rect.x0 = view_rect.x0;
rect.x1 = view_rect.x0 + outline_thickness;
}
if(is_active_view)
{
DoTheCursorInterpolation(app, frame_info, &global_cursor_rect, &global_last_cursor_rect, rect);
}
draw_rectangle(app, global_cursor_rect, roundness, ghost_color);
draw_rectangle(app, rect, roundness, cursor_color);
}
}
}
static void
F4_HighlightCursorMarkRange(Application_Links *app, View_ID view_id)
{
Rect_f32 view_rect = view_get_screen_rect(app, view_id);
Rect_f32 clip = draw_set_clip(app, view_rect);
f32 lower_bound_y;
f32 upper_bound_y;
if(global_last_cursor_rect.y0 < global_last_mark_rect.y0)
{
lower_bound_y = global_last_cursor_rect.y0;
upper_bound_y = global_last_mark_rect.y1;
}
else
{
lower_bound_y = global_last_mark_rect.y0;
upper_bound_y = global_last_cursor_rect.y1;
}
draw_rectangle(app, Rf32(view_rect.x0, lower_bound_y, view_rect.x0 + 4, upper_bound_y), 3.f,
fcolor_resolve(fcolor_change_alpha(fcolor_id(defcolor_comment), 0.25f)));
draw_set_clip(app, clip);
}
function void
F4_Cursor_Render(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, Frame_Info frame_info)
{
// NOTE(rjf): Cursor
switch (fcoder_mode)
{
case FCoderMode_Original:
{
F4_Cursor_RenderEmacsStyle(app, view_id, is_active_view, buffer, text_layout_id, roundness, outline_thickness, frame_info);
}break;
case FCoderMode_NotepadLike:
{
F4_Cursor_RenderNotepadStyle(app, view_id, is_active_view, buffer, text_layout_id, roundness,
outline_thickness, frame_info);
break;
}
}
}
//~ NOTE(rjf): Mark Annotation
#if 0
function void
F4_RenderMarkAnnotation(Application_Links *app, Buffer_ID buffer, Text_Layout_ID text_layout_id,
View_ID view_id, b32 is_active_view)
{
i64 pos = view_get_mark_pos(app, view_id);
if(view_get_cursor_pos(app, view_id) > pos && is_active_view)
{
Scratch_Block scratch(app);
ProfileScope(app, "[Fleury] Mark Annotation");
Token_Array token_array = get_token_array_from_buffer(app, buffer);
Face_ID face_id = global_small_code_face;
Rect_f32 view_rect = view_get_screen_rect(app, view_id);
Token *start_token = 0;
if(token_array.tokens != 0)
{
Token_Iterator_Array it = token_iterator_pos(0, &token_array, pos);
int max = 5;
int count = 0;
for(Token *token = 0; (token = token_it_read(&it)) != 0 && count < max;
token_it_inc_non_whitespace(&it), ++count)
{
if(token->kind == TokenBaseKind_Comment ||
token->kind == TokenBaseKind_Identifier ||
token->kind == TokenBaseKind_Keyword)
{
start_token = token;
break;
}
}
}
// NOTE(rjf): Draw.
if(start_token)
{
String_Const_u8 start_line = push_buffer_line(app, scratch, buffer,
get_line_number_from_pos(app, buffer, start_token->pos));
u64 first_non_whitespace_offset = 0;
for(u64 c = 0; c < start_line.size; ++c)
{
if(start_line.str[c] <= 32)
{
++first_non_whitespace_offset;
}
else
{
break;
}
}
start_line.str += first_non_whitespace_offset;
start_line.size -= first_non_whitespace_offset;
// NOTE(rjf): Special case to handle CRLF-newline files.
if(start_line.str[start_line.size - 1] == 13)
{
start_line.size -= 1;
}
Vec2_f32 draw_pos =
{
view_rect.x0 + 30,
global_cursor_rect.y0,
};
if(draw_pos.y < view_rect.y0)
{
draw_pos.y = view_rect.y0;
}
u32 color = finalize_color(defcolor_comment, 0);
color &= 0x00ffffff;
color |= 0x80000000;
draw_string_oriented(app, face_id, color, start_line, draw_pos, 0, V2f32(0.f, 1.f));
}
}
}
#endif