UnitActionPanel.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * UnitActionPanel.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 "UnitActionPanel.h"
  12. #include "BattleInterface.h"
  13. #include "BattleActionsController.h"
  14. #include "../GameEngine.h"
  15. #include "../eventsSDL/InputHandler.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 "../windows/CSpellWindow.h"
  22. #include "../../lib/CConfigHandler.h"
  23. #include "../../lib/GameLibrary.h"
  24. #include "../../lib/battle/CPlayerBattleCallback.h"
  25. #include "../../lib/json/JsonUtils.h"
  26. #include "../../lib/mapObjects/CGHeroInstance.h"
  27. #include "../../lib/spells/CSpellHandler.h"
  28. UnitActionPanel::UnitActionPanel(BattleInterface & owner)
  29. : CIntObject(0)
  30. , owner(owner)
  31. {
  32. OBJECT_CONSTRUCTION;
  33. addUsedEvents(LCLICK | SHOW_POPUP | MOVE);
  34. pos = Rect(0, 0, 52, 600);
  35. background = std::make_shared<CFilledTexture>(ImagePath::builtin("DIBOXBCK"), pos);
  36. rect = std::make_shared<TransparentFilledRectangle>(Rect(0, 0, pos.w + 1, pos.h + 1), ColorRGBA(0, 0, 0, 0), ColorRGBA(241, 216, 120, 255));
  37. }
  38. void UnitActionPanel::restoreAllActions()
  39. {
  40. owner.actionsController->resetCurrentStackPossibleActions();
  41. }
  42. void UnitActionPanel::setActions(int buttonIndex, const std::vector<PossiblePlayerBattleAction> & filteredActions)
  43. {
  44. for (const auto & button : buttons)
  45. if (button != buttons.at(buttonIndex))
  46. button->setSelectedSilent(false);
  47. owner.actionsController->setPriorityActions(filteredActions);
  48. if (filteredActions.front().spellcast())
  49. owner.actionsController->enterCreatureCastingMode();
  50. owner.actionsController->setPriorityActions(filteredActions);
  51. }
  52. void UnitActionPanel::testAndAddAction(const std::vector<PossiblePlayerBattleAction> & allActions, const std::vector<PossiblePlayerBattleAction::Actions> & actionFilter, const ImagePath & iconPath, const std::string & descriptionTextID)
  53. {
  54. std::vector<PossiblePlayerBattleAction> filteredActions;
  55. for (const auto & action : allActions)
  56. if (vstd::contains(actionFilter, action.get()))
  57. filteredActions.push_back(action);
  58. if (filteredActions.empty())
  59. return;
  60. int index = buttons.size();
  61. const auto & callback = [this, filteredActions, index](bool isSelected){ if (isSelected) setActions(index, filteredActions); else restoreAllActions(); };
  62. MetaString tooltip;
  63. tooltip.appendTextID(descriptionTextID);
  64. auto button = std::make_shared<CToggleButton>(Point(2, 7 + 50 * index), AnimationPath::builtin("battleUnitAction"), CButton::tooltip(tooltip.toString()), callback);
  65. button->setOverlay(std::make_shared<CPicture>(iconPath));
  66. button->setHighlightedBorderColor(Colors::WHITE);
  67. button->setAllowDeselection(true);
  68. buttons.push_back(button);
  69. }
  70. void UnitActionPanel::testAndAddSpell(const std::vector<PossiblePlayerBattleAction> & allActions, const SpellID & spellFilter)
  71. {
  72. std::vector<PossiblePlayerBattleAction> filteredActions;
  73. for (const auto & action : allActions)
  74. if (action.spellcast() && action.spell() == spellFilter)
  75. filteredActions.push_back(action);
  76. if (filteredActions.empty())
  77. return;
  78. int index = buttons.size();
  79. const auto & callback = [this, filteredActions, index](bool isSelected){ if (isSelected) setActions(index, filteredActions); else restoreAllActions();};
  80. MetaString tooltip;
  81. tooltip.appendTextID("core.genrltxt.26");
  82. tooltip.replaceName(spellFilter);
  83. std::string hoverText = tooltip.toString();
  84. std::string description = spellFilter.toSpell()->getDescriptionTranslated(0);
  85. auto button = std::make_shared<CToggleButton>(Point(2, 7 + 50 * index), AnimationPath::builtin("battleUnitAction"), CButton::tooltip(hoverText, description), callback);
  86. button->setOverlay(std::make_shared<CAnimImage>(AnimationPath::builtin("spellint"), spellFilter.getNum() + 1));
  87. button->setHighlightedBorderColor(Colors::WHITE);
  88. buttons.push_back(button);
  89. }
  90. void UnitActionPanel::setPossibleActions(const std::vector<PossiblePlayerBattleAction> & newActions)
  91. {
  92. OBJECT_CONSTRUCTION;
  93. buttons.clear();
  94. static const std::vector actionsMove = { PossiblePlayerBattleAction::MOVE_STACK };
  95. static const std::vector actionsInfo = { PossiblePlayerBattleAction::CREATURE_INFO, PossiblePlayerBattleAction::HERO_INFO };
  96. static const std::vector actionsShoot = { PossiblePlayerBattleAction::SHOOT };
  97. static const std::vector actionsGenie = { PossiblePlayerBattleAction::RANDOM_GENIE_SPELL };
  98. static const std::vector actionsAttack = { PossiblePlayerBattleAction::ATTACK, PossiblePlayerBattleAction::WALK_AND_ATTACK };
  99. static const std::vector actionsReturn = { PossiblePlayerBattleAction::ATTACK_AND_RETURN };
  100. testAndAddAction(newActions, actionsMove, ImagePath::builtin("battle/actionMove"), "vcmi.battle.action.move");
  101. testAndAddAction(newActions, actionsReturn, ImagePath::builtin("battle/actionReturn"), "vcmi.battle.action.return");
  102. testAndAddAction(newActions, actionsAttack, ImagePath::builtin("battle/actionAttack"), "vcmi.battle.action.attack");
  103. testAndAddAction(newActions, actionsShoot, ImagePath::builtin("battle/actionShoot"), "vcmi.battle.action.shoot");
  104. testAndAddAction(newActions, actionsGenie, ImagePath::builtin("battle/actionGenie"), "vcmi.battle.action.genie");
  105. std::vector<SpellID> spells;
  106. for (const auto & action : newActions)
  107. if (action.spellcast())
  108. spells.push_back(action.spell());
  109. for (const auto & spell : spells)
  110. testAndAddSpell(newActions, spell);
  111. // Not really a unit action, so place it at the end
  112. testAndAddAction(newActions, actionsInfo, ImagePath::builtin("battle/actionInfo"), "vcmi.battle.action.info");
  113. redraw();
  114. }
  115. void UnitActionPanel::show(Canvas & to)
  116. {
  117. showAll(to);
  118. CIntObject::show(to);
  119. }