-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathleif.c
3601 lines (2984 loc) · 131 KB
/
leif.c
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "include/leif/leif.h"
#include <cglm/mat4.h>
#include <cglm/types-struct.h>
#include <glad/glad.h>
#include <time.h>
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
#define STB_TRUETYPE_IMPLEMENTATION
#include <stb_truetype.h>
#define STB_IMAGE_RESIZE_IMPLEMENTATION
#include <stb_image_resize2.h>
#include <GLFW/glfw3.h>
#include <libclipboard.h>
#include <locale.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <limits.h>
#include <wchar.h>
#include <wctype.h>
#ifdef _WIN32
#define HOMEDIR "USERPROFILE"
#else
#define HOMEDIR (char*)"HOME"
#endif
#ifdef LF_GLFW
#ifndef GLFW_INCLUDE_NONE
#define GLFW_INCLUDE_NONE
#endif
#include <GLFW/glfw3.h>
#endif /* ifdef */
#define MAX(a, b) a > b ? a : b
#define MIN(a, b) a < b ? a : b
#define LF_TRACE(...) { printf("Leif: [TRACE]: "); printf(__VA_ARGS__); printf("\n"); }
#ifdef LF_DEBUG
#define LF_DEBUG(...) { printf("Leif: [DEBUG]: "); printf(__VA_ARGS__); printf("\n"); }
#else
#define LF_DEBUG(...)
#endif // LF_DEBUG
#define LF_INFO(...) { printf("Leif: [INFO]: "); printf(__VA_ARGS__); printf("\n"); }
#define LF_WARN(...) { printf("Leif: [WARN]: "); printf(__VA_ARGS__); printf("\n"); }
#define LF_ERROR(...) { printf("[LEIF ERROR]: "); printf(__VA_ARGS__); printf("\n"); }
#ifdef _MSC_VER
#define D_BREAK __debugbreak
#elif defined(__clang__) || defined(__GNUC__)
#define D_BREAK __builtin_trap
#else
#define D_BREAK
#endif
#ifdef _DEBUG
#define LF_ASSERT(cond, ...) { if(cond) {} else { printf("[LEIF]: Assertion failed: '"); printf(__VA_ARGS__); printf("' in file '%s' on line %i.\n", __FILE__, __LINE__); D_BREAK(); }}
#else
#define LF_ASSERT(cond, ...)
#endif // _DEBUG
#define LF_STACK_INIT_CAP 4
#ifdef LF_GLFW
#define MAX_KEYS GLFW_KEY_LAST
#define MAX_MOUSE_BUTTONS GLFW_MOUSE_BUTTON_LAST
#define KEY_CALLBACK_t GLFWkeyfun
#define MOUSE_BUTTON_CALLBACK_t GLFWmousebuttonfun
#define SCROLL_CALLBACK_t GLFWscrollfun
#define CURSOR_CALLBACK_t GLFWcursorposfun
#else
#define MAX_KEYS 0
#define MAX_MOUSE_BUTTONS 0
#define KEY_CALLBACK_t void*
#define MOUSE_BUTTON_CALLBACK_t void*
#define SCROLL_CALLBACK_t void*
#define CURSOR_CALLBACK_t void*
#endif
#define MAX_RENDER_BATCH 10000
#define MAX_TEX_COUNT_BATCH 32
#define MAX_KEY_CALLBACKS 4
#define MAX_MOUSE_BTTUON_CALLBACKS 4
#define MAX_SCROLL_CALLBACKS 4
#define MAX_CURSOR_POS_CALLBACKS 4
#define DJB2_INIT 5381
// -- Struct Defines ---
typedef struct {
uint32_t id;
} LfShader;
typedef struct {
vec2 pos; // 8 Bytes
vec4 border_color; // 16 Bytes
float border_width; // 4 Bytes
vec4 color; // 16 Bytes
vec2 texcoord; // 8 Bytes
float tex_index; // 4 Bytes
vec2 scale; // 8 Bytes
vec2 pos_px; // 8 Bytes
float corner_radius; // 4 Bytes
vec2 min_coord, max_coord; // 16 Bytes
} Vertex; // 88 Bytes per vertex
typedef struct {
bool keys[MAX_KEYS];
bool keys_changed[MAX_KEYS];
} LfKeyboard;
typedef struct {
bool buttons_current[MAX_MOUSE_BUTTONS];
bool buttons_last[MAX_MOUSE_BUTTONS];
double xpos, ypos, xpos_last, ypos_last, xpos_delta, ypos_delta;
bool first_mouse_press;
double xscroll_delta, yscroll_delta;
} LfMouse;
typedef struct {
bool is_dragging;
vec2s start_cursor_pos;
float start_scroll;
} DragState;
// State of input
typedef struct {
LfKeyboard keyboard;
LfMouse mouse;
// List of callbacks (user defined)
KEY_CALLBACK_t key_cbs[MAX_KEY_CALLBACKS];
MOUSE_BUTTON_CALLBACK_t mouse_button_cbs[MAX_MOUSE_BTTUON_CALLBACKS];
SCROLL_CALLBACK_t scroll_cbs[MAX_SCROLL_CALLBACKS];
CURSOR_CALLBACK_t cursor_pos_cbs[MAX_CURSOR_POS_CALLBACKS];
uint32_t key_cb_count, mouse_button_cb_count, scroll_cb_count, cursor_pos_cb_count;
} InputState;
// State of the batch renderer
typedef struct {
LfShader shader;
uint32_t vao, vbo, ibo;
uint32_t vert_count;
Vertex* verts;
vec4s vert_pos[4];
LfTexture textures[MAX_TEX_COUNT_BATCH];
uint32_t tex_index, tex_count,index_count;
} RenderState;
typedef struct {
LfUIElementProps* data;
uint32_t count, cap;
} PropsStack;
typedef struct {
bool init;
// Window
uint32_t dsp_w, dsp_h;
void* window_handle;
RenderState render;
InputState input;
LfTheme theme;
LfDiv current_div, prev_div;
int32_t current_line_height, prev_line_height;
vec2s pos_ptr, prev_pos_ptr;
// Pushable variables
LfFont* font_stack, *prev_font_stack;
LfUIElementProps div_props, prev_props_stack;
LfColor image_color_stack;
int64_t element_id_stack;
PropsStack props_stack;
// Event references
LfKeyEvent key_ev;
LfMouseButtonEvent mb_ev;
LfCursorPosEvent cp_ev;
LfScrollEvent scr_ev;
LfCharEvent ch_ev;
vec2s cull_start, cull_end;
LfTexture tex_arrow_down, tex_tick;
bool text_wrap, line_overflow, div_hoverable, input_grabbed;
uint64_t active_element_id;
float* scroll_velocity_ptr;
float* scroll_ptr;
LfDiv selected_div, selected_div_tmp, scrollbar_div, grabbed_div;
uint32_t drawcalls;
bool entered_div;
bool div_velocity_accelerating;
float last_time, delta_time;
clipboard_c* clipboard;
bool renderer_render;
DragState drag_state;
} LfState;
typedef enum {
INPUT_INT = 0,
INPUT_FLOAT,
INPUT_TEXT
} InputFieldType;
// Static object to retrieve state data during runtime
static LfState state;
// --- Renderer ---
static uint32_t shader_create(GLenum type, const char* src);
static LfShader shader_prg_create(const char* vert_src, const char* frag_src);
static void shader_set_mat(LfShader prg, const char* name, mat4 mat);
static void set_projection_matrix();
static void renderer_init();
static void renderer_flush();
static void renderer_begin();
static LfTextProps text_render_simple(vec2s pos, const char* text, LfFont font, LfColor font_color, bool no_render);
static LfTextProps text_render_simple_wide(vec2s pos, const wchar_t* text, LfFont font, LfColor font_color, bool no_render);
static LfClickableItemState button_ex(const char* file, int32_t line, vec2s pos, vec2s size, LfUIElementProps props, LfColor color, float border_width, bool click_color, bool hover_color, vec2s hitbox_override);
static LfClickableItemState button(const char* file, int32_t line, vec2s pos, vec2s size, LfUIElementProps props, LfColor color, float border_width, bool click_color, bool hover_color);
static LfClickableItemState div_container(vec2s pos, vec2s size, LfUIElementProps props, LfColor color, float border_width, bool click_color, bool hover_color);
static void next_line_on_overflow(vec2s size, float xoffset);
static bool item_should_cull(LfAABB item);
static void draw_scrollbar_on(LfDiv* div);
static void input_field(LfInputField* input, InputFieldType type, const char* file, int32_t line);
LfFont load_font(const char* filepath, uint32_t pixelsize, uint32_t tex_width, uint32_t tex_height, uint32_t line_gap_add);
static LfFont get_current_font();
static LfClickableItemState button_element_loc(void* text, const char* file, int32_t line, bool wide);
static LfClickableItemState button_fixed_element_loc(void* text, float width, float height, const char* file, int32_t line, bool wide);
static LfClickableItemState checkbox_element_loc(void* text, bool* val, LfColor tick_color, LfColor tex_color, const char* file, int32_t line, bool wide);
static void dropdown_menu_item_loc(void** items, void* placeholder, uint32_t item_count, float width, float height, int32_t* selected_index, bool* opened, const char* file, int32_t line, bool wide);
static int32_t menu_item_list_item_loc(void** items, uint32_t item_count, int32_t selected_index, LfMenuItemCallback per_cb, bool vertical, const char* file, int32_t line, bool wide);
// --- Utility ---
static int32_t get_max_char_height_font(LfFont font);
static void remove_i_str(char *str, int32_t index);
static void remove_substr_str(char *str, int start_index, int end_index);
static void insert_i_str(char *str, char ch, int32_t index);
static void insert_str_str(char *source, const char *insert, int32_t index);
static void substr_str(const char* str, int start_index, int end_index, char* substring);
static int map_vals(int value, int from_min, int from_max, int to_min, int to_max);
// --- Input ---
#ifdef LF_GLFW
static void glfw_key_callback(GLFWwindow* window, int32_t key, int scancode, int action, int mods);
static void glfw_mouse_button_callback(GLFWwindow* window, int32_t button, int action, int mods);
static void glfw_scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
static void glfw_cursor_callback(GLFWwindow* window, double xpos, double ypos);
static void glfw_char_callback(GLFWwindow* window, uint32_t charcode);
#endif
static void update_input();
static void clear_events();
static uint64_t djb2_hash(uint64_t hash, const void* buf, size_t size);
static void props_stack_create(PropsStack* stack);
static void props_stack_resize(PropsStack* stack, uint32_t newcap);
static void props_stack_push(PropsStack* stack, LfUIElementProps props);
static LfUIElementProps props_stack_pop(PropsStack* stack);
static LfUIElementProps props_stack_peak(PropsStack* stack);
static bool props_stack_empty(PropsStack* stack);
static LfUIElementProps get_props_for(LfUIElementProps props);
// --- Static Functions ---
uint32_t shader_create(GLenum type, const char* src) {
// Create && compile the shader with opengl
uint32_t shader = glCreateShader(type);
glShaderSource(shader, 1, &src, NULL);
glCompileShader(shader);
// Check for compilation errors
int32_t compiled;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
if(!compiled) {
LF_ERROR("Failed to compile %s shader.", type == GL_VERTEX_SHADER ? "vertex" : "fragment");
char info[512];
glGetShaderInfoLog(shader, 512, NULL, info);
LF_INFO("%s", info);
glDeleteShader(shader);
}
return shader;
}
LfShader shader_prg_create(const char* vert_src, const char* frag_src) {
// Creating vertex & fragment shader with the shader API
uint32_t vertex_shader = shader_create(GL_VERTEX_SHADER, vert_src);
uint32_t fragment_shader = shader_create(GL_FRAGMENT_SHADER, frag_src);
// Creating & linking the shader program with OpenGL
LfShader prg;
prg.id = glCreateProgram();
glAttachShader(prg.id, vertex_shader);
glAttachShader(prg.id, fragment_shader);
glLinkProgram(prg.id);
// Check for linking errors
int32_t linked;
glGetProgramiv(prg.id, GL_LINK_STATUS, &linked);
if(!linked) {
LF_ERROR("Failed to link shader program.");
char info[512];
glGetProgramInfoLog(prg.id, 512, NULL, info);
LF_INFO("%s", info);
glDeleteShader(vertex_shader);
glDeleteShader(fragment_shader);
glDeleteProgram(prg.id);
return prg;
}
// Delete the shaders after
glDeleteShader(vertex_shader);
glDeleteShader(fragment_shader);
return prg;
}
void shader_set_mat(LfShader prg, const char* name, mat4 mat) {
glUniformMatrix4fv(glGetUniformLocation(prg.id, name), 1, GL_FALSE, mat[0]);
}
void set_projection_matrix() {
float left = 0.0f;
float right = state.dsp_w;
float bottom = state.dsp_h;
float top = 0.0f;
float near = 0.1f;
float far = 100.0f;
// Create the orthographic projection matrix
mat4 orthoMatrix = GLM_MAT4_IDENTITY_INIT;
orthoMatrix[0][0] = 2.0f / (right - left);
orthoMatrix[1][1] = 2.0f / (top - bottom);
orthoMatrix[2][2] = -1;
orthoMatrix[3][0] = -(right + left) / (right - left);
orthoMatrix[3][1] = -(top + bottom) / (top - bottom);
shader_set_mat(state.render.shader, "u_proj", orthoMatrix);
}
void renderer_init() {
// OpenGL Setup
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
state.render.vert_count = 0;
state.render.verts = (Vertex*)malloc(sizeof(Vertex) * MAX_RENDER_BATCH * 4);
/* Creating vertex array & vertex buffer for the batch renderer */
glCreateVertexArrays(1, &state.render.vao);
glBindVertexArray(state.render.vao);
glCreateBuffers(1, &state.render.vbo);
glBindBuffer(GL_ARRAY_BUFFER, state.render.vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * MAX_RENDER_BATCH * 4, NULL,
GL_DYNAMIC_DRAW);
uint32_t* indices = (uint32_t*)malloc(sizeof(uint32_t) * MAX_RENDER_BATCH * 6);
uint32_t offset = 0;
for (uint32_t i = 0; i < MAX_RENDER_BATCH * 6; i += 6) {
indices[i + 0] = offset + 0;
indices[i + 1] = offset + 1;
indices[i + 2] = offset + 2;
indices[i + 3] = offset + 2;
indices[i + 4] = offset + 3;
indices[i + 5] = offset + 0;
offset += 4;
}
glCreateBuffers(1, &state.render.ibo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, state.render.ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, MAX_RENDER_BATCH * 6 * sizeof(uint32_t), indices, GL_STATIC_DRAW);
free(indices);
// Setting the vertex layout
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), NULL);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(intptr_t)offsetof(Vertex, border_color));
glEnableVertexAttribArray(1);
glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(intptr_t)offsetof(Vertex, border_width));
glEnableVertexAttribArray(2);
glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(intptr_t)offsetof(Vertex, color));
glEnableVertexAttribArray(3);
glVertexAttribPointer(4, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(intptr_t*)offsetof(Vertex, texcoord));
glEnableVertexAttribArray(4);
glVertexAttribPointer(5, 1, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(intptr_t*)offsetof(Vertex, tex_index));
glEnableVertexAttribArray(5);
glVertexAttribPointer(6, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(intptr_t*)offsetof(Vertex, scale));
glEnableVertexAttribArray(6);
glVertexAttribPointer(7, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(intptr_t*)offsetof(Vertex, pos_px));
glEnableVertexAttribArray(7);
glVertexAttribPointer(8, 1, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(intptr_t*)offsetof(Vertex, corner_radius));
glEnableVertexAttribArray(8);
glVertexAttribPointer(10, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(intptr_t*)offsetof(Vertex, min_coord));
glEnableVertexAttribArray(10);
glVertexAttribPointer(11, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(intptr_t*)offsetof(Vertex, max_coord));
glEnableVertexAttribArray(11);
// Creating the shader for the batch renderer
const char* vert_src =
"#version 450 core\n"
"layout (location = 0) in vec2 a_pos;\n"
"layout (location = 1) in vec4 a_border_color;\n"
"layout (location = 2) in float a_border_width;\n"
"layout (location = 3) in vec4 a_color;\n"
"layout (location = 4) in vec2 a_texcoord;\n"
"layout (location = 5) in float a_tex_index;\n"
"layout (location = 6) in vec2 a_scale;\n"
"layout (location = 7) in vec2 a_pos_px;\n"
"layout (location = 8) in float a_corner_radius;\n"
"layout (location = 10) in vec2 a_min_coord;\n"
"layout (location = 11) in vec2 a_max_coord;\n"
"uniform mat4 u_proj;\n"
"out vec4 v_border_color;\n"
"out float v_border_width;\n"
"out vec4 v_color;\n"
"out vec2 v_texcoord;\n"
"out float v_tex_index;\n"
"flat out vec2 v_scale;\n"
"flat out vec2 v_pos_px;\n"
"flat out float v_is_gradient;\n"
"out float v_corner_radius;\n"
"out vec2 v_min_coord;\n"
"out vec2 v_max_coord;\n"
"void main() {\n"
"v_color = a_color;\n"
"v_texcoord = a_texcoord;\n"
"v_tex_index = a_tex_index;\n"
"v_border_color = a_border_color;\n"
"v_border_width = a_border_width;\n"
"v_scale = a_scale;\n"
"v_pos_px = a_pos_px;\n"
"v_corner_radius = a_corner_radius;\n"
"v_min_coord = a_min_coord;\n"
"v_max_coord = a_max_coord;\n"
"gl_Position = u_proj * vec4(a_pos.x, a_pos.y, 0.0f, 1.0);\n"
"}\n";
const char* frag_src = "#version 450 core\n"
"out vec4 o_color;\n"
"in vec4 v_color;\n"
"in float v_tex_index;\n"
"in vec4 v_border_color;\n"
"in float v_border_width;\n"
"in vec2 v_texcoord;\n"
"flat in vec2 v_scale;\n"
"flat in vec2 v_pos_px;\n"
"in float v_corner_radius;\n"
"uniform sampler2D u_textures[32];\n"
"uniform vec2 u_screen_size;\n"
"in vec2 v_min_coord;\n"
"in vec2 v_max_coord;\n"
"float rounded_box_sdf(vec2 center_pos, vec2 size, float radius) {\n"
" return length(max(abs(center_pos)-size+radius,0.0))-radius;\n"
"}\n"
"void main() {\n"
" if(u_screen_size.y - gl_FragCoord.y < v_min_coord.y && v_min_coord.y != -1) {\n"
" discard;\n"
" }\n"
" if(u_screen_size.y - gl_FragCoord.y > v_max_coord.y && v_max_coord.y != -1) {\n"
" discard;\n"
" }\n"
" if ((gl_FragCoord.x < v_min_coord.x && v_min_coord.x != -1) || (gl_FragCoord.x > v_max_coord.x && v_max_coord.x != -1)) {\n"
" discard;\n"
" }\n"
" vec2 size = v_scale;\n"
" vec4 opaque_color, display_color;\n"
" if(v_tex_index == -1) {\n"
" opaque_color = v_color;\n"
" } else {\n"
" opaque_color = texture(u_textures[int(v_tex_index)], v_texcoord) * v_color;\n"
" }\n"
" if(v_corner_radius != 0.0f) {"
" display_color = opaque_color;\n"
" vec2 location = vec2(v_pos_px.x, -v_pos_px.y);\n"
" location.y += u_screen_size.y - size.y;\n"
" float edge_softness = 1.0f;\n"
" float radius = v_corner_radius * 2.0f;\n"
" float distance = rounded_box_sdf(gl_FragCoord.xy - location - (size/2.0f), size / 2.0f, radius);\n"
" float smoothed_alpha = 1.0f-smoothstep(0.0f, edge_softness * 2.0f,distance);\n"
" vec3 fill_color;\n"
" if(v_border_width != 0.0f) {\n"
" vec2 location_border = vec2(location.x + v_border_width, location.y + v_border_width);\n"
" vec2 size_border = vec2(size.x - v_border_width * 2, size.y - v_border_width * 2);\n"
" float distance_border = rounded_box_sdf(gl_FragCoord.xy - location_border - (size_border / 2.0f), size_border / 2.0f, radius);\n"
" if(distance_border <= 0.0f) {\n"
" fill_color = display_color.xyz;\n"
" } else {\n"
" fill_color = v_border_color.xyz;\n"
" }\n"
" } else {\n"
" fill_color = display_color.xyz;\n"
" }\n"
" if(v_border_width != 0.0f)\n"
" o_color = mix(vec4(0.0f, 0.0f, 0.0f, 0.0f), vec4(fill_color, smoothed_alpha), smoothed_alpha);\n"
" else\n"
" o_color = mix(vec4(0.0f, 0.0f, 0.0f, 0.0f), vec4(fill_color, display_color.a), smoothed_alpha);\n"
" } else {\n"
" vec4 fill_color = opaque_color;\n"
" if(v_border_width != 0.0f) {\n"
" vec2 location = vec2(v_pos_px.x, -v_pos_px.y);\n"
" location.y += u_screen_size.y - size.y;\n"
" vec2 location_border = vec2(location.x + v_border_width, location.y + v_border_width);\n"
" vec2 size_border = vec2(v_scale.x - v_border_width * 2, v_scale.y - v_border_width * 2);\n"
" float distance_border = rounded_box_sdf(gl_FragCoord.xy - location_border - (size_border / 2.0f), size_border / 2.0f, v_corner_radius);\n"
" if(distance_border > 0.0f) {\n"
" fill_color = v_border_color;\n"
"}\n"
" }\n"
" o_color = fill_color;\n"
" }\n"
"}\n";
state.render.shader = shader_prg_create(vert_src, frag_src);
// initializing vertex position data
state.render.vert_pos[0] = (vec4s){-0.5f, -0.5f, 0.0f, 1.0f};
state.render.vert_pos[1] = (vec4s){0.5f, -0.5f, 0.0f, 1.0f};
state.render.vert_pos[2] = (vec4s){0.5f, 0.5f, 0.0f, 1.0f};
state.render.vert_pos[3] = (vec4s){-0.5f, 0.5f, 0.0f, 1.0f};
// Populating the textures array in the shader with texture ids
int32_t tex_slots[MAX_TEX_COUNT_BATCH];
for(uint32_t i = 0; i < MAX_TEX_COUNT_BATCH; i++)
tex_slots[i] = i;
glUseProgram(state.render.shader.id);
set_projection_matrix();
glUniform1iv(glGetUniformLocation(state.render.shader.id, "u_textures"), MAX_TEX_COUNT_BATCH, tex_slots);
}
void renderer_begin() {
state.render.vert_count = 0;
state.render.index_count = 0;
state.render.tex_index = 0;
state.render.tex_count = 0;
state.drawcalls = 0;
}
void renderer_flush() {
if(state.render.vert_count <= 0) return;
// Bind the vertex buffer & shader set the vertex data, bind the textures & draw
glUseProgram(state.render.shader.id);
glBindBuffer(GL_ARRAY_BUFFER, state.render.vbo);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(Vertex) * state.render.vert_count,
state.render.verts);
for(uint32_t i = 0; i < state.render.tex_count; i++) {
glBindTextureUnit(i, state.render.textures[i].id);
state.drawcalls++;
}
vec2s renderSize = (vec2s){(float)state.dsp_w, (float)state.dsp_h};
glUniform2fv(glGetUniformLocation(state.render.shader.id, "u_screen_size"), 1, (float*)renderSize.raw);
glBindVertexArray(state.render.vao);
glDrawElements(GL_TRIANGLES, state.render.index_count, GL_UNSIGNED_INT, NULL);
}
LfTextProps text_render_simple(vec2s pos, const char* text, LfFont font, LfColor font_color, bool no_render) {
return lf_text_render(pos, text, font, font_color, -1, (vec2s){-1, -1}, no_render, false, -1, -1);
}
LfTextProps text_render_simple_wide(vec2s pos, const wchar_t* text, LfFont font, LfColor font_color, bool no_render) {
return lf_text_render_wchar(pos, text, font, font_color, -1, (vec2s){-1, -1}, no_render, false, -1, -1);
}
LfClickableItemState button(const char* file, int32_t line, vec2s pos, vec2s size, LfUIElementProps props, LfColor color, float border_width, bool click_color, bool hover_color) {
return button_ex(file, line, pos, size, props, color, border_width, click_color, hover_color, (vec2s){-1, -1});
}
LfClickableItemState button_ex(const char* file, int32_t line, vec2s pos, vec2s size, LfUIElementProps props, LfColor color, float border_width, bool click_color, bool hover_color, vec2s hitbox_override) {
uint64_t id = DJB2_INIT;
id = djb2_hash(id, file, strlen(file));
id = djb2_hash(id, &line, sizeof(line));
if(state.element_id_stack != -1) {
id = djb2_hash(id, &state.element_id_stack, sizeof(state.element_id_stack));
}
if(item_should_cull((LfAABB){.pos = pos, .size= size})) {
return LF_IDLE;
}
LfColor hover_color_rgb = hover_color ? (props.hover_color.a == 0.0f ? lf_color_brightness(color, 1.2) : props.hover_color) : color;
LfColor held_color_rgb = click_color ? lf_color_brightness(color, 1.3) : color;
bool is_hovered = lf_hovered(pos, (vec2s){hitbox_override.x != -1 ? hitbox_override.x : size.x,
hitbox_override.y != -1 ? hitbox_override.y : size.y});
if(state.active_element_id == 0) {
if(is_hovered && lf_mouse_button_went_down(GLFW_MOUSE_BUTTON_LEFT)) {
state.active_element_id = id;
}
} else if(state.active_element_id == id) {
if(is_hovered && lf_mouse_button_is_released(GLFW_MOUSE_BUTTON_LEFT)) {
lf_rect_render(pos, size, hover_color_rgb, props.border_color, border_width, props.corner_radius);
state.active_element_id = 0;
return LF_CLICKED;
}
}
if(is_hovered && lf_mouse_button_is_released(GLFW_MOUSE_BUTTON_LEFT)) {
state.active_element_id = 0;
}
if(is_hovered && lf_mouse_button_is_down(GLFW_MOUSE_BUTTON_LEFT)) {
lf_rect_render(pos, size, held_color_rgb, props.border_color, border_width, props.corner_radius);
return LF_HELD;
}
if(is_hovered && (!lf_mouse_button_went_down(GLFW_MOUSE_BUTTON_LEFT) && !lf_mouse_button_is_down(GLFW_MOUSE_BUTTON_LEFT))) {
lf_rect_render(pos, size, hover_color ? hover_color_rgb : color, props.border_color, border_width, props.corner_radius);
return LF_HOVERED;
}
lf_rect_render(pos, size, color, props.border_color, border_width, props.corner_radius);
return LF_IDLE;
}
LfClickableItemState div_container(vec2s pos, vec2s size, LfUIElementProps props, LfColor color, float border_width, bool click_color, bool hover_color) {
if(item_should_cull((LfAABB){.pos = pos, .size = size})) {
return LF_IDLE;
}
LfColor hover_color_rgb = hover_color ? (props.hover_color.a == 0.0f ? lf_color_brightness(color, 1.5) : props.hover_color) : color;
LfColor held_color_rgb = click_color ? lf_color_brightness(color, 1.8) : color;
bool is_hovered = lf_hovered(pos, size);
if(is_hovered && lf_mouse_button_is_released(GLFW_MOUSE_BUTTON_LEFT)) {
lf_rect_render(pos, size, hover_color_rgb, props.border_color, border_width, props.corner_radius);
return LF_CLICKED;
}
if(is_hovered && lf_mouse_button_is_down(GLFW_MOUSE_BUTTON_LEFT)) {
lf_rect_render(pos, size, held_color_rgb, props.border_color, border_width, props.corner_radius);
return LF_HELD;
}
if(is_hovered && (!lf_mouse_button_went_down(GLFW_MOUSE_BUTTON_LEFT) && !lf_mouse_button_is_down(GLFW_MOUSE_BUTTON_LEFT))) {
lf_rect_render(pos, size, hover_color ? hover_color_rgb : color, props.border_color, border_width, props.corner_radius);
return LF_HOVERED;
}
lf_rect_render(pos, size, color, props.border_color, border_width, props.corner_radius);
return LF_IDLE;
}
void next_line_on_overflow(vec2s size, float xoffset) {
if(!state.line_overflow) return;
// If the object does not fit in the area of the current div, advance to the next line
if(state.pos_ptr.x - state.current_div.aabb.pos.x + size.x > state.current_div.aabb.size.x) {
state.pos_ptr.y += state.current_line_height;
state.pos_ptr.x = state.current_div.aabb.pos.x + xoffset;
state.current_line_height = 0;
}
if(size.y > state.current_line_height) {
state.current_line_height = size.y;
}
}
bool item_should_cull(LfAABB item) {
bool intersect = true;
LfAABB window = (LfAABB){.pos = (vec2s){0, 0}, .size = (vec2s){state.dsp_w, state.dsp_h}};
if(item.size.x == -1 || item.size.y == -1) {
item.size.x = state.dsp_w;
item.size.y = get_current_font().font_size;
}
if (item.pos.x + item.size.x <= window.pos.x || item.pos.x >= window.pos.x + window.size.x)
intersect = false;
if (item.pos.y + item.size.y <= window.pos.y || item.pos.y >= window.pos.y + window.size.y)
intersect = false;
return !intersect && state.current_div.id == state.scrollbar_div.id;
return false;
}
void draw_scrollbar_on(LfDiv* div) {
lf_next_line();
if (state.current_div.id == div->id) {
state.scrollbar_div = *div;
LfDiv* selected = div;
float scroll = *state.scroll_ptr;
LfUIElementProps props = get_props_for(state.theme.scrollbar_props);
selected->total_area.x = state.pos_ptr.x;
selected->total_area.y = state.pos_ptr.y + state.div_props.corner_radius;
if (*state.scroll_ptr < -((div->total_area.y - *state.scroll_ptr) - div->aabb.pos.y - div->aabb.size.y) && *state.scroll_velocity_ptr < 0 && state.theme.div_smooth_scroll) {
*state.scroll_velocity_ptr = 0;
*state.scroll_ptr = -((div->total_area.y - *state.scroll_ptr) - div->aabb.pos.y - div->aabb.size.y);
}
float total_area = selected->total_area.y - scroll;
float visible_area = selected->aabb.size.y + selected->aabb.pos.y;
if (total_area > visible_area) {
const float min_scrollbar_height = 20;
float area_mapped = visible_area / total_area;
float scroll_mapped = (-1 * scroll) / total_area;
float scrollbar_height = MAX((selected->aabb.size.y * area_mapped - props.margin_top * 2), min_scrollbar_height);
LfAABB scrollbar_area = (LfAABB){
.pos = (vec2s){
selected->aabb.pos.x + selected->aabb.size.x - state.theme.scrollbar_width - props.margin_right - state.div_props.padding - state.div_props.border_width,
MIN((selected->aabb.pos.y + selected->aabb.size.y * scroll_mapped + props.margin_top + state.div_props.padding + state.div_props.border_width + state.div_props.corner_radius),
visible_area - scrollbar_height)},
.size = (vec2s){
state.theme.scrollbar_width,
scrollbar_height - state.div_props.border_width * 2 - state.div_props.corner_radius * 2},
};
vec2s cursorpos = (vec2s){lf_get_mouse_x(), lf_get_mouse_y()};
if (lf_mouse_button_went_down(GLFW_MOUSE_BUTTON_LEFT) && lf_hovered(scrollbar_area.pos, scrollbar_area.size)) {
state.drag_state.is_dragging = true;
state.drag_state.start_cursor_pos = cursorpos;
state.drag_state.start_scroll = *state.scroll_ptr;
}
if(state.drag_state.is_dragging) {
float cursor_delta = (cursorpos.y - state.drag_state.start_cursor_pos.y);
float new_scroll = state.drag_state.start_scroll - cursor_delta * (total_area / visible_area);
*state.scroll_ptr = new_scroll;
if (*state.scroll_ptr > 0) {
*state.scroll_ptr = 0;
} else if (*state.scroll_ptr < -(total_area - visible_area)) {
*state.scroll_ptr = -(total_area - visible_area);
}
}
if (lf_mouse_button_is_released(GLFW_MOUSE_BUTTON_LEFT)) {
state.drag_state.is_dragging = false;
}
lf_rect_render(scrollbar_area.pos, scrollbar_area.size,
props.color, props.border_color, props.border_width, props.corner_radius);
}
}
}
void input_field(LfInputField* input, InputFieldType type, const char* file, int32_t line) {
if(!input->buf) return;
if(!input->_init) {
lf_input_field_unselect_all(input);
input->_init = true;
}
LfUIElementProps props = get_props_for(state.theme.inputfield_props);
LfFont font = get_current_font();
state.pos_ptr.x += props.margin_left;
state.pos_ptr.y += props.margin_top;
float wrap_point = state.pos_ptr.x + input->width - props.padding;
if(input->selected) {
if(lf_mouse_button_went_down(GLFW_MOUSE_BUTTON_LEFT) && (lf_get_mouse_x_delta() == 0 && lf_get_mouse_y_delta() == 0)) {
LfTextProps selected_props = lf_text_render((vec2s){
state.pos_ptr.x + props.padding,
state.pos_ptr.y + props.padding
}, input->buf, font, LF_NO_COLOR,
wrap_point, (vec2s){lf_get_mouse_x(), lf_get_mouse_y()}, true, false, -1, -1);
input->cursor_index = selected_props.rendered_count;
lf_input_field_unselect_all(input);
input->mouse_selection_end = input->cursor_index;
input->mouse_selection_start = input->cursor_index;
} else if(lf_mouse_button_is_down(GLFW_MOUSE_BUTTON_LEFT) && (lf_get_mouse_x_delta() != 0 || lf_get_mouse_y_delta() != 0)) {
if(input->mouse_dir == 0) {
input->mouse_dir = (lf_get_mouse_x_delta() < 0) ? -1 : 1;
input->mouse_selection_end = input->cursor_index;
input->mouse_selection_start = input->cursor_index;
}
LfTextProps selected_props = lf_text_render((vec2s){
state.pos_ptr.x + props.padding,
state.pos_ptr.y + props.padding
}, input->buf, font, LF_NO_COLOR, wrap_point, (vec2s){lf_get_mouse_x(), lf_get_mouse_y()}, true, false, -1, -1);
input->cursor_index = selected_props.rendered_count;
if(input->mouse_dir == -1)
input->mouse_selection_start = input->cursor_index;
else if(input->mouse_dir == 1)
input->mouse_selection_end = input->cursor_index;
input->selection_start = input->mouse_selection_start;
input->selection_end = input->mouse_selection_end;
if(input->mouse_selection_start == input->mouse_selection_end) {
input->mouse_dir = (lf_get_mouse_x_delta() < 0) ? -1 : 1;
}
} else if(lf_mouse_button_is_released(GLFW_MOUSE_BUTTON_LEFT)){
input->mouse_dir = 0;
}
if(lf_char_event().happened && lf_char_event().charcode >= 0 && lf_char_event().charcode <= 127 &&
strlen(input->buf) + 1 <= input->buf_size && (input->max_chars ? strlen(input->buf) + 1 <= input->max_chars : true)) {
if(input->insert_override_callback) {
input->insert_override_callback(input);
} else {
if(input->selection_start != -1) {
int start = input->selection_dir != 0 ? input->selection_start : input->selection_start - 1;
int end = input->selection_end;
remove_substr_str(input->buf, start, end);
input->cursor_index = input->selection_start;
lf_input_field_unselect_all(input);
}
lf_input_insert_char_idx(input, lf_char_event().charcode, input->cursor_index++);
}
}
if(lf_key_event().happened && lf_key_event().pressed) {
switch(lf_key_event().keycode) {
case GLFW_KEY_BACKSPACE: {
if(input->selection_start != -1) {
int start = input->selection_dir != 0 ? input->selection_start : input->selection_start - 1;
int end = input->selection_end;
remove_substr_str(input->buf, start, end);
input->cursor_index = input->selection_start;
lf_input_field_unselect_all(input);
}
else {
if(input->cursor_index - 1 < 0) break;
remove_i_str(input->buf, input->cursor_index - 1);
input->cursor_index--;
}
break;
}
case GLFW_KEY_LEFT: {
if(input->cursor_index - 1 < 0 ) {
if(!lf_key_is_down(GLFW_KEY_LEFT_SHIFT))
lf_input_field_unselect_all(input);
break;
}
if(lf_key_is_down(GLFW_KEY_LEFT_SHIFT)) {
if(input->selection_end == -1) {
input->selection_end = input->cursor_index - 1;
input->selection_dir = -1;
}
input->cursor_index--;
if(input->selection_dir == 1) {
if(input->cursor_index != input->selection_start) {
input->selection_end = input->cursor_index - 1;
} else {
lf_input_field_unselect_all(input);
}
} else {
input->selection_start = input->cursor_index;
}
} else {
if(input->selection_end == -1)
input->cursor_index--;
lf_input_field_unselect_all(input);
}
break;
}
case GLFW_KEY_RIGHT: {
if(input->cursor_index + 1 > strlen(input->buf)){
if(!lf_key_is_down(GLFW_KEY_LEFT_SHIFT))
lf_input_field_unselect_all(input);
break;
}
if(lf_key_is_down(GLFW_KEY_LEFT_SHIFT)) {
if(input->selection_start == -1) {
input->selection_start = input->cursor_index;
input->selection_dir = 1;
}
if(input->selection_dir == -1) {
input->cursor_index++;
if(input->cursor_index - 1 != input->selection_end) {
input->selection_start = input->cursor_index;
} else {
lf_input_field_unselect_all(input);
}
} else {
input->selection_end = input->cursor_index;
input->cursor_index++;
}
} else {
if(input->selection_end == -1)
input->cursor_index++;
lf_input_field_unselect_all(input);
}
break;
}
case GLFW_KEY_ENTER: {
// TODO: There is a bug with the input cursor when inserting new line characters
/*
insert_i_str(input->buf, '\n', input->cursor_index++);
*/
break;
}
case GLFW_KEY_TAB: {
if(strlen(input->buf) + 1 <= input->buf_size && (input->max_chars ? strlen(input->buf) + 1 <= input->max_chars : true)) {
for(uint32_t i = 0; i < 2; i++) {
insert_i_str(input->buf, ' ', input->cursor_index++);
}
}
break;
}
case GLFW_KEY_A: {
if(!lf_key_is_down(GLFW_KEY_LEFT_CONTROL)) break;
bool selected_all = input->selection_start == 0 && input->selection_end == strlen(input->buf);
if(selected_all) {
lf_input_field_unselect_all(input);
} else {
input->selection_start = 0;
input->selection_end = strlen(input->buf);
}
break;
}
case GLFW_KEY_C: {
if(!lf_key_is_down(GLFW_KEY_LEFT_CONTROL)) break;
char selection[strlen(input->buf)];
memset(selection, 0, strlen(input->buf));
substr_str(input->buf, input->selection_start, input->selection_end, selection);
clipboard_set_text(state.clipboard, selection);
break;
}
case GLFW_KEY_V: {
if(!lf_key_is_down(GLFW_KEY_LEFT_CONTROL)) break;
int32_t length;
const char* clipboard_content = clipboard_text_ex(state.clipboard, &length, LCB_CLIPBOARD);
if(strlen(input->buf) + length > input->buf_size || (input->max_chars ? strlen(input->buf) + length > input->max_chars : false)) break;
lf_input_insert_str_idx(input, clipboard_content, length, input->cursor_index);
input->cursor_index += strlen(clipboard_content);
break;
}
case GLFW_KEY_X: {
if (!lf_key_is_down(GLFW_KEY_LEFT_CONTROL)) break;
char selection[strlen(input->buf)];
memset(selection, 0, strlen(input->buf));
substr_str(input->buf, input->selection_start, input->selection_end, selection);
clipboard_set_text(state.clipboard, selection);
int start = input->selection_dir != 0 ? input->selection_start : input->selection_start - 1;
remove_substr_str(input->buf, start, input->selection_end);
input->cursor_index = input->selection_start;
lf_input_field_unselect_all(input);
break;
}
default: {
break;
}
}
}
if(input->key_callback) {
input->key_callback(input);
}
}
LfTextProps textprops = lf_text_render((vec2s){state.pos_ptr.x + props.padding, state.pos_ptr.y + props.padding}, input->buf,
font, LF_NO_COLOR,