Gama C Library
Gama C API Documentation
button.h
Go to the documentation of this file.
1/**
2 * @file widgets/button.h
3 * @brief Defines the theme and functionality for a button widget.
4 *
5 * This file provides structures for customizing the appearance of buttons
6 * and a function to render an interactive button that responds to mouse input.
7 */
8#pragma once
9
10#include "../collision.h"
11#include "../draw.h"
12
13/**
14 * @brief Structure defining the visual theme for a button widget.
15 */
16typedef struct {
17 int enabled; /**< Whether the button is interactive (1) or disabled (0). */
18
19 double scale; /**< Default scale of the button. */
20 gmColor background; /**< Background color when normal. */
21 gmColor border; /**< Border color when normal. */
22 gmColor text; /**< Text color when normal. */
23
24 struct {
25 gmColor background; /**< Background color when focused/hovered. */
26 gmColor border; /**< Border color when focused/hovered. */
27 double scale; /**< Scale when focused/hovered. */
28 gmColor text; /**< Text color when focused/hovered. */
29 } focussed; /**< Theme properties when the button is focused or hovered. */
30
31 struct {
32 gmColor background; /**< Background color when active/pressed. */
33 gmColor border; /**< Border color when active/pressed. */
34 double scale; /**< Scale when active/pressed. */
35 gmColor text; /**< Text color when active/pressed. */
36 } active; /**< Theme properties when the button is active (mouse button down on it). */
37
38 struct {
39 gmColor background; /**< Background color when disabled. */
40 gmColor border; /**< Border color when disabled. */
41 gmColor text; /**< Text color when disabled. */
42 } disabled; /**< Theme properties when the button is disabled. */
43
44 double border_thickness; /**< Thickness of the button border. */
45 const char *font; /**< Font used for button text. */
47
48/**
49 * @brief Global button theme instance with default values.
50 */
51gmwButtonTheme gmwButton = {.enabled = 1,
52
53 // Normal
54 .background = 0xAA77AAFF,
55 .border = 0x7F4F7FFF,
56 .text = GM_WHITE,
57 .scale = 1.0,
58
59 // Focus / Hover
60 .focussed =
61 {
62 .background = 0xBA87BAFF,
63 .border = 0x9F6F9FFF,
64 .scale = 1.04,
65 .text = GM_WHITE,
66 },
67
68 // Active / Pressed
69 .active =
70 {
71 .background = 0x9B5F9BFF,
72 .border = 0x6F3F6FFF,
73 .scale = 0.97,
74 .text = GM_WHITE,
75 },
76
77 // Disabled
78 .disabled =
79 {
80 .background = 0x9A8F9AFF,
81 .border = 0x6E646EFF,
82 .text = 0xDDDDDDFF,
83 },
84
85 .border_thickness = 0.01,
86 .font = "default-ui"};
87
88/**
89 * @brief Creates and renders an interactive button widget.
90 *
91 * This function draws a button at the specified coordinates and handles
92 * its interactive states (normal, hovered, active, disabled) based on
93 * mouse input and the global `gmwButton` theme.
94 *
95 * @param x The x-coordinate of the button's center.
96 * @param y The y-coordinate of the button's center.
97 * @param width The width of the button.
98 * @param height The height of the button.
99 * @param text The text to display on the button (can be NULL).
100 * @param fontsize The size of the text font.
101 * @return 1 if the button is currently hovered over, 0 otherwise.
102 */
103int gmw_button(double x, double y, double width, double height,
104 const char *text, double fontsize) {
105
106 int enabled = gmwButton.enabled;
107
108 int hovered = enabled && gm_mouse_in_rect(x, y, width, height);
109 int clicked = enabled && gm_mouse.down && hovered;
110
111 double scale = !enabled ? gmwButton.scale
112 : clicked ? gmwButton.active.scale
113 : hovered ? gmwButton.focussed.scale
114 : gmwButton.scale;
115
116 gmColor bg = !enabled ? gmwButton.disabled.background
117 : clicked ? gmwButton.active.background
118 : hovered ? gmwButton.focussed.background
119 : gmwButton.background;
120
121 gmColor border = !enabled ? gmwButton.disabled.border
122 : clicked ? gmwButton.active.border
123 : hovered ? gmwButton.focussed.border
124 : gmwButton.border;
125
126 gmColor fg = !enabled ? gmwButton.disabled.text
127 : clicked ? gmwButton.active.text
128 : hovered ? gmwButton.focussed.text
129 : gmwButton.text;
130
131 // Scale expands symmetrically from the center
132 double sw = width * scale;
133 double sh = height * scale;
134
135 // Border (square, centered)
136 gm_draw_rectangle(x, y, sw + gmwButton.border_thickness * 2,
137 sh + gmwButton.border_thickness * 2, border);
138
139 // Background (centered)
140 gm_draw_rectangle(x, y, sw, sh, bg);
141
142 // Text (already center-based)
143 if (text) {
144 gm_draw_text(x, y, text, gmwButton.font, fontsize * scale, fg);
145 }
146
147 return hovered;
148}
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:103
gmwButtonTheme gmwButton
Global button theme instance with default values.
Definition button.h:51
Defines collision structures and provides functions for 2D collision detection.
int gm_mouse_in_rect(const double x, const double y, const double w, const double h)
Checks if the mouse cursor is currently within a given rectangular area.
Definition collision.h:172
#define GM_WHITE
White color.
Definition color.h:892
uint32_t gmColor
Type definition for color values, stored as a 32-bit unsigned integer. The color components are packe...
Definition color.h:13
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 centered at a point.
Definition draw.h:140
int32_t gm_draw_rectangle(double x, double y, double w, double h, gmColor c)
Draws a rectangle centered at a point.
Definition draw.h:57
struct _gmMouse gm_mouse
Definition mouse.h:32
Structure defining the visual theme for a button widget.
Definition button.h:16
const char * font
Definition button.h:45
double border_thickness
Definition button.h:44
int enabled
Definition button.h:17
double scale
Definition button.h:19
gmColor text
Definition button.h:22
gmColor border
Definition button.h:21
gmColor background
Definition button.h:20