Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added hotkey option to menu items. Used in Yes/No dialog. #2120

Merged
merged 1 commit into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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