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,16 +144,9 @@ void CGarrisonSlot::clickRight(tribool down, bool previousState)
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;
LOCPLINT->cb->getUpgradeInfo(getObj(), ID, pom);
@ -171,77 +164,12 @@ void CGarrisonSlot::clickLeft(tribool down, bool previousState)
elem->block(true);
redraw();
refr = true;
GH.pushInt(new CStackWindow(myStack, dism, pom, upgr));
}
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);
return true;
}
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;
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 CGarrisonSlot::highlightOrDropArtifact()
{
bool artSelected = false;
if (CWindowWithArtifacts* chw = dynamic_cast<CWindowWithArtifacts*>(GH.topInt())) //dirty solution
{
@ -277,8 +205,81 @@ void CGarrisonSlot::clickLeft(tribool down, bool previousState)
}
}
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;
}
// 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
}
}

View File

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