Gama C Library
Gama C API Documentation
Loading...
Searching...
No Matches
joystick.h
Go to the documentation of this file.
1#pragma once
2
3#include "../animate.h"
4#include "../draw.h"
5
6/**
7 * @brief Structure defining the visual theme for a joystick widget.
8 */
9typedef struct {
10 int enabled; /**< Whether the joystick is enabled */
11
12 double scale; /**< Overall joystick size scale */
13 gmColor background; /**< Circle background color */
14 gmColor border; /**< Border color */
15
16 struct {
17 double scale; /**< Scale factor when focused/hovered */
18 gmColor border; /**< Border color when focused/hovered */
19 } focussed;
20
21 struct {
22 double scale; /**< Scale factor when active pressed */
23 gmColor border; /**< Border color when active pressed */
24 } active;
25
26 gmColor knob; /**< Knob color */
27 gmColor knob_border; /**< Knob border color */
28
29 double border_width; /**< Width of the circle border */
31
32/**
33 * @brief Global joystick theme instance with default values.
34 */
36 .enabled = 1,
37 .scale = 1.0,
38 .background = 0x3A2A3AE0,
39 .border = 0x7F4F7FFF,
40 .focussed = {.scale = 1.05, .border = 0xAA77AAFF},
41 .active = {.scale = 0.95, .border = 0x7F4F7FFF},
42 .knob = 0xAA77AAFF,
43 .knob_border = 0x6F3F6FFF,
44 .border_width = 0.01};
45
46/**
47 * @brief Creates and renders an animated joystick widget that can be
48 * manipulated with the mouse.
49 * @param x The x-coordinate of the joystick's center.
50 * @param y The y-coordinate of the joystick's center.
51 * @param radius The radius of the joystick base.
52 * @param pos Pointer to a gmPos structure to store the joystick's logical
53 * position (-1 to 1).
54 * @param vpos Pointer to a gmPos structure for animated visual position (can be
55 * NULL to use pos).
56 * @return 1 if the joystick is currently hovered, 0 otherwise.
57 */
58int gm_joystick_anim(double x, double y, double radius, gmPos *pos,
59 gmPos *vpos) {
60
61 if (!gmwJoystick.enabled)
62 return 0;
63 if (vpos == NULL)
64 vpos = pos;
65
66 // Mouse vector relative to center
67 double dx = gm_mouse.position.x - x;
68 double dy = gm_mouse.position.y - y;
69
70 double dist = sqrt(dx * dx + dy * dy);
71
72 int hovered = dist <= radius;
73
74 int active = hovered && gm_mouse.down;
75
76 if (!gm_mouse.down) {
77 gm_anim_ease_out_cubic(&pos->x, 0, 0.1);
78 gm_anim_ease_out_cubic(&pos->y, 0, 0.1);
79 }
80
81 // Logical joystick position (-1..1)
82 double nx = 0.0, ny = 0.0;
83
84 if (active) {
85 if (dist > radius) {
86 dx = dx / dist * radius;
87 dy = dy / dist * radius;
88 }
89 nx = dx / radius;
90 ny = dy / radius;
91
92 pos->x = nx;
93 pos->y = ny;
94 }
95
96 // Animate knob smoothly to target pos->x/pos->y
97 gm_anim_ease_out_quad(&vpos->x, pos->x, 0.05);
98 gm_anim_ease_out_quad(&vpos->y, pos->y, 0.05);
99
100 // Draw joystick circle (border + background)
101 double draw_radius = radius * gmwJoystick.scale;
102 gm_draw_circle(x, y, draw_radius + gmwJoystick.border_width,
103 gmwJoystick.border);
104 gm_draw_circle(x, y, draw_radius, gmwJoystick.background);
105
106 // Draw knob
107 double knob_radius = draw_radius * 0.3; // 30% of radius
108 gm_draw_circle(x + vpos->x * draw_radius, y + vpos->y * draw_radius,
109 knob_radius + gmwJoystick.border_width,
110 gmwJoystick.knob_border);
112 x + vpos->x * draw_radius, y + vpos->y * draw_radius, knob_radius,
113 nx == 0 && ny == 0 ? gmwJoystick.knob & 0xEEEEFF44 : gmwJoystick.knob);
114
115 return hovered;
116}
117
118/**
119 * @brief Creates and renders a joystick widget that can be manipulated with the
120 * mouse.
121 * @param x The x-coordinate of the joystick's center.
122 * @param y The y-coordinate of the joystick's center.
123 * @param radius The radius of the joystick base.
124 * @param pos Pointer to a gmPos structure to store the joystick's logical
125 * position (-1 to 1).
126 * @return 1 if the joystick is currently hovered, 0 otherwise.
127 */
128int gmw_joystick(double x, double y, double radius, gmPos *pos) {
129 return gm_joystick_anim(x, y, radius, pos, NULL);
130}
Functions for animating values with various easing functions.
void gm_anim_ease_out_cubic(double *value, double target, double t)
Starts very fast and decelerates cubically to the target. More pronounced than quad.
Definition animate.h:66
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
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_circle(double center_x, double center_y, double radius, gmColor c)
Draws a circle.
Definition draw.h:76
struct _gmMouse gm_mouse
Definition gapi.h:19
int gmw_joystick(double x, double y, double radius, gmPos *pos)
Creates and renders a joystick widget that can be manipulated with the mouse.
Definition joystick.h:128
gmwJoystickTheme gmwJoystick
Global joystick theme instance with default values.
Definition joystick.h:35
int gm_joystick_anim(double x, double y, double radius, gmPos *pos, gmPos *vpos)
Creates and renders an animated joystick widget that can be manipulated with the mouse.
Definition joystick.h:58
Definition position.h:4
double x
Definition position.h:5
double y
Definition position.h:5
Structure defining the visual theme for a joystick widget.
Definition joystick.h:9
double border_width
Definition joystick.h:29
double scale
Definition joystick.h:12
gmColor background
Definition joystick.h:13
gmColor knob
Definition joystick.h:26
gmColor knob_border
Definition joystick.h:27
gmColor border
Definition joystick.h:14
int enabled
Definition joystick.h:10