Compare commits

...

8 commits

7 changed files with 205 additions and 56 deletions

View file

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

View file

@ -26,7 +26,8 @@ size_t add_die_to_active(enum die_type die) {
} }
void remove_die_from_active(size_t index) { void remove_die_from_active(size_t index) {
memmove(active_dice_set + index + 1, active_dice_set + index, MAX_ACTIVE_DICE - index - 1); memcpy(active_dice_set + index, active_dice_set + index + 1, MAX_ACTIVE_DICE - index - 1);
--current_active_count;
} }
struct roll_result_type roll_active_dice_set(enum die_type die) { struct roll_result_type roll_active_dice_set(enum die_type die) {
@ -42,3 +43,15 @@ struct roll_result_type roll_active_dice_set(enum die_type die) {
return results; return results;
} }
Clay_String die_to_str(enum die_type die) {
switch (die) {
case COIN: return CLAY_STRING("C");
case D4: return CLAY_STRING("4");
case D6: return CLAY_STRING("6");
case D8: return CLAY_STRING("8");
case D10: return CLAY_STRING("10");
case D12: return CLAY_STRING("12");
case D20: return CLAY_STRING("20");
case D100: return CLAY_STRING("100");
}
}

View file

@ -1,15 +1,17 @@
#ifndef DICE_H #ifndef DICE_H
#define DICE_H #define DICE_H
#include "renderer/clay_renderer_SDL3.h"
#include <stdlib.h> #include <stdlib.h>
#ifndef MAX_ACTIVE_DICE #ifndef MAX_ACTIVE_DICE
#define MAX_ACTIVE_DICE 5 #define MAX_ACTIVE_DICE 20
#endif #endif
enum die_type { enum die_type {
COIN = 2, COIN = 2,
D4 = 4, D4 = 4,
D6 = 6,
D8 = 8, D8 = 8,
D10 = 10, D10 = 10,
D12 = 12, D12 = 12,
@ -30,4 +32,6 @@ extern size_t add_die_to_active(enum die_type die);
extern void remove_die_from_active(size_t index); extern void remove_die_from_active(size_t index);
extern struct roll_result_type roll_active_dice_set(enum die_type die); extern struct roll_result_type roll_active_dice_set(enum die_type die);
extern Clay_String die_to_str(enum die_type die);
#endif // !DICE_H #endif // !DICE_H

View file

@ -1,38 +1,126 @@
#include "dice_container.h" #include "dice_container.h"
#include <SDL3/SDL_mouse.h>
#include <clay/clay.h> #include <clay/clay.h>
#include <stdint.h>
#include "style.h" #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 static inline
void DiceSelectorContainer() { void DiceSelectorContainer() {
CLAY(CLAY_ID("DiceSelector"), { CLAY(CLAY_ID("DiceSelector"), {
.CONTAINER(0),
.layout = { .layout = {
.layoutDirection = CLAY_TOP_TO_BOTTOM, .sizing = { CLAY_SIZING_FIT(), CLAY_SIZING_GROW() },
.sizing = { CLAY_SIZING_PERCENT(0.15), CLAY_SIZING_GROW(0) }, .childAlignment = { CLAY_ALIGN_X_CENTER, CLAY_ALIGN_Y_CENTER },
.padding = containerPadding,
.childGap = paragraphGap,
}, },
.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 static inline
void ActiveDiceContainer() { void ActiveDiceContainer() {
CLAY(CLAY_ID("ActiveDice"), { CLAY(CLAY_ID("ActiveDice"), {
.layout = { .layout = {
.layoutDirection = CLAY_TOP_TO_BOTTOM, .sizing = { CLAY_SIZING_GROW(), CLAY_SIZING_GROW() },
.sizing = { CLAY_SIZING_GROW(0), CLAY_SIZING_GROW(0) },
.padding = containerPadding,
.childGap = paragraphGap,
}, },
.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() { void DiceContainer() {
CLAY(CLAY_ID("DiceContainer"), { CLAY(CLAY_ID("DiceContainer"), {
.layout = { .layout = {
.layoutDirection = CLAY_LEFT_TO_RIGHT, .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 .childGap = containerGap
}, },
}) { }) {

View file

@ -1,4 +1,5 @@
#include <SDL3/SDL_hints.h> #include <SDL3/SDL_hints.h>
#include <SDL3/SDL_keycode.h>
#include <SDL3/SDL_oldnames.h> #include <SDL3/SDL_oldnames.h>
#include <clay/clay.h> #include <clay/clay.h>
#include "renderer/clay_renderer_SDL3.h" #include "renderer/clay_renderer_SDL3.h"
@ -48,7 +49,7 @@ void LogOutputResolution() {
SDL_Log("output size: %i, %d", w, h); SDL_Log("output size: %i, %d", w, h);
} }
static inline static
Clay_Dimensions MeasureText(Clay_StringSlice text, Clay_TextElementConfig *config, void *userData) { Clay_Dimensions MeasureText(Clay_StringSlice text, Clay_TextElementConfig *config, void *userData) {
TTF_Font **fonts = userData; TTF_Font **fonts = userData;
TTF_Font *font = fonts[config->fontId]; TTF_Font *font = fonts[config->fontId];
@ -57,10 +58,7 @@ Clay_Dimensions MeasureText(Clay_StringSlice text, Clay_TextElementConfig *confi
if (!TTF_GetStringSize(font, text.chars, text.length, &width, &height)) { if (!TTF_GetStringSize(font, text.chars, text.length, &width, &height)) {
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "MeasureText failed to measure text %s", SDL_GetError()); SDL_LogError(SDL_LOG_CATEGORY_ERROR, "MeasureText failed to measure text %s", SDL_GetError());
} }
return (Clay_Dimensions) { return (Clay_Dimensions) { width, height };
.width = text.length * config->fontSize,
.height = config->fontSize
};
} }
static static
@ -132,7 +130,14 @@ int main(int argc, char *argv[]) {
.textEngine = textEngine, .textEngine = textEngine,
}; };
SDL_Event event; SDL_Event event;
uint64_t startFrameTime = SDL_GetTicksNS();
double deltaTime = 0.0;
bool mouseButtonDown = false;
bool shiftDown = false;
while (running) { while (running) {
deltaTime = SDL_GetTicksNS() - startFrameTime;
startFrameTime = SDL_GetTicksNS();
Clay_Vector2 scrollMotion = { 0, 0 };
while (SDL_PollEvent(&event)) { while (SDL_PollEvent(&event)) {
HandleEvent(event); HandleEvent(event);
switch (event.type) { switch (event.type) {
@ -147,17 +152,32 @@ int main(int argc, char *argv[]) {
LogOutputResolution(); LogOutputResolution();
break; break;
case SDL_EVENT_MOUSE_WHEEL: case SDL_EVENT_MOUSE_WHEEL:
Clay_UpdateScrollContainers(true, (Clay_Vector2){ event.wheel.x, event.wheel.y }, 0.01f); if (shiftDown) {
scrollMotion = (Clay_Vector2) { event.wheel.y * 2.f, event.wheel.x * 2.f };
} else {
scrollMotion = (Clay_Vector2) { event.wheel.x * 2.f, event.wheel.y * 2.f };
}
break; break;
case SDL_EVENT_MOUSE_MOTION: case SDL_EVENT_MOUSE_MOTION:
Clay_SetPointerState((Clay_Vector2) { event.motion.x, event.motion.y }, event.motion.state & SDL_BUTTON_LEFT); Clay_SetPointerState((Clay_Vector2) { event.motion.x, event.motion.y }, mouseButtonDown);
break; break;
case SDL_EVENT_MOUSE_BUTTON_DOWN: case SDL_EVENT_MOUSE_BUTTON_DOWN:
Clay_SetPointerState((Clay_Vector2) { event.button.x, event.button.y }, event.button.button == SDL_BUTTON_LEFT); case SDL_EVENT_MOUSE_BUTTON_UP:
default: if (event.button.button == SDL_BUTTON_LEFT) {
mouseButtonDown = event.button.down;
Clay_SetPointerState((Clay_Vector2) { event.button.x, event.button.y }, mouseButtonDown);
}
break; break;
case SDL_EVENT_KEY_DOWN:
case SDL_EVENT_KEY_UP:
if (event.key.key == SDLK_LSHIFT || event.key.key == SDLK_RSHIFT) {
shiftDown = event.key.down;
}
break;
default: break;
} }
} }
Clay_UpdateScrollContainers(true, scrollMotion, deltaTime);
SDL_SetRenderDrawColor(renderer, 10, 10, 10, 255); SDL_SetRenderDrawColor(renderer, 10, 10, 10, 255);
SDL_RenderClear(renderer); SDL_RenderClear(renderer);
Clay_RenderCommandArray array = RenderApplication(); Clay_RenderCommandArray array = RenderApplication();

View file

@ -1,5 +1,7 @@
#include "style.h" #include "style.h"
#include "application.h"
#include "defs.h" #include "defs.h"
#include "dice.h"
Clay_Color ContainerBackgrounds(size_t idx) { Clay_Color ContainerBackgrounds(size_t idx) {
return (Clay_Color) { return (Clay_Color) {
@ -43,3 +45,28 @@ Clay_ElementDeclaration WindowStyle() {
.backgroundColor = WindowBackground() .backgroundColor = WindowBackground()
}; };
} }
Clay_Color DieColor(enum die_type die) {
switch(die) {
case COIN: return (Clay_Color) { 230, 184, 48, 255 };
case D4: return (Clay_Color) { 177, 56, 52, 255 };
case D6: return (Clay_Color) { 115, 177, 52, 255 };
case D8: return (Clay_Color) { 52, 177, 125, 255 };
case D10: return (Clay_Color) { 52, 177, 176, 255 };
case D12: return (Clay_Color) { 52, 93, 177, 255 };
case D20: return (Clay_Color) { 177, 52, 140, 255 };
case D100: return (Clay_Color) { 95, 52, 177, 255 };
default: return (Clay_Color) { 0, 0, 0, 255 };
}
}
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 color;
}

View file

@ -2,15 +2,23 @@
#define STYLE_H #define STYLE_H
#include "defs.h" #include "defs.h"
#include "dice.h"
#include <clay/clay.h> #include <clay/clay.h>
#include <stdint.h> #include <stdint.h>
////////////////////////////////////
// WINDOW STYLE
////////////////////////////////////
constexpr uint16_t windowPadding = 10;
constexpr float windowBackground = 0.15f;
//////////////////////////////////// ////////////////////////////////////
// CONTAINER STYLE // CONTAINER STYLE
//////////////////////////////////// ////////////////////////////////////
constexpr uint16_t containerGap = 10; constexpr uint16_t containerGap = 10;
constexpr double defaultRadius = 5.; constexpr double defaultRadius = 5.0;
constexpr float containerBackgrounds[] = { constexpr float containerBackgrounds[] = {
.2f, .2f,
@ -24,22 +32,14 @@ constexpr float containerBorders[] = {
}; };
constexpr Clay_Padding containerPadding = { constexpr Clay_Padding containerPadding = {
32, 32, 24, 24,
16, 16 24, 24,
}; };
#define INNER_CONTAINER(depth_)\ #define CONTAINER(depth_)\
backgroundColor = ContainerBackgrounds(depth_),\ backgroundColor = ContainerBackgrounds(depth_),\
.border = { ContainerBorders(depth_), CLAY_BORDER_ALL(2) },\ .border = { ContainerBorders(depth_), CLAY_BORDER_ALL(2) },\
.cornerRadius = defaultRadiusAll .cornerRadius = defaultRadiusAll
////////////////////////////////////
// WINDOW STYLE
////////////////////////////////////
constexpr uint16_t windowPadding = containerGap;
constexpr float windowBackground = .18f;
//////////////////////////////////// ////////////////////////////////////
// TEXT STYLE // TEXT STYLE
@ -58,30 +58,19 @@ constexpr uint16_t headerSizes[] = {
28, 16 28, 16
}; };
#define TEXT_STYLE(color_)\
textColor = TextColors(color_)
#define BODY(color_)\ #define BODY(color_)\
fontId = FONT_DEFAULT,\ fontId = FONT_DEFAULT,\
.fontSize = baseFontSize,\ .fontSize = baseFontSize
.TEXT_STYLE(color_)
#define H(level_, color_)\ #define H(level_)\
fontId = FONT_BOLD,\ fontId = FONT_BOLD,\
.fontSize = headerSizes[(level_)-1],\ .fontSize = headerSizes[(level_)-1]
.TEXT_STYLE(color_)
//////////////////////////////////// ////////////////////////////////////
// COMPILATIONS // COMPILATIONS
// | Functions and expressions that combine styling data from the settings above. // | Functions and expressions that combine styling data from the settings above.
//////////////////////////////////// ////////////////////////////////////
extern Clay_Color ContainerBackgrounds(size_t idx);
extern Clay_Color ContainerBorders(size_t idx);
extern Clay_Color TextColors(size_t idx);
extern Clay_Color WindowBackground();
extern Clay_ElementDeclaration WindowStyle();
constexpr Clay_Sizing layoutExpand = { constexpr Clay_Sizing layoutExpand = {
.width = CLAY_SIZING_GROW(0), .width = CLAY_SIZING_GROW(0),
.height = CLAY_SIZING_GROW(0) .height = CLAY_SIZING_GROW(0)
@ -92,4 +81,12 @@ constexpr Clay_CornerRadius defaultRadiusAll = {
defaultRadius, defaultRadius defaultRadius, defaultRadius
}; };
extern Clay_Color ContainerBackgrounds(size_t idx);
extern Clay_Color ContainerBorders(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);
#endif // !STYLE_H #endif // !STYLE_H