Working on redoing menu ui.
This commit is contained in:
		@@ -71,6 +71,7 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// User Interface Objects
 | 
					// User Interface Objects
 | 
				
			||||||
#include "ui/grid.h"
 | 
					#include "ui/grid.h"
 | 
				
			||||||
 | 
					#include "ui/menuv2.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include "ui/frame.h"
 | 
					#include "ui/frame.h"
 | 
				
			||||||
#include "ui/image.h"
 | 
					#include "ui/image.h"
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -56,6 +56,7 @@ typedef struct {
 | 
				
			|||||||
  gridbreakpoint_t breakpoints[GRID_BREAKPOINT_COUNT];
 | 
					  gridbreakpoint_t breakpoints[GRID_BREAKPOINT_COUNT];
 | 
				
			||||||
  breakpointsize_t breakpointSizes[GRID_BREAKPOINT_COUNT];
 | 
					  breakpointsize_t breakpointSizes[GRID_BREAKPOINT_COUNT];
 | 
				
			||||||
  uint8_t breakpointCount;
 | 
					  uint8_t breakpointCount;
 | 
				
			||||||
 | 
					  uint8_t breakpointCurrent;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /** Child items of the grid */
 | 
					  /** Child items of the grid */
 | 
				
			||||||
  gridchild_t children[GRID_CHILD_COUNT];
 | 
					  gridchild_t children[GRID_CHILD_COUNT];
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										22
									
								
								include/dawn/ui/menuv2.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								include/dawn/ui/menuv2.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,22 @@
 | 
				
			|||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Copyright (c) 2021 Dominic Masters
 | 
				
			||||||
 | 
					 * 
 | 
				
			||||||
 | 
					 * This software is released under the MIT License.
 | 
				
			||||||
 | 
					 * https://opensource.org/licenses/MIT
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#pragma once
 | 
				
			||||||
 | 
					#include "../libs.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/** Generic callback for menu events */
 | 
				
			||||||
 | 
					typedef void menuv2callback_t(void *user, uint8_t i);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/** Structure for a menu */
 | 
				
			||||||
 | 
					typedef struct {
 | 
				
			||||||
 | 
					  grid_t grid;
 | 
				
			||||||
 | 
					  uint8_t selected;
 | 
				
			||||||
 | 
					  uint8_t cursorX;
 | 
				
			||||||
 | 
					  uint8_t cursorY;
 | 
				
			||||||
 | 
					  void *user;
 | 
				
			||||||
 | 
					  menuv2callback_t *onSelect;
 | 
				
			||||||
 | 
					} menuv2_t;
 | 
				
			||||||
@@ -7,7 +7,8 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
#include "sandboxscene.h"
 | 
					#include "sandboxscene.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
grid_t grid;
 | 
					menuv2_t menu;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void gridTest(
 | 
					void gridTest(
 | 
				
			||||||
  void *user, int32_t i,
 | 
					  void *user, int32_t i,
 | 
				
			||||||
@@ -23,14 +24,17 @@ void gridTest(
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
bool sandboxSceneInit(sandboxscene_t *game) {
 | 
					bool sandboxSceneInit(sandboxscene_t *game) {
 | 
				
			||||||
  gridInit(&grid);
 | 
					  menu2Init(&menu);
 | 
				
			||||||
  grid.user = (void *)game;
 | 
					  gridAddBreakpoint(&menu.grid, -1, 3, 1, 0, 0);
 | 
				
			||||||
 | 
					  gridchild_t *child = gridAddChild(&menu.grid);
 | 
				
			||||||
 | 
					  gridChildAddBreakpoint(child, 0,0, 1,1);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  gridAddBreakpoint(&grid, 800, 2, 2, 8.0f, 8.0f);
 | 
					  child = gridAddChild(&menu.grid);
 | 
				
			||||||
  gridAddBreakpoint(&grid, -1, 6, 6, 16.0f, 16.0f);
 | 
					  gridChildAddBreakpoint(child, 0,1, 1,1);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  child = gridAddChild(&menu.grid);
 | 
				
			||||||
 | 
					  gridChildAddBreakpoint(child, 0,2, 1,1);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  gridchild_t *child = gridAddChild(&grid);
 | 
					 | 
				
			||||||
  child->onResize = &gridTest;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  assetTextureLoad(&game->texture, "test_texture.png");
 | 
					  assetTextureLoad(&game->texture, "test_texture.png");
 | 
				
			||||||
  assetShaderLoad(&game->shader, 
 | 
					  assetShaderLoad(&game->shader, 
 | 
				
			||||||
@@ -45,6 +49,9 @@ bool sandboxSceneInit(sandboxscene_t *game) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void sandboxSceneUpdate(sandboxscene_t *game, engine_t *engine) {
 | 
					void sandboxSceneUpdate(sandboxscene_t *game, engine_t *engine) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  cameraLookAt(&game->camera,
 | 
					  cameraLookAt(&game->camera,
 | 
				
			||||||
    0, 0, 10,
 | 
					    0, 0, 10,
 | 
				
			||||||
    0, 0, 0
 | 
					    0, 0, 0
 | 
				
			||||||
@@ -60,11 +67,18 @@ void sandboxSceneUpdate(sandboxscene_t *game, engine_t *engine) {
 | 
				
			|||||||
  shaderUseCamera(&game->shader, &game->camera);
 | 
					  shaderUseCamera(&game->shader, &game->camera);
 | 
				
			||||||
  shaderUseTexture(&game->shader, &game->texture);
 | 
					  shaderUseTexture(&game->shader, &game->texture);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  gridSetSize(&grid,
 | 
					  menu2Update(&menu, engine);
 | 
				
			||||||
 | 
					  gridSetSize(&menu.grid,
 | 
				
			||||||
    engine->render.width, engine->render.height,
 | 
					    engine->render.width, engine->render.height,
 | 
				
			||||||
    engine->render.width, engine->render.height,
 | 
					    engine->render.width, engine->render.height,
 | 
				
			||||||
    0, 0
 | 
					    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++) {
 | 
					  for(uint8_t i = 0; i < GRID_BRUH_COUNT; i++) {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -16,7 +16,9 @@
 | 
				
			|||||||
#include "../../display/renderlist.h"
 | 
					#include "../../display/renderlist.h"
 | 
				
			||||||
#include "../../display/texture.h"
 | 
					#include "../../display/texture.h"
 | 
				
			||||||
#include "../../file/asset.h"
 | 
					#include "../../file/asset.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include "../../ui/grid.h"
 | 
					#include "../../ui/grid.h"
 | 
				
			||||||
 | 
					#include "../../ui/menuv2.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Initialize the sandbox scene test game.
 | 
					 * Initialize the sandbox scene test game.
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -9,6 +9,7 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
void gridInit(grid_t *grid) {
 | 
					void gridInit(grid_t *grid) {
 | 
				
			||||||
  grid->breakpointCount = 0x00;
 | 
					  grid->breakpointCount = 0x00;
 | 
				
			||||||
 | 
					  grid->breakpointCurrent = 0x00;
 | 
				
			||||||
  grid->childCount = 0x00;
 | 
					  grid->childCount = 0x00;
 | 
				
			||||||
  grid->width = 0;
 | 
					  grid->width = 0;
 | 
				
			||||||
  grid->height = 0;
 | 
					  grid->height = 0;
 | 
				
			||||||
@@ -35,6 +36,7 @@ uint8_t gridAddBreakpoint(
 | 
				
			|||||||
gridchild_t * gridAddChild(grid_t *grid) {
 | 
					gridchild_t * gridAddChild(grid_t *grid) {
 | 
				
			||||||
  gridchild_t *child = grid->children + grid->childCount++;
 | 
					  gridchild_t *child = grid->children + grid->childCount++;
 | 
				
			||||||
  child->breakpointCount = 0;
 | 
					  child->breakpointCount = 0;
 | 
				
			||||||
 | 
					  child->onResize = NULL;
 | 
				
			||||||
  return child;
 | 
					  return child;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -53,6 +55,12 @@ uint8_t gridChildAddBreakpoint(gridchild_t *child,
 | 
				
			|||||||
  return i;
 | 
					  return i;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					gridchildbreakpoint_t * gridChildGetBreakpoint(gridchild_t *child, uint8_t bp) {
 | 
				
			||||||
 | 
					  return child->breakpoints + (
 | 
				
			||||||
 | 
					    bp >= child->breakpointCount ? child->breakpointCount - 1 : bp
 | 
				
			||||||
 | 
					  );
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void gridSetSize(grid_t *grid,
 | 
					void gridSetSize(grid_t *grid,
 | 
				
			||||||
  float screenWidth, float screenHeight,
 | 
					  float screenWidth, float screenHeight,
 | 
				
			||||||
  float width, float height,
 | 
					  float width, float height,
 | 
				
			||||||
@@ -86,6 +94,7 @@ void gridSetSize(grid_t *grid,
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
  if(breakpoint == 0xFF) breakpoint = grid->breakpointCount - 1;
 | 
					  if(breakpoint == 0xFF) breakpoint = grid->breakpointCount - 1;
 | 
				
			||||||
  gridbp = grid->breakpoints + breakpoint;
 | 
					  gridbp = grid->breakpoints + breakpoint;
 | 
				
			||||||
 | 
					  grid->breakpointCurrent = breakpoint;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // Determine the size of a single column/row
 | 
					  // Determine the size of a single column/row
 | 
				
			||||||
  sizeCol = (width - (gridbp->gutterX * (gridbp->columns-1))) / gridbp->columns;
 | 
					  sizeCol = (width - (gridbp->gutterX * (gridbp->columns-1))) / gridbp->columns;
 | 
				
			||||||
@@ -95,11 +104,9 @@ void gridSetSize(grid_t *grid,
 | 
				
			|||||||
  for(i = 0; i < grid->childCount; i++) {
 | 
					  for(i = 0; i < grid->childCount; i++) {
 | 
				
			||||||
    // Get the item and the definition.
 | 
					    // Get the item and the definition.
 | 
				
			||||||
    child = grid->children + i;
 | 
					    child = grid->children + i;
 | 
				
			||||||
    childbp = child->breakpoints + (
 | 
					    if(child->onResize == NULL) continue;
 | 
				
			||||||
      breakpoint >= child->breakpointCount ?
 | 
					    
 | 
				
			||||||
      child->breakpointCount - 1 :
 | 
					    childbp = gridChildGetBreakpoint(child, breakpoint);
 | 
				
			||||||
      breakpoint
 | 
					 | 
				
			||||||
    );
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Get the local X/Y
 | 
					    // Get the local X/Y
 | 
				
			||||||
    gx = (sizeCol * childbp->x) + (gridbp->gutterX * childbp->x);
 | 
					    gx = (sizeCol * childbp->x) + (gridbp->gutterX * childbp->x);
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -55,6 +55,16 @@ uint8_t gridChildAddBreakpoint(gridchild_t *child,
 | 
				
			|||||||
  uint8_t x, uint8_t y, uint8_t columns, uint8_t rows
 | 
					  uint8_t x, uint8_t y, uint8_t columns, uint8_t rows
 | 
				
			||||||
);
 | 
					);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Retreive the breakpoint to use for a child. Takes missing breakpoints into
 | 
				
			||||||
 | 
					 * consideration.
 | 
				
			||||||
 | 
					 * 
 | 
				
			||||||
 | 
					 * @param child Child to get the breakpoint from.
 | 
				
			||||||
 | 
					 * @param bp Breakpoint index to use.
 | 
				
			||||||
 | 
					 * @return The childs matching breakpoint
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					gridchildbreakpoint_t * gridChildGetBreakpoint(gridchild_t *child, uint8_t bp);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Set the size of a grid system. This will only update if it's found to be 
 | 
					 * Set the size of a grid system. This will only update if it's found to be 
 | 
				
			||||||
 * necessary.
 | 
					 * necessary.
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										91
									
								
								src/ui/menuv2.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										91
									
								
								src/ui/menuv2.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,91 @@
 | 
				
			|||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Copyright (c) 2021 Dominic Masters
 | 
				
			||||||
 | 
					 * 
 | 
				
			||||||
 | 
					 * This software is released under the MIT License.
 | 
				
			||||||
 | 
					 * https://opensource.org/licenses/MIT
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include "menuv2.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void menu2Init(menuv2_t *menu) {
 | 
				
			||||||
 | 
					  gridInit(&menu->grid);
 | 
				
			||||||
 | 
					  menu->grid.user = menu;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  menu->cursorX = 0;
 | 
				
			||||||
 | 
					  menu->cursorY = 0;
 | 
				
			||||||
 | 
					  menu->selected = 0;
 | 
				
			||||||
 | 
					  menu->user = NULL;
 | 
				
			||||||
 | 
					  menu->onSelect = NULL;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void menu2Update(menuv2_t *menu, engine_t *engine) {
 | 
				
			||||||
 | 
					  uint8_t x, y, i, j;
 | 
				
			||||||
 | 
					  gridchild_t *current;
 | 
				
			||||||
 | 
					  gridchildbreakpoint_t *currentbp;
 | 
				
			||||||
 | 
					  gridchild_t *item;
 | 
				
			||||||
 | 
					  gridchildbreakpoint_t *itembp;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  current = menu->grid.children + menu->selected;
 | 
				
			||||||
 | 
					  currentbp = gridChildGetBreakpoint(current, menu->grid.breakpointCurrent);
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
 | 
					  if(inputIsPressed(&engine->input, INPUT_ACCEPT)) {
 | 
				
			||||||
 | 
					    if(menu->onSelect != NULL) menu->onSelect(menu->user, menu->selected);
 | 
				
			||||||
 | 
					    return;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  // Handle press binds.
 | 
				
			||||||
 | 
					  if(inputIsPressed(&engine->input, INPUT_DOWN)) {
 | 
				
			||||||
 | 
					    x = 0;
 | 
				
			||||||
 | 
					    y = 1;
 | 
				
			||||||
 | 
					  } else if(inputIsPressed(&engine->input, INPUT_UP)) {
 | 
				
			||||||
 | 
					    x = 0;
 | 
				
			||||||
 | 
					    y = -1;
 | 
				
			||||||
 | 
					  } else if(inputIsPressed(&engine->input, INPUT_LEFT)) {
 | 
				
			||||||
 | 
					    x = -1;
 | 
				
			||||||
 | 
					    y = 0;
 | 
				
			||||||
 | 
					  } else if(inputIsPressed(&engine->input, INPUT_RIGHT)) {
 | 
				
			||||||
 | 
					    x = 1;
 | 
				
			||||||
 | 
					    y = 0;
 | 
				
			||||||
 | 
					  } else {
 | 
				
			||||||
 | 
					    x = 0;
 | 
				
			||||||
 | 
					    y = 0;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
 | 
					  // Update cursor positions
 | 
				
			||||||
 | 
					  if(x != 0 || y != 0) {
 | 
				
			||||||
 | 
					    if(x > 0) {
 | 
				
			||||||
 | 
					      menu->cursorX = (currentbp->x + currentbp->columns - 1) + x;
 | 
				
			||||||
 | 
					    } else if(x < 0) {
 | 
				
			||||||
 | 
					      menu->cursorX = currentbp->x + x;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if(y > 0) {
 | 
				
			||||||
 | 
					      menu->cursorY = (currentbp->y + currentbp->rows - 1) + y;
 | 
				
			||||||
 | 
					    } else if(y < 0) {
 | 
				
			||||||
 | 
					      menu->cursorY = currentbp->y + y;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    // Get the item selected
 | 
				
			||||||
 | 
					    j = GRID_CHILD_COUNT;
 | 
				
			||||||
 | 
					    for(i = 0; i < menu->grid.childCount; i++) {
 | 
				
			||||||
 | 
					      if(i == menu->selected) continue;
 | 
				
			||||||
 | 
					      item = menu->grid.children + i;
 | 
				
			||||||
 | 
					      itembp = gridChildGetBreakpoint(item, menu->grid.breakpointCurrent);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      if(
 | 
				
			||||||
 | 
					        itembp->x > menu->cursorX||(itembp->x+itembp->columns-1)<menu->cursorX||
 | 
				
			||||||
 | 
					        itembp->y > menu->cursorY||(itembp->y+itembp->rows-1) < menu->cursorY
 | 
				
			||||||
 | 
					      ) continue;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      j = i;
 | 
				
			||||||
 | 
					      break;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // 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);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										15
									
								
								src/ui/menuv2.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								src/ui/menuv2.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,15 @@
 | 
				
			|||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * 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 "grid.h"
 | 
				
			||||||
 | 
					#include "../input/input.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void menu2Init(menuv2_t *menu);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void menu2Update(menuv2_t *menu, engine_t *engine);
 | 
				
			||||||
		Reference in New Issue
	
	Block a user