1 /* Copyright (C) 2021-2023 Harry Godden (hgn) - All Rights Reserved */
6 * 1. layout is defined by subdividing
7 * 2. a parent node should never be resized by the content after creation
8 * 3. when the ui is in an interactive state, no controls should ever move
9 * 4. controls directly reference a memory location and use that as their
11 * 5. a maximum of ONE control per memory location can be drawn at any given
20 #include "vg/vg_tex.h"
21 #include "vg/vg_shader.h"
24 typedef ui_px ui_rect
[4];
25 typedef struct ui_vert ui_vert
;
32 /* Relative to cursor p0 */
35 k_ui_align_lwr
= 0xff,
36 k_ui_align_left
= 0x0000| 0x00,
37 k_ui_align_right
= 0x0000| 0x01,
38 k_ui_align_center
= 0x0000| 0x02,
40 k_ui_align_middle
= 0x0100,
41 k_ui_align_middle_left
= 0x0100| 0x00,
42 k_ui_align_middle_right
= 0x0100| 0x01,
43 k_ui_align_middle_center
= 0x0100| 0x02,
45 k_ui_align_bottom
= 0x0200,
46 k_ui_align_bottom_left
= 0x0200| 0x00,
47 k_ui_align_bottom_right
= 0x0200| 0x01,
48 k_ui_align_bottom_center
= 0x0200| 0x02,
60 enum ui_scheme_colour
{
75 typedef u32 ui_scheme
[8*4];
77 #define UI_RGB( STDHEX ) 0xff000000 |\
78 ((STDHEX&0x000000ff)<<16) |\
79 ((STDHEX&0x0000ff00) ) |\
80 ((STDHEX&0x00ff0000)>>16)
82 #define UI_TEXTBOX_MULTILINE 0x1
83 #define UI_TEXTBOX_WRAP 0x2
84 #define UI_TEXTBOX_AUTOFOCUS 0x4
86 #define UI_MODAL_OK 0x0
87 #define UI_MODAL_GOOD 0x1
88 #define UI_MODAL_BAD 0x2
89 #define UI_MODAL_WARN 0x3
90 #define UI_MODAL_TYPE_BITS 0x3
92 #define UI_MOUSE_LEFT (SDL_BUTTON(SDL_BUTTON_LEFT))
93 #define UI_MOUSE_RIGHT (SDL_BUTTON(SDL_BUTTON_RIGHT))
94 #define UI_MOUSE_MIDDLE (SDL_BUTTON(SDL_BUTTON_MIDDLE))
97 struct ui_vert
*vertex_buffer
;
99 u32 max_verts
, max_indices
,
100 cur_vert
, cur_indice
,
101 vert_start
, indice_start
;
104 void *focused_control_id
; /* uses the memory location of various locking
110 u32 focused_control_hit
;
111 enum ui_control_type
{
113 k_ui_control_textbox
,
117 focused_control_type
;
119 union{ /* controls that can be focused */
121 int cursor_user
, cursor_pos
;
125 struct ui_textbox_callbacks
{
126 void (*enter
)( char *, u32
),
127 (*up
)( char *, u32
),
128 (*down
)( char *, u32
),
129 (*change
)( char *, u32
);
151 struct ui_modal_callbacks
{
158 GLuint tex_glyphs
, vao
, vbo
, ebo
;
160 ui_px mouse
[2], mouse_click
[2];
162 u32 ignore_input_frames
;
165 ui_rect click_fader
, click_fader_end
;
166 float click_fade_opacity
;
178 SDL_Cursor
*cursor_map
[ k_ui_cursor_max
];
182 [ k_ui_bg
+0 ] = UI_RGB( 0x1d2021 ),
183 [ k_ui_bg
+1 ] = UI_RGB( 0x282828 ),
184 [ k_ui_bg
+2 ] = UI_RGB( 0x3c3836 ),
185 [ k_ui_bg
+3 ] = UI_RGB( 0x504945 ),
186 [ k_ui_bg
+4 ] = UI_RGB( 0x665c54 ),
187 [ k_ui_bg
+5 ] = UI_RGB( 0x7c6f64 ),
188 [ k_ui_bg
+6 ] = UI_RGB( 0x928374 ),
189 [ k_ui_bg
+7 ] = UI_RGB( 0xa89984 ),
191 [ k_ui_fg
+0 ] = UI_RGB( 0xebdbb2 ),
192 [ k_ui_fg
+1 ] = UI_RGB( 0xfbf1c7 ),
193 [ k_ui_fg
+2 ] = UI_RGB( 0xd5c4a1 ),
194 [ k_ui_fg
+3 ] = UI_RGB( 0xbdae93 ),
195 [ k_ui_fg
+4 ] = UI_RGB( 0xa89984 ),
196 [ k_ui_fg
+5 ] = UI_RGB( 0x000000 ),
197 [ k_ui_fg
+6 ] = UI_RGB( 0x000000 ),
198 [ k_ui_fg
+7 ] = UI_RGB( 0x000000 ),
200 [ k_ui_red
] = UI_RGB( 0xcc241d ),
201 [ k_ui_orange
] = UI_RGB( 0xd65d0e ),
202 [ k_ui_yellow
] = UI_RGB( 0xd79921 ),
203 [ k_ui_green
] = UI_RGB( 0x98971a ),
204 [ k_ui_aqua
] = UI_RGB( 0x689d6a ),
205 [ k_ui_blue
] = UI_RGB( 0x458588 ),
206 [ k_ui_purple
] = UI_RGB( 0xb16286 ),
207 [ k_ui_gray
] = UI_RGB( 0x928374 ),
208 [ k_ui_red
+ k_ui_brighter
] = UI_RGB( 0xfb4934 ),
209 [ k_ui_orange
+ k_ui_brighter
] = UI_RGB( 0xfe8019 ),
210 [ k_ui_yellow
+ k_ui_brighter
] = UI_RGB( 0xfabd2f ),
211 [ k_ui_green
+ k_ui_brighter
] = UI_RGB( 0xb8bb26 ),
212 [ k_ui_aqua
+ k_ui_brighter
] = UI_RGB( 0x8ec07c ),
213 [ k_ui_blue
+ k_ui_brighter
] = UI_RGB( 0x83a598 ),
214 [ k_ui_purple
+ k_ui_brighter
] = UI_RGB( 0xd3869b ),
215 [ k_ui_gray
+ k_ui_brighter
] = UI_RGB( 0xa89984 ),
219 static struct vg_shader _shader_ui
=
227 "layout (location=0) in vec2 a_co;"
228 "layout (location=1) in vec2 a_uv;"
229 "layout (location=2) in vec4 a_colour;"
232 "out vec2 aTexCoords;"
238 "gl_Position = vec4( uPv * vec3( a_co, 1.0 ), 1.0 );"
239 "aTexCoords = a_uv * 0.0078125;"
240 "aColour = a_colour;"
249 "uniform sampler2D uTexGlyphs;"
250 "out vec4 FragColor;"
252 "in vec2 aTexCoords;"
257 "vec2 rand_hash22( vec2 p )"
259 "vec3 p3 = fract(vec3(p.xyx) * 213.8976123);"
260 "p3 += dot(p3, p3.yzx+19.19);"
261 "return fract(vec2((p3.x + p3.y)*p3.z, (p3.x+p3.z)*p3.y));"
266 "vec4 diffuse = aColour;"
268 "if( diffuse.a == 0.0 )"
270 "diffuse.a = texture( uTexGlyphs, aTexCoords ).r;"
273 "FragColor = diffuse;"
278 static struct vg_shader _shader_ui_image
=
280 .name
= "[vg] ui_image",
286 "layout (location=0) in vec2 a_co;"
287 "layout (location=1) in vec2 a_uv;"
288 "layout (location=2) in vec4 a_colour;"
291 "out vec2 aTexCoords;"
297 "gl_Position = vec4( uPv * vec3( a_co, 1.0 ), 1.0 );"
298 "aTexCoords = a_uv * 0.0078125;"
299 "aColour = a_colour;"
308 "uniform sampler2D uTexImage;"
309 "out vec4 FragColor;"
311 "in vec2 aTexCoords;"
317 "vec4 colour = texture( uTexImage, aTexCoords );"
318 "FragColor = colour;"
322 #define UI_GLYPH_SPACING_X 8
324 VG_STATIC
void _vg_ui_init(void)
326 if( !vg_shader_compile( &_shader_ui
) ||
327 !vg_shader_compile( &_shader_ui_image
) )
328 vg_fatal_error( "Failed to compile ui shader" );
332 * ----------------------------------------
335 vg_ui
.max_indices
= 20000;
336 vg_ui
.max_verts
= 30000;
338 /* Generate the buffer we are gonna be drawing to */
339 glGenVertexArrays( 1, &vg_ui
.vao
);
340 glGenBuffers( 1, &vg_ui
.vbo
);
341 glGenBuffers( 1, &vg_ui
.ebo
);
343 glBindVertexArray( vg_ui
.vao
);
344 glBindBuffer( GL_ARRAY_BUFFER
, vg_ui
.vbo
);
346 glBufferData( GL_ARRAY_BUFFER
,
347 vg_ui
.max_verts
* sizeof( struct ui_vert
),
348 NULL
, GL_DYNAMIC_DRAW
);
349 glBindVertexArray( vg_ui
.vao
);
351 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, vg_ui
.ebo
);
352 glBufferData( GL_ELEMENT_ARRAY_BUFFER
,
353 vg_ui
.max_indices
* sizeof( u16
), NULL
, GL_DYNAMIC_DRAW
);
358 u32
const stride
= sizeof( struct ui_vert
);
361 glVertexAttribPointer( 0, 2, GL_SHORT
, GL_FALSE
, stride
,
362 (void *)offsetof( struct ui_vert
, co
) );
363 glEnableVertexAttribArray( 0 );
366 glVertexAttribPointer( 1, 2, GL_UNSIGNED_BYTE
, GL_FALSE
, stride
,
367 (void *)offsetof( struct ui_vert
, uv
) );
368 glEnableVertexAttribArray( 1 );
371 glVertexAttribPointer( 2, 4, GL_UNSIGNED_BYTE
, GL_TRUE
, stride
,
372 (void *)offsetof( struct ui_vert
, colour
) );
373 glEnableVertexAttribArray( 2 );
377 /* Alloc RAM default context */
378 u32 vert_size
= vg_ui
.max_verts
*sizeof(struct ui_vert
),
379 inds_size
= vg_align8( vg_ui
.max_indices
*sizeof(u16
) );
381 vg_ui
.vertex_buffer
= vg_linear_alloc( vg_mem
.rtmemory
, vert_size
);
382 vg_ui
.indice_buffer
= vg_linear_alloc( vg_mem
.rtmemory
, inds_size
);
385 * -----------------------------------------------------
388 /* Load default font */
390 #include "vg/vg_pxfont_thin.h"
393 u32 pixels
= 0, total
= 256*256, data
= 0;
396 while( pixels
< total
){
397 for( int b
= 31; b
>= 0; b
-- ){
398 image
[ pixels
++ ] = (compressed
[data
] & (0x1u
<< b
))? 0xffu
: 0x00u
;
400 if( pixels
>= total
){
408 glGenTextures( 1, &vg_ui
.tex_glyphs
);
409 glBindTexture( GL_TEXTURE_2D
, vg_ui
.tex_glyphs
);
410 glTexImage2D( GL_TEXTURE_2D
, 0, GL_R8
, 256, 256, 0,
411 GL_RED
, GL_UNSIGNED_BYTE
, image
);
414 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
415 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
416 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
417 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
421 * ---------------------------------------------------------------
424 vg_ui
.cursor_map
[ k_ui_cursor_default
] =
425 SDL_CreateSystemCursor( SDL_SYSTEM_CURSOR_ARROW
);
426 vg_ui
.cursor_map
[ k_ui_cursor_hand
] =
427 SDL_CreateSystemCursor( SDL_SYSTEM_CURSOR_HAND
);
428 vg_ui
.cursor_map
[ k_ui_cursor_ibeam
] =
429 SDL_CreateSystemCursor( SDL_SYSTEM_CURSOR_IBEAM
);
437 static void rect_copy( ui_rect a
, ui_rect b
)
439 for( int i
=0; i
<4; i
++ )
443 VG_STATIC
void ui_flush( enum ui_shader shader
)
445 u32 vertex_offset
= vg_ui
.vert_start
*sizeof(ui_vert
),
446 vertex_count
= vg_ui
.cur_vert
-vg_ui
.vert_start
,
447 vertex_size
= vertex_count
*sizeof(ui_vert
),
449 indice_offset
= vg_ui
.indice_start
*sizeof(u16
),
450 indice_count
= vg_ui
.cur_indice
-vg_ui
.indice_start
,
451 indice_size
= indice_count
* sizeof(u16
);
453 if( !vertex_size
|| !indice_size
)
456 glBindVertexArray( vg_ui
.vao
);
457 glBindBuffer( GL_ARRAY_BUFFER
, vg_ui
.vbo
);
458 glBufferSubData( GL_ARRAY_BUFFER
, vertex_offset
, vertex_size
,
459 vg_ui
.vertex_buffer
+vg_ui
.vert_start
);
461 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, vg_ui
.ebo
);
462 glBufferSubData( GL_ELEMENT_ARRAY_BUFFER
, indice_offset
, indice_size
,
463 vg_ui
.indice_buffer
+vg_ui
.indice_start
);
465 glDisable( GL_DEPTH_TEST
);
466 glEnable( GL_BLEND
);
467 glBlendFunc( GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
468 glBlendEquation( GL_FUNC_ADD
);
469 glDisable( GL_CULL_FACE
);
471 m3x3f view
= M3X3_IDENTITY
;
472 m3x3_translate( view
, (v3f
){ -1.0f
, 1.0f
, 0.0f
} );
473 m3x3_scale( view
, (v3f
){ 1.0f
/((float)vg
.window_x
*0.5f
),
474 -1.0f
/((float)vg
.window_y
*0.5f
), 1.0f
} );
476 if( shader
== k_ui_shader_colour
){
477 glUseProgram( _shader_ui
.id
);
479 glUniformMatrix3fv( glGetUniformLocation( _shader_ui
.id
, "uPv" ), 1,
480 GL_FALSE
, (float *)view
);
482 glActiveTexture( GL_TEXTURE0
);
483 glBindTexture( GL_TEXTURE_2D
, vg_ui
.tex_glyphs
);
484 glUniform1i( glGetUniformLocation( _shader_ui
.id
, "uTexGlyphs" ), 0 );
486 else if( shader
== k_ui_shader_image
){
487 glUseProgram( _shader_ui_image
.id
);
488 glUniformMatrix3fv( glGetUniformLocation( _shader_ui_image
.id
, "uPv" ), 1,
489 GL_FALSE
, (float *)view
);
490 glUniform1i( glGetUniformLocation(_shader_ui_image
.id
,"uTexImage"), 0 );
493 vg_fatal_error( "Invalid UI shader (%d)\n", shader
);
495 glDrawElements( GL_TRIANGLES
, indice_count
, GL_UNSIGNED_SHORT
,
496 (void *)(vg_ui
.indice_start
*sizeof(u16
)) );
498 glDisable( GL_BLEND
);
500 vg_ui
.indice_start
= vg_ui
.cur_indice
;
501 vg_ui
.vert_start
= vg_ui
.cur_vert
;
504 static void ui_fill_rect( ui_rect rect
, u32 colour
, ui_px uv
[4] )
506 /* this if far from ideal but stops us from crashing */
507 if( (vg_ui
.cur_vert
+ 4 > vg_ui
.max_verts
) ||
508 (vg_ui
.cur_indice
+ 6 > vg_ui
.max_indices
))
511 struct ui_vert
*vertices
= &vg_ui
.vertex_buffer
[ vg_ui
.cur_vert
];
512 u16
*indices
= &vg_ui
.indice_buffer
[ vg_ui
.cur_indice
];
514 for( int i
=0; i
<4; i
++ ){
515 vertices
[i
].colour
= colour
;
518 vertices
[0].co
[0] = rect
[0];
519 vertices
[0].co
[1] = rect
[1];
520 vertices
[0].uv
[0] = uv
[0];
521 vertices
[0].uv
[1] = uv
[1];
522 vertices
[1].co
[0] = rect
[0]+rect
[2];
523 vertices
[1].co
[1] = rect
[1];
524 vertices
[1].uv
[0] = uv
[2];
525 vertices
[1].uv
[1] = uv
[1];
526 vertices
[2].co
[0] = rect
[0]+rect
[2];
527 vertices
[2].co
[1] = rect
[1]+rect
[3];
528 vertices
[2].uv
[0] = uv
[2];
529 vertices
[2].uv
[1] = uv
[3];
530 vertices
[3].co
[0] = rect
[0];
531 vertices
[3].co
[1] = rect
[1]+rect
[3];
532 vertices
[3].uv
[0] = uv
[0];
533 vertices
[3].uv
[1] = uv
[3];
534 u16 ind_start
= vg_ui
.cur_vert
;
536 u16 start
= vg_ui
.cur_vert
;
537 u32 mesh
[] = { 0,2,1, 0,3,2 };
539 for( u32 i
=0; i
<vg_list_size(mesh
); i
++ ){
540 indices
[i
] = start
+mesh
[i
];
543 vg_ui
.cur_indice
+= 6;
547 static void ui_fill( ui_rect rect
, u32 colour
)
549 ui_fill_rect( rect
, colour
, (ui_px
[4]){ 4,4,4,4 } );
552 static void ui_outline( ui_rect rect
, ui_px thickness
, u32 colour
)
554 /* this if far from ideal but stops us from crashing */
555 if( (vg_ui
.cur_vert
+ 8 > vg_ui
.max_verts
) ||
556 (vg_ui
.cur_indice
+ 24 > vg_ui
.max_indices
))
559 struct ui_vert
*vertices
= &vg_ui
.vertex_buffer
[ vg_ui
.cur_vert
];
560 u16
*indices
= &vg_ui
.indice_buffer
[ vg_ui
.cur_indice
];
562 for( int i
=0; i
<8; i
++ ){
563 vertices
[i
].uv
[0] = 4;
564 vertices
[i
].uv
[1] = 4;
565 vertices
[i
].colour
= colour
;
568 vertices
[0].co
[0] = rect
[0];
569 vertices
[0].co
[1] = rect
[1];
570 vertices
[1].co
[0] = rect
[0]+rect
[2];
571 vertices
[1].co
[1] = rect
[1];
572 vertices
[2].co
[0] = rect
[0]+rect
[2];
573 vertices
[2].co
[1] = rect
[1]+rect
[3];
574 vertices
[3].co
[0] = rect
[0];
575 vertices
[3].co
[1] = rect
[1]+rect
[3];
576 vertices
[4].co
[0] = vertices
[0].co
[0]-thickness
;
577 vertices
[4].co
[1] = vertices
[0].co
[1]-thickness
;
578 vertices
[5].co
[0] = vertices
[1].co
[0]+thickness
;
579 vertices
[5].co
[1] = vertices
[1].co
[1]-thickness
;
580 vertices
[6].co
[0] = vertices
[2].co
[0]+thickness
;
581 vertices
[6].co
[1] = vertices
[2].co
[1]+thickness
;
582 vertices
[7].co
[0] = vertices
[3].co
[0]-thickness
;
583 vertices
[7].co
[1] = vertices
[3].co
[1]+thickness
;
585 u16 start
= vg_ui
.cur_vert
;
586 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 };
588 for( u32 i
=0; i
<vg_list_size(mesh
); i
++ ){
589 indices
[i
] = start
+mesh
[i
];
592 vg_ui
.cur_indice
+= 24;
596 static void ui_split( ui_rect rect
,
597 enum ui_axis other
, ui_px width
, ui_px gap
,
598 ui_rect l
, ui_rect r
)
600 enum ui_axis dir
= other
^ 0x1;
602 if( width
< 0 ) width
= rect
[ 2+dir
] + width
;
605 rect_copy( rect
, temp
);
607 l
[ dir
] = temp
[ dir
];
608 r
[ dir
] = temp
[ dir
] + width
+ (gap
/2);
609 l
[ other
] = temp
[ other
];
610 r
[ other
] = temp
[ other
];
611 l
[ 2+dir
] = width
- (gap
/2);
612 r
[ 2+dir
] = temp
[ 2+dir
] - width
- (gap
/2);
613 l
[ 2+other
] = temp
[ 2+other
];
614 r
[ 2+other
] = temp
[ 2+other
];
617 static ui_px
ui_text_line_width( const char *str
);
619 static void ui_rect_center( ui_rect parent
, ui_rect rect
)
621 rect
[0] = parent
[0] + (parent
[2]-rect
[2])/2;
622 rect
[1] = parent
[1] + (parent
[3]-rect
[3])/2;
625 static void ui_fit_item( ui_rect rect
, ui_px size
[2], ui_rect d
)
627 i32 rp
= (i32
)rect
[2] * (i32
)size
[1],
628 rc
= (i32
)size
[0] * (i32
)rect
[3];
630 enum ui_axis dir
, other
;
631 if( rc
> rp
) dir
= k_ui_axis_h
;
632 else dir
= k_ui_axis_v
;
635 d
[2+dir
] = rect
[2+dir
];
636 d
[2+other
] = (rect
[2+dir
] * size
[other
]) / size
[dir
];
638 ui_rect_center( rect
, d
);
641 static void ui_split_ratio( ui_rect rect
, enum ui_axis dir
, float ratio
,
642 ui_px gap
, ui_rect l
, ui_rect r
)
644 ui_px width
= (float)rect
[ 2+(dir
^0x1) ] * ratio
;
645 ui_split( rect
, dir
, width
, gap
, l
, r
);
648 static void ui_rect_pad( ui_rect rect
, ui_px pad
[2] )
656 static ui_px
ui_text_line_width( const char *str
)
659 const char *_c
= str
;
662 while( (c
= *(_c
++)) ){
663 if( c
>= 32 ) length
++;
664 else if( c
== '\n' ) break;
670 static ui_px
ui_text_string_height( const char *str
)
673 const char *_c
= str
;
676 while( (c
= *(_c
++)) ){
677 if( c
== '\n' ) height
++;
683 static ui_px
ui_text_aligned_x( const char *str
, ui_rect rect
, ui_px scale
,
684 enum ui_align align
)
686 enum ui_align lwr
= k_ui_align_lwr
& align
;
687 if( lwr
== k_ui_align_left
){
691 ui_px width
= ui_text_line_width( str
) * scale
;
693 if( lwr
== k_ui_align_right
)
694 return rect
[0] + rect
[2]-width
;
696 return rect
[0] + (rect
[2]-width
)/2;
700 static ui_px
ui_min( ui_px a
, ui_px b
){ return a
<b
?a
:b
; }
701 static ui_px
ui_max( ui_px a
, ui_px b
){ return a
>b
?a
:b
; }
702 static ui_px
ui_clamp( ui_px a
, ui_px min
, ui_px max
)
704 return ui_min( max
, ui_max( a
, min
) );
707 static int ui_clip( ui_rect parent
, ui_rect child
, ui_rect clipped
)
709 ui_px parent_max
[2], child_max
[2];
710 parent_max
[0] = parent
[0]+parent
[2];
711 parent_max
[1] = parent
[1]+parent
[3];
712 child_max
[0] = child
[0]+child
[2];
713 child_max
[1] = child
[1]+child
[3];
715 clipped
[0] = ui_clamp( child
[0], parent
[0], parent_max
[0] );
716 clipped
[1] = ui_clamp( child
[1], parent
[1], parent_max
[1] );
717 clipped
[2] = ui_clamp( child_max
[0], parent
[0], parent_max
[0] );
718 clipped
[3] = ui_clamp( child_max
[1], parent
[1], parent_max
[1] );
720 if( clipped
[0] == clipped
[2] ||
721 clipped
[1] == clipped
[3] )
724 clipped
[2] -= clipped
[0];
725 clipped
[3] -= clipped
[1];
730 static int ui_inside_rect( ui_rect rect
, ui_px co
[2] )
732 if( co
[0] >= rect
[0] &&
734 co
[0] < rect
[0]+rect
[2] &&
735 co
[1] < rect
[1]+rect
[3] )
743 static int ui_click_down( u32 mask
)
745 if( vg_ui
.ignore_input_frames
) return 0;
746 if( (vg_ui
.mouse_state
[0] & mask
) &&
747 !(vg_ui
.mouse_state
[1] & mask
) )
753 static int ui_clicking( u32 mask
)
755 if( vg_ui
.ignore_input_frames
) return 0;
756 return vg_ui
.mouse_state
[0] & mask
;
759 static int ui_click_up( u32 mask
)
761 if( vg_ui
.ignore_input_frames
) return 0;
762 if( (vg_ui
.mouse_state
[1] & mask
) &&
763 !(vg_ui
.mouse_state
[0] & mask
) )
769 static void ui_prerender(void)
772 vg_ui
.mouse_state
[1] = vg_ui
.mouse_state
[0];
773 vg_ui
.mouse_state
[0] = SDL_GetMouseState( &x
, &y
);
778 vg_ui
.cur_indice
= 0;
779 vg_ui
.vert_start
= 0;
780 vg_ui
.indice_start
= 0;
781 vg_ui
.focused_control_hit
= 0;
782 vg_ui
.cursor
= k_ui_cursor_default
;
783 vg_ui
.wants_mouse
= 0;
785 if( vg_ui
.ignore_input_frames
){
786 vg_ui
.ignore_input_frames
--;
790 if( ui_click_down(UI_MOUSE_LEFT
)||ui_click_down(UI_MOUSE_MIDDLE
) ){
791 vg_ui
.mouse_click
[0] = vg_ui
.mouse
[0];
792 vg_ui
.mouse_click
[1] = vg_ui
.mouse
[1];
796 static u32
ui_colour( enum ui_scheme_colour id
)
798 return vg_ui
.scheme
[ id
];
801 /* get an appropriately contrasting colour given the base */
802 static u32
ui_colourcont( enum ui_scheme_colour id
)
804 if ( id
< k_ui_bg
+6 ) return ui_colour( k_ui_fg
);
805 else if( id
< k_ui_fg
) return ui_colour( k_ui_bg
+1 );
806 else if( id
< k_ui_hue
) return ui_colour( k_ui_bg
+3 );
807 else if( id
< k_ui_red
+k_ui_brighter
) return ui_colour( k_ui_fg
);
808 else return ui_colour( k_ui_fg
+1 );
811 static void ui_hex_to_norm( u32 hex
, v4f norm
)
813 norm
[0] = ((hex
) & 0xff);
814 norm
[1] = ((hex
>>8 ) & 0xff);
815 norm
[2] = ((hex
>>16) & 0xff);
816 norm
[3] = ((hex
>>24) & 0xff);
817 v4_muls( norm
, 1.0f
/255.0f
, norm
);
820 static u32
ui_ntext( ui_rect rect
, const char *str
, u32 len
, ui_px scale
,
821 enum ui_align align
, u32 colour
)
824 if( colour
== 0 ) colour
= ui_colour( k_ui_fg
);
826 colour
&= 0x00ffffff;
828 const char *_c
= str
;
831 text_cursor
[0] = ui_text_aligned_x( str
, rect
, scale
, align
);
832 text_cursor
[1] = rect
[1];
833 text_cursor
[2] = 8*scale
;
834 text_cursor
[3] = 14*scale
;
836 u32 printed_chars
= 0;
838 if( align
& (k_ui_align_middle
|k_ui_align_bottom
) ){
839 ui_px height
= ui_text_string_height( str
) * scale
;
841 if( align
& k_ui_align_bottom
)
842 text_cursor
[1] += rect
[3]-height
;
844 text_cursor
[1] += (rect
[3]-height
)/2;
847 while( (c
= *(_c
++)) ){
848 if( printed_chars
>= len
){
850 text_cursor
[1] += 14*scale
;
851 text_cursor
[0] = ui_text_aligned_x( _c
, rect
, scale
, align
);
852 text_cursor
[0] -= UI_GLYPH_SPACING_X
*scale
;
855 u8 glyph_index
= '\xb6';
856 glyph_base
[0] = glyph_index
& 0xf;
857 glyph_base
[1] = (glyph_index
-glyph_base
[0])>>4;
861 ui_fill_rect( text_cursor
, 0x00ffffff, (ui_px
[4])
869 text_cursor
[0] += UI_GLYPH_SPACING_X
*scale
;
873 text_cursor
[1] += 14*scale
;
874 text_cursor
[0] = ui_text_aligned_x( _c
, rect
, scale
, align
);
881 glyph_base
[0] = glyph_index
& 0xf;
882 glyph_base
[1] = (glyph_index
-glyph_base
[0])>>4;
888 if( ui_clip( rect
, text_cursor
, rect_char
) ){
889 ui_fill_rect( rect_char
, colour
, (ui_px
[4])
898 else if( c
== '\x1B' ){
902 for( int i
=0; i
<3; i
++ ){
908 case '0': colour
= ui_colour( k_ui_fg
); break;
909 case '3'|'0'<<8: colour
= ui_colour( k_ui_bg
); break;
910 case '3'|'1'<<8: colour
= ui_colour( k_ui_red
); break;
911 case '3'|'2'<<8: colour
= ui_colour( k_ui_green
); break;
912 case '3'|'3'<<8: colour
= ui_colour( k_ui_yellow
); break;
913 case '3'|'4'<<8: colour
= ui_colour( k_ui_blue
); break;
914 case '3'|'5'<<8: colour
= ui_colour( k_ui_purple
); break;
915 case '3'|'6'<<8: colour
= ui_colour( k_ui_aqua
); break;
916 case '3'|'7'<<8: colour
= 0xffffffff; break;
919 colour
&= 0x00ffffff;
923 colour_id
|= _c
[i
] << (i
*8);
933 else if( c
== '\t' ){
934 text_cursor
[0] += UI_GLYPH_SPACING_X
*scale
*4;
939 text_cursor
[0] += UI_GLYPH_SPACING_X
*scale
;
943 return printed_chars
;
946 static void ui_text( ui_rect rect
, const char *str
, ui_px scale
,
947 enum ui_align align
, u32 colour
)
949 ui_ntext( rect
, str
, 1024, scale
, align
, colour
);
952 static void ui_image( ui_rect rect
, GLuint image
)
954 ui_flush( k_ui_shader_colour
);
955 glActiveTexture( GL_TEXTURE0
);
956 glBindTexture( GL_TEXTURE_2D
, image
);
957 ui_fill_rect( rect
, 0xffffffff, (ui_px
[4]){ 0,128, 128, 0 } );
958 ui_flush( k_ui_shader_image
);
961 static u32
v4f_u32_colour( v4f colour
)
963 u32 r
= colour
[0] * 255.0f
,
964 g
= colour
[1] * 255.0f
,
965 b
= colour
[2] * 255.0f
,
966 a
= colour
[3] * 255.0f
;
968 return r
| (g
<<8) | (b
<<16) | (a
<<24);
971 static void ui_defocus_all(void)
973 if( vg_ui
.focused_control_type
== k_ui_control_textbox
){
977 vg_ui
.focused_control_id
= NULL
;
978 vg_ui
.focused_control_hit
= 0;
979 vg_ui
.focused_control_type
= k_ui_control_none
;
982 enum ui_button_state
{
983 k_ui_button_none
= 0,
984 k_ui_button_click
= 1,
985 k_ui_button_holding_inside
= 2,
986 k_ui_button_holding_outside
= 3,
987 k_ui_button_hover
= 4
990 static enum ui_button_state
ui_colourbutton( ui_rect rect
,
991 enum ui_scheme_colour colour
){
992 int clickup
= ui_click_up(UI_MOUSE_LEFT
),
993 click
= ui_clicking(UI_MOUSE_LEFT
) | clickup
,
994 target
= ui_inside_rect( rect
, vg_ui
.mouse_click
) && click
,
995 hover
= ui_inside_rect( rect
, vg_ui
.mouse
);
997 u32 col_base
= vg_ui
.scheme
[ colour
],
998 col_highlight
= vg_ui
.scheme
[ k_ui_fg
],
999 col_hover
= vg_ui
.scheme
[ colour
+ k_ui_brighter
];
1001 if( vg_ui
.focused_control_type
!= k_ui_control_none
){
1009 vg_ui
.cursor
= k_ui_cursor_hand
;
1016 ui_fill( rect
, col_highlight
);
1017 vg_ui
.ignore_input_frames
= 2;
1018 rect_copy( rect
, vg_ui
.click_fader
);
1020 rect_copy( rect
, vg_ui
.click_fader_end
);
1021 vg_ui
.click_fader_end
[3] = 0;
1022 ui_rect_center( rect
, vg_ui
.click_fader_end
);
1024 vg_ui
.click_fade_opacity
= 1.0f
;
1026 return k_ui_button_click
;
1029 ui_fill( rect
, col_highlight
);
1030 return k_ui_button_holding_inside
;
1034 ui_fill( rect
, col_base
);
1035 ui_outline( rect
, 1, col_highlight
);
1036 return k_ui_button_holding_outside
;
1040 ui_fill( rect
, col_base
);
1041 return k_ui_button_none
;
1046 ui_fill( rect
, col_hover
);
1047 return k_ui_button_hover
;
1050 ui_fill( rect
, col_base
);
1051 return k_ui_button_none
;
1056 static enum ui_button_state
ui_colourbutton_text(
1057 ui_rect rect
, const char *string
, ui_px scale
,
1058 enum ui_scheme_colour colour
){
1059 enum ui_button_state state
= ui_colourbutton( rect
, colour
);
1060 ui_rect t
= { 0,0, ui_text_line_width( string
)*scale
, 14*scale
};
1061 ui_rect_center( rect
, t
);
1063 u32 text_colour
= ui_colourcont(colour
);
1064 if( state
== k_ui_button_holding_inside
)
1065 text_colour
= colour
;
1067 ui_text( t
, string
, scale
, k_ui_align_left
, text_colour
);
1071 static enum ui_button_state
ui_button_text( ui_rect rect
,
1072 const char *string
, ui_px scale
){
1073 return ui_colourbutton_text( rect
, string
, scale
, k_ui_bg
+4 );
1076 static void ui_enum_post(void);
1077 static void ui_postrender(void)
1079 if( vg_ui
.click_fade_opacity
> 0.0f
){
1080 float scale
= vg_ui
.click_fade_opacity
;
1081 scale
= vg_maxf( 1.0f
/255.0f
, scale
*scale
);
1083 vg_ui
.click_fade_opacity
-= vg
.time_frame_delta
* 3.8f
;
1084 u32 colour
= (0x00ffffff & ui_colour(k_ui_fg
))|0x7f000000;
1086 v4f begin
, end
, dest
;
1087 for( int i
=0; i
<4; i
++ ){
1088 begin
[i
] = vg_ui
.click_fader
[i
];
1089 end
[i
] = vg_ui
.click_fader_end
[i
]+1;
1092 v4_lerp( end
, begin
, scale
, dest
);
1095 for( int i
=0; i
<4; i
++ ){
1099 ui_fill( rect
, colour
);
1102 if( vg_ui
.focused_control_type
== k_ui_control_enum
){
1105 else if( vg_ui
.focused_control_type
== k_ui_control_modal
){
1106 ui_rect screen
= {0,0,vg
.window_x
,vg
.window_y
};
1107 ui_fill( screen
, 0xa0000000 );
1108 ui_rect box
= {0,0,400,200};
1110 u32 colour
= ui_colour(k_ui_fg
),
1111 type
= vg_ui
.modal
.options
& UI_MODAL_TYPE_BITS
;
1112 if ( type
== 1 ) colour
= ui_colour(k_ui_green
);
1113 else if( type
== 2 ) colour
= ui_colour(k_ui_red
);
1114 else if( type
== 3 ) colour
= ui_colour(k_ui_yellow
);
1116 ui_rect_center( screen
, box
);
1117 ui_fill( box
, ui_colour(k_ui_bg
) );
1118 ui_outline( box
, -1, colour
);
1121 rect_copy( box
, message
);
1123 ui_rect_center( box
, message
);
1125 ui_rect row0
, row1
, btn
;
1126 ui_split_ratio( message
, k_ui_axis_h
, 0.5f
, 0, row0
, row1
);
1127 row0
[0] += UI_GLYPH_SPACING_X
;
1128 ui_ntext( row0
, vg_ui
.modal
.message
, (box
[2]/UI_GLYPH_SPACING_X
)-2, 1,
1129 k_ui_align_left
, colour
);
1131 rect_copy( row1
, btn
);
1134 ui_rect_center( row1
, btn
);
1136 vg_ui
.focused_control_type
= k_ui_control_none
; /* HACK */
1137 if( ui_button_text( btn
, "OK", 1 ) != 1 )
1138 vg_ui
.focused_control_hit
= 1;
1139 vg_ui
.focused_control_type
= k_ui_control_modal
; /* HACK */
1140 vg_ui
.wants_mouse
= 1;
1143 ui_flush( k_ui_shader_colour
);
1145 if( !vg_ui
.focused_control_hit
){
1149 if( vg_ui
.wants_mouse
){
1150 SDL_SetWindowGrab( vg
.window
, SDL_FALSE
);
1151 SDL_SetRelativeMouseMode( SDL_FALSE
);
1154 SDL_SetWindowGrab( vg
.window
, SDL_TRUE
);
1155 SDL_SetRelativeMouseMode( SDL_TRUE
);
1158 SDL_SetCursor( vg_ui
.cursor_map
[ vg_ui
.cursor
] );
1162 static void ui_dev_colourview(void)
1164 ui_rect window
= {vg
.window_x
-256,0,256,vg
.window_y
}, swatch
;
1166 const char *names
[vg_list_size(vg_ui
.scheme
)] = {
1167 [k_ui_bg
] = "k_ui_bg", "k_ui_bg+1", "k_ui_bg+2", "k_ui_bg+3",
1168 "k_ui_bg+4", "k_ui_bg+5", "k_ui_bg+6", "k_ui_bg+7",
1170 [k_ui_fg
] = "k_ui_fg", "k_ui_fg+1", "k_ui_fg+2", "k_ui_fg+3",
1171 "k_ui_fg+4", "k_ui_fg+5", "k_ui_fg+6", "k_ui_fg+7",
1173 [k_ui_red
] = "k_ui_red", "k_ui_orange", "k_ui_yellow", "k_ui_green",
1174 "k_ui_aqua", "k_ui_blue", "k_ui_purple", "k_ui_gray",
1175 "k_ui_red+8","k_ui_orange+8","k_ui_yellow+8","k_ui_green+8",
1176 "k_ui_aqua+8","k_ui_blue+8","k_ui_purple+8","k_ui_gray+8" };
1179 ui_split_ratio( window
, k_ui_axis_v
, 0.5f
, 0, col
[0], col
[1] );
1181 for( int i
=0; i
<vg_list_size(vg_ui
.scheme
); i
++ ){
1182 int which
= (i
/8)%2;
1184 ui_split( col
[which
], k_ui_axis_h
, 24, 0, swatch
, col
[which
] );
1185 ui_fill( swatch
, ui_colour(i
) );
1188 ui_text(swatch
, names
[i
], 1, k_ui_align_middle_left
, ui_colourcont(i
));
1194 * -----------------------------------------------------------------------------
1197 static int ui_checkbox( ui_rect rect
, const char *str_label
, int *data
){
1199 ui_split( rect
, k_ui_axis_v
, -rect
[3], 0, label
, box
);
1200 ui_text( label
, str_label
, 1, k_ui_align_middle_left
, 0 );
1202 int changed
= ui_colourbutton( box
, k_ui_bg
)==1;
1204 *data
= (*data
) ^ 0x1;
1207 ui_rect_pad( box
, (ui_px
[2]){4,4} );
1208 ui_fill( box
, ui_colour( k_ui_orange
) );
1216 * -----------------------------------------------------------------------------
1220 * unfortunately no return value since we only find out that event in the
1223 static void ui_enum( ui_rect rect
, const char *str_label
,
1224 struct ui_enum_opt
*options
, u32 len
, i32
*value
)
1227 ui_split( rect
, k_ui_axis_v
, ui_text_line_width( str_label
) + 8,
1229 ui_text( label
, str_label
, 1, k_ui_align_middle_left
, 0 );
1231 const char *display
= "OUT OF RANGE";
1232 for( u32 i
=0; i
<len
; i
++ ){
1233 if( *value
== options
[i
].value
){
1234 display
= options
[i
].alias
;
1239 if( ui_button_text( box
, display
, 1 ) == 1 ){
1240 vg_ui
.focused_control_type
= k_ui_control_enum
;
1241 vg_ui
.ptr_enum
= value
;
1242 vg_ui
._enum
.option_count
= len
;
1243 vg_ui
._enum
.options
= options
;
1244 rect_copy( box
, vg_ui
._enum
.rect
);
1248 static void ui_enum_post(void){
1250 rect_copy( vg_ui
._enum
.rect
, drawer
);
1251 drawer
[3] *= vg_ui
._enum
.option_count
;
1254 int clickany
= ui_click_up(UI_MOUSE_LEFT
|UI_MOUSE_RIGHT
|UI_MOUSE_MIDDLE
),
1255 hover
= ui_inside_rect( drawer
, vg_ui
.mouse
);
1257 if( clickany
&& !hover
){
1262 vg_ui
.focused_control_type
= k_ui_control_none
;
1263 i32
*value
= vg_ui
.ptr_enum
;
1265 for( u32 i
=0; i
<vg_ui
._enum
.option_count
; i
++ ){
1267 ui_split( drawer
, k_ui_axis_h
, vg_ui
._enum
.rect
[3], 0, button
,drawer
);
1269 enum ui_scheme_colour colour
= k_ui_bg
+3;
1270 if( vg_ui
._enum
.options
[i
].value
== *value
)
1271 colour
= k_ui_orange
;
1273 if( ui_colourbutton_text( button
, vg_ui
._enum
.options
[i
].alias
, 1,
1275 *value
= vg_ui
._enum
.options
[i
].value
;
1281 vg_ui
.focused_control_type
= k_ui_control_enum
;
1284 vg_ui
.focused_control_hit
= 1;
1289 * -----------------------------------------------------------------------------
1292 static void _ui_textbox_make_selection( int *start
, int *end
)
1294 *start
= VG_MIN( vg_ui
.textbox
.cursor_pos
, vg_ui
.textbox
.cursor_user
);
1295 *end
= VG_MAX( vg_ui
.textbox
.cursor_pos
, vg_ui
.textbox
.cursor_user
);
1298 static void _ui_textbox_move_cursor( int *cursor0
, int *cursor1
,
1299 int dir
, int snap_together
)
1301 *cursor0
= VG_MAX( 0, vg_ui
.textbox
.cursor_user
+ dir
);
1304 VG_MIN( vg_ui
.textbox
.len
-1, strlen( vg_ui
.textbuf
)),
1308 *cursor1
= *cursor0
;
1311 static int _ui_textbox_makeroom( int datastart
, int length
)
1313 int move_to
= VG_MIN( datastart
+length
, vg_ui
.textbox
.len
-1 );
1314 int move_amount
= strlen( vg_ui
.textbuf
)-datastart
;
1315 int move_end
= VG_MIN( move_to
+move_amount
, vg_ui
.textbox
.len
-1 );
1316 move_amount
= move_end
-move_to
;
1319 memmove( &vg_ui
.textbuf
[ move_to
],
1320 &vg_ui
.textbuf
[ datastart
],
1323 vg_ui
.textbuf
[ move_end
] = '\0';
1325 return VG_MIN( length
, vg_ui
.textbox
.len
-datastart
-1 );
1328 static int _ui_textbox_delete_char( int direction
)
1331 _ui_textbox_make_selection( &start
, &end
);
1333 /* There is no selection */
1335 if( direction
== 1 ) end
= VG_MIN( end
+1, strlen( vg_ui
.textbuf
) );
1336 else if( direction
== -1 ) start
= VG_MAX( start
-1, 0 );
1339 /* Still no selction, no need to do anything */
1343 /* Copy the end->terminator to start */
1344 int remaining_length
= strlen( vg_ui
.textbuf
)+1-end
;
1345 memmove( &vg_ui
.textbuf
[ start
],
1346 &vg_ui
.textbuf
[ end
],
1351 static void _ui_textbox_to_clipboard(void)
1354 _ui_textbox_make_selection( &start
, &end
);
1358 memcpy( buffer
, &vg_ui
.textbuf
[ start
], end
-start
);
1359 buffer
[ end
-start
] = 0x00;
1360 SDL_SetClipboardText( buffer
);
1364 static void _ui_textbox_clipboard_paste(void)
1366 if( !SDL_HasClipboardText() )
1369 char *text
= SDL_GetClipboardText();
1374 int datastart
= _ui_textbox_delete_char( 0 );
1375 int length
= strlen( text
);
1376 int cpylength
= _ui_textbox_makeroom( datastart
, length
);
1378 memcpy( vg_ui
.textbuf
+ datastart
, text
, cpylength
);
1379 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
,
1380 &vg_ui
.textbox
.cursor_pos
, cpylength
, 1 );
1384 static void _ui_textbox_put_char( char c
)
1386 vg_ui
.textbox
.cursor_user
= _ui_textbox_delete_char(0);
1388 if( _ui_textbox_makeroom( vg_ui
.textbox
.cursor_user
, 1 ) )
1389 vg_ui
.textbuf
[ vg_ui
.textbox
.cursor_user
] = c
;
1391 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
,
1392 &vg_ui
.textbox
.cursor_pos
, 1, 1 );
1395 /* Receed secondary cursor */
1396 static void _ui_textbox_left_select(void)
1398 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
, NULL
, -1, 0 );
1401 /* Match and receed both cursors */
1402 static void _ui_textbox_left(void)
1404 int cursor_diff
= vg_ui
.textbox
.cursor_pos
- vg_ui
.textbox
.cursor_user
? 0: 1;
1406 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
,
1407 &vg_ui
.textbox
.cursor_pos
, -cursor_diff
, 1 );
1410 static void _ui_textbox_up(void)
1412 if( vg_ui
.textbox
.flags
& UI_TEXTBOX_MULTILINE
){
1413 int line_begin
= vg_ui
.textbox
.cursor_user
;
1415 while( line_begin
){
1416 if( vg_ui
.textbuf
[ line_begin
-1 ] == '\n' ){
1424 int line_above_begin
= line_begin
-1;
1426 while( line_above_begin
){
1427 if( vg_ui
.textbuf
[ line_above_begin
-1 ] == '\n' ){
1431 line_above_begin
--;
1434 int offset
= vg_ui
.textbox
.cursor_user
- line_begin
,
1435 line_length_above
= line_begin
- line_above_begin
-1;
1437 offset
= VG_MIN( line_length_above
, offset
);
1439 vg_ui
.textbox
.cursor_user
= line_above_begin
+offset
;
1440 vg_ui
.textbox
.cursor_pos
= line_above_begin
+offset
;
1443 vg_ui
.textbox
.cursor_user
= line_begin
;
1444 vg_ui
.textbox
.cursor_pos
= line_begin
;
1448 if( vg_ui
.textbox
.callbacks
.up
){
1449 vg_ui
.textbox
.callbacks
.up( vg_ui
.textbuf
, vg_ui
.textbox
.len
);
1454 static void _ui_textbox_down(void)
1456 if( vg_ui
.textbox
.flags
& UI_TEXTBOX_MULTILINE
){
1457 int line_begin
= vg_ui
.textbox
.cursor_user
;
1459 while( line_begin
){
1460 if( vg_ui
.textbuf
[ line_begin
-1 ] == '\n' ){
1467 int line_below_begin
= vg_ui
.textbox
.cursor_user
;
1470 if( vg_ui
.textbuf
[ line_below_begin
] == '\0' ){
1471 vg_ui
.textbox
.cursor_user
= line_below_begin
;
1472 vg_ui
.textbox
.cursor_pos
= line_below_begin
;
1476 if( vg_ui
.textbuf
[ line_below_begin
] == '\n' ){
1477 line_below_begin
++;
1481 line_below_begin
++;
1484 int line_below_end
= line_below_begin
;
1486 if( vg_ui
.textbuf
[ line_below_end
] == '\0' ||
1487 vg_ui
.textbuf
[ line_below_end
] == '\n' ){
1494 int offset
= vg_ui
.textbox
.cursor_user
- line_begin
,
1495 line_length_below
= line_below_end
- line_below_begin
-1;
1497 offset
= VG_MIN( line_length_below
, offset
);
1499 vg_ui
.textbox
.cursor_user
= line_below_begin
+offset
;
1500 vg_ui
.textbox
.cursor_pos
= line_below_begin
+offset
;
1503 if( vg_ui
.textbox
.callbacks
.down
){
1504 vg_ui
.textbox
.callbacks
.down( vg_ui
.textbuf
, vg_ui
.textbox
.len
);
1509 static void _ui_textbox_right_select(void)
1511 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
, NULL
, 1, 0 );
1514 static void _ui_textbox_right(void)
1516 int cursor_diff
= vg_ui
.textbox
.cursor_pos
- vg_ui
.textbox
.cursor_user
? 0: 1;
1518 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
,
1519 &vg_ui
.textbox
.cursor_pos
, +cursor_diff
, 1 );
1522 static void _ui_textbox_backspace(void)
1524 if( vg_ui
.focused_control_type
== k_ui_control_textbox
){
1525 vg_ui
.textbox
.cursor_user
= _ui_textbox_delete_char( -1 );
1526 vg_ui
.textbox
.cursor_pos
= vg_ui
.textbox
.cursor_user
;
1528 if( vg_ui
.textbox
.callbacks
.change
){
1529 vg_ui
.textbox
.callbacks
.change( vg_ui
.textbuf
, vg_ui
.textbox
.len
);
1534 static void _ui_textbox_delete(void)
1536 if( vg_ui
.focused_control_type
== k_ui_control_textbox
){
1537 vg_ui
.textbox
.cursor_user
= _ui_textbox_delete_char( 1 );
1538 vg_ui
.textbox
.cursor_pos
= vg_ui
.textbox
.cursor_user
;
1540 if( vg_ui
.textbox
.callbacks
.change
){
1541 vg_ui
.textbox
.callbacks
.change( vg_ui
.textbuf
, vg_ui
.textbox
.len
);
1546 static void _ui_textbox_home_select(void)
1548 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
, NULL
, -10000, 0 );
1551 static void _ui_textbox_home(void)
1553 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
,
1554 &vg_ui
.textbox
.cursor_pos
, -10000, 1 );
1557 static void _ui_textbox_end_select(void)
1559 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
, NULL
, 10000, 0 );
1562 static void _ui_textbox_end(void)
1564 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
,
1565 &vg_ui
.textbox
.cursor_pos
,
1566 vg_ui
.textbox
.len
-1, 1 );
1569 static void _ui_textbox_select_all(void)
1571 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_user
, NULL
, 10000, 0);
1572 _ui_textbox_move_cursor( &vg_ui
.textbox
.cursor_pos
, NULL
, -10000, 0);
1575 static void _ui_textbox_cut(void)
1577 _ui_textbox_to_clipboard();
1578 vg_ui
.textbox
.cursor_user
= _ui_textbox_delete_char(0);
1579 vg_ui
.textbox
.cursor_pos
= vg_ui
.textbox
.cursor_user
;
1582 static void _ui_textbox_enter(void)
1584 if( vg_ui
.focused_control_type
== k_ui_control_textbox
){
1585 if( vg_ui
.textbox
.callbacks
.enter
)
1586 vg_ui
.textbox
.callbacks
.enter( vg_ui
.textbuf
, vg_ui
.textbox
.len
);
1588 if( vg_ui
.focused_control_type
!= k_ui_control_textbox
) return;
1590 if( vg_ui
.textbox
.flags
& UI_TEXTBOX_MULTILINE
)
1591 _ui_textbox_put_char( '\n' );
1593 if( !(vg_ui
.textbox
.flags
& UI_TEXTBOX_AUTOFOCUS
) )
1600 * based on a visual character coordinate relative to the anchor of the textbox,
1601 * this works out the linear place in the buffer that coordinate maps to
1603 * input coordinates go in co[0], co[1], and the result index is in co[2]
1605 static void _ui_textbox_calc_index_from_grid( int co
[3], int wrap_length
)
1610 while( (c
= vg_ui
.textbuf
[i
[2]]) ){
1611 if( i
[1]==co
[1] && i
[0]>=co
[0] ) break;
1613 if( i
[0] >= wrap_length
){
1618 if( c
>= 32 && c
<= 126 ){
1622 else if( c
== '\n' ){
1625 if( i
[1] > co
[1] ) break;
1639 * based on the index specied in co[2], work out the visual character
1640 * coordinates and store them in co[0], co[1]
1642 static void _ui_textbox_index_calc_coords( int co
[3], int wrap_length
)
1650 while( (c
= vg_ui
.textbuf
[i
++]) ){
1651 if( i
> co
[2] ) break;
1652 if( co
[0] >= wrap_length
){
1656 if( c
>= 32 && c
<= 126 ) co
[0] ++;
1657 else if( c
== '\n' ){
1665 * calculate the number of characters remaining until either:
1666 * - the wrap_length limit is hit
1667 * - end of the line/string
1669 * index must be fully populated with visual X/Y, and linear index
1671 static int _ui_textbox_run_remaining( int index
[3], int wrap_length
)
1673 int i
=0, printed_chars
=0;
1675 while( (c
= vg_ui
.textbuf
[index
[2] + (i
++)]) ){
1676 if( index
[0]+i
>= wrap_length
) break;
1677 if( c
>= 32 && c
<= 126 ) printed_chars
++;
1678 else if( c
== '\n' ) break;
1681 return printed_chars
+1;
1684 static int ui_textbox( ui_rect rect
, char *buf
, u32 len
, u32 flags
,
1685 struct ui_textbox_callbacks
*callbacks
)
1687 int clickup
= ui_click_up(UI_MOUSE_LEFT
),
1688 clickdown
= ui_click_down(UI_MOUSE_LEFT
),
1689 click
= ui_clicking(UI_MOUSE_LEFT
) | clickup
,
1690 target
= ui_inside_rect( rect
, vg_ui
.mouse_click
) && click
,
1691 hover
= ui_inside_rect( rect
, vg_ui
.mouse
);
1693 /* allow instant transitions from textbox->textbox */
1694 if( (vg_ui
.focused_control_type
!= k_ui_control_none
) &&
1695 (vg_ui
.focused_control_type
!= k_ui_control_textbox
) ){
1701 flags
&= ~UI_TEXTBOX_AUTOFOCUS
;
1704 u32 col_base
= ui_colour( k_ui_bg
),
1705 col_highlight
= ui_colour( k_ui_fg
),
1706 col_cursor
= (0x00ffffff & ui_colour(k_ui_fg
))|0x7f000000;
1711 rect_copy( rect
, text_rect
);
1713 if( flags
& UI_TEXTBOX_MULTILINE
) text_rect
[3] = rect
[3]-16;
1714 else text_rect
[3] = 14;
1717 ui_rect_center( rect
, text_rect
);
1719 ui_px wrap_length
= 1024;
1721 if( flags
& UI_TEXTBOX_WRAP
)
1722 wrap_length
= text_rect
[2] / UI_GLYPH_SPACING_X
;
1725 vg_ui
.cursor
= k_ui_cursor_ibeam
;
1728 if( vg_ui
.focused_control_id
== buf
){
1729 ui_fill( rect
, col_base
);
1730 ui_ntext( text_rect
, buf
, wrap_length
, 1, k_ui_align_left
, 0 );
1732 if( !(flags
& UI_TEXTBOX_AUTOFOCUS
) && ((clickup
||clickdown
) && !target
)){
1736 vg_ui
.focused_control_hit
= 1;
1737 if( click
&& target
){
1739 (vg_ui
.mouse_click
[0] - text_rect
[0]) / UI_GLYPH_SPACING_X
,
1740 (vg_ui
.mouse_click
[1] - text_rect
[1]) / 14,
1744 (vg_ui
.mouse
[0] - text_rect
[0]) / UI_GLYPH_SPACING_X
,
1745 (vg_ui
.mouse
[1] - text_rect
[1]) / 14,
1749 if( flags
& UI_TEXTBOX_MULTILINE
){
1750 _ui_textbox_calc_index_from_grid( p0
, wrap_length
);
1751 _ui_textbox_calc_index_from_grid( p1
, wrap_length
);
1753 vg_ui
.textbox
.cursor_pos
= p0
[2];
1754 vg_ui
.textbox
.cursor_user
= p1
[2];
1757 int max
= strlen( buf
);
1758 vg_ui
.textbox
.cursor_pos
= VG_MAX( 0, VG_MIN( max
, p0
[0] )),
1759 vg_ui
.textbox
.cursor_user
= VG_MAX( 0, VG_MIN( max
, p1
[0] ));
1763 ui_outline( rect
, -2, vg_ui
.scheme
[ k_ui_orange
] );
1767 int c0
= vg_ui
.textbox
.cursor_pos
,
1768 c1
= vg_ui
.textbox
.cursor_user
,
1769 start
= VG_MIN( c0
, c1
),
1770 end
= VG_MAX( c0
, c1
),
1773 if( flags
& (UI_TEXTBOX_MULTILINE
|UI_TEXTBOX_WRAP
) ){
1775 int pos
[3], remaining
= chars
;
1778 _ui_textbox_index_calc_coords( pos
, wrap_length
);
1781 cursor
[0] = text_rect
[0] + pos
[0]*UI_GLYPH_SPACING_X
-1;
1782 cursor
[1] = text_rect
[1] + pos
[1]*14;
1785 ui_fill( cursor
, col_cursor
);
1786 rect_copy( cursor
, vg_ui
.click_fader_end
);
1790 int run
= _ui_textbox_run_remaining( pos
, wrap_length
);
1791 run
= VG_MIN( run
, remaining
);
1793 cursor
[0] = text_rect
[0] + pos
[0]*UI_GLYPH_SPACING_X
-1;
1794 cursor
[1] = text_rect
[1] + pos
[1]*14;
1795 cursor
[2] = (float)(run
)*(float)UI_GLYPH_SPACING_X
;
1798 ui_fill( cursor
, col_cursor
);
1805 rect_copy( cursor
, vg_ui
.click_fader_end
);
1809 cursor
[0] = text_rect
[0] + start
*UI_GLYPH_SPACING_X
-1;
1810 cursor
[1] = text_rect
[1];
1817 cursor
[2] = (float)(chars
)*(float)UI_GLYPH_SPACING_X
;
1820 if( (vg_ui
.click_fade_opacity
<=0.0f
) &&
1821 ui_clip( rect
, cursor
, cursor
) ){
1822 ui_fill( cursor
, col_cursor
);
1825 rect_copy( cursor
, vg_ui
.click_fader_end
);
1832 if( click
|| (flags
& UI_TEXTBOX_AUTOFOCUS
) ){
1833 if( (target
&& hover
) || (flags
& UI_TEXTBOX_AUTOFOCUS
) ){
1836 ui_fill( rect
, col_highlight
);
1837 vg_ui
.ignore_input_frames
= 2;
1838 rect_copy( rect
, vg_ui
.click_fader
);
1839 rect_copy( rect
, vg_ui
.click_fader_end
);
1841 vg_ui
.click_fade_opacity
= 1.0f
;
1842 vg_ui
.textbuf
= buf
;
1843 vg_ui
.focused_control_hit
= 1;
1844 vg_ui
.focused_control_type
= k_ui_control_textbox
;
1845 vg_ui
.textbox
.len
= len
;
1846 vg_ui
.textbox
.flags
= flags
;
1847 vg_ui
.textbox
.cursor_pos
= 0;
1848 vg_ui
.textbox
.cursor_user
= 0;
1851 vg_ui
.textbox
.callbacks
= *callbacks
;
1854 vg_ui
.textbox
.callbacks
.change
= NULL
;
1855 vg_ui
.textbox
.callbacks
.down
= NULL
;
1856 vg_ui
.textbox
.callbacks
.up
= NULL
;
1857 vg_ui
.textbox
.callbacks
.enter
= NULL
;
1860 SDL_StartTextInput();
1864 ui_fill( rect
, col_base
);
1867 ui_outline( rect
, -1, col_highlight
);
1870 ui_ntext( text_rect
, buf
, wrap_length
, 1, k_ui_align_left
, 0 );
1876 * -----------------------------------------------------------------------------
1878 static void ui_start_modal( const char *message
, u32 options
){
1879 vg_ui
.focused_control_type
= k_ui_control_modal
;
1880 vg_ui
.modal
.message
= message
;
1881 vg_ui
.modal
.callbacks
.close
= NULL
;
1882 vg_ui
.modal
.options
= options
;
1884 u32 type
= options
& UI_MODAL_TYPE_BITS
;
1885 if( type
== UI_MODAL_OK
) vg_info( message
);
1886 else if( type
== UI_MODAL_WARN
) vg_warn( message
);
1887 else if( type
== UI_MODAL_GOOD
) vg_success( message
);
1888 else if( type
== UI_MODAL_BAD
) vg_error( message
);
1893 * -----------------------------------------------------------------------------
1899 static void _ui_proc_key( SDL_Keysym ev
)
1901 if( vg_ui
.focused_control_type
!= k_ui_control_textbox
){
1905 struct textbox_mapping
1910 void (*handler
)(void);
1914 { 0, SDLK_LEFT
, _ui_textbox_left
},
1915 { KMOD_SHIFT
, SDLK_LEFT
, _ui_textbox_left_select
},
1916 { 0, SDLK_RIGHT
, _ui_textbox_right
},
1917 { KMOD_SHIFT
, SDLK_RIGHT
, _ui_textbox_right_select
},
1918 { 0, SDLK_DOWN
, _ui_textbox_down
},
1919 { 0, SDLK_UP
, _ui_textbox_up
},
1920 { 0, SDLK_BACKSPACE
, _ui_textbox_backspace
},
1921 { KMOD_SHIFT
, SDLK_BACKSPACE
, _ui_textbox_backspace
},
1922 { KMOD_CTRL
, SDLK_BACKSPACE
, _ui_textbox_backspace
},
1923 { 0, SDLK_DELETE
, _ui_textbox_delete
},
1924 { 0, SDLK_HOME
, _ui_textbox_home
},
1925 { KMOD_SHIFT
, SDLK_HOME
, _ui_textbox_home_select
},
1926 { 0, SDLK_END
, _ui_textbox_end
},
1927 { KMOD_SHIFT
, SDLK_END
, _ui_textbox_end_select
},
1928 { KMOD_CTRL
, SDLK_a
, _ui_textbox_select_all
},
1929 { KMOD_CTRL
, SDLK_c
, _ui_textbox_to_clipboard
},
1930 { KMOD_CTRL
, SDLK_x
, _ui_textbox_cut
},
1931 { KMOD_CTRL
, SDLK_v
, _ui_textbox_clipboard_paste
},
1932 { 0, SDLK_RETURN
, _ui_textbox_enter
},
1933 { 0, SDLK_ESCAPE
, ui_defocus_all
},
1938 if( ev
.mod
& KMOD_SHIFT
)
1941 if( ev
.mod
& KMOD_CTRL
)
1944 if( ev
.mod
& KMOD_ALT
)
1947 for( int i
=0; i
<vg_list_size( mappings
); i
++ ){
1948 struct textbox_mapping
*mapping
= &mappings
[i
];
1950 if( mapping
->key
== ev
.sym
){
1951 if( mapping
->mod
== 0 ){
1957 else if( (mod
& mapping
->mod
) == mapping
->mod
){
1966 * Callback for text entry mode
1968 VG_STATIC
void ui_proc_utf8( const char *text
)
1970 if( vg_ui
.focused_control_type
== k_ui_control_textbox
){
1971 const char *ptr
= text
;
1974 if( *ptr
!= '`' ) _ui_textbox_put_char( *ptr
);
1978 if( vg_ui
.textbox
.callbacks
.change
){
1979 vg_ui
.textbox
.callbacks
.change( vg_ui
.textbuf
, vg_ui
.textbox
.len
);
1984 static void ui_label( ui_rect rect
, const char *text
, ui_px size
,
1985 ui_px gap
, ui_rect r
)
1988 ui_px width
= (ui_text_line_width(text
)+UI_GLYPH_SPACING_X
) * size
;
1989 ui_split( rect
, k_ui_axis_v
, width
, gap
, l
, r
);
1990 ui_text( l
, text
, 1, k_ui_align_middle_left
, 0 );
1995 static ui_px k_vgi_widget_height
= 24,
1998 enum vgi_window_flag
{
1999 k_vgi_window_resizeable
= 0x1,
2000 k_vgi_window_moveable
= 0x2,
2001 k_vgi_window_closeable
= 0x4,
2002 k_vgi_window_standard
=
2003 k_vgi_window_moveable
|
2004 k_vgi_window_closeable
2007 static int vgi_window( ui_rect in_window
, const char *title
, u32 flags
,
2008 ui_rect out_panel
){
2009 ui_fill( in_window
, ui_colour( k_ui_bg
+1 ) );
2010 ui_outline( in_window
, 1, ui_colour( k_ui_bg
+7 ) );
2013 ui_split( in_window
, k_ui_axis_h
, k_vgi_widget_height
*k_vgi_scale
, 0,
2015 ui_fill( bar
, ui_colour( k_ui_bg
+7 ) );
2016 ui_text( bar
, title
, k_vgi_scale
, k_ui_align_middle_center
,
2017 ui_colourcont(k_ui_bg
+7) );
2019 if( flags
& k_vgi_window_closeable
){
2020 ui_rect quit_button
;
2021 ui_split( bar
, k_ui_axis_v
, bar
[2]-bar
[3], 2, bar
, quit_button
);
2022 if( ui_button_text( quit_button
, "X", 1 ) == 1 ){
2039 #endif /* VG_IMGUI_H */