dice-gui/src/dice_container.c

173 lines
4.5 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"
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_OnHover(&HandleAddDieButtonInteraction, die);
CLAY_TEXT(Dice_ToString(die), CLAY_TEXT_CONFIG({
.textColor = TextColors(0),
.H(2),
.textAlignment = CLAY_TEXT_ALIGN_CENTER
}));
}
}
static inline
void DiceSelectorContainer() {
CLAY(CLAY_ID("DiceSelector"), {
.PANEL(0),
.layout = {
.sizing = { CLAY_SIZING_FIT(), CLAY_SIZING_GROW() },
.childAlignment = { CLAY_ALIGN_X_CENTER, CLAY_ALIGN_Y_CENTER },
},
}) {
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 = panelPadding,
},
.clip = {
true, 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(index);
}
}
static inline
void RemoveDieButton(enum Dice_Die 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 },
},
.image = { GetDiceImage(die) },
}) {
size_t result_length;
struct Dice_ResultType const *result = Dice_GetLastResult(&result_length);
Clay_String string = {
.chars = result[index].string,
.length = result[index].string_len,
.isStaticallyAllocated = true
};
Clay_OnHover(&HandleRemoveDieButtonInteraction, index);
CLAY_TEXT(string, CLAY_TEXT_CONFIG({
.H(1),
.textColor = TextColors(0),
.textAlignment = CLAY_TEXT_ALIGN_CENTER,
}));
}
}
static inline
void ActiveDiceContainer() {
CLAY(CLAY_ID("ActiveDice"), {
.layout = {
.layoutDirection = CLAY_TOP_TO_BOTTOM,
.childAlignment = { CLAY_ALIGN_X_CENTER, CLAY_ALIGN_Y_CENTER },
.sizing = { CLAY_SIZING_GROW(), CLAY_SIZING_GROW() },
},
.PANEL(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 },
},
.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 = {
.layoutDirection = CLAY_LEFT_TO_RIGHT,
.childAlignment = { CLAY_ALIGN_X_CENTER, CLAY_ALIGN_Y_CENTER },
.childGap = 20,
.padding = { 0, 0, 0, 10 },
},
}) {
TextButton(CLAY_STRING("Roll"), proceedButton, &HandleRollSetButtonInteraction, 0);
CLAY_TEXT(Dice_GetLastResultTotal()->clay_string, CLAY_TEXT_CONFIG({
.H(3),
.textColor = TextColors(0),
}));
TextButton(CLAY_STRING("Clear"), warningButton, &HandleClearSetButtonInteraction, 0);
}
}
}
void DiceContainer() {
CLAY(CLAY_ID("DiceContainer"), {
.layout = {
.layoutDirection = CLAY_LEFT_TO_RIGHT,
.sizing = { CLAY_SIZING_GROW(), CLAY_SIZING_PERCENT(0.4) },
.childGap = containerGap
},
}) {
DiceSelectorContainer();
ActiveDiceContainer();
}
}