-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUI.h
More file actions
135 lines (102 loc) · 3.52 KB
/
UI.h
File metadata and controls
135 lines (102 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#ifndef _UI_H
#define _UI_H
#define text_width(text) strlen(text)
typedef struct {
int columns;
int rows;
int cellWidth;
int cellHeight;
int marginX;
int marginY;
int spacingX;
int spacingY;
int currentCellIndex;
} Grid;
Grid grid;
bool DrawButton(char* text, int y);
void BeginGrid(int columns, int rows, int cellWidth, int cellHeight, int marginX, int marginY, int spacingX, int spacingY);
void DrawImageEdgePoints(int SCREEN_WIDTH, int SCREEN_HEIGHT, Texture2D texture, int EDGE_SIZE);
Vector2 getNextGridPosition();
void EndGrid();
#endif // _UI_H
#ifdef UI_IMPLEMENTATION
const int EDGE_SIZE = 5;
bool DrawButton(char* text, int y)
{
Vector2 mousePoint = GetMousePosition();
bool buttonHovered = false;
Color buttonColor = WHITE;
Rectangle btn = { 20, y, 200, 50 };
// Check if mouse is hovering over the button
if (CheckCollisionPointRec(mousePoint, btn))
{
buttonHovered = true;
buttonColor = GRAY;
// If clicked
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
{
return true;
}
}
else
{
buttonHovered = false;
buttonColor = WHITE;
}
// 12ish pixels per character
float padding = (btn.width - (text_width(text)*12))/2;
DrawRectangleRec(btn, buttonColor);
DrawText(text, btn.x + padding, btn.y + 15, 20, buttonHovered ? WHITE : BLACK);
return false;
}
void BeginGrid(int columns, int rows, int cellWidth, int cellHeight, int marginX, int marginY, int spacingX, int spacingY) {
grid.columns = columns;
grid.rows = rows;
grid.cellWidth = cellWidth;
grid.cellHeight = cellHeight;
grid.marginX = marginX;
grid.marginY = marginY;
grid.spacingX = spacingX;
grid.spacingY = spacingY;
grid.currentCellIndex = 0; // Start at first cell
}
Vector2 getNextGridPosition()
{
int currentColumn = grid.currentCellIndex % grid.columns;
int currentRow = grid.currentCellIndex / grid.columns;
float x = grid.marginX + (currentColumn * (grid.cellWidth + grid.spacingX));
float y = grid.marginY + (currentRow * (grid.cellHeight + grid.spacingY));
grid.currentCellIndex++; // Move to next cell
return (Vector2){x, y};
}
void EndGrid()
{
grid.currentCellIndex = 0; // Reset grid index for next usage
}
void DrawImageEdgePoints(int SCREEN_WIDTH, int SCREEN_HEIGHT, Texture2D texture, int EDGE_SIZE)
{
}
Vector2 GetScaledDimensions(Texture2D texture, Rectangle bounds) {
float scale;
Vector2 newDimensions = {0};
// Calculate scale ratios for both width and height
float scaleX = bounds.width / texture.width;
float scaleY = bounds.height / texture.height;
// Use the smaller scale to ensure texture fits within bounds
scale = (scaleX < scaleY) ? scaleX : scaleY;
// Calculate new dimensions maintaining aspect ratio
newDimensions.x = texture.width * scale;
newDimensions.y = texture.height * scale;
return newDimensions;
}
void DrawTextureInBox(Texture2D texture, Rectangle bounds, Color tint) {
Vector2 dimensions = GetScaledDimensions(texture, bounds);
// Calculate position to center the texture in the bounds
float x = bounds.x + (bounds.width - dimensions.x) * 0.5f;
float y = bounds.y + (bounds.height - dimensions.y) * 0.5f;
// Draw the scaled texture
Rectangle source = (Rectangle){ 0, 0, texture.width, texture.height };
Rectangle dest = (Rectangle){ x, y, dimensions.x, dimensions.y };
DrawTexturePro(texture, source, dest, (Vector2){0, 0}, 0.0f, tint);
}
#endif // UI_IMPLEMENTATION