doubleclick on cards in hand

This commit is contained in:
Max-Wilhelm Bruker 2009-07-10 23:06:20 +02:00
parent 9749423d62
commit 44c64322d3
8 changed files with 91 additions and 15 deletions

View file

@ -104,6 +104,18 @@ QString CardInfo::getMainCardType() const
return result;
}
int CardInfo::getTableRow() const
{
QString mainCardType = getMainCardType();
if (mainCardType == "Land")
return 0;
if ((mainCardType == "Sorcery") || (mainCardType == "Instant"))
return 2;
if (mainCardType == "Creature")
return 3;
return 1;
}
void CardInfo::addToSet(CardSet *set)
{
set->append(this);
@ -337,7 +349,6 @@ int CardDatabase::loadFromFile(const QString &fileName)
clear();
setHash.reserve(setCount);
qDebug(QString("setCount = %1").arg(setCount).toLatin1());
for (unsigned int i = 0; i < setCount; i++) {
CardSet *newSet = new CardSet;
newSet->loadFromStream(in);

View file

@ -60,6 +60,7 @@ public:
QString getPowTough() const { return powtough; }
QStringList getText() const { return text; }
QString getMainCardType() const;
int getTableRow() const;
void addToSet(CardSet *set);
QPixmap *loadPixmap();
QPixmap *getPixmap(QSize size);

View file

@ -8,7 +8,7 @@
#include "player.h"
CardItem::CardItem(CardDatabase *_db, const QString &_name, int _cardid, QGraphicsItem *parent)
: AbstractGraphicsItem(parent), db(_db), name(_name), id(_cardid), tapped(false), attacking(false), facedown(false), counters(0), doesntUntap(false), dragItem(NULL)
: AbstractGraphicsItem(parent), db(_db), info(db->getCard(_name)), name(_name), id(_cardid), tapped(false), attacking(false), facedown(false), counters(0), doesntUntap(false), dragItem(NULL)
{
setCursor(Qt::OpenHandCursor);
setFlag(ItemIsSelectable);
@ -34,7 +34,7 @@ void CardItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QSizeF translatedSize = option->matrix.mapRect(boundingRect()).size();
if (tapped)
translatedSize.transpose();
QPixmap *translatedPixmap = db->getCard(name)->getPixmap(translatedSize.toSize());
QPixmap *translatedPixmap = info->getPixmap(translatedSize.toSize());
painter->save();
if (translatedPixmap) {
painter->resetTransform();
@ -70,6 +70,7 @@ void CardItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
void CardItem::setName(const QString &_name)
{
name = _name;
info = db->getCard(name);
update();
}
@ -128,7 +129,7 @@ CardDragItem *CardItem::createDragItem(int _id, const QPointF &_pos, const QPoin
deleteDragItem();
dragItem = new CardDragItem(this, _id, _pos, faceDown);
scene()->addItem(dragItem);
dragItem->updatePosition(_scenePos/* - dragItem->getHotSpot()*/);
dragItem->updatePosition(_scenePos);
return dragItem;
}
@ -181,11 +182,6 @@ void CardItem::mouseReleaseEvent(QGraphicsSceneMouseEvent */*event*/)
void CardItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{
if (!isSelected()) {
// Unselect all items, then select this one
scene()->setSelectionArea(QPainterPath());
setSelected(true);
}
event->accept();
CardZone *zone = (CardZone *) parentItem();
@ -193,7 +189,11 @@ void CardItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
if (!zone->getPlayer()->getLocal())
return;
if (zone->getHasCardAttr())
if (zone->getName() == "hand") {
TableZone *table = (TableZone *) zone->getPlayer()->getZones()->findZone("table");
QPoint gridPoint = table->getFreeGridPoint(info->getTableRow());
table->handleDropEvent(id, zone, table->mapFromGrid(gridPoint).toPoint(), false);
} else if (zone->getName() == "table")
((TableZone *) zone)->toggleTapped();
}

View file

@ -6,6 +6,7 @@
class CardDatabase;
class CardDragItem;
class CardZone;
class CardInfo;
const int CARD_WIDTH = 72;
const int CARD_HEIGHT = 102;
@ -22,6 +23,7 @@ enum CardItemType {
class CardItem : public AbstractGraphicsItem {
private:
CardDatabase *db;
CardInfo *info;
QString name;
int id;
bool tapped;
@ -30,6 +32,7 @@ private:
int counters;
QString annotation;
bool doesntUntap;
QPoint gridPoint;
CardDragItem *dragItem;
public:
enum { Type = typeCard };
@ -38,6 +41,8 @@ public:
~CardItem();
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
QPoint getGridPoint() const { return gridPoint; }
void setGridPoint(const QPoint &_gridPoint) { gridPoint = _gridPoint; }
int getId() const { return id; }
void setId(int _id) { id = _id; }
QString getName() const { return name; }

View file

@ -11,7 +11,7 @@ HandZone::HandZone(Player *_p, QGraphicsItem *parent)
QRectF HandZone::boundingRect() const
{
return QRectF(0, 0, 100, 510);
return QRectF(0, 0, 100, 578);
}
void HandZone::paint(QPainter *painter, const QStyleOptionGraphicsItem */*option*/, QWidget */*widget*/)

View file

@ -4,9 +4,52 @@
#include "client.h"
TableZone::TableZone(Player *_p, QGraphicsItem *parent)
: CardZone(_p, "table", true, false, parent), width(864), height(510)
: CardZone(_p, "table", true, false, parent), width(864), height(578)
{
cards = new CardList(true);
gridPoints << (QList<QPoint>() << QPoint(8, 12)
<< QPoint(9, 13)
<< QPoint(10, 14)
<< QPoint(12, 12)
<< QPoint(13, 13)
<< QPoint(14, 14)
<< QPoint(4, 12)
<< QPoint(5, 13)
<< QPoint(6, 14)
<< QPoint(16, 12)
<< QPoint(17, 13)
<< QPoint(18, 14)
<< QPoint(0, 12)
<< QPoint(1, 13)
<< QPoint(2, 14)
<< QPoint(20, 12)
<< QPoint(21, 13)
<< QPoint(22, 14))
<< (QList<QPoint>() << QPoint(10, 8)
<< QPoint(13, 8)
<< QPoint(7, 8)
<< QPoint(16, 8)
<< QPoint(4, 8)
<< QPoint(19, 8)
<< QPoint(1, 8)
<< QPoint(22, 8))
<< (QList<QPoint>() << QPoint(10, 4)
<< QPoint(13, 4)
<< QPoint(7, 4)
<< QPoint(16, 4)
<< QPoint(4, 4)
<< QPoint(19, 4)
<< QPoint(1, 4)
<< QPoint(22, 4))
<< (QList<QPoint>() << QPoint(10, 0)
<< QPoint(13, 0)
<< QPoint(7, 0)
<< QPoint(16, 0)
<< QPoint(4, 0)
<< QPoint(19, 0)
<< QPoint(1, 0)
<< QPoint(22, 0));
}
QRectF TableZone::boundingRect() const
@ -31,8 +74,8 @@ void TableZone::addCardImpl(CardItem *card, int _x, int _y)
y = height - CARD_HEIGHT - y;
card->setPos(x, y);
// }
card->setGridPoint(QPoint(_x, _y));
card->setZValue((y + CARD_HEIGHT) * width + x + 1000);
qDebug(QString("table: appended %1 at pos %2: zValue = %3, x = %4, y = %5").arg(card->getName()).arg(cards->size() - 1).arg(card->zValue()).arg(x).arg(y).toLatin1());
card->setParentItem(this);
card->setVisible(true);
card->update();
@ -65,7 +108,10 @@ void TableZone::toggleTapped()
CardItem *TableZone::getCardFromGrid(const QPoint &gridPoint) const
{
for (int i = 0; i < cards->size(); i++)
if (cards->at(i)->getGridPoint() == gridPoint)
return cards->at(i);
return 0;
}
QPointF TableZone::mapFromGrid(const QPoint &gridPoint) const
@ -89,3 +135,14 @@ QPoint TableZone::mapToGrid(const QPointF &mapPoint) const
return QPoint(round(((double) x * gridPointsPerCardX) / CARD_WIDTH), round(((double) y * gridPointsPerCardY) / CARD_HEIGHT));
}
QPoint TableZone::getFreeGridPoint(int row) const
{
Q_ASSERT(row < gridPoints.size());
QList<QPoint> pointList = gridPoints[row];
for (int i = 0; i < pointList.size(); i++)
if (!getCardFromGrid(pointList[i]))
return pointList[i];
return QPoint(0, 0);
}

View file

@ -6,6 +6,7 @@
class TableZone : public CardZone {
private:
int width, height;
QList<QList<QPoint> > gridPoints;
public:
static const int gridPointsPerCardX = 2;
static const int gridPointsPerCardY = 3;
@ -19,6 +20,7 @@ public:
CardItem *getCardFromGrid(const QPoint &gridPoint) const;
QPointF mapFromGrid(const QPoint &gridPoint) const;
QPoint mapToGrid(const QPointF &mapPoint) const;
QPoint getFreeGridPoint(int row) const;
protected:
void addCardImpl(CardItem *card, int x, int y);
};

View file

@ -232,7 +232,7 @@ MainWindow::MainWindow(QWidget *parent)
// db->importOracleDir();
// db->saveToFile("../cards.dat");
scene = new QGraphicsScene(0, 0, 1096, 1024, this);
scene = new QGraphicsScene(0, 0, 1096, 1160, this);
view = new GameView(scene);
// view->setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));