1 /* Copyright (C) 2021-2024 Harry Godden (hgn) - All Rights Reserved */
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
11 * 5. a maximum of ONE control per memory location can be drawn at any given
18 #include "vg_engine.h"
20 #include "vg_shader.h"
23 ui_px k_ui_widget_height
= 28,
27 #include "vg/vg_default_font.gc"
29 struct vg_imgui vg_ui
= {
31 [ k_ui_bg
+0 ] = UI_RGB( 0x1d2021 ),
32 [ k_ui_bg
+1 ] = UI_RGB( 0x282828 ),
33 [ k_ui_bg
+2 ] = UI_RGB( 0x3c3836 ),
34 [ k_ui_bg
+3 ] = UI_RGB( 0x504945 ),
35 [ k_ui_bg
+4 ] = UI_RGB( 0x665c54 ),
36 [ k_ui_bg
+5 ] = UI_RGB( 0x7c6f64 ),
37 [ k_ui_bg
+6 ] = UI_RGB( 0x928374 ),
38 [ k_ui_bg
+7 ] = UI_RGB( 0xa89984 ),
40 [ k_ui_fg
+0 ] = UI_RGB( 0xebdbb2 ),
41 [ k_ui_fg
+1 ] = UI_RGB( 0xfbf1c7 ),
42 [ k_ui_fg
+2 ] = UI_RGB( 0xd5c4a1 ),
43 [ k_ui_fg
+3 ] = UI_RGB( 0xbdae93 ),
44 [ k_ui_fg
+4 ] = UI_RGB( 0xa89984 ),
45 [ k_ui_fg
+5 ] = UI_RGB( 0x000000 ),
46 [ k_ui_fg
+6 ] = UI_RGB( 0x000000 ),
47 [ k_ui_fg
+7 ] = UI_RGB( 0x000000 ),
49 [ k_ui_red
] = UI_RGB( 0xcc241d ),
50 [ k_ui_orange
] = UI_RGB( 0xd65d0e ),
51 [ k_ui_yellow
] = UI_RGB( 0xd79921 ),
52 [ k_ui_green
] = UI_RGB( 0x98971a ),
53 [ k_ui_aqua
] = UI_RGB( 0x689d6a ),
54 [ k_ui_blue
] = UI_RGB( 0x458588 ),
55 [ k_ui_purple
] = UI_RGB( 0xb16286 ),
56 [ k_ui_gray
] = UI_RGB( 0x928374 ),
57 [ k_ui_red
+ k_ui_brighter
] = UI_RGB( 0xfb4934 ),
58 [ k_ui_orange
+ k_ui_brighter
] = UI_RGB( 0xfe8019 ),
59 [ k_ui_yellow
+ k_ui_brighter
] = UI_RGB( 0xfabd2f ),
60 [ k_ui_green
+ k_ui_brighter
] = UI_RGB( 0xb8bb26 ),
61 [ k_ui_aqua
+ k_ui_brighter
] = UI_RGB( 0x8ec07c ),
62 [ k_ui_blue
+ k_ui_brighter
] = UI_RGB( 0x83a598 ),
63 [ k_ui_purple
+ k_ui_brighter
] = UI_RGB( 0xd3869b ),
64 [ k_ui_gray
+ k_ui_brighter
] = UI_RGB( 0xa89984 ),
66 .font
= &vgf_default_small
,
67 .colour
= {1.0f
,1.0f
,1.0f
,1.0f
},
68 .bg_inverse_ratio
= {1,1}
71 static struct vg_shader _shader_ui
={
72 .name
= "[vg] ui - transparent",
76 "layout (location=0) in vec2 a_co;"
77 "layout (location=1) in vec2 a_uv;"
78 "layout (location=2) in vec4 a_colour;"
80 "uniform vec2 uBGInverseRatio;"
81 "uniform vec2 uInverseFontSheet;"
83 "out vec4 aTexCoords;"
87 "vec4 proj_pos = vec4( uPv * vec3( a_co, 1.0 ), 1.0 );"
88 "gl_Position = proj_pos;"
89 "aTexCoords = vec4( a_uv * uInverseFontSheet, "
90 " (proj_pos.xy*0.5+0.5) * uBGInverseRatio );"
97 "uniform sampler2D uTexGlyphs;"
98 "uniform sampler2D uTexBG;"
99 "uniform vec4 uColour;"
100 "uniform float uSpread;"
101 "out vec4 FragColor;"
103 "in vec4 aTexCoords;"
106 "vec2 rand_hash22( vec2 p ){"
107 "vec3 p3 = fract(vec3(p.xyx) * 213.8976123);"
108 "p3 += dot(p3, p3.yzx+19.19);"
109 "return fract(vec2((p3.x + p3.y)*p3.z, (p3.x+p3.z)*p3.y));"
113 "vec4 diffuse = aColour;"
115 "vec4 avg = vec4(0.0);"
117 "if( aColour.a == 0.0 ){"
119 "avg.a = texture( uTexGlyphs, aTexCoords.xy ).r;"
122 "if( uSpread > 0.0001 ){"
123 "for( int i=0; i<4; i ++ ){"
124 "vec2 spread = rand_hash22(aTexCoords.zw+vec2(float(i)));"
125 "avg += texture( uTexBG, aTexCoords.zw + (spread-0.5)*uSpread );"
129 "avg.rgb = mix( avg.rgb, aColour.rgb, aColour.a );"
136 "FragColor = avg * uColour;"
141 static struct vg_shader _shader_ui_image
= {
142 .name
= "[vg] ui_image",
147 "layout (location=0) in vec2 a_co;"
148 "layout (location=1) in vec2 a_uv;"
149 "layout (location=2) in vec4 a_colour;"
152 "out vec2 aTexCoords;"
158 "gl_Position = vec4( uPv * vec3( a_co, 1.0 ), 1.0 );"
159 "aTexCoords = a_uv * 0.00390625;"
160 "aColour = a_colour;"
169 "uniform sampler2D uTexImage;"
170 "uniform vec4 uColour;"
171 "out vec4 FragColor;"
173 "in vec2 aTexCoords;"
179 "vec4 colour = texture( uTexImage, aTexCoords );"
180 "FragColor = colour * uColour;"
185 static struct vg_shader _shader_ui_hsv
= {
186 .name
= "[vg] ui_hsv",
190 "layout (location=0) in vec2 a_co;"
191 "layout (location=1) in vec2 a_uv;"
192 "layout (location=2) in vec4 a_colour;"
195 "out vec2 aTexCoords;"
201 "gl_Position = vec4( uPv * vec3( a_co, 1.0 ), 1.0 );"
202 "aTexCoords = a_uv * 0.00390625;"
203 "aColour = a_colour;"
212 "uniform float uHue;"
213 "out vec4 FragColor;"
215 "in vec2 aTexCoords;"
221 "vec3 c = vec3( uHue, aTexCoords );"
222 "vec4 K = vec4(1.0,2.0/3.0,1.0/3.0,3.0);"
223 "vec3 p = abs(fract(c.xxx+K.xyz)*6.0 - K.www);"
224 "vec3 colour = c.z*mix(K.xxx,clamp(p-K.xxx,0.0,1.0),c.y);"
225 "FragColor = vec4( colour, 1.0 );"
230 void vg_ui_init(void)
232 if( !vg_shader_compile( &_shader_ui
) ||
233 !vg_shader_compile( &_shader_ui_image
) ||
234 !vg_shader_compile( &_shader_ui_hsv
) ){
235 vg_fatal_error( "Failed to compile ui shader" );
240 * ----------------------------------------
243 vg_ui
.max_indices
= 20000;
244 vg_ui
.max_verts
= 30000;
246 /* Generate the buffer we are gonna be drawing to */
247 glGenVertexArrays( 1, &vg_ui
.vao
);
248 glGenBuffers( 1, &vg_ui
.vbo
);
249 glGenBuffers( 1, &vg_ui
.ebo
);
251 glBindVertexArray( vg_ui
.vao
);
252 glBindBuffer( GL_ARRAY_BUFFER
, vg_ui
.vbo
);
254 glBufferData( GL_ARRAY_BUFFER
,
255 vg_ui
.max_verts
* sizeof( struct ui_vert
),
256 NULL
, GL_DYNAMIC_DRAW
);
257 glBindVertexArray( vg_ui
.vao
);
259 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, vg_ui
.ebo
);
260 glBufferData( GL_ELEMENT_ARRAY_BUFFER
,
261 vg_ui
.max_indices
* sizeof( u16
), NULL
, GL_DYNAMIC_DRAW
);
266 u32
const stride
= sizeof( struct ui_vert
);
269 glVertexAttribPointer( 0, 2, GL_SHORT
, GL_FALSE
, stride
,
270 (void *)offsetof( struct ui_vert
, co
) );
271 glEnableVertexAttribArray( 0 );
274 glVertexAttribPointer( 1, 2, GL_UNSIGNED_SHORT
, GL_FALSE
, stride
,
275 (void *)offsetof( struct ui_vert
, uv
) );
276 glEnableVertexAttribArray( 1 );
279 glVertexAttribPointer( 2, 4, GL_UNSIGNED_BYTE
, GL_TRUE
, stride
,
280 (void *)offsetof( struct ui_vert
, colour
) );
281 glEnableVertexAttribArray( 2 );
285 /* Alloc RAM default context */
286 u32 vert_size
= vg_ui
.max_verts
*sizeof(struct ui_vert
),
287 inds_size
= vg_align8( vg_ui
.max_indices
*sizeof(u16
) );
289 vg_ui
.vertex_buffer
= vg_linear_alloc( vg_mem
.rtmemory
, vert_size
);
290 vg_ui
.indice_buffer
= vg_linear_alloc( vg_mem
.rtmemory
, inds_size
);
293 * -----------------------------------------------------
296 /* Load default font */
298 vg_font_sheet
*sheet
= &vg_default_font_sheet
;
300 total
= sheet
->w
*sheet
->h
,
303 vg_linear_clear( vg_mem
.scratch
);
304 u8
*image
= vg_linear_alloc( vg_mem
.scratch
, total
);
306 while( pixels
< total
)
308 for( int b
= 31; b
>= 0; b
-- )
310 image
[ pixels
++ ] = (sheet
->bitmap
[data
] & (0x1u
<< b
))? 0xffu
: 0x00u
;
312 if( pixels
>= total
)
321 vg_ui
.inverse_font_sheet
[0] = 1.0/(f64
)sheet
->w
;
322 vg_ui
.inverse_font_sheet
[1] = 1.0/(f64
)sheet
->h
;
324 glGenTextures( 1, &vg_ui
.tex_glyphs
);
325 glBindTexture( GL_TEXTURE_2D
, vg_ui
.tex_glyphs
);
326 glTexImage2D( GL_TEXTURE_2D
, 0, GL_R8
, sheet
->w
, sheet
->h
, 0,
327 GL_RED
, GL_UNSIGNED_BYTE
, image
);
330 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
331 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
332 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
333 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
337 * ---------------------------------------------------------------
340 vg_ui
.cursor_map
[ k_ui_cursor_default
] =
341 SDL_CreateSystemCursor( SDL_SYSTEM_CURSOR_ARROW
);
342 vg_ui
.cursor_map
[ k_ui_cursor_hand
] =
343 SDL_CreateSystemCursor( SDL_SYSTEM_CURSOR_HAND
);
344 vg_ui
.cursor_map
[ k_ui_cursor_ibeam
] =
345 SDL_CreateSystemCursor( SDL_SYSTEM_CURSOR_IBEAM
);
348 void rect_copy( ui_rect a
, ui_rect b
){
349 for( int i
=0; i
<4; i
++ )
353 void ui_flush( enum ui_shader shader
, f32 w
, f32 h
){
354 u32 vertex_offset
= vg_ui
.vert_start
*sizeof(ui_vert
),
355 vertex_count
= vg_ui
.cur_vert
-vg_ui
.vert_start
,
356 vertex_size
= vertex_count
*sizeof(ui_vert
),
358 indice_offset
= vg_ui
.indice_start
*sizeof(u16
),
359 indice_count
= vg_ui
.cur_indice
-vg_ui
.indice_start
,
360 indice_size
= indice_count
* sizeof(u16
);
362 if( !vertex_size
|| !indice_size
)
365 glBindVertexArray( vg_ui
.vao
);
366 glBindBuffer( GL_ARRAY_BUFFER
, vg_ui
.vbo
);
367 glBufferSubData( GL_ARRAY_BUFFER
, vertex_offset
, vertex_size
,
368 vg_ui
.vertex_buffer
+vg_ui
.vert_start
);
370 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, vg_ui
.ebo
);
371 glBufferSubData( GL_ELEMENT_ARRAY_BUFFER
, indice_offset
, indice_size
,
372 vg_ui
.indice_buffer
+vg_ui
.indice_start
);
374 glDisable( GL_DEPTH_TEST
);
375 glEnable( GL_BLEND
);
376 glBlendFunc( GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
377 glBlendEquation( GL_FUNC_ADD
);
378 glDisable( GL_CULL_FACE
);
380 m3x3f view
= M3X3_IDENTITY
;
381 m3x3_translate( view
, (v3f
){ -1.0f
, 1.0f
, 0.0f
} );
382 m3x3_scale( view
, (v3f
){ 1.0f
/(w
*0.5f
),
383 -1.0f
/(h
*0.5f
), 1.0f
} );
385 if( shader
== k_ui_shader_colour
){
386 glUseProgram( _shader_ui
.id
);
388 glUniformMatrix3fv( glGetUniformLocation( _shader_ui
.id
, "uPv" ), 1,
389 GL_FALSE
, (float *)view
);
390 glUniform4fv( glGetUniformLocation( _shader_ui
.id
, "uColour" ), 1,
393 glActiveTexture( GL_TEXTURE0
);
394 glBindTexture( GL_TEXTURE_2D
, vg_ui
.tex_glyphs
);
395 glUniform1i( glGetUniformLocation( _shader_ui
.id
, "uTexGlyphs" ), 0 );
397 glActiveTexture( GL_TEXTURE1
);
398 glBindTexture( GL_TEXTURE_2D
, vg_ui
.tex_bg
);
399 glUniform1i( glGetUniformLocation( _shader_ui
.id
, "uTexBG" ), 1 );
400 glUniform1f( glGetUniformLocation( _shader_ui
.id
, "uSpread" ),
402 glUniform2fv( glGetUniformLocation( _shader_ui
.id
, "uBGInverseRatio" ),
403 1, vg_ui
.bg_inverse_ratio
);
404 glUniform2fv( glGetUniformLocation( _shader_ui
.id
, "uInverseFontSheet" ),
405 1, vg_ui
.inverse_font_sheet
);
407 else if( shader
== k_ui_shader_image
){
408 glUseProgram( _shader_ui_image
.id
);
409 glUniformMatrix3fv( glGetUniformLocation( _shader_ui_image
.id
, "uPv" ), 1,
410 GL_FALSE
, (float *)view
);
411 glUniform1i( glGetUniformLocation(_shader_ui_image
.id
,"uTexImage"), 0 );
412 glUniform4fv( glGetUniformLocation( _shader_ui_image
.id
, "uColour" ), 1,
415 else if( shader
== k_ui_shader_hsv
){
416 glUseProgram( _shader_ui_hsv
.id
);
417 glUniformMatrix3fv( glGetUniformLocation( _shader_ui_hsv
.id
, "uPv" ), 1,
418 GL_FALSE
, (float *)view
);
419 glUniform1f( glGetUniformLocation(_shader_ui_hsv
.id
,"uHue"), vg_ui
.hue
);
422 vg_fatal_error( "Invalid UI shader (%d)\n", shader
);
424 glDrawElements( GL_TRIANGLES
, indice_count
, GL_UNSIGNED_SHORT
,
425 (void *)(vg_ui
.indice_start
*sizeof(u16
)) );
427 glDisable( GL_BLEND
);
429 vg_ui
.indice_start
= vg_ui
.cur_indice
;
430 vg_ui
.vert_start
= vg_ui
.cur_vert
;
433 void ui_fill_rect( ui_rect rect
, u32 colour
, ui_px uv
[4] )
435 /* this if far from ideal but stops us from crashing */
436 if( (vg_ui
.cur_vert
+ 4 > vg_ui
.max_verts
) ||
437 (vg_ui
.cur_indice
+ 6 > vg_ui
.max_indices
))
440 struct ui_vert
*vertices
= &vg_ui
.vertex_buffer
[ vg_ui
.cur_vert
];
441 u16
*indices
= &vg_ui
.indice_buffer
[ vg_ui
.cur_indice
];
443 for( int i
=0; i
<4; i
++ ){
444 vertices
[i
].colour
= colour
;
447 vertices
[0].co
[0] = rect
[0];
448 vertices
[0].co
[1] = rect
[1];
449 vertices
[0].uv
[0] = uv
[0];
450 vertices
[0].uv
[1] = uv
[1];
451 vertices
[1].co
[0] = rect
[0]+rect
[2];
452 vertices
[1].co
[1] = rect
[1];
453 vertices
[1].uv
[0] = uv
[2];
454 vertices
[1].uv
[1] = uv
[1];
455 vertices
[2].co
[0] = rect
[0]+rect
[2];
456 vertices
[2].co
[1] = rect
[1]+rect
[3];
457 vertices
[2].uv
[0] = uv
[2];
458 vertices
[2].uv
[1] = uv
[3];
459 vertices
[3].co
[0] = rect
[0];
460 vertices
[3].co
[1] = rect
[1]+rect
[3];
461 vertices
[3].uv
[0] = uv
[0];
462 vertices
[3].uv
[1] = uv
[3];
463 u16 ind_start
= vg_ui
.cur_vert
;
465 u16 start
= vg_ui
.cur_vert
;
466 u32 mesh
[] = { 0,2,1, 0,3,2 };
468 for( u32 i
=0; i
<vg_list_size(mesh
); i
++ ){
469 indices
[i
] = start
+mesh
[i
];
472 vg_ui
.cur_indice
+= 6;
476 void ui_fill( ui_rect rect
, u32 colour
)
478 ui_fill_rect( rect
, colour
, (ui_px
[4]){ 4,4,4,4 } );
481 void ui_outline( ui_rect rect
, ui_px thickness
, u32 colour
, u32 mask
)
483 /* this if far from ideal but stops us from crashing */
484 if( (vg_ui
.cur_vert
+ 8 > vg_ui
.max_verts
) ||
485 (vg_ui
.cur_indice
+ 24 > vg_ui
.max_indices
))
488 struct ui_vert
*vertices
= &vg_ui
.vertex_buffer
[ vg_ui
.cur_vert
];
489 u16
*indices
= &vg_ui
.indice_buffer
[ vg_ui
.cur_indice
];
491 for( int i
=0; i
<8; i
++ ){
492 vertices
[i
].uv
[0] = 4;
493 vertices
[i
].uv
[1] = 4;
494 vertices
[i
].colour
= colour
;
497 vertices
[0].co
[0] = rect
[0];
498 vertices
[0].co
[1] = rect
[1];
499 vertices
[1].co
[0] = rect
[0]+rect
[2];
500 vertices
[1].co
[1] = rect
[1];
501 vertices
[2].co
[0] = rect
[0]+rect
[2];
502 vertices
[2].co
[1] = rect
[1]+rect
[3];
503 vertices
[3].co
[0] = rect
[0];
504 vertices
[3].co
[1] = rect
[1]+rect
[3];
505 vertices
[4].co
[0] = vertices
[0].co
[0]-thickness
;
506 vertices
[4].co
[1] = vertices
[0].co
[1]-thickness
;
507 vertices
[5].co
[0] = vertices
[1].co
[0]+thickness
;
508 vertices
[5].co
[1] = vertices
[1].co
[1]-thickness
;
509 vertices
[6].co
[0] = vertices
[2].co
[0]+thickness
;
510 vertices
[6].co
[1] = vertices
[2].co
[1]+thickness
;
511 vertices
[7].co
[0] = vertices
[3].co
[0]-thickness
;
512 vertices
[7].co
[1] = vertices
[3].co
[1]+thickness
;
514 u16 start
= vg_ui
.cur_vert
;
515 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 };
518 mask
= UI_TOP
|UI_LEFT
|UI_BOTTOM
|UI_RIGHT
;
521 for( u32 i
=0; i
<vg_list_size(mesh
)/6; i
++ ){
522 if( (0x1<<i
) & mask
){
523 for( u32 j
=0; j
<6; j
++ )
524 indices
[c
++] = start
+mesh
[i
*6+j
];
528 vg_ui
.cur_indice
+= c
;
532 void ui_split( ui_rect rect
, enum ui_axis other
, ui_px width
, ui_px gap
,
533 ui_rect l
, ui_rect r
){
534 enum ui_axis dir
= other
^ 0x1;
536 if( width
< 0 ) width
= rect
[ 2+dir
] + width
;
539 rect_copy( rect
, temp
);
541 l
[ dir
] = temp
[ dir
];
542 r
[ dir
] = temp
[ dir
] + width
+ (gap
/2);
543 l
[ other
] = temp
[ other
];
544 r
[ other
] = temp
[ other
];
545 l
[ 2+dir
] = width
- (gap
/2);
546 r
[ 2+dir
] = temp
[ 2+dir
] - width
- (gap
/2);
547 l
[ 2+other
] = temp
[ 2+other
];
548 r
[ 2+other
] = temp
[ 2+other
];
551 void ui_rect_center( ui_rect parent
, ui_rect rect
)
553 rect
[0] = parent
[0] + (parent
[2]-rect
[2])/2;
554 rect
[1] = parent
[1] + (parent
[3]-rect
[3])/2;
557 void ui_fit_item( ui_rect rect
, ui_px size
[2], ui_rect d
)
559 i32 rp
= (i32
)rect
[2] * (i32
)size
[1],
560 rc
= (i32
)size
[0] * (i32
)rect
[3];
562 enum ui_axis dir
, other
;
563 if( rc
> rp
) dir
= k_ui_axis_h
;
564 else dir
= k_ui_axis_v
;
567 d
[2+dir
] = rect
[2+dir
];
568 d
[2+other
] = (rect
[2+dir
] * size
[other
]) / size
[dir
];
570 ui_rect_center( rect
, d
);
573 void ui_split_ratio( ui_rect rect
, enum ui_axis dir
, float ratio
,
574 ui_px gap
, ui_rect l
, ui_rect r
)
576 ui_px width
= (float)rect
[ 2+(dir
^0x1) ] * ratio
;
577 ui_split( rect
, dir
, width
, gap
, l
, r
);
580 void ui_rect_pad( ui_rect rect
, ui_px pad
[2] )
588 ui_px
ui_text_line_width( const char *str
)
591 const char *_c
= str
;
594 while( (c
= *(_c
++)) ){
595 if( c
>= 32 ) length
++;
596 else if( c
== '\n' ) break;
599 return length
* vg_ui
.font
->sx
;
602 ui_px
ui_text_string_height( const char *str
)
605 const char *_c
= str
;
608 while( (c
= *(_c
++)) )
610 if( c
== '\n' ) height
++;
613 return height
* vg_ui
.font
->sy
;
616 ui_px
ui_text_aligned_x( const char *str
, ui_rect rect
, ui_px scale
,
617 enum ui_align align
)
619 enum ui_align lwr
= k_ui_align_lwr
& align
;
620 if( lwr
== k_ui_align_left
){
624 ui_px width
= ui_text_line_width( str
) * scale
;
626 if( lwr
== k_ui_align_right
)
627 return rect
[0] + rect
[2]-width
;
629 return rect
[0] + (rect
[2]-width
)/2;
633 static ui_px
ui_min( ui_px a
, ui_px b
){ return a
<b
?a
:b
; }
634 static ui_px
ui_max( ui_px a
, ui_px b
){ return a
>b
?a
:b
; }
635 static ui_px
ui_clamp( ui_px a
, ui_px min
, ui_px max
){
636 return ui_min( max
, ui_max( a
, min
) );
639 int ui_clip( ui_rect parent
, ui_rect child
, ui_rect clipped
)
641 ui_px parent_max
[2], child_max
[2];
642 parent_max
[0] = parent
[0]+parent
[2];
643 parent_max
[1] = parent
[1]+parent
[3];
644 child_max
[0] = child
[0]+child
[2];
645 child_max
[1] = child
[1]+child
[3];
647 clipped
[0] = ui_clamp( child
[0], parent
[0], parent_max
[0] );
648 clipped
[1] = ui_clamp( child
[1], parent
[1], parent_max
[1] );
649 clipped
[2] = ui_clamp( child_max
[0], parent
[0], parent_max
[0] );
650 clipped
[3] = ui_clamp( child_max
[1], parent
[1], parent_max
[1] );
652 if( clipped
[0] == clipped
[2] ||
653 clipped
[1] == clipped
[3] )
656 clipped
[2] -= clipped
[0];
657 clipped
[3] -= clipped
[1];
662 int ui_inside_rect( ui_rect rect
, ui_px co
[2] )
664 if( co
[0] >= rect
[0] &&
666 co
[0] < rect
[0]+rect
[2] &&
667 co
[1] < rect
[1]+rect
[3] ){
674 int ui_click_down( u32 mask
)
676 if( vg_ui
.ignore_input_frames
) return 0;
677 if( (vg_ui
.mouse_state
[0] & mask
) &&
678 !(vg_ui
.mouse_state
[1] & mask
) )
684 int ui_clicking( u32 mask
)
686 if( vg_ui
.ignore_input_frames
) return 0;
687 return vg_ui
.mouse_state
[0] & mask
;
690 int ui_click_up( u32 mask
)
692 if( vg_ui
.ignore_input_frames
) return 0;
693 if( (vg_ui
.mouse_state
[1] & mask
) &&
694 !(vg_ui
.mouse_state
[0] & mask
) )
700 void ui_set_mouse_pos( ui_px x
, ui_px y
)
702 SDL_WarpMouseInWindow( vg
.window
, x
, y
);
705 vg_ui
.mouse_pos_overriden
= 1;
708 void ui_prerender(void)
711 vg_ui
.mouse_state
[1] = vg_ui
.mouse_state
[0];
712 vg_ui
.mouse_state
[0] = SDL_GetMouseState( &x
, &y
);
713 vg_ui
.mouse_delta
[0] = x
-vg_ui
.mouse
[0];
714 vg_ui
.mouse_delta
[1] = y
-vg_ui
.mouse
[1];
716 if( !vg_ui
.mouse_pos_overriden
)
723 vg_ui
.cur_indice
= 0;
724 vg_ui
.vert_start
= 0;
725 vg_ui
.indice_start
= 0;
726 vg_ui
.focused_control_hit
= 0;
727 vg_ui
.cursor
= k_ui_cursor_default
;
728 vg_ui
.wants_mouse
= 0;
729 vg_ui
.mouse_pos_overriden
= 0;
731 if( vg_ui
.ignore_input_frames
)
733 vg_ui
.ignore_input_frames
--;
737 if( ui_click_down(UI_MOUSE_LEFT
)||ui_click_down(UI_MOUSE_MIDDLE
) )
739 vg_ui
.mouse_click
[0] = vg_ui
.mouse
[0];
740 vg_ui
.mouse_click
[1] = vg_ui
.mouse
[1];
744 u32
ui_colour( enum ui_scheme_colour id
)
746 return vg_ui
.scheme
[ id
];
749 /* get an appropriately contrasting colour given the base */
750 u32
ui_colourcont( enum ui_scheme_colour id
)
752 if ( id
< k_ui_bg
+6 ) return ui_colour( k_ui_fg
);
753 else if( id
< k_ui_fg
) return ui_colour( k_ui_bg
+1 );
754 else if( id
< k_ui_hue
) return ui_colour( k_ui_bg
+3 );
755 else if( id
< k_ui_red
+k_ui_brighter
) return ui_colour( k_ui_fg
);
756 else return ui_colour( k_ui_fg
+1 );
759 void ui_hex_to_norm( u32 hex
, v4f norm
)
761 norm
[0] = ((hex
) & 0xff);
762 norm
[1] = ((hex
>>8 ) & 0xff);
763 norm
[2] = ((hex
>>16) & 0xff);
764 norm
[3] = ((hex
>>24) & 0xff);
765 v4_muls( norm
, 1.0f
/255.0f
, norm
);
768 u32
v4f_u32_colour( v4f colour
)
770 u32 r
= colour
[0] * 255.0f
,
771 g
= colour
[1] * 255.0f
,
772 b
= colour
[2] * 255.0f
,
773 a
= colour
[3] * 255.0f
;
775 return r
| (g
<<8) | (b
<<16) | (a
<<24);
778 static void ui_text_glyph( const struct vg_font_face
*ff
,
779 u8 glyph
, ui_rect out_texcoords
)
781 const vg_font_char
*ch
= &ff
->map
[ glyph
];
783 out_texcoords
[0] = ch
->x
;
784 out_texcoords
[1] = ch
->y
;
785 out_texcoords
[2] = ch
->x
+ ff
->cw
;
786 out_texcoords
[3] = ch
->y
+ ff
->ch
;
789 u32
ui_opacity( u32 colour
, f32 opacity
)
791 u32 alpha
= opacity
* 255.0f
;
792 return (colour
& 0x00ffffff) | (alpha
<< 24);
795 u32
ui_ntext( ui_rect rect
, const char *str
, u32 len
, ui_px scale
,
796 enum ui_align align
, u32 colour
)
799 if( colour
== 0 ) colour
= ui_colour( k_ui_fg
);
801 colour
&= 0x00ffffff;
803 const char *_c
= str
;
806 text_cursor
[0] = ui_text_aligned_x( str
, rect
, scale
, align
);
807 text_cursor
[1] = rect
[1];
808 text_cursor
[2] = vg_ui
.font
->cw
*scale
;
809 text_cursor
[3] = vg_ui
.font
->ch
*scale
;
811 u32 printed_chars
= 0;
813 if( align
& (k_ui_align_middle
|k_ui_align_bottom
) )
815 ui_px height
= ui_text_string_height( str
) * scale
;
817 if( align
& k_ui_align_bottom
)
818 text_cursor
[1] += rect
[3]-height
;
820 text_cursor
[1] += (rect
[3]-height
)/2;
823 while( (c
= *(_c
++)) )
825 if( printed_chars
>= len
)
828 text_cursor
[1] += vg_ui
.font
->sy
*scale
;
829 text_cursor
[0] = ui_text_aligned_x( _c
, rect
, scale
, align
);
830 text_cursor
[0] -= vg_ui
.font
->sx
*scale
;
833 ui_text_glyph( vg_ui
.font
, '\xb6' /*FIXME*/, glyph
);
834 ui_fill_rect( text_cursor
, 0x00ffffff, glyph
);
835 text_cursor
[0] += vg_ui
.font
->sx
*scale
;
840 text_cursor
[1] += vg_ui
.font
->sy
*scale
;
841 text_cursor
[0] = ui_text_aligned_x( _c
, rect
, scale
, align
);
848 ui_text_glyph( vg_ui
.font
, c
, glyph
);
850 ui_rect cursor_clipped
;
851 if( ui_clip( rect
, text_cursor
, cursor_clipped
) )
853 ui_fill_rect( cursor_clipped
, colour
, glyph
);
856 else if( c
== '\x1B' )
861 for( int i
=0; i
<3; i
++ )
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;
881 colour
&= 0x00ffffff;
885 colour_id
|= _c
[i
] << (i
*8);
898 text_cursor
[0] += vg_ui
.font
->sx
*scale
*4;
903 text_cursor
[0] += vg_ui
.font
->sx
*scale
;
907 return printed_chars
;
910 void ui_text( ui_rect rect
, const char *str
, ui_px scale
,
911 enum ui_align align
, u32 colour
)
913 ui_ntext( rect
, str
, 1024, scale
, align
, colour
);
916 void ui_font_face( vg_font_face
*ff
)
922 * Standard layout stuff
923 * -----------------------------------------------------------------------------
926 void ui_panel( ui_rect in_rect
, ui_rect out_panel
)
928 ui_fill( in_rect
, ui_colour( k_ui_bg
+1 ) );
929 ui_outline( in_rect
, 1, ui_colour( k_ui_bg
+7 ), 0 );
930 rect_copy( in_rect
, out_panel
);
931 ui_rect_pad( out_panel
, (ui_px
[2]){ k_ui_padding
, k_ui_padding
} );
934 void ui_label( ui_rect rect
, const char *text
, ui_px size
,
935 ui_px gap
, ui_rect r
)
938 ui_px width
= (ui_text_line_width(text
)+vg_ui
.font
->sx
) * size
;
939 ui_split( rect
, k_ui_axis_v
, width
, gap
, l
, r
);
940 ui_text( l
, text
, 1, k_ui_align_middle_left
, 0 );
943 void ui_standard_widget( ui_rect inout_panel
, ui_rect out_rect
, ui_px count
)
945 ui_px height
= (count
* vg_ui
.font
->sy
+ 18) * k_ui_scale
;
946 ui_split( inout_panel
, k_ui_axis_h
, height
, k_ui_padding
,
947 out_rect
, inout_panel
);
950 void ui_info( ui_rect inout_panel
, const char *text
)
953 ui_standard_widget( inout_panel
, box
, 1 );
954 ui_text( box
, text
, 1, k_ui_align_middle_left
, 0 );
957 void ui_image( ui_rect rect
, GLuint image
)
959 ui_flush( k_ui_shader_colour
, vg
.window_x
, vg
.window_y
);
960 glActiveTexture( GL_TEXTURE0
);
961 glBindTexture( GL_TEXTURE_2D
, image
);
962 ui_fill_rect( rect
, 0xffffffff, (ui_px
[4]){ 0,256,256,0 } );
963 ui_flush( k_ui_shader_image
, vg
.window_x
, vg
.window_y
);
966 void ui_defocus_all(void)
968 if( vg_ui
.focused_control_type
== k_ui_control_textbox
){
970 if( vg_ui
.textbox
.callbacks
.escape
)
971 vg_ui
.textbox
.callbacks
.escape();
974 vg_ui
.focused_control_id
= NULL
;
975 vg_ui
.focused_control_hit
= 0;
976 vg_ui
.focused_control_type
= k_ui_control_none
;
979 /* TODO: split this out into a formatless button and one that auto fills */
980 enum ui_button_state
ui_colourbutton( ui_rect rect
,
981 enum ui_scheme_colour colour
,
982 enum ui_scheme_colour hover_colour
,
983 enum ui_scheme_colour hi_colour
,
986 int clickup
= ui_click_up(UI_MOUSE_LEFT
),
987 click
= ui_clicking(UI_MOUSE_LEFT
) | clickup
,
988 target
= ui_inside_rect( rect
, vg_ui
.mouse_click
) && click
,
989 hover
= ui_inside_rect( rect
, vg_ui
.mouse
);
991 u32 col_base
= vg_ui
.scheme
[ colour
],
992 col_highlight
= vg_ui
.scheme
[ hi_colour
? hi_colour
: k_ui_fg
],
993 col_hover
= vg_ui
.scheme
[ hover_colour
? hover_colour
:
994 colour
+ k_ui_brighter
];
996 if( vg_ui
.focused_control_type
!= k_ui_control_none
){
1004 vg_ui
.cursor
= k_ui_cursor_hand
;
1011 vg_ui
.ignore_input_frames
= 2;
1015 ui_fill( rect
, col_highlight
);
1016 rect_copy( rect
, vg_ui
.click_fader
);
1017 rect_copy( rect
, vg_ui
.click_fader_end
);
1018 vg_ui
.click_fader_end
[3] = 0;
1019 ui_rect_center( rect
, vg_ui
.click_fader_end
);
1020 vg_ui
.click_fade_opacity
= 1.0f
;
1023 return k_ui_button_click
;
1026 if( fill
) ui_fill( rect
, col_highlight
);
1027 return k_ui_button_holding_inside
;
1031 if( fill
) ui_fill( rect
, col_base
);
1032 ui_outline( rect
, 1, col_highlight
, 0 );
1033 return k_ui_button_holding_outside
;
1037 if( fill
) ui_fill( rect
, col_base
);
1038 return k_ui_button_none
;
1043 if( fill
) ui_fill( rect
, col_hover
);
1044 return k_ui_button_hover
;
1047 if( fill
) ui_fill( rect
, col_base
);
1048 return k_ui_button_none
;
1053 enum ui_button_state
ui_colourbutton_text(
1054 ui_rect rect
, const char *string
, ui_px scale
,
1055 enum ui_scheme_colour colour
){
1056 enum ui_button_state state
= ui_colourbutton( rect
, colour
, 0, 0, 1 );
1057 ui_rect t
= { 0,0, ui_text_line_width( string
)*scale
, 14*scale
};
1058 ui_rect_center( rect
, t
);
1060 u32 text_colour
= ui_colourcont(colour
);
1061 if( state
== k_ui_button_holding_inside
)
1062 text_colour
= colour
;
1064 ui_text( t
, string
, scale
, k_ui_align_left
, text_colour
);
1068 enum ui_button_state
ui_button_text( ui_rect rect
,
1069 const char *string
, ui_px scale
)
1071 return ui_colourbutton_text( rect
, string
, scale
, k_ui_bg
+4 );
1074 enum ui_button_state
ui_button( ui_rect inout_panel
, const char *string
)
1077 ui_standard_widget( inout_panel
, rect
, 1 );
1078 return ui_colourbutton_text( rect
, string
, 1, k_ui_bg
+4 );
1081 static void ui_enum_post(void);
1082 void ui_postrender(void)
1084 if( vg_ui
.click_fade_opacity
> 0.0f
){
1085 float scale
= vg_ui
.click_fade_opacity
;
1086 scale
= vg_maxf( 1.0f
/255.0f
, scale
*scale
);
1088 vg_ui
.click_fade_opacity
-= vg
.time_frame_delta
* 3.8f
;
1089 u32 colour
= (0x00ffffff & ui_colour(k_ui_fg
))|0x7f000000;
1091 v4f begin
, end
, dest
;
1092 for( int i
=0; i
<4; i
++ ){
1093 begin
[i
] = vg_ui
.click_fader
[i
];
1094 end
[i
] = vg_ui
.click_fader_end
[i
]+1;
1097 v4_lerp( end
, begin
, scale
, dest
);
1100 for( int i
=0; i
<4; i
++ ){
1104 ui_fill( rect
, colour
);
1107 if( vg_ui
.focused_control_type
== k_ui_control_enum
){
1110 else if( vg_ui
.focused_control_type
== k_ui_control_modal
){
1111 ui_rect screen
= {0,0,vg
.window_x
,vg
.window_y
};
1112 ui_fill( screen
, 0xa0000000 );
1113 ui_rect box
= {0,0,400,200};
1115 u32 colour
= ui_colour(k_ui_fg
),
1116 type
= vg_ui
.modal
.options
& UI_MODAL_TYPE_BITS
;
1117 if ( type
== 1 ) colour
= ui_colour(k_ui_green
);
1118 else if( type
== 2 ) colour
= ui_colour(k_ui_red
);
1119 else if( type
== 3 ) colour
= ui_colour(k_ui_yellow
);
1121 ui_rect_center( screen
, box
);
1122 ui_fill( box
, ui_colour(k_ui_bg
) );
1123 ui_outline( box
, -1, colour
, 0 );
1126 rect_copy( box
, message
);
1128 ui_rect_center( box
, message
);
1130 ui_rect row0
, row1
, btn
;
1131 ui_split_ratio( message
, k_ui_axis_h
, 0.5f
, 0, row0
, row1
);
1132 row0
[0] += vg_ui
.font
->sx
;
1133 ui_ntext( row0
, vg_ui
.modal
.message
, (box
[2]/vg_ui
.font
->sx
)-2, 1,
1134 k_ui_align_left
, colour
);
1136 rect_copy( row1
, btn
);
1139 ui_rect_center( row1
, btn
);
1141 vg_ui
.focused_control_type
= k_ui_control_none
; /* HACK */
1142 if( ui_button_text( btn
, "OK", 1 ) != 1 )
1143 vg_ui
.focused_control_hit
= 1;
1144 vg_ui
.focused_control_type
= k_ui_control_modal
; /* HACK */
1145 vg_ui
.wants_mouse
= 1;
1148 ui_flush( k_ui_shader_colour
, vg
.window_x
, vg
.window_y
);
1150 if( !vg_ui
.focused_control_hit
){
1154 if( vg_ui
.wants_mouse
){
1155 SDL_SetWindowGrab( vg
.window
, SDL_FALSE
);
1156 SDL_SetRelativeMouseMode( SDL_FALSE
);
1159 SDL_SetWindowGrab( vg
.window
, SDL_TRUE
);
1160 SDL_SetRelativeMouseMode( SDL_TRUE
);
1163 SDL_SetCursor( vg_ui
.cursor_map
[ vg_ui
.cursor
] );
1169 * -----------------------------------------------------------------------------
1172 int ui_checkbox( ui_rect inout_panel
, const char *str_label
, i32
*data
)
1174 ui_rect rect
, label
, box
;
1175 ui_standard_widget( inout_panel
, rect
, 1 );
1177 ui_split( rect
, k_ui_axis_v
, -rect
[3], 0, label
, box
);
1178 ui_text( label
, str_label
, k_ui_scale
, k_ui_align_middle_left
, 0 );
1180 int changed
= ui_colourbutton( box
, k_ui_bg
, 0, 0, 1 )==1;
1182 *data
= (*data
) ^ 0x1;
1185 ui_rect_pad( box
, (ui_px
[2]){4,4} );
1186 ui_fill( box
, ui_colour( k_ui_orange
) );
1194 * -----------------------------------------------------------------------------
1198 * unfortunately no return value since we only find out that event in the
1201 void ui_enum( ui_rect inout_panel
, const char *str_label
,
1202 struct ui_enum_opt
*options
, u32 len
, i32
*value
)
1204 ui_rect rect
, label
, box
;
1205 ui_standard_widget( inout_panel
, rect
, 1 );
1206 ui_label( rect
, str_label
, k_ui_scale
, 0, box
);
1208 const char *display
= "OUT OF RANGE";
1210 for( u32 i
=0; i
<len
; i
++ ){
1211 if( *value
== options
[i
].value
){
1212 display
= options
[i
].alias
;
1218 if( ui_button_text( box
, display
, k_ui_scale
) == 1 ){
1219 vg_ui
.focused_control_type
= k_ui_control_enum
;
1220 vg_ui
.ptr_enum
= value
;
1221 vg_ui
._enum
.option_count
= len
;
1222 vg_ui
._enum
.options
= options
;
1223 rect_copy( box
, vg_ui
._enum
.rect
);
1227 ui_outline( box
, 1, ui_colour(k_ui_red
), 0 );
1230 static void ui_enum_post(void){
1232 rect_copy( vg_ui
._enum
.rect
, drawer
);
1233 drawer
[3] *= vg_ui
._enum
.option_count
;
1236 int clickany
= ui_click_up(UI_MOUSE_LEFT
|UI_MOUSE_RIGHT
|UI_MOUSE_MIDDLE
),
1237 hover
= ui_inside_rect( drawer
, vg_ui
.mouse
);
1239 if( clickany
&& !hover
){
1244 vg_ui
.focused_control_type
= k_ui_control_none
;
1245 i32
*value
= vg_ui
.ptr_enum
;
1247 for( u32 i
=0; i
<vg_ui
._enum
.option_count
; i
++ ){
1249 ui_split( drawer
, k_ui_axis_h
, vg_ui
._enum
.rect
[3], 0, button
,drawer
);
1251 enum ui_scheme_colour colour
= k_ui_bg
+3;
1252 if( vg_ui
._enum
.options
[i
].value
== *value
)
1253 colour
= k_ui_orange
;
1255 if( ui_colourbutton_text( button
, vg_ui
._enum
.options
[i
].alias
,
1256 k_ui_scale
, colour
) == 1 ){
1257 *value
= vg_ui
._enum
.options
[i
].value
;
1263 vg_ui
.focused_control_type
= k_ui_control_enum
;
1266 vg_ui
.focused_control_hit
= 1;
1271 * -----------------------------------------------------------------------------
1274 static enum ui_button_state
_ui_slider(
1275 ui_rect box
, f32 min
, f32 max
, f32
*value
, const char *format
)
1279 enum ui_button_state
1281 k_ui_button_holding_inside
|
1282 k_ui_button_holding_outside
|
1285 mask_using
| k_ui_button_hover
,
1286 state
= ui_colourbutton( box
, k_ui_bg
, k_ui_bg
+2, k_ui_bg
+3, 1 );
1288 if( state
& mask_using
){
1289 t
= vg_clampf( (f32
)(vg_ui
.mouse
[0] - box
[0]) / (f32
)( box
[2] ),
1291 *value
= vg_lerpf( min
, max
, t
);
1294 t
= vg_clampf( (*value
- min
) / (max
-min
), 0.0f
, 1.0f
);
1296 ui_rect line
= { box
[0], box
[1], t
* (f32
)box
[2], box
[3] };
1297 ui_fill( line
, ui_colour(state
&mask_brighter
? k_ui_bg
+4: k_ui_bg
+2) );
1299 ui_outline( box
, 1, ui_colour(state
? k_ui_fg
+3: k_ui_bg
+3), 0 );
1301 /* TODO: replace this one day with our own function */
1303 snprintf( buf
, sizeof(buf
), format
? format
: "%.2f", *value
);
1304 ui_text( box
, buf
, 1, k_ui_align_middle_center
, 0 );
1309 bool ui_slider( ui_rect inout_panel
, const char *str_label
,
1310 f32 min
, f32 max
, f32
*value
, const char *format
)
1312 ui_rect rect
, label
, box
;
1313 ui_standard_widget( inout_panel
, rect
, 1 );
1314 ui_label( rect
, str_label
, k_ui_scale
, 0, box
);
1316 enum ui_button_state mask_using
=
1317 k_ui_button_holding_inside
|
1318 k_ui_button_holding_outside
|
1321 if( _ui_slider( box
, min
, max
, value
, format
) & mask_using
)
1329 * -----------------------------------------------------------------------------
1332 void ui_colourpicker( ui_rect inout_panel
, const char *str_label
, v4f value
)
1334 ui_rect widget
, left
, right
;
1335 ui_standard_widget( inout_panel
, widget
, 8 );
1336 ui_split_ratio( widget
, k_ui_axis_v
, 0.5f
, 8, left
, right
);
1339 ui_split_ratio( right
, k_ui_axis_h
, 0.5f
, 2, sliders
[0], sliders
[2] );
1340 ui_split_ratio( sliders
[0], k_ui_axis_h
, 0.5f
, 2, sliders
[0], sliders
[1] );
1341 ui_split_ratio( sliders
[2], k_ui_axis_h
, 0.5f
, 2, sliders
[2], sliders
[3] );
1344 vg_rgb_hsv( value
, hsv
);
1347 enum ui_button_state modified
= 0x00;
1349 for( u32 i
=0; i
<4; i
++ ){
1350 modified
|= _ui_slider( sliders
[i
], 0.0f
, 1.0f
, hsv
+i
,
1351 (const char *[]){ "hue %.2f",
1354 "alpha %.2f" }[i
] );
1357 ui_rect preview
, square
;
1358 ui_split_ratio( left
, k_ui_axis_v
, 0.8f
, 8, square
, preview
);
1360 u32 state
= ui_colourbutton( square
, 0, 0, 0, 0 );
1363 enum ui_button_state
1365 k_ui_button_holding_inside
|
1366 k_ui_button_holding_outside
|
1369 if( state
& mask_using
){
1370 for( u32 i
=0; i
<2; i
++ ){
1371 hsv
[1+i
] = vg_clampf(
1372 (f32
)(vg_ui
.mouse
[i
] - square
[i
]) / (f32
)(square
[2+i
]),
1376 hsv
[2] = 1.0f
-hsv
[2];
1379 if( modified
& (k_ui_button_click
|k_ui_button_holding_inside
|
1380 k_ui_button_holding_outside
) ){
1381 vg_hsv_rgb( hsv
, value
);
1385 ui_outline( square
, 1, ui_colour( state
? k_ui_fg
+3: k_ui_bg
+3 ), 0 );
1387 /* preview colour */
1389 vg_hsv_rgb( hsv
, colour
);
1391 ui_fill( preview
, v4f_u32_colour( colour
) );
1393 /* Draw hsv shader thingy */
1394 ui_flush( k_ui_shader_colour
, vg
.window_x
, vg
.window_y
);
1395 ui_fill_rect( square
, 0xffffffff, (ui_px
[4]){ 0,256,256,0 } );
1397 ui_flush( k_ui_shader_hsv
, vg
.window_x
, vg
.window_y
);
1399 ui_rect marker
= { square
[0] + hsv
[1] * (f32
)square
[2] - 4,
1400 square
[1] + (1.0f
-hsv
[2]) * (f32
)square
[3] - 4,
1403 square
[1] + (1.0f
-hsv
[2]) * (f32
)square
[3],
1405 ly
= { square
[0] + hsv
[1] * (f32
)square
[2],
1409 ui_fill( marker
, ui_colour( k_ui_fg
) );
1410 ui_fill( lx
, ui_colour( k_ui_fg
) );
1411 ui_fill( ly
, ui_colour( k_ui_fg
) );
1416 * -----------------------------------------------------------------------------
1419 static void _ui_textbox_make_selection( int *start
, int *end
){
1420 *start
= VG_MIN( vg_ui
.textbox
.cursor_pos
, vg_ui
.textbox
.cursor_user
);
1421 *end
= VG_MAX( vg_ui
.textbox
.cursor_pos
, vg_ui
.textbox
.cursor_user
);
1424 void _ui_textbox_move_cursor( int *cursor0
, int *cursor1
,
1425 int dir
, int snap_together
)
1427 *cursor0
= VG_MAX( 0, vg_ui
.textbox
.cursor_user
+ dir
);
1430 VG_MIN( vg_ui
.textbox
.len
-1, strlen( vg_ui
.textbuf
)),
1434 *cursor1
= *cursor0
;
1437 static int _ui_textbox_makeroom( int datastart
, int length
){
1438 int move_to
= VG_MIN( datastart
+length
, vg_ui
.textbox
.len
-1 );
1439 int move_amount
= strlen( vg_ui
.textbuf
)-datastart
;
1440 int move_end
= VG_MIN( move_to
+move_amount
, vg_ui
.textbox
.len
-1 );
1441 move_amount
= move_end
-move_to
;
1444 memmove( &vg_ui
.textbuf
[ move_to
],
1445 &vg_ui
.textbuf
[ datastart
],
1448 vg_ui
.textbuf
[ move_end
] = '\0';
1450 return VG_MIN( length
, vg_ui
.textbox
.len
-datastart
-1 );
1453 int _ui_textbox_delete_char( int direction
)
1456 _ui_textbox_make_selection( &start
, &end
);
1458 /* There is no selection */
1460 if( direction
== 1 ) end
= VG_MIN( end
+1, strlen( vg_ui
.textbuf
) );
1461 else if( direction
== -1 ) start
= VG_MAX( start
-1, 0 );
1464 /* Still no selction, no need to do anything */
1468 /* Copy the end->terminator to start */
1469 int remaining_length
= strlen( vg_ui
.textbuf
)+1-end
;
1470 memmove( &vg_ui
.textbuf
[ start
],
1471 &vg_ui
.textbuf
[ end
],
1476 static void _ui_textbox_to_clipboard(void){
1478 _ui_textbox_make_selection( &start
, &end
);
1482 memcpy( buffer
, &vg_ui
.textbuf
[ start
], end
-start
);
1483 buffer
[ end
-start
] = 0x00;
1484 SDL_SetClipboardText( buffer
);
1488 static void _ui_textbox_change_callback(void){
1489 if( vg_ui
.textbox
.callbacks
.change
){
1490 vg_ui
.textbox
.callbacks
.change( vg_ui
.textbuf
, vg_ui
.textbox
.len
);
1492 /* we gave permission to modify the buffer in this callback so.. */
1493 int len
= strlen( vg_ui
.textbuf
);
1494 vg_ui
.textbox
.cursor_user
= VG_MIN( vg_ui
.textbox
.cursor_user
, len
);
1495 vg_ui
.textbox
.cursor_pos
= VG_MIN( vg_ui
.textbox
.cursor_pos
, len
);
1499 void ui_start_modal( const char *message
, u32 options
);
1500 static void _ui_textbox_clipboard_paste(void){
1501 if( !SDL_HasClipboardText() )
1504 char *text
= SDL_GetClipboardText();
1509 int datastart
= _ui_textbox_delete_char( 0 );
1510 int length
= strlen( text
);
1512 if( (vg_ui
.textbox
.len
- strlen(vg_ui
.textbuf
)) < length
){
1513 ui_start_modal( "Clipboard content exceeds buffer size.", UI_MODAL_BAD
);
1517 int cpylength
= _ui_textbox_makeroom( datastart
, length
);
1519 memcpy( vg_ui
.textbuf
+ datastart
, text
, cpylength
);
1520 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
,
1521 &vg_ui
.textbox
.cursor_pos
, cpylength
, 1 );
1523 _ui_textbox_change_callback();
1526 void _ui_textbox_put_char( char c
)
1528 vg_ui
.textbox
.cursor_user
= _ui_textbox_delete_char(0);
1529 if( (vg_ui
.textbox
.len
- strlen(vg_ui
.textbuf
)) <= 1 ) return;
1531 if( _ui_textbox_makeroom( vg_ui
.textbox
.cursor_user
, 1 ) )
1532 vg_ui
.textbuf
[ vg_ui
.textbox
.cursor_user
] = c
;
1534 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
,
1535 &vg_ui
.textbox
.cursor_pos
, 1, 1 );
1538 /* Receed secondary cursor */
1539 void _ui_textbox_left_select(void)
1541 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
, NULL
, -1, 0 );
1544 /* Match and receed both cursors */
1545 void _ui_textbox_left(void)
1547 int cursor_diff
= vg_ui
.textbox
.cursor_pos
- vg_ui
.textbox
.cursor_user
? 0: 1;
1549 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
,
1550 &vg_ui
.textbox
.cursor_pos
, -cursor_diff
, 1 );
1553 void _ui_textbox_up(void)
1555 if( vg_ui
.textbox
.flags
& UI_TEXTBOX_MULTILINE
){
1556 int line_begin
= vg_ui
.textbox
.cursor_user
;
1558 while( line_begin
){
1559 if( vg_ui
.textbuf
[ line_begin
-1 ] == '\n' ){
1567 int line_above_begin
= line_begin
-1;
1569 while( line_above_begin
){
1570 if( vg_ui
.textbuf
[ line_above_begin
-1 ] == '\n' ){
1574 line_above_begin
--;
1577 int offset
= vg_ui
.textbox
.cursor_user
- line_begin
,
1578 line_length_above
= line_begin
- line_above_begin
-1;
1580 offset
= VG_MIN( line_length_above
, offset
);
1582 vg_ui
.textbox
.cursor_user
= line_above_begin
+offset
;
1583 vg_ui
.textbox
.cursor_pos
= line_above_begin
+offset
;
1586 vg_ui
.textbox
.cursor_user
= line_begin
;
1587 vg_ui
.textbox
.cursor_pos
= line_begin
;
1591 if( vg_ui
.textbox
.callbacks
.up
){
1592 vg_ui
.textbox
.callbacks
.up( vg_ui
.textbuf
, vg_ui
.textbox
.len
);
1597 void _ui_textbox_down(void)
1599 if( vg_ui
.textbox
.flags
& UI_TEXTBOX_MULTILINE
){
1600 int line_begin
= vg_ui
.textbox
.cursor_user
;
1602 while( line_begin
){
1603 if( vg_ui
.textbuf
[ line_begin
-1 ] == '\n' ){
1610 int line_below_begin
= vg_ui
.textbox
.cursor_user
;
1613 if( vg_ui
.textbuf
[ line_below_begin
] == '\0' ){
1614 vg_ui
.textbox
.cursor_user
= line_below_begin
;
1615 vg_ui
.textbox
.cursor_pos
= line_below_begin
;
1619 if( vg_ui
.textbuf
[ line_below_begin
] == '\n' ){
1620 line_below_begin
++;
1624 line_below_begin
++;
1627 int line_below_end
= line_below_begin
;
1629 if( vg_ui
.textbuf
[ line_below_end
] == '\0' ||
1630 vg_ui
.textbuf
[ line_below_end
] == '\n' ){
1637 int offset
= vg_ui
.textbox
.cursor_user
- line_begin
,
1638 line_length_below
= line_below_end
- line_below_begin
-1;
1640 offset
= VG_MIN( line_length_below
, offset
);
1642 vg_ui
.textbox
.cursor_user
= line_below_begin
+offset
;
1643 vg_ui
.textbox
.cursor_pos
= line_below_begin
+offset
;
1646 if( vg_ui
.textbox
.callbacks
.down
){
1647 vg_ui
.textbox
.callbacks
.down( vg_ui
.textbuf
, vg_ui
.textbox
.len
);
1652 void _ui_textbox_right_select(void)
1654 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
, NULL
, 1, 0 );
1657 void _ui_textbox_right(void)
1659 int cursor_diff
= vg_ui
.textbox
.cursor_pos
- vg_ui
.textbox
.cursor_user
? 0: 1;
1661 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
,
1662 &vg_ui
.textbox
.cursor_pos
, +cursor_diff
, 1 );
1665 void _ui_textbox_backspace(void)
1667 if( vg_ui
.focused_control_type
== k_ui_control_textbox
){
1668 vg_ui
.textbox
.cursor_user
= _ui_textbox_delete_char( -1 );
1669 vg_ui
.textbox
.cursor_pos
= vg_ui
.textbox
.cursor_user
;
1670 _ui_textbox_change_callback();
1674 void _ui_textbox_delete(void)
1676 if( vg_ui
.focused_control_type
== k_ui_control_textbox
){
1677 vg_ui
.textbox
.cursor_user
= _ui_textbox_delete_char( 1 );
1678 vg_ui
.textbox
.cursor_pos
= vg_ui
.textbox
.cursor_user
;
1679 _ui_textbox_change_callback();
1683 void _ui_textbox_home_select(void)
1685 i32 start
= vg_ui
.textbox
.cursor_user
;
1687 if( vg_ui
.textbox
.flags
& UI_TEXTBOX_MULTILINE
){
1689 if( vg_ui
.textbuf
[start
-1] == '\n' )
1698 vg_ui
.textbox
.cursor_user
= start
;
1701 void _ui_textbox_home(void)
1703 _ui_textbox_home_select();
1704 vg_ui
.textbox
.cursor_pos
= vg_ui
.textbox
.cursor_user
;
1707 void _ui_textbox_end_select(void)
1709 i32 end
= vg_ui
.textbox
.cursor_user
;
1711 if( vg_ui
.textbox
.flags
& UI_TEXTBOX_MULTILINE
){
1712 while( vg_ui
.textbuf
[end
] ){
1713 if( vg_ui
.textbuf
[end
] == '\n' )
1720 end
= VG_MIN( vg_ui
.textbox
.len
-1, strlen(vg_ui
.textbuf
) );
1722 vg_ui
.textbox
.cursor_user
= end
;
1725 void _ui_textbox_end(void)
1727 _ui_textbox_end_select();
1728 vg_ui
.textbox
.cursor_pos
= vg_ui
.textbox
.cursor_user
;
1731 void _ui_textbox_select_all(void)
1733 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
, NULL
, 10000, 0);
1734 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_pos
, NULL
, -10000, 0);
1737 void _ui_textbox_cut(void)
1739 _ui_textbox_to_clipboard();
1740 vg_ui
.textbox
.cursor_user
= _ui_textbox_delete_char(0);
1741 vg_ui
.textbox
.cursor_pos
= vg_ui
.textbox
.cursor_user
;
1742 _ui_textbox_change_callback();
1745 void _ui_textbox_enter(void)
1747 if( vg_ui
.focused_control_type
== k_ui_control_textbox
){
1748 vg_ui
.ignore_input_frames
= 2;
1750 if( vg_ui
.textbox
.callbacks
.enter
)
1751 vg_ui
.textbox
.callbacks
.enter( vg_ui
.textbuf
, vg_ui
.textbox
.len
);
1753 if( vg_ui
.focused_control_type
!= k_ui_control_textbox
) return;
1755 if( vg_ui
.textbox
.flags
& UI_TEXTBOX_MULTILINE
){
1756 _ui_textbox_put_char( '\n' );
1757 _ui_textbox_change_callback();
1760 if( !(vg_ui
.textbox
.flags
& UI_TEXTBOX_AUTOFOCUS
) )
1767 * based on a visual character coordinate relative to the anchor of the textbox,
1768 * this works out the linear place in the buffer that coordinate maps to
1770 * input coordinates go in co[0], co[1], and the result index is in co[2]
1772 static void _ui_textbox_calc_index_from_grid( int co
[3], int wrap_length
){
1776 while( (c
= vg_ui
.textbuf
[i
[2]]) ){
1777 if( i
[1]==co
[1] && i
[0]>=co
[0] ) break;
1779 if( i
[0] >= wrap_length
){
1784 if( c
>= 32 && c
<= 126 ){
1788 else if( c
== '\n' ){
1791 if( i
[1] > co
[1] ) break;
1805 * based on the index specied in co[2], work out the visual character
1806 * coordinates and store them in co[0], co[1]
1808 static void _ui_textbox_index_calc_coords( int co
[3], int wrap_length
){
1815 while( (c
= vg_ui
.textbuf
[i
++]) ){
1816 if( i
> co
[2] ) break;
1817 if( co
[0] >= wrap_length
){
1821 if( c
>= 32 && c
<= 126 ) co
[0] ++;
1822 else if( c
== '\n' ){
1830 * calculate the number of characters remaining until either:
1831 * - the wrap_length limit is hit
1832 * - end of the line/string
1834 * index must be fully populated with visual X/Y, and linear index
1836 static int _ui_textbox_run_remaining( int index
[3], int wrap_length
){
1837 int i
=0, printed_chars
=0;
1839 while( (c
= vg_ui
.textbuf
[index
[2] + (i
++)]) ){
1840 if( index
[0]+i
>= wrap_length
) break;
1841 if( c
>= 32 && c
<= 126 ) printed_chars
++;
1842 else if( c
== '\n' ) break;
1845 return printed_chars
+1;
1848 int ui_textbox( ui_rect inout_panel
, const char *label
,
1849 char *buf
, u32 len
, u32 lines
, u32 flags
,
1850 struct ui_textbox_callbacks
*callbacks
)
1852 if( lines
> 1 ) flags
|= UI_TEXTBOX_MULTILINE
;
1855 ui_standard_widget( inout_panel
, rect
, lines
);
1858 ui_label( rect
, label
, 1, 0, rect
);
1860 int clickup
= ui_click_up(UI_MOUSE_LEFT
),
1861 clickdown
= ui_click_down(UI_MOUSE_LEFT
),
1862 click
= ui_clicking(UI_MOUSE_LEFT
) | clickup
,
1863 target
= ui_inside_rect( rect
, vg_ui
.mouse_click
) && click
,
1864 hover
= ui_inside_rect( rect
, vg_ui
.mouse
);
1866 /* allow instant transitions from textbox->textbox */
1867 if( (vg_ui
.focused_control_type
!= k_ui_control_none
) &&
1868 (vg_ui
.focused_control_type
!= k_ui_control_textbox
) ){
1874 flags
&= ~UI_TEXTBOX_AUTOFOCUS
;
1877 u32 col_base
= ui_colour( k_ui_bg
),
1878 col_highlight
= ui_colour( k_ui_fg
),
1879 col_cursor
= (0x00ffffff & ui_colour(k_ui_fg
))|0x7f000000;
1884 rect_copy( rect
, text_rect
);
1886 if( flags
& UI_TEXTBOX_MULTILINE
) text_rect
[3] = rect
[3]-16;
1887 else text_rect
[3] = vg_ui
.font
->sy
;
1890 ui_rect_center( rect
, text_rect
);
1892 ui_px wrap_length
= 1024;
1894 if( flags
& UI_TEXTBOX_WRAP
)
1895 wrap_length
= text_rect
[2] / vg_ui
.font
->sx
;
1899 vg_ui
.cursor
= k_ui_cursor_ibeam
;
1902 if( vg_ui
.focused_control_id
== buf
)
1904 ui_fill( rect
, col_base
);
1905 ui_ntext( text_rect
, buf
, wrap_length
, 1, k_ui_align_left
, 0 );
1907 if( !(flags
& UI_TEXTBOX_AUTOFOCUS
) && ((clickup
||clickdown
) && !target
))
1913 vg_ui
.focused_control_hit
= 1;
1914 if( click
&& target
){
1916 (vg_ui
.mouse_click
[0] - text_rect
[0]) / vg_ui
.font
->sx
,
1917 (vg_ui
.mouse_click
[1] - text_rect
[1]) / vg_ui
.font
->sy
,
1921 (vg_ui
.mouse
[0] - text_rect
[0]) / vg_ui
.font
->sx
,
1922 (vg_ui
.mouse
[1] - text_rect
[1]) / vg_ui
.font
->sy
,
1926 if( flags
& UI_TEXTBOX_MULTILINE
)
1928 _ui_textbox_calc_index_from_grid( p0
, wrap_length
);
1929 _ui_textbox_calc_index_from_grid( p1
, wrap_length
);
1931 vg_ui
.textbox
.cursor_pos
= p0
[2];
1932 vg_ui
.textbox
.cursor_user
= p1
[2];
1936 int max
= strlen( buf
);
1937 vg_ui
.textbox
.cursor_pos
= VG_MAX( 0, VG_MIN( max
, p0
[0] )),
1938 vg_ui
.textbox
.cursor_user
= VG_MAX( 0, VG_MIN( max
, p1
[0] ));
1942 ui_outline( rect
, -2, vg_ui
.scheme
[ k_ui_orange
], 0 );
1946 int c0
= vg_ui
.textbox
.cursor_pos
,
1947 c1
= vg_ui
.textbox
.cursor_user
,
1948 start
= VG_MIN( c0
, c1
),
1949 end
= VG_MAX( c0
, c1
),
1952 if( flags
& (UI_TEXTBOX_WRAP
|UI_TEXTBOX_MULTILINE
) )
1954 int pos
[3], remaining
= chars
;
1957 _ui_textbox_index_calc_coords( pos
, wrap_length
);
1961 cursor
[0] = text_rect
[0] + pos
[0]*vg_ui
.font
->sx
-1;
1962 cursor
[1] = text_rect
[1] + pos
[1]*14;
1965 ui_fill( cursor
, col_cursor
);
1966 rect_copy( cursor
, vg_ui
.click_fader_end
);
1972 int run
= _ui_textbox_run_remaining( pos
, wrap_length
);
1973 run
= VG_MIN( run
, remaining
);
1975 cursor
[0] = text_rect
[0] + pos
[0]*vg_ui
.font
->sx
-1;
1976 cursor
[1] = text_rect
[1] + pos
[1]*14;
1977 cursor
[2] = (float)(run
)*(float)vg_ui
.font
->sx
;
1980 ui_fill( cursor
, col_cursor
);
1987 rect_copy( cursor
, vg_ui
.click_fader_end
);
1992 cursor
[0] = text_rect
[0] + start
*vg_ui
.font
->sx
-1;
1993 cursor
[1] = text_rect
[1];
2002 cursor
[2] = (float)(chars
)*(float)vg_ui
.font
->sx
;
2005 if( (vg_ui
.click_fade_opacity
<=0.0f
) &&
2006 ui_clip( rect
, cursor
, cursor
) )
2008 ui_fill( cursor
, col_cursor
);
2011 rect_copy( cursor
, vg_ui
.click_fader_end
);
2018 if( click
|| (flags
& UI_TEXTBOX_AUTOFOCUS
) )
2020 if( (target
&& hover
) || (flags
& UI_TEXTBOX_AUTOFOCUS
) )
2024 ui_fill( rect
, col_highlight
);
2025 vg_ui
.ignore_input_frames
= 2;
2026 rect_copy( rect
, vg_ui
.click_fader
);
2027 rect_copy( rect
, vg_ui
.click_fader_end
);
2029 vg_ui
.click_fade_opacity
= 1.0f
;
2030 vg_ui
.textbuf
= buf
;
2031 vg_ui
.focused_control_hit
= 1;
2032 vg_ui
.focused_control_type
= k_ui_control_textbox
;
2033 vg_ui
.textbox
.len
= len
;
2034 vg_ui
.textbox
.flags
= flags
;
2035 vg_ui
.textbox
.cursor_pos
= 0;
2036 vg_ui
.textbox
.cursor_user
= 0;
2040 vg_ui
.textbox
.callbacks
= *callbacks
;
2044 vg_ui
.textbox
.callbacks
.change
= NULL
;
2045 vg_ui
.textbox
.callbacks
.down
= NULL
;
2046 vg_ui
.textbox
.callbacks
.up
= NULL
;
2047 vg_ui
.textbox
.callbacks
.enter
= NULL
;
2050 SDL_StartTextInput();
2054 ui_fill( rect
, col_base
);
2058 ui_outline( rect
, -1, col_highlight
, 0 );
2061 ui_ntext( text_rect
, buf
, wrap_length
, 1, k_ui_align_left
, 0 );
2067 * -----------------------------------------------------------------------------
2070 void ui_tabs( ui_rect inout_panel
, ui_rect out_content_panel
,
2071 const char **titles
, u32 count
, i32
*page
)
2074 ui_standard_widget( inout_panel
, bar
, 1 );
2076 i32 cur_page
= *page
;
2078 f32 width
= (f32
)inout_panel
[2] / (f32
)count
;
2080 ui_px h
= (inout_panel
[1] + inout_panel
[3]) - (bar
[1]+bar
[3]);
2081 inout_panel
[1] = bar
[1]+bar
[3];
2084 ui_fill( inout_panel
, ui_colour( k_ui_bg
+2 ) );
2085 ui_outline( inout_panel
, 1, ui_colour( k_ui_bg
+5 ), 0 );
2087 rect_copy( inout_panel
, out_content_panel
);
2088 ui_rect_pad( out_content_panel
, (ui_px
[2]){ k_ui_padding
, k_ui_padding
} );
2091 for( i32 i
=0; i
<count
; i
++ )
2094 bar
[0] + ((f32
)i
*width
),
2100 enum ui_scheme_colour colour
= k_ui_bg
+4;
2104 ui_outline( button
, 1, ui_colour( k_ui_bg
+5 ),
2105 UI_TOP
|UI_LEFT
|UI_RIGHT
);
2109 if( ui_colourbutton_text( button
, titles
[i
], 1, colour
) == 1 )
2116 * -----------------------------------------------------------------------------
2118 void ui_start_modal( const char *message
, u32 options
)
2120 vg_ui
.focused_control_type
= k_ui_control_modal
;
2121 vg_ui
.modal
.message
= message
;
2122 vg_ui
.modal
.callbacks
.close
= NULL
;
2123 vg_ui
.modal
.options
= options
;
2125 u32 type
= options
& UI_MODAL_TYPE_BITS
;
2126 if( type
== UI_MODAL_OK
) vg_info( message
);
2127 else if( type
== UI_MODAL_WARN
) vg_warn( message
);
2128 else if( type
== UI_MODAL_GOOD
) vg_success( message
);
2129 else if( type
== UI_MODAL_BAD
) vg_error( message
);
2134 * -----------------------------------------------------------------------------
2140 void ui_proc_key( SDL_Keysym ev
)
2142 if( vg_ui
.focused_control_type
!= k_ui_control_textbox
){
2146 struct textbox_mapping
{
2150 void (*handler
)(void);
2154 { 0, SDLK_LEFT
, _ui_textbox_left
},
2155 { KMOD_SHIFT
, SDLK_LEFT
, _ui_textbox_left_select
},
2156 { 0, SDLK_RIGHT
, _ui_textbox_right
},
2157 { KMOD_SHIFT
, SDLK_RIGHT
, _ui_textbox_right_select
},
2158 { 0, SDLK_DOWN
, _ui_textbox_down
},
2159 { 0, SDLK_UP
, _ui_textbox_up
},
2160 { 0, SDLK_BACKSPACE
, _ui_textbox_backspace
},
2161 { KMOD_SHIFT
, SDLK_BACKSPACE
, _ui_textbox_backspace
},
2162 { KMOD_CTRL
, SDLK_BACKSPACE
, _ui_textbox_backspace
},
2163 { 0, SDLK_DELETE
, _ui_textbox_delete
},
2164 { 0, SDLK_HOME
, _ui_textbox_home
},
2165 { KMOD_SHIFT
, SDLK_HOME
, _ui_textbox_home_select
},
2166 { 0, SDLK_END
, _ui_textbox_end
},
2167 { KMOD_SHIFT
, SDLK_END
, _ui_textbox_end_select
},
2168 { KMOD_CTRL
, SDLK_a
, _ui_textbox_select_all
},
2169 { KMOD_CTRL
, SDLK_c
, _ui_textbox_to_clipboard
},
2170 { KMOD_CTRL
, SDLK_x
, _ui_textbox_cut
},
2171 { KMOD_CTRL
, SDLK_v
, _ui_textbox_clipboard_paste
},
2172 { 0, SDLK_RETURN
, _ui_textbox_enter
},
2173 { 0, SDLK_ESCAPE
, ui_defocus_all
},
2178 if( ev
.mod
& KMOD_SHIFT
)
2181 if( ev
.mod
& KMOD_CTRL
)
2184 if( ev
.mod
& KMOD_ALT
)
2187 for( int i
=0; i
<vg_list_size( mappings
); i
++ ){
2188 struct textbox_mapping
*mapping
= &mappings
[i
];
2190 if( mapping
->key
== ev
.sym
){
2191 if( mapping
->mod
== 0 ){
2197 else if( (mod
& mapping
->mod
) == mapping
->mod
){
2206 * Callback for text entry mode
2208 void ui_proc_utf8( const char *text
)
2210 if( vg_ui
.focused_control_type
== k_ui_control_textbox
){
2211 const char *ptr
= text
;
2214 if( *ptr
!= '`' ) _ui_textbox_put_char( *ptr
);
2218 _ui_textbox_change_callback();
2224 * -----------------------------------------------------------------------------
2227 void ui_dev_colourview(void)
2229 ui_rect window
= {vg
.window_x
-256,0,256,vg
.window_y
}, swatch
;
2231 const char *names
[vg_list_size(vg_ui
.scheme
)] = {
2232 [k_ui_bg
] = "k_ui_bg", "k_ui_bg+1", "k_ui_bg+2", "k_ui_bg+3",
2233 "k_ui_bg+4", "k_ui_bg+5", "k_ui_bg+6", "k_ui_bg+7",
2235 [k_ui_fg
] = "k_ui_fg", "k_ui_fg+1", "k_ui_fg+2", "k_ui_fg+3",
2236 "k_ui_fg+4", "k_ui_fg+5", "k_ui_fg+6", "k_ui_fg+7",
2238 [k_ui_red
] = "k_ui_red", "k_ui_orange", "k_ui_yellow", "k_ui_green",
2239 "k_ui_aqua", "k_ui_blue", "k_ui_purple", "k_ui_gray",
2240 "k_ui_red+8","k_ui_orange+8","k_ui_yellow+8","k_ui_green+8",
2241 "k_ui_aqua+8","k_ui_blue+8","k_ui_purple+8","k_ui_gray+8" };
2244 ui_split_ratio( window
, k_ui_axis_v
, 0.5f
, 0, col
[0], col
[1] );
2246 for( int i
=0; i
<vg_list_size(vg_ui
.scheme
); i
++ ){
2247 int which
= (i
/8)%2;
2249 ui_split( col
[which
], k_ui_axis_h
, 24, 0, swatch
, col
[which
] );
2250 ui_fill( swatch
, ui_colour(i
) );
2253 ui_text(swatch
, names
[i
], 1, k_ui_align_middle_left
, ui_colourcont(i
));