Only highlight the widgets on mouse down when they are also hovered #106

Merged
MartinFouilleul merged 3 commits from ilidemi/orca:fix-hovered-active into main 2023-09-16 14:41:21 +00:00
2 changed files with 95 additions and 67 deletions

View File

@ -524,6 +524,11 @@ void oc_ui_box_deactivate(oc_ui_box* box)
box->active = false;
}
void oc_ui_box_set_active(oc_ui_box* box, bool active)
{
box->active = active;
}
bool oc_ui_box_active(oc_ui_box* box)
{
return (box->active);
@ -539,16 +544,6 @@ bool oc_ui_box_hot(oc_ui_box* box)
return (box->hot);
}
void oc_ui_box_set_dragging(oc_ui_box* box, bool dragging)
{
box->dragging = dragging;
}
bool oc_ui_box_dragging(oc_ui_box* box)
{
return (box->dragging);
}
oc_ui_sig oc_ui_box_sig(oc_ui_box* box)
{
//NOTE: compute input signals
@ -872,6 +867,10 @@ bool oc_ui_style_selector_match(oc_ui_box* box, oc_ui_style_rule* rule, oc_ui_se
{
res = res && oc_ui_box_hovering(box, oc_ui_mouse_position());
}
if(selector->status & OC_UI_HOT)
{
res = res && box->hot;
}
if(selector->status & OC_UI_ACTIVE)
{
res = res && box->active;
@ -1600,7 +1599,7 @@ oc_ui_sig oc_ui_button_behavior(oc_ui_box* box)
{
oc_ui_box_set_hot(box, false);
}
if(!sig.dragging)
if(!sig.hovering && !sig.dragging)
{
oc_ui_box_deactivate(box);
}
@ -1638,11 +1637,17 @@ oc_ui_sig oc_ui_button_str8(oc_str8 label)
oc_ui_pattern_push(&ui->frameArena, &hoverPattern, (oc_ui_selector){ .kind = OC_UI_SEL_STATUS, .status = OC_UI_HOVER });
oc_ui_style hoverStyle = { .bgColor = theme->fill1 };
oc_ui_style_match_before(hoverPattern, &hoverStyle, OC_UI_STYLE_BG_COLOR);
/*
oc_ui_pattern activePattern = { 0 };
oc_ui_pattern_push(&ui->frameArena, &activePattern, (oc_ui_selector){ .kind = OC_UI_SEL_STATUS, .status = OC_UI_ACTIVE });
oc_ui_style activeStyle = { .bgColor = theme->fill2 };
oc_ui_style activeStyle = { .bgColor = theme->fill1 };
oc_ui_style_match_before(activePattern, &activeStyle, OC_UI_STYLE_BG_COLOR);
*/
oc_ui_pattern activeAndHoverPattern = { 0 };
oc_ui_pattern_push(&ui->frameArena, &activeAndHoverPattern, (oc_ui_selector){ .kind = OC_UI_SEL_STATUS, .status = OC_UI_ACTIVE });
oc_ui_pattern_push(&ui->frameArena, &activeAndHoverPattern, (oc_ui_selector){ .op = OC_UI_SEL_AND, .kind = OC_UI_SEL_STATUS, .status = OC_UI_HOVER });
oc_ui_style activeAndHoverStyle = { .bgColor = theme->fill2 };
oc_ui_style_match_before(activeAndHoverPattern, &activeAndHoverStyle, OC_UI_STYLE_BG_COLOR);
oc_ui_flags flags = OC_UI_FLAG_CLICKABLE
| OC_UI_FLAG_CLIP
@ -1715,11 +1720,17 @@ oc_ui_sig oc_ui_checkbox(const char* name, bool* checked)
oc_ui_pattern_push(&ui->frameArena, &hoverPattern, (oc_ui_selector){ .kind = OC_UI_SEL_STATUS, .status = OC_UI_HOVER });
oc_ui_style hoverStyle = { .bgColor = theme->primaryHover };
oc_ui_style_match_before(hoverPattern, &hoverStyle, OC_UI_STYLE_BG_COLOR);
/*
oc_ui_pattern activePattern = { 0 };
oc_ui_pattern_push(&ui->frameArena, &activePattern, (oc_ui_selector){ .kind = OC_UI_SEL_STATUS, .status = OC_UI_ACTIVE });
oc_ui_style activeStyle = { .bgColor = theme->primaryActive };
oc_ui_style activeStyle = { .bgColor = theme->primaryHover };
oc_ui_style_match_before(activePattern, &activeStyle, OC_UI_STYLE_BG_COLOR);
*/
oc_ui_pattern activeAndHoverPattern = { 0 };
oc_ui_pattern_push(&ui->frameArena, &activeAndHoverPattern, (oc_ui_selector){ .kind = OC_UI_SEL_STATUS, .status = OC_UI_ACTIVE });
oc_ui_pattern_push(&ui->frameArena, &activeAndHoverPattern, (oc_ui_selector){ .op = OC_UI_SEL_AND, .kind = OC_UI_SEL_STATUS, .status = OC_UI_HOVER });
oc_ui_style activeAndHoverStyle = { .bgColor = theme->primaryActive };
oc_ui_style_match_before(activeAndHoverPattern, &activeAndHoverStyle, OC_UI_STYLE_BG_COLOR);
oc_ui_flags flags = OC_UI_FLAG_CLICKABLE
| OC_UI_FLAG_CLIP
@ -1757,11 +1768,19 @@ oc_ui_sig oc_ui_checkbox(const char* name, bool* checked)
.borderColor = theme->primary };
oc_ui_style_match_before(hoverPattern, &hoverStyle, OC_UI_STYLE_BG_COLOR | OC_UI_STYLE_BORDER_COLOR);
/*
oc_ui_pattern activePattern = { 0 };
oc_ui_pattern_push(&ui->frameArena, &activePattern, (oc_ui_selector){ .kind = OC_UI_SEL_STATUS, .status = OC_UI_ACTIVE });
oc_ui_style activeStyle = { .bgColor = theme->fill1,
oc_ui_style activeStyle = { .bgColor = theme->fill0,
.borderColor = theme->primary };
oc_ui_style_match_before(activePattern, &activeStyle, OC_UI_STYLE_BG_COLOR | OC_UI_STYLE_BORDER_COLOR);
*/
oc_ui_pattern activeAndHoverPattern = { 0 };
oc_ui_pattern_push(&ui->frameArena, &activeAndHoverPattern, (oc_ui_selector){ .kind = OC_UI_SEL_STATUS, .status = OC_UI_ACTIVE });
oc_ui_pattern_push(&ui->frameArena, &activeAndHoverPattern, (oc_ui_selector){ .op = OC_UI_SEL_AND, .kind = OC_UI_SEL_STATUS, .status = OC_UI_HOVER });
oc_ui_style activeAndHoverStyle = { .bgColor = theme->fill1,
.borderColor = theme->primary };
oc_ui_style_match_before(activeAndHoverPattern, &activeAndHoverStyle, OC_UI_STYLE_BG_COLOR);
oc_ui_flags flags = OC_UI_FLAG_CLICKABLE
| OC_UI_FLAG_CLIP
@ -2307,10 +2326,10 @@ void oc_ui_menu_begin(const char* label)
oc_ui_style hoverStyle = { .bgColor = theme->fill0 };
oc_ui_style_match_before(hoverPattern, &hoverStyle, OC_UI_STYLE_BG_COLOR);
oc_ui_pattern draggingPattern = { 0 };
oc_ui_pattern_push(&ui->frameArena, &draggingPattern, (oc_ui_selector){ .kind = OC_UI_SEL_STATUS, .status = OC_UI_DRAGGING });
oc_ui_style draggingStyle = { .bgColor = theme->fill2 };
oc_ui_style_match_before(draggingPattern, &draggingStyle, OC_UI_STYLE_BG_COLOR);
oc_ui_pattern activePattern = { 0 };
oc_ui_pattern_push(&ui->frameArena, &activePattern, (oc_ui_selector){ .kind = OC_UI_SEL_STATUS, .status = OC_UI_ACTIVE });
oc_ui_style activeStyle = { .bgColor = theme->fill2 };
oc_ui_style_match_before(activePattern, &activeStyle, OC_UI_STYLE_BG_COLOR);
oc_ui_box* button = oc_ui_box_begin("button", OC_UI_FLAG_CLICKABLE | OC_UI_FLAG_DRAW_BACKGROUND);
@ -2409,10 +2428,10 @@ oc_ui_sig oc_ui_menu_button(const char* name)
oc_ui_style hoverStyle = { .bgColor = theme->fill0 };
oc_ui_style_match_before(hoverPattern, &hoverStyle, OC_UI_STYLE_BG_COLOR);
oc_ui_pattern draggingPattern = { 0 };
oc_ui_pattern_push(&ui->frameArena, &draggingPattern, (oc_ui_selector){ .kind = OC_UI_SEL_STATUS, .status = OC_UI_DRAGGING });
oc_ui_style draggingStyle = { .bgColor = theme->fill2 };
oc_ui_style_match_before(draggingPattern, &draggingStyle, OC_UI_STYLE_BG_COLOR);
oc_ui_pattern activePattern = { 0 };
oc_ui_pattern_push(&ui->frameArena, &activePattern, (oc_ui_selector){ .kind = OC_UI_SEL_STATUS, .status = OC_UI_ACTIVE });
oc_ui_style activeStyle = { .bgColor = theme->fill2 };
oc_ui_style_match_before(activePattern, &activeStyle, OC_UI_STYLE_BG_COLOR);
oc_ui_flags flags = OC_UI_FLAG_CLICKABLE
| OC_UI_FLAG_CLIP
@ -2501,12 +2520,13 @@ oc_ui_select_popup_info oc_ui_select_popup(const char* name, oc_ui_select_popup_
.borderSize = 1 };
oc_ui_style_match_after(activePattern, &activeStyle, OC_UI_STYLE_BORDER_COLOR | OC_UI_STYLE_BORDER_SIZE);
oc_ui_pattern draggingPattern = { 0 };
oc_ui_pattern_push(&ui->frameArena, &draggingPattern, (oc_ui_selector){ .kind = OC_UI_SEL_STATUS, .status = OC_UI_DRAGGING });
oc_ui_style draggingStyle = { .bgColor = theme->fill2,
oc_ui_pattern mouseDownPattern = { 0 };
oc_ui_pattern_push(&ui->frameArena, &mouseDownPattern, (oc_ui_selector){ .kind = OC_UI_SEL_STATUS, .status = OC_UI_ACTIVE });
oc_ui_pattern_push(&ui->frameArena, &mouseDownPattern, (oc_ui_selector){ .op = OC_UI_SEL_AND, .kind = OC_UI_SEL_STATUS, .status = OC_UI_HOVER });
oc_ui_style mouseDownStyle = { .bgColor = theme->fill2,
.borderColor = theme->primary,
.borderSize = 1 };
oc_ui_style_match_after(draggingPattern, &draggingStyle, OC_UI_STYLE_BG_COLOR | OC_UI_STYLE_BORDER_COLOR | OC_UI_STYLE_BORDER_SIZE);
oc_ui_style_match_after(mouseDownPattern, &mouseDownStyle, OC_UI_STYLE_BG_COLOR | OC_UI_STYLE_BORDER_COLOR | OC_UI_STYLE_BORDER_SIZE);
oc_ui_box* button = oc_ui_box_make("button",
OC_UI_FLAG_CLICKABLE
@ -2624,9 +2644,9 @@ oc_ui_select_popup_info oc_ui_select_popup(const char* name, oc_ui_select_popup_
oc_ui_pattern_push(&ui->frameArena, &hoverPattern, (oc_ui_selector){ .kind = OC_UI_SEL_STATUS, .status = OC_UI_HOVER });
oc_ui_style_match_before(hoverPattern, &(oc_ui_style){ .bgColor = theme->fill0 }, OC_UI_STYLE_BG_COLOR);
oc_ui_pattern draggingPattern = { 0 };
oc_ui_pattern_push(&ui->frameArena, &draggingPattern, (oc_ui_selector){ .kind = OC_UI_SEL_STATUS, .status = OC_UI_DRAGGING });
oc_ui_style_match_before(draggingPattern, &(oc_ui_style){ .bgColor = theme->fill2 }, OC_UI_STYLE_BG_COLOR);
oc_ui_pattern activePattern = { 0 };
oc_ui_pattern_push(&ui->frameArena, &activePattern, (oc_ui_selector){ .kind = OC_UI_SEL_STATUS, .status = OC_UI_ACTIVE });
oc_ui_style_match_before(activePattern, &(oc_ui_style){ .bgColor = theme->fill2 }, OC_UI_STYLE_BG_COLOR);
oc_ui_box* wrapper = oc_ui_box_begin_str8(info->options[i],
OC_UI_FLAG_CLICKABLE | OC_UI_FLAG_DRAW_BACKGROUND);
@ -2672,11 +2692,18 @@ oc_ui_select_popup_info oc_ui_select_popup(const char* name, oc_ui_select_popup_
oc_ui_box_deactivate(button);
oc_ui_box_deactivate(panel);
}
else if(oc_ui_box_sig(button).clicked)
else
{
oc_ui_sig sig = oc_ui_box_sig(button);
if(sig.pressed)
{
oc_ui_box_activate(button);
}
if(sig.clicked)
{
oc_ui_box_activate(panel);
}
}
oc_ui_box_set_closed(panel, !oc_ui_box_active(panel));
}
result.changed = result.selectedIndex != info->selectedIndex;
@ -2732,7 +2759,7 @@ oc_ui_radio_group_info oc_ui_radio_group(const char* name, oc_ui_radio_group_inf
}
oc_ui_box_set_hot(radio, sig.hovering);
oc_ui_box_set_dragging(radio, sig.dragging);
oc_ui_box_set_active(radio, sig.hovering && sig.dragging);
const char* defaultTagStr = "radio";
const char* selectedTagStr = "radio_selected";
@ -2757,23 +2784,23 @@ oc_ui_radio_group_info oc_ui_radio_group(const char* name, oc_ui_radio_group_inf
| OC_UI_STYLE_BORDER_SIZE;
oc_ui_style_box_before(radio, defaultPattern, &defaultStyle, defaultMask);
oc_ui_pattern hoverPattern = { 0 };
oc_ui_pattern_push(&ui->frameArena, &hoverPattern, (oc_ui_selector){ .kind = OC_UI_SEL_TAG, .tag = defaultTag });
oc_ui_pattern_push(&ui->frameArena, &hoverPattern, (oc_ui_selector){ .op = OC_UI_SEL_AND, .kind = OC_UI_SEL_STATUS, .status = OC_UI_HOVER });
oc_ui_style hoverStyle = { .bgColor = theme->fill0,
oc_ui_pattern hotPattern = { 0 };
oc_ui_pattern_push(&ui->frameArena, &hotPattern, (oc_ui_selector){ .kind = OC_UI_SEL_TAG, .tag = defaultTag });
oc_ui_pattern_push(&ui->frameArena, &hotPattern, (oc_ui_selector){ .op = OC_UI_SEL_AND, .kind = OC_UI_SEL_STATUS, .status = OC_UI_HOT });
oc_ui_style hotStyle = { .bgColor = theme->fill0,
.borderColor = theme->primary };
oc_ui_style_mask hoverMask = OC_UI_STYLE_BG_COLOR
oc_ui_style_mask hotMask = OC_UI_STYLE_BG_COLOR
| OC_UI_STYLE_BORDER_COLOR;
oc_ui_style_box_after(radio, hoverPattern, &hoverStyle, hoverMask);
oc_ui_style_box_after(radio, hotPattern, &hotStyle, hotMask);
oc_ui_pattern draggingPattern = { 0 };
oc_ui_pattern_push(&ui->frameArena, &draggingPattern, (oc_ui_selector){ .kind = OC_UI_SEL_TAG, .tag = defaultTag });
oc_ui_pattern_push(&ui->frameArena, &draggingPattern, (oc_ui_selector){ .op = OC_UI_SEL_AND, .kind = OC_UI_SEL_STATUS, .status = OC_UI_DRAGGING });
oc_ui_style draggingStyle = { .bgColor = theme->fill1,
oc_ui_pattern activePattern = { 0 };
oc_ui_pattern_push(&ui->frameArena, &activePattern, (oc_ui_selector){ .kind = OC_UI_SEL_TAG, .tag = defaultTag });
oc_ui_pattern_push(&ui->frameArena, &activePattern, (oc_ui_selector){ .op = OC_UI_SEL_AND, .kind = OC_UI_SEL_STATUS, .status = OC_UI_ACTIVE });
oc_ui_style activeStyle = { .bgColor = theme->fill1,
.borderColor = theme->primary };
oc_ui_style_mask draggingMask = OC_UI_STYLE_BG_COLOR
oc_ui_style_mask activeMask = OC_UI_STYLE_BG_COLOR
| OC_UI_STYLE_BORDER_COLOR;
oc_ui_style_box_after(radio, draggingPattern, &draggingStyle, draggingMask);
oc_ui_style_box_after(radio, activePattern, &activeStyle, activeMask);
oc_ui_tag selectedTag = oc_ui_tag_make(selectedTagStr);
oc_ui_pattern selectedPattern = { 0 };
@ -2784,17 +2811,17 @@ oc_ui_radio_group_info oc_ui_radio_group(const char* name, oc_ui_radio_group_inf
| OC_UI_STYLE_BG_COLOR;
oc_ui_style_box_before(radio, selectedPattern, &selectedStyle, selectedMask);
oc_ui_pattern selectedHoverPattern = { 0 };
oc_ui_pattern_push(&ui->frameArena, &selectedHoverPattern, (oc_ui_selector){ .kind = OC_UI_SEL_TAG, .tag = selectedTag });
oc_ui_pattern_push(&ui->frameArena, &selectedHoverPattern, (oc_ui_selector){ .op = OC_UI_SEL_AND, .kind = OC_UI_SEL_STATUS, .status = OC_UI_HOVER });
oc_ui_style selectedHoverStyle = { .bgColor = theme->primaryHover };
oc_ui_style_box_after(radio, selectedHoverPattern, &selectedHoverStyle, OC_UI_STYLE_BG_COLOR);
oc_ui_pattern selectedHotPattern = { 0 };
oc_ui_pattern_push(&ui->frameArena, &selectedHotPattern, (oc_ui_selector){ .kind = OC_UI_SEL_TAG, .tag = selectedTag });
oc_ui_pattern_push(&ui->frameArena, &selectedHotPattern, (oc_ui_selector){ .op = OC_UI_SEL_AND, .kind = OC_UI_SEL_STATUS, .status = OC_UI_HOT });
oc_ui_style selectedHotStyle = { .bgColor = theme->primaryHover };
oc_ui_style_box_after(radio, selectedHotPattern, &selectedHotStyle, OC_UI_STYLE_BG_COLOR);
oc_ui_pattern selectedDraggingPattern = { 0 };
oc_ui_pattern_push(&ui->frameArena, &selectedDraggingPattern, (oc_ui_selector){ .kind = OC_UI_SEL_TAG, .tag = selectedTag });
oc_ui_pattern_push(&ui->frameArena, &selectedDraggingPattern, (oc_ui_selector){ .op = OC_UI_SEL_AND, .kind = OC_UI_SEL_STATUS, .status = OC_UI_DRAGGING });
oc_ui_style selectedDraggingStyle = { .bgColor = theme->primaryActive };
oc_ui_style_box_after(radio, selectedDraggingPattern, &selectedDraggingStyle, OC_UI_STYLE_BG_COLOR);
oc_ui_pattern selectedActivePattern = { 0 };
oc_ui_pattern_push(&ui->frameArena, &selectedActivePattern, (oc_ui_selector){ .kind = OC_UI_SEL_TAG, .tag = selectedTag });
oc_ui_pattern_push(&ui->frameArena, &selectedActivePattern, (oc_ui_selector){ .op = OC_UI_SEL_AND, .kind = OC_UI_SEL_STATUS, .status = OC_UI_ACTIVE });
oc_ui_style selectedActiveStyle = { .bgColor = theme->primaryActive };
oc_ui_style_box_after(radio, selectedActivePattern, &selectedActiveStyle, OC_UI_STYLE_BG_COLOR);
oc_ui_container("label", 0)
{

View File

@ -388,12 +388,13 @@ typedef enum
typedef u8 oc_ui_status;
enum
enum oc_ui_status_enum
{
OC_UI_NONE = 0,
OC_UI_HOVER = 1 << 1,
OC_UI_ACTIVE = 1 << 2,
OC_UI_DRAGGING = 1 << 3,
OC_UI_HOT = 1 << 2,
OC_UI_ACTIVE = 1 << 3,
OC_UI_DRAGGING = 1 << 4,
};
typedef enum