Просмотр исходного кода

pokerus: Separate out accessor functions from scene

Signed-off-by: Kris Bahnsen <Kris@KBEmbedded.com>
Kris Bahnsen 1 год назад
Родитель
Сommit
eafd365c43

+ 14 - 0
src/include/pokemon_pokerus.h

@@ -0,0 +1,14 @@
+#ifndef POKEMON_POKERUS_H
+#define POKEMON_POKERUS_H
+
+#include <src/include/pokemon_data.h>
+
+#pragma once
+
+const char* pokerus_get_status_str(PokemonData* pdata);
+
+void pokerus_set_strain(PokemonData* pdata, uint8_t strain);
+
+void pokerus_set_days(PokemonData *pdata, uint8_t days);
+
+#endif // POKEMON_POKERUS_H

+ 51 - 0
src/pokemon_pokerus.c

@@ -0,0 +1,51 @@
+#include <gui/modules/variable_item_list.h>
+#include <furi.h>
+
+#include <src/include/pokemon_data.h>
+
+static const char* pokerus_states[] = {
+    "Clean",
+    "Infected",
+    "Cured",
+    "",
+};
+
+const char* pokerus_get_status_str(PokemonData* pdata) {
+    uint8_t pokerus;
+
+    pokerus = pokemon_stat_get(pdata, STAT_POKERUS, NONE);
+
+    if(pokerus == 0x00)
+        return pokerus_states[0];
+
+    if((pokerus & 0x0f) != 0x00)
+        return pokerus_states[1];
+
+    return pokerus_states[2];
+}
+
+void pokerus_set_strain(PokemonData* pdata, uint8_t strain) {
+    uint8_t pokerus;
+
+    /* Need to read/modify/write the existing stat */
+    pokerus = pokemon_stat_get(pdata, STAT_POKERUS, NONE);
+    pokerus &= 0x0f;
+    pokerus |= (strain << 4);
+
+    if((pokerus & 0xf0) == 0x00)
+        pokerus = 0;
+
+    pokemon_stat_set(pdata, STAT_POKERUS, NONE, pokerus);
+}
+
+void pokerus_set_days(PokemonData *pdata, uint8_t days) {
+    uint8_t pokerus;
+
+    days &= 0x0f;
+
+    /* Need to read/modify/write the existing stat */
+    pokerus = pokemon_stat_get(pdata, STAT_POKERUS, NONE);
+    pokerus &= 0xf0;
+    pokerus |= days;
+    pokemon_stat_set(pdata, STAT_POKERUS, NONE, pokerus);
+}

+ 0 - 10
src/scenes/include/pokemon_pokerus.h

@@ -1,10 +0,0 @@
-#ifndef POKEMON_POKERUS_H
-#define POKEMON_POKERUS_H
-
-#include <src/include/pokemon_app.h>
-
-#pragma once
-
-const char* select_pokerus_status(PokemonFap* pokemon_fap);
-
-#endif // POKEMON_POKERUS_H

+ 2 - 2
src/scenes/pokemon_gen.c

@@ -12,7 +12,7 @@
 #include <src/scenes/include/pokemon_menu.h>
 #include <src/include/pokemon_shiny.h>
 #include <src/include/pokemon_gender.h>
-#include <src/scenes/include/pokemon_pokerus.h>
+#include <src/include/pokemon_pokerus.h>
 #include <src/include/unown_form.h>
 
 static void scene_change_from_main_cb(void* context, uint32_t index) {
@@ -188,7 +188,7 @@ void pokemon_scene_gen_on_enter(void* context) {
         submenu_add_item(
             pokemon_fap->submenu, buf, PokemonSceneGender, scene_change_from_main_cb, pokemon_fap);
 
-        snprintf(buf, sizeof(buf), "Pokerus:       %s", select_pokerus_status(pokemon_fap));
+        snprintf(buf, sizeof(buf), "Pokerus:       %s", pokerus_get_status_str(pokemon_fap->pdata));
         submenu_add_item(
             pokemon_fap->submenu, buf, PokemonScenePokerus, scene_change_from_main_cb, pokemon_fap);
 

+ 4 - 31
src/scenes/pokemon_pokerus.c

@@ -4,13 +4,7 @@
 #include <src/include/pokemon_app.h>
 
 #include <src/scenes/include/pokemon_scene.h>
-
-static const char* pokerus_states[] = {
-    "Clean",
-    "Infected",
-    "Cured",
-    "",
-};
+#include <src/include/pokemon_pokerus.h>
 
 static const char* strains[] = {
     "None",
@@ -21,16 +15,6 @@ static const char* strains[] = {
     "",
 };
 
-const char* select_pokerus_status(PokemonFap* pokemon_fap) {
-    uint8_t pokerus;
-
-    pokerus = pokemon_stat_get(pokemon_fap->pdata, STAT_POKERUS, NONE);
-
-    if(pokerus == 0x00) return pokerus_states[0];
-    if((pokerus & 0x0f) != 0x00) return pokerus_states[1];
-    return pokerus_states[2];
-}
-
 struct pokerus_itemlist {
     VariableItem* strain;
     VariableItem* days;
@@ -41,13 +25,8 @@ static void select_pokerus_rebuild_list(PokemonFap* pokemon_fap);
 
 static void select_strain_callback(VariableItem* item) {
     uint8_t index = variable_item_get_current_value_index(item);
-    uint8_t pokerus;
     PokemonFap* pokemon_fap = variable_item_get_context(item);
 
-    /* Need to read/modify/write the existing stat */
-    pokerus = pokemon_stat_get(pokemon_fap->pdata, STAT_POKERUS, NONE);
-    pokerus &= 0x0f;
-
     /* Need to set the new text from the mangled index */
     variable_item_set_current_value_text(item, strains[index]);
 
@@ -58,9 +37,8 @@ static void select_strain_callback(VariableItem* item) {
         index = 0x04; // Map this back to the A strain
     else
         index--;
-    pokerus |= (index << 4);
-    if((pokerus & 0xf0) == 0x00) pokerus = 0;
-    pokemon_stat_set(pokemon_fap->pdata, STAT_POKERUS, NONE, pokerus);
+
+    pokerus_set_strain(pokemon_fap->pdata, index);
 
     select_pokerus_rebuild_list(pokemon_fap);
     variable_item_list_set_selected_item(pokemon_fap->variable_item_list, 0);
@@ -68,14 +46,9 @@ static void select_strain_callback(VariableItem* item) {
 
 static void select_days_callback(VariableItem* item) {
     uint8_t index = variable_item_get_current_value_index(item);
-    uint8_t pokerus;
     PokemonFap* pokemon_fap = variable_item_get_context(item);
 
-    /* Need to read/modify/write the existing stat */
-    pokerus = pokemon_stat_get(pokemon_fap->pdata, STAT_POKERUS, NONE);
-    pokerus &= 0xf0;
-    pokerus |= index;
-    pokemon_stat_set(pokemon_fap->pdata, STAT_POKERUS, NONE, pokerus);
+    pokerus_set_days(pokemon_fap->pdata, index);
 
     select_pokerus_rebuild_list(pokemon_fap);
     variable_item_list_set_selected_item(pokemon_fap->variable_item_list, 1);