Beispiel #1
0
void input_manager::init() {
    init_keycode_mapping();

    std::ifstream data_file;
    picojson::value input_value;

    std::string file_name = "data/raw/keybindings.json";
    data_file.open(file_name.c_str());

    if(!data_file.good()) {
        throw "Could not read " + file_name;
    }

    data_file >> input_value;
    data_file.close();

    if(!input_value.is<picojson::array>()) {
        throw file_name + "is not an array";
    }

    //Crawl through once and create an entry for every definition
    const picojson::array& root = input_value.get<picojson::array>();

    for (picojson::array::const_iterator entry = root.begin();
         entry != root.end(); ++entry) {
        if( !(entry->is<picojson::object>()) ){
            debugmsg("Invalid keybinding setting, entry not a JSON object");
            continue;
        }

        // JSON object representing the action
        const picojson::value& action_object = *entry;

        const std::string& action_id = action_object.get("id").get<std::string>();

        std::string context = "default";
        if(action_object.contains("category")) {
            context = action_object.get("category").get<std::string>();
        }

        // Iterate over the bindings JSON array
        const picojson::array& keybindings = action_object.get("bindings").get<picojson::array>();
        for (picojson::array::const_iterator subentry = keybindings.begin();
             subentry != keybindings.end(); ++subentry) {

            const picojson::value& keybinding = *subentry;
            const std::string& input_method = keybinding.get("input_method").get<std::string>();
            input_event new_event;
            if(input_method == "keyboard") {
                new_event.type = CATA_INPUT_KEYBOARD;
            } else if(input_method == "gamepad") {
                new_event.type = CATA_INPUT_GAMEPAD;
            }

            if(keybinding.get("key").is<std::string>()) {
                const std::string& key = keybinding.get("key").get<std::string>();

                new_event.sequence.push_back(inp_mngr.get_keycode(key));
            } else if(keybinding.get("key").is<picojson::array>()) {
                picojson::array keys = keybinding.get("key").get<picojson::array>();
                for(int i=0; i<keys.size(); i++) {
                    const std::string& next_key = keybinding.get("key").get(i).get<std::string>();

                    new_event.sequence.push_back(inp_mngr.get_keycode(next_key));
                }
            }

            if(context == "default") {
                action_to_input[action_id].push_back(new_event);
            } else {
                action_contexts[context][action_id].push_back(new_event);
            }
        }

        if(!action_object.contains("name")) {
            actionID_to_name[action_id] = action_id;
        } else {
            actionID_to_name[action_id] = action_object.get("name").get<std::string>();
        }
    }
}