bb98812f3d90a76148ea0bcedac79f1e6b91188b
[vg.git] / vg_imgui.h
1 /* Copyright (C) 2021-2023 Harry Godden (hgn) - All Rights Reserved */
2
3 /*
4 * Principles:
5 *
6 * 1. layout is defined by subdividing
7 * 2. a parent node should never be resized by the content after creation
8 * 3. when the ui is in an interactive state, no controls should ever move
9 */
10
11 #ifndef VG_IMGUI_H
12 #define VG_IMGUI_H
13
14 #define VG_GAME
15 #include "vg/vg.h"
16 #include "vg/vg_tex.h"
17 #include "vg/vg_shader.h"
18
19 typedef i16 ui_px;
20 typedef ui_px ui_rect[4];
21 typedef struct ui_vert ui_vert;
22
23 enum ui_axis {
24 k_ui_axis_h = 0x0u,
25 k_ui_axis_v = 0x1u,
26 };
27
28 /* Relative to cursor p0 */
29 enum ui_align
30 { /* DC BA */
31 k_ui_align_left = 0x0000| 0x00,
32 k_ui_align_right = 0x0000| 0x01,
33 k_ui_align_center = 0x0000| 0x02,
34
35 k_ui_align_middle = 0x0100,
36 k_ui_align_middle_left = 0x0100| 0x00,
37 k_ui_align_middle_right = 0x0100| 0x01,
38 k_ui_align_middle_center = 0x0100| 0x02,
39
40 k_ui_align_bottom = 0x0200,
41 k_ui_align_bottom_left = 0x0200| 0x00,
42 k_ui_align_bottom_right = 0x0200| 0x01,
43 k_ui_align_bottom_center = 0x0200| 0x02,
44 };
45
46 #pragma pack(push,1)
47 struct ui_vert
48 {
49 ui_px co[2];
50 u8 uv[2];
51 u32 colour;
52 };
53 #pragma pack(pop)
54
55 enum ui_scheme_colour{
56 k_ui_bg = 0,
57 k_ui_fg = 8,
58 k_ui_hue = 16,
59 k_ui_red = 16,
60 k_ui_orange,
61 k_ui_yellow,
62 k_ui_green,
63 k_ui_aqua,
64 k_ui_blue,
65 k_ui_purple,
66 k_ui_gray,
67 k_ui_brighter = 8
68 };
69
70 typedef u32 ui_scheme[8*4];
71
72 #define UI_RGB( STDHEX ) 0xff000000 |\
73 ((STDHEX&0x000000ff)<<16) |\
74 ((STDHEX&0x0000ff00) ) |\
75 ((STDHEX&0x00ff0000)>>16)
76
77 #define UI_TEXTBOX_MULTILINE 0x1
78 #define UI_TEXTBOX_WRAP 0x2
79 #define UI_TEXTBOX_AUTOFOCUS 0x4
80 #define UI_MOUSE_LEFT (SDL_BUTTON(SDL_BUTTON_LEFT))
81 #define UI_MOUSE_RIGHT (SDL_BUTTON(SDL_BUTTON_RIGHT))
82 #define UI_MOUSE_MIDDLE (SDL_BUTTON(SDL_BUTTON_MIDDLE))
83
84 struct{
85 struct ui_vert *vertex_buffer;
86 u16 *indice_buffer;
87 u32 max_verts, max_indices,
88 cur_vert, cur_indice,
89 vert_start, indice_start;
90
91 union {
92 void *focused_control_id; /* uses the memory location of various locking
93 controls as an id */
94 char *textbuf;
95 };
96
97 u32 focused_control_hit;
98 enum ui_control_type{
99 k_ui_control_none,
100 k_ui_control_textbox,
101 }
102 focused_control_type;
103
104 struct ui_textbuf{
105 int cursor_user, cursor_pos;
106 u32 len;
107 u32 flags;
108 }
109 textbox;
110
111 void (*textbuf_on_enter) ( char *buf, u32 len );
112 void (*textbuf_on_up) ( char *buf, u32 len );
113 void (*textbuf_on_down) ( char *buf, u32 len );
114 void (*textbuf_on_change)( char *buf, u32 len );
115
116 GLuint tex_glyphs, vao, vbo, ebo;
117
118 ui_px mouse[2], mouse_click[2];
119 u32 mouse_state[2];
120 u32 ignore_input_frames;
121 int wants_mouse;
122
123 ui_rect click_fader, click_fader_end;
124 float click_fade_opacity;
125
126 ui_scheme scheme;
127
128 enum ui_cursor{
129 k_ui_cursor_default,
130 k_ui_cursor_ibeam,
131 k_ui_cursor_hand,
132 k_ui_cursor_max
133 }
134 cursor;
135
136 SDL_Cursor *cursor_map[ k_ui_cursor_max ];
137 }
138 static vg_ui = {
139 .scheme = {
140 [ k_ui_bg+0 ] = UI_RGB( 0x1d2021 ),
141 [ k_ui_bg+1 ] = UI_RGB( 0x282828 ),
142 [ k_ui_bg+2 ] = UI_RGB( 0x3c3836 ),
143 [ k_ui_bg+3 ] = UI_RGB( 0x504945 ),
144 [ k_ui_bg+4 ] = UI_RGB( 0x665c54 ),
145 [ k_ui_bg+5 ] = UI_RGB( 0x7c6f64 ),
146 [ k_ui_bg+6 ] = UI_RGB( 0x928374 ),
147 [ k_ui_bg+7 ] = UI_RGB( 0xa89984 ),
148
149 [ k_ui_fg+0 ] = UI_RGB( 0xebdbb2 ),
150 [ k_ui_fg+1 ] = UI_RGB( 0xfbf1c7 ),
151 [ k_ui_fg+2 ] = UI_RGB( 0xd5c4a1 ),
152 [ k_ui_fg+3 ] = UI_RGB( 0xbdae93 ),
153 [ k_ui_fg+4 ] = UI_RGB( 0xa89984 ),
154 [ k_ui_fg+5 ] = UI_RGB( 0x000000 ),
155 [ k_ui_fg+6 ] = UI_RGB( 0x000000 ),
156 [ k_ui_fg+7 ] = UI_RGB( 0x000000 ),
157
158 [ k_ui_red ] = UI_RGB( 0xcc241d ),
159 [ k_ui_orange ] = UI_RGB( 0xd65d0e ),
160 [ k_ui_yellow ] = UI_RGB( 0xd79921 ),
161 [ k_ui_green ] = UI_RGB( 0x98971a ),
162 [ k_ui_aqua ] = UI_RGB( 0x689d6a ),
163 [ k_ui_blue ] = UI_RGB( 0x458588 ),
164 [ k_ui_purple ] = UI_RGB( 0xb16286 ),
165 [ k_ui_gray ] = UI_RGB( 0x928374 ),
166 [ k_ui_red + k_ui_brighter ] = UI_RGB( 0xfb4934 ),
167 [ k_ui_orange + k_ui_brighter ] = UI_RGB( 0xfe8019 ),
168 [ k_ui_yellow + k_ui_brighter ] = UI_RGB( 0xfabd2f ),
169 [ k_ui_green + k_ui_brighter ] = UI_RGB( 0xb8bb26 ),
170 [ k_ui_aqua + k_ui_brighter ] = UI_RGB( 0x8ec07c ),
171 [ k_ui_blue + k_ui_brighter ] = UI_RGB( 0x83a598 ),
172 [ k_ui_purple + k_ui_brighter ] = UI_RGB( 0xd3869b ),
173 [ k_ui_gray + k_ui_brighter ] = UI_RGB( 0xa89984 ),
174 }
175 };
176
177 static struct vg_shader _shader_ui =
178 {
179 .name = "[vg] ui",
180 .link = NULL,
181 .vs =
182 {
183 .orig_file = NULL,
184 .static_src =
185 "layout (location=0) in vec2 a_co;"
186 "layout (location=1) in vec2 a_uv;"
187 "layout (location=2) in vec4 a_colour;"
188 "uniform mat3 uPv;"
189 ""
190 "out vec2 aTexCoords;"
191 "out vec4 aColour;"
192 "out vec2 aWsp;"
193 ""
194 "void main()"
195 "{"
196 "gl_Position = vec4( uPv * vec3( a_co, 1.0 ), 1.0 );"
197 "aTexCoords = a_uv * 0.0078125;"
198 "aColour = a_colour;"
199
200 "aWsp = a_co;"
201 "}",
202 },
203 .fs =
204 {
205 .orig_file = NULL,
206 .static_src =
207 "uniform sampler2D uTexGlyphs;"
208 "out vec4 FragColor;"
209 ""
210 "in vec2 aTexCoords;"
211 "in vec4 aColour;"
212 ""
213 "in vec2 aWsp;"
214
215 "vec2 rand_hash22( vec2 p )"
216 "{"
217 "vec3 p3 = fract(vec3(p.xyx) * 213.8976123);"
218 "p3 += dot(p3, p3.yzx+19.19);"
219 "return fract(vec2((p3.x + p3.y)*p3.z, (p3.x+p3.z)*p3.y));"
220 "}"
221 ""
222 "void main()"
223 "{"
224 "vec4 diffuse = aColour;"
225
226 "if( diffuse.a == 0.0 )"
227 "{"
228 "diffuse.a = texture( uTexGlyphs, aTexCoords ).r;"
229 "}"
230
231 "FragColor = diffuse;"
232 "}"
233 }
234 };
235
236 static struct vg_shader _shader_ui_image =
237 {
238 .name = "[vg] ui_image",
239 .link = NULL,
240 .vs =
241 {
242 .orig_file = NULL,
243 .static_src =
244 "layout (location=0) in vec2 a_co;"
245 "layout (location=1) in vec2 a_uv;"
246 "layout (location=2) in vec4 a_colour;"
247 "uniform mat3 uPv;"
248
249 "out vec2 aTexCoords;"
250 "out vec4 aColour;"
251 "out vec2 aWsp;"
252
253 "void main()"
254 "{"
255 "gl_Position = vec4( uPv * vec3( a_co, 1.0 ), 1.0 );"
256 "aTexCoords = a_uv * 0.0078125;"
257 "aColour = a_colour;"
258
259 "aWsp = a_co;"
260 "}",
261 },
262 .fs =
263 {
264 .orig_file = NULL,
265 .static_src =
266 "uniform sampler2D uTexImage;"
267 "out vec4 FragColor;"
268
269 "in vec2 aTexCoords;"
270 "in vec4 aColour;"
271 "in vec2 aWsp;"
272
273 "void main()"
274 "{"
275 "vec4 colour = texture( uTexImage, aTexCoords );"
276
277 /* wtf is this?? */
278 #if 0
279 "float value = dot(vec4(1.0),colour)*0.25;"
280
281 "vec3 col = vec3(pow(cos(value*3.14159265*2.0)*0.5+0.5,0.5))"
282 "* vec3(step(value,0.5),0.3,step(1.0-value,0.5));"
283 "FragColor = vec4( col*4.0, 1.0 );"
284 #endif
285
286 "FragColor = colour;"
287 "}"
288 }
289 };
290 #define UI_GLYPH_SPACING_X 8
291
292 VG_STATIC void _vg_ui_init(void)
293 {
294 if( !vg_shader_compile( &_shader_ui ) ||
295 !vg_shader_compile( &_shader_ui_image ) )
296 vg_fatal_error( "Failed to compile ui shader" );
297
298 /*
299 * Vertex buffer
300 * ----------------------------------------
301 */
302
303 vg_ui.max_indices = 20000;
304 vg_ui.max_verts = 30000;
305
306 /* Generate the buffer we are gonna be drawing to */
307 glGenVertexArrays( 1, &vg_ui.vao );
308 glGenBuffers( 1, &vg_ui.vbo );
309 glGenBuffers( 1, &vg_ui.ebo );
310
311 glBindVertexArray( vg_ui.vao );
312 glBindBuffer( GL_ARRAY_BUFFER, vg_ui.vbo );
313
314 glBufferData( GL_ARRAY_BUFFER,
315 vg_ui.max_verts * sizeof( struct ui_vert ),
316 NULL, GL_DYNAMIC_DRAW );
317 glBindVertexArray( vg_ui.vao );
318
319 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, vg_ui.ebo );
320 glBufferData( GL_ELEMENT_ARRAY_BUFFER,
321 vg_ui.max_indices * sizeof( u16 ), NULL, GL_DYNAMIC_DRAW );
322
323 VG_CHECK_GL_ERR();
324
325 /* Set pointers */
326 u32 const stride = sizeof( struct ui_vert );
327
328 /* XY */
329 glVertexAttribPointer( 0, 2, GL_SHORT, GL_FALSE, stride,
330 (void *)offsetof( struct ui_vert, co ) );
331 glEnableVertexAttribArray( 0 );
332
333 /* UV */
334 glVertexAttribPointer( 1, 2, GL_UNSIGNED_BYTE, GL_FALSE, stride,
335 (void *)offsetof( struct ui_vert, uv ) );
336 glEnableVertexAttribArray( 1 );
337
338 /* COLOUR */
339 glVertexAttribPointer( 2, 4, GL_UNSIGNED_BYTE, GL_TRUE, stride,
340 (void *)offsetof( struct ui_vert, colour ) );
341 glEnableVertexAttribArray( 2 );
342
343 VG_CHECK_GL_ERR();
344
345 /* Alloc RAM default context */
346 u32 vert_size = vg_ui.max_verts*sizeof(struct ui_vert),
347 inds_size = vg_align8( vg_ui.max_indices*sizeof(u16) );
348
349 vg_ui.vertex_buffer = vg_linear_alloc( vg_mem.rtmemory, vert_size );
350 vg_ui.indice_buffer = vg_linear_alloc( vg_mem.rtmemory, inds_size );
351
352 /* font
353 * -----------------------------------------------------
354 */
355
356 /* Load default font */
357 u32 compressed[] = {
358 #include "vg/vg_pxfont_thin.h"
359 };
360
361 u32 pixels = 0, total = 256*256, data = 0;
362 u8 image[256*256];
363
364 while( pixels < total ){
365 for( int b = 31; b >= 0; b-- ){
366 image[ pixels ++ ] = (compressed[data] & (0x1u << b))? 0xffu: 0x00u;
367
368 if( pixels >= total ){
369 total = 0;
370 break;
371 }
372 }
373 data++;
374 }
375
376 glGenTextures( 1, &vg_ui.tex_glyphs );
377 glBindTexture( GL_TEXTURE_2D, vg_ui.tex_glyphs );
378 glTexImage2D( GL_TEXTURE_2D, 0, GL_R8, 256, 256, 0,
379 GL_RED, GL_UNSIGNED_BYTE, image );
380
381 VG_CHECK_GL_ERR();
382 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
383 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
384 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
385 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
386
387 /*
388 * Cursors
389 * ---------------------------------------------------------------
390 */
391
392 vg_ui.cursor_map[ k_ui_cursor_default ] =
393 SDL_CreateSystemCursor( SDL_SYSTEM_CURSOR_ARROW );
394 vg_ui.cursor_map[ k_ui_cursor_hand ] =
395 SDL_CreateSystemCursor( SDL_SYSTEM_CURSOR_HAND );
396 vg_ui.cursor_map[ k_ui_cursor_ibeam ] =
397 SDL_CreateSystemCursor( SDL_SYSTEM_CURSOR_IBEAM );
398 }
399
400 enum ui_shader {
401 k_ui_shader_colour,
402 k_ui_shader_image
403 };
404
405 static void rect_copy( ui_rect a, ui_rect b )
406 {
407 for( int i=0; i<4; i++ )
408 b[i] = a[i];
409 }
410
411 VG_STATIC void ui_flush( enum ui_shader shader )
412 {
413 u32 vertex_offset = vg_ui.vert_start*sizeof(ui_vert),
414 vertex_count = vg_ui.cur_vert-vg_ui.vert_start,
415 vertex_size = vertex_count*sizeof(ui_vert),
416
417 indice_offset = vg_ui.indice_start*sizeof(u16),
418 indice_count = vg_ui.cur_indice-vg_ui.indice_start,
419 indice_size = indice_count * sizeof(u16);
420
421 if( !vertex_size || !indice_size )
422 return;
423
424 glBindVertexArray( vg_ui.vao );
425 glBindBuffer( GL_ARRAY_BUFFER, vg_ui.vbo );
426 glBufferSubData( GL_ARRAY_BUFFER, vertex_offset, vertex_size,
427 vg_ui.vertex_buffer+vg_ui.vert_start );
428
429 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, vg_ui.ebo );
430 glBufferSubData( GL_ELEMENT_ARRAY_BUFFER, indice_offset, indice_size,
431 vg_ui.indice_buffer+vg_ui.indice_start );
432
433 glDisable( GL_DEPTH_TEST );
434 glEnable( GL_BLEND );
435 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
436 glBlendEquation( GL_FUNC_ADD );
437
438 m3x3f view = M3X3_IDENTITY;
439 m3x3_translate( view, (v3f){ -1.0f, 1.0f, 0.0f } );
440 m3x3_scale( view, (v3f){ 1.0f/((float)vg.window_x*0.5f),
441 -1.0f/((float)vg.window_y*0.5f), 1.0f } );
442
443 if( shader == k_ui_shader_colour ){
444 glUseProgram( _shader_ui.id );
445
446 glUniformMatrix3fv( glGetUniformLocation( _shader_ui.id, "uPv" ), 1,
447 GL_FALSE, (float *)view );
448
449 glActiveTexture( GL_TEXTURE0 );
450 glBindTexture( GL_TEXTURE_2D, vg_ui.tex_glyphs );
451 glUniform1i( glGetUniformLocation( _shader_ui.id, "uTexGlyphs" ), 0 );
452 }
453 else if( shader == k_ui_shader_image ){
454 glUseProgram( _shader_ui_image.id );
455 glUniformMatrix3fv( glGetUniformLocation( _shader_ui_image.id, "uPv" ), 1,
456 GL_FALSE, (float *)view );
457 glUniform1i( glGetUniformLocation(_shader_ui_image.id,"uTexImage"), 0 );
458 }
459 else
460 vg_fatal_error( "Invalid UI shader (%d)\n", shader );
461
462 glDrawElements( GL_TRIANGLES, indice_count, GL_UNSIGNED_SHORT,
463 (void *)(vg_ui.indice_start*sizeof(u16)) );
464
465 glDisable( GL_BLEND );
466
467 vg_ui.indice_start = vg_ui.cur_indice;
468 vg_ui.vert_start = vg_ui.cur_vert;
469 }
470
471 static void ui_fill_rect( ui_rect rect, u32 colour, ui_px uv[4] )
472 {
473 /* this if far from ideal but stops us from crashing */
474 if( (vg_ui.cur_vert + 4 > vg_ui.max_verts) ||
475 (vg_ui.cur_indice + 6 > vg_ui.max_indices))
476 return;
477
478 struct ui_vert *vertices = &vg_ui.vertex_buffer[ vg_ui.cur_vert ];
479 u16 *indices = &vg_ui.indice_buffer[ vg_ui.cur_indice ];
480
481 for( int i=0; i<4; i++ ){
482 vertices[i].colour = colour;
483 }
484
485 vertices[0].co[0] = rect[0];
486 vertices[0].co[1] = rect[1];
487 vertices[0].uv[0] = uv[0];
488 vertices[0].uv[1] = uv[1];
489 vertices[1].co[0] = rect[0]+rect[2];
490 vertices[1].co[1] = rect[1];
491 vertices[1].uv[0] = uv[2];
492 vertices[1].uv[1] = uv[1];
493 vertices[2].co[0] = rect[0]+rect[2];
494 vertices[2].co[1] = rect[1]+rect[3];
495 vertices[2].uv[0] = uv[2];
496 vertices[2].uv[1] = uv[3];
497 vertices[3].co[0] = rect[0];
498 vertices[3].co[1] = rect[1]+rect[3];
499 vertices[3].uv[0] = uv[0];
500 vertices[3].uv[1] = uv[3];
501 u16 ind_start = vg_ui.cur_vert;
502
503 u16 start = vg_ui.cur_vert;
504 u32 mesh[] = { 0,2,1, 0,3,2 };
505
506 for( u32 i=0; i<vg_list_size(mesh); i++ ){
507 indices[i] = start+mesh[i];
508 }
509
510 vg_ui.cur_indice += 6;
511 vg_ui.cur_vert += 4;
512 }
513
514 static void ui_fill( ui_rect rect, u32 colour )
515 {
516 ui_fill_rect( rect, colour, (ui_px[4]){ 4,4,4,4 } );
517 }
518
519 static void ui_outline( ui_rect rect, ui_px thickness, u32 colour )
520 {
521 /* this if far from ideal but stops us from crashing */
522 if( (vg_ui.cur_vert + 8 > vg_ui.max_verts) ||
523 (vg_ui.cur_indice + 24 > vg_ui.max_indices))
524 return;
525
526 struct ui_vert *vertices = &vg_ui.vertex_buffer[ vg_ui.cur_vert ];
527 u16 *indices = &vg_ui.indice_buffer[ vg_ui.cur_indice ];
528
529 for( int i=0; i<8; i++ ){
530 vertices[i].uv[0] = 4;
531 vertices[i].uv[1] = 4;
532 vertices[i].colour = colour;
533 }
534
535 vertices[0].co[0] = rect[0];
536 vertices[0].co[1] = rect[1];
537 vertices[1].co[0] = rect[0]+rect[2];
538 vertices[1].co[1] = rect[1];
539 vertices[2].co[0] = rect[0]+rect[2];
540 vertices[2].co[1] = rect[1]+rect[3];
541 vertices[3].co[0] = rect[0];
542 vertices[3].co[1] = rect[1]+rect[3];
543 vertices[4].co[0] = vertices[0].co[0]-thickness;
544 vertices[4].co[1] = vertices[0].co[1]-thickness;
545 vertices[5].co[0] = vertices[1].co[0]+thickness;
546 vertices[5].co[1] = vertices[1].co[1]-thickness;
547 vertices[6].co[0] = vertices[2].co[0]+thickness;
548 vertices[6].co[1] = vertices[2].co[1]+thickness;
549 vertices[7].co[0] = vertices[3].co[0]-thickness;
550 vertices[7].co[1] = vertices[3].co[1]+thickness;
551
552 u16 start = vg_ui.cur_vert;
553 u32 mesh[] = { 0,5,4, 0,1,5, 1,6,5, 1,2,6, 2,7,6, 2,3,7, 3,4,7, 3,0,4 };
554
555 for( u32 i=0; i<vg_list_size(mesh); i++ ){
556 indices[i] = start+mesh[i];
557 }
558
559 vg_ui.cur_indice += 24;
560 vg_ui.cur_vert += 8;
561 }
562
563 static void ui_split_px( ui_rect rect,
564 enum ui_axis other, ui_px width, ui_px pad,
565 ui_rect l, ui_rect r )
566 {
567 enum ui_axis dir = other ^ 0x1;
568
569 ui_rect temp;
570 rect_copy( rect, temp );
571
572 l[ dir ] = temp[ dir ] + pad;
573 r[ dir ] = temp[ dir ] + width + (pad/2);
574 l[ other ] = temp[ other ] + pad;
575 r[ other ] = temp[ other ] + pad;
576 l[ 2+dir ] = width - ((3*pad)/2);
577 r[ 2+dir ] = temp[ 2+dir ] - width - ((3*pad)/2);
578 l[ 2+other ] = temp[ 2+other ] - pad*2;
579 r[ 2+other ] = temp[ 2+other ] - pad*2;
580 }
581
582 static void ui_rect_center( ui_rect parent, ui_rect rect )
583 {
584 rect[0] = parent[0] + (parent[2]-rect[2])/2;
585 rect[1] = parent[1] + (parent[3]-rect[3])/2;
586 }
587
588 static void ui_fit_item( ui_rect rect, ui_px size[2], ui_rect d )
589 {
590 i32 rp = (i32)rect[2] * (i32)size[1],
591 rc = (i32)size[0] * (i32)rect[3];
592
593 enum ui_axis dir, other;
594 if( rc > rp ) dir = k_ui_axis_h;
595 else dir = k_ui_axis_v;
596 other = dir ^ 0x1;
597
598 d[2+dir] = rect[2+dir];
599 d[2+other] = (rect[2+dir] * size[other]) / size[dir];
600
601 ui_rect_center( rect, d );
602 }
603
604 static void ui_split_ratio( ui_rect rect, enum ui_axis dir, float ratio,
605 ui_px pad, ui_rect l, ui_rect r )
606 {
607 ui_px width = (float)rect[ 2+(dir^0x1) ] * ratio;
608 ui_split_px( rect, dir, width, pad, l, r );
609 }
610
611 static void ui_rect_pad( ui_rect rect, ui_px pad )
612 {
613 rect[0] += pad;
614 rect[1] += pad;
615 rect[2] -= pad*2;
616 rect[3] -= pad*2;
617 }
618
619 static ui_px ui_text_line_width( const char *str )
620 {
621 int length = 0;
622 const char *_c = str;
623 u8 c;
624
625 while( (c = *(_c ++)) ){
626 if( c >= 32 ) length ++;
627 else if( c == '\n' ) break;
628 }
629
630 return length * 8;
631 }
632
633 static ui_px ui_text_string_height( const char *str )
634 {
635 int height = 1;
636 const char *_c = str;
637 u8 c;
638
639 while( (c = *(_c ++)) ){
640 if( c == '\n' ) height ++;
641 }
642
643 return height * 14;
644 }
645
646 static ui_px ui_text_aligned_x( const char *str, ui_rect rect, ui_px scale,
647 enum ui_align align )
648 {
649 if( align == k_ui_align_left ){
650 return rect[0];
651 }
652 else{
653 ui_px width = ui_text_line_width( str ) * scale;
654
655 if( align == k_ui_align_right )
656 return rect[0] + rect[2]-width;
657 else
658 return rect[0] + (rect[2]-width)/2;
659 }
660 }
661
662 static ui_px ui_min( ui_px a, ui_px b ){ return a<b?a:b; }
663 static ui_px ui_max( ui_px a, ui_px b ){ return a>b?a:b; }
664 static ui_px ui_clamp( ui_px a, ui_px min, ui_px max )
665 {
666 return ui_min( max, ui_max( a, min ) );
667 }
668
669 static int ui_clip( ui_rect parent, ui_rect child, ui_rect clipped )
670 {
671 ui_px parent_max[2], child_max[2];
672 parent_max[0] = parent[0]+parent[2];
673 parent_max[1] = parent[1]+parent[3];
674 child_max[0] = child[0]+child[2];
675 child_max[1] = child[1]+child[3];
676
677 clipped[0] = ui_clamp( child[0], parent[0], parent_max[0] );
678 clipped[1] = ui_clamp( child[1], parent[1], parent_max[1] );
679 clipped[2] = ui_clamp( child_max[0], parent[0], parent_max[0] );
680 clipped[3] = ui_clamp( child_max[1], parent[1], parent_max[1] );
681
682 if( clipped[0] == clipped[2] ||
683 clipped[1] == clipped[3] )
684 return 0;
685
686 clipped[2] -= clipped[0];
687 clipped[3] -= clipped[1];
688
689 return 1;
690 }
691
692 static int ui_inside_rect( ui_rect rect, ui_px co[2] )
693 {
694 if( co[0] >= rect[0] &&
695 co[1] >= rect[1] &&
696 co[0] <= rect[0]+rect[2] &&
697 co[1] <= rect[1]+rect[3] )
698 {
699 return 1;
700 }
701 else
702 return 0;
703 }
704
705 static int ui_click_down( u32 mask )
706 {
707 if( vg_ui.ignore_input_frames ) return 0;
708 if( (vg_ui.mouse_state[0] & mask) &&
709 !(vg_ui.mouse_state[1] & mask) )
710 return 1;
711 else
712 return 0;
713 }
714
715 static int ui_clicking( u32 mask )
716 {
717 if( vg_ui.ignore_input_frames ) return 0;
718 return vg_ui.mouse_state[0] & mask;
719 }
720
721 static int ui_click_up( u32 mask )
722 {
723 if( vg_ui.ignore_input_frames ) return 0;
724 if( (vg_ui.mouse_state[1] & mask) &&
725 !(vg_ui.mouse_state[0] & mask) )
726 return 1;
727 else
728 return 0;
729 }
730
731 static void ui_prerender(void)
732 {
733 int x, y;
734 vg_ui.mouse_state[1] = vg_ui.mouse_state[0];
735 vg_ui.mouse_state[0] = SDL_GetMouseState( &x, &y );
736 vg_ui.mouse[0] = x;
737 vg_ui.mouse[1] = y;
738
739 vg_ui.cur_vert = 0;
740 vg_ui.cur_indice = 0;
741 vg_ui.vert_start = 0;
742 vg_ui.indice_start = 0;
743 vg_ui.focused_control_hit = 0;
744 vg_ui.cursor = k_ui_cursor_default;
745 vg_ui.wants_mouse = 0;
746
747 if( vg_ui.ignore_input_frames ){
748 vg_ui.ignore_input_frames --;
749 return;
750 }
751
752 if( ui_click_down(UI_MOUSE_LEFT)||ui_click_down(UI_MOUSE_MIDDLE) ){
753 vg_ui.mouse_click[0] = vg_ui.mouse[0];
754 vg_ui.mouse_click[1] = vg_ui.mouse[1];
755 }
756 }
757
758 static u32 ui_colour( enum ui_scheme_colour id )
759 {
760 return vg_ui.scheme[ id ];
761 }
762
763 /* get an appropriately contrasting colour given the base */
764 static u32 ui_colourcont( enum ui_scheme_colour id )
765 {
766 if ( id < k_ui_bg+6 ) return ui_colour( k_ui_fg );
767 else if( id < k_ui_fg ) return ui_colour( k_ui_bg+1 );
768 else if( id < k_ui_hue ) return ui_colour( k_ui_bg+3 );
769 else if( id < k_ui_red+k_ui_brighter ) return ui_colour( k_ui_fg );
770 else return ui_colour( k_ui_fg+1 );
771 }
772
773 static u32 ui_ntext( ui_rect rect, const char *str, u32 len, ui_px scale,
774 enum ui_align align, u32 colour )
775 {
776 ui_rect text_cursor;
777 if( colour == 0 ) colour = ui_colour( k_ui_fg );
778
779 colour &= 0x00ffffff;
780
781 const char *_c = str;
782 u8 c;
783
784 text_cursor[0] = ui_text_aligned_x( str, rect, scale, align );
785 text_cursor[1] = rect[1];
786 text_cursor[2] = 8*scale;
787 text_cursor[3] = 14*scale;
788
789 u32 printed_chars = 0;
790
791 if( align & (k_ui_align_middle|k_ui_align_bottom) ){
792 ui_px height = ui_text_string_height( str ) * scale;
793
794 if( align & k_ui_align_bottom )
795 text_cursor[1] += rect[3]-height;
796 else
797 text_cursor[1] += (rect[3]-height)/2;
798 }
799
800 while( (c = *(_c ++)) ){
801 if( printed_chars >= len ){
802 printed_chars = 0;
803 text_cursor[1] += 14*scale;
804 text_cursor[0] = ui_text_aligned_x( _c, rect, scale, align );
805 text_cursor[0] -= UI_GLYPH_SPACING_X*scale;
806
807 u8 glyph_base[2];
808 u8 glyph_index = '\x90';
809 glyph_base[0] = glyph_index & 0xf;
810 glyph_base[1] = (glyph_index-glyph_base[0])>>4;
811 glyph_base[0] *= 8;
812 glyph_base[1] *= 8;
813
814 ui_fill_rect( text_cursor, 0x00ffffff, (ui_px[4])
815 {
816 glyph_base[0]+2,
817 glyph_base[1]+1,
818 glyph_base[0]+6,
819 glyph_base[1]+8
820 });
821
822 text_cursor[0] += UI_GLYPH_SPACING_X*scale;
823 }
824
825 if( c == '\n' ){
826 text_cursor[1] += 14*scale;
827 text_cursor[0] = ui_text_aligned_x( _c, rect, scale, align );
828 printed_chars = 0;
829 continue;
830 }
831 else if( c >= 33 ){
832 u8 glyph_base[2];
833 u8 glyph_index = c;
834 glyph_base[0] = glyph_index & 0xf;
835 glyph_base[1] = (glyph_index-glyph_base[0])>>4;
836 glyph_base[0] *= 8;
837 glyph_base[1] *= 8;
838
839 ui_rect rect_char;
840
841 if( ui_clip( rect, text_cursor, rect_char ) ){
842 ui_fill_rect( rect_char, colour, (ui_px[4])
843 {
844 glyph_base[0]+2,
845 glyph_base[1]+1,
846 glyph_base[0]+6,
847 glyph_base[1]+8
848 });
849 }
850 }
851 else if( c == '\x1B' ){
852 /* vt codes */
853 _c ++;
854 u16 colour_id = 0;
855 for( int i=0; i<3; i ++ ){
856 if( _c[i] ){
857 if( _c[i] == 'm' ){
858 _c = _c + i + 1;
859
860 switch( colour_id ){
861 case '0': colour = ui_colour( k_ui_fg ); break;
862 case '3'|'0'<<8: colour = ui_colour( k_ui_bg ); break;
863 case '3'|'1'<<8: colour = ui_colour( k_ui_red ); break;
864 case '3'|'2'<<8: colour = ui_colour( k_ui_green ); break;
865 case '3'|'3'<<8: colour = ui_colour( k_ui_yellow ); break;
866 case '3'|'4'<<8: colour = ui_colour( k_ui_blue ); break;
867 case '3'|'5'<<8: colour = ui_colour( k_ui_purple ); break;
868 case '3'|'6'<<8: colour = ui_colour( k_ui_aqua ); break;
869 case '3'|'7'<<8: colour = 0xffffffff; break;
870 }
871
872 colour &= 0x00ffffff;
873 break;
874 }
875
876 colour_id |= _c[i] << (i*8);
877 }
878 else{
879 _c = _c +i;
880 break;
881 }
882 }
883
884 continue;
885 }
886 else if( c == '\t' ){
887 text_cursor[0] += UI_GLYPH_SPACING_X*scale*4;
888 printed_chars += 4;
889 continue;
890 }
891
892 text_cursor[0] += UI_GLYPH_SPACING_X*scale;
893 printed_chars ++;
894 }
895
896 return printed_chars;
897 }
898
899 static void ui_text( ui_rect rect, const char *str, ui_px scale,
900 enum ui_align align, u32 colour )
901 {
902 ui_ntext( rect, str, 1024, scale, align, colour );
903 }
904
905 static void ui_image( ui_rect rect, GLuint image )
906 {
907 ui_flush( k_ui_shader_colour );
908
909 glActiveTexture( GL_TEXTURE0 );
910 glBindTexture( GL_TEXTURE_2D, image );
911 ui_fill_rect( rect, 0xffffffff, (ui_px[4]){ 0,128, 128, 0 } );
912 ui_flush( k_ui_shader_image );
913 }
914
915 static u32 v4f_u32_colour( v4f colour )
916 {
917 u32 r = colour[0] * 255.0f,
918 g = colour[1] * 255.0f,
919 b = colour[2] * 255.0f,
920 a = colour[3] * 255.0f;
921
922 return r | (g<<8) | (b<<16) | (a<<24);
923 }
924
925 static void ui_defocus_all(void)
926 {
927 if( vg_ui.focused_control_type == k_ui_control_textbox ){
928 SDL_StopTextInput();
929 }
930
931 vg_ui.focused_control_id = NULL;
932 vg_ui.focused_control_hit = 0;
933 vg_ui.focused_control_type = k_ui_control_none;
934 }
935
936 static int ui_colourbutton( ui_rect rect, enum ui_scheme_colour colour )
937 {
938 int clickup= ui_click_up(UI_MOUSE_LEFT),
939 click = ui_clicking(UI_MOUSE_LEFT) | clickup,
940 target = ui_inside_rect( rect, vg_ui.mouse_click ) && click,
941 hover = ui_inside_rect( rect, vg_ui.mouse );
942
943 u32 col_base = vg_ui.scheme[ colour ],
944 col_highlight = vg_ui.scheme[ k_ui_fg ],
945 col_hover = vg_ui.scheme[ colour + k_ui_brighter ];
946
947 if( hover ){
948 vg_ui.cursor = k_ui_cursor_hand;
949 }
950
951 if( click ){
952 if( target ){
953 if( hover ){
954 if( clickup ){
955 ui_fill( rect, col_highlight );
956 vg_ui.ignore_input_frames = 2;
957 rect_copy( rect, vg_ui.click_fader );
958
959 rect_copy( rect, vg_ui.click_fader_end );
960 vg_ui.click_fader_end[3] = 0;
961 ui_rect_center( rect, vg_ui.click_fader_end );
962
963 vg_ui.click_fade_opacity = 1.0f;
964 ui_defocus_all();
965 return 1;
966 }
967 else{
968 ui_fill( rect, col_highlight );
969 return 0;
970 }
971 }
972 else{
973 ui_fill( rect, col_base );
974 ui_outline( rect, 1, col_highlight );
975 return 0;
976 }
977 }
978 else{
979 ui_fill( rect, col_base );
980 return 0;
981 }
982 }
983 else{
984 if( hover ){
985 ui_fill( rect, col_hover );
986 return 0;
987 }
988 else{
989 ui_fill( rect, col_base );
990 return 0;
991 }
992 }
993 }
994
995 static int ui_colourbutton_text( ui_rect rect, const char *string, ui_px scale,
996 enum ui_scheme_colour colour )
997 {
998 int result = ui_colourbutton( rect, colour );
999 ui_rect t = { 0,0, ui_text_line_width( string )*scale, 14*scale };
1000 ui_rect_center( rect, t );
1001 ui_text( t, string, scale, k_ui_align_left, ui_colourcont(colour) );
1002 return result;
1003 }
1004
1005 static int ui_button_text( ui_rect rect, const char *string, ui_px scale )
1006 {
1007 return ui_colourbutton_text( rect, string, scale, k_ui_bg+4 );
1008 }
1009
1010 static void ui_postrender(void)
1011 {
1012 if( vg_ui.click_fade_opacity > 0.0f ){
1013
1014 float scale = vg_ui.click_fade_opacity;
1015 scale = vg_maxf( 1.0f/255.0f, scale*scale );
1016
1017 vg_ui.click_fade_opacity -= vg.time_frame_delta * 3.8f;
1018 u32 colour = (0x00ffffff & ui_colour(k_ui_fg))|0x7f000000;
1019
1020 v4f begin, end, dest;
1021 for( int i=0; i<4; i++ ){
1022 begin[i] = vg_ui.click_fader[i];
1023 end[i] = vg_ui.click_fader_end[i]+1;
1024 }
1025
1026 v4_lerp( end, begin, scale, dest );
1027
1028 ui_rect rect;
1029 for( int i=0; i<4; i++ ){
1030 rect[i] = dest[i];
1031 }
1032
1033 ui_fill( rect, colour );
1034 }
1035 ui_flush( k_ui_shader_colour );
1036
1037 if( !vg_ui.focused_control_hit ){
1038 ui_defocus_all();
1039 }
1040
1041 if( vg_ui.wants_mouse ){
1042 SDL_SetWindowGrab( vg.window, SDL_FALSE );
1043 SDL_SetRelativeMouseMode( SDL_FALSE );
1044 }
1045 else{
1046 SDL_SetWindowGrab( vg.window, SDL_TRUE );
1047 SDL_SetRelativeMouseMode( SDL_TRUE );
1048 }
1049
1050 SDL_SetCursor( vg_ui.cursor_map[ vg_ui.cursor ] );
1051 SDL_ShowCursor(1);
1052 }
1053
1054 static void ui_dev_colourview(void)
1055 {
1056 ui_rect window = {vg.window_x-256,0,256,vg.window_y}, swatch;
1057
1058 const char *names[vg_list_size(vg_ui.scheme)] = {
1059 [k_ui_bg] = "k_ui_bg", "k_ui_bg+1", "k_ui_bg+2", "k_ui_bg+3",
1060 "k_ui_bg+4", "k_ui_bg+5", "k_ui_bg+6", "k_ui_bg+7",
1061
1062 [k_ui_fg] = "k_ui_fg", "k_ui_fg+1", "k_ui_fg+2", "k_ui_fg+3",
1063 "k_ui_fg+4", "k_ui_fg+5", "k_ui_fg+6", "k_ui_fg+7",
1064
1065 [k_ui_red] = "k_ui_red", "k_ui_orange", "k_ui_yellow", "k_ui_green",
1066 "k_ui_aqua", "k_ui_blue", "k_ui_purple", "k_ui_gray",
1067 "k_ui_red+8","k_ui_orange+8","k_ui_yellow+8","k_ui_green+8",
1068 "k_ui_aqua+8","k_ui_blue+8","k_ui_purple+8","k_ui_gray+8" };
1069
1070 ui_rect col[2];
1071 ui_split_ratio( window, k_ui_axis_v, 0.5f, 0.0f, col[0], col[1] );
1072
1073 for( int i=0; i<vg_list_size(vg_ui.scheme); i++ ){
1074 int which = (i/8)%2;
1075
1076 ui_split_px( col[which], k_ui_axis_h, 24, 0, swatch, col[which] );
1077 ui_fill( swatch, ui_colour(i) );
1078
1079 if( names[i] )
1080 ui_text(swatch, names[i], 1, k_ui_align_middle_left, ui_colourcont(i));
1081 }
1082 }
1083
1084 /*
1085 * checkbox
1086 * -----------------------------------------------------------------------------
1087 */
1088
1089 static int ui_checkbox( ui_rect rect, const char *str_label, int *data )
1090 {
1091 ui_rect label, box;
1092 ui_split_px( rect, k_ui_axis_v, rect[2]-rect[3], 0, label, box );
1093 ui_text( label, str_label, 1, k_ui_align_middle_left, 0 );
1094
1095 int changed = ui_colourbutton( box, k_ui_bg );
1096 if( changed )
1097 *data = (*data) ^ 0x1;
1098
1099 if( *data ){
1100 ui_rect_pad( box, 4 );
1101 ui_fill( box, ui_colour( k_ui_orange ) );
1102 }
1103
1104 return changed;
1105 }
1106
1107 /*
1108 * Textbox chaos
1109 * -----------------------------------------------------------------------------
1110 */
1111
1112 static void _ui_textbox_make_selection( int *start, int *end )
1113 {
1114 *start = VG_MIN( vg_ui.textbox.cursor_pos, vg_ui.textbox.cursor_user );
1115 *end = VG_MAX( vg_ui.textbox.cursor_pos, vg_ui.textbox.cursor_user );
1116 }
1117
1118 static void _ui_textbox_move_cursor( int *cursor0, int *cursor1,
1119 int dir, int snap_together )
1120 {
1121 *cursor0 = VG_MAX( 0, vg_ui.textbox.cursor_user + dir );
1122 *cursor0 =
1123 VG_MIN(
1124 VG_MIN( vg_ui.textbox.len-1, strlen( vg_ui.textbuf )),
1125 *cursor0 );
1126
1127 if( snap_together )
1128 *cursor1 = *cursor0;
1129 }
1130
1131 static int _ui_textbox_makeroom( int datastart, int length )
1132 {
1133 int move_to = VG_MIN( datastart+length, vg_ui.textbox.len-1 );
1134 int move_amount = strlen( vg_ui.textbuf )-datastart;
1135 int move_end = VG_MIN( move_to+move_amount, vg_ui.textbox.len-1 );
1136 move_amount = move_end-move_to;
1137
1138 if( move_amount )
1139 memmove( &vg_ui.textbuf[ move_to ],
1140 &vg_ui.textbuf[ datastart ],
1141 move_end-move_to );
1142
1143 vg_ui.textbuf[ move_end ] = '\0';
1144
1145 return VG_MIN( length, vg_ui.textbox.len-datastart-1 );
1146 }
1147
1148 static int _ui_textbox_delete_char( int direction )
1149 {
1150 int start, end;
1151 _ui_textbox_make_selection( &start, &end );
1152
1153 /* There is no selection */
1154 if( !(end-start) ){
1155 if( direction == 1 ) end = VG_MIN( end+1, strlen( vg_ui.textbuf ) );
1156 else if( direction == -1 ) start = VG_MAX( start-1, 0 );
1157 }
1158
1159 /* Still no selction, no need to do anything */
1160 if( !(end-start) )
1161 return start;
1162
1163 /* Copy the end->terminator to start */
1164 int remaining_length = strlen( vg_ui.textbuf )+1-end;
1165 memmove( &vg_ui.textbuf[ start ],
1166 &vg_ui.textbuf[ end ],
1167 remaining_length );
1168 return start;
1169 }
1170
1171 static void _ui_textbox_to_clipboard(void)
1172 {
1173 int start, end;
1174 _ui_textbox_make_selection( &start, &end );
1175 char buffer[512];
1176
1177 if( end-start ){
1178 memcpy( buffer, &vg_ui.textbuf[ start ], end-start );
1179 buffer[ end-start ] = 0x00;
1180 SDL_SetClipboardText( buffer );
1181 }
1182 }
1183
1184 static void _ui_textbox_clipboard_paste(void)
1185 {
1186 if( !SDL_HasClipboardText() )
1187 return;
1188
1189 char *text = SDL_GetClipboardText();
1190
1191 if( !text )
1192 return;
1193
1194 int datastart = _ui_textbox_delete_char( 0 );
1195 int length = strlen( text );
1196 int cpylength = _ui_textbox_makeroom( datastart, length );
1197
1198 memcpy( vg_ui.textbuf + datastart, text, cpylength);
1199 _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user,
1200 &vg_ui.textbox.cursor_pos, cpylength, 1 );
1201 SDL_free( text );
1202 }
1203
1204 static void _ui_textbox_put_char( char c )
1205 {
1206 vg_ui.textbox.cursor_user = _ui_textbox_delete_char(0);
1207
1208 if( _ui_textbox_makeroom( vg_ui.textbox.cursor_user, 1 ) )
1209 vg_ui.textbuf[ vg_ui.textbox.cursor_user ] = c;
1210
1211 _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user,
1212 &vg_ui.textbox.cursor_pos, 1, 1 );
1213 }
1214
1215 /* Receed secondary cursor */
1216 static void _ui_textbox_left_select(void)
1217 {
1218 _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user, NULL, -1, 0 );
1219 }
1220
1221 /* Match and receed both cursors */
1222 static void _ui_textbox_left(void)
1223 {
1224 int cursor_diff = vg_ui.textbox.cursor_pos - vg_ui.textbox.cursor_user? 0: 1;
1225
1226 _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user,
1227 &vg_ui.textbox.cursor_pos, -cursor_diff, 1 );
1228 }
1229
1230 static void _ui_textbox_up(void)
1231 {
1232 if( vg_ui.textbox.flags & UI_TEXTBOX_MULTILINE ){
1233 int line_begin = vg_ui.textbox.cursor_user;
1234
1235 while( line_begin ){
1236 if( vg_ui.textbuf[ line_begin-1 ] == '\n' ){
1237 break;
1238 }
1239
1240 line_begin --;
1241 }
1242
1243 if( line_begin ){
1244 int line_above_begin = line_begin-1;
1245
1246 while( line_above_begin ){
1247 if( vg_ui.textbuf[ line_above_begin-1 ] == '\n' ){
1248 break;
1249 }
1250
1251 line_above_begin --;
1252 }
1253
1254 int offset = vg_ui.textbox.cursor_user - line_begin,
1255 line_length_above = line_begin - line_above_begin -1;
1256
1257 offset = VG_MIN( line_length_above, offset );
1258
1259 vg_ui.textbox.cursor_user = line_above_begin+offset;
1260 vg_ui.textbox.cursor_pos = line_above_begin+offset;
1261 }
1262 else{
1263 vg_ui.textbox.cursor_user = line_begin;
1264 vg_ui.textbox.cursor_pos = line_begin;
1265 }
1266 }
1267 else{
1268 if( vg_ui.textbuf_on_up ){
1269 vg_ui.textbuf_on_up( vg_ui.textbuf, vg_ui.textbox.len );
1270 }
1271 }
1272 }
1273
1274 static void _ui_textbox_down(void)
1275 {
1276 if( vg_ui.textbox.flags & UI_TEXTBOX_MULTILINE ){
1277 int line_begin = vg_ui.textbox.cursor_user;
1278
1279 while( line_begin ){
1280 if( vg_ui.textbuf[ line_begin-1 ] == '\n' ){
1281 break;
1282 }
1283
1284 line_begin --;
1285 }
1286
1287 int line_below_begin = vg_ui.textbox.cursor_user;
1288
1289 while(1){
1290 if( vg_ui.textbuf[ line_below_begin ] == '\0' ){
1291 vg_ui.textbox.cursor_user = line_below_begin;
1292 vg_ui.textbox.cursor_pos = line_below_begin;
1293 return;
1294 }
1295
1296 if( vg_ui.textbuf[ line_below_begin ] == '\n' ){
1297 line_below_begin ++;
1298 break;
1299 }
1300
1301 line_below_begin ++;
1302 }
1303
1304 int line_below_end = line_below_begin;
1305 while(1){
1306 if( vg_ui.textbuf[ line_below_end ] == '\0' ||
1307 vg_ui.textbuf[ line_below_end ] == '\n' ){
1308 line_below_end ++;
1309 break;
1310 }
1311 line_below_end ++;
1312 }
1313
1314 int offset = vg_ui.textbox.cursor_user - line_begin,
1315 line_length_below = line_below_end - line_below_begin -1;
1316
1317 offset = VG_MIN( line_length_below, offset );
1318
1319 vg_ui.textbox.cursor_user = line_below_begin+offset;
1320 vg_ui.textbox.cursor_pos = line_below_begin+offset;
1321 }
1322 else{
1323 if( vg_ui.textbuf_on_down ){
1324 vg_ui.textbuf_on_down( vg_ui.textbuf, vg_ui.textbox.len );
1325 }
1326 }
1327 }
1328
1329 static void _ui_textbox_right_select(void)
1330 {
1331 _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user, NULL, 1, 0 );
1332 }
1333
1334 static void _ui_textbox_right(void)
1335 {
1336 int cursor_diff = vg_ui.textbox.cursor_pos - vg_ui.textbox.cursor_user? 0: 1;
1337
1338 _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user,
1339 &vg_ui.textbox.cursor_pos, +cursor_diff, 1 );
1340 }
1341
1342 static void _ui_textbox_backspace(void)
1343 {
1344 if( vg_ui.focused_control_type == k_ui_control_textbox ){
1345 vg_ui.textbox.cursor_user = _ui_textbox_delete_char( -1 );
1346 vg_ui.textbox.cursor_pos = vg_ui.textbox.cursor_user;
1347
1348 if( vg_ui.textbuf_on_change )
1349 vg_ui.textbuf_on_change( vg_ui.textbuf, vg_ui.textbox.len );
1350 }
1351 }
1352
1353 static void _ui_textbox_delete(void)
1354 {
1355 if( vg_ui.focused_control_type == k_ui_control_textbox ){
1356 vg_ui.textbox.cursor_user = _ui_textbox_delete_char( 1 );
1357 vg_ui.textbox.cursor_pos = vg_ui.textbox.cursor_user;
1358
1359 if( vg_ui.textbuf_on_change )
1360 vg_ui.textbuf_on_change( vg_ui.textbuf, vg_ui.textbox.len );
1361 }
1362 }
1363
1364 static void _ui_textbox_home_select(void)
1365 {
1366 _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user, NULL, -10000, 0 );
1367 }
1368
1369 static void _ui_textbox_home(void)
1370 {
1371 _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user,
1372 &vg_ui.textbox.cursor_pos, -10000, 1 );
1373 }
1374
1375 static void _ui_textbox_end_select(void)
1376 {
1377 _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user, NULL, 10000, 0 );
1378 }
1379
1380 static void _ui_textbox_end(void)
1381 {
1382 _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user,
1383 &vg_ui.textbox.cursor_pos,
1384 vg_ui.textbox.len-1, 1 );
1385 }
1386
1387 static void _ui_textbox_select_all(void)
1388 {
1389 _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user, NULL, 10000, 0);
1390 _ui_textbox_move_cursor( &vg_ui.textbox.cursor_pos, NULL, -10000, 0);
1391 }
1392
1393 static void _ui_textbox_cut(void)
1394 {
1395 _ui_textbox_to_clipboard();
1396 vg_ui.textbox.cursor_user = _ui_textbox_delete_char(0);
1397 vg_ui.textbox.cursor_pos = vg_ui.textbox.cursor_user;
1398 }
1399
1400 static void _ui_textbox_enter(void)
1401 {
1402 if( vg_ui.focused_control_type == k_ui_control_textbox ){
1403 if( vg_ui.textbox.flags & UI_TEXTBOX_MULTILINE ){
1404 _ui_textbox_put_char( '\n' );
1405 }
1406 else{
1407 if( !(vg_ui.textbox.flags & UI_TEXTBOX_AUTOFOCUS ) )
1408 ui_defocus_all();
1409 }
1410
1411 if( vg_ui.textbuf_on_enter ){
1412 vg_ui.textbuf_on_enter( vg_ui.textbuf, vg_ui.textbox.len );
1413 }
1414 }
1415 }
1416
1417 /*
1418 * based on a visual character coordinate relative to the anchor of the textbox,
1419 * this works out the linear place in the buffer that coordinate maps to
1420 *
1421 * input coordinates go in co[0], co[1], and the result index is in co[2]
1422 */
1423 static void _ui_textbox_calc_index_from_grid( int co[3], int wrap_length )
1424 {
1425 int i[3] = {0,0,0};
1426
1427 char c;
1428 while( (c = vg_ui.textbuf[i[2]]) ){
1429 if( i[1]==co[1] && i[0]>=co[0] ) break;
1430
1431 if( i[0] >= wrap_length ){
1432 i[1] ++;
1433 i[0] = 0;
1434 }
1435
1436 if( c >= 32 && c <= 126 ){
1437 i[0] ++;
1438 i[2] ++;
1439 }
1440 else if( c == '\n' ){
1441 i[1] ++;
1442
1443 if( i[1] > co[1] ) break;
1444
1445 i[2] ++;
1446 i[0] = 0;
1447 }
1448 else i[2] ++;
1449 }
1450
1451 co[0] = i[0];
1452 co[1] = i[1];
1453 co[2] = i[2];
1454 }
1455
1456 /*
1457 * based on the index specied in co[2], work out the visual character
1458 * coordinates and store them in co[0], co[1]
1459 */
1460 static void _ui_textbox_index_calc_coords( int co[3], int wrap_length )
1461 {
1462 co[0] = 0;
1463 co[1] = 0;
1464
1465 char c;
1466 int i=0;
1467
1468 while( (c = vg_ui.textbuf[i ++]) ){
1469 if( i > co[2] ) break;
1470 if( co[0] >= wrap_length ){
1471 co[1] ++;
1472 co[0] = 0;
1473 }
1474 if( c >= 32 && c <= 126 ) co[0] ++;
1475 else if( c == '\n' ){
1476 co[1] ++;
1477 co[0] = 0;
1478 }
1479 }
1480 }
1481
1482 /*
1483 * calculate the number of characters remaining until either:
1484 * - the wrap_length limit is hit
1485 * - end of the line/string
1486 *
1487 * index must be fully populated with visual X/Y, and linear index
1488 */
1489 static int _ui_textbox_run_remaining( int index[3], int wrap_length )
1490 {
1491 int i=0, printed_chars=0;
1492 char c;
1493 while( (c = vg_ui.textbuf[index[2] + (i ++)]) ){
1494 if( index[0]+i >= wrap_length ) break;
1495 if( c >= 32 && c <= 126 ) printed_chars ++;
1496 else if( c == '\n' ) break;
1497 }
1498
1499 return printed_chars+1;
1500 }
1501
1502 static int ui_textbox( ui_rect rect, char *buf, u32 len, u32 flags )
1503 {
1504 int clickup= ui_click_up(UI_MOUSE_LEFT),
1505 click = ui_clicking(UI_MOUSE_LEFT) | clickup,
1506 target = ui_inside_rect( rect, vg_ui.mouse_click ) && click,
1507 hover = ui_inside_rect( rect, vg_ui.mouse );
1508
1509 u32 col_base = ui_colour( k_ui_bg ),
1510 col_highlight = ui_colour( k_ui_fg ),
1511 col_cursor = (0x00ffffff & ui_colour(k_ui_fg))|0x7f000000;
1512
1513 ui_px border = -1;
1514
1515 ui_rect text_rect;
1516 rect_copy( rect, text_rect );
1517
1518 if( flags & UI_TEXTBOX_MULTILINE ) text_rect[3] = rect[3]-16;
1519 else text_rect[3] = 14;
1520
1521 text_rect[2] -= 16;
1522 ui_rect_center( rect, text_rect );
1523
1524 ui_px wrap_length = 1024;
1525
1526 if( flags & UI_TEXTBOX_WRAP )
1527 wrap_length = text_rect[2] / UI_GLYPH_SPACING_X;
1528
1529 if( hover ){
1530 vg_ui.cursor = k_ui_cursor_ibeam;
1531 }
1532
1533 if( vg_ui.focused_control_id == buf ){
1534 ui_fill( rect, col_base );
1535 ui_ntext( text_rect, buf, wrap_length, 1, k_ui_align_left, 0 );
1536
1537 if( !(flags & UI_TEXTBOX_AUTOFOCUS) &&
1538 ((clickup||ui_click_down(UI_MOUSE_LEFT)) && !target) ){
1539 ui_defocus_all();
1540 }
1541 else{
1542 vg_ui.focused_control_hit = 1;
1543 if( click && target ){
1544 int p0[3] ={
1545 (vg_ui.mouse_click[0] - text_rect[0]) / UI_GLYPH_SPACING_X,
1546 (vg_ui.mouse_click[1] - text_rect[1]) / 14,
1547 -1
1548 },
1549 p1[3] = {
1550 (vg_ui.mouse[0] - text_rect[0]) / UI_GLYPH_SPACING_X,
1551 (vg_ui.mouse[1] - text_rect[1]) / 14,
1552 -1
1553 };
1554
1555 if( flags & UI_TEXTBOX_MULTILINE ){
1556 _ui_textbox_calc_index_from_grid( p0, wrap_length );
1557 _ui_textbox_calc_index_from_grid( p1, wrap_length );
1558
1559 vg_ui.textbox.cursor_pos = p0[2];
1560 vg_ui.textbox.cursor_user = p1[2];
1561 }
1562 else{
1563 int max = strlen( buf );
1564 vg_ui.textbox.cursor_pos = VG_MAX( 0, VG_MIN( max, p0[0] )),
1565 vg_ui.textbox.cursor_user = VG_MAX( 0, VG_MIN( max, p1[0] ));
1566 }
1567 }
1568
1569 ui_outline( rect, -2, vg_ui.scheme[ k_ui_orange ] );
1570
1571 ui_rect cursor;
1572
1573 int c0 = vg_ui.textbox.cursor_pos,
1574 c1 = vg_ui.textbox.cursor_user,
1575 start = VG_MIN( c0, c1 ),
1576 end = VG_MAX( c0, c1 ),
1577 chars = end-start;
1578
1579 if( flags & (UI_TEXTBOX_MULTILINE|UI_TEXTBOX_WRAP) ){
1580
1581 int pos[3], remaining = chars;
1582
1583 pos[2] = start;
1584 _ui_textbox_index_calc_coords( pos, wrap_length );
1585
1586 if( start==end ){
1587 cursor[0] = text_rect[0] + pos[0]*UI_GLYPH_SPACING_X-1;
1588 cursor[1] = text_rect[1] + pos[1]*14;
1589 cursor[2] = 2;
1590 cursor[3] = 13;
1591 ui_fill( cursor, col_cursor );
1592 rect_copy( cursor, vg_ui.click_fader_end );
1593 }
1594 else{
1595 while( remaining ){
1596 /* TODO: scan for newlines and stuff
1597 * eol or wrap_length can have line breaks! */
1598
1599 int run = _ui_textbox_run_remaining( pos, wrap_length );
1600 run = VG_MIN( run, remaining );
1601
1602 cursor[0] = text_rect[0] + pos[0]*UI_GLYPH_SPACING_X-1;
1603 cursor[1] = text_rect[1] + pos[1]*14;
1604 cursor[2] = (float)(run)*(float)UI_GLYPH_SPACING_X;
1605 cursor[3] = 13;
1606
1607 ui_fill( cursor, col_cursor );
1608
1609 remaining -= run;
1610 pos[0] = 0;
1611 pos[1] ++;
1612 pos[2] += run;
1613 }
1614 rect_copy( cursor, vg_ui.click_fader_end );
1615 }
1616 }
1617 else{
1618 cursor[0] = text_rect[0] + start*UI_GLYPH_SPACING_X-1;
1619 cursor[1] = text_rect[1];
1620 cursor[3] = 13;
1621
1622 if( start==end ){
1623 cursor[2] = 2;
1624 }
1625 else{
1626 cursor[2] = (float)(chars)*(float)UI_GLYPH_SPACING_X;
1627 }
1628
1629 if( (vg_ui.click_fade_opacity<=0.0f) &&
1630 ui_clip( rect, cursor, cursor ) ){
1631 ui_fill( cursor, col_cursor );
1632 }
1633
1634 rect_copy( cursor, vg_ui.click_fader_end );
1635 }
1636 }
1637
1638 return 0;
1639 }
1640
1641 if( click || (flags & UI_TEXTBOX_AUTOFOCUS) ){
1642 if( (target && hover) || (flags & UI_TEXTBOX_AUTOFOCUS) ){
1643 ui_defocus_all();
1644
1645 ui_fill( rect, col_highlight );
1646 vg_ui.ignore_input_frames = 2;
1647 rect_copy( rect, vg_ui.click_fader );
1648 rect_copy( rect, vg_ui.click_fader_end );
1649
1650 vg_ui.click_fade_opacity = 1.0f;
1651 vg_ui.textbuf = buf;
1652 vg_ui.focused_control_hit = 1;
1653 vg_ui.focused_control_type = k_ui_control_textbox;
1654 vg_ui.textbox.len = len;
1655 vg_ui.textbox.flags = flags;
1656 vg_ui.textbox.cursor_pos = 0;
1657 vg_ui.textbox.cursor_user = 0;
1658
1659 SDL_StartTextInput();
1660 }
1661 }
1662
1663 ui_fill( rect, col_base );
1664
1665 if( hover ){
1666 ui_outline( rect, -1, col_highlight );
1667 }
1668
1669 ui_ntext( text_rect, buf, wrap_length, 1, k_ui_align_left, 0 );
1670 return 0;
1671 }
1672
1673 /*
1674 * Input handling
1675 * -----------------------------------------------------------------------------
1676 */
1677
1678 /*
1679 * Handles binds
1680 */
1681 static void _ui_proc_key( SDL_Keysym ev )
1682 {
1683 if( vg_ui.focused_control_type != k_ui_control_textbox ){
1684 return;
1685 }
1686
1687 struct textbox_mapping
1688 {
1689 u16 mod;
1690 SDL_Keycode key;
1691
1692 void (*handler)(void);
1693 }
1694 mappings[] =
1695 {
1696 { 0, SDLK_LEFT, _ui_textbox_left },
1697 { KMOD_SHIFT, SDLK_LEFT, _ui_textbox_left_select },
1698 { 0, SDLK_RIGHT, _ui_textbox_right },
1699 { KMOD_SHIFT, SDLK_RIGHT, _ui_textbox_right_select },
1700 { 0, SDLK_DOWN, _ui_textbox_down },
1701 { 0, SDLK_UP, _ui_textbox_up },
1702 { 0, SDLK_BACKSPACE, _ui_textbox_backspace },
1703 { KMOD_SHIFT, SDLK_BACKSPACE, _ui_textbox_backspace },
1704 { KMOD_CTRL, SDLK_BACKSPACE, _ui_textbox_backspace },
1705 { 0, SDLK_DELETE, _ui_textbox_delete },
1706 { 0, SDLK_HOME, _ui_textbox_home },
1707 { KMOD_SHIFT, SDLK_HOME, _ui_textbox_home_select },
1708 { 0, SDLK_END, _ui_textbox_end },
1709 { KMOD_SHIFT, SDLK_END, _ui_textbox_end_select },
1710 { KMOD_CTRL, SDLK_a, _ui_textbox_select_all },
1711 { KMOD_CTRL, SDLK_c, _ui_textbox_to_clipboard },
1712 { KMOD_CTRL, SDLK_x, _ui_textbox_cut },
1713 { KMOD_CTRL, SDLK_v, _ui_textbox_clipboard_paste },
1714 { 0, SDLK_RETURN, _ui_textbox_enter },
1715 { 0, SDLK_ESCAPE, ui_defocus_all },
1716 };
1717
1718 SDL_Keymod mod = 0;
1719
1720 if( ev.mod & KMOD_SHIFT )
1721 mod |= KMOD_SHIFT;
1722
1723 if( ev.mod & KMOD_CTRL )
1724 mod |= KMOD_CTRL;
1725
1726 if( ev.mod & KMOD_ALT )
1727 mod |= KMOD_ALT;
1728
1729 for( int i=0; i<vg_list_size( mappings ); i++ ){
1730 struct textbox_mapping *mapping = &mappings[i];
1731
1732 if( mapping->key == ev.sym ){
1733 if( mapping->mod == 0 ){
1734 if( mod == 0 ){
1735 mapping->handler();
1736 return;
1737 }
1738 }
1739 else if( (mod & mapping->mod) == mapping->mod ){
1740 mapping->handler();
1741 return;
1742 }
1743 }
1744 }
1745 }
1746
1747 /*
1748 * Callback for text entry mode
1749 */
1750 VG_STATIC void ui_proc_utf8( const char *text )
1751 {
1752 if( vg_ui.focused_control_type == k_ui_control_textbox ){
1753 const char *ptr = text;
1754
1755 while( *ptr ){
1756 if( *ptr != '`' ) _ui_textbox_put_char( *ptr );
1757 ptr ++;
1758 }
1759
1760 if( vg_ui.textbuf_on_change ){
1761 vg_ui.textbuf_on_change( vg_ui.textbuf, vg_ui.textbox.len );
1762 }
1763 }
1764 }
1765
1766 #endif /* VG_IMGUI_H */