KeyBindingsWindow.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * KeyBindingsWindow.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "KeyBindingsWindow.h"
  12. #include "../../CPlayerInterface.h"
  13. #include "../../GameEngine.h"
  14. #include "../../GameInstance.h"
  15. #include "../../gui/Shortcut.h"
  16. #include "../../gui/ShortcutHandler.h"
  17. #include "../../gui/WindowHandler.h"
  18. #include "../../widgets/Buttons.h"
  19. #include "../../widgets/GraphicalPrimitiveCanvas.h"
  20. #include "../../widgets/Images.h"
  21. #include "../../widgets/TextControls.h"
  22. #include "../../widgets/Slider.h"
  23. #include "../../windows/InfoWindows.h"
  24. #include "../../../lib/CConfigHandler.h"
  25. #include "../../../lib/texts/MetaString.h"
  26. #include "../../../lib/json/JsonNode.h"
  27. #include "../../../lib/json/JsonUtils.h"
  28. KeyBindingsWindow::KeyBindingsWindow()
  29. : CWindowObject(BORDERED)
  30. {
  31. OBJECT_CONSTRUCTION;
  32. pos.w = 500;
  33. pos.h = 450;
  34. updateShadow();
  35. center();
  36. backgroundTexture = std::make_shared<CFilledTexture>(ImagePath::builtin("DiBoxBck"), Rect(0, 0, pos.w, pos.h));
  37. buttonOk = std::make_shared<CButton>(Point(218, 404), AnimationPath::builtin("IOKAY"), CButton::tooltip(), [this](){
  38. close();
  39. ENGINE->shortcuts().reloadShortcuts();
  40. }, EShortcut::GLOBAL_ACCEPT);
  41. labelTitle = std::make_shared<CLabel>(
  42. pos.w / 2, 20, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, MetaString::createFromTextID("vcmi.keyBindings.button.hover").toString()
  43. );
  44. backgroundRect = std::make_shared<TransparentFilledRectangle>(Rect(8, 48, pos.w - 16, 348), ColorRGBA(0, 0, 0, 64), ColorRGBA(128, 100, 75), 1);
  45. int count = 0;
  46. for(auto & group : keyBindingsConfig.toJsonNode().Struct())
  47. {
  48. count++;
  49. count += group.second.Struct().size();
  50. }
  51. slider = std::make_shared<CSlider>(Point(backgroundRect->pos.x - pos.x + backgroundRect->pos.w - 18, backgroundRect->pos.y - pos.y + 1), backgroundRect->pos.h - 3, [this](int pos){ fillList(pos); redraw(); }, MAX_LINES, count, 0, Orientation::VERTICAL, CSlider::BROWN);
  52. slider->setPanningStep(LINE_HEIGHT);
  53. slider->setScrollBounds(Rect(-backgroundRect->pos.w + slider->pos.w, 0, slider->pos.x - pos.x + slider->pos.w, slider->pos.h));
  54. buttonReset = std::make_shared<CButton>(Point(411, 403), AnimationPath::builtin("settingsWindow/button80"), std::make_pair("", MetaString::createFromTextID("vcmi.keyBindings.reset.help").toString()));
  55. buttonReset->setOverlay(std::make_shared<CLabel>(0, 0, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, MetaString::createFromTextID("vcmi.keyBindings.reset").toString()));
  56. buttonReset->addCallback([this](){
  57. GAME->interface()->showYesNoDialog(MetaString::createFromTextID("vcmi.keyBindings.resetConfirm").toString(), [this](){
  58. resetKeyBinding();
  59. }, nullptr);
  60. });
  61. fillList(0);
  62. }
  63. void KeyBindingsWindow::fillList(int start)
  64. {
  65. OBJECT_CONSTRUCTION;
  66. listElements.clear();
  67. int i = 0;
  68. [&]{
  69. for(auto group = keyBindingsConfig.toJsonNode().Struct().rbegin(); group != keyBindingsConfig.toJsonNode().Struct().rend(); ++group)
  70. {
  71. if(i >= start)
  72. listElements.push_back(std::make_shared<KeyBindingElement>(group->first, listElements.size()));
  73. i++;
  74. if(listElements.size() == MAX_LINES)
  75. return;
  76. for(auto & elem : group->second.Struct())
  77. {
  78. if(i >= start)
  79. listElements.push_back(std::make_shared<KeyBindingElement>(elem.first, elem.second, listElements.size(), [this, group](const std::string & id, const std::string & keyName){
  80. auto str = MetaString::createFromTextID("vcmi.keyBindings.inputSet");
  81. str.replaceTextID("vcmi.keyBindings.keyBinding." + id);
  82. str.replaceRawString(keyName);
  83. GAME->interface()->showYesNoDialog(str.toString(), [this, group, id, keyName](){
  84. setKeyBinding(id, group->first, keyName, true);
  85. }, [this, group, id, keyName](){
  86. setKeyBinding(id, group->first, keyName, false);
  87. });
  88. }));
  89. i++;
  90. if(listElements.size() == MAX_LINES)
  91. return;
  92. }
  93. }
  94. }();
  95. }
  96. void KeyBindingsWindow::setKeyBinding(const std::string & id, const std::string & group, const std::string & keyName, bool append)
  97. {
  98. auto existing = keyBindingsConfig[group][id];
  99. Settings existingWrite = keyBindingsConfig.write[group][id];
  100. if((existing.isVector() || (existing.isString() && !existing.String().empty())) && append)
  101. {
  102. JsonVector tmp;
  103. if(existing.isVector())
  104. tmp = existing.Vector();
  105. if(existing.isString())
  106. tmp.push_back(existing);
  107. if(!vstd::contains(tmp, JsonNode(keyName)))
  108. tmp.push_back(JsonNode(keyName));
  109. existingWrite->Vector() = tmp;
  110. }
  111. else
  112. existingWrite->String() = keyName;
  113. fillList(slider->getValue());
  114. }
  115. void KeyBindingsWindow::resetKeyBinding()
  116. {
  117. {
  118. Settings write = keyBindingsConfig.write;
  119. write->clear();
  120. }
  121. {
  122. Settings write = keyBindingsConfig.write;
  123. write->Struct() = JsonUtils::assembleFromFiles("config/keyBindingsConfig.json").Struct();
  124. }
  125. fillList(slider->getValue());
  126. }
  127. KeyBindingElement::KeyBindingElement(std::string id, JsonNode keys, int elem, std::function<void(const std::string & id, const std::string & keyName)> func)
  128. : func(func)
  129. {
  130. OBJECT_CONSTRUCTION;
  131. pos.x += 14;
  132. pos.y += 56;
  133. pos.y += elem * LINE_HEIGHT;
  134. pos.w = 455;
  135. pos.h = LINE_HEIGHT;
  136. addUsedEvents(SHOW_POPUP);
  137. popupText = MetaString::createFromTextID("vcmi.keyBindings.popup");
  138. popupText.replaceTextID("vcmi.keyBindings.keyBinding." + id);
  139. std::string keyBinding = "";
  140. if(keys.isString())
  141. {
  142. keyBinding = keys.String();
  143. popupText.appendRawString(keyBinding);
  144. }
  145. else if(keys.isVector())
  146. {
  147. std::vector<std::string> strings;
  148. std::transform(keys.Vector().begin(), keys.Vector().end(), std::back_inserter(strings), [](const auto& k) { return k.String(); });
  149. keyBinding = boost::join(strings, " | ");
  150. popupText.appendRawString(boost::join(strings, "\n"));
  151. }
  152. labelName = std::make_shared<CLabel>(
  153. 0, LINE_HEIGHT / 2, FONT_SMALL, ETextAlignment::CENTERLEFT, Colors::WHITE, MetaString::createFromTextID("vcmi.keyBindings.keyBinding." + id).toString(), 245
  154. );
  155. labelKeys = std::make_shared<CLabel>(
  156. 250, LINE_HEIGHT / 2, FONT_SMALL, ETextAlignment::CENTERLEFT, Colors::WHITE, keyBinding, 170
  157. );
  158. buttonEdit = std::make_shared<CButton>(Point(422, 3), AnimationPath::builtin("settingsWindow/button32"), std::make_pair("", MetaString::createFromTextID("vcmi.keyBindings.editButton.help").toString()));
  159. buttonEdit->setOverlay(std::make_shared<CPicture>(ImagePath::builtin("settingsWindow/gear")));
  160. buttonEdit->addCallback([id, func](){
  161. ENGINE->windows().createAndPushWindow<KeyBindingsEditWindow>(id, [func](const std::string & id, const std::string & keyName){
  162. if(func)
  163. func(id, keyName);
  164. });
  165. });
  166. if(elem < MAX_LINES - 1)
  167. seperationLine = std::make_shared<TransparentFilledRectangle>(Rect(0, LINE_HEIGHT, 456, 1), ColorRGBA(0, 0, 0, 64), ColorRGBA(128, 100, 75), 1);
  168. }
  169. KeyBindingElement::KeyBindingElement(std::string group, int elem)
  170. : func(nullptr)
  171. {
  172. OBJECT_CONSTRUCTION;
  173. pos.x += 14;
  174. pos.y += 56;
  175. pos.y += elem * LINE_HEIGHT;
  176. labelName = std::make_shared<CLabel>(
  177. 0, LINE_HEIGHT / 2, FONT_SMALL, ETextAlignment::CENTERLEFT, Colors::YELLOW, MetaString::createFromTextID("vcmi.keyBindings.group." + group).toString(), 300
  178. );
  179. if(elem < MAX_LINES - 1)
  180. seperationLine = std::make_shared<TransparentFilledRectangle>(Rect(0, LINE_HEIGHT, 456, 1), ColorRGBA(0, 0, 0, 64), ColorRGBA(128, 100, 75), 1);
  181. }
  182. void KeyBindingElement::showPopupWindow(const Point & cursorPosition)
  183. {
  184. CRClickPopup::createAndPush(popupText.toString());
  185. }
  186. KeyBindingsEditWindow::KeyBindingsEditWindow(const std::string & id, std::function<void(const std::string & id, const std::string & keyName)> func)
  187. : CWindowObject(BORDERED)
  188. , id(id)
  189. , func(func)
  190. {
  191. OBJECT_CONSTRUCTION;
  192. pos.w = 250;
  193. pos.h = 150;
  194. auto str = MetaString::createFromTextID("vcmi.keyBindings.input");
  195. str.replaceTextID("vcmi.keyBindings.keyBinding." + id);
  196. backgroundTexture = std::make_shared<CFilledTexture>(ImagePath::builtin("DiBoxBck"), Rect(0, 0, pos.w, pos.h));
  197. text = std::make_shared<CTextBox>(str.toString(), Rect(0, 0, 250, 150), 0, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE);
  198. updateShadow();
  199. center();
  200. addUsedEvents(LCLICK | KEY_NAME);
  201. }
  202. void KeyBindingsEditWindow::keyReleased(const std::string & keyName)
  203. {
  204. if(boost::algorithm::ends_with(keyName, "Ctrl") || boost::algorithm::ends_with(keyName, "Shift") || boost::algorithm::ends_with(keyName, "Alt")) // skip if only control key pressed
  205. return;
  206. close();
  207. func(id, keyName);
  208. }
  209. void KeyBindingsEditWindow::notFocusedClick()
  210. {
  211. close(); // possibility to close without setting key (e.g. on touch screens)
  212. }