Skip to content

Commit

Permalink
views/select_pokemon: Refactor input processing
Browse files Browse the repository at this point in the history
Only care about one event type, turn the lookup of that in to a
switch statement
  • Loading branch information
kbembedded committed Sep 15, 2023
1 parent ddc7edb commit a75ac8f
Showing 1 changed file with 35 additions and 7 deletions.
42 changes: 35 additions & 7 deletions views/select_pokemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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*,
Expand All @@ -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*,
Expand All @@ -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*,
Expand All @@ -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*,
Expand All @@ -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*,
Expand All @@ -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;
Expand Down

0 comments on commit a75ac8f

Please sign in to comment.