Explorar o código

small settings refactor

rdefeo hai 1 ano
pai
achega
bcc43cb5a0
Modificáronse 4 ficheiros con 39 adicións e 32 borrados
  1. 9 9
      pinball0.cxx
  2. 2 8
      pinball0.h
  3. 18 14
      settings.cxx
  4. 10 1
      settings.h

+ 9 - 9
pinball0.cxx

@@ -261,7 +261,7 @@ static void pinball_draw_callback(Canvas* const canvas, void* ctx) {
         if(pb->settings.sound_enabled) {
             canvas_draw_disc(canvas, x, y + 3, 2);
         }
-        if(pb->selected_setting == 0) {
+        if(pb->settings.selected_setting == 0) {
             canvas_draw_triangle(canvas, 2, y + 3, 8, 5, CanvasDirectionLeftToRight);
         }
         y += 12;
@@ -271,7 +271,7 @@ static void pinball_draw_callback(Canvas* const canvas, void* ctx) {
         if(pb->settings.led_enabled) {
             canvas_draw_disc(canvas, x, y + 3, 2);
         }
-        if(pb->selected_setting == 1) {
+        if(pb->settings.selected_setting == 1) {
             canvas_draw_triangle(canvas, 2, y + 3, 8, 5, CanvasDirectionLeftToRight);
         }
         y += 12;
@@ -281,7 +281,7 @@ static void pinball_draw_callback(Canvas* const canvas, void* ctx) {
         if(pb->settings.vibrate_enabled) {
             canvas_draw_disc(canvas, x, y + 3, 2);
         }
-        if(pb->selected_setting == 2) {
+        if(pb->settings.selected_setting == 2) {
             canvas_draw_triangle(canvas, 2, y + 3, 8, 5, CanvasDirectionLeftToRight);
         }
         y += 12;
@@ -291,7 +291,7 @@ static void pinball_draw_callback(Canvas* const canvas, void* ctx) {
         if(pb->settings.debug_mode) {
             canvas_draw_disc(canvas, x, y + 3, 2);
         }
-        if(pb->selected_setting == 3) {
+        if(pb->settings.selected_setting == 3) {
             canvas_draw_triangle(canvas, 2, y + 3, 8, 5, CanvasDirectionLeftToRight);
         }
 
@@ -448,8 +448,8 @@ extern "C" int32_t pinball0_app(void* p) {
                                                        app->table_list.menu_items.size();
                             break;
                         case GM_Settings:
-                            if(app->selected_setting > 0) {
-                                app->selected_setting--;
+                            if(app->settings.selected_setting > 0) {
+                                app->settings.selected_setting--;
                             }
                             break;
                         default:
@@ -472,8 +472,8 @@ extern "C" int32_t pinball0_app(void* p) {
                             // notify_game_over(app);
                             break;
                         case GM_Settings:
-                            if(app->selected_setting < app->max_settings - 1) {
-                                app->selected_setting++;
+                            if(app->settings.selected_setting < app->settings.max_settings - 1) {
+                                app->settings.selected_setting++;
                             }
                             break;
                         default:
@@ -503,7 +503,7 @@ extern "C" int32_t pinball0_app(void* p) {
                             }
                         } break;
                         case GM_Settings:
-                            switch(app->selected_setting) {
+                            switch(app->settings.selected_setting) {
                             case 0:
                                 app->settings.sound_enabled = !app->settings.sound_enabled;
                                 break;

+ 2 - 8
pinball0.h

@@ -16,6 +16,7 @@
 
 #include "vec2.h"
 #include "objects.h"
+#include "settings.h"
 
 // #define DRAW_NORMALS
 
@@ -84,14 +85,7 @@ typedef struct PinballApp {
     uint32_t idle_start; // tracks time of last key press
 
     // user settings
-    struct {
-        bool sound_enabled;
-        bool vibrate_enabled;
-        bool led_enabled;
-        bool debug_mode;
-    } settings;
-    int selected_setting;
-    int max_settings;
+    PinballSettings settings;
 
     // system objects
     Storage* storage;

+ 18 - 14
settings.cxx

@@ -1,6 +1,7 @@
 #include <flipper_format/flipper_format.h>
 
 #include "settings.h"
+#include "pinball0.h"
 
 #define PINBALL_SETTINGS_FILENAME     ".pinball0.conf"
 #define PINBALL_SETTINGS_PATH         APP_DATA_PATH(PINBALL_SETTINGS_FILENAME)
@@ -12,13 +13,14 @@ void pinball_load_settings(PinballApp* pb) {
     FuriString* tmp_str = furi_string_alloc();
     uint32_t tmp_data32 = 0;
 
+    PinballSettings& settings = pb->settings;
     // init the settings to default values, then overwrite them if found in the settings file
-    pb->settings.sound_enabled = true;
-    pb->settings.led_enabled = true;
-    pb->settings.vibrate_enabled = true;
-    pb->settings.debug_mode = false;
-    pb->selected_setting = 0;
-    pb->max_settings = 4;
+    settings.sound_enabled = true;
+    settings.led_enabled = true;
+    settings.vibrate_enabled = true;
+    settings.debug_mode = false;
+    settings.selected_setting = 0;
+    settings.max_settings = 4;
 
     do {
         if(!flipper_format_file_open_existing(fff_settings, PINBALL_SETTINGS_PATH)) {
@@ -36,16 +38,16 @@ void pinball_load_settings(PinballApp* pb) {
             break;
         }
         if(flipper_format_read_uint32(fff_settings, "Sound", &tmp_data32, 1)) {
-            pb->settings.sound_enabled = (tmp_data32 == 0) ? false : true;
+            settings.sound_enabled = (tmp_data32 == 0) ? false : true;
         }
         if(flipper_format_read_uint32(fff_settings, "LED", &tmp_data32, 1)) {
-            pb->settings.led_enabled = (tmp_data32 == 0) ? false : true;
+            settings.led_enabled = (tmp_data32 == 0) ? false : true;
         }
         if(flipper_format_read_uint32(fff_settings, "Vibrate", &tmp_data32, 1)) {
-            pb->settings.vibrate_enabled = (tmp_data32 == 0) ? false : true;
+            settings.vibrate_enabled = (tmp_data32 == 0) ? false : true;
         }
         if(flipper_format_read_uint32(fff_settings, "Debug", &tmp_data32, 1)) {
-            pb->settings.debug_mode = (tmp_data32 == 0) ? false : true;
+            settings.debug_mode = (tmp_data32 == 0) ? false : true;
         }
 
     } while(false);
@@ -57,6 +59,8 @@ void pinball_load_settings(PinballApp* pb) {
 void pinball_save_settings(PinballApp* pb) {
     FlipperFormat* fff_settings = flipper_format_file_alloc(pb->storage);
     uint32_t tmp_data32 = 0;
+    PinballSettings& settings = pb->settings;
+
     FURI_LOG_I(TAG, "SETTINGS: Saving settings");
     do {
         if(!flipper_format_file_open_always(fff_settings, PINBALL_SETTINGS_PATH)) {
@@ -69,22 +73,22 @@ void pinball_save_settings(PinballApp* pb) {
             break;
         }
         // now write out our settings data
-        tmp_data32 = pb->settings.sound_enabled ? 1 : 0;
+        tmp_data32 = settings.sound_enabled ? 1 : 0;
         if(!flipper_format_write_uint32(fff_settings, "Sound", &tmp_data32, 1)) {
             FURI_LOG_E(TAG, "SETTINGS: Failed to write 'Sound'");
             break;
         }
-        tmp_data32 = pb->settings.led_enabled ? 1 : 0;
+        tmp_data32 = settings.led_enabled ? 1 : 0;
         if(!flipper_format_write_uint32(fff_settings, "LED", &tmp_data32, 1)) {
             FURI_LOG_E(TAG, "SETTINGS: Failed to write 'LED'");
             break;
         }
-        tmp_data32 = pb->settings.vibrate_enabled ? 1 : 0;
+        tmp_data32 = settings.vibrate_enabled ? 1 : 0;
         if(!flipper_format_write_uint32(fff_settings, "Vibrate", &tmp_data32, 1)) {
             FURI_LOG_E(TAG, "SETTINGS: Failed to write 'Vibrate'");
             break;
         }
-        tmp_data32 = pb->settings.debug_mode ? 1 : 0;
+        tmp_data32 = settings.debug_mode ? 1 : 0;
         if(!flipper_format_write_uint32(fff_settings, "Debug", &tmp_data32, 1)) {
             FURI_LOG_E(TAG, "SETTINGS: Failed to write 'Debug'");
             break;

+ 10 - 1
settings.h

@@ -1,7 +1,16 @@
 #pragma once
 
-#include "pinball0.h"
+typedef struct {
+    bool sound_enabled;
+    bool vibrate_enabled;
+    bool led_enabled;
+    bool debug_mode;
 
+    int selected_setting;
+    int max_settings;
+} PinballSettings;
+
+struct PinballApp;
 // Read game settings from .pinball0.conf
 void pinball_load_settings(PinballApp* pb);