1 /* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */
8 #include "vg/vg_shader.h"
10 static struct vg_shader _shader_ui
=
18 "layout (location=0) in vec2 a_co;"
19 "layout (location=1) in vec2 a_uv;"
20 "layout (location=2) in vec4 a_colour;"
21 "layout (location=3) in vec4 a_clip;"
24 "out vec2 aTexCoords;"
31 "gl_Position = vec4( uPv * vec3( a_co, 1.0 ), 1.0 );"
32 "aTexCoords = a_uv * 0.0078125;"
43 "uniform sampler2D uTexGlyphs;"
54 "float clip_blend = step( aWsp.x, aClip.z ) *"
55 "step( aWsp.y, aClip.w ) *"
56 "step( aClip.x, aWsp.x ) *"
57 "step( aClip.y, aWsp.y ); "
59 "vec4 glyph = vec4(1.0,1.0,1.0,1.0);"
61 "if( aColour.a == 0.0 )"
63 "glyph = texture( uTexGlyphs, aTexCoords );"
64 "glyph.a = smoothstep( 0.47, 0.53, glyph.r );"
68 "glyph.a = aColour.a;"
71 "FragColor = vec4( aColour.rgb, glyph.a*clip_blend );"
77 typedef u32 ui_colour
;
78 typedef ui_px ui_rect
[4];
79 typedef struct ui_colourset ui_colourset
;
81 /* Relative to cursor p0 */
84 k_text_align_left
= 0,
85 k_text_align_right
= 1,
86 k_text_align_center
= 2
101 ui_colour background
;
131 struct ui_vert
*vertex_buffer
;
133 u32 num_verts
, num_indices
;
138 u32 capture_mouse_id
;
143 int click_state
; /* 0: released, 1: on down, 2: pressed, 3: on release */
145 ui_colourset
*colours
;
161 #define UI_GLYPH_SPACING_X 9
163 static GLuint ui_glyph_texture
= 0;
164 static ui_colourset ui_default_colours
= {
170 VG_STATIC
void ui_init_context(void)
172 if( !vg_shader_compile( &_shader_ui
) )
173 vg_fatal_exit_loop( "Failed to compile ui shader" );
177 * ----------------------------------------
179 u32 index_buffer_size
= 20000,
180 vertex_buffer_size
= (index_buffer_size
+(index_buffer_size
/2));
182 /* Generate the buffer we are gonna be drawing to */
183 glGenVertexArrays( 1, &vg_uictx
.vao
);
184 glGenBuffers( 1, &vg_uictx
.vbo
);
185 glGenBuffers( 1, &vg_uictx
.ebo
);
187 glBindVertexArray( vg_uictx
.vao
);
188 glBindBuffer( GL_ARRAY_BUFFER
, vg_uictx
.vbo
);
190 glBufferData( GL_ARRAY_BUFFER
,
191 vertex_buffer_size
* sizeof( struct ui_vert
),
192 NULL
, GL_DYNAMIC_DRAW
);
193 glBindVertexArray( vg_uictx
.vao
);
195 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, vg_uictx
.ebo
);
196 glBufferData( GL_ELEMENT_ARRAY_BUFFER
,
197 index_buffer_size
* sizeof( u16
), NULL
, GL_DYNAMIC_DRAW
);
202 u32
const stride
= sizeof( struct ui_vert
);
205 glVertexAttribPointer( 0, 2, GL_SHORT
, GL_FALSE
,
206 stride
, (void *)offsetof( struct ui_vert
, co
) );
207 glEnableVertexAttribArray( 0 );
210 glVertexAttribPointer( 1, 2, GL_UNSIGNED_BYTE
, GL_FALSE
,
211 stride
, (void *)offsetof( struct ui_vert
, uv
) );
212 glEnableVertexAttribArray( 1 );
215 glVertexAttribPointer( 2, 4, GL_UNSIGNED_BYTE
, GL_TRUE
, stride
,
216 (void *)offsetof( struct ui_vert
, colour
) );
217 glEnableVertexAttribArray( 2 );
220 glVertexAttribPointer( 3, 4, GL_SHORT
, GL_FALSE
, stride
,
221 (void *)offsetof( struct ui_vert
, clip
) );
222 glEnableVertexAttribArray( 3 );
226 /* Alloc RAM default context */
227 u32 vert_size
= vertex_buffer_size
*sizeof(struct ui_vert
),
228 inds_size
= index_buffer_size
*sizeof(u16
);
230 vg_uictx
.vertex_buffer
=
231 vg_create_linear_allocator(vg_mem
.rtmemory
,vert_size
);
233 vg_uictx
.indice_buffer
=
234 vg_create_linear_allocator(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
.num_indices
;
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
.num_verts
* 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
.num_indices
* 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 struct ui_vert
*vertices
= &vg_uictx
.vertex_buffer
[ vg_uictx
.num_verts
];
497 u16
*indices
= &vg_uictx
.indice_buffer
[ vg_uictx
.num_indices
];
499 vertices
[0].co
[0] = rect
[0];
500 vertices
[0].co
[1] = rect
[1];
501 vertices
[0].uv
[0] = uv
[0];
502 vertices
[0].uv
[1] = uv
[1];
503 vertices
[0].colour
= colour
;
504 vertices
[1].co
[0] = rect
[0]+rect
[2];
505 vertices
[1].co
[1] = rect
[1];
506 vertices
[1].uv
[0] = uv
[2];
507 vertices
[1].uv
[1] = uv
[1];
508 vertices
[1].colour
= colour
;
509 vertices
[2].co
[0] = rect
[0]+rect
[2];
510 vertices
[2].co
[1] = rect
[1]+rect
[3];
511 vertices
[2].uv
[0] = uv
[2];
512 vertices
[2].uv
[1] = uv
[3];
513 vertices
[2].colour
= colour
;
514 vertices
[3].co
[0] = rect
[0];
515 vertices
[3].co
[1] = rect
[1]+rect
[3];
516 vertices
[3].uv
[0] = uv
[0];
517 vertices
[3].uv
[1] = uv
[3];
518 vertices
[3].colour
= colour
;
519 u16 ind_start
= vg_uictx
.num_verts
;
521 ui_rect_copy( vg_uictx
.clipping
, vertices
[0].clip
);
522 ui_rect_copy( vg_uictx
.clipping
, vertices
[1].clip
);
523 ui_rect_copy( vg_uictx
.clipping
, vertices
[2].clip
);
524 ui_rect_copy( vg_uictx
.clipping
, vertices
[3].clip
);
526 indices
[0] = ind_start
+0;
527 indices
[1] = ind_start
+2;
528 indices
[2] = ind_start
+1;
530 indices
[3] = ind_start
+0;
531 indices
[4] = ind_start
+3;
532 indices
[5] = ind_start
+2;
534 vg_uictx
.num_indices
+= 6;
535 vg_uictx
.num_verts
+= 4;
540 static struct ui_vert
*ui_fill_rect( ui_rect rect
, u32 colour
)
542 return ui_fill_rect_uv( rect
, colour
, (ui_px
[4]){ 4,4, 4,4 } );
545 static ui_px
ui_text_line_offset( const char *str
, ui_px scale
,
546 enum ui_text_align align
)
548 if( align
== k_text_align_left
)
552 const char *_c
= str
;
555 while( (c
= *(_c
++)) )
556 if( c
>= 32 && c
<= 126 )
561 if( align
== k_text_align_right
)
562 return -length
* scale
*8;
564 return (-length
* scale
*8) / 2;
567 VG_STATIC
void ui_text( ui_px pos
[2],
568 const char *str
, ui_px scale
, enum ui_text_align align
)
571 u32 current_colour
= 0x00ffffff;
573 const char *_c
= str
;
576 text_cursor
[0] = pos
[0] + ui_text_line_offset( str
, scale
, align
);
577 text_cursor
[1] = pos
[1];
578 text_cursor
[2] = 8*scale
;
579 text_cursor
[3] = 14*scale
;
581 while( (c
= *(_c
++)) )
585 text_cursor
[1] += 14*scale
;
586 text_cursor
[0] = pos
[0] + ui_text_line_offset( _c
, scale
, align
);
593 glyph_base
[0] = glyph_index
& 0xf;
594 glyph_base
[1] = (glyph_index
-glyph_base
[0])>>4;
599 ui_fill_rect_uv( text_cursor
, current_colour
, (ui_px
[4])
607 else if( c
== '\x1B' )
611 for( int i
= 0; i
< 3; i
++ )
621 case '0': current_colour
= 0x00ffffff; break;
622 case '3'|'1'<<8: current_colour
= 0x00201fee; break;
623 case '3'|'2'<<8: current_colour
= 0x0037e420; break;
624 case '3'|'3'<<8: current_colour
= 0x000ed8e2; break;
625 case '3'|'4'<<8: current_colour
= 0x00f15010; break;
626 case '3'|'5'<<8: current_colour
= 0x00ee20ee; break;
627 case '3'|'6'<<8: current_colour
= 0x00eeee20; break;
628 case '3'|'7'<<8: current_colour
= 0x00ffffff; break;
634 colour_id
|= _c
[i
] << (i
*8);
647 text_cursor
[0] += UI_GLYPH_SPACING_X
*scale
*4;
651 text_cursor
[0] += UI_GLYPH_SPACING_X
*scale
;
658 VG_STATIC
void ui_begin( ui_px res_x
, ui_px res_y
)
660 vg_uictx
.cursor
[0] = 0;
661 vg_uictx
.cursor
[1] = 0;
662 vg_uictx
.cursor
[2] = res_x
;
663 vg_uictx
.cursor
[3] = res_y
;
665 ui_rect_copy( vg_uictx
.cursor
, vg_uictx
.stack
[0].rect
);
666 vg_uictx
.stack
[0].mouse_over
= 1;
668 vg_uictx
.stack_count
= 1;
670 vg_uictx
.num_verts
= 0;
671 vg_uictx
.num_indices
= 0;
675 if( vg_uictx
.click_state
== 0 )
676 vg_uictx
.capture_mouse_id
= 0;
678 vg_uictx
.image_count
= 0;
681 VG_STATIC
void ui_resolve(void)
683 if( vg_uictx
.stack_count
-1 )
685 vg_error( "[UI] Mismatched node create/drestroy!" );
689 if( vg_uictx
.click_state
== 3 || vg_uictx
.click_state
== 0 )
691 vg_uictx
.capture_lock
= 0;
695 VG_STATIC
void ui_set_mouse( int x
, int y
, int click_state
)
697 vg_uictx
.mouse
[0] = x
;
698 vg_uictx
.mouse
[1] = y
;
700 vg_uictx
.click_state
= click_state
;
704 * High level controls
712 ui_px drag_offset
[2];
717 k_button_released
= 0,
718 k_button_start_click
,
723 static int ui_button(void)
725 u32 uid
= vg_uictx
.control_id
++;
729 ui_capture_mouse( uid
);
733 ui_fill_rect( vg_uictx
.cursor
, vg_uictx
.colours
->hover
);
735 if( vg_uictx
.click_state
== 1 )
737 vg_uictx
.capture_lock
= 1;
738 return k_button_start_click
;
740 else if( vg_uictx
.capture_lock
&& vg_uictx
.click_state
== 3 )
741 return k_button_click
;
742 else if( vg_uictx
.capture_lock
&& vg_uictx
.click_state
== 2 )
743 return k_button_hold
;
745 return k_button_click
;
748 ui_fill_rect( vg_uictx
.cursor
, vg_uictx
.colours
->main
);
751 return k_button_released
;
754 static int ui_window( struct ui_window
*window
, u32 control_group
)
758 window
->transform
[0] = vg_uictx
.mouse
[0]+window
->drag_offset
[0];
759 window
->transform
[1] = vg_uictx
.mouse
[1]+window
->drag_offset
[1];
761 ui_clamp_rect( vg_uictx
.stack
[0].rect
, window
->transform
);
763 if( vg_uictx
.click_state
== 0 || vg_uictx
.click_state
== 3 )
769 ui_rect_copy( window
->transform
, vg_uictx
.cursor
);
773 ui_capture_mouse( vg_uictx
.control_id
++ );
776 vg_uictx
.cursor
[3] = 25;
779 ui_capture_mouse( vg_uictx
.control_id
++ );
781 struct ui_vert
*drag_bar
= ui_fill_rect( vg_uictx
.cursor
, 0xff555555 );
784 vg_uictx
.cursor
[0] += 2;
785 vg_uictx
.cursor
[1] += 2;
786 ui_text( vg_uictx
.cursor
, window
->title
, 2, 0 );
789 vg_uictx
.cursor
[3] = 25;
790 vg_uictx
.cursor
[2] = 25;
793 ui_rect_pad( vg_uictx
.cursor
, 4 );
797 vg_info( "Click clacked\n" );
799 vg_uictx
.cursor
[0] += 2;
800 ui_text( vg_uictx
.cursor
, "x", 2, 0 );
805 drag_bar
[0].colour
= 0xff777777;
806 drag_bar
[1].colour
= 0xff777777;
807 drag_bar
[2].colour
= 0xff777777;
808 drag_bar
[3].colour
= 0xff777777;
811 if( vg_uictx
.click_state
== 1 )
814 window
->drag_offset
[0] = window
->transform
[0]-vg_uictx
.mouse
[0];
815 window
->drag_offset
[1] = window
->transform
[1]-vg_uictx
.mouse
[1];
825 VG_STATIC
void ui_push_image( ui_rect rc
, GLuint image
)
827 struct ui_image
*img
= &vg_uictx
.images
[ vg_uictx
.image_count
++ ];
828 ui_rect_copy( rc
, img
->rc
);
838 struct ui_slider_vector
840 float *data
, min
, max
;
841 struct ui_slider sub
[4];
850 VG_STATIC
void ui_slider( struct ui_slider
*slider
)
854 ui_px slider_start
= vg_uictx
.cursor
[0];
856 float const ftotal
= vg_uictx
.cursor
[2],
857 fwidth
= ftotal
*0.25f
,
858 fmove
= ftotal
- fwidth
,
859 fstart
= fwidth
*0.5f
,
860 frange
= slider
->max
-slider
->min
,
861 fpos
= (*slider
->data
- slider
->min
) / frange
;
863 ui_fill_rect( vg_uictx
.cursor
, 0xff111111 );
864 vg_uictx
.cursor
[2] = fwidth
;
865 vg_uictx
.cursor
[0] = slider_start
+ fpos
* fmove
;
867 u32 uid
= vg_uictx
.control_id
++;
868 int status
= ui_button();
869 if( vg_uictx
.capture_lock
&& (vg_uictx
.capture_mouse_id
== uid
))
871 float ui_new
= vg_uictx
.mouse
[0],
872 local
= ui_new
- (slider_start
+ fstart
),
873 zo
= vg_clampf(local
/ fmove
,0.0f
,1.0f
);
875 *slider
->data
= vg_lerpf( slider
->min
, slider
->max
, zo
);
878 vg_uictx
.cursor
[0] += 4;
879 vg_uictx
.cursor
[1] += 4;
882 snprintf( buf
, 12, "%.2f", *slider
->data
);
883 ui_text( vg_uictx
.cursor
, buf
, 1, 0 );
888 VG_STATIC
void ui_slider_vector( struct ui_slider_vector
*slider
)
890 for( int i
=0; i
<slider
->len
; i
++ )
892 slider
->sub
[i
].data
= &slider
->data
[i
];
893 slider
->sub
[i
].min
= slider
->min
;
894 slider
->sub
[i
].max
= slider
->max
;
895 ui_slider( &slider
->sub
[i
] );
899 VG_STATIC
void ui_checkbox( struct ui_checkbox
*cb
)
901 if( ui_button() == k_button_click
)
905 ui_rect_pad( vg_uictx
.cursor
, 4 );
907 ui_fill_rect( vg_uictx
.cursor
, 0xff00e052 );
909 ui_fill_rect( vg_uictx
.cursor
, 0xff0052e0 );