V-Gears 0
Free Final Fantasy VII engine.
InputManagerCommands.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2022 The V-Gears Team
3 *
4 * This file is part of V-Gears
5 *
6 * V-Gears is free software: you can redistribute it and/or modify it under
7 * terms of the GNU General Public License as published by the Free Software
8 * Foundation, version 3.0 (GPLv3) of the License.
9 *
10 * V-Gears is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16#include <OgreStringConverter.h>
17
18#include "ConfigCmdHandler.h"
19#include "ConfigVarHandler.h"
20#include "Console.h"
21#include "Logger.h"
22#include "Utilites.h"
23
35bool ParseKeys(const Ogre::String& string, ButtonList& key_codes){
36 Ogre::StringVector keys = Ogre::StringUtil::split(string, "+");
37
38 for (unsigned int i = 0; i < keys.size(); ++ i)
39 key_codes.push_back(StringToKey(keys[i]));
40
41 bool fail = false;
42 for (unsigned int i = 0; i < key_codes.size(); ++ i){
43 if (key_codes[i] == OIS::KC_UNASSIGNED){
45 "Failed to parse key string \"" + string
46 + "\". Can't recognize key " + Ogre::StringConverter::toString(i)
47 );
48 fail = true;
49 }
50 }
51 return fail;
52}
53
64void CmdBind(const Ogre::StringVector& params){
65 if (params.size() != 3){
66 Console::getSingleton().AddTextToOutput(
67 "Usage: /bind <key1>+[key2]+[key3] \"<command line>\""
68 );
69 return;
70 }
71 ButtonList key_codes;
72 if (ParseKeys(params[1], key_codes) == false){
73 Ogre::StringVector params_cmd = StringTokenise(params[2]);
74
75 // Handle command
76 ConfigCmd* cmd = ConfigCmdHandler::getSingleton().Find(params_cmd[0]);
77 if (cmd != NULL){
78 InputManager::getSingleton().BindCommand(
79 cmd, params_cmd, key_codes
80 );
82 "Bind \"" + params[1] + "\" to command \"" + params[2] + "\"."
83 );
84 }
85 else{
87 "Can't find command \"" + params_cmd[0]
88 + "\" in bind command \"" + params[2] + "\"."
89 );
90 }
91 }
92}
93
104void CmdBindGameEvent(const Ogre::StringVector& params){
105 if (params.size() != 3){
106 Console::getSingleton().AddTextToOutput(
107 "Usage: /game_bind <key1>+[key2]+[key3] \"<game event>\""
108 );
109 return;
110 }
111 ButtonList key_codes;
112 if (ParseKeys(params[1], key_codes) == false){
113 InputManager::getSingleton().BindGameEvent(params[2], key_codes);
115 "Bind \"" + params[1] + "\" to game event \"" + params[2] + "\"."
116 );
117 }
118}
119
120// TODO: Move this to InpuManager.cpp?
122 ConfigCmdHandler::getSingleton().AddCommand(
123 "bind", "Bind command to keys", "", CmdBind, NULL
124 );
125 ConfigCmdHandler::getSingleton().AddCommand(
126 "bind_game_event", "Bind game event to keys", "", CmdBindGameEvent, NULL
127 );
128}
bool ParseKeys(const Ogre::String &string, ButtonList &key_codes)
Parses a list of keys to retrieve their keycodes.
Definition: InputManagerCommands.h:35
void CmdBind(const Ogre::StringVector &params)
Binds a key combination to a command.
Definition: InputManagerCommands.h:64
void CmdBindGameEvent(const Ogre::StringVector &params)
Binds a key combination to a game event.
Definition: InputManagerCommands.h:104
std::vector< int > ButtonList
Definition: InputManager.h:28
Ogre::StringVector StringTokenise(const Ogre::String &str, const Ogre::String &delimiters, const Ogre::String &delimiters_preserve, const Ogre::String &quote, const Ogre::String &esc)
Tokenizes a string.
Definition: Utilites.cpp:277
OIS::KeyCode StringToKey(const Ogre::String &str)
Obtains a key code from a name.
Definition: Utilites.cpp:269
A configuration command.
Definition: ConfigCmd.h:31
void InitCmd()
Initializes all command bindings.
Definition: InputManagerCommands.h:121
#define LOG_ERROR(message)
Prints an error log message.
Definition: Logger.h:28
#define LOG_TRIVIAL(message)
Prints a trivial log message.
Definition: Logger.h:50
Ogre::String String
Definition: TypeDefine.h:37