void *focused_control_id; /* uses the memory location of various locking
controls as an id */
char *textbuf;
+ struct ui_dropdown_value{
+ i32 index, value;
+ }
+ *ptr_dropdown;
};
u32 focused_control_hit;
enum ui_control_type{
k_ui_control_none,
k_ui_control_textbox,
+ k_ui_control_dropdown
}
focused_control_type;
+ union{
struct ui_textbuf{
int cursor_user, cursor_pos;
u32 len;
callbacks;
}
textbox;
+
+ struct ui_dropdown{
+ struct ui_dropdown_opt{
+ const char *alias;
+ i32 value;
+ }
+ *options;
+ u32 option_count;
+ ui_rect rect;
+ }
+ dropdown;
+ };
GLuint tex_glyphs, vao, vbo, ebo;
"void main()"
"{"
"vec4 colour = texture( uTexImage, aTexCoords );"
-
- /* wtf is this?? */
-#if 0
- "float value = dot(vec4(1.0),colour)*0.25;"
-
- "vec3 col = vec3(pow(cos(value*3.14159265*2.0)*0.5+0.5,0.5))"
- "* vec3(step(value,0.5),0.3,step(1.0-value,0.5));"
- "FragColor = vec4( col*4.0, 1.0 );"
-#endif
-
"FragColor = colour;"
"}"
}
glEnable( GL_BLEND );
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
glBlendEquation( GL_FUNC_ADD );
+ glDisable( GL_CULL_FACE );
m3x3f view = M3X3_IDENTITY;
m3x3_translate( view, (v3f){ -1.0f, 1.0f, 0.0f } );
vg_ui.cur_vert += 8;
}
-static void ui_split_px( ui_rect rect,
- enum ui_axis other, ui_px width, ui_px pad,
- ui_rect l, ui_rect r )
+static void ui_split( ui_rect rect,
+ enum ui_axis other, ui_px width, ui_px gap,
+ ui_rect l, ui_rect r )
{
enum ui_axis dir = other ^ 0x1;
+ if( width < 0 ) width = rect[ 2+dir ] + width;
+
ui_rect temp;
rect_copy( rect, temp );
- l[ dir ] = temp[ dir ] + pad;
- r[ dir ] = temp[ dir ] + width + (pad/2);
- l[ other ] = temp[ other ] + pad;
- r[ other ] = temp[ other ] + pad;
- l[ 2+dir ] = width - ((3*pad)/2);
- r[ 2+dir ] = temp[ 2+dir ] - width - ((3*pad)/2);
- l[ 2+other ] = temp[ 2+other ] - pad*2;
- r[ 2+other ] = temp[ 2+other ] - pad*2;
+ l[ dir ] = temp[ dir ];
+ r[ dir ] = temp[ dir ] + width + (gap/2);
+ l[ other ] = temp[ other ];
+ r[ other ] = temp[ other ];
+ l[ 2+dir ] = width - (gap/2);
+ r[ 2+dir ] = temp[ 2+dir ] - width - (gap/2);
+ l[ 2+other ] = temp[ 2+other ];
+ r[ 2+other ] = temp[ 2+other ];
+}
+
+static ui_px ui_text_line_width( const char *str );
+static void ui_split_label( ui_rect rect, const char *text, ui_px size,
+ ui_px gap, ui_rect l, ui_rect r )
+{
+ ui_px width = ui_text_line_width( text ) * size;
+ ui_split( rect, k_ui_axis_v, width, gap, l, r );
}
static void ui_rect_center( ui_rect parent, ui_rect rect )
}
static void ui_split_ratio( ui_rect rect, enum ui_axis dir, float ratio,
- ui_px pad, ui_rect l, ui_rect r )
+ ui_px gap, ui_rect l, ui_rect r )
{
ui_px width = (float)rect[ 2+(dir^0x1) ] * ratio;
- ui_split_px( rect, dir, width, pad, l, r );
+ ui_split( rect, dir, width, gap, l, r );
}
-static void ui_rect_pad( ui_rect rect, ui_px pad )
+static void ui_rect_pad( ui_rect rect, ui_px pad[2] )
{
- rect[0] += pad;
- rect[1] += pad;
- rect[2] -= pad*2;
- rect[3] -= pad*2;
+ rect[0] += pad[0];
+ rect[1] += pad[1];
+ rect[2] -= pad[0]*2;
+ rect[3] -= pad[1]*2;
}
static ui_px ui_text_line_width( const char *str )
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 } );
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;
}
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 );
ui_fill( rect, colour );
}
+
+ if( vg_ui.focused_control_type == k_ui_control_dropdown ){
+ ui_enum_post();
+ }
+
ui_flush( k_ui_shader_colour );
if( !vg_ui.focused_control_hit ){
"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.0f, col[0], col[1] );
+ 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_px( col[which], k_ui_axis_h, 24, 0, swatch, col[which] );
+ ui_split( col[which], k_ui_axis_h, 24, 0, swatch, col[which] );
ui_fill( swatch, ui_colour(i) );
if( names[i] )
static int ui_checkbox( ui_rect rect, const char *str_label, int *data )
{
ui_rect label, box;
- ui_split_px( rect, k_ui_axis_v, rect[2]-rect[3], 0, 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 );
*data = (*data) ^ 0x1;
if( *data ){
- ui_rect_pad( box, 4 );
+ 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], 1, 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
* -----------------------------------------------------------------------------
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;
+ }
+
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_fill( rect, col_base );
ui_ntext( text_rect, buf, wrap_length, 1, k_ui_align_left, 0 );
- if( !(flags & UI_TEXTBOX_AUTOFOCUS) &&
- ((clickup||ui_click_down(UI_MOUSE_LEFT)) && !target) ){
+ if( !(flags & UI_TEXTBOX_AUTOFOCUS) && ((clickup||clickdown) && !target)){
ui_defocus_all();
}
else{
}
else{
while( remaining ){
- /* TODO: scan for newlines and stuff
- * eol or wrap_length can have line breaks! */
-
int run = _ui_textbox_run_remaining( pos, wrap_length );
run = VG_MIN( run, remaining );