Skip to content

Commit

Permalink
cutom help texts for buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
badda71 committed Jan 1, 2020
1 parent 6c6d1de commit c91ea82
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 0 deletions.
2 changes: 2 additions & 0 deletions source/common/arch/3ds/uibottom.c
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,8 @@ char *get_key_help(int key, int inmenu, int trylen) {
resources_get_int( "JoyDevice2", &dev2);

if (!inmenu) {
if (custom_help_text[key]) return custom_help_text[key];

// check key mapping
if (resolvemapping && (i1=keymap3ds[key])!=0) {
if (i1 & 0x01000000) { // keymap
Expand Down
65 changes: 65 additions & 0 deletions source/common/arch/3ds/vice3ds.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "kbd.h"
#include "mousedrv.h"
#include "lib.h"
#include "log.h"
#include "resources.h"
#include "archdep_xdg.h"
#include "archdep_cp.h"
Expand Down Expand Up @@ -256,6 +257,62 @@ static int set_chg_root_directory(const char *val, void *param)
return -1;
}

char *custom_help_text[HELPTEXT_MAX]={NULL};
static char* custom_help_texts=NULL;

static int load_help_texts_from_resource() {
char *p,*saveptr;
int keynum;

if (custom_help_texts == NULL) return 0;
char *buffer=lib_stralloc(custom_help_texts);
// clear help texts
for (int i = 0; i < HELPTEXT_MAX; ++i) {
if (custom_help_text[i]) free(custom_help_text[i]);
custom_help_text[i] = NULL;
}

p = strtok_r(buffer, " \t:", &saveptr);
while (p) {
keynum = (int)strtol(p,NULL,10);

if (keynum >= HELPTEXT_MAX || keynum<1) {
log_error(LOG_ERR, "Help text keynum not valid: %i!", keynum);
strtok_r(NULL, "|\r\n", &saveptr);
} else if ((p = strtok_r(NULL, "|\r\n", &saveptr)) != NULL) {
custom_help_text[keynum]=lib_stralloc(p);
}
p = strtok_r(NULL, " \t:", &saveptr);
}
lib_free(buffer);
uibottom_must_redraw |= UIB_RECALC_SBUTTONS;
return 0;
}

int save_help_texts_to_resource() {
if (custom_help_texts) free(custom_help_texts);
custom_help_texts=malloc(1);
custom_help_texts[0]=0;
int count=0;

// write the hotkeys
for (int i = 0; i < HELPTEXT_MAX; ++i) {
if (custom_help_text[i]) {
custom_help_texts=realloc(custom_help_texts, strlen(custom_help_texts) + strlen (custom_help_text[i]) + 7);
sprintf(custom_help_texts + strlen(custom_help_texts), "%s%d %s", count++?"|":"", i, custom_help_text[i]);
}
}
return 0;
}

static int set_custom_help_texts(const char *val, void *param)
{
if (util_string_set(&custom_help_texts, val)) {
return 0;
}
return load_help_texts_from_resource();
}

static resource_string_t resources_string[] = {
{ "Command01", "load\"*\",8,1\\run\\", RES_EVENT_NO, NULL,
&command[0], set_command, (void*)0},
Expand Down Expand Up @@ -300,6 +357,9 @@ static resource_string_t resources_string[] = {

{ "ChgRootDirectory", "", RES_EVENT_NO, NULL,
&chg_root_directory, set_chg_root_directory, NULL},

{ "CustomHelpTexts", "", RES_EVENT_NO, NULL,
&custom_help_texts, set_custom_help_texts, NULL},
RESOURCE_STRING_LIST_END
};
/*
Expand Down Expand Up @@ -337,6 +397,11 @@ void vice3ds_resources_shutdown(void)
sync_handle[i]=0L;
}
}
// free custom help texts
for (int i = 0; i < HELPTEXT_MAX; ++i) {
if (custom_help_text[i]) free(custom_help_text[i]);
custom_help_text[i] = NULL;
}
}

int do_common_3DS_actions(SDL_Event *e){
Expand Down
4 changes: 4 additions & 0 deletions source/common/arch/3ds/vice3ds.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@
*
*/
#define KEYMAPPINGS_DEFAULT "cc 02010000 ce 02030000 d6 011e0000 d7 01110000 d8 011f0000 d9 011d0000 e8 01031900 f6 01030000 f7 01200000 f8 010d0000"
#define HELPTEXT_MAX 256

extern char *chg_root_directory;
extern int keymap3ds[256];
extern char *keymap3ds_resource;
extern char *custom_help_text[HELPTEXT_MAX];

extern int LED3DS_On(unsigned char r, unsigned char g, unsigned char b);
extern int LED3DS_Off(void);
Expand All @@ -42,4 +44,6 @@ extern int do_common_3DS_actions(SDL_Event *e);
extern void waitSync(int);
extern void triggerSync(int);
extern void copy_autocopy_dir();
extern int save_help_texts_to_resource();


74 changes: 74 additions & 0 deletions source/common/arch/sdl/menu_help.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,13 @@
#include "menu_help.h"
#include "ui.h"
#include "uimenu.h"
#include "uipoll.h"
#include "util.h"
#include "version.h"
#include "vicefeatures.h"
#include "uibottom.h"
#include "kbd.h"
#include "vice3ds.h"

const char info_about_text[] =
" Vice3DS\n"
Expand Down Expand Up @@ -102,6 +105,67 @@ static UI_MENU_CALLBACK(helpscreen_callback)
return NULL;
}

static UI_MENU_CALLBACK(help_text_add_callback)
{
SDL_Event e;
char buf[40];
char *value = NULL;

if (activated) {
SDL_Event s = sdl_ui_poll_event("Key", "Custom Help Text", SDL_POLL_KEYBOARD, 5);
if (s.type == SDL_KEYDOWN) {
while (SDL_PollEvent(&e)); // clear event queue
int key=s.key.keysym.sym;
if (key>0 && key<HELPTEXT_MAX) {
snprintf(buf, 40, "Custom Help Text for: %s", get_3ds_keyname(key));
value = sdl_ui_text_input_dialog(buf, custom_help_text[key]?custom_help_text[key]:"");
if (value) {
if (custom_help_text[key]) free(custom_help_text[key]);
if (value[0]==0) {
custom_help_text[key]=NULL;
free(value);
uibottom_must_redraw |= UIB_RECALC_SBUTTONS;
ui_message("Deleted help text for key %s", get_3ds_keyname(key));
} else {
custom_help_text[key]=value;
uibottom_must_redraw |= UIB_RECALC_SBUTTONS;
ui_message("Set help text for key %s to \"%s\"", get_3ds_keyname(key),value);
}
save_help_texts_to_resource();
// recalc sbuttons in case the help text is stated there
}
}
}
}
return NULL;
}

static UI_MENU_CALLBACK(help_text_list_callback)
{
int i,count=0;
char *buf;
if (activated) {
for (i=1;i<HELPTEXT_MAX;i++)
if (custom_help_text[i]!=0) count++;

buf=malloc(40*(count+3));
buf[0]=0;

if (count == 0) {
sprintf(buf+strlen(buf),"-- NONE --");
} else {
for (int i=1;i<HELPTEXT_MAX;i++) {
if (custom_help_text[i] == NULL) continue;
snprintf(buf+strlen(buf),41,"%-10s: %s",get_3ds_keyname(i), custom_help_text[i]);
sprintf(buf+strlen(buf),"\n");
}
}
ui_show_text("Custom Help Texts",buf);
free(buf);
}
return NULL;
}

const ui_menu_entry_t help_menu[] = {
{ "About",
MENU_ENTRY_DIALOG,
Expand All @@ -122,6 +186,16 @@ const ui_menu_entry_t help_menu[] = {
{ "Show help button on bottom screen",
MENU_ENTRY_RESOURCE_TOGGLE,
toggle_HelpButtonOn_callback,
NULL },
SDL_MENU_ITEM_SEPARATOR,
SDL_MENU_ITEM_TITLE("Custom help texts"),
{ "Add/delete custom help text",
MENU_ENTRY_DIALOG,
help_text_add_callback,
NULL },
{ "List custom help texts",
MENU_ENTRY_DIALOG,
help_text_list_callback,
NULL },
SDL_MENU_LIST_END
};

0 comments on commit c91ea82

Please sign in to comment.