#include "vg_log.h"
#include <string.h>
-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;
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; i<vg_list_size(mesh); i++ )
indices[i] = start+mesh[i];
- g_ui_ctx->cur_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++ )
{
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 )
}
}
- 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 )
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;
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 )
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;
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;
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;
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;
}
}
-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 )
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;
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 ) )
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' )
_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;
}
}
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 );
}
/*
* -----------------------------------------------------------------------------
*/
-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;
}
if( hover )
- g_ui_ctx->cursor = k_ui_cursor_hand;
+ ctx->cursor = k_ui_cursor_hand;
if( click )
{
{
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;
}
/* 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 );
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 );
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 );
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 );
}
}
* -----------------------------------------------------------------------------
*/
-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);
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;
* 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; i<len; i ++ ){
- if( *value == options[i].value ){
+ for( u32 i=0; i<len; i ++ )
+ {
+ if( *value == options[i].value )
+ {
display = options[i].alias;
valid = 1;
break;
}
}
- if( ui_button_text( box, display, g_ui_ctx->scale ) == 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<g_ui_ctx->_enum.option_count; i++ ){
+ for( u32 i=0; i<ctx->_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;
}
/*
* -----------------------------------------------------------------------------
*/
-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
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;
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 );
}
/*
* -----------------------------------------------------------------------------
*/
-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];
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",
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
{
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 );
}
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,
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 ) );
}
/*
* -----------------------------------------------------------------------------
*/
-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 );
}
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;
}
}
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 --;
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 );
}
}
}
*
* 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;
* 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;
}
*
* 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;
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;
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;
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 );
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;
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;
}
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 );
}
}
{
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;
}
* -----------------------------------------------------------------------------
*/
-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;
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; i<count; i++ )
if( i == cur_page )
{
colour = k_ui_bg+2;
- ui_outline( button, 1, ui_colour( k_ui_bg+5 ),
+ ui_outline( ctx, button, 1, ui_colour(ctx, k_ui_bg+5 ),
UI_TOP|UI_LEFT|UI_RIGHT );
button[3] ++;
}
- if( ui_colourbutton_text( button, titles[i], 1, colour ) == 1 )
+ if( ui_colourbutton_text( ctx, button, titles[i], 1, colour ) == 1 )
*page = i;
}
}
* Modal UI
* -----------------------------------------------------------------------------
*/
-void ui_start_modal( const char *message, u32 options )
+void ui_start_modal( ui_context *ctx, const char *message, u32 options )
{
- g_ui_ctx->focused_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 );
* -----------------------------------------------------------------------------
*/
-/*
- * 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; i<vg_list_size( mappings ); i++ ){
- struct textbox_mapping *mapping = &mappings[i];
-
- if( mapping->key == 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 );
}
}
* -----------------------------------------------------------------------------
*/
-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",
ui_rect col[2];
ui_split_ratio( window, k_ui_axis_v, 0.5f, 0, col[0], col[1] );
- for( int i=0; i<vg_list_size(g_ui_ctx->scheme); i++ ){
+ for( int i=0; i<vg_list_size(ctx->scheme); 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));
+ }
}
}