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

views/select_pokemon: Refactor input processing

Only care about one event type, turn the lookup of that in to a
switch statement
Kris Bahnsen 2 лет назад
Родитель
Сommit
a75ac8f128
1 измененных файлов с 35 добавлено и 7 удалено
  1. 35 7
      views/select_pokemon.cpp

+ 35 - 7
views/select_pokemon.cpp

@@ -28,7 +28,12 @@ static bool select_pokemon_input_callback(InputEvent* event, void* context) {
     SelectPokemon* select_pokemon = (SelectPokemon*)context;
     bool consumed = false;
 
-    if(event->type == InputTypePress && event->key == InputKeyOk) {
+    /* We only handle InputTypePress at the moment */
+    if(event->type != InputTypePress) return consumed;
+
+    switch(event->key) {
+    /* Advance to next view with the selected pokemon */
+    case InputKeyOk:
         with_view_model_cpp(
             select_pokemon->view,
             SelectPokemonModel*,
@@ -40,10 +45,16 @@ static bool select_pokemon_input_callback(InputEvent* event, void* context) {
             false);
         view_dispatcher_switch_to_view(select_pokemon->app->view_dispatcher, AppViewTrade);
         consumed = true;
-    } else if(event->type == InputTypePress && event->key == InputKeyBack) {
+        break;
+
+    /* Return to the previous view */
+    case InputKeyBack:
         view_dispatcher_switch_to_view(select_pokemon->app->view_dispatcher, VIEW_NONE);
         consumed = true;
-    } else if(event->type == InputTypePress && event->key == InputKeyLeft) {
+        break;
+
+    /* Move back one through the pokedex listing */
+    case InputKeyLeft:
         with_view_model_cpp(
             select_pokemon->view,
             SelectPokemonModel*,
@@ -57,7 +68,12 @@ static bool select_pokemon_input_callback(InputEvent* event, void* context) {
             },
             true);
         consumed = true;
-    } else if(event->type == InputTypePress && event->key == InputKeyDown) {
+        break;
+
+    /* Move back ten through the pokemon listing, wrap to max pokemon on
+         * underflow.
+         */
+    case InputKeyDown:
         with_view_model_cpp(
             select_pokemon->view,
             SelectPokemonModel*,
@@ -71,7 +87,10 @@ static bool select_pokemon_input_callback(InputEvent* event, void* context) {
             },
             true);
         consumed = true;
-    } else if(event->type == InputTypePress && event->key == InputKeyRight) {
+        break;
+
+    /* Move forward one through the pokedex listing */
+    case InputKeyRight:
         with_view_model_cpp(
             select_pokemon->view,
             SelectPokemonModel*,
@@ -85,7 +104,12 @@ static bool select_pokemon_input_callback(InputEvent* event, void* context) {
             },
             true);
         consumed = true;
-    } else if(event->type == InputTypePress && event->key == InputKeyUp) {
+        break;
+
+    /* Move forward ten through the pokemon listing, wrap to min pokemon on
+         * overflow.
+         */
+    case InputKeyUp:
         with_view_model_cpp(
             select_pokemon->view,
             SelectPokemonModel*,
@@ -95,11 +119,15 @@ static bool select_pokemon_input_callback(InputEvent* event, void* context) {
                     model->current_pokemon += 10;
                 } else {
                     model->current_pokemon = 0;
-                    ;
                 }
             },
             true);
         consumed = true;
+        break;
+
+    default:
+        // Do Nothing
+        break;
     }
 
     return consumed;