Gama C Library
Gama C API Documentation
frame.h
Go to the documentation of this file.
1/**
2 * @file widgets/frame.h
3 * @brief Defines the theme and functionality for a frame widget.
4 *
5 * This file provides structures for customizing the appearance of frames
6 * (panels) and a function to render them. Frames are typically used as
7 * containers for other UI elements or for displaying information.
8 */
9#pragma once
10
11#include "../collision.h"
12#include "../draw.h"
13
14/**
15 * @brief Structure defining the visual theme for a frame widget.
16 */
17typedef struct {
18 int enabled; /**< Whether the frame is interactive or displays focused state (1) or always displays inactive state (0). */
19
20 gmColor background; /**< Background color when normal. */
21 gmColor border; /**< Border color when normal. */
22 double scale; /**< Scale factor 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 factor when focused/hovered. */
28 } focussed; /**< Theme properties when the frame is focused or hovered. */
29
30 struct {
31 gmColor background; /**< Background color when inactive. */
32 gmColor border; /**< Border color when inactive. */
33 double scale; /**< Scale factor when inactive. */
34 } inactive; /**< Theme properties when the frame is inactive. */
35
36 double border_width; /**< Width of the frame border. */
38
39/**
40 * @brief Global frame theme instance with default values.
41 */
42gmwFrameTheme gmwFrame = {.enabled = 1,
43
44 // Normal (semi-transparent overlay)
45 .background = 0x2A1E2AE0, // dark purple, ~88% alpha
46 .border = 0xAA77AAAF,
47 .scale = 1.0,
48
49 // Focused (menu panel highlighted)
50 .focussed =
51 {
52 .background = 0x3A2A3AE8,
53 .border = 0xBA87BAAF,
54 .scale = 1.01,
55 },
56
57 // Inactive (background panels)
58 .inactive =
59 {
60 .background = 0x201820C8,
61 .border = 0x6F4F6FAF,
62 .scale = 1.0,
63 },
64
65 .border_width = 0.015};
66
67/**
68 * @brief Creates and renders a frame widget (a bordered panel).
69 *
70 * This function draws a rectangular frame at the specified coordinates,
71 * applying styling based on the global `gmwFrame` theme and its state
72 * (hovered/focused or normal).
73 *
74 * @param x The x-coordinate of the frame's center.
75 * @param y The y-coordinate of the frame's center.
76 * @param width The width of the frame.
77 * @param height The height of the frame.
78 * @return 1 if the frame is currently hovered over, 0 otherwise.
79 */
80int gmw_frame(double x, double y, double width, double height) {
81 int enabled = gmwFrame.enabled;
82 int hovered = enabled && gm_mouse_in_rect(x, y, width, height);
83
84 double scale = hovered ? gmwFrame.focussed.scale : gmwFrame.scale;
85
86 gmColor bg = hovered ? gmwFrame.focussed.background : gmwFrame.background;
87
88 gmColor border = hovered ? gmwFrame.focussed.border : gmwFrame.border;
89
90 double sw = width * scale;
91 double sh = height * scale;
92
93 // Border (centered, square)
94 gm_draw_rectangle(x, y, sw + gmwFrame.border_width * 2,
95 sh + gmwFrame.border_width * 2, border);
96
97 // Background (semi-transparent)
98 gm_draw_rectangle(x, y, sw, sh, bg);
99 return hovered;
100}
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
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_rectangle(double x, double y, double w, double h, gmColor c)
Draws a rectangle centered at a point.
Definition draw.h:57
gmwFrameTheme gmwFrame
Global frame theme instance with default values.
Definition frame.h:42
int gmw_frame(double x, double y, double width, double height)
Creates and renders a frame widget (a bordered panel).
Definition frame.h:80
Structure defining the visual theme for a frame widget.
Definition frame.h:17
double scale
Definition frame.h:22
int enabled
Definition frame.h:18
double border_width
Definition frame.h:36
gmColor background
Definition frame.h:20
gmColor border
Definition frame.h:21