Textfield time

This commit is contained in:
2024-10-20 00:46:44 +02:00
parent 3d98ba0023
commit 89c95c0656
6 changed files with 253 additions and 77 deletions

View File

@@ -36,6 +36,8 @@ Interaction_State :: struct {
left_mouse_pressed: bool;
right_mouse_pressed: bool;
editing: bool;
normalized_local_mouse_coordinates: Vector2; // Coordinates inside the rect in the range [0,1]
}
@@ -71,6 +73,8 @@ UI_Box :: struct {
interaction : Interaction_State;
_number_text: Static_Array(u8, 8);
style : struct {
texture: Texture_Handle;
background_color: Color;
@@ -971,4 +975,5 @@ padding_top : float;
padding_bottom : float;
#load "../core/stack.jai";
#load "../core/static_array.jai";

View File

@@ -7,6 +7,15 @@ ui_full_size_background :: (color: Color = .{0,0,0,1}, identifier: s64 = 0, loc
background := ui_box_make(.DRAW_BACKGROUND, hash=get_hash(loc, identifier));
}
ui_container_layout :: (color: Color = .{0,0,0,0}, identifier: s64 = 0, loc := #caller_location) {
ui_set_next_size_x(.CHILDREN_SUM);
ui_set_next_size_y(.CHILDREN_SUM);
ui_set_next_background_color(color);
container := ui_box_make(.DRAW_BACKGROUND, hash=get_hash(loc, identifier));
}
ui_button :: (text: string, identifier: s64 = 0, loc := #caller_location) -> clicked: bool, Interaction_State {
ui_set_next_text(text);
ui_set_next_background_color(.{0.2,0.2,0.2,1});
@@ -99,6 +108,151 @@ ui_label_fill_parent_x :: (text: string, identifier: s64 = 0, loc := #caller_loc
box := ui_box_make(.DRAW_TEXT | .ANIMATE_ON_HOVER, get_hash(loc, identifier));
}
ui_textfield :: (label: string, text: *string, identifier: s64 = 0, loc := #caller_location) {
ui_container_layout();
ui_push_parent(ui_state.last_box, .LEFT, .HORIZONTAL);
{
ui_label(label);
ui_set_next_background_color(.{0.0,1.0,1.0,0.0});
ui_set_next_border_color(.{0.3,0.3,0.3,1.0});
ui_set_next_size_x(.PCT, 1);
text_size := get_text_size(engine.renderer, <<text, ui_state.fonts.button);
ui_set_next_size_y(.PIXELS, text_size.y); // TODO
ui_set_next_padding(5);
//ui_set_next_text_alignment(.CENTER_VERTICALLY);
outer := ui_box_make(.ANIMATE_ON_HOVER | .CLICKABLE | .DRAW_BORDER, get_hash(loc, identifier));
if outer.interaction.editing {
outer.style.border_color = .{0.3,0.3,1.0,1.0};
}
ui_push_parent(outer, alignment=.LEFT, axis=.HORIZONTAL);
{
if text.count > 0 {
ui_set_next_text(<<text);
ui_set_next_text_color(.{1,1,1,1});
ui_set_next_text_alignment(.CENTER_VERTICALLY);
ui_set_next_text_color(.{1,1,1,1});
ui_set_next_size_x(.TEXT_DIM);
ui_set_next_size_y(.TEXT_DIM);
text_widget := ui_box_make(.DRAW_TEXT, get_hash(loc, identifier));
}
if outer.interaction.clicked {
outer.interaction.editing = true;
}
if outer.interaction.editing {
if key_down(.BACKSPACE) {
if text.count > 0 {
text.count -= 1;
}
}
if key_down(.RETURN) {
outer.interaction.editing = false;
}
if engine.input.has_char {
new_str := alloc_string(text.count + 1);
memcpy(new_str.data, text.data, text.count);
new_str[new_str.count-1] = xx engine.input.current_char;
free(<<text);
<<text = new_str;
}
// Cursor
ui_set_next_background_color(.{0.0,0.5,0.5,1.0});
ui_set_next_size_x(.PIXELS, 3);
ui_set_next_size_y(.PCT, 1);
cursor := ui_box_make(.DRAW_BACKGROUND, get_hash(loc, identifier));
}
}
ui_pop_parent();
}
ui_pop_parent();
}
ui_float_field :: (value: *float, identifier: s64 = 0, loc := #caller_location) {
ui_set_next_background_color(.{0.0,1.0,1.0,0.0});
ui_set_next_border_color(.{0.3,0.3,0.3,1.0});
ui_set_next_size_x(.CHILDREN_SUM, 1);
text_size := get_text_size(engine.renderer, "0.00", ui_state.fonts.button);
ui_set_next_size_y(.PIXELS, text_size.y); // TODO
ui_set_next_padding(5);
//ui_set_next_text_alignment(.CENTER_VERTICALLY);
outer := ui_box_make(.ANIMATE_ON_HOVER | .CLICKABLE | .DRAW_BORDER, get_hash(loc, identifier));
if outer.interaction.editing {
outer.style.border_color = .{0.3,0.3,1.0,1.0};
}
ui_push_parent(outer, alignment=.LEFT, axis=.HORIZONTAL);
{
//text.count > 0 {
ui_set_next_text(tprint("%", <<value));
ui_set_next_text_color(.{1,1,1,1});
ui_set_next_text_alignment(.CENTER_VERTICALLY);
ui_set_next_text_color(.{1,1,1,1});
ui_set_next_size_x(.TEXT_DIM);
ui_set_next_size_y(.TEXT_DIM);
text_widget := ui_box_make(.DRAW_TEXT, get_hash(loc, identifier));
//}
if outer.interaction.clicked {
outer.interaction.editing = true;
}
//if outer.interaction.editing {
// if key_down(.BACKSPACE) {
// if text.count > 0 {
// text.count -= 1;
// }
// }
// if key_down(.RETURN) {
// outer.interaction.editing = false;
// }
// if engine.input.has_char {
// new_str := alloc_string(text.count + 1);
// memcpy(new_str.data, text.data, text.count);
// new_str[new_str.count-1] = xx engine.input.current_char;
// free(<<text);
// <<text = new_str;
// }
// // Cursor
// ui_set_next_background_color(.{0.0,0.5,0.5,1.0});
// ui_set_next_size_x(.PIXELS, 3);
// ui_set_next_size_y(.PCT, 1);
// cursor := ui_box_make(.DRAW_BACKGROUND, get_hash(loc, identifier));
//
}
ui_pop_parent();
}
ui_vector_field :: (label: string, value: *Vector3, identifier: s64 = 0, loc := #caller_location) {
ui_container_layout(identifier=identifier, loc=loc);
ui_push_parent(ui_state.last_box, .LEFT, .HORIZONTAL);
{
ui_label(label);
ui_label("X");
ui_float_field(*value.x);
ui_label("Y");
ui_float_field(*value.y);
ui_label("Z");
ui_float_field(*value.z);
}
ui_pop_parent();
}
ui_clickable_label :: (text: string, selected: bool = false, identifier: s64 = 0, loc := #caller_location) -> clicked: bool, Interaction_State {
ui_set_next_text(text);
if selected {