Working on redoing menu ui.
This commit is contained in:
		@@ -9,6 +9,7 @@
 | 
			
		||||
 | 
			
		||||
void gridInit(grid_t *grid) {
 | 
			
		||||
  grid->breakpointCount = 0x00;
 | 
			
		||||
  grid->breakpointCurrent = 0x00;
 | 
			
		||||
  grid->childCount = 0x00;
 | 
			
		||||
  grid->width = 0;
 | 
			
		||||
  grid->height = 0;
 | 
			
		||||
@@ -35,6 +36,7 @@ uint8_t gridAddBreakpoint(
 | 
			
		||||
gridchild_t * gridAddChild(grid_t *grid) {
 | 
			
		||||
  gridchild_t *child = grid->children + grid->childCount++;
 | 
			
		||||
  child->breakpointCount = 0;
 | 
			
		||||
  child->onResize = NULL;
 | 
			
		||||
  return child;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -53,6 +55,12 @@ uint8_t gridChildAddBreakpoint(gridchild_t *child,
 | 
			
		||||
  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,
 | 
			
		||||
  float screenWidth, float screenHeight,
 | 
			
		||||
  float width, float height,
 | 
			
		||||
@@ -86,6 +94,7 @@ void gridSetSize(grid_t *grid,
 | 
			
		||||
  }
 | 
			
		||||
  if(breakpoint == 0xFF) breakpoint = grid->breakpointCount - 1;
 | 
			
		||||
  gridbp = grid->breakpoints + breakpoint;
 | 
			
		||||
  grid->breakpointCurrent = breakpoint;
 | 
			
		||||
 | 
			
		||||
  // Determine the size of a single column/row
 | 
			
		||||
  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++) {
 | 
			
		||||
    // Get the item and the definition.
 | 
			
		||||
    child = grid->children + i;
 | 
			
		||||
    childbp = child->breakpoints + (
 | 
			
		||||
      breakpoint >= child->breakpointCount ?
 | 
			
		||||
      child->breakpointCount - 1 :
 | 
			
		||||
      breakpoint
 | 
			
		||||
    );
 | 
			
		||||
    if(child->onResize == NULL) continue;
 | 
			
		||||
    
 | 
			
		||||
    childbp = gridChildGetBreakpoint(child, breakpoint);
 | 
			
		||||
 | 
			
		||||
    // Get the local X/Y
 | 
			
		||||
    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
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 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 
 | 
			
		||||
 * 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