📄 cardpile.cpp
字号:
//$Id: CardPile.cpp,v 1.61 2006/05/31 22:12:30 markus Rel $//PROJECT : Cardgames//SUBSYSTEM : Common//REFERENCES ://TODO ://BUGS ://REVISION : $Revision: 1.61 $//AUTHOR : Markus Schwab//CREATED : 03.04.2002//COPYRIGHT : Copyright (C) 2002 - 2006// This program is free software; you can redistribute it and/or modify// it under the terms of the GNU General Public License as published by// the Free Software Foundation; either version 2 of the License, or// (at your option) any later version.// This program is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the// GNU General Public License for more details.// You should have received a copy of the GNU General Public License// along with this program; if not, write to the Free Software// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.#include "CardPile.h"//-----------------------------------------------------------------------------/// Constructor; adds all controls to the dialog/// \param set: Specifier for type of cardset/// \param show: Flag, if cards show their faces//-----------------------------------------------------------------------------ICardPile::ICardPile (PileStyle s, ShowOpt show) : style (s), showOpt (show) { TRACE3 ("ICardPile::ICardPile (PileStyle) - " << (int)style); Check3 (s < LAST);}//-----------------------------------------------------------------------------/// Destructor//-----------------------------------------------------------------------------ICardPile::~ICardPile () { TRACE9 ("ICardPile::~ICardPile ()");}//-----------------------------------------------------------------------------/// Sets the top card of the pile/// \param newCard: New top-card//-----------------------------------------------------------------------------void ICardPile::setTopCard (CardWidget& card) { TRACE5 ("ICardPile::setTopCard (CardWidget&) - Card " << card << " -> new size: " << size () + 1); if ((style > NORMAL) && size ()) { Check3 (operator[] (size () - 1)); resize (size () - 1, style); } if (showOpt < DONT_CHANGE) card.showFace ((bool)showOpt); push_back (&card); resize (size () - 1, NORMAL);}//-----------------------------------------------------------------------------/// Returns and removes the top card of the pile/// \returns \c CardWidget&: Reference to (removed) card//-----------------------------------------------------------------------------CardWidget& ICardPile::removeTopCard () { TRACE5 ("ICardPile::removeTopCard () - Size: " << size ()); Check3 (size () > 0); Check3 (operator[] (size () - 1)); CardWidget& card (getTopCard ()); pop_back (); TRACE5 ("ICardPile::removeTopCard () - Card " << card << " -> new size: " << size ()); if (style > NORMAL) resize (size () - 1, NORMAL); return card;}//-----------------------------------------------------------------------------/// Flips the topmost card of the pile//-----------------------------------------------------------------------------void ICardPile::flipTopCard () { Check3 (size () > 0); getTopCard ().flip ();}//-----------------------------------------------------------------------------/// Sets the top card of the pile visible as indicated/// \param visible: Flag if cardface should be shown or back//-----------------------------------------------------------------------------void ICardPile::showTopCardFace (bool visible) { Check3 (size () > 0); getTopCard ().showFace (visible);}//-----------------------------------------------------------------------------/// Adds various cards to the pile/// \param visible: Flag if cardface should be shown or back//-----------------------------------------------------------------------------void ICardPile::setTopCards (const std::vector<CardWidget*>& staple) { std::vector<CardWidget*>::const_iterator i; for (i = staple.begin (); i != staple.end (); ++i) { Check3 (*i); setTopCard (**i); }}//-----------------------------------------------------------------------------/// Adds various cards to the pile/// \param visible: Flag if cardface should be shown or back//-----------------------------------------------------------------------------void ICardPile::setTopCards (const std::vector<CardWidget*>& staple, bool visible) { std::vector<CardWidget*>::const_iterator i; for (i = staple.begin (); i != staple.end (); ++i) { Check3 (*i); setTopCard (**i, visible); }}//-----------------------------------------------------------------------------/// Removes all cards from pile//-----------------------------------------------------------------------------void ICardPile::clear () { while (size ()) remove (getTopCard ());}//-----------------------------------------------------------------------------/// Returns the card with the passed ID/// \param id: ID of card to return/// \returns \c CardWidget*: Pointer to card with passed ID (or NULL)//-----------------------------------------------------------------------------CardWidget* ICardPile::get (unsigned int id) const { std::vector<CardWidget*>::const_iterator i; for (i = begin (); i != end (); ++i) { Check3 (*i); if ((*i)->id () == id) return *i; } return NULL;}//-----------------------------------------------------------------------------/// Inserts a card into the pile/// \param card: Card to insert/// \param pos: Position of new card/// \returns unsigned int: Position where card was inserted//-----------------------------------------------------------------------------unsigned int ICardPile::insert (CardWidget& card, unsigned int pos) { TRACE5 ("ICardPile::insert (CardWidget, unsigned int&) - Card " << card << " at " << pos); Check3 (pos <= size ()); if (showOpt < DONT_CHANGE) card.showFace ((bool)showOpt); std::vector<CardWidget*>::iterator i (std::vector<CardWidget*>::insert (begin () + pos, &card)); if (style > NORMAL) // Cards to display compressed? resize ((pos == (size () - 1)) ? pos - 1 : pos, style); return i - begin ();}//-----------------------------------------------------------------------------/// Inserts a card into the pile; sorted by the passed function/// \param card: Card to insert/// \param fnSort: Function, how to sort/// \returns unsigned int: Position where card was inserted//-----------------------------------------------------------------------------unsigned int ICardPile::insertSorted (CardWidget& card, CMPFUNC fnSort) { TRACE5 ("ICardPile::insertSorted (CardWidget&, CMPFUNC) - Card " << card); return insert (card, (upper_bound (begin (), end (), &card, fnSort) - begin ()));}//-----------------------------------------------------------------------------/// Removes the passed card from the collection/// \param card: Card to remove//-----------------------------------------------------------------------------CardWidget& ICardPile::remove (CardWidget& card) { TRACE8 ("ICardPile::remove (CardWidget&) - " << card); Check3 (size () > 0); // Search for card and remove it std::vector<CardWidget*>::iterator i (std::find (begin (), end (), &card)); Check3 (i != end ()); i = erase (i); // Check if we have to resize a card if (style > NORMAL) // If last card was removed: Resize new last card (if any) (i == end ()) ? resize (i - begin () - 1, NORMAL) : resize (card, NORMAL); return card;}//-----------------------------------------------------------------------------/// Removes the card at the passed position from the collection/// \param card: Card(position) to remove//-----------------------------------------------------------------------------CardWidget& ICardPile::remove (unsigned int pos) { Check1 (size () > pos); TRACE8 ("ICardPile::remove (unsigned int) - Card at pos " << pos << " (" << *operator[] (pos) << ')'); // Remove card on passed position std::vector<CardWidget*>::iterator i (begin () + pos); CardWidget* pTemp (*i); Check3 (pTemp); i = erase (i); // Check if we have to resize a card if (style > NORMAL) // If last card was removed: Resize new last card (if any) (i == end ()) ? resize (size () - 1, NORMAL) : resize (*pTemp, NORMAL); return *pTemp;}//-----------------------------------------------------------------------------/// Changes the drawing-style of the collection/// \param s: New style//-----------------------------------------------------------------------------void ICardPile::setStyle (PileStyle s) { TRACE5 ("ICardPile::setStyle (PileStyle) - Size = " << size ()); if (s == style) return; style = s; for (int i (0); i < static_cast <int> (size () - 1); ++i) { Check3 (operator[] (i)); resize (i, style); } if (size ()) resize (size () - 1, NORMAL);}//-----------------------------------------------------------------------------/// Sorts the cards in the pile with regard of the colour/// \param a: Card to compare/// \param b: Card to compare/// \returns \c bool: True, if a < b//-----------------------------------------------------------------------------bool ICardPile::compCards (const CardWidget* a, const CardWidget* b) { Check3 (a); Check3 (b); TRACE9 ("ICardPile::compCards (const CardWidget*, const CardWidget*) - " << *a << " < " << *b << " = " << ((a->colour () == b->colour ()) ? a->number () < b->number () : a->colour () < b->colour ())); return ((a->colour () == b->colour ()) ? a->number () < b->number () : a->colour () < b->colour ());}//-----------------------------------------------------------------------------/// Sorts the cards in the pile without regard of the colour/// \param a: Card to compare/// \param b: Card to compare/// \returns \c bool: True, if a < b//-----------------------------------------------------------------------------bool ICardPile::compCardsByNr (const CardWidget* a, const CardWidget* b) { Check3 (a); Check3 (b); TRACE9 ("ICardPile::compCardsByNr (const CardWidget*, const CardWidget*) - " << a->number () << " < " << b->number () << " == " << (a->number () < b->number ())); return a->number () < b->number ();}//-----------------------------------------------------------------------------/// Sorts the cards in the pile without regard of the colour/// \param a: Card to compare/// \param b: Card to compare/// \returns \c bool: True, if a < b//-----------------------------------------------------------------------------bool ICardPile::compCardsByID (const CardWidget* a, const CardWidget* b) { Check3 (a); Check3 (b); return a->id () < b->id ();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -