Fixed buffer overflowing because of mouse input

This commit is contained in:
2021-08-30 22:50:16 -07:00
parent 840a1a55e3
commit 92d1721509
7 changed files with 102 additions and 39 deletions

View File

@ -38,36 +38,56 @@ void menuUpdate(menu_t *menu, engine_t *engine) {
} else if(inputIsPressed(&engine->input, INPUT_RIGHT)) {
x = 1;
y = 0;
} else {
x = 0;
y = 0;
}
// Update cursor positions
if(x > 0) {
menu->cursorX = (current->x + current->width - 1) + x;
} else if(x < 0) {
menu->cursorX = current->x + x;
}
if(x != 0 || y != 0) {
if(x > 0) {
menu->cursorX = (current->x + current->width - 1) + x;
} else if(x < 0) {
menu->cursorX = current->x + x;
}
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(y > 0) {
menu->cursorY = (current->y + current->height - 1) + y;
} else if(y < 0) {
menu->cursorY = current->y + y;
}
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;
// Get the item selected
int32_t targetIndex = -1;
for(i = 0; i < menu->itemCount; i++) {
if(i == menu->selected) continue;
item = menu->items + i;
targetIndex = i;
break;
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;
}
// Was a target found?
if(targetIndex == -1) return;
menu->selected = targetIndex;
}
if(targetIndex == -1) return;
menu->selected = targetIndex;
}
menuitem_t * menuAdd(menu_t *menu) {
menuitem_t *item = menu->items + menu->itemCount;
item->i = menu->itemCount;
item->x = 0;
item->y = 0;
item->width = 1;
item->height = 1;
menu->itemCount++;
return item;
}

View File

@ -26,4 +26,12 @@ void menuInit(menu_t *menu);
* @param menu Menu to update.
* @param engine Engine to update from.
*/
void menuUpdate(menu_t *menu, engine_t *engine);
void menuUpdate(menu_t *menu, engine_t *engine);
/**
* Add an item to the menu
*
* @param menu Menu to add to.
* @return Item to add to.
*/
menuitem_t * menuAdd(menu_t *menu);