revision 2
[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_prerender(void)
708 {
709 int x, y;
710 vg_ui.mouse_state[1] = vg_ui.mouse_state[0];
711 vg_ui.mouse_state[0] = SDL_GetMouseState( &x, &y );
712 vg_ui.mouse_delta[0] = x-vg_ui.mouse[0];
713 vg_ui.mouse_delta[1] = y-vg_ui.mouse[1];
714 vg_ui.mouse[0] = x;
715 vg_ui.mouse[1] = y;
716
717 vg_ui.cur_vert = 0;
718 vg_ui.cur_indice = 0;
719 vg_ui.vert_start = 0;
720 vg_ui.indice_start = 0;
721 vg_ui.focused_control_hit = 0;
722 vg_ui.cursor = k_ui_cursor_default;
723 vg_ui.wants_mouse = 0;
724
725 if( vg_ui.ignore_input_frames ){
726 vg_ui.ignore_input_frames --;
727 return;
728 }
729
730 if( ui_click_down(UI_MOUSE_LEFT)||ui_click_down(UI_MOUSE_MIDDLE) ){
731 vg_ui.mouse_click[0] = vg_ui.mouse[0];
732 vg_ui.mouse_click[1] = vg_ui.mouse[1];
733 }
734 }
735
736 u32 ui_colour( enum ui_scheme_colour id )
737 {
738 return vg_ui.scheme[ id ];
739 }
740
741 /* get an appropriately contrasting colour given the base */
742 u32 ui_colourcont( enum ui_scheme_colour id )
743 {
744 if ( id < k_ui_bg+6 ) return ui_colour( k_ui_fg );
745 else if( id < k_ui_fg ) return ui_colour( k_ui_bg+1 );
746 else if( id < k_ui_hue ) return ui_colour( k_ui_bg+3 );
747 else if( id < k_ui_red+k_ui_brighter ) return ui_colour( k_ui_fg );
748 else return ui_colour( k_ui_fg+1 );
749 }
750
751 void ui_hex_to_norm( u32 hex, v4f norm )
752 {
753 norm[0] = ((hex ) & 0xff);
754 norm[1] = ((hex>>8 ) & 0xff);
755 norm[2] = ((hex>>16) & 0xff);
756 norm[3] = ((hex>>24) & 0xff);
757 v4_muls( norm, 1.0f/255.0f, norm );
758 }
759
760 u32 v4f_u32_colour( v4f colour )
761 {
762 u32 r = colour[0] * 255.0f,
763 g = colour[1] * 255.0f,
764 b = colour[2] * 255.0f,
765 a = colour[3] * 255.0f;
766
767 return r | (g<<8) | (b<<16) | (a<<24);
768 }
769
770 static void ui_text_glyph( const struct ui_font *font, ui_px scale,
771 u8 glyph, ui_rect out_texcoords ){
772 glyph -= font->ascii_start;
773
774 ui_px per_row = font->sheet_size / font->glyph_width,
775 column = (ui_px)glyph % per_row,
776 row = (glyph - column) / per_row;
777
778 out_texcoords[0] = column * font->glyph_width;
779 out_texcoords[1] = row * font->glyph_height + font->offset_y;
780 out_texcoords[2] = out_texcoords[0] + font->glyph_width;
781 out_texcoords[3] = out_texcoords[1] + font->glyph_height;
782 }
783
784 u32 ui_opacity( u32 colour, f32 opacity )
785 {
786 u32 alpha = opacity * 255.0f;
787 return (colour & 0x00ffffff) | (alpha << 24);
788 }
789
790 u32 ui_ntext( ui_rect rect, const char *str, u32 len, ui_px scale,
791 enum ui_align align, u32 colour )
792 {
793 ui_rect text_cursor;
794 if( colour == 0 ) colour = ui_colour( k_ui_fg );
795
796 colour &= 0x00ffffff;
797
798 const char *_c = str;
799 u8 c;
800
801 text_cursor[0] = ui_text_aligned_x( str, rect, scale, align );
802 text_cursor[1] = rect[1];
803 text_cursor[2] = vg_ui.font->glyph_width*scale;
804 text_cursor[3] = vg_ui.font->glyph_height*scale;
805
806 u32 printed_chars = 0;
807
808 if( align & (k_ui_align_middle|k_ui_align_bottom) ){
809 ui_px height = ui_text_string_height( str ) * scale;
810
811 if( align & k_ui_align_bottom )
812 text_cursor[1] += rect[3]-height;
813 else
814 text_cursor[1] += (rect[3]-height)/2;
815 }
816
817 while( (c = *(_c ++)) ){
818 if( printed_chars >= len ){
819 printed_chars = 0;
820 text_cursor[1] += vg_ui.font->line_height*scale;
821 text_cursor[0] = ui_text_aligned_x( _c, rect, scale, align );
822 text_cursor[0] -= vg_ui.font->spacing*scale;
823
824 ui_rect glyph;
825 ui_text_glyph( vg_ui.font, scale, '\xb6' /*FIXME*/, glyph );
826 ui_fill_rect( text_cursor, 0x00ffffff, glyph );
827 text_cursor[0] += vg_ui.font->spacing*scale;
828 }
829
830 if( c == '\n' ){
831 text_cursor[1] += vg_ui.font->line_height*scale;
832 text_cursor[0] = ui_text_aligned_x( _c, rect, scale, align );
833 printed_chars = 0;
834 continue;
835 }
836 else if( c >= 33 ){
837 ui_rect glyph;
838 ui_text_glyph( vg_ui.font, scale, c, glyph );
839
840 ui_rect cursor_clipped;
841 if( ui_clip( rect, text_cursor, cursor_clipped ) ){
842 ui_fill_rect( cursor_clipped, colour, glyph );
843 }
844 }
845 else if( c == '\x1B' ){
846 /* vt codes */
847 _c ++;
848 u16 colour_id = 0;
849 for( int i=0; i<3; i ++ ){
850 if( _c[i] ){
851 if( _c[i] == 'm' ){
852 _c = _c + i + 1;
853
854 switch( colour_id ){
855 case '0': colour = ui_colour( k_ui_fg ); break;
856 case '3'|'0'<<8: colour = ui_colour( k_ui_bg ); break;
857 case '3'|'1'<<8: colour = ui_colour( k_ui_red ); break;
858 case '3'|'2'<<8: colour = ui_colour( k_ui_green ); break;
859 case '3'|'3'<<8: colour = ui_colour( k_ui_yellow ); break;
860 case '3'|'4'<<8: colour = ui_colour( k_ui_blue ); break;
861 case '3'|'5'<<8: colour = ui_colour( k_ui_purple ); break;
862 case '3'|'6'<<8: colour = ui_colour( k_ui_aqua ); break;
863 case '3'|'7'<<8: colour = 0xffffffff; break;
864 }
865
866 colour &= 0x00ffffff;
867 break;
868 }
869
870 colour_id |= _c[i] << (i*8);
871 }
872 else{
873 _c = _c +i;
874 break;
875 }
876 }
877
878 continue;
879 }
880 else if( c == '\t' ){
881 text_cursor[0] += vg_ui.font->spacing*scale*4;
882 printed_chars += 4;
883 continue;
884 }
885
886 text_cursor[0] += vg_ui.font->spacing*scale;
887 printed_chars ++;
888 }
889
890 return printed_chars;
891 }
892
893 void ui_text( ui_rect rect, const char *str, ui_px scale,
894 enum ui_align align, u32 colour )
895 {
896 ui_ntext( rect, str, 1024, scale, align, colour );
897 }
898
899 /*
900 * Standard layout stuff
901 * -----------------------------------------------------------------------------
902 */
903
904 void ui_panel( ui_rect in_rect, ui_rect out_panel )
905 {
906 ui_fill( in_rect, ui_colour( k_ui_bg+1 ) );
907 ui_outline( in_rect, 1, ui_colour( k_ui_bg+7 ), 0 );
908 rect_copy( in_rect, out_panel );
909 ui_rect_pad( out_panel, (ui_px[2]){ k_ui_padding, k_ui_padding } );
910 }
911
912 void ui_label( ui_rect rect, const char *text, ui_px size,
913 ui_px gap, ui_rect r )
914 {
915 ui_rect l;
916 ui_px width = (ui_text_line_width(text)+vg_ui.font->spacing) * size;
917 ui_split( rect, k_ui_axis_v, width, gap, l, r );
918 ui_text( l, text, 1, k_ui_align_middle_left, 0 );
919 }
920
921 void ui_standard_widget( ui_rect inout_panel, ui_rect out_rect, ui_px count )
922 {
923 ui_px height = (count * vg_ui.font->glyph_height + 18) * k_ui_scale;
924 ui_split( inout_panel, k_ui_axis_h, height, k_ui_padding,
925 out_rect, inout_panel );
926 }
927
928 void ui_info( ui_rect inout_panel, const char *text )
929 {
930 ui_rect box;
931 ui_standard_widget( inout_panel, box, 1 );
932 ui_text( box, text, 1, k_ui_align_middle_left, 0 );
933 }
934
935 void ui_image( ui_rect rect, GLuint image )
936 {
937 ui_flush( k_ui_shader_colour, vg.window_x, vg.window_y );
938 glActiveTexture( GL_TEXTURE0 );
939 glBindTexture( GL_TEXTURE_2D, image );
940 ui_fill_rect( rect, 0xffffffff, (ui_px[4]){ 0,256,256,0 } );
941 ui_flush( k_ui_shader_image, vg.window_x, vg.window_y );
942 }
943
944 void ui_defocus_all(void)
945 {
946 if( vg_ui.focused_control_type == k_ui_control_textbox ){
947 SDL_StopTextInput();
948 if( vg_ui.textbox.callbacks.escape )
949 vg_ui.textbox.callbacks.escape();
950 }
951
952 vg_ui.focused_control_id = NULL;
953 vg_ui.focused_control_hit = 0;
954 vg_ui.focused_control_type = k_ui_control_none;
955 }
956
957 /* TODO: split this out into a formatless button and one that auto fills */
958 enum ui_button_state ui_colourbutton( ui_rect rect,
959 enum ui_scheme_colour colour,
960 enum ui_scheme_colour hover_colour,
961 enum ui_scheme_colour hi_colour,
962 bool const fill )
963 {
964 int clickup= ui_click_up(UI_MOUSE_LEFT),
965 click = ui_clicking(UI_MOUSE_LEFT) | clickup,
966 target = ui_inside_rect( rect, vg_ui.mouse_click ) && click,
967 hover = ui_inside_rect( rect, vg_ui.mouse );
968
969 u32 col_base = vg_ui.scheme[ colour ],
970 col_highlight = vg_ui.scheme[ hi_colour? hi_colour: k_ui_fg ],
971 col_hover = vg_ui.scheme[ hover_colour? hover_colour:
972 colour + k_ui_brighter ];
973
974 if( vg_ui.focused_control_type != k_ui_control_none ){
975 clickup = 0;
976 click = 0;
977 target = 0;
978 hover = 0;
979 }
980
981 if( hover ){
982 vg_ui.cursor = k_ui_cursor_hand;
983 }
984
985 if( click ){
986 if( target ){
987 if( hover ){
988 if( clickup ){
989 vg_ui.ignore_input_frames = 2;
990 ui_defocus_all();
991
992 if( fill ) {
993 ui_fill( rect, col_highlight );
994 rect_copy( rect, vg_ui.click_fader );
995 rect_copy( rect, vg_ui.click_fader_end );
996 vg_ui.click_fader_end[3] = 0;
997 ui_rect_center( rect, vg_ui.click_fader_end );
998 vg_ui.click_fade_opacity = 1.0f;
999 }
1000
1001 return k_ui_button_click;
1002 }
1003 else{
1004 if( fill ) ui_fill( rect, col_highlight );
1005 return k_ui_button_holding_inside;
1006 }
1007 }
1008 else{
1009 if( fill ) ui_fill( rect, col_base );
1010 ui_outline( rect, 1, col_highlight, 0 );
1011 return k_ui_button_holding_outside;
1012 }
1013 }
1014 else{
1015 if( fill ) ui_fill( rect, col_base );
1016 return k_ui_button_none;
1017 }
1018 }
1019 else{
1020 if( hover ){
1021 if( fill ) ui_fill( rect, col_hover );
1022 return k_ui_button_hover;
1023 }
1024 else{
1025 if( fill ) ui_fill( rect, col_base );
1026 return k_ui_button_none;
1027 }
1028 }
1029 }
1030
1031 enum ui_button_state ui_colourbutton_text(
1032 ui_rect rect, const char *string, ui_px scale,
1033 enum ui_scheme_colour colour ){
1034 enum ui_button_state state = ui_colourbutton( rect, colour, 0, 0, 1 );
1035 ui_rect t = { 0,0, ui_text_line_width( string )*scale, 14*scale };
1036 ui_rect_center( rect, t );
1037
1038 u32 text_colour = ui_colourcont(colour);
1039 if( state == k_ui_button_holding_inside )
1040 text_colour = colour;
1041
1042 ui_text( t, string, scale, k_ui_align_left, text_colour );
1043 return state;
1044 }
1045
1046 enum ui_button_state ui_button_text( ui_rect rect,
1047 const char *string, ui_px scale )
1048 {
1049 return ui_colourbutton_text( rect, string, scale, k_ui_bg+4 );
1050 }
1051
1052 enum ui_button_state ui_button( ui_rect inout_panel, const char *string )
1053 {
1054 ui_rect rect;
1055 ui_standard_widget( inout_panel, rect, 1 );
1056 return ui_colourbutton_text( rect, string, 1, k_ui_bg+4 );
1057 }
1058
1059 static void ui_enum_post(void);
1060 void ui_postrender(void)
1061 {
1062 if( vg_ui.click_fade_opacity > 0.0f ){
1063 float scale = vg_ui.click_fade_opacity;
1064 scale = vg_maxf( 1.0f/255.0f, scale*scale );
1065
1066 vg_ui.click_fade_opacity -= vg.time_frame_delta * 3.8f;
1067 u32 colour = (0x00ffffff & ui_colour(k_ui_fg))|0x7f000000;
1068
1069 v4f begin, end, dest;
1070 for( int i=0; i<4; i++ ){
1071 begin[i] = vg_ui.click_fader[i];
1072 end[i] = vg_ui.click_fader_end[i]+1;
1073 }
1074
1075 v4_lerp( end, begin, scale, dest );
1076
1077 ui_rect rect;
1078 for( int i=0; i<4; i++ ){
1079 rect[i] = dest[i];
1080 }
1081
1082 ui_fill( rect, colour );
1083 }
1084
1085 if( vg_ui.focused_control_type == k_ui_control_enum ){
1086 ui_enum_post();
1087 }
1088 else if( vg_ui.focused_control_type == k_ui_control_modal ){
1089 ui_rect screen = {0,0,vg.window_x,vg.window_y};
1090 ui_fill( screen, 0xa0000000 );
1091 ui_rect box = {0,0,400,200};
1092
1093 u32 colour = ui_colour(k_ui_fg),
1094 type = vg_ui.modal.options & UI_MODAL_TYPE_BITS;
1095 if ( type == 1 ) colour = ui_colour(k_ui_green);
1096 else if( type == 2 ) colour = ui_colour(k_ui_red);
1097 else if( type == 3 ) colour = ui_colour(k_ui_yellow);
1098
1099 ui_rect_center( screen, box );
1100 ui_fill( box, ui_colour(k_ui_bg) );
1101 ui_outline( box, -1, colour, 0 );
1102
1103 ui_rect message;
1104 rect_copy( box, message );
1105 message[3] = 100;
1106 ui_rect_center( box, message );
1107
1108 ui_rect row0, row1, btn;
1109 ui_split_ratio( message, k_ui_axis_h, 0.5f, 0, row0, row1 );
1110 row0[0] += vg_ui.font->spacing;
1111 ui_ntext( row0, vg_ui.modal.message, (box[2]/vg_ui.font->spacing)-2, 1,
1112 k_ui_align_left, colour );
1113
1114 rect_copy( row1, btn );
1115 btn[2] = 86;
1116 btn[3] = 28;
1117 ui_rect_center( row1, btn );
1118
1119 vg_ui.focused_control_type = k_ui_control_none; /* HACK */
1120 if( ui_button_text( btn, "OK", 1 ) != 1 )
1121 vg_ui.focused_control_hit = 1;
1122 vg_ui.focused_control_type = k_ui_control_modal; /* HACK */
1123 vg_ui.wants_mouse = 1;
1124 }
1125
1126 ui_flush( k_ui_shader_colour, vg.window_x, vg.window_y );
1127
1128 if( !vg_ui.focused_control_hit ){
1129 ui_defocus_all();
1130 }
1131
1132 if( vg_ui.wants_mouse ){
1133 SDL_SetWindowGrab( vg.window, SDL_FALSE );
1134 SDL_SetRelativeMouseMode( SDL_FALSE );
1135 }
1136 else{
1137 SDL_SetWindowGrab( vg.window, SDL_TRUE );
1138 SDL_SetRelativeMouseMode( SDL_TRUE );
1139 }
1140
1141 SDL_SetCursor( vg_ui.cursor_map[ vg_ui.cursor ] );
1142 SDL_ShowCursor(1);
1143 }
1144
1145 /*
1146 * checkbox
1147 * -----------------------------------------------------------------------------
1148 */
1149
1150 int ui_checkbox( ui_rect inout_panel, const char *str_label, i32 *data )
1151 {
1152 ui_rect rect, label, box;
1153 ui_standard_widget( inout_panel, rect, 1 );
1154
1155 ui_split( rect, k_ui_axis_v, -rect[3], 0, label, box );
1156 ui_text( label, str_label, k_ui_scale, k_ui_align_middle_left, 0 );
1157
1158 int changed = ui_colourbutton( box, k_ui_bg, 0, 0, 1 )==1;
1159 if( changed )
1160 *data = (*data) ^ 0x1;
1161
1162 if( *data ){
1163 ui_rect_pad( box, (ui_px[2]){4,4} );
1164 ui_fill( box, ui_colour( k_ui_orange ) );
1165 }
1166
1167 return changed;
1168 }
1169
1170 /*
1171 * Dropdown / Enum
1172 * -----------------------------------------------------------------------------
1173 */
1174
1175 /*
1176 * unfortunately no return value since we only find out that event in the
1177 * postrender step.
1178 */
1179 void ui_enum( ui_rect inout_panel, const char *str_label,
1180 struct ui_enum_opt *options, u32 len, i32 *value )
1181 {
1182 ui_rect rect, label, box;
1183 ui_standard_widget( inout_panel, rect, 1 );
1184 ui_label( rect, str_label, k_ui_scale, 0, box );
1185
1186 const char *display = "OUT OF RANGE";
1187 int valid = 0;
1188 for( u32 i=0; i<len; i ++ ){
1189 if( *value == options[i].value ){
1190 display = options[i].alias;
1191 valid = 1;
1192 break;
1193 }
1194 }
1195
1196 if( ui_button_text( box, display, k_ui_scale ) == 1 ){
1197 vg_ui.focused_control_type = k_ui_control_enum;
1198 vg_ui.ptr_enum = value;
1199 vg_ui._enum.option_count = len;
1200 vg_ui._enum.options = options;
1201 rect_copy( box, vg_ui._enum.rect );
1202 }
1203
1204 if( !valid )
1205 ui_outline( box, 1, ui_colour(k_ui_red), 0 );
1206 }
1207
1208 static void ui_enum_post(void){
1209 ui_rect drawer;
1210 rect_copy( vg_ui._enum.rect, drawer );
1211 drawer[3] *= vg_ui._enum.option_count;
1212
1213 int close = 0;
1214 int clickany= ui_click_up(UI_MOUSE_LEFT|UI_MOUSE_RIGHT|UI_MOUSE_MIDDLE),
1215 hover = ui_inside_rect( drawer, vg_ui.mouse );
1216
1217 if( clickany && !hover ){
1218 return;
1219 }
1220
1221 /* HACK */
1222 vg_ui.focused_control_type = k_ui_control_none;
1223 i32 *value = vg_ui.ptr_enum;
1224
1225 for( u32 i=0; i<vg_ui._enum.option_count; i++ ){
1226 ui_rect button;
1227 ui_split( drawer, k_ui_axis_h, vg_ui._enum.rect[3], 0, button,drawer );
1228
1229 enum ui_scheme_colour colour = k_ui_bg+3;
1230 if( vg_ui._enum.options[i].value == *value )
1231 colour = k_ui_orange;
1232
1233 if( ui_colourbutton_text( button, vg_ui._enum.options[i].alias,
1234 k_ui_scale, colour ) == 1 ){
1235 *value = vg_ui._enum.options[i].value;
1236 close = 1;
1237 }
1238 }
1239
1240 /* HACK */
1241 vg_ui.focused_control_type = k_ui_control_enum;
1242
1243 if( !close )
1244 vg_ui.focused_control_hit = 1;
1245 }
1246
1247 /*
1248 * Slider
1249 * -----------------------------------------------------------------------------
1250 */
1251
1252 static enum ui_button_state _ui_slider(
1253 ui_rect box, f32 min, f32 max, f32 *value, const char *format )
1254 {
1255 f32 t;
1256
1257 enum ui_button_state
1258 mask_using =
1259 k_ui_button_holding_inside |
1260 k_ui_button_holding_outside |
1261 k_ui_button_click,
1262 mask_brighter =
1263 mask_using | k_ui_button_hover,
1264 state = ui_colourbutton( box, k_ui_bg, k_ui_bg+2, k_ui_bg+3, 1 );
1265
1266 if( state & mask_using ){
1267 t = vg_clampf( (f32)(vg_ui.mouse[0] - box[0]) / (f32)( box[2] ),
1268 0.0f, 1.0f );
1269 *value = vg_lerpf( min, max, t );
1270 }
1271 else
1272 t = vg_clampf( (*value - min) / (max-min), 0.0f, 1.0f );
1273
1274 ui_rect line = { box[0], box[1], t * (f32)box[2], box[3] };
1275 ui_fill( line, ui_colour(state&mask_brighter? k_ui_bg+4: k_ui_bg+2) );
1276
1277 ui_outline( box, 1, ui_colour(state? k_ui_fg+3: k_ui_bg+3), 0 );
1278
1279 /* TODO: replace this one day with our own function */
1280 char buf[32];
1281 snprintf( buf, sizeof(buf), format? format: "%.2f", *value );
1282 ui_text( box, buf, 1, k_ui_align_middle_center, 0 );
1283
1284 return state;
1285 }
1286
1287 void ui_slider( ui_rect inout_panel, const char *str_label,
1288 f32 min, f32 max, f32 *value, const char *format )
1289 {
1290 ui_rect rect, label, box;
1291 ui_standard_widget( inout_panel, rect, 1 );
1292 ui_label( rect, str_label, k_ui_scale, 0, box );
1293 _ui_slider( box, min, max, value, format );
1294 }
1295
1296 /*
1297 * Colour picker
1298 * -----------------------------------------------------------------------------
1299 */
1300
1301 void ui_colourpicker( ui_rect inout_panel, const char *str_label, v4f value )
1302 {
1303 ui_rect widget, left, right;
1304 ui_standard_widget( inout_panel, widget, 8 );
1305 ui_split_ratio( widget, k_ui_axis_v, 0.5f, 8, left, right );
1306
1307 ui_rect sliders[4];
1308 ui_split_ratio( right, k_ui_axis_h, 0.5f, 2, sliders[0], sliders[2] );
1309 ui_split_ratio( sliders[0], k_ui_axis_h, 0.5f, 2, sliders[0], sliders[1] );
1310 ui_split_ratio( sliders[2], k_ui_axis_h, 0.5f, 2, sliders[2], sliders[3] );
1311
1312 v4f hsv;
1313 vg_rgb_hsv( value, hsv );
1314 hsv[3] = value[3];
1315
1316 enum ui_button_state modified = 0x00;
1317
1318 for( u32 i=0; i<4; i ++ ){
1319 modified |= _ui_slider( sliders[i], 0.0f, 1.0f, hsv+i,
1320 (const char *[]){ "hue %.2f",
1321 "sat %.2f",
1322 "lum %.2f",
1323 "alpha %.2f" }[i] );
1324 }
1325
1326 ui_rect preview, square;
1327 ui_split_ratio( left, k_ui_axis_v, 0.8f, 8, square, preview );
1328
1329 u32 state = ui_colourbutton( square, 0, 0, 0, 0 );
1330 modified |= state;
1331
1332 enum ui_button_state
1333 mask_using =
1334 k_ui_button_holding_inside |
1335 k_ui_button_holding_outside |
1336 k_ui_button_click;
1337
1338 if( state & mask_using ){
1339 for( u32 i=0; i<2; i ++ ){
1340 hsv[1+i] = vg_clampf(
1341 (f32)(vg_ui.mouse[i] - square[i]) / (f32)(square[2+i]),
1342 0.0f, 1.0f );
1343 }
1344
1345 hsv[2] = 1.0f-hsv[2];
1346 }
1347
1348 if( modified & (k_ui_button_click|k_ui_button_holding_inside|
1349 k_ui_button_holding_outside) ){
1350 vg_hsv_rgb( hsv, value );
1351 value[3] = hsv[3];
1352 }
1353
1354 ui_outline( square, 1, ui_colour( state? k_ui_fg+3: k_ui_bg+3 ), 0 );
1355
1356 /* preview colour */
1357 v4f colour;
1358 vg_hsv_rgb( hsv, colour );
1359 colour[3] = 1.0f;
1360 ui_fill( preview, v4f_u32_colour( colour ) );
1361
1362 /* Draw hsv shader thingy */
1363 ui_flush( k_ui_shader_colour, vg.window_x, vg.window_y );
1364 ui_fill_rect( square, 0xffffffff, (ui_px[4]){ 0,256,256,0 } );
1365 vg_ui.hue = hsv[0];
1366 ui_flush( k_ui_shader_hsv, vg.window_x, vg.window_y );
1367
1368 ui_rect marker = { square[0] + hsv[1] * (f32)square[2] - 4,
1369 square[1] + (1.0f-hsv[2]) * (f32)square[3] - 4,
1370 8, 8 },
1371 lx = { square[0],
1372 square[1] + (1.0f-hsv[2]) * (f32)square[3],
1373 square[2], 1 },
1374 ly = { square[0] + hsv[1] * (f32)square[2],
1375 square[1],
1376 1, square[3] };
1377
1378 ui_fill( marker, ui_colour( k_ui_fg ) );
1379 ui_fill( lx, ui_colour( k_ui_fg ) );
1380 ui_fill( ly, ui_colour( k_ui_fg ) );
1381 }
1382
1383 /*
1384 * Textbox chaos
1385 * -----------------------------------------------------------------------------
1386 */
1387
1388 static void _ui_textbox_make_selection( int *start, int *end ){
1389 *start = VG_MIN( vg_ui.textbox.cursor_pos, vg_ui.textbox.cursor_user );
1390 *end = VG_MAX( vg_ui.textbox.cursor_pos, vg_ui.textbox.cursor_user );
1391 }
1392
1393 void _ui_textbox_move_cursor( int *cursor0, int *cursor1,
1394 int dir, int snap_together )
1395 {
1396 *cursor0 = VG_MAX( 0, vg_ui.textbox.cursor_user + dir );
1397 *cursor0 =
1398 VG_MIN(
1399 VG_MIN( vg_ui.textbox.len-1, strlen( vg_ui.textbuf )),
1400 *cursor0 );
1401
1402 if( snap_together )
1403 *cursor1 = *cursor0;
1404 }
1405
1406 static int _ui_textbox_makeroom( int datastart, int length ){
1407 int move_to = VG_MIN( datastart+length, vg_ui.textbox.len-1 );
1408 int move_amount = strlen( vg_ui.textbuf )-datastart;
1409 int move_end = VG_MIN( move_to+move_amount, vg_ui.textbox.len-1 );
1410 move_amount = move_end-move_to;
1411
1412 if( move_amount )
1413 memmove( &vg_ui.textbuf[ move_to ],
1414 &vg_ui.textbuf[ datastart ],
1415 move_end-move_to );
1416
1417 vg_ui.textbuf[ move_end ] = '\0';
1418
1419 return VG_MIN( length, vg_ui.textbox.len-datastart-1 );
1420 }
1421
1422 int _ui_textbox_delete_char( int direction )
1423 {
1424 int start, end;
1425 _ui_textbox_make_selection( &start, &end );
1426
1427 /* There is no selection */
1428 if( !(end-start) ){
1429 if( direction == 1 ) end = VG_MIN( end+1, strlen( vg_ui.textbuf ) );
1430 else if( direction == -1 ) start = VG_MAX( start-1, 0 );
1431 }
1432
1433 /* Still no selction, no need to do anything */
1434 if( !(end-start) )
1435 return start;
1436
1437 /* Copy the end->terminator to start */
1438 int remaining_length = strlen( vg_ui.textbuf )+1-end;
1439 memmove( &vg_ui.textbuf[ start ],
1440 &vg_ui.textbuf[ end ],
1441 remaining_length );
1442 return start;
1443 }
1444
1445 static void _ui_textbox_to_clipboard(void){
1446 int start, end;
1447 _ui_textbox_make_selection( &start, &end );
1448 char buffer[512];
1449
1450 if( end-start ){
1451 memcpy( buffer, &vg_ui.textbuf[ start ], end-start );
1452 buffer[ end-start ] = 0x00;
1453 SDL_SetClipboardText( buffer );
1454 }
1455 }
1456
1457 static void _ui_textbox_change_callback(void){
1458 if( vg_ui.textbox.callbacks.change ){
1459 vg_ui.textbox.callbacks.change( vg_ui.textbuf, vg_ui.textbox.len );
1460
1461 /* we gave permission to modify the buffer in this callback so.. */
1462 int len = strlen( vg_ui.textbuf );
1463 vg_ui.textbox.cursor_user = VG_MIN( vg_ui.textbox.cursor_user, len );
1464 vg_ui.textbox.cursor_pos = VG_MIN( vg_ui.textbox.cursor_pos, len );
1465 }
1466 }
1467
1468 void ui_start_modal( const char *message, u32 options );
1469 static void _ui_textbox_clipboard_paste(void){
1470 if( !SDL_HasClipboardText() )
1471 return;
1472
1473 char *text = SDL_GetClipboardText();
1474
1475 if( !text )
1476 return;
1477
1478 int datastart = _ui_textbox_delete_char( 0 );
1479 int length = strlen( text );
1480
1481 if( (vg_ui.textbox.len - strlen(vg_ui.textbuf)) < length ){
1482 ui_start_modal( "Clipboard content exceeds buffer size.", UI_MODAL_BAD );
1483 return;
1484 }
1485
1486 int cpylength = _ui_textbox_makeroom( datastart, length );
1487
1488 memcpy( vg_ui.textbuf + datastart, text, cpylength);
1489 _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user,
1490 &vg_ui.textbox.cursor_pos, cpylength, 1 );
1491 SDL_free( text );
1492 _ui_textbox_change_callback();
1493 }
1494
1495 void _ui_textbox_put_char( char c )
1496 {
1497 vg_ui.textbox.cursor_user = _ui_textbox_delete_char(0);
1498 if( (vg_ui.textbox.len - strlen(vg_ui.textbuf)) <= 1 ) return;
1499
1500 if( _ui_textbox_makeroom( vg_ui.textbox.cursor_user, 1 ) )
1501 vg_ui.textbuf[ vg_ui.textbox.cursor_user ] = c;
1502
1503 _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user,
1504 &vg_ui.textbox.cursor_pos, 1, 1 );
1505 }
1506
1507 /* Receed secondary cursor */
1508 void _ui_textbox_left_select(void)
1509 {
1510 _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user, NULL, -1, 0 );
1511 }
1512
1513 /* Match and receed both cursors */
1514 void _ui_textbox_left(void)
1515 {
1516 int cursor_diff = vg_ui.textbox.cursor_pos - vg_ui.textbox.cursor_user? 0: 1;
1517
1518 _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user,
1519 &vg_ui.textbox.cursor_pos, -cursor_diff, 1 );
1520 }
1521
1522 void _ui_textbox_up(void)
1523 {
1524 if( vg_ui.textbox.flags & UI_TEXTBOX_MULTILINE ){
1525 int line_begin = vg_ui.textbox.cursor_user;
1526
1527 while( line_begin ){
1528 if( vg_ui.textbuf[ line_begin-1 ] == '\n' ){
1529 break;
1530 }
1531
1532 line_begin --;
1533 }
1534
1535 if( line_begin ){
1536 int line_above_begin = line_begin-1;
1537
1538 while( line_above_begin ){
1539 if( vg_ui.textbuf[ line_above_begin-1 ] == '\n' ){
1540 break;
1541 }
1542
1543 line_above_begin --;
1544 }
1545
1546 int offset = vg_ui.textbox.cursor_user - line_begin,
1547 line_length_above = line_begin - line_above_begin -1;
1548
1549 offset = VG_MIN( line_length_above, offset );
1550
1551 vg_ui.textbox.cursor_user = line_above_begin+offset;
1552 vg_ui.textbox.cursor_pos = line_above_begin+offset;
1553 }
1554 else{
1555 vg_ui.textbox.cursor_user = line_begin;
1556 vg_ui.textbox.cursor_pos = line_begin;
1557 }
1558 }
1559 else{
1560 if( vg_ui.textbox.callbacks.up ){
1561 vg_ui.textbox.callbacks.up( vg_ui.textbuf, vg_ui.textbox.len );
1562 }
1563 }
1564 }
1565
1566 void _ui_textbox_down(void)
1567 {
1568 if( vg_ui.textbox.flags & UI_TEXTBOX_MULTILINE ){
1569 int line_begin = vg_ui.textbox.cursor_user;
1570
1571 while( line_begin ){
1572 if( vg_ui.textbuf[ line_begin-1 ] == '\n' ){
1573 break;
1574 }
1575
1576 line_begin --;
1577 }
1578
1579 int line_below_begin = vg_ui.textbox.cursor_user;
1580
1581 while(1){
1582 if( vg_ui.textbuf[ line_below_begin ] == '\0' ){
1583 vg_ui.textbox.cursor_user = line_below_begin;
1584 vg_ui.textbox.cursor_pos = line_below_begin;
1585 return;
1586 }
1587
1588 if( vg_ui.textbuf[ line_below_begin ] == '\n' ){
1589 line_below_begin ++;
1590 break;
1591 }
1592
1593 line_below_begin ++;
1594 }
1595
1596 int line_below_end = line_below_begin;
1597 while(1){
1598 if( vg_ui.textbuf[ line_below_end ] == '\0' ||
1599 vg_ui.textbuf[ line_below_end ] == '\n' ){
1600 line_below_end ++;
1601 break;
1602 }
1603 line_below_end ++;
1604 }
1605
1606 int offset = vg_ui.textbox.cursor_user - line_begin,
1607 line_length_below = line_below_end - line_below_begin -1;
1608
1609 offset = VG_MIN( line_length_below, offset );
1610
1611 vg_ui.textbox.cursor_user = line_below_begin+offset;
1612 vg_ui.textbox.cursor_pos = line_below_begin+offset;
1613 }
1614 else{
1615 if( vg_ui.textbox.callbacks.down ){
1616 vg_ui.textbox.callbacks.down( vg_ui.textbuf, vg_ui.textbox.len );
1617 }
1618 }
1619 }
1620
1621 void _ui_textbox_right_select(void)
1622 {
1623 _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user, NULL, 1, 0 );
1624 }
1625
1626 void _ui_textbox_right(void)
1627 {
1628 int cursor_diff = vg_ui.textbox.cursor_pos - vg_ui.textbox.cursor_user? 0: 1;
1629
1630 _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user,
1631 &vg_ui.textbox.cursor_pos, +cursor_diff, 1 );
1632 }
1633
1634 void _ui_textbox_backspace(void)
1635 {
1636 if( vg_ui.focused_control_type == k_ui_control_textbox ){
1637 vg_ui.textbox.cursor_user = _ui_textbox_delete_char( -1 );
1638 vg_ui.textbox.cursor_pos = vg_ui.textbox.cursor_user;
1639 _ui_textbox_change_callback();
1640 }
1641 }
1642
1643 void _ui_textbox_delete(void)
1644 {
1645 if( vg_ui.focused_control_type == k_ui_control_textbox ){
1646 vg_ui.textbox.cursor_user = _ui_textbox_delete_char( 1 );
1647 vg_ui.textbox.cursor_pos = vg_ui.textbox.cursor_user;
1648 _ui_textbox_change_callback();
1649 }
1650 }
1651
1652 void _ui_textbox_home_select(void)
1653 {
1654 i32 start = vg_ui.textbox.cursor_user;
1655
1656 if( vg_ui.textbox.flags & UI_TEXTBOX_MULTILINE ){
1657 while( start ){
1658 if( vg_ui.textbuf[start-1] == '\n' )
1659 break;
1660 else
1661 start --;
1662 }
1663 }
1664 else
1665 start = 0;
1666
1667 vg_ui.textbox.cursor_user = start;
1668 }
1669
1670 void _ui_textbox_home(void)
1671 {
1672 _ui_textbox_home_select();
1673 vg_ui.textbox.cursor_pos = vg_ui.textbox.cursor_user;
1674 }
1675
1676 void _ui_textbox_end_select(void)
1677 {
1678 i32 end = vg_ui.textbox.cursor_user;
1679
1680 if( vg_ui.textbox.flags & UI_TEXTBOX_MULTILINE ){
1681 while( vg_ui.textbuf[end] ){
1682 if( vg_ui.textbuf[end] == '\n' )
1683 break;
1684 else
1685 end ++;
1686 }
1687 }
1688 else
1689 end = VG_MIN( vg_ui.textbox.len-1, strlen(vg_ui.textbuf) );
1690
1691 vg_ui.textbox.cursor_user = end;
1692 }
1693
1694 void _ui_textbox_end(void)
1695 {
1696 _ui_textbox_end_select();
1697 vg_ui.textbox.cursor_pos = vg_ui.textbox.cursor_user;
1698 }
1699
1700 void _ui_textbox_select_all(void)
1701 {
1702 _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user, NULL, 10000, 0);
1703 _ui_textbox_move_cursor( &vg_ui.textbox.cursor_pos, NULL, -10000, 0);
1704 }
1705
1706 void _ui_textbox_cut(void)
1707 {
1708 _ui_textbox_to_clipboard();
1709 vg_ui.textbox.cursor_user = _ui_textbox_delete_char(0);
1710 vg_ui.textbox.cursor_pos = vg_ui.textbox.cursor_user;
1711 _ui_textbox_change_callback();
1712 }
1713
1714 void _ui_textbox_enter(void)
1715 {
1716 if( vg_ui.focused_control_type == k_ui_control_textbox ){
1717 vg_ui.ignore_input_frames = 2;
1718
1719 if( vg_ui.textbox.callbacks.enter )
1720 vg_ui.textbox.callbacks.enter( vg_ui.textbuf, vg_ui.textbox.len );
1721
1722 if( vg_ui.focused_control_type != k_ui_control_textbox ) return;
1723
1724 if( vg_ui.textbox.flags & UI_TEXTBOX_MULTILINE ){
1725 _ui_textbox_put_char( '\n' );
1726 _ui_textbox_change_callback();
1727 }
1728 else{
1729 if( !(vg_ui.textbox.flags & UI_TEXTBOX_AUTOFOCUS ) )
1730 ui_defocus_all();
1731 }
1732 }
1733 }
1734
1735 /*
1736 * based on a visual character coordinate relative to the anchor of the textbox,
1737 * this works out the linear place in the buffer that coordinate maps to
1738 *
1739 * input coordinates go in co[0], co[1], and the result index is in co[2]
1740 */
1741 static void _ui_textbox_calc_index_from_grid( int co[3], int wrap_length ){
1742 int i[3] = {0,0,0};
1743
1744 char c;
1745 while( (c = vg_ui.textbuf[i[2]]) ){
1746 if( i[1]==co[1] && i[0]>=co[0] ) break;
1747
1748 if( i[0] >= wrap_length ){
1749 i[1] ++;
1750 i[0] = 0;
1751 }
1752
1753 if( c >= 32 && c <= 126 ){
1754 i[0] ++;
1755 i[2] ++;
1756 }
1757 else if( c == '\n' ){
1758 i[1] ++;
1759
1760 if( i[1] > co[1] ) break;
1761
1762 i[2] ++;
1763 i[0] = 0;
1764 }
1765 else i[2] ++;
1766 }
1767
1768 co[0] = i[0];
1769 co[1] = i[1];
1770 co[2] = i[2];
1771 }
1772
1773 /*
1774 * based on the index specied in co[2], work out the visual character
1775 * coordinates and store them in co[0], co[1]
1776 */
1777 static void _ui_textbox_index_calc_coords( int co[3], int wrap_length ){
1778 co[0] = 0;
1779 co[1] = 0;
1780
1781 char c;
1782 int i=0;
1783
1784 while( (c = vg_ui.textbuf[i ++]) ){
1785 if( i > co[2] ) break;
1786 if( co[0] >= wrap_length ){
1787 co[1] ++;
1788 co[0] = 0;
1789 }
1790 if( c >= 32 && c <= 126 ) co[0] ++;
1791 else if( c == '\n' ){
1792 co[1] ++;
1793 co[0] = 0;
1794 }
1795 }
1796 }
1797
1798 /*
1799 * calculate the number of characters remaining until either:
1800 * - the wrap_length limit is hit
1801 * - end of the line/string
1802 *
1803 * index must be fully populated with visual X/Y, and linear index
1804 */
1805 static int _ui_textbox_run_remaining( int index[3], int wrap_length ){
1806 int i=0, printed_chars=0;
1807 char c;
1808 while( (c = vg_ui.textbuf[index[2] + (i ++)]) ){
1809 if( index[0]+i >= wrap_length ) break;
1810 if( c >= 32 && c <= 126 ) printed_chars ++;
1811 else if( c == '\n' ) break;
1812 }
1813
1814 return printed_chars+1;
1815 }
1816
1817 int ui_textbox( ui_rect inout_panel, const char *label,
1818 char *buf, u32 len, u32 lines, u32 flags,
1819 struct ui_textbox_callbacks *callbacks )
1820 {
1821 if( lines > 1 ) flags |= UI_TEXTBOX_MULTILINE;
1822
1823 ui_rect rect;
1824 ui_standard_widget( inout_panel, rect, lines );
1825
1826 if( label )
1827 ui_label( rect, label, 1, 0, rect );
1828
1829 int clickup= ui_click_up(UI_MOUSE_LEFT),
1830 clickdown = ui_click_down(UI_MOUSE_LEFT),
1831 click = ui_clicking(UI_MOUSE_LEFT) | clickup,
1832 target = ui_inside_rect( rect, vg_ui.mouse_click ) && click,
1833 hover = ui_inside_rect( rect, vg_ui.mouse );
1834
1835 /* allow instant transitions from textbox->textbox */
1836 if( (vg_ui.focused_control_type != k_ui_control_none) &&
1837 (vg_ui.focused_control_type != k_ui_control_textbox) ){
1838 clickup = 0;
1839 clickdown = 0;
1840 click = 0;
1841 target = 0;
1842 hover = 0;
1843 flags &= ~UI_TEXTBOX_AUTOFOCUS;
1844 }
1845
1846 u32 col_base = ui_colour( k_ui_bg ),
1847 col_highlight = ui_colour( k_ui_fg ),
1848 col_cursor = (0x00ffffff & ui_colour(k_ui_fg))|0x7f000000;
1849
1850 ui_px border = -1;
1851
1852 ui_rect text_rect;
1853 rect_copy( rect, text_rect );
1854
1855 if( flags & UI_TEXTBOX_MULTILINE ) text_rect[3] = rect[3]-16;
1856 else text_rect[3] = vg_ui.font->line_height;
1857
1858 text_rect[2] -= 16;
1859 ui_rect_center( rect, text_rect );
1860
1861 ui_px wrap_length = 1024;
1862
1863 if( flags & UI_TEXTBOX_WRAP )
1864 wrap_length = text_rect[2] / vg_ui.font->spacing;
1865
1866 if( hover ){
1867 vg_ui.cursor = k_ui_cursor_ibeam;
1868 }
1869
1870 if( vg_ui.focused_control_id == buf ){
1871 ui_fill( rect, col_base );
1872 ui_ntext( text_rect, buf, wrap_length, 1, k_ui_align_left, 0 );
1873
1874 if( !(flags & UI_TEXTBOX_AUTOFOCUS) && ((clickup||clickdown) && !target)){
1875 ui_defocus_all();
1876 }
1877 else{
1878 vg_ui.focused_control_hit = 1;
1879 if( click && target ){
1880 int p0[3] ={
1881 (vg_ui.mouse_click[0] - text_rect[0]) / vg_ui.font->spacing,
1882 (vg_ui.mouse_click[1] - text_rect[1]) / vg_ui.font->line_height,
1883 -1
1884 },
1885 p1[3] = {
1886 (vg_ui.mouse[0] - text_rect[0]) / vg_ui.font->spacing,
1887 (vg_ui.mouse[1] - text_rect[1]) / vg_ui.font->line_height,
1888 -1
1889 };
1890
1891 if( flags & UI_TEXTBOX_MULTILINE ){
1892 _ui_textbox_calc_index_from_grid( p0, wrap_length );
1893 _ui_textbox_calc_index_from_grid( p1, wrap_length );
1894
1895 vg_ui.textbox.cursor_pos = p0[2];
1896 vg_ui.textbox.cursor_user = p1[2];
1897 }
1898 else{
1899 int max = strlen( buf );
1900 vg_ui.textbox.cursor_pos = VG_MAX( 0, VG_MIN( max, p0[0] )),
1901 vg_ui.textbox.cursor_user = VG_MAX( 0, VG_MIN( max, p1[0] ));
1902 }
1903 }
1904
1905 ui_outline( rect, -2, vg_ui.scheme[ k_ui_orange ], 0 );
1906
1907 ui_rect cursor;
1908
1909 int c0 = vg_ui.textbox.cursor_pos,
1910 c1 = vg_ui.textbox.cursor_user,
1911 start = VG_MIN( c0, c1 ),
1912 end = VG_MAX( c0, c1 ),
1913 chars = end-start;
1914
1915 if( flags & (UI_TEXTBOX_WRAP|UI_TEXTBOX_MULTILINE) ){
1916 int pos[3], remaining = chars;
1917
1918 pos[2] = start;
1919 _ui_textbox_index_calc_coords( pos, wrap_length );
1920
1921 if( start==end ){
1922 cursor[0] = text_rect[0] + pos[0]*vg_ui.font->spacing-1;
1923 cursor[1] = text_rect[1] + pos[1]*14;
1924 cursor[2] = 2;
1925 cursor[3] = 13;
1926 ui_fill( cursor, col_cursor );
1927 rect_copy( cursor, vg_ui.click_fader_end );
1928 }
1929 else{
1930 while( remaining ){
1931 int run = _ui_textbox_run_remaining( pos, wrap_length );
1932 run = VG_MIN( run, remaining );
1933
1934 cursor[0] = text_rect[0] + pos[0]*vg_ui.font->spacing-1;
1935 cursor[1] = text_rect[1] + pos[1]*14;
1936 cursor[2] = (float)(run)*(float)vg_ui.font->spacing;
1937 cursor[3] = 13;
1938
1939 ui_fill( cursor, col_cursor );
1940
1941 remaining -= run;
1942 pos[0] = 0;
1943 pos[1] ++;
1944 pos[2] += run;
1945 }
1946 rect_copy( cursor, vg_ui.click_fader_end );
1947 }
1948 }
1949 else{
1950 cursor[0] = text_rect[0] + start*vg_ui.font->spacing-1;
1951 cursor[1] = text_rect[1];
1952 cursor[3] = 13;
1953
1954 if( start==end ){
1955 cursor[2] = 2;
1956 }
1957 else{
1958 cursor[2] = (float)(chars)*(float)vg_ui.font->spacing;
1959 }
1960
1961 if( (vg_ui.click_fade_opacity<=0.0f) &&
1962 ui_clip( rect, cursor, cursor ) ){
1963 ui_fill( cursor, col_cursor );
1964 }
1965
1966 rect_copy( cursor, vg_ui.click_fader_end );
1967 }
1968 }
1969
1970 return 0;
1971 }
1972
1973 if( click || (flags & UI_TEXTBOX_AUTOFOCUS) ){
1974 if( (target && hover) || (flags & UI_TEXTBOX_AUTOFOCUS) ){
1975 ui_defocus_all();
1976
1977 ui_fill( rect, col_highlight );
1978 vg_ui.ignore_input_frames = 2;
1979 rect_copy( rect, vg_ui.click_fader );
1980 rect_copy( rect, vg_ui.click_fader_end );
1981
1982 vg_ui.click_fade_opacity = 1.0f;
1983 vg_ui.textbuf = buf;
1984 vg_ui.focused_control_hit = 1;
1985 vg_ui.focused_control_type = k_ui_control_textbox;
1986 vg_ui.textbox.len = len;
1987 vg_ui.textbox.flags = flags;
1988 vg_ui.textbox.cursor_pos = 0;
1989 vg_ui.textbox.cursor_user = 0;
1990
1991 if( callbacks ){
1992 vg_ui.textbox.callbacks = *callbacks;
1993 }
1994 else{
1995 vg_ui.textbox.callbacks.change = NULL;
1996 vg_ui.textbox.callbacks.down = NULL;
1997 vg_ui.textbox.callbacks.up = NULL;
1998 vg_ui.textbox.callbacks.enter = NULL;
1999 }
2000
2001 SDL_StartTextInput();
2002 }
2003 }
2004
2005 ui_fill( rect, col_base );
2006
2007 if( hover ){
2008 ui_outline( rect, -1, col_highlight, 0 );
2009 }
2010
2011 ui_ntext( text_rect, buf, wrap_length, 1, k_ui_align_left, 0 );
2012 return 0;
2013 }
2014
2015 /*
2016 * Tabs
2017 * -----------------------------------------------------------------------------
2018 */
2019
2020 void ui_tabs( ui_rect inout_panel, ui_rect out_content_panel,
2021 const char **titles, u32 count, i32 *page )
2022 {
2023 ui_rect bar;
2024 ui_standard_widget( inout_panel, bar, 1 );
2025
2026 i32 cur_page = *page;
2027
2028 f32 width = (f32)inout_panel[2] / (f32)count;
2029
2030 ui_px h = (inout_panel[1] + inout_panel[3]) - (bar[1]+bar[3]);
2031 inout_panel[1] = bar[1]+bar[3];
2032 inout_panel[3] = h;
2033
2034 ui_fill( inout_panel, ui_colour( k_ui_bg+2 ) );
2035 ui_outline( inout_panel, 1, ui_colour( k_ui_bg+5 ), 0 );
2036
2037 rect_copy( inout_panel, out_content_panel );
2038 ui_rect_pad( out_content_panel, (ui_px[2]){ k_ui_padding, k_ui_padding } );
2039
2040 /* place buttons */
2041 for( i32 i=0; i<count; i++ ){
2042 ui_rect button = {
2043 bar[0] + ((f32)i*width),
2044 bar[1],
2045 width,
2046 bar[3]-1
2047 };
2048
2049 enum ui_scheme_colour colour = k_ui_bg+4;
2050 if( i == cur_page ){
2051 colour = k_ui_bg+2;
2052 ui_outline( button, 1, ui_colour( k_ui_bg+5 ),
2053 UI_TOP|UI_LEFT|UI_RIGHT );
2054 button[3] ++;
2055 }
2056
2057 if( ui_colourbutton_text( button, titles[i], 1, colour ) == 1 )
2058 *page = i;
2059 }
2060 }
2061
2062 /*
2063 * Modal UI
2064 * -----------------------------------------------------------------------------
2065 */
2066 void ui_start_modal( const char *message, u32 options )
2067 {
2068 vg_ui.focused_control_type = k_ui_control_modal;
2069 vg_ui.modal.message = message;
2070 vg_ui.modal.callbacks.close = NULL;
2071 vg_ui.modal.options = options;
2072
2073 u32 type = options & UI_MODAL_TYPE_BITS;
2074 if( type == UI_MODAL_OK ) vg_info( message );
2075 else if( type == UI_MODAL_WARN ) vg_warn( message );
2076 else if( type == UI_MODAL_GOOD ) vg_success( message );
2077 else if( type == UI_MODAL_BAD ) vg_error( message );
2078 }
2079
2080 /*
2081 * Input handling
2082 * -----------------------------------------------------------------------------
2083 */
2084
2085 /*
2086 * Handles binds
2087 */
2088 void ui_proc_key( SDL_Keysym ev )
2089 {
2090 if( vg_ui.focused_control_type != k_ui_control_textbox ){
2091 return;
2092 }
2093
2094 struct textbox_mapping{
2095 u16 mod;
2096 SDL_Keycode key;
2097
2098 void (*handler)(void);
2099 }
2100 mappings[] =
2101 {
2102 { 0, SDLK_LEFT, _ui_textbox_left },
2103 { KMOD_SHIFT, SDLK_LEFT, _ui_textbox_left_select },
2104 { 0, SDLK_RIGHT, _ui_textbox_right },
2105 { KMOD_SHIFT, SDLK_RIGHT, _ui_textbox_right_select },
2106 { 0, SDLK_DOWN, _ui_textbox_down },
2107 { 0, SDLK_UP, _ui_textbox_up },
2108 { 0, SDLK_BACKSPACE, _ui_textbox_backspace },
2109 { KMOD_SHIFT, SDLK_BACKSPACE, _ui_textbox_backspace },
2110 { KMOD_CTRL, SDLK_BACKSPACE, _ui_textbox_backspace },
2111 { 0, SDLK_DELETE, _ui_textbox_delete },
2112 { 0, SDLK_HOME, _ui_textbox_home },
2113 { KMOD_SHIFT, SDLK_HOME, _ui_textbox_home_select },
2114 { 0, SDLK_END, _ui_textbox_end },
2115 { KMOD_SHIFT, SDLK_END, _ui_textbox_end_select },
2116 { KMOD_CTRL, SDLK_a, _ui_textbox_select_all },
2117 { KMOD_CTRL, SDLK_c, _ui_textbox_to_clipboard },
2118 { KMOD_CTRL, SDLK_x, _ui_textbox_cut },
2119 { KMOD_CTRL, SDLK_v, _ui_textbox_clipboard_paste },
2120 { 0, SDLK_RETURN, _ui_textbox_enter },
2121 { 0, SDLK_ESCAPE, ui_defocus_all },
2122 };
2123
2124 SDL_Keymod mod = 0;
2125
2126 if( ev.mod & KMOD_SHIFT )
2127 mod |= KMOD_SHIFT;
2128
2129 if( ev.mod & KMOD_CTRL )
2130 mod |= KMOD_CTRL;
2131
2132 if( ev.mod & KMOD_ALT )
2133 mod |= KMOD_ALT;
2134
2135 for( int i=0; i<vg_list_size( mappings ); i++ ){
2136 struct textbox_mapping *mapping = &mappings[i];
2137
2138 if( mapping->key == ev.sym ){
2139 if( mapping->mod == 0 ){
2140 if( mod == 0 ){
2141 mapping->handler();
2142 return;
2143 }
2144 }
2145 else if( (mod & mapping->mod) == mapping->mod ){
2146 mapping->handler();
2147 return;
2148 }
2149 }
2150 }
2151 }
2152
2153 /*
2154 * Callback for text entry mode
2155 */
2156 void ui_proc_utf8( const char *text )
2157 {
2158 if( vg_ui.focused_control_type == k_ui_control_textbox ){
2159 const char *ptr = text;
2160
2161 while( *ptr ){
2162 if( *ptr != '`' ) _ui_textbox_put_char( *ptr );
2163 ptr ++;
2164 }
2165
2166 _ui_textbox_change_callback();
2167 }
2168 }
2169
2170 /*
2171 * Development utils
2172 * -----------------------------------------------------------------------------
2173 */
2174
2175 void ui_dev_colourview(void)
2176 {
2177 ui_rect window = {vg.window_x-256,0,256,vg.window_y}, swatch;
2178
2179 const char *names[vg_list_size(vg_ui.scheme)] = {
2180 [k_ui_bg] = "k_ui_bg", "k_ui_bg+1", "k_ui_bg+2", "k_ui_bg+3",
2181 "k_ui_bg+4", "k_ui_bg+5", "k_ui_bg+6", "k_ui_bg+7",
2182
2183 [k_ui_fg] = "k_ui_fg", "k_ui_fg+1", "k_ui_fg+2", "k_ui_fg+3",
2184 "k_ui_fg+4", "k_ui_fg+5", "k_ui_fg+6", "k_ui_fg+7",
2185
2186 [k_ui_red] = "k_ui_red", "k_ui_orange", "k_ui_yellow", "k_ui_green",
2187 "k_ui_aqua", "k_ui_blue", "k_ui_purple", "k_ui_gray",
2188 "k_ui_red+8","k_ui_orange+8","k_ui_yellow+8","k_ui_green+8",
2189 "k_ui_aqua+8","k_ui_blue+8","k_ui_purple+8","k_ui_gray+8" };
2190
2191 ui_rect col[2];
2192 ui_split_ratio( window, k_ui_axis_v, 0.5f, 0, col[0], col[1] );
2193
2194 for( int i=0; i<vg_list_size(vg_ui.scheme); i++ ){
2195 int which = (i/8)%2;
2196
2197 ui_split( col[which], k_ui_axis_h, 24, 0, swatch, col[which] );
2198 ui_fill( swatch, ui_colour(i) );
2199
2200 if( names[i] )
2201 ui_text(swatch, names[i], 1, k_ui_align_middle_left, ui_colourcont(i));
2202 }
2203 }