Gama C Library
Gama C API Documentation
Loading...
Searching...
No Matches
switch.h
Go to the documentation of this file.
1#pragma once
2
3#include "../animate.h"
4#include "../collision.h"
5#include "../draw.h"
6
7/**
8 * @brief Structure defining the visual theme for a switch widget.
9 */
10typedef struct {
11 int enabled; /**< Whether the switch is enabled */
12
13 double scale; /**< Default scale of the switch */
14
15 struct {
16 gmColor background; /**< Background color when switched off */
17 gmColor border; /**< Border color when switched off */
18 gmColor knob; /**< Knob color when switched off */
19 gmColor knob_border; /**< Knob border color when switched off */
20 } off;
21
22 struct {
23 gmColor background; /**< Background color when switched on */
24 gmColor border; /**< Border color when switched on */
25 gmColor knob; /**< Knob color when switched on */
26 gmColor knob_border; /**< Knob border color when switched on */
27 } on;
28
29 struct {
30 double scale; /**< Scale factor when focused/hovered */
31 gmColor border; /**< Border color when focused/hovered */
32 } focussed;
33
34 struct {
35 double scale; /**< Scale factor when active pressed */
36 gmColor border; /**< Border color when active pressed */
37 } active;
38
39 double border_width; /**< Width of the switch border */
41
42/**
43 * @brief Global switch theme instance with default values.
44 */
45gmwSwitchTheme gmwSwitch = {.enabled = 1,
46
47 // OFF state (muted)
48 .off =
49 {
50 .background = 0x2A1E2AE0,
51 .border = 0x5F4F5FFF,
52 .knob = 0x8A7A8AFF,
53 .knob_border = 0x5F4F5FFF,
54 },
55
56 // ON state (clear & branded)
57 .on =
58 {
59 .background = 0x4A2A4AE8,
60 .border = 0xAA77AAFF,
61 .knob = 0xEED6EEFF,
62 .knob_border = 0xAA77AAFF,
63 },
64
65 // Hover
66 .focussed =
67 {
68 .scale = 1.04,
69 .border = 0xBA87BAFF,
70 },
71
72 // Pressed
73 .active =
74 {
75 .scale = 0.97,
76 .border = 0x7F4F7FFF,
77 },
78
79 .border_width = 0.01};
80
81/**
82 * @brief Creates and renders an animated switch widget that toggles on click.
83 * @param x The x-coordinate of the switch's center.
84 * @param y The y-coordinate of the switch's center.
85 * @param width The width of the switch.
86 * @param height The height of the switch.
87 * @param value Pointer to an integer to store the switch state (0=off, 1=on).
88 * @param anim Pointer to a double for animated visual position (can be NULL to
89 * use value).
90 * @return 1 if the switch was clicked (toggled), 0 otherwise.
91 */
92int gmw_switch_anim(double x, double y, double width, double height, int *value,
93 double *anim) {
94
95 int enabled = gmwSwitch.enabled;
96
97 int hovered = enabled && gm_mouse_in_rect(x, y, width, height);
98 int clicked = enabled && gm_mouse.pressed && hovered;
99
100 if (clicked) {
101 *value = !(*value);
102 }
103 double knob_pos;
104 if (anim != NULL) {
105 if (*anim > 1)
106 *anim = 1;
107 else if (*anim < 0)
108 *anim = 0;
109 gm_anim_ease_out_quad(anim, (double)(*value), 0.05);
110 knob_pos = *anim;
111 } else {
112 knob_pos = (double)(*value);
113 }
114 int on = *value != 0;
115
116 double scale = gm_mouse.down && hovered ? gmwSwitch.active.scale
117 : hovered ? gmwSwitch.focussed.scale
118 : 1.0;
119
120 gmColor bg = on ? gmwSwitch.on.background : gmwSwitch.off.background;
121 gmColor border = on ? gmwSwitch.on.border : gmwSwitch.off.border;
122 gmColor knob = on ? gmwSwitch.on.knob : gmwSwitch.off.knob;
123 gmColor knob_b = on ? gmwSwitch.on.knob_border : gmwSwitch.off.knob_border;
124
125 if (hovered)
126 border = gmwSwitch.focussed.border;
127 if (gm_mouse.down && hovered)
128 border = gmwSwitch.active.border;
129
130 double sw = width * scale;
131 double sh = height * scale;
132
133 // Outer border
134 gm_draw_rectangle(x, y, sw + gmwSwitch.border_width * 2,
135 sh + gmwSwitch.border_width * 2, border);
136
137 // Background
138 gm_draw_rectangle(x, y, sw, sh, bg);
139
140 // Knob (square)
141 double knob_size = sh * 0.8;
142 double knob_x_offset = (knob_pos * 2 - 1) *
143 (sw * 0.5 - knob_size * 0.5 - gmwSwitch.border_width);
144 // Knob border
145 gm_draw_rectangle(x + knob_x_offset, y,
146 knob_size + gmwSwitch.border_width * 2,
147 knob_size + gmwSwitch.border_width * 2, knob_b);
148
149 // Knob
150 gm_draw_rectangle(x + knob_x_offset, y, knob_size, knob_size, knob);
151
152 return clicked;
153}
154
155/**
156 * @brief Creates and renders a switch widget that toggles on click.
157 * @param x The x-coordinate of the switch's center.
158 * @param y The y-coordinate of the switch's center.
159 * @param width The width of the switch.
160 * @param height The height of the switch.
161 * @param value Pointer to an integer to store the switch state (0=off, 1=on).
162 * @return 1 if the switch was clicked (toggled), 0 otherwise.
163 */
164int gmw_switch(double x, double y, double width, double height, int *value) {
165 return gmw_switch_anim(x, y, width, height, value, NULL);
166}
Functions for animating values with various easing functions.
void gm_anim_ease_out_quad(double *value, const double target, double t)
Starts fast and decelerates quadratically to the target. More pronounced than spring.
Definition animate.h:45
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
Functions for drawing shapes, text, and images.
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 switch widget.
Definition switch.h:10
double scale
Definition switch.h:13
gmColor knob
Definition switch.h:18
gmColor knob_border
Definition switch.h:19
double border_width
Definition switch.h:39
gmColor border
Definition switch.h:17
int enabled
Definition switch.h:11
gmColor background
Definition switch.h:16
int gmw_switch(double x, double y, double width, double height, int *value)
Creates and renders a switch widget that toggles on click.
Definition switch.h:164
int gmw_switch_anim(double x, double y, double width, double height, int *value, double *anim)
Creates and renders an animated switch widget that toggles on click.
Definition switch.h:92
gmwSwitchTheme gmwSwitch
Global switch theme instance with default values.
Definition switch.h:45