From a8c79ebac5f1d1e7d317ce8c0e228544e9d34d5e Mon Sep 17 00:00:00 2001 From: Sara Date: Wed, 17 Sep 2025 21:59:25 +0200 Subject: [PATCH] feat: added logic for dice adding and removing from active set --- src/dice_container.c | 114 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 101 insertions(+), 13 deletions(-) diff --git a/src/dice_container.c b/src/dice_container.c index 187a09b..aa98c8b 100644 --- a/src/dice_container.c +++ b/src/dice_container.c @@ -1,38 +1,126 @@ #include "dice_container.h" +#include #include +#include #include "style.h" +#include "dice.h" + +static +void HandleAddDieButtonInteraction(Clay_ElementId element, Clay_PointerData pointer, intptr_t die) { + if (pointer.state == CLAY_POINTER_DATA_PRESSED_THIS_FRAME) { + add_die_to_active((enum die_type)die); + } +} + +static inline +void AddDieButton(enum die_type die) { + CLAY(CLAY_IDI("AddDieButton", die), { + .layout = { + .sizing = { CLAY_SIZING_FIXED(100), CLAY_SIZING_FIXED(100) }, + .childAlignment = { CLAY_ALIGN_X_CENTER, CLAY_ALIGN_Y_CENTER }, + }, + .backgroundColor = DieButtonColor(die, Clay_Hovered()), + }) { + Clay_OnHover(&HandleAddDieButtonInteraction, die); + CLAY_TEXT(die_to_str(die), CLAY_TEXT_CONFIG({ + .textColor = TextColors(0), + .H(2), + })); + } +} static inline void DiceSelectorContainer() { CLAY(CLAY_ID("DiceSelector"), { + .CONTAINER(0), .layout = { - .layoutDirection = CLAY_TOP_TO_BOTTOM, - .sizing = { CLAY_SIZING_PERCENT(0.15), CLAY_SIZING_GROW(0) }, - .padding = containerPadding, - .childGap = paragraphGap, + .sizing = { CLAY_SIZING_FIT(), CLAY_SIZING_GROW() }, + .childAlignment = { CLAY_ALIGN_X_CENTER, CLAY_ALIGN_Y_CENTER }, }, - .INNER_CONTAINER(0), - }) { } + }) { + CLAY(CLAY_ID("DiceSelectorInner"), { + .layout = { + .layoutDirection = CLAY_TOP_TO_BOTTOM, + .childGap = paragraphGap, + .childAlignment = { CLAY_ALIGN_X_CENTER, CLAY_ALIGN_Y_CENTER }, + .sizing = { CLAY_SIZING_FIT(), CLAY_SIZING_FIT() }, + .padding = containerPadding, + }, + .clip = { + false, true, Clay_GetScrollOffset(), + }, + }) { + AddDieButton(COIN); + AddDieButton(D4); + AddDieButton(D6); + AddDieButton(D8); + AddDieButton(D10); + AddDieButton(D12); + AddDieButton(D20); + AddDieButton(D100); + } + } +} + +static +void HandleRemoveDieButtonInteraction(Clay_ElementId element, Clay_PointerData pointer, intptr_t index) { + if (pointer.state == CLAY_POINTER_DATA_PRESSED_THIS_FRAME) { + remove_die_from_active(index); + } +} + +static inline +void RemoveDieButton(enum die_type die, int index) { + CLAY(CLAY_IDI("RemoveDieButton", index), { + .layout = { + .sizing = { CLAY_SIZING_FIXED(200), CLAY_SIZING_FIXED(200) }, + .childAlignment = { CLAY_ALIGN_X_CENTER, CLAY_ALIGN_Y_CENTER }, + }, + .backgroundColor = DieButtonColor(die, Clay_Hovered()), + }) { + Clay_OnHover(&HandleRemoveDieButtonInteraction, index); + CLAY_TEXT(die_to_str(die), CLAY_TEXT_CONFIG({ + .H(1), + .textColor = TextColors(0) + })); + } } static inline void ActiveDiceContainer() { CLAY(CLAY_ID("ActiveDice"), { .layout = { - .layoutDirection = CLAY_TOP_TO_BOTTOM, - .sizing = { CLAY_SIZING_GROW(0), CLAY_SIZING_GROW(0) }, - .padding = containerPadding, - .childGap = paragraphGap, + .sizing = { CLAY_SIZING_GROW(), CLAY_SIZING_GROW() }, }, - .INNER_CONTAINER(0), - }) { } + .clip = { + true, true, + { Clay_GetScrollOffset().x, 0 }, + }, + .CONTAINER(0), + }) { + CLAY(CLAY_ID("ActiveDiceInner"), { + .layout = { + .sizing = { CLAY_SIZING_GROW(), CLAY_SIZING_GROW() }, + .layoutDirection = CLAY_LEFT_TO_RIGHT, + .childAlignment = { CLAY_ALIGN_X_CENTER, CLAY_ALIGN_Y_CENTER }, + .childGap = 16, + .padding = { 100, 100, 0, 0 }, + }, + }) { + size_t dice_count = 0; + enum die_type const *dice = get_active_dice_set(&dice_count); + for (size_t i = 0; i < dice_count; ++i) { + RemoveDieButton(dice[i], i); + } + } + } } void DiceContainer() { CLAY(CLAY_ID("DiceContainer"), { .layout = { .layoutDirection = CLAY_LEFT_TO_RIGHT, - .sizing = { CLAY_SIZING_GROW(0), CLAY_SIZING_PERCENT(0.4) }, + .sizing = { CLAY_SIZING_GROW(), CLAY_SIZING_PERCENT(0.4) }, .childGap = containerGap }, }) {