Command Pattern refactor for input processing?

Posted by Casey on Game Development See other posts from Game Development or by Casey
Published on 2012-12-18T03:35:10Z Indexed on 2012/12/18 5:13 UTC
Read the original article Hit count: 259

Filed under:
|

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();
    }
}

© Game Development or respective owner

Related posts about c++

Related posts about refactoring