-static u32 ui_colourcont( 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 );
-}
-
-static void ui_hex_to_norm( u32 hex, v4f norm )
-{
- norm[0] = ((hex ) & 0xff);
- norm[1] = ((hex>>8 ) & 0xff);
- norm[2] = ((hex>>16) & 0xff);
- norm[3] = ((hex>>24) & 0xff);
- v4_muls( norm, 1.0f/255.0f, norm );
-}
-
-static u32 ui_ntext( ui_rect rect, const char *str, u32 len, ui_px scale,
- enum ui_align align, u32 colour )
-{
- ui_rect text_cursor;
- if( colour == 0 ) colour = ui_colour( k_ui_fg );
-
- colour &= 0x00ffffff;
-
- const char *_c = str;
- u8 c;
-
- text_cursor[0] = ui_text_aligned_x( str, rect, scale, align );
- text_cursor[1] = rect[1];
- text_cursor[2] = 8*scale;
- text_cursor[3] = 14*scale;
-
- u32 printed_chars = 0;
-
- if( align & (k_ui_align_middle|k_ui_align_bottom) ){
- ui_px height = ui_text_string_height( str ) * scale;
-
- if( align & k_ui_align_bottom )
- text_cursor[1] += rect[3]-height;
- else
- text_cursor[1] += (rect[3]-height)/2;
- }
-
- while( (c = *(_c ++)) ){
- if( printed_chars >= len ){
- printed_chars = 0;
- text_cursor[1] += 14*scale;
- text_cursor[0] = ui_text_aligned_x( _c, rect, scale, align );
- text_cursor[0] -= UI_GLYPH_SPACING_X*scale;
-
- u8 glyph_base[2];
- u8 glyph_index = '\xb6';
- glyph_base[0] = glyph_index & 0xf;
- glyph_base[1] = (glyph_index-glyph_base[0])>>4;
- glyph_base[0] *= 8;
- glyph_base[1] *= 8;
-
- ui_fill_rect( text_cursor, 0x00ffffff, (ui_px[4])
- {
- glyph_base[0]+2,
- glyph_base[1]+1,
- glyph_base[0]+6,
- glyph_base[1]+8
- });
-
- text_cursor[0] += UI_GLYPH_SPACING_X*scale;
- }
-
- if( c == '\n' ){
- text_cursor[1] += 14*scale;
- text_cursor[0] = ui_text_aligned_x( _c, rect, scale, align );
- printed_chars = 0;
- continue;
- }
- else if( c >= 33 ){
- u8 glyph_base[2];
- u8 glyph_index = c;
- glyph_base[0] = glyph_index & 0xf;
- glyph_base[1] = (glyph_index-glyph_base[0])>>4;
- glyph_base[0] *= 8;
- glyph_base[1] *= 8;
-
- ui_rect rect_char;
-
- if( ui_clip( rect, text_cursor, rect_char ) ){
- ui_fill_rect( rect_char, colour, (ui_px[4])
- {
- glyph_base[0]+2,
- glyph_base[1]+1,
- glyph_base[0]+6,
- glyph_base[1]+8
- });
- }
- }
- else if( c == '\x1B' ){
- /* vt codes */
- _c ++;
- u16 colour_id = 0;
- for( int i=0; i<3; i ++ ){
- if( _c[i] ){
- if( _c[i] == 'm' ){
- _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 '3'|'7'<<8: colour = 0xffffffff; break;
- }
-
- colour &= 0x00ffffff;
- break;
- }
-
- colour_id |= _c[i] << (i*8);
- }
- else{
- _c = _c +i;
- break;
- }
- }
-
- continue;
- }
- else if( c == '\t' ){
- text_cursor[0] += UI_GLYPH_SPACING_X*scale*4;
- printed_chars += 4;
- continue;
- }
-
- text_cursor[0] += UI_GLYPH_SPACING_X*scale;
- printed_chars ++;
- }
-
- return printed_chars;
-}
-
-static void ui_text( ui_rect rect, const char *str, ui_px scale,
- enum ui_align align, u32 colour )
-{
- ui_ntext( rect, str, 1024, scale, align, colour );
-}
-
-static void ui_image( ui_rect rect, GLuint image )
-{
- ui_flush( k_ui_shader_colour );
- glActiveTexture( GL_TEXTURE0 );
- glBindTexture( GL_TEXTURE_2D, image );
- ui_fill_rect( rect, 0xffffffff, (ui_px[4]){ 0,128, 128, 0 } );
- ui_flush( k_ui_shader_image );
-}
-
-static u32 v4f_u32_colour( v4f colour )
-{
- u32 r = colour[0] * 255.0f,
- g = colour[1] * 255.0f,
- b = colour[2] * 255.0f,
- a = colour[3] * 255.0f;
-
- return r | (g<<8) | (b<<16) | (a<<24);
-}
-
-static void ui_defocus_all(void)
-{
- if( vg_ui.focused_control_type == k_ui_control_textbox ){
- SDL_StopTextInput();
- }
-
- vg_ui.focused_control_id = NULL;
- vg_ui.focused_control_hit = 0;
- vg_ui.focused_control_type = k_ui_control_none;
-}
-
-static int ui_colourbutton( ui_rect rect, enum ui_scheme_colour colour )
-{
- int clickup= ui_click_up(UI_MOUSE_LEFT),
- click = ui_clicking(UI_MOUSE_LEFT) | clickup,
- target = ui_inside_rect( rect, vg_ui.mouse_click ) && click,
- hover = ui_inside_rect( rect, vg_ui.mouse );
-
- u32 col_base = vg_ui.scheme[ colour ],
- col_highlight = vg_ui.scheme[ k_ui_fg ],
- col_hover = vg_ui.scheme[ colour + k_ui_brighter ];
-
- if( vg_ui.focused_control_type != k_ui_control_none ){
- clickup = 0;
- click = 0;
- target = 0;
- hover = 0;
- }
-
- if( hover ){
- vg_ui.cursor = k_ui_cursor_hand;
- }
-
- if( click ){
- if( target ){
- if( hover ){
- if( clickup ){
- ui_fill( rect, col_highlight );
- vg_ui.ignore_input_frames = 2;
- rect_copy( rect, vg_ui.click_fader );
-
- rect_copy( rect, vg_ui.click_fader_end );
- vg_ui.click_fader_end[3] = 0;
- ui_rect_center( rect, vg_ui.click_fader_end );
-
- vg_ui.click_fade_opacity = 1.0f;
- ui_defocus_all();
- return 1;
- }
- else{
- ui_fill( rect, col_highlight );
- return 0;
- }
- }
- else{
- ui_fill( rect, col_base );
- ui_outline( rect, 1, col_highlight );
- return 0;
- }
- }
- else{
- ui_fill( rect, col_base );
- return 0;
- }
- }
- else{
- if( hover ){
- ui_fill( rect, col_hover );
- return 0;
- }
- else{
- ui_fill( rect, col_base );
- return 0;
- }
- }
-}
-
-static int ui_colourbutton_text( ui_rect rect, const char *string, ui_px scale,
- enum ui_scheme_colour colour )
-{
- int result = ui_colourbutton( rect, colour );
- ui_rect t = { 0,0, ui_text_line_width( string )*scale, 14*scale };
- ui_rect_center( rect, t );
- ui_text( t, string, scale, k_ui_align_left, ui_colourcont(colour) );
- return result;
-}
-
-static int ui_button_text( ui_rect rect, const char *string, ui_px scale )
-{
- return ui_colourbutton_text( rect, string, scale, k_ui_bg+4 );
-}
-
-static void ui_enum_post(void);
-static void ui_postrender(void)
-{
- if( vg_ui.click_fade_opacity > 0.0f ){
- float scale = vg_ui.click_fade_opacity;
- scale = vg_maxf( 1.0f/255.0f, scale*scale );
-
- vg_ui.click_fade_opacity -= vg.time_frame_delta * 3.8f;
- u32 colour = (0x00ffffff & ui_colour(k_ui_fg))|0x7f000000;
-
- v4f begin, end, dest;
- for( int i=0; i<4; i++ ){
- begin[i] = vg_ui.click_fader[i];
- end[i] = vg_ui.click_fader_end[i]+1;
- }
-
- v4_lerp( end, begin, scale, dest );
-
- ui_rect rect;
- for( int i=0; i<4; i++ ){
- rect[i] = dest[i];
- }
-
- ui_fill( rect, colour );
- }
-
- if( vg_ui.focused_control_type == k_ui_control_dropdown ){
- ui_enum_post();
- }
- else if( vg_ui.focused_control_type == k_ui_control_modal ){
- ui_rect screen = {0,0,vg.window_x,vg.window_y};
- ui_fill( screen, 0xa0000000 );
- ui_rect box = {0,0,400,200};
-
- u32 colour = ui_colour(k_ui_fg),
- type = vg_ui.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);
-
- ui_rect_center( screen, box );
- ui_fill( box, ui_colour(k_ui_bg) );
- ui_outline( box, -1, colour );
-
- ui_rect message;
- rect_copy( box, message );
- message[3] = 100;
- ui_rect_center( box, message );
-
- ui_rect row0, row1, btn;
- ui_split_ratio( message, k_ui_axis_h, 0.5f, 0, row0, row1 );
- row0[0] += UI_GLYPH_SPACING_X;
- ui_ntext( row0, vg_ui.modal.message, (box[2]/UI_GLYPH_SPACING_X)-2, 1,
- k_ui_align_left, colour );
-
- rect_copy( row1, btn );
- btn[2] = 86;
- btn[3] = 28;
- ui_rect_center( row1, btn );
-
- vg_ui.focused_control_type = k_ui_control_none; /* HACK */
- if( !ui_button_text( btn, "OK", 1 ) )
- vg_ui.focused_control_hit = 1;
- vg_ui.focused_control_type = k_ui_control_modal; /* HACK */
- vg_ui.wants_mouse = 1;
- }
-
- ui_flush( k_ui_shader_colour );
-
- if( !vg_ui.focused_control_hit ){
- ui_defocus_all();
- }
-
- if( vg_ui.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.cursor ] );
- SDL_ShowCursor(1);
-}
-
-static void ui_dev_colourview(void)
-{
- ui_rect window = {vg.window_x-256,0,256,vg.window_y}, swatch;
-
- const char *names[vg_list_size(vg_ui.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",
-
- [k_ui_fg] = "k_ui_fg", "k_ui_fg+1", "k_ui_fg+2", "k_ui_fg+3",
- "k_ui_fg+4", "k_ui_fg+5", "k_ui_fg+6", "k_ui_fg+7",
-
- [k_ui_red] = "k_ui_red", "k_ui_orange", "k_ui_yellow", "k_ui_green",
- "k_ui_aqua", "k_ui_blue", "k_ui_purple", "k_ui_gray",
- "k_ui_red+8","k_ui_orange+8","k_ui_yellow+8","k_ui_green+8",
- "k_ui_aqua+8","k_ui_blue+8","k_ui_purple+8","k_ui_gray+8" };
-
- 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(vg_ui.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) );
-
- if( names[i] )
- ui_text(swatch, names[i], 1, k_ui_align_middle_left, ui_colourcont(i));
- }
-}
-
-/*
- * checkbox
- * -----------------------------------------------------------------------------
- */
-
-static int ui_checkbox( ui_rect rect, const char *str_label, int *data )
-{
- ui_rect label, box;
- ui_split( rect, k_ui_axis_v, -rect[3], 0, label, box );
- ui_text( label, str_label, 1, k_ui_align_middle_left, 0 );
-
- int changed = ui_colourbutton( box, k_ui_bg );
- if( changed )
- *data = (*data) ^ 0x1;
-
- if( *data ){
- ui_rect_pad( box, (ui_px[2]){4,4} );
- ui_fill( box, ui_colour( k_ui_orange ) );
- }
-
- return changed;
-}
-
-/*
- * Dropdown / Enum
- * -----------------------------------------------------------------------------
- */
-
-/*
- * unfortunately no return value since we only find out that event in the
- * postrender step.
- */
-static void ui_enum( ui_rect rect, const char *str_label,
- struct ui_dropdown_opt *options, u32 len,
- struct ui_dropdown_value *value )
-{
- ui_rect label, box;
- ui_split( rect, k_ui_axis_v, ui_text_line_width( str_label ) + 8,
- 0, label, box );
- ui_text( label, str_label, 1, k_ui_align_middle_left, 0 );
-
- if( ui_button_text( box, options[ value->index ].alias, 1 ) ){
- vg_ui.focused_control_type = k_ui_control_dropdown;
- vg_ui.ptr_dropdown = value;
- vg_ui.dropdown.option_count = len;
- vg_ui.dropdown.options = options;
- rect_copy( box, vg_ui.dropdown.rect );
- }
-}
-
-static void ui_enum_post(void)
-{
- ui_rect drawer;
- rect_copy( vg_ui.dropdown.rect, drawer );
- drawer[3] *= vg_ui.dropdown.option_count;
-
- int close = 0;
- int clickany= ui_click_up(UI_MOUSE_LEFT|UI_MOUSE_RIGHT|UI_MOUSE_MIDDLE),
- hover = ui_inside_rect( drawer, vg_ui.mouse );
-
- if( clickany && !hover ){
- return;
- }
-
- /* HACK */
- vg_ui.focused_control_type = k_ui_control_none;
- struct ui_dropdown_value *value = vg_ui.ptr_dropdown;
-
- for( u32 i=0; i<vg_ui.dropdown.option_count; i++ ){
- ui_rect button;
- ui_split( drawer, k_ui_axis_h, vg_ui.dropdown.rect[3], 0, button,drawer );
-
- enum ui_scheme_colour colour = k_ui_bg+3;
- if( i == value->index ) colour = k_ui_orange;
-
- if( ui_colourbutton_text( button, vg_ui.dropdown.options[i].alias, 1,
- colour ) ){
- value->index = i;
- value->value = vg_ui.dropdown.options[i].value;
- close = 1;
- }
- }
-
- /* HACK */
- vg_ui.focused_control_type = k_ui_control_dropdown;
-
- if( !close )
- vg_ui.focused_control_hit = 1;
-}
-
-/*
- * Textbox chaos
- * -----------------------------------------------------------------------------
- */
-
-static void _ui_textbox_make_selection( int *start, int *end )
-{
- *start = VG_MIN( vg_ui.textbox.cursor_pos, vg_ui.textbox.cursor_user );
- *end = VG_MAX( vg_ui.textbox.cursor_pos, vg_ui.textbox.cursor_user );
-}
-
-static void _ui_textbox_move_cursor( int *cursor0, int *cursor1,
- int dir, int snap_together )
-{
- *cursor0 = VG_MAX( 0, vg_ui.textbox.cursor_user + dir );
- *cursor0 =
- VG_MIN(
- VG_MIN( vg_ui.textbox.len-1, strlen( vg_ui.textbuf )),
- *cursor0 );
-
- if( snap_together )
- *cursor1 = *cursor0;
-}
-
-static int _ui_textbox_makeroom( int datastart, int length )
-{
- int move_to = VG_MIN( datastart+length, vg_ui.textbox.len-1 );
- int move_amount = strlen( vg_ui.textbuf )-datastart;
- int move_end = VG_MIN( move_to+move_amount, vg_ui.textbox.len-1 );
- move_amount = move_end-move_to;
-
- if( move_amount )
- memmove( &vg_ui.textbuf[ move_to ],
- &vg_ui.textbuf[ datastart ],
- move_end-move_to );
-
- vg_ui.textbuf[ move_end ] = '\0';
-
- return VG_MIN( length, vg_ui.textbox.len-datastart-1 );
-}
-
-static int _ui_textbox_delete_char( int direction )
-{
- int start, end;
- _ui_textbox_make_selection( &start, &end );
-
- /* There is no selection */
- if( !(end-start) ){
- if( direction == 1 ) end = VG_MIN( end+1, strlen( vg_ui.textbuf ) );
- else if( direction == -1 ) start = VG_MAX( start-1, 0 );
- }
-
- /* Still no selction, no need to do anything */
- if( !(end-start) )
- return start;
-
- /* Copy the end->terminator to start */
- int remaining_length = strlen( vg_ui.textbuf )+1-end;
- memmove( &vg_ui.textbuf[ start ],
- &vg_ui.textbuf[ end ],
- remaining_length );
- return start;
-}
-
-static void _ui_textbox_to_clipboard(void)
-{
- int start, end;
- _ui_textbox_make_selection( &start, &end );
- char buffer[512];
-
- if( end-start ){
- memcpy( buffer, &vg_ui.textbuf[ start ], end-start );
- buffer[ end-start ] = 0x00;
- SDL_SetClipboardText( buffer );
- }
-}
-
-static void _ui_textbox_clipboard_paste(void)
-{
- if( !SDL_HasClipboardText() )
- return;
-
- char *text = SDL_GetClipboardText();
-
- if( !text )
- return;
-
- int datastart = _ui_textbox_delete_char( 0 );
- int length = strlen( text );
- int cpylength = _ui_textbox_makeroom( datastart, length );
-
- memcpy( vg_ui.textbuf + datastart, text, cpylength);
- _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user,
- &vg_ui.textbox.cursor_pos, cpylength, 1 );
- SDL_free( text );
-}
-
-static void _ui_textbox_put_char( char c )
-{
- vg_ui.textbox.cursor_user = _ui_textbox_delete_char(0);
-
- if( _ui_textbox_makeroom( vg_ui.textbox.cursor_user, 1 ) )
- vg_ui.textbuf[ vg_ui.textbox.cursor_user ] = c;
-
- _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user,
- &vg_ui.textbox.cursor_pos, 1, 1 );
-}
-
-/* Receed secondary cursor */
-static void _ui_textbox_left_select(void)
-{
- _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user, NULL, -1, 0 );
-}
-
-/* Match and receed both cursors */
-static void _ui_textbox_left(void)
-{
- int cursor_diff = vg_ui.textbox.cursor_pos - vg_ui.textbox.cursor_user? 0: 1;
-
- _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user,
- &vg_ui.textbox.cursor_pos, -cursor_diff, 1 );
-}
-
-static void _ui_textbox_up(void)
-{
- if( vg_ui.textbox.flags & UI_TEXTBOX_MULTILINE ){
- int line_begin = vg_ui.textbox.cursor_user;
-
- while( line_begin ){
- if( vg_ui.textbuf[ line_begin-1 ] == '\n' ){
- break;
- }
-
- line_begin --;
- }
-
- if( line_begin ){
- int line_above_begin = line_begin-1;
-
- while( line_above_begin ){
- if( vg_ui.textbuf[ line_above_begin-1 ] == '\n' ){
- break;
- }
-
- line_above_begin --;
- }
-
- int offset = vg_ui.textbox.cursor_user - line_begin,
- line_length_above = line_begin - line_above_begin -1;
-
- offset = VG_MIN( line_length_above, offset );
-
- vg_ui.textbox.cursor_user = line_above_begin+offset;
- vg_ui.textbox.cursor_pos = line_above_begin+offset;
- }
- else{
- vg_ui.textbox.cursor_user = line_begin;
- vg_ui.textbox.cursor_pos = line_begin;
- }
- }
- else{
- if( vg_ui.textbox.callbacks.up ){
- vg_ui.textbox.callbacks.up( vg_ui.textbuf, vg_ui.textbox.len );
- }
- }
-}
-
-static void _ui_textbox_down(void)
-{
- if( vg_ui.textbox.flags & UI_TEXTBOX_MULTILINE ){
- int line_begin = vg_ui.textbox.cursor_user;
-
- while( line_begin ){
- if( vg_ui.textbuf[ line_begin-1 ] == '\n' ){
- break;
- }
-
- line_begin --;
- }
-
- int line_below_begin = vg_ui.textbox.cursor_user;
-
- while(1){
- if( vg_ui.textbuf[ line_below_begin ] == '\0' ){
- vg_ui.textbox.cursor_user = line_below_begin;
- vg_ui.textbox.cursor_pos = line_below_begin;
- return;
- }
-
- if( vg_ui.textbuf[ line_below_begin ] == '\n' ){
- line_below_begin ++;
- break;
- }
-
- line_below_begin ++;
- }
-
- int line_below_end = line_below_begin;
- while(1){
- if( vg_ui.textbuf[ line_below_end ] == '\0' ||
- vg_ui.textbuf[ line_below_end ] == '\n' ){
- line_below_end ++;
- break;
- }
- line_below_end ++;
- }
-
- int offset = vg_ui.textbox.cursor_user - line_begin,
- line_length_below = line_below_end - line_below_begin -1;
-
- offset = VG_MIN( line_length_below, offset );
-
- vg_ui.textbox.cursor_user = line_below_begin+offset;
- vg_ui.textbox.cursor_pos = line_below_begin+offset;
- }
- else{
- if( vg_ui.textbox.callbacks.down ){
- vg_ui.textbox.callbacks.down( vg_ui.textbuf, vg_ui.textbox.len );
- }
- }
-}
-
-static void _ui_textbox_right_select(void)
-{
- _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user, NULL, 1, 0 );
-}
-
-static void _ui_textbox_right(void)
-{
- int cursor_diff = vg_ui.textbox.cursor_pos - vg_ui.textbox.cursor_user? 0: 1;
-
- _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user,
- &vg_ui.textbox.cursor_pos, +cursor_diff, 1 );
-}
-
-static void _ui_textbox_backspace(void)
-{
- if( vg_ui.focused_control_type == k_ui_control_textbox ){
- vg_ui.textbox.cursor_user = _ui_textbox_delete_char( -1 );
- vg_ui.textbox.cursor_pos = vg_ui.textbox.cursor_user;
-
- if( vg_ui.textbox.callbacks.change ){
- vg_ui.textbox.callbacks.change( vg_ui.textbuf, vg_ui.textbox.len );
- }
- }
-}
-
-static void _ui_textbox_delete(void)
-{
- if( vg_ui.focused_control_type == k_ui_control_textbox ){
- vg_ui.textbox.cursor_user = _ui_textbox_delete_char( 1 );
- vg_ui.textbox.cursor_pos = vg_ui.textbox.cursor_user;
-
- if( vg_ui.textbox.callbacks.change ){
- vg_ui.textbox.callbacks.change( vg_ui.textbuf, vg_ui.textbox.len );
- }
- }
-}
-
-static void _ui_textbox_home_select(void)
-{
- _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user, NULL, -10000, 0 );
-}
-
-static void _ui_textbox_home(void)
-{
- _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user,
- &vg_ui.textbox.cursor_pos, -10000, 1 );
-}
-
-static void _ui_textbox_end_select(void)
-{
- _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user, NULL, 10000, 0 );
-}
-
-static void _ui_textbox_end(void)
-{
- _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user,
- &vg_ui.textbox.cursor_pos,
- vg_ui.textbox.len-1, 1 );
-}
-
-static void _ui_textbox_select_all(void)
-{
- _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user, NULL, 10000, 0);
- _ui_textbox_move_cursor( &vg_ui.textbox.cursor_pos, NULL, -10000, 0);
-}
-
-static void _ui_textbox_cut(void)
-{
- _ui_textbox_to_clipboard();
- vg_ui.textbox.cursor_user = _ui_textbox_delete_char(0);
- vg_ui.textbox.cursor_pos = vg_ui.textbox.cursor_user;
-}
-
-static void _ui_textbox_enter(void)
-{
- if( vg_ui.focused_control_type == k_ui_control_textbox ){
- if( vg_ui.textbox.callbacks.enter )
- vg_ui.textbox.callbacks.enter( vg_ui.textbuf, vg_ui.textbox.len );
-
- if( vg_ui.focused_control_type != k_ui_control_textbox ) return;
-
- if( vg_ui.textbox.flags & UI_TEXTBOX_MULTILINE )
- _ui_textbox_put_char( '\n' );
- else{
- if( !(vg_ui.textbox.flags & UI_TEXTBOX_AUTOFOCUS ) )
- ui_defocus_all();
- }
- }
-}
-
-/*
- * based on a visual character coordinate relative to the anchor of the textbox,
- * this works out the linear place in the buffer that coordinate maps to
- *
- * 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 )
-{
- int i[3] = {0,0,0};
-
- char c;
- while( (c = vg_ui.textbuf[i[2]]) ){
- if( i[1]==co[1] && i[0]>=co[0] ) break;
-
- if( i[0] >= wrap_length ){
- i[1] ++;
- i[0] = 0;
- }
-
- if( c >= 32 && c <= 126 ){
- i[0] ++;
- i[2] ++;
- }
- else if( c == '\n' ){
- i[1] ++;
-
- if( i[1] > co[1] ) break;
-
- i[2] ++;
- i[0] = 0;
- }
- else i[2] ++;
- }
-
- co[0] = i[0];
- co[1] = i[1];
- co[2] = i[2];
-}
-
-/*
- * 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 )
-{
- co[0] = 0;
- co[1] = 0;
-
- char c;
- int i=0;
-
- while( (c = vg_ui.textbuf[i ++]) ){
- if( i > co[2] ) break;
- if( co[0] >= wrap_length ){
- co[1] ++;
- co[0] = 0;
- }
- if( c >= 32 && c <= 126 ) co[0] ++;
- else if( c == '\n' ){
- co[1] ++;
- co[0] = 0;
- }
- }
-}
-
-/*
- * calculate the number of characters remaining until either:
- * - the wrap_length limit is hit
- * - end of the line/string
- *
- * index must be fully populated with visual X/Y, and linear index
- */
-static int _ui_textbox_run_remaining( int index[3], int wrap_length )
-{
- int i=0, printed_chars=0;
- char c;
- while( (c = vg_ui.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;
-}
-
-static int ui_textbox( ui_rect rect, char *buf, u32 len, u32 flags,
- struct ui_textbox_callbacks *callbacks )
-{
- 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, vg_ui.mouse_click ) && click,
- hover = ui_inside_rect( rect, vg_ui.mouse );
-
- /* allow instant transitions from textbox->textbox */
- if( (vg_ui.focused_control_type != k_ui_control_none) &&
- (vg_ui.focused_control_type != k_ui_control_textbox) ){
- clickup = 0;
- clickdown = 0;
- click = 0;
- target = 0;
- hover = 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;
-
- ui_px border = -1;
-
- ui_rect text_rect;
- rect_copy( rect, text_rect );
-
- if( flags & UI_TEXTBOX_MULTILINE ) text_rect[3] = rect[3]-16;
- else text_rect[3] = 14;
-
- 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] / UI_GLYPH_SPACING_X;
-
- if( hover ){
- vg_ui.cursor = k_ui_cursor_ibeam;
- }
-
- if( vg_ui.focused_control_id == buf ){
- ui_fill( rect, col_base );
- ui_ntext( text_rect, buf, wrap_length, 1, k_ui_align_left, 0 );
-
- if( !(flags & UI_TEXTBOX_AUTOFOCUS) && ((clickup||clickdown) && !target)){
- ui_defocus_all();
- }
- else{
- vg_ui.focused_control_hit = 1;
- if( click && target ){
- int p0[3] ={
- (vg_ui.mouse_click[0] - text_rect[0]) / UI_GLYPH_SPACING_X,
- (vg_ui.mouse_click[1] - text_rect[1]) / 14,
- -1
- },
- p1[3] = {
- (vg_ui.mouse[0] - text_rect[0]) / UI_GLYPH_SPACING_X,
- (vg_ui.mouse[1] - text_rect[1]) / 14,
- -1
- };
-
- if( flags & UI_TEXTBOX_MULTILINE ){
- _ui_textbox_calc_index_from_grid( p0, wrap_length );
- _ui_textbox_calc_index_from_grid( p1, wrap_length );
-
- vg_ui.textbox.cursor_pos = p0[2];
- vg_ui.textbox.cursor_user = p1[2];
- }
- else{
- int max = strlen( buf );
- vg_ui.textbox.cursor_pos = VG_MAX( 0, VG_MIN( max, p0[0] )),
- vg_ui.textbox.cursor_user = VG_MAX( 0, VG_MIN( max, p1[0] ));
- }
- }
-
- ui_outline( rect, -2, vg_ui.scheme[ k_ui_orange ] );
-
- ui_rect cursor;
-
- int c0 = vg_ui.textbox.cursor_pos,
- c1 = vg_ui.textbox.cursor_user,
- start = VG_MIN( c0, c1 ),
- end = VG_MAX( c0, c1 ),
- chars = end-start;
-
- if( flags & (UI_TEXTBOX_MULTILINE|UI_TEXTBOX_WRAP) ){
-
- int pos[3], remaining = chars;
-
- pos[2] = start;
- _ui_textbox_index_calc_coords( pos, wrap_length );
-
- if( start==end ){
- cursor[0] = text_rect[0] + pos[0]*UI_GLYPH_SPACING_X-1;
- cursor[1] = text_rect[1] + pos[1]*14;
- cursor[2] = 2;
- cursor[3] = 13;
- ui_fill( cursor, col_cursor );
- rect_copy( cursor, vg_ui.click_fader_end );
- }
- else{
- while( remaining ){
- int run = _ui_textbox_run_remaining( pos, wrap_length );
- run = VG_MIN( run, remaining );
-
- cursor[0] = text_rect[0] + pos[0]*UI_GLYPH_SPACING_X-1;
- cursor[1] = text_rect[1] + pos[1]*14;
- cursor[2] = (float)(run)*(float)UI_GLYPH_SPACING_X;
- cursor[3] = 13;
-
- ui_fill( cursor, col_cursor );
-
- remaining -= run;
- pos[0] = 0;
- pos[1] ++;
- pos[2] += run;
- }
- rect_copy( cursor, vg_ui.click_fader_end );
- }
- }
- else{
- cursor[0] = text_rect[0] + start*UI_GLYPH_SPACING_X-1;
- cursor[1] = text_rect[1];
- cursor[3] = 13;
-
- if( start==end ){
- cursor[2] = 2;
- }
- else{
- cursor[2] = (float)(chars)*(float)UI_GLYPH_SPACING_X;
- }
-
- if( (vg_ui.click_fade_opacity<=0.0f) &&
- ui_clip( rect, cursor, cursor ) ){
- ui_fill( cursor, col_cursor );
- }
-
- rect_copy( cursor, vg_ui.click_fader_end );
- }
- }
-
- return 0;
- }
-
- if( click || (flags & UI_TEXTBOX_AUTOFOCUS) ){
- if( (target && hover) || (flags & UI_TEXTBOX_AUTOFOCUS) ){
- ui_defocus_all();
-
- ui_fill( rect, col_highlight );
- vg_ui.ignore_input_frames = 2;
- rect_copy( rect, vg_ui.click_fader );
- rect_copy( rect, vg_ui.click_fader_end );
-
- vg_ui.click_fade_opacity = 1.0f;
- vg_ui.textbuf = buf;
- vg_ui.focused_control_hit = 1;
- vg_ui.focused_control_type = k_ui_control_textbox;
- vg_ui.textbox.len = len;
- vg_ui.textbox.flags = flags;
- vg_ui.textbox.cursor_pos = 0;
- vg_ui.textbox.cursor_user = 0;
-
- if( callbacks ){
- vg_ui.textbox.callbacks = *callbacks;
- }
- else{
- vg_ui.textbox.callbacks.change = NULL;
- vg_ui.textbox.callbacks.down = NULL;
- vg_ui.textbox.callbacks.up = NULL;
- vg_ui.textbox.callbacks.enter = NULL;
- }
-
- SDL_StartTextInput();
- }
- }
-
- ui_fill( rect, col_base );
-
- if( hover ){
- ui_outline( rect, -1, col_highlight );
- }
-
- ui_ntext( text_rect, buf, wrap_length, 1, k_ui_align_left, 0 );
- return 0;
-}
-
-/*
- * Modal UI
- * -----------------------------------------------------------------------------
- */
-static void ui_start_modal( const char *message, u32 options ){
- vg_ui.focused_control_type = k_ui_control_modal;
- vg_ui.modal.message = message;
- vg_ui.modal.callbacks.close = NULL;
- vg_ui.modal.options = options;
-
- u32 type = options & UI_MODAL_TYPE_BITS;
- if( type == UI_MODAL_OK ) vg_info( message );
- else if( type == UI_MODAL_WARN ) vg_warn( message );
- else if( type == UI_MODAL_GOOD ) vg_success( message );
- else if( type == UI_MODAL_BAD ) vg_error( message );
-}
-
-/*
- * Input handling
- * -----------------------------------------------------------------------------
- */
-
-/*
- * Handles binds
- */
-static void _ui_proc_key( SDL_Keysym ev )
-{
- if( vg_ui.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;
- }
- }
- }
-}
-
-/*
- * Callback for text entry mode
- */
-VG_STATIC void ui_proc_utf8( const char *text )
-{
- if( vg_ui.focused_control_type == k_ui_control_textbox ){
- const char *ptr = text;
-
- while( *ptr ){
- if( *ptr != '`' ) _ui_textbox_put_char( *ptr );
- ptr ++;
- }
-
- if( vg_ui.textbox.callbacks.change ){
- vg_ui.textbox.callbacks.change( vg_ui.textbuf, vg_ui.textbox.len );
- }
- }
-}
-
-static void ui_label( 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)+UI_GLYPH_SPACING_X) * size;
- ui_split( rect, k_ui_axis_v, width, gap, l, r );
- ui_text( l, text, 1, k_ui_align_middle_left, 0 );
-}
-
-
-
-static ui_px k_vgi_widget_height = 24,
- k_vgi_scale = 1;
-
-enum vgi_window_flag{
- k_vgi_window_resizeable = 0x1,
- k_vgi_window_moveable = 0x2,
- k_vgi_window_closeable = 0x4,
- k_vgi_window_standard =
- k_vgi_window_moveable|
- k_vgi_window_closeable
-};
-
-static int vgi_window( ui_rect in_window, const char *title, u32 flags,
- ui_rect out_panel ){
- ui_fill( in_window, ui_colour( k_ui_bg+1 ) );
- ui_outline( in_window, 1, ui_colour( k_ui_bg+7 ) );
-
- ui_rect bar;
- ui_split( in_window, k_ui_axis_h, k_vgi_widget_height*k_vgi_scale, 0,
- bar, out_panel );
- ui_fill( bar, ui_colour( k_ui_bg+7 ) );
- ui_text( bar, title, k_vgi_scale, k_ui_align_middle_center,
- ui_colourcont(k_ui_bg+7) );
-
- if( flags & k_vgi_window_closeable ){
- ui_rect quit_button;
- ui_split( bar, k_ui_axis_v, bar[2]-bar[3], 2, bar, quit_button );
- if( ui_button_text( quit_button, "X", 1 ) ){
- return 0;
- }
- }
-
- return 1;
-}
-
-
-
-
-
-
-
-
-
-
-#endif /* VG_IMGUI_H */