/* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */
+/*
+ * TODO: Get rid of many context design
+ */
+
#ifndef VG_UI_H
#define VG_UI_H
+#include "vg/vg.h"
+#include "vg/vg_tex.h"
+#include "vg/vg_shader.h"
+
static struct vg_shader _shader_ui =
{
.name = "[vg] ui",
}
*verts;
#pragma pack(pop)
+
+ u32 control_id;
u32 num_verts;
u16 *indices;
u32 stack_count;
u32 capture_mouse_id;
int capture_lock;
- u32 id_base;
- int glyph_base;
+
/* User input */
ui_px mouse[2];
static ui_ctx ui_global_ctx;
-static void ui_init_context( ui_ctx *ctx, int index_buffer_size )
+static void ui_context_vg_free( ui_ctx *ctx )
{
- u32 vertex_buffer_size = (index_buffer_size+(index_buffer_size/2));
-
- /* Generate the buffer we are gonna be drawing to */
- {
- glGenVertexArrays(1, &ctx->vao);
- glGenBuffers( 1, &ctx->vbo );
- glGenBuffers( 1, &ctx->ebo );
- glBindVertexArray( ctx->vao );
-
- glBindBuffer( GL_ARRAY_BUFFER, ctx->vbo );
-
- glBufferData( GL_ARRAY_BUFFER,
- vertex_buffer_size * sizeof( struct ui_vert ),
- NULL, GL_DYNAMIC_DRAW );
- glBindVertexArray( ctx->vao );
-
- glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, ctx->ebo );
- glBufferData( GL_ELEMENT_ARRAY_BUFFER,
- index_buffer_size * sizeof( u16 ), NULL, GL_DYNAMIC_DRAW );
-
- u32 const stride = sizeof( struct ui_vert );
-
- /* XY */
- glVertexAttribPointer( 0, 2, GL_SHORT, GL_FALSE,
- stride, (void *)offsetof( struct ui_vert, co ) );
- glEnableVertexAttribArray( 0 );
-
- /* UV */
- glVertexAttribPointer( 1, 2, GL_UNSIGNED_BYTE, GL_FALSE,
- stride, (void *)offsetof( struct ui_vert, uv ) );
- glEnableVertexAttribArray( 1 );
-
- /* COLOUR */
- glVertexAttribPointer( 2, 4, GL_UNSIGNED_BYTE, GL_TRUE, stride,
- (void *)offsetof( struct ui_vert, colour ) );
- glEnableVertexAttribArray( 2 );
-
- /* CLIPPING */
- glVertexAttribPointer( 3, 4, GL_SHORT, GL_FALSE, stride,
- (void *)offsetof( struct ui_vert, clip ) );
- glEnableVertexAttribArray( 3 );
- }
+ glDeleteVertexArrays( 1, &ctx->vao );
+ glDeleteBuffers( 1, &ctx->vbo );
+ glDeleteBuffers( 1, &ctx->ebo );
- /* Initialize default context */
- {
- ctx->verts = (struct ui_vert *)malloc(
- vertex_buffer_size * sizeof(struct ui_vert) );
- ctx->indices = (u16*)malloc( index_buffer_size * sizeof(u16) );
-
- if( !ctx->colours )
- ctx->colours = &ui_default_colours;
- }
+ vg_free( ctx->verts );
+ vg_free( ctx->indices );
}
-static void ui_context_free( ui_ctx *ctx )
+static int ui_init_context( ui_ctx *ctx, int index_buffer_size )
{
- glDeleteVertexArrays( 1, &ctx->vao );
- glDeleteBuffers( 1, &ctx->vbo );
- glDeleteBuffers( 1, &ctx->ebo );
+ u32 vertex_buffer_size = (index_buffer_size+(index_buffer_size/2));
- free( ctx->verts );
- free( ctx->indices );
+ /* Generate the buffer we are gonna be drawing to */
+ glGenVertexArrays( 1, &ctx->vao );
+ glGenBuffers( 1, &ctx->vbo );
+ glGenBuffers( 1, &ctx->ebo );
+
+ glBindVertexArray( ctx->vao );
+ glBindBuffer( GL_ARRAY_BUFFER, ctx->vbo );
+
+ glBufferData( GL_ARRAY_BUFFER,
+ vertex_buffer_size * sizeof( struct ui_vert ),
+ NULL, GL_DYNAMIC_DRAW );
+ glBindVertexArray( ctx->vao );
+
+ glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, ctx->ebo );
+ glBufferData( GL_ELEMENT_ARRAY_BUFFER,
+ index_buffer_size * sizeof( u16 ), NULL, GL_DYNAMIC_DRAW );
+
+ VG_CHECK_GL_ERR();
+
+ /* Set pointers */
+ u32 const stride = sizeof( struct ui_vert );
+
+ /* XY */
+ glVertexAttribPointer( 0, 2, GL_SHORT, GL_FALSE,
+ stride, (void *)offsetof( struct ui_vert, co ) );
+ glEnableVertexAttribArray( 0 );
+
+ /* UV */
+ glVertexAttribPointer( 1, 2, GL_UNSIGNED_BYTE, GL_FALSE,
+ stride, (void *)offsetof( struct ui_vert, uv ) );
+ glEnableVertexAttribArray( 1 );
+
+ /* COLOUR */
+ glVertexAttribPointer( 2, 4, GL_UNSIGNED_BYTE, GL_TRUE, stride,
+ (void *)offsetof( struct ui_vert, colour ) );
+ glEnableVertexAttribArray( 2 );
+
+ /* CLIPPING */
+ glVertexAttribPointer( 3, 4, GL_SHORT, GL_FALSE, stride,
+ (void *)offsetof( struct ui_vert, clip ) );
+ glEnableVertexAttribArray( 3 );
+
+ VG_CHECK_GL_ERR();
+
+ /* Alloc RAM default context */
+ ctx->verts = (struct ui_vert *)vg_alloc(
+ vertex_buffer_size * sizeof(struct ui_vert) );
+ if( !ctx->verts )
+ goto il_fail_alloc_verts;
+
+ ctx->indices = (u16*)vg_alloc( index_buffer_size * sizeof(u16) );
+ if( !ctx->indices )
+ goto il_fail_alloc_indices;
+
+ if( !ctx->colours )
+ ctx->colours = &ui_default_colours;
+
+ return 1;
+
+ vg_free( ctx->indices );
+ ctx->indices = NULL;
+il_fail_alloc_indices:
+ vg_free( ctx->verts );
+ ctx->verts = NULL;
+il_fail_alloc_verts:
+ glDeleteBuffers( 1, &ctx->ebo );
+ glDeleteBuffers( 1, &ctx->vbo );
+ glDeleteVertexArrays( 1, &ctx->vao );
+ VG_CHECK_GL_ERR();
+ return 0;
}
-static void ui_default_init(void)
+static int ui_default_init(void)
{
/* Load default font */
u32 compressed[] = {
};
u32 pixels = 0, total = 256*256, data = 0;
- u8 *image = malloc( total );
+ u8 *image = vg_alloc( total );
while( pixels < total )
{
glGenTextures( 1, &ui_glyph_texture );
glBindTexture( GL_TEXTURE_2D, ui_glyph_texture );
-
glTexImage2D( GL_TEXTURE_2D, 0, GL_R8, 256, 256, 0,
GL_RED, GL_UNSIGNED_BYTE, image );
+ vg_free( image );
+
+ VG_CHECK_GL_ERR();
+
vg_tex2d_clamp();
vg_tex2d_nearest();
-
- free( image );
- vg_shader_register( &_shader_ui );
- ui_init_context( &ui_global_ctx, 20000 );
+ if( !ui_init_context( &ui_global_ctx, 20000 ) ) goto il_generic_fail;
+ if( !vg_shader_compile( &_shader_ui ) ) goto il_shader_fail;
+
+ return 1;
+
+il_shader_fail:
+il_generic_fail:
+ glDeleteTextures( 1, &ui_glyph_texture );
+ vg_free( image );
+ return 0;
}
static void ui_default_free(void)
{
+ vg_free_shader( &_shader_ui );
glDeleteTextures( 1, &ui_glyph_texture );
- ui_context_free( &ui_global_ctx );
+ ui_context_vg_free( &ui_global_ctx );
}
static struct ui_vert *ui_fill_rect_uv( ui_ctx *ctx, ui_rect rect,
static void ui_new_node( ui_ctx *ctx )
{
if( ctx->stack_count == vg_list_size( ctx->stack ) )
- vg_exiterr( "[UI] Stack overflow while creating box!" );
+ {
+ vg_error( "[UI] Stack overflow while creating box!" );
+ return;
+ }
struct ui_qnode *parent = &ctx->stack[ ctx->stack_count-1 ];
struct ui_qnode *node = &ctx->stack[ ctx->stack_count++ ];
dest[1] = vg_max( parent[1], dest[1] );
}
-static u32 ui_group_id( ui_ctx *ctx, u32 lesser_unique )
-{
- return ctx->id_base | lesser_unique;
-}
-
static void ui_capture_mouse( ui_ctx *ctx, u32 id )
{
- u32 group_uid = ui_group_id(ctx,id);
-
struct ui_qnode *node = &ctx->stack[ ctx->stack_count-1 ];
- node->capture_id = group_uid;
+ node->capture_id = id;
if( !ctx->capture_lock && node->mouse_over )
{
- ctx->capture_mouse_id = group_uid;
+ ctx->capture_mouse_id = id;
}
}
static void ui_resolve( ui_ctx *ctx )
{
if( ctx->stack_count-1 )
- vg_exiterr( "[UI] Mismatched node create/drestroy!" );
+ {
+ vg_error( "[UI] Mismatched node create/drestroy!" );
+ return;
+ }
if( ctx->click_state == 3 || ctx->click_state == 0 )
{
k_button_hold
};
-static int ui_button( ui_ctx *ctx, u32 id )
+static int ui_button( ui_ctx *ctx )
{
+ u32 uid = ctx->control_id ++;
+
ui_new_node( ctx );
{
- ui_capture_mouse( ctx, id );
+ ui_capture_mouse( ctx, uid );
if( ui_hasmouse(ctx) )
{
return k_button_click;
else if( ctx->capture_lock && ctx->click_state == 2 )
return k_button_hold;
+
+ return k_button_click;
}
else
ui_fill_rect( ctx, ctx->cursor, ctx->colours->main );
static int ui_window( ui_ctx *ctx, struct ui_window *window, u32 control_group )
{
- ctx->id_base = control_group << 16;
-
if( window->drag )
{
window->transform[0] = ctx->mouse[0]+window->drag_offset[0];
ui_new_node( ctx );
{
- ui_capture_mouse( ctx, __COUNTER__ );
+ ui_capture_mouse( ctx, ctx->control_id ++ );
/* Drag bar */
ctx->cursor[3] = 25;
ui_new_node( ctx );
{
- ui_capture_mouse( ctx, __COUNTER__ );
+ ui_capture_mouse( ctx, ctx->control_id ++ );
struct ui_vert *drag_bar = ui_fill_rect( ctx, ctx->cursor, 0xff555555 );
ui_align_top( ctx );
ui_rect_pad( ctx->cursor, 4 );
- if( ui_button( ctx, __COUNTER__ ) )
+ if( ui_button( ctx ) )
{
vg_info( "Click clacked\n" );
}
ctx->cursor[2] = fwidth;
ctx->cursor[0] = slider_start + fpos * fmove;
- /* TODO.. */
- u32 uid = (u32)(u64)slider->data;
- int status = ui_button( ctx, uid );
-
- if( ctx->capture_lock &&
- (ctx->capture_mouse_id == ui_group_id( ctx, uid )))
+ u32 uid = ctx->control_id ++;
+ int status = ui_button( ctx );
+ if( ctx->capture_lock && (ctx->capture_mouse_id == uid))
{
float ui_new = ctx->mouse[0],
local = ui_new - (slider_start + fstart),
snprintf( buf, 12, "%.2f", *slider->data );
ui_text( ctx, ctx->cursor, buf, 1, 0 );
ui_end_down( ctx );
-
ui_end_down( ctx );
}
static void ui_checkbox( ui_ctx *ctx, struct ui_checkbox *cb )
{
- u32 uid = (u32)(u64)cb->data;
- if( ui_button(ctx,uid) == k_button_click )
+ if( ui_button(ctx) == k_button_click )
*cb->data ^= 0x1;
ui_new_node(ctx);
#define gui_align_top() ui_align_top( &ui_global_ctx )
#define gui_align_left() ui_align_left( &ui_global_ctx )
#define gui_clamp_rect(...) ui_clamp_rect( &ui_global_ctx, __VA_ARGS__)
-#define gui_group_id(...) ui_group_id( &ui_global_ctx, __VA_ARGS__)
#define gui_capture_mouse(...) ui_capture_mouse( &ui_global_ctx, __VA_ARGS__)
#define gui_set_clip(...) ui_set_clip( &ui_global_ctx, __VA_ARGS__)
#define gui_release_clip() ui_release_clip( &ui_global_ctx )
#define gui_push_image(...) ui_push_image( &ui_global_ctx, __VA_ARGS__ )
#define gui_reset_colours(...) ui_reset_colours( &ui_global_ctx )
-#endif
+#endif /* VG_UI_H */