((STDHEX&0x0000ff00) ) |\
((STDHEX&0x00ff0000)>>16)
+#define UI_TEXTBOX_MULTILINE 0x1
+#define UI_TEXTBOX_WRAP 0x2
+
struct{
struct ui_vert *vertex_buffer;
u16 *indice_buffer;
k_ui_control_textbox,
}
focused_control_type;
- int cursor_user, cursor_pos, string_length;
- u32 textbuf_len;
+
+ struct ui_textbuf{
+ int cursor_user, cursor_pos;
+ u32 len;
+ u32 flags;
+ }
+ textbox;
GLuint tex_glyphs, vao, vbo, ebo;
else return ui_colour( k_ui_fg+1 );
}
-static void ui_text( ui_rect rect, const char *str, ui_px scale,
+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;
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;
}
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 = '\x90';
+ 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 ){
+ 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;
}
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 )
*/
static void _ui_textbox_make_selection( int *start, int *end )
{
- *start = VG_MIN( vg_ui.cursor_pos, vg_ui.cursor_user );
- *end = VG_MAX( vg_ui.cursor_pos, vg_ui.cursor_user );
+ *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.cursor_user + dir );
+ *cursor0 = VG_MAX( 0, vg_ui.textbox.cursor_user + dir );
*cursor0 =
VG_MIN(
- VG_MIN( vg_ui.textbuf_len-1, strlen( vg_ui.textbuf )),
+ VG_MIN( vg_ui.textbox.len-1, strlen( vg_ui.textbuf )),
*cursor0 );
if( snap_together )
static int _ui_textbox_makeroom( int datastart, int length )
{
- int move_to = VG_MIN( datastart+length, vg_ui.textbuf_len-1 );
+ 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.textbuf_len-1 );
+ int move_end = VG_MIN( move_to+move_amount, vg_ui.textbox.len-1 );
move_amount = move_end-move_to;
if( move_amount )
vg_ui.textbuf[ move_end ] = '\0';
- return VG_MIN( length, vg_ui.textbuf_len-datastart-1 );
+ return VG_MIN( length, vg_ui.textbox.len-datastart-1 );
}
static int _ui_textbox_delete_char( int direction )
int cpylength = _ui_textbox_makeroom( datastart, length );
memcpy( vg_ui.textbuf + datastart, text, cpylength);
- _ui_textbox_move_cursor( &vg_ui.cursor_user,
- &vg_ui.cursor_pos, cpylength, 1 );
+ _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.cursor_user = _ui_textbox_delete_char(0);
+ vg_ui.textbox.cursor_user = _ui_textbox_delete_char(0);
- if( _ui_textbox_makeroom( vg_ui.cursor_user, 1 ) )
- vg_ui.textbuf[ vg_ui.cursor_user ] = c;
+ 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.cursor_user, &vg_ui.cursor_pos, 1, 1 );
+ _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.cursor_user, NULL, -1, 0 );
+ _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.cursor_pos - vg_ui.cursor_user? 0: 1;
+ 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;
- _ui_textbox_move_cursor( &vg_ui.cursor_user,
- &vg_ui.cursor_pos, -cursor_diff, 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{
+
+ }
+}
+
+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{
+
+ }
}
static void _ui_textbox_right_select(void)
{
- _ui_textbox_move_cursor( &vg_ui.cursor_user, NULL, 1, 0 );
+ _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user, NULL, 1, 0 );
}
static void _ui_textbox_right(void)
{
- int cursor_diff = vg_ui.cursor_pos - vg_ui.cursor_user? 0: 1;
+ int cursor_diff = vg_ui.textbox.cursor_pos - vg_ui.textbox.cursor_user? 0: 1;
- _ui_textbox_move_cursor( &vg_ui.cursor_user,
- &vg_ui.cursor_pos, +cursor_diff, 1 );
+ _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user,
+ &vg_ui.textbox.cursor_pos, +cursor_diff, 1 );
}
static void _ui_textbox_backspace(void)
{
- vg_ui.cursor_user = _ui_textbox_delete_char( -1 );
- vg_ui.cursor_pos = vg_ui.cursor_user;
+ vg_ui.textbox.cursor_user = _ui_textbox_delete_char( -1 );
+ vg_ui.textbox.cursor_pos = vg_ui.textbox.cursor_user;
}
static void _ui_textbox_delete(void)
{
- vg_ui.cursor_user = _ui_textbox_delete_char( 1 );
- vg_ui.cursor_pos = vg_ui.cursor_user;
+ vg_ui.textbox.cursor_user = _ui_textbox_delete_char( 1 );
+ vg_ui.textbox.cursor_pos = vg_ui.textbox.cursor_user;
}
static void _ui_textbox_home_select(void)
{
- _ui_textbox_move_cursor( &vg_ui.cursor_user, NULL, -10000, 0 );
+ _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user, NULL, -10000, 0 );
}
static void _ui_textbox_home(void)
{
- _ui_textbox_move_cursor( &vg_ui.cursor_user,
- &vg_ui.cursor_pos, -10000, 1 );
+ _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.cursor_user, NULL, 10000, 0 );
+ _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user, NULL, 10000, 0 );
}
static void _ui_textbox_end(void)
{
- _ui_textbox_move_cursor( &vg_ui.cursor_user,
- &vg_ui.cursor_pos,
- vg_ui.textbuf_len-1, 1 );
+ _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.cursor_user, NULL, 10000, 0);
- _ui_textbox_move_cursor( &vg_ui.cursor_pos, NULL, -10000, 0);
+ _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.cursor_user = _ui_textbox_delete_char(0);
- vg_ui.cursor_pos = vg_ui.cursor_user;
+ 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( !strlen( vg_ui.textbuf ) )
- return;
+ if( vg_ui.focused_control_type == k_ui_control_textbox ){
+ if( vg_ui.textbox.flags & UI_TEXTBOX_MULTILINE ){
+ _ui_textbox_put_char( '\n' );
+ }
+ else{
+ ui_defocus_all();
+ }
+ }
}
/*
{ KMOD_SHIFT, SDLK_LEFT, _ui_textbox_left_select },
{ 0, SDLK_RIGHT, _ui_textbox_right },
{ KMOD_SHIFT, SDLK_RIGHT, _ui_textbox_right_select },
-#if 0
{ 0, SDLK_DOWN, _ui_textbox_down },
{ 0, SDLK_UP, _ui_textbox_up },
-#endif
{ 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 },
{ KMOD_CTRL, SDLK_n, _ui_textbox_suggest_next },
{ KMOD_CTRL, SDLK_p, _ui_textbox_suggest_prev }
#endif
- { 0, SDLK_ESCAPE, ui_defocus_all }
+ { 0, SDLK_ESCAPE, ui_defocus_all },
};
+ if( vg_ui.focused_control_type == k_ui_control_none ){
+ return;
+ }
+
SDL_Keymod mod = 0;
if( ev.mod & KMOD_SHIFT )
-
static void _ui_textbox_cursor_rect( ui_rect text_rect, ui_rect cursor )
{
- int start = VG_MIN( vg_ui.cursor_pos, vg_ui.cursor_user ),
- end = VG_MAX( vg_ui.cursor_pos, vg_ui.cursor_user );
+ int 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 );
cursor[0] = text_rect[0] + start*UI_GLYPH_SPACING_X-1;
cursor[1] = text_rect[1];
cursor[2] = start == end? 2: (float)(end-start)*(float)UI_GLYPH_SPACING_X;
- cursor[3] = text_rect[3];
-
+ cursor[3] = 14;
+}
+
+static void _ui_textbox_calc_index_from_grid( int co[3], int char_width )
+{
+ 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] >= char_width ){
+ 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];
+}
+
+static
+void _ui_textbox_index_calc_coords( int co[3], int char_width )
+{
+ 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] >= char_width ){
+ co[1] ++;
+ co[0] = 0;
+ }
+ if( c >= 32 && c <= 126 ) co[0] ++;
+ else if( c == '\n' ){
+ co[1] ++;
+ co[0] = 0;
+ }
+ }
}
+static int _ui_textbox_run_remaining( int index[3], int char_width )
+{
+ int i=0, printed_chars=0;
+ char c;
+ while( (c = vg_ui.textbuf[index[2] + (i ++)]) ){
+ if( index[0]+i >= char_width ) 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 )
+static int ui_textbox( ui_rect rect, char *buf, u32 len, u32 flags )
{
int clickup= ui_click_up(),
click = ui_clicking() | clickup,
ui_rect text_rect;
rect_copy( rect, text_rect );
- text_rect[3] = 14;
+
+ 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 char_width = 1024;
+
+ if( flags & UI_TEXTBOX_WRAP )
+ char_width = 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_text( text_rect, buf, 1, k_ui_align_left, 0 );
+ ui_ntext( text_rect, buf, char_width, 1, k_ui_align_left, 0 );
if( (clickup||ui_click_down()) && !target ){
ui_defocus_all();
}
else{
vg_ui.focused_control_hit = 1;
-
if( click && target ){
- ui_px d0 = vg_ui.mouse_click[0] - text_rect[0],
- d1 = vg_ui.mouse[0] - text_rect[0];
-
- int max = strlen( vg_ui.textbuf );
-
- vg_ui.cursor_pos = VG_MAX( 0, VG_MIN( max, d0/UI_GLYPH_SPACING_X )),
- vg_ui.cursor_user= VG_MAX( 0, VG_MIN( max, d1/UI_GLYPH_SPACING_X ));
+ 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, char_width );
+ _ui_textbox_calc_index_from_grid( p1, char_width );
+
+ 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;
- _ui_textbox_cursor_rect( text_rect, cursor );
- rect_copy( cursor, vg_ui.click_fader_end );
- if( (vg_ui.click_fade_opacity<=0.0f) &&
- ui_clip( rect, cursor, cursor ) ){
- ui_fill( cursor, col_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, char_width );
+
+ 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 ){
+ /* TODO: scan for newlines and stuff
+ * eol or char_width can have line breaks! */
+
+ int run = _ui_textbox_run_remaining( pos, char_width );
+ 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 );
}
}
if( click ){
if( target && hover ){
+ ui_defocus_all();
+
ui_fill( rect, col_highlight );
vg_ui.ignore_input_frames = 2;
rect_copy( rect, vg_ui.click_fader );
- _ui_textbox_cursor_rect( text_rect, vg_ui.click_fader_end );
-
+ 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.textbuf_len = len;
+ vg_ui.textbox.len = len;
+ vg_ui.textbox.flags = flags;
+ vg_ui.textbox.cursor_pos = 0;
+ vg_ui.textbox.cursor_user = 0;
+
vg_info( "SDL_StartTextInput()\n" );
SDL_StartTextInput();
}
ui_outline( rect, -1, col_highlight );
}
- ui_text( text_rect, buf, 1, k_ui_align_left, 0 );
+ ui_ntext( text_rect, buf, char_width, 1, k_ui_align_left, 0 );
return 0;
}