ShortcutsWindow.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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/texts/MetaString.h"
  24. #include "../../../lib/json/JsonNode.h"
  25. #include "../../../lib/json/JsonUtils.h"
  26. ShortcutsWindow::ShortcutsWindow()
  27. : CWindowObject(BORDERED)
  28. {
  29. OBJECT_CONSTRUCTION;
  30. pos.w = 500;
  31. pos.h = 450;
  32. updateShadow();
  33. center();
  34. backgroundTexture = std::make_shared<CFilledTexture>(ImagePath::builtin("DiBoxBck"), Rect(0, 0, pos.w, pos.h));
  35. buttonOk = std::make_shared<CButton>(Point(218, 404), AnimationPath::builtin("IOKAY"), CButton::tooltip(), [this](){ close(); }, EShortcut::GLOBAL_ACCEPT);
  36. labelTitle = std::make_shared<CLabel>(
  37. pos.w / 2, 20, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, MetaString::createFromTextID("vcmi.shortcuts.button.hover").toString()
  38. );
  39. backgroundRect = std::make_shared<TransparentFilledRectangle>(Rect(8, 48, pos.w - 16, 348), ColorRGBA(0, 0, 0, 64), ColorRGBA(128, 100, 75), 1);
  40. shortcuts = JsonUtils::assembleFromFiles("config/shortcutsConfig");
  41. int count = 0;
  42. for(auto & group : shortcuts.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. fillList(0);
  51. }
  52. void ShortcutsWindow::fillList(int start)
  53. {
  54. OBJECT_CONSTRUCTION;
  55. listElements.clear();
  56. int i = 0;
  57. [&]{
  58. for(auto group = shortcuts.Struct().rbegin(); group != shortcuts.Struct().rend(); ++group)
  59. {
  60. if(i >= start)
  61. listElements.push_back(std::make_shared<ShortcutElement>(group->first, listElements.size()));
  62. i++;
  63. if(listElements.size() == MAX_LINES)
  64. return;
  65. for(auto & elem : group->second.Struct())
  66. {
  67. if(i >= start)
  68. listElements.push_back(std::make_shared<ShortcutElement>(elem.first, elem.second, listElements.size(), [this](const std::string & id, const std::string & keyName){
  69. auto str = MetaString::createFromTextID("vcmi.shortcuts.inputSet");
  70. str.replaceTextID("vcmi.shortcuts.shortcut." + id);
  71. str.replaceRawString(keyName);
  72. GAME->interface()->showYesNoDialog(str.toString(), [this, id, keyName](){
  73. setKeyBinding(id, keyName, true);
  74. }, [this, id, keyName](){
  75. setKeyBinding(id, keyName, false);
  76. });
  77. }));
  78. i++;
  79. if(listElements.size() == MAX_LINES)
  80. return;
  81. }
  82. }
  83. }();
  84. }
  85. void ShortcutsWindow::setKeyBinding(const std::string & id, const std::string & keyName, bool append)
  86. {
  87. std::cout << id << " " << keyName << " " << append << "\n";
  88. }
  89. ShortcutElement::ShortcutElement(std::string id, JsonNode keys, int elem, std::function<void(const std::string & id, const std::string & keyName)> func)
  90. : func(func)
  91. {
  92. OBJECT_CONSTRUCTION;
  93. pos.x += 14;
  94. pos.y += 56;
  95. pos.y += elem * LINE_HEIGHT;
  96. std::string keyBinding = "";
  97. if(keys.isString())
  98. keyBinding = keys.String();
  99. else if(keys.isVector())
  100. {
  101. std::vector<std::string> strings;
  102. std::transform(keys.Vector().begin(), keys.Vector().end(), std::back_inserter(strings), [](const auto& k) { return k.String(); });
  103. keyBinding = boost::join(strings, " {gray||} ");
  104. }
  105. labelName = std::make_shared<CLabel>(
  106. 0, LINE_HEIGHT / 2, FONT_SMALL, ETextAlignment::CENTERLEFT, Colors::WHITE, MetaString::createFromTextID("vcmi.shortcuts.shortcut." + id).toString(), 245
  107. );
  108. labelKeys = std::make_shared<CLabel>(
  109. 250, LINE_HEIGHT / 2, FONT_SMALL, ETextAlignment::CENTERLEFT, Colors::WHITE, keyBinding, 170
  110. );
  111. buttonEdit = std::make_shared<CButton>(Point(422, 3), AnimationPath::builtin("settingsWindow/button32"), std::make_pair("", MetaString::createFromTextID("vcmi.shortcuts.editButton.help").toString()));
  112. buttonEdit->setOverlay(std::make_shared<CPicture>(ImagePath::builtin("settingsWindow/gear")));
  113. buttonEdit->addCallback([id, func](){
  114. ENGINE->windows().createAndPushWindow<ShortcutsEditWindow>(id, [func](const std::string & id, const std::string & keyName){
  115. if(func)
  116. func(id, keyName);
  117. });
  118. });
  119. if(elem < MAX_LINES - 1)
  120. seperationLine = std::make_shared<TransparentFilledRectangle>(Rect(0, LINE_HEIGHT, 456, 1), ColorRGBA(0, 0, 0, 64), ColorRGBA(128, 100, 75), 1);
  121. }
  122. ShortcutElement::ShortcutElement(std::string group, int elem)
  123. : func(nullptr)
  124. {
  125. OBJECT_CONSTRUCTION;
  126. pos.x += 14;
  127. pos.y += 56;
  128. pos.y += elem * LINE_HEIGHT;
  129. labelName = std::make_shared<CLabel>(
  130. 0, LINE_HEIGHT / 2, FONT_SMALL, ETextAlignment::CENTERLEFT, Colors::YELLOW, MetaString::createFromTextID("vcmi.shortcuts.group." + group).toString(), 300
  131. );
  132. if(elem < MAX_LINES - 1)
  133. seperationLine = std::make_shared<TransparentFilledRectangle>(Rect(0, LINE_HEIGHT, 456, 1), ColorRGBA(0, 0, 0, 64), ColorRGBA(128, 100, 75), 1);
  134. }
  135. ShortcutsEditWindow::ShortcutsEditWindow(const std::string & id, std::function<void(const std::string & id, const std::string & keyName)> func)
  136. : CWindowObject(BORDERED)
  137. , id(id)
  138. , func(func)
  139. {
  140. OBJECT_CONSTRUCTION;
  141. pos.w = 250;
  142. pos.h = 150;
  143. auto str = MetaString::createFromTextID("vcmi.shortcuts.input");
  144. str.replaceTextID("vcmi.shortcuts.shortcut." + id);
  145. backgroundTexture = std::make_shared<CFilledTexture>(ImagePath::builtin("DiBoxBck"), Rect(0, 0, pos.w, pos.h));
  146. text = std::make_shared<CTextBox>(str.toString(), Rect(0, 0, 250, 150), 0, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE);
  147. updateShadow();
  148. center();
  149. addUsedEvents(KEY_NAME);
  150. }
  151. void ShortcutsEditWindow::keyPressed(const std::string & keyName)
  152. {
  153. 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
  154. return;
  155. close();
  156. func(id, keyName);
  157. }