1 /* Copyright (C) 2021-2023 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
20 #include "vg/vg_tex.h"
21 #include "vg/vg_shader.h"
24 typedef ui_px ui_rect
[4];
25 typedef ui_px ui_point
[2];
26 typedef struct ui_vert ui_vert
;
33 /* Relative to cursor p0 */
36 k_ui_align_lwr
= 0xff,
37 k_ui_align_left
= 0x0000| 0x00,
38 k_ui_align_right
= 0x0000| 0x01,
39 k_ui_align_center
= 0x0000| 0x02,
41 k_ui_align_middle
= 0x0100,
42 k_ui_align_middle_left
= 0x0100| 0x00,
43 k_ui_align_middle_right
= 0x0100| 0x01,
44 k_ui_align_middle_center
= 0x0100| 0x02,
46 k_ui_align_bottom
= 0x0200,
47 k_ui_align_bottom_left
= 0x0200| 0x00,
48 k_ui_align_bottom_right
= 0x0200| 0x01,
49 k_ui_align_bottom_center
= 0x0200| 0x02,
61 enum ui_scheme_colour
{
76 static ui_px k_ui_widget_height
= 28,
80 typedef u32 ui_scheme
[8*4];
93 typedef struct ui_font ui_font
;
95 static const ui_font vg_ui_font_small
= {
116 #define UI_RGB( STDHEX ) 0xff000000 |\
117 ((STDHEX&0x000000ff)<<16) |\
118 ((STDHEX&0x0000ff00) ) |\
119 ((STDHEX&0x00ff0000)>>16)
121 #define UI_TEXTBOX_MULTILINE 0x1
122 #define UI_TEXTBOX_WRAP 0x2
123 #define UI_TEXTBOX_AUTOFOCUS 0x4
125 #define UI_MODAL_OK 0x0
126 #define UI_MODAL_GOOD 0x1
127 #define UI_MODAL_BAD 0x2
128 #define UI_MODAL_WARN 0x3
129 #define UI_MODAL_TYPE_BITS 0x3
131 #define UI_MOUSE_LEFT (SDL_BUTTON(SDL_BUTTON_LEFT))
132 #define UI_MOUSE_RIGHT (SDL_BUTTON(SDL_BUTTON_RIGHT))
133 #define UI_MOUSE_MIDDLE (SDL_BUTTON(SDL_BUTTON_MIDDLE))
137 #define UI_BOTTOM 0x4
141 struct ui_vert
*vertex_buffer
;
143 u32 max_verts
, max_indices
,
144 cur_vert
, cur_indice
,
145 vert_start
, indice_start
;
148 void *focused_control_id
; /* uses the memory location of various locking
154 u32 focused_control_hit
;
155 enum ui_control_type
{
157 k_ui_control_textbox
,
161 focused_control_type
;
163 union{ /* controls that can be focused */
165 int cursor_user
, cursor_pos
;
169 struct ui_textbox_callbacks
{
170 void (*enter
)( char *, u32
),
171 (*up
)( char *, u32
),
172 (*down
)( char *, u32
),
173 (*change
)( char *, u32
),
196 struct ui_modal_callbacks
{
203 GLuint tex_glyphs
, vao
, vbo
, ebo
, tex_bg
;
204 v2f bg_inverse_ratio
;
206 ui_px mouse
[2], mouse_click
[2];
208 u32 ignore_input_frames
;
211 ui_rect click_fader
, click_fader_end
;
212 float click_fade_opacity
;
226 SDL_Cursor
*cursor_map
[ k_ui_cursor_max
];
229 f32 hue
; /* lol this sucks */
233 [ k_ui_bg
+0 ] = UI_RGB( 0x1d2021 ),
234 [ k_ui_bg
+1 ] = UI_RGB( 0x282828 ),
235 [ k_ui_bg
+2 ] = UI_RGB( 0x3c3836 ),
236 [ k_ui_bg
+3 ] = UI_RGB( 0x504945 ),
237 [ k_ui_bg
+4 ] = UI_RGB( 0x665c54 ),
238 [ k_ui_bg
+5 ] = UI_RGB( 0x7c6f64 ),
239 [ k_ui_bg
+6 ] = UI_RGB( 0x928374 ),
240 [ k_ui_bg
+7 ] = UI_RGB( 0xa89984 ),
242 [ k_ui_fg
+0 ] = UI_RGB( 0xebdbb2 ),
243 [ k_ui_fg
+1 ] = UI_RGB( 0xfbf1c7 ),
244 [ k_ui_fg
+2 ] = UI_RGB( 0xd5c4a1 ),
245 [ k_ui_fg
+3 ] = UI_RGB( 0xbdae93 ),
246 [ k_ui_fg
+4 ] = UI_RGB( 0xa89984 ),
247 [ k_ui_fg
+5 ] = UI_RGB( 0x000000 ),
248 [ k_ui_fg
+6 ] = UI_RGB( 0x000000 ),
249 [ k_ui_fg
+7 ] = UI_RGB( 0x000000 ),
251 [ k_ui_red
] = UI_RGB( 0xcc241d ),
252 [ k_ui_orange
] = UI_RGB( 0xd65d0e ),
253 [ k_ui_yellow
] = UI_RGB( 0xd79921 ),
254 [ k_ui_green
] = UI_RGB( 0x98971a ),
255 [ k_ui_aqua
] = UI_RGB( 0x689d6a ),
256 [ k_ui_blue
] = UI_RGB( 0x458588 ),
257 [ k_ui_purple
] = UI_RGB( 0xb16286 ),
258 [ k_ui_gray
] = UI_RGB( 0x928374 ),
259 [ k_ui_red
+ k_ui_brighter
] = UI_RGB( 0xfb4934 ),
260 [ k_ui_orange
+ k_ui_brighter
] = UI_RGB( 0xfe8019 ),
261 [ k_ui_yellow
+ k_ui_brighter
] = UI_RGB( 0xfabd2f ),
262 [ k_ui_green
+ k_ui_brighter
] = UI_RGB( 0xb8bb26 ),
263 [ k_ui_aqua
+ k_ui_brighter
] = UI_RGB( 0x8ec07c ),
264 [ k_ui_blue
+ k_ui_brighter
] = UI_RGB( 0x83a598 ),
265 [ k_ui_purple
+ k_ui_brighter
] = UI_RGB( 0xd3869b ),
266 [ k_ui_gray
+ k_ui_brighter
] = UI_RGB( 0xa89984 ),
268 .font
= &vg_ui_font_small
,
269 .colour
= {1.0f
,1.0f
,1.0f
,1.0f
},
270 .bg_inverse_ratio
= {1,1}
273 static struct vg_shader _shader_ui
={
274 .name
= "[vg] ui - transparent",
279 "layout (location=0) in vec2 a_co;"
280 "layout (location=1) in vec2 a_uv;"
281 "layout (location=2) in vec4 a_colour;"
283 "uniform vec2 uBGInverseRatio;"
285 "out vec4 aTexCoords;"
289 "vec4 proj_pos = vec4( uPv * vec3( a_co, 1.0 ), 1.0 );"
290 "gl_Position = proj_pos;"
291 "aTexCoords = vec4( a_uv * 0.00390625, "
292 " (proj_pos.xy*0.5+0.5) * uBGInverseRatio );"
293 "aColour = a_colour;"
299 "uniform sampler2D uTexGlyphs;"
300 "uniform sampler2D uTexBG;"
301 "uniform vec4 uColour;"
302 "uniform float uSpread;"
303 "out vec4 FragColor;"
305 "in vec4 aTexCoords;"
308 "vec2 rand_hash22( vec2 p ){"
309 "vec3 p3 = fract(vec3(p.xyx) * 213.8976123);"
310 "p3 += dot(p3, p3.yzx+19.19);"
311 "return fract(vec2((p3.x + p3.y)*p3.z, (p3.x+p3.z)*p3.y));"
315 "vec4 diffuse = aColour;"
317 "vec4 avg = vec4(0.0);"
319 "if( aColour.a == 0.0 ){"
321 "avg.a = texture( uTexGlyphs, aTexCoords.xy ).r;"
324 "if( uSpread > 0.0001 ){"
325 "for( int i=0; i<4; i ++ ){"
326 "vec2 spread = rand_hash22(aTexCoords.zw+vec2(float(i)));"
327 "avg += texture( uTexBG, aTexCoords.zw + (spread-0.5)*uSpread );"
331 "avg.rgb = mix( avg.rgb, aColour.rgb, aColour.a );"
338 "FragColor = avg * uColour;"
343 static struct vg_shader _shader_ui_image
= {
344 .name
= "[vg] ui_image",
350 "layout (location=0) in vec2 a_co;"
351 "layout (location=1) in vec2 a_uv;"
352 "layout (location=2) in vec4 a_colour;"
355 "out vec2 aTexCoords;"
361 "gl_Position = vec4( uPv * vec3( a_co, 1.0 ), 1.0 );"
362 "aTexCoords = a_uv * 0.00390625;"
363 "aColour = a_colour;"
372 "uniform sampler2D uTexImage;"
373 "uniform vec4 uColour;"
374 "out vec4 FragColor;"
376 "in vec2 aTexCoords;"
382 "vec4 colour = texture( uTexImage, aTexCoords );"
383 "FragColor = colour * uColour;"
388 static struct vg_shader _shader_ui_hsv
= {
389 .name
= "[vg] ui_hsv",
394 "layout (location=0) in vec2 a_co;"
395 "layout (location=1) in vec2 a_uv;"
396 "layout (location=2) in vec4 a_colour;"
399 "out vec2 aTexCoords;"
405 "gl_Position = vec4( uPv * vec3( a_co, 1.0 ), 1.0 );"
406 "aTexCoords = a_uv * 0.00390625;"
407 "aColour = a_colour;"
416 "uniform float uHue;"
417 "out vec4 FragColor;"
419 "in vec2 aTexCoords;"
425 "vec3 c = vec3( uHue, aTexCoords );"
426 "vec4 K = vec4(1.0,2.0/3.0,1.0/3.0,3.0);"
427 "vec3 p = abs(fract(c.xxx+K.xyz)*6.0 - K.www);"
428 "vec3 colour = c.z*mix(K.xxx,clamp(p-K.xxx,0.0,1.0),c.y);"
429 "FragColor = vec4( colour, 1.0 );"
434 static void _vg_ui_init(void){
435 if( !vg_shader_compile( &_shader_ui
) ||
436 !vg_shader_compile( &_shader_ui_image
) ||
437 !vg_shader_compile( &_shader_ui_hsv
) ){
438 vg_fatal_error( "Failed to compile ui shader" );
443 * ----------------------------------------
446 vg_ui
.max_indices
= 20000;
447 vg_ui
.max_verts
= 30000;
449 /* Generate the buffer we are gonna be drawing to */
450 glGenVertexArrays( 1, &vg_ui
.vao
);
451 glGenBuffers( 1, &vg_ui
.vbo
);
452 glGenBuffers( 1, &vg_ui
.ebo
);
454 glBindVertexArray( vg_ui
.vao
);
455 glBindBuffer( GL_ARRAY_BUFFER
, vg_ui
.vbo
);
457 glBufferData( GL_ARRAY_BUFFER
,
458 vg_ui
.max_verts
* sizeof( struct ui_vert
),
459 NULL
, GL_DYNAMIC_DRAW
);
460 glBindVertexArray( vg_ui
.vao
);
462 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, vg_ui
.ebo
);
463 glBufferData( GL_ELEMENT_ARRAY_BUFFER
,
464 vg_ui
.max_indices
* sizeof( u16
), NULL
, GL_DYNAMIC_DRAW
);
469 u32
const stride
= sizeof( struct ui_vert
);
472 glVertexAttribPointer( 0, 2, GL_SHORT
, GL_FALSE
, stride
,
473 (void *)offsetof( struct ui_vert
, co
) );
474 glEnableVertexAttribArray( 0 );
477 glVertexAttribPointer( 1, 2, GL_UNSIGNED_SHORT
, GL_FALSE
, stride
,
478 (void *)offsetof( struct ui_vert
, uv
) );
479 glEnableVertexAttribArray( 1 );
482 glVertexAttribPointer( 2, 4, GL_UNSIGNED_BYTE
, GL_TRUE
, stride
,
483 (void *)offsetof( struct ui_vert
, colour
) );
484 glEnableVertexAttribArray( 2 );
488 /* Alloc RAM default context */
489 u32 vert_size
= vg_ui
.max_verts
*sizeof(struct ui_vert
),
490 inds_size
= vg_align8( vg_ui
.max_indices
*sizeof(u16
) );
492 vg_ui
.vertex_buffer
= vg_linear_alloc( vg_mem
.rtmemory
, vert_size
);
493 vg_ui
.indice_buffer
= vg_linear_alloc( vg_mem
.rtmemory
, inds_size
);
496 * -----------------------------------------------------
499 /* Load default font */
501 #include "vg/vg_pxfont_thin.h"
504 u32 pixels
= 0, total
= 256*256, data
= 0;
507 while( pixels
< total
){
508 for( int b
= 31; b
>= 0; b
-- ){
509 image
[ pixels
++ ] = (compressed
[data
] & (0x1u
<< b
))? 0xffu
: 0x00u
;
511 if( pixels
>= total
){
519 glGenTextures( 1, &vg_ui
.tex_glyphs
);
520 glBindTexture( GL_TEXTURE_2D
, vg_ui
.tex_glyphs
);
521 glTexImage2D( GL_TEXTURE_2D
, 0, GL_R8
, 256, 256, 0,
522 GL_RED
, GL_UNSIGNED_BYTE
, image
);
525 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
526 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
527 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
528 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
532 * ---------------------------------------------------------------
535 vg_ui
.cursor_map
[ k_ui_cursor_default
] =
536 SDL_CreateSystemCursor( SDL_SYSTEM_CURSOR_ARROW
);
537 vg_ui
.cursor_map
[ k_ui_cursor_hand
] =
538 SDL_CreateSystemCursor( SDL_SYSTEM_CURSOR_HAND
);
539 vg_ui
.cursor_map
[ k_ui_cursor_ibeam
] =
540 SDL_CreateSystemCursor( SDL_SYSTEM_CURSOR_IBEAM
);
549 static void rect_copy( ui_rect a
, ui_rect b
){
550 for( int i
=0; i
<4; i
++ )
554 static void ui_flush( enum ui_shader shader
, f32 w
, f32 h
){
555 u32 vertex_offset
= vg_ui
.vert_start
*sizeof(ui_vert
),
556 vertex_count
= vg_ui
.cur_vert
-vg_ui
.vert_start
,
557 vertex_size
= vertex_count
*sizeof(ui_vert
),
559 indice_offset
= vg_ui
.indice_start
*sizeof(u16
),
560 indice_count
= vg_ui
.cur_indice
-vg_ui
.indice_start
,
561 indice_size
= indice_count
* sizeof(u16
);
563 if( !vertex_size
|| !indice_size
)
566 glBindVertexArray( vg_ui
.vao
);
567 glBindBuffer( GL_ARRAY_BUFFER
, vg_ui
.vbo
);
568 glBufferSubData( GL_ARRAY_BUFFER
, vertex_offset
, vertex_size
,
569 vg_ui
.vertex_buffer
+vg_ui
.vert_start
);
571 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, vg_ui
.ebo
);
572 glBufferSubData( GL_ELEMENT_ARRAY_BUFFER
, indice_offset
, indice_size
,
573 vg_ui
.indice_buffer
+vg_ui
.indice_start
);
575 glDisable( GL_DEPTH_TEST
);
576 glEnable( GL_BLEND
);
577 glBlendFunc( GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
578 glBlendEquation( GL_FUNC_ADD
);
579 glDisable( GL_CULL_FACE
);
581 m3x3f view
= M3X3_IDENTITY
;
582 m3x3_translate( view
, (v3f
){ -1.0f
, 1.0f
, 0.0f
} );
583 m3x3_scale( view
, (v3f
){ 1.0f
/(w
*0.5f
),
584 -1.0f
/(h
*0.5f
), 1.0f
} );
586 if( shader
== k_ui_shader_colour
){
587 glUseProgram( _shader_ui
.id
);
589 glUniformMatrix3fv( glGetUniformLocation( _shader_ui
.id
, "uPv" ), 1,
590 GL_FALSE
, (float *)view
);
591 glUniform4fv( glGetUniformLocation( _shader_ui
.id
, "uColour" ), 1,
594 glActiveTexture( GL_TEXTURE0
);
595 glBindTexture( GL_TEXTURE_2D
, vg_ui
.tex_glyphs
);
596 glUniform1i( glGetUniformLocation( _shader_ui
.id
, "uTexGlyphs" ), 0 );
598 glActiveTexture( GL_TEXTURE1
);
599 glBindTexture( GL_TEXTURE_2D
, vg_ui
.tex_bg
);
600 glUniform1i( glGetUniformLocation( _shader_ui
.id
, "uTexBG" ), 1 );
601 glUniform1f( glGetUniformLocation( _shader_ui
.id
, "uSpread" ),
603 glUniform2fv( glGetUniformLocation( _shader_ui
.id
, "uBGInverseRatio" ),
604 1, vg_ui
.bg_inverse_ratio
);
606 else if( shader
== k_ui_shader_image
){
607 glUseProgram( _shader_ui_image
.id
);
608 glUniformMatrix3fv( glGetUniformLocation( _shader_ui_image
.id
, "uPv" ), 1,
609 GL_FALSE
, (float *)view
);
610 glUniform1i( glGetUniformLocation(_shader_ui_image
.id
,"uTexImage"), 0 );
611 glUniform4fv( glGetUniformLocation( _shader_ui_image
.id
, "uColour" ), 1,
614 else if( shader
== k_ui_shader_hsv
){
615 glUseProgram( _shader_ui_hsv
.id
);
616 glUniformMatrix3fv( glGetUniformLocation( _shader_ui_hsv
.id
, "uPv" ), 1,
617 GL_FALSE
, (float *)view
);
618 glUniform1f( glGetUniformLocation(_shader_ui_hsv
.id
,"uHue"), vg_ui
.hue
);
621 vg_fatal_error( "Invalid UI shader (%d)\n", shader
);
623 glDrawElements( GL_TRIANGLES
, indice_count
, GL_UNSIGNED_SHORT
,
624 (void *)(vg_ui
.indice_start
*sizeof(u16
)) );
626 glDisable( GL_BLEND
);
628 vg_ui
.indice_start
= vg_ui
.cur_indice
;
629 vg_ui
.vert_start
= vg_ui
.cur_vert
;
632 static void ui_fill_rect( ui_rect rect
, u32 colour
, ui_px uv
[4] ){
633 /* this if far from ideal but stops us from crashing */
634 if( (vg_ui
.cur_vert
+ 4 > vg_ui
.max_verts
) ||
635 (vg_ui
.cur_indice
+ 6 > vg_ui
.max_indices
))
638 struct ui_vert
*vertices
= &vg_ui
.vertex_buffer
[ vg_ui
.cur_vert
];
639 u16
*indices
= &vg_ui
.indice_buffer
[ vg_ui
.cur_indice
];
641 for( int i
=0; i
<4; i
++ ){
642 vertices
[i
].colour
= colour
;
645 vertices
[0].co
[0] = rect
[0];
646 vertices
[0].co
[1] = rect
[1];
647 vertices
[0].uv
[0] = uv
[0];
648 vertices
[0].uv
[1] = uv
[1];
649 vertices
[1].co
[0] = rect
[0]+rect
[2];
650 vertices
[1].co
[1] = rect
[1];
651 vertices
[1].uv
[0] = uv
[2];
652 vertices
[1].uv
[1] = uv
[1];
653 vertices
[2].co
[0] = rect
[0]+rect
[2];
654 vertices
[2].co
[1] = rect
[1]+rect
[3];
655 vertices
[2].uv
[0] = uv
[2];
656 vertices
[2].uv
[1] = uv
[3];
657 vertices
[3].co
[0] = rect
[0];
658 vertices
[3].co
[1] = rect
[1]+rect
[3];
659 vertices
[3].uv
[0] = uv
[0];
660 vertices
[3].uv
[1] = uv
[3];
661 u16 ind_start
= vg_ui
.cur_vert
;
663 u16 start
= vg_ui
.cur_vert
;
664 u32 mesh
[] = { 0,2,1, 0,3,2 };
666 for( u32 i
=0; i
<vg_list_size(mesh
); i
++ ){
667 indices
[i
] = start
+mesh
[i
];
670 vg_ui
.cur_indice
+= 6;
674 static void ui_fill( ui_rect rect
, u32 colour
){
675 ui_fill_rect( rect
, colour
, (ui_px
[4]){ 4,4,4,4 } );
678 static void ui_outline( ui_rect rect
, ui_px thickness
, u32 colour
, u32 mask
){
679 /* this if far from ideal but stops us from crashing */
680 if( (vg_ui
.cur_vert
+ 8 > vg_ui
.max_verts
) ||
681 (vg_ui
.cur_indice
+ 24 > vg_ui
.max_indices
))
684 struct ui_vert
*vertices
= &vg_ui
.vertex_buffer
[ vg_ui
.cur_vert
];
685 u16
*indices
= &vg_ui
.indice_buffer
[ vg_ui
.cur_indice
];
687 for( int i
=0; i
<8; i
++ ){
688 vertices
[i
].uv
[0] = 4;
689 vertices
[i
].uv
[1] = 4;
690 vertices
[i
].colour
= colour
;
693 vertices
[0].co
[0] = rect
[0];
694 vertices
[0].co
[1] = rect
[1];
695 vertices
[1].co
[0] = rect
[0]+rect
[2];
696 vertices
[1].co
[1] = rect
[1];
697 vertices
[2].co
[0] = rect
[0]+rect
[2];
698 vertices
[2].co
[1] = rect
[1]+rect
[3];
699 vertices
[3].co
[0] = rect
[0];
700 vertices
[3].co
[1] = rect
[1]+rect
[3];
701 vertices
[4].co
[0] = vertices
[0].co
[0]-thickness
;
702 vertices
[4].co
[1] = vertices
[0].co
[1]-thickness
;
703 vertices
[5].co
[0] = vertices
[1].co
[0]+thickness
;
704 vertices
[5].co
[1] = vertices
[1].co
[1]-thickness
;
705 vertices
[6].co
[0] = vertices
[2].co
[0]+thickness
;
706 vertices
[6].co
[1] = vertices
[2].co
[1]+thickness
;
707 vertices
[7].co
[0] = vertices
[3].co
[0]-thickness
;
708 vertices
[7].co
[1] = vertices
[3].co
[1]+thickness
;
710 u16 start
= vg_ui
.cur_vert
;
711 u32 mesh
[] = { 0,5,4, 0,1,5, 1,6,5, 1,2,6, 2,7,6, 2,3,7, 3,4,7, 3,0,4 };
714 mask
= UI_TOP
|UI_LEFT
|UI_BOTTOM
|UI_RIGHT
;
717 for( u32 i
=0; i
<vg_list_size(mesh
)/6; i
++ ){
718 if( (0x1<<i
) & mask
){
719 for( u32 j
=0; j
<6; j
++ )
720 indices
[c
++] = start
+mesh
[i
*6+j
];
724 vg_ui
.cur_indice
+= c
;
728 static void ui_split( ui_rect rect
,
729 enum ui_axis other
, ui_px width
, ui_px gap
,
730 ui_rect l
, ui_rect r
){
731 enum ui_axis dir
= other
^ 0x1;
733 if( width
< 0 ) width
= rect
[ 2+dir
] + width
;
736 rect_copy( rect
, temp
);
738 l
[ dir
] = temp
[ dir
];
739 r
[ dir
] = temp
[ dir
] + width
+ (gap
/2);
740 l
[ other
] = temp
[ other
];
741 r
[ other
] = temp
[ other
];
742 l
[ 2+dir
] = width
- (gap
/2);
743 r
[ 2+dir
] = temp
[ 2+dir
] - width
- (gap
/2);
744 l
[ 2+other
] = temp
[ 2+other
];
745 r
[ 2+other
] = temp
[ 2+other
];
748 static ui_px
ui_text_line_width( const char *str
);
750 static void ui_rect_center( ui_rect parent
, ui_rect rect
){
751 rect
[0] = parent
[0] + (parent
[2]-rect
[2])/2;
752 rect
[1] = parent
[1] + (parent
[3]-rect
[3])/2;
755 static void ui_fit_item( ui_rect rect
, ui_px size
[2], ui_rect d
){
756 i32 rp
= (i32
)rect
[2] * (i32
)size
[1],
757 rc
= (i32
)size
[0] * (i32
)rect
[3];
759 enum ui_axis dir
, other
;
760 if( rc
> rp
) dir
= k_ui_axis_h
;
761 else dir
= k_ui_axis_v
;
764 d
[2+dir
] = rect
[2+dir
];
765 d
[2+other
] = (rect
[2+dir
] * size
[other
]) / size
[dir
];
767 ui_rect_center( rect
, d
);
770 static void ui_split_ratio( ui_rect rect
, enum ui_axis dir
, float ratio
,
771 ui_px gap
, ui_rect l
, ui_rect r
){
772 ui_px width
= (float)rect
[ 2+(dir
^0x1) ] * ratio
;
773 ui_split( rect
, dir
, width
, gap
, l
, r
);
776 static void ui_rect_pad( ui_rect rect
, ui_px pad
[2] ){
783 static ui_px
ui_text_line_width( const char *str
){
785 const char *_c
= str
;
788 while( (c
= *(_c
++)) ){
789 if( c
>= 32 ) length
++;
790 else if( c
== '\n' ) break;
793 return length
* vg_ui
.font
->spacing
;
796 static ui_px
ui_text_string_height( const char *str
){
798 const char *_c
= str
;
801 while( (c
= *(_c
++)) ){
802 if( c
== '\n' ) height
++;
808 static ui_px
ui_text_aligned_x( const char *str
, ui_rect rect
, ui_px scale
,
809 enum ui_align align
){
810 enum ui_align lwr
= k_ui_align_lwr
& align
;
811 if( lwr
== k_ui_align_left
){
815 ui_px width
= ui_text_line_width( str
) * scale
;
817 if( lwr
== k_ui_align_right
)
818 return rect
[0] + rect
[2]-width
;
820 return rect
[0] + (rect
[2]-width
)/2;
824 static ui_px
ui_min( ui_px a
, ui_px b
){ return a
<b
?a
:b
; }
825 static ui_px
ui_max( ui_px a
, ui_px b
){ return a
>b
?a
:b
; }
826 static ui_px
ui_clamp( ui_px a
, ui_px min
, ui_px max
){
827 return ui_min( max
, ui_max( a
, min
) );
830 static int ui_clip( ui_rect parent
, ui_rect child
, ui_rect clipped
){
831 ui_px parent_max
[2], child_max
[2];
832 parent_max
[0] = parent
[0]+parent
[2];
833 parent_max
[1] = parent
[1]+parent
[3];
834 child_max
[0] = child
[0]+child
[2];
835 child_max
[1] = child
[1]+child
[3];
837 clipped
[0] = ui_clamp( child
[0], parent
[0], parent_max
[0] );
838 clipped
[1] = ui_clamp( child
[1], parent
[1], parent_max
[1] );
839 clipped
[2] = ui_clamp( child_max
[0], parent
[0], parent_max
[0] );
840 clipped
[3] = ui_clamp( child_max
[1], parent
[1], parent_max
[1] );
842 if( clipped
[0] == clipped
[2] ||
843 clipped
[1] == clipped
[3] )
846 clipped
[2] -= clipped
[0];
847 clipped
[3] -= clipped
[1];
852 static int ui_inside_rect( ui_rect rect
, ui_px co
[2] ){
853 if( co
[0] >= rect
[0] &&
855 co
[0] < rect
[0]+rect
[2] &&
856 co
[1] < rect
[1]+rect
[3] ){
863 static int ui_click_down( u32 mask
){
864 if( vg_ui
.ignore_input_frames
) return 0;
865 if( (vg_ui
.mouse_state
[0] & mask
) &&
866 !(vg_ui
.mouse_state
[1] & mask
) )
872 static int ui_clicking( u32 mask
){
873 if( vg_ui
.ignore_input_frames
) return 0;
874 return vg_ui
.mouse_state
[0] & mask
;
877 static int ui_click_up( u32 mask
){
878 if( vg_ui
.ignore_input_frames
) return 0;
879 if( (vg_ui
.mouse_state
[1] & mask
) &&
880 !(vg_ui
.mouse_state
[0] & mask
) )
886 static void ui_prerender(void){
888 vg_ui
.mouse_state
[1] = vg_ui
.mouse_state
[0];
889 vg_ui
.mouse_state
[0] = SDL_GetMouseState( &x
, &y
);
894 vg_ui
.cur_indice
= 0;
895 vg_ui
.vert_start
= 0;
896 vg_ui
.indice_start
= 0;
897 vg_ui
.focused_control_hit
= 0;
898 vg_ui
.cursor
= k_ui_cursor_default
;
899 vg_ui
.wants_mouse
= 0;
901 if( vg_ui
.ignore_input_frames
){
902 vg_ui
.ignore_input_frames
--;
906 if( ui_click_down(UI_MOUSE_LEFT
)||ui_click_down(UI_MOUSE_MIDDLE
) ){
907 vg_ui
.mouse_click
[0] = vg_ui
.mouse
[0];
908 vg_ui
.mouse_click
[1] = vg_ui
.mouse
[1];
912 static u32
ui_colour( enum ui_scheme_colour id
){
913 return vg_ui
.scheme
[ id
];
916 /* get an appropriately contrasting colour given the base */
917 static u32
ui_colourcont( enum ui_scheme_colour id
){
918 if ( id
< k_ui_bg
+6 ) return ui_colour( k_ui_fg
);
919 else if( id
< k_ui_fg
) return ui_colour( k_ui_bg
+1 );
920 else if( id
< k_ui_hue
) return ui_colour( k_ui_bg
+3 );
921 else if( id
< k_ui_red
+k_ui_brighter
) return ui_colour( k_ui_fg
);
922 else return ui_colour( k_ui_fg
+1 );
925 static void ui_hex_to_norm( u32 hex
, v4f norm
){
926 norm
[0] = ((hex
) & 0xff);
927 norm
[1] = ((hex
>>8 ) & 0xff);
928 norm
[2] = ((hex
>>16) & 0xff);
929 norm
[3] = ((hex
>>24) & 0xff);
930 v4_muls( norm
, 1.0f
/255.0f
, norm
);
933 static void ui_text_glyph( const struct ui_font
*font
, ui_px scale
,
934 u8 glyph
, ui_rect out_texcoords
){
935 glyph
-= font
->ascii_start
;
937 ui_px per_row
= font
->sheet_size
/ font
->glyph_width
,
938 column
= (ui_px
)glyph
% per_row
,
939 row
= (glyph
- column
) / per_row
;
941 out_texcoords
[0] = column
* font
->glyph_width
;
942 out_texcoords
[1] = row
* font
->glyph_height
+ font
->offset_y
;
943 out_texcoords
[2] = out_texcoords
[0] + font
->glyph_width
;
944 out_texcoords
[3] = out_texcoords
[1] + font
->glyph_height
;
947 static u32
ui_opacity( u32 colour
, f32 opacity
){
948 u32 alpha
= opacity
* 255.0f
;
949 return (colour
& 0x00ffffff) | (alpha
<< 24);
952 static u32
ui_ntext( ui_rect rect
, const char *str
, u32 len
, ui_px scale
,
953 enum ui_align align
, u32 colour
){
955 if( colour
== 0 ) colour
= ui_colour( k_ui_fg
);
957 colour
&= 0x00ffffff;
959 const char *_c
= str
;
962 text_cursor
[0] = ui_text_aligned_x( str
, rect
, scale
, align
);
963 text_cursor
[1] = rect
[1];
964 text_cursor
[2] = vg_ui
.font
->glyph_width
*scale
;
965 text_cursor
[3] = vg_ui
.font
->glyph_height
*scale
;
967 u32 printed_chars
= 0;
969 if( align
& (k_ui_align_middle
|k_ui_align_bottom
) ){
970 ui_px height
= ui_text_string_height( str
) * scale
;
972 if( align
& k_ui_align_bottom
)
973 text_cursor
[1] += rect
[3]-height
;
975 text_cursor
[1] += (rect
[3]-height
)/2;
978 while( (c
= *(_c
++)) ){
979 if( printed_chars
>= len
){
981 text_cursor
[1] += vg_ui
.font
->line_height
*scale
;
982 text_cursor
[0] = ui_text_aligned_x( _c
, rect
, scale
, align
);
983 text_cursor
[0] -= vg_ui
.font
->spacing
*scale
;
986 ui_text_glyph( vg_ui
.font
, scale
, '\xb6' /*FIXME*/, glyph
);
987 ui_fill_rect( text_cursor
, 0x00ffffff, glyph
);
988 text_cursor
[0] += vg_ui
.font
->spacing
*scale
;
992 text_cursor
[1] += vg_ui
.font
->line_height
*scale
;
993 text_cursor
[0] = ui_text_aligned_x( _c
, rect
, scale
, align
);
999 ui_text_glyph( vg_ui
.font
, scale
, c
, glyph
);
1001 ui_rect cursor_clipped
;
1002 if( ui_clip( rect
, text_cursor
, cursor_clipped
) ){
1003 ui_fill_rect( cursor_clipped
, colour
, glyph
);
1006 else if( c
== '\x1B' ){
1010 for( int i
=0; i
<3; i
++ ){
1015 switch( colour_id
){
1016 case '0': colour
= ui_colour( k_ui_fg
); break;
1017 case '3'|'0'<<8: colour
= ui_colour( k_ui_bg
); break;
1018 case '3'|'1'<<8: colour
= ui_colour( k_ui_red
); break;
1019 case '3'|'2'<<8: colour
= ui_colour( k_ui_green
); break;
1020 case '3'|'3'<<8: colour
= ui_colour( k_ui_yellow
); break;
1021 case '3'|'4'<<8: colour
= ui_colour( k_ui_blue
); break;
1022 case '3'|'5'<<8: colour
= ui_colour( k_ui_purple
); break;
1023 case '3'|'6'<<8: colour
= ui_colour( k_ui_aqua
); break;
1024 case '3'|'7'<<8: colour
= 0xffffffff; break;
1027 colour
&= 0x00ffffff;
1031 colour_id
|= _c
[i
] << (i
*8);
1041 else if( c
== '\t' ){
1042 text_cursor
[0] += vg_ui
.font
->spacing
*scale
*4;
1047 text_cursor
[0] += vg_ui
.font
->spacing
*scale
;
1051 return printed_chars
;
1054 static void ui_text( ui_rect rect
, const char *str
, ui_px scale
,
1055 enum ui_align align
, u32 colour
){
1056 ui_ntext( rect
, str
, 1024, scale
, align
, colour
);
1060 * Standard layout stuff
1061 * -----------------------------------------------------------------------------
1064 static void ui_panel( ui_rect in_rect
, ui_rect out_panel
){
1065 ui_fill( in_rect
, ui_colour( k_ui_bg
+1 ) );
1066 ui_outline( in_rect
, 1, ui_colour( k_ui_bg
+7 ), 0 );
1067 rect_copy( in_rect
, out_panel
);
1068 ui_rect_pad( out_panel
, (ui_px
[2]){ k_ui_padding
, k_ui_padding
} );
1071 static void ui_label( ui_rect rect
, const char *text
, ui_px size
,
1072 ui_px gap
, ui_rect r
){
1074 ui_px width
= (ui_text_line_width(text
)+vg_ui
.font
->spacing
) * size
;
1075 ui_split( rect
, k_ui_axis_v
, width
, gap
, l
, r
);
1076 ui_text( l
, text
, 1, k_ui_align_middle_left
, 0 );
1079 static void ui_standard_widget( ui_rect inout_panel
, ui_rect out_rect
,
1081 ui_px height
= (count
* vg_ui
.font
->glyph_height
+ 18) * k_ui_scale
;
1082 ui_split( inout_panel
, k_ui_axis_h
, height
, k_ui_padding
,
1083 out_rect
, inout_panel
);
1086 static void ui_info( ui_rect inout_panel
, const char *text
){
1088 ui_standard_widget( inout_panel
, box
, 1 );
1089 ui_text( box
, text
, 1, k_ui_align_middle_left
, 0 );
1092 static void ui_image( ui_rect rect
, GLuint image
){
1093 ui_flush( k_ui_shader_colour
, vg
.window_x
, vg
.window_y
);
1094 glActiveTexture( GL_TEXTURE0
);
1095 glBindTexture( GL_TEXTURE_2D
, image
);
1096 ui_fill_rect( rect
, 0xffffffff, (ui_px
[4]){ 0,256,256,0 } );
1097 ui_flush( k_ui_shader_image
, vg
.window_x
, vg
.window_y
);
1100 static u32
v4f_u32_colour( v4f colour
){
1101 u32 r
= colour
[0] * 255.0f
,
1102 g
= colour
[1] * 255.0f
,
1103 b
= colour
[2] * 255.0f
,
1104 a
= colour
[3] * 255.0f
;
1106 return r
| (g
<<8) | (b
<<16) | (a
<<24);
1109 static void ui_defocus_all(void){
1110 if( vg_ui
.focused_control_type
== k_ui_control_textbox
){
1111 SDL_StopTextInput();
1112 if( vg_ui
.textbox
.callbacks
.escape
)
1113 vg_ui
.textbox
.callbacks
.escape();
1116 vg_ui
.focused_control_id
= NULL
;
1117 vg_ui
.focused_control_hit
= 0;
1118 vg_ui
.focused_control_type
= k_ui_control_none
;
1121 enum ui_button_state
{
1122 k_ui_button_none
= 0x0,
1123 k_ui_button_click
= 0x1,
1124 k_ui_button_holding_inside
= 0x2,
1125 k_ui_button_holding_outside
= 0x4,
1126 k_ui_button_hover
= 0x8
1129 /* TODO: split this out into a formatless button and one that auto fills */
1130 static enum ui_button_state
ui_colourbutton( ui_rect rect
,
1131 enum ui_scheme_colour colour
,
1132 enum ui_scheme_colour hover_colour
,
1133 enum ui_scheme_colour hi_colour
,
1135 int clickup
= ui_click_up(UI_MOUSE_LEFT
),
1136 click
= ui_clicking(UI_MOUSE_LEFT
) | clickup
,
1137 target
= ui_inside_rect( rect
, vg_ui
.mouse_click
) && click
,
1138 hover
= ui_inside_rect( rect
, vg_ui
.mouse
);
1140 u32 col_base
= vg_ui
.scheme
[ colour
],
1141 col_highlight
= vg_ui
.scheme
[ hi_colour
? hi_colour
: k_ui_fg
],
1142 col_hover
= vg_ui
.scheme
[ hover_colour
? hover_colour
:
1143 colour
+ k_ui_brighter
];
1145 if( vg_ui
.focused_control_type
!= k_ui_control_none
){
1153 vg_ui
.cursor
= k_ui_cursor_hand
;
1160 vg_ui
.ignore_input_frames
= 2;
1164 ui_fill( rect
, col_highlight
);
1165 rect_copy( rect
, vg_ui
.click_fader
);
1166 rect_copy( rect
, vg_ui
.click_fader_end
);
1167 vg_ui
.click_fader_end
[3] = 0;
1168 ui_rect_center( rect
, vg_ui
.click_fader_end
);
1169 vg_ui
.click_fade_opacity
= 1.0f
;
1172 return k_ui_button_click
;
1175 if( fill
) ui_fill( rect
, col_highlight
);
1176 return k_ui_button_holding_inside
;
1180 if( fill
) ui_fill( rect
, col_base
);
1181 ui_outline( rect
, 1, col_highlight
, 0 );
1182 return k_ui_button_holding_outside
;
1186 if( fill
) ui_fill( rect
, col_base
);
1187 return k_ui_button_none
;
1192 if( fill
) ui_fill( rect
, col_hover
);
1193 return k_ui_button_hover
;
1196 if( fill
) ui_fill( rect
, col_base
);
1197 return k_ui_button_none
;
1202 static enum ui_button_state
ui_colourbutton_text(
1203 ui_rect rect
, const char *string
, ui_px scale
,
1204 enum ui_scheme_colour colour
){
1205 enum ui_button_state state
= ui_colourbutton( rect
, colour
, 0, 0, 1 );
1206 ui_rect t
= { 0,0, ui_text_line_width( string
)*scale
, 14*scale
};
1207 ui_rect_center( rect
, t
);
1209 u32 text_colour
= ui_colourcont(colour
);
1210 if( state
== k_ui_button_holding_inside
)
1211 text_colour
= colour
;
1213 ui_text( t
, string
, scale
, k_ui_align_left
, text_colour
);
1217 static enum ui_button_state
ui_button_text( ui_rect rect
,
1218 const char *string
, ui_px scale
){
1219 return ui_colourbutton_text( rect
, string
, scale
, k_ui_bg
+4 );
1222 static enum ui_button_state
ui_button( ui_rect inout_panel
,
1223 const char *string
){
1225 ui_standard_widget( inout_panel
, rect
, 1 );
1226 return ui_colourbutton_text( rect
, string
, 1, k_ui_bg
+4 );
1229 static void ui_enum_post(void);
1230 static void ui_postrender(void){
1231 if( vg_ui
.click_fade_opacity
> 0.0f
){
1232 float scale
= vg_ui
.click_fade_opacity
;
1233 scale
= vg_maxf( 1.0f
/255.0f
, scale
*scale
);
1235 vg_ui
.click_fade_opacity
-= vg
.time_frame_delta
* 3.8f
;
1236 u32 colour
= (0x00ffffff & ui_colour(k_ui_fg
))|0x7f000000;
1238 v4f begin
, end
, dest
;
1239 for( int i
=0; i
<4; i
++ ){
1240 begin
[i
] = vg_ui
.click_fader
[i
];
1241 end
[i
] = vg_ui
.click_fader_end
[i
]+1;
1244 v4_lerp( end
, begin
, scale
, dest
);
1247 for( int i
=0; i
<4; i
++ ){
1251 ui_fill( rect
, colour
);
1254 if( vg_ui
.focused_control_type
== k_ui_control_enum
){
1257 else if( vg_ui
.focused_control_type
== k_ui_control_modal
){
1258 ui_rect screen
= {0,0,vg
.window_x
,vg
.window_y
};
1259 ui_fill( screen
, 0xa0000000 );
1260 ui_rect box
= {0,0,400,200};
1262 u32 colour
= ui_colour(k_ui_fg
),
1263 type
= vg_ui
.modal
.options
& UI_MODAL_TYPE_BITS
;
1264 if ( type
== 1 ) colour
= ui_colour(k_ui_green
);
1265 else if( type
== 2 ) colour
= ui_colour(k_ui_red
);
1266 else if( type
== 3 ) colour
= ui_colour(k_ui_yellow
);
1268 ui_rect_center( screen
, box
);
1269 ui_fill( box
, ui_colour(k_ui_bg
) );
1270 ui_outline( box
, -1, colour
, 0 );
1273 rect_copy( box
, message
);
1275 ui_rect_center( box
, message
);
1277 ui_rect row0
, row1
, btn
;
1278 ui_split_ratio( message
, k_ui_axis_h
, 0.5f
, 0, row0
, row1
);
1279 row0
[0] += vg_ui
.font
->spacing
;
1280 ui_ntext( row0
, vg_ui
.modal
.message
, (box
[2]/vg_ui
.font
->spacing
)-2, 1,
1281 k_ui_align_left
, colour
);
1283 rect_copy( row1
, btn
);
1286 ui_rect_center( row1
, btn
);
1288 vg_ui
.focused_control_type
= k_ui_control_none
; /* HACK */
1289 if( ui_button_text( btn
, "OK", 1 ) != 1 )
1290 vg_ui
.focused_control_hit
= 1;
1291 vg_ui
.focused_control_type
= k_ui_control_modal
; /* HACK */
1292 vg_ui
.wants_mouse
= 1;
1295 ui_flush( k_ui_shader_colour
, vg
.window_x
, vg
.window_y
);
1297 if( !vg_ui
.focused_control_hit
){
1301 if( vg_ui
.wants_mouse
){
1302 SDL_SetWindowGrab( vg
.window
, SDL_FALSE
);
1303 SDL_SetRelativeMouseMode( SDL_FALSE
);
1306 SDL_SetWindowGrab( vg
.window
, SDL_TRUE
);
1307 SDL_SetRelativeMouseMode( SDL_TRUE
);
1310 SDL_SetCursor( vg_ui
.cursor_map
[ vg_ui
.cursor
] );
1316 * -----------------------------------------------------------------------------
1319 static int ui_checkbox( ui_rect inout_panel
, const char *str_label
, i32
*data
){
1320 ui_rect rect
, label
, box
;
1321 ui_standard_widget( inout_panel
, rect
, 1 );
1323 ui_split( rect
, k_ui_axis_v
, -rect
[3], 0, label
, box
);
1324 ui_text( label
, str_label
, k_ui_scale
, k_ui_align_middle_left
, 0 );
1326 int changed
= ui_colourbutton( box
, k_ui_bg
, 0, 0, 1 )==1;
1328 *data
= (*data
) ^ 0x1;
1331 ui_rect_pad( box
, (ui_px
[2]){4,4} );
1332 ui_fill( box
, ui_colour( k_ui_orange
) );
1340 * -----------------------------------------------------------------------------
1344 * unfortunately no return value since we only find out that event in the
1347 static void ui_enum( ui_rect inout_panel
, const char *str_label
,
1348 struct ui_enum_opt
*options
, u32 len
, i32
*value
){
1349 ui_rect rect
, label
, box
;
1350 ui_standard_widget( inout_panel
, rect
, 1 );
1351 ui_label( rect
, str_label
, k_ui_scale
, 0, box
);
1353 const char *display
= "OUT OF RANGE";
1355 for( u32 i
=0; i
<len
; i
++ ){
1356 if( *value
== options
[i
].value
){
1357 display
= options
[i
].alias
;
1363 if( ui_button_text( box
, display
, k_ui_scale
) == 1 ){
1364 vg_ui
.focused_control_type
= k_ui_control_enum
;
1365 vg_ui
.ptr_enum
= value
;
1366 vg_ui
._enum
.option_count
= len
;
1367 vg_ui
._enum
.options
= options
;
1368 rect_copy( box
, vg_ui
._enum
.rect
);
1372 ui_outline( box
, 1, ui_colour(k_ui_red
), 0 );
1375 static void ui_enum_post(void){
1377 rect_copy( vg_ui
._enum
.rect
, drawer
);
1378 drawer
[3] *= vg_ui
._enum
.option_count
;
1381 int clickany
= ui_click_up(UI_MOUSE_LEFT
|UI_MOUSE_RIGHT
|UI_MOUSE_MIDDLE
),
1382 hover
= ui_inside_rect( drawer
, vg_ui
.mouse
);
1384 if( clickany
&& !hover
){
1389 vg_ui
.focused_control_type
= k_ui_control_none
;
1390 i32
*value
= vg_ui
.ptr_enum
;
1392 for( u32 i
=0; i
<vg_ui
._enum
.option_count
; i
++ ){
1394 ui_split( drawer
, k_ui_axis_h
, vg_ui
._enum
.rect
[3], 0, button
,drawer
);
1396 enum ui_scheme_colour colour
= k_ui_bg
+3;
1397 if( vg_ui
._enum
.options
[i
].value
== *value
)
1398 colour
= k_ui_orange
;
1400 if( ui_colourbutton_text( button
, vg_ui
._enum
.options
[i
].alias
,
1401 k_ui_scale
, colour
) == 1 ){
1402 *value
= vg_ui
._enum
.options
[i
].value
;
1408 vg_ui
.focused_control_type
= k_ui_control_enum
;
1411 vg_ui
.focused_control_hit
= 1;
1416 * -----------------------------------------------------------------------------
1419 static enum ui_button_state
_ui_slider( ui_rect box
,
1420 f32 min
, f32 max
, f32
*value
, const char *format
){
1423 enum ui_button_state
1425 k_ui_button_holding_inside
|
1426 k_ui_button_holding_outside
|
1429 mask_using
| k_ui_button_hover
,
1430 state
= ui_colourbutton( box
, k_ui_bg
, k_ui_bg
+2, k_ui_bg
+3, 1 );
1432 if( state
& mask_using
){
1433 t
= vg_clampf( (f32
)(vg_ui
.mouse
[0] - box
[0]) / (f32
)( box
[2] ),
1435 *value
= vg_lerpf( min
, max
, t
);
1438 t
= vg_clampf( (*value
- min
) / (max
-min
), 0.0f
, 1.0f
);
1440 ui_rect line
= { box
[0], box
[1], t
* (f32
)box
[2], box
[3] };
1441 ui_fill( line
, ui_colour(state
&mask_brighter
? k_ui_bg
+4: k_ui_bg
+2) );
1443 ui_outline( box
, 1, ui_colour(state
? k_ui_fg
+3: k_ui_bg
+3), 0 );
1445 /* TODO: replace this one day with our own function */
1447 snprintf( buf
, sizeof(buf
), format
? format
: "%.2f", *value
);
1448 ui_text( box
, buf
, 1, k_ui_align_middle_center
, 0 );
1453 static void ui_slider( ui_rect inout_panel
, const char *str_label
,
1454 f32 min
, f32 max
, f32
*value
, const char *format
){
1455 ui_rect rect
, label
, box
;
1456 ui_standard_widget( inout_panel
, rect
, 1 );
1457 ui_label( rect
, str_label
, k_ui_scale
, 0, box
);
1458 _ui_slider( box
, min
, max
, value
, format
);
1463 * -----------------------------------------------------------------------------
1466 static void ui_colourpicker( ui_rect inout_panel
, const char *str_label
,
1468 ui_rect widget
, left
, right
;
1469 ui_standard_widget( inout_panel
, widget
, 8 );
1470 ui_split_ratio( widget
, k_ui_axis_v
, 0.5f
, 8, left
, right
);
1473 ui_split_ratio( right
, k_ui_axis_h
, 0.5f
, 2, sliders
[0], sliders
[2] );
1474 ui_split_ratio( sliders
[0], k_ui_axis_h
, 0.5f
, 2, sliders
[0], sliders
[1] );
1475 ui_split_ratio( sliders
[2], k_ui_axis_h
, 0.5f
, 2, sliders
[2], sliders
[3] );
1478 vg_rgb_hsv( value
, hsv
);
1481 enum ui_button_state modified
= 0x00;
1483 for( u32 i
=0; i
<4; i
++ ){
1484 modified
|= _ui_slider( sliders
[i
], 0.0f
, 1.0f
, hsv
+i
,
1485 (const char *[]){ "hue %.2f",
1488 "alpha %.2f" }[i
] );
1491 ui_rect preview
, square
;
1492 ui_split_ratio( left
, k_ui_axis_v
, 0.8f
, 8, square
, preview
);
1494 u32 state
= ui_colourbutton( square
, 0, 0, 0, 0 );
1497 enum ui_button_state
1499 k_ui_button_holding_inside
|
1500 k_ui_button_holding_outside
|
1503 if( state
& mask_using
){
1504 for( u32 i
=0; i
<2; i
++ ){
1505 hsv
[1+i
] = vg_clampf(
1506 (f32
)(vg_ui
.mouse
[i
] - square
[i
]) / (f32
)(square
[2+i
]),
1510 hsv
[2] = 1.0f
-hsv
[2];
1513 if( modified
& (k_ui_button_click
|k_ui_button_holding_inside
|
1514 k_ui_button_holding_outside
) ){
1515 vg_hsv_rgb( hsv
, value
);
1519 ui_outline( square
, 1, ui_colour( state
? k_ui_fg
+3: k_ui_bg
+3 ), 0 );
1521 /* preview colour */
1523 vg_hsv_rgb( hsv
, colour
);
1525 ui_fill( preview
, v4f_u32_colour( colour
) );
1527 /* Draw hsv shader thingy */
1528 ui_flush( k_ui_shader_colour
, vg
.window_x
, vg
.window_y
);
1529 ui_fill_rect( square
, 0xffffffff, (ui_px
[4]){ 0,256,256,0 } );
1531 ui_flush( k_ui_shader_hsv
, vg
.window_x
, vg
.window_y
);
1533 ui_rect marker
= { square
[0] + hsv
[1] * (f32
)square
[2] - 4,
1534 square
[1] + (1.0f
-hsv
[2]) * (f32
)square
[3] - 4,
1537 square
[1] + (1.0f
-hsv
[2]) * (f32
)square
[3],
1539 ly
= { square
[0] + hsv
[1] * (f32
)square
[2],
1543 ui_fill( marker
, ui_colour( k_ui_fg
) );
1544 ui_fill( lx
, ui_colour( k_ui_fg
) );
1545 ui_fill( ly
, ui_colour( k_ui_fg
) );
1550 * -----------------------------------------------------------------------------
1553 static void _ui_textbox_make_selection( int *start
, int *end
){
1554 *start
= VG_MIN( vg_ui
.textbox
.cursor_pos
, vg_ui
.textbox
.cursor_user
);
1555 *end
= VG_MAX( vg_ui
.textbox
.cursor_pos
, vg_ui
.textbox
.cursor_user
);
1558 static void _ui_textbox_move_cursor( int *cursor0
, int *cursor1
,
1559 int dir
, int snap_together
){
1560 *cursor0
= VG_MAX( 0, vg_ui
.textbox
.cursor_user
+ dir
);
1563 VG_MIN( vg_ui
.textbox
.len
-1, strlen( vg_ui
.textbuf
)),
1567 *cursor1
= *cursor0
;
1570 static int _ui_textbox_makeroom( int datastart
, int length
){
1571 int move_to
= VG_MIN( datastart
+length
, vg_ui
.textbox
.len
-1 );
1572 int move_amount
= strlen( vg_ui
.textbuf
)-datastart
;
1573 int move_end
= VG_MIN( move_to
+move_amount
, vg_ui
.textbox
.len
-1 );
1574 move_amount
= move_end
-move_to
;
1577 memmove( &vg_ui
.textbuf
[ move_to
],
1578 &vg_ui
.textbuf
[ datastart
],
1581 vg_ui
.textbuf
[ move_end
] = '\0';
1583 return VG_MIN( length
, vg_ui
.textbox
.len
-datastart
-1 );
1586 static int _ui_textbox_delete_char( int direction
){
1588 _ui_textbox_make_selection( &start
, &end
);
1590 /* There is no selection */
1592 if( direction
== 1 ) end
= VG_MIN( end
+1, strlen( vg_ui
.textbuf
) );
1593 else if( direction
== -1 ) start
= VG_MAX( start
-1, 0 );
1596 /* Still no selction, no need to do anything */
1600 /* Copy the end->terminator to start */
1601 int remaining_length
= strlen( vg_ui
.textbuf
)+1-end
;
1602 memmove( &vg_ui
.textbuf
[ start
],
1603 &vg_ui
.textbuf
[ end
],
1608 static void _ui_textbox_to_clipboard(void){
1610 _ui_textbox_make_selection( &start
, &end
);
1614 memcpy( buffer
, &vg_ui
.textbuf
[ start
], end
-start
);
1615 buffer
[ end
-start
] = 0x00;
1616 SDL_SetClipboardText( buffer
);
1620 static void _ui_textbox_change_callback(void){
1621 if( vg_ui
.textbox
.callbacks
.change
){
1622 vg_ui
.textbox
.callbacks
.change( vg_ui
.textbuf
, vg_ui
.textbox
.len
);
1624 /* we gave permission to modify the buffer in this callback so.. */
1625 int len
= strlen( vg_ui
.textbuf
);
1626 vg_ui
.textbox
.cursor_user
= VG_MIN( vg_ui
.textbox
.cursor_user
, len
);
1627 vg_ui
.textbox
.cursor_pos
= VG_MIN( vg_ui
.textbox
.cursor_pos
, len
);
1631 static void ui_start_modal( const char *message
, u32 options
);
1632 static void _ui_textbox_clipboard_paste(void){
1633 if( !SDL_HasClipboardText() )
1636 char *text
= SDL_GetClipboardText();
1641 int datastart
= _ui_textbox_delete_char( 0 );
1642 int length
= strlen( text
);
1644 if( (vg_ui
.textbox
.len
- strlen(vg_ui
.textbuf
)) < length
){
1645 ui_start_modal( "Clipboard content exceeds buffer size.", UI_MODAL_BAD
);
1649 int cpylength
= _ui_textbox_makeroom( datastart
, length
);
1651 memcpy( vg_ui
.textbuf
+ datastart
, text
, cpylength
);
1652 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
,
1653 &vg_ui
.textbox
.cursor_pos
, cpylength
, 1 );
1655 _ui_textbox_change_callback();
1658 static void _ui_textbox_put_char( char c
){
1659 vg_ui
.textbox
.cursor_user
= _ui_textbox_delete_char(0);
1660 if( (vg_ui
.textbox
.len
- strlen(vg_ui
.textbuf
)) <= 1 ) return;
1662 if( _ui_textbox_makeroom( vg_ui
.textbox
.cursor_user
, 1 ) )
1663 vg_ui
.textbuf
[ vg_ui
.textbox
.cursor_user
] = c
;
1665 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
,
1666 &vg_ui
.textbox
.cursor_pos
, 1, 1 );
1669 /* Receed secondary cursor */
1670 static void _ui_textbox_left_select(void){
1671 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
, NULL
, -1, 0 );
1674 /* Match and receed both cursors */
1675 static void _ui_textbox_left(void){
1676 int cursor_diff
= vg_ui
.textbox
.cursor_pos
- vg_ui
.textbox
.cursor_user
? 0: 1;
1678 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
,
1679 &vg_ui
.textbox
.cursor_pos
, -cursor_diff
, 1 );
1682 static void _ui_textbox_up(void){
1683 if( vg_ui
.textbox
.flags
& UI_TEXTBOX_MULTILINE
){
1684 int line_begin
= vg_ui
.textbox
.cursor_user
;
1686 while( line_begin
){
1687 if( vg_ui
.textbuf
[ line_begin
-1 ] == '\n' ){
1695 int line_above_begin
= line_begin
-1;
1697 while( line_above_begin
){
1698 if( vg_ui
.textbuf
[ line_above_begin
-1 ] == '\n' ){
1702 line_above_begin
--;
1705 int offset
= vg_ui
.textbox
.cursor_user
- line_begin
,
1706 line_length_above
= line_begin
- line_above_begin
-1;
1708 offset
= VG_MIN( line_length_above
, offset
);
1710 vg_ui
.textbox
.cursor_user
= line_above_begin
+offset
;
1711 vg_ui
.textbox
.cursor_pos
= line_above_begin
+offset
;
1714 vg_ui
.textbox
.cursor_user
= line_begin
;
1715 vg_ui
.textbox
.cursor_pos
= line_begin
;
1719 if( vg_ui
.textbox
.callbacks
.up
){
1720 vg_ui
.textbox
.callbacks
.up( vg_ui
.textbuf
, vg_ui
.textbox
.len
);
1725 static void _ui_textbox_down(void){
1726 if( vg_ui
.textbox
.flags
& UI_TEXTBOX_MULTILINE
){
1727 int line_begin
= vg_ui
.textbox
.cursor_user
;
1729 while( line_begin
){
1730 if( vg_ui
.textbuf
[ line_begin
-1 ] == '\n' ){
1737 int line_below_begin
= vg_ui
.textbox
.cursor_user
;
1740 if( vg_ui
.textbuf
[ line_below_begin
] == '\0' ){
1741 vg_ui
.textbox
.cursor_user
= line_below_begin
;
1742 vg_ui
.textbox
.cursor_pos
= line_below_begin
;
1746 if( vg_ui
.textbuf
[ line_below_begin
] == '\n' ){
1747 line_below_begin
++;
1751 line_below_begin
++;
1754 int line_below_end
= line_below_begin
;
1756 if( vg_ui
.textbuf
[ line_below_end
] == '\0' ||
1757 vg_ui
.textbuf
[ line_below_end
] == '\n' ){
1764 int offset
= vg_ui
.textbox
.cursor_user
- line_begin
,
1765 line_length_below
= line_below_end
- line_below_begin
-1;
1767 offset
= VG_MIN( line_length_below
, offset
);
1769 vg_ui
.textbox
.cursor_user
= line_below_begin
+offset
;
1770 vg_ui
.textbox
.cursor_pos
= line_below_begin
+offset
;
1773 if( vg_ui
.textbox
.callbacks
.down
){
1774 vg_ui
.textbox
.callbacks
.down( vg_ui
.textbuf
, vg_ui
.textbox
.len
);
1779 static void _ui_textbox_right_select(void){
1780 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
, NULL
, 1, 0 );
1783 static void _ui_textbox_right(void){
1784 int cursor_diff
= vg_ui
.textbox
.cursor_pos
- vg_ui
.textbox
.cursor_user
? 0: 1;
1786 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
,
1787 &vg_ui
.textbox
.cursor_pos
, +cursor_diff
, 1 );
1790 static void _ui_textbox_backspace(void){
1791 if( vg_ui
.focused_control_type
== k_ui_control_textbox
){
1792 vg_ui
.textbox
.cursor_user
= _ui_textbox_delete_char( -1 );
1793 vg_ui
.textbox
.cursor_pos
= vg_ui
.textbox
.cursor_user
;
1794 _ui_textbox_change_callback();
1798 static void _ui_textbox_delete(void){
1799 if( vg_ui
.focused_control_type
== k_ui_control_textbox
){
1800 vg_ui
.textbox
.cursor_user
= _ui_textbox_delete_char( 1 );
1801 vg_ui
.textbox
.cursor_pos
= vg_ui
.textbox
.cursor_user
;
1802 _ui_textbox_change_callback();
1806 static void _ui_textbox_home_select(void){
1807 i32 start
= vg_ui
.textbox
.cursor_user
;
1809 if( vg_ui
.textbox
.flags
& UI_TEXTBOX_MULTILINE
){
1811 if( vg_ui
.textbuf
[start
-1] == '\n' )
1820 vg_ui
.textbox
.cursor_user
= start
;
1823 static void _ui_textbox_home(void){
1824 _ui_textbox_home_select();
1825 vg_ui
.textbox
.cursor_pos
= vg_ui
.textbox
.cursor_user
;
1828 static void _ui_textbox_end_select(void){
1829 i32 end
= vg_ui
.textbox
.cursor_user
;
1831 if( vg_ui
.textbox
.flags
& UI_TEXTBOX_MULTILINE
){
1832 while( vg_ui
.textbuf
[end
] ){
1833 if( vg_ui
.textbuf
[end
] == '\n' )
1840 end
= VG_MIN( vg_ui
.textbox
.len
-1, strlen(vg_ui
.textbuf
) );
1842 vg_ui
.textbox
.cursor_user
= end
;
1845 static void _ui_textbox_end(void){
1846 _ui_textbox_end_select();
1847 vg_ui
.textbox
.cursor_pos
= vg_ui
.textbox
.cursor_user
;
1850 static void _ui_textbox_select_all(void){
1851 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
, NULL
, 10000, 0);
1852 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_pos
, NULL
, -10000, 0);
1855 static void _ui_textbox_cut(void){
1856 _ui_textbox_to_clipboard();
1857 vg_ui
.textbox
.cursor_user
= _ui_textbox_delete_char(0);
1858 vg_ui
.textbox
.cursor_pos
= vg_ui
.textbox
.cursor_user
;
1859 _ui_textbox_change_callback();
1862 static void _ui_textbox_enter(void){
1863 if( vg_ui
.focused_control_type
== k_ui_control_textbox
){
1864 vg_ui
.ignore_input_frames
= 2;
1866 if( vg_ui
.textbox
.callbacks
.enter
)
1867 vg_ui
.textbox
.callbacks
.enter( vg_ui
.textbuf
, vg_ui
.textbox
.len
);
1869 if( vg_ui
.focused_control_type
!= k_ui_control_textbox
) return;
1871 if( vg_ui
.textbox
.flags
& UI_TEXTBOX_MULTILINE
){
1872 _ui_textbox_put_char( '\n' );
1873 _ui_textbox_change_callback();
1876 if( !(vg_ui
.textbox
.flags
& UI_TEXTBOX_AUTOFOCUS
) )
1883 * based on a visual character coordinate relative to the anchor of the textbox,
1884 * this works out the linear place in the buffer that coordinate maps to
1886 * input coordinates go in co[0], co[1], and the result index is in co[2]
1888 static void _ui_textbox_calc_index_from_grid( int co
[3], int wrap_length
){
1892 while( (c
= vg_ui
.textbuf
[i
[2]]) ){
1893 if( i
[1]==co
[1] && i
[0]>=co
[0] ) break;
1895 if( i
[0] >= wrap_length
){
1900 if( c
>= 32 && c
<= 126 ){
1904 else if( c
== '\n' ){
1907 if( i
[1] > co
[1] ) break;
1921 * based on the index specied in co[2], work out the visual character
1922 * coordinates and store them in co[0], co[1]
1924 static void _ui_textbox_index_calc_coords( int co
[3], int wrap_length
){
1931 while( (c
= vg_ui
.textbuf
[i
++]) ){
1932 if( i
> co
[2] ) break;
1933 if( co
[0] >= wrap_length
){
1937 if( c
>= 32 && c
<= 126 ) co
[0] ++;
1938 else if( c
== '\n' ){
1946 * calculate the number of characters remaining until either:
1947 * - the wrap_length limit is hit
1948 * - end of the line/string
1950 * index must be fully populated with visual X/Y, and linear index
1952 static int _ui_textbox_run_remaining( int index
[3], int wrap_length
){
1953 int i
=0, printed_chars
=0;
1955 while( (c
= vg_ui
.textbuf
[index
[2] + (i
++)]) ){
1956 if( index
[0]+i
>= wrap_length
) break;
1957 if( c
>= 32 && c
<= 126 ) printed_chars
++;
1958 else if( c
== '\n' ) break;
1961 return printed_chars
+1;
1964 static int ui_textbox( ui_rect inout_panel
, const char *label
,
1965 char *buf
, u32 len
, u32 lines
, u32 flags
,
1966 struct ui_textbox_callbacks
*callbacks
){
1967 assert( lines
> 0 );
1970 if( lines
> 1 ) flags
|= UI_TEXTBOX_MULTILINE
;
1973 ui_standard_widget( inout_panel
, rect
, lines
);
1976 ui_label( rect
, label
, 1, 0, rect
);
1978 int clickup
= ui_click_up(UI_MOUSE_LEFT
),
1979 clickdown
= ui_click_down(UI_MOUSE_LEFT
),
1980 click
= ui_clicking(UI_MOUSE_LEFT
) | clickup
,
1981 target
= ui_inside_rect( rect
, vg_ui
.mouse_click
) && click
,
1982 hover
= ui_inside_rect( rect
, vg_ui
.mouse
);
1984 /* allow instant transitions from textbox->textbox */
1985 if( (vg_ui
.focused_control_type
!= k_ui_control_none
) &&
1986 (vg_ui
.focused_control_type
!= k_ui_control_textbox
) ){
1992 flags
&= ~UI_TEXTBOX_AUTOFOCUS
;
1995 u32 col_base
= ui_colour( k_ui_bg
),
1996 col_highlight
= ui_colour( k_ui_fg
),
1997 col_cursor
= (0x00ffffff & ui_colour(k_ui_fg
))|0x7f000000;
2002 rect_copy( rect
, text_rect
);
2004 if( flags
& UI_TEXTBOX_MULTILINE
) text_rect
[3] = rect
[3]-16;
2005 else text_rect
[3] = vg_ui
.font
->line_height
;
2008 ui_rect_center( rect
, text_rect
);
2010 ui_px wrap_length
= 1024;
2012 if( flags
& UI_TEXTBOX_WRAP
)
2013 wrap_length
= text_rect
[2] / vg_ui
.font
->spacing
;
2016 vg_ui
.cursor
= k_ui_cursor_ibeam
;
2019 if( vg_ui
.focused_control_id
== buf
){
2020 ui_fill( rect
, col_base
);
2021 ui_ntext( text_rect
, buf
, wrap_length
, 1, k_ui_align_left
, 0 );
2023 if( !(flags
& UI_TEXTBOX_AUTOFOCUS
) && ((clickup
||clickdown
) && !target
)){
2027 vg_ui
.focused_control_hit
= 1;
2028 if( click
&& target
){
2030 (vg_ui
.mouse_click
[0] - text_rect
[0]) / vg_ui
.font
->spacing
,
2031 (vg_ui
.mouse_click
[1] - text_rect
[1]) / vg_ui
.font
->line_height
,
2035 (vg_ui
.mouse
[0] - text_rect
[0]) / vg_ui
.font
->spacing
,
2036 (vg_ui
.mouse
[1] - text_rect
[1]) / vg_ui
.font
->line_height
,
2040 if( flags
& UI_TEXTBOX_MULTILINE
){
2041 _ui_textbox_calc_index_from_grid( p0
, wrap_length
);
2042 _ui_textbox_calc_index_from_grid( p1
, wrap_length
);
2044 vg_ui
.textbox
.cursor_pos
= p0
[2];
2045 vg_ui
.textbox
.cursor_user
= p1
[2];
2048 int max
= strlen( buf
);
2049 vg_ui
.textbox
.cursor_pos
= VG_MAX( 0, VG_MIN( max
, p0
[0] )),
2050 vg_ui
.textbox
.cursor_user
= VG_MAX( 0, VG_MIN( max
, p1
[0] ));
2054 ui_outline( rect
, -2, vg_ui
.scheme
[ k_ui_orange
], 0 );
2058 int c0
= vg_ui
.textbox
.cursor_pos
,
2059 c1
= vg_ui
.textbox
.cursor_user
,
2060 start
= VG_MIN( c0
, c1
),
2061 end
= VG_MAX( c0
, c1
),
2064 if( flags
& (UI_TEXTBOX_WRAP
|UI_TEXTBOX_MULTILINE
) ){
2065 int pos
[3], remaining
= chars
;
2068 _ui_textbox_index_calc_coords( pos
, wrap_length
);
2071 cursor
[0] = text_rect
[0] + pos
[0]*vg_ui
.font
->spacing
-1;
2072 cursor
[1] = text_rect
[1] + pos
[1]*14;
2075 ui_fill( cursor
, col_cursor
);
2076 rect_copy( cursor
, vg_ui
.click_fader_end
);
2080 int run
= _ui_textbox_run_remaining( pos
, wrap_length
);
2081 run
= VG_MIN( run
, remaining
);
2083 cursor
[0] = text_rect
[0] + pos
[0]*vg_ui
.font
->spacing
-1;
2084 cursor
[1] = text_rect
[1] + pos
[1]*14;
2085 cursor
[2] = (float)(run
)*(float)vg_ui
.font
->spacing
;
2088 ui_fill( cursor
, col_cursor
);
2095 rect_copy( cursor
, vg_ui
.click_fader_end
);
2099 cursor
[0] = text_rect
[0] + start
*vg_ui
.font
->spacing
-1;
2100 cursor
[1] = text_rect
[1];
2107 cursor
[2] = (float)(chars
)*(float)vg_ui
.font
->spacing
;
2110 if( (vg_ui
.click_fade_opacity
<=0.0f
) &&
2111 ui_clip( rect
, cursor
, cursor
) ){
2112 ui_fill( cursor
, col_cursor
);
2115 rect_copy( cursor
, vg_ui
.click_fader_end
);
2122 if( click
|| (flags
& UI_TEXTBOX_AUTOFOCUS
) ){
2123 if( (target
&& hover
) || (flags
& UI_TEXTBOX_AUTOFOCUS
) ){
2126 ui_fill( rect
, col_highlight
);
2127 vg_ui
.ignore_input_frames
= 2;
2128 rect_copy( rect
, vg_ui
.click_fader
);
2129 rect_copy( rect
, vg_ui
.click_fader_end
);
2131 vg_ui
.click_fade_opacity
= 1.0f
;
2132 vg_ui
.textbuf
= buf
;
2133 vg_ui
.focused_control_hit
= 1;
2134 vg_ui
.focused_control_type
= k_ui_control_textbox
;
2135 vg_ui
.textbox
.len
= len
;
2136 vg_ui
.textbox
.flags
= flags
;
2137 vg_ui
.textbox
.cursor_pos
= 0;
2138 vg_ui
.textbox
.cursor_user
= 0;
2141 vg_ui
.textbox
.callbacks
= *callbacks
;
2144 vg_ui
.textbox
.callbacks
.change
= NULL
;
2145 vg_ui
.textbox
.callbacks
.down
= NULL
;
2146 vg_ui
.textbox
.callbacks
.up
= NULL
;
2147 vg_ui
.textbox
.callbacks
.enter
= NULL
;
2150 SDL_StartTextInput();
2154 ui_fill( rect
, col_base
);
2157 ui_outline( rect
, -1, col_highlight
, 0 );
2160 ui_ntext( text_rect
, buf
, wrap_length
, 1, k_ui_align_left
, 0 );
2166 * -----------------------------------------------------------------------------
2169 static void ui_tabs( ui_rect inout_panel
, ui_rect out_content_panel
,
2170 const char **titles
, u32 count
, i32
*page
){
2172 ui_standard_widget( inout_panel
, bar
, 1 );
2174 i32 cur_page
= *page
;
2176 f32 width
= (f32
)inout_panel
[2] / (f32
)count
;
2178 ui_px h
= (inout_panel
[1] + inout_panel
[3]) - (bar
[1]+bar
[3]);
2179 inout_panel
[1] = bar
[1]+bar
[3];
2182 ui_fill( inout_panel
, ui_colour( k_ui_bg
+2 ) );
2183 ui_outline( inout_panel
, 1, ui_colour( k_ui_bg
+5 ), 0 );
2185 rect_copy( inout_panel
, out_content_panel
);
2186 ui_rect_pad( out_content_panel
, (ui_px
[2]){ k_ui_padding
, k_ui_padding
} );
2189 for( i32 i
=0; i
<count
; i
++ ){
2191 bar
[0] + ((f32
)i
*width
),
2197 enum ui_scheme_colour colour
= k_ui_bg
+4;
2198 if( i
== cur_page
){
2200 ui_outline( button
, 1, ui_colour( k_ui_bg
+5 ),
2201 UI_TOP
|UI_LEFT
|UI_RIGHT
);
2205 if( ui_colourbutton_text( button
, titles
[i
], 1, colour
) == 1 )
2212 * -----------------------------------------------------------------------------
2214 static void ui_start_modal( const char *message
, u32 options
){
2215 vg_ui
.focused_control_type
= k_ui_control_modal
;
2216 vg_ui
.modal
.message
= message
;
2217 vg_ui
.modal
.callbacks
.close
= NULL
;
2218 vg_ui
.modal
.options
= options
;
2220 u32 type
= options
& UI_MODAL_TYPE_BITS
;
2221 if( type
== UI_MODAL_OK
) vg_info( message
);
2222 else if( type
== UI_MODAL_WARN
) vg_warn( message
);
2223 else if( type
== UI_MODAL_GOOD
) vg_success( message
);
2224 else if( type
== UI_MODAL_BAD
) vg_error( message
);
2229 * -----------------------------------------------------------------------------
2235 static void _ui_proc_key( SDL_Keysym ev
){
2236 if( vg_ui
.focused_control_type
!= k_ui_control_textbox
){
2240 struct textbox_mapping
{
2244 void (*handler
)(void);
2248 { 0, SDLK_LEFT
, _ui_textbox_left
},
2249 { KMOD_SHIFT
, SDLK_LEFT
, _ui_textbox_left_select
},
2250 { 0, SDLK_RIGHT
, _ui_textbox_right
},
2251 { KMOD_SHIFT
, SDLK_RIGHT
, _ui_textbox_right_select
},
2252 { 0, SDLK_DOWN
, _ui_textbox_down
},
2253 { 0, SDLK_UP
, _ui_textbox_up
},
2254 { 0, SDLK_BACKSPACE
, _ui_textbox_backspace
},
2255 { KMOD_SHIFT
, SDLK_BACKSPACE
, _ui_textbox_backspace
},
2256 { KMOD_CTRL
, SDLK_BACKSPACE
, _ui_textbox_backspace
},
2257 { 0, SDLK_DELETE
, _ui_textbox_delete
},
2258 { 0, SDLK_HOME
, _ui_textbox_home
},
2259 { KMOD_SHIFT
, SDLK_HOME
, _ui_textbox_home_select
},
2260 { 0, SDLK_END
, _ui_textbox_end
},
2261 { KMOD_SHIFT
, SDLK_END
, _ui_textbox_end_select
},
2262 { KMOD_CTRL
, SDLK_a
, _ui_textbox_select_all
},
2263 { KMOD_CTRL
, SDLK_c
, _ui_textbox_to_clipboard
},
2264 { KMOD_CTRL
, SDLK_x
, _ui_textbox_cut
},
2265 { KMOD_CTRL
, SDLK_v
, _ui_textbox_clipboard_paste
},
2266 { 0, SDLK_RETURN
, _ui_textbox_enter
},
2267 { 0, SDLK_ESCAPE
, ui_defocus_all
},
2272 if( ev
.mod
& KMOD_SHIFT
)
2275 if( ev
.mod
& KMOD_CTRL
)
2278 if( ev
.mod
& KMOD_ALT
)
2281 for( int i
=0; i
<vg_list_size( mappings
); i
++ ){
2282 struct textbox_mapping
*mapping
= &mappings
[i
];
2284 if( mapping
->key
== ev
.sym
){
2285 if( mapping
->mod
== 0 ){
2291 else if( (mod
& mapping
->mod
) == mapping
->mod
){
2300 * Callback for text entry mode
2302 static void ui_proc_utf8( const char *text
){
2303 if( vg_ui
.focused_control_type
== k_ui_control_textbox
){
2304 const char *ptr
= text
;
2307 if( *ptr
!= '`' ) _ui_textbox_put_char( *ptr
);
2311 _ui_textbox_change_callback();
2317 * -----------------------------------------------------------------------------
2320 static void ui_dev_colourview(void){
2321 ui_rect window
= {vg
.window_x
-256,0,256,vg
.window_y
}, swatch
;
2323 const char *names
[vg_list_size(vg_ui
.scheme
)] = {
2324 [k_ui_bg
] = "k_ui_bg", "k_ui_bg+1", "k_ui_bg+2", "k_ui_bg+3",
2325 "k_ui_bg+4", "k_ui_bg+5", "k_ui_bg+6", "k_ui_bg+7",
2327 [k_ui_fg
] = "k_ui_fg", "k_ui_fg+1", "k_ui_fg+2", "k_ui_fg+3",
2328 "k_ui_fg+4", "k_ui_fg+5", "k_ui_fg+6", "k_ui_fg+7",
2330 [k_ui_red
] = "k_ui_red", "k_ui_orange", "k_ui_yellow", "k_ui_green",
2331 "k_ui_aqua", "k_ui_blue", "k_ui_purple", "k_ui_gray",
2332 "k_ui_red+8","k_ui_orange+8","k_ui_yellow+8","k_ui_green+8",
2333 "k_ui_aqua+8","k_ui_blue+8","k_ui_purple+8","k_ui_gray+8" };
2336 ui_split_ratio( window
, k_ui_axis_v
, 0.5f
, 0, col
[0], col
[1] );
2338 for( int i
=0; i
<vg_list_size(vg_ui
.scheme
); i
++ ){
2339 int which
= (i
/8)%2;
2341 ui_split( col
[which
], k_ui_axis_h
, 24, 0, swatch
, col
[which
] );
2342 ui_fill( swatch
, ui_colour(i
) );
2345 ui_text(swatch
, names
[i
], 1, k_ui_align_middle_left
, ui_colourcont(i
));
2349 #endif /* VG_IMGUI_H */