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