Added framed text menu.

This commit is contained in:
2021-09-10 10:13:39 -07:00
parent 7a121d4f3b
commit f3ecafb4a9
11 changed files with 182 additions and 28 deletions

75
src/ui/framedtextmenu.c Normal file
View File

@ -0,0 +1,75 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "framedtextmenu.h"
void framedTextMenuInit(framedtextmenu_t *menu, font_t *font, texture_t *text) {
frameInit(&menu->frame);
textMenuInit(&menu->menu, font);
menu->frame.texture = text;
menu->width = -1;
menu->height = -1;
}
void framedTextMenuUpdate(framedtextmenu_t *menu, engine_t *engine) {
float iw, ih;
uint8_t bp;
gridbreakpoint_t *breakpoint;
bp = gridGetBreakpoint(&menu->menu.menu.grid, engine->render.width);
breakpoint = menu->menu.menu.grid.breakpoints + bp;
if(menu->width < 0) {
if(menu->menu.menu.grid.childCount > 0) {
iw = 0;
for(uint8_t i = 0; i < menu->menu.menu.grid.childCount; i++) {
iw = mathMax(menu->menu.labels[i].info.width, iw);
}
iw *= breakpoint->columns;
if(iw == 0) iw = engine->render.width - FRAME_BORDER_SIZE_FULL;
} else {
iw = engine->render.width - FRAME_BORDER_SIZE_FULL;
}
} else {
iw = menu->width - FRAME_BORDER_SIZE_FULL;
}
if(menu->height < 0) {
if(menu->menu.menu.grid.childCount > 0) {
ih = breakpoint->rows * (
fontGetScale(menu->menu.labels->fontSize) * FONT_LINE_HEIGHT
);
} else {
ih = 0;
}
} else {
ih = menu->height - FRAME_BORDER_SIZE_FULL;
}
frameSetInnerSize(&menu->frame, iw, ih);
gridSetSize(&menu->menu.menu.grid,
engine->render.width, engine->render.height,
iw, ih,
0, 0
);
menu2Update(&menu->menu.menu, engine);
}
void framedTextMenuRender(
framedtextmenu_t *menu, shader_t *shader, float x, float y
) {
frameRender(&menu->frame, shader, x, y);
textMenuListRender(&menu->menu, shader,
x + FRAME_BORDER_SIZE, y + FRAME_BORDER_SIZE
);
}
void framedTextMenuDispose(framedtextmenu_t *menu) {
textMenuDispse(&menu->menu);
frameDispose(&menu->frame);
}

48
src/ui/framedtextmenu.h Normal file
View File

@ -0,0 +1,48 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include <dawn/dawn.h>
#include "frame.h"
#include "textmenu.h"
#include "grid.h"
/**
* Initialize a framed text ui element.
*
* @param menu Menu to initialize.
* @param font Font to use for the text.
* @param text Texture for the frame.
*/
void framedTextMenuInit(framedtextmenu_t *menu, font_t *font, texture_t *text);
/**
* Update a framed text menu ui element.
*
* @param menu Menu to update.
* @param engine Engine to use while updating.
*/
void framedTextMenuUpdate(framedtextmenu_t *menu, engine_t *engine);
/**
* Render a framed text menu.
*
* @param menu Menu to render.
* @param shader Shader to use.
* @param x X Position.
* @param y Y Position.
*/
void framedTextMenuRender(
framedtextmenu_t *menu, shader_t *shader, float x, float y
);
/**
* Dispose a previously created framed text menu.
*
* @param menu Menu to dispose.
*/
void framedTextMenuDispose(framedtextmenu_t *menu);

View File

@ -59,6 +59,15 @@ gridchildbreakpoint_t * gridChildGetBreakpoint(gridchild_t *child, uint8_t bp) {
);
}
uint8_t gridGetBreakpoint(grid_t *grid, float screenWidth) {
uint8_t i;
for(i = 0; i < grid->breakpointCount; i++) {
if(grid->breakpointSizes[i].width < screenWidth) continue;
return i;
}
return grid->breakpointCount - 1;
}
void gridSetSize(grid_t *grid,
float screenWidth, float screenHeight,
float width, float height,
@ -80,12 +89,7 @@ void gridSetSize(grid_t *grid,
grid->height = height;
// Determine breakpoint
breakpoint = 0xFF;
for(i = 0; i < grid->breakpointCount; i++) {
if(grid->breakpointSizes[i].width < screenWidth) continue;
breakpoint = i;
}
if(breakpoint == 0xFF) breakpoint = grid->breakpointCount - 1;
breakpoint = gridGetBreakpoint(grid, screenWidth);
gridbp = grid->breakpoints + breakpoint;
grid->breakpointCurrent = breakpoint;

View File

@ -55,6 +55,15 @@ uint8_t gridChildAddBreakpoint(gridchild_t *child,
uint8_t x, uint8_t y, uint8_t columns, uint8_t rows
);
/**
* Get the breakpoint (index) from a grid and screen width.
*
* @param grid Grid to get from.
* @param screenWidth Screen width to use.
* @return Grid index.
*/
uint8_t gridGetBreakpoint(grid_t *grid, float screenWidth);
/**
* Retreive the breakpoint to use for a child. Takes missing breakpoints into
* consideration.

View File

@ -28,7 +28,7 @@ void _textMenuOnResize(
info->y = y;
info->width = width;
info->height = height;
label->maxWidth = width;
label->maxWidth = width <= 0 ? -1 : width;
labelSetText(label, textMenu->font, textMenu->texts[i]);
}
@ -59,7 +59,7 @@ void textMenuListRender(textmenu_t *menu, shader_t *shader, float x, float y) {
info = menu->labelInfo + menu->menu.selected;
menu->rectangle.width = info->width;
menu->rectangle.height = info->height;
rectangleRender(&menu->rectangle, shader, info->x, info->y);
rectangleRender(&menu->rectangle, shader, x + info->x, y + info->y);
for(i = 0; i < menu->menu.grid.childCount; i++) {
label = menu->labels + i;