1 /* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */
3 SHADER_DEFINE( shader_ui
,
5 "layout (location=0) in vec2 a_co;"
6 "layout (location=1) in vec2 a_uv;"
7 "layout (location=2) in vec4 a_colour;"
8 "layout (location=3) in vec4 a_clip;"
11 "out vec2 aTexCoords;"
18 "gl_Position = vec4( uPv * vec3( a_co, 1.0 ), 1.0 );"
19 "aTexCoords = a_uv * 0.0078125;"
27 "uniform sampler2D uTexGlyphs;"
38 "float clip_blend = step( aWsp.x, aClip.z ) *"
39 "step( aWsp.y, aClip.w ) *"
40 "step( aClip.x, aWsp.x ) *"
41 "step( aClip.y, aWsp.y ); "
43 "vec4 glyph = vec4(1.0,1.0,1.0,1.0);"
45 "if( aColour.a == 0.0 )"
47 "glyph = texture( uTexGlyphs, aTexCoords );"
48 "glyph.a = smoothstep( 0.47, 0.53, glyph.r );"
52 "glyph.a = aColour.a;"
55 "FragColor = vec4( aColour.rgb, glyph.a*clip_blend );"
58 UNIFORMS({ "uPv", "uTexGlyphs" })
62 typedef u32 ui_colour
;
63 typedef ui_px ui_rect
[4];
64 typedef struct ui_ctx ui_ctx
;
65 typedef struct ui_colourset ui_colourset
;
67 /* Relative to cursor p0 */
70 k_text_align_left
= 0,
71 k_text_align_right
= 1,
72 k_text_align_center
= 2
123 u32 capture_mouse_id
;
130 int click_state
; /* 0: released, 1: on down, 2: pressed, 3: on release */
132 ui_colourset
*colours
;
147 #define UI_GLYPH_SPACING_X 9
149 static GLuint ui_glyph_texture
= 0;
150 static ui_colourset ui_default_colours
= {
156 static ui_ctx ui_global_ctx
;
158 static void ui_init_context( ui_ctx
*ctx
, int index_buffer_size
)
160 u32 vertex_buffer_size
= (index_buffer_size
+(index_buffer_size
/2));
162 /* Generate the buffer we are gonna be drawing to */
164 glGenVertexArrays(1, &ctx
->vao
);
165 glGenBuffers( 1, &ctx
->vbo
);
166 glGenBuffers( 1, &ctx
->ebo
);
167 glBindVertexArray( ctx
->vao
);
169 glBindBuffer( GL_ARRAY_BUFFER
, ctx
->vbo
);
171 glBufferData( GL_ARRAY_BUFFER
,
172 vertex_buffer_size
* sizeof( struct ui_vert
),
173 NULL
, GL_DYNAMIC_DRAW
);
174 glBindVertexArray( ctx
->vao
);
176 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, ctx
->ebo
);
177 glBufferData( GL_ELEMENT_ARRAY_BUFFER
,
178 index_buffer_size
* sizeof( u16
), NULL
, GL_DYNAMIC_DRAW
);
180 u32
const stride
= sizeof( struct ui_vert
);
183 glVertexAttribPointer( 0, 2, GL_SHORT
, GL_FALSE
,
184 stride
, (void *)offsetof( struct ui_vert
, co
) );
185 glEnableVertexAttribArray( 0 );
188 glVertexAttribPointer( 1, 2, GL_UNSIGNED_BYTE
, GL_FALSE
,
189 stride
, (void *)offsetof( struct ui_vert
, uv
) );
190 glEnableVertexAttribArray( 1 );
193 glVertexAttribPointer( 2, 4, GL_UNSIGNED_BYTE
, GL_TRUE
, stride
,
194 (void *)offsetof( struct ui_vert
, colour
) );
195 glEnableVertexAttribArray( 2 );
198 glVertexAttribPointer( 3, 4, GL_SHORT
, GL_FALSE
, stride
,
199 (void *)offsetof( struct ui_vert
, clip
) );
200 glEnableVertexAttribArray( 3 );
203 /* Initialize default context */
205 ctx
->verts
= (struct ui_vert
*)malloc(
206 vertex_buffer_size
* sizeof(struct ui_vert
) );
207 ctx
->indices
= (u16
*)malloc( index_buffer_size
* sizeof(u16
) );
210 ctx
->colours
= &ui_default_colours
;
214 static void ui_context_free( ui_ctx
*ctx
)
216 glDeleteVertexArrays( 1, &ctx
->vao
);
217 glDeleteBuffers( 1, &ctx
->vbo
);
218 glDeleteBuffers( 1, &ctx
->ebo
);
221 free( ctx
->indices
);
224 static void ui_default_init(void)
226 /* Load default font */
228 #include "vg/vg_pxfont.h"
231 u32 pixels
= 0, total
= 256*256, data
= 0;
232 u8
*image
= malloc( total
);
234 while( pixels
< total
)
236 for( int b
= 31; b
>= 0; b
-- )
238 image
[ pixels
++ ] = (compressed
[data
] & (0x1 << b
))? 0xff: 0x00;
240 if( pixels
>= total
)
249 glGenTextures( 1, &ui_glyph_texture
);
250 glBindTexture( GL_TEXTURE_2D
, ui_glyph_texture
);
252 glTexImage2D( GL_TEXTURE_2D
, 0, GL_R8
, 256, 256, 0,
253 GL_RED
, GL_UNSIGNED_BYTE
, image
);
260 SHADER_INIT( shader_ui
);
261 ui_init_context( &ui_global_ctx
, 20000 );
264 static void ui_default_free(void)
266 glDeleteTextures( 1, &ui_glyph_texture
);
267 ui_context_free( &ui_global_ctx
);
270 static struct ui_vert
*ui_fill_rect_uv( ui_ctx
*ctx
, ui_rect rect
,
271 u32 colour
, ui_px uv
[4] );
273 static void ui_draw( ui_ctx
*ctx
, m3x3f view_override
)
275 u32 num_indices_normal
= ctx
->num_indices
;
277 /* Append images to back of buffer */
278 for( int i
= 0; i
< ctx
->image_count
; i
++ )
280 ui_fill_rect_uv( ctx
, ctx
->images
[i
].rc
,
281 0xffffffff, (ui_px
[4]){0,0,128,128} );
284 glBindVertexArray( ctx
->vao
);
286 glBindBuffer( GL_ARRAY_BUFFER
, ctx
->vbo
);
287 glBufferSubData( GL_ARRAY_BUFFER
, 0,
288 ctx
->num_verts
* sizeof( struct ui_vert
), ctx
->verts
);
290 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, ctx
->ebo
);
291 glBufferSubData( GL_ELEMENT_ARRAY_BUFFER
, 0,
292 ctx
->num_indices
* sizeof( u16
), ctx
->indices
);
295 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
296 glBlendEquation(GL_FUNC_ADD
);
298 SHADER_USE( shader_ui
);
300 m3x3f view
= M3X3_IDENTITY
;
304 view_override
= view
;
306 m3x3_translate( view
, (v3f
){ -1.0f
, 1.0f
, 0.0f
} );
307 m3x3_scale( view
, (v3f
){ 1.0f
/((float)vg_window_x
*0.5f
),
308 -1.0f
/((float)vg_window_y
*0.5f
), 1.0f
} );
311 glUniformMatrix3fv( SHADER_UNIFORM( shader_ui
, "uPv" ), 1,
312 GL_FALSE
, (float *)view_override
);
314 glActiveTexture( GL_TEXTURE0
);
315 glBindTexture( GL_TEXTURE_2D
, ui_glyph_texture
);
316 glUniform1i( SHADER_UNIFORM( shader_ui
, "uTexGlyphs" ), 0 );
318 glDrawElements( GL_TRIANGLES
, num_indices_normal
,
319 GL_UNSIGNED_SHORT
, (void*)(0) );
321 /* Draw image elements */
322 for( int i
= 0; i
< ctx
->image_count
; i
++ )
324 struct ui_image
*img
= &ctx
->images
[i
];
326 glBindTexture( GL_TEXTURE_2D
, img
->image
);
327 glDrawElements( GL_TRIANGLES
, 6, GL_UNSIGNED_SHORT
,
328 (void*)( (num_indices_normal
+ 6*i
)*sizeof(u16
) ) );
337 static void ui_rect_copy( ui_rect src
, ui_rect dst
)
345 static void ui_rect_pad( ui_rect rect
, ui_px pad
)
356 static struct ui_qnode
*ui_current( ui_ctx
*ctx
)
358 return &ctx
->stack
[ ctx
->stack_count
-1 ];
361 static void ui_new_node( ui_ctx
*ctx
)
363 if( ctx
->stack_count
== vg_list_size( ctx
->stack
) )
364 vg_exiterr( "[UI] Stack overflow while creating box!" );
366 struct ui_qnode
*parent
= &ctx
->stack
[ ctx
->stack_count
-1 ];
367 struct ui_qnode
*node
= &ctx
->stack
[ ctx
->stack_count
++ ];
368 ui_rect_copy( ctx
->cursor
, node
->rect
);
370 if( parent
->mouse_over
)
372 if( ctx
->mouse
[0] >= node
->rect
[0] &&
373 ctx
->mouse
[0] < node
->rect
[0]+node
->rect
[2] &&
374 ctx
->mouse
[1] >= node
->rect
[1] &&
375 ctx
->mouse
[1] < node
->rect
[1]+node
->rect
[3] )
376 node
->mouse_over
= 1;
378 node
->mouse_over
= 0;
382 node
->mouse_over
= 0;
386 static int ui_hasmouse( ui_ctx
*ctx
)
388 struct ui_qnode
*node
= ui_current( ctx
);
389 return (node
->mouse_over
&& (node
->capture_id
== ctx
->capture_mouse_id
));
392 static void ui_end( ui_ctx
*ctx
)
394 struct ui_qnode
*node
= &ctx
->stack
[ --ctx
->stack_count
];
395 ui_rect_copy( node
->rect
, ctx
->cursor
);
398 static void ui_end_down( ui_ctx
*ctx
)
400 ui_px height
= ui_current( ctx
)->rect
[3];
402 ctx
->cursor
[1] += height
;
405 static void ui_end_right( ui_ctx
*ctx
)
407 ui_px width
= ui_current( ctx
)->rect
[2];
409 ctx
->cursor
[0] += width
;
412 static void ui_fill_y( ui_ctx
*ctx
)
414 struct ui_qnode
*node
= ui_current( ctx
);
415 ctx
->cursor
[3] = node
->rect
[3] - (ctx
->cursor
[1]-node
->rect
[1]);
418 static void ui_fill_x( ui_ctx
*ctx
)
420 struct ui_qnode
*node
= ui_current( ctx
);
421 ctx
->cursor
[2] = node
->rect
[2] - (ctx
->cursor
[0]-node
->rect
[0]);
424 static void ui_align_bottom( ui_ctx
*ctx
)
426 struct ui_qnode
*node
= ui_current( ctx
);
427 ctx
->cursor
[1] = node
->rect
[1] + node
->rect
[3] - ctx
->cursor
[3];
430 static void ui_align_right( ui_ctx
*ctx
)
432 struct ui_qnode
*node
= ui_current( ctx
);
433 ctx
->cursor
[0] = node
->rect
[0] + node
->rect
[2] - ctx
->cursor
[2];
436 static void ui_align_top( ui_ctx
*ctx
)
438 ctx
->cursor
[1] = ui_current( ctx
)->rect
[1];
441 static void ui_align_left( ui_ctx
*ctx
)
443 ctx
->cursor
[0] = ui_current( ctx
)->rect
[0];
446 static void ui_clamp_rect( ui_rect parent
, ui_rect dest
)
448 dest
[0] = vg_min( parent
[0] + parent
[2] - dest
[2], dest
[0] );
449 dest
[1] = vg_min( parent
[1] + parent
[3] - dest
[3], dest
[1] );
450 dest
[0] = vg_max( parent
[0], dest
[0] );
451 dest
[1] = vg_max( parent
[1], dest
[1] );
454 static u32
ui_group_id( ui_ctx
*ctx
, u32 lesser_unique
)
456 return ctx
->id_base
| lesser_unique
;
459 static void ui_capture_mouse( ui_ctx
*ctx
, u32 id
)
461 u32 group_uid
= ui_group_id(ctx
,id
);
463 struct ui_qnode
*node
= &ctx
->stack
[ ctx
->stack_count
-1 ];
464 node
->capture_id
= group_uid
;
466 if( !ctx
->capture_lock
&& node
->mouse_over
)
468 ctx
->capture_mouse_id
= group_uid
;
472 static int ui_want_mouse( ui_ctx
*ctx
)
474 return ctx
->capture_mouse_id
== 0? 0: 1;
477 static void ui_set_clip( ui_ctx
*ctx
, ui_rect clip
)
479 ctx
->clipping
[0] = clip
[0];
480 ctx
->clipping
[1] = clip
[1];
481 ctx
->clipping
[2] = clip
[0] + clip
[2];
482 ctx
->clipping
[3] = clip
[1] + clip
[3];
485 static void ui_release_clip( ui_ctx
*ctx
)
487 ctx
->clipping
[0] = -32000;
488 ctx
->clipping
[1] = -32000;
489 ctx
->clipping
[2] = 32000;
490 ctx
->clipping
[3] = 32000;
493 static struct ui_vert
*ui_fill_rect_uv( ui_ctx
*ctx
, ui_rect rect
,
494 u32 colour
, ui_px uv
[4] )
496 struct ui_vert
*vertices
= &ctx
->verts
[ ctx
->num_verts
];
497 vertices
[0].co
[0] = rect
[0];
498 vertices
[0].co
[1] = rect
[1];
499 vertices
[0].uv
[0] = uv
[0];
500 vertices
[0].uv
[1] = uv
[1];
501 vertices
[0].colour
= colour
;
502 vertices
[1].co
[0] = rect
[0]+rect
[2];
503 vertices
[1].co
[1] = rect
[1];
504 vertices
[1].uv
[0] = uv
[2];
505 vertices
[1].uv
[1] = uv
[1];
506 vertices
[1].colour
= colour
;
507 vertices
[2].co
[0] = rect
[0]+rect
[2];
508 vertices
[2].co
[1] = rect
[1]+rect
[3];
509 vertices
[2].uv
[0] = uv
[2];
510 vertices
[2].uv
[1] = uv
[3];
511 vertices
[2].colour
= colour
;
512 vertices
[3].co
[0] = rect
[0];
513 vertices
[3].co
[1] = rect
[1]+rect
[3];
514 vertices
[3].uv
[0] = uv
[0];
515 vertices
[3].uv
[1] = uv
[3];
516 vertices
[3].colour
= colour
;
517 u16 ind_start
= ctx
->num_verts
;
518 u16
*indices
= &ctx
->indices
[ ctx
->num_indices
];
520 ui_rect_copy( ctx
->clipping
, vertices
[0].clip
);
521 ui_rect_copy( ctx
->clipping
, vertices
[1].clip
);
522 ui_rect_copy( ctx
->clipping
, vertices
[2].clip
);
523 ui_rect_copy( ctx
->clipping
, vertices
[3].clip
);
525 indices
[0] = ind_start
+0;
526 indices
[1] = ind_start
+2;
527 indices
[2] = ind_start
+1;
529 indices
[3] = ind_start
+0;
530 indices
[4] = ind_start
+3;
531 indices
[5] = ind_start
+2;
533 ctx
->num_indices
+= 6;
539 static struct ui_vert
*ui_fill_rect( ui_ctx
*ctx
, ui_rect rect
, u32 colour
)
541 return ui_fill_rect_uv( ctx
, rect
, colour
, (ui_px
[4]){ 4,4, 4,4 } );
544 static ui_px
ui_text_line_offset( const char *str
, ui_px scale
,
545 enum ui_text_align align
)
547 if( align
== k_text_align_left
)
551 const char *_c
= str
;
554 while( (c
= *(_c
++)) )
555 if( c
>= 32 && c
<= 126 )
560 if( align
== k_text_align_right
)
561 return -length
* scale
*8;
563 return (-length
* scale
*8) / 2;
566 static void ui_text( ui_ctx
*ctx
, ui_px pos
[2],
567 const char *str
, ui_px scale
, enum ui_text_align align
)
570 u32 current_colour
= 0x00ffffff;
572 const char *_c
= str
;
575 text_cursor
[0] = pos
[0] + ui_text_line_offset( str
, scale
, align
);
576 text_cursor
[1] = pos
[1];
577 text_cursor
[2] = 8*scale
;
578 text_cursor
[3] = 14*scale
;
580 while( (c
= *(_c
++)) )
584 text_cursor
[1] += 14*scale
;
585 text_cursor
[0] = pos
[0] + ui_text_line_offset( _c
, scale
, align
);
592 glyph_base
[0] = glyph_index
& 0xf;
593 glyph_base
[1] = (glyph_index
-glyph_base
[0])>>4;
598 ui_fill_rect_uv( ctx
, text_cursor
, current_colour
, (ui_px
[4])
606 else if( c
== '\x1B' )
610 for( int i
= 0; i
< 3; i
++ )
620 case '0': current_colour
= 0x00ffffff; break;
621 case '3'|'1'<<8: current_colour
= 0x00201fee; break;
622 case '3'|'2'<<8: current_colour
= 0x0037e420; break;
623 case '3'|'3'<<8: current_colour
= 0x000ed8e2; break;
624 case '3'|'4'<<8: current_colour
= 0x00f15010; break;
625 case '3'|'5'<<8: current_colour
= 0x00ee20ee; break;
626 case '3'|'6'<<8: current_colour
= 0x00eeee20; break;
627 case '3'|'7'<<8: current_colour
= 0x00ffffff; break;
633 colour_id
|= _c
[i
] << (i
*8);
646 text_cursor
[0] += UI_GLYPH_SPACING_X
*scale
*4;
650 text_cursor
[0] += UI_GLYPH_SPACING_X
*scale
;
657 static void ui_begin( ui_ctx
*ctx
, ui_px res_x
, ui_px res_y
)
661 ctx
->cursor
[2] = res_x
;
662 ctx
->cursor
[3] = res_y
;
664 ui_rect_copy( ctx
->cursor
, ctx
->stack
[0].rect
);
665 ctx
->stack
[0].mouse_over
= 1;
667 ctx
->stack_count
= 1;
670 ctx
->num_indices
= 0;
672 ui_release_clip( ctx
);
674 if( ctx
->click_state
== 0 )
675 ctx
->capture_mouse_id
= 0;
677 ctx
->image_count
= 0;
680 static void ui_resolve( ui_ctx
*ctx
)
682 if( ctx
->stack_count
-1 )
683 vg_exiterr( "[UI] Mismatched node create/drestroy!" );
685 if( ctx
->click_state
== 3 || ctx
->click_state
== 0 )
687 ctx
->capture_lock
= 0;
691 static void ui_set_mouse( ui_ctx
*ctx
, int x
, int y
, int click_state
)
696 ctx
->click_state
= click_state
;
700 * High level controls
708 ui_px drag_offset
[2];
713 k_button_released
= 0,
714 k_button_start_click
,
719 static int ui_button( ui_ctx
*ctx
, u32 id
)
723 ui_capture_mouse( ctx
, id
);
725 if( ui_hasmouse(ctx
) )
727 ui_fill_rect( ctx
, ctx
->cursor
, ctx
->colours
->hover
);
729 if( ctx
->click_state
== 1 )
731 ctx
->capture_lock
= 1;
732 return k_button_start_click
;
734 else if( ctx
->capture_lock
&& ctx
->click_state
== 3 )
735 return k_button_click
;
736 else if( ctx
->capture_lock
&& ctx
->click_state
== 2 )
737 return k_button_hold
;
740 ui_fill_rect( ctx
, ctx
->cursor
, ctx
->colours
->main
);
743 return k_button_released
;
746 static int ui_window( ui_ctx
*ctx
, struct ui_window
*window
, u32 control_group
)
748 ctx
->id_base
= control_group
<< 16;
752 window
->transform
[0] = ctx
->mouse
[0]+window
->drag_offset
[0];
753 window
->transform
[1] = ctx
->mouse
[1]+window
->drag_offset
[1];
755 ui_clamp_rect( ctx
->stack
[0].rect
, window
->transform
);
757 if( ctx
->click_state
== 0 || ctx
->click_state
== 3 )
763 ui_rect_copy( window
->transform
, ctx
->cursor
);
767 ui_capture_mouse( ctx
, __COUNTER__
);
773 ui_capture_mouse( ctx
, __COUNTER__
);
775 struct ui_vert
*drag_bar
= ui_fill_rect( ctx
, ctx
->cursor
, 0xff555555 );
780 ui_text( ctx
, ctx
->cursor
, window
->title
, 2, 0 );
785 ui_align_right( ctx
);
787 ui_rect_pad( ctx
->cursor
, 4 );
789 if( ui_button( ctx
, __COUNTER__
) )
791 vg_info( "Click clacked\n" );
794 ui_text( ctx
, ctx
->cursor
, "x", 2, 0 );
797 if( ui_hasmouse( ctx
) )
799 drag_bar
[0].colour
= 0xff777777;
800 drag_bar
[1].colour
= 0xff777777;
801 drag_bar
[2].colour
= 0xff777777;
802 drag_bar
[3].colour
= 0xff777777;
805 if( ctx
->click_state
== 1 )
808 window
->drag_offset
[0] = window
->transform
[0]-ctx
->mouse
[0];
809 window
->drag_offset
[1] = window
->transform
[1]-ctx
->mouse
[1];
819 static void ui_push_image( ui_ctx
*ctx
, ui_rect rc
, GLuint image
)
821 struct ui_image
*img
= &ctx
->images
[ ctx
->image_count
++ ];
822 ui_rect_copy( rc
, img
->rc
);
827 #define gui_draw(...) ui_draw( &ui_global_ctx, __VA_ARGS__)
828 #define gui_current(...) ui_current( &ui_global_ctx, __VA_ARGS__)
829 #define gui_new_node() ui_new_node( &ui_global_ctx )
830 #define gui_hasmouse(...) ui_hasmouse( &ui_global_ctx, __VA_ARGS__)
831 #define gui_end() ui_end( &ui_global_ctx )
832 #define gui_end_down() ui_end_down( &ui_global_ctx )
833 #define gui_end_right() ui_end_right( &ui_global_ctx )
834 #define gui_fill_y() ui_fill_y( &ui_global_ctx)
835 #define gui_fill_x() ui_fill_x( &ui_global_ctx)
836 #define gui_align_bottom() ui_align_bottom( &ui_global_ctx )
837 #define gui_align_right() ui_align_right( &ui_global_ctx )
838 #define gui_align_top() ui_align_top( &ui_global_ctx )
839 #define gui_align_left() ui_align_left( &ui_global_ctx )
840 #define gui_clamp_rect(...) ui_clamp_rect( &ui_global_ctx, __VA_ARGS__)
841 #define gui_group_id(...) ui_group_id( &ui_global_ctx, __VA_ARGS__)
842 #define gui_capture_mouse(...) ui_capture_mouse( &ui_global_ctx, __VA_ARGS__)
843 #define gui_set_clip(...) ui_set_clip( &ui_global_ctx, __VA_ARGS__)
844 #define gui_release_clip() ui_release_clip( &ui_global_ctx )
845 #define gui_fill_rect_uv(...) ui_fill_rect_uv( &ui_global_ctx, __VA_ARGS__)
846 #define gui_fill_rect(...) ui_fill_rect( &ui_global_ctx, __VA_ARGS__)
847 #define gui_text(...) ui_text( &ui_global_ctx, __VA_ARGS__)
848 #define gui_begin(...) ui_begin( &ui_global_ctx, __VA_ARGS__)
849 #define gui_resolve(...) ui_resolve( &ui_global_ctx, __VA_ARGS__)
850 #define gui_set_mouse(...) ui_set_mouse( &ui_global_ctx, __VA_ARGS__)
851 #define gui_button(...) ui_button( &ui_global_ctx, __VA_ARGS__)
852 #define gui_window(...) ui_window( &ui_global_ctx, __VA_ARGS__)
853 #define gui_want_mouse() ui_want_mouse( &ui_global_ctx )
854 #define gui_push_image(...) ui_push_image( &ui_global_ctx, __VA_ARGS__ )
855 #define gui_reset_colours(...) ui_reset_colours( &ui_global_ctx )