Explorar o código

Add keyboard shortcuts for quick army management from HD mod.

Just want add fast some really nice shortcuts from HD mod.
1) [LShift] + LClick – splits a half units from the selected stack into
an empty slot. WARNING: The second behaviour ("smart split, ignores
single creatures") of this HD mod option is not done.
2) [LCtrl] + LClick – splits a single unit from the selected stack into
an empty slot.
3) [LCtrl] + [LShift] + LClick – split single units from the selected
stack into all empty hero/garrison slots
FeniksFire %!s(int64=8) %!d(string=hai) anos
pai
achega
86021f0bda
Modificáronse 2 ficheiros con 48 adicións e 1 borrados
  1. 43 0
      client/widgets/CGarrisonInt.cpp
  2. 5 1
      client/widgets/CGarrisonInt.h

+ 43 - 0
client/widgets/CGarrisonInt.cpp

@@ -281,7 +281,10 @@ void CGarrisonSlot::clickLeft(tribool down, bool previousState)
 		bool refr = false;
 		const CGarrisonSlot * selection = owner->getSelection();
 		if(!selection)
+		{
 			refr = highlightOrDropArtifact();
+			handleSplittingShortcuts();
+		}
 		else if(selection == this)
 			refr = viewInfo();
 		// Re-highlight if troops aren't removable or not ours.
@@ -396,6 +399,35 @@ CGarrisonSlot::CGarrisonSlot(CGarrisonInt *Owner, int x, int y, SlotID IID, CGar
 		stackCount->setText(boost::lexical_cast<std::string>(myStack->count));
 }
 
+void CGarrisonSlot::splitIntoParts(CGarrisonSlot::EGarrisonType type, int amount, int maxOfSplittedSlots)
+{
+	owner->pb = type;
+	for(CGarrisonSlot * slot : owner->getEmptySlots(type))
+	{
+		owner->p2 = slot->ID;
+		owner->splitStacks(1, amount);
+		maxOfSplittedSlots--;
+		if(!maxOfSplittedSlots || owner->getSelection()->myStack->count <= 1)
+			break;
+	}
+}
+
+void CGarrisonSlot::handleSplittingShortcuts()
+{
+	const Uint8 * state = SDL_GetKeyboardState(NULL);
+	if(owner->getSelection() && owner->getEmptySlots(owner->getSelection()->upg).size() && owner->getSelection()->myStack->count > 1){
+		if (state[SDL_SCANCODE_LCTRL] && state[SDL_SCANCODE_LSHIFT])
+			splitIntoParts(owner->getSelection()->upg, 1, 7);
+		else if(state[SDL_SCANCODE_LCTRL])
+			splitIntoParts(owner->getSelection()->upg, 1, 1);
+		else if(state[SDL_SCANCODE_LSHIFT])
+			splitIntoParts(owner->getSelection()->upg, owner->getSelection()->myStack->count/2 , 1);
+		else
+			return;
+		owner->selectSlot(nullptr);
+	}
+}
+
 void CGarrisonInt::addSplitBtn(CButton * button)
 {
 	addChild(button);
@@ -519,6 +551,17 @@ bool CGarrisonInt::getSplittingMode()
 	return inSplittingMode;
 }
 
+std::vector<CGarrisonSlot *> CGarrisonInt::getEmptySlots(CGarrisonSlot::EGarrisonType type)
+{
+	std::vector<CGarrisonSlot *> emptySlots;
+	for(CGarrisonSlot * slot : availableSlots)
+	{
+		if(type == slot->upg && ((slot->our() || slot->ally()) && slot->creature == nullptr))
+			emptySlots.push_back(slot);
+	}
+	return emptySlots;
+}
+
 void CGarrisonInt::setArmy(const CArmedInstance *army, bool bottomGarrison)
 {
 	owned[bottomGarrison] =  army ? (army->tempOwner == LOCPLINT->playerID || army->tempOwner == PlayerColor::UNFLAGGABLE) : false;

+ 5 - 1
client/widgets/CGarrisonInt.h

@@ -55,6 +55,9 @@ public:
 	void update();
 	CGarrisonSlot(CGarrisonInt *Owner, int x, int y, SlotID IID, EGarrisonType Upg=EGarrisonType::UP, const CStackInstance * Creature=nullptr);
 
+	void splitIntoParts(EGarrisonType type, int amount, int maxOfSplittedSlots);
+	void handleSplittingShortcuts();
+
 	friend class CGarrisonInt;
 };
 
@@ -83,7 +86,8 @@ public:
 		 twoRows,         ///< slots Will be placed in 2 rows
 		 owned[2];        ///< player Owns up or down army ([0] upper, [1] lower)
 
-	std::vector<CGarrisonSlot*> availableSlots;  ///< Slots of upper and lower garrison
+	std::vector<CGarrisonSlot *> availableSlots;  ///< Slots of upper and lower garrison
+	std::vector<CGarrisonSlot *> getEmptySlots(CGarrisonSlot::EGarrisonType type);
 
 	const CArmedInstance *armedObjs[2];  ///< [0] is upper, [1] is down