diff --git a/projects/mtg/include/CardSelector.h b/projects/mtg/include/CardSelector.h index 7922c26ab..b49583228 100644 --- a/projects/mtg/include/CardSelector.h +++ b/projects/mtg/include/CardSelector.h @@ -48,6 +48,7 @@ class ObjectSelector : public GuiLayer LimitorFunctor* limitor; Pos bigpos; map lasts; + stack< pair*, SelectorZone> > limitorStack; stack memoryStack; T* fetchMemory(SelectorMemory&); @@ -60,10 +61,13 @@ class ObjectSelector : public GuiLayer bool CheckUserInput(JButton key); void Update(float dt); void Render(); - void Limit(LimitorFunctor* limitor, SelectorZone); void Push(); void Pop(); + void Limit(LimitorFunctor* limitor, SelectorZone); + void PushLimitor(); + void PopLimitor(); + typedef T Target; }; diff --git a/projects/mtg/src/CardSelector.cpp b/projects/mtg/src/CardSelector.cpp index 9cc6b36cf..60a80bc83 100644 --- a/projects/mtg/src/CardSelector.cpp +++ b/projects/mtg/src/CardSelector.cpp @@ -47,146 +47,128 @@ template<> void CardSelector::Remove(CardSelector::Target* card) { for (vector::iterator it = cards.begin(); it != cards.end(); ++it) - { - if (card == *it) - { - if (active == *it) - { - CardView* c = dynamic_cast(active); if (c) c->zoom = 1.0; - active = closest(cards, limitor, active); - c = dynamic_cast(active); if (c) c->zoom = 1.4; - } - if (active == *it) active = NULL; - cards.erase(it); - return; - } - } + if (card == *it) + { + if (active == *it) + { + CardView* c = dynamic_cast(active); if (c) c->zoom = 1.0; + active = closest(cards, limitor, active); + c = dynamic_cast(active); if (c) c->zoom = 1.4; + } + if (active == *it) active = NULL; + cards.erase(it); + return; + } } template<> -CardSelector::Target* CardSelector::fetchMemory(SelectorMemory& memory) -{ +CardSelector::Target* CardSelector::fetchMemory(SelectorMemory& memory) { if (NULL == memory.object) return NULL; for (vector::iterator it = cards.begin(); it != cards.end(); ++it) - { - if (*it == memory.object) - { - if ((NULL == limitor) || (limitor->select(memory.object))) - return memory.object; - else break; - } + if (*it == memory.object) { + if ((NULL == limitor) || (limitor->select(memory.object))) + return memory.object; + else break; } // We come here if the card is not in the selector any more, or if // it is there but it is now refused by the limitor. return closest(cards, limitor, memory.x, memory.y); } template<> -void CardSelector::Push() -{ +void CardSelector::Push() { memoryStack.push(SelectorMemory(active)); } template<> -void CardSelector::Pop() -{ +void CardSelector::Pop() { Target* oldactive = active; - if (!memoryStack.empty()) - { - active = fetchMemory(memoryStack.top()); - memoryStack.pop(); - SelectorZone oldowner; - if (CardView *q = dynamic_cast(oldactive)) oldowner = q->owner; else oldowner = nullZone; - if (nullZone != oldowner) lasts[oldowner] = SelectorMemory(oldactive); - } - if (active != oldactive) - { - { CardView* c = dynamic_cast(oldactive); if (c) c->zoom = 1.0; } //Is this needed, I think it is one in Leaving(0) ? - { CardView* c = dynamic_cast(active); if (c) c->zoom = 1.4; } //Is this needed, I think it is one in Entering() ? - if (oldactive) oldactive->Leaving(JGE_BTN_NONE); - if (active) active->Entering(); - } + if (!memoryStack.empty()) { + active = fetchMemory(memoryStack.top()); + memoryStack.pop(); + SelectorZone oldowner; + if (CardView *q = dynamic_cast(oldactive)) oldowner = q->owner; else oldowner = nullZone; + if (nullZone != oldowner) lasts[oldowner] = SelectorMemory(oldactive); + } + if (active != oldactive) { + { CardView* c = dynamic_cast(oldactive); if (c) c->zoom = 1.0; } //Is this needed, I think it is one in Leaving(0) ? + { CardView* c = dynamic_cast(active); if (c) c->zoom = 1.4; } //Is this needed, I think it is one in Entering() ? + if (oldactive) oldactive->Leaving(JGE_BTN_NONE); + if (active) active->Entering(); + } } template<> bool CardSelector::CheckUserInput(JButton key) { - if (!active) - { - for (vector::iterator it = cards.begin(); it != cards.end(); ++it) - if ((NULL == limitor) || (limitor->select(*it))) - { - active = *it; - active->Entering(); - return true; - } - return true; - } + if (!active) { + for (vector::iterator it = cards.begin(); it != cards.end(); ++it) + if ((NULL == limitor) || (limitor->select(*it))) { + active = *it; + active->Entering(); + return true; + } + return true; + } Target* oldactive = active; - switch (key) - { - case JGE_BTN_SEC: - GameObserver::GetInstance()->cancelCurrentAction(); - return true; - case JGE_BTN_OK: - GameObserver::GetInstance()->ButtonPressed(active); - return true; - break; - case JGE_BTN_LEFT: - active = closest(cards, limitor, active); - break; - case JGE_BTN_RIGHT: - active = closest(cards, limitor, active); - break; - case JGE_BTN_UP: - active = closest(cards, limitor, active); - break; - case JGE_BTN_DOWN: - active = closest(cards, limitor, active); - break; - case JGE_BTN_CANCEL: - bigMode = (bigMode+1) % NB_BIG_MODES; - if(bigMode == BIG_MODE_TEXT) - options[Options::DISABLECARDS].number = 1; - else - options[Options::DISABLECARDS].number = 0; - return true; - default: - return false; - } - if (active != oldactive) - { - SelectorZone oldowner, owner; - if (CardView *q = dynamic_cast(oldactive)) oldowner = q->owner; else oldowner = nullZone; - if (CardView *q = dynamic_cast(active)) owner = q->owner; else owner = nullZone; - if (oldowner != owner) - { - if (nullZone != owner) + switch (key) { + case JGE_BTN_SEC: + GameObserver::GetInstance()->cancelCurrentAction(); + return true; + case JGE_BTN_OK: + GameObserver::GetInstance()->ButtonPressed(active); + return true; + break; + case JGE_BTN_LEFT: + active = closest(cards, limitor, active); + break; + case JGE_BTN_RIGHT: + active = closest(cards, limitor, active); + break; + case JGE_BTN_UP: + active = closest(cards, limitor, active); + break; + case JGE_BTN_DOWN: + active = closest(cards, limitor, active); + break; + case JGE_BTN_CANCEL: + bigMode = (bigMode+1) % NB_BIG_MODES; + if(bigMode == BIG_MODE_TEXT) + options[Options::DISABLECARDS].number = 1; + else + options[Options::DISABLECARDS].number = 0; + return true; + default: + return false; + } + if (active != oldactive) { + SelectorZone oldowner, owner; + if (CardView *q = dynamic_cast(oldactive)) oldowner = q->owner; else oldowner = nullZone; + if (CardView *q = dynamic_cast(active)) owner = q->owner; else owner = nullZone; + if (oldowner != owner) { + if (nullZone != owner) { + if (PlayGuiObject* old = fetchMemory(lasts[owner])) + switch (key) { - if (PlayGuiObject* old = fetchMemory(lasts[owner])) - switch (key) - { - case JGE_BTN_LEFT: if (old->x < oldactive->x) active = old; break; - case JGE_BTN_RIGHT: if (old->x > oldactive->x) active = old; break; - case JGE_BTN_UP: if (old->y < oldactive->y) active = old; break; - case JGE_BTN_DOWN: if (old->y > oldactive->y) active = old; break; - default: if (old) active = old; break; - } + case JGE_BTN_LEFT: if (old->x < oldactive->x) active = old; break; + case JGE_BTN_RIGHT: if (old->x > oldactive->x) active = old; break; + case JGE_BTN_UP: if (old->y < oldactive->y) active = old; break; + case JGE_BTN_DOWN: if (old->y > oldactive->y) active = old; break; + default: if (old) active = old; break; } - lasts[oldowner] = SelectorMemory(oldactive); - } - } - if (active != oldactive) - { - { CardView* c = dynamic_cast(oldactive); if (c) c->zoom = 1.0; } - { CardView* c = dynamic_cast(active); if (c) c->zoom = 1.4; } - if (oldactive) oldactive->Leaving(JGE_BTN_NONE); - if (active) active->Entering(); + } + lasts[oldowner] = SelectorMemory(oldactive); } + } + if (active != oldactive) { + { CardView* c = dynamic_cast(oldactive); if (c) c->zoom = 1.0; } + { CardView* c = dynamic_cast(active); if (c) c->zoom = 1.4; } + if (oldactive) oldactive->Leaving(JGE_BTN_NONE); + if (active) active->Entering(); + } return true; } template<> -void CardSelector::Update(float dt) -{ +void CardSelector::Update(float dt) { float boundary = duel->RightBoundary(); float position = boundary - CardGui::BigWidth / 2; if (CardView* c = dynamic_cast(active)) @@ -199,60 +181,67 @@ void CardSelector::Update(float dt) } template<> -void CardSelector::Render() -{ - if (active) - { - active->Render(); - if (CardView* c = dynamic_cast(active)){ - switch(bigMode){ - case BIG_MODE_SHOW: - c->RenderBig(bigpos); - break; - case BIG_MODE_TEXT: - c->alternateRenderBig(bigpos); - break; - default: - break; - } +void CardSelector::Render() { + if (active) { + active->Render(); + if (CardView* c = dynamic_cast(active)) { + switch(bigMode) { + case BIG_MODE_SHOW: + c->RenderBig(bigpos); + break; + case BIG_MODE_TEXT: + c->alternateRenderBig(bigpos); + break; + default: + break; } } + } } template<> -void CardSelector::Limit(LimitorFunctor* limitor, SelectorZone destzone) -{ +void CardSelector::Limit(LimitorFunctor* limitor, SelectorZone destzone) { this->limitor = limitor; - if (limitor && !limitor->select(active)) - { - Target* oldactive = active; - SelectorZone oldowner; - if (CardView *q = dynamic_cast(oldactive)) oldowner = q->owner; else oldowner = nullZone; - if (oldowner != destzone) - { - if (nullZone != destzone) - if (PlayGuiObject* old = fetchMemory(lasts[destzone])) - active = old; - lasts[oldowner] = SelectorMemory(oldactive); - } + if (limitor && !limitor->select(active)) { + Target* oldactive = active; + SelectorZone oldowner; + if (CardView *q = dynamic_cast(oldactive)) oldowner = q->owner; else oldowner = nullZone; + if (oldowner != destzone) { + if (nullZone != destzone) + if (PlayGuiObject* old = fetchMemory(lasts[destzone])) + active = old; + lasts[oldowner] = SelectorMemory(oldactive); + } - if (limitor && !limitor->select(active)) - { - active = NULL; - for (vector::iterator it = cards.begin(); it != cards.end(); ++it) - if (limitor->select(*it)) - { - active = *it; - break; - } - } - - if (active != oldactive) - { - { CardView* c = dynamic_cast(oldactive); if (c) c->zoom = 1.0; } - { CardView* c = dynamic_cast(active); if (c) c->zoom = 1.4; } - if (oldactive) oldactive->Leaving(JGE_BTN_NONE); - if (active) active->Entering(); + if (limitor && !limitor->select(active)) { + active = NULL; + for (vector::iterator it = cards.begin(); it != cards.end(); ++it) + if (limitor->select(*it)) { + active = *it; + break; } } + + if (active != oldactive) { + { CardView* c = dynamic_cast(oldactive); if (c) c->zoom = 1.0; } + { CardView* c = dynamic_cast(active); if (c) c->zoom = 1.4; } + if (oldactive) oldactive->Leaving(JGE_BTN_NONE); + if (active) active->Entering(); + } + } +} + +template<> +void CardSelector::PushLimitor() { + if (NULL == limitor) return; + SelectorZone owner; + if (CardView *q = dynamic_cast(active)) owner = q->owner; else owner = nullZone; + limitorStack.push(make_pair(limitor, owner)); +} + +template<> +void CardSelector::PopLimitor() { + if (limitorStack.empty()) return; + Limit(limitorStack.top().first, limitorStack.top().second); + limitorStack.pop(); } diff --git a/projects/mtg/src/MTGRules.cpp b/projects/mtg/src/MTGRules.cpp index 9e96bae9c..fb1c22665 100644 --- a/projects/mtg/src/MTGRules.cpp +++ b/projects/mtg/src/MTGRules.cpp @@ -139,9 +139,11 @@ int MTGAttackRule::reactToClick(MTGCardInstance * card){ //Graphically select the next card that can attack if(!card->isAttacker()){ CardSelector * cs = game->mLayers->cs; + cs->PushLimitor(); cs->Limit(this,CardSelector::playZone); cs->CheckUserInput(JGE_BTN_RIGHT); cs->Limit(NULL,CardSelector::playZone); + cs->PopLimitor(); } card->toggleAttacker(); return 1;