ControllerPtr Manager::create_controller(const Pathname& filename) { ControllerPtr controller(new Controller(desc)); ReaderObject reader_object = Reader::parse(filename); if (reader_object.get_name() != "pingus-controller") { raise_exception(std::runtime_error, "Controller: invalid config file '" << filename.str() << "'"); } else { ReaderMapping reader = reader_object.get_mapping(); ReaderMapping controls_mapping; if (!reader.read_mapping("controls", controls_mapping)) { log_warn("%1%: 'controls' section missing", filename); } else { for (const auto& key : controls_mapping.get_keys()) { ReaderCollection collection; if (!controls_mapping.read_collection(key.c_str(), collection)) { log_error("%1%: mapping must contain object at %2%", filename, key); } else { if (StringUtil::has_suffix(key, "pointer")) { int id = desc.get_definition(key).id; ControllerPointer* ctrl_pointer = controller->get_pointer(id); for(const auto& object : collection.get_objects()) { auto pointer = create_pointer(object, ctrl_pointer); if (pointer) { ctrl_pointer->add_pointer(std::move(pointer)); } else { log_error("Manager: pointer: Couldn't create pointer %1%", object.get_name()); } } } else if (StringUtil::has_suffix(key, "scroller")) { int id = desc.get_definition(key).id; ControllerScroller* ctrl_scroller = controller->get_scroller(id); for(const auto& object : collection.get_objects()) { auto scroller = create_scroller(object, ctrl_scroller); if (scroller) { ctrl_scroller->add_scroller(std::move(scroller)); } else { log_error("Manager: scroller: Couldn't create scroller %1%", object.get_name()); } } } else if (StringUtil::has_suffix(key, "button")) { int id = desc.get_definition(key).id; ControllerButton* ctrl_button = controller->get_button(id); for(const auto& object : collection.get_objects()) { auto button = create_button(object, ctrl_button); if (button) { ctrl_button->add_button(std::move(button)); } else { log_error("Manager: button: Couldn't create button %1%", object.get_name()); } } } else if (StringUtil::has_suffix(key, "axis")) { int id = desc.get_definition(key).id; ControllerAxis* ctrl_axis = controller->get_axis(id); for(const auto& object : collection.get_objects()) { auto axis = create_axis(object, ctrl_axis); if (axis) { ctrl_axis->add_axis(std::move(axis)); } else { log_error("Manager: axis: Couldn't create axis %1%", object.get_name()); } } } else if (StringUtil::has_suffix(key, "keyboard")) { int id = desc.get_definition(key).id; ControllerKeyboard* ctrl_keyboard = controller->get_keyboard(id); for(const auto& object : collection.get_objects()) { std::unique_ptr<Keyboard> keyboard = create_keyboard(object, ctrl_keyboard); if (keyboard) { ctrl_keyboard->add_keyboard(std::move(keyboard)); } else { log_error("Manager: keyboard: Couldn't create keyboard %1%", object.get_name()); } } } else { raise_exception(std::runtime_error, "Manager: Unkown Element in Controller Config: " << key); } } } } } controllers.push_back(controller); return controller; }
Controller* Manager::create_controller(const Pathname& filename) { std::unique_ptr<Controller> controller(new Controller(desc)); FileReader reader = FileReader::parse(filename); if (reader.get_name() != "pingus-controller") { throw std::runtime_error("Controller: invalid config file '" + filename.str() + "'"); } else { const std::vector<FileReader>& sections = reader.get_sections(); for (std::vector<FileReader>::const_iterator i = sections.begin(); i != sections.end(); ++i) { if (StringUtil::has_suffix(i->get_name(), "pointer")) { const std::vector<FileReader>& pointers = i->get_sections(); for(std::vector<FileReader>::const_iterator j = pointers.begin(); j != pointers.end(); ++j) { int id = desc.get_definition(i->get_name()).id; ControllerPointer* ctrl_pointer = controller->get_pointer(id); Pointer* pointer = create_pointer(*j, ctrl_pointer); if (pointer) ctrl_pointer->add_pointer(pointer); else std::cout << "Manager: pointer: Couldn't create pointer " << j->get_name() << std::endl; } } else if (StringUtil::has_suffix(i->get_name(), "scroller")) { const std::vector<FileReader>& scrollers = i->get_sections(); for(std::vector<FileReader>::const_iterator j = scrollers.begin(); j != scrollers.end(); ++j) { int id = desc.get_definition(i->get_name()).id; ControllerScroller* ctrl_scroller = controller->get_scroller(id); Scroller* scroller = create_scroller(*j, ctrl_scroller); if (scroller) ctrl_scroller->add_scroller(scroller); else std::cout << "Manager: scroller: Couldn't create scroller " << j->get_name() << std::endl; } } else if (StringUtil::has_suffix(i->get_name(), "button")) { const std::vector<FileReader>& buttons = i->get_sections(); for(std::vector<FileReader>::const_iterator j = buttons.begin(); j != buttons.end(); ++j) { int id = desc.get_definition(i->get_name()).id; ControllerButton* ctrl_button = controller->get_button(id); Button* button = create_button(*j, ctrl_button); if (button) ctrl_button->add_button(button); else std::cout << "Manager: button: Couldn't create button " << j->get_name() << std::endl; } } else if (StringUtil::has_suffix(i->get_name(), "axis")) { const std::vector<FileReader>& axes = i->get_sections(); for(std::vector<FileReader>::const_iterator j = axes.begin(); j != axes.end(); ++j) { int id = desc.get_definition(i->get_name()).id; ControllerAxis* ctrl_axis = controller->get_axis(id); Axis* axis = create_axis(*j, ctrl_axis); if (axis) ctrl_axis->add_axis(axis); else std::cout << "Manager: axis: Couldn't create axis " << j->get_name() << std::endl; } } else if (StringUtil::has_suffix(i->get_name(), "keyboard")) { const std::vector<FileReader>& keyboards = i->get_sections(); for(std::vector<FileReader>::const_iterator j = keyboards.begin(); j != keyboards.end(); ++j) { int id = desc.get_definition(i->get_name()).id; ControllerKeyboard* ctrl_keyboard = controller->get_keyboard(id); Keyboard* keyboard = create_keyboard(*j, ctrl_keyboard); if (keyboard) ctrl_keyboard->add_keyboard(keyboard); else std::cout << "Manager: keyboard: Couldn't create keyboard " << j->get_name() << std::endl; } } else { throw std::runtime_error(std::string("Manager: Unkown Element in Controller Config: ") + i->get_name()); } } } Controller* ctrl = controller.release(); controllers.push_back(ctrl); return ctrl; }