libgui 3.00
libgui is a GUI library for the GameCube, Wii, and Wii U
 
Loading...
Searching...
No Matches
GuiGlyph.h
1/****************************************************************************
2 * libgui
3 * Daryl Borth 2026
4 * GuiGlyph.h
5 ***************************************************************************/
6#pragma once
7
8#include <vector>
9#include <cmath>
10#include <algorithm>
11#include "GuiElement.h"
12#include "GuiPaint.h"
13#include "GuiTheme.h"
14
17enum class GLYPH {
21 DOWN,
22 LEFT,
23 RIGHT,
24 PLUS,
25 MINUS,
28 HOME
29};
30
31class GuiText;
32
37class GuiGlyph : public GuiElement
38{
39 public:
42 GuiGlyph(GLYPH kind, const char * letter = "");
43 ~GuiGlyph();
44 void draw() override;
45
48 static void size(GLYPH kind, float& w, float& h);
49
52 static void build(GuiMesh& mesh, GLYPH kind, float w, float h);
53
54 private:
55 GLYPH kind;
56 GuiText * text;
57 GuiMesh mesh;
58 float meshFeather;
59 std::vector<MeshVertex> scratch;
60};
61
62inline void GuiGlyph::size(GLYPH kind, float& w, float& h)
63{
64 const GuiTheme& theme = GuiTheme::current();
65 switch(kind)
66 {
67 case GLYPH::BUMPER: w = theme.glyphBumperWidth; h = theme.glyphBumperHeight; break;
68 default: w = theme.glyphSize; h = theme.glyphSize; break;
69 }
70}
71
72inline void GuiGlyph::build(GuiMesh& mesh, GLYPH kind, float w, float h)
73{
74 const GuiTheme& theme = GuiTheme::current();
75 const GuiFill light = GuiFill::solid(theme.text);
76 const GuiFill dark = GuiFill::solid(theme.textInverse); // the symbol on the badge: the ink that reads on theme.text
77 const GuiRect box = { 0.0f, 0.0f, w, h };
78 const float cx = w * 0.5f, cy = h * 0.5f, u = std::min(w, h) / 22.0f; // u = size relative to a 22-pixel glyph
79
80 switch(kind)
81 {
82 case GLYPH::BUMPER:
83 mesh.addRoundRect(box, GuiCorners(6.0f * u), light);
84 return;
85 case GLYPH::CAPTURE:
86 mesh.addRoundRect(box, GuiCorners(5.0f * u), light);
87 mesh.strokeEllipse(GuiRect{ cx - 6.0f * u, cy - 6.0f * u, 12.0f * u, 12.0f * u }, 1.6f * u, STROKE::INSIDE, dark);
88 return;
89 default:
90 break;
91 }
92
93 mesh.addEllipse(box, light);
94 switch(kind)
95 {
96 case GLYPH::UP:
97 case GLYPH::DOWN:
98 case GLYPH::LEFT:
99 case GLYPH::RIGHT:
100 {
101 // a triangle pointing up, turned to face the right way
102 const float a = kind == GLYPH::UP ? 0.0f : kind == GLYPH::RIGHT ? 90.0f : kind == GLYPH::DOWN ? 180.0f : 270.0f;
103 const float rad = a * 3.14159265f / 180.0f, c = cosf(rad), s = sinf(rad);
104 const float pts[3][2] = { { 0.0f, -5.5f * u }, { -5.0f * u, 4.0f * u }, { 5.0f * u, 4.0f * u } };
105 std::vector<GuiPoint> tri;
106 for(int i = 0; i < 3; i++)
107 tri.push_back(GuiPoint{ cx + pts[i][0] * c - pts[i][1] * s, cy + pts[i][0] * s + pts[i][1] * c });
108 mesh.addPolygon(tri, dark);
109 break;
110 }
111 case GLYPH::PLUS:
112 mesh.addRect(GuiRect{ cx - 5.5f * u, cy - 1.3f * u, 11.0f * u, 2.6f * u }, dark);
113 mesh.addRect(GuiRect{ cx - 1.3f * u, cy - 5.5f * u, 2.6f * u, 11.0f * u }, dark);
114 break;
115 case GLYPH::MINUS:
116 mesh.addRect(GuiRect{ cx - 5.5f * u, cy - 1.3f * u, 11.0f * u, 2.6f * u }, dark);
117 break;
118 case GLYPH::HOME:
119 {
120 std::vector<GuiPoint> house = { { cx, cy - 6.0f * u }, { cx + 6.5f * u, cy - 0.5f * u }, { cx + 4.8f * u, cy + 5.5f * u },
121 { cx - 4.8f * u, cy + 5.5f * u }, { cx - 6.5f * u, cy - 0.5f * u } };
122 mesh.addPolygon(house, dark);
123 break;
124 }
125 case GLYPH::STICK:
126 mesh.strokeEllipse(GuiRect{ 2.5f * u, 2.5f * u, w - 5.0f * u, h - 5.0f * u }, 1.2f * u, STROKE::INSIDE, dark);
127 break;
128 default:
129 break;
130 }
131}
GuiElement()
Constructor.
Definition GuiElement.cpp:9
GuiGlyph(GLYPH kind, const char *letter="")
Definition GuiGlyph.cpp:9
void draw() override
Called constantly to redraw the element.
Definition GuiGlyph.cpp:36
Accumulates shapes as a triangle list of MeshVertex.
Display, manage, and manipulate text in the GUI.
Definition GuiText.h:10
static void size(GLYPH kind, float &w, float &h)
Gets the natural size of a glyph, from the theme (glyphSize, glyphBumperWidth, glyphBumperHeight).
Definition GuiGlyph.h:62
GLYPH
Small control icons for lists and hints (the "A" in a circle, a bumper, a D-pad arrow).
Definition GuiGlyph.h:17
static void build(GuiMesh &mesh, GLYPH kind, float w, float h)
Builds a glyph filling a w x h box at the origin, in the theme's light-on-dark colors.
Definition GuiGlyph.h:72
@ BUMPER
A light rounded rectangle (L, R, ZL, ZR)
Definition GuiGlyph.h:19
@ BUTTON
A light circle (face buttons; the letter goes on it)
Definition GuiGlyph.h:18
@ STICK
A circle with an inner ring (stick click; the letter goes on it)
Definition GuiGlyph.h:26
@ CAPTURE
A rounded square with a ring.
Definition GuiGlyph.h:27
@ UP
D-pad arrows: a circle with a triangle.
Definition GuiGlyph.h:20
@ INSIDE
Entirely inside the edge (the shape keeps its size)
Definition GuiGeometry.h:59
Per-corner radii, in the order top-left, top-right, bottom-right, bottom-left.
Definition GuiGeometry.h:46
A solid color or a two-color linear gradient.
Definition GuiGeometry.h:66
static GuiFill solid(PixelColor c)
A single flat color.
A 2D point in design-canvas pixels.
Definition GuiGeometry.h:23
An axis-aligned rectangle (top-left origin) in design-canvas pixels.
Definition GuiGeometry.h:33
Colors, metrics and the style recipes built from them.
Definition GuiTheme.h:21
static GuiTheme & current()
The theme the rest of the library reads. The first call fills it in with GuiThemeInit().
Definition GuiTheme.cpp:35