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_delta
[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
);
890 vg_ui
.mouse_delta
[0] = x
-vg_ui
.mouse
[0];
891 vg_ui
.mouse_delta
[1] = y
-vg_ui
.mouse
[1];
896 vg_ui
.cur_indice
= 0;
897 vg_ui
.vert_start
= 0;
898 vg_ui
.indice_start
= 0;
899 vg_ui
.focused_control_hit
= 0;
900 vg_ui
.cursor
= k_ui_cursor_default
;
901 vg_ui
.wants_mouse
= 0;
903 if( vg_ui
.ignore_input_frames
){
904 vg_ui
.ignore_input_frames
--;
908 if( ui_click_down(UI_MOUSE_LEFT
)||ui_click_down(UI_MOUSE_MIDDLE
) ){
909 vg_ui
.mouse_click
[0] = vg_ui
.mouse
[0];
910 vg_ui
.mouse_click
[1] = vg_ui
.mouse
[1];
914 static u32
ui_colour( enum ui_scheme_colour id
){
915 return vg_ui
.scheme
[ id
];
918 /* get an appropriately contrasting colour given the base */
919 static u32
ui_colourcont( enum ui_scheme_colour id
){
920 if ( id
< k_ui_bg
+6 ) return ui_colour( k_ui_fg
);
921 else if( id
< k_ui_fg
) return ui_colour( k_ui_bg
+1 );
922 else if( id
< k_ui_hue
) return ui_colour( k_ui_bg
+3 );
923 else if( id
< k_ui_red
+k_ui_brighter
) return ui_colour( k_ui_fg
);
924 else return ui_colour( k_ui_fg
+1 );
927 static void ui_hex_to_norm( u32 hex
, v4f norm
){
928 norm
[0] = ((hex
) & 0xff);
929 norm
[1] = ((hex
>>8 ) & 0xff);
930 norm
[2] = ((hex
>>16) & 0xff);
931 norm
[3] = ((hex
>>24) & 0xff);
932 v4_muls( norm
, 1.0f
/255.0f
, norm
);
935 static void ui_text_glyph( const struct ui_font
*font
, ui_px scale
,
936 u8 glyph
, ui_rect out_texcoords
){
937 glyph
-= font
->ascii_start
;
939 ui_px per_row
= font
->sheet_size
/ font
->glyph_width
,
940 column
= (ui_px
)glyph
% per_row
,
941 row
= (glyph
- column
) / per_row
;
943 out_texcoords
[0] = column
* font
->glyph_width
;
944 out_texcoords
[1] = row
* font
->glyph_height
+ font
->offset_y
;
945 out_texcoords
[2] = out_texcoords
[0] + font
->glyph_width
;
946 out_texcoords
[3] = out_texcoords
[1] + font
->glyph_height
;
949 static u32
ui_opacity( u32 colour
, f32 opacity
){
950 u32 alpha
= opacity
* 255.0f
;
951 return (colour
& 0x00ffffff) | (alpha
<< 24);
954 static u32
ui_ntext( ui_rect rect
, const char *str
, u32 len
, ui_px scale
,
955 enum ui_align align
, u32 colour
){
957 if( colour
== 0 ) colour
= ui_colour( k_ui_fg
);
959 colour
&= 0x00ffffff;
961 const char *_c
= str
;
964 text_cursor
[0] = ui_text_aligned_x( str
, rect
, scale
, align
);
965 text_cursor
[1] = rect
[1];
966 text_cursor
[2] = vg_ui
.font
->glyph_width
*scale
;
967 text_cursor
[3] = vg_ui
.font
->glyph_height
*scale
;
969 u32 printed_chars
= 0;
971 if( align
& (k_ui_align_middle
|k_ui_align_bottom
) ){
972 ui_px height
= ui_text_string_height( str
) * scale
;
974 if( align
& k_ui_align_bottom
)
975 text_cursor
[1] += rect
[3]-height
;
977 text_cursor
[1] += (rect
[3]-height
)/2;
980 while( (c
= *(_c
++)) ){
981 if( printed_chars
>= len
){
983 text_cursor
[1] += vg_ui
.font
->line_height
*scale
;
984 text_cursor
[0] = ui_text_aligned_x( _c
, rect
, scale
, align
);
985 text_cursor
[0] -= vg_ui
.font
->spacing
*scale
;
988 ui_text_glyph( vg_ui
.font
, scale
, '\xb6' /*FIXME*/, glyph
);
989 ui_fill_rect( text_cursor
, 0x00ffffff, glyph
);
990 text_cursor
[0] += vg_ui
.font
->spacing
*scale
;
994 text_cursor
[1] += vg_ui
.font
->line_height
*scale
;
995 text_cursor
[0] = ui_text_aligned_x( _c
, rect
, scale
, align
);
1001 ui_text_glyph( vg_ui
.font
, scale
, c
, glyph
);
1003 ui_rect cursor_clipped
;
1004 if( ui_clip( rect
, text_cursor
, cursor_clipped
) ){
1005 ui_fill_rect( cursor_clipped
, colour
, glyph
);
1008 else if( c
== '\x1B' ){
1012 for( int i
=0; i
<3; i
++ ){
1017 switch( colour_id
){
1018 case '0': colour
= ui_colour( k_ui_fg
); break;
1019 case '3'|'0'<<8: colour
= ui_colour( k_ui_bg
); break;
1020 case '3'|'1'<<8: colour
= ui_colour( k_ui_red
); break;
1021 case '3'|'2'<<8: colour
= ui_colour( k_ui_green
); break;
1022 case '3'|'3'<<8: colour
= ui_colour( k_ui_yellow
); break;
1023 case '3'|'4'<<8: colour
= ui_colour( k_ui_blue
); break;
1024 case '3'|'5'<<8: colour
= ui_colour( k_ui_purple
); break;
1025 case '3'|'6'<<8: colour
= ui_colour( k_ui_aqua
); break;
1026 case '3'|'7'<<8: colour
= 0xffffffff; break;
1029 colour
&= 0x00ffffff;
1033 colour_id
|= _c
[i
] << (i
*8);
1043 else if( c
== '\t' ){
1044 text_cursor
[0] += vg_ui
.font
->spacing
*scale
*4;
1049 text_cursor
[0] += vg_ui
.font
->spacing
*scale
;
1053 return printed_chars
;
1056 static void ui_text( ui_rect rect
, const char *str
, ui_px scale
,
1057 enum ui_align align
, u32 colour
){
1058 ui_ntext( rect
, str
, 1024, scale
, align
, colour
);
1062 * Standard layout stuff
1063 * -----------------------------------------------------------------------------
1066 static void ui_panel( ui_rect in_rect
, ui_rect out_panel
){
1067 ui_fill( in_rect
, ui_colour( k_ui_bg
+1 ) );
1068 ui_outline( in_rect
, 1, ui_colour( k_ui_bg
+7 ), 0 );
1069 rect_copy( in_rect
, out_panel
);
1070 ui_rect_pad( out_panel
, (ui_px
[2]){ k_ui_padding
, k_ui_padding
} );
1073 static void ui_label( ui_rect rect
, const char *text
, ui_px size
,
1074 ui_px gap
, ui_rect r
){
1076 ui_px width
= (ui_text_line_width(text
)+vg_ui
.font
->spacing
) * size
;
1077 ui_split( rect
, k_ui_axis_v
, width
, gap
, l
, r
);
1078 ui_text( l
, text
, 1, k_ui_align_middle_left
, 0 );
1081 static void ui_standard_widget( ui_rect inout_panel
, ui_rect out_rect
,
1083 ui_px height
= (count
* vg_ui
.font
->glyph_height
+ 18) * k_ui_scale
;
1084 ui_split( inout_panel
, k_ui_axis_h
, height
, k_ui_padding
,
1085 out_rect
, inout_panel
);
1088 static void ui_info( ui_rect inout_panel
, const char *text
){
1090 ui_standard_widget( inout_panel
, box
, 1 );
1091 ui_text( box
, text
, 1, k_ui_align_middle_left
, 0 );
1094 static void ui_image( ui_rect rect
, GLuint image
){
1095 ui_flush( k_ui_shader_colour
, vg
.window_x
, vg
.window_y
);
1096 glActiveTexture( GL_TEXTURE0
);
1097 glBindTexture( GL_TEXTURE_2D
, image
);
1098 ui_fill_rect( rect
, 0xffffffff, (ui_px
[4]){ 0,256,256,0 } );
1099 ui_flush( k_ui_shader_image
, vg
.window_x
, vg
.window_y
);
1102 static u32
v4f_u32_colour( v4f colour
){
1103 u32 r
= colour
[0] * 255.0f
,
1104 g
= colour
[1] * 255.0f
,
1105 b
= colour
[2] * 255.0f
,
1106 a
= colour
[3] * 255.0f
;
1108 return r
| (g
<<8) | (b
<<16) | (a
<<24);
1111 static void ui_defocus_all(void){
1112 if( vg_ui
.focused_control_type
== k_ui_control_textbox
){
1113 SDL_StopTextInput();
1114 if( vg_ui
.textbox
.callbacks
.escape
)
1115 vg_ui
.textbox
.callbacks
.escape();
1118 vg_ui
.focused_control_id
= NULL
;
1119 vg_ui
.focused_control_hit
= 0;
1120 vg_ui
.focused_control_type
= k_ui_control_none
;
1123 enum ui_button_state
{
1124 k_ui_button_none
= 0x0,
1125 k_ui_button_click
= 0x1,
1126 k_ui_button_holding_inside
= 0x2,
1127 k_ui_button_holding_outside
= 0x4,
1128 k_ui_button_hover
= 0x8
1131 /* TODO: split this out into a formatless button and one that auto fills */
1132 static enum ui_button_state
ui_colourbutton( ui_rect rect
,
1133 enum ui_scheme_colour colour
,
1134 enum ui_scheme_colour hover_colour
,
1135 enum ui_scheme_colour hi_colour
,
1137 int clickup
= ui_click_up(UI_MOUSE_LEFT
),
1138 click
= ui_clicking(UI_MOUSE_LEFT
) | clickup
,
1139 target
= ui_inside_rect( rect
, vg_ui
.mouse_click
) && click
,
1140 hover
= ui_inside_rect( rect
, vg_ui
.mouse
);
1142 u32 col_base
= vg_ui
.scheme
[ colour
],
1143 col_highlight
= vg_ui
.scheme
[ hi_colour
? hi_colour
: k_ui_fg
],
1144 col_hover
= vg_ui
.scheme
[ hover_colour
? hover_colour
:
1145 colour
+ k_ui_brighter
];
1147 if( vg_ui
.focused_control_type
!= k_ui_control_none
){
1155 vg_ui
.cursor
= k_ui_cursor_hand
;
1162 vg_ui
.ignore_input_frames
= 2;
1166 ui_fill( rect
, col_highlight
);
1167 rect_copy( rect
, vg_ui
.click_fader
);
1168 rect_copy( rect
, vg_ui
.click_fader_end
);
1169 vg_ui
.click_fader_end
[3] = 0;
1170 ui_rect_center( rect
, vg_ui
.click_fader_end
);
1171 vg_ui
.click_fade_opacity
= 1.0f
;
1174 return k_ui_button_click
;
1177 if( fill
) ui_fill( rect
, col_highlight
);
1178 return k_ui_button_holding_inside
;
1182 if( fill
) ui_fill( rect
, col_base
);
1183 ui_outline( rect
, 1, col_highlight
, 0 );
1184 return k_ui_button_holding_outside
;
1188 if( fill
) ui_fill( rect
, col_base
);
1189 return k_ui_button_none
;
1194 if( fill
) ui_fill( rect
, col_hover
);
1195 return k_ui_button_hover
;
1198 if( fill
) ui_fill( rect
, col_base
);
1199 return k_ui_button_none
;
1204 static enum ui_button_state
ui_colourbutton_text(
1205 ui_rect rect
, const char *string
, ui_px scale
,
1206 enum ui_scheme_colour colour
){
1207 enum ui_button_state state
= ui_colourbutton( rect
, colour
, 0, 0, 1 );
1208 ui_rect t
= { 0,0, ui_text_line_width( string
)*scale
, 14*scale
};
1209 ui_rect_center( rect
, t
);
1211 u32 text_colour
= ui_colourcont(colour
);
1212 if( state
== k_ui_button_holding_inside
)
1213 text_colour
= colour
;
1215 ui_text( t
, string
, scale
, k_ui_align_left
, text_colour
);
1219 static enum ui_button_state
ui_button_text( ui_rect rect
,
1220 const char *string
, ui_px scale
){
1221 return ui_colourbutton_text( rect
, string
, scale
, k_ui_bg
+4 );
1224 static enum ui_button_state
ui_button( ui_rect inout_panel
,
1225 const char *string
){
1227 ui_standard_widget( inout_panel
, rect
, 1 );
1228 return ui_colourbutton_text( rect
, string
, 1, k_ui_bg
+4 );
1231 static void ui_enum_post(void);
1232 static void ui_postrender(void){
1233 if( vg_ui
.click_fade_opacity
> 0.0f
){
1234 float scale
= vg_ui
.click_fade_opacity
;
1235 scale
= vg_maxf( 1.0f
/255.0f
, scale
*scale
);
1237 vg_ui
.click_fade_opacity
-= vg
.time_frame_delta
* 3.8f
;
1238 u32 colour
= (0x00ffffff & ui_colour(k_ui_fg
))|0x7f000000;
1240 v4f begin
, end
, dest
;
1241 for( int i
=0; i
<4; i
++ ){
1242 begin
[i
] = vg_ui
.click_fader
[i
];
1243 end
[i
] = vg_ui
.click_fader_end
[i
]+1;
1246 v4_lerp( end
, begin
, scale
, dest
);
1249 for( int i
=0; i
<4; i
++ ){
1253 ui_fill( rect
, colour
);
1256 if( vg_ui
.focused_control_type
== k_ui_control_enum
){
1259 else if( vg_ui
.focused_control_type
== k_ui_control_modal
){
1260 ui_rect screen
= {0,0,vg
.window_x
,vg
.window_y
};
1261 ui_fill( screen
, 0xa0000000 );
1262 ui_rect box
= {0,0,400,200};
1264 u32 colour
= ui_colour(k_ui_fg
),
1265 type
= vg_ui
.modal
.options
& UI_MODAL_TYPE_BITS
;
1266 if ( type
== 1 ) colour
= ui_colour(k_ui_green
);
1267 else if( type
== 2 ) colour
= ui_colour(k_ui_red
);
1268 else if( type
== 3 ) colour
= ui_colour(k_ui_yellow
);
1270 ui_rect_center( screen
, box
);
1271 ui_fill( box
, ui_colour(k_ui_bg
) );
1272 ui_outline( box
, -1, colour
, 0 );
1275 rect_copy( box
, message
);
1277 ui_rect_center( box
, message
);
1279 ui_rect row0
, row1
, btn
;
1280 ui_split_ratio( message
, k_ui_axis_h
, 0.5f
, 0, row0
, row1
);
1281 row0
[0] += vg_ui
.font
->spacing
;
1282 ui_ntext( row0
, vg_ui
.modal
.message
, (box
[2]/vg_ui
.font
->spacing
)-2, 1,
1283 k_ui_align_left
, colour
);
1285 rect_copy( row1
, btn
);
1288 ui_rect_center( row1
, btn
);
1290 vg_ui
.focused_control_type
= k_ui_control_none
; /* HACK */
1291 if( ui_button_text( btn
, "OK", 1 ) != 1 )
1292 vg_ui
.focused_control_hit
= 1;
1293 vg_ui
.focused_control_type
= k_ui_control_modal
; /* HACK */
1294 vg_ui
.wants_mouse
= 1;
1297 ui_flush( k_ui_shader_colour
, vg
.window_x
, vg
.window_y
);
1299 if( !vg_ui
.focused_control_hit
){
1303 if( vg_ui
.wants_mouse
){
1304 SDL_SetWindowGrab( vg
.window
, SDL_FALSE
);
1305 SDL_SetRelativeMouseMode( SDL_FALSE
);
1308 SDL_SetWindowGrab( vg
.window
, SDL_TRUE
);
1309 SDL_SetRelativeMouseMode( SDL_TRUE
);
1312 SDL_SetCursor( vg_ui
.cursor_map
[ vg_ui
.cursor
] );
1318 * -----------------------------------------------------------------------------
1321 static int ui_checkbox( ui_rect inout_panel
, const char *str_label
, i32
*data
){
1322 ui_rect rect
, label
, box
;
1323 ui_standard_widget( inout_panel
, rect
, 1 );
1325 ui_split( rect
, k_ui_axis_v
, -rect
[3], 0, label
, box
);
1326 ui_text( label
, str_label
, k_ui_scale
, k_ui_align_middle_left
, 0 );
1328 int changed
= ui_colourbutton( box
, k_ui_bg
, 0, 0, 1 )==1;
1330 *data
= (*data
) ^ 0x1;
1333 ui_rect_pad( box
, (ui_px
[2]){4,4} );
1334 ui_fill( box
, ui_colour( k_ui_orange
) );
1342 * -----------------------------------------------------------------------------
1346 * unfortunately no return value since we only find out that event in the
1349 static void ui_enum( ui_rect inout_panel
, const char *str_label
,
1350 struct ui_enum_opt
*options
, u32 len
, i32
*value
){
1351 ui_rect rect
, label
, box
;
1352 ui_standard_widget( inout_panel
, rect
, 1 );
1353 ui_label( rect
, str_label
, k_ui_scale
, 0, box
);
1355 const char *display
= "OUT OF RANGE";
1357 for( u32 i
=0; i
<len
; i
++ ){
1358 if( *value
== options
[i
].value
){
1359 display
= options
[i
].alias
;
1365 if( ui_button_text( box
, display
, k_ui_scale
) == 1 ){
1366 vg_ui
.focused_control_type
= k_ui_control_enum
;
1367 vg_ui
.ptr_enum
= value
;
1368 vg_ui
._enum
.option_count
= len
;
1369 vg_ui
._enum
.options
= options
;
1370 rect_copy( box
, vg_ui
._enum
.rect
);
1374 ui_outline( box
, 1, ui_colour(k_ui_red
), 0 );
1377 static void ui_enum_post(void){
1379 rect_copy( vg_ui
._enum
.rect
, drawer
);
1380 drawer
[3] *= vg_ui
._enum
.option_count
;
1383 int clickany
= ui_click_up(UI_MOUSE_LEFT
|UI_MOUSE_RIGHT
|UI_MOUSE_MIDDLE
),
1384 hover
= ui_inside_rect( drawer
, vg_ui
.mouse
);
1386 if( clickany
&& !hover
){
1391 vg_ui
.focused_control_type
= k_ui_control_none
;
1392 i32
*value
= vg_ui
.ptr_enum
;
1394 for( u32 i
=0; i
<vg_ui
._enum
.option_count
; i
++ ){
1396 ui_split( drawer
, k_ui_axis_h
, vg_ui
._enum
.rect
[3], 0, button
,drawer
);
1398 enum ui_scheme_colour colour
= k_ui_bg
+3;
1399 if( vg_ui
._enum
.options
[i
].value
== *value
)
1400 colour
= k_ui_orange
;
1402 if( ui_colourbutton_text( button
, vg_ui
._enum
.options
[i
].alias
,
1403 k_ui_scale
, colour
) == 1 ){
1404 *value
= vg_ui
._enum
.options
[i
].value
;
1410 vg_ui
.focused_control_type
= k_ui_control_enum
;
1413 vg_ui
.focused_control_hit
= 1;
1418 * -----------------------------------------------------------------------------
1421 static enum ui_button_state
_ui_slider( ui_rect box
,
1422 f32 min
, f32 max
, f32
*value
, const char *format
){
1425 enum ui_button_state
1427 k_ui_button_holding_inside
|
1428 k_ui_button_holding_outside
|
1431 mask_using
| k_ui_button_hover
,
1432 state
= ui_colourbutton( box
, k_ui_bg
, k_ui_bg
+2, k_ui_bg
+3, 1 );
1434 if( state
& mask_using
){
1435 t
= vg_clampf( (f32
)(vg_ui
.mouse
[0] - box
[0]) / (f32
)( box
[2] ),
1437 *value
= vg_lerpf( min
, max
, t
);
1440 t
= vg_clampf( (*value
- min
) / (max
-min
), 0.0f
, 1.0f
);
1442 ui_rect line
= { box
[0], box
[1], t
* (f32
)box
[2], box
[3] };
1443 ui_fill( line
, ui_colour(state
&mask_brighter
? k_ui_bg
+4: k_ui_bg
+2) );
1445 ui_outline( box
, 1, ui_colour(state
? k_ui_fg
+3: k_ui_bg
+3), 0 );
1447 /* TODO: replace this one day with our own function */
1449 snprintf( buf
, sizeof(buf
), format
? format
: "%.2f", *value
);
1450 ui_text( box
, buf
, 1, k_ui_align_middle_center
, 0 );
1455 static void ui_slider( ui_rect inout_panel
, const char *str_label
,
1456 f32 min
, f32 max
, f32
*value
, const char *format
){
1457 ui_rect rect
, label
, box
;
1458 ui_standard_widget( inout_panel
, rect
, 1 );
1459 ui_label( rect
, str_label
, k_ui_scale
, 0, box
);
1460 _ui_slider( box
, min
, max
, value
, format
);
1465 * -----------------------------------------------------------------------------
1468 static void ui_colourpicker( ui_rect inout_panel
, const char *str_label
,
1470 ui_rect widget
, left
, right
;
1471 ui_standard_widget( inout_panel
, widget
, 8 );
1472 ui_split_ratio( widget
, k_ui_axis_v
, 0.5f
, 8, left
, right
);
1475 ui_split_ratio( right
, k_ui_axis_h
, 0.5f
, 2, sliders
[0], sliders
[2] );
1476 ui_split_ratio( sliders
[0], k_ui_axis_h
, 0.5f
, 2, sliders
[0], sliders
[1] );
1477 ui_split_ratio( sliders
[2], k_ui_axis_h
, 0.5f
, 2, sliders
[2], sliders
[3] );
1480 vg_rgb_hsv( value
, hsv
);
1483 enum ui_button_state modified
= 0x00;
1485 for( u32 i
=0; i
<4; i
++ ){
1486 modified
|= _ui_slider( sliders
[i
], 0.0f
, 1.0f
, hsv
+i
,
1487 (const char *[]){ "hue %.2f",
1490 "alpha %.2f" }[i
] );
1493 ui_rect preview
, square
;
1494 ui_split_ratio( left
, k_ui_axis_v
, 0.8f
, 8, square
, preview
);
1496 u32 state
= ui_colourbutton( square
, 0, 0, 0, 0 );
1499 enum ui_button_state
1501 k_ui_button_holding_inside
|
1502 k_ui_button_holding_outside
|
1505 if( state
& mask_using
){
1506 for( u32 i
=0; i
<2; i
++ ){
1507 hsv
[1+i
] = vg_clampf(
1508 (f32
)(vg_ui
.mouse
[i
] - square
[i
]) / (f32
)(square
[2+i
]),
1512 hsv
[2] = 1.0f
-hsv
[2];
1515 if( modified
& (k_ui_button_click
|k_ui_button_holding_inside
|
1516 k_ui_button_holding_outside
) ){
1517 vg_hsv_rgb( hsv
, value
);
1521 ui_outline( square
, 1, ui_colour( state
? k_ui_fg
+3: k_ui_bg
+3 ), 0 );
1523 /* preview colour */
1525 vg_hsv_rgb( hsv
, colour
);
1527 ui_fill( preview
, v4f_u32_colour( colour
) );
1529 /* Draw hsv shader thingy */
1530 ui_flush( k_ui_shader_colour
, vg
.window_x
, vg
.window_y
);
1531 ui_fill_rect( square
, 0xffffffff, (ui_px
[4]){ 0,256,256,0 } );
1533 ui_flush( k_ui_shader_hsv
, vg
.window_x
, vg
.window_y
);
1535 ui_rect marker
= { square
[0] + hsv
[1] * (f32
)square
[2] - 4,
1536 square
[1] + (1.0f
-hsv
[2]) * (f32
)square
[3] - 4,
1539 square
[1] + (1.0f
-hsv
[2]) * (f32
)square
[3],
1541 ly
= { square
[0] + hsv
[1] * (f32
)square
[2],
1545 ui_fill( marker
, ui_colour( k_ui_fg
) );
1546 ui_fill( lx
, ui_colour( k_ui_fg
) );
1547 ui_fill( ly
, ui_colour( k_ui_fg
) );
1552 * -----------------------------------------------------------------------------
1555 static void _ui_textbox_make_selection( int *start
, int *end
){
1556 *start
= VG_MIN( vg_ui
.textbox
.cursor_pos
, vg_ui
.textbox
.cursor_user
);
1557 *end
= VG_MAX( vg_ui
.textbox
.cursor_pos
, vg_ui
.textbox
.cursor_user
);
1560 static void _ui_textbox_move_cursor( int *cursor0
, int *cursor1
,
1561 int dir
, int snap_together
){
1562 *cursor0
= VG_MAX( 0, vg_ui
.textbox
.cursor_user
+ dir
);
1565 VG_MIN( vg_ui
.textbox
.len
-1, strlen( vg_ui
.textbuf
)),
1569 *cursor1
= *cursor0
;
1572 static int _ui_textbox_makeroom( int datastart
, int length
){
1573 int move_to
= VG_MIN( datastart
+length
, vg_ui
.textbox
.len
-1 );
1574 int move_amount
= strlen( vg_ui
.textbuf
)-datastart
;
1575 int move_end
= VG_MIN( move_to
+move_amount
, vg_ui
.textbox
.len
-1 );
1576 move_amount
= move_end
-move_to
;
1579 memmove( &vg_ui
.textbuf
[ move_to
],
1580 &vg_ui
.textbuf
[ datastart
],
1583 vg_ui
.textbuf
[ move_end
] = '\0';
1585 return VG_MIN( length
, vg_ui
.textbox
.len
-datastart
-1 );
1588 static int _ui_textbox_delete_char( int direction
){
1590 _ui_textbox_make_selection( &start
, &end
);
1592 /* There is no selection */
1594 if( direction
== 1 ) end
= VG_MIN( end
+1, strlen( vg_ui
.textbuf
) );
1595 else if( direction
== -1 ) start
= VG_MAX( start
-1, 0 );
1598 /* Still no selction, no need to do anything */
1602 /* Copy the end->terminator to start */
1603 int remaining_length
= strlen( vg_ui
.textbuf
)+1-end
;
1604 memmove( &vg_ui
.textbuf
[ start
],
1605 &vg_ui
.textbuf
[ end
],
1610 static void _ui_textbox_to_clipboard(void){
1612 _ui_textbox_make_selection( &start
, &end
);
1616 memcpy( buffer
, &vg_ui
.textbuf
[ start
], end
-start
);
1617 buffer
[ end
-start
] = 0x00;
1618 SDL_SetClipboardText( buffer
);
1622 static void _ui_textbox_change_callback(void){
1623 if( vg_ui
.textbox
.callbacks
.change
){
1624 vg_ui
.textbox
.callbacks
.change( vg_ui
.textbuf
, vg_ui
.textbox
.len
);
1626 /* we gave permission to modify the buffer in this callback so.. */
1627 int len
= strlen( vg_ui
.textbuf
);
1628 vg_ui
.textbox
.cursor_user
= VG_MIN( vg_ui
.textbox
.cursor_user
, len
);
1629 vg_ui
.textbox
.cursor_pos
= VG_MIN( vg_ui
.textbox
.cursor_pos
, len
);
1633 static void ui_start_modal( const char *message
, u32 options
);
1634 static void _ui_textbox_clipboard_paste(void){
1635 if( !SDL_HasClipboardText() )
1638 char *text
= SDL_GetClipboardText();
1643 int datastart
= _ui_textbox_delete_char( 0 );
1644 int length
= strlen( text
);
1646 if( (vg_ui
.textbox
.len
- strlen(vg_ui
.textbuf
)) < length
){
1647 ui_start_modal( "Clipboard content exceeds buffer size.", UI_MODAL_BAD
);
1651 int cpylength
= _ui_textbox_makeroom( datastart
, length
);
1653 memcpy( vg_ui
.textbuf
+ datastart
, text
, cpylength
);
1654 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
,
1655 &vg_ui
.textbox
.cursor_pos
, cpylength
, 1 );
1657 _ui_textbox_change_callback();
1660 static void _ui_textbox_put_char( char c
){
1661 vg_ui
.textbox
.cursor_user
= _ui_textbox_delete_char(0);
1662 if( (vg_ui
.textbox
.len
- strlen(vg_ui
.textbuf
)) <= 1 ) return;
1664 if( _ui_textbox_makeroom( vg_ui
.textbox
.cursor_user
, 1 ) )
1665 vg_ui
.textbuf
[ vg_ui
.textbox
.cursor_user
] = c
;
1667 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
,
1668 &vg_ui
.textbox
.cursor_pos
, 1, 1 );
1671 /* Receed secondary cursor */
1672 static void _ui_textbox_left_select(void){
1673 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
, NULL
, -1, 0 );
1676 /* Match and receed both cursors */
1677 static void _ui_textbox_left(void){
1678 int cursor_diff
= vg_ui
.textbox
.cursor_pos
- vg_ui
.textbox
.cursor_user
? 0: 1;
1680 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
,
1681 &vg_ui
.textbox
.cursor_pos
, -cursor_diff
, 1 );
1684 static void _ui_textbox_up(void){
1685 if( vg_ui
.textbox
.flags
& UI_TEXTBOX_MULTILINE
){
1686 int line_begin
= vg_ui
.textbox
.cursor_user
;
1688 while( line_begin
){
1689 if( vg_ui
.textbuf
[ line_begin
-1 ] == '\n' ){
1697 int line_above_begin
= line_begin
-1;
1699 while( line_above_begin
){
1700 if( vg_ui
.textbuf
[ line_above_begin
-1 ] == '\n' ){
1704 line_above_begin
--;
1707 int offset
= vg_ui
.textbox
.cursor_user
- line_begin
,
1708 line_length_above
= line_begin
- line_above_begin
-1;
1710 offset
= VG_MIN( line_length_above
, offset
);
1712 vg_ui
.textbox
.cursor_user
= line_above_begin
+offset
;
1713 vg_ui
.textbox
.cursor_pos
= line_above_begin
+offset
;
1716 vg_ui
.textbox
.cursor_user
= line_begin
;
1717 vg_ui
.textbox
.cursor_pos
= line_begin
;
1721 if( vg_ui
.textbox
.callbacks
.up
){
1722 vg_ui
.textbox
.callbacks
.up( vg_ui
.textbuf
, vg_ui
.textbox
.len
);
1727 static void _ui_textbox_down(void){
1728 if( vg_ui
.textbox
.flags
& UI_TEXTBOX_MULTILINE
){
1729 int line_begin
= vg_ui
.textbox
.cursor_user
;
1731 while( line_begin
){
1732 if( vg_ui
.textbuf
[ line_begin
-1 ] == '\n' ){
1739 int line_below_begin
= vg_ui
.textbox
.cursor_user
;
1742 if( vg_ui
.textbuf
[ line_below_begin
] == '\0' ){
1743 vg_ui
.textbox
.cursor_user
= line_below_begin
;
1744 vg_ui
.textbox
.cursor_pos
= line_below_begin
;
1748 if( vg_ui
.textbuf
[ line_below_begin
] == '\n' ){
1749 line_below_begin
++;
1753 line_below_begin
++;
1756 int line_below_end
= line_below_begin
;
1758 if( vg_ui
.textbuf
[ line_below_end
] == '\0' ||
1759 vg_ui
.textbuf
[ line_below_end
] == '\n' ){
1766 int offset
= vg_ui
.textbox
.cursor_user
- line_begin
,
1767 line_length_below
= line_below_end
- line_below_begin
-1;
1769 offset
= VG_MIN( line_length_below
, offset
);
1771 vg_ui
.textbox
.cursor_user
= line_below_begin
+offset
;
1772 vg_ui
.textbox
.cursor_pos
= line_below_begin
+offset
;
1775 if( vg_ui
.textbox
.callbacks
.down
){
1776 vg_ui
.textbox
.callbacks
.down( vg_ui
.textbuf
, vg_ui
.textbox
.len
);
1781 static void _ui_textbox_right_select(void){
1782 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
, NULL
, 1, 0 );
1785 static void _ui_textbox_right(void){
1786 int cursor_diff
= vg_ui
.textbox
.cursor_pos
- vg_ui
.textbox
.cursor_user
? 0: 1;
1788 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
,
1789 &vg_ui
.textbox
.cursor_pos
, +cursor_diff
, 1 );
1792 static void _ui_textbox_backspace(void){
1793 if( vg_ui
.focused_control_type
== k_ui_control_textbox
){
1794 vg_ui
.textbox
.cursor_user
= _ui_textbox_delete_char( -1 );
1795 vg_ui
.textbox
.cursor_pos
= vg_ui
.textbox
.cursor_user
;
1796 _ui_textbox_change_callback();
1800 static void _ui_textbox_delete(void){
1801 if( vg_ui
.focused_control_type
== k_ui_control_textbox
){
1802 vg_ui
.textbox
.cursor_user
= _ui_textbox_delete_char( 1 );
1803 vg_ui
.textbox
.cursor_pos
= vg_ui
.textbox
.cursor_user
;
1804 _ui_textbox_change_callback();
1808 static void _ui_textbox_home_select(void){
1809 i32 start
= vg_ui
.textbox
.cursor_user
;
1811 if( vg_ui
.textbox
.flags
& UI_TEXTBOX_MULTILINE
){
1813 if( vg_ui
.textbuf
[start
-1] == '\n' )
1822 vg_ui
.textbox
.cursor_user
= start
;
1825 static void _ui_textbox_home(void){
1826 _ui_textbox_home_select();
1827 vg_ui
.textbox
.cursor_pos
= vg_ui
.textbox
.cursor_user
;
1830 static void _ui_textbox_end_select(void){
1831 i32 end
= vg_ui
.textbox
.cursor_user
;
1833 if( vg_ui
.textbox
.flags
& UI_TEXTBOX_MULTILINE
){
1834 while( vg_ui
.textbuf
[end
] ){
1835 if( vg_ui
.textbuf
[end
] == '\n' )
1842 end
= VG_MIN( vg_ui
.textbox
.len
-1, strlen(vg_ui
.textbuf
) );
1844 vg_ui
.textbox
.cursor_user
= end
;
1847 static void _ui_textbox_end(void){
1848 _ui_textbox_end_select();
1849 vg_ui
.textbox
.cursor_pos
= vg_ui
.textbox
.cursor_user
;
1852 static void _ui_textbox_select_all(void){
1853 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
, NULL
, 10000, 0);
1854 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_pos
, NULL
, -10000, 0);
1857 static void _ui_textbox_cut(void){
1858 _ui_textbox_to_clipboard();
1859 vg_ui
.textbox
.cursor_user
= _ui_textbox_delete_char(0);
1860 vg_ui
.textbox
.cursor_pos
= vg_ui
.textbox
.cursor_user
;
1861 _ui_textbox_change_callback();
1864 static void _ui_textbox_enter(void){
1865 if( vg_ui
.focused_control_type
== k_ui_control_textbox
){
1866 vg_ui
.ignore_input_frames
= 2;
1868 if( vg_ui
.textbox
.callbacks
.enter
)
1869 vg_ui
.textbox
.callbacks
.enter( vg_ui
.textbuf
, vg_ui
.textbox
.len
);
1871 if( vg_ui
.focused_control_type
!= k_ui_control_textbox
) return;
1873 if( vg_ui
.textbox
.flags
& UI_TEXTBOX_MULTILINE
){
1874 _ui_textbox_put_char( '\n' );
1875 _ui_textbox_change_callback();
1878 if( !(vg_ui
.textbox
.flags
& UI_TEXTBOX_AUTOFOCUS
) )
1885 * based on a visual character coordinate relative to the anchor of the textbox,
1886 * this works out the linear place in the buffer that coordinate maps to
1888 * input coordinates go in co[0], co[1], and the result index is in co[2]
1890 static void _ui_textbox_calc_index_from_grid( int co
[3], int wrap_length
){
1894 while( (c
= vg_ui
.textbuf
[i
[2]]) ){
1895 if( i
[1]==co
[1] && i
[0]>=co
[0] ) break;
1897 if( i
[0] >= wrap_length
){
1902 if( c
>= 32 && c
<= 126 ){
1906 else if( c
== '\n' ){
1909 if( i
[1] > co
[1] ) break;
1923 * based on the index specied in co[2], work out the visual character
1924 * coordinates and store them in co[0], co[1]
1926 static void _ui_textbox_index_calc_coords( int co
[3], int wrap_length
){
1933 while( (c
= vg_ui
.textbuf
[i
++]) ){
1934 if( i
> co
[2] ) break;
1935 if( co
[0] >= wrap_length
){
1939 if( c
>= 32 && c
<= 126 ) co
[0] ++;
1940 else if( c
== '\n' ){
1948 * calculate the number of characters remaining until either:
1949 * - the wrap_length limit is hit
1950 * - end of the line/string
1952 * index must be fully populated with visual X/Y, and linear index
1954 static int _ui_textbox_run_remaining( int index
[3], int wrap_length
){
1955 int i
=0, printed_chars
=0;
1957 while( (c
= vg_ui
.textbuf
[index
[2] + (i
++)]) ){
1958 if( index
[0]+i
>= wrap_length
) break;
1959 if( c
>= 32 && c
<= 126 ) printed_chars
++;
1960 else if( c
== '\n' ) break;
1963 return printed_chars
+1;
1966 static int ui_textbox( ui_rect inout_panel
, const char *label
,
1967 char *buf
, u32 len
, u32 lines
, u32 flags
,
1968 struct ui_textbox_callbacks
*callbacks
){
1969 assert( lines
> 0 );
1972 if( lines
> 1 ) flags
|= UI_TEXTBOX_MULTILINE
;
1975 ui_standard_widget( inout_panel
, rect
, lines
);
1978 ui_label( rect
, label
, 1, 0, rect
);
1980 int clickup
= ui_click_up(UI_MOUSE_LEFT
),
1981 clickdown
= ui_click_down(UI_MOUSE_LEFT
),
1982 click
= ui_clicking(UI_MOUSE_LEFT
) | clickup
,
1983 target
= ui_inside_rect( rect
, vg_ui
.mouse_click
) && click
,
1984 hover
= ui_inside_rect( rect
, vg_ui
.mouse
);
1986 /* allow instant transitions from textbox->textbox */
1987 if( (vg_ui
.focused_control_type
!= k_ui_control_none
) &&
1988 (vg_ui
.focused_control_type
!= k_ui_control_textbox
) ){
1994 flags
&= ~UI_TEXTBOX_AUTOFOCUS
;
1997 u32 col_base
= ui_colour( k_ui_bg
),
1998 col_highlight
= ui_colour( k_ui_fg
),
1999 col_cursor
= (0x00ffffff & ui_colour(k_ui_fg
))|0x7f000000;
2004 rect_copy( rect
, text_rect
);
2006 if( flags
& UI_TEXTBOX_MULTILINE
) text_rect
[3] = rect
[3]-16;
2007 else text_rect
[3] = vg_ui
.font
->line_height
;
2010 ui_rect_center( rect
, text_rect
);
2012 ui_px wrap_length
= 1024;
2014 if( flags
& UI_TEXTBOX_WRAP
)
2015 wrap_length
= text_rect
[2] / vg_ui
.font
->spacing
;
2018 vg_ui
.cursor
= k_ui_cursor_ibeam
;
2021 if( vg_ui
.focused_control_id
== buf
){
2022 ui_fill( rect
, col_base
);
2023 ui_ntext( text_rect
, buf
, wrap_length
, 1, k_ui_align_left
, 0 );
2025 if( !(flags
& UI_TEXTBOX_AUTOFOCUS
) && ((clickup
||clickdown
) && !target
)){
2029 vg_ui
.focused_control_hit
= 1;
2030 if( click
&& target
){
2032 (vg_ui
.mouse_click
[0] - text_rect
[0]) / vg_ui
.font
->spacing
,
2033 (vg_ui
.mouse_click
[1] - text_rect
[1]) / vg_ui
.font
->line_height
,
2037 (vg_ui
.mouse
[0] - text_rect
[0]) / vg_ui
.font
->spacing
,
2038 (vg_ui
.mouse
[1] - text_rect
[1]) / vg_ui
.font
->line_height
,
2042 if( flags
& UI_TEXTBOX_MULTILINE
){
2043 _ui_textbox_calc_index_from_grid( p0
, wrap_length
);
2044 _ui_textbox_calc_index_from_grid( p1
, wrap_length
);
2046 vg_ui
.textbox
.cursor_pos
= p0
[2];
2047 vg_ui
.textbox
.cursor_user
= p1
[2];
2050 int max
= strlen( buf
);
2051 vg_ui
.textbox
.cursor_pos
= VG_MAX( 0, VG_MIN( max
, p0
[0] )),
2052 vg_ui
.textbox
.cursor_user
= VG_MAX( 0, VG_MIN( max
, p1
[0] ));
2056 ui_outline( rect
, -2, vg_ui
.scheme
[ k_ui_orange
], 0 );
2060 int c0
= vg_ui
.textbox
.cursor_pos
,
2061 c1
= vg_ui
.textbox
.cursor_user
,
2062 start
= VG_MIN( c0
, c1
),
2063 end
= VG_MAX( c0
, c1
),
2066 if( flags
& (UI_TEXTBOX_WRAP
|UI_TEXTBOX_MULTILINE
) ){
2067 int pos
[3], remaining
= chars
;
2070 _ui_textbox_index_calc_coords( pos
, wrap_length
);
2073 cursor
[0] = text_rect
[0] + pos
[0]*vg_ui
.font
->spacing
-1;
2074 cursor
[1] = text_rect
[1] + pos
[1]*14;
2077 ui_fill( cursor
, col_cursor
);
2078 rect_copy( cursor
, vg_ui
.click_fader_end
);
2082 int run
= _ui_textbox_run_remaining( pos
, wrap_length
);
2083 run
= VG_MIN( run
, remaining
);
2085 cursor
[0] = text_rect
[0] + pos
[0]*vg_ui
.font
->spacing
-1;
2086 cursor
[1] = text_rect
[1] + pos
[1]*14;
2087 cursor
[2] = (float)(run
)*(float)vg_ui
.font
->spacing
;
2090 ui_fill( cursor
, col_cursor
);
2097 rect_copy( cursor
, vg_ui
.click_fader_end
);
2101 cursor
[0] = text_rect
[0] + start
*vg_ui
.font
->spacing
-1;
2102 cursor
[1] = text_rect
[1];
2109 cursor
[2] = (float)(chars
)*(float)vg_ui
.font
->spacing
;
2112 if( (vg_ui
.click_fade_opacity
<=0.0f
) &&
2113 ui_clip( rect
, cursor
, cursor
) ){
2114 ui_fill( cursor
, col_cursor
);
2117 rect_copy( cursor
, vg_ui
.click_fader_end
);
2124 if( click
|| (flags
& UI_TEXTBOX_AUTOFOCUS
) ){
2125 if( (target
&& hover
) || (flags
& UI_TEXTBOX_AUTOFOCUS
) ){
2128 ui_fill( rect
, col_highlight
);
2129 vg_ui
.ignore_input_frames
= 2;
2130 rect_copy( rect
, vg_ui
.click_fader
);
2131 rect_copy( rect
, vg_ui
.click_fader_end
);
2133 vg_ui
.click_fade_opacity
= 1.0f
;
2134 vg_ui
.textbuf
= buf
;
2135 vg_ui
.focused_control_hit
= 1;
2136 vg_ui
.focused_control_type
= k_ui_control_textbox
;
2137 vg_ui
.textbox
.len
= len
;
2138 vg_ui
.textbox
.flags
= flags
;
2139 vg_ui
.textbox
.cursor_pos
= 0;
2140 vg_ui
.textbox
.cursor_user
= 0;
2143 vg_ui
.textbox
.callbacks
= *callbacks
;
2146 vg_ui
.textbox
.callbacks
.change
= NULL
;
2147 vg_ui
.textbox
.callbacks
.down
= NULL
;
2148 vg_ui
.textbox
.callbacks
.up
= NULL
;
2149 vg_ui
.textbox
.callbacks
.enter
= NULL
;
2152 SDL_StartTextInput();
2156 ui_fill( rect
, col_base
);
2159 ui_outline( rect
, -1, col_highlight
, 0 );
2162 ui_ntext( text_rect
, buf
, wrap_length
, 1, k_ui_align_left
, 0 );
2168 * -----------------------------------------------------------------------------
2171 static void ui_tabs( ui_rect inout_panel
, ui_rect out_content_panel
,
2172 const char **titles
, u32 count
, i32
*page
){
2174 ui_standard_widget( inout_panel
, bar
, 1 );
2176 i32 cur_page
= *page
;
2178 f32 width
= (f32
)inout_panel
[2] / (f32
)count
;
2180 ui_px h
= (inout_panel
[1] + inout_panel
[3]) - (bar
[1]+bar
[3]);
2181 inout_panel
[1] = bar
[1]+bar
[3];
2184 ui_fill( inout_panel
, ui_colour( k_ui_bg
+2 ) );
2185 ui_outline( inout_panel
, 1, ui_colour( k_ui_bg
+5 ), 0 );
2187 rect_copy( inout_panel
, out_content_panel
);
2188 ui_rect_pad( out_content_panel
, (ui_px
[2]){ k_ui_padding
, k_ui_padding
} );
2191 for( i32 i
=0; i
<count
; i
++ ){
2193 bar
[0] + ((f32
)i
*width
),
2199 enum ui_scheme_colour colour
= k_ui_bg
+4;
2200 if( i
== cur_page
){
2202 ui_outline( button
, 1, ui_colour( k_ui_bg
+5 ),
2203 UI_TOP
|UI_LEFT
|UI_RIGHT
);
2207 if( ui_colourbutton_text( button
, titles
[i
], 1, colour
) == 1 )
2214 * -----------------------------------------------------------------------------
2216 static void ui_start_modal( const char *message
, u32 options
){
2217 vg_ui
.focused_control_type
= k_ui_control_modal
;
2218 vg_ui
.modal
.message
= message
;
2219 vg_ui
.modal
.callbacks
.close
= NULL
;
2220 vg_ui
.modal
.options
= options
;
2222 u32 type
= options
& UI_MODAL_TYPE_BITS
;
2223 if( type
== UI_MODAL_OK
) vg_info( message
);
2224 else if( type
== UI_MODAL_WARN
) vg_warn( message
);
2225 else if( type
== UI_MODAL_GOOD
) vg_success( message
);
2226 else if( type
== UI_MODAL_BAD
) vg_error( message
);
2231 * -----------------------------------------------------------------------------
2237 static void _ui_proc_key( SDL_Keysym ev
){
2238 if( vg_ui
.focused_control_type
!= k_ui_control_textbox
){
2242 struct textbox_mapping
{
2246 void (*handler
)(void);
2250 { 0, SDLK_LEFT
, _ui_textbox_left
},
2251 { KMOD_SHIFT
, SDLK_LEFT
, _ui_textbox_left_select
},
2252 { 0, SDLK_RIGHT
, _ui_textbox_right
},
2253 { KMOD_SHIFT
, SDLK_RIGHT
, _ui_textbox_right_select
},
2254 { 0, SDLK_DOWN
, _ui_textbox_down
},
2255 { 0, SDLK_UP
, _ui_textbox_up
},
2256 { 0, SDLK_BACKSPACE
, _ui_textbox_backspace
},
2257 { KMOD_SHIFT
, SDLK_BACKSPACE
, _ui_textbox_backspace
},
2258 { KMOD_CTRL
, SDLK_BACKSPACE
, _ui_textbox_backspace
},
2259 { 0, SDLK_DELETE
, _ui_textbox_delete
},
2260 { 0, SDLK_HOME
, _ui_textbox_home
},
2261 { KMOD_SHIFT
, SDLK_HOME
, _ui_textbox_home_select
},
2262 { 0, SDLK_END
, _ui_textbox_end
},
2263 { KMOD_SHIFT
, SDLK_END
, _ui_textbox_end_select
},
2264 { KMOD_CTRL
, SDLK_a
, _ui_textbox_select_all
},
2265 { KMOD_CTRL
, SDLK_c
, _ui_textbox_to_clipboard
},
2266 { KMOD_CTRL
, SDLK_x
, _ui_textbox_cut
},
2267 { KMOD_CTRL
, SDLK_v
, _ui_textbox_clipboard_paste
},
2268 { 0, SDLK_RETURN
, _ui_textbox_enter
},
2269 { 0, SDLK_ESCAPE
, ui_defocus_all
},
2274 if( ev
.mod
& KMOD_SHIFT
)
2277 if( ev
.mod
& KMOD_CTRL
)
2280 if( ev
.mod
& KMOD_ALT
)
2283 for( int i
=0; i
<vg_list_size( mappings
); i
++ ){
2284 struct textbox_mapping
*mapping
= &mappings
[i
];
2286 if( mapping
->key
== ev
.sym
){
2287 if( mapping
->mod
== 0 ){
2293 else if( (mod
& mapping
->mod
) == mapping
->mod
){
2302 * Callback for text entry mode
2304 static void ui_proc_utf8( const char *text
){
2305 if( vg_ui
.focused_control_type
== k_ui_control_textbox
){
2306 const char *ptr
= text
;
2309 if( *ptr
!= '`' ) _ui_textbox_put_char( *ptr
);
2313 _ui_textbox_change_callback();
2319 * -----------------------------------------------------------------------------
2322 static void ui_dev_colourview(void){
2323 ui_rect window
= {vg
.window_x
-256,0,256,vg
.window_y
}, swatch
;
2325 const char *names
[vg_list_size(vg_ui
.scheme
)] = {
2326 [k_ui_bg
] = "k_ui_bg", "k_ui_bg+1", "k_ui_bg+2", "k_ui_bg+3",
2327 "k_ui_bg+4", "k_ui_bg+5", "k_ui_bg+6", "k_ui_bg+7",
2329 [k_ui_fg
] = "k_ui_fg", "k_ui_fg+1", "k_ui_fg+2", "k_ui_fg+3",
2330 "k_ui_fg+4", "k_ui_fg+5", "k_ui_fg+6", "k_ui_fg+7",
2332 [k_ui_red
] = "k_ui_red", "k_ui_orange", "k_ui_yellow", "k_ui_green",
2333 "k_ui_aqua", "k_ui_blue", "k_ui_purple", "k_ui_gray",
2334 "k_ui_red+8","k_ui_orange+8","k_ui_yellow+8","k_ui_green+8",
2335 "k_ui_aqua+8","k_ui_blue+8","k_ui_purple+8","k_ui_gray+8" };
2338 ui_split_ratio( window
, k_ui_axis_v
, 0.5f
, 0, col
[0], col
[1] );
2340 for( int i
=0; i
<vg_list_size(vg_ui
.scheme
); i
++ ){
2341 int which
= (i
/8)%2;
2343 ui_split( col
[which
], k_ui_axis_h
, 24, 0, swatch
, col
[which
] );
2344 ui_fill( swatch
, ui_colour(i
) );
2347 ui_text(swatch
, names
[i
], 1, k_ui_align_middle_left
, ui_colourcont(i
));
2351 #endif /* VG_IMGUI_H */