ShortcutsWindow.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 "../../GameEngine.h"
  13. #include "../../gui/Shortcut.h"
  14. #include "../../gui/WindowHandler.h"
  15. #include "../../widgets/Buttons.h"
  16. #include "../../widgets/GraphicalPrimitiveCanvas.h"
  17. #include "../../widgets/Images.h"
  18. #include "../../widgets/TextControls.h"
  19. #include "../../widgets/Slider.h"
  20. #include "../../windows/InfoWindows.h"
  21. #include "../../../lib/texts/MetaString.h"
  22. #include "../../../lib/json/JsonNode.h"
  23. #include "../../../lib/json/JsonUtils.h"
  24. ShortcutsWindow::ShortcutsWindow()
  25. : CWindowObject(BORDERED)
  26. {
  27. OBJECT_CONSTRUCTION;
  28. pos.w = 500;
  29. pos.h = 450;
  30. updateShadow();
  31. center();
  32. backgroundTexture = std::make_shared<CFilledTexture>(ImagePath::builtin("DiBoxBck"), Rect(0, 0, pos.w, pos.h));
  33. buttonOk = std::make_shared<CButton>(Point(218, 404), AnimationPath::builtin("IOKAY"), CButton::tooltip(), [this](){ close(); }, EShortcut::GLOBAL_ACCEPT);
  34. labelTitle = std::make_shared<CLabel>(
  35. pos.w / 2, 20, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, MetaString::createFromTextID("vcmi.shortcuts.button.hover").toString()
  36. );
  37. backgroundRect = std::make_shared<TransparentFilledRectangle>(Rect(8, 48, pos.w - 16, 348), ColorRGBA(0, 0, 0, 64), ColorRGBA(128, 100, 75), 1);
  38. shortcuts = JsonUtils::assembleFromFiles("config/shortcutsConfig");
  39. int count = 0;
  40. for(auto & group : shortcuts.Struct())
  41. {
  42. count++;
  43. count += group.second.Struct().size();
  44. }
  45. 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);
  46. slider->setPanningStep(LINE_HEIGHT);
  47. slider->setScrollBounds(Rect(-backgroundRect->pos.w + slider->pos.w, 0, slider->pos.x - pos.x + slider->pos.w, slider->pos.h));
  48. fillList(0);
  49. }
  50. void ShortcutsWindow::fillList(int start)
  51. {
  52. OBJECT_CONSTRUCTION;
  53. listElements.clear();
  54. int i = 0;
  55. [&]{
  56. for(auto group = shortcuts.Struct().rbegin(); group != shortcuts.Struct().rend(); ++group)
  57. {
  58. if(i >= start)
  59. listElements.push_back(std::make_shared<ShortcutElement>(group->first, listElements.size()));
  60. i++;
  61. if(listElements.size() == MAX_LINES)
  62. return;
  63. for(auto & elem : group->second.Struct())
  64. {
  65. if(i >= start)
  66. listElements.push_back(std::make_shared<ShortcutElement>(elem.first, elem.second, listElements.size()));
  67. i++;
  68. if(listElements.size() == MAX_LINES)
  69. return;
  70. }
  71. }
  72. }();
  73. }
  74. ShortcutElement::ShortcutElement(std::string id, JsonNode keys, int elem)
  75. {
  76. OBJECT_CONSTRUCTION;
  77. pos.x += 14;
  78. pos.y += 56;
  79. pos.y += elem * LINE_HEIGHT;
  80. std::string keyBinding = "";
  81. if(keys.isString())
  82. keyBinding = keys.String();
  83. else if(keys.isVector())
  84. {
  85. std::vector<std::string> strings;
  86. std::transform(keys.Vector().begin(), keys.Vector().end(), std::back_inserter(strings), [](const auto& k) { return k.String(); });
  87. keyBinding = boost::join(strings, " | ");
  88. }
  89. labelName = std::make_shared<CLabel>(
  90. 0, LINE_HEIGHT / 2, FONT_SMALL, ETextAlignment::CENTERLEFT, Colors::WHITE, MetaString::createFromTextID("vcmi.shortcuts.shortcut." + id).toString(), 245
  91. );
  92. labelKeys = std::make_shared<CLabel>(
  93. 250, LINE_HEIGHT / 2, FONT_SMALL, ETextAlignment::CENTERLEFT, Colors::WHITE, keyBinding, 170
  94. );
  95. buttonEdit = std::make_shared<CButton>(Point(422, 3), AnimationPath::builtin("settingsWindow/button32"), std::make_pair("", MetaString::createFromTextID("vcmi.shortcuts.editButton.help").toString()));
  96. buttonEdit->setOverlay(std::make_shared<CPicture>(ImagePath::builtin("settingsWindow/gear")));
  97. buttonEdit->addCallback([id](){
  98. ENGINE->windows().createAndPushWindow<ShortcutsEditWindow>(id);
  99. });
  100. if(elem < MAX_LINES - 1)
  101. seperationLine = std::make_shared<TransparentFilledRectangle>(Rect(0, LINE_HEIGHT, 456, 1), ColorRGBA(0, 0, 0, 64), ColorRGBA(128, 100, 75), 1);
  102. }
  103. ShortcutElement::ShortcutElement(std::string group, int elem)
  104. {
  105. OBJECT_CONSTRUCTION;
  106. pos.x += 14;
  107. pos.y += 56;
  108. pos.y += elem * LINE_HEIGHT;
  109. labelName = std::make_shared<CLabel>(
  110. 0, LINE_HEIGHT / 2, FONT_SMALL, ETextAlignment::CENTERLEFT, Colors::YELLOW, MetaString::createFromTextID("vcmi.shortcuts.group." + group).toString(), 300
  111. );
  112. if(elem < MAX_LINES - 1)
  113. seperationLine = std::make_shared<TransparentFilledRectangle>(Rect(0, LINE_HEIGHT, 456, 1), ColorRGBA(0, 0, 0, 64), ColorRGBA(128, 100, 75), 1);
  114. }
  115. ShortcutsEditWindow::ShortcutsEditWindow(const std::string & id)
  116. : CWindowObject(BORDERED)
  117. {
  118. OBJECT_CONSTRUCTION;
  119. pos.w = 200;
  120. pos.h = 100;
  121. updateShadow();
  122. center();
  123. addUsedEvents(KEY_NAME);
  124. }
  125. void ShortcutsEditWindow::keyPressed(const std::string & keyName)
  126. {
  127. std::cout << keyName << "\n";
  128. }