1 /* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */
9 #include "vg/vg_shader.h"
11 static struct vg_shader _shader_ui
=
19 "layout (location=0) in vec2 a_co;"
20 "layout (location=1) in vec2 a_uv;"
21 "layout (location=2) in vec4 a_colour;"
22 "layout (location=3) in vec4 a_clip;"
25 "out vec2 aTexCoords;"
32 "gl_Position = vec4( uPv * vec3( a_co, 1.0 ), 1.0 );"
33 "aTexCoords = a_uv * 0.0078125;"
44 "uniform sampler2D uTexGlyphs;"
55 "float clip_blend = step( aWsp.x, aClip.z ) *"
56 "step( aWsp.y, aClip.w ) *"
57 "step( aClip.x, aWsp.x ) *"
58 "step( aClip.y, aWsp.y ); "
60 "vec4 glyph = vec4(1.0,1.0,1.0,1.0);"
62 "if( aColour.a == 0.0 )"
64 "glyph = texture( uTexGlyphs, aTexCoords );"
65 "glyph.a = smoothstep( 0.47, 0.53, glyph.r );"
69 "glyph.a = aColour.a;"
72 "FragColor = vec4( aColour.rgb, glyph.a*clip_blend );"
78 typedef u32 ui_colour
;
79 typedef ui_px ui_rect
[4];
80 typedef struct ui_colourset ui_colourset
;
82 /* Relative to cursor p0 */
85 k_text_align_left
= 0,
86 k_text_align_right
= 1,
87 k_text_align_center
= 2
102 ui_colour background
;
132 struct ui_vert
*vertex_buffer
;
134 u32 max_verts
, max_indices
, cur_vert
, cur_indice
;
139 u32 capture_mouse_id
;
144 int click_state
; /* 0: released, 1: on down, 2: pressed, 3: on release */
146 ui_colourset
*colours
;
162 #define UI_GLYPH_SPACING_X 9
164 static GLuint ui_glyph_texture
= 0;
165 static ui_colourset ui_default_colours
= {
171 VG_STATIC
void _vg_ui_init(void)
173 if( !vg_shader_compile( &_shader_ui
) )
174 vg_fatal_exit_loop( "Failed to compile ui shader" );
178 * ----------------------------------------
181 vg_uictx
.max_indices
= 20000;
182 vg_uictx
.max_verts
= 30000;
183 vg_uictx
.colours
= &ui_default_colours
;
185 /* Generate the buffer we are gonna be drawing to */
186 glGenVertexArrays( 1, &vg_uictx
.vao
);
187 glGenBuffers( 1, &vg_uictx
.vbo
);
188 glGenBuffers( 1, &vg_uictx
.ebo
);
190 glBindVertexArray( vg_uictx
.vao
);
191 glBindBuffer( GL_ARRAY_BUFFER
, vg_uictx
.vbo
);
193 glBufferData( GL_ARRAY_BUFFER
,
194 vg_uictx
.max_verts
* sizeof( struct ui_vert
),
195 NULL
, GL_DYNAMIC_DRAW
);
196 glBindVertexArray( vg_uictx
.vao
);
198 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, vg_uictx
.ebo
);
199 glBufferData( GL_ELEMENT_ARRAY_BUFFER
,
200 vg_uictx
.max_indices
* sizeof( u16
), NULL
, GL_DYNAMIC_DRAW
);
205 u32
const stride
= sizeof( struct ui_vert
);
208 glVertexAttribPointer( 0, 2, GL_SHORT
, GL_FALSE
,
209 stride
, (void *)offsetof( struct ui_vert
, co
) );
210 glEnableVertexAttribArray( 0 );
213 glVertexAttribPointer( 1, 2, GL_UNSIGNED_BYTE
, GL_FALSE
,
214 stride
, (void *)offsetof( struct ui_vert
, uv
) );
215 glEnableVertexAttribArray( 1 );
218 glVertexAttribPointer( 2, 4, GL_UNSIGNED_BYTE
, GL_TRUE
, stride
,
219 (void *)offsetof( struct ui_vert
, colour
) );
220 glEnableVertexAttribArray( 2 );
223 glVertexAttribPointer( 3, 4, GL_SHORT
, GL_FALSE
, stride
,
224 (void *)offsetof( struct ui_vert
, clip
) );
225 glEnableVertexAttribArray( 3 );
229 /* Alloc RAM default context */
230 u32 vert_size
= vg_uictx
.max_verts
*sizeof(struct ui_vert
),
231 inds_size
= vg_align8( vg_uictx
.max_indices
*sizeof(u16
) );
233 vg_uictx
.vertex_buffer
= vg_linear_alloc( vg_mem
.rtmemory
, vert_size
);
234 vg_uictx
.indice_buffer
= vg_linear_alloc( vg_mem
.rtmemory
, inds_size
);
237 * -----------------------------------------------------
240 /* Load default font */
242 #include "vg/vg_pxfont.h"
245 u32 pixels
= 0, total
= 256*256, data
= 0;
248 while( pixels
< total
)
250 for( int b
= 31; b
>= 0; b
-- )
252 image
[ pixels
++ ] = (compressed
[data
] & (0x1 << b
))? 0xff: 0x00;
254 if( pixels
>= total
)
263 glGenTextures( 1, &ui_glyph_texture
);
264 glBindTexture( GL_TEXTURE_2D
, ui_glyph_texture
);
265 glTexImage2D( GL_TEXTURE_2D
, 0, GL_R8
, 256, 256, 0,
266 GL_RED
, GL_UNSIGNED_BYTE
, image
);
273 static struct ui_vert
*ui_fill_rect_uv( ui_rect rect
, u32 colour
, ui_px uv
[4] );
275 VG_STATIC
void ui_draw( m3x3f view_override
)
277 u32 num_indices_normal
= vg_uictx
.cur_indice
;
279 /* Append images to back of buffer */
280 for( int i
= 0; i
< vg_uictx
.image_count
; i
++ )
282 ui_fill_rect_uv( vg_uictx
.images
[i
].rc
, 0xffffffff,
283 (ui_px
[4]){0,0,128,128} );
286 glBindVertexArray( vg_uictx
.vao
);
288 glBindBuffer( GL_ARRAY_BUFFER
, vg_uictx
.vbo
);
289 glBufferSubData( GL_ARRAY_BUFFER
, 0,
290 vg_uictx
.cur_vert
* sizeof( struct ui_vert
),
291 vg_uictx
.vertex_buffer
);
293 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, vg_uictx
.ebo
);
294 glBufferSubData( GL_ELEMENT_ARRAY_BUFFER
, 0,
295 vg_uictx
.cur_indice
* sizeof( u16
),
296 vg_uictx
.indice_buffer
);
299 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
300 glBlendEquation(GL_FUNC_ADD
);
302 glUseProgram( _shader_ui
.id
);
304 m3x3f view
= M3X3_IDENTITY
;
308 view_override
= view
;
310 m3x3_translate( view
, (v3f
){ -1.0f
, 1.0f
, 0.0f
} );
311 m3x3_scale( view
, (v3f
){ 1.0f
/((float)vg
.window_x
*0.5f
),
312 -1.0f
/((float)vg
.window_y
*0.5f
), 1.0f
} );
316 glUniformMatrix3fv( glGetUniformLocation( _shader_ui
.id
, "uPv" ), 1,
317 GL_FALSE
, (float *)view_override
);
319 glActiveTexture( GL_TEXTURE0
);
320 glBindTexture( GL_TEXTURE_2D
, ui_glyph_texture
);
321 glUniform1i( glGetUniformLocation( _shader_ui
.id
, "uTexGlyphs" ), 0 );
323 glDrawElements( GL_TRIANGLES
, num_indices_normal
,
324 GL_UNSIGNED_SHORT
, (void*)(0) );
326 /* Draw image elements */
327 for( int i
= 0; i
< vg_uictx
.image_count
; i
++ )
329 struct ui_image
*img
= &vg_uictx
.images
[i
];
331 glBindTexture( GL_TEXTURE_2D
, img
->image
);
332 glDrawElements( GL_TRIANGLES
, 6, GL_UNSIGNED_SHORT
,
333 (void*)( (num_indices_normal
+ 6*i
)*sizeof(u16
) ) );
342 VG_STATIC
void ui_rect_copy( ui_rect src
, ui_rect dst
)
350 VG_STATIC
void ui_rect_pad( ui_rect rect
, ui_px pad
)
361 static struct ui_qnode
*ui_current(void)
363 return &vg_uictx
.stack
[ vg_uictx
.stack_count
-1 ];
366 VG_STATIC
void ui_new_node(void)
368 if( vg_uictx
.stack_count
== vg_list_size( vg_uictx
.stack
) )
370 vg_error( "[UI] Stack overflow while creating box!" );
374 struct ui_qnode
*parent
= &vg_uictx
.stack
[ vg_uictx
.stack_count
-1 ];
375 struct ui_qnode
*node
= &vg_uictx
.stack
[ vg_uictx
.stack_count
++ ];
376 ui_rect_copy( vg_uictx
.cursor
, node
->rect
);
378 if( parent
->mouse_over
)
380 if( vg_uictx
.mouse
[0] >= node
->rect
[0] &&
381 vg_uictx
.mouse
[0] < node
->rect
[0]+node
->rect
[2] &&
382 vg_uictx
.mouse
[1] >= node
->rect
[1] &&
383 vg_uictx
.mouse
[1] < node
->rect
[1]+node
->rect
[3] )
384 node
->mouse_over
= 1;
386 node
->mouse_over
= 0;
390 node
->mouse_over
= 0;
394 static int ui_hasmouse(void)
396 struct ui_qnode
*node
= ui_current();
397 return (node
->mouse_over
&& (node
->capture_id
== vg_uictx
.capture_mouse_id
));
400 VG_STATIC
void ui_end(void)
402 struct ui_qnode
*node
= &vg_uictx
.stack
[ --vg_uictx
.stack_count
];
403 ui_rect_copy( node
->rect
, vg_uictx
.cursor
);
406 VG_STATIC
void ui_end_down(void)
408 ui_px height
= ui_current()->rect
[3];
410 vg_uictx
.cursor
[1] += height
;
413 VG_STATIC
void ui_end_right(void)
415 ui_px width
= ui_current()->rect
[2];
417 vg_uictx
.cursor
[0] += width
;
420 VG_STATIC
void ui_fill_y(void)
422 struct ui_qnode
*node
= ui_current();
423 vg_uictx
.cursor
[3] = node
->rect
[3] - (vg_uictx
.cursor
[1]-node
->rect
[1]);
426 VG_STATIC
void ui_fill_x(void)
428 struct ui_qnode
*node
= ui_current();
429 vg_uictx
.cursor
[2] = node
->rect
[2] - (vg_uictx
.cursor
[0]-node
->rect
[0]);
432 VG_STATIC
void ui_align_bottom(void)
434 struct ui_qnode
*node
= ui_current();
435 vg_uictx
.cursor
[1] = node
->rect
[1] + node
->rect
[3] - vg_uictx
.cursor
[3];
438 VG_STATIC
void ui_align_right(void)
440 struct ui_qnode
*node
= ui_current();
441 vg_uictx
.cursor
[0] = node
->rect
[0] + node
->rect
[2] - vg_uictx
.cursor
[2];
444 VG_STATIC
void ui_align_top(void)
446 vg_uictx
.cursor
[1] = ui_current()->rect
[1];
449 VG_STATIC
void ui_align_left(void)
451 vg_uictx
.cursor
[0] = ui_current()->rect
[0];
454 VG_STATIC
void ui_clamp_rect( ui_rect parent
, ui_rect dest
)
456 dest
[0] = vg_min( parent
[0] + parent
[2] - dest
[2], dest
[0] );
457 dest
[1] = vg_min( parent
[1] + parent
[3] - dest
[3], dest
[1] );
458 dest
[0] = vg_max( parent
[0], dest
[0] );
459 dest
[1] = vg_max( parent
[1], dest
[1] );
462 VG_STATIC
void ui_capture_mouse( u32 id
)
464 struct ui_qnode
*node
= &vg_uictx
.stack
[ vg_uictx
.stack_count
-1 ];
465 node
->capture_id
= id
;
467 if( !vg_uictx
.capture_lock
&& node
->mouse_over
)
469 vg_uictx
.capture_mouse_id
= id
;
473 static int ui_want_mouse(void)
475 return vg_uictx
.capture_mouse_id
== 0? 0: 1;
478 VG_STATIC
void ui_set_clip( ui_rect clip
)
480 vg_uictx
.clipping
[0] = clip
[0];
481 vg_uictx
.clipping
[1] = clip
[1];
482 vg_uictx
.clipping
[2] = clip
[0] + clip
[2];
483 vg_uictx
.clipping
[3] = clip
[1] + clip
[3];
486 VG_STATIC
void ui_release_clip(void)
488 vg_uictx
.clipping
[0] = -32000;
489 vg_uictx
.clipping
[1] = -32000;
490 vg_uictx
.clipping
[2] = 32000;
491 vg_uictx
.clipping
[3] = 32000;
494 static struct ui_vert
*ui_fill_rect_uv( ui_rect rect
, u32 colour
, ui_px uv
[4] )
496 /* this if far from ideal but stops us from crashing */
497 if( (vg_uictx
.cur_vert
+ 6 > vg_uictx
.max_verts
) ||
498 (vg_uictx
.cur_indice
+ 4 > vg_uictx
.max_indices
))
499 return vg_uictx
.vertex_buffer
;
501 struct ui_vert
*vertices
= &vg_uictx
.vertex_buffer
[ vg_uictx
.cur_vert
];
502 u16
*indices
= &vg_uictx
.indice_buffer
[ vg_uictx
.cur_indice
];
504 vertices
[0].co
[0] = rect
[0];
505 vertices
[0].co
[1] = rect
[1];
506 vertices
[0].uv
[0] = uv
[0];
507 vertices
[0].uv
[1] = uv
[1];
508 vertices
[0].colour
= colour
;
509 vertices
[1].co
[0] = rect
[0]+rect
[2];
510 vertices
[1].co
[1] = rect
[1];
511 vertices
[1].uv
[0] = uv
[2];
512 vertices
[1].uv
[1] = uv
[1];
513 vertices
[1].colour
= colour
;
514 vertices
[2].co
[0] = rect
[0]+rect
[2];
515 vertices
[2].co
[1] = rect
[1]+rect
[3];
516 vertices
[2].uv
[0] = uv
[2];
517 vertices
[2].uv
[1] = uv
[3];
518 vertices
[2].colour
= colour
;
519 vertices
[3].co
[0] = rect
[0];
520 vertices
[3].co
[1] = rect
[1]+rect
[3];
521 vertices
[3].uv
[0] = uv
[0];
522 vertices
[3].uv
[1] = uv
[3];
523 vertices
[3].colour
= colour
;
524 u16 ind_start
= vg_uictx
.cur_vert
;
526 ui_rect_copy( vg_uictx
.clipping
, vertices
[0].clip
);
527 ui_rect_copy( vg_uictx
.clipping
, vertices
[1].clip
);
528 ui_rect_copy( vg_uictx
.clipping
, vertices
[2].clip
);
529 ui_rect_copy( vg_uictx
.clipping
, vertices
[3].clip
);
531 indices
[0] = ind_start
+0;
532 indices
[1] = ind_start
+2;
533 indices
[2] = ind_start
+1;
535 indices
[3] = ind_start
+0;
536 indices
[4] = ind_start
+3;
537 indices
[5] = ind_start
+2;
539 vg_uictx
.cur_indice
+= 6;
540 vg_uictx
.cur_vert
+= 4;
545 static struct ui_vert
*ui_fill_rect( ui_rect rect
, u32 colour
)
547 return ui_fill_rect_uv( rect
, colour
, (ui_px
[4]){ 4,4, 4,4 } );
550 static ui_px
ui_text_line_offset( const char *str
, ui_px scale
,
551 enum ui_text_align align
)
553 if( align
== k_text_align_left
)
557 const char *_c
= str
;
560 while( (c
= *(_c
++)) )
561 if( c
>= 32 && c
<= 126 )
566 if( align
== k_text_align_right
)
567 return -length
* scale
*8;
569 return (-length
* scale
*8) / 2;
572 VG_STATIC
void ui_text( ui_px pos
[2],
573 const char *str
, ui_px scale
, enum ui_text_align align
)
576 u32 current_colour
= 0x00ffffff;
578 const char *_c
= str
;
581 text_cursor
[0] = pos
[0] + ui_text_line_offset( str
, scale
, align
);
582 text_cursor
[1] = pos
[1];
583 text_cursor
[2] = 8*scale
;
584 text_cursor
[3] = 14*scale
;
586 while( (c
= *(_c
++)) )
590 text_cursor
[1] += 14*scale
;
591 text_cursor
[0] = pos
[0] + ui_text_line_offset( _c
, scale
, align
);
598 glyph_base
[0] = glyph_index
& 0xf;
599 glyph_base
[1] = (glyph_index
-glyph_base
[0])>>4;
604 ui_fill_rect_uv( text_cursor
, current_colour
, (ui_px
[4])
612 else if( c
== '\x1B' )
616 for( int i
= 0; i
< 3; i
++ )
626 case '0': current_colour
= 0x00ffffff; break;
627 case '3'|'1'<<8: current_colour
= 0x00201fee; break;
628 case '3'|'2'<<8: current_colour
= 0x0037e420; break;
629 case '3'|'3'<<8: current_colour
= 0x000ed8e2; break;
630 case '3'|'4'<<8: current_colour
= 0x00f15010; break;
631 case '3'|'5'<<8: current_colour
= 0x00ee20ee; break;
632 case '3'|'6'<<8: current_colour
= 0x00eeee20; break;
633 case '3'|'7'<<8: current_colour
= 0x00ffffff; break;
639 colour_id
|= _c
[i
] << (i
*8);
652 text_cursor
[0] += UI_GLYPH_SPACING_X
*scale
*4;
656 text_cursor
[0] += UI_GLYPH_SPACING_X
*scale
;
663 VG_STATIC
void ui_begin( ui_px res_x
, ui_px res_y
)
665 vg_uictx
.cursor
[0] = 0;
666 vg_uictx
.cursor
[1] = 0;
667 vg_uictx
.cursor
[2] = res_x
;
668 vg_uictx
.cursor
[3] = res_y
;
670 ui_rect_copy( vg_uictx
.cursor
, vg_uictx
.stack
[0].rect
);
671 vg_uictx
.stack
[0].mouse_over
= 1;
673 vg_uictx
.stack_count
= 1;
675 vg_uictx
.cur_vert
= 0;
676 vg_uictx
.cur_indice
= 0;
680 if( vg_uictx
.click_state
== 0 )
681 vg_uictx
.capture_mouse_id
= 0;
683 vg_uictx
.image_count
= 0;
686 VG_STATIC
void ui_resolve(void)
688 if( vg_uictx
.stack_count
-1 )
690 vg_error( "[UI] Mismatched node create/drestroy!" );
694 if( vg_uictx
.click_state
== 3 || vg_uictx
.click_state
== 0 )
696 vg_uictx
.capture_lock
= 0;
700 VG_STATIC
void ui_set_mouse( int x
, int y
, int click_state
)
702 vg_uictx
.mouse
[0] = x
;
703 vg_uictx
.mouse
[1] = y
;
705 vg_uictx
.click_state
= click_state
;
709 * High level controls
717 ui_px drag_offset
[2];
722 k_button_released
= 0,
723 k_button_start_click
,
728 static int ui_button(void)
730 u32 uid
= vg_uictx
.control_id
++;
734 ui_capture_mouse( uid
);
738 ui_fill_rect( vg_uictx
.cursor
, vg_uictx
.colours
->hover
);
740 if( vg_uictx
.click_state
== 1 )
742 vg_uictx
.capture_lock
= 1;
743 return k_button_start_click
;
745 else if( vg_uictx
.capture_lock
&& vg_uictx
.click_state
== 3 )
746 return k_button_click
;
747 else if( vg_uictx
.capture_lock
&& vg_uictx
.click_state
== 2 )
748 return k_button_hold
;
750 return k_button_click
;
753 ui_fill_rect( vg_uictx
.cursor
, vg_uictx
.colours
->main
);
756 return k_button_released
;
759 static int ui_window( struct ui_window
*window
, u32 control_group
)
763 window
->transform
[0] = vg_uictx
.mouse
[0]+window
->drag_offset
[0];
764 window
->transform
[1] = vg_uictx
.mouse
[1]+window
->drag_offset
[1];
766 ui_clamp_rect( vg_uictx
.stack
[0].rect
, window
->transform
);
768 if( vg_uictx
.click_state
== 0 || vg_uictx
.click_state
== 3 )
774 ui_rect_copy( window
->transform
, vg_uictx
.cursor
);
778 ui_capture_mouse( vg_uictx
.control_id
++ );
781 vg_uictx
.cursor
[3] = 25;
784 ui_capture_mouse( vg_uictx
.control_id
++ );
786 struct ui_vert
*drag_bar
= ui_fill_rect( vg_uictx
.cursor
, 0xff555555 );
789 vg_uictx
.cursor
[0] += 2;
790 vg_uictx
.cursor
[1] += 2;
791 ui_text( vg_uictx
.cursor
, window
->title
, 2, 0 );
794 vg_uictx
.cursor
[3] = 25;
795 vg_uictx
.cursor
[2] = 25;
798 ui_rect_pad( vg_uictx
.cursor
, 4 );
802 vg_info( "Click clacked\n" );
804 vg_uictx
.cursor
[0] += 2;
805 ui_text( vg_uictx
.cursor
, "x", 2, 0 );
810 drag_bar
[0].colour
= 0xff777777;
811 drag_bar
[1].colour
= 0xff777777;
812 drag_bar
[2].colour
= 0xff777777;
813 drag_bar
[3].colour
= 0xff777777;
816 if( vg_uictx
.click_state
== 1 )
819 window
->drag_offset
[0] = window
->transform
[0]-vg_uictx
.mouse
[0];
820 window
->drag_offset
[1] = window
->transform
[1]-vg_uictx
.mouse
[1];
830 VG_STATIC
void ui_push_image( ui_rect rc
, GLuint image
)
832 struct ui_image
*img
= &vg_uictx
.images
[ vg_uictx
.image_count
++ ];
833 ui_rect_copy( rc
, img
->rc
);
843 struct ui_slider_vector
845 float *data
, min
, max
;
846 struct ui_slider sub
[4];
855 VG_STATIC
void ui_slider( struct ui_slider
*slider
)
859 ui_px slider_start
= vg_uictx
.cursor
[0];
861 float const ftotal
= vg_uictx
.cursor
[2],
862 fwidth
= ftotal
*0.25f
,
863 fmove
= ftotal
- fwidth
,
864 fstart
= fwidth
*0.5f
,
865 frange
= slider
->max
-slider
->min
,
866 fpos
= (*slider
->data
- slider
->min
) / frange
;
868 ui_fill_rect( vg_uictx
.cursor
, 0xff111111 );
869 vg_uictx
.cursor
[2] = fwidth
;
870 vg_uictx
.cursor
[0] = slider_start
+ fpos
* fmove
;
872 u32 uid
= vg_uictx
.control_id
++;
873 int status
= ui_button();
874 if( vg_uictx
.capture_lock
&& (vg_uictx
.capture_mouse_id
== uid
))
876 float ui_new
= vg_uictx
.mouse
[0],
877 local
= ui_new
- (slider_start
+ fstart
),
878 zo
= vg_clampf(local
/ fmove
,0.0f
,1.0f
);
880 *slider
->data
= vg_lerpf( slider
->min
, slider
->max
, zo
);
883 vg_uictx
.cursor
[0] += 4;
884 vg_uictx
.cursor
[1] += 4;
887 snprintf( buf
, 12, "%.2f", *slider
->data
);
888 ui_text( vg_uictx
.cursor
, buf
, 1, 0 );
893 VG_STATIC
void ui_slider_vector( struct ui_slider_vector
*slider
)
895 for( int i
=0; i
<slider
->len
; i
++ )
897 slider
->sub
[i
].data
= &slider
->data
[i
];
898 slider
->sub
[i
].min
= slider
->min
;
899 slider
->sub
[i
].max
= slider
->max
;
900 ui_slider( &slider
->sub
[i
] );
904 VG_STATIC
void ui_checkbox( struct ui_checkbox
*cb
)
906 if( ui_button() == k_button_click
)
910 ui_rect_pad( vg_uictx
.cursor
, 4 );
912 ui_fill_rect( vg_uictx
.cursor
, 0xff00e052 );
914 ui_fill_rect( vg_uictx
.cursor
, 0xff0052e0 );