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
.ignore_input_frames
= 2;
1034 vg_ui
.focused_control_id
= NULL
;
1035 vg_ui
.focused_control_hit
= 0;
1036 vg_ui
.focused_control_type
= k_ui_control_none
;
1039 enum ui_button_state
{
1040 k_ui_button_none
= 0,
1041 k_ui_button_click
= 1,
1042 k_ui_button_holding_inside
= 2,
1043 k_ui_button_holding_outside
= 3,
1044 k_ui_button_hover
= 4
1047 static enum ui_button_state
ui_colourbutton( ui_rect rect
,
1048 enum ui_scheme_colour colour
){
1049 int clickup
= ui_click_up(UI_MOUSE_LEFT
),
1050 click
= ui_clicking(UI_MOUSE_LEFT
) | clickup
,
1051 target
= ui_inside_rect( rect
, vg_ui
.mouse_click
) && click
,
1052 hover
= ui_inside_rect( rect
, vg_ui
.mouse
);
1054 u32 col_base
= vg_ui
.scheme
[ colour
],
1055 col_highlight
= vg_ui
.scheme
[ k_ui_fg
],
1056 col_hover
= vg_ui
.scheme
[ colour
+ k_ui_brighter
];
1058 if( vg_ui
.focused_control_type
!= k_ui_control_none
){
1066 vg_ui
.cursor
= k_ui_cursor_hand
;
1073 ui_fill( rect
, col_highlight
);
1074 vg_ui
.ignore_input_frames
= 2;
1075 rect_copy( rect
, vg_ui
.click_fader
);
1077 rect_copy( rect
, vg_ui
.click_fader_end
);
1078 vg_ui
.click_fader_end
[3] = 0;
1079 ui_rect_center( rect
, vg_ui
.click_fader_end
);
1081 vg_ui
.click_fade_opacity
= 1.0f
;
1083 return k_ui_button_click
;
1086 ui_fill( rect
, col_highlight
);
1087 return k_ui_button_holding_inside
;
1091 ui_fill( rect
, col_base
);
1092 ui_outline( rect
, 1, col_highlight
, 0 );
1093 return k_ui_button_holding_outside
;
1097 ui_fill( rect
, col_base
);
1098 return k_ui_button_none
;
1103 ui_fill( rect
, col_hover
);
1104 return k_ui_button_hover
;
1107 ui_fill( rect
, col_base
);
1108 return k_ui_button_none
;
1113 static enum ui_button_state
ui_colourbutton_text(
1114 ui_rect rect
, const char *string
, ui_px scale
,
1115 enum ui_scheme_colour colour
){
1116 enum ui_button_state state
= ui_colourbutton( rect
, colour
);
1117 ui_rect t
= { 0,0, ui_text_line_width( string
)*scale
, 14*scale
};
1118 ui_rect_center( rect
, t
);
1120 u32 text_colour
= ui_colourcont(colour
);
1121 if( state
== k_ui_button_holding_inside
)
1122 text_colour
= colour
;
1124 ui_text( t
, string
, scale
, k_ui_align_left
, text_colour
);
1128 static enum ui_button_state
ui_button_text( ui_rect rect
,
1129 const char *string
, ui_px scale
){
1130 return ui_colourbutton_text( rect
, string
, scale
, k_ui_bg
+4 );
1133 static enum ui_button_state
ui_button( ui_rect inout_panel
,
1134 const char *string
){
1136 ui_standard_widget( inout_panel
, rect
, 1 );
1137 return ui_colourbutton_text( rect
, string
, 1, k_ui_bg
+4 );
1140 static void ui_enum_post(void);
1141 static void ui_postrender(void){
1142 if( vg_ui
.click_fade_opacity
> 0.0f
){
1143 float scale
= vg_ui
.click_fade_opacity
;
1144 scale
= vg_maxf( 1.0f
/255.0f
, scale
*scale
);
1146 vg_ui
.click_fade_opacity
-= vg
.time_frame_delta
* 3.8f
;
1147 u32 colour
= (0x00ffffff & ui_colour(k_ui_fg
))|0x7f000000;
1149 v4f begin
, end
, dest
;
1150 for( int i
=0; i
<4; i
++ ){
1151 begin
[i
] = vg_ui
.click_fader
[i
];
1152 end
[i
] = vg_ui
.click_fader_end
[i
]+1;
1155 v4_lerp( end
, begin
, scale
, dest
);
1158 for( int i
=0; i
<4; i
++ ){
1162 ui_fill( rect
, colour
);
1165 if( vg_ui
.focused_control_type
== k_ui_control_enum
){
1168 else if( vg_ui
.focused_control_type
== k_ui_control_modal
){
1169 ui_rect screen
= {0,0,vg
.window_x
,vg
.window_y
};
1170 ui_fill( screen
, 0xa0000000 );
1171 ui_rect box
= {0,0,400,200};
1173 u32 colour
= ui_colour(k_ui_fg
),
1174 type
= vg_ui
.modal
.options
& UI_MODAL_TYPE_BITS
;
1175 if ( type
== 1 ) colour
= ui_colour(k_ui_green
);
1176 else if( type
== 2 ) colour
= ui_colour(k_ui_red
);
1177 else if( type
== 3 ) colour
= ui_colour(k_ui_yellow
);
1179 ui_rect_center( screen
, box
);
1180 ui_fill( box
, ui_colour(k_ui_bg
) );
1181 ui_outline( box
, -1, colour
, 0 );
1184 rect_copy( box
, message
);
1186 ui_rect_center( box
, message
);
1188 ui_rect row0
, row1
, btn
;
1189 ui_split_ratio( message
, k_ui_axis_h
, 0.5f
, 0, row0
, row1
);
1190 row0
[0] += vg_ui
.font
->spacing
;
1191 ui_ntext( row0
, vg_ui
.modal
.message
, (box
[2]/vg_ui
.font
->spacing
)-2, 1,
1192 k_ui_align_left
, colour
);
1194 rect_copy( row1
, btn
);
1197 ui_rect_center( row1
, btn
);
1199 vg_ui
.focused_control_type
= k_ui_control_none
; /* HACK */
1200 if( ui_button_text( btn
, "OK", 1 ) != 1 )
1201 vg_ui
.focused_control_hit
= 1;
1202 vg_ui
.focused_control_type
= k_ui_control_modal
; /* HACK */
1203 vg_ui
.wants_mouse
= 1;
1206 ui_flush( k_ui_shader_colour
);
1208 if( !vg_ui
.focused_control_hit
){
1212 if( vg_ui
.wants_mouse
){
1213 SDL_SetWindowGrab( vg
.window
, SDL_FALSE
);
1214 SDL_SetRelativeMouseMode( SDL_FALSE
);
1217 SDL_SetWindowGrab( vg
.window
, SDL_TRUE
);
1218 SDL_SetRelativeMouseMode( SDL_TRUE
);
1221 SDL_SetCursor( vg_ui
.cursor_map
[ vg_ui
.cursor
] );
1227 * -----------------------------------------------------------------------------
1230 static int ui_checkbox( ui_rect inout_panel
, const char *str_label
, i32
*data
){
1231 ui_rect rect
, label
, box
;
1232 ui_standard_widget( inout_panel
, rect
, 1 );
1234 ui_split( rect
, k_ui_axis_v
, -rect
[3], 0, label
, box
);
1235 ui_text( label
, str_label
, k_ui_scale
, k_ui_align_middle_left
, 0 );
1237 int changed
= ui_colourbutton( box
, k_ui_bg
)==1;
1239 *data
= (*data
) ^ 0x1;
1242 ui_rect_pad( box
, (ui_px
[2]){4,4} );
1243 ui_fill( box
, ui_colour( k_ui_orange
) );
1251 * -----------------------------------------------------------------------------
1255 * unfortunately no return value since we only find out that event in the
1258 static void ui_enum( ui_rect inout_panel
, const char *str_label
,
1259 struct ui_enum_opt
*options
, u32 len
, i32
*value
){
1260 ui_rect rect
, label
, box
;
1261 ui_standard_widget( inout_panel
, rect
, 1 );
1262 ui_label( rect
, str_label
, k_ui_scale
, 0, box
);
1264 const char *display
= "OUT OF RANGE";
1266 for( u32 i
=0; i
<len
; i
++ ){
1267 if( *value
== options
[i
].value
){
1268 display
= options
[i
].alias
;
1274 if( ui_button_text( box
, display
, k_ui_scale
) == 1 ){
1275 vg_ui
.focused_control_type
= k_ui_control_enum
;
1276 vg_ui
.ptr_enum
= value
;
1277 vg_ui
._enum
.option_count
= len
;
1278 vg_ui
._enum
.options
= options
;
1279 rect_copy( box
, vg_ui
._enum
.rect
);
1283 ui_outline( box
, 1, ui_colour(k_ui_red
), 0 );
1286 static void ui_enum_post(void){
1288 rect_copy( vg_ui
._enum
.rect
, drawer
);
1289 drawer
[3] *= vg_ui
._enum
.option_count
;
1292 int clickany
= ui_click_up(UI_MOUSE_LEFT
|UI_MOUSE_RIGHT
|UI_MOUSE_MIDDLE
),
1293 hover
= ui_inside_rect( drawer
, vg_ui
.mouse
);
1295 if( clickany
&& !hover
){
1300 vg_ui
.focused_control_type
= k_ui_control_none
;
1301 i32
*value
= vg_ui
.ptr_enum
;
1303 for( u32 i
=0; i
<vg_ui
._enum
.option_count
; i
++ ){
1305 ui_split( drawer
, k_ui_axis_h
, vg_ui
._enum
.rect
[3], 0, button
,drawer
);
1307 enum ui_scheme_colour colour
= k_ui_bg
+3;
1308 if( vg_ui
._enum
.options
[i
].value
== *value
)
1309 colour
= k_ui_orange
;
1311 if( ui_colourbutton_text( button
, vg_ui
._enum
.options
[i
].alias
,
1312 k_ui_scale
, colour
) == 1 ){
1313 *value
= vg_ui
._enum
.options
[i
].value
;
1319 vg_ui
.focused_control_type
= k_ui_control_enum
;
1322 vg_ui
.focused_control_hit
= 1;
1327 * -----------------------------------------------------------------------------
1330 static void _ui_textbox_make_selection( int *start
, int *end
){
1331 *start
= VG_MIN( vg_ui
.textbox
.cursor_pos
, vg_ui
.textbox
.cursor_user
);
1332 *end
= VG_MAX( vg_ui
.textbox
.cursor_pos
, vg_ui
.textbox
.cursor_user
);
1335 static void _ui_textbox_move_cursor( int *cursor0
, int *cursor1
,
1336 int dir
, int snap_together
){
1337 *cursor0
= VG_MAX( 0, vg_ui
.textbox
.cursor_user
+ dir
);
1340 VG_MIN( vg_ui
.textbox
.len
-1, strlen( vg_ui
.textbuf
)),
1344 *cursor1
= *cursor0
;
1347 static int _ui_textbox_makeroom( int datastart
, int length
){
1348 int move_to
= VG_MIN( datastart
+length
, vg_ui
.textbox
.len
-1 );
1349 int move_amount
= strlen( vg_ui
.textbuf
)-datastart
;
1350 int move_end
= VG_MIN( move_to
+move_amount
, vg_ui
.textbox
.len
-1 );
1351 move_amount
= move_end
-move_to
;
1354 memmove( &vg_ui
.textbuf
[ move_to
],
1355 &vg_ui
.textbuf
[ datastart
],
1358 vg_ui
.textbuf
[ move_end
] = '\0';
1360 return VG_MIN( length
, vg_ui
.textbox
.len
-datastart
-1 );
1363 static int _ui_textbox_delete_char( int direction
){
1365 _ui_textbox_make_selection( &start
, &end
);
1367 /* There is no selection */
1369 if( direction
== 1 ) end
= VG_MIN( end
+1, strlen( vg_ui
.textbuf
) );
1370 else if( direction
== -1 ) start
= VG_MAX( start
-1, 0 );
1373 /* Still no selction, no need to do anything */
1377 /* Copy the end->terminator to start */
1378 int remaining_length
= strlen( vg_ui
.textbuf
)+1-end
;
1379 memmove( &vg_ui
.textbuf
[ start
],
1380 &vg_ui
.textbuf
[ end
],
1385 static void _ui_textbox_to_clipboard(void){
1387 _ui_textbox_make_selection( &start
, &end
);
1391 memcpy( buffer
, &vg_ui
.textbuf
[ start
], end
-start
);
1392 buffer
[ end
-start
] = 0x00;
1393 SDL_SetClipboardText( buffer
);
1397 static void ui_start_modal( const char *message
, u32 options
);
1398 static void _ui_textbox_clipboard_paste(void){
1399 if( !SDL_HasClipboardText() )
1402 char *text
= SDL_GetClipboardText();
1407 int datastart
= _ui_textbox_delete_char( 0 );
1408 int length
= strlen( text
);
1410 if( (vg_ui
.textbox
.len
- strlen(vg_ui
.textbuf
)) < length
){
1411 ui_start_modal( "Clipboard content exceeds buffer size.", UI_MODAL_BAD
);
1415 int cpylength
= _ui_textbox_makeroom( datastart
, length
);
1417 memcpy( vg_ui
.textbuf
+ datastart
, text
, cpylength
);
1418 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
,
1419 &vg_ui
.textbox
.cursor_pos
, cpylength
, 1 );
1423 static void _ui_textbox_put_char( char c
){
1424 vg_ui
.textbox
.cursor_user
= _ui_textbox_delete_char(0);
1425 if( (vg_ui
.textbox
.len
- strlen(vg_ui
.textbuf
)) <= 1 ) return;
1427 if( _ui_textbox_makeroom( vg_ui
.textbox
.cursor_user
, 1 ) )
1428 vg_ui
.textbuf
[ vg_ui
.textbox
.cursor_user
] = c
;
1430 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
,
1431 &vg_ui
.textbox
.cursor_pos
, 1, 1 );
1434 /* Receed secondary cursor */
1435 static void _ui_textbox_left_select(void){
1436 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
, NULL
, -1, 0 );
1439 /* Match and receed both cursors */
1440 static void _ui_textbox_left(void){
1441 int cursor_diff
= vg_ui
.textbox
.cursor_pos
- vg_ui
.textbox
.cursor_user
? 0: 1;
1443 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
,
1444 &vg_ui
.textbox
.cursor_pos
, -cursor_diff
, 1 );
1447 static void _ui_textbox_up(void){
1448 if( vg_ui
.textbox
.flags
& UI_TEXTBOX_MULTILINE
){
1449 int line_begin
= vg_ui
.textbox
.cursor_user
;
1451 while( line_begin
){
1452 if( vg_ui
.textbuf
[ line_begin
-1 ] == '\n' ){
1460 int line_above_begin
= line_begin
-1;
1462 while( line_above_begin
){
1463 if( vg_ui
.textbuf
[ line_above_begin
-1 ] == '\n' ){
1467 line_above_begin
--;
1470 int offset
= vg_ui
.textbox
.cursor_user
- line_begin
,
1471 line_length_above
= line_begin
- line_above_begin
-1;
1473 offset
= VG_MIN( line_length_above
, offset
);
1475 vg_ui
.textbox
.cursor_user
= line_above_begin
+offset
;
1476 vg_ui
.textbox
.cursor_pos
= line_above_begin
+offset
;
1479 vg_ui
.textbox
.cursor_user
= line_begin
;
1480 vg_ui
.textbox
.cursor_pos
= line_begin
;
1484 if( vg_ui
.textbox
.callbacks
.up
){
1485 vg_ui
.textbox
.callbacks
.up( vg_ui
.textbuf
, vg_ui
.textbox
.len
);
1490 static void _ui_textbox_down(void){
1491 if( vg_ui
.textbox
.flags
& UI_TEXTBOX_MULTILINE
){
1492 int line_begin
= vg_ui
.textbox
.cursor_user
;
1494 while( line_begin
){
1495 if( vg_ui
.textbuf
[ line_begin
-1 ] == '\n' ){
1502 int line_below_begin
= vg_ui
.textbox
.cursor_user
;
1505 if( vg_ui
.textbuf
[ line_below_begin
] == '\0' ){
1506 vg_ui
.textbox
.cursor_user
= line_below_begin
;
1507 vg_ui
.textbox
.cursor_pos
= line_below_begin
;
1511 if( vg_ui
.textbuf
[ line_below_begin
] == '\n' ){
1512 line_below_begin
++;
1516 line_below_begin
++;
1519 int line_below_end
= line_below_begin
;
1521 if( vg_ui
.textbuf
[ line_below_end
] == '\0' ||
1522 vg_ui
.textbuf
[ line_below_end
] == '\n' ){
1529 int offset
= vg_ui
.textbox
.cursor_user
- line_begin
,
1530 line_length_below
= line_below_end
- line_below_begin
-1;
1532 offset
= VG_MIN( line_length_below
, offset
);
1534 vg_ui
.textbox
.cursor_user
= line_below_begin
+offset
;
1535 vg_ui
.textbox
.cursor_pos
= line_below_begin
+offset
;
1538 if( vg_ui
.textbox
.callbacks
.down
){
1539 vg_ui
.textbox
.callbacks
.down( vg_ui
.textbuf
, vg_ui
.textbox
.len
);
1544 static void _ui_textbox_right_select(void){
1545 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
, NULL
, 1, 0 );
1548 static void _ui_textbox_right(void){
1549 int cursor_diff
= vg_ui
.textbox
.cursor_pos
- vg_ui
.textbox
.cursor_user
? 0: 1;
1551 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
,
1552 &vg_ui
.textbox
.cursor_pos
, +cursor_diff
, 1 );
1555 static void _ui_textbox_backspace(void){
1556 if( vg_ui
.focused_control_type
== k_ui_control_textbox
){
1557 vg_ui
.textbox
.cursor_user
= _ui_textbox_delete_char( -1 );
1558 vg_ui
.textbox
.cursor_pos
= vg_ui
.textbox
.cursor_user
;
1560 if( vg_ui
.textbox
.callbacks
.change
){
1561 vg_ui
.textbox
.callbacks
.change( vg_ui
.textbuf
, vg_ui
.textbox
.len
);
1566 static void _ui_textbox_delete(void){
1567 if( vg_ui
.focused_control_type
== k_ui_control_textbox
){
1568 vg_ui
.textbox
.cursor_user
= _ui_textbox_delete_char( 1 );
1569 vg_ui
.textbox
.cursor_pos
= vg_ui
.textbox
.cursor_user
;
1571 if( vg_ui
.textbox
.callbacks
.change
){
1572 vg_ui
.textbox
.callbacks
.change( vg_ui
.textbuf
, vg_ui
.textbox
.len
);
1577 static void _ui_textbox_home_select(void){
1578 i32 start
= vg_ui
.textbox
.cursor_user
;
1580 if( vg_ui
.textbox
.flags
& UI_TEXTBOX_MULTILINE
){
1582 if( vg_ui
.textbuf
[start
-1] == '\n' )
1591 vg_ui
.textbox
.cursor_user
= start
;
1594 static void _ui_textbox_home(void){
1595 _ui_textbox_home_select();
1596 vg_ui
.textbox
.cursor_pos
= vg_ui
.textbox
.cursor_user
;
1599 static void _ui_textbox_end_select(void){
1600 i32 end
= vg_ui
.textbox
.cursor_user
;
1602 if( vg_ui
.textbox
.flags
& UI_TEXTBOX_MULTILINE
){
1603 while( vg_ui
.textbuf
[end
] ){
1604 if( vg_ui
.textbuf
[end
] == '\n' )
1611 end
= VG_MIN( vg_ui
.textbox
.len
-1, strlen(vg_ui
.textbuf
) );
1613 vg_ui
.textbox
.cursor_user
= end
;
1616 static void _ui_textbox_end(void){
1617 _ui_textbox_end_select();
1618 vg_ui
.textbox
.cursor_pos
= vg_ui
.textbox
.cursor_user
;
1621 static void _ui_textbox_select_all(void){
1622 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
, NULL
, 10000, 0);
1623 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_pos
, NULL
, -10000, 0);
1626 static void _ui_textbox_cut(void){
1627 _ui_textbox_to_clipboard();
1628 vg_ui
.textbox
.cursor_user
= _ui_textbox_delete_char(0);
1629 vg_ui
.textbox
.cursor_pos
= vg_ui
.textbox
.cursor_user
;
1632 static void _ui_textbox_enter(void){
1633 if( vg_ui
.focused_control_type
== k_ui_control_textbox
){
1634 vg_ui
.ignore_input_frames
= 2;
1636 if( vg_ui
.textbox
.callbacks
.enter
)
1637 vg_ui
.textbox
.callbacks
.enter( vg_ui
.textbuf
, vg_ui
.textbox
.len
);
1639 if( vg_ui
.focused_control_type
!= k_ui_control_textbox
) return;
1641 if( vg_ui
.textbox
.flags
& UI_TEXTBOX_MULTILINE
)
1642 _ui_textbox_put_char( '\n' );
1644 if( !(vg_ui
.textbox
.flags
& UI_TEXTBOX_AUTOFOCUS
) )
1651 * based on a visual character coordinate relative to the anchor of the textbox,
1652 * this works out the linear place in the buffer that coordinate maps to
1654 * input coordinates go in co[0], co[1], and the result index is in co[2]
1656 static void _ui_textbox_calc_index_from_grid( int co
[3], int wrap_length
){
1660 while( (c
= vg_ui
.textbuf
[i
[2]]) ){
1661 if( i
[1]==co
[1] && i
[0]>=co
[0] ) break;
1663 if( i
[0] >= wrap_length
){
1668 if( c
>= 32 && c
<= 126 ){
1672 else if( c
== '\n' ){
1675 if( i
[1] > co
[1] ) break;
1689 * based on the index specied in co[2], work out the visual character
1690 * coordinates and store them in co[0], co[1]
1692 static void _ui_textbox_index_calc_coords( int co
[3], int wrap_length
){
1699 while( (c
= vg_ui
.textbuf
[i
++]) ){
1700 if( i
> co
[2] ) break;
1701 if( co
[0] >= wrap_length
){
1705 if( c
>= 32 && c
<= 126 ) co
[0] ++;
1706 else if( c
== '\n' ){
1714 * calculate the number of characters remaining until either:
1715 * - the wrap_length limit is hit
1716 * - end of the line/string
1718 * index must be fully populated with visual X/Y, and linear index
1720 static int _ui_textbox_run_remaining( int index
[3], int wrap_length
){
1721 int i
=0, printed_chars
=0;
1723 while( (c
= vg_ui
.textbuf
[index
[2] + (i
++)]) ){
1724 if( index
[0]+i
>= wrap_length
) break;
1725 if( c
>= 32 && c
<= 126 ) printed_chars
++;
1726 else if( c
== '\n' ) break;
1729 return printed_chars
+1;
1732 static int ui_textbox( ui_rect inout_panel
, const char *label
,
1733 char *buf
, u32 len
, u32 lines
, u32 flags
,
1734 struct ui_textbox_callbacks
*callbacks
){
1735 assert( lines
> 0 );
1738 if( lines
> 1 ) flags
|= UI_TEXTBOX_MULTILINE
;
1741 ui_standard_widget( inout_panel
, rect
, lines
);
1744 ui_label( rect
, label
, 1, 0, rect
);
1746 int clickup
= ui_click_up(UI_MOUSE_LEFT
),
1747 clickdown
= ui_click_down(UI_MOUSE_LEFT
),
1748 click
= ui_clicking(UI_MOUSE_LEFT
) | clickup
,
1749 target
= ui_inside_rect( rect
, vg_ui
.mouse_click
) && click
,
1750 hover
= ui_inside_rect( rect
, vg_ui
.mouse
);
1752 /* allow instant transitions from textbox->textbox */
1753 if( (vg_ui
.focused_control_type
!= k_ui_control_none
) &&
1754 (vg_ui
.focused_control_type
!= k_ui_control_textbox
) ){
1760 flags
&= ~UI_TEXTBOX_AUTOFOCUS
;
1763 u32 col_base
= ui_colour( k_ui_bg
),
1764 col_highlight
= ui_colour( k_ui_fg
),
1765 col_cursor
= (0x00ffffff & ui_colour(k_ui_fg
))|0x7f000000;
1770 rect_copy( rect
, text_rect
);
1772 if( flags
& UI_TEXTBOX_MULTILINE
) text_rect
[3] = rect
[3]-16;
1773 else text_rect
[3] = vg_ui
.font
->line_height
;
1776 ui_rect_center( rect
, text_rect
);
1778 ui_px wrap_length
= 1024;
1780 if( flags
& UI_TEXTBOX_WRAP
)
1781 wrap_length
= text_rect
[2] / vg_ui
.font
->spacing
;
1784 vg_ui
.cursor
= k_ui_cursor_ibeam
;
1787 if( vg_ui
.focused_control_id
== buf
){
1788 ui_fill( rect
, col_base
);
1789 ui_ntext( text_rect
, buf
, wrap_length
, 1, k_ui_align_left
, 0 );
1791 if( !(flags
& UI_TEXTBOX_AUTOFOCUS
) && ((clickup
||clickdown
) && !target
)){
1795 vg_ui
.focused_control_hit
= 1;
1796 if( click
&& target
){
1798 (vg_ui
.mouse_click
[0] - text_rect
[0]) / vg_ui
.font
->spacing
,
1799 (vg_ui
.mouse_click
[1] - text_rect
[1]) / vg_ui
.font
->line_height
,
1803 (vg_ui
.mouse
[0] - text_rect
[0]) / vg_ui
.font
->spacing
,
1804 (vg_ui
.mouse
[1] - text_rect
[1]) / vg_ui
.font
->line_height
,
1808 if( flags
& UI_TEXTBOX_MULTILINE
){
1809 _ui_textbox_calc_index_from_grid( p0
, wrap_length
);
1810 _ui_textbox_calc_index_from_grid( p1
, wrap_length
);
1812 vg_ui
.textbox
.cursor_pos
= p0
[2];
1813 vg_ui
.textbox
.cursor_user
= p1
[2];
1816 int max
= strlen( buf
);
1817 vg_ui
.textbox
.cursor_pos
= VG_MAX( 0, VG_MIN( max
, p0
[0] )),
1818 vg_ui
.textbox
.cursor_user
= VG_MAX( 0, VG_MIN( max
, p1
[0] ));
1822 ui_outline( rect
, -2, vg_ui
.scheme
[ k_ui_orange
], 0 );
1826 int c0
= vg_ui
.textbox
.cursor_pos
,
1827 c1
= vg_ui
.textbox
.cursor_user
,
1828 start
= VG_MIN( c0
, c1
),
1829 end
= VG_MAX( c0
, c1
),
1832 if( flags
& (UI_TEXTBOX_WRAP
|UI_TEXTBOX_MULTILINE
) ){
1833 int pos
[3], remaining
= chars
;
1836 _ui_textbox_index_calc_coords( pos
, wrap_length
);
1839 cursor
[0] = text_rect
[0] + pos
[0]*vg_ui
.font
->spacing
-1;
1840 cursor
[1] = text_rect
[1] + pos
[1]*14;
1843 ui_fill( cursor
, col_cursor
);
1844 rect_copy( cursor
, vg_ui
.click_fader_end
);
1848 int run
= _ui_textbox_run_remaining( pos
, wrap_length
);
1849 run
= VG_MIN( run
, remaining
);
1851 cursor
[0] = text_rect
[0] + pos
[0]*vg_ui
.font
->spacing
-1;
1852 cursor
[1] = text_rect
[1] + pos
[1]*14;
1853 cursor
[2] = (float)(run
)*(float)vg_ui
.font
->spacing
;
1856 ui_fill( cursor
, col_cursor
);
1863 rect_copy( cursor
, vg_ui
.click_fader_end
);
1867 cursor
[0] = text_rect
[0] + start
*vg_ui
.font
->spacing
-1;
1868 cursor
[1] = text_rect
[1];
1875 cursor
[2] = (float)(chars
)*(float)vg_ui
.font
->spacing
;
1878 if( (vg_ui
.click_fade_opacity
<=0.0f
) &&
1879 ui_clip( rect
, cursor
, cursor
) ){
1880 ui_fill( cursor
, col_cursor
);
1883 rect_copy( cursor
, vg_ui
.click_fader_end
);
1890 if( click
|| (flags
& UI_TEXTBOX_AUTOFOCUS
) ){
1891 if( (target
&& hover
) || (flags
& UI_TEXTBOX_AUTOFOCUS
) ){
1894 ui_fill( rect
, col_highlight
);
1895 vg_ui
.ignore_input_frames
= 2;
1896 rect_copy( rect
, vg_ui
.click_fader
);
1897 rect_copy( rect
, vg_ui
.click_fader_end
);
1899 vg_ui
.click_fade_opacity
= 1.0f
;
1900 vg_ui
.textbuf
= buf
;
1901 vg_ui
.focused_control_hit
= 1;
1902 vg_ui
.focused_control_type
= k_ui_control_textbox
;
1903 vg_ui
.textbox
.len
= len
;
1904 vg_ui
.textbox
.flags
= flags
;
1905 vg_ui
.textbox
.cursor_pos
= 0;
1906 vg_ui
.textbox
.cursor_user
= 0;
1909 vg_ui
.textbox
.callbacks
= *callbacks
;
1912 vg_ui
.textbox
.callbacks
.change
= NULL
;
1913 vg_ui
.textbox
.callbacks
.down
= NULL
;
1914 vg_ui
.textbox
.callbacks
.up
= NULL
;
1915 vg_ui
.textbox
.callbacks
.enter
= NULL
;
1918 SDL_StartTextInput();
1922 ui_fill( rect
, col_base
);
1925 ui_outline( rect
, -1, col_highlight
, 0 );
1928 ui_ntext( text_rect
, buf
, wrap_length
, 1, k_ui_align_left
, 0 );
1934 * -----------------------------------------------------------------------------
1937 static void ui_tabs( ui_rect inout_panel
, ui_rect out_content_panel
,
1938 const char **titles
, u32 count
, i32
*page
){
1940 ui_standard_widget( inout_panel
, bar
, 1 );
1942 i32 cur_page
= *page
;
1944 f32 width
= (f32
)inout_panel
[2] / (f32
)count
;
1946 ui_px h
= (inout_panel
[1] + inout_panel
[3]) - (bar
[1]+bar
[3]);
1947 inout_panel
[1] = bar
[1]+bar
[3];
1950 ui_fill( inout_panel
, ui_colour( k_ui_bg
+2 ) );
1951 ui_outline( inout_panel
, 1, ui_colour( k_ui_bg
+5 ), 0 );
1953 rect_copy( inout_panel
, out_content_panel
);
1954 ui_rect_pad( out_content_panel
, (ui_px
[2]){ k_ui_padding
, k_ui_padding
} );
1957 for( i32 i
=0; i
<count
; i
++ ){
1959 bar
[0] + ((f32
)i
*width
),
1965 enum ui_scheme_colour colour
= k_ui_bg
+4;
1966 if( i
== cur_page
){
1968 ui_outline( button
, 1, ui_colour( k_ui_bg
+5 ),
1969 UI_TOP
|UI_LEFT
|UI_RIGHT
);
1973 if( ui_colourbutton_text( button
, titles
[i
], 1, colour
) == 1 )
1980 * -----------------------------------------------------------------------------
1982 static void ui_start_modal( const char *message
, u32 options
){
1983 vg_ui
.focused_control_type
= k_ui_control_modal
;
1984 vg_ui
.modal
.message
= message
;
1985 vg_ui
.modal
.callbacks
.close
= NULL
;
1986 vg_ui
.modal
.options
= options
;
1988 u32 type
= options
& UI_MODAL_TYPE_BITS
;
1989 if( type
== UI_MODAL_OK
) vg_info( message
);
1990 else if( type
== UI_MODAL_WARN
) vg_warn( message
);
1991 else if( type
== UI_MODAL_GOOD
) vg_success( message
);
1992 else if( type
== UI_MODAL_BAD
) vg_error( message
);
1997 * -----------------------------------------------------------------------------
2003 static void _ui_proc_key( SDL_Keysym ev
){
2004 if( vg_ui
.focused_control_type
!= k_ui_control_textbox
){
2008 struct textbox_mapping
{
2012 void (*handler
)(void);
2016 { 0, SDLK_LEFT
, _ui_textbox_left
},
2017 { KMOD_SHIFT
, SDLK_LEFT
, _ui_textbox_left_select
},
2018 { 0, SDLK_RIGHT
, _ui_textbox_right
},
2019 { KMOD_SHIFT
, SDLK_RIGHT
, _ui_textbox_right_select
},
2020 { 0, SDLK_DOWN
, _ui_textbox_down
},
2021 { 0, SDLK_UP
, _ui_textbox_up
},
2022 { 0, SDLK_BACKSPACE
, _ui_textbox_backspace
},
2023 { KMOD_SHIFT
, SDLK_BACKSPACE
, _ui_textbox_backspace
},
2024 { KMOD_CTRL
, SDLK_BACKSPACE
, _ui_textbox_backspace
},
2025 { 0, SDLK_DELETE
, _ui_textbox_delete
},
2026 { 0, SDLK_HOME
, _ui_textbox_home
},
2027 { KMOD_SHIFT
, SDLK_HOME
, _ui_textbox_home_select
},
2028 { 0, SDLK_END
, _ui_textbox_end
},
2029 { KMOD_SHIFT
, SDLK_END
, _ui_textbox_end_select
},
2030 { KMOD_CTRL
, SDLK_a
, _ui_textbox_select_all
},
2031 { KMOD_CTRL
, SDLK_c
, _ui_textbox_to_clipboard
},
2032 { KMOD_CTRL
, SDLK_x
, _ui_textbox_cut
},
2033 { KMOD_CTRL
, SDLK_v
, _ui_textbox_clipboard_paste
},
2034 { 0, SDLK_RETURN
, _ui_textbox_enter
},
2035 { 0, SDLK_ESCAPE
, ui_defocus_all
},
2040 if( ev
.mod
& KMOD_SHIFT
)
2043 if( ev
.mod
& KMOD_CTRL
)
2046 if( ev
.mod
& KMOD_ALT
)
2049 for( int i
=0; i
<vg_list_size( mappings
); i
++ ){
2050 struct textbox_mapping
*mapping
= &mappings
[i
];
2052 if( mapping
->key
== ev
.sym
){
2053 if( mapping
->mod
== 0 ){
2059 else if( (mod
& mapping
->mod
) == mapping
->mod
){
2068 * Callback for text entry mode
2070 static void ui_proc_utf8( const char *text
){
2071 if( vg_ui
.focused_control_type
== k_ui_control_textbox
){
2072 const char *ptr
= text
;
2075 if( *ptr
!= '`' ) _ui_textbox_put_char( *ptr
);
2079 if( vg_ui
.textbox
.callbacks
.change
){
2080 vg_ui
.textbox
.callbacks
.change( vg_ui
.textbuf
, vg_ui
.textbox
.len
);
2087 * -----------------------------------------------------------------------------
2090 static void ui_dev_colourview(void){
2091 ui_rect window
= {vg
.window_x
-256,0,256,vg
.window_y
}, swatch
;
2093 const char *names
[vg_list_size(vg_ui
.scheme
)] = {
2094 [k_ui_bg
] = "k_ui_bg", "k_ui_bg+1", "k_ui_bg+2", "k_ui_bg+3",
2095 "k_ui_bg+4", "k_ui_bg+5", "k_ui_bg+6", "k_ui_bg+7",
2097 [k_ui_fg
] = "k_ui_fg", "k_ui_fg+1", "k_ui_fg+2", "k_ui_fg+3",
2098 "k_ui_fg+4", "k_ui_fg+5", "k_ui_fg+6", "k_ui_fg+7",
2100 [k_ui_red
] = "k_ui_red", "k_ui_orange", "k_ui_yellow", "k_ui_green",
2101 "k_ui_aqua", "k_ui_blue", "k_ui_purple", "k_ui_gray",
2102 "k_ui_red+8","k_ui_orange+8","k_ui_yellow+8","k_ui_green+8",
2103 "k_ui_aqua+8","k_ui_blue+8","k_ui_purple+8","k_ui_gray+8" };
2106 ui_split_ratio( window
, k_ui_axis_v
, 0.5f
, 0, col
[0], col
[1] );
2108 for( int i
=0; i
<vg_list_size(vg_ui
.scheme
); i
++ ){
2109 int which
= (i
/8)%2;
2111 ui_split( col
[which
], k_ui_axis_h
, 24, 0, swatch
, col
[which
] );
2112 ui_fill( swatch
, ui_colour(i
) );
2115 ui_text(swatch
, names
[i
], 1, k_ui_align_middle_left
, ui_colourcont(i
));
2119 #endif /* VG_IMGUI_H */