feat: fully implemented roll button

This commit is contained in:
Sara 2025-09-18 10:03:46 +02:00
parent 11e7e23962
commit 9e8933f2f1
8 changed files with 125 additions and 41 deletions

View file

@ -11,7 +11,7 @@ void DiceLogContainer() {
.sizing = layoutExpand,
.padding = CLAY_PADDING_ALL(16),
},
.CONTAINER(0),
.PANEL(0),
}) {}
}
@ -22,7 +22,7 @@ void InitiativeListContainer() {
.sizing = layoutExpand,
.padding = CLAY_PADDING_ALL(16),
},
.CONTAINER(0),
.PANEL(0)
}) {}
}

View file

@ -1,10 +1,8 @@
#include "dice.h"
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
int roll_die(enum die_type die) {
int const max = die - 1;
int const max = die;
return (rand() % max) + 1;
}
@ -28,6 +26,8 @@ size_t add_die_to_active(enum die_type die) {
return MAX_ACTIVE_DICE;
}
active_dice_set[current_active_count] = die;
roll_results[current_active_count].roll = die;
roll_results[current_active_count].string_len = SDL_snprintf(roll_results[current_active_count].string, MAX_ROLL_STR_LEN, "%d", die);
return current_active_count++;
}
@ -36,12 +36,20 @@ void remove_die_from_active(size_t index) {
--current_active_count;
}
void roll_active_dice_set(enum die_type die) {
void roll_active_dice_set() {
for (size_t i = 0; i < current_active_count; ++i) {
roll_results[i].roll = roll_die(active_dice_set[i]);
snprintf(roll_results[i].string, MAX_ROLL_STR_LEN, "%d", roll_results[i].roll);
roll_results[i].string_len = SDL_snprintf(roll_results[i].string, MAX_ROLL_STR_LEN, "%d", roll_results[i].roll);
roll_total.roll += roll_results[i].roll;
}
roll_total.string_len = SDL_snprintf(roll_total.string, MAX_ROLL_STR_LEN, "%d", roll_total.roll);
}
struct roll_result_type *get_current_results(size_t *out_length) {
if (out_length != nullptr) {
*out_length = current_active_count;
}
return roll_results;
}
Clay_String die_to_str(enum die_type die) {

View file

@ -35,9 +35,9 @@ extern enum die_type const *get_active_dice_set(size_t *out_length);
extern size_t add_die_to_active(enum die_type die);
extern void remove_die_from_active(size_t index);
extern struct roll_result_type *current_state(size_t *out_length);
extern struct roll_result_type *get_current_results(size_t *out_length);
extern void roll_active_dice_set(enum die_type die);
extern void roll_active_dice_set();
extern Clay_String die_to_str(enum die_type die);

View file

@ -2,9 +2,28 @@
#include <SDL3/SDL_mouse.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) {
roll_active_dice_set();
}
}
static inline
void RollSetButton() {
CLAY(CLAY_ID("RollSetButton"), {
.layout = {
.padding = { 30, 30, 10, 10 },
}
}) {
TextButton(CLAY_STRING("Roll"), buttonBackground, &HandleRollSetButtonInteraction, 0);
}
}
static
void HandleAddDieButtonInteraction(Clay_ElementId element, Clay_PointerData pointer, intptr_t die) {
if (pointer.state == CLAY_POINTER_DATA_PRESSED_THIS_FRAME) {
@ -32,7 +51,7 @@ void AddDieButton(enum die_type die) {
static inline
void DiceSelectorContainer() {
CLAY(CLAY_ID("DiceSelector"), {
.CONTAINER(0),
.PANEL(0),
.layout = {
.sizing = { CLAY_SIZING_FIT(), CLAY_SIZING_GROW() },
.childAlignment = { CLAY_ALIGN_X_CENTER, CLAY_ALIGN_Y_CENTER },
@ -44,7 +63,7 @@ void DiceSelectorContainer() {
.childGap = paragraphGap,
.childAlignment = { CLAY_ALIGN_X_CENTER, CLAY_ALIGN_Y_CENTER },
.sizing = { CLAY_SIZING_FIT(), CLAY_SIZING_FIT() },
.padding = containerPadding,
.padding = panelPadding,
},
.clip = {
false, true, Clay_GetScrollOffset(),
@ -78,8 +97,15 @@ void RemoveDieButton(enum die_type die, int index) {
},
.backgroundColor = DieButtonColor(die, Clay_Hovered()),
}) {
size_t result_length;
struct roll_result_type const *result = get_current_results(&result_length);
Clay_String string = {
.chars = result[index].string,
.length = result[index].string_len,
.isStaticallyAllocated = true
};
Clay_OnHover(&HandleRemoveDieButtonInteraction, index);
CLAY_TEXT(die_to_str(die), CLAY_TEXT_CONFIG({
CLAY_TEXT(string, CLAY_TEXT_CONFIG({
.H(1),
.textColor = TextColors(0)
}));
@ -90,13 +116,11 @@ 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() },
},
.clip = {
true, true,
{ Clay_GetScrollOffset().x, 0 },
},
.CONTAINER(0),
.PANEL(0),
}) {
CLAY(CLAY_ID("ActiveDiceInner"), {
.layout = {
@ -106,6 +130,10 @@ void ActiveDiceContainer() {
.childGap = 16,
.padding = { 100, 100, 0, 0 },
},
.clip = {
true, true,
{ Clay_GetScrollOffset().x, 0 },
},
}) {
size_t dice_count = 0;
enum die_type const *dice = get_active_dice_set(&dice_count);
@ -113,6 +141,7 @@ void ActiveDiceContainer() {
RemoveDieButton(dice[i], i);
}
}
RollSetButton();
}
}

20
src/elements.c Normal file
View file

@ -0,0 +1,20 @@
#include "elements.h"
#include "style.h"
void TextButton(Clay_String text, Clay_Color color, OnHoveredFn onHovered, intptr_t onHoveredData) {
CLAY_AUTO_ID({
.layout = {
.childAlignment = { CLAY_ALIGN_X_CENTER, CLAY_ALIGN_Y_CENTER },
.padding = buttonPadding,
},
.cornerRadius = buttonRadii,
.backgroundColor = Clay_Hovered() ? ToHoveredColor(color) : color,
}) {
CLAY_TEXT(text, CLAY_TEXT_CONFIG({
.BODY(),
.textColor = TextColors(0),
}));
Clay_OnHover(onHovered, onHoveredData);
}
}

10
src/elements.h Normal file
View file

@ -0,0 +1,10 @@
#ifndef ELEMENTS_H
#define ELEMENTS_H
#include <clay/clay.h>
typedef void(*OnHoveredFn)(Clay_ElementId element, Clay_PointerData pointer, intptr_t data);
extern void TextButton(Clay_String text, Clay_Color color, OnHoveredFn onHovered, intptr_t onHoveredData);
#endif // !ELEMENTS_H

View file

@ -3,20 +3,20 @@
#include "defs.h"
#include "dice.h"
Clay_Color ContainerBackgrounds(size_t idx) {
Clay_Color PanelBackground(size_t idx) {
return (Clay_Color) {
255*containerBackgrounds[idx],
255*containerBackgrounds[idx],
255*containerBackgrounds[idx],
255*panelBackground[idx],
255*panelBackground[idx],
255*panelBackground[idx],
255
};
}
Clay_Color ContainerBorders(size_t idx) {
Clay_Color PanelBorder(size_t idx) {
return (Clay_Color) {
255*containerBorders[idx],
255*containerBorders[idx],
255*containerBorders[idx],
255*panelBorder[idx],
255*panelBorder[idx],
255*panelBorder[idx],
255
};
}
@ -61,12 +61,13 @@ Clay_Color DieColor(enum die_type die) {
}
Clay_Color DieButtonColor(enum die_type die, bool selected) {
Clay_Color color = DieColor(die);
if (selected) {
float avg = (color.r + color.g + color.b) / 3.f;
color.r = (color.r - avg) * 0.8f + avg - 30;
color.g = (color.g - avg) * 0.8f + avg - 30;
color.b = (color.b - avg) * 0.8f + avg - 30;
}
return selected ? ToHoveredColor(DieColor(die)) : DieColor(die);
}
Clay_Color ToHoveredColor(Clay_Color color) {
float avg = (color.r + color.g + color.b) / 3.f;
color.r = (color.r - avg) * 0.8f + avg - 30;
color.g = (color.g - avg) * 0.8f + avg - 30;
color.b = (color.b - avg) * 0.8f + avg - 30;
return color;
}

View file

@ -20,25 +20,25 @@ constexpr float windowBackground = 0.15f;
constexpr uint16_t containerGap = 10;
constexpr double defaultRadius = 5.0;
constexpr float containerBackgrounds[] = {
constexpr float panelBackground[] = {
.2f,
.3f,
.4f
};
constexpr float containerBorders[] = {
constexpr float panelBorder[] = {
.3f,
.4f,
.5f
};
constexpr Clay_Padding containerPadding = {
constexpr Clay_Padding panelPadding = {
24, 24,
24, 24,
};
#define CONTAINER(depth_)\
backgroundColor = ContainerBackgrounds(depth_),\
.border = { ContainerBorders(depth_), CLAY_BORDER_ALL(2) },\
#define PANEL(depth_)\
backgroundColor = PanelBackground(depth_),\
.border = { PanelBackground(depth_), CLAY_BORDER_ALL(2) },\
.cornerRadius = defaultRadiusAll
////////////////////////////////////
@ -58,7 +58,7 @@ constexpr uint16_t headerSizes[] = {
28, 16
};
#define BODY(color_)\
#define BODY()\
fontId = FONT_DEFAULT,\
.fontSize = baseFontSize
@ -66,6 +66,21 @@ constexpr uint16_t headerSizes[] = {
fontId = FONT_BOLD,\
.fontSize = headerSizes[(level_)-1]
////////////////////////////////////
// BUTTONS
////////////////////////////////////
constexpr Clay_Color buttonBackground = {
177, 56, 52, 255
};
constexpr Clay_Padding buttonPadding = {
24, 24,
4, 4,
};
constexpr Clay_CornerRadius buttonRadii = {
3, 3, 3, 3
};
////////////////////////////////////
// COMPILATIONS
// | Functions and expressions that combine styling data from the settings above.
@ -81,12 +96,13 @@ constexpr Clay_CornerRadius defaultRadiusAll = {
defaultRadius, defaultRadius
};
extern Clay_Color ContainerBackgrounds(size_t idx);
extern Clay_Color ContainerBorders(size_t idx);
extern Clay_Color PanelBackground(size_t idx);
extern Clay_Color PanelBackground(size_t idx);
extern Clay_Color TextColors(size_t idx);
extern Clay_Color WindowBackground();
extern Clay_ElementDeclaration WindowStyle();
extern Clay_Color DieColor(enum die_type die);
extern Clay_Color DieButtonColor(enum die_type die, bool selected);
extern Clay_Color ToHoveredColor(Clay_Color color);
#endif // !STYLE_H