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
16 #include "vg/vg_tex.h"
17 #include "vg/vg_shader.h"
20 typedef ui_px ui_rect
[4];
21 typedef struct ui_vert ui_vert
;
28 /* Relative to cursor p0 */
31 k_ui_align_left
= 0x0000| 0x00,
32 k_ui_align_right
= 0x0000| 0x01,
33 k_ui_align_center
= 0x0000| 0x02,
35 k_ui_align_middle
= 0x0100,
36 k_ui_align_middle_left
= 0x0100| 0x00,
37 k_ui_align_middle_right
= 0x0100| 0x01,
38 k_ui_align_middle_center
= 0x0100| 0x02,
40 k_ui_align_bottom
= 0x0200,
41 k_ui_align_bottom_left
= 0x0200| 0x00,
42 k_ui_align_bottom_right
= 0x0200| 0x01,
43 k_ui_align_bottom_center
= 0x0200| 0x02,
55 enum ui_scheme_colour
{
70 typedef u32 ui_scheme
[8*4];
72 #define UI_RGB( STDHEX ) 0xff000000 |\
73 ((STDHEX&0x000000ff)<<16) |\
74 ((STDHEX&0x0000ff00) ) |\
75 ((STDHEX&0x00ff0000)>>16)
78 struct ui_vert
*vertex_buffer
;
80 u32 max_verts
, max_indices
,
82 vert_start
, indice_start
;
85 void *focused_control_id
; /* uses the memory location of various locking
90 u32 focused_control_hit
;
96 int cursor_user
, cursor_pos
, string_length
;
99 GLuint tex_glyphs
, vao
, vbo
, ebo
;
101 ui_px mouse
[2], mouse_click
[2];
103 u32 ignore_input_frames
;
105 ui_rect click_fader
, click_fader_end
;
106 float click_fade_opacity
;
118 SDL_Cursor
*cursor_map
[ k_ui_cursor_max
];
122 [ k_ui_bg
+0 ] = UI_RGB( 0x1d2021 ),
123 [ k_ui_bg
+1 ] = UI_RGB( 0x282828 ),
124 [ k_ui_bg
+2 ] = UI_RGB( 0x3c3836 ),
125 [ k_ui_bg
+3 ] = UI_RGB( 0x504945 ),
126 [ k_ui_bg
+4 ] = UI_RGB( 0x665c54 ),
127 [ k_ui_bg
+5 ] = UI_RGB( 0x7c6f64 ),
128 [ k_ui_bg
+6 ] = UI_RGB( 0x928374 ),
129 [ k_ui_bg
+7 ] = UI_RGB( 0xa89984 ),
131 [ k_ui_fg
+0 ] = UI_RGB( 0xebdbb2 ),
132 [ k_ui_fg
+1 ] = UI_RGB( 0xfbf1c7 ),
133 [ k_ui_fg
+2 ] = UI_RGB( 0xd5c4a1 ),
134 [ k_ui_fg
+3 ] = UI_RGB( 0xbdae93 ),
135 [ k_ui_fg
+4 ] = UI_RGB( 0xa89984 ),
136 [ k_ui_fg
+5 ] = UI_RGB( 0x000000 ),
137 [ k_ui_fg
+6 ] = UI_RGB( 0x000000 ),
138 [ k_ui_fg
+7 ] = UI_RGB( 0x000000 ),
140 [ k_ui_red
] = UI_RGB( 0xcc241d ),
141 [ k_ui_orange
] = UI_RGB( 0xd65d0e ),
142 [ k_ui_yellow
] = UI_RGB( 0xd79921 ),
143 [ k_ui_green
] = UI_RGB( 0x98971a ),
144 [ k_ui_aqua
] = UI_RGB( 0x689d6a ),
145 [ k_ui_blue
] = UI_RGB( 0x458588 ),
146 [ k_ui_purple
] = UI_RGB( 0xb16286 ),
147 [ k_ui_gray
] = UI_RGB( 0x928374 ),
148 [ k_ui_red
+ k_ui_brighter
] = UI_RGB( 0xfb4934 ),
149 [ k_ui_orange
+ k_ui_brighter
] = UI_RGB( 0xfe8019 ),
150 [ k_ui_yellow
+ k_ui_brighter
] = UI_RGB( 0xfabd2f ),
151 [ k_ui_green
+ k_ui_brighter
] = UI_RGB( 0xb8bb26 ),
152 [ k_ui_aqua
+ k_ui_brighter
] = UI_RGB( 0x8ec07c ),
153 [ k_ui_blue
+ k_ui_brighter
] = UI_RGB( 0x83a598 ),
154 [ k_ui_purple
+ k_ui_brighter
] = UI_RGB( 0xd3869b ),
155 [ k_ui_gray
+ k_ui_brighter
] = UI_RGB( 0xa89984 ),
159 static struct vg_shader _shader_ui
=
167 "layout (location=0) in vec2 a_co;"
168 "layout (location=1) in vec2 a_uv;"
169 "layout (location=2) in vec4 a_colour;"
172 "out vec2 aTexCoords;"
178 "gl_Position = vec4( uPv * vec3( a_co, 1.0 ), 1.0 );"
179 "aTexCoords = a_uv * 0.0078125;"
180 "aColour = a_colour;"
189 "uniform sampler2D uTexGlyphs;"
190 "out vec4 FragColor;"
192 "in vec2 aTexCoords;"
197 "vec2 rand_hash22( vec2 p )"
199 "vec3 p3 = fract(vec3(p.xyx) * 213.8976123);"
200 "p3 += dot(p3, p3.yzx+19.19);"
201 "return fract(vec2((p3.x + p3.y)*p3.z, (p3.x+p3.z)*p3.y));"
206 "vec4 diffuse = aColour;"
208 "if( diffuse.a == 0.0 )"
210 "diffuse.a = texture( uTexGlyphs, aTexCoords ).r;"
213 "FragColor = diffuse;"
218 static struct vg_shader _shader_ui_image
=
220 .name
= "[vg] ui_image",
226 "layout (location=0) in vec2 a_co;"
227 "layout (location=1) in vec2 a_uv;"
228 "layout (location=2) in vec4 a_colour;"
231 "out vec2 aTexCoords;"
237 "gl_Position = vec4( uPv * vec3( a_co, 1.0 ), 1.0 );"
238 "aTexCoords = a_uv * 0.0078125;"
239 "aColour = a_colour;"
248 "uniform sampler2D uTexImage;"
249 "out vec4 FragColor;"
251 "in vec2 aTexCoords;"
257 "vec4 colour = texture( uTexImage, aTexCoords );"
261 "float value = dot(vec4(1.0),colour)*0.25;"
263 "vec3 col = vec3(pow(cos(value*3.14159265*2.0)*0.5+0.5,0.5))"
264 "* vec3(step(value,0.5),0.3,step(1.0-value,0.5));"
265 "FragColor = vec4( col*4.0, 1.0 );"
268 "FragColor = colour;"
272 #define UI_GLYPH_SPACING_X 8
274 VG_STATIC
void _vg_ui_init(void)
276 if( !vg_shader_compile( &_shader_ui
) ||
277 !vg_shader_compile( &_shader_ui_image
) )
278 vg_fatal_error( "Failed to compile ui shader" );
282 * ----------------------------------------
285 vg_ui
.max_indices
= 20000;
286 vg_ui
.max_verts
= 30000;
288 /* Generate the buffer we are gonna be drawing to */
289 glGenVertexArrays( 1, &vg_ui
.vao
);
290 glGenBuffers( 1, &vg_ui
.vbo
);
291 glGenBuffers( 1, &vg_ui
.ebo
);
293 glBindVertexArray( vg_ui
.vao
);
294 glBindBuffer( GL_ARRAY_BUFFER
, vg_ui
.vbo
);
296 glBufferData( GL_ARRAY_BUFFER
,
297 vg_ui
.max_verts
* sizeof( struct ui_vert
),
298 NULL
, GL_DYNAMIC_DRAW
);
299 glBindVertexArray( vg_ui
.vao
);
301 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, vg_ui
.ebo
);
302 glBufferData( GL_ELEMENT_ARRAY_BUFFER
,
303 vg_ui
.max_indices
* sizeof( u16
), NULL
, GL_DYNAMIC_DRAW
);
308 u32
const stride
= sizeof( struct ui_vert
);
311 glVertexAttribPointer( 0, 2, GL_SHORT
, GL_FALSE
, stride
,
312 (void *)offsetof( struct ui_vert
, co
) );
313 glEnableVertexAttribArray( 0 );
316 glVertexAttribPointer( 1, 2, GL_UNSIGNED_BYTE
, GL_FALSE
, stride
,
317 (void *)offsetof( struct ui_vert
, uv
) );
318 glEnableVertexAttribArray( 1 );
321 glVertexAttribPointer( 2, 4, GL_UNSIGNED_BYTE
, GL_TRUE
, stride
,
322 (void *)offsetof( struct ui_vert
, colour
) );
323 glEnableVertexAttribArray( 2 );
327 /* Alloc RAM default context */
328 u32 vert_size
= vg_ui
.max_verts
*sizeof(struct ui_vert
),
329 inds_size
= vg_align8( vg_ui
.max_indices
*sizeof(u16
) );
331 vg_ui
.vertex_buffer
= vg_linear_alloc( vg_mem
.rtmemory
, vert_size
);
332 vg_ui
.indice_buffer
= vg_linear_alloc( vg_mem
.rtmemory
, inds_size
);
335 * -----------------------------------------------------
338 /* Load default font */
340 #include "vg/vg_pxfont_thin.h"
343 u32 pixels
= 0, total
= 256*256, data
= 0;
346 while( pixels
< total
){
347 for( int b
= 31; b
>= 0; b
-- ){
348 image
[ pixels
++ ] = (compressed
[data
] & (0x1u
<< b
))? 0xffu
: 0x00u
;
350 if( pixels
>= total
){
358 glGenTextures( 1, &vg_ui
.tex_glyphs
);
359 glBindTexture( GL_TEXTURE_2D
, vg_ui
.tex_glyphs
);
360 glTexImage2D( GL_TEXTURE_2D
, 0, GL_R8
, 256, 256, 0,
361 GL_RED
, GL_UNSIGNED_BYTE
, image
);
364 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
365 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
366 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
367 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
371 * ---------------------------------------------------------------
374 vg_ui
.cursor_map
[ k_ui_cursor_default
] =
375 SDL_CreateSystemCursor( SDL_SYSTEM_CURSOR_ARROW
);
376 vg_ui
.cursor_map
[ k_ui_cursor_hand
] =
377 SDL_CreateSystemCursor( SDL_SYSTEM_CURSOR_HAND
);
378 vg_ui
.cursor_map
[ k_ui_cursor_ibeam
] =
379 SDL_CreateSystemCursor( SDL_SYSTEM_CURSOR_IBEAM
);
387 static void rect_copy( ui_rect a
, ui_rect b
)
389 for( int i
=0; i
<4; i
++ )
393 VG_STATIC
void ui_flush( enum ui_shader shader
)
395 u32 vertex_offset
= vg_ui
.vert_start
*sizeof(ui_vert
),
396 vertex_count
= vg_ui
.cur_vert
-vg_ui
.vert_start
,
397 vertex_size
= vertex_count
*sizeof(ui_vert
),
399 indice_offset
= vg_ui
.indice_start
*sizeof(u16
),
400 indice_count
= vg_ui
.cur_indice
-vg_ui
.indice_start
,
401 indice_size
= indice_count
* sizeof(u16
);
403 if( !vertex_size
|| !indice_size
)
406 glBindVertexArray( vg_ui
.vao
);
407 glBindBuffer( GL_ARRAY_BUFFER
, vg_ui
.vbo
);
408 glBufferSubData( GL_ARRAY_BUFFER
, vertex_offset
, vertex_size
,
409 vg_ui
.vertex_buffer
+vg_ui
.vert_start
);
411 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, vg_ui
.ebo
);
412 glBufferSubData( GL_ELEMENT_ARRAY_BUFFER
, indice_offset
, indice_size
,
413 vg_ui
.indice_buffer
+vg_ui
.indice_start
);
415 glEnable( GL_BLEND
);
416 glBlendFunc( GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
417 glBlendEquation( GL_FUNC_ADD
);
419 m3x3f view
= M3X3_IDENTITY
;
420 m3x3_translate( view
, (v3f
){ -1.0f
, 1.0f
, 0.0f
} );
421 m3x3_scale( view
, (v3f
){ 1.0f
/((float)vg
.window_x
*0.5f
),
422 -1.0f
/((float)vg
.window_y
*0.5f
), 1.0f
} );
424 if( shader
== k_ui_shader_colour
){
425 glUseProgram( _shader_ui
.id
);
427 glUniformMatrix3fv( glGetUniformLocation( _shader_ui
.id
, "uPv" ), 1,
428 GL_FALSE
, (float *)view
);
430 glActiveTexture( GL_TEXTURE0
);
431 glBindTexture( GL_TEXTURE_2D
, vg_ui
.tex_glyphs
);
432 glUniform1i( glGetUniformLocation( _shader_ui
.id
, "uTexGlyphs" ), 0 );
434 else if( shader
== k_ui_shader_image
){
435 glUseProgram( _shader_ui_image
.id
);
436 glUniformMatrix3fv( glGetUniformLocation( _shader_ui_image
.id
, "uPv" ), 1,
437 GL_FALSE
, (float *)view
);
438 glUniform1i( glGetUniformLocation(_shader_ui_image
.id
,"uTexImage"), 0 );
441 vg_fatal_error( "Invalid UI shader (%d)\n", shader
);
443 glDrawElements( GL_TRIANGLES
, indice_count
, GL_UNSIGNED_SHORT
,
444 (void *)(vg_ui
.indice_start
*sizeof(u16
)) );
446 glDisable( GL_BLEND
);
448 vg_ui
.indice_start
= vg_ui
.cur_indice
;
449 vg_ui
.vert_start
= vg_ui
.cur_vert
;
452 static void ui_fill_rect( ui_rect rect
, u32 colour
, ui_px uv
[4] )
454 /* this if far from ideal but stops us from crashing */
455 if( (vg_ui
.cur_vert
+ 4 > vg_ui
.max_verts
) ||
456 (vg_ui
.cur_indice
+ 6 > vg_ui
.max_indices
))
459 struct ui_vert
*vertices
= &vg_ui
.vertex_buffer
[ vg_ui
.cur_vert
];
460 u16
*indices
= &vg_ui
.indice_buffer
[ vg_ui
.cur_indice
];
462 for( int i
=0; i
<4; i
++ ){
463 vertices
[i
].colour
= colour
;
466 vertices
[0].co
[0] = rect
[0];
467 vertices
[0].co
[1] = rect
[1];
468 vertices
[0].uv
[0] = uv
[0];
469 vertices
[0].uv
[1] = uv
[1];
470 vertices
[1].co
[0] = rect
[0]+rect
[2];
471 vertices
[1].co
[1] = rect
[1];
472 vertices
[1].uv
[0] = uv
[2];
473 vertices
[1].uv
[1] = uv
[1];
474 vertices
[2].co
[0] = rect
[0]+rect
[2];
475 vertices
[2].co
[1] = rect
[1]+rect
[3];
476 vertices
[2].uv
[0] = uv
[2];
477 vertices
[2].uv
[1] = uv
[3];
478 vertices
[3].co
[0] = rect
[0];
479 vertices
[3].co
[1] = rect
[1]+rect
[3];
480 vertices
[3].uv
[0] = uv
[0];
481 vertices
[3].uv
[1] = uv
[3];
482 u16 ind_start
= vg_ui
.cur_vert
;
484 u16 start
= vg_ui
.cur_vert
;
485 u32 mesh
[] = { 0,2,1, 0,3,2 };
487 for( u32 i
=0; i
<vg_list_size(mesh
); i
++ ){
488 indices
[i
] = start
+mesh
[i
];
491 vg_ui
.cur_indice
+= 6;
495 static void ui_fill( ui_rect rect
, u32 colour
)
497 ui_fill_rect( rect
, colour
, (ui_px
[4]){ 4,4,4,4 } );
500 static void ui_outline( ui_rect rect
, ui_px thickness
, u32 colour
)
502 /* this if far from ideal but stops us from crashing */
503 if( (vg_ui
.cur_vert
+ 8 > vg_ui
.max_verts
) ||
504 (vg_ui
.cur_indice
+ 24 > vg_ui
.max_indices
))
507 struct ui_vert
*vertices
= &vg_ui
.vertex_buffer
[ vg_ui
.cur_vert
];
508 u16
*indices
= &vg_ui
.indice_buffer
[ vg_ui
.cur_indice
];
510 for( int i
=0; i
<8; i
++ ){
511 vertices
[i
].uv
[0] = 4;
512 vertices
[i
].uv
[1] = 4;
513 vertices
[i
].colour
= colour
;
516 vertices
[0].co
[0] = rect
[0];
517 vertices
[0].co
[1] = rect
[1];
518 vertices
[1].co
[0] = rect
[0]+rect
[2];
519 vertices
[1].co
[1] = rect
[1];
520 vertices
[2].co
[0] = rect
[0]+rect
[2];
521 vertices
[2].co
[1] = rect
[1]+rect
[3];
522 vertices
[3].co
[0] = rect
[0];
523 vertices
[3].co
[1] = rect
[1]+rect
[3];
524 vertices
[4].co
[0] = vertices
[0].co
[0]-thickness
;
525 vertices
[4].co
[1] = vertices
[0].co
[1]-thickness
;
526 vertices
[5].co
[0] = vertices
[1].co
[0]+thickness
;
527 vertices
[5].co
[1] = vertices
[1].co
[1]-thickness
;
528 vertices
[6].co
[0] = vertices
[2].co
[0]+thickness
;
529 vertices
[6].co
[1] = vertices
[2].co
[1]+thickness
;
530 vertices
[7].co
[0] = vertices
[3].co
[0]-thickness
;
531 vertices
[7].co
[1] = vertices
[3].co
[1]+thickness
;
533 u16 start
= vg_ui
.cur_vert
;
534 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 };
536 for( u32 i
=0; i
<vg_list_size(mesh
); i
++ ){
537 indices
[i
] = start
+mesh
[i
];
540 vg_ui
.cur_indice
+= 24;
544 static void ui_split_px( ui_rect rect
,
545 enum ui_axis other
, ui_px width
, ui_px pad
,
546 ui_rect l
, ui_rect r
)
548 enum ui_axis dir
= other
^ 0x1;
551 rect_copy( rect
, temp
);
553 l
[ dir
] = temp
[ dir
] + pad
;
554 r
[ dir
] = temp
[ dir
] + width
+ (pad
/2);
555 l
[ other
] = temp
[ other
] + pad
;
556 r
[ other
] = temp
[ other
] + pad
;
557 l
[ 2+dir
] = width
- ((3*pad
)/2);
558 r
[ 2+dir
] = temp
[ 2+dir
] - width
- ((3*pad
)/2);
559 l
[ 2+other
] = temp
[ 2+other
] - pad
*2;
560 r
[ 2+other
] = temp
[ 2+other
] - pad
*2;
563 static void ui_rect_center( ui_rect parent
, ui_rect rect
)
565 rect
[0] = parent
[0] + (parent
[2]-rect
[2])/2;
566 rect
[1] = parent
[1] + (parent
[3]-rect
[3])/2;
569 static void ui_fit_item( ui_rect rect
, ui_px size
[2], ui_rect d
)
571 i32 rp
= (i32
)rect
[2] * (i32
)size
[1],
572 rc
= (i32
)size
[0] * (i32
)rect
[3];
574 enum ui_axis dir
, other
;
575 if( rc
> rp
) dir
= k_ui_axis_h
;
576 else dir
= k_ui_axis_v
;
579 d
[2+dir
] = rect
[2+dir
];
580 d
[2+other
] = (rect
[2+dir
] * size
[other
]) / size
[dir
];
582 ui_rect_center( rect
, d
);
585 static void ui_split_ratio( ui_rect rect
, enum ui_axis dir
, float ratio
,
586 ui_px pad
, ui_rect l
, ui_rect r
)
588 ui_px width
= (float)rect
[ 2+(dir
^0x1) ] * ratio
;
589 ui_split_px( rect
, dir
, width
, pad
, l
, r
);
592 static void ui_rect_pad( ui_rect rect
, ui_px pad
)
600 static ui_px
ui_text_line_width( const char *str
)
603 const char *_c
= str
;
606 while( (c
= *(_c
++)) ){
607 if( c
>= 32 && c
<= 126 )
616 static ui_px
ui_text_string_height( const char *str
)
619 const char *_c
= str
;
622 while( (c
= *(_c
++)) ){
623 if( c
== '\n' ) height
++;
629 static ui_px
ui_text_aligned_x( const char *str
, ui_rect rect
, ui_px scale
,
630 enum ui_align align
)
632 if( align
== k_ui_align_left
){
636 ui_px width
= ui_text_line_width( str
) * scale
;
638 if( align
== k_ui_align_right
)
639 return rect
[0] + rect
[2]-width
;
641 return rect
[0] + (rect
[2]-width
)/2;
645 static ui_px
ui_min( ui_px a
, ui_px b
){ return a
<b
?a
:b
; }
646 static ui_px
ui_max( ui_px a
, ui_px b
){ return a
>b
?a
:b
; }
647 static ui_px
ui_clamp( ui_px a
, ui_px min
, ui_px max
)
649 return ui_min( max
, ui_max( a
, min
) );
652 static int ui_clip( ui_rect parent
, ui_rect child
, ui_rect clipped
)
654 ui_px parent_max
[2], child_max
[2];
655 parent_max
[0] = parent
[0]+parent
[2];
656 parent_max
[1] = parent
[1]+parent
[3];
657 child_max
[0] = child
[0]+child
[2];
658 child_max
[1] = child
[1]+child
[3];
660 clipped
[0] = ui_clamp( child
[0], parent
[0], parent_max
[0] );
661 clipped
[1] = ui_clamp( child
[1], parent
[1], parent_max
[1] );
662 clipped
[2] = ui_clamp( child_max
[0], parent
[0], parent_max
[0] );
663 clipped
[3] = ui_clamp( child_max
[1], parent
[1], parent_max
[1] );
665 if( clipped
[0] == clipped
[2] ||
666 clipped
[1] == clipped
[3] )
669 clipped
[2] -= clipped
[0];
670 clipped
[3] -= clipped
[1];
675 static int ui_inside_rect( ui_rect rect
, ui_px co
[2] )
677 if( co
[0] >= rect
[0] &&
679 co
[0] <= rect
[0]+rect
[2] &&
680 co
[1] <= rect
[1]+rect
[3] )
688 static int ui_click_down(void)
690 if( vg_ui
.ignore_input_frames
) return 0;
692 if( (vg_ui
.mouse_state
[0] & SDL_BUTTON(SDL_BUTTON_LEFT
)) &&
693 !(vg_ui
.mouse_state
[1] & SDL_BUTTON(SDL_BUTTON_LEFT
)) )
699 static int ui_clicking(void)
701 if( vg_ui
.ignore_input_frames
) return 0;
702 return vg_ui
.mouse_state
[0] & SDL_BUTTON(SDL_BUTTON_LEFT
);
705 static int ui_click_up(void)
707 if( vg_ui
.ignore_input_frames
) return 0;
708 if( (vg_ui
.mouse_state
[1] & SDL_BUTTON(SDL_BUTTON_LEFT
)) &&
709 !(vg_ui
.mouse_state
[0] & SDL_BUTTON(SDL_BUTTON_LEFT
)) )
715 static void ui_prerender(void)
718 vg_ui
.mouse_state
[1] = vg_ui
.mouse_state
[0];
719 vg_ui
.mouse_state
[0] = SDL_GetMouseState( &x
, &y
);
724 vg_ui
.cur_indice
= 0;
725 vg_ui
.vert_start
= 0;
726 vg_ui
.indice_start
= 0;
727 vg_ui
.focused_control_hit
= 0;
728 vg_ui
.cursor
= k_ui_cursor_default
;
730 if( vg_ui
.ignore_input_frames
){
731 vg_ui
.ignore_input_frames
--;
735 if( ui_click_down() ){
736 vg_ui
.mouse_click
[0] = vg_ui
.mouse
[0];
737 vg_ui
.mouse_click
[1] = vg_ui
.mouse
[1];
741 static u32
ui_colour( enum ui_scheme_colour id
)
743 return vg_ui
.scheme
[ id
];
746 /* get an appropriately contrasting colour given the base */
747 static u32
ui_colourcont( enum ui_scheme_colour id
)
749 if ( id
< k_ui_bg
+6 ) return ui_colour( k_ui_fg
);
750 else if( id
< k_ui_fg
) return ui_colour( k_ui_bg
+1 );
751 else if( id
< k_ui_hue
) return ui_colour( k_ui_bg
+3 );
752 else if( id
< k_ui_red
+k_ui_brighter
) return ui_colour( k_ui_fg
);
753 else return ui_colour( k_ui_fg
+1 );
756 static void ui_text( ui_rect rect
, const char *str
, ui_px scale
,
757 enum ui_align align
, u32 colour
)
760 if( colour
== 0 ) colour
= ui_colour( k_ui_fg
);
762 colour
&= 0x00ffffff;
764 const char *_c
= str
;
767 text_cursor
[0] = ui_text_aligned_x( str
, rect
, scale
, align
);
768 text_cursor
[1] = rect
[1];
769 text_cursor
[2] = 8*scale
;
770 text_cursor
[3] = 14*scale
;
772 if( align
& (k_ui_align_middle
|k_ui_align_bottom
) ){
773 ui_px height
= ui_text_string_height( str
) * scale
;
775 if( align
& k_ui_align_bottom
)
776 text_cursor
[1] += rect
[3]-height
;
778 text_cursor
[1] += (rect
[3]-height
)/2;
781 while( (c
= *(_c
++)) ){
783 text_cursor
[1] += 14*scale
;
784 text_cursor
[0] = ui_text_aligned_x( _c
, rect
, scale
, align
);
790 glyph_base
[0] = glyph_index
& 0xf;
791 glyph_base
[1] = (glyph_index
-glyph_base
[0])>>4;
798 if( ui_clip( rect
, text_cursor
, rect_char
) ){
799 ui_fill_rect( rect_char
, colour
, (ui_px
[4])
808 else if( c
== '\x1B' ){
812 for( int i
=0; i
<3; i
++ ){
818 case '0': colour
= ui_colour( k_ui_fg
); break;
819 case '3'|'0'<<8: colour
= ui_colour( k_ui_bg
); break;
820 case '3'|'1'<<8: colour
= ui_colour( k_ui_red
); break;
821 case '3'|'2'<<8: colour
= ui_colour( k_ui_green
); break;
822 case '3'|'3'<<8: colour
= ui_colour( k_ui_yellow
); break;
823 case '3'|'4'<<8: colour
= ui_colour( k_ui_blue
); break;
824 case '3'|'5'<<8: colour
= ui_colour( k_ui_purple
); break;
825 case '3'|'6'<<8: colour
= ui_colour( k_ui_aqua
); break;
826 case '3'|'7'<<8: colour
= ui_colour( 0xffffffff ); break;
829 colour
&= 0x00ffffff;
833 colour_id
|= _c
[i
] << (i
*8);
843 else if( c
== '\t' ){
844 text_cursor
[0] += UI_GLYPH_SPACING_X
*scale
*4;
848 text_cursor
[0] += UI_GLYPH_SPACING_X
*scale
;
852 static void ui_image( ui_rect rect
, GLuint image
)
854 ui_flush( k_ui_shader_colour
);
856 glActiveTexture( GL_TEXTURE0
);
857 glBindTexture( GL_TEXTURE_2D
, image
);
858 ui_fill_rect( rect
, 0xffffffff, (ui_px
[4]){ 0,0, 255,255 } );
859 ui_flush( k_ui_shader_image
);
862 static u32
v4f_u32_colour( v4f colour
)
864 u32 r
= colour
[0] * 255.0f
,
865 g
= colour
[1] * 255.0f
,
866 b
= colour
[2] * 255.0f
,
867 a
= colour
[3] * 255.0f
;
869 return r
| (g
<<8) | (b
<<16) | (a
<<24);
872 static void ui_defocus_all(void)
874 if( vg_ui
.focused_control_type
== k_ui_control_textbox
){
875 vg_info( "SDL_StopTextInput()\n" );
879 vg_ui
.focused_control_id
= NULL
;
880 vg_ui
.focused_control_hit
= 0;
881 vg_ui
.focused_control_type
= k_ui_control_none
;
884 static int ui_button( ui_rect rect
, enum ui_scheme_colour colour
)
886 int clickup
= ui_click_up(),
887 click
= ui_clicking() | clickup
,
888 target
= ui_inside_rect( rect
, vg_ui
.mouse_click
) && click
,
889 hover
= ui_inside_rect( rect
, vg_ui
.mouse
);
891 u32 col_base
= vg_ui
.scheme
[ colour
],
892 col_highlight
= vg_ui
.scheme
[ k_ui_fg
],
893 col_hover
= vg_ui
.scheme
[ colour
+ k_ui_brighter
];
896 vg_ui
.cursor
= k_ui_cursor_hand
;
903 ui_fill( rect
, col_highlight
);
904 vg_ui
.ignore_input_frames
= 2;
905 rect_copy( rect
, vg_ui
.click_fader
);
907 rect_copy( rect
, vg_ui
.click_fader_end
);
908 vg_ui
.click_fader_end
[3] = 0;
909 ui_rect_center( rect
, vg_ui
.click_fader_end
);
911 vg_ui
.click_fade_opacity
= 1.0f
;
916 ui_fill( rect
, col_highlight
);
921 ui_fill( rect
, col_base
);
922 ui_outline( rect
, 1, col_highlight
);
927 ui_fill( rect
, col_base
);
933 ui_fill( rect
, col_hover
);
937 ui_fill( rect
, col_base
);
943 static int ui_button_text( ui_rect rect
, const char *string
, ui_px scale
,
944 enum ui_scheme_colour colour
)
946 int result
= ui_button( rect
, colour
);
947 ui_rect t
= { 0,0, ui_text_line_width( string
)*scale
, 14*scale
};
948 ui_rect_center( rect
, t
);
949 ui_text( t
, string
, scale
, k_ui_align_left
, ui_colourcont(colour
) );
953 static void ui_postrender(void)
955 if( vg_ui
.click_fade_opacity
> 0.0f
){
957 float scale
= vg_ui
.click_fade_opacity
;
958 scale
= vg_maxf( 1.0f
/255.0f
, scale
*scale
);
960 vg_ui
.click_fade_opacity
-= vg
.time_frame_delta
* 3.8f
;
961 u32 colour
= (0x00ffffff & ui_colour(k_ui_fg
))|0x7f000000;
963 v4f begin
, end
, dest
;
964 for( int i
=0; i
<4; i
++ ){
965 begin
[i
] = vg_ui
.click_fader
[i
];
966 end
[i
] = vg_ui
.click_fader_end
[i
]+1;
969 v4_lerp( end
, begin
, scale
, dest
);
972 for( int i
=0; i
<4; i
++ ){
976 ui_fill( rect
, colour
);
978 ui_flush( k_ui_shader_colour
);
980 if( !vg_ui
.focused_control_hit
){
984 SDL_SetCursor( vg_ui
.cursor_map
[ vg_ui
.cursor
] );
990 static void ui_dev_colourview(void)
992 ui_rect window
= {vg
.window_x
-256,0,256,vg
.window_y
}, swatch
;
994 const char *names
[vg_list_size(vg_ui
.scheme
)] = {
995 [k_ui_bg
] = "k_ui_bg", "k_ui_bg+1", "k_ui_bg+2", "k_ui_bg+3",
996 "k_ui_bg+4", "k_ui_bg+5", "k_ui_bg+6", "k_ui_bg+7",
998 [k_ui_fg
] = "k_ui_fg", "k_ui_fg+1", "k_ui_fg+2", "k_ui_fg+3",
999 "k_ui_fg+4", "k_ui_fg+5", "k_ui_fg+6", "k_ui_fg+7",
1001 [k_ui_red
] = "k_ui_red", "k_ui_orange", "k_ui_yellow", "k_ui_green",
1002 "k_ui_aqua", "k_ui_blue", "k_ui_purple", "k_ui_gray",
1003 "k_ui_red+8","k_ui_orange+8","k_ui_yellow+8","k_ui_green+8",
1004 "k_ui_aqua+8","k_ui_blue+8","k_ui_purple+8","k_ui_gray+8" };
1007 ui_split_ratio( window
, k_ui_axis_v
, 0.5f
, 0.0f
, col
[0], col
[1] );
1009 for( int i
=0; i
<vg_list_size(vg_ui
.scheme
); i
++ ){
1010 int which
= (i
/8)%2;
1012 ui_split_px( col
[which
], k_ui_axis_h
, 24, 0, swatch
, col
[which
] );
1013 ui_fill( swatch
, ui_colour(i
) );
1016 ui_text(swatch
, names
[i
], 1, k_ui_align_middle_left
, ui_colourcont(i
));
1025 * text box interface
1027 static void _ui_textbox_make_selection( int *start
, int *end
)
1029 *start
= VG_MIN( vg_ui
.cursor_pos
, vg_ui
.cursor_user
);
1030 *end
= VG_MAX( vg_ui
.cursor_pos
, vg_ui
.cursor_user
);
1033 static void _ui_textbox_move_cursor( int *cursor0
, int *cursor1
,
1034 int dir
, int snap_together
)
1036 *cursor0
= VG_MAX( 0, vg_ui
.cursor_user
+ dir
);
1039 VG_MIN( vg_ui
.textbuf_len
-1, strlen( vg_ui
.textbuf
)),
1043 *cursor1
= *cursor0
;
1046 static int _ui_textbox_makeroom( int datastart
, int length
)
1048 int move_to
= VG_MIN( datastart
+length
, vg_ui
.textbuf_len
-1 );
1049 int move_amount
= strlen( vg_ui
.textbuf
)-datastart
;
1051 VG_MIN( move_to
+move_amount
, vg_ui
.textbuf_len
-1 );
1052 move_amount
= move_end
-move_to
;
1055 memmove( &vg_ui
.textbuf
[ move_to
],
1056 &vg_ui
.textbuf
[ datastart
],
1059 vg_ui
.textbuf
[ move_end
] = '\0';
1061 return VG_MIN( length
, vg_ui
.textbuf_len
-datastart
-1 );
1064 static int _ui_textbox_delete_char( int direction
)
1067 _ui_textbox_make_selection( &start
, &end
);
1069 /* There is no selection */
1071 if( direction
== 1 ) end
= VG_MIN( end
+1, strlen( vg_ui
.textbuf
) );
1072 else if( direction
== -1 ) start
= VG_MAX( start
-1, 0 );
1075 /* Still no selction, no need to do anything */
1079 /* Copy the end->terminator to start */
1080 int remaining_length
= strlen( vg_ui
.textbuf
)+1-end
;
1081 memmove( &vg_ui
.textbuf
[ start
],
1082 &vg_ui
.textbuf
[ end
],
1087 static void _ui_textbox_to_clipboard(void)
1090 _ui_textbox_make_selection( &start
, &end
);
1094 memcpy( buffer
, &vg_ui
.textbuf
[ start
], end
-start
);
1095 buffer
[ end
-start
] = 0x00;
1096 SDL_SetClipboardText( buffer
);
1100 static void _ui_textbox_clipboard_paste(void)
1102 if( !SDL_HasClipboardText() )
1105 char *text
= SDL_GetClipboardText();
1110 int datastart
= _ui_textbox_delete_char( 0 );
1111 int length
= strlen( text
);
1112 int cpylength
= _ui_textbox_makeroom( datastart
, length
);
1114 memcpy( vg_ui
.textbuf
+ datastart
, text
, cpylength
);
1115 _ui_textbox_move_cursor( &vg_ui
.cursor_user
,
1116 &vg_ui
.cursor_pos
, cpylength
, 1 );
1120 static void _ui_textbox_put_char( char c
)
1122 vg_ui
.cursor_user
= _ui_textbox_delete_char(0);
1124 if( _ui_textbox_makeroom( vg_ui
.cursor_user
, 1 ) )
1125 vg_ui
.textbuf
[ vg_ui
.cursor_user
] = c
;
1127 _ui_textbox_move_cursor( &vg_ui
.cursor_user
, &vg_ui
.cursor_pos
, 1, 1 );
1130 /* Receed secondary cursor */
1131 static void _ui_textbox_left_select(void)
1133 _ui_textbox_move_cursor( &vg_ui
.cursor_user
, NULL
, -1, 0 );
1136 /* Match and receed both cursors */
1137 static void _ui_textbox_left(void)
1139 int cursor_diff
= vg_ui
.cursor_pos
- vg_ui
.cursor_user
? 0: 1;
1141 _ui_textbox_move_cursor( &vg_ui
.cursor_user
,
1142 &vg_ui
.cursor_pos
, -cursor_diff
, 1 );
1145 static void _ui_textbox_right_select(void)
1147 _ui_textbox_move_cursor( &vg_ui
.cursor_user
, NULL
, 1, 0 );
1150 static void _ui_textbox_right(void)
1152 int cursor_diff
= vg_ui
.cursor_pos
- vg_ui
.cursor_user
? 0: 1;
1154 _ui_textbox_move_cursor( &vg_ui
.cursor_user
,
1155 &vg_ui
.cursor_pos
, +cursor_diff
, 1 );
1158 static void _ui_textbox_backspace(void)
1160 vg_ui
.cursor_user
= _ui_textbox_delete_char( -1 );
1161 vg_ui
.cursor_pos
= vg_ui
.cursor_user
;
1164 static void _ui_textbox_delete(void)
1166 vg_ui
.cursor_user
= _ui_textbox_delete_char( 1 );
1167 vg_ui
.cursor_pos
= vg_ui
.cursor_user
;
1170 static void _ui_textbox_home_select(void)
1172 _ui_textbox_move_cursor( &vg_ui
.cursor_user
, NULL
, -10000, 0 );
1175 static void _ui_textbox_home(void)
1177 _ui_textbox_move_cursor( &vg_ui
.cursor_user
,
1178 &vg_ui
.cursor_pos
, -10000, 1 );
1181 static void _ui_textbox_end_select(void)
1183 _ui_textbox_move_cursor( &vg_ui
.cursor_user
, NULL
, 10000, 0 );
1186 static void _ui_textbox_end(void)
1188 _ui_textbox_move_cursor( &vg_ui
.cursor_user
,
1190 vg_ui
.textbuf_len
-1, 1 );
1193 static void _ui_textbox_select_all(void)
1195 _ui_textbox_move_cursor( &vg_ui
.cursor_user
, NULL
, 10000, 0);
1196 _ui_textbox_move_cursor( &vg_ui
.cursor_pos
, NULL
, -10000, 0);
1199 static void _ui_textbox_cut(void)
1201 _ui_textbox_to_clipboard();
1202 vg_ui
.cursor_user
= _ui_textbox_delete_char(0);
1203 vg_ui
.cursor_pos
= vg_ui
.cursor_user
;
1206 static void _ui_textbox_enter(void)
1208 if( !strlen( vg_ui
.textbuf
) )
1215 static void _ui_proc_key( SDL_Keysym ev
)
1217 struct textbox_mapping
1222 void (*handler
)(void);
1226 { 0, SDLK_LEFT
, _ui_textbox_left
},
1227 { KMOD_SHIFT
, SDLK_LEFT
, _ui_textbox_left_select
},
1228 { 0, SDLK_RIGHT
, _ui_textbox_right
},
1229 { KMOD_SHIFT
, SDLK_RIGHT
, _ui_textbox_right_select
},
1231 { 0, SDLK_DOWN
, _ui_textbox_down
},
1232 { 0, SDLK_UP
, _ui_textbox_up
},
1234 { 0, SDLK_BACKSPACE
, _ui_textbox_backspace
},
1235 { 0, SDLK_DELETE
, _ui_textbox_delete
},
1236 { 0, SDLK_HOME
, _ui_textbox_home
},
1237 { KMOD_SHIFT
, SDLK_HOME
, _ui_textbox_home_select
},
1238 { 0, SDLK_END
, _ui_textbox_end
},
1239 { KMOD_SHIFT
, SDLK_END
, _ui_textbox_end_select
},
1240 { KMOD_CTRL
, SDLK_a
, _ui_textbox_select_all
},
1241 { KMOD_CTRL
, SDLK_c
, _ui_textbox_to_clipboard
},
1242 { KMOD_CTRL
, SDLK_x
, _ui_textbox_cut
},
1243 { KMOD_CTRL
, SDLK_v
, _ui_textbox_clipboard_paste
},
1244 { 0, SDLK_RETURN
, _ui_textbox_enter
},
1246 { KMOD_CTRL
, SDLK_n
, _ui_textbox_suggest_next
},
1247 { KMOD_CTRL
, SDLK_p
, _ui_textbox_suggest_prev
}
1249 { 0, SDLK_ESCAPE
, ui_defocus_all
}
1254 if( ev
.mod
& KMOD_SHIFT
)
1257 if( ev
.mod
& KMOD_CTRL
)
1260 if( ev
.mod
& KMOD_ALT
)
1263 for( int i
=0; i
<vg_list_size( mappings
); i
++ ){
1264 struct textbox_mapping
*mapping
= &mappings
[i
];
1266 if( mapping
->key
== ev
.sym
){
1267 if( mapping
->mod
== 0 ){
1273 else if( (mod
& mapping
->mod
) == mapping
->mod
){
1282 * Callback for text entry mode
1284 VG_STATIC
void ui_proc_utf8( const char *text
)
1286 const char *ptr
= text
;
1289 if( *ptr
!= '`' ) _ui_textbox_put_char( *ptr
);
1316 static void _ui_textbox_cursor_rect( ui_rect text_rect
, ui_rect cursor
)
1318 int start
= VG_MIN( vg_ui
.cursor_pos
, vg_ui
.cursor_user
),
1319 end
= VG_MAX( vg_ui
.cursor_pos
, vg_ui
.cursor_user
);
1321 cursor
[0] = text_rect
[0] + start
*UI_GLYPH_SPACING_X
-1;
1322 cursor
[1] = text_rect
[1];
1323 cursor
[2] = start
== end
? 2: (float)(end
-start
)*(float)UI_GLYPH_SPACING_X
;
1324 cursor
[3] = text_rect
[3];
1330 static int ui_textbox( ui_rect rect
, char *buf
, u32 len
)
1332 int clickup
= ui_click_up(),
1333 click
= ui_clicking() | clickup
,
1334 target
= ui_inside_rect( rect
, vg_ui
.mouse_click
) && click
,
1335 hover
= ui_inside_rect( rect
, vg_ui
.mouse
);
1337 u32 col_base
= ui_colour( k_ui_bg
),
1338 col_highlight
= ui_colour( k_ui_fg
),
1339 col_cursor
= (0x00ffffff & ui_colour(k_ui_fg
))|0x7f000000;
1344 rect_copy( rect
, text_rect
);
1347 ui_rect_center( rect
, text_rect
);
1350 vg_ui
.cursor
= k_ui_cursor_ibeam
;
1353 if( vg_ui
.focused_control_id
== buf
){
1354 ui_fill( rect
, col_base
);
1355 ui_text( text_rect
, buf
, 1, k_ui_align_left
, 0 );
1357 if( (clickup
||ui_click_down()) && !target
){
1361 vg_ui
.focused_control_hit
= 1;
1363 if( click
&& target
){
1364 ui_px d0
= vg_ui
.mouse_click
[0] - text_rect
[0],
1365 d1
= vg_ui
.mouse
[0] - text_rect
[0];
1367 int max
= strlen( vg_ui
.textbuf
);
1369 vg_ui
.cursor_pos
= VG_MAX( 0, VG_MIN( max
, d0
/UI_GLYPH_SPACING_X
)),
1370 vg_ui
.cursor_user
= VG_MAX( 0, VG_MIN( max
, d1
/UI_GLYPH_SPACING_X
));
1373 ui_outline( rect
, -2, vg_ui
.scheme
[ k_ui_orange
] );
1376 _ui_textbox_cursor_rect( text_rect
, cursor
);
1377 rect_copy( cursor
, vg_ui
.click_fader_end
);
1379 if( (vg_ui
.click_fade_opacity
<=0.0f
) &&
1380 ui_clip( rect
, cursor
, cursor
) ){
1381 ui_fill( cursor
, col_cursor
);
1389 if( target
&& hover
){
1390 ui_fill( rect
, col_highlight
);
1391 vg_ui
.ignore_input_frames
= 2;
1392 rect_copy( rect
, vg_ui
.click_fader
);
1393 _ui_textbox_cursor_rect( text_rect
, vg_ui
.click_fader_end
);
1395 vg_ui
.click_fade_opacity
= 1.0f
;
1396 vg_ui
.textbuf
= buf
;
1397 vg_ui
.focused_control_hit
= 1;
1398 vg_ui
.focused_control_type
= k_ui_control_textbox
;
1399 vg_ui
.textbuf_len
= len
;
1400 vg_info( "SDL_StartTextInput()\n" );
1401 SDL_StartTextInput();
1405 ui_fill( rect
, col_base
);
1408 ui_outline( rect
, -1, col_highlight
);
1411 ui_text( text_rect
, buf
, 1, k_ui_align_left
, 0 );
1415 #endif /* VG_IMGUI_H */