Gama C Library
Gama C API Documentation
Loading...
Searching...
No Matches
button.h
Go to the documentation of this file.
1#pragma once
2
3#include "../collision.h"
4#include "../draw.h"
5
6/**
7 * @brief Structure defining the visual theme for a button widget.
8 */
9typedef struct {
10 int enabled; /**< Whether the button is enabled */
11
12 double scale; /**< Default scale of the button */
13 gmColor background; /**< Background color when normal */
14 gmColor border; /**< Border color when normal */
15 gmColor text; /**< Text color when normal */
16
17 struct {
18 gmColor background; /**< Background color when focused/hovered */
19 gmColor border; /**< Border color when focused/hovered */
20 double scale; /**< Scale when focused/hovered */
21 gmColor text; /**< Text color when focused/hovered */
22 } focussed;
23
24 struct {
25 gmColor background; /**< Background color when active/pressed */
26 gmColor border; /**< Border color when active/pressed */
27 double scale; /**< Scale when active/pressed */
28 gmColor text; /**< Text color when active/pressed */
29 } active;
30
31 struct {
32 gmColor background; /**< Background color when disabled */
33 gmColor border; /**< Border color when disabled */
34 gmColor text; /**< Text color when disabled */
35 } disabled;
36
37 double border_thickness; /**< Thickness of the button border */
38 const char *font; /**< Font used for button text */
40
41/**
42 * @brief Global button theme instance with default values.
43 */
44gmwButtonTheme gmwButton = {.enabled = 1,
45
46 // Normal
47 .background = 0xAA77AAFF,
48 .border = 0x7F4F7FFF,
49 .text = GM_WHITE,
50 .scale = 1.0,
51
52 // Focus / Hover
53 .focussed =
54 {
55 .background = 0xBA87BAFF,
56 .border = 0x9F6F9FFF,
57 .scale = 1.04,
58 .text = GM_WHITE,
59 },
60
61 // Active / Pressed
62 .active =
63 {
64 .background = 0x9B5F9BFF,
65 .border = 0x6F3F6FFF,
66 .scale = 0.97,
67 .text = GM_WHITE,
68 },
69
70 // Disabled
71 .disabled =
72 {
73 .background = 0x9A8F9AFF,
74 .border = 0x6E646EFF,
75 .text = 0xDDDDDDFF,
76 },
77
78 .border_thickness = 0.01,
79 .font = "default-ui"};
80
81/**
82 * @brief Creates and renders an interactive button widget.
83 * @param x The x-coordinate of the button's center.
84 * @param y The y-coordinate of the button's center.
85 * @param width The width of the button.
86 * @param height The height of the button.
87 * @param text The text to display on the button (can be NULL).
88 * @param fontsize The size of the text font.
89 * @return 1 if the button is currently hovered, 0 otherwise.
90 */
91int gmw_button(double x, double y, double width, double height,
92 const char *text, double fontsize) {
93
94 int enabled = gmwButton.enabled;
95
96 int hovered = enabled && gm_mouse_in_rect(x, y, width, height);
97 int clicked = enabled && gm_mouse.down && hovered;
98
99 double scale = !enabled ? gmwButton.scale
100 : clicked ? gmwButton.active.scale
101 : hovered ? gmwButton.focussed.scale
102 : gmwButton.scale;
103
104 gmColor bg = !enabled ? gmwButton.disabled.background
105 : clicked ? gmwButton.active.background
106 : hovered ? gmwButton.focussed.background
107 : gmwButton.background;
108
109 gmColor border = !enabled ? gmwButton.disabled.border
110 : clicked ? gmwButton.active.border
111 : hovered ? gmwButton.focussed.border
112 : gmwButton.border;
113
114 gmColor fg = !enabled ? gmwButton.disabled.text
115 : clicked ? gmwButton.active.text
116 : hovered ? gmwButton.focussed.text
117 : gmwButton.text;
118
119 // Scale expands symmetrically from the center
120 double sw = width * scale;
121 double sh = height * scale;
122
123 // Border (square, centered)
124 gm_draw_rectangle(x, y, sw + gmwButton.border_thickness * 2,
125 sh + gmwButton.border_thickness * 2, border);
126
127 // Background (centered)
128 gm_draw_rectangle(x, y, sw, sh, bg);
129
130 // Text (already center-based)
131 if (text) {
132 gm_draw_text(x, y, text, gmwButton.font, fontsize * scale, fg);
133 }
134
135 return hovered;
136}
int gmw_button(double x, double y, double width, double height, const char *text, double fontsize)
Creates and renders an interactive button widget.
Definition button.h:91
gmwButtonTheme gmwButton
Global button theme instance with default values.
Definition button.h:44
int gm_mouse_in_rect(const double x, const double y, const double w, const double h)
Definition collision.h:102
unsigned int gmColor
Type definition for color values in RGBA format.
Definition color.h:8
#define GM_WHITE
White color.
Definition color.h:851
Functions for drawing shapes, text, and images.
int32_t gm_draw_text(double x, double y, const char *text, const char *font, double font_size, gmColor c)
Draws text.
Definition draw.h:135
int32_t gm_draw_rectangle(double x, double y, double w, double h, gmColor c)
Draws a rectangle.
Definition draw.h:47
struct _gmMouse gm_mouse
Definition gapi.h:19
Structure defining the visual theme for a button widget.
Definition button.h:9
const char * font
Definition button.h:38
double border_thickness
Definition button.h:37
int enabled
Definition button.h:10
double scale
Definition button.h:12
gmColor text
Definition button.h:15
gmColor border
Definition button.h:14
gmColor background
Definition button.h:13