From 7c100f0c37632ac0a5e1c23259cc463ed43267aa Mon Sep 17 00:00:00 2001 From: Steve Date: Sun, 17 Apr 2016 11:26:04 +0100 Subject: [PATCH] Added button to restore default controls. --- data/widgets/controls.json | 14 ++++++++++-- src/system/controls.c | 44 ++++++++++++++++++++++++++++++++++++++ src/system/controls.h | 2 ++ 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/data/widgets/controls.json b/data/widgets/controls.json index 198110a..629e346 100644 --- a/data/widgets/controls.json +++ b/data/widgets/controls.json @@ -103,9 +103,19 @@ "group" : "controls", "type" : "WT_BUTTON", "text" : "OK", - "x" : -1, + "x" : 500, "y" : 650, - "w" : 100, + "w" : 200, + "h": 34 + }, + { + "name" : "restore", + "group" : "controls", + "type" : "WT_BUTTON", + "text" : "Restore Defaults", + "x" : 780, + "y" : 650, + "w" : 200, "h": 34 } ] diff --git a/src/system/controls.c b/src/system/controls.c index 142dfb6..1f06728 100644 --- a/src/system/controls.c +++ b/src/system/controls.c @@ -20,6 +20,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "controls.h" +static void restoreDefaults(void); + static const char *controlName[CONTROL_MAX]; static Widget *controlWidget[CONTROL_MAX]; @@ -70,6 +72,8 @@ void initControlsDisplay(void) sprintf(controlWidget[i]->options[1], "Btn %d", app.mouseControls[i]); } } + + getWidget("restore", "controls")->action = restoreDefaults; } int isControl(int type) @@ -185,3 +189,43 @@ void drawControls(void) drawWidgets("controls"); } + +static void restoreDefaults(void) +{ + int i; + cJSON *root, *controlsJSON, *node; + char *text; + + text = readFile("data/app/"CONFIG_FILENAME); + + root = cJSON_Parse(text); + + controlsJSON = cJSON_GetObjectItem(root, "controls"); + if (controlsJSON) + { + node = cJSON_GetObjectItem(controlsJSON, "keys")->child; + while (node) + { + i = lookup(node->string); + + app.keyControls[i] = node->valueint; + + node = node->next; + } + + node = cJSON_GetObjectItem(controlsJSON, "mouse")->child; + while (node) + { + i = lookup(node->string); + + app.mouseControls[i] = node->valueint; + + node = node->next; + } + } + + cJSON_Delete(root); + free(text); + + initControlsDisplay(); +} diff --git a/src/system/controls.h b/src/system/controls.h index 4fe85d7..7725544 100644 --- a/src/system/controls.h +++ b/src/system/controls.h @@ -19,6 +19,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include "../common.h" +#include "../json/cJSON.h" extern void drawText(int x, int y, int size, int align, SDL_Color c, const char *format, ...); extern char *getTranslatedString(char *string); @@ -27,6 +28,7 @@ extern void drawWidgets(char *groupName); extern char *getLookupName(char *prefix, long num); extern long lookup(char *name); extern void limitTextWidth(int width); +extern char *readFile(char *filename); extern App app; extern Colors colors;