ShortcutsWindow.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * ShortcutsWindow.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 "ShortcutsWindow.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. ShortcutsWindow::ShortcutsWindow()
  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.shortcuts.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 : shortcutsConfig.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.shortcuts.reset").toString()));
  51. buttonReset->setOverlay(std::make_shared<CLabel>(0, 0, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, MetaString::createFromTextID("vcmi.shortcuts.reset").toString()));
  52. buttonReset->addCallback([this](){
  53. GAME->interface()->showYesNoDialog(MetaString::createFromTextID("vcmi.shortcuts.resetConfirm").toString(), [this](){
  54. resetKeyBinding();
  55. }, nullptr);
  56. });
  57. fillList(0);
  58. }
  59. void ShortcutsWindow::fillList(int start)
  60. {
  61. OBJECT_CONSTRUCTION;
  62. listElements.clear();
  63. int i = 0;
  64. [&]{
  65. for(auto group = shortcutsConfig.toJsonNode().Struct().rbegin(); group != shortcutsConfig.toJsonNode().Struct().rend(); ++group)
  66. {
  67. if(i >= start)
  68. listElements.push_back(std::make_shared<ShortcutElement>(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<ShortcutElement>(elem.first, elem.second, listElements.size(), [this, group](const std::string & id, const std::string & keyName){
  76. auto str = MetaString::createFromTextID("vcmi.shortcuts.inputSet");
  77. str.replaceTextID("vcmi.shortcuts.shortcut." + 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 ShortcutsWindow::setKeyBinding(const std::string & id, const std::string & group, const std::string & keyName, bool append)
  93. {
  94. auto existing = shortcutsConfig[group][id];
  95. Settings existingWrite = shortcutsConfig.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. tmp.push_back(JsonNode(keyName));
  104. existingWrite->Vector() = tmp;
  105. }
  106. else
  107. existingWrite->String() = keyName;
  108. fillList(slider->getValue());
  109. }
  110. void ShortcutsWindow::resetKeyBinding()
  111. {
  112. // FIXME: Not working yet
  113. Settings write = shortcutsConfig.write;
  114. write->clear();
  115. write->Struct() = JsonUtils::assembleFromFiles("config/shortcutsConfig.json").Struct();
  116. fillList(slider->getValue());
  117. }
  118. ShortcutElement::ShortcutElement(std::string id, JsonNode keys, int elem, std::function<void(const std::string & id, const std::string & keyName)> func)
  119. : func(func)
  120. {
  121. OBJECT_CONSTRUCTION;
  122. pos.x += 14;
  123. pos.y += 56;
  124. pos.y += elem * LINE_HEIGHT;
  125. std::string keyBinding = "";
  126. if(keys.isString())
  127. keyBinding = keys.String();
  128. else if(keys.isVector())
  129. {
  130. std::vector<std::string> strings;
  131. std::transform(keys.Vector().begin(), keys.Vector().end(), std::back_inserter(strings), [](const auto& k) { return k.String(); });
  132. keyBinding = boost::join(strings, " {gray||} ");
  133. }
  134. labelName = std::make_shared<CLabel>(
  135. 0, LINE_HEIGHT / 2, FONT_SMALL, ETextAlignment::CENTERLEFT, Colors::WHITE, MetaString::createFromTextID("vcmi.shortcuts.shortcut." + id).toString(), 245
  136. );
  137. labelKeys = std::make_shared<CLabel>(
  138. 250, LINE_HEIGHT / 2, FONT_SMALL, ETextAlignment::CENTERLEFT, Colors::WHITE, keyBinding, 170
  139. );
  140. buttonEdit = std::make_shared<CButton>(Point(422, 3), AnimationPath::builtin("settingsWindow/button32"), std::make_pair("", MetaString::createFromTextID("vcmi.shortcuts.editButton.help").toString()));
  141. buttonEdit->setOverlay(std::make_shared<CPicture>(ImagePath::builtin("settingsWindow/gear")));
  142. buttonEdit->addCallback([id, func](){
  143. ENGINE->windows().createAndPushWindow<ShortcutsEditWindow>(id, [func](const std::string & id, const std::string & keyName){
  144. if(func)
  145. func(id, keyName);
  146. });
  147. });
  148. if(elem < MAX_LINES - 1)
  149. seperationLine = std::make_shared<TransparentFilledRectangle>(Rect(0, LINE_HEIGHT, 456, 1), ColorRGBA(0, 0, 0, 64), ColorRGBA(128, 100, 75), 1);
  150. }
  151. ShortcutElement::ShortcutElement(std::string group, int elem)
  152. : func(nullptr)
  153. {
  154. OBJECT_CONSTRUCTION;
  155. pos.x += 14;
  156. pos.y += 56;
  157. pos.y += elem * LINE_HEIGHT;
  158. labelName = std::make_shared<CLabel>(
  159. 0, LINE_HEIGHT / 2, FONT_SMALL, ETextAlignment::CENTERLEFT, Colors::YELLOW, MetaString::createFromTextID("vcmi.shortcuts.group." + group).toString(), 300
  160. );
  161. if(elem < MAX_LINES - 1)
  162. seperationLine = std::make_shared<TransparentFilledRectangle>(Rect(0, LINE_HEIGHT, 456, 1), ColorRGBA(0, 0, 0, 64), ColorRGBA(128, 100, 75), 1);
  163. }
  164. ShortcutsEditWindow::ShortcutsEditWindow(const std::string & id, std::function<void(const std::string & id, const std::string & keyName)> func)
  165. : CWindowObject(BORDERED)
  166. , id(id)
  167. , func(func)
  168. {
  169. OBJECT_CONSTRUCTION;
  170. pos.w = 250;
  171. pos.h = 150;
  172. auto str = MetaString::createFromTextID("vcmi.shortcuts.input");
  173. str.replaceTextID("vcmi.shortcuts.shortcut." + id);
  174. backgroundTexture = std::make_shared<CFilledTexture>(ImagePath::builtin("DiBoxBck"), Rect(0, 0, pos.w, pos.h));
  175. text = std::make_shared<CTextBox>(str.toString(), Rect(0, 0, 250, 150), 0, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE);
  176. updateShadow();
  177. center();
  178. addUsedEvents(KEY_NAME);
  179. }
  180. void ShortcutsEditWindow::keyPressed(const std::string & keyName)
  181. {
  182. 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
  183. return;
  184. close();
  185. func(id, keyName);
  186. }