KeyBindingsWindow.cpp 8.4 KB

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