4604327dd25197767b935ac0c6bf1485c7fd3887
[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 * 4. controls directly reference a memory location and use that as their
10 * unique id
11 * 5. a maximum of ONE control per memory location can be drawn at any given
12 * point.
13 */
14
15 #ifndef VG_IMGUI_H
16 #define VG_IMGUI_H
17
18 #define VG_GAME
19 #include "vg/vg.h"
20 #include "vg/vg_tex.h"
21 #include "vg/vg_shader.h"
22
23 typedef i16 ui_px;
24 typedef ui_px ui_rect[4];
25 typedef ui_px ui_point[2];
26 typedef struct ui_vert ui_vert;
27
28 enum ui_axis {
29 k_ui_axis_h = 0x0u,
30 k_ui_axis_v = 0x1u,
31 };
32
33 /* Relative to cursor p0 */
34 enum ui_align
35 { /* DC BA */
36 k_ui_align_lwr = 0xff,
37 k_ui_align_left = 0x0000| 0x00,
38 k_ui_align_right = 0x0000| 0x01,
39 k_ui_align_center = 0x0000| 0x02,
40
41 k_ui_align_middle = 0x0100,
42 k_ui_align_middle_left = 0x0100| 0x00,
43 k_ui_align_middle_right = 0x0100| 0x01,
44 k_ui_align_middle_center = 0x0100| 0x02,
45
46 k_ui_align_bottom = 0x0200,
47 k_ui_align_bottom_left = 0x0200| 0x00,
48 k_ui_align_bottom_right = 0x0200| 0x01,
49 k_ui_align_bottom_center = 0x0200| 0x02,
50 };
51
52 #pragma pack(push,1)
53 struct ui_vert
54 {
55 ui_px co[2];
56 u16 uv[2];
57 u32 colour;
58 };
59 #pragma pack(pop)
60
61 enum ui_scheme_colour{
62 k_ui_bg = 0,
63 k_ui_fg = 8,
64 k_ui_hue = 16,
65 k_ui_red = 16,
66 k_ui_orange,
67 k_ui_yellow,
68 k_ui_green,
69 k_ui_aqua,
70 k_ui_blue,
71 k_ui_purple,
72 k_ui_gray,
73 k_ui_brighter = 8
74 };
75
76 static ui_px k_ui_widget_height = 28,
77 k_ui_scale = 1,
78 k_ui_padding = 8;
79
80 typedef u32 ui_scheme[8*4];
81
82 struct ui_font {
83 ui_px glyph_width,
84 glyph_height,
85 glyph_baseline,
86 line_height,
87 sheet_size,
88 spacing,
89 offset_y;
90
91 u8 ascii_start;
92 };
93 typedef struct ui_font ui_font;
94
95 static const ui_font vg_ui_font_small = {
96 .glyph_width = 8,
97 .glyph_height = 14,
98 .glyph_baseline = 4,
99 .line_height = 14,
100 .sheet_size = 256,
101 .spacing = 8,
102 .ascii_start = ' ',
103 .offset_y = 0
104 },
105 vg_ui_font_big = {
106 .glyph_width = 12,
107 .glyph_height = 21,
108 .glyph_baseline = 6,
109 .line_height = 21,
110 .sheet_size = 256,
111 .spacing = 10,
112 .ascii_start = ' ',
113 .offset_y = 84
114 };
115
116 #define UI_RGB( STDHEX ) 0xff000000 |\
117 ((STDHEX&0x000000ff)<<16) |\
118 ((STDHEX&0x0000ff00) ) |\
119 ((STDHEX&0x00ff0000)>>16)
120
121 #define UI_TEXTBOX_MULTILINE 0x1
122 #define UI_TEXTBOX_WRAP 0x2
123 #define UI_TEXTBOX_AUTOFOCUS 0x4
124
125 #define UI_MODAL_OK 0x0
126 #define UI_MODAL_GOOD 0x1
127 #define UI_MODAL_BAD 0x2
128 #define UI_MODAL_WARN 0x3
129 #define UI_MODAL_TYPE_BITS 0x3
130
131 #define UI_MOUSE_LEFT (SDL_BUTTON(SDL_BUTTON_LEFT))
132 #define UI_MOUSE_RIGHT (SDL_BUTTON(SDL_BUTTON_RIGHT))
133 #define UI_MOUSE_MIDDLE (SDL_BUTTON(SDL_BUTTON_MIDDLE))
134
135 #define UI_TOP 0x1
136 #define UI_LEFT 0x2
137 #define UI_BOTTOM 0x4
138 #define UI_RIGHT 0x8
139
140 struct{
141 struct ui_vert *vertex_buffer;
142 u16 *indice_buffer;
143 u32 max_verts, max_indices,
144 cur_vert, cur_indice,
145 vert_start, indice_start;
146
147 union {
148 void *focused_control_id; /* uses the memory location of various locking
149 controls as an id */
150 char *textbuf;
151 i32 *ptr_enum;
152 };
153
154 u32 focused_control_hit;
155 enum ui_control_type{
156 k_ui_control_none,
157 k_ui_control_textbox,
158 k_ui_control_enum,
159 k_ui_control_modal
160 }
161 focused_control_type;
162
163 union{ /* controls that can be focused */
164 struct ui_textbuf{
165 int cursor_user, cursor_pos;
166 u32 len;
167 u32 flags;
168
169 struct ui_textbox_callbacks{
170 void (*enter)( char *, u32 ),
171 (*up)( char *, u32 ),
172 (*down)( char *, u32 ),
173 (*change)( char *, u32 ),
174 (*escape)( void );
175 }
176 callbacks;
177 }
178 textbox;
179
180 struct ui_enum{
181 struct ui_enum_opt{
182 i32 value;
183 const char *alias;
184 }
185 *options;
186 u32 option_count;
187 ui_rect rect;
188 }
189 _enum;
190 };
191
192 struct ui_modal{
193 const char *message;
194 u32 options;
195
196 struct ui_modal_callbacks{
197 void (*close)(u32);
198 }
199 callbacks;
200 }
201 modal;
202
203 GLuint tex_glyphs, vao, vbo, ebo, tex_bg;
204 v2f bg_inverse_ratio;
205
206 ui_px mouse[2], mouse_delta[2], mouse_click[2];
207 u32 mouse_state[2];
208 u32 ignore_input_frames;
209 int wants_mouse;
210
211 ui_rect click_fader, click_fader_end;
212 float click_fade_opacity;
213 f32 frosting;
214
215 ui_scheme scheme;
216 const ui_font *font;
217
218 enum ui_cursor{
219 k_ui_cursor_default,
220 k_ui_cursor_ibeam,
221 k_ui_cursor_hand,
222 k_ui_cursor_max
223 }
224 cursor;
225
226 SDL_Cursor *cursor_map[ k_ui_cursor_max ];
227 v4f colour;
228
229 f32 hue; /* lol this sucks */
230 }
231 static vg_ui = {
232 .scheme = {
233 [ k_ui_bg+0 ] = UI_RGB( 0x1d2021 ),
234 [ k_ui_bg+1 ] = UI_RGB( 0x282828 ),
235 [ k_ui_bg+2 ] = UI_RGB( 0x3c3836 ),
236 [ k_ui_bg+3 ] = UI_RGB( 0x504945 ),
237 [ k_ui_bg+4 ] = UI_RGB( 0x665c54 ),
238 [ k_ui_bg+5 ] = UI_RGB( 0x7c6f64 ),
239 [ k_ui_bg+6 ] = UI_RGB( 0x928374 ),
240 [ k_ui_bg+7 ] = UI_RGB( 0xa89984 ),
241
242 [ k_ui_fg+0 ] = UI_RGB( 0xebdbb2 ),
243 [ k_ui_fg+1 ] = UI_RGB( 0xfbf1c7 ),
244 [ k_ui_fg+2 ] = UI_RGB( 0xd5c4a1 ),
245 [ k_ui_fg+3 ] = UI_RGB( 0xbdae93 ),
246 [ k_ui_fg+4 ] = UI_RGB( 0xa89984 ),
247 [ k_ui_fg+5 ] = UI_RGB( 0x000000 ),
248 [ k_ui_fg+6 ] = UI_RGB( 0x000000 ),
249 [ k_ui_fg+7 ] = UI_RGB( 0x000000 ),
250
251 [ k_ui_red ] = UI_RGB( 0xcc241d ),
252 [ k_ui_orange ] = UI_RGB( 0xd65d0e ),
253 [ k_ui_yellow ] = UI_RGB( 0xd79921 ),
254 [ k_ui_green ] = UI_RGB( 0x98971a ),
255 [ k_ui_aqua ] = UI_RGB( 0x689d6a ),
256 [ k_ui_blue ] = UI_RGB( 0x458588 ),
257 [ k_ui_purple ] = UI_RGB( 0xb16286 ),
258 [ k_ui_gray ] = UI_RGB( 0x928374 ),
259 [ k_ui_red + k_ui_brighter ] = UI_RGB( 0xfb4934 ),
260 [ k_ui_orange + k_ui_brighter ] = UI_RGB( 0xfe8019 ),
261 [ k_ui_yellow + k_ui_brighter ] = UI_RGB( 0xfabd2f ),
262 [ k_ui_green + k_ui_brighter ] = UI_RGB( 0xb8bb26 ),
263 [ k_ui_aqua + k_ui_brighter ] = UI_RGB( 0x8ec07c ),
264 [ k_ui_blue + k_ui_brighter ] = UI_RGB( 0x83a598 ),
265 [ k_ui_purple + k_ui_brighter ] = UI_RGB( 0xd3869b ),
266 [ k_ui_gray + k_ui_brighter ] = UI_RGB( 0xa89984 ),
267 },
268 .font = &vg_ui_font_small,
269 .colour = {1.0f,1.0f,1.0f,1.0f},
270 .bg_inverse_ratio = {1,1}
271 };
272
273 static struct vg_shader _shader_ui ={
274 .name = "[vg] ui - transparent",
275 .link = NULL,
276 .vs = {
277 .orig_file = NULL,
278 .static_src =
279 "layout (location=0) in vec2 a_co;"
280 "layout (location=1) in vec2 a_uv;"
281 "layout (location=2) in vec4 a_colour;"
282 "uniform mat3 uPv;"
283 "uniform vec2 uBGInverseRatio;"
284 ""
285 "out vec4 aTexCoords;"
286 "out vec4 aColour;"
287 ""
288 "void main(){"
289 "vec4 proj_pos = vec4( uPv * vec3( a_co, 1.0 ), 1.0 );"
290 "gl_Position = proj_pos;"
291 "aTexCoords = vec4( a_uv * 0.00390625, "
292 " (proj_pos.xy*0.5+0.5) * uBGInverseRatio );"
293 "aColour = a_colour;"
294 "}",
295 },
296 .fs = {
297 .orig_file = NULL,
298 .static_src =
299 "uniform sampler2D uTexGlyphs;"
300 "uniform sampler2D uTexBG;"
301 "uniform vec4 uColour;"
302 "uniform float uSpread;"
303 "out vec4 FragColor;"
304 ""
305 "in vec4 aTexCoords;"
306 "in vec4 aColour;"
307 ""
308 "vec2 rand_hash22( vec2 p ){"
309 "vec3 p3 = fract(vec3(p.xyx) * 213.8976123);"
310 "p3 += dot(p3, p3.yzx+19.19);"
311 "return fract(vec2((p3.x + p3.y)*p3.z, (p3.x+p3.z)*p3.y));"
312 "}"
313 ""
314 "void main(){"
315 "vec4 diffuse = aColour;"
316
317 "vec4 avg = vec4(0.0);"
318
319 "if( aColour.a == 0.0 ){"
320 "avg = aColour;"
321 "avg.a = texture( uTexGlyphs, aTexCoords.xy ).r;"
322 "}"
323 "else{"
324 "if( uSpread > 0.0001 ){"
325 "for( int i=0; i<4; i ++ ){"
326 "vec2 spread = rand_hash22(aTexCoords.zw+vec2(float(i)));"
327 "avg += texture( uTexBG, aTexCoords.zw + (spread-0.5)*uSpread );"
328 "}"
329 "avg *= 0.25;"
330 "avg.a = 1.0;"
331 "avg.rgb = mix( avg.rgb, aColour.rgb, aColour.a );"
332 "}"
333 "else{"
334 "avg = aColour;"
335 "}"
336 "}"
337
338 "FragColor = avg * uColour;"
339 "}"
340 }
341 };
342
343 static struct vg_shader _shader_ui_image = {
344 .name = "[vg] ui_image",
345 .link = NULL,
346 .vs =
347 {
348 .orig_file = NULL,
349 .static_src =
350 "layout (location=0) in vec2 a_co;"
351 "layout (location=1) in vec2 a_uv;"
352 "layout (location=2) in vec4 a_colour;"
353 "uniform mat3 uPv;"
354
355 "out vec2 aTexCoords;"
356 "out vec4 aColour;"
357 "out vec2 aWsp;"
358
359 "void main()"
360 "{"
361 "gl_Position = vec4( uPv * vec3( a_co, 1.0 ), 1.0 );"
362 "aTexCoords = a_uv * 0.00390625;"
363 "aColour = a_colour;"
364
365 "aWsp = a_co;"
366 "}",
367 },
368 .fs =
369 {
370 .orig_file = NULL,
371 .static_src =
372 "uniform sampler2D uTexImage;"
373 "uniform vec4 uColour;"
374 "out vec4 FragColor;"
375
376 "in vec2 aTexCoords;"
377 "in vec4 aColour;"
378 "in vec2 aWsp;"
379
380 "void main()"
381 "{"
382 "vec4 colour = texture( uTexImage, aTexCoords );"
383 "FragColor = colour * uColour;"
384 "}"
385 }
386 };
387
388 static struct vg_shader _shader_ui_hsv = {
389 .name = "[vg] ui_hsv",
390 .link = NULL,
391 .vs = {
392 .orig_file = NULL,
393 .static_src =
394 "layout (location=0) in vec2 a_co;"
395 "layout (location=1) in vec2 a_uv;"
396 "layout (location=2) in vec4 a_colour;"
397 "uniform mat3 uPv;"
398
399 "out vec2 aTexCoords;"
400 "out vec4 aColour;"
401 "out vec2 aWsp;"
402
403 "void main()"
404 "{"
405 "gl_Position = vec4( uPv * vec3( a_co, 1.0 ), 1.0 );"
406 "aTexCoords = a_uv * 0.00390625;"
407 "aColour = a_colour;"
408
409 "aWsp = a_co;"
410 "}",
411 },
412 .fs =
413 {
414 .orig_file = NULL,
415 .static_src =
416 "uniform float uHue;"
417 "out vec4 FragColor;"
418
419 "in vec2 aTexCoords;"
420 "in vec4 aColour;"
421 "in vec2 aWsp;"
422
423 "void main()"
424 "{"
425 "vec3 c = vec3( uHue, aTexCoords );"
426 "vec4 K = vec4(1.0,2.0/3.0,1.0/3.0,3.0);"
427 "vec3 p = abs(fract(c.xxx+K.xyz)*6.0 - K.www);"
428 "vec3 colour = c.z*mix(K.xxx,clamp(p-K.xxx,0.0,1.0),c.y);"
429 "FragColor = vec4( colour, 1.0 );"
430 "}"
431 }
432 };
433
434 static void _vg_ui_init(void){
435 if( !vg_shader_compile( &_shader_ui ) ||
436 !vg_shader_compile( &_shader_ui_image ) ||
437 !vg_shader_compile( &_shader_ui_hsv ) ){
438 vg_fatal_error( "Failed to compile ui shader" );
439 }
440
441 /*
442 * Vertex buffer
443 * ----------------------------------------
444 */
445
446 vg_ui.max_indices = 20000;
447 vg_ui.max_verts = 30000;
448
449 /* Generate the buffer we are gonna be drawing to */
450 glGenVertexArrays( 1, &vg_ui.vao );
451 glGenBuffers( 1, &vg_ui.vbo );
452 glGenBuffers( 1, &vg_ui.ebo );
453
454 glBindVertexArray( vg_ui.vao );
455 glBindBuffer( GL_ARRAY_BUFFER, vg_ui.vbo );
456
457 glBufferData( GL_ARRAY_BUFFER,
458 vg_ui.max_verts * sizeof( struct ui_vert ),
459 NULL, GL_DYNAMIC_DRAW );
460 glBindVertexArray( vg_ui.vao );
461
462 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, vg_ui.ebo );
463 glBufferData( GL_ELEMENT_ARRAY_BUFFER,
464 vg_ui.max_indices * sizeof( u16 ), NULL, GL_DYNAMIC_DRAW );
465
466 VG_CHECK_GL_ERR();
467
468 /* Set pointers */
469 u32 const stride = sizeof( struct ui_vert );
470
471 /* XY */
472 glVertexAttribPointer( 0, 2, GL_SHORT, GL_FALSE, stride,
473 (void *)offsetof( struct ui_vert, co ) );
474 glEnableVertexAttribArray( 0 );
475
476 /* UV */
477 glVertexAttribPointer( 1, 2, GL_UNSIGNED_SHORT, GL_FALSE, stride,
478 (void *)offsetof( struct ui_vert, uv ) );
479 glEnableVertexAttribArray( 1 );
480
481 /* COLOUR */
482 glVertexAttribPointer( 2, 4, GL_UNSIGNED_BYTE, GL_TRUE, stride,
483 (void *)offsetof( struct ui_vert, colour ) );
484 glEnableVertexAttribArray( 2 );
485
486 VG_CHECK_GL_ERR();
487
488 /* Alloc RAM default context */
489 u32 vert_size = vg_ui.max_verts*sizeof(struct ui_vert),
490 inds_size = vg_align8( vg_ui.max_indices*sizeof(u16) );
491
492 vg_ui.vertex_buffer = vg_linear_alloc( vg_mem.rtmemory, vert_size );
493 vg_ui.indice_buffer = vg_linear_alloc( vg_mem.rtmemory, inds_size );
494
495 /* font
496 * -----------------------------------------------------
497 */
498
499 /* Load default font */
500 u32 compressed[] = {
501 #include "vg/vg_pxfont_thin.h"
502 };
503
504 u32 pixels = 0, total = 256*256, data = 0;
505 u8 image[256*256];
506
507 while( pixels < total ){
508 for( int b = 31; b >= 0; b-- ){
509 image[ pixels ++ ] = (compressed[data] & (0x1u << b))? 0xffu: 0x00u;
510
511 if( pixels >= total ){
512 total = 0;
513 break;
514 }
515 }
516 data++;
517 }
518
519 glGenTextures( 1, &vg_ui.tex_glyphs );
520 glBindTexture( GL_TEXTURE_2D, vg_ui.tex_glyphs );
521 glTexImage2D( GL_TEXTURE_2D, 0, GL_R8, 256, 256, 0,
522 GL_RED, GL_UNSIGNED_BYTE, image );
523
524 VG_CHECK_GL_ERR();
525 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
526 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
527 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
528 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
529
530 /*
531 * Cursors
532 * ---------------------------------------------------------------
533 */
534
535 vg_ui.cursor_map[ k_ui_cursor_default ] =
536 SDL_CreateSystemCursor( SDL_SYSTEM_CURSOR_ARROW );
537 vg_ui.cursor_map[ k_ui_cursor_hand ] =
538 SDL_CreateSystemCursor( SDL_SYSTEM_CURSOR_HAND );
539 vg_ui.cursor_map[ k_ui_cursor_ibeam ] =
540 SDL_CreateSystemCursor( SDL_SYSTEM_CURSOR_IBEAM );
541 }
542
543 enum ui_shader {
544 k_ui_shader_colour,
545 k_ui_shader_image,
546 k_ui_shader_hsv,
547 };
548
549 static void rect_copy( ui_rect a, ui_rect b ){
550 for( int i=0; i<4; i++ )
551 b[i] = a[i];
552 }
553
554 static void ui_flush( enum ui_shader shader, f32 w, f32 h ){
555 u32 vertex_offset = vg_ui.vert_start*sizeof(ui_vert),
556 vertex_count = vg_ui.cur_vert-vg_ui.vert_start,
557 vertex_size = vertex_count*sizeof(ui_vert),
558
559 indice_offset = vg_ui.indice_start*sizeof(u16),
560 indice_count = vg_ui.cur_indice-vg_ui.indice_start,
561 indice_size = indice_count * sizeof(u16);
562
563 if( !vertex_size || !indice_size )
564 return;
565
566 glBindVertexArray( vg_ui.vao );
567 glBindBuffer( GL_ARRAY_BUFFER, vg_ui.vbo );
568 glBufferSubData( GL_ARRAY_BUFFER, vertex_offset, vertex_size,
569 vg_ui.vertex_buffer+vg_ui.vert_start );
570
571 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, vg_ui.ebo );
572 glBufferSubData( GL_ELEMENT_ARRAY_BUFFER, indice_offset, indice_size,
573 vg_ui.indice_buffer+vg_ui.indice_start );
574
575 glDisable( GL_DEPTH_TEST );
576 glEnable( GL_BLEND );
577 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
578 glBlendEquation( GL_FUNC_ADD );
579 glDisable( GL_CULL_FACE );
580
581 m3x3f view = M3X3_IDENTITY;
582 m3x3_translate( view, (v3f){ -1.0f, 1.0f, 0.0f } );
583 m3x3_scale( view, (v3f){ 1.0f/(w*0.5f),
584 -1.0f/(h*0.5f), 1.0f } );
585
586 if( shader == k_ui_shader_colour ){
587 glUseProgram( _shader_ui.id );
588
589 glUniformMatrix3fv( glGetUniformLocation( _shader_ui.id, "uPv" ), 1,
590 GL_FALSE, (float *)view );
591 glUniform4fv( glGetUniformLocation( _shader_ui.id, "uColour" ), 1,
592 vg_ui.colour );
593
594 glActiveTexture( GL_TEXTURE0 );
595 glBindTexture( GL_TEXTURE_2D, vg_ui.tex_glyphs );
596 glUniform1i( glGetUniformLocation( _shader_ui.id, "uTexGlyphs" ), 0 );
597
598 glActiveTexture( GL_TEXTURE1 );
599 glBindTexture( GL_TEXTURE_2D, vg_ui.tex_bg );
600 glUniform1i( glGetUniformLocation( _shader_ui.id, "uTexBG" ), 1 );
601 glUniform1f( glGetUniformLocation( _shader_ui.id, "uSpread" ),
602 vg_ui.frosting );
603 glUniform2fv( glGetUniformLocation( _shader_ui.id, "uBGInverseRatio" ),
604 1, vg_ui.bg_inverse_ratio );
605 }
606 else if( shader == k_ui_shader_image ){
607 glUseProgram( _shader_ui_image.id );
608 glUniformMatrix3fv( glGetUniformLocation( _shader_ui_image.id, "uPv" ), 1,
609 GL_FALSE, (float *)view );
610 glUniform1i( glGetUniformLocation(_shader_ui_image.id,"uTexImage"), 0 );
611 glUniform4fv( glGetUniformLocation( _shader_ui_image.id, "uColour" ), 1,
612 vg_ui.colour );
613 }
614 else if( shader == k_ui_shader_hsv ){
615 glUseProgram( _shader_ui_hsv.id );
616 glUniformMatrix3fv( glGetUniformLocation( _shader_ui_hsv.id, "uPv" ), 1,
617 GL_FALSE, (float *)view );
618 glUniform1f( glGetUniformLocation(_shader_ui_hsv.id,"uHue"), vg_ui.hue );
619 }
620 else
621 vg_fatal_error( "Invalid UI shader (%d)\n", shader );
622
623 glDrawElements( GL_TRIANGLES, indice_count, GL_UNSIGNED_SHORT,
624 (void *)(vg_ui.indice_start*sizeof(u16)) );
625
626 glDisable( GL_BLEND );
627
628 vg_ui.indice_start = vg_ui.cur_indice;
629 vg_ui.vert_start = vg_ui.cur_vert;
630 }
631
632 static void ui_fill_rect( ui_rect rect, u32 colour, ui_px uv[4] ){
633 /* this if far from ideal but stops us from crashing */
634 if( (vg_ui.cur_vert + 4 > vg_ui.max_verts) ||
635 (vg_ui.cur_indice + 6 > vg_ui.max_indices))
636 return;
637
638 struct ui_vert *vertices = &vg_ui.vertex_buffer[ vg_ui.cur_vert ];
639 u16 *indices = &vg_ui.indice_buffer[ vg_ui.cur_indice ];
640
641 for( int i=0; i<4; i++ ){
642 vertices[i].colour = colour;
643 }
644
645 vertices[0].co[0] = rect[0];
646 vertices[0].co[1] = rect[1];
647 vertices[0].uv[0] = uv[0];
648 vertices[0].uv[1] = uv[1];
649 vertices[1].co[0] = rect[0]+rect[2];
650 vertices[1].co[1] = rect[1];
651 vertices[1].uv[0] = uv[2];
652 vertices[1].uv[1] = uv[1];
653 vertices[2].co[0] = rect[0]+rect[2];
654 vertices[2].co[1] = rect[1]+rect[3];
655 vertices[2].uv[0] = uv[2];
656 vertices[2].uv[1] = uv[3];
657 vertices[3].co[0] = rect[0];
658 vertices[3].co[1] = rect[1]+rect[3];
659 vertices[3].uv[0] = uv[0];
660 vertices[3].uv[1] = uv[3];
661 u16 ind_start = vg_ui.cur_vert;
662
663 u16 start = vg_ui.cur_vert;
664 u32 mesh[] = { 0,2,1, 0,3,2 };
665
666 for( u32 i=0; i<vg_list_size(mesh); i++ ){
667 indices[i] = start+mesh[i];
668 }
669
670 vg_ui.cur_indice += 6;
671 vg_ui.cur_vert += 4;
672 }
673
674 static void ui_fill( ui_rect rect, u32 colour ){
675 ui_fill_rect( rect, colour, (ui_px[4]){ 4,4,4,4 } );
676 }
677
678 static void ui_outline( ui_rect rect, ui_px thickness, u32 colour, u32 mask ){
679 /* this if far from ideal but stops us from crashing */
680 if( (vg_ui.cur_vert + 8 > vg_ui.max_verts) ||
681 (vg_ui.cur_indice + 24 > vg_ui.max_indices))
682 return;
683
684 struct ui_vert *vertices = &vg_ui.vertex_buffer[ vg_ui.cur_vert ];
685 u16 *indices = &vg_ui.indice_buffer[ vg_ui.cur_indice ];
686
687 for( int i=0; i<8; i++ ){
688 vertices[i].uv[0] = 4;
689 vertices[i].uv[1] = 4;
690 vertices[i].colour = colour;
691 }
692
693 vertices[0].co[0] = rect[0];
694 vertices[0].co[1] = rect[1];
695 vertices[1].co[0] = rect[0]+rect[2];
696 vertices[1].co[1] = rect[1];
697 vertices[2].co[0] = rect[0]+rect[2];
698 vertices[2].co[1] = rect[1]+rect[3];
699 vertices[3].co[0] = rect[0];
700 vertices[3].co[1] = rect[1]+rect[3];
701 vertices[4].co[0] = vertices[0].co[0]-thickness;
702 vertices[4].co[1] = vertices[0].co[1]-thickness;
703 vertices[5].co[0] = vertices[1].co[0]+thickness;
704 vertices[5].co[1] = vertices[1].co[1]-thickness;
705 vertices[6].co[0] = vertices[2].co[0]+thickness;
706 vertices[6].co[1] = vertices[2].co[1]+thickness;
707 vertices[7].co[0] = vertices[3].co[0]-thickness;
708 vertices[7].co[1] = vertices[3].co[1]+thickness;
709
710 u16 start = vg_ui.cur_vert;
711 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 };
712
713 if( !mask )
714 mask = UI_TOP|UI_LEFT|UI_BOTTOM|UI_RIGHT;
715
716 u32 c = 0;
717 for( u32 i=0; i<vg_list_size(mesh)/6; i++ ){
718 if( (0x1<<i) & mask ){
719 for( u32 j=0; j<6; j++ )
720 indices[c ++] = start+mesh[i*6+j];
721 }
722 }
723
724 vg_ui.cur_indice += c;
725 vg_ui.cur_vert += 8;
726 }
727
728 static void ui_split( ui_rect rect,
729 enum ui_axis other, ui_px width, ui_px gap,
730 ui_rect l, ui_rect r ){
731 enum ui_axis dir = other ^ 0x1;
732
733 if( width < 0 ) width = rect[ 2+dir ] + width;
734
735 ui_rect temp;
736 rect_copy( rect, temp );
737
738 l[ dir ] = temp[ dir ];
739 r[ dir ] = temp[ dir ] + width + (gap/2);
740 l[ other ] = temp[ other ];
741 r[ other ] = temp[ other ];
742 l[ 2+dir ] = width - (gap/2);
743 r[ 2+dir ] = temp[ 2+dir ] - width - (gap/2);
744 l[ 2+other ] = temp[ 2+other ];
745 r[ 2+other ] = temp[ 2+other ];
746 }
747
748 static ui_px ui_text_line_width( const char *str );
749
750 static void ui_rect_center( ui_rect parent, ui_rect rect ){
751 rect[0] = parent[0] + (parent[2]-rect[2])/2;
752 rect[1] = parent[1] + (parent[3]-rect[3])/2;
753 }
754
755 static void ui_fit_item( ui_rect rect, ui_px size[2], ui_rect d ){
756 i32 rp = (i32)rect[2] * (i32)size[1],
757 rc = (i32)size[0] * (i32)rect[3];
758
759 enum ui_axis dir, other;
760 if( rc > rp ) dir = k_ui_axis_h;
761 else dir = k_ui_axis_v;
762 other = dir ^ 0x1;
763
764 d[2+dir] = rect[2+dir];
765 d[2+other] = (rect[2+dir] * size[other]) / size[dir];
766
767 ui_rect_center( rect, d );
768 }
769
770 static void ui_split_ratio( ui_rect rect, enum ui_axis dir, float ratio,
771 ui_px gap, ui_rect l, ui_rect r ){
772 ui_px width = (float)rect[ 2+(dir^0x1) ] * ratio;
773 ui_split( rect, dir, width, gap, l, r );
774 }
775
776 static void ui_rect_pad( ui_rect rect, ui_px pad[2] ){
777 rect[0] += pad[0];
778 rect[1] += pad[1];
779 rect[2] -= pad[0]*2;
780 rect[3] -= pad[1]*2;
781 }
782
783 static ui_px ui_text_line_width( const char *str ){
784 int length = 0;
785 const char *_c = str;
786 u8 c;
787
788 while( (c = *(_c ++)) ){
789 if( c >= 32 ) length ++;
790 else if( c == '\n' ) break;
791 }
792
793 return length * vg_ui.font->spacing;
794 }
795
796 static ui_px ui_text_string_height( const char *str ){
797 int height = 1;
798 const char *_c = str;
799 u8 c;
800
801 while( (c = *(_c ++)) ){
802 if( c == '\n' ) height ++;
803 }
804
805 return height * 14;
806 }
807
808 static ui_px ui_text_aligned_x( const char *str, ui_rect rect, ui_px scale,
809 enum ui_align align ){
810 enum ui_align lwr = k_ui_align_lwr & align;
811 if( lwr == k_ui_align_left ){
812 return rect[0];
813 }
814 else{
815 ui_px width = ui_text_line_width( str ) * scale;
816
817 if( lwr == k_ui_align_right )
818 return rect[0] + rect[2]-width;
819 else
820 return rect[0] + (rect[2]-width)/2;
821 }
822 }
823
824 static ui_px ui_min( ui_px a, ui_px b ){ return a<b?a:b; }
825 static ui_px ui_max( ui_px a, ui_px b ){ return a>b?a:b; }
826 static ui_px ui_clamp( ui_px a, ui_px min, ui_px max ){
827 return ui_min( max, ui_max( a, min ) );
828 }
829
830 static int ui_clip( ui_rect parent, ui_rect child, ui_rect clipped ){
831 ui_px parent_max[2], child_max[2];
832 parent_max[0] = parent[0]+parent[2];
833 parent_max[1] = parent[1]+parent[3];
834 child_max[0] = child[0]+child[2];
835 child_max[1] = child[1]+child[3];
836
837 clipped[0] = ui_clamp( child[0], parent[0], parent_max[0] );
838 clipped[1] = ui_clamp( child[1], parent[1], parent_max[1] );
839 clipped[2] = ui_clamp( child_max[0], parent[0], parent_max[0] );
840 clipped[3] = ui_clamp( child_max[1], parent[1], parent_max[1] );
841
842 if( clipped[0] == clipped[2] ||
843 clipped[1] == clipped[3] )
844 return 0;
845
846 clipped[2] -= clipped[0];
847 clipped[3] -= clipped[1];
848
849 return 1;
850 }
851
852 static int ui_inside_rect( ui_rect rect, ui_px co[2] ){
853 if( co[0] >= rect[0] &&
854 co[1] >= rect[1] &&
855 co[0] < rect[0]+rect[2] &&
856 co[1] < rect[1]+rect[3] ){
857 return 1;
858 }
859 else
860 return 0;
861 }
862
863 static int ui_click_down( u32 mask ){
864 if( vg_ui.ignore_input_frames ) return 0;
865 if( (vg_ui.mouse_state[0] & mask) &&
866 !(vg_ui.mouse_state[1] & mask) )
867 return 1;
868 else
869 return 0;
870 }
871
872 static int ui_clicking( u32 mask ){
873 if( vg_ui.ignore_input_frames ) return 0;
874 return vg_ui.mouse_state[0] & mask;
875 }
876
877 static int ui_click_up( u32 mask ){
878 if( vg_ui.ignore_input_frames ) return 0;
879 if( (vg_ui.mouse_state[1] & mask) &&
880 !(vg_ui.mouse_state[0] & mask) )
881 return 1;
882 else
883 return 0;
884 }
885
886 static void ui_prerender(void){
887 int x, y;
888 vg_ui.mouse_state[1] = vg_ui.mouse_state[0];
889 vg_ui.mouse_state[0] = SDL_GetMouseState( &x, &y );
890 vg_ui.mouse_delta[0] = x-vg_ui.mouse[0];
891 vg_ui.mouse_delta[1] = y-vg_ui.mouse[1];
892 vg_ui.mouse[0] = x;
893 vg_ui.mouse[1] = y;
894
895 vg_ui.cur_vert = 0;
896 vg_ui.cur_indice = 0;
897 vg_ui.vert_start = 0;
898 vg_ui.indice_start = 0;
899 vg_ui.focused_control_hit = 0;
900 vg_ui.cursor = k_ui_cursor_default;
901 vg_ui.wants_mouse = 0;
902
903 if( vg_ui.ignore_input_frames ){
904 vg_ui.ignore_input_frames --;
905 return;
906 }
907
908 if( ui_click_down(UI_MOUSE_LEFT)||ui_click_down(UI_MOUSE_MIDDLE) ){
909 vg_ui.mouse_click[0] = vg_ui.mouse[0];
910 vg_ui.mouse_click[1] = vg_ui.mouse[1];
911 }
912 }
913
914 static u32 ui_colour( enum ui_scheme_colour id ){
915 return vg_ui.scheme[ id ];
916 }
917
918 /* get an appropriately contrasting colour given the base */
919 static u32 ui_colourcont( enum ui_scheme_colour id ){
920 if ( id < k_ui_bg+6 ) return ui_colour( k_ui_fg );
921 else if( id < k_ui_fg ) return ui_colour( k_ui_bg+1 );
922 else if( id < k_ui_hue ) return ui_colour( k_ui_bg+3 );
923 else if( id < k_ui_red+k_ui_brighter ) return ui_colour( k_ui_fg );
924 else return ui_colour( k_ui_fg+1 );
925 }
926
927 static void ui_hex_to_norm( u32 hex, v4f norm ){
928 norm[0] = ((hex ) & 0xff);
929 norm[1] = ((hex>>8 ) & 0xff);
930 norm[2] = ((hex>>16) & 0xff);
931 norm[3] = ((hex>>24) & 0xff);
932 v4_muls( norm, 1.0f/255.0f, norm );
933 }
934
935 static void ui_text_glyph( const struct ui_font *font, ui_px scale,
936 u8 glyph, ui_rect out_texcoords ){
937 glyph -= font->ascii_start;
938
939 ui_px per_row = font->sheet_size / font->glyph_width,
940 column = (ui_px)glyph % per_row,
941 row = (glyph - column) / per_row;
942
943 out_texcoords[0] = column * font->glyph_width;
944 out_texcoords[1] = row * font->glyph_height + font->offset_y;
945 out_texcoords[2] = out_texcoords[0] + font->glyph_width;
946 out_texcoords[3] = out_texcoords[1] + font->glyph_height;
947 }
948
949 static u32 ui_opacity( u32 colour, f32 opacity ){
950 u32 alpha = opacity * 255.0f;
951 return (colour & 0x00ffffff) | (alpha << 24);
952 }
953
954 static u32 ui_ntext( ui_rect rect, const char *str, u32 len, ui_px scale,
955 enum ui_align align, u32 colour ){
956 ui_rect text_cursor;
957 if( colour == 0 ) colour = ui_colour( k_ui_fg );
958
959 colour &= 0x00ffffff;
960
961 const char *_c = str;
962 u8 c;
963
964 text_cursor[0] = ui_text_aligned_x( str, rect, scale, align );
965 text_cursor[1] = rect[1];
966 text_cursor[2] = vg_ui.font->glyph_width*scale;
967 text_cursor[3] = vg_ui.font->glyph_height*scale;
968
969 u32 printed_chars = 0;
970
971 if( align & (k_ui_align_middle|k_ui_align_bottom) ){
972 ui_px height = ui_text_string_height( str ) * scale;
973
974 if( align & k_ui_align_bottom )
975 text_cursor[1] += rect[3]-height;
976 else
977 text_cursor[1] += (rect[3]-height)/2;
978 }
979
980 while( (c = *(_c ++)) ){
981 if( printed_chars >= len ){
982 printed_chars = 0;
983 text_cursor[1] += vg_ui.font->line_height*scale;
984 text_cursor[0] = ui_text_aligned_x( _c, rect, scale, align );
985 text_cursor[0] -= vg_ui.font->spacing*scale;
986
987 ui_rect glyph;
988 ui_text_glyph( vg_ui.font, scale, '\xb6' /*FIXME*/, glyph );
989 ui_fill_rect( text_cursor, 0x00ffffff, glyph );
990 text_cursor[0] += vg_ui.font->spacing*scale;
991 }
992
993 if( c == '\n' ){
994 text_cursor[1] += vg_ui.font->line_height*scale;
995 text_cursor[0] = ui_text_aligned_x( _c, rect, scale, align );
996 printed_chars = 0;
997 continue;
998 }
999 else if( c >= 33 ){
1000 ui_rect glyph;
1001 ui_text_glyph( vg_ui.font, scale, c, glyph );
1002
1003 ui_rect cursor_clipped;
1004 if( ui_clip( rect, text_cursor, cursor_clipped ) ){
1005 ui_fill_rect( cursor_clipped, colour, glyph );
1006 }
1007 }
1008 else if( c == '\x1B' ){
1009 /* vt codes */
1010 _c ++;
1011 u16 colour_id = 0;
1012 for( int i=0; i<3; i ++ ){
1013 if( _c[i] ){
1014 if( _c[i] == 'm' ){
1015 _c = _c + i + 1;
1016
1017 switch( colour_id ){
1018 case '0': colour = ui_colour( k_ui_fg ); break;
1019 case '3'|'0'<<8: colour = ui_colour( k_ui_bg ); break;
1020 case '3'|'1'<<8: colour = ui_colour( k_ui_red ); break;
1021 case '3'|'2'<<8: colour = ui_colour( k_ui_green ); break;
1022 case '3'|'3'<<8: colour = ui_colour( k_ui_yellow ); break;
1023 case '3'|'4'<<8: colour = ui_colour( k_ui_blue ); break;
1024 case '3'|'5'<<8: colour = ui_colour( k_ui_purple ); break;
1025 case '3'|'6'<<8: colour = ui_colour( k_ui_aqua ); break;
1026 case '3'|'7'<<8: colour = 0xffffffff; break;
1027 }
1028
1029 colour &= 0x00ffffff;
1030 break;
1031 }
1032
1033 colour_id |= _c[i] << (i*8);
1034 }
1035 else{
1036 _c = _c +i;
1037 break;
1038 }
1039 }
1040
1041 continue;
1042 }
1043 else if( c == '\t' ){
1044 text_cursor[0] += vg_ui.font->spacing*scale*4;
1045 printed_chars += 4;
1046 continue;
1047 }
1048
1049 text_cursor[0] += vg_ui.font->spacing*scale;
1050 printed_chars ++;
1051 }
1052
1053 return printed_chars;
1054 }
1055
1056 static void ui_text( ui_rect rect, const char *str, ui_px scale,
1057 enum ui_align align, u32 colour ){
1058 ui_ntext( rect, str, 1024, scale, align, colour );
1059 }
1060
1061 /*
1062 * Standard layout stuff
1063 * -----------------------------------------------------------------------------
1064 */
1065
1066 static void ui_panel( ui_rect in_rect, ui_rect out_panel ){
1067 ui_fill( in_rect, ui_colour( k_ui_bg+1 ) );
1068 ui_outline( in_rect, 1, ui_colour( k_ui_bg+7 ), 0 );
1069 rect_copy( in_rect, out_panel );
1070 ui_rect_pad( out_panel, (ui_px[2]){ k_ui_padding, k_ui_padding } );
1071 }
1072
1073 static void ui_label( ui_rect rect, const char *text, ui_px size,
1074 ui_px gap, ui_rect r ){
1075 ui_rect l;
1076 ui_px width = (ui_text_line_width(text)+vg_ui.font->spacing) * size;
1077 ui_split( rect, k_ui_axis_v, width, gap, l, r );
1078 ui_text( l, text, 1, k_ui_align_middle_left, 0 );
1079 }
1080
1081 static void ui_standard_widget( ui_rect inout_panel, ui_rect out_rect,
1082 ui_px count ){
1083 ui_px height = (count * vg_ui.font->glyph_height + 18) * k_ui_scale;
1084 ui_split( inout_panel, k_ui_axis_h, height, k_ui_padding,
1085 out_rect, inout_panel );
1086 }
1087
1088 static void ui_info( ui_rect inout_panel, const char *text ){
1089 ui_rect box;
1090 ui_standard_widget( inout_panel, box, 1 );
1091 ui_text( box, text, 1, k_ui_align_middle_left, 0 );
1092 }
1093
1094 static void ui_image( ui_rect rect, GLuint image ){
1095 ui_flush( k_ui_shader_colour, vg.window_x, vg.window_y );
1096 glActiveTexture( GL_TEXTURE0 );
1097 glBindTexture( GL_TEXTURE_2D, image );
1098 ui_fill_rect( rect, 0xffffffff, (ui_px[4]){ 0,256,256,0 } );
1099 ui_flush( k_ui_shader_image, vg.window_x, vg.window_y );
1100 }
1101
1102 static u32 v4f_u32_colour( v4f colour ){
1103 u32 r = colour[0] * 255.0f,
1104 g = colour[1] * 255.0f,
1105 b = colour[2] * 255.0f,
1106 a = colour[3] * 255.0f;
1107
1108 return r | (g<<8) | (b<<16) | (a<<24);
1109 }
1110
1111 static void ui_defocus_all(void){
1112 if( vg_ui.focused_control_type == k_ui_control_textbox ){
1113 SDL_StopTextInput();
1114 if( vg_ui.textbox.callbacks.escape )
1115 vg_ui.textbox.callbacks.escape();
1116 }
1117
1118 vg_ui.focused_control_id = NULL;
1119 vg_ui.focused_control_hit = 0;
1120 vg_ui.focused_control_type = k_ui_control_none;
1121 }
1122
1123 enum ui_button_state {
1124 k_ui_button_none = 0x0,
1125 k_ui_button_click = 0x1,
1126 k_ui_button_holding_inside = 0x2,
1127 k_ui_button_holding_outside = 0x4,
1128 k_ui_button_hover = 0x8
1129 };
1130
1131 /* TODO: split this out into a formatless button and one that auto fills */
1132 static enum ui_button_state ui_colourbutton( ui_rect rect,
1133 enum ui_scheme_colour colour,
1134 enum ui_scheme_colour hover_colour,
1135 enum ui_scheme_colour hi_colour,
1136 bool const fill ){
1137 int clickup= ui_click_up(UI_MOUSE_LEFT),
1138 click = ui_clicking(UI_MOUSE_LEFT) | clickup,
1139 target = ui_inside_rect( rect, vg_ui.mouse_click ) && click,
1140 hover = ui_inside_rect( rect, vg_ui.mouse );
1141
1142 u32 col_base = vg_ui.scheme[ colour ],
1143 col_highlight = vg_ui.scheme[ hi_colour? hi_colour: k_ui_fg ],
1144 col_hover = vg_ui.scheme[ hover_colour? hover_colour:
1145 colour + k_ui_brighter ];
1146
1147 if( vg_ui.focused_control_type != k_ui_control_none ){
1148 clickup = 0;
1149 click = 0;
1150 target = 0;
1151 hover = 0;
1152 }
1153
1154 if( hover ){
1155 vg_ui.cursor = k_ui_cursor_hand;
1156 }
1157
1158 if( click ){
1159 if( target ){
1160 if( hover ){
1161 if( clickup ){
1162 vg_ui.ignore_input_frames = 2;
1163 ui_defocus_all();
1164
1165 if( fill ) {
1166 ui_fill( rect, col_highlight );
1167 rect_copy( rect, vg_ui.click_fader );
1168 rect_copy( rect, vg_ui.click_fader_end );
1169 vg_ui.click_fader_end[3] = 0;
1170 ui_rect_center( rect, vg_ui.click_fader_end );
1171 vg_ui.click_fade_opacity = 1.0f;
1172 }
1173
1174 return k_ui_button_click;
1175 }
1176 else{
1177 if( fill ) ui_fill( rect, col_highlight );
1178 return k_ui_button_holding_inside;
1179 }
1180 }
1181 else{
1182 if( fill ) ui_fill( rect, col_base );
1183 ui_outline( rect, 1, col_highlight, 0 );
1184 return k_ui_button_holding_outside;
1185 }
1186 }
1187 else{
1188 if( fill ) ui_fill( rect, col_base );
1189 return k_ui_button_none;
1190 }
1191 }
1192 else{
1193 if( hover ){
1194 if( fill ) ui_fill( rect, col_hover );
1195 return k_ui_button_hover;
1196 }
1197 else{
1198 if( fill ) ui_fill( rect, col_base );
1199 return k_ui_button_none;
1200 }
1201 }
1202 }
1203
1204 static enum ui_button_state ui_colourbutton_text(
1205 ui_rect rect, const char *string, ui_px scale,
1206 enum ui_scheme_colour colour ){
1207 enum ui_button_state state = ui_colourbutton( rect, colour, 0, 0, 1 );
1208 ui_rect t = { 0,0, ui_text_line_width( string )*scale, 14*scale };
1209 ui_rect_center( rect, t );
1210
1211 u32 text_colour = ui_colourcont(colour);
1212 if( state == k_ui_button_holding_inside )
1213 text_colour = colour;
1214
1215 ui_text( t, string, scale, k_ui_align_left, text_colour );
1216 return state;
1217 }
1218
1219 static enum ui_button_state ui_button_text( ui_rect rect,
1220 const char *string, ui_px scale ){
1221 return ui_colourbutton_text( rect, string, scale, k_ui_bg+4 );
1222 }
1223
1224 static enum ui_button_state ui_button( ui_rect inout_panel,
1225 const char *string ){
1226 ui_rect rect;
1227 ui_standard_widget( inout_panel, rect, 1 );
1228 return ui_colourbutton_text( rect, string, 1, k_ui_bg+4 );
1229 }
1230
1231 static void ui_enum_post(void);
1232 static void ui_postrender(void){
1233 if( vg_ui.click_fade_opacity > 0.0f ){
1234 float scale = vg_ui.click_fade_opacity;
1235 scale = vg_maxf( 1.0f/255.0f, scale*scale );
1236
1237 vg_ui.click_fade_opacity -= vg.time_frame_delta * 3.8f;
1238 u32 colour = (0x00ffffff & ui_colour(k_ui_fg))|0x7f000000;
1239
1240 v4f begin, end, dest;
1241 for( int i=0; i<4; i++ ){
1242 begin[i] = vg_ui.click_fader[i];
1243 end[i] = vg_ui.click_fader_end[i]+1;
1244 }
1245
1246 v4_lerp( end, begin, scale, dest );
1247
1248 ui_rect rect;
1249 for( int i=0; i<4; i++ ){
1250 rect[i] = dest[i];
1251 }
1252
1253 ui_fill( rect, colour );
1254 }
1255
1256 if( vg_ui.focused_control_type == k_ui_control_enum ){
1257 ui_enum_post();
1258 }
1259 else if( vg_ui.focused_control_type == k_ui_control_modal ){
1260 ui_rect screen = {0,0,vg.window_x,vg.window_y};
1261 ui_fill( screen, 0xa0000000 );
1262 ui_rect box = {0,0,400,200};
1263
1264 u32 colour = ui_colour(k_ui_fg),
1265 type = vg_ui.modal.options & UI_MODAL_TYPE_BITS;
1266 if ( type == 1 ) colour = ui_colour(k_ui_green);
1267 else if( type == 2 ) colour = ui_colour(k_ui_red);
1268 else if( type == 3 ) colour = ui_colour(k_ui_yellow);
1269
1270 ui_rect_center( screen, box );
1271 ui_fill( box, ui_colour(k_ui_bg) );
1272 ui_outline( box, -1, colour, 0 );
1273
1274 ui_rect message;
1275 rect_copy( box, message );
1276 message[3] = 100;
1277 ui_rect_center( box, message );
1278
1279 ui_rect row0, row1, btn;
1280 ui_split_ratio( message, k_ui_axis_h, 0.5f, 0, row0, row1 );
1281 row0[0] += vg_ui.font->spacing;
1282 ui_ntext( row0, vg_ui.modal.message, (box[2]/vg_ui.font->spacing)-2, 1,
1283 k_ui_align_left, colour );
1284
1285 rect_copy( row1, btn );
1286 btn[2] = 86;
1287 btn[3] = 28;
1288 ui_rect_center( row1, btn );
1289
1290 vg_ui.focused_control_type = k_ui_control_none; /* HACK */
1291 if( ui_button_text( btn, "OK", 1 ) != 1 )
1292 vg_ui.focused_control_hit = 1;
1293 vg_ui.focused_control_type = k_ui_control_modal; /* HACK */
1294 vg_ui.wants_mouse = 1;
1295 }
1296
1297 ui_flush( k_ui_shader_colour, vg.window_x, vg.window_y );
1298
1299 if( !vg_ui.focused_control_hit ){
1300 ui_defocus_all();
1301 }
1302
1303 if( vg_ui.wants_mouse ){
1304 SDL_SetWindowGrab( vg.window, SDL_FALSE );
1305 SDL_SetRelativeMouseMode( SDL_FALSE );
1306 }
1307 else{
1308 SDL_SetWindowGrab( vg.window, SDL_TRUE );
1309 SDL_SetRelativeMouseMode( SDL_TRUE );
1310 }
1311
1312 SDL_SetCursor( vg_ui.cursor_map[ vg_ui.cursor ] );
1313 SDL_ShowCursor(1);
1314 }
1315
1316 /*
1317 * checkbox
1318 * -----------------------------------------------------------------------------
1319 */
1320
1321 static int ui_checkbox( ui_rect inout_panel, const char *str_label, i32 *data ){
1322 ui_rect rect, label, box;
1323 ui_standard_widget( inout_panel, rect, 1 );
1324
1325 ui_split( rect, k_ui_axis_v, -rect[3], 0, label, box );
1326 ui_text( label, str_label, k_ui_scale, k_ui_align_middle_left, 0 );
1327
1328 int changed = ui_colourbutton( box, k_ui_bg, 0, 0, 1 )==1;
1329 if( changed )
1330 *data = (*data) ^ 0x1;
1331
1332 if( *data ){
1333 ui_rect_pad( box, (ui_px[2]){4,4} );
1334 ui_fill( box, ui_colour( k_ui_orange ) );
1335 }
1336
1337 return changed;
1338 }
1339
1340 /*
1341 * Dropdown / Enum
1342 * -----------------------------------------------------------------------------
1343 */
1344
1345 /*
1346 * unfortunately no return value since we only find out that event in the
1347 * postrender step.
1348 */
1349 static void ui_enum( ui_rect inout_panel, const char *str_label,
1350 struct ui_enum_opt *options, u32 len, i32 *value ){
1351 ui_rect rect, label, box;
1352 ui_standard_widget( inout_panel, rect, 1 );
1353 ui_label( rect, str_label, k_ui_scale, 0, box );
1354
1355 const char *display = "OUT OF RANGE";
1356 int valid = 0;
1357 for( u32 i=0; i<len; i ++ ){
1358 if( *value == options[i].value ){
1359 display = options[i].alias;
1360 valid = 1;
1361 break;
1362 }
1363 }
1364
1365 if( ui_button_text( box, display, k_ui_scale ) == 1 ){
1366 vg_ui.focused_control_type = k_ui_control_enum;
1367 vg_ui.ptr_enum = value;
1368 vg_ui._enum.option_count = len;
1369 vg_ui._enum.options = options;
1370 rect_copy( box, vg_ui._enum.rect );
1371 }
1372
1373 if( !valid )
1374 ui_outline( box, 1, ui_colour(k_ui_red), 0 );
1375 }
1376
1377 static void ui_enum_post(void){
1378 ui_rect drawer;
1379 rect_copy( vg_ui._enum.rect, drawer );
1380 drawer[3] *= vg_ui._enum.option_count;
1381
1382 int close = 0;
1383 int clickany= ui_click_up(UI_MOUSE_LEFT|UI_MOUSE_RIGHT|UI_MOUSE_MIDDLE),
1384 hover = ui_inside_rect( drawer, vg_ui.mouse );
1385
1386 if( clickany && !hover ){
1387 return;
1388 }
1389
1390 /* HACK */
1391 vg_ui.focused_control_type = k_ui_control_none;
1392 i32 *value = vg_ui.ptr_enum;
1393
1394 for( u32 i=0; i<vg_ui._enum.option_count; i++ ){
1395 ui_rect button;
1396 ui_split( drawer, k_ui_axis_h, vg_ui._enum.rect[3], 0, button,drawer );
1397
1398 enum ui_scheme_colour colour = k_ui_bg+3;
1399 if( vg_ui._enum.options[i].value == *value )
1400 colour = k_ui_orange;
1401
1402 if( ui_colourbutton_text( button, vg_ui._enum.options[i].alias,
1403 k_ui_scale, colour ) == 1 ){
1404 *value = vg_ui._enum.options[i].value;
1405 close = 1;
1406 }
1407 }
1408
1409 /* HACK */
1410 vg_ui.focused_control_type = k_ui_control_enum;
1411
1412 if( !close )
1413 vg_ui.focused_control_hit = 1;
1414 }
1415
1416 /*
1417 * Slider
1418 * -----------------------------------------------------------------------------
1419 */
1420
1421 static enum ui_button_state _ui_slider( ui_rect box,
1422 f32 min, f32 max, f32 *value, const char *format ){
1423 f32 t;
1424
1425 enum ui_button_state
1426 mask_using =
1427 k_ui_button_holding_inside |
1428 k_ui_button_holding_outside |
1429 k_ui_button_click,
1430 mask_brighter =
1431 mask_using | k_ui_button_hover,
1432 state = ui_colourbutton( box, k_ui_bg, k_ui_bg+2, k_ui_bg+3, 1 );
1433
1434 if( state & mask_using ){
1435 t = vg_clampf( (f32)(vg_ui.mouse[0] - box[0]) / (f32)( box[2] ),
1436 0.0f, 1.0f );
1437 *value = vg_lerpf( min, max, t );
1438 }
1439 else
1440 t = vg_clampf( (*value - min) / (max-min), 0.0f, 1.0f );
1441
1442 ui_rect line = { box[0], box[1], t * (f32)box[2], box[3] };
1443 ui_fill( line, ui_colour(state&mask_brighter? k_ui_bg+4: k_ui_bg+2) );
1444
1445 ui_outline( box, 1, ui_colour(state? k_ui_fg+3: k_ui_bg+3), 0 );
1446
1447 /* TODO: replace this one day with our own function */
1448 char buf[32];
1449 snprintf( buf, sizeof(buf), format? format: "%.2f", *value );
1450 ui_text( box, buf, 1, k_ui_align_middle_center, 0 );
1451
1452 return state;
1453 }
1454
1455 static void ui_slider( ui_rect inout_panel, const char *str_label,
1456 f32 min, f32 max, f32 *value, const char *format ){
1457 ui_rect rect, label, box;
1458 ui_standard_widget( inout_panel, rect, 1 );
1459 ui_label( rect, str_label, k_ui_scale, 0, box );
1460 _ui_slider( box, min, max, value, format );
1461 }
1462
1463 /*
1464 * Colour picker
1465 * -----------------------------------------------------------------------------
1466 */
1467
1468 static void ui_colourpicker( ui_rect inout_panel, const char *str_label,
1469 v4f value ){
1470 ui_rect widget, left, right;
1471 ui_standard_widget( inout_panel, widget, 8 );
1472 ui_split_ratio( widget, k_ui_axis_v, 0.5f, 8, left, right );
1473
1474 ui_rect sliders[4];
1475 ui_split_ratio( right, k_ui_axis_h, 0.5f, 2, sliders[0], sliders[2] );
1476 ui_split_ratio( sliders[0], k_ui_axis_h, 0.5f, 2, sliders[0], sliders[1] );
1477 ui_split_ratio( sliders[2], k_ui_axis_h, 0.5f, 2, sliders[2], sliders[3] );
1478
1479 v4f hsv;
1480 vg_rgb_hsv( value, hsv );
1481 hsv[3] = value[3];
1482
1483 enum ui_button_state modified = 0x00;
1484
1485 for( u32 i=0; i<4; i ++ ){
1486 modified |= _ui_slider( sliders[i], 0.0f, 1.0f, hsv+i,
1487 (const char *[]){ "hue %.2f",
1488 "sat %.2f",
1489 "lum %.2f",
1490 "alpha %.2f" }[i] );
1491 }
1492
1493 ui_rect preview, square;
1494 ui_split_ratio( left, k_ui_axis_v, 0.8f, 8, square, preview );
1495
1496 u32 state = ui_colourbutton( square, 0, 0, 0, 0 );
1497 modified |= state;
1498
1499 enum ui_button_state
1500 mask_using =
1501 k_ui_button_holding_inside |
1502 k_ui_button_holding_outside |
1503 k_ui_button_click;
1504
1505 if( state & mask_using ){
1506 for( u32 i=0; i<2; i ++ ){
1507 hsv[1+i] = vg_clampf(
1508 (f32)(vg_ui.mouse[i] - square[i]) / (f32)(square[2+i]),
1509 0.0f, 1.0f );
1510 }
1511
1512 hsv[2] = 1.0f-hsv[2];
1513 }
1514
1515 if( modified & (k_ui_button_click|k_ui_button_holding_inside|
1516 k_ui_button_holding_outside) ){
1517 vg_hsv_rgb( hsv, value );
1518 value[3] = hsv[3];
1519 }
1520
1521 ui_outline( square, 1, ui_colour( state? k_ui_fg+3: k_ui_bg+3 ), 0 );
1522
1523 /* preview colour */
1524 v4f colour;
1525 vg_hsv_rgb( hsv, colour );
1526 colour[3] = 1.0f;
1527 ui_fill( preview, v4f_u32_colour( colour ) );
1528
1529 /* Draw hsv shader thingy */
1530 ui_flush( k_ui_shader_colour, vg.window_x, vg.window_y );
1531 ui_fill_rect( square, 0xffffffff, (ui_px[4]){ 0,256,256,0 } );
1532 vg_ui.hue = hsv[0];
1533 ui_flush( k_ui_shader_hsv, vg.window_x, vg.window_y );
1534
1535 ui_rect marker = { square[0] + hsv[1] * (f32)square[2] - 4,
1536 square[1] + (1.0f-hsv[2]) * (f32)square[3] - 4,
1537 8, 8 },
1538 lx = { square[0],
1539 square[1] + (1.0f-hsv[2]) * (f32)square[3],
1540 square[2], 1 },
1541 ly = { square[0] + hsv[1] * (f32)square[2],
1542 square[1],
1543 1, square[3] };
1544
1545 ui_fill( marker, ui_colour( k_ui_fg ) );
1546 ui_fill( lx, ui_colour( k_ui_fg ) );
1547 ui_fill( ly, ui_colour( k_ui_fg ) );
1548 }
1549
1550 /*
1551 * Textbox chaos
1552 * -----------------------------------------------------------------------------
1553 */
1554
1555 static void _ui_textbox_make_selection( int *start, int *end ){
1556 *start = VG_MIN( vg_ui.textbox.cursor_pos, vg_ui.textbox.cursor_user );
1557 *end = VG_MAX( vg_ui.textbox.cursor_pos, vg_ui.textbox.cursor_user );
1558 }
1559
1560 static void _ui_textbox_move_cursor( int *cursor0, int *cursor1,
1561 int dir, int snap_together ){
1562 *cursor0 = VG_MAX( 0, vg_ui.textbox.cursor_user + dir );
1563 *cursor0 =
1564 VG_MIN(
1565 VG_MIN( vg_ui.textbox.len-1, strlen( vg_ui.textbuf )),
1566 *cursor0 );
1567
1568 if( snap_together )
1569 *cursor1 = *cursor0;
1570 }
1571
1572 static int _ui_textbox_makeroom( int datastart, int length ){
1573 int move_to = VG_MIN( datastart+length, vg_ui.textbox.len-1 );
1574 int move_amount = strlen( vg_ui.textbuf )-datastart;
1575 int move_end = VG_MIN( move_to+move_amount, vg_ui.textbox.len-1 );
1576 move_amount = move_end-move_to;
1577
1578 if( move_amount )
1579 memmove( &vg_ui.textbuf[ move_to ],
1580 &vg_ui.textbuf[ datastart ],
1581 move_end-move_to );
1582
1583 vg_ui.textbuf[ move_end ] = '\0';
1584
1585 return VG_MIN( length, vg_ui.textbox.len-datastart-1 );
1586 }
1587
1588 static int _ui_textbox_delete_char( int direction ){
1589 int start, end;
1590 _ui_textbox_make_selection( &start, &end );
1591
1592 /* There is no selection */
1593 if( !(end-start) ){
1594 if( direction == 1 ) end = VG_MIN( end+1, strlen( vg_ui.textbuf ) );
1595 else if( direction == -1 ) start = VG_MAX( start-1, 0 );
1596 }
1597
1598 /* Still no selction, no need to do anything */
1599 if( !(end-start) )
1600 return start;
1601
1602 /* Copy the end->terminator to start */
1603 int remaining_length = strlen( vg_ui.textbuf )+1-end;
1604 memmove( &vg_ui.textbuf[ start ],
1605 &vg_ui.textbuf[ end ],
1606 remaining_length );
1607 return start;
1608 }
1609
1610 static void _ui_textbox_to_clipboard(void){
1611 int start, end;
1612 _ui_textbox_make_selection( &start, &end );
1613 char buffer[512];
1614
1615 if( end-start ){
1616 memcpy( buffer, &vg_ui.textbuf[ start ], end-start );
1617 buffer[ end-start ] = 0x00;
1618 SDL_SetClipboardText( buffer );
1619 }
1620 }
1621
1622 static void _ui_textbox_change_callback(void){
1623 if( vg_ui.textbox.callbacks.change ){
1624 vg_ui.textbox.callbacks.change( vg_ui.textbuf, vg_ui.textbox.len );
1625
1626 /* we gave permission to modify the buffer in this callback so.. */
1627 int len = strlen( vg_ui.textbuf );
1628 vg_ui.textbox.cursor_user = VG_MIN( vg_ui.textbox.cursor_user, len );
1629 vg_ui.textbox.cursor_pos = VG_MIN( vg_ui.textbox.cursor_pos, len );
1630 }
1631 }
1632
1633 static void ui_start_modal( const char *message, u32 options );
1634 static void _ui_textbox_clipboard_paste(void){
1635 if( !SDL_HasClipboardText() )
1636 return;
1637
1638 char *text = SDL_GetClipboardText();
1639
1640 if( !text )
1641 return;
1642
1643 int datastart = _ui_textbox_delete_char( 0 );
1644 int length = strlen( text );
1645
1646 if( (vg_ui.textbox.len - strlen(vg_ui.textbuf)) < length ){
1647 ui_start_modal( "Clipboard content exceeds buffer size.", UI_MODAL_BAD );
1648 return;
1649 }
1650
1651 int cpylength = _ui_textbox_makeroom( datastart, length );
1652
1653 memcpy( vg_ui.textbuf + datastart, text, cpylength);
1654 _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user,
1655 &vg_ui.textbox.cursor_pos, cpylength, 1 );
1656 SDL_free( text );
1657 _ui_textbox_change_callback();
1658 }
1659
1660 static void _ui_textbox_put_char( char c ){
1661 vg_ui.textbox.cursor_user = _ui_textbox_delete_char(0);
1662 if( (vg_ui.textbox.len - strlen(vg_ui.textbuf)) <= 1 ) return;
1663
1664 if( _ui_textbox_makeroom( vg_ui.textbox.cursor_user, 1 ) )
1665 vg_ui.textbuf[ vg_ui.textbox.cursor_user ] = c;
1666
1667 _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user,
1668 &vg_ui.textbox.cursor_pos, 1, 1 );
1669 }
1670
1671 /* Receed secondary cursor */
1672 static void _ui_textbox_left_select(void){
1673 _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user, NULL, -1, 0 );
1674 }
1675
1676 /* Match and receed both cursors */
1677 static void _ui_textbox_left(void){
1678 int cursor_diff = vg_ui.textbox.cursor_pos - vg_ui.textbox.cursor_user? 0: 1;
1679
1680 _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user,
1681 &vg_ui.textbox.cursor_pos, -cursor_diff, 1 );
1682 }
1683
1684 static void _ui_textbox_up(void){
1685 if( vg_ui.textbox.flags & UI_TEXTBOX_MULTILINE ){
1686 int line_begin = vg_ui.textbox.cursor_user;
1687
1688 while( line_begin ){
1689 if( vg_ui.textbuf[ line_begin-1 ] == '\n' ){
1690 break;
1691 }
1692
1693 line_begin --;
1694 }
1695
1696 if( line_begin ){
1697 int line_above_begin = line_begin-1;
1698
1699 while( line_above_begin ){
1700 if( vg_ui.textbuf[ line_above_begin-1 ] == '\n' ){
1701 break;
1702 }
1703
1704 line_above_begin --;
1705 }
1706
1707 int offset = vg_ui.textbox.cursor_user - line_begin,
1708 line_length_above = line_begin - line_above_begin -1;
1709
1710 offset = VG_MIN( line_length_above, offset );
1711
1712 vg_ui.textbox.cursor_user = line_above_begin+offset;
1713 vg_ui.textbox.cursor_pos = line_above_begin+offset;
1714 }
1715 else{
1716 vg_ui.textbox.cursor_user = line_begin;
1717 vg_ui.textbox.cursor_pos = line_begin;
1718 }
1719 }
1720 else{
1721 if( vg_ui.textbox.callbacks.up ){
1722 vg_ui.textbox.callbacks.up( vg_ui.textbuf, vg_ui.textbox.len );
1723 }
1724 }
1725 }
1726
1727 static void _ui_textbox_down(void){
1728 if( vg_ui.textbox.flags & UI_TEXTBOX_MULTILINE ){
1729 int line_begin = vg_ui.textbox.cursor_user;
1730
1731 while( line_begin ){
1732 if( vg_ui.textbuf[ line_begin-1 ] == '\n' ){
1733 break;
1734 }
1735
1736 line_begin --;
1737 }
1738
1739 int line_below_begin = vg_ui.textbox.cursor_user;
1740
1741 while(1){
1742 if( vg_ui.textbuf[ line_below_begin ] == '\0' ){
1743 vg_ui.textbox.cursor_user = line_below_begin;
1744 vg_ui.textbox.cursor_pos = line_below_begin;
1745 return;
1746 }
1747
1748 if( vg_ui.textbuf[ line_below_begin ] == '\n' ){
1749 line_below_begin ++;
1750 break;
1751 }
1752
1753 line_below_begin ++;
1754 }
1755
1756 int line_below_end = line_below_begin;
1757 while(1){
1758 if( vg_ui.textbuf[ line_below_end ] == '\0' ||
1759 vg_ui.textbuf[ line_below_end ] == '\n' ){
1760 line_below_end ++;
1761 break;
1762 }
1763 line_below_end ++;
1764 }
1765
1766 int offset = vg_ui.textbox.cursor_user - line_begin,
1767 line_length_below = line_below_end - line_below_begin -1;
1768
1769 offset = VG_MIN( line_length_below, offset );
1770
1771 vg_ui.textbox.cursor_user = line_below_begin+offset;
1772 vg_ui.textbox.cursor_pos = line_below_begin+offset;
1773 }
1774 else{
1775 if( vg_ui.textbox.callbacks.down ){
1776 vg_ui.textbox.callbacks.down( vg_ui.textbuf, vg_ui.textbox.len );
1777 }
1778 }
1779 }
1780
1781 static void _ui_textbox_right_select(void){
1782 _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user, NULL, 1, 0 );
1783 }
1784
1785 static void _ui_textbox_right(void){
1786 int cursor_diff = vg_ui.textbox.cursor_pos - vg_ui.textbox.cursor_user? 0: 1;
1787
1788 _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user,
1789 &vg_ui.textbox.cursor_pos, +cursor_diff, 1 );
1790 }
1791
1792 static void _ui_textbox_backspace(void){
1793 if( vg_ui.focused_control_type == k_ui_control_textbox ){
1794 vg_ui.textbox.cursor_user = _ui_textbox_delete_char( -1 );
1795 vg_ui.textbox.cursor_pos = vg_ui.textbox.cursor_user;
1796 _ui_textbox_change_callback();
1797 }
1798 }
1799
1800 static void _ui_textbox_delete(void){
1801 if( vg_ui.focused_control_type == k_ui_control_textbox ){
1802 vg_ui.textbox.cursor_user = _ui_textbox_delete_char( 1 );
1803 vg_ui.textbox.cursor_pos = vg_ui.textbox.cursor_user;
1804 _ui_textbox_change_callback();
1805 }
1806 }
1807
1808 static void _ui_textbox_home_select(void){
1809 i32 start = vg_ui.textbox.cursor_user;
1810
1811 if( vg_ui.textbox.flags & UI_TEXTBOX_MULTILINE ){
1812 while( start ){
1813 if( vg_ui.textbuf[start-1] == '\n' )
1814 break;
1815 else
1816 start --;
1817 }
1818 }
1819 else
1820 start = 0;
1821
1822 vg_ui.textbox.cursor_user = start;
1823 }
1824
1825 static void _ui_textbox_home(void){
1826 _ui_textbox_home_select();
1827 vg_ui.textbox.cursor_pos = vg_ui.textbox.cursor_user;
1828 }
1829
1830 static void _ui_textbox_end_select(void){
1831 i32 end = vg_ui.textbox.cursor_user;
1832
1833 if( vg_ui.textbox.flags & UI_TEXTBOX_MULTILINE ){
1834 while( vg_ui.textbuf[end] ){
1835 if( vg_ui.textbuf[end] == '\n' )
1836 break;
1837 else
1838 end ++;
1839 }
1840 }
1841 else
1842 end = VG_MIN( vg_ui.textbox.len-1, strlen(vg_ui.textbuf) );
1843
1844 vg_ui.textbox.cursor_user = end;
1845 }
1846
1847 static void _ui_textbox_end(void){
1848 _ui_textbox_end_select();
1849 vg_ui.textbox.cursor_pos = vg_ui.textbox.cursor_user;
1850 }
1851
1852 static void _ui_textbox_select_all(void){
1853 _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user, NULL, 10000, 0);
1854 _ui_textbox_move_cursor( &vg_ui.textbox.cursor_pos, NULL, -10000, 0);
1855 }
1856
1857 static void _ui_textbox_cut(void){
1858 _ui_textbox_to_clipboard();
1859 vg_ui.textbox.cursor_user = _ui_textbox_delete_char(0);
1860 vg_ui.textbox.cursor_pos = vg_ui.textbox.cursor_user;
1861 _ui_textbox_change_callback();
1862 }
1863
1864 static void _ui_textbox_enter(void){
1865 if( vg_ui.focused_control_type == k_ui_control_textbox ){
1866 vg_ui.ignore_input_frames = 2;
1867
1868 if( vg_ui.textbox.callbacks.enter )
1869 vg_ui.textbox.callbacks.enter( vg_ui.textbuf, vg_ui.textbox.len );
1870
1871 if( vg_ui.focused_control_type != k_ui_control_textbox ) return;
1872
1873 if( vg_ui.textbox.flags & UI_TEXTBOX_MULTILINE ){
1874 _ui_textbox_put_char( '\n' );
1875 _ui_textbox_change_callback();
1876 }
1877 else{
1878 if( !(vg_ui.textbox.flags & UI_TEXTBOX_AUTOFOCUS ) )
1879 ui_defocus_all();
1880 }
1881 }
1882 }
1883
1884 /*
1885 * based on a visual character coordinate relative to the anchor of the textbox,
1886 * this works out the linear place in the buffer that coordinate maps to
1887 *
1888 * input coordinates go in co[0], co[1], and the result index is in co[2]
1889 */
1890 static void _ui_textbox_calc_index_from_grid( int co[3], int wrap_length ){
1891 int i[3] = {0,0,0};
1892
1893 char c;
1894 while( (c = vg_ui.textbuf[i[2]]) ){
1895 if( i[1]==co[1] && i[0]>=co[0] ) break;
1896
1897 if( i[0] >= wrap_length ){
1898 i[1] ++;
1899 i[0] = 0;
1900 }
1901
1902 if( c >= 32 && c <= 126 ){
1903 i[0] ++;
1904 i[2] ++;
1905 }
1906 else if( c == '\n' ){
1907 i[1] ++;
1908
1909 if( i[1] > co[1] ) break;
1910
1911 i[2] ++;
1912 i[0] = 0;
1913 }
1914 else i[2] ++;
1915 }
1916
1917 co[0] = i[0];
1918 co[1] = i[1];
1919 co[2] = i[2];
1920 }
1921
1922 /*
1923 * based on the index specied in co[2], work out the visual character
1924 * coordinates and store them in co[0], co[1]
1925 */
1926 static void _ui_textbox_index_calc_coords( int co[3], int wrap_length ){
1927 co[0] = 0;
1928 co[1] = 0;
1929
1930 char c;
1931 int i=0;
1932
1933 while( (c = vg_ui.textbuf[i ++]) ){
1934 if( i > co[2] ) break;
1935 if( co[0] >= wrap_length ){
1936 co[1] ++;
1937 co[0] = 0;
1938 }
1939 if( c >= 32 && c <= 126 ) co[0] ++;
1940 else if( c == '\n' ){
1941 co[1] ++;
1942 co[0] = 0;
1943 }
1944 }
1945 }
1946
1947 /*
1948 * calculate the number of characters remaining until either:
1949 * - the wrap_length limit is hit
1950 * - end of the line/string
1951 *
1952 * index must be fully populated with visual X/Y, and linear index
1953 */
1954 static int _ui_textbox_run_remaining( int index[3], int wrap_length ){
1955 int i=0, printed_chars=0;
1956 char c;
1957 while( (c = vg_ui.textbuf[index[2] + (i ++)]) ){
1958 if( index[0]+i >= wrap_length ) break;
1959 if( c >= 32 && c <= 126 ) printed_chars ++;
1960 else if( c == '\n' ) break;
1961 }
1962
1963 return printed_chars+1;
1964 }
1965
1966 static int ui_textbox( ui_rect inout_panel, const char *label,
1967 char *buf, u32 len, u32 lines, u32 flags,
1968 struct ui_textbox_callbacks *callbacks ){
1969 assert( lines > 0 );
1970 assert( buf );
1971
1972 if( lines > 1 ) flags |= UI_TEXTBOX_MULTILINE;
1973
1974 ui_rect rect;
1975 ui_standard_widget( inout_panel, rect, lines );
1976
1977 if( label )
1978 ui_label( rect, label, 1, 0, rect );
1979
1980 int clickup= ui_click_up(UI_MOUSE_LEFT),
1981 clickdown = ui_click_down(UI_MOUSE_LEFT),
1982 click = ui_clicking(UI_MOUSE_LEFT) | clickup,
1983 target = ui_inside_rect( rect, vg_ui.mouse_click ) && click,
1984 hover = ui_inside_rect( rect, vg_ui.mouse );
1985
1986 /* allow instant transitions from textbox->textbox */
1987 if( (vg_ui.focused_control_type != k_ui_control_none) &&
1988 (vg_ui.focused_control_type != k_ui_control_textbox) ){
1989 clickup = 0;
1990 clickdown = 0;
1991 click = 0;
1992 target = 0;
1993 hover = 0;
1994 flags &= ~UI_TEXTBOX_AUTOFOCUS;
1995 }
1996
1997 u32 col_base = ui_colour( k_ui_bg ),
1998 col_highlight = ui_colour( k_ui_fg ),
1999 col_cursor = (0x00ffffff & ui_colour(k_ui_fg))|0x7f000000;
2000
2001 ui_px border = -1;
2002
2003 ui_rect text_rect;
2004 rect_copy( rect, text_rect );
2005
2006 if( flags & UI_TEXTBOX_MULTILINE ) text_rect[3] = rect[3]-16;
2007 else text_rect[3] = vg_ui.font->line_height;
2008
2009 text_rect[2] -= 16;
2010 ui_rect_center( rect, text_rect );
2011
2012 ui_px wrap_length = 1024;
2013
2014 if( flags & UI_TEXTBOX_WRAP )
2015 wrap_length = text_rect[2] / vg_ui.font->spacing;
2016
2017 if( hover ){
2018 vg_ui.cursor = k_ui_cursor_ibeam;
2019 }
2020
2021 if( vg_ui.focused_control_id == buf ){
2022 ui_fill( rect, col_base );
2023 ui_ntext( text_rect, buf, wrap_length, 1, k_ui_align_left, 0 );
2024
2025 if( !(flags & UI_TEXTBOX_AUTOFOCUS) && ((clickup||clickdown) && !target)){
2026 ui_defocus_all();
2027 }
2028 else{
2029 vg_ui.focused_control_hit = 1;
2030 if( click && target ){
2031 int p0[3] ={
2032 (vg_ui.mouse_click[0] - text_rect[0]) / vg_ui.font->spacing,
2033 (vg_ui.mouse_click[1] - text_rect[1]) / vg_ui.font->line_height,
2034 -1
2035 },
2036 p1[3] = {
2037 (vg_ui.mouse[0] - text_rect[0]) / vg_ui.font->spacing,
2038 (vg_ui.mouse[1] - text_rect[1]) / vg_ui.font->line_height,
2039 -1
2040 };
2041
2042 if( flags & UI_TEXTBOX_MULTILINE ){
2043 _ui_textbox_calc_index_from_grid( p0, wrap_length );
2044 _ui_textbox_calc_index_from_grid( p1, wrap_length );
2045
2046 vg_ui.textbox.cursor_pos = p0[2];
2047 vg_ui.textbox.cursor_user = p1[2];
2048 }
2049 else{
2050 int max = strlen( buf );
2051 vg_ui.textbox.cursor_pos = VG_MAX( 0, VG_MIN( max, p0[0] )),
2052 vg_ui.textbox.cursor_user = VG_MAX( 0, VG_MIN( max, p1[0] ));
2053 }
2054 }
2055
2056 ui_outline( rect, -2, vg_ui.scheme[ k_ui_orange ], 0 );
2057
2058 ui_rect cursor;
2059
2060 int c0 = vg_ui.textbox.cursor_pos,
2061 c1 = vg_ui.textbox.cursor_user,
2062 start = VG_MIN( c0, c1 ),
2063 end = VG_MAX( c0, c1 ),
2064 chars = end-start;
2065
2066 if( flags & (UI_TEXTBOX_WRAP|UI_TEXTBOX_MULTILINE) ){
2067 int pos[3], remaining = chars;
2068
2069 pos[2] = start;
2070 _ui_textbox_index_calc_coords( pos, wrap_length );
2071
2072 if( start==end ){
2073 cursor[0] = text_rect[0] + pos[0]*vg_ui.font->spacing-1;
2074 cursor[1] = text_rect[1] + pos[1]*14;
2075 cursor[2] = 2;
2076 cursor[3] = 13;
2077 ui_fill( cursor, col_cursor );
2078 rect_copy( cursor, vg_ui.click_fader_end );
2079 }
2080 else{
2081 while( remaining ){
2082 int run = _ui_textbox_run_remaining( pos, wrap_length );
2083 run = VG_MIN( run, remaining );
2084
2085 cursor[0] = text_rect[0] + pos[0]*vg_ui.font->spacing-1;
2086 cursor[1] = text_rect[1] + pos[1]*14;
2087 cursor[2] = (float)(run)*(float)vg_ui.font->spacing;
2088 cursor[3] = 13;
2089
2090 ui_fill( cursor, col_cursor );
2091
2092 remaining -= run;
2093 pos[0] = 0;
2094 pos[1] ++;
2095 pos[2] += run;
2096 }
2097 rect_copy( cursor, vg_ui.click_fader_end );
2098 }
2099 }
2100 else{
2101 cursor[0] = text_rect[0] + start*vg_ui.font->spacing-1;
2102 cursor[1] = text_rect[1];
2103 cursor[3] = 13;
2104
2105 if( start==end ){
2106 cursor[2] = 2;
2107 }
2108 else{
2109 cursor[2] = (float)(chars)*(float)vg_ui.font->spacing;
2110 }
2111
2112 if( (vg_ui.click_fade_opacity<=0.0f) &&
2113 ui_clip( rect, cursor, cursor ) ){
2114 ui_fill( cursor, col_cursor );
2115 }
2116
2117 rect_copy( cursor, vg_ui.click_fader_end );
2118 }
2119 }
2120
2121 return 0;
2122 }
2123
2124 if( click || (flags & UI_TEXTBOX_AUTOFOCUS) ){
2125 if( (target && hover) || (flags & UI_TEXTBOX_AUTOFOCUS) ){
2126 ui_defocus_all();
2127
2128 ui_fill( rect, col_highlight );
2129 vg_ui.ignore_input_frames = 2;
2130 rect_copy( rect, vg_ui.click_fader );
2131 rect_copy( rect, vg_ui.click_fader_end );
2132
2133 vg_ui.click_fade_opacity = 1.0f;
2134 vg_ui.textbuf = buf;
2135 vg_ui.focused_control_hit = 1;
2136 vg_ui.focused_control_type = k_ui_control_textbox;
2137 vg_ui.textbox.len = len;
2138 vg_ui.textbox.flags = flags;
2139 vg_ui.textbox.cursor_pos = 0;
2140 vg_ui.textbox.cursor_user = 0;
2141
2142 if( callbacks ){
2143 vg_ui.textbox.callbacks = *callbacks;
2144 }
2145 else{
2146 vg_ui.textbox.callbacks.change = NULL;
2147 vg_ui.textbox.callbacks.down = NULL;
2148 vg_ui.textbox.callbacks.up = NULL;
2149 vg_ui.textbox.callbacks.enter = NULL;
2150 }
2151
2152 SDL_StartTextInput();
2153 }
2154 }
2155
2156 ui_fill( rect, col_base );
2157
2158 if( hover ){
2159 ui_outline( rect, -1, col_highlight, 0 );
2160 }
2161
2162 ui_ntext( text_rect, buf, wrap_length, 1, k_ui_align_left, 0 );
2163 return 0;
2164 }
2165
2166 /*
2167 * Tabs
2168 * -----------------------------------------------------------------------------
2169 */
2170
2171 static void ui_tabs( ui_rect inout_panel, ui_rect out_content_panel,
2172 const char **titles, u32 count, i32 *page ){
2173 ui_rect bar;
2174 ui_standard_widget( inout_panel, bar, 1 );
2175
2176 i32 cur_page = *page;
2177
2178 f32 width = (f32)inout_panel[2] / (f32)count;
2179
2180 ui_px h = (inout_panel[1] + inout_panel[3]) - (bar[1]+bar[3]);
2181 inout_panel[1] = bar[1]+bar[3];
2182 inout_panel[3] = h;
2183
2184 ui_fill( inout_panel, ui_colour( k_ui_bg+2 ) );
2185 ui_outline( inout_panel, 1, ui_colour( k_ui_bg+5 ), 0 );
2186
2187 rect_copy( inout_panel, out_content_panel );
2188 ui_rect_pad( out_content_panel, (ui_px[2]){ k_ui_padding, k_ui_padding } );
2189
2190 /* place buttons */
2191 for( i32 i=0; i<count; i++ ){
2192 ui_rect button = {
2193 bar[0] + ((f32)i*width),
2194 bar[1],
2195 width,
2196 bar[3]-1
2197 };
2198
2199 enum ui_scheme_colour colour = k_ui_bg+4;
2200 if( i == cur_page ){
2201 colour = k_ui_bg+2;
2202 ui_outline( button, 1, ui_colour( k_ui_bg+5 ),
2203 UI_TOP|UI_LEFT|UI_RIGHT );
2204 button[3] ++;
2205 }
2206
2207 if( ui_colourbutton_text( button, titles[i], 1, colour ) == 1 )
2208 *page = i;
2209 }
2210 }
2211
2212 /*
2213 * Modal UI
2214 * -----------------------------------------------------------------------------
2215 */
2216 static void ui_start_modal( const char *message, u32 options ){
2217 vg_ui.focused_control_type = k_ui_control_modal;
2218 vg_ui.modal.message = message;
2219 vg_ui.modal.callbacks.close = NULL;
2220 vg_ui.modal.options = options;
2221
2222 u32 type = options & UI_MODAL_TYPE_BITS;
2223 if( type == UI_MODAL_OK ) vg_info( message );
2224 else if( type == UI_MODAL_WARN ) vg_warn( message );
2225 else if( type == UI_MODAL_GOOD ) vg_success( message );
2226 else if( type == UI_MODAL_BAD ) vg_error( message );
2227 }
2228
2229 /*
2230 * Input handling
2231 * -----------------------------------------------------------------------------
2232 */
2233
2234 /*
2235 * Handles binds
2236 */
2237 static void _ui_proc_key( SDL_Keysym ev ){
2238 if( vg_ui.focused_control_type != k_ui_control_textbox ){
2239 return;
2240 }
2241
2242 struct textbox_mapping{
2243 u16 mod;
2244 SDL_Keycode key;
2245
2246 void (*handler)(void);
2247 }
2248 mappings[] =
2249 {
2250 { 0, SDLK_LEFT, _ui_textbox_left },
2251 { KMOD_SHIFT, SDLK_LEFT, _ui_textbox_left_select },
2252 { 0, SDLK_RIGHT, _ui_textbox_right },
2253 { KMOD_SHIFT, SDLK_RIGHT, _ui_textbox_right_select },
2254 { 0, SDLK_DOWN, _ui_textbox_down },
2255 { 0, SDLK_UP, _ui_textbox_up },
2256 { 0, SDLK_BACKSPACE, _ui_textbox_backspace },
2257 { KMOD_SHIFT, SDLK_BACKSPACE, _ui_textbox_backspace },
2258 { KMOD_CTRL, SDLK_BACKSPACE, _ui_textbox_backspace },
2259 { 0, SDLK_DELETE, _ui_textbox_delete },
2260 { 0, SDLK_HOME, _ui_textbox_home },
2261 { KMOD_SHIFT, SDLK_HOME, _ui_textbox_home_select },
2262 { 0, SDLK_END, _ui_textbox_end },
2263 { KMOD_SHIFT, SDLK_END, _ui_textbox_end_select },
2264 { KMOD_CTRL, SDLK_a, _ui_textbox_select_all },
2265 { KMOD_CTRL, SDLK_c, _ui_textbox_to_clipboard },
2266 { KMOD_CTRL, SDLK_x, _ui_textbox_cut },
2267 { KMOD_CTRL, SDLK_v, _ui_textbox_clipboard_paste },
2268 { 0, SDLK_RETURN, _ui_textbox_enter },
2269 { 0, SDLK_ESCAPE, ui_defocus_all },
2270 };
2271
2272 SDL_Keymod mod = 0;
2273
2274 if( ev.mod & KMOD_SHIFT )
2275 mod |= KMOD_SHIFT;
2276
2277 if( ev.mod & KMOD_CTRL )
2278 mod |= KMOD_CTRL;
2279
2280 if( ev.mod & KMOD_ALT )
2281 mod |= KMOD_ALT;
2282
2283 for( int i=0; i<vg_list_size( mappings ); i++ ){
2284 struct textbox_mapping *mapping = &mappings[i];
2285
2286 if( mapping->key == ev.sym ){
2287 if( mapping->mod == 0 ){
2288 if( mod == 0 ){
2289 mapping->handler();
2290 return;
2291 }
2292 }
2293 else if( (mod & mapping->mod) == mapping->mod ){
2294 mapping->handler();
2295 return;
2296 }
2297 }
2298 }
2299 }
2300
2301 /*
2302 * Callback for text entry mode
2303 */
2304 static void ui_proc_utf8( const char *text ){
2305 if( vg_ui.focused_control_type == k_ui_control_textbox ){
2306 const char *ptr = text;
2307
2308 while( *ptr ){
2309 if( *ptr != '`' ) _ui_textbox_put_char( *ptr );
2310 ptr ++;
2311 }
2312
2313 _ui_textbox_change_callback();
2314 }
2315 }
2316
2317 /*
2318 * Development utils
2319 * -----------------------------------------------------------------------------
2320 */
2321
2322 static void ui_dev_colourview(void){
2323 ui_rect window = {vg.window_x-256,0,256,vg.window_y}, swatch;
2324
2325 const char *names[vg_list_size(vg_ui.scheme)] = {
2326 [k_ui_bg] = "k_ui_bg", "k_ui_bg+1", "k_ui_bg+2", "k_ui_bg+3",
2327 "k_ui_bg+4", "k_ui_bg+5", "k_ui_bg+6", "k_ui_bg+7",
2328
2329 [k_ui_fg] = "k_ui_fg", "k_ui_fg+1", "k_ui_fg+2", "k_ui_fg+3",
2330 "k_ui_fg+4", "k_ui_fg+5", "k_ui_fg+6", "k_ui_fg+7",
2331
2332 [k_ui_red] = "k_ui_red", "k_ui_orange", "k_ui_yellow", "k_ui_green",
2333 "k_ui_aqua", "k_ui_blue", "k_ui_purple", "k_ui_gray",
2334 "k_ui_red+8","k_ui_orange+8","k_ui_yellow+8","k_ui_green+8",
2335 "k_ui_aqua+8","k_ui_blue+8","k_ui_purple+8","k_ui_gray+8" };
2336
2337 ui_rect col[2];
2338 ui_split_ratio( window, k_ui_axis_v, 0.5f, 0, col[0], col[1] );
2339
2340 for( int i=0; i<vg_list_size(vg_ui.scheme); i++ ){
2341 int which = (i/8)%2;
2342
2343 ui_split( col[which], k_ui_axis_h, 24, 0, swatch, col[which] );
2344 ui_fill( swatch, ui_colour(i) );
2345
2346 if( names[i] )
2347 ui_text(swatch, names[i], 1, k_ui_align_middle_left, ui_colourcont(i));
2348 }
2349 }
2350
2351 #endif /* VG_IMGUI_H */