1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-03-19 21:10:12 +02:00

Garrison: Refactor Left Click

Restructure large nested if statements into more shallow blocks
Factor out contents of if statements to functions for better readability
    * viewInfo
    * hightlightOrDropArtifact
    * split
This commit is contained in:
Sandy Carter 2015-08-24 16:39:56 -04:00
parent 3d327e650a
commit b008d24c68
2 changed files with 129 additions and 124 deletions

View File

@ -144,15 +144,8 @@ void CGarrisonSlot::clickRight(tribool down, bool previousState)
GH.pushInt(new CStackWindow(myStack, true)); GH.pushInt(new CStackWindow(myStack, true));
} }
} }
void CGarrisonSlot::clickLeft(tribool down, bool previousState)
{ bool CGarrisonSlot::viewInfo()
if(down)
{
bool refr = false;
const CGarrisonSlot * selection = owner->getSelection();
if(selection)
{
if(selection == this) //view info
{ {
UpgradeInfo pom; UpgradeInfo pom;
LOCPLINT->cb->getUpgradeInfo(getObj(), ID, pom); LOCPLINT->cb->getUpgradeInfo(getObj(), ID, pom);
@ -171,76 +164,11 @@ void CGarrisonSlot::clickLeft(tribool down, bool previousState)
elem->block(true); elem->block(true);
redraw(); redraw();
refr = true;
GH.pushInt(new CStackWindow(myStack, dism, pom, upgr)); GH.pushInt(new CStackWindow(myStack, dism, pom, upgr));
} return true;
else
{
// Only allow certain moves if troops aren't removable or not ours.
if ( ( selection->our()//our creature is selected
|| selection->creature == creature )//or we are rebalancing army
&& ( owner->removableUnits
|| (upg == 0 && ( selection->upg == 1 && !creature ) )
|| (upg == 1 && selection->upg == 1 ) ) )
{
//we want to split
if((owner->getSplittingMode() || LOCPLINT->shiftPressed())
&& (!creature
|| (creature == selection->creature)))
{
owner->p2 = ID; //store the second stack pos
owner->pb = upg;//store the second stack owner (up or down army)
owner->setSplittingMode(false);
int minLeft=0, minRight=0;
if(upg != selection->upg) //not splitting within same army
{
if(selection->getObj()->stacksCount() == 1 //we're splitting away the last stack
&& selection->getObj()->needsLastStack() )
{
minLeft = 1;
}
if(getObj()->stacksCount() == 1 //destination army can't be emptied, unless we're rebalancing two stacks of same creature
&& selection->creature == creature
&& getObj()->needsLastStack() )
{
minRight = 1;
}
} }
int countLeft = selection->myStack ? selection->myStack->count : 0; bool CGarrisonSlot::highlightOrDropArtifact()
int countRight = myStack ? myStack->count : 0;
GH.pushInt(new CSplitWindow(selection->creature, std::bind(&CGarrisonInt::splitStacks, owner, _1, _2),
minLeft, minRight, countLeft, countRight));
refr = true;
}
else if(creature != selection->creature) //swap
{
LOCPLINT->cb->swapCreatures(
(!upg)?(owner->armedObjs[0]):(owner->armedObjs[1]),
(!selection->upg)?(owner->armedObjs[0]):(owner->armedObjs[1]),
ID,selection->ID);
}
else //merge
{
LOCPLINT->cb->mergeStacks(
(!selection->upg)?(owner->armedObjs[0]):(owner->armedObjs[1]),
(!upg)?(owner->armedObjs[0]):(owner->armedObjs[1]),
selection->ID,ID);
}
}
else // Highlight
{
if(creature)
owner->selectSlot(this);
redraw();
refr = true;
}
}
}
else //highlight or drop artifact
{ {
bool artSelected = false; bool artSelected = false;
if (CWindowWithArtifacts* chw = dynamic_cast<CWindowWithArtifacts*>(GH.topInt())) //dirty solution if (CWindowWithArtifacts* chw = dynamic_cast<CWindowWithArtifacts*>(GH.topInt())) //dirty solution
@ -277,8 +205,81 @@ void CGarrisonSlot::clickLeft(tribool down, bool previousState)
} }
} }
redraw(); redraw();
return true;
}
bool CGarrisonSlot::split()
{
const CGarrisonSlot * selection = owner->getSelection();
owner->p2 = ID; // store the second stack pos
owner->pb = upg; // store the second stack owner (up or down army)
owner->setSplittingMode(false);
int minLeft=0, minRight=0;
if(upg != selection->upg) // not splitting within same army
{
if(selection->getObj()->stacksCount() == 1 // we're splitting away the last stack
&& selection->getObj()->needsLastStack() )
{
minLeft = 1;
}
// destination army can't be emptied, unless we're rebalancing two stacks of same creature
if(getObj()->stacksCount() == 1
&& selection->creature == creature
&& getObj()->needsLastStack() )
{
minRight = 1;
}
}
int countLeft = selection->myStack ? selection->myStack->count : 0;
int countRight = myStack ? myStack->count : 0;
GH.pushInt(new CSplitWindow(selection->creature, std::bind(&CGarrisonInt::splitStacks, owner, _1, _2),
minLeft, minRight, countLeft, countRight));
return true;
}
void CGarrisonSlot::clickLeft(tribool down, bool previousState)
{
if(down)
{
bool refr = false;
const CGarrisonSlot * selection = owner->getSelection();
if(!selection)
refr = highlightOrDropArtifact();
else if(selection == this)
refr = viewInfo();
// Only allow certain moves if troops aren't removable or not ours.
else if (!( ( selection->our()//our creature is selected
|| selection->creature == creature )//or we are rebalancing army
&& ( owner->removableUnits
|| (upg == 0 && ( selection->upg == 1 && !creature ) )
|| (upg == 1 && selection->upg == 1 ) ) ))
{
// Highlight
if(creature)
owner->selectSlot(this);
redraw();
refr = true; refr = true;
} }
// we want to split
else if( (owner->getSplittingMode() || LOCPLINT->shiftPressed())
&& (!creature || creature == selection->creature) )
refr = split();
// swap
else if(creature != selection->creature)
LOCPLINT->cb->swapCreatures(
(!upg)?(owner->armedObjs[0]):(owner->armedObjs[1]),
(!selection->upg)?(owner->armedObjs[0]):(owner->armedObjs[1]),
ID,selection->ID);
// merge
else
LOCPLINT->cb->mergeStacks(
(!selection->upg)?(owner->armedObjs[0]):(owner->armedObjs[1]),
(!upg)?(owner->armedObjs[0]):(owner->armedObjs[1]),
selection->ID,ID);
if(refr) {hover(false); hover(true); } //to refresh statusbar if(refr) {hover(false); hover(true); } //to refresh statusbar
} }
} }

View File

@ -34,6 +34,10 @@ class CGarrisonSlot : public CIntObject
CAnimImage * selectionImage; // image for selection, not always visible CAnimImage * selectionImage; // image for selection, not always visible
CLabel * stackCount; CLabel * stackCount;
bool viewInfo();
bool highlightOrDropArtifact();
bool split();
void setHighlight(bool on); void setHighlight(bool on);
public: public:
virtual void hover (bool on); //call-in virtual void hover (bool on); //call-in