shortcutApi.cpp 5.3 KB

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