dice-gui/src/dice_container.cpp

170 lines
4.6 KiB
C++

#include "dice_container.h"
#include <SDL3/SDL_mouse.h>
#include <SDL3_image/SDL_image.h>
#include <clay/clay.h>
#include <stdint.h>
#include "elements.h"
#include "style.h"
#include "dice.h"
#include "ui_data.h"
static
void HandleRollSetButtonInteraction(Clay_ElementId element, Clay_PointerData pointer, intptr_t data) {
if (pointer.state == CLAY_POINTER_DATA_PRESSED_THIS_FRAME) {
Dice_RollActiveSet();
}
}
static
void HandleClearSetButtonInteraction(Clay_ElementId element, Clay_PointerData pointer, intptr_t data) {
if (pointer.state == CLAY_POINTER_DATA_PRESSED_THIS_FRAME) {
Dice_ClearActiveSet();
}
}
static
void HandleAddDieButtonInteraction(Clay_ElementId element, Clay_PointerData pointer, intptr_t die) {
if (pointer.state == CLAY_POINTER_DATA_PRESSED_THIS_FRAME) {
Dice_AddToActiveSet((enum Dice_Die)die);
}
}
static inline
void AddDieButton(enum Dice_Die 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 },
},
.image = { GetDiceImage(die, Clay_Hovered()) },
}) {
Clay_OnHover(&HandleAddDieButtonInteraction, die);
CLAY_TEXT(Dice_ToString(die), CLAY_TEXT_CONFIG(Header(2, {
.textColor = TextColors(0),
.textAlignment = CLAY_TEXT_ALIGN_CENTER,
})));
}
}
static inline
void DiceSelectorContainer() {
CLAY(CLAY_ID("DiceSelector"), PanelContainer(0, (Clay_ElementDeclaration) {
.layout = {
.sizing = { CLAY_SIZING_FIT(), CLAY_SIZING_GROW() },
}
})) {
CLAY_AUTO_ID(ListContainer(0, {
.layout = {
.sizing = { CLAY_SIZING_GROW(), CLAY_SIZING_GROW() },
.padding = { 2, 2, 5, 5 },
.childAlignment = { CLAY_ALIGN_X_CENTER, CLAY_ALIGN_Y_TOP },
.layoutDirection = CLAY_TOP_TO_BOTTOM,
},
.clip = {
false, true, Clay_GetScrollOffset(),
},
})) {
AddDieButton(D4);
AddDieButton(D6);
AddDieButton(D8);
AddDieButton(D10);
AddDieButton(D12);
AddDieButton(D20);
AddDieButton(D100);
AddDieButton(COIN);
}
}
}
static
void HandleRemoveDieButtonInteraction(Clay_ElementId element, Clay_PointerData pointer, intptr_t index) {
if (pointer.state == CLAY_POINTER_DATA_PRESSED_THIS_FRAME) {
Dice_RemoveFromActiveSet((size_t)index);
}
}
static inline
void RemoveDieButton(enum Dice_Die die, size_t 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 },
},
.image = {
GetDiceImage(die, Clay_Hovered())
},
}) {
Clay_OnHover(&HandleRemoveDieButtonInteraction, (intptr_t)index);
size_t result_length;
struct Dice_ResultType const *result = Dice_GetLastResult(&result_length);
CLAY_TEXT(
UiData_StoreClayStr(Dice_ResultToString(result[index])),
CLAY_TEXT_CONFIG(Header(1, {
.textColor = TextColors(0),
.textAlignment = CLAY_TEXT_ALIGN_CENTER,
}
))
);
}
}
static inline
void ActiveDiceContainer() {
CLAY(CLAY_ID("ActiveDice"), PanelContainer(0, {
.layout = {
.sizing = { CLAY_SIZING_GROW(), CLAY_SIZING_GROW() },
.childAlignment = { CLAY_ALIGN_X_CENTER, CLAY_ALIGN_Y_CENTER },
.layoutDirection = CLAY_TOP_TO_BOTTOM,
},
})) {
CLAY(CLAY_ID("ActiveDiceInner"), {
.layout = {
.sizing = { CLAY_SIZING_FIT(), CLAY_SIZING_GROW() },
.padding = { 100, 100, 0, 0 },
.childGap = 16,
.childAlignment = { CLAY_ALIGN_X_LEFT, CLAY_ALIGN_Y_CENTER },
.layoutDirection = CLAY_LEFT_TO_RIGHT,
},
.clip = {
true, true, { Clay_GetScrollOffset().x, 0 }
}
}) {
size_t dice_count = 0;
enum Dice_Die const *dice = Dice_GetActiveSet(&dice_count);
for (size_t i = 0; i < dice_count; ++i) {
RemoveDieButton(dice[i], i);
}
}
CLAY(CLAY_ID("ActiveDiceControls"), {
.layout = {
.padding = { 0, 0, 0, 10 },
.childGap = 20,
.childAlignment = { CLAY_ALIGN_X_CENTER, CLAY_ALIGN_Y_CENTER },
.layoutDirection = CLAY_LEFT_TO_RIGHT,
},
}) {
TextButton(CLAY_STRING("Roll"), proceedButton, &HandleRollSetButtonInteraction, 0);
int result = Dice_GetLastResultTotal();
CLAY_TEXT(UiData_StoreClayStr(Dice_ResultToString({ result, NONE })),
CLAY_TEXT_CONFIG(Header(1, {
.textColor = TextColors(0),
}))
);
TextButton(CLAY_STRING("Clear"), warningButton, &HandleClearSetButtonInteraction, 0);
}
}
}
void DiceContainer() {
CLAY(CLAY_ID("DiceContainer"), {
.layout = {
.sizing = { CLAY_SIZING_GROW(), CLAY_SIZING_PERCENT(0.4) },
.childGap = containerGap,
.layoutDirection = CLAY_LEFT_TO_RIGHT,
},
}) {
DiceSelectorContainer();
ActiveDiceContainer();
}
}