-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuicomponents.cpp
More file actions
123 lines (102 loc) · 5.01 KB
/
uicomponents.cpp
File metadata and controls
123 lines (102 loc) · 5.01 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
#include "uicomponents.h"
#include "raylib.h"
#include "raymath.h"
#define SCL(val) ((float)(val) * aspectScale)
static bool isPointInRect(Vector2 point, Rectangle rect) {
return (point.x >= rect.x && point.x <= rect.x + rect.width && point.y >= rect.y && point.y <= rect.y + rect.height);
}
void Gradient::drawGradientRect(Shader shader, Rectangle rect, Color c1, Color c2, bool vertical) {
BeginShaderMode(shader);
Vector4 start = { c1.r/255.0f, c1.g/255.0f, c1.b/255.0f, c1.a/255.0f };
Vector4 end = { c2.r/255.0f, c2.g/255.0f, c2.b/255.0f, c2.a/255.0f };
Vector4 bounds = { rect.x, rect.y, rect.width, rect.height };
SetShaderValue(shader, GetShaderLocation(shader, "startColor"), &start, SHADER_UNIFORM_VEC4);
SetShaderValue(shader, GetShaderLocation(shader, "endColor"), &end, SHADER_UNIFORM_VEC4);
SetShaderValue(shader, GetShaderLocation(shader, "rectBounds"), &bounds, SHADER_UNIFORM_VEC4);
int vertFlag = vertical ? 1 : 0;
SetShaderValue(shader, GetShaderLocation(shader, "vertical"), &vertFlag, SHADER_UNIFORM_INT);
DrawRectangleRec(rect, WHITE);
EndShaderMode();
}
void Button::drawOutline(int offsetX, int offsetY, float aspectScale, Color outlineColor, Color arrowColor) {
Vector2 outlinePoints[6] = {
Vector2{offsetX + SCL(x + 1), offsetY + SCL(y + 1)},
Vector2{offsetX + SCL(x + 1), offsetY + SCL(y + height - 1)},
Vector2{offsetX + SCL(x + width - 25), offsetY + SCL(y + height - 1)},
Vector2{offsetX + SCL(x + width - 1), offsetY + SCL(y + height - 21)},
Vector2{offsetX + SCL(x + width - 1), offsetY + SCL(y + 1)},
Vector2{offsetX + SCL(x + 1) + 1, offsetY + SCL(y + 1)},
};
DrawSplineLinear(outlinePoints, 6, 2 * aspectScale, outlineColor);
Vector2 arrowPoints[6] = {
Vector2{offsetX + SCL(x + width - 36), offsetY + SCL(y + 18)},
Vector2{offsetX + SCL(x + width - 36), offsetY + SCL(y + 18 + (45.0f / 8.0f))},
Vector2{offsetX + SCL(x + width - 20), offsetY + SCL(y + 33)},
Vector2{offsetX + SCL(x + width - 26), offsetY + SCL(y + 33)},
Vector2{offsetX + SCL(x + width - 36), offsetY + SCL(y + 48)},
Vector2{offsetX + SCL(x + width - 36), offsetY + SCL(y + 48 - (45.0f / 8.0f))},
};
DrawTriangleStrip(arrowPoints, 6, arrowColor);
}
void Button::drawSolid(int offsetX, int offsetY, float aspectScale, Color shapeColor, Color arrowColor) {
Vector2 outlinePoints[5] = {
Vector2{offsetX + SCL(x), offsetY + SCL(y)},
Vector2{offsetX + SCL(x), offsetY + SCL(y + height)},
Vector2{offsetX + SCL(x + width - 24), offsetY + SCL(y + height)},
Vector2{offsetX + SCL(x + width), offsetY + SCL(y + height - 20)},
Vector2{offsetX + SCL(x + width), offsetY + SCL(y)},
};
DrawTriangleFan(outlinePoints, 5, shapeColor);
Vector2 arrowPoints[6] = {
Vector2{offsetX + SCL(x + width - 36), offsetY + SCL(y + 18)},
Vector2{offsetX + SCL(x + width - 36), offsetY + SCL(y + 18 + (45.0f / 8.0f))},
Vector2{offsetX + SCL(x + width - 20), offsetY + SCL(y + 33)},
Vector2{offsetX + SCL(x + width - 26), offsetY + SCL(y + 33)},
Vector2{offsetX + SCL(x + width - 36), offsetY + SCL(y + 48)},
Vector2{offsetX + SCL(x + width - 36), offsetY + SCL(y + 48 - (45.0f / 8.0f))},
};
DrawTriangleStrip(arrowPoints, 6, arrowColor);
}
void Button::drawBase(int offsetX, int offsetY, float aspectScale, ColorStyle colorStyle) {
if (colorStyle.solid) {
drawSolid(offsetX, offsetY, aspectScale, colorStyle.color1, colorStyle.color2);
} else {
drawOutline(offsetX, offsetY, aspectScale, colorStyle.color1, colorStyle.color2);
}
if (fontPtr && fontPtr->texture.id != 0) { // checking if font is valid
float fontSize = SCL(30);
Vector2 textSize = MeasureTextEx(*fontPtr, text.c_str(), fontSize, 1);
float textX = offsetX + SCL(x + width - 50) - textSize.x; // some px offset, trying 60px from right for now
float textY = offsetY + SCL(y) + (SCL(height) - textSize.y) / 2; // vertically centered
Color textColor = colorStyle.color2;
DrawTextEx(*fontPtr, text.c_str(), Vector2{textX, textY}, fontSize, 1, textColor);
}
};
void Button::draw(int offsetX, int offsetY, float aspectScale, Vector2 mousePos, bool mouseClicked) {
Rectangle buttonRect = { // button rectangle for detection
offsetX + SCL(x),
offsetY + SCL(y),
SCL(width),
SCL(height)
};
bool isHovered = isPointInRect(mousePos, buttonRect); // for hover
if (isSelected || isHovered) {
drawBase(offsetX, offsetY, aspectScale, selectedStyle);
} else {
drawBase(offsetX, offsetY, aspectScale, normalStyle);
}
if (isSelected && selectedStyle.showAccent) {
DrawRectangle(offsetX + SCL(x + width + 18), offsetY + SCL(y), SCL(4), SCL(45), selectedStyle.color1); // vertical accent
}
}
bool Button::clicked(int offsetX, int offsetY, float aspectScale, Vector2 mousePos, bool mouseClicked) {
Rectangle buttonRect = { // button rectangle for detection
offsetX + SCL(x),
offsetY + SCL(y),
SCL(width),
SCL(height)
};
bool isHovered = isPointInRect(mousePos, buttonRect); // for hover
return isHovered && mouseClicked; // conditions for click
}
#undef SCL