"float clip_blend = step( aWsp.x, aClip.z ) * step( aWsp.y, aClip.w ) * step( aClip.x, aWsp.x ) * step( aClip.y, aWsp.y );"
"vec4 glyph = texture( uTexGlyphs, aTexCoords );"
- "FragColor = aColour * vec4( 1.0, 1.0, 1.0, glyph.r * clip_blend );"
+ "FragColor = vec4( aColour.rgb * glyph.rgb, aColour.a*glyph.a*clip_blend );"
"}"
,
UNIFORMS({ "uPv", "uTexGlyphs" })
typedef u32 ui_colour;
typedef ui_px ui_rect[4];
typedef struct ui_ctx ui_ctx;
+typedef struct ui_colourset ui_colourset;
+
+struct ui_colourset
+{
+ union
+ {
+ struct
+ {
+ ui_colour main;
+ ui_colour hover;
+ ui_colour active;
+ };
+ struct
+ {
+ ui_colour background;
+ ui_colour bar;
+ ui_colour bar_hover;
+ };
+ };
+};
struct ui_ctx
{
u32 capture_mouse_id;
int capture_lock;
u32 id_base;
+ int glyph_base;
// User input
ui_px mouse[2];
int click_state; // 0: released, 1: on down, 2: pressed, 3: on release
+
+ ui_colourset *colours_main;
+ ui_colourset *colours_current;
+
+ GLuint vao;
+ GLuint vbo;
+ GLuint ebo;
+
+ struct ui_image
+ {
+ ui_rect rc;
+ GLuint image;
+ }
+ images[16];
+ int image_count;
};
// Globals
int ui_glyph_override = 0;
ui_px ui_glyph_spacing_x = 6;
GLuint ui_glyph_texture = 0;
-GLuint ui_vao;
-GLuint ui_vbo;
-GLuint ui_ebo;
-
-#define UI_BUFFER_SIZE 30000
-#define UI_INDEX_SIZE 20000
-ui_ctx ui_global_ctx = { .padding = 8 };
+ui_colourset ui_default_colours = {
+ .main = 0xff00ff00,
+ .hover = 0xffff00ff,
+ .active = 0xffff0000
+};
+ui_ctx ui_global_ctx = {
+ .padding = 8,
+ .colours_main = &ui_default_colours
+};
// Initialization
// ===========================================================================================================
+static void ui_reset_colours( ui_ctx *ctx );
+static void ui_init_context( ui_ctx *ctx, int index_buffer_size )
+{
+ ui_reset_colours( 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 );
+ }
+
+ // 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) );
+ }
+}
+
+static void ui_context_free( ui_ctx *ctx )
+{
+ glDeleteVertexArrays( 1, &ctx->vao );
+ glDeleteBuffers( 1, &ctx->vbo );
+ glDeleteBuffers( 1, &ctx->ebo );
+
+ free( ctx->verts );
+ free( ctx->indices );
+}
+
static void ui_override_font( GLuint new_tex, ui_px space_x )
{
if( ui_glyph_texture )
}
// Setup OpenGL memory
- {
- SHADER_INIT( shader_ui );
-
- // Generate the buffer we are gonna be drawing to
- glGenVertexArrays(1, &ui_vao);
- glGenBuffers( 1, &ui_vbo );
- glGenBuffers( 1, &ui_ebo );
- glBindVertexArray( ui_vao );
-
- glBindBuffer( GL_ARRAY_BUFFER, ui_vbo );
-
- glBufferData( GL_ARRAY_BUFFER, UI_BUFFER_SIZE * sizeof( struct ui_vert ), NULL, GL_DYNAMIC_DRAW );
- glBindVertexArray( ui_vao );
-
- glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, ui_ebo );
- glBufferData( GL_ELEMENT_ARRAY_BUFFER, UI_INDEX_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 );
- }
-
- // Initialize default context
- {
- ui_global_ctx.verts = (struct ui_vert *)malloc( UI_BUFFER_SIZE * sizeof(struct ui_vert) );
- ui_global_ctx.indices = (u16*)malloc( UI_INDEX_SIZE * sizeof(u16) );
- }
+ SHADER_INIT( shader_ui );
+ ui_init_context( &ui_global_ctx, 20000 );
}
static void ui_default_free(void)
if( !ui_glyph_override )
glDeleteTextures( 1, &ui_glyph_texture );
- glDeleteVertexArrays( 1, &ui_vao );
- glDeleteBuffers( 1, &ui_vbo );
- glDeleteBuffers( 1, &ui_ebo );
-
- free( ui_global_ctx.verts );
- free( ui_global_ctx.indices );
+ ui_context_free( &ui_global_ctx );
}
+static struct ui_vert *ui_fill_rect_uv( ui_ctx *ctx, ui_rect rect, u32 colour, ui_px uv[4] );
static void ui_draw( ui_ctx *ctx, m3x3f view_override )
{
- glBindVertexArray( ui_vao );
+ u32 num_indices_normal = ctx->num_indices;
- glBindBuffer( GL_ARRAY_BUFFER, ui_vbo );
+ // Append images to back of buffer
+ for( int i = 0; i < ctx->image_count; i ++ )
+ ui_fill_rect_uv( ctx, ctx->images[i].rc, 0xffffffff, (ui_px[4]){0,0,128,128} );
+
+ glBindVertexArray( ctx->vao );
+
+ glBindBuffer( GL_ARRAY_BUFFER, ctx->vbo );
glBufferSubData( GL_ARRAY_BUFFER, 0, ctx->num_verts * sizeof( struct ui_vert ), ctx->verts );
- glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, ui_ebo );
+ glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, ctx->ebo );
glBufferSubData( GL_ELEMENT_ARRAY_BUFFER, 0, ctx->num_indices * sizeof( u16 ), ctx->indices );
glEnable(GL_BLEND);
glBindTexture( GL_TEXTURE_2D, ui_glyph_texture );
glUniform1i( SHADER_UNIFORM( shader_ui, "uTexGlyphs" ), 0 );
- glDrawElements( GL_TRIANGLES, ctx->num_indices, GL_UNSIGNED_SHORT, (void*)(0) );
+ glDrawElements( GL_TRIANGLES, num_indices_normal, GL_UNSIGNED_SHORT, (void*)(0) );
- //vg_info( "Verts: %u, Indices: %u\n", ctx->num_verts, ctx->num_indices );
+ // Draw image elements
+ for( int i = 0; i < ctx->image_count; i ++ )
+ {
+ struct ui_image *img = &ctx->images[i];
+
+ glBindTexture( GL_TEXTURE_2D, img->image );
+ glDrawElements( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, (void*)( (num_indices_normal + 6*i)*sizeof(u16) ) );
+ }
glDisable(GL_BLEND);
}
}
}
+static int ui_want_mouse( ui_ctx *ctx )
+{
+ return ctx->capture_mouse_id == 0? 0: 1;
+}
+
static void ui_set_clip( ui_ctx *ctx, ui_rect clip )
{
ctx->clipping[0] = clip[0];
return ui_fill_rect_uv( ctx, rect, colour, (ui_px[4]){ 4,124,4,124 } );
}
-static void ui_text( ui_ctx *ctx, const char *str, ui_px scale, int alignment )
+static void ui_text_use_title( ui_ctx *ctx )
+{
+ ctx->glyph_base = 0;
+}
+
+static void ui_text_use_paragraph( ui_ctx *ctx )
+{
+ ctx->glyph_base = 6;
+}
+
+enum text_alignment
+{
+ k_text_alignment_left = 0,
+ k_text_alignment_center,
+ k_text_alignment_right
+};
+
+static ui_px ui_text( ui_ctx *ctx, const char *str, ui_px scale, enum text_alignment alignment )
{
ui_rect text_cursor;
text_cursor[0] = ctx->cursor[0];
text_cursor[1] = ctx->cursor[1];
- text_cursor[2] = scale*8;
- text_cursor[3] = scale*8;
+ text_cursor[2] = (scale*8)/2;
+ text_cursor[3] = (scale*8)/2;
u32 current_colour = ctx->override_colour;
+ ui_px offset = 0;
+ if( alignment != k_text_alignment_left )
+ {
+ const char *pch = str;
+ for(;;)
+ {
+ offset += (ui_glyph_spacing_x*scale)/4;
+ if( !(*pch) || *pch == '\n' )
+ break;
+ pch ++;
+ }
+
+ if( alignment == k_text_alignment_right )
+ text_cursor[0] = ctx->cursor[0]+ctx->cursor[2]-offset;
+ else
+ text_cursor[0] = (ctx->cursor[0]+(ctx->cursor[2]/2))-(offset/2);
+ }
+
const char *_c = str;
char c;
while( (c = *(_c ++)) )
{
if( c == '\n' )
{
- text_cursor[1] += 10*scale;
+ text_cursor[1] += (7*scale)/2;
text_cursor[0] = ctx->cursor[0];
continue;
}
{
u8 glyph_base[2];
u8 glyph_index = c - 32;
- glyph_base[0] = glyph_index&0xf;
- glyph_base[1] = (glyph_index-glyph_base[0])>>4;
-
+ glyph_base[0] = (glyph_index&0xf);
+ glyph_base[1] = ctx->glyph_base + ((glyph_index-glyph_base[0])>>4);
+
glyph_base[0] *= 8;
glyph_base[1] *= 8;
continue;
}
- text_cursor[0] += (ui_glyph_spacing_x*scale)/2;
+ text_cursor[0] += (ui_glyph_spacing_x*scale)/4;
}
+
+ return text_cursor[0];
}
// API control
ctx->num_indices = 0;
ui_release_clip( ctx );
+
+ if( ctx->click_state == 0 )
+ ctx->capture_mouse_id = 0;
+
+ ctx->image_count = 0;
}
static void ui_resolve( ui_ctx *ctx )
ui_px drag_offset[2];
};
+enum button_state
+{
+ k_button_released = 0,
+ k_button_start_click,
+ k_button_click,
+ k_button_hold
+};
+
static int ui_button( ui_ctx *ctx, u32 id )
{
ui_new_node( ctx );
if( ui_hasmouse(ctx) )
{
- ui_fill_rect( ctx, ctx->cursor, 0xffcccccc );
-
+ ui_fill_rect( ctx, ctx->cursor, ctx->colours_current->hover );
+
if( ctx->click_state == 1 )
- ctx->capture_lock = 1;
- else if( ctx->capture_lock && ctx->click_state == 3 )
{
- return 1;
+ ctx->capture_lock = 1;
+ return k_button_start_click;
}
+ else if( ctx->capture_lock && ctx->click_state == 3 )
+ return k_button_click;
+ else if( ctx->capture_lock && ctx->click_state == 2 )
+ return k_button_hold;
}
else
- ui_fill_rect( ctx, ctx->cursor, 0xff999999 );
+ ui_fill_rect( ctx, ctx->cursor, ctx->colours_current->main );
}
- return 0;
+ return k_button_released;
}
static int ui_window( ui_ctx *ctx, struct ui_window *window, u32 control_group )
return 1;
}
+
+struct ui_scrollbar
+{
+ int drag;
+ ui_px drag_offset;
+
+ ui_px py;
+ ui_px bar_height;
+ ui_px view_height;
+};
+
+static void ui_scrollbar( ui_ctx *ctx, struct ui_scrollbar *scrollbar, u32 id )
+{
+ scrollbar->view_height = ctx->cursor[3];
+
+ if( scrollbar->drag )
+ {
+ scrollbar->py = ctx->mouse[1]+scrollbar->drag_offset;
+ scrollbar->py = VG_MAX( scrollbar->py, 0 );
+ scrollbar->py = VG_MIN( scrollbar->py, ctx->cursor[3] - scrollbar->bar_height );
+
+ if( ctx->click_state == 0 || ctx->click_state == 3 )
+ scrollbar->drag = 0;
+ }
+
+ ui_new_node( ctx );
+ {
+ ui_fill_rect( ctx, ctx->cursor, ctx->colours_current->background );
+ ui_capture_mouse( ctx, id );
+
+ ctx->cursor[1] += scrollbar->py;
+ ctx->cursor[3] = scrollbar->bar_height;
+
+ ui_new_node( ctx );
+ {
+ ui_capture_mouse( ctx, __COUNTER__ );
+ struct ui_vert *drag_bar = ui_fill_rect( ctx, ctx->cursor, ctx->colours_current->bar );
+
+ if( ui_hasmouse( ctx ) || scrollbar->drag )
+ {
+ drag_bar[0].colour = ctx->colours_current->bar_hover;
+ drag_bar[1].colour = ctx->colours_current->bar_hover;
+ drag_bar[2].colour = ctx->colours_current->bar_hover;
+ drag_bar[3].colour = ctx->colours_current->bar_hover;
+
+ // start drag
+ if( ctx->click_state == 1 )
+ {
+ scrollbar->drag = 1;
+ scrollbar->drag_offset = scrollbar->py - ctx->mouse[1];
+ }
+ }
+ }
+ ui_end_down( ctx );
+ }
+ ui_end( ctx );
+}
+
+static ui_px ui_calculate_content_scroll( struct ui_scrollbar *scrollbar, ui_px content )
+{
+ float overlap = vg_maxf( 0.0f, (float)(content - scrollbar->view_height) );
+
+ float range = scrollbar->view_height - scrollbar->bar_height;
+ return ((float)scrollbar->py / range) * overlap;
+}
+
+static void ui_override_colours( ui_ctx *ctx, ui_colourset *set )
+{
+ ctx->colours_current = set;
+}
+
+static void ui_reset_colours( ui_ctx *ctx )
+{
+ ctx->colours_current = ctx->colours_main;
+ ctx->override_colour = 0xffffffff;
+}
+
+static void ui_push_image( ui_ctx *ctx, ui_rect rc, GLuint image )
+{
+ struct ui_image *img = &ctx->images[ ctx->image_count ++ ];
+ ui_rect_copy( rc, img->rc );
+ img->image = image;
+}
+
+// Shortnames
+#define gui_draw(...) ui_draw( &ui_global_ctx, __VA_ARGS__)
+#define gui_current(...) ui_current( &ui_global_ctx, __VA_ARGS__)
+#define gui_new_node() ui_new_node( &ui_global_ctx )
+#define gui_hasmouse(...) ui_hasmouse( &ui_global_ctx, __VA_ARGS__)
+#define gui_end() ui_end( &ui_global_ctx )
+#define gui_end_down() ui_end_down( &ui_global_ctx )
+#define gui_end_right() ui_end_right( &ui_global_ctx )
+#define gui_fill_y() ui_fill_y( &ui_global_ctx)
+#define gui_fill_x() ui_fill_x( &ui_global_ctx)
+#define gui_align_bottom() ui_align_bottom( &ui_global_ctx )
+#define gui_align_right() ui_align_right( &ui_global_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_fill_rect_uv(...) ui_fill_rect_uv( &ui_global_ctx, __VA_ARGS__)
+#define gui_fill_rect(...) ui_fill_rect( &ui_global_ctx, __VA_ARGS__)
+#define gui_text(...) ui_text( &ui_global_ctx, __VA_ARGS__)
+#define gui_begin(...) ui_begin( &ui_global_ctx, __VA_ARGS__)
+#define gui_resolve(...) ui_resolve( &ui_global_ctx, __VA_ARGS__)
+#define gui_set_mouse(...) ui_set_mouse( &ui_global_ctx, __VA_ARGS__)
+#define gui_button(...) ui_button( &ui_global_ctx, __VA_ARGS__)
+#define gui_window(...) ui_window( &ui_global_ctx, __VA_ARGS__)
+#define gui_want_mouse() ui_want_mouse( &ui_global_ctx )
+#define gui_push_image(...) ui_push_image( &ui_global_ctx, __VA_ARGS__ )
+#define gui_scrollbar(...) ui_scrollbar( &ui_global_ctx, __VA_ARGS__)
+#define gui_override_colours(...) ui_override_colours( &ui_global_ctx, __VA_ARGS__)
+#define gui_reset_colours(...) ui_reset_colours( &ui_global_ctx )