shortcutApi.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #include "shortcutApi.h"
  2. #include <stringManipulation/stringManipulation.h>
  3. #include <iostream>
  4. #include <unordered_map>
  5. #include <set>
  6. #include <imgui.h>
  7. #include <imgui.h>
  8. struct Mapping
  9. {
  10. short normal = 0;
  11. short imgui = 0;
  12. };
  13. std::unordered_map<std::string, Mapping> buttonMapping =
  14. {
  15. {"a", {pika::Button::A, ImGuiKey_A}},
  16. { "b", {pika::Button::B, ImGuiKey_B} },
  17. { "c", {pika::Button::C, ImGuiKey_C} },
  18. { "d", {pika::Button::D, ImGuiKey_D} },
  19. { "e", {pika::Button::E, ImGuiKey_E} },
  20. { "f", {pika::Button::F, ImGuiKey_F} },
  21. { "g", {pika::Button::G, ImGuiKey_G} },
  22. { "h", {pika::Button::H, ImGuiKey_H} },
  23. { "i", {pika::Button::I, ImGuiKey_I} },
  24. { "j", {pika::Button::J, ImGuiKey_J} },
  25. { "k", {pika::Button::K, ImGuiKey_K} },
  26. { "l", {pika::Button::L, ImGuiKey_L} },
  27. { "m", {pika::Button::M, ImGuiKey_M} },
  28. { "n", {pika::Button::N, ImGuiKey_N} },
  29. { "o", {pika::Button::O, ImGuiKey_O} },
  30. { "p", {pika::Button::P, ImGuiKey_P} },
  31. { "q", {pika::Button::Q, ImGuiKey_Q} },
  32. { "r", {pika::Button::R, ImGuiKey_R} },
  33. { "s", {pika::Button::S, ImGuiKey_S} },
  34. { "t", {pika::Button::T, ImGuiKey_T} },
  35. { "u", {pika::Button::U, ImGuiKey_U} },
  36. { "v", {pika::Button::V, ImGuiKey_V} },
  37. { "w", {pika::Button::W, ImGuiKey_W} },
  38. { "x", {pika::Button::X, ImGuiKey_X} },
  39. { "y", {pika::Button::Y, ImGuiKey_Y} },
  40. { "z", {pika::Button::Z, ImGuiKey_Z} },
  41. { "0", {pika::Button::NR0, ImGuiKey_0}}, { "1", {pika::Button::NR1, ImGuiKey_1} }, { "2", {pika::Button::NR2, ImGuiKey_2} }, { "3", {pika::Button::NR3, ImGuiKey_3} },
  42. { "4", {pika::Button::NR4, ImGuiKey_0}}, { "5", {pika::Button::NR5, ImGuiKey_5} }, { "6", {pika::Button::NR6, ImGuiKey_6} }, { "7", {pika::Button::NR7, ImGuiKey_7} },
  43. { "8", {pika::Button::NR8, ImGuiKey_8}}, { "9", {pika::Button::NR9, ImGuiKey_9} },
  44. { "space", {pika::Button::Space , ImGuiKey_Space}},
  45. { "enter", {pika::Button::Enter, ImGuiKey_Enter} },
  46. { "escape", {pika::Button::Escape, ImGuiKey_Escape} },
  47. { "up", {pika::Button::Up, ImGuiKey_UpArrow} },
  48. { "down", {pika::Button::Down , ImGuiKey_DownArrow}},
  49. { "left", {pika::Button::Left , ImGuiKey_LeftArrow}},
  50. { "right", {pika::Button::Right , ImGuiKey_RightArrow}},
  51. { "ctrl", {pika::Button::LeftCtrl , ImGuiKey_LeftCtrl}},
  52. { "tab", {pika::Button::Tab , ImGuiKey_Tab}},
  53. { "alt", {pika::Button::LeftAlt , ImGuiKey_LeftAlt}},
  54. };
  55. namespace pika
  56. {
  57. std::vector<std::string> tokenizeShortcutSimple(const char *shortcut)
  58. {
  59. char data[256] = {};
  60. pika::removeCharacters(data, shortcut, "\n \t\r\v", sizeof(data));
  61. pika::toLower(data, data, sizeof(data));
  62. auto token = pika::split(data, '+');
  63. return token;
  64. };
  65. std::vector<std::string> tokenizeShortcutNormalized(const char *shortcut)
  66. {
  67. auto token = tokenizeShortcutSimple(shortcut);
  68. std::set<std::string> tokenSet;
  69. for (auto &i : token)
  70. {
  71. auto it = buttonMapping.find(i);
  72. if (it != buttonMapping.end())
  73. {
  74. tokenSet.insert(it->first);
  75. }
  76. }
  77. std::vector<std::string> ret;
  78. ret.reserve(tokenSet.size());
  79. for (auto &i : tokenSet)
  80. {
  81. ret.push_back(std::move(i));
  82. }
  83. return ret;
  84. }
  85. std::string normalizeShortcutName(const char *shortcut)
  86. {
  87. auto t = tokenizeShortcutNormalized(shortcut);
  88. std::string ret = "";
  89. for (int i = 0; i < t.size(); i++)
  90. {
  91. t[i][0] = std::toupper(t[i][0]);
  92. ret += t[i];
  93. if (i < t.size()-1)
  94. {
  95. ret += "+";
  96. }
  97. }
  98. return ret;
  99. }
  100. //todo shortcut should rely on glfw backend when imgui is disabeled in production build
  101. bool shortcut(const pika::Input &input, const char *shortcut)
  102. {
  103. auto token = tokenizeShortcutSimple(shortcut);
  104. if (token.empty()) { return 0; }
  105. bool pressed = false;
  106. if (0)
  107. { //noraml implementation
  108. for (auto &t : token)
  109. {
  110. auto it = buttonMapping.find(t);
  111. if (it != buttonMapping.end())
  112. {
  113. if (input.buttons[it->second.normal].pressed())
  114. {
  115. pressed = true;
  116. }
  117. else if (!input.buttons[it->second.normal].held())
  118. {
  119. return false;
  120. }
  121. }
  122. }
  123. }
  124. else
  125. { //imgui backend
  126. for (auto &t : token)
  127. {
  128. auto it = buttonMapping.find(t);
  129. if (it != buttonMapping.end())
  130. {
  131. if (ImGui::IsKeyPressed(it->second.imgui, false))
  132. {
  133. pressed = true;
  134. }
  135. else if (!ImGui::IsKeyDown(it->second.imgui))
  136. {
  137. return false;
  138. }
  139. }
  140. }
  141. }
  142. return pressed;
  143. }
  144. bool MenuItem(const pika::Input &input, const char *label, const char *shortcut, bool *p_selected, bool enabled)
  145. {
  146. if (pika::shortcut(input, shortcut))
  147. {
  148. *p_selected = !*p_selected;
  149. }
  150. return ImGui::MenuItem(label, shortcut, p_selected, enabled);
  151. }
  152. void ShortcutManager::update(const pika::Input &input)
  153. {
  154. for (auto &i : registeredShortcuts)
  155. {
  156. if (shortcut(input, i.second.shortcut.c_str()))
  157. {
  158. *i.second.toggle = !*i.second.toggle;
  159. }
  160. }
  161. }
  162. void ShortcutManager::registerShortcut(const char *name, const char *s, bool *toggle)
  163. {
  164. if (registeredShortcuts.find(name) != registeredShortcuts.end())
  165. {
  166. //todo log error
  167. }
  168. else
  169. {
  170. registeredShortcuts[name]
  171. = Shortcut{std::move(normalizeShortcutName(s)), toggle};
  172. }
  173. }
  174. const char *ShortcutManager::getShortcut(const char *name)
  175. {
  176. auto it = registeredShortcuts.find(name);
  177. if (it == registeredShortcuts.end()) { return ""; }
  178. else
  179. { return it->second.shortcut.c_str(); };
  180. }
  181. }