From 9ef961f871127db09a7a425f5f9d0699e7109bd2 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Wed, 8 Sep 2021 23:10:59 -0700 Subject: [PATCH] Added textmenu --- include/dawn/dawn.h | 5 +- include/dawn/game/sandbox/sandboxscene.h | 1 + include/dawn/ui/grid.h | 8 +-- include/dawn/ui/textmenu.h | 31 ++++++++++ src/game/sandbox/sandboxscene.c | 71 ++++++---------------- src/game/sandbox/sandboxscene.h | 1 + src/ui/grid.c | 16 ++--- src/ui/menuv2.c | 1 - src/ui/textmenu.c | 76 ++++++++++++++++++++++++ src/ui/textmenu.h | 58 ++++++++++++++++++ 10 files changed, 196 insertions(+), 72 deletions(-) create mode 100644 include/dawn/ui/textmenu.h create mode 100644 src/ui/textmenu.c create mode 100644 src/ui/textmenu.h diff --git a/include/dawn/dawn.h b/include/dawn/dawn.h index 9973a84c..9de48443 100644 --- a/include/dawn/dawn.h +++ b/include/dawn/dawn.h @@ -71,14 +71,15 @@ // User Interface Objects #include "ui/grid.h" +#include "ui/label.h" #include "ui/menuv2.h" +#include "ui/rectangle.h" +#include "ui/textmenu.h" #include "ui/frame.h" #include "ui/image.h" -#include "ui/label.h" #include "ui/menu.h" #include "ui/menulist.h" -#include "ui/rectangle.h" // Utility Objects #include "util/array.h" diff --git a/include/dawn/game/sandbox/sandboxscene.h b/include/dawn/game/sandbox/sandboxscene.h index c81fff80..d648ee2b 100644 --- a/include/dawn/game/sandbox/sandboxscene.h +++ b/include/dawn/game/sandbox/sandboxscene.h @@ -26,6 +26,7 @@ typedef struct { primitive_t primitive; texture_t texture; shader_t shader; + font_t font; gridbruh_t items[GRID_BRUH_COUNT]; } sandboxscene_t; \ No newline at end of file diff --git a/include/dawn/ui/grid.h b/include/dawn/ui/grid.h index a8fdd400..3645493c 100644 --- a/include/dawn/ui/grid.h +++ b/include/dawn/ui/grid.h @@ -29,7 +29,7 @@ typedef struct { /** Callback to receive when a grid child is to be resized */ typedef void gridchildresizecallback_t( - void *user, int32_t i, + void *user, uint8_t i, float screenWidth, float screenHeight, float x, float y, float width, float height @@ -47,7 +47,6 @@ typedef struct { typedef struct { gridchildbreakpoint_t breakpoints[GRID_BREAKPOINT_COUNT]; uint8_t breakpointCount; - gridchildresizecallback_t *onResize; } gridchild_t; /** Definitio of a grid */ @@ -65,9 +64,10 @@ typedef struct { /** Internal size reference, used for caching resize events */ float width; float height; - float x; - float y; /** Pointer to any custom user data */ void *user; + + /** Callback to listen for when children are resized */ + gridchildresizecallback_t *onResize; } grid_t; \ No newline at end of file diff --git a/include/dawn/ui/textmenu.h b/include/dawn/ui/textmenu.h new file mode 100644 index 00000000..2123abc8 --- /dev/null +++ b/include/dawn/ui/textmenu.h @@ -0,0 +1,31 @@ +// Copyright (c) 2021 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "../libs.h" +#include "menuv2.h" +#include "grid.h" +#include "label.h" +#include "rectangle.h" + +typedef void textmenucallback_t(void *user, uint8_t i, char *text); + +typedef struct { + float x, y; + float width, height; +} textmenulabelinfo_t; + +typedef struct { + menuv2_t menu; + char *texts[GRID_CHILD_COUNT]; + + font_t *font; + label_t labels[GRID_CHILD_COUNT]; + rectangle_t rectangle; + textmenulabelinfo_t labelInfo[GRID_CHILD_COUNT]; + + textmenucallback_t *onSelect; + void *user; +} textmenu_t; \ No newline at end of file diff --git a/src/game/sandbox/sandboxscene.c b/src/game/sandbox/sandboxscene.c index e78b0d57..c0fd91c3 100644 --- a/src/game/sandbox/sandboxscene.c +++ b/src/game/sandbox/sandboxscene.c @@ -7,51 +7,30 @@ #include "sandboxscene.h" -menuv2_t menu; - - -void gridTest( - void *user, int32_t i, - float screenWidth, float screenHeight, - float x, float y, - float width, float height -) { - sandboxscene_t *game = (sandboxscene_t *)user; - game->items[i].x = x; - game->items[i].y = y; - game->items[i].width = width; - game->items[i].height = height; -} +textmenu_t menu; bool sandboxSceneInit(sandboxscene_t *game) { - menu2Init(&menu); - gridAddBreakpoint(&menu.grid, -1, 3, 1, 0, 0); - gridchild_t *child = gridAddChild(&menu.grid); - gridChildAddBreakpoint(child, 0,0, 1,1); - - child = gridAddChild(&menu.grid); - gridChildAddBreakpoint(child, 0,1, 1,1); - - child = gridAddChild(&menu.grid); - gridChildAddBreakpoint(child, 0,2, 1,1); - - + assetFontLoad(&game->font, "fonts/opensans/OpenSans-Regular.ttf"); assetTextureLoad(&game->texture, "test_texture.png"); assetShaderLoad(&game->shader, "shaders/textured.vert", "shaders/textured.frag" ); - quadInit(&game->primitive, 0, - 0,0,0,0, - 1,1,1,1 - ); + + textMenuInit(&menu, &game->font); + gridAddBreakpoint(&menu.menu.grid, -1, 3, 1, 0, 0); + gridchild_t *child = textMenuListAdd(&menu, "First"); + gridChildAddBreakpoint(child, 0,0, 1,1); + + child = textMenuListAdd(&menu, "Second"); + gridChildAddBreakpoint(child, 0,1, 1,1); + + child = textMenuListAdd(&menu, "Third"); + gridChildAddBreakpoint(child, 0,2, 1,1); return true; } void sandboxSceneUpdate(sandboxscene_t *game, engine_t *engine) { - - - cameraLookAt(&game->camera, 0, 0, 10, 0, 0, 0 @@ -67,29 +46,13 @@ void sandboxSceneUpdate(sandboxscene_t *game, engine_t *engine) { shaderUseCamera(&game->shader, &game->camera); shaderUseTexture(&game->shader, &game->texture); - menu2Update(&menu, engine); - gridSetSize(&menu.grid, - engine->render.width, engine->render.height, + gridSetSize(&menu.menu.grid, engine->render.width, engine->render.height, + engine->render.width, FONT_LINE_HEIGHT * fontGetScale(16.0f) * 3, 0, 0 ); - - // gridSetSize(&grid, - // engine->render.width, engine->render.height, - // engine->render.width, engine->render.height, - // 0, 0 - // ); - - - for(uint8_t i = 0; i < GRID_BRUH_COUNT; i++) { - gridbruh_t *b = game->items + i; - shaderUsePositionAndScale(&game->shader, - b->x, b->y, 0, - 0, 0, 0, - b->width, b->height, 1 - ); - primitiveDraw(&game->primitive, 0, -1); - } + menu2Update(&menu.menu, engine); + textMenuListRender(&menu, &game->shader, 0, 0); } void sandboxSceneDispose(sandboxscene_t *game) { diff --git a/src/game/sandbox/sandboxscene.h b/src/game/sandbox/sandboxscene.h index 22068888..df5c871c 100644 --- a/src/game/sandbox/sandboxscene.h +++ b/src/game/sandbox/sandboxscene.h @@ -19,6 +19,7 @@ #include "../../ui/grid.h" #include "../../ui/menuv2.h" +#include "../../ui/textmenu.h" /** * Initialize the sandbox scene test game. diff --git a/src/ui/grid.c b/src/ui/grid.c index aeb6aa12..dc53a17a 100644 --- a/src/ui/grid.c +++ b/src/ui/grid.c @@ -13,8 +13,7 @@ void gridInit(grid_t *grid) { grid->childCount = 0x00; grid->width = 0; grid->height = 0; - grid->x = 0; - grid->y = 0; + grid->onResize = NULL; } uint8_t gridAddBreakpoint( @@ -36,7 +35,6 @@ uint8_t gridAddBreakpoint( gridchild_t * gridAddChild(grid_t *grid) { gridchild_t *child = grid->children + grid->childCount++; child->breakpointCount = 0; - child->onResize = NULL; return child; } @@ -73,18 +71,13 @@ void gridSetSize(grid_t *grid, float sizeCol, sizeRow, gx, gy, gw, gh; // Need to resize? - if(( - grid->width == width && grid->height == height && - grid->x == x && grid->y == y - )) { + if(grid->width == width && grid->height == height) { return; } // Update properties grid->width = width; grid->height = height; - grid->x = x; - grid->y = y; // Determine breakpoint breakpoint = 0xFF; @@ -100,11 +93,12 @@ void gridSetSize(grid_t *grid, sizeCol = (width - (gridbp->gutterX * (gridbp->columns-1))) / gridbp->columns; sizeRow = (height - (gridbp->gutterY * (gridbp->rows - 1))) / gridbp->rows; + if(grid->onResize == NULL) return; + // Resize children for(i = 0; i < grid->childCount; i++) { // Get the item and the definition. child = grid->children + i; - if(child->onResize == NULL) continue; childbp = gridChildGetBreakpoint(child, breakpoint); @@ -123,7 +117,7 @@ void gridSetSize(grid_t *grid, ); // Fire the resize event. - child->onResize( + grid->onResize( grid->user, i, screenWidth, screenHeight, x + gx, y + gy, diff --git a/src/ui/menuv2.c b/src/ui/menuv2.c index aaf31d79..4e5bf227 100644 --- a/src/ui/menuv2.c +++ b/src/ui/menuv2.c @@ -86,6 +86,5 @@ void menu2Update(menuv2_t *menu, engine_t *engine) { // Was a target found? if(j == GRID_CHILD_COUNT) return; menu->selected = j; - printf("Selected %u :: %u x %u \n", j, itembp->x, itembp->y); } } \ No newline at end of file diff --git a/src/ui/textmenu.c b/src/ui/textmenu.c new file mode 100644 index 00000000..e8e44c46 --- /dev/null +++ b/src/ui/textmenu.c @@ -0,0 +1,76 @@ +/** + * Copyright (c) 2021 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#include "textmenu.h" + +void _textMenuOnSelect(menuv2_t *menu, uint8_t i) { + textmenu_t *textMenu = (textmenu_t *)menu->user; + + if(textMenu->onSelect != NULL) { + textMenu->onSelect(textMenu->user, i, textMenu->texts[i]); + } +} + +void _textMenuOnResize( + void *user, uint8_t i, + float screenWidth, float screenHeight, + float x, float y, + float width, float height +) { + textmenu_t *textMenu = (textmenu_t *)((menuv2_t *)user); + label_t *label = textMenu->labels + i; + textmenulabelinfo_t *info = textMenu->labelInfo + i; + info->x = x; + info->y = y; + info->width = width; + info->height = height; + labelSetText(label, textMenu->font, textMenu->texts[i]); +} + +void textMenuInit(textmenu_t *menu, font_t *font) { + menu2Init(&menu->menu); + rectangleInit(&menu->rectangle); + rectangleSetColor(&menu->rectangle, MENULIST_SELECTION_COLOR); + + menu->menu.user = menu; + menu->menu.onSelect = &_textMenuOnSelect; + menu->font = font; + menu->onSelect = NULL; + menu->menu.grid.onResize = &_textMenuOnResize; +} + +gridchild_t * textMenuListAdd(textmenu_t *menu, char *item) { + menu->texts[menu->menu.grid.childCount] = item; + labelInit(menu->labels + menu->menu.grid.childCount); + (menu->labels + menu->menu.grid.childCount)->font = menu->font; + return gridAddChild(&menu->menu.grid); +} + +void textMenuListRender(textmenu_t *menu, shader_t *shader, float x, float y) { + uint8_t i; + label_t *label; + textmenulabelinfo_t *info; + + info = menu->labelInfo + menu->menu.selected; + menu->rectangle.width = info->width; + menu->rectangle.height = info->height; + rectangleRender(&menu->rectangle, shader, info->x, info->y); + + for(i = 0; i < menu->menu.grid.childCount; i++) { + label = menu->labels + i; + info = menu->labelInfo + i; + labelRender(label, shader, x + info->x, y + info->y); + } +} + +void textMenuDispse(textmenu_t *menu) { + uint8_t i; + rectangleDispose(&menu->rectangle); + for(i = 0; i < menu->menu.grid.childCount; i++) { + labelDispose(menu->labels + i); + } +} \ No newline at end of file diff --git a/src/ui/textmenu.h b/src/ui/textmenu.h new file mode 100644 index 00000000..1d57e7b8 --- /dev/null +++ b/src/ui/textmenu.h @@ -0,0 +1,58 @@ +/** + * Copyright (c) 2021 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#pragma once +#include +#include "menuv2.h" +#include "grid.h" +#include "label.h" +#include "rectangle.h" + +/** Callback to be notified from the menu when the menu items are selected. */ +void _textMenuOnSelect(menuv2_t *menu, uint8_t i); + +/** Callback to lisen for resize events from the grid subsystem */ +void _textMenuOnResize( + void *user, uint8_t i, + float screenWidth, float screenHeight, + float x, float y, + float width, float height +); + +/** + * Initialize a text menu item. + * + * @param menu Menu to initialize. + * @param font Font to use during initialization. + */ +void textMenuInit(textmenu_t *menu, font_t *font); + +/** + * Add text to a menu list. + * + * @param menu Menu to add to. + * @param item Text to add (must be a constant value). + * @return The grid subsystem item. + */ +gridchild_t * textMenuListAdd(textmenu_t *menu, char *item); + +/** + * Render a text menu list. + * + * @param menu Menu to render. + * @param shader Shader to use. + * @param x X position of the menu. + * @param y Y position of the menu. + */ +void textMenuListRender(textmenu_t *menu, shader_t *shader, float x, float y); + +/** + * Dispose/Cleanup a text menu. + * + * @param menu Text menu to dispose. + */ +void textMenuDispse(textmenu_t *menu); \ No newline at end of file