InputManager.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. #include <algorithm>
  16. #include "core/Console.h"
  17. #include "core/InputManager.h"
  18. #include "core/InputManagerCommands.h"
  19. #include "core/Logger.h"
  20. #include "core/Timer.h"
  21. #include "core/Manager.h"
  22. #include "ConfigCmdHandler.h"
  23. /**
  24. * Input manager singleton.
  25. */
  26. template<>InputManager *Ogre::Singleton<InputManager>::msSingleton = nullptr;
  27. InputManager::InputManager():repeat_first_wait_(true), repeat_timer_(0){
  28. InitCmd();
  29. Reset();
  30. Update();
  31. }
  32. InputManager::~InputManager(){}
  33. void InputManager::Input(const VGears::Event& event){}
  34. void InputManager::UpdateDebug(){}
  35. void InputManager::OnResize(){}
  36. void InputManager::ClearField(){}
  37. void InputManager::ClearBattle(){}
  38. void InputManager::ClearWorld(){}
  39. void InputManager::Reset(){
  40. for (int button = 0; button < 256; ++ button) button_state_[button] = false;
  41. }
  42. void InputManager::ButtonPressed(int button, char text, bool down){
  43. // A script asked for the next key (to let the player choose one): report it as an event and do
  44. // nothing else with it.
  45. if (capture_next_key_ == true && down == true){
  46. capture_next_key_ = false;
  47. VGears::Event capture_event;
  48. capture_event.type = VGears::ET_KEY_PRESS;
  49. capture_event.param1 = button;
  50. capture_event.param2 = text;
  51. capture_event.event = "KEY_CAPTURE";
  52. event_queue_.push_back(capture_event);
  53. return;
  54. }
  55. if (button_state_[button] != down){
  56. button_state_[button] = down;
  57. button_text_[button] = text;
  58. VGears::Event event;
  59. event.type = (down == true) ? VGears::ET_KEY_PRESS : VGears::ET_KEY_RELEASE;
  60. event.param1 = button;
  61. event.param2 = text;
  62. event_queue_.push_back(event);
  63. repeat_first_wait_ = true;
  64. repeat_timer_ = 0;
  65. }
  66. if (Console::getSingleton().IsVisible() != true){
  67. if (down == true){
  68. ActivateBinds(button);
  69. AddGameEvents(button, VGears::ET_KEY_PRESS);
  70. }
  71. else AddGameEvents(button, VGears::ET_KEY_RELEASE);
  72. }
  73. }
  74. void InputManager::MousePressed(int button, bool down){
  75. VGears::Event event;
  76. event.type = (down == true) ? VGears::ET_KEY_PRESS : VGears::ET_KEY_RELEASE;
  77. event.param1 = button;
  78. event_queue_.push_back(event);
  79. }
  80. void InputManager::MouseMoved(int x, int y){
  81. VGears::Event event;
  82. event.type = VGears::ET_MOUSE_MOVE;
  83. event.param1 = x;
  84. event.param2 = y;
  85. event_queue_.push_back(event);
  86. }
  87. void InputManager::MouseScrolled(int value){
  88. VGears::Event event;
  89. event.type = VGears::ET_MOUSE_SCROLL;
  90. event.param1 = value;
  91. event_queue_.push_back(event);
  92. }
  93. void InputManager::Update(){
  94. repeat_timer_ += Timer::getSingleton().GetSystemTimeDelta();
  95. if (
  96. (repeat_first_wait_ == true && repeat_timer_ >= 0.5)
  97. || (repeat_first_wait_ == false && repeat_timer_ >= 0.05)
  98. ){
  99. for (int button = 0; button < 256; ++ button){
  100. if (button_state_[button] == true){
  101. VGears::Event event;
  102. event.type = VGears::ET_KEY_REPEAT_WAIT;
  103. event.param1 = button;
  104. event.param2 = button_text_[button];
  105. event_queue_.push_back(event);
  106. if (Console::getSingleton().IsVisible() != true)
  107. AddGameEvents(button, VGears::ET_KEY_REPEAT_WAIT);
  108. }
  109. }
  110. repeat_first_wait_ = false;
  111. repeat_timer_ = 0;
  112. }
  113. for (int button = 0; button < 256; ++ button){
  114. if (button_state_[button] == true){
  115. VGears::Event event;
  116. event.type = VGears::ET_KEY_REPEAT;
  117. event.param1 = button;
  118. event.param2 = button_text_[button];
  119. event_queue_.push_back(event);
  120. if (Console::getSingleton().IsVisible() != true)
  121. AddGameEvents(button, VGears::ET_KEY_REPEAT);
  122. }
  123. }
  124. }
  125. bool InputManager::IsButtonPressed(int button) const{return button_state_[button];}
  126. bool InputManager::IsGameEventPressed(const Ogre::String& event) const{
  127. if (Console::getSingleton().IsVisible() == true) return false;
  128. for (const BindGameEventInfo& bind : bind_game_events_){
  129. if (bind.event != event || bind.buttons.empty()) continue;
  130. bool all_pressed = true;
  131. for (const int button : bind.buttons){
  132. if (IsButtonPressed(button) == false){
  133. all_pressed = false;
  134. break;
  135. }
  136. }
  137. if (all_pressed == true) return true;
  138. }
  139. return false;
  140. }
  141. void InputManager::GetInputEvents(InputEventArray& input_events){
  142. input_events = event_queue_;
  143. event_queue_.clear();
  144. }
  145. void InputManager::BindCommand(
  146. ConfigCmd* cmd, const Ogre::StringVector& params, const ButtonList& buttons
  147. ){
  148. BindInfo info;
  149. info.cmd = cmd;
  150. info.params = params;
  151. info.buttons = buttons;
  152. binds_.push_back(info);
  153. }
  154. void InputManager::BindGameEvent(const Ogre::String& event, const ButtonList& buttons){
  155. BindGameEventInfo info;
  156. info.event = event;
  157. info.buttons = buttons;
  158. bind_game_events_.push_back(info);
  159. }
  160. void InputManager::ScriptSetGameEventKeys(const char* event, const char* keys){
  161. const Ogre::String event_name(event);
  162. bind_game_events_.erase(
  163. std::remove_if(
  164. bind_game_events_.begin(), bind_game_events_.end(),
  165. [&event_name](const BindGameEventInfo& bind){return bind.event == event_name;}
  166. ),
  167. bind_game_events_.end()
  168. );
  169. Ogre::StringVector combinations = Ogre::StringUtil::split(Ogre::String(keys), ",");
  170. for (Ogre::String& combination : combinations){
  171. Ogre::StringUtil::trim(combination);
  172. if (combination.empty()) continue;
  173. ButtonList key_codes;
  174. if (ParseKeys(combination, key_codes) == false) BindGameEvent(event_name, key_codes);
  175. }
  176. }
  177. std::string InputManager::ScriptGetGameEventKeys(const char* event) const{
  178. const Ogre::String event_name(event);
  179. std::string keys;
  180. for (const BindGameEventInfo& bind : bind_game_events_){
  181. if (bind.event != event_name) continue;
  182. if (!keys.empty()) keys += ",";
  183. for (unsigned int i = 0; i < bind.buttons.size(); ++ i){
  184. if (i > 0) keys += "+";
  185. keys += KeyToString(static_cast<OIS::KeyCode>(bind.buttons[i]));
  186. }
  187. }
  188. return keys;
  189. }
  190. void InputManager::ScriptCaptureNextKey(){capture_next_key_ = true;}
  191. void InputManager::SnapshotDefaultGameEvents(){
  192. default_game_event_keys_.clear();
  193. for (const BindGameEventInfo& bind : bind_game_events_){
  194. if (default_game_event_keys_.count(bind.event) == 0){
  195. default_game_event_keys_[bind.event] = ScriptGetGameEventKeys(bind.event.c_str());
  196. }
  197. }
  198. }
  199. std::string InputManager::ScriptGetDefaultGameEventKeys(const char* event) const{
  200. const auto it = default_game_event_keys_.find(Ogre::String(event));
  201. return (it == default_game_event_keys_.end()) ? std::string() : it->second;
  202. }
  203. void InputManager::EnsureDefaultGameEvents(){
  204. const struct{const char* event; OIS::KeyCode key;} defaults[] = {
  205. {"Up", OIS::KC_UP}, {"Down", OIS::KC_DOWN}, {"Left", OIS::KC_LEFT}, {"Right", OIS::KC_RIGHT}
  206. };
  207. for (const auto& entry : defaults){
  208. if (ScriptGetGameEventKeys(entry.event).empty()){
  209. BindGameEvent(entry.event, ButtonList(1, static_cast<int>(entry.key)));
  210. }
  211. }
  212. }
  213. void InputManager::ActivateBinds(const int button) {
  214. std::vector< int > binds_indexes;
  215. for (unsigned int i = 0; i < binds_.size(); ++ i){
  216. // if we found pressed button in this bind
  217. if (
  218. std::find(binds_[i].buttons.begin(), binds_[i].buttons.end(), button)
  219. != binds_[i].buttons.end()
  220. ){
  221. unsigned int j = 0;
  222. for (; j < binds_[i].buttons.size(); ++ j)
  223. if (IsButtonPressed(binds_[i].buttons[j]) == false) break;
  224. if (j >= binds_[i].buttons.size()) binds_indexes.push_back(i);
  225. }
  226. }
  227. std::vector< int > binds_to_activate;
  228. unsigned int max_size = 0;
  229. for (unsigned int i = 0; i < binds_indexes.size(); ++ i){
  230. if (max_size < binds_[binds_indexes[i]].buttons.size()){
  231. max_size = binds_[binds_indexes[i]].buttons.size();
  232. binds_to_activate.clear();
  233. binds_to_activate.push_back(binds_indexes[i]);
  234. }
  235. else if (max_size == binds_[binds_indexes[i]].buttons.size())
  236. binds_to_activate.push_back(binds_indexes[i]);
  237. }
  238. for (unsigned int i = 0; i < binds_to_activate.size(); ++ i){
  239. binds_[binds_to_activate[i]].cmd->GetHandler()(
  240. binds_[binds_to_activate[i]].params
  241. );
  242. }
  243. }
  244. void InputManager::AddGameEvents(const int button, const VGears::EventType type){
  245. std::vector< int > binds_indexes;
  246. for (unsigned int i = 0; i < bind_game_events_.size(); ++ i){
  247. // If the button is found in this bind
  248. if (
  249. std::find(
  250. bind_game_events_[i].buttons.begin(), bind_game_events_[i].buttons.end(), button
  251. ) != bind_game_events_[i].buttons.end()
  252. ){
  253. unsigned int j = 0;
  254. for (; j < bind_game_events_[i].buttons.size(); ++ j){
  255. if (bind_game_events_[i].buttons[j] != button)
  256. if (IsButtonPressed(bind_game_events_[i].buttons[j]) == false) break;
  257. }
  258. if (j >= bind_game_events_[i].buttons.size()) binds_indexes.push_back(i);
  259. }
  260. }
  261. std::vector< int > binds_to_activate;
  262. unsigned int max_size = 0;
  263. for (unsigned int i = 0; i < binds_indexes.size(); ++ i){
  264. if (max_size < bind_game_events_[binds_indexes[i]].buttons.size()){
  265. max_size = bind_game_events_[binds_indexes[i]].buttons.size();
  266. binds_to_activate.clear();
  267. binds_to_activate.push_back(binds_indexes[i]);
  268. }
  269. else if (max_size == bind_game_events_[binds_indexes[i]].buttons.size())
  270. binds_to_activate.push_back(binds_indexes[i]);
  271. }
  272. for (unsigned int i = 0; i < binds_to_activate.size(); ++ i){
  273. VGears::Event event;
  274. event.type = type;
  275. event.param1 = button;
  276. event.event = bind_game_events_[binds_to_activate[i]].event;
  277. event_queue_.push_back(event);
  278. }
  279. }
  280. void InputManager::UpdateField(){Update();}
  281. void InputManager::UpdateBattle(){Update();}
  282. void InputManager::UpdateWorld(){Update();}