From: hgn Date: Sun, 19 May 2024 15:23:21 +0000 (+0100) Subject: blubber X-Git-Url: https://harrygodden.com/git/?a=commitdiff_plain;h=eb40c8529ed0aea7acf47e66b347dfa97922cba1;p=vg.git blubber --- diff --git a/vg_console.c b/vg_console.c index 57a8134..1913757 100644 --- a/vg_console.c +++ b/vg_console.c @@ -600,7 +600,7 @@ void vg_console_draw(void) SDL_AtomicLock( &vg_log.print_sl ); int ptr = vg_log.log_line_current; - int const fh = ui_current_context()->font->sy, log_lines = 32; + int const fh = vg_ui.ctx->font->sy, log_lines = 32; int console_lines = VG_MIN( log_lines, vg_log.log_line_count ); ui_rect rect_log = { 0, 0, vg.window_x, log_lines*fh }, @@ -610,17 +610,18 @@ void vg_console_draw(void) /* * log */ - u32 bg_colour = (ui_colour( k_ui_bg )&0x00ffffff)|0x9f000000; + u32 bg_colour = (ui_colour( ctx, k_ui_bg )&0x00ffffff)|0x9f000000; - ui_fill( rect_log, bg_colour ); + ui_fill( ctx, rect_log, bg_colour ); rect_line[1] = rect_log[1]+rect_log[3]-fh; - for( int i=0; ifont->sx*vg_console.suggestion_pastepos; + rect_suggest[0] += 6 + ctx->font->sx*vg_console.suggestion_pastepos; rect_suggest[1] += rect_input[3]; - rect_suggest[2] = ui_current_context()->font->sx * vg_console.suggestion_maxlen; + rect_suggest[2] = ctx->font->sx * vg_console.suggestion_maxlen; rect_suggest[3] = vg_console.suggestion_count * fh; - ui_fill( rect_suggest, bg_colour ); + ui_fill( ctx, rect_suggest, bg_colour ); rect_suggest[3] = fh; - for( int i=0; itex_bg = vg.tex_missing; + vg_ui.tex_bg = vg.tex_missing; #endif /* internal */ @@ -200,13 +200,13 @@ static void _vg_process_events(void) if( event.type == SDL_KEYDOWN ) { if( vg_console.enabled && - (g_ui_ctx->focused_control_type != k_ui_control_modal) ) + (vg_ui.ctx.focused_control_type != k_ui_control_modal) ) { if( event.key.keysym.sym == SDLK_ESCAPE || event.key.keysym.scancode == SDL_SCANCODE_GRAVE ) { vg_console.enabled = 0; - ui_defocus_all(); + ui_defocus_all( &vg_ui.ctx ); } else if( (event.key.keysym.mod & KMOD_CTRL) && event.key.keysym.sym == SDLK_n ){ @@ -218,17 +218,18 @@ static void _vg_process_events(void) } else { - ui_proc_key( event.key.keysym ); + vg_ui_handle_sdl_key( &vg_ui.ctx, event.key.keysym ); } } - else{ + else + { if( event.key.keysym.scancode == SDL_SCANCODE_GRAVE ) { vg_console.enabled = 1; } else { - ui_proc_key( event.key.keysym ); + vg_ui_handle_sdl_key( &vg_ui.ctx, event.key.keysym ); } } } @@ -248,32 +249,39 @@ static void _vg_process_events(void) { vg_input_controller_event( &event ); } - else if( event.type == SDL_MOUSEMOTION ){ + else if( event.type == SDL_MOUSEMOTION ) + { vg.mouse_delta[0] += event.motion.xrel; vg.mouse_delta[1] += event.motion.yrel; } - else if( event.type == SDL_WINDOWEVENT ){ - if( event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED ){ + else if( event.type == SDL_WINDOWEVENT ) + { + if( event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED ) + { int w, h; SDL_GL_GetDrawableSize( vg.window, &w, &h ); - if( !w || !h ){ + if( !w || !h ) + { vg_warn( "Got a invalid framebuffer size: " "%dx%d... ignoring\n", w, h ); } - else{ + else + { vg.window_x = w; vg.window_y = h; vg_framebuffer_resize(w,h); } } - else if( event.window.event == SDL_WINDOWEVENT_CLOSE ){ + else if( event.window.event == SDL_WINDOWEVENT_CLOSE ) + { vg.window_should_close = 1; } } - else if( event.type == SDL_TEXTINPUT ){ - ui_proc_utf8( event.text.text ); + else if( event.type == SDL_TEXTINPUT ) + { + ui_proc_utf8( &vg_ui.ctx, event.text.text ); } } @@ -328,27 +336,27 @@ static void _vg_gameloop_render(void) /* ui */ vg.engine_stage = k_engine_stage_ui; { - ui_prerender( (ui_px[2]){ vg.window_x, vg.window_y }, - (ui_px[2]){ vg.mouse_pos[0], vg.mouse_pos[1] }, - vg.mouse_state ); - ui_set_screen( vg.window_x, vg.window_y ); + ui_prerender( &vg_ui.ctx ); + vg_ui_set_screen( vg.window_x, vg.window_y ); + ui_update_mouse( &vg_ui.ctx, + (ui_px[2]){ vg.mouse_pos[0], vg.mouse_pos[1] }, vg.mouse_state ); if( vg_console.enabled ) { - ui_ignore_input_frames( 10 ); + ui_ignore_input_frames( &vg_ui.ctx, 10 ); vg_gui(); - ui_ignore_input_frames( 0 ); - ui_capture_mouse( 1 ); - vg_console_draw(); + ui_ignore_input_frames( &vg_ui.ctx, 0 ); + ui_capture_mouse( &vg_ui.ctx, 1 ); + vg_console_draw( &vg_ui.ctx ); } - else vg_gui(); + else vg_gui( &vg_ui.ctx ); if( vg.settings_open ) - vg_settings_gui(); + vg_settings_gui( &vg_ui.ctx ); /* vg tools */ #ifndef VG_NO_AUDIO - audio_debug_ui( vg.pv ); + audio_debug_ui( &vg_ui.ctx, vg.pv ); #endif /* profiling */ @@ -356,6 +364,7 @@ static void _vg_gameloop_render(void) int frame_target = vg.display_refresh_rate; if( vg.fps_limit > 0 ) frame_target = vg.fps_limit; vg_profile_drawn( + &vg_ui.ctx, (struct vg_profile *[]){ &vg_prof_update,&vg_prof_render,&vg_prof_swap}, 3, (1.0f/(float)frame_target)*1000.0f, @@ -379,10 +388,11 @@ static void _vg_gameloop_render(void) vg.time_fixed_extrapolate, vg.time_frame_delta, vg.time_spinning ); - ui_text( (ui_rect){258,4,900,900},perf,1,0,k_ui_align_left); + ui_text( &vg_ui.ctx, + (ui_rect){258,4,900,900},perf,1,0,k_ui_align_left); } - ui_postrender( vg.time_frame_delta ); - ui_post_update(); + ui_postrender( &vg_ui.ctx, vg.time_frame_delta ); + vg_ui_post_update(); } } diff --git a/vg_ui/imgui.c b/vg_ui/imgui.c index 7cf9df8..7ad4c46 100644 --- a/vg_ui/imgui.c +++ b/vg_ui/imgui.c @@ -7,48 +7,34 @@ #include "vg_log.h" #include -ui_context *g_ui_ctx; - -//__attribute__((deprecated("message", "replacement"))) -void ui_bind_context( ui_context *context ) -{ - g_ui_ctx = context; -} - -//__attribute__((deprecated("message", "replacement"))) -ui_context *ui_current_context(void) -{ - return g_ui_ctx; -} - -void ui_init( ui_vert *verts_buf, u32 verts_max, +void ui_init( ui_context *ctx, + ui_vert *verts_buf, u32 verts_max, u16 *indices_buf, u32 indices_max ) { - g_ui_ctx->vertex_buffer = verts_buf; - g_ui_ctx->max_verts = verts_max; - g_ui_ctx->cur_vert = 0; - g_ui_ctx->vert_start = 0; - - g_ui_ctx->indice_buffer = indices_buf; - g_ui_ctx->max_indices = indices_max; - g_ui_ctx->cur_indice = 0; - g_ui_ctx->indice_start = 0; + ctx->vertex_buffer = verts_buf; + ctx->max_verts = verts_max; + ctx->cur_vert = 0; + ctx->vert_start = 0; + ctx->indice_buffer = indices_buf; + ctx->max_indices = indices_max; + ctx->cur_indice = 0; + ctx->indice_start = 0; if( !verts_buf || !indices_buf ) exit(0); } -ui_vert *ui_fill_rect( ui_rect rect, u32 colour, ui_px uv[4] ) +ui_vert *ui_fill_rect( ui_context *ctx, ui_rect rect, u32 colour, ui_px uv[4] ) { /* this if far from ideal but stops us from crashing */ - if( (g_ui_ctx->cur_vert + 4 > g_ui_ctx->max_verts) || - (g_ui_ctx->cur_indice + 6 > g_ui_ctx->max_indices)) + if( (ctx->cur_vert + 4 > ctx->max_verts) || + (ctx->cur_indice + 6 > ctx->max_indices)) { - return &g_ui_ctx->vertex_buffer[0]; + return &ctx->vertex_buffer[0]; } - ui_vert *vertices = &g_ui_ctx->vertex_buffer[ g_ui_ctx->cur_vert ]; - u16 *indices = &g_ui_ctx->indice_buffer[ g_ui_ctx->cur_indice ]; + ui_vert *vertices = &ctx->vertex_buffer[ ctx->cur_vert ]; + u16 *indices = &ctx->indice_buffer[ ctx->cur_indice ]; for( int i=0; i<4; i++ ) vertices[i].colour = colour; @@ -70,32 +56,33 @@ ui_vert *ui_fill_rect( ui_rect rect, u32 colour, ui_px uv[4] ) vertices[3].uv[0] = uv[0]; vertices[3].uv[1] = uv[3]; - u16 start = g_ui_ctx->cur_vert; + u16 start = ctx->cur_vert; u32 mesh[] = { 0,2,1, 0,3,2 }; for( u32 i=0; icur_indice += 6; - g_ui_ctx->cur_vert += 4; + ctx->cur_indice += 6; + ctx->cur_vert += 4; return vertices; } -ui_vert *ui_fill( ui_rect rect, u32 colour ) +ui_vert *ui_fill( ui_context *ctx, ui_rect rect, u32 colour ) { - return ui_fill_rect( rect, colour, (ui_px[4]){ 4,4,4,4 } ); + return ui_fill_rect( ctx, rect, colour, (ui_px[4]){ 4,4,4,4 } ); } -void ui_outline( ui_rect rect, ui_px thickness, u32 colour, u32 mask ) +void ui_outline( ui_context *ctx, + ui_rect rect, ui_px thickness, u32 colour, u32 mask ) { /* this if far from ideal but stops us from crashing */ - if( (g_ui_ctx->cur_vert + 8 > g_ui_ctx->max_verts) || - (g_ui_ctx->cur_indice + 24 > g_ui_ctx->max_indices)) + if( (ctx->cur_vert + 8 > ctx->max_verts) || + (ctx->cur_indice + 24 > ctx->max_indices)) return; - ui_vert *vertices = &g_ui_ctx->vertex_buffer[ g_ui_ctx->cur_vert ]; - u16 *indices = &g_ui_ctx->indice_buffer[ g_ui_ctx->cur_indice ]; + ui_vert *vertices = &ctx->vertex_buffer[ ctx->cur_vert ]; + u16 *indices = &ctx->indice_buffer[ ctx->cur_indice ]; for( int i=0; i<8; i++ ) { @@ -121,7 +108,7 @@ void ui_outline( ui_rect rect, ui_px thickness, u32 colour, u32 mask ) vertices[7].co[0] = vertices[3].co[0]-thickness; vertices[7].co[1] = vertices[3].co[1]+thickness; - u16 start = g_ui_ctx->cur_vert; + u16 start = ctx->cur_vert; u32 mesh[] = { 0,5,4, 0,1,5, 1,6,5, 1,2,6, 2,7,6, 2,3,7, 3,4,7, 3,0,4 }; if( !mask ) @@ -137,8 +124,8 @@ void ui_outline( ui_rect rect, ui_px thickness, u32 colour, u32 mask ) } } - g_ui_ctx->cur_indice += c; - g_ui_ctx->cur_vert += 8; + ctx->cur_indice += c; + ctx->cur_vert += 8; } void rect_copy( ui_rect a, ui_rect b ) @@ -176,14 +163,6 @@ void ui_split_ratio( ui_rect rect, enum ui_axis dir, float ratio, void ui_rect_pad( ui_rect rect, ui_px pad[2] ) { - ui_px tmp[2]; - if( !pad ) - { - tmp[0] = g_ui_ctx->padding; - tmp[1] = g_ui_ctx->padding; - pad = tmp; - } - rect[0] += pad[0]; rect[1] += pad[1]; rect[2] -= pad[0]*2; @@ -232,97 +211,102 @@ int ui_inside_rect( ui_rect rect, ui_px co[2] ) return 0; } -int ui_click_down( u32 mask ) +int ui_click_down( ui_context *ctx, u32 mask ) { - if( g_ui_ctx->ignore_input_frames ) return 0; - if( (g_ui_ctx->mouse_state[0] & mask) && - !(g_ui_ctx->mouse_state[1] & mask) ) + if( ctx->ignore_input_frames ) return 0; + if( (ctx->mouse_state[0] & mask) && + !(ctx->mouse_state[1] & mask) ) return 1; else return 0; } -int ui_clicking( u32 mask ) +int ui_clicking( ui_context *ctx, u32 mask ) { - if( g_ui_ctx->ignore_input_frames ) return 0; - return g_ui_ctx->mouse_state[0] & mask; + if( ctx->ignore_input_frames ) return 0; + return ctx->mouse_state[0] & mask; } -int ui_click_up( u32 mask ) +int ui_click_up( ui_context *ctx, u32 mask ) { - if( g_ui_ctx->ignore_input_frames ) return 0; - if( (g_ui_ctx->mouse_state[1] & mask) && - !(g_ui_ctx->mouse_state[0] & mask) ) + if( ctx->ignore_input_frames ) return 0; + if( (ctx->mouse_state[1] & mask) && + !(ctx->mouse_state[0] & mask) ) return 1; else return 0; } -void ui_prerender( ui_px dims[2], ui_px mouse[2], i32 mouse_state ) +void ui_update_mouse( ui_context *ctx, ui_px mouse[2], i32 mouse_state ) { - g_ui_ctx->mouse_state[1] = g_ui_ctx->mouse_state[0]; - g_ui_ctx->mouse_state[0] = mouse_state; - g_ui_ctx->mouse_delta[0] = mouse[0]-g_ui_ctx->mouse[0]; - g_ui_ctx->mouse_delta[1] = mouse[1]-g_ui_ctx->mouse[1]; - g_ui_ctx->area[0] = dims[0]; - g_ui_ctx->area[1] = dims[1]; + ctx->mouse_state[1] = ctx->mouse_state[0]; + ctx->mouse_state[0] = mouse_state; + ctx->mouse_delta[0] = mouse[0]-ctx->mouse[0]; + ctx->mouse_delta[1] = mouse[1]-ctx->mouse[1]; - if( !g_ui_ctx->mouse_pos_overriden ) + if( !ctx->mouse_pos_overriden ) { - g_ui_ctx->mouse[0] = mouse[0]; - g_ui_ctx->mouse[1] = mouse[1]; + ctx->mouse[0] = mouse[0]; + ctx->mouse[1] = mouse[1]; } - g_ui_ctx->cur_vert = 0; - g_ui_ctx->cur_indice = 0; - g_ui_ctx->vert_start = 0; - g_ui_ctx->indice_start = 0; - g_ui_ctx->focused_control_hit = 0; - g_ui_ctx->cursor = k_ui_cursor_default; - g_ui_ctx->wants_mouse = 0; - g_ui_ctx->mouse_pos_overriden = 0; - - if( g_ui_ctx->ignore_input_frames ) + if( ctx->ignore_input_frames ) { - g_ui_ctx->ignore_input_frames --; + ctx->ignore_input_frames --; return; } - if( ui_click_down(UI_MOUSE_LEFT)||ui_click_down(UI_MOUSE_MIDDLE)|| - ui_click_down(UI_MOUSE_RIGHT) ) + if( ui_click_down(ctx, UI_MOUSE_LEFT)|| + ui_click_down(ctx, UI_MOUSE_MIDDLE)|| + ui_click_down(ctx, UI_MOUSE_RIGHT) ) { - g_ui_ctx->mouse_click[0] = g_ui_ctx->mouse[0]; - g_ui_ctx->mouse_click[1] = g_ui_ctx->mouse[1]; + ctx->mouse_click[0] = ctx->mouse[0]; + ctx->mouse_click[1] = ctx->mouse[1]; } } -void ui_ignore_input_frames( u32 frames ) +void ui_prerender( ui_context *ctx ) { - g_ui_ctx->ignore_input_frames = frames; + ctx->cur_vert = 0; + ctx->cur_indice = 0; + ctx->vert_start = 0; + ctx->indice_start = 0; + ctx->focused_control_hit = 0; + ctx->cursor = k_ui_cursor_default; + ctx->wants_mouse = 0; + ctx->mouse_pos_overriden = 0; } -void ui_capture_mouse( bool on ) +void ui_set_area( ui_context *ctx, i32 width, i32 height ) { - g_ui_ctx->wants_mouse = on; + ctx->area[0] = width; + ctx->area[1] = height; } -void ui_impl_render_batch( ui_batch *batch ); +void ui_ignore_input_frames( ui_context *ctx, u32 frames ) +{ + ctx->ignore_input_frames = frames; +} + +void ui_capture_mouse( ui_context *ctx, bool on ) +{ + ctx->wants_mouse = on; +} -void ui_flush( enum ui_shader shader ) +void ui_flush( ui_context *ctx, enum ui_shader shader, void *shader_data ) { ui_batch batch; - batch.shader = shader; - batch.vert_offset = g_ui_ctx->vert_start * sizeof(ui_vert); - batch.indice_offset = g_ui_ctx->indice_start * sizeof(u16); - batch.vert_buf = g_ui_ctx->vertex_buffer + g_ui_ctx->vert_start; - batch.vert_count = g_ui_ctx->cur_vert - g_ui_ctx->vert_start; - batch.indice_buf = g_ui_ctx->indice_buffer + g_ui_ctx->indice_start; - batch.indice_count = g_ui_ctx->cur_indice - g_ui_ctx->indice_start; + batch.vert_offset = ctx->vert_start * sizeof(ui_vert); + batch.indice_offset = ctx->indice_start * sizeof(u16); + batch.vert_buf = ctx->vertex_buffer + ctx->vert_start; + batch.vert_count = ctx->cur_vert - ctx->vert_start; + batch.indice_buf = ctx->indice_buffer + ctx->indice_start; + batch.indice_count = ctx->cur_indice - ctx->indice_start; - ui_impl_render_batch( &batch ); + ctx->render_batch( ctx, &batch, shader, shader_data ); - g_ui_ctx->indice_start = g_ui_ctx->cur_indice; - g_ui_ctx->vert_start = g_ui_ctx->cur_vert; + ctx->indice_start = ctx->cur_indice; + ctx->vert_start = ctx->cur_vert; } void ui_rect_center( ui_rect parent, ui_rect rect ) @@ -347,7 +331,7 @@ void ui_fit_item( ui_rect rect, ui_px size[2], ui_rect d ) ui_rect_center( rect, d ); } -ui_px ui_text_line_width( const char *str ) +ui_px ui_text_line_width( ui_context *ctx, const char *str ) { int length = 0; const char *_c = str; @@ -358,10 +342,10 @@ ui_px ui_text_line_width( const char *str ) else if( c == '\n' ) break; } - return length * g_ui_ctx->font->sx; + return length * ctx->font->sx; } -ui_px ui_text_string_height( const char *str ) +ui_px ui_text_string_height( ui_context *ctx, const char *str ) { int height = 1; const char *_c = str; @@ -372,10 +356,11 @@ ui_px ui_text_string_height( const char *str ) if( c == '\n' ) height ++; } - return height * g_ui_ctx->font->sy; + return height * ctx->font->sy; } -ui_px ui_text_aligned_x( const char *str, ui_rect rect, ui_px scale, +ui_px ui_text_aligned_x( ui_context *ctx, + const char *str, ui_rect rect, ui_px scale, enum ui_align align ) { enum ui_align lwr = k_ui_align_lwr & align; @@ -383,7 +368,7 @@ ui_px ui_text_aligned_x( const char *str, ui_rect rect, ui_px scale, return rect[0]; } else{ - ui_px width = ui_text_line_width( str ) * scale; + ui_px width = ui_text_line_width( ctx, str ) * scale; if( lwr == k_ui_align_right ) return rect[0] + rect[2]-width; @@ -392,19 +377,19 @@ ui_px ui_text_aligned_x( const char *str, ui_rect rect, ui_px scale, } } -u32 ui_colour( enum ui_scheme_colour id ) +u32 ui_colour( ui_context *ctx, enum ui_scheme_colour id ) { - return g_ui_ctx->scheme[ id ]; + return ctx->scheme[ id ]; } /* get an appropriately contrasting colour given the base */ -u32 ui_colourcont( enum ui_scheme_colour id ) +u32 ui_colourcont( ui_context *ctx, enum ui_scheme_colour id ) { - if ( id < k_ui_bg+6 ) return ui_colour( k_ui_fg ); - else if( id < k_ui_fg ) return ui_colour( k_ui_bg+1 ); - else if( id < k_ui_hue ) return ui_colour( k_ui_bg+3 ); - else if( id < k_ui_red+k_ui_brighter ) return ui_colour( k_ui_fg ); - else return ui_colour( k_ui_fg+1 ); + if ( id < k_ui_bg+6 ) return ui_colour(ctx, k_ui_fg ); + else if( id < k_ui_fg ) return ui_colour(ctx, k_ui_bg+1 ); + else if( id < k_ui_hue ) return ui_colour(ctx, k_ui_bg+3 ); + else if( id < k_ui_red+k_ui_brighter ) return ui_colour(ctx, k_ui_fg ); + else return ui_colour(ctx, k_ui_fg+1 ); } void ui_hex_to_norm( u32 hex, v4f norm ) @@ -443,29 +428,30 @@ u32 ui_opacity( u32 colour, f32 opacity ) return (colour & 0x00ffffff) | (alpha << 24); } -u32 ui_ntext( ui_rect rect, const char *str, u32 len, ui_px scale, +u32 ui_ntext( ui_context *ctx, + ui_rect rect, const char *str, u32 len, ui_px scale, enum ui_align align, u32 colour ) { ui_px glow_text = 0; ui_rect text_cursor; - if( colour == 0 ) colour = ui_colour( k_ui_fg ); + if( colour == 0 ) colour = ui_colour(ctx, k_ui_fg ); colour &= 0x00ffffff; const char *_c = str; u8 c; - text_cursor[0] = ui_text_aligned_x( str, rect, scale, align ); + text_cursor[0] = ui_text_aligned_x( ctx, str, rect, scale, align ); text_cursor[1] = rect[1]; - text_cursor[2] = g_ui_ctx->font->cw*scale; - text_cursor[3] = g_ui_ctx->font->ch*scale; + text_cursor[2] = ctx->font->cw*scale; + text_cursor[3] = ctx->font->ch*scale; u32 printed_chars = 0; if( align & (k_ui_align_middle|k_ui_align_bottom) ) { - ui_px height = ui_text_string_height( str ) * scale; + ui_px height = ui_text_string_height( ctx, str ) * scale; if( align & k_ui_align_bottom ) text_cursor[1] += rect[3]-height; @@ -478,27 +464,27 @@ u32 ui_ntext( ui_rect rect, const char *str, u32 len, ui_px scale, if( printed_chars >= len ) { printed_chars = 0; - text_cursor[1] += g_ui_ctx->font->sy*scale; - text_cursor[0] = ui_text_aligned_x( _c, rect, scale, align ); - text_cursor[0] -= g_ui_ctx->font->sx*scale; + text_cursor[1] += ctx->font->sy*scale; + text_cursor[0] = ui_text_aligned_x( ctx, _c, rect, scale, align ); + text_cursor[0] -= ctx->font->sx*scale; ui_rect glyph; - ui_text_glyph( g_ui_ctx->font, '\xb1' /*FIXME*/, glyph ); - ui_fill_rect( text_cursor, 0x00ffffff, glyph ); - text_cursor[0] += g_ui_ctx->font->sx*scale; + ui_text_glyph( ctx->font, '\xb1' /*FIXME*/, glyph ); + ui_fill_rect( ctx, text_cursor, 0x00ffffff, glyph ); + text_cursor[0] += ctx->font->sx*scale; } if( c == '\n' ) { - text_cursor[1] += g_ui_ctx->font->sy*scale; - text_cursor[0] = ui_text_aligned_x( _c, rect, scale, align ); + text_cursor[1] += ctx->font->sy*scale; + text_cursor[0] = ui_text_aligned_x( ctx, _c, rect, scale, align ); printed_chars = 0; continue; } else if( c >= 33 ) { ui_rect glyph; - ui_text_glyph( g_ui_ctx->font, c, glyph ); + ui_text_glyph( ctx->font, c, glyph ); ui_rect cursor_clipped; if( ui_clip( rect, text_cursor, cursor_clipped ) ) @@ -506,11 +492,11 @@ u32 ui_ntext( ui_rect rect, const char *str, u32 len, ui_px scale, if( glow_text ) { cursor_clipped[1] += glow_text; - ui_fill_rect( cursor_clipped, 0x00ffffff, glyph ); + ui_fill_rect( ctx, cursor_clipped, 0x00ffffff, glyph ); cursor_clipped[1] -= glow_text; } - ui_fill_rect( cursor_clipped, colour, glyph ); + ui_fill_rect( ctx, cursor_clipped, colour, glyph ); } } else if( c == '\x1B' ) @@ -527,14 +513,14 @@ u32 ui_ntext( ui_rect rect, const char *str, u32 len, ui_px scale, _c = _c + i + 1; switch( colour_id ){ - case '0': colour = ui_colour( k_ui_fg ); break; - case '3'|'0'<<8: colour = ui_colour( k_ui_bg ); break; - case '3'|'1'<<8: colour = ui_colour( k_ui_red ); break; - case '3'|'2'<<8: colour = ui_colour( k_ui_green ); break; - case '3'|'3'<<8: colour = ui_colour( k_ui_yellow ); break; - case '3'|'4'<<8: colour = ui_colour( k_ui_blue ); break; - case '3'|'5'<<8: colour = ui_colour( k_ui_purple ); break; - case '3'|'6'<<8: colour = ui_colour( k_ui_aqua ); break; + case '0': colour = ui_colour(ctx, k_ui_fg ); break; + case '3'|'0'<<8: colour = ui_colour(ctx, k_ui_bg ); break; + case '3'|'1'<<8: colour = ui_colour(ctx, k_ui_red ); break; + case '3'|'2'<<8: colour = ui_colour(ctx, k_ui_green ); break; + case '3'|'3'<<8: colour = ui_colour(ctx, k_ui_yellow ); break; + case '3'|'4'<<8: colour = ui_colour(ctx, k_ui_blue ); break; + case '3'|'5'<<8: colour = ui_colour(ctx, k_ui_purple ); break; + case '3'|'6'<<8: colour = ui_colour(ctx, k_ui_aqua ); break; case '3'|'7'<<8: colour = 0xffffffff; break; } @@ -561,32 +547,23 @@ u32 ui_ntext( ui_rect rect, const char *str, u32 len, ui_px scale, } else if( c == '\t' ) { - text_cursor[0] += g_ui_ctx->font->sx*scale*4; + text_cursor[0] += ctx->font->sx*scale*4; printed_chars += 4; continue; } - text_cursor[0] += g_ui_ctx->font->sx*scale; + text_cursor[0] += ctx->font->sx*scale; printed_chars ++; } return printed_chars; } -u32 ui_text( ui_rect rect, const char *str, ui_px scale, +u32 ui_text( ui_context *ctx, + ui_rect rect, const char *str, ui_px scale, enum ui_align align, u32 colour ) { - return ui_ntext( rect, str, 1024, scale, align, colour ); -} - -void ui_font_face( vg_font_face *ff ) -{ - g_ui_ctx->font = ff; -} - -const vg_font_face *ui_current_font(void) -{ - return g_ui_ctx->font; + return ui_ntext( ctx, rect, str, 1024, scale, align, colour ); } /* @@ -594,76 +571,75 @@ const vg_font_face *ui_current_font(void) * ----------------------------------------------------------------------------- */ -void ui_panel( ui_rect in_rect, ui_rect out_panel ) +void ui_panel( ui_context *ctx, ui_rect in_rect, ui_rect out_panel ) { - ui_fill( in_rect, ui_colour( k_ui_bg+1 ) ); - ui_outline( in_rect, 1, ui_colour( k_ui_bg+7 ), 0 ); + ui_fill( ctx, in_rect, ui_colour(ctx, k_ui_bg+1 ) ); + ui_outline( ctx, in_rect, 1, ui_colour(ctx, k_ui_bg+7 ), 0 ); rect_copy( in_rect, out_panel ); ui_rect_pad( out_panel, NULL ); } -void ui_label( ui_rect rect, const char *text, ui_px size, +void ui_label( ui_context *ctx, + ui_rect rect, const char *text, ui_px size, ui_px gap, ui_rect r ) { ui_rect l; - ui_px width = (ui_text_line_width(text)+g_ui_ctx->font->sx) * size; + ui_px width = (ui_text_line_width(ctx,text)+ctx->font->sx) * size; ui_split( rect, k_ui_axis_v, width, gap, l, r ); - ui_text( l, text, 1, k_ui_align_middle_left, 0 ); + ui_text( ctx, l, text, 1, k_ui_align_middle_left, 0 ); } -ui_px ui_standard_widget_height( ui_px count ) +ui_px ui_standard_widget_height( ui_context *ctx, ui_px count ) { - return (count * g_ui_ctx->font->sy + 18) * g_ui_ctx->scale; + return (count * ctx->font->sy + 18) * ctx->scale; } -void ui_standard_widget( ui_rect inout_panel, ui_rect out_rect, ui_px count ) +void ui_standard_widget( ui_context *ctx, + ui_rect inout_panel, ui_rect out_rect, ui_px count ) { - ui_px height = ui_standard_widget_height( count ); - ui_split( inout_panel, k_ui_axis_h, height, g_ui_ctx->padding, + ui_px height = ui_standard_widget_height( ctx, count ); + ui_split( inout_panel, k_ui_axis_h, height, ctx->padding, out_rect, inout_panel ); } -void ui_info( ui_rect inout_panel, const char *text ) +void ui_info( ui_context *ctx, ui_rect inout_panel, const char *text ) { ui_rect box; - ui_standard_widget( inout_panel, box, 1 ); - ui_text( box, text, 1, k_ui_align_middle_left, 0 ); + ui_standard_widget( ctx, inout_panel, box, 1 ); + ui_text( ctx, box, text, 1, k_ui_align_middle_left, 0 ); } -void ui_image( ui_rect rect, GLuint image ) +void ui_image( ui_context *ctx, ui_rect rect, void *resource ) { - ui_flush( k_ui_shader_colour ); - glActiveTexture( GL_TEXTURE0 ); - glBindTexture( GL_TEXTURE_2D, image ); - ui_fill_rect( rect, 0xffffffff, (ui_px[4]){ 0,256,256,0 } ); - ui_flush( k_ui_shader_image ); + ui_flush( ctx, k_ui_shader_colour, NULL ); + ui_fill_rect( ctx, rect, 0xffffffff, (ui_px[4]){ 0,256,256,0 } ); + + struct ui_batch_shader_data_image inf = { .resource = resource }; + ui_flush( ctx, k_ui_shader_image, &inf ); } -void ui_defocus_all(void) +void ui_defocus_all( ui_context *ctx ) { - if( g_ui_ctx->focused_control_type == k_ui_control_textbox ) + if( ctx->focused_control_type == k_ui_control_textbox ) { -#if VG_ANDROID -#else - SDL_StopTextInput(); -#endif - if( g_ui_ctx->textbox.callbacks.escape ) - g_ui_ctx->textbox.callbacks.escape(); + ctx->stop_text_input(); + if( ctx->textbox.callbacks.escape ) + ctx->textbox.callbacks.escape(); } - g_ui_ctx->focused_control_id = NULL; - g_ui_ctx->focused_control_hit = 0; - g_ui_ctx->focused_control_type = k_ui_control_none; + ctx->focused_control_id = NULL; + ctx->focused_control_hit = 0; + ctx->focused_control_type = k_ui_control_none; } -enum ui_button_state ui_button_base( ui_rect rect ) +enum ui_button_state ui_button_base( ui_context *ctx, ui_rect rect ) { - int clickup= ui_click_up(UI_MOUSE_LEFT), - click = ui_clicking(UI_MOUSE_LEFT) | clickup, - target = ui_inside_rect( rect, g_ui_ctx->mouse_click ) && click, - hover = ui_inside_rect( rect, g_ui_ctx->mouse ); + int clickup= ui_click_up(ctx, UI_MOUSE_LEFT), + click = ui_clicking(ctx, UI_MOUSE_LEFT) | clickup, + target = ui_inside_rect( rect, ctx->mouse_click ) && click, + hover = ui_inside_rect( rect, ctx->mouse ); - if( g_ui_ctx->focused_control_type != k_ui_control_none ) + if( ctx->focused_control_type != k_ui_control_none ) { clickup = 0; click = 0; @@ -672,7 +648,7 @@ enum ui_button_state ui_button_base( ui_rect rect ) } if( hover ) - g_ui_ctx->cursor = k_ui_cursor_hand; + ctx->cursor = k_ui_cursor_hand; if( click ) { @@ -682,8 +658,8 @@ enum ui_button_state ui_button_base( ui_rect rect ) { if( clickup ) { - g_ui_ctx->ignore_input_frames = 2; - ui_defocus_all(); + ui_ignore_input_frames( ctx, 2 ); + ui_defocus_all( ctx ); return k_ui_button_click; } else return k_ui_button_holding_inside; @@ -700,87 +676,89 @@ enum ui_button_state ui_button_base( ui_rect rect ) } /* TODO: split this out into a formatless button and one that auto fills */ -enum ui_button_state ui_colourbutton( ui_rect rect, +enum ui_button_state ui_colourbutton( ui_context *ctx, ui_rect rect, enum ui_scheme_colour colour, enum ui_scheme_colour hover_colour, enum ui_scheme_colour hi_colour ) { - enum ui_button_state state = ui_button_base( rect ); + enum ui_button_state state = ui_button_base( ctx, rect ); - u32 col_base = g_ui_ctx->scheme[ colour ], - col_highlight = g_ui_ctx->scheme[ hi_colour? hi_colour: k_ui_fg ], - col_hover = g_ui_ctx->scheme[ hover_colour? hover_colour: + u32 col_base = ctx->scheme[ colour ], + col_highlight = ctx->scheme[ hi_colour? hi_colour: k_ui_fg ], + col_hover = ctx->scheme[ hover_colour? hover_colour: colour + k_ui_brighter ]; if( state == k_ui_button_click ) { - ui_fill( rect, col_highlight ); - rect_copy( rect, g_ui_ctx->click_fader ); - rect_copy( rect, g_ui_ctx->click_fader_end ); - g_ui_ctx->click_fader_end[3] = 0; - ui_rect_center( rect, g_ui_ctx->click_fader_end ); - g_ui_ctx->click_fade_opacity = 1.0f; + ui_fill( ctx, rect, col_highlight ); + rect_copy( rect, ctx->click_fader ); + rect_copy( rect, ctx->click_fader_end ); + ctx->click_fader_end[3] = 0; + ui_rect_center( rect, ctx->click_fader_end ); + ctx->click_fade_opacity = 1.0f; } else if( state == k_ui_button_holding_inside ) { - ui_fill( rect, col_highlight ); + ui_fill( ctx, rect, col_highlight ); } else if( state == k_ui_button_holding_outside ) { - ui_fill( rect, col_base ); - ui_outline( rect, 1, col_highlight, 0 ); + ui_fill( ctx, rect, col_base ); + ui_outline( ctx, rect, 1, col_highlight, 0 ); } else if( state == k_ui_button_hover ) { - ui_fill( rect, col_hover ); + ui_fill( ctx, rect, col_hover ); } - else ui_fill( rect, col_base ); + else ui_fill( ctx, rect, col_base ); return state; } enum ui_button_state ui_colourbutton_text( + ui_context *ctx, ui_rect rect, const char *string, ui_px scale, enum ui_scheme_colour colour ) { - enum ui_button_state state = ui_colourbutton( rect, colour, 0, 0 ); + enum ui_button_state state = ui_colourbutton( ctx, rect, colour, 0, 0 ); - u32 text_colour = ui_colourcont(colour); + u32 text_colour = ui_colourcont( ctx, colour ); if( state == k_ui_button_holding_inside ) text_colour = colour; - ui_text( rect, string, scale, k_ui_align_middle_center, text_colour ); + ui_text( ctx, rect, string, scale, k_ui_align_middle_center, text_colour ); return state; } -enum ui_button_state ui_button_text( ui_rect rect, +enum ui_button_state ui_button_text( ui_context *ctx, ui_rect rect, const char *string, ui_px scale ) { - return ui_colourbutton_text( rect, string, scale, k_ui_bg+4 ); + return ui_colourbutton_text( ctx, rect, string, scale, k_ui_bg+4 ); } -enum ui_button_state ui_button( ui_rect inout_panel, const char *string ) +enum ui_button_state ui_button( ui_context *ctx, + ui_rect inout_panel, const char *string ) { ui_rect rect; - ui_standard_widget( inout_panel, rect, 1 ); - return ui_colourbutton_text( rect, string, 1, k_ui_bg+4 ); + ui_standard_widget( ctx, inout_panel, rect, 1 ); + return ui_colourbutton_text( ctx, rect, string, 1, k_ui_bg+4 ); } -static void ui_enum_post(void); -void ui_postrender( f32 delta_time ) +static void ui_enum_post( ui_context *ctx ); +void ui_postrender( ui_context *ctx, f32 delta_time ) { - if( g_ui_ctx->click_fade_opacity > 0.0f ) + if( ctx->click_fade_opacity > 0.0f ) { - float scale = g_ui_ctx->click_fade_opacity; + float scale = ctx->click_fade_opacity; scale = vg_maxf( 1.0f/255.0f, scale*scale ); - g_ui_ctx->click_fade_opacity -= delta_time * 3.8f; - u32 colour = (0x00ffffff & ui_colour(k_ui_fg))|0x7f000000; + ctx->click_fade_opacity -= delta_time * 3.8f; + u32 colour = (0x00ffffff & ui_colour(ctx,k_ui_fg))|0x7f000000; v4f begin, end, dest; for( int i=0; i<4; i++ ){ - begin[i] = g_ui_ctx->click_fader[i]; - end[i] = g_ui_ctx->click_fader_end[i]+1; + begin[i] = ctx->click_fader[i]; + end[i] = ctx->click_fader_end[i]+1; } v4_lerp( end, begin, scale, dest ); @@ -790,26 +768,28 @@ void ui_postrender( f32 delta_time ) rect[i] = dest[i]; } - ui_fill( rect, colour ); + ui_fill( ctx, rect, colour ); } - if( g_ui_ctx->focused_control_type == k_ui_control_enum ){ - ui_enum_post(); + if( ctx->focused_control_type == k_ui_control_enum ) + { + ui_enum_post( ctx ); } - else if( g_ui_ctx->focused_control_type == k_ui_control_modal ){ - ui_rect screen = { 0,0, g_ui_ctx->area[0], g_ui_ctx->area[1] }; - ui_fill( screen, 0xa0000000 ); + else if( ctx->focused_control_type == k_ui_control_modal ) + { + ui_rect screen = { 0,0, ctx->area[0], ctx->area[1] }; + ui_fill( ctx, screen, 0xa0000000 ); ui_rect box = {0,0,400,200}; - u32 colour = ui_colour(k_ui_fg), - type = g_ui_ctx->modal.options & UI_MODAL_TYPE_BITS; - if ( type == 1 ) colour = ui_colour(k_ui_green); - else if( type == 2 ) colour = ui_colour(k_ui_red); - else if( type == 3 ) colour = ui_colour(k_ui_yellow); + u32 colour = ui_colour(ctx,k_ui_fg), + type = ctx->modal.options & UI_MODAL_TYPE_BITS; + if ( type == 1 ) colour = ui_colour(ctx,k_ui_green); + else if( type == 2 ) colour = ui_colour(ctx,k_ui_red); + else if( type == 3 ) colour = ui_colour(ctx,k_ui_yellow); ui_rect_center( screen, box ); - ui_fill( box, ui_colour(k_ui_bg) ); - ui_outline( box, -1, colour, 0 ); + ui_fill( ctx, box, ui_colour(ctx,k_ui_bg) ); + ui_outline( ctx, box, -1, colour, 0 ); ui_rect message; rect_copy( box, message ); @@ -818,8 +798,8 @@ void ui_postrender( f32 delta_time ) ui_rect row0, row1, btn; ui_split_ratio( message, k_ui_axis_h, 0.5f, 0, row0, row1 ); - row0[0] += g_ui_ctx->font->sx; - ui_ntext( row0, g_ui_ctx->modal.message, (box[2]/g_ui_ctx->font->sx)-2, 1, + row0[0] += ctx->font->sx; + ui_ntext( ctx, row0, ctx->modal.message, (box[2]/ctx->font->sx)-2, 1, k_ui_align_left, colour ); rect_copy( row1, btn ); @@ -827,18 +807,18 @@ void ui_postrender( f32 delta_time ) btn[3] = 28; ui_rect_center( row1, btn ); - g_ui_ctx->focused_control_type = k_ui_control_none; /* HACK */ - if( ui_button_text( btn, "OK", 1 ) != 1 ) - g_ui_ctx->focused_control_hit = 1; - g_ui_ctx->focused_control_type = k_ui_control_modal; /* HACK */ - g_ui_ctx->wants_mouse = 1; + ctx->focused_control_type = k_ui_control_none; /* HACK */ + if( ui_button_text( ctx, btn, "OK", 1 ) != 1 ) + ctx->focused_control_hit = 1; + ctx->focused_control_type = k_ui_control_modal; /* HACK */ + ctx->wants_mouse = 1; } - ui_flush( k_ui_shader_colour ); + ui_flush( ctx, k_ui_shader_colour, NULL ); - if( !g_ui_ctx->focused_control_hit ) + if( !ctx->focused_control_hit ) { - ui_defocus_all(); + ui_defocus_all( ctx ); } } @@ -847,43 +827,44 @@ void ui_postrender( f32 delta_time ) * ----------------------------------------------------------------------------- */ -enum ui_button_state ui_checkbox_base( ui_rect box, i32 *data ) +enum ui_button_state ui_checkbox_base( ui_context *ctx, ui_rect box, i32 *data ) { - enum ui_button_state state = ui_button_base( box ); + enum ui_button_state state = ui_button_base( ctx, box ); if( state == k_ui_button_click ) *data = (*data) ^ 0x1; return state; } -int ui_checkbox( ui_rect inout_panel, const char *str_label, i32 *data ) +int ui_checkbox( ui_context *ctx, + ui_rect inout_panel, const char *str_label, i32 *data ) { ui_rect rect, label, box; - ui_standard_widget( inout_panel, rect, 1 ); + ui_standard_widget( ctx, inout_panel, rect, 1 ); ui_split( rect, k_ui_axis_v, -rect[3], 0, label, box ); - ui_text( label, str_label, g_ui_ctx->scale, k_ui_align_middle_left, 0 ); + ui_text( ctx, label, str_label, ctx->scale, k_ui_align_middle_left, 0 ); - enum ui_button_state state = ui_checkbox_base( box, data ); + enum ui_button_state state = ui_checkbox_base( ctx, box, data ); if( state == k_ui_button_holding_inside ) { - ui_fill( box, ui_colour(k_ui_bg+2) ); - ui_outline( box, 1, ui_colour(k_ui_fg), 0 ); + ui_fill( ctx, box, ui_colour(ctx,k_ui_bg+2) ); + ui_outline( ctx, box, 1, ui_colour(ctx,k_ui_fg), 0 ); } else if( state == k_ui_button_holding_outside ) { - ui_fill( box, ui_colour(k_ui_bg) ); - ui_outline( box, 1, ui_colour(k_ui_fg), 0 ); + ui_fill( ctx, box, ui_colour(ctx,k_ui_bg) ); + ui_outline( ctx, box, 1, ui_colour(ctx,k_ui_fg), 0 ); } else if( state == k_ui_button_hover ) { - ui_fill( box, ui_colour(k_ui_bg) ); - ui_outline( box, 1, ui_colour(k_ui_fg), 0 ); + ui_fill( ctx, box, ui_colour(ctx,k_ui_bg) ); + ui_outline( ctx, box, 1, ui_colour(ctx,k_ui_fg), 0 ); } else { - ui_fill( box, ui_colour(k_ui_bg) ); - ui_outline( box, 1, ui_colour(k_ui_bg+4), 0 ); + ui_fill( ctx, box, ui_colour(ctx,k_ui_bg) ); + ui_outline( ctx, box, 1, ui_colour(ctx,k_ui_bg+4), 0 ); } bool changed = (state == k_ui_button_click); @@ -891,7 +872,7 @@ int ui_checkbox( ui_rect inout_panel, const char *str_label, i32 *data ) if( *data ) { ui_rect_pad( box, (ui_px[2]){4,4} ); - ui_fill( box, ui_colour( k_ui_orange ) ); + ui_fill( ctx, box, ui_colour(ctx, k_ui_orange ) ); } return changed; @@ -906,72 +887,76 @@ int ui_checkbox( ui_rect inout_panel, const char *str_label, i32 *data ) * unfortunately no return value since we only find out that event in the * postrender step. */ -void ui_enum( ui_rect inout_panel, const char *str_label, +void ui_enum( ui_context *ctx, ui_rect inout_panel, const char *str_label, struct ui_enum_opt *options, u32 len, i32 *value ) { ui_rect rect, label, box; - ui_standard_widget( inout_panel, rect, 1 ); - ui_label( rect, str_label, g_ui_ctx->scale, 0, box ); + ui_standard_widget( ctx, inout_panel, rect, 1 ); + ui_label( ctx, rect, str_label, ctx->scale, 0, box ); const char *display = "OUT OF RANGE"; int valid = 0; - for( u32 i=0; iscale ) == 1 ){ - g_ui_ctx->focused_control_type = k_ui_control_enum; - g_ui_ctx->ptr_enum = value; - g_ui_ctx->_enum.option_count = len; - g_ui_ctx->_enum.options = options; - rect_copy( box, g_ui_ctx->_enum.rect ); + if( ui_button_text( ctx, box, display, ctx->scale ) == 1 ) + { + ctx->focused_control_type = k_ui_control_enum; + ctx->ptr_enum = value; + ctx->_enum.option_count = len; + ctx->_enum.options = options; + rect_copy( box, ctx->_enum.rect ); } if( !valid ) - ui_outline( box, 1, ui_colour(k_ui_red), 0 ); + ui_outline( ctx, box, 1, ui_colour(ctx,k_ui_red), 0 ); } -static void ui_enum_post(void){ +static void ui_enum_post( ui_context *ctx ) +{ ui_rect drawer; - rect_copy( g_ui_ctx->_enum.rect, drawer ); - drawer[3] *= g_ui_ctx->_enum.option_count; + rect_copy( ctx->_enum.rect, drawer ); + drawer[3] *= ctx->_enum.option_count; int close = 0; - int clickany= ui_click_up(UI_MOUSE_LEFT|UI_MOUSE_RIGHT|UI_MOUSE_MIDDLE), - hover = ui_inside_rect( drawer, g_ui_ctx->mouse ); + int clickany= ui_click_up(ctx, UI_MOUSE_LEFT|UI_MOUSE_RIGHT|UI_MOUSE_MIDDLE), + hover = ui_inside_rect( drawer, ctx->mouse ); if( clickany && !hover ){ return; } /* HACK */ - g_ui_ctx->focused_control_type = k_ui_control_none; - i32 *value = g_ui_ctx->ptr_enum; + ctx->focused_control_type = k_ui_control_none; + i32 *value = ctx->ptr_enum; - for( u32 i=0; i_enum.option_count; i++ ){ + for( u32 i=0; i_enum.option_count; i++ ){ ui_rect button; - ui_split( drawer, k_ui_axis_h, g_ui_ctx->_enum.rect[3], 0, button,drawer ); + ui_split( drawer, k_ui_axis_h, ctx->_enum.rect[3], 0, button,drawer ); enum ui_scheme_colour colour = k_ui_bg+3; - if( g_ui_ctx->_enum.options[i].value == *value ) + if( ctx->_enum.options[i].value == *value ) colour = k_ui_orange; - if( ui_colourbutton_text( button, g_ui_ctx->_enum.options[i].alias, - g_ui_ctx->scale, colour ) == 1 ){ - *value = g_ui_ctx->_enum.options[i].value; + if( ui_colourbutton_text( ctx, button, ctx->_enum.options[i].alias, + ctx->scale, colour ) == 1 ){ + *value = ctx->_enum.options[i].value; close = 1; } } /* HACK */ - g_ui_ctx->focused_control_type = k_ui_control_enum; + ctx->focused_control_type = k_ui_control_enum; if( !close ) - g_ui_ctx->focused_control_hit = 1; + ctx->focused_control_hit = 1; } /* @@ -979,19 +964,19 @@ static void ui_enum_post(void){ * ----------------------------------------------------------------------------- */ -enum ui_button_state ui_slider_base( +enum ui_button_state ui_slider_base( ui_context *ctx, ui_rect box, f32 min, f32 max, f32 *value, f32 *out_t ) { enum ui_button_state mask_using = k_ui_button_holding_inside | k_ui_button_holding_outside | k_ui_button_click, - state = ui_button_base( box ); + state = ui_button_base( ctx, box ); f32 t; if( state & mask_using ) { - t = vg_clampf( (f32)(g_ui_ctx->mouse[0] - box[0]) / (f32)( box[2] ), 0,1 ); + t = vg_clampf( (f32)(ctx->mouse[0] - box[0]) / (f32)( box[2] ), 0,1 ); *value = vg_lerpf( min, max, t ); } else @@ -1002,15 +987,17 @@ enum ui_button_state ui_slider_base( return state; } -void ui_slider_text( ui_rect box, const char *format, f32 value ) +void ui_slider_text( ui_context *ctx, + ui_rect box, const char *format, f32 value ) { /* TODO: replace this one day with our own function */ char buf[32]; snprintf( buf, sizeof(buf), format? format: "%.2f", value ); - ui_text( box, buf, 1, k_ui_align_middle_center, 0 ); + ui_text( ctx, box, buf, 1, k_ui_align_middle_center, 0 ); } -bool ui_slider_standard( ui_rect box, f32 min, f32 max, f32 *value, +bool ui_slider_standard( ui_context *ctx, + ui_rect box, f32 min, f32 max, f32 *value, const char *format ) { f32 t; @@ -1020,26 +1007,27 @@ bool ui_slider_standard( ui_rect box, f32 min, f32 max, f32 *value, k_ui_button_holding_outside | k_ui_button_click, mask_brighter = mask_using | k_ui_button_hover, - state = ui_slider_base( box, min, max, value, &t ); + state = ui_slider_base( ctx, box, min, max, value, &t ); ui_rect line = { box[0], box[1], t * (f32)box[2], box[3] }; - ui_fill( line, ui_colour(state&mask_brighter? k_ui_bg+4: k_ui_bg+2) ); + ui_fill( ctx, line, + ui_colour(ctx,state&mask_brighter? k_ui_bg+4: k_ui_bg+2) ); - ui_fill( (ui_rect){ box[0]+line[2], box[1], box[2]-line[2], box[3] }, - ui_colour( k_ui_bg ) ); - ui_outline( box, 1, ui_colour(state? k_ui_fg+3: k_ui_bg+3), 0 ); - ui_slider_text( box, NULL, *value ); + ui_fill( ctx, (ui_rect){ box[0]+line[2], box[1], box[2]-line[2], box[3] }, + ui_colour(ctx, k_ui_bg ) ); + ui_outline( ctx, box, 1, ui_colour(ctx,state? k_ui_fg+3: k_ui_bg+3), 0 ); + ui_slider_text( ctx, box, NULL, *value ); return (state & mask_using) && 1; } -bool ui_slider( ui_rect inout_panel, const char *str_label, +bool ui_slider( ui_context *ctx, ui_rect inout_panel, const char *str_label, f32 min, f32 max, f32 *value ) { ui_rect rect, label, box; - ui_standard_widget( inout_panel, rect, 1 ); - ui_label( rect, str_label, g_ui_ctx->scale, 0, box ); - return ui_slider_standard( box, min, max, value, NULL ); + ui_standard_widget( ctx, inout_panel, rect, 1 ); + ui_label( ctx, rect, str_label, ctx->scale, 0, box ); + return ui_slider_standard( ctx, box, min, max, value, NULL ); } /* @@ -1047,10 +1035,11 @@ bool ui_slider( ui_rect inout_panel, const char *str_label, * ----------------------------------------------------------------------------- */ -void ui_colourpicker( ui_rect inout_panel, const char *str_label, v4f value ) +void ui_colourpicker( ui_context *ctx, + ui_rect inout_panel, const char *str_label, v4f value ) { ui_rect widget, left, right; - ui_standard_widget( inout_panel, widget, 8 ); + ui_standard_widget( ctx, inout_panel, widget, 8 ); ui_split_ratio( widget, k_ui_axis_v, 0.5f, 8, left, right ); ui_rect sliders[4]; @@ -1066,7 +1055,7 @@ void ui_colourpicker( ui_rect inout_panel, const char *str_label, v4f value ) for( u32 i=0; i<4; i ++ ) { - modified |= ui_slider_standard( sliders[i], 0.0f, 1.0f, hsv+i, + modified |= ui_slider_standard( ctx, sliders[i], 0.0f, 1.0f, hsv+i, (const char *[]){ "hue %.2f", "sat %.2f", "lum %.2f", @@ -1076,7 +1065,7 @@ void ui_colourpicker( ui_rect inout_panel, const char *str_label, v4f value ) ui_rect preview, square; ui_split_ratio( left, k_ui_axis_v, 0.8f, 8, square, preview ); - u32 state = ui_button_base( square ); + u32 state = ui_button_base( ctx, square ); modified |= state; enum ui_button_state @@ -1089,7 +1078,7 @@ void ui_colourpicker( ui_rect inout_panel, const char *str_label, v4f value ) { for( u32 i=0; i<2; i ++ ){ hsv[1+i] = vg_clampf( - (f32)(g_ui_ctx->mouse[i] - square[i]) / (f32)(square[2+i]), + (f32)(ctx->mouse[i] - square[i]) / (f32)(square[2+i]), 0.0f, 1.0f ); } @@ -1103,19 +1092,21 @@ void ui_colourpicker( ui_rect inout_panel, const char *str_label, v4f value ) value[3] = hsv[3]; } - ui_outline( square, 1, ui_colour( state? k_ui_fg+3: k_ui_bg+3 ), 0 ); + ui_outline( ctx, square, + 1, ui_colour(ctx, state? k_ui_fg+3: k_ui_bg+3 ), 0 ); /* preview colour */ v4f colour; vg_hsv_rgb( hsv, colour ); colour[3] = 1.0f; - ui_fill( preview, v4f_u32_colour( colour ) ); + ui_fill( ctx, preview, v4f_u32_colour( colour ) ); /* Draw hsv shader thingy */ - ui_flush( k_ui_shader_colour ); - ui_fill_rect( square, 0xffffffff, (ui_px[4]){ 0,256,256,0 } ); - g_ui_ctx->hue = hsv[0]; - ui_flush( k_ui_shader_hsv ); + ui_flush( ctx, k_ui_shader_colour, NULL ); + ui_fill_rect( ctx, square, 0xffffffff, (ui_px[4]){ 0,256,256,0 } ); + + struct ui_batch_shader_data_hsv inf = { .hue = hsv[0] }; + ui_flush( ctx, k_ui_shader_hsv, &inf ); ui_rect marker = { square[0] + hsv[1] * (f32)square[2] - 4, square[1] + (1.0f-hsv[2]) * (f32)square[3] - 4, @@ -1127,9 +1118,9 @@ void ui_colourpicker( ui_rect inout_panel, const char *str_label, v4f value ) square[1], 1, square[3] }; - ui_fill( marker, ui_colour( k_ui_fg ) ); - ui_fill( lx, ui_colour( k_ui_fg ) ); - ui_fill( ly, ui_colour( k_ui_fg ) ); + ui_fill( ctx, marker, ui_colour(ctx, k_ui_fg ) ); + ui_fill( ctx, lx, ui_colour(ctx, k_ui_fg ) ); + ui_fill( ctx, ly, ui_colour(ctx, k_ui_fg ) ); } /* @@ -1137,50 +1128,50 @@ void ui_colourpicker( ui_rect inout_panel, const char *str_label, v4f value ) * ----------------------------------------------------------------------------- */ -static void _ui_textbox_make_selection( int *start, int *end ) +static void _ui_textbox_make_selection( ui_context *ctx, int *start, int *end ) { - *start = VG_MIN( g_ui_ctx->textbox.cursor_pos, g_ui_ctx->textbox.cursor_user ); - *end = VG_MAX( g_ui_ctx->textbox.cursor_pos, g_ui_ctx->textbox.cursor_user ); + *start = VG_MIN( ctx->textbox.cursor_pos, ctx->textbox.cursor_user ); + *end = VG_MAX( ctx->textbox.cursor_pos, ctx->textbox.cursor_user ); } -void _ui_textbox_move_cursor( int *cursor0, int *cursor1, +void _ui_textbox_move_cursor( ui_context *ctx, int *cursor0, int *cursor1, int dir, int snap_together ) { - *cursor0 = VG_MAX( 0, g_ui_ctx->textbox.cursor_user + dir ); + *cursor0 = VG_MAX( 0, ctx->textbox.cursor_user + dir ); *cursor0 = VG_MIN( - VG_MIN( g_ui_ctx->textbox.len-1, strlen( g_ui_ctx->textbuf )), + VG_MIN( ctx->textbox.len-1, strlen( ctx->textbuf )), *cursor0 ); if( snap_together ) *cursor1 = *cursor0; } -static int _ui_textbox_makeroom( int datastart, int length ) +static int _ui_textbox_makeroom( ui_context *ctx, int datastart, int length ) { - int move_to = VG_MIN( datastart+length, g_ui_ctx->textbox.len-1 ); - int move_amount = strlen( g_ui_ctx->textbuf )-datastart; - int move_end = VG_MIN( move_to+move_amount, g_ui_ctx->textbox.len-1 ); + int move_to = VG_MIN( datastart+length, ctx->textbox.len-1 ); + int move_amount = strlen( ctx->textbuf )-datastart; + int move_end = VG_MIN( move_to+move_amount, ctx->textbox.len-1 ); move_amount = move_end-move_to; if( move_amount ) - memmove( &g_ui_ctx->textbuf[ move_to ], - &g_ui_ctx->textbuf[ datastart ], + memmove( &ctx->textbuf[ move_to ], + &ctx->textbuf[ datastart ], move_end-move_to ); - g_ui_ctx->textbuf[ move_end ] = '\0'; + ctx->textbuf[ move_end ] = '\0'; - return VG_MIN( length, g_ui_ctx->textbox.len-datastart-1 ); + return VG_MIN( length, ctx->textbox.len-datastart-1 ); } -int _ui_textbox_delete_char( int direction ) +int _ui_textbox_delete_char( ui_context *ctx, int direction ) { int start, end; - _ui_textbox_make_selection( &start, &end ); + _ui_textbox_make_selection( ctx, &start, &end ); /* There is no selection */ if( !(end-start) ){ - if( direction == 1 ) end = VG_MIN( end+1, strlen( g_ui_ctx->textbuf ) ); + if( direction == 1 ) end = VG_MIN( end+1, strlen( ctx->textbuf ) ); else if( direction == -1 ) start = VG_MAX( start-1, 0 ); } @@ -1189,171 +1180,183 @@ int _ui_textbox_delete_char( int direction ) return start; /* Copy the end->terminator to start */ - int remaining_length = strlen( g_ui_ctx->textbuf )+1-end; - memmove( &g_ui_ctx->textbuf[ start ], - &g_ui_ctx->textbuf[ end ], + int remaining_length = strlen( ctx->textbuf )+1-end; + memmove( &ctx->textbuf[ start ], + &ctx->textbuf[ end ], remaining_length ); return start; } -static void _ui_textbox_to_clipboard(void) +void _ui_textbox_to_clipboard( ui_context *ctx ) { int start, end; - _ui_textbox_make_selection( &start, &end ); + _ui_textbox_make_selection( ctx, &start, &end ); char buffer[512]; if( end-start ) { - memcpy( buffer, &g_ui_ctx->textbuf[ start ], end-start ); + memcpy( buffer, &ctx->textbuf[ start ], end-start ); buffer[ end-start ] = 0x00; - -#ifdef VG_ANDROID -#else - SDL_SetClipboardText( buffer ); -#endif + ctx->set_clipboard_text( buffer ); } } -static void _ui_textbox_change_callback(void) +static void _ui_textbox_change_callback( ui_context *ctx ) { - if( g_ui_ctx->textbox.callbacks.change ) + if( ctx->textbox.callbacks.change ) { - g_ui_ctx->textbox.callbacks.change( g_ui_ctx->textbuf, - g_ui_ctx->textbox.len ); + ctx->textbox.callbacks.change( ctx->textbuf, + ctx->textbox.len ); /* we gave permission to modify the buffer in this callback so.. */ - int len = strlen( g_ui_ctx->textbuf ); - g_ui_ctx->textbox.cursor_user = VG_MIN( g_ui_ctx->textbox.cursor_user, len ); - g_ui_ctx->textbox.cursor_pos = VG_MIN( g_ui_ctx->textbox.cursor_pos, len ); + int len = strlen( ctx->textbuf ); + ctx->textbox.cursor_user = VG_MIN( ctx->textbox.cursor_user, len ); + ctx->textbox.cursor_pos = VG_MIN( ctx->textbox.cursor_pos, len ); } } -void ui_start_modal( const char *message, u32 options ); -static void _ui_textbox_clipboard_paste(void) +void ui_start_modal( ui_context *ctx, const char *message, u32 options ); +void _ui_textbox_clipboard_paste( ui_context *ctx ) { -#if VG_ANDROID - return; -#else - if( !SDL_HasClipboardText() ) + if( !ctx->have_clipboard_text() ) return; - char *text = SDL_GetClipboardText(); + char *text = ctx->get_clipboard_text(); if( !text ) return; - int datastart = _ui_textbox_delete_char( 0 ); + int datastart = _ui_textbox_delete_char( ctx, 0 ); int length = strlen( text ); - if( (g_ui_ctx->textbox.len - strlen(g_ui_ctx->textbuf)) < length ){ - ui_start_modal( "Clipboard content exceeds buffer size.", UI_MODAL_BAD ); + if( (ctx->textbox.len - strlen(ctx->textbuf)) < length ){ + ui_start_modal( ctx, + "Clipboard content exceeds buffer size.", UI_MODAL_BAD ); return; } - int cpylength = _ui_textbox_makeroom( datastart, length ); + int cpylength = _ui_textbox_makeroom( ctx, datastart, length ); - memcpy( g_ui_ctx->textbuf + datastart, text, cpylength); - _ui_textbox_move_cursor( &g_ui_ctx->textbox.cursor_user, - &g_ui_ctx->textbox.cursor_pos, cpylength, 1 ); - SDL_free( text ); - _ui_textbox_change_callback(); -#endif + memcpy( ctx->textbuf + datastart, text, cpylength); + _ui_textbox_move_cursor( ctx, + &ctx->textbox.cursor_user, + &ctx->textbox.cursor_pos, cpylength, 1 ); + + ctx->free_clipboard_text( text ); + _ui_textbox_change_callback( ctx ); } -void _ui_textbox_put_char( char c ) +void _ui_textbox_put_char( ui_context *ctx, char c ) { - g_ui_ctx->textbox.cursor_user = _ui_textbox_delete_char(0); - if( (g_ui_ctx->textbox.len - strlen(g_ui_ctx->textbuf)) <= 1 ) return; + ctx->textbox.cursor_user = _ui_textbox_delete_char(ctx, 0); + if( (ctx->textbox.len - strlen(ctx->textbuf)) <= 1 ) return; - if( _ui_textbox_makeroom( g_ui_ctx->textbox.cursor_user, 1 ) ) - g_ui_ctx->textbuf[ g_ui_ctx->textbox.cursor_user ] = c; + if( _ui_textbox_makeroom( ctx, ctx->textbox.cursor_user, 1 ) ) + ctx->textbuf[ ctx->textbox.cursor_user ] = c; - _ui_textbox_move_cursor( &g_ui_ctx->textbox.cursor_user, - &g_ui_ctx->textbox.cursor_pos, 1, 1 ); + _ui_textbox_move_cursor( ctx, + &ctx->textbox.cursor_user, + &ctx->textbox.cursor_pos, 1, 1 ); } /* Receed secondary cursor */ -void _ui_textbox_left_select(void) +void _ui_textbox_left_select( ui_context *ctx ) { - _ui_textbox_move_cursor( &g_ui_ctx->textbox.cursor_user, NULL, -1, 0 ); + _ui_textbox_move_cursor( ctx, &ctx->textbox.cursor_user, NULL, -1, 0 ); } /* Match and receed both cursors */ -void _ui_textbox_left(void) +void _ui_textbox_left( ui_context *ctx ) { - int cursor_diff = g_ui_ctx->textbox.cursor_pos - g_ui_ctx->textbox.cursor_user? 0: 1; + int cursor_diff = ctx->textbox.cursor_pos - ctx->textbox.cursor_user? 0: 1; - _ui_textbox_move_cursor( &g_ui_ctx->textbox.cursor_user, - &g_ui_ctx->textbox.cursor_pos, -cursor_diff, 1 ); + _ui_textbox_move_cursor( ctx, + &ctx->textbox.cursor_user, + &ctx->textbox.cursor_pos, -cursor_diff, 1 ); } -void _ui_textbox_up(void) +void _ui_textbox_up( ui_context *ctx ) { - if( g_ui_ctx->textbox.flags & UI_TEXTBOX_MULTILINE ){ - int line_begin = g_ui_ctx->textbox.cursor_user; + if( ctx->textbox.flags & UI_TEXTBOX_MULTILINE ) + { + int line_begin = ctx->textbox.cursor_user; - while( line_begin ){ - if( g_ui_ctx->textbuf[ line_begin-1 ] == '\n' ){ + while( line_begin ) + { + if( ctx->textbuf[ line_begin-1 ] == '\n' ) + { break; } line_begin --; } - if( line_begin ){ + if( line_begin ) + { int line_above_begin = line_begin-1; - while( line_above_begin ){ - if( g_ui_ctx->textbuf[ line_above_begin-1 ] == '\n' ){ + while( line_above_begin ) + { + if( ctx->textbuf[ line_above_begin-1 ] == '\n' ) + { break; } line_above_begin --; } - int offset = g_ui_ctx->textbox.cursor_user - line_begin, + int offset = ctx->textbox.cursor_user - line_begin, line_length_above = line_begin - line_above_begin -1; offset = VG_MIN( line_length_above, offset ); - g_ui_ctx->textbox.cursor_user = line_above_begin+offset; - g_ui_ctx->textbox.cursor_pos = line_above_begin+offset; + ctx->textbox.cursor_user = line_above_begin+offset; + ctx->textbox.cursor_pos = line_above_begin+offset; } - else{ - g_ui_ctx->textbox.cursor_user = line_begin; - g_ui_ctx->textbox.cursor_pos = line_begin; + else + { + ctx->textbox.cursor_user = line_begin; + ctx->textbox.cursor_pos = line_begin; } } - else{ - if( g_ui_ctx->textbox.callbacks.up ){ - g_ui_ctx->textbox.callbacks.up( g_ui_ctx->textbuf, g_ui_ctx->textbox.len ); + else + { + if( ctx->textbox.callbacks.up ) + { + ctx->textbox.callbacks.up( ctx->textbuf, ctx->textbox.len ); } } } -void _ui_textbox_down(void) +void _ui_textbox_down( ui_context *ctx ) { - if( g_ui_ctx->textbox.flags & UI_TEXTBOX_MULTILINE ){ - int line_begin = g_ui_ctx->textbox.cursor_user; + if( ctx->textbox.flags & UI_TEXTBOX_MULTILINE ) + { + int line_begin = ctx->textbox.cursor_user; - while( line_begin ){ - if( g_ui_ctx->textbuf[ line_begin-1 ] == '\n' ){ + while( line_begin ) + { + if( ctx->textbuf[ line_begin-1 ] == '\n' ) + { break; } line_begin --; } - int line_below_begin = g_ui_ctx->textbox.cursor_user; + int line_below_begin = ctx->textbox.cursor_user; - while(1){ - if( g_ui_ctx->textbuf[ line_below_begin ] == '\0' ){ - g_ui_ctx->textbox.cursor_user = line_below_begin; - g_ui_ctx->textbox.cursor_pos = line_below_begin; + while(1) + { + if( ctx->textbuf[ line_below_begin ] == '\0' ) + { + ctx->textbox.cursor_user = line_below_begin; + ctx->textbox.cursor_pos = line_below_begin; return; } - if( g_ui_ctx->textbuf[ line_below_begin ] == '\n' ){ + if( ctx->textbuf[ line_below_begin ] == '\n' ) + { line_below_begin ++; break; } @@ -1362,68 +1365,76 @@ void _ui_textbox_down(void) } int line_below_end = line_below_begin; - while(1){ - if( g_ui_ctx->textbuf[ line_below_end ] == '\0' || - g_ui_ctx->textbuf[ line_below_end ] == '\n' ){ + while(1) + { + if( ctx->textbuf[ line_below_end ] == '\0' || + ctx->textbuf[ line_below_end ] == '\n' ){ line_below_end ++; break; } line_below_end ++; } - int offset = g_ui_ctx->textbox.cursor_user - line_begin, + int offset = ctx->textbox.cursor_user - line_begin, line_length_below = line_below_end - line_below_begin -1; offset = VG_MIN( line_length_below, offset ); - g_ui_ctx->textbox.cursor_user = line_below_begin+offset; - g_ui_ctx->textbox.cursor_pos = line_below_begin+offset; + ctx->textbox.cursor_user = line_below_begin+offset; + ctx->textbox.cursor_pos = line_below_begin+offset; } - else{ - if( g_ui_ctx->textbox.callbacks.down ){ - g_ui_ctx->textbox.callbacks.down( g_ui_ctx->textbuf, g_ui_ctx->textbox.len ); + else + { + if( ctx->textbox.callbacks.down ) + { + ctx->textbox.callbacks.down( ctx->textbuf, ctx->textbox.len ); } } } -void _ui_textbox_right_select(void) +void _ui_textbox_right_select( ui_context *ctx ) { - _ui_textbox_move_cursor( &g_ui_ctx->textbox.cursor_user, NULL, 1, 0 ); + _ui_textbox_move_cursor( ctx, &ctx->textbox.cursor_user, NULL, 1, 0 ); } -void _ui_textbox_right(void) +void _ui_textbox_right( ui_context *ctx ) { - int cursor_diff = g_ui_ctx->textbox.cursor_pos - g_ui_ctx->textbox.cursor_user? 0: 1; + int cursor_diff = ctx->textbox.cursor_pos - ctx->textbox.cursor_user? 0: 1; - _ui_textbox_move_cursor( &g_ui_ctx->textbox.cursor_user, - &g_ui_ctx->textbox.cursor_pos, +cursor_diff, 1 ); + _ui_textbox_move_cursor( ctx, + &ctx->textbox.cursor_user, + &ctx->textbox.cursor_pos, +cursor_diff, 1 ); } -void _ui_textbox_backspace(void) +void _ui_textbox_backspace( ui_context *ctx ) { - if( g_ui_ctx->focused_control_type == k_ui_control_textbox ){ - g_ui_ctx->textbox.cursor_user = _ui_textbox_delete_char( -1 ); - g_ui_ctx->textbox.cursor_pos = g_ui_ctx->textbox.cursor_user; - _ui_textbox_change_callback(); + if( ctx->focused_control_type == k_ui_control_textbox ) + { + ctx->textbox.cursor_user = _ui_textbox_delete_char( ctx, -1 ); + ctx->textbox.cursor_pos = ctx->textbox.cursor_user; + _ui_textbox_change_callback( ctx ); } } -void _ui_textbox_delete(void) +void _ui_textbox_delete( ui_context *ctx ) { - if( g_ui_ctx->focused_control_type == k_ui_control_textbox ){ - g_ui_ctx->textbox.cursor_user = _ui_textbox_delete_char( 1 ); - g_ui_ctx->textbox.cursor_pos = g_ui_ctx->textbox.cursor_user; - _ui_textbox_change_callback(); + if( ctx->focused_control_type == k_ui_control_textbox ) + { + ctx->textbox.cursor_user = _ui_textbox_delete_char( ctx, 1 ); + ctx->textbox.cursor_pos = ctx->textbox.cursor_user; + _ui_textbox_change_callback( ctx ); } } -void _ui_textbox_home_select(void) +void _ui_textbox_home_select( ui_context *ctx ) { - i32 start = g_ui_ctx->textbox.cursor_user; + i32 start = ctx->textbox.cursor_user; - if( g_ui_ctx->textbox.flags & UI_TEXTBOX_MULTILINE ){ - while( start ){ - if( g_ui_ctx->textbuf[start-1] == '\n' ) + if( ctx->textbox.flags & UI_TEXTBOX_MULTILINE ) + { + while( start ) + { + if( ctx->textbuf[start-1] == '\n' ) break; else start --; @@ -1432,70 +1443,75 @@ void _ui_textbox_home_select(void) else start = 0; - g_ui_ctx->textbox.cursor_user = start; + ctx->textbox.cursor_user = start; } -void _ui_textbox_home(void) +void _ui_textbox_home( ui_context *ctx ) { - _ui_textbox_home_select(); - g_ui_ctx->textbox.cursor_pos = g_ui_ctx->textbox.cursor_user; + _ui_textbox_home_select( ctx ); + ctx->textbox.cursor_pos = ctx->textbox.cursor_user; } -void _ui_textbox_end_select(void) +void _ui_textbox_end_select( ui_context *ctx ) { - i32 end = g_ui_ctx->textbox.cursor_user; + i32 end = ctx->textbox.cursor_user; - if( g_ui_ctx->textbox.flags & UI_TEXTBOX_MULTILINE ){ - while( g_ui_ctx->textbuf[end] ){ - if( g_ui_ctx->textbuf[end] == '\n' ) + if( ctx->textbox.flags & UI_TEXTBOX_MULTILINE ) + { + while( ctx->textbuf[end] ) + { + if( ctx->textbuf[end] == '\n' ) break; else end ++; } } else - end = VG_MIN( g_ui_ctx->textbox.len-1, strlen(g_ui_ctx->textbuf) ); + end = VG_MIN( ctx->textbox.len-1, strlen(ctx->textbuf) ); - g_ui_ctx->textbox.cursor_user = end; + ctx->textbox.cursor_user = end; } -void _ui_textbox_end(void) +void _ui_textbox_end( ui_context *ctx ) { - _ui_textbox_end_select(); - g_ui_ctx->textbox.cursor_pos = g_ui_ctx->textbox.cursor_user; + _ui_textbox_end_select( ctx ); + ctx->textbox.cursor_pos = ctx->textbox.cursor_user; } -void _ui_textbox_select_all(void) +void _ui_textbox_select_all( ui_context *ctx ) { - _ui_textbox_move_cursor( &g_ui_ctx->textbox.cursor_user, NULL, 10000, 0); - _ui_textbox_move_cursor( &g_ui_ctx->textbox.cursor_pos, NULL, -10000, 0); + _ui_textbox_move_cursor( ctx, &ctx->textbox.cursor_user, NULL, 10000, 0 ); + _ui_textbox_move_cursor( ctx, &ctx->textbox.cursor_pos, NULL, -10000, 0 ); } -void _ui_textbox_cut(void) +void _ui_textbox_cut( ui_context *ctx ) { - _ui_textbox_to_clipboard(); - g_ui_ctx->textbox.cursor_user = _ui_textbox_delete_char(0); - g_ui_ctx->textbox.cursor_pos = g_ui_ctx->textbox.cursor_user; - _ui_textbox_change_callback(); + _ui_textbox_to_clipboard( ctx ); + ctx->textbox.cursor_user = _ui_textbox_delete_char( ctx, 0 ); + ctx->textbox.cursor_pos = ctx->textbox.cursor_user; + _ui_textbox_change_callback( ctx ); } -void _ui_textbox_enter(void) +void _ui_textbox_enter( ui_context *ctx ) { - if( g_ui_ctx->focused_control_type == k_ui_control_textbox ){ - g_ui_ctx->ignore_input_frames = 2; + if( ctx->focused_control_type == k_ui_control_textbox ) + { + ui_ignore_input_frames( ctx, 2 ); - if( g_ui_ctx->textbox.callbacks.enter ) - g_ui_ctx->textbox.callbacks.enter( g_ui_ctx->textbuf, g_ui_ctx->textbox.len ); + if( ctx->textbox.callbacks.enter ) + ctx->textbox.callbacks.enter( ctx->textbuf, ctx->textbox.len ); - if( g_ui_ctx->focused_control_type != k_ui_control_textbox ) return; + if( ctx->focused_control_type != k_ui_control_textbox ) return; - if( g_ui_ctx->textbox.flags & UI_TEXTBOX_MULTILINE ){ - _ui_textbox_put_char( '\n' ); - _ui_textbox_change_callback(); + if( ctx->textbox.flags & UI_TEXTBOX_MULTILINE ) + { + _ui_textbox_put_char( ctx, '\n' ); + _ui_textbox_change_callback( ctx ); } - else{ - if( !(g_ui_ctx->textbox.flags & UI_TEXTBOX_AUTOFOCUS ) ) - ui_defocus_all(); + else + { + if( !(ctx->textbox.flags & UI_TEXTBOX_AUTOFOCUS ) ) + ui_defocus_all( ctx ); } } } @@ -1506,23 +1522,29 @@ void _ui_textbox_enter(void) * * input coordinates go in co[0], co[1], and the result index is in co[2] */ -static void _ui_textbox_calc_index_from_grid( int co[3], int wrap_length ){ +static void _ui_textbox_calc_index_from_grid( ui_context *ctx, + int co[3], int wrap_length ) +{ int i[3] = {0,0,0}; char c; - while( (c = g_ui_ctx->textbuf[i[2]]) ){ + while( (c = ctx->textbuf[i[2]]) ) + { if( i[1]==co[1] && i[0]>=co[0] ) break; - if( i[0] >= wrap_length ){ + if( i[0] >= wrap_length ) + { i[1] ++; i[0] = 0; } - if( c >= 32 && c <= 126 ){ + if( c >= 32 && c <= 126 ) + { i[0] ++; i[2] ++; } - else if( c == '\n' ){ + else if( c == '\n' ) + { i[1] ++; if( i[1] > co[1] ) break; @@ -1542,21 +1564,26 @@ static void _ui_textbox_calc_index_from_grid( int co[3], int wrap_length ){ * based on the index specied in co[2], work out the visual character * coordinates and store them in co[0], co[1] */ -static void _ui_textbox_index_calc_coords( int co[3], int wrap_length ){ +static void _ui_textbox_index_calc_coords( ui_context *ctx, + int co[3], int wrap_length ) +{ co[0] = 0; co[1] = 0; char c; int i=0; - while( (c = g_ui_ctx->textbuf[i ++]) ){ + while( (c = ctx->textbuf[i ++]) ) + { if( i > co[2] ) break; - if( co[0] >= wrap_length ){ + if( co[0] >= wrap_length ) + { co[1] ++; co[0] = 0; } if( c >= 32 && c <= 126 ) co[0] ++; - else if( c == '\n' ){ + else if( c == '\n' ) + { co[1] ++; co[0] = 0; } @@ -1570,10 +1597,13 @@ static void _ui_textbox_index_calc_coords( int co[3], int wrap_length ){ * * index must be fully populated with visual X/Y, and linear index */ -static int _ui_textbox_run_remaining( int index[3], int wrap_length ){ +static int _ui_textbox_run_remaining( ui_context *ctx, + int index[3], int wrap_length ) +{ int i=0, printed_chars=0; char c; - while( (c = g_ui_ctx->textbuf[index[2] + (i ++)]) ){ + while( (c = ctx->textbuf[index[2] + (i ++)]) ) + { if( index[0]+i >= wrap_length ) break; if( c >= 32 && c <= 126 ) printed_chars ++; else if( c == '\n' ) break; @@ -1582,27 +1612,27 @@ static int _ui_textbox_run_remaining( int index[3], int wrap_length ){ return printed_chars+1; } -int ui_textbox( ui_rect inout_panel, const char *label, +int ui_textbox( ui_context *ctx, ui_rect inout_panel, const char *label, char *buf, u32 len, u32 lines, u32 flags, struct ui_textbox_callbacks *callbacks ) { if( lines > 1 ) flags |= UI_TEXTBOX_MULTILINE; ui_rect rect; - ui_standard_widget( inout_panel, rect, lines ); + ui_standard_widget( ctx, inout_panel, rect, lines ); if( label ) - ui_label( rect, label, 1, 0, rect ); + ui_label( ctx, rect, label, 1, 0, rect ); - int clickup= ui_click_up(UI_MOUSE_LEFT), - clickdown = ui_click_down(UI_MOUSE_LEFT), - click = ui_clicking(UI_MOUSE_LEFT) | clickup, - target = ui_inside_rect( rect, g_ui_ctx->mouse_click ) && click, - hover = ui_inside_rect( rect, g_ui_ctx->mouse ); + int clickup= ui_click_up(ctx, UI_MOUSE_LEFT), + clickdown = ui_click_down(ctx, UI_MOUSE_LEFT), + click = ui_clicking(ctx, UI_MOUSE_LEFT) | clickup, + target = ui_inside_rect( rect, ctx->mouse_click ) && click, + hover = ui_inside_rect( rect, ctx->mouse ); /* allow instant transitions from textbox->textbox */ - if( (g_ui_ctx->focused_control_type != k_ui_control_none) && - (g_ui_ctx->focused_control_type != k_ui_control_textbox) ){ + if( (ctx->focused_control_type != k_ui_control_none) && + (ctx->focused_control_type != k_ui_control_textbox) ){ clickup = 0; clickdown = 0; click = 0; @@ -1611,9 +1641,9 @@ int ui_textbox( ui_rect inout_panel, const char *label, flags &= ~UI_TEXTBOX_AUTOFOCUS; } - u32 col_base = ui_colour( k_ui_bg ), - col_highlight = ui_colour( k_ui_fg ), - col_cursor = (0x00ffffff & ui_colour(k_ui_fg))|0x7f000000; + u32 col_base = ui_colour(ctx, k_ui_bg ), + col_highlight = ui_colour(ctx, k_ui_fg ), + col_cursor = (0x00ffffff & ui_colour(ctx,k_ui_fg))|0x7f000000; ui_px border = -1; @@ -1621,7 +1651,7 @@ int ui_textbox( ui_rect inout_panel, const char *label, rect_copy( rect, text_rect ); if( flags & UI_TEXTBOX_MULTILINE ) text_rect[3] = rect[3]-16; - else text_rect[3] = g_ui_ctx->font->sy; + else text_rect[3] = ctx->font->sy; text_rect[2] -= 16; ui_rect_center( rect, text_rect ); @@ -1629,59 +1659,60 @@ int ui_textbox( ui_rect inout_panel, const char *label, ui_px wrap_length = 1024; if( flags & UI_TEXTBOX_WRAP ) - wrap_length = text_rect[2] / g_ui_ctx->font->sx; + wrap_length = text_rect[2] / ctx->font->sx; if( hover ) { - g_ui_ctx->cursor = k_ui_cursor_ibeam; + ctx->cursor = k_ui_cursor_ibeam; } - if( g_ui_ctx->focused_control_id == buf ) + if( ctx->focused_control_id == buf ) { - ui_fill( rect, col_base ); - ui_ntext( text_rect, buf, wrap_length, 1, k_ui_align_left, 0 ); + ui_fill( ctx, rect, col_base ); + ui_ntext( ctx, text_rect, buf, wrap_length, 1, k_ui_align_left, 0 ); if( !(flags & UI_TEXTBOX_AUTOFOCUS) && ((clickup||clickdown) && !target)) { - ui_defocus_all(); + ui_defocus_all( ctx ); } else { - g_ui_ctx->focused_control_hit = 1; - if( click && target ){ + ctx->focused_control_hit = 1; + if( click && target ) + { int p0[3] ={ - (g_ui_ctx->mouse_click[0] - text_rect[0]) / g_ui_ctx->font->sx, - (g_ui_ctx->mouse_click[1] - text_rect[1]) / g_ui_ctx->font->sy, + (ctx->mouse_click[0] - text_rect[0]) / ctx->font->sx, + (ctx->mouse_click[1] - text_rect[1]) / ctx->font->sy, -1 }, p1[3] = { - (g_ui_ctx->mouse[0] - text_rect[0]) / g_ui_ctx->font->sx, - (g_ui_ctx->mouse[1] - text_rect[1]) / g_ui_ctx->font->sy, + (ctx->mouse[0] - text_rect[0]) / ctx->font->sx, + (ctx->mouse[1] - text_rect[1]) / ctx->font->sy, -1 }; if( flags & UI_TEXTBOX_MULTILINE ) { - _ui_textbox_calc_index_from_grid( p0, wrap_length ); - _ui_textbox_calc_index_from_grid( p1, wrap_length ); + _ui_textbox_calc_index_from_grid( ctx, p0, wrap_length ); + _ui_textbox_calc_index_from_grid( ctx, p1, wrap_length ); - g_ui_ctx->textbox.cursor_pos = p0[2]; - g_ui_ctx->textbox.cursor_user = p1[2]; + ctx->textbox.cursor_pos = p0[2]; + ctx->textbox.cursor_user = p1[2]; } else { int max = strlen( buf ); - g_ui_ctx->textbox.cursor_pos = VG_MAX( 0, VG_MIN( max, p0[0] )), - g_ui_ctx->textbox.cursor_user = VG_MAX( 0, VG_MIN( max, p1[0] )); + ctx->textbox.cursor_pos = VG_MAX( 0, VG_MIN( max, p0[0] )), + ctx->textbox.cursor_user = VG_MAX( 0, VG_MIN( max, p1[0] )); } } - ui_outline( rect, -2, g_ui_ctx->scheme[ k_ui_orange ], 0 ); + ui_outline( ctx, rect, -2, ctx->scheme[ k_ui_orange ], 0 ); ui_rect cursor; - int c0 = g_ui_ctx->textbox.cursor_pos, - c1 = g_ui_ctx->textbox.cursor_user, + int c0 = ctx->textbox.cursor_pos, + c1 = ctx->textbox.cursor_user, start = VG_MIN( c0, c1 ), end = VG_MAX( c0, c1 ), chars = end-start; @@ -1691,42 +1722,42 @@ int ui_textbox( ui_rect inout_panel, const char *label, int pos[3], remaining = chars; pos[2] = start; - _ui_textbox_index_calc_coords( pos, wrap_length ); + _ui_textbox_index_calc_coords( ctx, pos, wrap_length ); if( start==end ) { - cursor[0] = text_rect[0] + pos[0]*g_ui_ctx->font->sx-1; + cursor[0] = text_rect[0] + pos[0]*ctx->font->sx-1; cursor[1] = text_rect[1] + pos[1]*14; cursor[2] = 2; cursor[3] = 13; - ui_fill( cursor, col_cursor ); - rect_copy( cursor, g_ui_ctx->click_fader_end ); + ui_fill( ctx, cursor, col_cursor ); + rect_copy( cursor, ctx->click_fader_end ); } else { while( remaining ) { - int run = _ui_textbox_run_remaining( pos, wrap_length ); + int run = _ui_textbox_run_remaining( ctx, pos, wrap_length ); run = VG_MIN( run, remaining ); - cursor[0] = text_rect[0] + pos[0]*g_ui_ctx->font->sx-1; + cursor[0] = text_rect[0] + pos[0]*ctx->font->sx-1; cursor[1] = text_rect[1] + pos[1]*14; - cursor[2] = (float)(run)*(float)g_ui_ctx->font->sx; + cursor[2] = (float)(run)*(float)ctx->font->sx; cursor[3] = 13; - ui_fill( cursor, col_cursor ); + ui_fill( ctx, cursor, col_cursor ); remaining -= run; pos[0] = 0; pos[1] ++; pos[2] += run; } - rect_copy( cursor, g_ui_ctx->click_fader_end ); + rect_copy( cursor, ctx->click_fader_end ); } } else { - cursor[0] = text_rect[0] + start*g_ui_ctx->font->sx-1; + cursor[0] = text_rect[0] + start*ctx->font->sx-1; cursor[1] = text_rect[1]; cursor[3] = 13; @@ -1736,16 +1767,16 @@ int ui_textbox( ui_rect inout_panel, const char *label, } else { - cursor[2] = (float)(chars)*(float)g_ui_ctx->font->sx; + cursor[2] = (float)(chars)*(float)ctx->font->sx; } - if( (g_ui_ctx->click_fade_opacity<=0.0f) && + if( (ctx->click_fade_opacity<=0.0f) && ui_clip( rect, cursor, cursor ) ) { - ui_fill( cursor, col_cursor ); + ui_fill( ctx, cursor, col_cursor ); } - rect_copy( cursor, g_ui_ctx->click_fader_end ); + rect_copy( cursor, ctx->click_fader_end ); } } @@ -1756,49 +1787,46 @@ int ui_textbox( ui_rect inout_panel, const char *label, { if( (target && hover) || (flags & UI_TEXTBOX_AUTOFOCUS) ) { - ui_defocus_all(); + ui_defocus_all( ctx ); - ui_fill( rect, col_highlight ); - g_ui_ctx->ignore_input_frames = 2; - rect_copy( rect, g_ui_ctx->click_fader ); - rect_copy( rect, g_ui_ctx->click_fader_end ); + ui_fill( ctx, rect, col_highlight ); + ui_ignore_input_frames( ctx, 2 ); + rect_copy( rect, ctx->click_fader ); + rect_copy( rect, ctx->click_fader_end ); - g_ui_ctx->click_fade_opacity = 1.0f; - g_ui_ctx->textbuf = buf; - g_ui_ctx->focused_control_hit = 1; - g_ui_ctx->focused_control_type = k_ui_control_textbox; - g_ui_ctx->textbox.len = len; - g_ui_ctx->textbox.flags = flags; - g_ui_ctx->textbox.cursor_pos = 0; - g_ui_ctx->textbox.cursor_user = 0; + ctx->click_fade_opacity = 1.0f; + ctx->textbuf = buf; + ctx->focused_control_hit = 1; + ctx->focused_control_type = k_ui_control_textbox; + ctx->textbox.len = len; + ctx->textbox.flags = flags; + ctx->textbox.cursor_pos = 0; + ctx->textbox.cursor_user = 0; if( callbacks ) { - g_ui_ctx->textbox.callbacks = *callbacks; + ctx->textbox.callbacks = *callbacks; } else { - g_ui_ctx->textbox.callbacks.change = NULL; - g_ui_ctx->textbox.callbacks.down = NULL; - g_ui_ctx->textbox.callbacks.up = NULL; - g_ui_ctx->textbox.callbacks.enter = NULL; + ctx->textbox.callbacks.change = NULL; + ctx->textbox.callbacks.down = NULL; + ctx->textbox.callbacks.up = NULL; + ctx->textbox.callbacks.enter = NULL; } -#ifdef VG_ANDROID -#else - SDL_StartTextInput(); -#endif + ctx->start_text_input(); } } - ui_fill( rect, col_base ); + ui_fill( ctx, rect, col_base ); if( hover ) { - ui_outline( rect, -1, col_highlight, 0 ); + ui_outline( ctx, rect, -1, col_highlight, 0 ); } - ui_ntext( text_rect, buf, wrap_length, 1, k_ui_align_left, 0 ); + ui_ntext( ctx, text_rect, buf, wrap_length, 1, k_ui_align_left, 0 ); return 0; } @@ -1807,11 +1835,11 @@ int ui_textbox( ui_rect inout_panel, const char *label, * ----------------------------------------------------------------------------- */ -void ui_tabs( ui_rect inout_panel, ui_rect out_content_panel, +void ui_tabs( ui_context *ctx, ui_rect inout_panel, ui_rect out_content_panel, const char **titles, u32 count, i32 *page ) { ui_rect bar; - ui_standard_widget( inout_panel, bar, 1 ); + ui_standard_widget( ctx, inout_panel, bar, 1 ); i32 cur_page = *page; @@ -1821,12 +1849,12 @@ void ui_tabs( ui_rect inout_panel, ui_rect out_content_panel, inout_panel[1] = bar[1]+bar[3]; inout_panel[3] = h; - ui_fill( inout_panel, ui_colour( k_ui_bg+2 ) ); - ui_outline( inout_panel, 1, ui_colour( k_ui_bg+5 ), 0 ); + ui_fill( ctx, inout_panel, ui_colour(ctx, k_ui_bg+2 ) ); + ui_outline( ctx, inout_panel, 1, ui_colour(ctx, k_ui_bg+5 ), 0 ); rect_copy( inout_panel, out_content_panel ); ui_rect_pad( out_content_panel, - (ui_px[2]){ g_ui_ctx->padding, g_ui_ctx->padding } ); + (ui_px[2]){ ctx->padding, ctx->padding } ); /* place buttons */ for( i32 i=0; ifocused_control_type = k_ui_control_modal; - g_ui_ctx->modal.message = message; - g_ui_ctx->modal.callbacks.close = NULL; - g_ui_ctx->modal.options = options; + ctx->focused_control_type = k_ui_control_modal; + ctx->modal.message = message; + ctx->modal.callbacks.close = NULL; + ctx->modal.options = options; u32 type = options & UI_MODAL_TYPE_BITS; if( type == UI_MODAL_OK ) vg_info( message ); @@ -1875,93 +1903,24 @@ void ui_start_modal( const char *message, u32 options ) * ----------------------------------------------------------------------------- */ -/* - * Handles binds - */ -#ifdef VG_ANDROID -#else -void ui_proc_key( SDL_Keysym ev ) -{ - if( g_ui_ctx->focused_control_type != k_ui_control_textbox ){ - return; - } - - struct textbox_mapping{ - u16 mod; - SDL_Keycode key; - - void (*handler)(void); - } - mappings[] = - { - { 0, SDLK_LEFT, _ui_textbox_left }, - { KMOD_SHIFT, SDLK_LEFT, _ui_textbox_left_select }, - { 0, SDLK_RIGHT, _ui_textbox_right }, - { KMOD_SHIFT, SDLK_RIGHT, _ui_textbox_right_select }, - { 0, SDLK_DOWN, _ui_textbox_down }, - { 0, SDLK_UP, _ui_textbox_up }, - { 0, SDLK_BACKSPACE, _ui_textbox_backspace }, - { KMOD_SHIFT, SDLK_BACKSPACE, _ui_textbox_backspace }, - { KMOD_CTRL, SDLK_BACKSPACE, _ui_textbox_backspace }, - { 0, SDLK_DELETE, _ui_textbox_delete }, - { 0, SDLK_HOME, _ui_textbox_home }, - { KMOD_SHIFT, SDLK_HOME, _ui_textbox_home_select }, - { 0, SDLK_END, _ui_textbox_end }, - { KMOD_SHIFT, SDLK_END, _ui_textbox_end_select }, - { KMOD_CTRL, SDLK_a, _ui_textbox_select_all }, - { KMOD_CTRL, SDLK_c, _ui_textbox_to_clipboard }, - { KMOD_CTRL, SDLK_x, _ui_textbox_cut }, - { KMOD_CTRL, SDLK_v, _ui_textbox_clipboard_paste }, - { 0, SDLK_RETURN, _ui_textbox_enter }, - { 0, SDLK_ESCAPE, ui_defocus_all }, - }; - - SDL_Keymod mod = 0; - - if( ev.mod & KMOD_SHIFT ) - mod |= KMOD_SHIFT; - - if( ev.mod & KMOD_CTRL ) - mod |= KMOD_CTRL; - - if( ev.mod & KMOD_ALT ) - mod |= KMOD_ALT; - - for( int i=0; ikey == ev.sym ){ - if( mapping->mod == 0 ){ - if( mod == 0 ){ - mapping->handler(); - return; - } - } - else if( (mod & mapping->mod) == mapping->mod ){ - mapping->handler(); - return; - } - } - } -} -#endif - /* * Callback for text entry mode */ -void ui_proc_utf8( const char *text ) +void ui_proc_utf8( ui_context *ctx, const char *text ) { - if( g_ui_ctx->focused_control_type == k_ui_control_textbox ) + if( ctx->focused_control_type == k_ui_control_textbox ) { const char *ptr = text; while( *ptr ) { - if( *ptr != '`' ) _ui_textbox_put_char( *ptr ); + if( *ptr != '`' ) + _ui_textbox_put_char( ctx, *ptr ); + ptr ++; } - _ui_textbox_change_callback(); + _ui_textbox_change_callback( ctx ); } } @@ -1970,11 +1929,11 @@ void ui_proc_utf8( const char *text ) * ----------------------------------------------------------------------------- */ -void ui_dev_colourview(void) +void ui_dev_colourview( ui_context *ctx ) { - ui_rect window = {g_ui_ctx->area[0]-256,0,256,g_ui_ctx->area[1]}, swatch; + ui_rect window = {ctx->area[0]-256,0,256,ctx->area[1]}, swatch; - const char *names[vg_list_size(g_ui_ctx->scheme)] = { + const char *names[vg_list_size(ctx->scheme)] = { [k_ui_bg] = "k_ui_bg", "k_ui_bg+1", "k_ui_bg+2", "k_ui_bg+3", "k_ui_bg+4", "k_ui_bg+5", "k_ui_bg+6", "k_ui_bg+7", @@ -1989,13 +1948,17 @@ void ui_dev_colourview(void) ui_rect col[2]; ui_split_ratio( window, k_ui_axis_v, 0.5f, 0, col[0], col[1] ); - for( int i=0; ischeme); i++ ){ + for( int i=0; ischeme); i++ ) + { int which = (i/8)%2; ui_split( col[which], k_ui_axis_h, 24, 0, swatch, col[which] ); - ui_fill( swatch, ui_colour(i) ); + ui_fill( ctx, swatch, ui_colour(ctx,i) ); if( names[i] ) - ui_text(swatch, names[i], 1, k_ui_align_middle_left, ui_colourcont(i)); + { + ui_text( ctx, swatch, names[i], 1, + k_ui_align_middle_left, ui_colourcont(ctx,i)); + } } } diff --git a/vg_ui/imgui.h b/vg_ui/imgui.h index 4da6162..2c4eb38 100644 --- a/vg_ui/imgui.h +++ b/vg_ui/imgui.h @@ -195,7 +195,6 @@ struct ui_context ui_scheme scheme; const vg_font_face *font; - v2f inverse_font_sheet; enum ui_cursor { @@ -207,32 +206,51 @@ struct ui_context cursor; ui_px widget_height, scale, padding; + + void (*render_batch)(ui_context *ctx, ui_batch *batch, + enum ui_shader shader, void *shader_data ); + bool (*have_clipboard_text)(void); + char *(*get_clipboard_text)(void); + void (*free_clipboard_text)(void *text); + int (*set_clipboard_text)(const char *text); + + void (*start_text_input)(void); + void (*stop_text_input)(void); +}; + +struct ui_batch_shader_data_hsv +{ f32 hue; +}; - /* at some point this should be implementation specific? */ - v4f colour; - f32 frosting; - v2f bg_inverse_ratio; - GLuint tex_bg; +struct ui_batch_shader_data_image +{ + void *resource; }; struct ui_batch { - enum ui_shader shader; + ui_vert *vert_buf; /* base vertex array + [vert_offset] */ + u32 vert_count, + vert_offset; /* in bytes */ - ui_vert *vert_buf; - u32 vert_count, vert_offset; - - u16 *indice_buf; - u32 indice_count, indice_offset; + u16 *indice_buf; /* base indice array + [indice_offset] */ + u32 indice_count, + indice_offset; /* in bytes */ }; -void ui_bind_context( ui_context *context ); -void ui_init( ui_vert *verts_buf, u32 verts_max, +/* set the memory buffer and limits of ui context */ +void ui_init( ui_context *ctx, + ui_vert *verts_buf, u32 verts_max, u16 *indices_buf, u32 indices_max ); -ui_vert *ui_fill_rect( ui_rect rect, u32 colour, ui_px uv[4] ); -ui_vert *ui_fill( ui_rect rect, u32 colour ); -void ui_outline( ui_rect rect, ui_px thickness, u32 colour, u32 mask ); + +/* simple fill drawing commands */ +ui_vert *ui_fill_rect( ui_context *ctx, ui_rect rect, u32 colour, ui_px uv[4] ); +ui_vert *ui_fill( ui_context *ctx, ui_rect rect, u32 colour ); +void ui_outline( ui_context *ctx, ui_rect rect, ui_px thickness, + u32 colour, u32 mask ); + +/* rect controls */ void rect_copy( ui_rect a, ui_rect b ); void ui_split( ui_rect rect, enum ui_axis other, ui_px width, ui_px gap, ui_rect l, ui_rect r ); @@ -241,104 +259,126 @@ void ui_split_ratio( ui_rect rect, enum ui_axis dir, float ratio, void ui_rect_pad( ui_rect rect, ui_px pad[2] ); int ui_clip( ui_rect parent, ui_rect child, ui_rect clipped ); int ui_inside_rect( ui_rect rect, ui_px co[2] ); -int ui_click_down( u32 mask ); -int ui_clicking( u32 mask ); -int ui_click_up( u32 mask ); -void ui_prerender( ui_px dims[2], ui_px mouse[2], i32 mouse_state ); -void ui_ignore_input_frames( u32 frames ); -void ui_capture_mouse( bool on ); -void ui_flush( enum ui_shader shader ); -void ui_rect_center( ui_rect parent, ui_rect rect ); void ui_fit_item( ui_rect rect, ui_px size[2], ui_rect d ); -ui_px ui_text_line_width( const char *str ); -ui_px ui_text_string_height( const char *str ); -ui_px ui_text_aligned_x( const char *str, ui_rect rect, ui_px scale, + +/* ui interactions */ +void ui_update_mouse( ui_context *ctx, ui_px mouse[2], i32 mouse_state ); +int ui_click_down( ui_context *ctx, u32 mask ); +int ui_clicking( ui_context *ctx, u32 mask ); +int ui_click_up( ui_context *ctx, u32 mask ); +void ui_ignore_input_frames( ui_context *ctx, u32 frames ); +void ui_capture_mouse( ui_context *ctx, bool on ); + +/* ui cycle */ +void ui_prerender( ui_context *ctx ); +void ui_flush( ui_context *ctx, enum ui_shader shader, void *shader_data ); +void ui_rect_center( ui_rect parent, ui_rect rect ); +void ui_set_area( ui_context *ctx, i32 width, i32 height ); + +/* text drawing */ +ui_px ui_text_line_width( ui_context *ctx, const char *str ); +ui_px ui_text_string_height( ui_context *ctx, const char *str ); +ui_px ui_text_aligned_x( ui_context *ctx, + const char *str, ui_rect rect, ui_px scale, enum ui_align align ); -u32 ui_colour( enum ui_scheme_colour id ); -u32 ui_colourcont( enum ui_scheme_colour id ); +u32 ui_ntext( ui_context *ctx, + ui_rect rect, const char *str, u32 len, ui_px scale, + enum ui_align align, u32 colour ); +u32 ui_text( ui_context *ctx, + ui_rect rect, const char *str, ui_px scale, + enum ui_align align, u32 colour ); + +/* colour schemes */ +u32 ui_colour( ui_context *ctx, enum ui_scheme_colour id ); +u32 ui_colourcont( ui_context *ctx, enum ui_scheme_colour id ); + +/* generic colour functions */ void ui_hex_to_norm( u32 hex, v4f norm ); u32 v4f_u32_colour( v4f colour ); u32 ui_opacity( u32 colour, f32 opacity ); -u32 ui_ntext( ui_rect rect, const char *str, u32 len, ui_px scale, - enum ui_align align, u32 colour ); -u32 ui_text( ui_rect rect, const char *str, ui_px scale, - enum ui_align align, u32 colour ); -void ui_font_face( vg_font_face *ff ); -void ui_panel( ui_rect in_rect, ui_rect out_panel ); -void ui_label( ui_rect rect, const char *text, ui_px size, + +/* standard widgets & controls */ +ui_px ui_standard_widget_height( ui_context *ctx, ui_px count ); +void ui_standard_widget( ui_context *ctx, + ui_rect inout_panel, ui_rect out_rect, ui_px count ); +void ui_panel( ui_context *ctx, ui_rect in_rect, ui_rect out_panel ); +void ui_label( ui_context *ctx, + ui_rect rect, const char *text, ui_px size, ui_px gap, ui_rect r ); -ui_px ui_standard_widget_height( ui_px count ); -void ui_standard_widget( ui_rect inout_panel, ui_rect out_rect, ui_px count ); -void ui_info( ui_rect inout_panel, const char *text ); -void ui_image( ui_rect rect, GLuint image ); -void ui_defocus_all(void); -enum ui_button_state ui_button_base( ui_rect rect ); +void ui_info( ui_context *ctx, ui_rect inout_panel, const char *text ); +void ui_image( ui_context *ctx, ui_rect rect, void *resource ); + +enum ui_button_state ui_button_base( ui_context *ctx, ui_rect rect ); /* TODO: split this out into a formatless button and one that auto fills */ -enum ui_button_state ui_colourbutton( ui_rect rect, +enum ui_button_state ui_colourbutton( ui_context *ctx, ui_rect rect, enum ui_scheme_colour colour, enum ui_scheme_colour hover_colour, enum ui_scheme_colour hi_colour ); enum ui_button_state ui_colourbutton_text( + ui_context *ctx, ui_rect rect, const char *string, ui_px scale, enum ui_scheme_colour colour ); -enum ui_button_state ui_button_text( ui_rect rect, + +enum ui_button_state ui_button_text( ui_context *ctx, ui_rect rect, const char *string, ui_px scale ); -enum ui_button_state ui_button( ui_rect inout_panel, const char *string ); -void ui_postrender( f32 delta_time ); -enum ui_button_state ui_checkbox_base( ui_rect box, i32 *data ); -int ui_checkbox( ui_rect inout_panel, const char *str_label, i32 *data ); -void ui_enum( ui_rect inout_panel, const char *str_label, +enum ui_button_state ui_button( ui_context *ctx, + ui_rect inout_panel, const char *string ); +void ui_postrender( ui_context *ctx, f32 delta_time ); +enum ui_button_state ui_checkbox_base( ui_context *ctx, + ui_rect box, i32 *data ); +int ui_checkbox( ui_context *ctx, + ui_rect inout_panel, const char *str_label, i32 *data ); +void ui_enum( ui_context *ctx, ui_rect inout_panel, const char *str_label, struct ui_enum_opt *options, u32 len, i32 *value ); enum ui_button_state ui_slider_base( + ui_context *ctx, ui_rect box, f32 min, f32 max, f32 *value, f32 *out_t ); -void ui_slider_text( ui_rect box, const char *format, f32 value ); -bool ui_slider_standard( ui_rect box, f32 min, f32 max, f32 *value, +void ui_slider_text( ui_context *ctx, + ui_rect box, const char *format, f32 value ); +bool ui_slider_standard( ui_context *ctx, + ui_rect box, f32 min, f32 max, f32 *value, const char *format ); -bool ui_slider( ui_rect inout_panel, const char *str_label, +bool ui_slider( ui_context *ctx, ui_rect inout_panel, const char *str_label, f32 min, f32 max, f32 *value ); -void ui_colourpicker( ui_rect inout_panel, const char *str_label, v4f value ); -void _ui_textbox_move_cursor( int *cursor0, int *cursor1, +void ui_colourpicker( ui_context *ctx, + ui_rect inout_panel, const char *str_label, v4f value ); +void _ui_textbox_move_cursor( ui_context *ctx, int *cursor0, int *cursor1, int dir, int snap_together ); -int _ui_textbox_delete_char( int direction ); -void ui_start_modal( const char *message, u32 options ); -void _ui_textbox_put_char( char c ); -void _ui_textbox_left_select(void); -void _ui_textbox_left(void); -void _ui_textbox_up(void); -void _ui_textbox_down(void); -void _ui_textbox_right_select(void); -void _ui_textbox_right(void); -void _ui_textbox_backspace(void); -void _ui_textbox_delete(void); -void _ui_textbox_home_select(void); -void _ui_textbox_home(void); -void _ui_textbox_end_select(void); -void _ui_textbox_end(void); -void _ui_textbox_select_all(void); -void _ui_textbox_cut(void); -void _ui_textbox_enter(void); -int ui_textbox( ui_rect inout_panel, const char *label, +int _ui_textbox_delete_char( ui_context *ctx, int direction ); +void ui_start_modal( ui_context *ctx, const char *message, u32 options ); + +void _ui_textbox_put_char( ui_context *ctx, char c ); +void _ui_textbox_left_select( ui_context *ctx ); +void _ui_textbox_left( ui_context *ctx ); +void _ui_textbox_up( ui_context *ctx ); +void _ui_textbox_down( ui_context *ctx ); +void _ui_textbox_right_select( ui_context *ctx ); +void _ui_textbox_right( ui_context *ctx ); +void _ui_textbox_backspace( ui_context *ctx ); +void _ui_textbox_delete( ui_context *ctx ); +void _ui_textbox_home_select( ui_context *ctx ); +void _ui_textbox_home( ui_context *ctx ); +void _ui_textbox_end_select( ui_context *ctx ); +void _ui_textbox_end( ui_context *ctx ); +void _ui_textbox_select_all( ui_context *ctx ); +void _ui_textbox_cut( ui_context *ctx ); +void _ui_textbox_enter( ui_context *ctx ); +void _ui_textbox_to_clipboard( ui_context *ctx ); +void _ui_textbox_clipboard_paste( ui_context *ctx ); + +int ui_textbox( ui_context *ctx, ui_rect inout_panel, const char *label, char *buf, u32 len, u32 lines, u32 flags, struct ui_textbox_callbacks *callbacks ); -void ui_tabs( ui_rect inout_panel, ui_rect out_content_panel, +void ui_tabs( ui_context *ctx, ui_rect inout_panel, ui_rect out_content_panel, const char **titles, u32 count, i32 *page ); -void ui_start_modal( const char *message, u32 options ); -#ifdef VG_ANDROID -#else -void ui_proc_key( SDL_Keysym ev ); -#endif -void ui_proc_utf8( const char *text ); -void ui_dev_colourview(void); -void ui_post_update(void); -void ui_set_mouse_pos( ui_px x, ui_px y ); -void vg_ui_init(void); +void ui_defocus_all( ui_context *ctx ); + +void ui_start_modal( ui_context *ctx, const char *message, u32 options ); -const vg_font_face *ui_current_font(void); -ui_context *ui_current_context(void); +void ui_proc_utf8( ui_context *ctx, const char *text ); +void ui_dev_colourview( ui_context *ctx ); extern vg_font_sheet vg_default_font_sheet; extern vg_font_face vgf_default_small, vgf_default_large, vgf_default_title; -void ui_set_screen( i32 width, i32 height ); -extern ui_context *g_ui_ctx; diff --git a/vg_ui/imgui_impl_opengl.c b/vg_ui/imgui_impl_opengl.c index 5e43e14..2e0bdc6 100644 --- a/vg_ui/imgui_impl_opengl.c +++ b/vg_ui/imgui_impl_opengl.c @@ -1,5 +1,7 @@ #include "vg_opengl.h" #include "vg_shader.h" +#include "vg_ui/imgui.h" +#include "vg_engine.h" struct { @@ -7,13 +9,20 @@ struct m3x3f pv; ui_context ctx; GLuint tex_glyphs; + v2f inverse_font_sheet; #ifdef VG_ANDROID #else SDL_Cursor *cursor_map[ k_ui_cursor_max ]; #endif + + /* at some point this should be implementation specific? */ + v4f colour; + f32 frosting; + v2f bg_inverse_ratio; + GLuint tex_bg; } -static opengl_ui = +static vg_ui = { .ctx = { @@ -55,12 +64,12 @@ static opengl_ui = [ k_ui_gray + k_ui_brighter ] = UI_RGB( 0xa89984 ), }, .font = &vgf_default_small, - .colour = {1.0f,1.0f,1.0f,1.0f}, - .bg_inverse_ratio = {1,1}, .scale = 1, .padding = 8, .widget_height = 28 - } + }, + .colour = {1.0f,1.0f,1.0f,1.0f}, + .bg_inverse_ratio = {1,1}, }; #if 0 @@ -265,13 +274,198 @@ static struct vg_shader _shader_ui_hsv = { }; #endif -void ui_set_screen( i32 width, i32 height ) +void vg_ui_set_screen( i32 width, i32 height ) { - m3x3_identity( opengl_ui.pv ); - m3x3_translate( opengl_ui.pv, (v3f){ -1.0f, 1.0f, 0.0f } ); - m3x3_scale( opengl_ui.pv, (v3f){ 1.0f/((f32)width*0.5f), + ui_set_area( &vg_ui.ctx, width, height ); + m3x3_identity( vg_ui.pv ); + m3x3_translate( vg_ui.pv, (v3f){ -1.0f, 1.0f, 0.0f } ); + m3x3_scale( vg_ui.pv, (v3f){ 1.0f/((f32)width*0.5f), -1.0f/((f32)height*0.5f), 1.0f } ); } +void ui_impl_render_batch( ui_context *ctx, ui_batch *batch, + enum ui_shader shader, void *shader_data ) +{ + glBindVertexArray( vg_ui.vao ); + glBindBuffer( GL_ARRAY_BUFFER, vg_ui.vbo ); + glBufferSubData( GL_ARRAY_BUFFER, + batch->vert_offset, + batch->vert_count*sizeof(ui_vert), + batch->vert_buf ); + + glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, vg_ui.ebo ); + glBufferSubData( GL_ELEMENT_ARRAY_BUFFER, + batch->indice_offset, + batch->indice_count*sizeof(u16), + batch->indice_buf ); + + glDisable( GL_DEPTH_TEST ); + glDisable( GL_CULL_FACE ); + glEnable( GL_BLEND ); + glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ); + glBlendEquation( GL_FUNC_ADD ); + + if( shader == k_ui_shader_colour ) + { + glUseProgram( _shader_ui.id ); + glUniformMatrix3fv( glGetUniformLocation( _shader_ui.id, "uPv" ), 1, + GL_FALSE, (float *)vg_ui.pv ); + glUniform4fv( glGetUniformLocation( _shader_ui.id, "uColour" ), 1, + vg_ui.colour ); + + glActiveTexture( GL_TEXTURE0 ); + glBindTexture( GL_TEXTURE_2D, vg_ui.tex_glyphs ); + glUniform1i( glGetUniformLocation( _shader_ui.id, "uTexGlyphs" ), 0 ); + + glActiveTexture( GL_TEXTURE1 ); + glBindTexture( GL_TEXTURE_2D, vg_ui.tex_bg ); + glUniform1i( glGetUniformLocation( _shader_ui.id, "uTexBG" ), 1 ); + glUniform1f( glGetUniformLocation( _shader_ui.id, "uSpread" ), + vg_ui.frosting ); + glUniform2fv( glGetUniformLocation( _shader_ui.id, "uBGInverseRatio" ), + 1, vg_ui.bg_inverse_ratio ); + glUniform2fv( glGetUniformLocation( _shader_ui.id, "uInverseFontSheet" ), + 1, vg_ui.inverse_font_sheet ); + } + else if( shader == k_ui_shader_image ) + { + glUseProgram( _shader_ui_image.id ); + glUniformMatrix3fv( glGetUniformLocation( _shader_ui_image.id, "uPv" ), 1, + GL_FALSE, (float *)vg_ui.pv ); + glUniform1i( glGetUniformLocation(_shader_ui_image.id,"uTexImage"), 0 ); + glUniform4fv( glGetUniformLocation( _shader_ui_image.id, "uColour" ), 1, + vg_ui.colour ); + + struct ui_batch_shader_data_image *inf = shader_data; + GLuint *image = inf->resource; + + glActiveTexture( GL_TEXTURE0 ); + glBindTexture( GL_TEXTURE_2D, *image ); + } + else if( shader == k_ui_shader_hsv ) + { + struct ui_batch_shader_data_hsv *inf = shader_data; + glUseProgram( _shader_ui_hsv.id ); + glUniformMatrix3fv( glGetUniformLocation( _shader_ui_hsv.id, "uPv" ), 1, + GL_FALSE, (float *)vg_ui.pv ); + glUniform1f( glGetUniformLocation(_shader_ui_hsv.id,"uHue"), inf->hue ); + } + else + vg_fatal_error( "Invalid UI shader (%d)\n", shader ); + + glDrawElements( GL_TRIANGLES, batch->indice_count, GL_UNSIGNED_SHORT, + (void *)((size_t)batch->indice_offset) ); +} + +void vg_ui_post_update(void) +{ +#ifdef VG_ANDROID +#else + if( vg_ui.ctx.wants_mouse ) + { + SDL_SetWindowGrab( vg.window, SDL_FALSE ); + SDL_SetRelativeMouseMode( SDL_FALSE ); + } + else + { + SDL_SetWindowGrab( vg.window, SDL_TRUE ); + SDL_SetRelativeMouseMode( SDL_TRUE ); + } + + SDL_SetCursor( vg_ui.cursor_map[ vg_ui.ctx.cursor ] ); + SDL_ShowCursor(1); +#endif +} + +void vg_ui_set_mouse_pos( ui_px x, ui_px y ) +{ +#ifdef VG_ANDROID +#else + SDL_WarpMouseInWindow( vg.window, x, y ); + vg_ui.ctx.mouse[0] = x; + vg_ui.ctx.mouse[1] = y; + vg_ui.ctx.mouse_pos_overriden = 1; +#endif +} + +#ifdef VG_ANDROID +#else +void vg_ui_handle_sdl_key( ui_context *ctx, SDL_Keysym ev ) +{ + if( ctx->focused_control_type != k_ui_control_textbox ) + { + return; + } + + struct textbox_mapping + { + u16 mod; + SDL_Keycode key; + + void (*handler)( ui_context *ctx ); + } + mappings[] = + { + { 0, SDLK_LEFT, _ui_textbox_left }, + { KMOD_SHIFT, SDLK_LEFT, _ui_textbox_left_select }, + { 0, SDLK_RIGHT, _ui_textbox_right }, + { KMOD_SHIFT, SDLK_RIGHT, _ui_textbox_right_select }, + { 0, SDLK_DOWN, _ui_textbox_down }, + { 0, SDLK_UP, _ui_textbox_up }, + { 0, SDLK_BACKSPACE, _ui_textbox_backspace }, + { KMOD_SHIFT, SDLK_BACKSPACE, _ui_textbox_backspace }, + { KMOD_CTRL, SDLK_BACKSPACE, _ui_textbox_backspace }, + { 0, SDLK_DELETE, _ui_textbox_delete }, + { 0, SDLK_HOME, _ui_textbox_home }, + { KMOD_SHIFT, SDLK_HOME, _ui_textbox_home_select }, + { 0, SDLK_END, _ui_textbox_end }, + { KMOD_SHIFT, SDLK_END, _ui_textbox_end_select }, + { KMOD_CTRL, SDLK_a, _ui_textbox_select_all }, + { KMOD_CTRL, SDLK_c, _ui_textbox_to_clipboard }, + { KMOD_CTRL, SDLK_x, _ui_textbox_cut }, + { KMOD_CTRL, SDLK_v, _ui_textbox_clipboard_paste }, + { 0, SDLK_RETURN, _ui_textbox_enter }, + { 0, SDLK_ESCAPE, ui_defocus_all }, + }; + + SDL_Keymod mod = 0; + + if( ev.mod & KMOD_SHIFT ) + mod |= KMOD_SHIFT; + + if( ev.mod & KMOD_CTRL ) + mod |= KMOD_CTRL; + + if( ev.mod & KMOD_ALT ) + mod |= KMOD_ALT; + + for( int i=0; ikey == ev.sym ) + { + if( mapping->mod == 0 ) + { + if( mod == 0 ) + { + mapping->handler( ctx ); + return; + } + } + else if( (mod & mapping->mod) == mapping->mod ) + { + mapping->handler( ctx ); + return; + } + } + } +} + +bool _wrap_sdl_hasclipboard_text(void) +{ + return SDL_HasClipboardText(); +} +#endif void vg_ui_init(void) { @@ -283,20 +477,33 @@ void vg_ui_init(void) } u32 verts = 30000, indices = 20000; - ui_bind_context( &opengl_ui.ctx ); - ui_init( malloc( sizeof(ui_vert)*verts ), verts, + ui_init( &vg_ui.ctx, + malloc( sizeof(ui_vert)*verts ), verts, malloc( sizeof(u16)*indices ), indices ); + /* callbacks */ + vg_ui.ctx.render_batch = ui_impl_render_batch; + +#ifdef VG_ANDROID +#else + vg_ui.ctx.have_clipboard_text = _wrap_sdl_hasclipboard_text; + vg_ui.ctx.get_clipboard_text = SDL_GetClipboardText; + vg_ui.ctx.free_clipboard_text = SDL_free; + vg_ui.ctx.set_clipboard_text = SDL_SetClipboardText; + vg_ui.ctx.start_text_input = SDL_StartTextInput; + vg_ui.ctx.stop_text_input = SDL_StopTextInput; +#endif + /* Generate the buffer we are gonna be drawing to */ - glGenVertexArrays( 1, &opengl_ui.vao ); - glGenBuffers( 1, &opengl_ui.vbo ); - glGenBuffers( 1, &opengl_ui.ebo ); + glGenVertexArrays( 1, &vg_ui.vao ); + glGenBuffers( 1, &vg_ui.vbo ); + glGenBuffers( 1, &vg_ui.ebo ); - glBindVertexArray( opengl_ui.vao ); - glBindBuffer( GL_ARRAY_BUFFER, opengl_ui.vbo ); + glBindVertexArray( vg_ui.vao ); + glBindBuffer( GL_ARRAY_BUFFER, vg_ui.vbo ); glBufferData( GL_ARRAY_BUFFER, verts*sizeof(ui_vert), NULL, GL_DYNAMIC_DRAW ); - glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, opengl_ui.ebo ); + glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, vg_ui.ebo ); glBufferData( GL_ELEMENT_ARRAY_BUFFER, indices*sizeof(u16), NULL, GL_DYNAMIC_DRAW ); @@ -320,11 +527,11 @@ void vg_ui_init(void) #ifdef VG_ANDROID #else - opengl_ui.cursor_map[ k_ui_cursor_default ] = + vg_ui.cursor_map[ k_ui_cursor_default ] = SDL_CreateSystemCursor( SDL_SYSTEM_CURSOR_ARROW ); - opengl_ui.cursor_map[ k_ui_cursor_hand ] = + vg_ui.cursor_map[ k_ui_cursor_hand ] = SDL_CreateSystemCursor( SDL_SYSTEM_CURSOR_HAND ); - opengl_ui.cursor_map[ k_ui_cursor_ibeam ] = + vg_ui.cursor_map[ k_ui_cursor_ibeam ] = SDL_CreateSystemCursor( SDL_SYSTEM_CURSOR_IBEAM ); #endif @@ -357,11 +564,11 @@ void vg_ui_init(void) data++; } - opengl_ui.ctx.inverse_font_sheet[0] = 1.0/(f64)sheet->w; - opengl_ui.ctx.inverse_font_sheet[1] = 1.0/(f64)sheet->h; + vg_ui.inverse_font_sheet[0] = 1.0/(f64)sheet->w; + vg_ui.inverse_font_sheet[1] = 1.0/(f64)sheet->h; - glGenTextures( 1, &opengl_ui.tex_glyphs ); - glBindTexture( GL_TEXTURE_2D, opengl_ui.tex_glyphs ); + glGenTextures( 1, &vg_ui.tex_glyphs ); + glBindTexture( GL_TEXTURE_2D, vg_ui.tex_glyphs ); glTexImage2D( GL_TEXTURE_2D, 0, GL_R8, sheet->w, sheet->h, 0, GL_RED, GL_UNSIGNED_BYTE, image ); @@ -375,105 +582,8 @@ void vg_ui_init(void) void ui_free(void) { vg_free_shader( &_shader_ui ); - ui_bind_context( NULL ); - free( opengl_ui.ctx.indice_buffer ); - free( opengl_ui.ctx.vertex_buffer ); -} - -void ui_impl_render_batch( ui_batch *batch ) -{ - glBindVertexArray( opengl_ui.vao ); - glBindBuffer( GL_ARRAY_BUFFER, opengl_ui.vbo ); - glBufferSubData( GL_ARRAY_BUFFER, - batch->vert_offset, - batch->vert_count*sizeof(ui_vert), - batch->vert_buf ); - - glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, opengl_ui.ebo ); - glBufferSubData( GL_ELEMENT_ARRAY_BUFFER, - batch->indice_offset, - batch->indice_count*sizeof(u16), - batch->indice_buf ); - - glDisable( GL_DEPTH_TEST ); - glDisable( GL_CULL_FACE ); - glEnable( GL_BLEND ); - glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ); - glBlendEquation( GL_FUNC_ADD ); - - if( batch->shader == k_ui_shader_colour ) - { - glUseProgram( _shader_ui.id ); - glUniformMatrix3fv( glGetUniformLocation( _shader_ui.id, "uPv" ), 1, - GL_FALSE, (float *)opengl_ui.pv ); - glUniform4fv( glGetUniformLocation( _shader_ui.id, "uColour" ), 1, - g_ui_ctx->colour ); - - glActiveTexture( GL_TEXTURE0 ); - glBindTexture( GL_TEXTURE_2D, opengl_ui.tex_glyphs ); - glUniform1i( glGetUniformLocation( _shader_ui.id, "uTexGlyphs" ), 0 ); - - glActiveTexture( GL_TEXTURE1 ); - glBindTexture( GL_TEXTURE_2D, g_ui_ctx->tex_bg ); - glUniform1i( glGetUniformLocation( _shader_ui.id, "uTexBG" ), 1 ); - glUniform1f( glGetUniformLocation( _shader_ui.id, "uSpread" ), - g_ui_ctx->frosting ); - glUniform2fv( glGetUniformLocation( _shader_ui.id, "uBGInverseRatio" ), - 1, g_ui_ctx->bg_inverse_ratio ); - glUniform2fv( glGetUniformLocation( _shader_ui.id, "uInverseFontSheet" ), - 1, g_ui_ctx->inverse_font_sheet ); - } - else if( batch->shader == k_ui_shader_image ) - { - glUseProgram( _shader_ui_image.id ); - glUniformMatrix3fv( glGetUniformLocation( _shader_ui_image.id, "uPv" ), 1, - GL_FALSE, (float *)opengl_ui.pv ); - glUniform1i( glGetUniformLocation(_shader_ui_image.id,"uTexImage"), 0 ); - glUniform4fv( glGetUniformLocation( _shader_ui_image.id, "uColour" ), 1, - g_ui_ctx->colour ); - } - else if( batch->shader == k_ui_shader_hsv ) - { - glUseProgram( _shader_ui_hsv.id ); - glUniformMatrix3fv( glGetUniformLocation( _shader_ui_hsv.id, "uPv" ), 1, - GL_FALSE, (float *)opengl_ui.pv ); - glUniform1f( glGetUniformLocation(_shader_ui_hsv.id,"uHue"), - g_ui_ctx->hue ); - } - else - vg_fatal_error( "Invalid UI shader (%d)\n", batch->shader ); - - glDrawElements( GL_TRIANGLES, batch->indice_count, GL_UNSIGNED_SHORT, - (void *)((size_t)batch->indice_offset) ); -} - -void ui_post_update(void) -{ -#ifdef VG_ANDROID -#else - if( g_ui_ctx->wants_mouse ) - { - SDL_SetWindowGrab( vg.window, SDL_FALSE ); - SDL_SetRelativeMouseMode( SDL_FALSE ); - } - else - { - SDL_SetWindowGrab( vg.window, SDL_TRUE ); - SDL_SetRelativeMouseMode( SDL_TRUE ); - } - - SDL_SetCursor( opengl_ui.cursor_map[ g_ui_ctx->cursor ] ); - SDL_ShowCursor(1); -#endif -} - -void ui_set_mouse_pos( ui_px x, ui_px y ) -{ -#ifdef VG_ANDROID -#else - SDL_WarpMouseInWindow( vg.window, x, y ); - g_ui_ctx->mouse[0] = x; - g_ui_ctx->mouse[1] = y; - g_ui_ctx->mouse_pos_overriden = 1; -#endif + vg_free_shader( &_shader_ui_hsv ); + vg_free_shader( &_shader_ui_image ); + free( vg_ui.ctx.indice_buffer ); + free( vg_ui.ctx.vertex_buffer ); }