libgui 2.01
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 switch(kind)
65 {
66 case GLYPH::BUMPER: w = 30.0f; h = 20.0f; break;
67 default: w = 22.0f; h = 22.0f; break;
68 }
69}
70
71inline void GuiGlyph::build(GuiMesh& mesh, GLYPH kind, float w, float h)
72{
73 const GuiTheme& theme = GuiTheme::current();
74 const GuiFill light = GuiFill::solid(theme.text);
75 const GuiFill dark = GuiFill::solid(theme.background);
76 const GuiRect box = { 0.0f, 0.0f, w, h };
77 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
78
79 switch(kind)
80 {
81 case GLYPH::BUMPER:
82 mesh.addRoundRect(box, GuiCorners(6.0f * u), light);
83 return;
84 case GLYPH::CAPTURE:
85 mesh.addRoundRect(box, GuiCorners(5.0f * u), light);
86 mesh.strokeEllipse(GuiRect{ cx - 6.0f * u, cy - 6.0f * u, 12.0f * u, 12.0f * u }, 1.6f * u, STROKE::INSIDE, dark);
87 return;
88 default:
89 break;
90 }
91
92 mesh.addEllipse(box, light);
93 switch(kind)
94 {
95 case GLYPH::UP:
96 case GLYPH::DOWN:
97 case GLYPH::LEFT:
98 case GLYPH::RIGHT:
99 {
100 // a triangle pointing up, turned to face the right way
101 const float a = kind == GLYPH::UP ? 0.0f : kind == GLYPH::RIGHT ? 90.0f : kind == GLYPH::DOWN ? 180.0f : 270.0f;
102 const float rad = a * 3.14159265f / 180.0f, c = cosf(rad), s = sinf(rad);
103 const float pts[3][2] = { { 0.0f, -5.5f * u }, { -5.0f * u, 4.0f * u }, { 5.0f * u, 4.0f * u } };
104 std::vector<GuiPoint> tri;
105 for(int i = 0; i < 3; i++)
106 tri.push_back(GuiPoint{ cx + pts[i][0] * c - pts[i][1] * s, cy + pts[i][0] * s + pts[i][1] * c });
107 mesh.addPolygon(tri, dark);
108 break;
109 }
110 case GLYPH::PLUS:
111 mesh.addRect(GuiRect{ cx - 5.5f * u, cy - 1.3f * u, 11.0f * u, 2.6f * u }, dark);
112 mesh.addRect(GuiRect{ cx - 1.3f * u, cy - 5.5f * u, 2.6f * u, 11.0f * u }, dark);
113 break;
114 case GLYPH::MINUS:
115 mesh.addRect(GuiRect{ cx - 5.5f * u, cy - 1.3f * u, 11.0f * u, 2.6f * u }, dark);
116 break;
117 case GLYPH::HOME:
118 {
119 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 },
120 { cx - 4.8f * u, cy + 5.5f * u }, { cx - 6.5f * u, cy - 0.5f * u } };
121 mesh.addPolygon(house, dark);
122 break;
123 }
124 case GLYPH::STICK:
125 mesh.strokeEllipse(GuiRect{ 2.5f * u, 2.5f * u, w - 5.0f * u, h - 5.0f * u }, 1.2f * u, STROKE::INSIDE, dark);
126 break;
127 default:
128 break;
129 }
130}
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:31
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.
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:71
@ 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
@ STICK
An analog stick: an outer ring with a cap that can move.
@ INSIDE
Entirely inside the edge (the shape keeps its size)
Definition GuiGeometry.h:53
Per-corner radii, in the order top-left, top-right, bottom-right, bottom-left.
Definition GuiGeometry.h:40
A solid color or a two-color linear gradient.
Definition GuiGeometry.h:60
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:30
Colors, metrics and the style recipes built from them.
Definition GuiTheme.h:16
static GuiTheme & current()
The theme the rest of the library reads. Starts as the default dark theme.
Definition GuiTheme.cpp:35