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