Command Pattern refactor for input processing?
- by Casey
According to Game Coding Complete 4th. ed. processing input via the following is considered unmanagable and inflexible. But does not show an example. I've used the Command pattern to represent GUI button commands but could not figure out how to represent the input from the keyboard and/or mouse.
if(g_keyboard->KeyDown(KEY_ESC)) {
quit = true;
return;
}
//Processing
if(g_keyboard->KeyDown(KEY_T)) {
g_show_test_gateway = !g_show_test_gateway;
}
if(g_mouse->ButtonDown(a2de::Mouse::BUTTON2)) {
g_selected_part = GWPart::PART_NONE;
SetMouseImageToPartImage();
}
ResetButtonStates();
g_prevButton = g_curButton;
g_curButton = GetButtonHovered();
if(g_curButton) {
g_mouse->SetImageToDefault();
if(g_mouse->ButtonDown(a2de::Mouse::BUTTON1) || g_mouse->ButtonPress(a2de::Mouse::BUTTON1)) {
ButtonPressCommand curCommand(g_curButton);
curCommand.Execute();
} else if(g_mouse->ButtonUp(a2de::Mouse::BUTTON1)) {
if(g_prevButton == g_curButton) {
ButtonReleaseCommand curCommand(g_curButton);
curCommand.Execute();
if(g_curButton->GetType() == "export") {
ExportCommand curCommand(g_curButton, *g_gateway);
curCommand.Execute();
}
} else {
ResetButtonStates();
}
} else {
ButtonHoverCommand curCommand(g_curButton);
curCommand.Execute();
}
} else {
g_status_message.clear();
SetMouseImageToPartImage();
if(g_mouse->ButtonDown(a2de::Mouse::BUTTON1)) {
CreatePartCommand curCommand(*g_gateway, g_selected_part, a2de::Vector2D(g_mouse->GetX(), g_mouse->GetY()));
curCommand.Execute();
}
}