shortcutApi.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. { "esc", {pika::Button::Escape, ImGuiKey_Escape} },
  52. { "up", {pika::Button::Up, ImGuiKey_UpArrow} },
  53. { "down", {pika::Button::Down , ImGuiKey_DownArrow}},
  54. { "left", {pika::Button::Left , ImGuiKey_LeftArrow}},
  55. { "right", {pika::Button::Right , ImGuiKey_RightArrow}},
  56. { "ctrl", {pika::Button::LeftCtrl , ImGuiKey_LeftCtrl}},
  57. { "tab", {pika::Button::Tab , ImGuiKey_Tab}},
  58. { "alt", {pika::Button::LeftAlt , ImGuiKey_LeftAlt}},
  59. };
  60. }
  61. namespace pika
  62. {
  63. std::vector<std::string> tokenizeShortcutSimple(const char *shortcut)
  64. {
  65. char data[256] = {};
  66. pika::removeCharacters(data, shortcut, "\n \t\r\v", sizeof(data));
  67. pika::toLower(data, data, sizeof(data));
  68. auto token = pika::split(data, '+');
  69. return token;
  70. };
  71. std::vector<std::string> tokenizeShortcutNormalized(const char *shortcut)
  72. {
  73. auto token = tokenizeShortcutSimple(shortcut);
  74. std::set<std::string> tokenSet;
  75. for (auto &i : token)
  76. {
  77. auto it = buttonMapping.find(i);
  78. if (it != buttonMapping.end())
  79. {
  80. tokenSet.insert(it->first);
  81. }
  82. }
  83. std::vector<std::string> ret;
  84. ret.reserve(tokenSet.size());
  85. for (auto &i : tokenSet)
  86. {
  87. ret.push_back(std::move(i));
  88. }
  89. return ret;
  90. }
  91. std::string normalizeShortcutName(const char *shortcut)
  92. {
  93. auto t = tokenizeShortcutNormalized(shortcut);
  94. std::string ret = "";
  95. for (int i = 0; i < t.size(); i++)
  96. {
  97. t[i][0] = std::toupper(t[i][0]);
  98. ret += t[i];
  99. if (i < t.size()-1)
  100. {
  101. ret += "+";
  102. }
  103. }
  104. return ret;
  105. }
  106. //todo shortcut should rely on glfw backend when imgui is disabeled in production build
  107. bool shortcut(const pika::Input &input, const char *shortcut)
  108. {
  109. auto token = tokenizeShortcutSimple(shortcut);
  110. if (token.empty()) { return 0; }
  111. bool pressed = false;
  112. if (0)
  113. { //noraml implementation
  114. for (auto &t : token)
  115. {
  116. auto it = buttonMapping.find(t);
  117. if (it != buttonMapping.end())
  118. {
  119. if (input.buttons[it->second.normal].pressed())
  120. {
  121. pressed = true;
  122. }
  123. else if (!input.buttons[it->second.normal].held())
  124. {
  125. return false;
  126. }
  127. }
  128. }
  129. }
  130. else
  131. { //imgui backend
  132. for (auto &t : token)
  133. {
  134. auto it = buttonMapping.find(t);
  135. if (it != buttonMapping.end())
  136. {
  137. if (ImGui::IsKeyPressed(it->second.imgui, false))
  138. {
  139. pressed = true;
  140. }
  141. else if (!ImGui::IsKeyDown(it->second.imgui))
  142. {
  143. return false;
  144. }
  145. }
  146. }
  147. }
  148. return pressed;
  149. }
  150. bool MenuItem(const pika::Input &input, const char *label, const char *shortcut, bool *p_selected, bool enabled)
  151. {
  152. if (pika::shortcut(input, shortcut))
  153. {
  154. *p_selected = !*p_selected;
  155. }
  156. return ImGui::MenuItem(label, shortcut, p_selected, enabled);
  157. }
  158. void ShortcutManager::update(const pika::Input &input)
  159. {
  160. for (auto &i : registeredShortcuts)
  161. {
  162. if (shortcut(input, i.second.shortcut.c_str()))
  163. {
  164. *i.second.toggle = !*i.second.toggle;
  165. }
  166. }
  167. }
  168. bool ShortcutManager::registerShortcut(const char *name, const char *s, bool *toggle, bool editable)
  169. {
  170. if (registeredShortcuts.find(name) != registeredShortcuts.end())
  171. {
  172. //todo log error
  173. return 0;
  174. }
  175. else
  176. {
  177. registeredShortcuts[name]
  178. = Shortcut{std::move(normalizeShortcutName(s)), toggle, editable};
  179. return 1;
  180. }
  181. }
  182. const char *ShortcutManager::getShortcut(const char *name)
  183. {
  184. auto it = registeredShortcuts.find(name);
  185. if (it == registeredShortcuts.end()) { return ""; }
  186. else
  187. { return it->second.shortcut.c_str(); };
  188. }
  189. }