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
;
205 ui_px mouse
[2], mouse_click
[2];
207 u32 ignore_input_frames
;
210 ui_rect click_fader
, click_fader_end
;
211 float click_fade_opacity
;
224 SDL_Cursor
*cursor_map
[ k_ui_cursor_max
];
230 [ k_ui_bg
+0 ] = UI_RGB( 0x1d2021 ),
231 [ k_ui_bg
+1 ] = UI_RGB( 0x282828 ),
232 [ k_ui_bg
+2 ] = UI_RGB( 0x3c3836 ),
233 [ k_ui_bg
+3 ] = UI_RGB( 0x504945 ),
234 [ k_ui_bg
+4 ] = UI_RGB( 0x665c54 ),
235 [ k_ui_bg
+5 ] = UI_RGB( 0x7c6f64 ),
236 [ k_ui_bg
+6 ] = UI_RGB( 0x928374 ),
237 [ k_ui_bg
+7 ] = UI_RGB( 0xa89984 ),
239 [ k_ui_fg
+0 ] = UI_RGB( 0xebdbb2 ),
240 [ k_ui_fg
+1 ] = UI_RGB( 0xfbf1c7 ),
241 [ k_ui_fg
+2 ] = UI_RGB( 0xd5c4a1 ),
242 [ k_ui_fg
+3 ] = UI_RGB( 0xbdae93 ),
243 [ k_ui_fg
+4 ] = UI_RGB( 0xa89984 ),
244 [ k_ui_fg
+5 ] = UI_RGB( 0x000000 ),
245 [ k_ui_fg
+6 ] = UI_RGB( 0x000000 ),
246 [ k_ui_fg
+7 ] = UI_RGB( 0x000000 ),
248 [ k_ui_red
] = UI_RGB( 0xcc241d ),
249 [ k_ui_orange
] = UI_RGB( 0xd65d0e ),
250 [ k_ui_yellow
] = UI_RGB( 0xd79921 ),
251 [ k_ui_green
] = UI_RGB( 0x98971a ),
252 [ k_ui_aqua
] = UI_RGB( 0x689d6a ),
253 [ k_ui_blue
] = UI_RGB( 0x458588 ),
254 [ k_ui_purple
] = UI_RGB( 0xb16286 ),
255 [ k_ui_gray
] = UI_RGB( 0x928374 ),
256 [ k_ui_red
+ k_ui_brighter
] = UI_RGB( 0xfb4934 ),
257 [ k_ui_orange
+ k_ui_brighter
] = UI_RGB( 0xfe8019 ),
258 [ k_ui_yellow
+ k_ui_brighter
] = UI_RGB( 0xfabd2f ),
259 [ k_ui_green
+ k_ui_brighter
] = UI_RGB( 0xb8bb26 ),
260 [ k_ui_aqua
+ k_ui_brighter
] = UI_RGB( 0x8ec07c ),
261 [ k_ui_blue
+ k_ui_brighter
] = UI_RGB( 0x83a598 ),
262 [ k_ui_purple
+ k_ui_brighter
] = UI_RGB( 0xd3869b ),
263 [ k_ui_gray
+ k_ui_brighter
] = UI_RGB( 0xa89984 ),
265 .font
= &vg_ui_font_small
,
266 .colour
= {1.0f
,1.0f
,1.0f
,1.0f
}
269 static struct vg_shader _shader_ui
=
277 "layout (location=0) in vec2 a_co;"
278 "layout (location=1) in vec2 a_uv;"
279 "layout (location=2) in vec4 a_colour;"
282 "out vec2 aTexCoords;"
288 "gl_Position = vec4( uPv * vec3( a_co, 1.0 ), 1.0 );"
289 "aTexCoords = a_uv * 0.00390625;" /* TODO: Uniform */
290 "aColour = a_colour;"
299 "uniform sampler2D uTexGlyphs;"
300 "uniform vec4 uColour;"
301 "out vec4 FragColor;"
303 "in vec2 aTexCoords;"
311 "vec4 diffuse = aColour;"
313 "if( diffuse.a == 0.0 )"
315 "diffuse.a = texture( uTexGlyphs, aTexCoords ).r;"
318 "FragColor = diffuse * uColour;"
323 static struct vg_shader _shader_ui_image
= {
324 .name
= "[vg] ui_image",
330 "layout (location=0) in vec2 a_co;"
331 "layout (location=1) in vec2 a_uv;"
332 "layout (location=2) in vec4 a_colour;"
335 "out vec2 aTexCoords;"
341 "gl_Position = vec4( uPv * vec3( a_co, 1.0 ), 1.0 );"
342 "aTexCoords = a_uv * 0.00390625;"
343 "aColour = a_colour;"
352 "uniform sampler2D uTexImage;"
353 "uniform vec4 uColour;"
354 "out vec4 FragColor;"
356 "in vec2 aTexCoords;"
362 "vec4 colour = texture( uTexImage, aTexCoords );"
363 "FragColor = colour * uColour;"
368 static void _vg_ui_init(void){
369 if( !vg_shader_compile( &_shader_ui
) ||
370 !vg_shader_compile( &_shader_ui_image
) )
371 vg_fatal_error( "Failed to compile ui shader" );
375 * ----------------------------------------
378 vg_ui
.max_indices
= 20000;
379 vg_ui
.max_verts
= 30000;
381 /* Generate the buffer we are gonna be drawing to */
382 glGenVertexArrays( 1, &vg_ui
.vao
);
383 glGenBuffers( 1, &vg_ui
.vbo
);
384 glGenBuffers( 1, &vg_ui
.ebo
);
386 glBindVertexArray( vg_ui
.vao
);
387 glBindBuffer( GL_ARRAY_BUFFER
, vg_ui
.vbo
);
389 glBufferData( GL_ARRAY_BUFFER
,
390 vg_ui
.max_verts
* sizeof( struct ui_vert
),
391 NULL
, GL_DYNAMIC_DRAW
);
392 glBindVertexArray( vg_ui
.vao
);
394 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, vg_ui
.ebo
);
395 glBufferData( GL_ELEMENT_ARRAY_BUFFER
,
396 vg_ui
.max_indices
* sizeof( u16
), NULL
, GL_DYNAMIC_DRAW
);
401 u32
const stride
= sizeof( struct ui_vert
);
404 glVertexAttribPointer( 0, 2, GL_SHORT
, GL_FALSE
, stride
,
405 (void *)offsetof( struct ui_vert
, co
) );
406 glEnableVertexAttribArray( 0 );
409 glVertexAttribPointer( 1, 2, GL_UNSIGNED_SHORT
, GL_FALSE
, stride
,
410 (void *)offsetof( struct ui_vert
, uv
) );
411 glEnableVertexAttribArray( 1 );
414 glVertexAttribPointer( 2, 4, GL_UNSIGNED_BYTE
, GL_TRUE
, stride
,
415 (void *)offsetof( struct ui_vert
, colour
) );
416 glEnableVertexAttribArray( 2 );
420 /* Alloc RAM default context */
421 u32 vert_size
= vg_ui
.max_verts
*sizeof(struct ui_vert
),
422 inds_size
= vg_align8( vg_ui
.max_indices
*sizeof(u16
) );
424 vg_ui
.vertex_buffer
= vg_linear_alloc( vg_mem
.rtmemory
, vert_size
);
425 vg_ui
.indice_buffer
= vg_linear_alloc( vg_mem
.rtmemory
, inds_size
);
428 * -----------------------------------------------------
431 /* Load default font */
433 #include "vg/vg_pxfont_thin.h"
436 u32 pixels
= 0, total
= 256*256, data
= 0;
439 while( pixels
< total
){
440 for( int b
= 31; b
>= 0; b
-- ){
441 image
[ pixels
++ ] = (compressed
[data
] & (0x1u
<< b
))? 0xffu
: 0x00u
;
443 if( pixels
>= total
){
451 glGenTextures( 1, &vg_ui
.tex_glyphs
);
452 glBindTexture( GL_TEXTURE_2D
, vg_ui
.tex_glyphs
);
453 glTexImage2D( GL_TEXTURE_2D
, 0, GL_R8
, 256, 256, 0,
454 GL_RED
, GL_UNSIGNED_BYTE
, image
);
457 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
458 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
459 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
460 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
464 * ---------------------------------------------------------------
467 vg_ui
.cursor_map
[ k_ui_cursor_default
] =
468 SDL_CreateSystemCursor( SDL_SYSTEM_CURSOR_ARROW
);
469 vg_ui
.cursor_map
[ k_ui_cursor_hand
] =
470 SDL_CreateSystemCursor( SDL_SYSTEM_CURSOR_HAND
);
471 vg_ui
.cursor_map
[ k_ui_cursor_ibeam
] =
472 SDL_CreateSystemCursor( SDL_SYSTEM_CURSOR_IBEAM
);
480 static void rect_copy( ui_rect a
, ui_rect b
){
481 for( int i
=0; i
<4; i
++ )
485 static void ui_flush( enum ui_shader shader
){
486 u32 vertex_offset
= vg_ui
.vert_start
*sizeof(ui_vert
),
487 vertex_count
= vg_ui
.cur_vert
-vg_ui
.vert_start
,
488 vertex_size
= vertex_count
*sizeof(ui_vert
),
490 indice_offset
= vg_ui
.indice_start
*sizeof(u16
),
491 indice_count
= vg_ui
.cur_indice
-vg_ui
.indice_start
,
492 indice_size
= indice_count
* sizeof(u16
);
494 if( !vertex_size
|| !indice_size
)
497 glBindVertexArray( vg_ui
.vao
);
498 glBindBuffer( GL_ARRAY_BUFFER
, vg_ui
.vbo
);
499 glBufferSubData( GL_ARRAY_BUFFER
, vertex_offset
, vertex_size
,
500 vg_ui
.vertex_buffer
+vg_ui
.vert_start
);
502 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, vg_ui
.ebo
);
503 glBufferSubData( GL_ELEMENT_ARRAY_BUFFER
, indice_offset
, indice_size
,
504 vg_ui
.indice_buffer
+vg_ui
.indice_start
);
506 glDisable( GL_DEPTH_TEST
);
507 glEnable( GL_BLEND
);
508 glBlendFunc( GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
509 glBlendEquation( GL_FUNC_ADD
);
510 glDisable( GL_CULL_FACE
);
512 m3x3f view
= M3X3_IDENTITY
;
513 m3x3_translate( view
, (v3f
){ -1.0f
, 1.0f
, 0.0f
} );
514 m3x3_scale( view
, (v3f
){ 1.0f
/((float)vg
.window_x
*0.5f
),
515 -1.0f
/((float)vg
.window_y
*0.5f
), 1.0f
} );
517 if( shader
== k_ui_shader_colour
){
518 glUseProgram( _shader_ui
.id
);
520 glUniformMatrix3fv( glGetUniformLocation( _shader_ui
.id
, "uPv" ), 1,
521 GL_FALSE
, (float *)view
);
522 glUniform4fv( glGetUniformLocation( _shader_ui
.id
, "uColour" ), 1,
525 glActiveTexture( GL_TEXTURE0
);
526 glBindTexture( GL_TEXTURE_2D
, vg_ui
.tex_glyphs
);
527 glUniform1i( glGetUniformLocation( _shader_ui
.id
, "uTexGlyphs" ), 0 );
529 else if( shader
== k_ui_shader_image
){
530 glUseProgram( _shader_ui_image
.id
);
531 glUniformMatrix3fv( glGetUniformLocation( _shader_ui_image
.id
, "uPv" ), 1,
532 GL_FALSE
, (float *)view
);
533 glUniform1i( glGetUniformLocation(_shader_ui_image
.id
,"uTexImage"), 0 );
534 glUniform4fv( glGetUniformLocation( _shader_ui_image
.id
, "uColour" ), 1,
538 vg_fatal_error( "Invalid UI shader (%d)\n", shader
);
540 glDrawElements( GL_TRIANGLES
, indice_count
, GL_UNSIGNED_SHORT
,
541 (void *)(vg_ui
.indice_start
*sizeof(u16
)) );
543 glDisable( GL_BLEND
);
545 vg_ui
.indice_start
= vg_ui
.cur_indice
;
546 vg_ui
.vert_start
= vg_ui
.cur_vert
;
549 static void ui_fill_rect( ui_rect rect
, u32 colour
, ui_px uv
[4] ){
550 /* this if far from ideal but stops us from crashing */
551 if( (vg_ui
.cur_vert
+ 4 > vg_ui
.max_verts
) ||
552 (vg_ui
.cur_indice
+ 6 > vg_ui
.max_indices
))
555 struct ui_vert
*vertices
= &vg_ui
.vertex_buffer
[ vg_ui
.cur_vert
];
556 u16
*indices
= &vg_ui
.indice_buffer
[ vg_ui
.cur_indice
];
558 for( int i
=0; i
<4; i
++ ){
559 vertices
[i
].colour
= colour
;
562 vertices
[0].co
[0] = rect
[0];
563 vertices
[0].co
[1] = rect
[1];
564 vertices
[0].uv
[0] = uv
[0];
565 vertices
[0].uv
[1] = uv
[1];
566 vertices
[1].co
[0] = rect
[0]+rect
[2];
567 vertices
[1].co
[1] = rect
[1];
568 vertices
[1].uv
[0] = uv
[2];
569 vertices
[1].uv
[1] = uv
[1];
570 vertices
[2].co
[0] = rect
[0]+rect
[2];
571 vertices
[2].co
[1] = rect
[1]+rect
[3];
572 vertices
[2].uv
[0] = uv
[2];
573 vertices
[2].uv
[1] = uv
[3];
574 vertices
[3].co
[0] = rect
[0];
575 vertices
[3].co
[1] = rect
[1]+rect
[3];
576 vertices
[3].uv
[0] = uv
[0];
577 vertices
[3].uv
[1] = uv
[3];
578 u16 ind_start
= vg_ui
.cur_vert
;
580 u16 start
= vg_ui
.cur_vert
;
581 u32 mesh
[] = { 0,2,1, 0,3,2 };
583 for( u32 i
=0; i
<vg_list_size(mesh
); i
++ ){
584 indices
[i
] = start
+mesh
[i
];
587 vg_ui
.cur_indice
+= 6;
591 static void ui_fill( ui_rect rect
, u32 colour
){
592 ui_fill_rect( rect
, colour
, (ui_px
[4]){ 4,4,4,4 } );
595 static void ui_outline( ui_rect rect
, ui_px thickness
, u32 colour
, u32 mask
){
596 /* this if far from ideal but stops us from crashing */
597 if( (vg_ui
.cur_vert
+ 8 > vg_ui
.max_verts
) ||
598 (vg_ui
.cur_indice
+ 24 > vg_ui
.max_indices
))
601 struct ui_vert
*vertices
= &vg_ui
.vertex_buffer
[ vg_ui
.cur_vert
];
602 u16
*indices
= &vg_ui
.indice_buffer
[ vg_ui
.cur_indice
];
604 for( int i
=0; i
<8; i
++ ){
605 vertices
[i
].uv
[0] = 4;
606 vertices
[i
].uv
[1] = 4;
607 vertices
[i
].colour
= colour
;
610 vertices
[0].co
[0] = rect
[0];
611 vertices
[0].co
[1] = rect
[1];
612 vertices
[1].co
[0] = rect
[0]+rect
[2];
613 vertices
[1].co
[1] = rect
[1];
614 vertices
[2].co
[0] = rect
[0]+rect
[2];
615 vertices
[2].co
[1] = rect
[1]+rect
[3];
616 vertices
[3].co
[0] = rect
[0];
617 vertices
[3].co
[1] = rect
[1]+rect
[3];
618 vertices
[4].co
[0] = vertices
[0].co
[0]-thickness
;
619 vertices
[4].co
[1] = vertices
[0].co
[1]-thickness
;
620 vertices
[5].co
[0] = vertices
[1].co
[0]+thickness
;
621 vertices
[5].co
[1] = vertices
[1].co
[1]-thickness
;
622 vertices
[6].co
[0] = vertices
[2].co
[0]+thickness
;
623 vertices
[6].co
[1] = vertices
[2].co
[1]+thickness
;
624 vertices
[7].co
[0] = vertices
[3].co
[0]-thickness
;
625 vertices
[7].co
[1] = vertices
[3].co
[1]+thickness
;
627 u16 start
= vg_ui
.cur_vert
;
628 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 };
631 mask
= UI_TOP
|UI_LEFT
|UI_BOTTOM
|UI_RIGHT
;
634 for( u32 i
=0; i
<vg_list_size(mesh
)/6; i
++ ){
635 if( (0x1<<i
) & mask
){
636 for( u32 j
=0; j
<6; j
++ )
637 indices
[c
++] = start
+mesh
[i
*6+j
];
641 vg_ui
.cur_indice
+= c
;
645 static void ui_split( ui_rect rect
,
646 enum ui_axis other
, ui_px width
, ui_px gap
,
647 ui_rect l
, ui_rect r
){
648 enum ui_axis dir
= other
^ 0x1;
650 if( width
< 0 ) width
= rect
[ 2+dir
] + width
;
653 rect_copy( rect
, temp
);
655 l
[ dir
] = temp
[ dir
];
656 r
[ dir
] = temp
[ dir
] + width
+ (gap
/2);
657 l
[ other
] = temp
[ other
];
658 r
[ other
] = temp
[ other
];
659 l
[ 2+dir
] = width
- (gap
/2);
660 r
[ 2+dir
] = temp
[ 2+dir
] - width
- (gap
/2);
661 l
[ 2+other
] = temp
[ 2+other
];
662 r
[ 2+other
] = temp
[ 2+other
];
665 static ui_px
ui_text_line_width( const char *str
);
667 static void ui_rect_center( ui_rect parent
, ui_rect rect
){
668 rect
[0] = parent
[0] + (parent
[2]-rect
[2])/2;
669 rect
[1] = parent
[1] + (parent
[3]-rect
[3])/2;
672 static void ui_fit_item( ui_rect rect
, ui_px size
[2], ui_rect d
){
673 i32 rp
= (i32
)rect
[2] * (i32
)size
[1],
674 rc
= (i32
)size
[0] * (i32
)rect
[3];
676 enum ui_axis dir
, other
;
677 if( rc
> rp
) dir
= k_ui_axis_h
;
678 else dir
= k_ui_axis_v
;
681 d
[2+dir
] = rect
[2+dir
];
682 d
[2+other
] = (rect
[2+dir
] * size
[other
]) / size
[dir
];
684 ui_rect_center( rect
, d
);
687 static void ui_split_ratio( ui_rect rect
, enum ui_axis dir
, float ratio
,
688 ui_px gap
, ui_rect l
, ui_rect r
){
689 ui_px width
= (float)rect
[ 2+(dir
^0x1) ] * ratio
;
690 ui_split( rect
, dir
, width
, gap
, l
, r
);
693 static void ui_rect_pad( ui_rect rect
, ui_px pad
[2] ){
700 static ui_px
ui_text_line_width( const char *str
){
702 const char *_c
= str
;
705 while( (c
= *(_c
++)) ){
706 if( c
>= 32 ) length
++;
707 else if( c
== '\n' ) break;
710 return length
* vg_ui
.font
->spacing
;
713 static ui_px
ui_text_string_height( const char *str
){
715 const char *_c
= str
;
718 while( (c
= *(_c
++)) ){
719 if( c
== '\n' ) height
++;
725 static ui_px
ui_text_aligned_x( const char *str
, ui_rect rect
, ui_px scale
,
726 enum ui_align align
){
727 enum ui_align lwr
= k_ui_align_lwr
& align
;
728 if( lwr
== k_ui_align_left
){
732 ui_px width
= ui_text_line_width( str
) * scale
;
734 if( lwr
== k_ui_align_right
)
735 return rect
[0] + rect
[2]-width
;
737 return rect
[0] + (rect
[2]-width
)/2;
741 static ui_px
ui_min( ui_px a
, ui_px b
){ return a
<b
?a
:b
; }
742 static ui_px
ui_max( ui_px a
, ui_px b
){ return a
>b
?a
:b
; }
743 static ui_px
ui_clamp( ui_px a
, ui_px min
, ui_px max
){
744 return ui_min( max
, ui_max( a
, min
) );
747 static int ui_clip( ui_rect parent
, ui_rect child
, ui_rect clipped
){
748 ui_px parent_max
[2], child_max
[2];
749 parent_max
[0] = parent
[0]+parent
[2];
750 parent_max
[1] = parent
[1]+parent
[3];
751 child_max
[0] = child
[0]+child
[2];
752 child_max
[1] = child
[1]+child
[3];
754 clipped
[0] = ui_clamp( child
[0], parent
[0], parent_max
[0] );
755 clipped
[1] = ui_clamp( child
[1], parent
[1], parent_max
[1] );
756 clipped
[2] = ui_clamp( child_max
[0], parent
[0], parent_max
[0] );
757 clipped
[3] = ui_clamp( child_max
[1], parent
[1], parent_max
[1] );
759 if( clipped
[0] == clipped
[2] ||
760 clipped
[1] == clipped
[3] )
763 clipped
[2] -= clipped
[0];
764 clipped
[3] -= clipped
[1];
769 static int ui_inside_rect( ui_rect rect
, ui_px co
[2] ){
770 if( co
[0] >= rect
[0] &&
772 co
[0] < rect
[0]+rect
[2] &&
773 co
[1] < rect
[1]+rect
[3] ){
780 static int ui_click_down( u32 mask
){
781 if( vg_ui
.ignore_input_frames
) return 0;
782 if( (vg_ui
.mouse_state
[0] & mask
) &&
783 !(vg_ui
.mouse_state
[1] & mask
) )
789 static int ui_clicking( u32 mask
){
790 if( vg_ui
.ignore_input_frames
) return 0;
791 return vg_ui
.mouse_state
[0] & mask
;
794 static int ui_click_up( u32 mask
){
795 if( vg_ui
.ignore_input_frames
) return 0;
796 if( (vg_ui
.mouse_state
[1] & mask
) &&
797 !(vg_ui
.mouse_state
[0] & mask
) )
803 static void ui_prerender(void){
805 vg_ui
.mouse_state
[1] = vg_ui
.mouse_state
[0];
806 vg_ui
.mouse_state
[0] = SDL_GetMouseState( &x
, &y
);
811 vg_ui
.cur_indice
= 0;
812 vg_ui
.vert_start
= 0;
813 vg_ui
.indice_start
= 0;
814 vg_ui
.focused_control_hit
= 0;
815 vg_ui
.cursor
= k_ui_cursor_default
;
816 vg_ui
.wants_mouse
= 0;
818 if( vg_ui
.ignore_input_frames
){
819 vg_ui
.ignore_input_frames
--;
823 if( ui_click_down(UI_MOUSE_LEFT
)||ui_click_down(UI_MOUSE_MIDDLE
) ){
824 vg_ui
.mouse_click
[0] = vg_ui
.mouse
[0];
825 vg_ui
.mouse_click
[1] = vg_ui
.mouse
[1];
829 static u32
ui_colour( enum ui_scheme_colour id
){
830 return vg_ui
.scheme
[ id
];
833 /* get an appropriately contrasting colour given the base */
834 static u32
ui_colourcont( enum ui_scheme_colour id
){
835 if ( id
< k_ui_bg
+6 ) return ui_colour( k_ui_fg
);
836 else if( id
< k_ui_fg
) return ui_colour( k_ui_bg
+1 );
837 else if( id
< k_ui_hue
) return ui_colour( k_ui_bg
+3 );
838 else if( id
< k_ui_red
+k_ui_brighter
) return ui_colour( k_ui_fg
);
839 else return ui_colour( k_ui_fg
+1 );
842 static void ui_hex_to_norm( u32 hex
, v4f norm
){
843 norm
[0] = ((hex
) & 0xff);
844 norm
[1] = ((hex
>>8 ) & 0xff);
845 norm
[2] = ((hex
>>16) & 0xff);
846 norm
[3] = ((hex
>>24) & 0xff);
847 v4_muls( norm
, 1.0f
/255.0f
, norm
);
850 static void ui_text_glyph( const struct ui_font
*font
, ui_px scale
,
851 u8 glyph
, ui_rect out_texcoords
){
852 glyph
-= font
->ascii_start
;
854 ui_px per_row
= font
->sheet_size
/ font
->glyph_width
,
855 column
= (ui_px
)glyph
% per_row
,
856 row
= (glyph
- column
) / per_row
;
858 out_texcoords
[0] = column
* font
->glyph_width
;
859 out_texcoords
[1] = row
* font
->glyph_height
+ font
->offset_y
;
860 out_texcoords
[2] = out_texcoords
[0] + font
->glyph_width
;
861 out_texcoords
[3] = out_texcoords
[1] + font
->glyph_height
;
864 static u32
ui_opacity( u32 colour
, f32 opacity
){
865 u32 alpha
= opacity
* 255.0f
;
866 return (colour
& 0x00ffffff) | (alpha
<< 24);
869 static u32
ui_ntext( ui_rect rect
, const char *str
, u32 len
, ui_px scale
,
870 enum ui_align align
, u32 colour
){
872 if( colour
== 0 ) colour
= ui_colour( k_ui_fg
);
874 colour
&= 0x00ffffff;
876 const char *_c
= str
;
879 text_cursor
[0] = ui_text_aligned_x( str
, rect
, scale
, align
);
880 text_cursor
[1] = rect
[1];
881 text_cursor
[2] = vg_ui
.font
->glyph_width
*scale
;
882 text_cursor
[3] = vg_ui
.font
->glyph_height
*scale
;
884 u32 printed_chars
= 0;
886 if( align
& (k_ui_align_middle
|k_ui_align_bottom
) ){
887 ui_px height
= ui_text_string_height( str
) * scale
;
889 if( align
& k_ui_align_bottom
)
890 text_cursor
[1] += rect
[3]-height
;
892 text_cursor
[1] += (rect
[3]-height
)/2;
895 while( (c
= *(_c
++)) ){
896 if( printed_chars
>= len
){
898 text_cursor
[1] += vg_ui
.font
->line_height
*scale
;
899 text_cursor
[0] = ui_text_aligned_x( _c
, rect
, scale
, align
);
900 text_cursor
[0] -= vg_ui
.font
->spacing
*scale
;
903 ui_text_glyph( vg_ui
.font
, scale
, '\xb6' /*FIXME*/, glyph
);
904 ui_fill_rect( text_cursor
, 0x00ffffff, glyph
);
905 text_cursor
[0] += vg_ui
.font
->spacing
*scale
;
909 text_cursor
[1] += vg_ui
.font
->line_height
*scale
;
910 text_cursor
[0] = ui_text_aligned_x( _c
, rect
, scale
, align
);
916 ui_text_glyph( vg_ui
.font
, scale
, c
, glyph
);
918 ui_rect cursor_clipped
;
919 if( ui_clip( rect
, text_cursor
, cursor_clipped
) ){
920 ui_fill_rect( cursor_clipped
, colour
, glyph
);
923 else if( c
== '\x1B' ){
927 for( int i
=0; i
<3; i
++ ){
933 case '0': colour
= ui_colour( k_ui_fg
); break;
934 case '3'|'0'<<8: colour
= ui_colour( k_ui_bg
); break;
935 case '3'|'1'<<8: colour
= ui_colour( k_ui_red
); break;
936 case '3'|'2'<<8: colour
= ui_colour( k_ui_green
); break;
937 case '3'|'3'<<8: colour
= ui_colour( k_ui_yellow
); break;
938 case '3'|'4'<<8: colour
= ui_colour( k_ui_blue
); break;
939 case '3'|'5'<<8: colour
= ui_colour( k_ui_purple
); break;
940 case '3'|'6'<<8: colour
= ui_colour( k_ui_aqua
); break;
941 case '3'|'7'<<8: colour
= 0xffffffff; break;
944 colour
&= 0x00ffffff;
948 colour_id
|= _c
[i
] << (i
*8);
958 else if( c
== '\t' ){
959 text_cursor
[0] += vg_ui
.font
->spacing
*scale
*4;
964 text_cursor
[0] += vg_ui
.font
->spacing
*scale
;
968 return printed_chars
;
971 static void ui_text( ui_rect rect
, const char *str
, ui_px scale
,
972 enum ui_align align
, u32 colour
){
973 ui_ntext( rect
, str
, 1024, scale
, align
, colour
);
977 * Standard layout stuff
978 * -----------------------------------------------------------------------------
981 static void ui_panel( ui_rect in_rect
, ui_rect out_panel
){
982 ui_fill( in_rect
, ui_colour( k_ui_bg
+1 ) );
983 ui_outline( in_rect
, 1, ui_colour( k_ui_bg
+7 ), 0 );
984 rect_copy( in_rect
, out_panel
);
985 ui_rect_pad( out_panel
, (ui_px
[2]){ k_ui_padding
, k_ui_padding
} );
988 static void ui_label( ui_rect rect
, const char *text
, ui_px size
,
989 ui_px gap
, ui_rect r
){
991 ui_px width
= (ui_text_line_width(text
)+vg_ui
.font
->spacing
) * size
;
992 ui_split( rect
, k_ui_axis_v
, width
, gap
, l
, r
);
993 ui_text( l
, text
, 1, k_ui_align_middle_left
, 0 );
996 static void ui_standard_widget( ui_rect inout_panel
, ui_rect out_rect
,
998 ui_px height
= (count
* vg_ui
.font
->glyph_height
+ 18) * k_ui_scale
;
999 ui_split( inout_panel
, k_ui_axis_h
, height
, k_ui_padding
,
1000 out_rect
, inout_panel
);
1003 static void ui_info( ui_rect inout_panel
, const char *text
){
1005 ui_standard_widget( inout_panel
, box
, 1 );
1006 ui_text( box
, text
, 1, k_ui_align_middle_left
, 0 );
1009 static void ui_image( ui_rect rect
, GLuint image
){
1010 ui_flush( k_ui_shader_colour
);
1011 glActiveTexture( GL_TEXTURE0
);
1012 glBindTexture( GL_TEXTURE_2D
, image
);
1013 ui_fill_rect( rect
, 0xffffffff, (ui_px
[4]){ 0,256,256,0 } );
1014 ui_flush( k_ui_shader_image
);
1017 static u32
v4f_u32_colour( v4f colour
){
1018 u32 r
= colour
[0] * 255.0f
,
1019 g
= colour
[1] * 255.0f
,
1020 b
= colour
[2] * 255.0f
,
1021 a
= colour
[3] * 255.0f
;
1023 return r
| (g
<<8) | (b
<<16) | (a
<<24);
1026 static void ui_defocus_all(void){
1027 if( vg_ui
.focused_control_type
== k_ui_control_textbox
){
1028 SDL_StopTextInput();
1029 if( vg_ui
.textbox
.callbacks
.escape
)
1030 vg_ui
.textbox
.callbacks
.escape();
1033 vg_ui
.focused_control_id
= NULL
;
1034 vg_ui
.focused_control_hit
= 0;
1035 vg_ui
.focused_control_type
= k_ui_control_none
;
1038 enum ui_button_state
{
1039 k_ui_button_none
= 0,
1040 k_ui_button_click
= 1,
1041 k_ui_button_holding_inside
= 2,
1042 k_ui_button_holding_outside
= 3,
1043 k_ui_button_hover
= 4
1046 static enum ui_button_state
ui_colourbutton( ui_rect rect
,
1047 enum ui_scheme_colour colour
){
1048 int clickup
= ui_click_up(UI_MOUSE_LEFT
),
1049 click
= ui_clicking(UI_MOUSE_LEFT
) | clickup
,
1050 target
= ui_inside_rect( rect
, vg_ui
.mouse_click
) && click
,
1051 hover
= ui_inside_rect( rect
, vg_ui
.mouse
);
1053 u32 col_base
= vg_ui
.scheme
[ colour
],
1054 col_highlight
= vg_ui
.scheme
[ k_ui_fg
],
1055 col_hover
= vg_ui
.scheme
[ colour
+ k_ui_brighter
];
1057 if( vg_ui
.focused_control_type
!= k_ui_control_none
){
1065 vg_ui
.cursor
= k_ui_cursor_hand
;
1072 ui_fill( rect
, col_highlight
);
1073 vg_ui
.ignore_input_frames
= 2;
1074 rect_copy( rect
, vg_ui
.click_fader
);
1076 rect_copy( rect
, vg_ui
.click_fader_end
);
1077 vg_ui
.click_fader_end
[3] = 0;
1078 ui_rect_center( rect
, vg_ui
.click_fader_end
);
1080 vg_ui
.click_fade_opacity
= 1.0f
;
1082 return k_ui_button_click
;
1085 ui_fill( rect
, col_highlight
);
1086 return k_ui_button_holding_inside
;
1090 ui_fill( rect
, col_base
);
1091 ui_outline( rect
, 1, col_highlight
, 0 );
1092 return k_ui_button_holding_outside
;
1096 ui_fill( rect
, col_base
);
1097 return k_ui_button_none
;
1102 ui_fill( rect
, col_hover
);
1103 return k_ui_button_hover
;
1106 ui_fill( rect
, col_base
);
1107 return k_ui_button_none
;
1112 static enum ui_button_state
ui_colourbutton_text(
1113 ui_rect rect
, const char *string
, ui_px scale
,
1114 enum ui_scheme_colour colour
){
1115 enum ui_button_state state
= ui_colourbutton( rect
, colour
);
1116 ui_rect t
= { 0,0, ui_text_line_width( string
)*scale
, 14*scale
};
1117 ui_rect_center( rect
, t
);
1119 u32 text_colour
= ui_colourcont(colour
);
1120 if( state
== k_ui_button_holding_inside
)
1121 text_colour
= colour
;
1123 ui_text( t
, string
, scale
, k_ui_align_left
, text_colour
);
1127 static enum ui_button_state
ui_button_text( ui_rect rect
,
1128 const char *string
, ui_px scale
){
1129 return ui_colourbutton_text( rect
, string
, scale
, k_ui_bg
+4 );
1132 static enum ui_button_state
ui_button( ui_rect inout_panel
,
1133 const char *string
){
1135 ui_standard_widget( inout_panel
, rect
, 1 );
1136 return ui_colourbutton_text( rect
, string
, 1, k_ui_bg
+4 );
1139 static void ui_enum_post(void);
1140 static void ui_postrender(void){
1141 if( vg_ui
.click_fade_opacity
> 0.0f
){
1142 float scale
= vg_ui
.click_fade_opacity
;
1143 scale
= vg_maxf( 1.0f
/255.0f
, scale
*scale
);
1145 vg_ui
.click_fade_opacity
-= vg
.time_frame_delta
* 3.8f
;
1146 u32 colour
= (0x00ffffff & ui_colour(k_ui_fg
))|0x7f000000;
1148 v4f begin
, end
, dest
;
1149 for( int i
=0; i
<4; i
++ ){
1150 begin
[i
] = vg_ui
.click_fader
[i
];
1151 end
[i
] = vg_ui
.click_fader_end
[i
]+1;
1154 v4_lerp( end
, begin
, scale
, dest
);
1157 for( int i
=0; i
<4; i
++ ){
1161 ui_fill( rect
, colour
);
1164 if( vg_ui
.focused_control_type
== k_ui_control_enum
){
1167 else if( vg_ui
.focused_control_type
== k_ui_control_modal
){
1168 ui_rect screen
= {0,0,vg
.window_x
,vg
.window_y
};
1169 ui_fill( screen
, 0xa0000000 );
1170 ui_rect box
= {0,0,400,200};
1172 u32 colour
= ui_colour(k_ui_fg
),
1173 type
= vg_ui
.modal
.options
& UI_MODAL_TYPE_BITS
;
1174 if ( type
== 1 ) colour
= ui_colour(k_ui_green
);
1175 else if( type
== 2 ) colour
= ui_colour(k_ui_red
);
1176 else if( type
== 3 ) colour
= ui_colour(k_ui_yellow
);
1178 ui_rect_center( screen
, box
);
1179 ui_fill( box
, ui_colour(k_ui_bg
) );
1180 ui_outline( box
, -1, colour
, 0 );
1183 rect_copy( box
, message
);
1185 ui_rect_center( box
, message
);
1187 ui_rect row0
, row1
, btn
;
1188 ui_split_ratio( message
, k_ui_axis_h
, 0.5f
, 0, row0
, row1
);
1189 row0
[0] += vg_ui
.font
->spacing
;
1190 ui_ntext( row0
, vg_ui
.modal
.message
, (box
[2]/vg_ui
.font
->spacing
)-2, 1,
1191 k_ui_align_left
, colour
);
1193 rect_copy( row1
, btn
);
1196 ui_rect_center( row1
, btn
);
1198 vg_ui
.focused_control_type
= k_ui_control_none
; /* HACK */
1199 if( ui_button_text( btn
, "OK", 1 ) != 1 )
1200 vg_ui
.focused_control_hit
= 1;
1201 vg_ui
.focused_control_type
= k_ui_control_modal
; /* HACK */
1202 vg_ui
.wants_mouse
= 1;
1205 ui_flush( k_ui_shader_colour
);
1207 if( !vg_ui
.focused_control_hit
){
1211 if( vg_ui
.wants_mouse
){
1212 SDL_SetWindowGrab( vg
.window
, SDL_FALSE
);
1213 SDL_SetRelativeMouseMode( SDL_FALSE
);
1216 SDL_SetWindowGrab( vg
.window
, SDL_TRUE
);
1217 SDL_SetRelativeMouseMode( SDL_TRUE
);
1220 SDL_SetCursor( vg_ui
.cursor_map
[ vg_ui
.cursor
] );
1226 * -----------------------------------------------------------------------------
1229 static int ui_checkbox( ui_rect inout_panel
, const char *str_label
, i32
*data
){
1230 ui_rect rect
, label
, box
;
1231 ui_standard_widget( inout_panel
, rect
, 1 );
1233 ui_split( rect
, k_ui_axis_v
, -rect
[3], 0, label
, box
);
1234 ui_text( label
, str_label
, k_ui_scale
, k_ui_align_middle_left
, 0 );
1236 int changed
= ui_colourbutton( box
, k_ui_bg
)==1;
1238 *data
= (*data
) ^ 0x1;
1241 ui_rect_pad( box
, (ui_px
[2]){4,4} );
1242 ui_fill( box
, ui_colour( k_ui_orange
) );
1250 * -----------------------------------------------------------------------------
1254 * unfortunately no return value since we only find out that event in the
1257 static void ui_enum( ui_rect inout_panel
, const char *str_label
,
1258 struct ui_enum_opt
*options
, u32 len
, i32
*value
){
1259 ui_rect rect
, label
, box
;
1260 ui_standard_widget( inout_panel
, rect
, 1 );
1261 ui_label( rect
, str_label
, k_ui_scale
, 0, box
);
1263 const char *display
= "OUT OF RANGE";
1265 for( u32 i
=0; i
<len
; i
++ ){
1266 if( *value
== options
[i
].value
){
1267 display
= options
[i
].alias
;
1273 if( ui_button_text( box
, display
, k_ui_scale
) == 1 ){
1274 vg_ui
.focused_control_type
= k_ui_control_enum
;
1275 vg_ui
.ptr_enum
= value
;
1276 vg_ui
._enum
.option_count
= len
;
1277 vg_ui
._enum
.options
= options
;
1278 rect_copy( box
, vg_ui
._enum
.rect
);
1282 ui_outline( box
, 1, ui_colour(k_ui_red
), 0 );
1285 static void ui_enum_post(void){
1287 rect_copy( vg_ui
._enum
.rect
, drawer
);
1288 drawer
[3] *= vg_ui
._enum
.option_count
;
1291 int clickany
= ui_click_up(UI_MOUSE_LEFT
|UI_MOUSE_RIGHT
|UI_MOUSE_MIDDLE
),
1292 hover
= ui_inside_rect( drawer
, vg_ui
.mouse
);
1294 if( clickany
&& !hover
){
1299 vg_ui
.focused_control_type
= k_ui_control_none
;
1300 i32
*value
= vg_ui
.ptr_enum
;
1302 for( u32 i
=0; i
<vg_ui
._enum
.option_count
; i
++ ){
1304 ui_split( drawer
, k_ui_axis_h
, vg_ui
._enum
.rect
[3], 0, button
,drawer
);
1306 enum ui_scheme_colour colour
= k_ui_bg
+3;
1307 if( vg_ui
._enum
.options
[i
].value
== *value
)
1308 colour
= k_ui_orange
;
1310 if( ui_colourbutton_text( button
, vg_ui
._enum
.options
[i
].alias
,
1311 k_ui_scale
, colour
) == 1 ){
1312 *value
= vg_ui
._enum
.options
[i
].value
;
1318 vg_ui
.focused_control_type
= k_ui_control_enum
;
1321 vg_ui
.focused_control_hit
= 1;
1326 * -----------------------------------------------------------------------------
1329 static void _ui_textbox_make_selection( int *start
, int *end
){
1330 *start
= VG_MIN( vg_ui
.textbox
.cursor_pos
, vg_ui
.textbox
.cursor_user
);
1331 *end
= VG_MAX( vg_ui
.textbox
.cursor_pos
, vg_ui
.textbox
.cursor_user
);
1334 static void _ui_textbox_move_cursor( int *cursor0
, int *cursor1
,
1335 int dir
, int snap_together
){
1336 *cursor0
= VG_MAX( 0, vg_ui
.textbox
.cursor_user
+ dir
);
1339 VG_MIN( vg_ui
.textbox
.len
-1, strlen( vg_ui
.textbuf
)),
1343 *cursor1
= *cursor0
;
1346 static int _ui_textbox_makeroom( int datastart
, int length
){
1347 int move_to
= VG_MIN( datastart
+length
, vg_ui
.textbox
.len
-1 );
1348 int move_amount
= strlen( vg_ui
.textbuf
)-datastart
;
1349 int move_end
= VG_MIN( move_to
+move_amount
, vg_ui
.textbox
.len
-1 );
1350 move_amount
= move_end
-move_to
;
1353 memmove( &vg_ui
.textbuf
[ move_to
],
1354 &vg_ui
.textbuf
[ datastart
],
1357 vg_ui
.textbuf
[ move_end
] = '\0';
1359 return VG_MIN( length
, vg_ui
.textbox
.len
-datastart
-1 );
1362 static int _ui_textbox_delete_char( int direction
){
1364 _ui_textbox_make_selection( &start
, &end
);
1366 /* There is no selection */
1368 if( direction
== 1 ) end
= VG_MIN( end
+1, strlen( vg_ui
.textbuf
) );
1369 else if( direction
== -1 ) start
= VG_MAX( start
-1, 0 );
1372 /* Still no selction, no need to do anything */
1376 /* Copy the end->terminator to start */
1377 int remaining_length
= strlen( vg_ui
.textbuf
)+1-end
;
1378 memmove( &vg_ui
.textbuf
[ start
],
1379 &vg_ui
.textbuf
[ end
],
1384 static void _ui_textbox_to_clipboard(void){
1386 _ui_textbox_make_selection( &start
, &end
);
1390 memcpy( buffer
, &vg_ui
.textbuf
[ start
], end
-start
);
1391 buffer
[ end
-start
] = 0x00;
1392 SDL_SetClipboardText( buffer
);
1396 static void ui_start_modal( const char *message
, u32 options
);
1397 static void _ui_textbox_clipboard_paste(void){
1398 if( !SDL_HasClipboardText() )
1401 char *text
= SDL_GetClipboardText();
1406 int datastart
= _ui_textbox_delete_char( 0 );
1407 int length
= strlen( text
);
1409 if( (vg_ui
.textbox
.len
- strlen(vg_ui
.textbuf
)) < length
){
1410 ui_start_modal( "Clipboard content exceeds buffer size.", UI_MODAL_BAD
);
1414 int cpylength
= _ui_textbox_makeroom( datastart
, length
);
1416 memcpy( vg_ui
.textbuf
+ datastart
, text
, cpylength
);
1417 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
,
1418 &vg_ui
.textbox
.cursor_pos
, cpylength
, 1 );
1422 static void _ui_textbox_put_char( char c
){
1423 vg_ui
.textbox
.cursor_user
= _ui_textbox_delete_char(0);
1424 if( (vg_ui
.textbox
.len
- strlen(vg_ui
.textbuf
)) <= 1 ) return;
1426 if( _ui_textbox_makeroom( vg_ui
.textbox
.cursor_user
, 1 ) )
1427 vg_ui
.textbuf
[ vg_ui
.textbox
.cursor_user
] = c
;
1429 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
,
1430 &vg_ui
.textbox
.cursor_pos
, 1, 1 );
1433 /* Receed secondary cursor */
1434 static void _ui_textbox_left_select(void){
1435 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
, NULL
, -1, 0 );
1438 /* Match and receed both cursors */
1439 static void _ui_textbox_left(void){
1440 int cursor_diff
= vg_ui
.textbox
.cursor_pos
- vg_ui
.textbox
.cursor_user
? 0: 1;
1442 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
,
1443 &vg_ui
.textbox
.cursor_pos
, -cursor_diff
, 1 );
1446 static void _ui_textbox_up(void){
1447 if( vg_ui
.textbox
.flags
& UI_TEXTBOX_MULTILINE
){
1448 int line_begin
= vg_ui
.textbox
.cursor_user
;
1450 while( line_begin
){
1451 if( vg_ui
.textbuf
[ line_begin
-1 ] == '\n' ){
1459 int line_above_begin
= line_begin
-1;
1461 while( line_above_begin
){
1462 if( vg_ui
.textbuf
[ line_above_begin
-1 ] == '\n' ){
1466 line_above_begin
--;
1469 int offset
= vg_ui
.textbox
.cursor_user
- line_begin
,
1470 line_length_above
= line_begin
- line_above_begin
-1;
1472 offset
= VG_MIN( line_length_above
, offset
);
1474 vg_ui
.textbox
.cursor_user
= line_above_begin
+offset
;
1475 vg_ui
.textbox
.cursor_pos
= line_above_begin
+offset
;
1478 vg_ui
.textbox
.cursor_user
= line_begin
;
1479 vg_ui
.textbox
.cursor_pos
= line_begin
;
1483 if( vg_ui
.textbox
.callbacks
.up
){
1484 vg_ui
.textbox
.callbacks
.up( vg_ui
.textbuf
, vg_ui
.textbox
.len
);
1489 static void _ui_textbox_down(void){
1490 if( vg_ui
.textbox
.flags
& UI_TEXTBOX_MULTILINE
){
1491 int line_begin
= vg_ui
.textbox
.cursor_user
;
1493 while( line_begin
){
1494 if( vg_ui
.textbuf
[ line_begin
-1 ] == '\n' ){
1501 int line_below_begin
= vg_ui
.textbox
.cursor_user
;
1504 if( vg_ui
.textbuf
[ line_below_begin
] == '\0' ){
1505 vg_ui
.textbox
.cursor_user
= line_below_begin
;
1506 vg_ui
.textbox
.cursor_pos
= line_below_begin
;
1510 if( vg_ui
.textbuf
[ line_below_begin
] == '\n' ){
1511 line_below_begin
++;
1515 line_below_begin
++;
1518 int line_below_end
= line_below_begin
;
1520 if( vg_ui
.textbuf
[ line_below_end
] == '\0' ||
1521 vg_ui
.textbuf
[ line_below_end
] == '\n' ){
1528 int offset
= vg_ui
.textbox
.cursor_user
- line_begin
,
1529 line_length_below
= line_below_end
- line_below_begin
-1;
1531 offset
= VG_MIN( line_length_below
, offset
);
1533 vg_ui
.textbox
.cursor_user
= line_below_begin
+offset
;
1534 vg_ui
.textbox
.cursor_pos
= line_below_begin
+offset
;
1537 if( vg_ui
.textbox
.callbacks
.down
){
1538 vg_ui
.textbox
.callbacks
.down( vg_ui
.textbuf
, vg_ui
.textbox
.len
);
1543 static void _ui_textbox_right_select(void){
1544 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
, NULL
, 1, 0 );
1547 static void _ui_textbox_right(void){
1548 int cursor_diff
= vg_ui
.textbox
.cursor_pos
- vg_ui
.textbox
.cursor_user
? 0: 1;
1550 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
,
1551 &vg_ui
.textbox
.cursor_pos
, +cursor_diff
, 1 );
1554 static void _ui_textbox_backspace(void){
1555 if( vg_ui
.focused_control_type
== k_ui_control_textbox
){
1556 vg_ui
.textbox
.cursor_user
= _ui_textbox_delete_char( -1 );
1557 vg_ui
.textbox
.cursor_pos
= vg_ui
.textbox
.cursor_user
;
1559 if( vg_ui
.textbox
.callbacks
.change
){
1560 vg_ui
.textbox
.callbacks
.change( vg_ui
.textbuf
, vg_ui
.textbox
.len
);
1565 static void _ui_textbox_delete(void){
1566 if( vg_ui
.focused_control_type
== k_ui_control_textbox
){
1567 vg_ui
.textbox
.cursor_user
= _ui_textbox_delete_char( 1 );
1568 vg_ui
.textbox
.cursor_pos
= vg_ui
.textbox
.cursor_user
;
1570 if( vg_ui
.textbox
.callbacks
.change
){
1571 vg_ui
.textbox
.callbacks
.change( vg_ui
.textbuf
, vg_ui
.textbox
.len
);
1576 static void _ui_textbox_home_select(void){
1577 i32 start
= vg_ui
.textbox
.cursor_user
;
1579 if( vg_ui
.textbox
.flags
& UI_TEXTBOX_MULTILINE
){
1581 if( vg_ui
.textbuf
[start
-1] == '\n' )
1590 vg_ui
.textbox
.cursor_user
= start
;
1593 static void _ui_textbox_home(void){
1594 _ui_textbox_home_select();
1595 vg_ui
.textbox
.cursor_pos
= vg_ui
.textbox
.cursor_user
;
1598 static void _ui_textbox_end_select(void){
1599 i32 end
= vg_ui
.textbox
.cursor_user
;
1601 if( vg_ui
.textbox
.flags
& UI_TEXTBOX_MULTILINE
){
1602 while( vg_ui
.textbuf
[end
] ){
1603 if( vg_ui
.textbuf
[end
] == '\n' )
1610 end
= VG_MIN( vg_ui
.textbox
.len
-1, strlen(vg_ui
.textbuf
) );
1612 vg_ui
.textbox
.cursor_user
= end
;
1615 static void _ui_textbox_end(void){
1616 _ui_textbox_end_select();
1617 vg_ui
.textbox
.cursor_pos
= vg_ui
.textbox
.cursor_user
;
1620 static void _ui_textbox_select_all(void){
1621 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
, NULL
, 10000, 0);
1622 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_pos
, NULL
, -10000, 0);
1625 static void _ui_textbox_cut(void){
1626 _ui_textbox_to_clipboard();
1627 vg_ui
.textbox
.cursor_user
= _ui_textbox_delete_char(0);
1628 vg_ui
.textbox
.cursor_pos
= vg_ui
.textbox
.cursor_user
;
1631 static void _ui_textbox_enter(void){
1632 if( vg_ui
.focused_control_type
== k_ui_control_textbox
){
1633 vg_ui
.ignore_input_frames
= 2;
1635 if( vg_ui
.textbox
.callbacks
.enter
)
1636 vg_ui
.textbox
.callbacks
.enter( vg_ui
.textbuf
, vg_ui
.textbox
.len
);
1638 if( vg_ui
.focused_control_type
!= k_ui_control_textbox
) return;
1640 if( vg_ui
.textbox
.flags
& UI_TEXTBOX_MULTILINE
)
1641 _ui_textbox_put_char( '\n' );
1643 if( !(vg_ui
.textbox
.flags
& UI_TEXTBOX_AUTOFOCUS
) )
1650 * based on a visual character coordinate relative to the anchor of the textbox,
1651 * this works out the linear place in the buffer that coordinate maps to
1653 * input coordinates go in co[0], co[1], and the result index is in co[2]
1655 static void _ui_textbox_calc_index_from_grid( int co
[3], int wrap_length
){
1659 while( (c
= vg_ui
.textbuf
[i
[2]]) ){
1660 if( i
[1]==co
[1] && i
[0]>=co
[0] ) break;
1662 if( i
[0] >= wrap_length
){
1667 if( c
>= 32 && c
<= 126 ){
1671 else if( c
== '\n' ){
1674 if( i
[1] > co
[1] ) break;
1688 * based on the index specied in co[2], work out the visual character
1689 * coordinates and store them in co[0], co[1]
1691 static void _ui_textbox_index_calc_coords( int co
[3], int wrap_length
){
1698 while( (c
= vg_ui
.textbuf
[i
++]) ){
1699 if( i
> co
[2] ) break;
1700 if( co
[0] >= wrap_length
){
1704 if( c
>= 32 && c
<= 126 ) co
[0] ++;
1705 else if( c
== '\n' ){
1713 * calculate the number of characters remaining until either:
1714 * - the wrap_length limit is hit
1715 * - end of the line/string
1717 * index must be fully populated with visual X/Y, and linear index
1719 static int _ui_textbox_run_remaining( int index
[3], int wrap_length
){
1720 int i
=0, printed_chars
=0;
1722 while( (c
= vg_ui
.textbuf
[index
[2] + (i
++)]) ){
1723 if( index
[0]+i
>= wrap_length
) break;
1724 if( c
>= 32 && c
<= 126 ) printed_chars
++;
1725 else if( c
== '\n' ) break;
1728 return printed_chars
+1;
1731 static int ui_textbox( ui_rect inout_panel
, const char *label
,
1732 char *buf
, u32 len
, u32 lines
, u32 flags
,
1733 struct ui_textbox_callbacks
*callbacks
){
1734 assert( lines
> 0 );
1737 if( lines
> 1 ) flags
|= UI_TEXTBOX_MULTILINE
;
1740 ui_standard_widget( inout_panel
, rect
, lines
);
1743 ui_label( rect
, label
, 1, 0, rect
);
1745 int clickup
= ui_click_up(UI_MOUSE_LEFT
),
1746 clickdown
= ui_click_down(UI_MOUSE_LEFT
),
1747 click
= ui_clicking(UI_MOUSE_LEFT
) | clickup
,
1748 target
= ui_inside_rect( rect
, vg_ui
.mouse_click
) && click
,
1749 hover
= ui_inside_rect( rect
, vg_ui
.mouse
);
1751 /* allow instant transitions from textbox->textbox */
1752 if( (vg_ui
.focused_control_type
!= k_ui_control_none
) &&
1753 (vg_ui
.focused_control_type
!= k_ui_control_textbox
) ){
1759 flags
&= ~UI_TEXTBOX_AUTOFOCUS
;
1762 u32 col_base
= ui_colour( k_ui_bg
),
1763 col_highlight
= ui_colour( k_ui_fg
),
1764 col_cursor
= (0x00ffffff & ui_colour(k_ui_fg
))|0x7f000000;
1769 rect_copy( rect
, text_rect
);
1771 if( flags
& UI_TEXTBOX_MULTILINE
) text_rect
[3] = rect
[3]-16;
1772 else text_rect
[3] = vg_ui
.font
->line_height
;
1775 ui_rect_center( rect
, text_rect
);
1777 ui_px wrap_length
= 1024;
1779 if( flags
& UI_TEXTBOX_WRAP
)
1780 wrap_length
= text_rect
[2] / vg_ui
.font
->spacing
;
1783 vg_ui
.cursor
= k_ui_cursor_ibeam
;
1786 if( vg_ui
.focused_control_id
== buf
){
1787 ui_fill( rect
, col_base
);
1788 ui_ntext( text_rect
, buf
, wrap_length
, 1, k_ui_align_left
, 0 );
1790 if( !(flags
& UI_TEXTBOX_AUTOFOCUS
) && ((clickup
||clickdown
) && !target
)){
1794 vg_ui
.focused_control_hit
= 1;
1795 if( click
&& target
){
1797 (vg_ui
.mouse_click
[0] - text_rect
[0]) / vg_ui
.font
->spacing
,
1798 (vg_ui
.mouse_click
[1] - text_rect
[1]) / vg_ui
.font
->line_height
,
1802 (vg_ui
.mouse
[0] - text_rect
[0]) / vg_ui
.font
->spacing
,
1803 (vg_ui
.mouse
[1] - text_rect
[1]) / vg_ui
.font
->line_height
,
1807 if( flags
& UI_TEXTBOX_MULTILINE
){
1808 _ui_textbox_calc_index_from_grid( p0
, wrap_length
);
1809 _ui_textbox_calc_index_from_grid( p1
, wrap_length
);
1811 vg_ui
.textbox
.cursor_pos
= p0
[2];
1812 vg_ui
.textbox
.cursor_user
= p1
[2];
1815 int max
= strlen( buf
);
1816 vg_ui
.textbox
.cursor_pos
= VG_MAX( 0, VG_MIN( max
, p0
[0] )),
1817 vg_ui
.textbox
.cursor_user
= VG_MAX( 0, VG_MIN( max
, p1
[0] ));
1821 ui_outline( rect
, -2, vg_ui
.scheme
[ k_ui_orange
], 0 );
1825 int c0
= vg_ui
.textbox
.cursor_pos
,
1826 c1
= vg_ui
.textbox
.cursor_user
,
1827 start
= VG_MIN( c0
, c1
),
1828 end
= VG_MAX( c0
, c1
),
1831 if( flags
& (UI_TEXTBOX_WRAP
|UI_TEXTBOX_MULTILINE
) ){
1832 int pos
[3], remaining
= chars
;
1835 _ui_textbox_index_calc_coords( pos
, wrap_length
);
1838 cursor
[0] = text_rect
[0] + pos
[0]*vg_ui
.font
->spacing
-1;
1839 cursor
[1] = text_rect
[1] + pos
[1]*14;
1842 ui_fill( cursor
, col_cursor
);
1843 rect_copy( cursor
, vg_ui
.click_fader_end
);
1847 int run
= _ui_textbox_run_remaining( pos
, wrap_length
);
1848 run
= VG_MIN( run
, remaining
);
1850 cursor
[0] = text_rect
[0] + pos
[0]*vg_ui
.font
->spacing
-1;
1851 cursor
[1] = text_rect
[1] + pos
[1]*14;
1852 cursor
[2] = (float)(run
)*(float)vg_ui
.font
->spacing
;
1855 ui_fill( cursor
, col_cursor
);
1862 rect_copy( cursor
, vg_ui
.click_fader_end
);
1866 cursor
[0] = text_rect
[0] + start
*vg_ui
.font
->spacing
-1;
1867 cursor
[1] = text_rect
[1];
1874 cursor
[2] = (float)(chars
)*(float)vg_ui
.font
->spacing
;
1877 if( (vg_ui
.click_fade_opacity
<=0.0f
) &&
1878 ui_clip( rect
, cursor
, cursor
) ){
1879 ui_fill( cursor
, col_cursor
);
1882 rect_copy( cursor
, vg_ui
.click_fader_end
);
1889 if( click
|| (flags
& UI_TEXTBOX_AUTOFOCUS
) ){
1890 if( (target
&& hover
) || (flags
& UI_TEXTBOX_AUTOFOCUS
) ){
1893 ui_fill( rect
, col_highlight
);
1894 vg_ui
.ignore_input_frames
= 2;
1895 rect_copy( rect
, vg_ui
.click_fader
);
1896 rect_copy( rect
, vg_ui
.click_fader_end
);
1898 vg_ui
.click_fade_opacity
= 1.0f
;
1899 vg_ui
.textbuf
= buf
;
1900 vg_ui
.focused_control_hit
= 1;
1901 vg_ui
.focused_control_type
= k_ui_control_textbox
;
1902 vg_ui
.textbox
.len
= len
;
1903 vg_ui
.textbox
.flags
= flags
;
1904 vg_ui
.textbox
.cursor_pos
= 0;
1905 vg_ui
.textbox
.cursor_user
= 0;
1908 vg_ui
.textbox
.callbacks
= *callbacks
;
1911 vg_ui
.textbox
.callbacks
.change
= NULL
;
1912 vg_ui
.textbox
.callbacks
.down
= NULL
;
1913 vg_ui
.textbox
.callbacks
.up
= NULL
;
1914 vg_ui
.textbox
.callbacks
.enter
= NULL
;
1917 SDL_StartTextInput();
1921 ui_fill( rect
, col_base
);
1924 ui_outline( rect
, -1, col_highlight
, 0 );
1927 ui_ntext( text_rect
, buf
, wrap_length
, 1, k_ui_align_left
, 0 );
1933 * -----------------------------------------------------------------------------
1936 static void ui_tabs( ui_rect inout_panel
, ui_rect out_content_panel
,
1937 const char **titles
, u32 count
, i32
*page
){
1939 ui_standard_widget( inout_panel
, bar
, 1 );
1941 i32 cur_page
= *page
;
1943 f32 width
= (f32
)inout_panel
[2] / (f32
)count
;
1945 ui_px h
= (inout_panel
[1] + inout_panel
[3]) - (bar
[1]+bar
[3]);
1946 inout_panel
[1] = bar
[1]+bar
[3];
1949 ui_fill( inout_panel
, ui_colour( k_ui_bg
+2 ) );
1950 ui_outline( inout_panel
, 1, ui_colour( k_ui_bg
+5 ), 0 );
1952 rect_copy( inout_panel
, out_content_panel
);
1953 ui_rect_pad( out_content_panel
, (ui_px
[2]){ k_ui_padding
, k_ui_padding
} );
1956 for( i32 i
=0; i
<count
; i
++ ){
1958 bar
[0] + ((f32
)i
*width
),
1964 enum ui_scheme_colour colour
= k_ui_bg
+4;
1965 if( i
== cur_page
){
1967 ui_outline( button
, 1, ui_colour( k_ui_bg
+5 ),
1968 UI_TOP
|UI_LEFT
|UI_RIGHT
);
1972 if( ui_colourbutton_text( button
, titles
[i
], 1, colour
) == 1 )
1979 * -----------------------------------------------------------------------------
1981 static void ui_start_modal( const char *message
, u32 options
){
1982 vg_ui
.focused_control_type
= k_ui_control_modal
;
1983 vg_ui
.modal
.message
= message
;
1984 vg_ui
.modal
.callbacks
.close
= NULL
;
1985 vg_ui
.modal
.options
= options
;
1987 u32 type
= options
& UI_MODAL_TYPE_BITS
;
1988 if( type
== UI_MODAL_OK
) vg_info( message
);
1989 else if( type
== UI_MODAL_WARN
) vg_warn( message
);
1990 else if( type
== UI_MODAL_GOOD
) vg_success( message
);
1991 else if( type
== UI_MODAL_BAD
) vg_error( message
);
1996 * -----------------------------------------------------------------------------
2002 static void _ui_proc_key( SDL_Keysym ev
){
2003 if( vg_ui
.focused_control_type
!= k_ui_control_textbox
){
2007 struct textbox_mapping
{
2011 void (*handler
)(void);
2015 { 0, SDLK_LEFT
, _ui_textbox_left
},
2016 { KMOD_SHIFT
, SDLK_LEFT
, _ui_textbox_left_select
},
2017 { 0, SDLK_RIGHT
, _ui_textbox_right
},
2018 { KMOD_SHIFT
, SDLK_RIGHT
, _ui_textbox_right_select
},
2019 { 0, SDLK_DOWN
, _ui_textbox_down
},
2020 { 0, SDLK_UP
, _ui_textbox_up
},
2021 { 0, SDLK_BACKSPACE
, _ui_textbox_backspace
},
2022 { KMOD_SHIFT
, SDLK_BACKSPACE
, _ui_textbox_backspace
},
2023 { KMOD_CTRL
, SDLK_BACKSPACE
, _ui_textbox_backspace
},
2024 { 0, SDLK_DELETE
, _ui_textbox_delete
},
2025 { 0, SDLK_HOME
, _ui_textbox_home
},
2026 { KMOD_SHIFT
, SDLK_HOME
, _ui_textbox_home_select
},
2027 { 0, SDLK_END
, _ui_textbox_end
},
2028 { KMOD_SHIFT
, SDLK_END
, _ui_textbox_end_select
},
2029 { KMOD_CTRL
, SDLK_a
, _ui_textbox_select_all
},
2030 { KMOD_CTRL
, SDLK_c
, _ui_textbox_to_clipboard
},
2031 { KMOD_CTRL
, SDLK_x
, _ui_textbox_cut
},
2032 { KMOD_CTRL
, SDLK_v
, _ui_textbox_clipboard_paste
},
2033 { 0, SDLK_RETURN
, _ui_textbox_enter
},
2034 { 0, SDLK_ESCAPE
, ui_defocus_all
},
2039 if( ev
.mod
& KMOD_SHIFT
)
2042 if( ev
.mod
& KMOD_CTRL
)
2045 if( ev
.mod
& KMOD_ALT
)
2048 for( int i
=0; i
<vg_list_size( mappings
); i
++ ){
2049 struct textbox_mapping
*mapping
= &mappings
[i
];
2051 if( mapping
->key
== ev
.sym
){
2052 if( mapping
->mod
== 0 ){
2058 else if( (mod
& mapping
->mod
) == mapping
->mod
){
2067 * Callback for text entry mode
2069 static void ui_proc_utf8( const char *text
){
2070 if( vg_ui
.focused_control_type
== k_ui_control_textbox
){
2071 const char *ptr
= text
;
2074 if( *ptr
!= '`' ) _ui_textbox_put_char( *ptr
);
2078 if( vg_ui
.textbox
.callbacks
.change
){
2079 vg_ui
.textbox
.callbacks
.change( vg_ui
.textbuf
, vg_ui
.textbox
.len
);
2086 * -----------------------------------------------------------------------------
2089 static void ui_dev_colourview(void){
2090 ui_rect window
= {vg
.window_x
-256,0,256,vg
.window_y
}, swatch
;
2092 const char *names
[vg_list_size(vg_ui
.scheme
)] = {
2093 [k_ui_bg
] = "k_ui_bg", "k_ui_bg+1", "k_ui_bg+2", "k_ui_bg+3",
2094 "k_ui_bg+4", "k_ui_bg+5", "k_ui_bg+6", "k_ui_bg+7",
2096 [k_ui_fg
] = "k_ui_fg", "k_ui_fg+1", "k_ui_fg+2", "k_ui_fg+3",
2097 "k_ui_fg+4", "k_ui_fg+5", "k_ui_fg+6", "k_ui_fg+7",
2099 [k_ui_red
] = "k_ui_red", "k_ui_orange", "k_ui_yellow", "k_ui_green",
2100 "k_ui_aqua", "k_ui_blue", "k_ui_purple", "k_ui_gray",
2101 "k_ui_red+8","k_ui_orange+8","k_ui_yellow+8","k_ui_green+8",
2102 "k_ui_aqua+8","k_ui_blue+8","k_ui_purple+8","k_ui_gray+8" };
2105 ui_split_ratio( window
, k_ui_axis_v
, 0.5f
, 0, col
[0], col
[1] );
2107 for( int i
=0; i
<vg_list_size(vg_ui
.scheme
); i
++ ){
2108 int which
= (i
/8)%2;
2110 ui_split( col
[which
], k_ui_axis_h
, 24, 0, swatch
, col
[which
] );
2111 ui_fill( swatch
, ui_colour(i
) );
2114 ui_text(swatch
, names
[i
], 1, k_ui_align_middle_left
, ui_colourcont(i
));
2118 #endif /* VG_IMGUI_H */