GUI: Show icons in key remapping

This commit is contained in:
Jeffrey Pfau 2016-01-31 20:09:50 -08:00
parent d53004650d
commit 8e99508717
9 changed files with 111 additions and 20 deletions

View File

@ -70,6 +70,25 @@ unsigned GUIFontGlyphWidth(const struct GUIFont* font, uint32_t glyph) {
return defaultFontMetrics[glyph].width; return defaultFontMetrics[glyph].width;
} }
void GUIFontIconMetrics(const struct GUIFont* font, enum GUIIcon icon, unsigned* w, unsigned* h) {
UNUSED(font);
if (icon >= GUI_ICON_MAX) {
if (w) {
*w = 0;
}
if (h) {
*h = 0;
}
} else {
if (w) {
*w = defaultIconMetrics[icon].width;
}
if (h) {
*h = defaultIconMetrics[icon].height;
}
}
}
void GUIFontDrawGlyph(const struct GUIFont* font, int glyph_x, int glyph_y, uint32_t color, uint32_t glyph) { void GUIFontDrawGlyph(const struct GUIFont* font, int glyph_x, int glyph_y, uint32_t color, uint32_t glyph) {
ctrActivateTexture(&font->texture); ctrActivateTexture(&font->texture);
@ -133,5 +152,5 @@ void GUIFontDrawIconSize(const struct GUIFont* font, int x, int y, int w, int h,
} }
struct GUIIconMetric metric = defaultIconMetrics[icon]; struct GUIIconMetric metric = defaultIconMetrics[icon];
ctrAddRectScaled(color, x, y, w, h, metric.x, metric.y, metric.width, metric.height); ctrAddRectScaled(color, x, y, w ? w : metric.width, h ? h : metric.height, metric.x, metric.y, metric.width, metric.height);
} }

View File

@ -49,6 +49,25 @@ unsigned GUIFontGlyphWidth(const struct GUIFont* font, uint32_t glyph) {
return defaultFontMetrics[glyph].width * 2; return defaultFontMetrics[glyph].width * 2;
} }
void GUIFontIconMetrics(const struct GUIFont* font, enum GUIIcon icon, unsigned* w, unsigned* h) {
UNUSED(font);
if (icon >= GUI_ICON_MAX) {
if (w) {
*w = 0;
}
if (h) {
*h = 0;
}
} else {
if (w) {
*w = defaultIconMetrics[icon].width * 2;
}
if (h) {
*h = defaultIconMetrics[icon].height * 2;
}
}
}
void GUIFontDrawGlyph(const struct GUIFont* font, int x, int y, uint32_t color, uint32_t glyph) { void GUIFontDrawGlyph(const struct GUIFont* font, int x, int y, uint32_t color, uint32_t glyph) {
if (glyph > 0x7F) { if (glyph > 0x7F) {
glyph = '?'; glyph = '?';
@ -116,5 +135,5 @@ void GUIFontDrawIconSize(const struct GUIFont* font, int x, int y, int w, int h,
vita2d_draw_texture_tint_part_scale(font->icons, x, y, vita2d_draw_texture_tint_part_scale(font->icons, x, y,
metric.x * 2, metric.y * 2, metric.x * 2, metric.y * 2,
metric.width * 2, metric.height * 2, metric.width * 2, metric.height * 2,
w / (float) metric.width, h / (float) metric.height, color); w ? (w / (float) metric.width) : 1, h ? (h / (float) metric.height) : 1, color);
} }

View File

@ -136,10 +136,10 @@ int main() {
"R", "R",
0, // L2? 0, // L2?
0, // R2? 0, // R2?
"Triangle", "\1\xC",
"Circle", "\1\xA",
"Cross", "\1\xB",
"Square" "\1\xD"
}, },
.nKeys = 16 .nKeys = 16
}, },

View File

@ -65,6 +65,25 @@ unsigned GUIFontGlyphWidth(const struct GUIFont* font, uint32_t glyph) {
return defaultFontMetrics[glyph].width * 2; return defaultFontMetrics[glyph].width * 2;
} }
void GUIFontIconMetrics(const struct GUIFont* font, enum GUIIcon icon, unsigned* w, unsigned* h) {
UNUSED(font);
if (icon >= GUI_ICON_MAX) {
if (w) {
*w = 0;
}
if (h) {
*h = 0;
}
} else {
if (w) {
*w = defaultIconMetrics[icon].width * 2;
}
if (h) {
*h = defaultIconMetrics[icon].height * 2;
}
}
}
void GUIFontDrawGlyph(const struct GUIFont* font, int x, int y, uint32_t color, uint32_t glyph) { void GUIFontDrawGlyph(const struct GUIFont* font, int x, int y, uint32_t color, uint32_t glyph) {
color = (color >> 24) | (color << 8); color = (color >> 24) | (color << 8);
GXTexObj tex; GXTexObj tex;
@ -202,6 +221,13 @@ void GUIFontDrawIconSize(const struct GUIFont* font, int x, int y, int w, int h,
float u[4]; float u[4];
float v[4]; float v[4];
if (!h) {
h = metric.height * 2;
}
if (!w) {
w = metric.width * 2;
}
u[0] = u[3] = metric.x / 256.f; u[0] = u[3] = metric.x / 256.f;
u[1] = u[2] = (metric.x + metric.width) / 256.f; u[1] = u[2] = (metric.x + metric.width) / 256.f;
v[0] = v[1] = (metric.y + metric.height) / 64.f; v[0] = v[1] = (metric.y + metric.height) / 64.f;

View File

@ -264,15 +264,15 @@ int main(int argc, char* argv[]) {
"1", "1",
"B", "B",
"A", "A",
"Minus", "-",
0, 0,
0, 0,
"Home", "\1\xE",
"Left", "Left",
"Right", "Right",
"Down", "Down",
"Up", "Up",
"Plus", "+",
0, 0,
0, 0,
0, 0,
@ -311,9 +311,9 @@ int main(int argc, char* argv[]) {
"ZL", "ZL",
0, 0,
"R", "R",
"Plus", "+",
"Home", "\1\xE",
"Minus", "-",
"L", "L",
"Down", "Down",
"Right", "Right",

View File

@ -151,5 +151,5 @@ struct GUIIconMetric defaultIconMetrics[] = {
[GUI_ICON_BUTTON_CROSS] = { 18, 34, 12, 11 }, [GUI_ICON_BUTTON_CROSS] = { 18, 34, 12, 11 },
[GUI_ICON_BUTTON_TRIANGLE] = { 34, 34, 12, 11 }, [GUI_ICON_BUTTON_TRIANGLE] = { 34, 34, 12, 11 },
[GUI_ICON_BUTTON_SQUARE] = { 50, 34, 12, 11 }, [GUI_ICON_BUTTON_SQUARE] = { 50, 34, 12, 11 },
[GUI_ICON_BUTTON_HOME] = { 66, 34, 16, 16 }, [GUI_ICON_BUTTON_HOME] = { 66, 34, 12, 11 },
}; };

View File

@ -5,12 +5,23 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "util/gui/font.h" #include "util/gui/font.h"
#include "util/string.h"
unsigned GUIFontSpanWidth(const struct GUIFont* font, const char* text) { unsigned GUIFontSpanWidth(const struct GUIFont* font, const char* text) {
unsigned width = 0; unsigned width = 0;
size_t i; size_t len = strlen(text);
for (i = 0; text[i]; ++i) { while (len) {
char c = text[i]; uint32_t c = utf8Char(&text, &len);
width += GUIFontGlyphWidth(font, c); if (c == '\1') {
c = utf8Char(&text, &len);
if (c < GUI_ICON_MAX) {
unsigned w;
GUIFontIconMetrics(font, c, &w, 0);
width += w;
}
} else {
width += GUIFontGlyphWidth(font, c);
}
} }
return width; return width;
} }
@ -29,8 +40,18 @@ void GUIFontPrint(const struct GUIFont* font, int x, int y, enum GUIAlignment al
size_t len = strlen(text); size_t len = strlen(text);
while (len) { while (len) {
uint32_t c = utf8Char(&text, &len); uint32_t c = utf8Char(&text, &len);
GUIFontDrawGlyph(font, x, y, color, c); if (c == '\1') {
x += GUIFontGlyphWidth(font, c); c = utf8Char(&text, &len);
if (c < GUI_ICON_MAX) {
GUIFontDrawIcon(font, x, y, GUI_ALIGN_BOTTOM, GUI_ORIENT_0, color, c);
unsigned w;
GUIFontIconMetrics(font, c, &w, 0);
x += w;
}
} else {
GUIFontDrawGlyph(font, x, y, color, c);
x += GUIFontGlyphWidth(font, c);
}
} }
} }

View File

@ -75,6 +75,7 @@ struct GUIIconMetric {
unsigned GUIFontHeight(const struct GUIFont*); unsigned GUIFontHeight(const struct GUIFont*);
unsigned GUIFontGlyphWidth(const struct GUIFont*, uint32_t glyph); unsigned GUIFontGlyphWidth(const struct GUIFont*, uint32_t glyph);
unsigned GUIFontSpanWidth(const struct GUIFont*, const char* text); unsigned GUIFontSpanWidth(const struct GUIFont*, const char* text);
void GUIFontIconMetrics(const struct GUIFont*, enum GUIIcon icon, unsigned* w, unsigned* h);
ATTRIBUTE_FORMAT(printf, 6, 7) ATTRIBUTE_FORMAT(printf, 6, 7)
void GUIFontPrintf(const struct GUIFont*, int x, int y, enum GUIAlignment, uint32_t color, const char* text, ...); void GUIFontPrintf(const struct GUIFont*, int x, int y, enum GUIAlignment, uint32_t color, const char* text, ...);

View File

@ -165,8 +165,13 @@ enum GUIMenuExitReason GUIShowMenu(struct GUIParams* params, struct GUIMenu* men
if (itemsPerScreen < GUIMenuItemListSize(&menu->items)) { if (itemsPerScreen < GUIMenuItemListSize(&menu->items)) {
size_t top = 2 * lineHeight; size_t top = 2 * lineHeight;
size_t bottom = params->height - 8; size_t bottom = params->height - 8;
unsigned w;
unsigned right;
GUIFontIconMetrics(params->font, GUI_ICON_SCROLLBAR_BUTTON, &right, 0);
GUIFontIconMetrics(params->font, GUI_ICON_SCROLLBAR_TRACK, &w, 0);
right = (right - w) / 2;
GUIFontDrawIcon(params->font, params->width - 8, top, GUI_ALIGN_HCENTER | GUI_ALIGN_BOTTOM, GUI_ORIENT_VMIRROR, 0xFFFFFFFF, GUI_ICON_SCROLLBAR_BUTTON); GUIFontDrawIcon(params->font, params->width - 8, top, GUI_ALIGN_HCENTER | GUI_ALIGN_BOTTOM, GUI_ORIENT_VMIRROR, 0xFFFFFFFF, GUI_ICON_SCROLLBAR_BUTTON);
GUIFontDrawIconSize(params->font, params->width - 9, top, 2, bottom - top, 0xFFFFFFFF, GUI_ICON_SCROLLBAR_TRACK); GUIFontDrawIconSize(params->font, params->width - right - 8, top, 0, bottom - top, 0xFFFFFFFF, GUI_ICON_SCROLLBAR_TRACK);
GUIFontDrawIcon(params->font, params->width - 8, bottom, GUI_ALIGN_HCENTER | GUI_ALIGN_TOP, GUI_ORIENT_0, 0xFFFFFFFF, GUI_ICON_SCROLLBAR_BUTTON); GUIFontDrawIcon(params->font, params->width - 8, bottom, GUI_ALIGN_HCENTER | GUI_ALIGN_TOP, GUI_ORIENT_0, 0xFFFFFFFF, GUI_ICON_SCROLLBAR_BUTTON);
y = menu->index * (bottom - top - 16) / GUIMenuItemListSize(&menu->items); y = menu->index * (bottom - top - 16) / GUIMenuItemListSize(&menu->items);