Skip to content

Commit

Permalink
Merge pull request #2120 from sthilaid/menu-item-hotkey-cleaned
Browse files Browse the repository at this point in the history
Added hotkey option to menu items. Used in Yes/No dialog.
  • Loading branch information
nesbox authored Feb 7, 2023
2 parents 39d96f7 + 1add08c commit b12c171
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
11 changes: 10 additions & 1 deletion src/studio/screens/menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ static void drawMenu(Menu* menu, s32 x, s32 y)
}

if(tic_api_btnp(menu->tic, A, -1, -1)
|| tic_api_keyp(tic, tic_key_return, Hold, Period))
|| tic_api_keyp(tic, tic_key_return, Hold, Period))
{
if(option)
{
Expand Down Expand Up @@ -286,6 +286,15 @@ static void drawMenu(Menu* menu, s32 x, s32 y)
tic_rect rect = {x + (TIC80_WIDTH - width) / 2 + menu->anim.offset,
y + TextMargin + ItemHeight * (i - menu->pos) - menu->anim.pos, it->width, TIC_FONT_HEIGHT};

if (it->hotkey != tic_key_unknown && tic_api_keyp(tic, it->hotkey, Hold, Period))
{
// hotkeys not supported on options for simplicity
if(it->option == NULL && it->handler)
onMenuItem(menu, it);

menu->pos = it - menu->items; // set pos so that close will call this handler
}

bool down = false;
if(animIdle(menu) && checkMousePos(menu->studio, &rect) && it->handler)
{
Expand Down
2 changes: 2 additions & 0 deletions src/studio/screens/menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#pragma once

#include "tic80_types.h"
#include "tic.h"

typedef struct Menu Menu;
struct tic_mem;
Expand Down Expand Up @@ -55,6 +56,7 @@ typedef struct
const char* help;
bool back;
s32 width;
tic_keycode hotkey;
} MenuItem;

void studio_menu_init(Menu* menu, const MenuItem* items, s32 rows, s32 pos, s32 backPos, MenuItemHandler handler, void* data);
Expand Down
9 changes: 6 additions & 3 deletions src/studio/studio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1372,13 +1372,16 @@ void confirmDialog(Studio* studio, const char** text, s32 rows, ConfirmCallback
studio->menuMode = studio->mode;
studio->mode = TIC_MENU_MODE;

static const MenuItem Answers[] =
static MenuItem Answers[] =
{
{"", NULL},
{"NO", confirmNo},
{"YES", confirmYes},
{"(N)O", confirmNo},
{"(Y)ES", confirmYes},
};

Answers[1].hotkey = tic_key_n;
Answers[2].hotkey = tic_key_y;

s32 count = rows + COUNT_OF(Answers);
MenuItem* items = malloc(sizeof items[0] * count);
SCOPE(free(items))
Expand Down

0 comments on commit b12c171

Please sign in to comment.