Added textmenu
This commit is contained in:
		@@ -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,
 | 
			
		||||
 
 | 
			
		||||
@@ -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);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										76
									
								
								src/ui/textmenu.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										76
									
								
								src/ui/textmenu.c
									
									
									
									
									
										Normal file
									
								
							@@ -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);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										58
									
								
								src/ui/textmenu.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										58
									
								
								src/ui/textmenu.h
									
									
									
									
									
										Normal file
									
								
							@@ -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 <dawn/dawn.h>
 | 
			
		||||
#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);
 | 
			
		||||
		Reference in New Issue
	
	Block a user