-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit.c
1182 lines (1012 loc) · 30.6 KB
/
edit.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
/*
*
* https://ramiroblan.co
* mail: [email protected]
*
*/
#include <stdio.h>
#include <stdlib.h>
#define SHOW_FPS
#define CHAR_GRID
#ifdef SHOW_FPS
#include <time.h>
struct timespec start, stop;
#endif
#define GLFW_INCLUDE_ES3
#include <GLFW/glfw3.h>
#include <math.h>
#include <float.h>
#ifdef __EMSCRIPTEN__
#include <emscripten/emscripten.h>
#include <emscripten/html5.h>
#endif
#include "edit.h"
#include "scroll.h"
#include "gap.h"
#include "linenumber.h"
#include "shaders.c"
GLFWwindow *window;
GLuint vertex_buffer,
vertex_buffer_scroll,
vertex_buffer_linenumber,
vertex_shader,
fragment_shader_text,
fragment_shader_scroll,
fragment_shader_linenumber,
program_text,
program_scroll,
program_linenumber,
vpos_location_text,
vpos_location_scroll,
vpos_location_linenumber,
mouse_location,
grid_location,
grid_location_linenumber,
grid_Y_offset_location,
grid_Y_offset_location_linenumber,
grid_X_offset_location,
scroll_bar_location,
cursor_location,
cursor_blink_location,
chars_location,
selection_location,
linenumber_location;
GLuint font_tex,
char_tex,
select_tex,
linenumber_tex;
GLfloat mouse_pos[2];
struct
{
unsigned int mouse, shift, start_pos;
}
selection_state = {0, 0, 0};
FILE *file_tex;
char rgba[4];
unsigned char bmp_header[132];
unsigned int bmp_dataPos;
unsigned int bmp_width, bmp_height;
unsigned int bmp_image_size;
unsigned char *bmp_data;
//GRID grid = {0., 0., 40., 38.};
//GRID grid = {0., 0., 7., 10.};
GRID grid = {0., 0., 20., 19.};
//GRID grid = {0., 0., 7., 9., 5., 5.};
//GRID grid = {0., 0., 1., 3.};
unsigned int grid_full_size;
GLfloat grid_x_offset = 5., grid_y_offset = 5.;
SCROLL_BAR scroll_bar = {400., 700.};
double scroll_bar_grab = 0;
W_SIZE window_size = {0, 0};
int fd;
int i, j, k, l, m, n;
double curr_frame_time, last_frame_time, fps_avg_count = 0, fps_avg;
unsigned int fps_int, fps_dec;
unsigned int blink_state = 1, blink_count = 0;
PARA *curr_paragraph = NULL;
PARA paragraphs_head = { NULL, NULL, NULL, 1, 0, 0, NULL, 0, 0 };
PARA **grid_lines = NULL;
PARA *top_paragraph_scroll = NULL;
unsigned int lines_scroll_top_diff = 0;
unsigned int curr_line = 0;
unsigned int paragraphs_count = 1;
unsigned int lines_count = 1;
GLfloat *line_numbers;
unsigned int top_line_number = 1;
unsigned int lines_decimals_count = 0;
unsigned int lines_scroll = 0;
unsigned int paragraph_cursor = 0;
unsigned int grid_paragraph_count = INITIAL_GRID_PARAS;
unsigned int chars_buffer_size = INITIAL_CHARS_BUFFER_SIZE;
GLfloat *chars_tex;
GLfloat *selection_tex;
GLfloat *linenumbers_tex;
#ifndef __EMSCRIPTEN__
const char *paste_buff;
#endif
static struct
{
float x, y;
}
SCREEN[6] =
{
{-1.f,1.f},
{-1.f,-1.f},
{1.f,-1.f},
{-1.f,1.f},
{1.f,1.f},
{1.f,-1.f}
},
SCROLL[6] =
{
{-1.f,1.f},
{-1.f,-1.f},
{1.f,-1.f},
{-1.f,1.f},
{1.f,1.f},
{1.f,-1.f}
},
LINECOL[6] =
{
{-1.f,1.f},
{-1.f,-1.f},
{.5f,-1.f},
{-1.f,1.f},
{.5f,1.f},
{.5f,-1.f}
} ;
static void print_to_screen(const char *buff)
{
#ifdef __EMSCRIPTEN__
emscripten_console_log(buff);
#else
printf("%s", buff);
#endif
}
static void error_callback(int error, const char *description)
{
print_to_screen("Error!");
print_to_screen(description);
}
static inline void load_char_tex(void)
{
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, char_tex);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, grid.width, grid.height, GL_RED, GL_FLOAT, chars_tex);
}
static inline void load_linenumber_tex(void)
{
glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D, linenumber_tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, lines_decimals_count, grid.height, 0, GL_RED, GL_FLOAT, linenumbers_tex);
}
inline void load_selection_tex(void)
{
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, select_tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, grid.width, grid.height, 0, GL_RED, GL_FLOAT, selection_tex);
}
inline void cursor_reset(void)
{
blink_count = 0;
blink_state = 1;
glUniform1f(cursor_blink_location, 1.);
}
static void cursor_position(GLfloat x, GLfloat y)
{
float aux[] = {++x, y};
glUniform2fv(cursor_location, 1, aux);
}
static inline void update_lines_count(short action)
{
lines_count += action;
curr_paragraph->lines_count += action;
}
static inline void update_all_paragraphs_lines_count(void)
{
PARA *paragraph_i = ¶graphs_head;
lines_count = 0;
do
{
lines_count += paragraph_i->lines_count = paragraph_i->buffer_count ? ceil(paragraph_i->buffer_count / grid.width) : 1;
paragraph_i = paragraph_i->next;
}
while(paragraph_i);
}
void get_top_paragraph_scroll(void)
{
top_line_number = 1;
top_paragraph_scroll = ¶graphs_head;
lines_scroll_top_diff = lines_scroll;
while(lines_scroll_top_diff >= top_paragraph_scroll->lines_count)
{
lines_scroll_top_diff -= top_paragraph_scroll->lines_count;
top_paragraph_scroll = top_paragraph_scroll->next;
++top_line_number;
}
}
static void update_grid_lines(void)
{
PARA *paragraph_i = top_paragraph_scroll;
k = m = 0;
lines_decimals_count = 0;
for(i = top_line_number + grid.height; i; i /= 10) ++lines_decimals_count; // Set to either grid.height or larger visible line
i = paragraph_i->lines_count-lines_scroll_top_diff;
if(i == paragraph_i->lines_count) linenumbers_tex[m] = (top_line_number+34.)/256.f;
else linenumbers_tex[m] = 0.;
goto FIRST_LINE_CONT;
do
{
linenumbers_tex[m] = 0.;
FIRST_LINE_CONT:
grid_lines[m] = paragraph_i;
line_numbers[m] = top_line_number;
++m;
--i;
}
while(i > 0);
i = top_line_number;
while(paragraph_i->next)
{
paragraph_i = paragraph_i->next;
++i;
j = paragraph_i->lines_count;
linenumbers_tex[m] = (i+34.)/256.f;
goto LINE_CONT;
do
{
linenumbers_tex[m] = 0.;
LINE_CONT:
grid_lines[m] = paragraph_i;
line_numbers[m] = i;
++m;
--j;
}
while(j > 0);
}
for(;m < grid.height; ++m)
{
grid_lines[m] = NULL; //Make sure grid_paragraph_count is updated when array gets resized
line_numbers[m] = 0;
linenumbers_tex[m] = 0.;
}
}
static void update_chars_tex(void)
{
PARA *paragraph_i = top_paragraph_scroll;
k = m = 0;
// if(paragraph_i->lines_count > lines_scroll_top_diff)
// {
n = lines_scroll_top_diff * grid.width;
i = paragraph_i->buffer_count + paragraph_i->gap_count - n;
for(j = 0; j < i; ++j) chars_tex[j] = paragraph_i->buffer[n + j];
goto CARRY_ON_PARAS;
// }
do
{
if(!paragraph_i->buffer_count) //Empty line
{
for(j = 0; j < grid.width; ++j) chars_tex[k + j] = 0.;
k += grid.width;
}
else
{
//for(j = 0; j < paragraph_i->buffer_count; ++j) chars_tex[k + j] = paragraph_i->buffer[j];
//gap
for(j = 0; j < paragraph_i->gap_pos; ++j) chars_tex[k + j] = paragraph_i->buffer[j];
for(i = 0; i < paragraph_i->gap_count; ++i) chars_tex[k + j + i] = paragraph_i->gap[i];
//k += i;
for(; j < paragraph_i->buffer_count; ++j) chars_tex[k + j + i] = paragraph_i->buffer[j+paragraph_i->gap_del];
j += i;
CARRY_ON_PARAS:
k += j;
l = k + grid.width-ceil((paragraph_i->buffer_count + paragraph_i->gap_count) % (int) grid.width);
for(;k < l; ++k) chars_tex[k] = 0.;
}
// if(curr_paragraph == paragraph_i)
// {
// curr_line = k/grid.width-1;
// cursor_position(paragraph_cursor % (int) grid.width+1, curr_line);
// }
//for(j = paragraph_i->lines_count; j >= 0; --j) grid_lines[m++] = paragraph_i;
//paragraph_i = paragraph_i->next;
}
while((paragraph_i = paragraph_i->next) && k < grid_full_size);
//for(;k < grid_full_size; ++k) chars_tex[k] = 65.f/256.f;
for(;k < grid_full_size; ++k) chars_tex[k] = 0.f;
update_grid_lines(); // Remove from here
load_char_tex();
}
static void new_paragraph(void)
{
PARA *paragraph_aux;
paragraph_cursor = 0;
paragraph_aux = (PARA*) malloc(sizeof(PARA));
paragraph_aux->buffer = (GLfloat*) malloc(sizeof(GLfloat)*PARA_BUFFER_SIZE);
paragraph_aux->buffer_pos = 0;
paragraph_aux->buffer_count = 0;
paragraph_aux->lines_count = 0;
paragraph_aux->gap = (GLfloat*) malloc(sizeof(GLfloat)*PARA_BUFFER_SIZE);
paragraph_aux->gap_pos = 0;
paragraph_aux->gap_count = 0;
paragraph_aux->gap_del = 0;
paragraph_aux->next = curr_paragraph->next;
paragraph_aux->prev = curr_paragraph;
curr_paragraph->next = paragraph_aux;
curr_paragraph = paragraph_aux;
++paragraphs_count;
}
static void free_paragraph(PARA *paragraph_aux)
{
free(paragraph_aux->buffer);
free(paragraph_aux->gap);
free(paragraph_aux);
}
static void mouse_position_callback(GLFWwindow *window, double xpos, double ypos)
{
glUseProgram(program_text);
mouse_pos[0] = ceil((xpos - grid_x_offset)/grid.cell_width);
mouse_pos[1] = ceil(ypos/grid.cell_height) - 1;
if(scroll_bar_grab) {
if(scroll_bar_grab > ypos)
scroll_callback(window, 0, -1);
}
if(xpos < window_size.width-SCROLL_BAR_WIDTH && xpos > grid_x_offset)
{
#ifndef __EMSCRIPTEN__
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
#endif
glUniform2fv(mouse_location, 1, mouse_pos);
cursor_reset();
}
#ifndef __EMSCRIPTEN__
else glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
#endif
}
static void mouse_button_callback(GLFWwindow *window, int button, int action, int mods)
{
int mouse_Y_scroll;
double xpos, ypos;
if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS)
{
glfwGetCursorPos(window, &xpos, &ypos);
mouse_Y_scroll = mouse_pos[1] + lines_scroll;
if(xpos < window_size.width-SCROLL_BAR_WIDTH)
{
merge_gap();
if(grid_lines[mouse_Y_scroll])
{
selection_state.mouse = 1;
selection_state.start_pos = mouse_Y_scroll * grid.width + mouse_pos[0];
curr_paragraph = grid_lines[mouse_Y_scroll];
for(i = 1; grid_lines[mouse_Y_scroll - i] == curr_paragraph; ++i);
paragraph_cursor = mouse_pos[0] + grid.width * --i - 1;
printf("line: %d paragraph selected: %p cursor: %d\n", mouse_Y_scroll, (void*)grid_lines[mouse_Y_scroll], paragraph_cursor);
cursor_position(mouse_pos[0] - 1, mouse_pos[1]);
}
cursor_reset();
}
else
{
//if(window_size.height - ypos > scroll_bar.top)
//else
if(window_size.height - ypos > scroll_bar.bottom)
scroll_bar_grab = 1;
//else
}
}
if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_RELEASE)
{
selection_state.mouse = 0;
//for(i = 0; i<grid_lines[(int)mouse_pos[1]]->buffer_count; ++i)
//selection_tex[(int) (mouse_pos[1] * grid.width + mouse_pos[0])] = 1./256.;
//selection_tex[(int) (mouse_pos[0] + mouse_pos[1] * grid.width)] = 1./256.;
scroll_bar_grab = 0;
}
//glfwGetCursorPos(window, &aux[0], &aux[1]);
}
static void resize_editor()
{
printf("w_w: %d w_h: %d\n", window_size.width, window_size.height);
float width_minus_scrollbar = window_size.width-SCROLL_BAR_WIDTH;
float width_grid_ratio = (width_minus_scrollbar-grid_x_offset)/grid.cell_width;
float height_grid_ratio = window_size.height/grid.cell_height;
update_all_paragraphs_lines_count();
cursor_reset();
printf(">>>>%d<<<<<\n", lines_decimals_count);
grid_x_offset = lines_decimals_count * grid.cell_width;
grid.width = floor(width_grid_ratio);
grid.height = floor(height_grid_ratio);
grid_full_size = grid.width*grid.height;
grid_y_offset = (height_grid_ratio-grid.height)*grid.cell_height;
glUniform4fv(grid_location, 1, (GLfloat*) &grid);
SCROLL[0].x =
SCROLL[1].x =
SCROLL[3].x = width_minus_scrollbar*2/window_size.width - 1.f;
SCREEN[0].x =
SCREEN[1].x =
SCREEN[3].x =
LINECOL[2].x =
LINECOL[4].x =
LINECOL[5].x = grid_x_offset * 2.f/window_size.width - 1.f;
SCREEN[2].x =
SCREEN[4].x =
SCREEN[5].x = (width_minus_scrollbar-(width_grid_ratio-grid.width)*grid.cell_width)*2/window_size.width - 1.f;
SCREEN[1].y =
SCREEN[2].y =
SCREEN[5].y =
LINECOL[1].y =
LINECOL[2].y =
LINECOL[5].y = grid_y_offset*2/window_size.height - 1.f;
grid_y_offset = grid.cell_height - grid_y_offset;
glUniform1f(grid_Y_offset_location, (GLfloat) grid_y_offset);
glUniform1f(grid_X_offset_location, (GLfloat) grid_x_offset);
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_linenumber);
glBufferData(GL_ARRAY_BUFFER, sizeof(LINECOL), LINECOL, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_scroll);
glBufferData(GL_ARRAY_BUFFER, sizeof(SCROLL), SCROLL, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(SCREEN), SCREEN, GL_STATIC_DRAW);
//glfwGetFramebufferSize(window, &window_size.width, &window_size.height);
glViewport(0, 0, window_size.width, window_size.height);
if(grid.height != grid_paragraph_count)
{
printf("Reallocing_____>\n");
grid_paragraph_count = grid.height;
grid_lines = (PARA**) realloc(grid_lines, grid_paragraph_count * sizeof(PARA*));
line_numbers = (GLfloat*) realloc(line_numbers, grid_paragraph_count * sizeof(unsigned int));
}
// while(grid_full_size > chars_buffer_size)
// {
// chars_tex = (GLfloat*) realloc(chars_tex, chars_buffer_size+INITIAL_CHARS_BUFFER_SIZE);
// //for(i = chars_buffer_size; i<(chars_buffer_size+INITIAL_CHARS_BUFFER_SIZE); ++i) chars_tex[i] = 0.;
// chars_buffer_size += INITIAL_CHARS_BUFFER_SIZE;
// if(chars_tex == NULL)
// {
// print_to_screen("Expanding mem failed!\n");
// exit(1);
// }
// }
//cursor_position(paragraph_cursor % (int)grid.width, curr_line);
// Init texture. Move to subteximage ?
//load_char_tex();
//glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, char_tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, grid.width, grid.height, 0, GL_RED, GL_FLOAT, chars_tex);
//glActiveTexture(GL_TEXTURE3);
linenumber_update(lines_count);
// glUseProgram(program_linenumber);
// glUniform4fv(grid_location_linenumber, 1, (GLfloat*) &grid);
// glBindTexture(GL_TEXTURE_2D, linenumber_tex);
// glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, lines_decimals_count+1, grid.height, 0, GL_RED, GL_FLOAT, linenumbers_tex);
}
static inline void window_size_callback(GLFWwindow *window, int width, int height)
{
window_size.width = width;
window_size.height = height;
resize_editor();
}
static inline void insert_one_char(unsigned int codepoint)
{
if(curr_paragraph->gap_count)
{
curr_paragraph->gap[curr_paragraph->gap_count++] = (codepoint-14)/256.;
++paragraph_cursor;
}
else if(paragraph_cursor < curr_paragraph->buffer_count)
{
//if(!curr_paragraph->gap_count)
curr_paragraph->gap_pos = paragraph_cursor;
curr_paragraph->gap[curr_paragraph->gap_count++] = (codepoint-14)/256.;
++paragraph_cursor;
}
else
{
curr_paragraph->buffer[paragraph_cursor++] = (codepoint-14)/256.;
++curr_paragraph->buffer_count;
}
if(paragraph_cursor && !(paragraph_cursor % (int) grid.width))
//if(paragraph_cursor && !((curr_paragraph->buffer_count + curr_paragraph->gap_count) % (int) grid.width))
{
update_lines_count(LINE_ADD);
++curr_line;
if(curr_line >= grid.height)
{
printf("OUT SCROLL %d\n", curr_line);
lines_scroll = curr_line - grid.height + 1;
get_top_paragraph_scroll();
}
}
}
static inline void character_callback(GLFWwindow *window, unsigned int codepoint)
{
// switch(codepoint)
// {
// case 32 ... 126:
if(codepoint >= 32 && codepoint <= 126)
{
insert_one_char(codepoint);
cursor_position((paragraph_cursor) % (int) grid.width, curr_line);
cursor_reset();
// }
// else
// {
// chars_tex[paragraph_cursor % (int)grid.width + curr_line * (int)grid.width] = (codepoint-32)/256.;
// load_char_tex();
// }
// break;
}
}
static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods)
{
int i;
PARA *paragraph_aux;
if(action == GLFW_PRESS || action == GLFW_REPEAT) switch(key)
{
case GLFW_KEY_UP:
if (mods == GLFW_MOD_SHIFT)
{
grid.cell_height -= 1.;
resize_editor();
}
else
{
merge_gap();
if(paragraph_cursor > grid.width)
{
printf("cursor same para\n");
//paragraph_cursor = paragraph_cursor - paragraph_cursor % (int)grid.width;
paragraph_cursor -= grid.width;
--curr_line;
}
else if(curr_paragraph->prev)
{
merge_gap();
curr_paragraph = curr_paragraph->prev;
if(curr_paragraph->buffer_count < paragraph_cursor) paragraph_cursor = curr_paragraph->buffer_count;
--curr_line;
}
}
break;
case GLFW_KEY_DOWN:
if (mods == GLFW_MOD_SHIFT)
{
grid.cell_height += 1.;
resize_editor();
}
else
{
merge_gap();
if(paragraph_cursor + grid.width <= curr_paragraph->buffer_count)
{
printf("____________X\n");
paragraph_cursor += grid.width;
++curr_line;
}
//else if(grid.width - curr_paragraph->buffer_count % paragraph_cursor)
else if(curr_paragraph->buffer_count - paragraph_cursor > grid.width)
{
printf("____________Y\n");
paragraph_cursor = curr_paragraph->buffer_count;
++curr_line;
}
else if(curr_paragraph->next)
{
printf("____________Z\n");
merge_gap();
curr_paragraph = curr_paragraph->next;
if(curr_paragraph->buffer_count < paragraph_cursor) paragraph_cursor = curr_paragraph->buffer_count;
++curr_line;
}
else printf("____________Q\n");
}
break;
case GLFW_KEY_RIGHT:
if (mods == GLFW_MOD_SHIFT)
{
grid.cell_width += 1.;
resize_editor();
}
else
{
merge_gap();
if(paragraph_cursor < curr_paragraph->buffer_count)
{
++paragraph_cursor;
if(!((paragraph_cursor) % (int)grid.width)) ++curr_line;
}
}
break;
case GLFW_KEY_LEFT:
if (mods == GLFW_MOD_SHIFT)
{
grid.cell_width -= 1.;
resize_editor();
}
else
{
merge_gap();
if(paragraph_cursor)
{
if(!((paragraph_cursor) % (int)grid.width)) --curr_line;
--paragraph_cursor;
}
}
break;
case GLFW_KEY_LEFT_SHIFT:
break;
#ifndef __EMSCRIPTEN__
paste_buff = glfwGetClipboardString(window);
i = 0;
while(paste_buff[i] != '\0')
{
if(paste_buff[i] == 13 || paste_buff[i] == 10) new_paragraph(); // CR/LF
else
{
curr_paragraph->buffer[paragraph_cursor] = (paste_buff[i]-14)/256.;
++curr_paragraph->buffer_count;
++paragraph_cursor;
}
++i;
}
//update_lines_count();
//update_all_paragraphs_lines_count();
#endif
break;
case GLFW_KEY_PAGE_UP:
lines_scroll -= grid.height;
//update_lines_count();
break;
case GLFW_KEY_PAGE_DOWN:
lines_scroll += grid.height;
//update_lines_count();
break;
case GLFW_KEY_END:
merge_gap();
paragraph_cursor = curr_paragraph->buffer_count;
break;
case GLFW_KEY_HOME:
merge_gap();
paragraph_cursor = 0;
curr_line -= curr_paragraph->lines_count;
break;
case GLFW_KEY_BACKSPACE:
if(curr_paragraph->gap_count)
{
printf("backspace gap\n");
if(!((paragraph_cursor)%(int)grid.width))
{
printf("remove line\n");
--curr_line;
update_lines_count(LINE_REMOVE);
}
--curr_paragraph->gap_count;
--paragraph_cursor;
}
else
{
merge_gap();
if(curr_paragraph->buffer_count && paragraph_cursor)
{
if(!((paragraph_cursor)%(int)grid.width))
{
printf("remove line\n");
--curr_line;
update_lines_count(LINE_REMOVE);
}
--curr_paragraph->buffer_count;
--paragraph_cursor;
if(paragraph_cursor != curr_paragraph->buffer_count)
{
printf("backspace middle\n");
curr_paragraph->gap_pos = paragraph_cursor;
// if(!((paragraph_cursor)%(int)grid.width))
// {
// printf("backspace remove line\n");
// }
++curr_paragraph->gap_del;
}
else
{
printf("backspace end\n");
}
}
else
{
if(curr_paragraph->prev)
{
printf("backspace prev line\n");
curr_paragraph->prev->next = curr_paragraph->next;
if(curr_paragraph->next) curr_paragraph->next->prev = curr_paragraph->prev;
paragraph_aux = curr_paragraph;
curr_paragraph = curr_paragraph->prev;
paragraph_cursor = curr_paragraph->buffer_count;
free_paragraph(paragraph_aux);
--curr_line;
//update_lines_count(LINE_REMOVE);
}
}
}
break;
case GLFW_KEY_ENTER:
merge_gap();
//chars_tex[paragraph_cursor] = 0;
//paragraph_cursor += 160-paragraph_cursor%160;
if(paragraph_cursor != curr_paragraph->buffer_count)
{
printf("chop paragraph\n");
}
new_paragraph();
update_lines_count(LINE_ADD);
++curr_line;
//if(lines_count/(lines_decimals_count+1)*10) resize_editor();
break;
case GLFW_KEY_ESCAPE:
//for(i = 0; i < chars_buffer_size; ++i) selection_tex[i] = 0.;
//load_selection_tex();
break;
case GLFW_KEY_TAB:
#ifdef __EMSCRIPTEN__
//call_alert();
#endif
for(i = 0; i < grid.height; ++i)
{
printf("[%d]: %p %12f\n", i, (void*)grid_lines[i], line_numbers[i]);
}
printf("curr_paragraph: %p prev_paragraph: %p lines_count: %u curr_line: %u\ntop: %f bottom: %f\nparagraph_cursor: %d paragraph_count: %d\ngap_pos: %d gap_del: %d gap_count: %d top_line_number: %d\n", (void*)curr_paragraph, (void*)curr_paragraph->prev, lines_count, curr_line, scroll_bar.top, scroll_bar.bottom, paragraph_cursor, curr_paragraph->buffer_count, curr_paragraph->gap_pos, curr_paragraph->gap_del, curr_paragraph->gap_count, top_line_number);
break;
case GLFW_KEY_RIGHT_SHIFT:
#ifdef __EMSCRIPTEN__
//copy_to_clipboard((char)chars_tex[0], paragraph_cursor);
#endif
break;
}
//update_chars_tex();
//cursor_position();
cursor_position((paragraph_cursor) % (int) grid.width, curr_line);
cursor_reset();
}
#ifdef __EMSCRIPTEN__
int paste_char(char *c) {
if(*c == 13 || *c == 10) // CR/LF
{
new_paragraph();
}
curr_paragraph->buffer[paragraph_cursor] = (c[0]-14)/256.;
++curr_paragraph->buffer_count;
++paragraph_cursor;
return 0;
}
static EM_BOOL on_web_display_size_changed(int event_type, const EmscriptenUiEvent *event, void *user_data)
{
double w, h;
emscripten_get_element_css_size( "#canvas", &w, &h);
printf("____%f %f\n", w, h);
window_size_callback(window, (int) w, (int) h);
return 0;
}
static void frame(void) {}
void draw(void) {
glfwPollEvents();
#else
static void frame(void) {
++blink_count;
if(BLINKS_START > blink_count) glfwWaitEventsTimeout(.5);
else if(blink_count <= BLINKS_LIMIT)
{
glUniform1f(cursor_blink_location, (GLfloat) blink_state);
blink_state ^= 1;
glfwWaitEventsTimeout(.5);
}
else glfwWaitEvents();
#endif
#ifdef SHOW_FPS
clock_gettime(CLOCK_REALTIME, &start);
#endif
// REMOVE MOST OF THESE FROM HERE
update_chars_tex();
load_selection_tex();
scrollbar_update();
cursor_position(paragraph_cursor % (int)grid.width, curr_line);
linenumber_update(lines_count); //For now
//load_linenumber_tex();
//cursor_position(paragraph_cursor % (int) grid.width+1, grid.width-1);
//sleep(5);
//stop = clock();
//#ifdef SHOW_FPS
// curr_frame_time = glfwGetTime();
// fps_avg += 1000./(curr_frame_time - last_frame_time);
// last_frame_time = curr_frame_time;
//// blink_count += curr_frame_time - last_frame_time;
//// if(blink_count > BLINK_TIME)
//// {
//// blink_count = 0;
//// chars_tex[(paragraph_cursor)] = chars_tex[(paragraph_cursor)] ? 0 : 95./256.;
//// update_chars_tex();
//// }
// if(fps_avg_count == FPS_AVG_COUNT)
// {
//
// fps_avg = fps_avg/FPS_AVG_COUNT/1000.;
// fps_int = fps_avg;
// fps_dec = (fps_avg-fps_int)*1000;
// chars_tex[grid_full_size-3] = 38./256.;
// chars_tex[grid_full_size-2] = 48./256.;
// chars_tex[grid_full_size-1] = 51./256.;
// i = 5;
// do
// {
// j = fps_dec / 10;
// chars_tex[grid_full_size-i] = (fps_dec-j*10+16)/256.;
// fps_dec = j;
// ++i;
// }
// while(fps_dec);
// chars_tex[grid_full_size-i] = 14./256.;
// ++i;
// do
// {
// j = fps_int / 10;
// chars_tex[grid_full_size-i] = (fps_int-j*10+16)/256.;
// fps_int = j;
// ++i;
// }
// while(fps_int);
//
// fps_avg_count = 0;
// fps_avg = 0;
// update_chars_tex();
//
// } else ++fps_avg_count;
//#endif
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(program_scroll);
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_scroll);
glVertexAttribPointer(vpos_location_scroll, 2, GL_FLOAT, GL_FALSE, sizeof(SCROLL[0]), (void*) 0);
glDrawArrays(GL_TRIANGLES, 0, 6);
glUseProgram(program_linenumber);
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_linenumber);
glVertexAttribPointer(vpos_location_linenumber, 2, GL_FLOAT, GL_FALSE, sizeof(LINECOL[0]), (void*) 0);
glDrawArrays(GL_TRIANGLES, 0, 6);
glUseProgram(program_text);
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
glVertexAttribPointer(vpos_location_text, 2, GL_FLOAT, GL_FALSE, sizeof(SCREEN[0]), (void*) 0);
glDrawArrays(GL_TRIANGLES, 0, 6);
glfwSwapBuffers(window);
#ifdef SHOW_FPS
clock_gettime(CLOCK_REALTIME, &stop);
printf(">FPS: %lf\n", (double)1./((stop.tv_sec - start.tv_sec) + (stop.tv_nsec - start.tv_nsec)/1E9));
#endif
}
int main(void) {
int success;
char infoLog[516];
print_to_screen("main function started");
glfwSetErrorCallback(error_callback);
if (glfwInit() != GL_TRUE)
{
print_to_screen("glfwInit() failed!");
glfwTerminate();
exit(1);