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 struct ui_vert ui_vert
;
32 /* Relative to cursor p0 */
35 k_ui_align_lwr
= 0xff,
36 k_ui_align_left
= 0x0000| 0x00,
37 k_ui_align_right
= 0x0000| 0x01,
38 k_ui_align_center
= 0x0000| 0x02,
40 k_ui_align_middle
= 0x0100,
41 k_ui_align_middle_left
= 0x0100| 0x00,
42 k_ui_align_middle_right
= 0x0100| 0x01,
43 k_ui_align_middle_center
= 0x0100| 0x02,
45 k_ui_align_bottom
= 0x0200,
46 k_ui_align_bottom_left
= 0x0200| 0x00,
47 k_ui_align_bottom_right
= 0x0200| 0x01,
48 k_ui_align_bottom_center
= 0x0200| 0x02,
60 enum ui_scheme_colour
{
75 static ui_px k_ui_widget_height
= 28,
79 typedef u32 ui_scheme
[8*4];
92 typedef struct ui_font ui_font
;
94 static const ui_font vg_ui_font_small
= {
115 #define UI_RGB( STDHEX ) 0xff000000 |\
116 ((STDHEX&0x000000ff)<<16) |\
117 ((STDHEX&0x0000ff00) ) |\
118 ((STDHEX&0x00ff0000)>>16)
120 #define UI_TEXTBOX_MULTILINE 0x1
121 #define UI_TEXTBOX_WRAP 0x2
122 #define UI_TEXTBOX_AUTOFOCUS 0x4
124 #define UI_MODAL_OK 0x0
125 #define UI_MODAL_GOOD 0x1
126 #define UI_MODAL_BAD 0x2
127 #define UI_MODAL_WARN 0x3
128 #define UI_MODAL_TYPE_BITS 0x3
130 #define UI_MOUSE_LEFT (SDL_BUTTON(SDL_BUTTON_LEFT))
131 #define UI_MOUSE_RIGHT (SDL_BUTTON(SDL_BUTTON_RIGHT))
132 #define UI_MOUSE_MIDDLE (SDL_BUTTON(SDL_BUTTON_MIDDLE))
136 #define UI_BOTTOM 0x4
140 struct ui_vert
*vertex_buffer
;
142 u32 max_verts
, max_indices
,
143 cur_vert
, cur_indice
,
144 vert_start
, indice_start
;
147 void *focused_control_id
; /* uses the memory location of various locking
153 u32 focused_control_hit
;
154 enum ui_control_type
{
156 k_ui_control_textbox
,
160 focused_control_type
;
162 union{ /* controls that can be focused */
164 int cursor_user
, cursor_pos
;
168 struct ui_textbox_callbacks
{
169 void (*enter
)( char *, u32
),
170 (*up
)( char *, u32
),
171 (*down
)( char *, u32
),
172 (*change
)( char *, u32
);
194 struct ui_modal_callbacks
{
201 GLuint tex_glyphs
, vao
, vbo
, ebo
;
203 ui_px mouse
[2], mouse_click
[2];
205 u32 ignore_input_frames
;
208 ui_rect click_fader
, click_fader_end
;
209 float click_fade_opacity
;
222 SDL_Cursor
*cursor_map
[ k_ui_cursor_max
];
228 [ k_ui_bg
+0 ] = UI_RGB( 0x1d2021 ),
229 [ k_ui_bg
+1 ] = UI_RGB( 0x282828 ),
230 [ k_ui_bg
+2 ] = UI_RGB( 0x3c3836 ),
231 [ k_ui_bg
+3 ] = UI_RGB( 0x504945 ),
232 [ k_ui_bg
+4 ] = UI_RGB( 0x665c54 ),
233 [ k_ui_bg
+5 ] = UI_RGB( 0x7c6f64 ),
234 [ k_ui_bg
+6 ] = UI_RGB( 0x928374 ),
235 [ k_ui_bg
+7 ] = UI_RGB( 0xa89984 ),
237 [ k_ui_fg
+0 ] = UI_RGB( 0xebdbb2 ),
238 [ k_ui_fg
+1 ] = UI_RGB( 0xfbf1c7 ),
239 [ k_ui_fg
+2 ] = UI_RGB( 0xd5c4a1 ),
240 [ k_ui_fg
+3 ] = UI_RGB( 0xbdae93 ),
241 [ k_ui_fg
+4 ] = UI_RGB( 0xa89984 ),
242 [ k_ui_fg
+5 ] = UI_RGB( 0x000000 ),
243 [ k_ui_fg
+6 ] = UI_RGB( 0x000000 ),
244 [ k_ui_fg
+7 ] = UI_RGB( 0x000000 ),
246 [ k_ui_red
] = UI_RGB( 0xcc241d ),
247 [ k_ui_orange
] = UI_RGB( 0xd65d0e ),
248 [ k_ui_yellow
] = UI_RGB( 0xd79921 ),
249 [ k_ui_green
] = UI_RGB( 0x98971a ),
250 [ k_ui_aqua
] = UI_RGB( 0x689d6a ),
251 [ k_ui_blue
] = UI_RGB( 0x458588 ),
252 [ k_ui_purple
] = UI_RGB( 0xb16286 ),
253 [ k_ui_gray
] = UI_RGB( 0x928374 ),
254 [ k_ui_red
+ k_ui_brighter
] = UI_RGB( 0xfb4934 ),
255 [ k_ui_orange
+ k_ui_brighter
] = UI_RGB( 0xfe8019 ),
256 [ k_ui_yellow
+ k_ui_brighter
] = UI_RGB( 0xfabd2f ),
257 [ k_ui_green
+ k_ui_brighter
] = UI_RGB( 0xb8bb26 ),
258 [ k_ui_aqua
+ k_ui_brighter
] = UI_RGB( 0x8ec07c ),
259 [ k_ui_blue
+ k_ui_brighter
] = UI_RGB( 0x83a598 ),
260 [ k_ui_purple
+ k_ui_brighter
] = UI_RGB( 0xd3869b ),
261 [ k_ui_gray
+ k_ui_brighter
] = UI_RGB( 0xa89984 ),
263 .font
= &vg_ui_font_small
,
264 .colour
= {1.0f
,1.0f
,1.0f
,1.0f
}
267 static struct vg_shader _shader_ui
=
275 "layout (location=0) in vec2 a_co;"
276 "layout (location=1) in vec2 a_uv;"
277 "layout (location=2) in vec4 a_colour;"
280 "out vec2 aTexCoords;"
286 "gl_Position = vec4( uPv * vec3( a_co, 1.0 ), 1.0 );"
287 "aTexCoords = a_uv * 0.00390625;" /* TODO: Uniform */
288 "aColour = a_colour;"
297 "uniform sampler2D uTexGlyphs;"
298 "uniform vec4 uColour;"
299 "out vec4 FragColor;"
301 "in vec2 aTexCoords;"
309 "vec4 diffuse = aColour;"
311 "if( diffuse.a == 0.0 )"
313 "diffuse.a = texture( uTexGlyphs, aTexCoords ).r;"
316 "FragColor = diffuse * uColour;"
321 static struct vg_shader _shader_ui_image
= {
322 .name
= "[vg] ui_image",
328 "layout (location=0) in vec2 a_co;"
329 "layout (location=1) in vec2 a_uv;"
330 "layout (location=2) in vec4 a_colour;"
333 "out vec2 aTexCoords;"
339 "gl_Position = vec4( uPv * vec3( a_co, 1.0 ), 1.0 );"
340 "aTexCoords = a_uv * 0.00390625;"
341 "aColour = a_colour;"
350 "uniform sampler2D uTexImage;"
351 "uniform vec4 uColour;"
352 "out vec4 FragColor;"
354 "in vec2 aTexCoords;"
360 "vec4 colour = texture( uTexImage, aTexCoords );"
361 "FragColor = colour * uColour;"
366 static void _vg_ui_init(void){
367 if( !vg_shader_compile( &_shader_ui
) ||
368 !vg_shader_compile( &_shader_ui_image
) )
369 vg_fatal_error( "Failed to compile ui shader" );
373 * ----------------------------------------
376 vg_ui
.max_indices
= 20000;
377 vg_ui
.max_verts
= 30000;
379 /* Generate the buffer we are gonna be drawing to */
380 glGenVertexArrays( 1, &vg_ui
.vao
);
381 glGenBuffers( 1, &vg_ui
.vbo
);
382 glGenBuffers( 1, &vg_ui
.ebo
);
384 glBindVertexArray( vg_ui
.vao
);
385 glBindBuffer( GL_ARRAY_BUFFER
, vg_ui
.vbo
);
387 glBufferData( GL_ARRAY_BUFFER
,
388 vg_ui
.max_verts
* sizeof( struct ui_vert
),
389 NULL
, GL_DYNAMIC_DRAW
);
390 glBindVertexArray( vg_ui
.vao
);
392 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, vg_ui
.ebo
);
393 glBufferData( GL_ELEMENT_ARRAY_BUFFER
,
394 vg_ui
.max_indices
* sizeof( u16
), NULL
, GL_DYNAMIC_DRAW
);
399 u32
const stride
= sizeof( struct ui_vert
);
402 glVertexAttribPointer( 0, 2, GL_SHORT
, GL_FALSE
, stride
,
403 (void *)offsetof( struct ui_vert
, co
) );
404 glEnableVertexAttribArray( 0 );
407 glVertexAttribPointer( 1, 2, GL_UNSIGNED_SHORT
, GL_FALSE
, stride
,
408 (void *)offsetof( struct ui_vert
, uv
) );
409 glEnableVertexAttribArray( 1 );
412 glVertexAttribPointer( 2, 4, GL_UNSIGNED_BYTE
, GL_TRUE
, stride
,
413 (void *)offsetof( struct ui_vert
, colour
) );
414 glEnableVertexAttribArray( 2 );
418 /* Alloc RAM default context */
419 u32 vert_size
= vg_ui
.max_verts
*sizeof(struct ui_vert
),
420 inds_size
= vg_align8( vg_ui
.max_indices
*sizeof(u16
) );
422 vg_ui
.vertex_buffer
= vg_linear_alloc( vg_mem
.rtmemory
, vert_size
);
423 vg_ui
.indice_buffer
= vg_linear_alloc( vg_mem
.rtmemory
, inds_size
);
426 * -----------------------------------------------------
429 /* Load default font */
431 #include "vg/vg_pxfont_thin.h"
434 u32 pixels
= 0, total
= 256*256, data
= 0;
437 while( pixels
< total
){
438 for( int b
= 31; b
>= 0; b
-- ){
439 image
[ pixels
++ ] = (compressed
[data
] & (0x1u
<< b
))? 0xffu
: 0x00u
;
441 if( pixels
>= total
){
449 glGenTextures( 1, &vg_ui
.tex_glyphs
);
450 glBindTexture( GL_TEXTURE_2D
, vg_ui
.tex_glyphs
);
451 glTexImage2D( GL_TEXTURE_2D
, 0, GL_R8
, 256, 256, 0,
452 GL_RED
, GL_UNSIGNED_BYTE
, image
);
455 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
456 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
457 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
458 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
462 * ---------------------------------------------------------------
465 vg_ui
.cursor_map
[ k_ui_cursor_default
] =
466 SDL_CreateSystemCursor( SDL_SYSTEM_CURSOR_ARROW
);
467 vg_ui
.cursor_map
[ k_ui_cursor_hand
] =
468 SDL_CreateSystemCursor( SDL_SYSTEM_CURSOR_HAND
);
469 vg_ui
.cursor_map
[ k_ui_cursor_ibeam
] =
470 SDL_CreateSystemCursor( SDL_SYSTEM_CURSOR_IBEAM
);
478 static void rect_copy( ui_rect a
, ui_rect b
){
479 for( int i
=0; i
<4; i
++ )
483 static void ui_flush( enum ui_shader shader
){
484 u32 vertex_offset
= vg_ui
.vert_start
*sizeof(ui_vert
),
485 vertex_count
= vg_ui
.cur_vert
-vg_ui
.vert_start
,
486 vertex_size
= vertex_count
*sizeof(ui_vert
),
488 indice_offset
= vg_ui
.indice_start
*sizeof(u16
),
489 indice_count
= vg_ui
.cur_indice
-vg_ui
.indice_start
,
490 indice_size
= indice_count
* sizeof(u16
);
492 if( !vertex_size
|| !indice_size
)
495 glBindVertexArray( vg_ui
.vao
);
496 glBindBuffer( GL_ARRAY_BUFFER
, vg_ui
.vbo
);
497 glBufferSubData( GL_ARRAY_BUFFER
, vertex_offset
, vertex_size
,
498 vg_ui
.vertex_buffer
+vg_ui
.vert_start
);
500 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, vg_ui
.ebo
);
501 glBufferSubData( GL_ELEMENT_ARRAY_BUFFER
, indice_offset
, indice_size
,
502 vg_ui
.indice_buffer
+vg_ui
.indice_start
);
504 glDisable( GL_DEPTH_TEST
);
505 glEnable( GL_BLEND
);
506 glBlendFunc( GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
507 glBlendEquation( GL_FUNC_ADD
);
508 glDisable( GL_CULL_FACE
);
510 m3x3f view
= M3X3_IDENTITY
;
511 m3x3_translate( view
, (v3f
){ -1.0f
, 1.0f
, 0.0f
} );
512 m3x3_scale( view
, (v3f
){ 1.0f
/((float)vg
.window_x
*0.5f
),
513 -1.0f
/((float)vg
.window_y
*0.5f
), 1.0f
} );
515 if( shader
== k_ui_shader_colour
){
516 glUseProgram( _shader_ui
.id
);
518 glUniformMatrix3fv( glGetUniformLocation( _shader_ui
.id
, "uPv" ), 1,
519 GL_FALSE
, (float *)view
);
520 glUniform4fv( glGetUniformLocation( _shader_ui
.id
, "uColour" ), 1,
523 glActiveTexture( GL_TEXTURE0
);
524 glBindTexture( GL_TEXTURE_2D
, vg_ui
.tex_glyphs
);
525 glUniform1i( glGetUniformLocation( _shader_ui
.id
, "uTexGlyphs" ), 0 );
527 else if( shader
== k_ui_shader_image
){
528 glUseProgram( _shader_ui_image
.id
);
529 glUniformMatrix3fv( glGetUniformLocation( _shader_ui_image
.id
, "uPv" ), 1,
530 GL_FALSE
, (float *)view
);
531 glUniform1i( glGetUniformLocation(_shader_ui_image
.id
,"uTexImage"), 0 );
532 glUniform4fv( glGetUniformLocation( _shader_ui_image
.id
, "uColour" ), 1,
536 vg_fatal_error( "Invalid UI shader (%d)\n", shader
);
538 glDrawElements( GL_TRIANGLES
, indice_count
, GL_UNSIGNED_SHORT
,
539 (void *)(vg_ui
.indice_start
*sizeof(u16
)) );
541 glDisable( GL_BLEND
);
543 vg_ui
.indice_start
= vg_ui
.cur_indice
;
544 vg_ui
.vert_start
= vg_ui
.cur_vert
;
547 static void ui_fill_rect( ui_rect rect
, u32 colour
, ui_px uv
[4] ){
548 /* this if far from ideal but stops us from crashing */
549 if( (vg_ui
.cur_vert
+ 4 > vg_ui
.max_verts
) ||
550 (vg_ui
.cur_indice
+ 6 > vg_ui
.max_indices
))
553 struct ui_vert
*vertices
= &vg_ui
.vertex_buffer
[ vg_ui
.cur_vert
];
554 u16
*indices
= &vg_ui
.indice_buffer
[ vg_ui
.cur_indice
];
556 for( int i
=0; i
<4; i
++ ){
557 vertices
[i
].colour
= colour
;
560 vertices
[0].co
[0] = rect
[0];
561 vertices
[0].co
[1] = rect
[1];
562 vertices
[0].uv
[0] = uv
[0];
563 vertices
[0].uv
[1] = uv
[1];
564 vertices
[1].co
[0] = rect
[0]+rect
[2];
565 vertices
[1].co
[1] = rect
[1];
566 vertices
[1].uv
[0] = uv
[2];
567 vertices
[1].uv
[1] = uv
[1];
568 vertices
[2].co
[0] = rect
[0]+rect
[2];
569 vertices
[2].co
[1] = rect
[1]+rect
[3];
570 vertices
[2].uv
[0] = uv
[2];
571 vertices
[2].uv
[1] = uv
[3];
572 vertices
[3].co
[0] = rect
[0];
573 vertices
[3].co
[1] = rect
[1]+rect
[3];
574 vertices
[3].uv
[0] = uv
[0];
575 vertices
[3].uv
[1] = uv
[3];
576 u16 ind_start
= vg_ui
.cur_vert
;
578 u16 start
= vg_ui
.cur_vert
;
579 u32 mesh
[] = { 0,2,1, 0,3,2 };
581 for( u32 i
=0; i
<vg_list_size(mesh
); i
++ ){
582 indices
[i
] = start
+mesh
[i
];
585 vg_ui
.cur_indice
+= 6;
589 static void ui_fill( ui_rect rect
, u32 colour
){
590 ui_fill_rect( rect
, colour
, (ui_px
[4]){ 4,4,4,4 } );
593 static void ui_outline( ui_rect rect
, ui_px thickness
, u32 colour
, u32 mask
){
594 /* this if far from ideal but stops us from crashing */
595 if( (vg_ui
.cur_vert
+ 8 > vg_ui
.max_verts
) ||
596 (vg_ui
.cur_indice
+ 24 > vg_ui
.max_indices
))
599 struct ui_vert
*vertices
= &vg_ui
.vertex_buffer
[ vg_ui
.cur_vert
];
600 u16
*indices
= &vg_ui
.indice_buffer
[ vg_ui
.cur_indice
];
602 for( int i
=0; i
<8; i
++ ){
603 vertices
[i
].uv
[0] = 4;
604 vertices
[i
].uv
[1] = 4;
605 vertices
[i
].colour
= colour
;
608 vertices
[0].co
[0] = rect
[0];
609 vertices
[0].co
[1] = rect
[1];
610 vertices
[1].co
[0] = rect
[0]+rect
[2];
611 vertices
[1].co
[1] = rect
[1];
612 vertices
[2].co
[0] = rect
[0]+rect
[2];
613 vertices
[2].co
[1] = rect
[1]+rect
[3];
614 vertices
[3].co
[0] = rect
[0];
615 vertices
[3].co
[1] = rect
[1]+rect
[3];
616 vertices
[4].co
[0] = vertices
[0].co
[0]-thickness
;
617 vertices
[4].co
[1] = vertices
[0].co
[1]-thickness
;
618 vertices
[5].co
[0] = vertices
[1].co
[0]+thickness
;
619 vertices
[5].co
[1] = vertices
[1].co
[1]-thickness
;
620 vertices
[6].co
[0] = vertices
[2].co
[0]+thickness
;
621 vertices
[6].co
[1] = vertices
[2].co
[1]+thickness
;
622 vertices
[7].co
[0] = vertices
[3].co
[0]-thickness
;
623 vertices
[7].co
[1] = vertices
[3].co
[1]+thickness
;
625 u16 start
= vg_ui
.cur_vert
;
626 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 };
629 mask
= UI_TOP
|UI_LEFT
|UI_BOTTOM
|UI_RIGHT
;
632 for( u32 i
=0; i
<vg_list_size(mesh
)/6; i
++ ){
633 if( (0x1<<i
) & mask
){
634 for( u32 j
=0; j
<6; j
++ )
635 indices
[c
++] = start
+mesh
[i
*6+j
];
639 vg_ui
.cur_indice
+= c
;
643 static void ui_split( ui_rect rect
,
644 enum ui_axis other
, ui_px width
, ui_px gap
,
645 ui_rect l
, ui_rect r
){
646 enum ui_axis dir
= other
^ 0x1;
648 if( width
< 0 ) width
= rect
[ 2+dir
] + width
;
651 rect_copy( rect
, temp
);
653 l
[ dir
] = temp
[ dir
];
654 r
[ dir
] = temp
[ dir
] + width
+ (gap
/2);
655 l
[ other
] = temp
[ other
];
656 r
[ other
] = temp
[ other
];
657 l
[ 2+dir
] = width
- (gap
/2);
658 r
[ 2+dir
] = temp
[ 2+dir
] - width
- (gap
/2);
659 l
[ 2+other
] = temp
[ 2+other
];
660 r
[ 2+other
] = temp
[ 2+other
];
663 static ui_px
ui_text_line_width( const char *str
);
665 static void ui_rect_center( ui_rect parent
, ui_rect rect
){
666 rect
[0] = parent
[0] + (parent
[2]-rect
[2])/2;
667 rect
[1] = parent
[1] + (parent
[3]-rect
[3])/2;
670 static void ui_fit_item( ui_rect rect
, ui_px size
[2], ui_rect d
){
671 i32 rp
= (i32
)rect
[2] * (i32
)size
[1],
672 rc
= (i32
)size
[0] * (i32
)rect
[3];
674 enum ui_axis dir
, other
;
675 if( rc
> rp
) dir
= k_ui_axis_h
;
676 else dir
= k_ui_axis_v
;
679 d
[2+dir
] = rect
[2+dir
];
680 d
[2+other
] = (rect
[2+dir
] * size
[other
]) / size
[dir
];
682 ui_rect_center( rect
, d
);
685 static void ui_split_ratio( ui_rect rect
, enum ui_axis dir
, float ratio
,
686 ui_px gap
, ui_rect l
, ui_rect r
){
687 ui_px width
= (float)rect
[ 2+(dir
^0x1) ] * ratio
;
688 ui_split( rect
, dir
, width
, gap
, l
, r
);
691 static void ui_rect_pad( ui_rect rect
, ui_px pad
[2] ){
698 static ui_px
ui_text_line_width( const char *str
){
700 const char *_c
= str
;
703 while( (c
= *(_c
++)) ){
704 if( c
>= 32 ) length
++;
705 else if( c
== '\n' ) break;
708 return length
* vg_ui
.font
->spacing
;
711 static ui_px
ui_text_string_height( const char *str
){
713 const char *_c
= str
;
716 while( (c
= *(_c
++)) ){
717 if( c
== '\n' ) height
++;
723 static ui_px
ui_text_aligned_x( const char *str
, ui_rect rect
, ui_px scale
,
724 enum ui_align align
){
725 enum ui_align lwr
= k_ui_align_lwr
& align
;
726 if( lwr
== k_ui_align_left
){
730 ui_px width
= ui_text_line_width( str
) * scale
;
732 if( lwr
== k_ui_align_right
)
733 return rect
[0] + rect
[2]-width
;
735 return rect
[0] + (rect
[2]-width
)/2;
739 static ui_px
ui_min( ui_px a
, ui_px b
){ return a
<b
?a
:b
; }
740 static ui_px
ui_max( ui_px a
, ui_px b
){ return a
>b
?a
:b
; }
741 static ui_px
ui_clamp( ui_px a
, ui_px min
, ui_px max
){
742 return ui_min( max
, ui_max( a
, min
) );
745 static int ui_clip( ui_rect parent
, ui_rect child
, ui_rect clipped
){
746 ui_px parent_max
[2], child_max
[2];
747 parent_max
[0] = parent
[0]+parent
[2];
748 parent_max
[1] = parent
[1]+parent
[3];
749 child_max
[0] = child
[0]+child
[2];
750 child_max
[1] = child
[1]+child
[3];
752 clipped
[0] = ui_clamp( child
[0], parent
[0], parent_max
[0] );
753 clipped
[1] = ui_clamp( child
[1], parent
[1], parent_max
[1] );
754 clipped
[2] = ui_clamp( child_max
[0], parent
[0], parent_max
[0] );
755 clipped
[3] = ui_clamp( child_max
[1], parent
[1], parent_max
[1] );
757 if( clipped
[0] == clipped
[2] ||
758 clipped
[1] == clipped
[3] )
761 clipped
[2] -= clipped
[0];
762 clipped
[3] -= clipped
[1];
767 static int ui_inside_rect( ui_rect rect
, ui_px co
[2] ){
768 if( co
[0] >= rect
[0] &&
770 co
[0] < rect
[0]+rect
[2] &&
771 co
[1] < rect
[1]+rect
[3] ){
778 static int ui_click_down( u32 mask
){
779 if( vg_ui
.ignore_input_frames
) return 0;
780 if( (vg_ui
.mouse_state
[0] & mask
) &&
781 !(vg_ui
.mouse_state
[1] & mask
) )
787 static int ui_clicking( u32 mask
){
788 if( vg_ui
.ignore_input_frames
) return 0;
789 return vg_ui
.mouse_state
[0] & mask
;
792 static int ui_click_up( u32 mask
){
793 if( vg_ui
.ignore_input_frames
) return 0;
794 if( (vg_ui
.mouse_state
[1] & mask
) &&
795 !(vg_ui
.mouse_state
[0] & mask
) )
801 static void ui_prerender(void){
803 vg_ui
.mouse_state
[1] = vg_ui
.mouse_state
[0];
804 vg_ui
.mouse_state
[0] = SDL_GetMouseState( &x
, &y
);
809 vg_ui
.cur_indice
= 0;
810 vg_ui
.vert_start
= 0;
811 vg_ui
.indice_start
= 0;
812 vg_ui
.focused_control_hit
= 0;
813 vg_ui
.cursor
= k_ui_cursor_default
;
814 vg_ui
.wants_mouse
= 0;
816 if( vg_ui
.ignore_input_frames
){
817 vg_ui
.ignore_input_frames
--;
821 if( ui_click_down(UI_MOUSE_LEFT
)||ui_click_down(UI_MOUSE_MIDDLE
) ){
822 vg_ui
.mouse_click
[0] = vg_ui
.mouse
[0];
823 vg_ui
.mouse_click
[1] = vg_ui
.mouse
[1];
827 static u32
ui_colour( enum ui_scheme_colour id
){
828 return vg_ui
.scheme
[ id
];
831 /* get an appropriately contrasting colour given the base */
832 static u32
ui_colourcont( enum ui_scheme_colour id
){
833 if ( id
< k_ui_bg
+6 ) return ui_colour( k_ui_fg
);
834 else if( id
< k_ui_fg
) return ui_colour( k_ui_bg
+1 );
835 else if( id
< k_ui_hue
) return ui_colour( k_ui_bg
+3 );
836 else if( id
< k_ui_red
+k_ui_brighter
) return ui_colour( k_ui_fg
);
837 else return ui_colour( k_ui_fg
+1 );
840 static void ui_hex_to_norm( u32 hex
, v4f norm
){
841 norm
[0] = ((hex
) & 0xff);
842 norm
[1] = ((hex
>>8 ) & 0xff);
843 norm
[2] = ((hex
>>16) & 0xff);
844 norm
[3] = ((hex
>>24) & 0xff);
845 v4_muls( norm
, 1.0f
/255.0f
, norm
);
848 static void ui_text_glyph( const struct ui_font
*font
, ui_px scale
,
849 u8 glyph
, ui_rect out_texcoords
){
850 glyph
-= font
->ascii_start
;
852 ui_px per_row
= font
->sheet_size
/ font
->glyph_width
,
853 column
= (ui_px
)glyph
% per_row
,
854 row
= (glyph
- column
) / per_row
;
856 out_texcoords
[0] = column
* font
->glyph_width
;
857 out_texcoords
[1] = row
* font
->glyph_height
+ font
->offset_y
;
858 out_texcoords
[2] = out_texcoords
[0] + font
->glyph_width
;
859 out_texcoords
[3] = out_texcoords
[1] + font
->glyph_height
;
862 static u32
ui_opacity( u32 colour
, f32 opacity
){
863 u32 alpha
= opacity
* 255.0f
;
864 return (colour
& 0x00ffffff) | (alpha
<< 24);
867 static u32
ui_ntext( ui_rect rect
, const char *str
, u32 len
, ui_px scale
,
868 enum ui_align align
, u32 colour
){
870 if( colour
== 0 ) colour
= ui_colour( k_ui_fg
);
872 colour
&= 0x00ffffff;
874 const char *_c
= str
;
877 text_cursor
[0] = ui_text_aligned_x( str
, rect
, scale
, align
);
878 text_cursor
[1] = rect
[1];
879 text_cursor
[2] = vg_ui
.font
->glyph_width
*scale
;
880 text_cursor
[3] = vg_ui
.font
->glyph_height
*scale
;
882 u32 printed_chars
= 0;
884 if( align
& (k_ui_align_middle
|k_ui_align_bottom
) ){
885 ui_px height
= ui_text_string_height( str
) * scale
;
887 if( align
& k_ui_align_bottom
)
888 text_cursor
[1] += rect
[3]-height
;
890 text_cursor
[1] += (rect
[3]-height
)/2;
893 while( (c
= *(_c
++)) ){
894 if( printed_chars
>= len
){
896 text_cursor
[1] += vg_ui
.font
->line_height
*scale
;
897 text_cursor
[0] = ui_text_aligned_x( _c
, rect
, scale
, align
);
898 text_cursor
[0] -= vg_ui
.font
->spacing
*scale
;
901 ui_text_glyph( vg_ui
.font
, scale
, '\xb6' /*FIXME*/, glyph
);
902 ui_fill_rect( text_cursor
, 0x00ffffff, glyph
);
903 text_cursor
[0] += vg_ui
.font
->spacing
*scale
;
907 text_cursor
[1] += vg_ui
.font
->line_height
*scale
;
908 text_cursor
[0] = ui_text_aligned_x( _c
, rect
, scale
, align
);
914 ui_text_glyph( vg_ui
.font
, scale
, c
, glyph
);
916 ui_rect cursor_clipped
;
917 if( ui_clip( rect
, text_cursor
, cursor_clipped
) ){
918 ui_fill_rect( cursor_clipped
, colour
, glyph
);
921 else if( c
== '\x1B' ){
925 for( int i
=0; i
<3; i
++ ){
931 case '0': colour
= ui_colour( k_ui_fg
); break;
932 case '3'|'0'<<8: colour
= ui_colour( k_ui_bg
); break;
933 case '3'|'1'<<8: colour
= ui_colour( k_ui_red
); break;
934 case '3'|'2'<<8: colour
= ui_colour( k_ui_green
); break;
935 case '3'|'3'<<8: colour
= ui_colour( k_ui_yellow
); break;
936 case '3'|'4'<<8: colour
= ui_colour( k_ui_blue
); break;
937 case '3'|'5'<<8: colour
= ui_colour( k_ui_purple
); break;
938 case '3'|'6'<<8: colour
= ui_colour( k_ui_aqua
); break;
939 case '3'|'7'<<8: colour
= 0xffffffff; break;
942 colour
&= 0x00ffffff;
946 colour_id
|= _c
[i
] << (i
*8);
956 else if( c
== '\t' ){
957 text_cursor
[0] += vg_ui
.font
->spacing
*scale
*4;
962 text_cursor
[0] += vg_ui
.font
->spacing
*scale
;
966 return printed_chars
;
969 static void ui_text( ui_rect rect
, const char *str
, ui_px scale
,
970 enum ui_align align
, u32 colour
){
971 ui_ntext( rect
, str
, 1024, scale
, align
, colour
);
975 * Standard layout stuff
976 * -----------------------------------------------------------------------------
979 static void ui_panel( ui_rect in_rect
, ui_rect out_panel
){
980 ui_fill( in_rect
, ui_colour( k_ui_bg
+1 ) );
981 ui_outline( in_rect
, 1, ui_colour( k_ui_bg
+7 ), 0 );
982 rect_copy( in_rect
, out_panel
);
983 ui_rect_pad( out_panel
, (ui_px
[2]){ k_ui_padding
, k_ui_padding
} );
986 static void ui_label( ui_rect rect
, const char *text
, ui_px size
,
987 ui_px gap
, ui_rect r
){
989 ui_px width
= (ui_text_line_width(text
)+vg_ui
.font
->spacing
) * size
;
990 ui_split( rect
, k_ui_axis_v
, width
, gap
, l
, r
);
991 ui_text( l
, text
, 1, k_ui_align_middle_left
, 0 );
994 static void ui_standard_widget( ui_rect inout_panel
, ui_rect out_rect
,
996 ui_px height
= (count
* vg_ui
.font
->glyph_height
+ 18) * k_ui_scale
;
997 ui_split( inout_panel
, k_ui_axis_h
, height
, k_ui_padding
,
998 out_rect
, inout_panel
);
1001 static void ui_info( ui_rect inout_panel
, const char *text
){
1003 ui_standard_widget( inout_panel
, box
, 1 );
1004 ui_text( box
, text
, 1, k_ui_align_middle_left
, 0 );
1007 static void ui_image( ui_rect rect
, GLuint image
){
1008 ui_flush( k_ui_shader_colour
);
1009 glActiveTexture( GL_TEXTURE0
);
1010 glBindTexture( GL_TEXTURE_2D
, image
);
1011 ui_fill_rect( rect
, 0xffffffff, (ui_px
[4]){ 0,256,256,0 } );
1012 ui_flush( k_ui_shader_image
);
1015 static u32
v4f_u32_colour( v4f colour
){
1016 u32 r
= colour
[0] * 255.0f
,
1017 g
= colour
[1] * 255.0f
,
1018 b
= colour
[2] * 255.0f
,
1019 a
= colour
[3] * 255.0f
;
1021 return r
| (g
<<8) | (b
<<16) | (a
<<24);
1024 static void ui_defocus_all(void){
1025 if( vg_ui
.focused_control_type
== k_ui_control_textbox
){
1026 SDL_StopTextInput();
1029 vg_ui
.focused_control_id
= NULL
;
1030 vg_ui
.focused_control_hit
= 0;
1031 vg_ui
.focused_control_type
= k_ui_control_none
;
1034 enum ui_button_state
{
1035 k_ui_button_none
= 0,
1036 k_ui_button_click
= 1,
1037 k_ui_button_holding_inside
= 2,
1038 k_ui_button_holding_outside
= 3,
1039 k_ui_button_hover
= 4
1042 static enum ui_button_state
ui_colourbutton( ui_rect rect
,
1043 enum ui_scheme_colour colour
){
1044 int clickup
= ui_click_up(UI_MOUSE_LEFT
),
1045 click
= ui_clicking(UI_MOUSE_LEFT
) | clickup
,
1046 target
= ui_inside_rect( rect
, vg_ui
.mouse_click
) && click
,
1047 hover
= ui_inside_rect( rect
, vg_ui
.mouse
);
1049 u32 col_base
= vg_ui
.scheme
[ colour
],
1050 col_highlight
= vg_ui
.scheme
[ k_ui_fg
],
1051 col_hover
= vg_ui
.scheme
[ colour
+ k_ui_brighter
];
1053 if( vg_ui
.focused_control_type
!= k_ui_control_none
){
1061 vg_ui
.cursor
= k_ui_cursor_hand
;
1068 ui_fill( rect
, col_highlight
);
1069 vg_ui
.ignore_input_frames
= 2;
1070 rect_copy( rect
, vg_ui
.click_fader
);
1072 rect_copy( rect
, vg_ui
.click_fader_end
);
1073 vg_ui
.click_fader_end
[3] = 0;
1074 ui_rect_center( rect
, vg_ui
.click_fader_end
);
1076 vg_ui
.click_fade_opacity
= 1.0f
;
1078 return k_ui_button_click
;
1081 ui_fill( rect
, col_highlight
);
1082 return k_ui_button_holding_inside
;
1086 ui_fill( rect
, col_base
);
1087 ui_outline( rect
, 1, col_highlight
, 0 );
1088 return k_ui_button_holding_outside
;
1092 ui_fill( rect
, col_base
);
1093 return k_ui_button_none
;
1098 ui_fill( rect
, col_hover
);
1099 return k_ui_button_hover
;
1102 ui_fill( rect
, col_base
);
1103 return k_ui_button_none
;
1108 static enum ui_button_state
ui_colourbutton_text(
1109 ui_rect rect
, const char *string
, ui_px scale
,
1110 enum ui_scheme_colour colour
){
1111 enum ui_button_state state
= ui_colourbutton( rect
, colour
);
1112 ui_rect t
= { 0,0, ui_text_line_width( string
)*scale
, 14*scale
};
1113 ui_rect_center( rect
, t
);
1115 u32 text_colour
= ui_colourcont(colour
);
1116 if( state
== k_ui_button_holding_inside
)
1117 text_colour
= colour
;
1119 ui_text( t
, string
, scale
, k_ui_align_left
, text_colour
);
1123 static enum ui_button_state
ui_button_text( ui_rect rect
,
1124 const char *string
, ui_px scale
){
1125 return ui_colourbutton_text( rect
, string
, scale
, k_ui_bg
+4 );
1128 static enum ui_button_state
ui_button( ui_rect inout_panel
,
1129 const char *string
){
1131 ui_standard_widget( inout_panel
, rect
, 1 );
1132 return ui_colourbutton_text( rect
, string
, 1, k_ui_bg
+4 );
1135 static void ui_enum_post(void);
1136 static void ui_postrender(void){
1137 if( vg_ui
.click_fade_opacity
> 0.0f
){
1138 float scale
= vg_ui
.click_fade_opacity
;
1139 scale
= vg_maxf( 1.0f
/255.0f
, scale
*scale
);
1141 vg_ui
.click_fade_opacity
-= vg
.time_frame_delta
* 3.8f
;
1142 u32 colour
= (0x00ffffff & ui_colour(k_ui_fg
))|0x7f000000;
1144 v4f begin
, end
, dest
;
1145 for( int i
=0; i
<4; i
++ ){
1146 begin
[i
] = vg_ui
.click_fader
[i
];
1147 end
[i
] = vg_ui
.click_fader_end
[i
]+1;
1150 v4_lerp( end
, begin
, scale
, dest
);
1153 for( int i
=0; i
<4; i
++ ){
1157 ui_fill( rect
, colour
);
1160 if( vg_ui
.focused_control_type
== k_ui_control_enum
){
1163 else if( vg_ui
.focused_control_type
== k_ui_control_modal
){
1164 ui_rect screen
= {0,0,vg
.window_x
,vg
.window_y
};
1165 ui_fill( screen
, 0xa0000000 );
1166 ui_rect box
= {0,0,400,200};
1168 u32 colour
= ui_colour(k_ui_fg
),
1169 type
= vg_ui
.modal
.options
& UI_MODAL_TYPE_BITS
;
1170 if ( type
== 1 ) colour
= ui_colour(k_ui_green
);
1171 else if( type
== 2 ) colour
= ui_colour(k_ui_red
);
1172 else if( type
== 3 ) colour
= ui_colour(k_ui_yellow
);
1174 ui_rect_center( screen
, box
);
1175 ui_fill( box
, ui_colour(k_ui_bg
) );
1176 ui_outline( box
, -1, colour
, 0 );
1179 rect_copy( box
, message
);
1181 ui_rect_center( box
, message
);
1183 ui_rect row0
, row1
, btn
;
1184 ui_split_ratio( message
, k_ui_axis_h
, 0.5f
, 0, row0
, row1
);
1185 row0
[0] += vg_ui
.font
->spacing
;
1186 ui_ntext( row0
, vg_ui
.modal
.message
, (box
[2]/vg_ui
.font
->spacing
)-2, 1,
1187 k_ui_align_left
, colour
);
1189 rect_copy( row1
, btn
);
1192 ui_rect_center( row1
, btn
);
1194 vg_ui
.focused_control_type
= k_ui_control_none
; /* HACK */
1195 if( ui_button_text( btn
, "OK", 1 ) != 1 )
1196 vg_ui
.focused_control_hit
= 1;
1197 vg_ui
.focused_control_type
= k_ui_control_modal
; /* HACK */
1198 vg_ui
.wants_mouse
= 1;
1201 ui_flush( k_ui_shader_colour
);
1203 if( !vg_ui
.focused_control_hit
){
1207 if( vg_ui
.wants_mouse
){
1208 SDL_SetWindowGrab( vg
.window
, SDL_FALSE
);
1209 SDL_SetRelativeMouseMode( SDL_FALSE
);
1212 SDL_SetWindowGrab( vg
.window
, SDL_TRUE
);
1213 SDL_SetRelativeMouseMode( SDL_TRUE
);
1216 SDL_SetCursor( vg_ui
.cursor_map
[ vg_ui
.cursor
] );
1222 * -----------------------------------------------------------------------------
1225 static int ui_checkbox( ui_rect inout_panel
, const char *str_label
, i32
*data
){
1226 ui_rect rect
, label
, box
;
1227 ui_standard_widget( inout_panel
, rect
, 1 );
1229 ui_split( rect
, k_ui_axis_v
, -rect
[3], 0, label
, box
);
1230 ui_text( label
, str_label
, k_ui_scale
, k_ui_align_middle_left
, 0 );
1232 int changed
= ui_colourbutton( box
, k_ui_bg
)==1;
1234 *data
= (*data
) ^ 0x1;
1237 ui_rect_pad( box
, (ui_px
[2]){4,4} );
1238 ui_fill( box
, ui_colour( k_ui_orange
) );
1246 * -----------------------------------------------------------------------------
1250 * unfortunately no return value since we only find out that event in the
1253 static void ui_enum( ui_rect inout_panel
, const char *str_label
,
1254 struct ui_enum_opt
*options
, u32 len
, i32
*value
){
1255 ui_rect rect
, label
, box
;
1256 ui_standard_widget( inout_panel
, rect
, 1 );
1257 ui_label( rect
, str_label
, k_ui_scale
, 0, box
);
1259 const char *display
= "OUT OF RANGE";
1261 for( u32 i
=0; i
<len
; i
++ ){
1262 if( *value
== options
[i
].value
){
1263 display
= options
[i
].alias
;
1269 if( ui_button_text( box
, display
, k_ui_scale
) == 1 ){
1270 vg_ui
.focused_control_type
= k_ui_control_enum
;
1271 vg_ui
.ptr_enum
= value
;
1272 vg_ui
._enum
.option_count
= len
;
1273 vg_ui
._enum
.options
= options
;
1274 rect_copy( box
, vg_ui
._enum
.rect
);
1278 ui_outline( box
, 1, ui_colour(k_ui_red
), 0 );
1281 static void ui_enum_post(void){
1283 rect_copy( vg_ui
._enum
.rect
, drawer
);
1284 drawer
[3] *= vg_ui
._enum
.option_count
;
1287 int clickany
= ui_click_up(UI_MOUSE_LEFT
|UI_MOUSE_RIGHT
|UI_MOUSE_MIDDLE
),
1288 hover
= ui_inside_rect( drawer
, vg_ui
.mouse
);
1290 if( clickany
&& !hover
){
1295 vg_ui
.focused_control_type
= k_ui_control_none
;
1296 i32
*value
= vg_ui
.ptr_enum
;
1298 for( u32 i
=0; i
<vg_ui
._enum
.option_count
; i
++ ){
1300 ui_split( drawer
, k_ui_axis_h
, vg_ui
._enum
.rect
[3], 0, button
,drawer
);
1302 enum ui_scheme_colour colour
= k_ui_bg
+3;
1303 if( vg_ui
._enum
.options
[i
].value
== *value
)
1304 colour
= k_ui_orange
;
1306 if( ui_colourbutton_text( button
, vg_ui
._enum
.options
[i
].alias
,
1307 k_ui_scale
, colour
) == 1 ){
1308 *value
= vg_ui
._enum
.options
[i
].value
;
1314 vg_ui
.focused_control_type
= k_ui_control_enum
;
1317 vg_ui
.focused_control_hit
= 1;
1322 * -----------------------------------------------------------------------------
1325 static void _ui_textbox_make_selection( int *start
, int *end
){
1326 *start
= VG_MIN( vg_ui
.textbox
.cursor_pos
, vg_ui
.textbox
.cursor_user
);
1327 *end
= VG_MAX( vg_ui
.textbox
.cursor_pos
, vg_ui
.textbox
.cursor_user
);
1330 static void _ui_textbox_move_cursor( int *cursor0
, int *cursor1
,
1331 int dir
, int snap_together
){
1332 *cursor0
= VG_MAX( 0, vg_ui
.textbox
.cursor_user
+ dir
);
1335 VG_MIN( vg_ui
.textbox
.len
-1, strlen( vg_ui
.textbuf
)),
1339 *cursor1
= *cursor0
;
1342 static int _ui_textbox_makeroom( int datastart
, int length
){
1343 int move_to
= VG_MIN( datastart
+length
, vg_ui
.textbox
.len
-1 );
1344 int move_amount
= strlen( vg_ui
.textbuf
)-datastart
;
1345 int move_end
= VG_MIN( move_to
+move_amount
, vg_ui
.textbox
.len
-1 );
1346 move_amount
= move_end
-move_to
;
1349 memmove( &vg_ui
.textbuf
[ move_to
],
1350 &vg_ui
.textbuf
[ datastart
],
1353 vg_ui
.textbuf
[ move_end
] = '\0';
1355 return VG_MIN( length
, vg_ui
.textbox
.len
-datastart
-1 );
1358 static int _ui_textbox_delete_char( int direction
){
1360 _ui_textbox_make_selection( &start
, &end
);
1362 /* There is no selection */
1364 if( direction
== 1 ) end
= VG_MIN( end
+1, strlen( vg_ui
.textbuf
) );
1365 else if( direction
== -1 ) start
= VG_MAX( start
-1, 0 );
1368 /* Still no selction, no need to do anything */
1372 /* Copy the end->terminator to start */
1373 int remaining_length
= strlen( vg_ui
.textbuf
)+1-end
;
1374 memmove( &vg_ui
.textbuf
[ start
],
1375 &vg_ui
.textbuf
[ end
],
1380 static void _ui_textbox_to_clipboard(void){
1382 _ui_textbox_make_selection( &start
, &end
);
1386 memcpy( buffer
, &vg_ui
.textbuf
[ start
], end
-start
);
1387 buffer
[ end
-start
] = 0x00;
1388 SDL_SetClipboardText( buffer
);
1392 static void ui_start_modal( const char *message
, u32 options
);
1393 static void _ui_textbox_clipboard_paste(void){
1394 if( !SDL_HasClipboardText() )
1397 char *text
= SDL_GetClipboardText();
1402 int datastart
= _ui_textbox_delete_char( 0 );
1403 int length
= strlen( text
);
1405 if( (vg_ui
.textbox
.len
- strlen(vg_ui
.textbuf
)) < length
){
1406 ui_start_modal( "Clipboard content exceeds buffer size.", UI_MODAL_BAD
);
1410 int cpylength
= _ui_textbox_makeroom( datastart
, length
);
1412 memcpy( vg_ui
.textbuf
+ datastart
, text
, cpylength
);
1413 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
,
1414 &vg_ui
.textbox
.cursor_pos
, cpylength
, 1 );
1418 static void _ui_textbox_put_char( char c
){
1419 vg_ui
.textbox
.cursor_user
= _ui_textbox_delete_char(0);
1420 if( (vg_ui
.textbox
.len
- strlen(vg_ui
.textbuf
)) <= 1 ) return;
1422 if( _ui_textbox_makeroom( vg_ui
.textbox
.cursor_user
, 1 ) )
1423 vg_ui
.textbuf
[ vg_ui
.textbox
.cursor_user
] = c
;
1425 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
,
1426 &vg_ui
.textbox
.cursor_pos
, 1, 1 );
1429 /* Receed secondary cursor */
1430 static void _ui_textbox_left_select(void){
1431 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
, NULL
, -1, 0 );
1434 /* Match and receed both cursors */
1435 static void _ui_textbox_left(void){
1436 int cursor_diff
= vg_ui
.textbox
.cursor_pos
- vg_ui
.textbox
.cursor_user
? 0: 1;
1438 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
,
1439 &vg_ui
.textbox
.cursor_pos
, -cursor_diff
, 1 );
1442 static void _ui_textbox_up(void){
1443 if( vg_ui
.textbox
.flags
& UI_TEXTBOX_MULTILINE
){
1444 int line_begin
= vg_ui
.textbox
.cursor_user
;
1446 while( line_begin
){
1447 if( vg_ui
.textbuf
[ line_begin
-1 ] == '\n' ){
1455 int line_above_begin
= line_begin
-1;
1457 while( line_above_begin
){
1458 if( vg_ui
.textbuf
[ line_above_begin
-1 ] == '\n' ){
1462 line_above_begin
--;
1465 int offset
= vg_ui
.textbox
.cursor_user
- line_begin
,
1466 line_length_above
= line_begin
- line_above_begin
-1;
1468 offset
= VG_MIN( line_length_above
, offset
);
1470 vg_ui
.textbox
.cursor_user
= line_above_begin
+offset
;
1471 vg_ui
.textbox
.cursor_pos
= line_above_begin
+offset
;
1474 vg_ui
.textbox
.cursor_user
= line_begin
;
1475 vg_ui
.textbox
.cursor_pos
= line_begin
;
1479 if( vg_ui
.textbox
.callbacks
.up
){
1480 vg_ui
.textbox
.callbacks
.up( vg_ui
.textbuf
, vg_ui
.textbox
.len
);
1485 static void _ui_textbox_down(void){
1486 if( vg_ui
.textbox
.flags
& UI_TEXTBOX_MULTILINE
){
1487 int line_begin
= vg_ui
.textbox
.cursor_user
;
1489 while( line_begin
){
1490 if( vg_ui
.textbuf
[ line_begin
-1 ] == '\n' ){
1497 int line_below_begin
= vg_ui
.textbox
.cursor_user
;
1500 if( vg_ui
.textbuf
[ line_below_begin
] == '\0' ){
1501 vg_ui
.textbox
.cursor_user
= line_below_begin
;
1502 vg_ui
.textbox
.cursor_pos
= line_below_begin
;
1506 if( vg_ui
.textbuf
[ line_below_begin
] == '\n' ){
1507 line_below_begin
++;
1511 line_below_begin
++;
1514 int line_below_end
= line_below_begin
;
1516 if( vg_ui
.textbuf
[ line_below_end
] == '\0' ||
1517 vg_ui
.textbuf
[ line_below_end
] == '\n' ){
1524 int offset
= vg_ui
.textbox
.cursor_user
- line_begin
,
1525 line_length_below
= line_below_end
- line_below_begin
-1;
1527 offset
= VG_MIN( line_length_below
, offset
);
1529 vg_ui
.textbox
.cursor_user
= line_below_begin
+offset
;
1530 vg_ui
.textbox
.cursor_pos
= line_below_begin
+offset
;
1533 if( vg_ui
.textbox
.callbacks
.down
){
1534 vg_ui
.textbox
.callbacks
.down( vg_ui
.textbuf
, vg_ui
.textbox
.len
);
1539 static void _ui_textbox_right_select(void){
1540 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
, NULL
, 1, 0 );
1543 static void _ui_textbox_right(void){
1544 int cursor_diff
= vg_ui
.textbox
.cursor_pos
- vg_ui
.textbox
.cursor_user
? 0: 1;
1546 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
,
1547 &vg_ui
.textbox
.cursor_pos
, +cursor_diff
, 1 );
1550 static void _ui_textbox_backspace(void){
1551 if( vg_ui
.focused_control_type
== k_ui_control_textbox
){
1552 vg_ui
.textbox
.cursor_user
= _ui_textbox_delete_char( -1 );
1553 vg_ui
.textbox
.cursor_pos
= vg_ui
.textbox
.cursor_user
;
1555 if( vg_ui
.textbox
.callbacks
.change
){
1556 vg_ui
.textbox
.callbacks
.change( vg_ui
.textbuf
, vg_ui
.textbox
.len
);
1561 static void _ui_textbox_delete(void){
1562 if( vg_ui
.focused_control_type
== k_ui_control_textbox
){
1563 vg_ui
.textbox
.cursor_user
= _ui_textbox_delete_char( 1 );
1564 vg_ui
.textbox
.cursor_pos
= vg_ui
.textbox
.cursor_user
;
1566 if( vg_ui
.textbox
.callbacks
.change
){
1567 vg_ui
.textbox
.callbacks
.change( vg_ui
.textbuf
, vg_ui
.textbox
.len
);
1572 static void _ui_textbox_home_select(void){
1573 i32 start
= vg_ui
.textbox
.cursor_user
;
1575 if( vg_ui
.textbox
.flags
& UI_TEXTBOX_MULTILINE
){
1577 if( vg_ui
.textbuf
[start
-1] == '\n' )
1586 vg_ui
.textbox
.cursor_user
= start
;
1589 static void _ui_textbox_home(void){
1590 _ui_textbox_home_select();
1591 vg_ui
.textbox
.cursor_pos
= vg_ui
.textbox
.cursor_user
;
1594 static void _ui_textbox_end_select(void){
1595 i32 end
= vg_ui
.textbox
.cursor_user
;
1597 if( vg_ui
.textbox
.flags
& UI_TEXTBOX_MULTILINE
){
1598 while( vg_ui
.textbuf
[end
] ){
1599 if( vg_ui
.textbuf
[end
] == '\n' )
1606 end
= VG_MIN( vg_ui
.textbox
.len
-1, strlen(vg_ui
.textbuf
) );
1608 vg_ui
.textbox
.cursor_user
= end
;
1611 static void _ui_textbox_end(void){
1612 _ui_textbox_end_select();
1613 vg_ui
.textbox
.cursor_pos
= vg_ui
.textbox
.cursor_user
;
1616 static void _ui_textbox_select_all(void){
1617 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
, NULL
, 10000, 0);
1618 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_pos
, NULL
, -10000, 0);
1621 static void _ui_textbox_cut(void){
1622 _ui_textbox_to_clipboard();
1623 vg_ui
.textbox
.cursor_user
= _ui_textbox_delete_char(0);
1624 vg_ui
.textbox
.cursor_pos
= vg_ui
.textbox
.cursor_user
;
1627 static void _ui_textbox_enter(void){
1628 if( vg_ui
.focused_control_type
== k_ui_control_textbox
){
1629 if( vg_ui
.textbox
.callbacks
.enter
)
1630 vg_ui
.textbox
.callbacks
.enter( vg_ui
.textbuf
, vg_ui
.textbox
.len
);
1632 if( vg_ui
.focused_control_type
!= k_ui_control_textbox
) return;
1634 if( vg_ui
.textbox
.flags
& UI_TEXTBOX_MULTILINE
)
1635 _ui_textbox_put_char( '\n' );
1637 if( !(vg_ui
.textbox
.flags
& UI_TEXTBOX_AUTOFOCUS
) )
1644 * based on a visual character coordinate relative to the anchor of the textbox,
1645 * this works out the linear place in the buffer that coordinate maps to
1647 * input coordinates go in co[0], co[1], and the result index is in co[2]
1649 static void _ui_textbox_calc_index_from_grid( int co
[3], int wrap_length
){
1653 while( (c
= vg_ui
.textbuf
[i
[2]]) ){
1654 if( i
[1]==co
[1] && i
[0]>=co
[0] ) break;
1656 if( i
[0] >= wrap_length
){
1661 if( c
>= 32 && c
<= 126 ){
1665 else if( c
== '\n' ){
1668 if( i
[1] > co
[1] ) break;
1682 * based on the index specied in co[2], work out the visual character
1683 * coordinates and store them in co[0], co[1]
1685 static void _ui_textbox_index_calc_coords( int co
[3], int wrap_length
){
1692 while( (c
= vg_ui
.textbuf
[i
++]) ){
1693 if( i
> co
[2] ) break;
1694 if( co
[0] >= wrap_length
){
1698 if( c
>= 32 && c
<= 126 ) co
[0] ++;
1699 else if( c
== '\n' ){
1707 * calculate the number of characters remaining until either:
1708 * - the wrap_length limit is hit
1709 * - end of the line/string
1711 * index must be fully populated with visual X/Y, and linear index
1713 static int _ui_textbox_run_remaining( int index
[3], int wrap_length
){
1714 int i
=0, printed_chars
=0;
1716 while( (c
= vg_ui
.textbuf
[index
[2] + (i
++)]) ){
1717 if( index
[0]+i
>= wrap_length
) break;
1718 if( c
>= 32 && c
<= 126 ) printed_chars
++;
1719 else if( c
== '\n' ) break;
1722 return printed_chars
+1;
1725 static int ui_textbox( ui_rect inout_panel
, const char *label
,
1726 char *buf
, u32 len
, u32 lines
, u32 flags
,
1727 struct ui_textbox_callbacks
*callbacks
){
1728 assert( lines
> 0 );
1731 if( lines
> 1 ) flags
|= UI_TEXTBOX_MULTILINE
;
1734 ui_standard_widget( inout_panel
, rect
, lines
);
1737 ui_label( rect
, label
, 1, 0, rect
);
1739 int clickup
= ui_click_up(UI_MOUSE_LEFT
),
1740 clickdown
= ui_click_down(UI_MOUSE_LEFT
),
1741 click
= ui_clicking(UI_MOUSE_LEFT
) | clickup
,
1742 target
= ui_inside_rect( rect
, vg_ui
.mouse_click
) && click
,
1743 hover
= ui_inside_rect( rect
, vg_ui
.mouse
);
1745 /* allow instant transitions from textbox->textbox */
1746 if( (vg_ui
.focused_control_type
!= k_ui_control_none
) &&
1747 (vg_ui
.focused_control_type
!= k_ui_control_textbox
) ){
1753 flags
&= ~UI_TEXTBOX_AUTOFOCUS
;
1756 u32 col_base
= ui_colour( k_ui_bg
),
1757 col_highlight
= ui_colour( k_ui_fg
),
1758 col_cursor
= (0x00ffffff & ui_colour(k_ui_fg
))|0x7f000000;
1763 rect_copy( rect
, text_rect
);
1765 if( flags
& UI_TEXTBOX_MULTILINE
) text_rect
[3] = rect
[3]-16;
1766 else text_rect
[3] = vg_ui
.font
->line_height
;
1769 ui_rect_center( rect
, text_rect
);
1771 ui_px wrap_length
= 1024;
1773 if( flags
& UI_TEXTBOX_WRAP
)
1774 wrap_length
= text_rect
[2] / vg_ui
.font
->spacing
;
1777 vg_ui
.cursor
= k_ui_cursor_ibeam
;
1780 if( vg_ui
.focused_control_id
== buf
){
1781 ui_fill( rect
, col_base
);
1782 ui_ntext( text_rect
, buf
, wrap_length
, 1, k_ui_align_left
, 0 );
1784 if( !(flags
& UI_TEXTBOX_AUTOFOCUS
) && ((clickup
||clickdown
) && !target
)){
1788 vg_ui
.focused_control_hit
= 1;
1789 if( click
&& target
){
1791 (vg_ui
.mouse_click
[0] - text_rect
[0]) / vg_ui
.font
->spacing
,
1792 (vg_ui
.mouse_click
[1] - text_rect
[1]) / vg_ui
.font
->line_height
,
1796 (vg_ui
.mouse
[0] - text_rect
[0]) / vg_ui
.font
->spacing
,
1797 (vg_ui
.mouse
[1] - text_rect
[1]) / vg_ui
.font
->line_height
,
1801 if( flags
& UI_TEXTBOX_MULTILINE
){
1802 _ui_textbox_calc_index_from_grid( p0
, wrap_length
);
1803 _ui_textbox_calc_index_from_grid( p1
, wrap_length
);
1805 vg_ui
.textbox
.cursor_pos
= p0
[2];
1806 vg_ui
.textbox
.cursor_user
= p1
[2];
1809 int max
= strlen( buf
);
1810 vg_ui
.textbox
.cursor_pos
= VG_MAX( 0, VG_MIN( max
, p0
[0] )),
1811 vg_ui
.textbox
.cursor_user
= VG_MAX( 0, VG_MIN( max
, p1
[0] ));
1815 ui_outline( rect
, -2, vg_ui
.scheme
[ k_ui_orange
], 0 );
1819 int c0
= vg_ui
.textbox
.cursor_pos
,
1820 c1
= vg_ui
.textbox
.cursor_user
,
1821 start
= VG_MIN( c0
, c1
),
1822 end
= VG_MAX( c0
, c1
),
1825 if( flags
& (UI_TEXTBOX_WRAP
|UI_TEXTBOX_MULTILINE
) ){
1826 int pos
[3], remaining
= chars
;
1829 _ui_textbox_index_calc_coords( pos
, wrap_length
);
1832 cursor
[0] = text_rect
[0] + pos
[0]*vg_ui
.font
->spacing
-1;
1833 cursor
[1] = text_rect
[1] + pos
[1]*14;
1836 ui_fill( cursor
, col_cursor
);
1837 rect_copy( cursor
, vg_ui
.click_fader_end
);
1841 int run
= _ui_textbox_run_remaining( pos
, wrap_length
);
1842 run
= VG_MIN( run
, remaining
);
1844 cursor
[0] = text_rect
[0] + pos
[0]*vg_ui
.font
->spacing
-1;
1845 cursor
[1] = text_rect
[1] + pos
[1]*14;
1846 cursor
[2] = (float)(run
)*(float)vg_ui
.font
->spacing
;
1849 ui_fill( cursor
, col_cursor
);
1856 rect_copy( cursor
, vg_ui
.click_fader_end
);
1860 cursor
[0] = text_rect
[0] + start
*vg_ui
.font
->spacing
-1;
1861 cursor
[1] = text_rect
[1];
1868 cursor
[2] = (float)(chars
)*(float)vg_ui
.font
->spacing
;
1871 if( (vg_ui
.click_fade_opacity
<=0.0f
) &&
1872 ui_clip( rect
, cursor
, cursor
) ){
1873 ui_fill( cursor
, col_cursor
);
1876 rect_copy( cursor
, vg_ui
.click_fader_end
);
1883 if( click
|| (flags
& UI_TEXTBOX_AUTOFOCUS
) ){
1884 if( (target
&& hover
) || (flags
& UI_TEXTBOX_AUTOFOCUS
) ){
1887 ui_fill( rect
, col_highlight
);
1888 vg_ui
.ignore_input_frames
= 2;
1889 rect_copy( rect
, vg_ui
.click_fader
);
1890 rect_copy( rect
, vg_ui
.click_fader_end
);
1892 vg_ui
.click_fade_opacity
= 1.0f
;
1893 vg_ui
.textbuf
= buf
;
1894 vg_ui
.focused_control_hit
= 1;
1895 vg_ui
.focused_control_type
= k_ui_control_textbox
;
1896 vg_ui
.textbox
.len
= len
;
1897 vg_ui
.textbox
.flags
= flags
;
1898 vg_ui
.textbox
.cursor_pos
= 0;
1899 vg_ui
.textbox
.cursor_user
= 0;
1902 vg_ui
.textbox
.callbacks
= *callbacks
;
1905 vg_ui
.textbox
.callbacks
.change
= NULL
;
1906 vg_ui
.textbox
.callbacks
.down
= NULL
;
1907 vg_ui
.textbox
.callbacks
.up
= NULL
;
1908 vg_ui
.textbox
.callbacks
.enter
= NULL
;
1911 SDL_StartTextInput();
1915 ui_fill( rect
, col_base
);
1918 ui_outline( rect
, -1, col_highlight
, 0 );
1921 ui_ntext( text_rect
, buf
, wrap_length
, 1, k_ui_align_left
, 0 );
1927 * -----------------------------------------------------------------------------
1930 static void ui_tabs( ui_rect inout_panel
, ui_rect out_content_panel
,
1931 const char **titles
, u32 count
, i32
*page
){
1933 ui_standard_widget( inout_panel
, bar
, 1 );
1935 i32 cur_page
= *page
;
1937 f32 width
= (f32
)inout_panel
[2] / (f32
)count
;
1939 ui_px h
= (inout_panel
[1] + inout_panel
[3]) - (bar
[1]+bar
[3]);
1940 inout_panel
[1] = bar
[1]+bar
[3];
1943 ui_fill( inout_panel
, ui_colour( k_ui_bg
+2 ) );
1944 ui_outline( inout_panel
, 1, ui_colour( k_ui_bg
+5 ), 0 );
1946 rect_copy( inout_panel
, out_content_panel
);
1947 ui_rect_pad( out_content_panel
, (ui_px
[2]){ k_ui_padding
, k_ui_padding
} );
1950 for( i32 i
=0; i
<count
; i
++ ){
1952 bar
[0] + ((f32
)i
*width
),
1958 enum ui_scheme_colour colour
= k_ui_bg
+4;
1959 if( i
== cur_page
){
1961 ui_outline( button
, 1, ui_colour( k_ui_bg
+5 ),
1962 UI_TOP
|UI_LEFT
|UI_RIGHT
);
1966 if( ui_colourbutton_text( button
, titles
[i
], 1, colour
) == 1 )
1973 * -----------------------------------------------------------------------------
1975 static void ui_start_modal( const char *message
, u32 options
){
1976 vg_ui
.focused_control_type
= k_ui_control_modal
;
1977 vg_ui
.modal
.message
= message
;
1978 vg_ui
.modal
.callbacks
.close
= NULL
;
1979 vg_ui
.modal
.options
= options
;
1981 u32 type
= options
& UI_MODAL_TYPE_BITS
;
1982 if( type
== UI_MODAL_OK
) vg_info( message
);
1983 else if( type
== UI_MODAL_WARN
) vg_warn( message
);
1984 else if( type
== UI_MODAL_GOOD
) vg_success( message
);
1985 else if( type
== UI_MODAL_BAD
) vg_error( message
);
1990 * -----------------------------------------------------------------------------
1996 static void _ui_proc_key( SDL_Keysym ev
){
1997 if( vg_ui
.focused_control_type
!= k_ui_control_textbox
){
2001 struct textbox_mapping
{
2005 void (*handler
)(void);
2009 { 0, SDLK_LEFT
, _ui_textbox_left
},
2010 { KMOD_SHIFT
, SDLK_LEFT
, _ui_textbox_left_select
},
2011 { 0, SDLK_RIGHT
, _ui_textbox_right
},
2012 { KMOD_SHIFT
, SDLK_RIGHT
, _ui_textbox_right_select
},
2013 { 0, SDLK_DOWN
, _ui_textbox_down
},
2014 { 0, SDLK_UP
, _ui_textbox_up
},
2015 { 0, SDLK_BACKSPACE
, _ui_textbox_backspace
},
2016 { KMOD_SHIFT
, SDLK_BACKSPACE
, _ui_textbox_backspace
},
2017 { KMOD_CTRL
, SDLK_BACKSPACE
, _ui_textbox_backspace
},
2018 { 0, SDLK_DELETE
, _ui_textbox_delete
},
2019 { 0, SDLK_HOME
, _ui_textbox_home
},
2020 { KMOD_SHIFT
, SDLK_HOME
, _ui_textbox_home_select
},
2021 { 0, SDLK_END
, _ui_textbox_end
},
2022 { KMOD_SHIFT
, SDLK_END
, _ui_textbox_end_select
},
2023 { KMOD_CTRL
, SDLK_a
, _ui_textbox_select_all
},
2024 { KMOD_CTRL
, SDLK_c
, _ui_textbox_to_clipboard
},
2025 { KMOD_CTRL
, SDLK_x
, _ui_textbox_cut
},
2026 { KMOD_CTRL
, SDLK_v
, _ui_textbox_clipboard_paste
},
2027 { 0, SDLK_RETURN
, _ui_textbox_enter
},
2028 { 0, SDLK_ESCAPE
, ui_defocus_all
},
2033 if( ev
.mod
& KMOD_SHIFT
)
2036 if( ev
.mod
& KMOD_CTRL
)
2039 if( ev
.mod
& KMOD_ALT
)
2042 for( int i
=0; i
<vg_list_size( mappings
); i
++ ){
2043 struct textbox_mapping
*mapping
= &mappings
[i
];
2045 if( mapping
->key
== ev
.sym
){
2046 if( mapping
->mod
== 0 ){
2052 else if( (mod
& mapping
->mod
) == mapping
->mod
){
2061 * Callback for text entry mode
2063 static void ui_proc_utf8( const char *text
){
2064 if( vg_ui
.focused_control_type
== k_ui_control_textbox
){
2065 const char *ptr
= text
;
2068 if( *ptr
!= '`' ) _ui_textbox_put_char( *ptr
);
2072 if( vg_ui
.textbox
.callbacks
.change
){
2073 vg_ui
.textbox
.callbacks
.change( vg_ui
.textbuf
, vg_ui
.textbox
.len
);
2080 * -----------------------------------------------------------------------------
2083 static void ui_dev_colourview(void){
2084 ui_rect window
= {vg
.window_x
-256,0,256,vg
.window_y
}, swatch
;
2086 const char *names
[vg_list_size(vg_ui
.scheme
)] = {
2087 [k_ui_bg
] = "k_ui_bg", "k_ui_bg+1", "k_ui_bg+2", "k_ui_bg+3",
2088 "k_ui_bg+4", "k_ui_bg+5", "k_ui_bg+6", "k_ui_bg+7",
2090 [k_ui_fg
] = "k_ui_fg", "k_ui_fg+1", "k_ui_fg+2", "k_ui_fg+3",
2091 "k_ui_fg+4", "k_ui_fg+5", "k_ui_fg+6", "k_ui_fg+7",
2093 [k_ui_red
] = "k_ui_red", "k_ui_orange", "k_ui_yellow", "k_ui_green",
2094 "k_ui_aqua", "k_ui_blue", "k_ui_purple", "k_ui_gray",
2095 "k_ui_red+8","k_ui_orange+8","k_ui_yellow+8","k_ui_green+8",
2096 "k_ui_aqua+8","k_ui_blue+8","k_ui_purple+8","k_ui_gray+8" };
2099 ui_split_ratio( window
, k_ui_axis_v
, 0.5f
, 0, col
[0], col
[1] );
2101 for( int i
=0; i
<vg_list_size(vg_ui
.scheme
); i
++ ){
2102 int which
= (i
/8)%2;
2104 ui_split( col
[which
], k_ui_axis_h
, 24, 0, swatch
, col
[which
] );
2105 ui_fill( swatch
, ui_colour(i
) );
2108 ui_text(swatch
, names
[i
], 1, k_ui_align_middle_left
, ui_colourcont(i
));
2112 #endif /* VG_IMGUI_H */