From 7abd2f30cc61d650f154c15968b8cc5e9ba7b7bd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jo=C3=A3o=20Magalh=C3=A3es?= <joamag@gmail.com>
Date: Mon, 28 Nov 2022 23:06:35 +0000
Subject: [PATCH] feat: support for background and extra settings Theme stored
 in `localStorage`

---
 CHANGELOG.md           |  1 +
 frontends/web/index.ts |  2 +-
 frontends/web/ts/gb.ts | 20 +++++++++++++++++++-
 3 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 48e8e1db..c0a3446c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 ### Added
 
 * Support for theme and palette selection
+* Theme stored in `localStorage`
 
 ### Changed
 
diff --git a/frontends/web/index.ts b/frontends/web/index.ts
index aaddcffc..560d43e9 100644
--- a/frontends/web/index.ts
+++ b/frontends/web/index.ts
@@ -40,7 +40,7 @@ const BACKGROUNDS = [
 
     // creates the emulator structure and initializes the
     // React app with both the parameters and the emulator
-    const emulator = new GameboyEmulator();
+    const emulator = new GameboyEmulator({ background: background });
     startApp("app", {
         emulator: emulator,
         fullscreen: fullscreen,
diff --git a/frontends/web/ts/gb.ts b/frontends/web/ts/gb.ts
index e674e5cd..221fef8d 100644
--- a/frontends/web/ts/gb.ts
+++ b/frontends/web/ts/gb.ts
@@ -93,6 +93,18 @@ export class GameboyEmulator extends EmulatorBase implements Emulator {
     private romSize = 0;
     private cartridge: Cartridge | null = null;
 
+    /**
+     * Associative map for extra settings to be used in
+     * opaque local storage operations, associated setting
+     * name with its value as a string.
+     */
+    private extraSettings: Record<string, string> = {};
+
+    constructor(extraSettings = {}) {
+        super();
+        this.extraSettings = extraSettings;
+    }
+
     /**
      * Runs the initialization and main loop execution for
      * the Game Boy emulator.
@@ -592,6 +604,11 @@ export class GameboyEmulator extends EmulatorBase implements Emulator {
         }
     }
 
+    onBackground(background: string) {
+        this.extraSettings.background = background;
+        this.storeSettings();
+    }
+
     /**
      * Tries to load game RAM from the `localStorage` using the
      * current cartridge title as the name of the item and
@@ -620,7 +637,8 @@ export class GameboyEmulator extends EmulatorBase implements Emulator {
     private storeSettings() {
         if (!window.localStorage) return;
         const settings = {
-            palette: PALETTES[this.paletteIndex].name
+            palette: PALETTES[this.paletteIndex].name,
+            ...this.extraSettings
         };
         localStorage.setItem("settings", JSON.stringify(settings));
     }
-- 
GitLab