Working on menu.
This commit is contained in:
@ -22,18 +22,18 @@
|
||||
typedef struct {
|
||||
int32_t x;
|
||||
int32_t y;
|
||||
int32_t width;
|
||||
int32_t height;
|
||||
int32_t i;
|
||||
} menuitem_t;
|
||||
|
||||
typedef struct {
|
||||
int32_t columns;
|
||||
int32_t rows;
|
||||
menuitem_t items[MENU_ITEMS_MAX];
|
||||
int32_t itemCount;
|
||||
int32_t selected;
|
||||
int32_t cursorX;
|
||||
int32_t cursorY;
|
||||
|
||||
int32_t x;
|
||||
int32_t y;
|
||||
|
||||
uint8_t direction;
|
||||
|
||||
menuitem_t* items[MENU_ITEMS_MAX];
|
||||
bool holdAllow;
|
||||
float holdLast;
|
||||
// bool holdAllow;
|
||||
// float holdLast;
|
||||
} menu_t;
|
@ -7,58 +7,67 @@
|
||||
|
||||
#include "menu.h"
|
||||
|
||||
void menuInit(menu_t *menu, int32_t columns, int32_t rows) {
|
||||
void menuInit(menu_t *menu) {
|
||||
int32_t i;
|
||||
menu->columns = columns;
|
||||
menu->rows = rows;
|
||||
menu->holdAllow = true;
|
||||
menu->direction = 0x00;
|
||||
for(i = 0; i < MENU_ITEMS_MAX; i++) menu->items[i] = NULL;
|
||||
|
||||
menu->itemCount = 0;
|
||||
menu->selected = 0;
|
||||
menu->cursorX = 0;
|
||||
menu->cursorY = 0;
|
||||
|
||||
for(i = 0; i < MENU_ITEMS_MAX; i++) menu->items[i].i = -1;
|
||||
}
|
||||
|
||||
void menuUpdate(menu_t *menu, engine_t *engine) {
|
||||
inputbind_t bind;
|
||||
float heldTime, heldDue;
|
||||
menuitem_t *current;
|
||||
menuitem_t *item;
|
||||
int32_t x, y, i;
|
||||
|
||||
current = menu->items + menu->selected;
|
||||
|
||||
// Handle press binds.
|
||||
if(inputIsPressed(&engine->input, INPUT_DOWN)) {
|
||||
menu->y = (menu->y + 1) % menu->rows;
|
||||
menu->direction = MENU_DIRECTION_DOWN;
|
||||
x = 0;
|
||||
y = 1;
|
||||
} else if(inputIsPressed(&engine->input, INPUT_UP)) {
|
||||
menu->y = (menu->y - 1) % menu->rows;
|
||||
menu->direction = MENU_DIRECTION_DOWN;
|
||||
x = 0;
|
||||
y = -1;
|
||||
} else if(inputIsPressed(&engine->input, INPUT_LEFT)) {
|
||||
menu->x = (menu->x - 1) % menu->columns;
|
||||
menu->direction = MENU_DIRECTION_DOWN;
|
||||
x = -1;
|
||||
y = 0;
|
||||
} else if(inputIsPressed(&engine->input, INPUT_RIGHT)) {
|
||||
menu->x = (menu->x + 1) % menu->columns;
|
||||
menu->direction = MENU_DIRECTION_DOWN;
|
||||
x = 1;
|
||||
y = 0;
|
||||
}
|
||||
|
||||
// Handle held
|
||||
if(menu->holdAllow && menu->direction != 0x00) {
|
||||
bind = (
|
||||
menu->direction == MENU_DIRECTION_DOWN ? INPUT_DOWN :
|
||||
menu->direction == MENU_DIRECTION_UP ? INPUT_UP :
|
||||
menu->direction == MENU_DIRECTION_LEFT ? INPUT_LEFT :
|
||||
menu->direction == MENU_DIRECTION_RIGHT ? INPUT_RIGHT :
|
||||
INPUT_NULL
|
||||
);
|
||||
|
||||
if(inputIsDown(&engine->input, bind)) {
|
||||
heldTime = engine->time.current - inputGetAccuated(&engine->input, bind);
|
||||
heldDue = engine->time.current - menu->holdLast;
|
||||
|
||||
// Have we held long enough?
|
||||
if(heldTime >= MENU_HOLD_DURATION) {
|
||||
|
||||
menu->holdLast = engine->time.current;
|
||||
}
|
||||
} else {
|
||||
menu->holdLast = engine->time.current;
|
||||
}
|
||||
// Update cursor positions
|
||||
if(x > 0) {
|
||||
menu->cursorX = (current->x + current->width - 1) + x;
|
||||
} else if(x < 0) {
|
||||
menu->cursorX = current->x + x;
|
||||
}
|
||||
|
||||
// Handle Mouse
|
||||
if(y > 0) {
|
||||
menu->cursorY = (current->y + current->height - 1) + y;
|
||||
} else if(y < 0) {
|
||||
menu->cursorY = current->y + y;
|
||||
}
|
||||
|
||||
// Get the item selected
|
||||
int32_t targetIndex = -1;
|
||||
for(i = 0; i < menu->itemCount; i++) {
|
||||
if(i == menu->selected) continue;
|
||||
item = menu->items + i;
|
||||
|
||||
if(
|
||||
item->i == -1 ||
|
||||
item->x > menu->cursorX || (item->x + item->width - 1) < menu->cursorX ||
|
||||
item->y > menu->cursorY || (item->y + item->height - 1) < menu->cursorY
|
||||
) continue;
|
||||
|
||||
targetIndex = i;
|
||||
break;
|
||||
}
|
||||
if(targetIndex == -1) return;
|
||||
menu->selected = targetIndex;
|
||||
}
|
@ -9,6 +9,7 @@
|
||||
#include <dawn/dawn.h>
|
||||
#include "../input/input.h"
|
||||
#include "../epoch/epoch.h"
|
||||
#include "../util/array.h"
|
||||
|
||||
/**
|
||||
* Initialize a menu.
|
||||
@ -17,7 +18,7 @@
|
||||
* @param columns Count of rows.
|
||||
* @param rows Count of columns.
|
||||
*/
|
||||
void menuInit(menu_t *menu, int32_t columns, int32_t rows);
|
||||
void menuInit(menu_t *menu);
|
||||
|
||||
/**
|
||||
* Updates the menu to handle inputs.
|
||||
|
Reference in New Issue
Block a user