improved card drag, still unfinished

This commit is contained in:
Max-Wilhelm Bruker 2009-07-07 16:32:32 +02:00
parent 2e0b16e90b
commit 9749423d62
8 changed files with 67 additions and 78 deletions

View file

@ -1,10 +1,11 @@
#include "carddragitem.h"
#include "cardzone.h"
#include "carddatabase.h"
#include "tablezone.h"
#include <QtGui>
CardDragItem::CardDragItem(CardItem *_item, int _id, const QPointF &_hotSpot, bool _faceDown, CardDragItem *parentDrag)
: QGraphicsItem(), id(_id), item(_item), hotSpot(_hotSpot), faceDown(_faceDown)
: QGraphicsItem(), id(_id), item(_item), hotSpot(_hotSpot), faceDown(_faceDown), currentZone(0)
{
if (parentDrag)
parentDrag->addChildDrag(this);
@ -37,27 +38,27 @@ void CardDragItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opti
item->paint(painter, option, widget);
}
void CardDragItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
void CardDragItem::updatePosition(const QPointF &cursorScenePos)
{
event->accept();
QPointF sp = event->scenePos();
QList<QGraphicsItem *> colliding = scene()->items(sp);
QList<QGraphicsItem *> colliding = scene()->items(cursorScenePos);
CardZone *cursorZone = 0;
for (int i = colliding.size() - 1; i >= 0; i--)
if ((cursorZone = qgraphicsitem_cast<CardZone *>(colliding.at(i))))
break;
QPointF newPos;
if (!cursorZone)
return;
else if (cursorZone->getName() == "table") {
currentZone = cursorZone;
QPointF newPos;
if (cursorZone->getName() == "table") {
TableZone *tableZone = (TableZone *) cursorZone;
QPointF cp = cursorZone->scenePos();
QPointF localpos = sp - hotSpot - cp;
QPointF localpos = cursorScenePos - hotSpot - cp;
newPos = QPointF(round(localpos.x() / RASTER_WIDTH) * RASTER_WIDTH, round(localpos.y() / RASTER_HEIGHT) * RASTER_HEIGHT) + cp;
newPos = cp + tableZone->mapFromGrid(tableZone->mapToGrid(localpos));
} else
newPos = sp - hotSpot;
newPos = cursorScenePos - hotSpot;
if (newPos != pos()) {
for (int i = 0; i < childDrags.size(); i++)
childDrags[i]->setPos(newPos + childDrags[i]->getHotSpot());
@ -66,6 +67,12 @@ void CardDragItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
}
}
void CardDragItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
event->accept();
updatePosition(event->scenePos());
}
void CardDragItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
setCursor(Qt::OpenHandCursor);
@ -75,24 +82,12 @@ void CardDragItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
sc->removeItem(this);
QList<QGraphicsItem *> colliding = sc->items(event->scenePos());
// qDebug(QString("drop: %1 collisions").arg(colliding.size()).toLatin1());
CardZone *dropZone = 0;
for (int i = colliding.size() - 1; i >= 0; i--) {
QRectF bbox = colliding.at(i)->boundingRect();
// qDebug(QString("bbox x %1 y %2 w %3 h %4").arg(bbox.x()).arg(bbox.y()).arg(bbox.width()).arg(bbox.height()).toLatin1());
if ((dropZone = qgraphicsitem_cast<CardZone *>(colliding.at(i)))) {
// qDebug("zone found");
break;
}
}
if (dropZone) {
if (currentZone) {
CardZone *startZone = qgraphicsitem_cast<CardZone *>(item->parentItem());
dropZone->handleDropEvent(id, startZone, (sp - dropZone->scenePos()).toPoint(), faceDown);
currentZone->handleDropEvent(id, startZone, (sp - currentZone->scenePos()).toPoint(), faceDown);
for (int i = 0; i < childDrags.size(); i++) {
CardDragItem *c = childDrags[i];
dropZone->handleDropEvent(c->id, startZone, (sp - dropZone->scenePos() + c->getHotSpot()).toPoint(), faceDown);
currentZone->handleDropEvent(c->id, startZone, (sp - currentZone->scenePos() + c->getHotSpot()).toPoint(), faceDown);
sc->removeItem(c);
}
}

View file

@ -14,6 +14,7 @@ private:
QPointF hotSpot;
bool faceDown;
QList<CardDragItem *> childDrags;
CardZone *currentZone;
public:
enum { Type = typeCardDrag };
int type() const { return Type; }
@ -23,6 +24,7 @@ public:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
QPointF getHotSpot() const { return hotSpot; }
void addChildDrag(CardDragItem *child);
void updatePosition(const QPointF &cursorScenePos);
protected:
void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);

View file

@ -128,7 +128,7 @@ CardDragItem *CardItem::createDragItem(int _id, const QPointF &_pos, const QPoin
deleteDragItem();
dragItem = new CardDragItem(this, _id, _pos, faceDown);
scene()->addItem(dragItem);
dragItem->setPos(_scenePos - dragItem->getHotSpot());
dragItem->updatePosition(_scenePos/* - dragItem->getHotSpot()*/);
return dragItem;
}

View file

@ -9,8 +9,6 @@ class CardZone;
const int CARD_WIDTH = 72;
const int CARD_HEIGHT = 102;
const int RASTER_WIDTH = 36;
const int RASTER_HEIGHT = 34;
const int MAX_COUNTERS_ON_CARD = 999;

View file

@ -87,8 +87,6 @@ Game::Game(CardDatabase *_db, Client *_client, QGraphicsScene *_scene, QMenu *_a
connect(aRemoveCounter, SIGNAL(triggered()), this, SLOT(actRemoveCounter()));
aSetCounters = new QAction(tr("&Set counters..."), this);
connect(aSetCounters, SIGNAL(triggered()), this, SLOT(actSetCounters()));
aRearrange = new QAction(tr("&Rearrange"), this);
connect(aRearrange, SIGNAL(triggered()), this, SLOT(actRearrange()));
cardMenu->addAction(aTap);
cardMenu->addAction(aUntap);
@ -99,8 +97,6 @@ Game::Game(CardDatabase *_db, Client *_client, QGraphicsScene *_scene, QMenu *_a
cardMenu->addAction(aAddCounter);
cardMenu->addAction(aRemoveCounter);
cardMenu->addAction(aSetCounters);
cardMenu->addSeparator();
cardMenu->addAction(aRearrange);
dlgStartGame = new DlgStartGame(db);
connect(dlgStartGame, SIGNAL(newDeckLoaded(const QStringList &)), client, SLOT(submitDeck(const QStringList &)));
@ -414,32 +410,6 @@ void Game::actSetCounters()
}
}
void Game::actRearrange()
{
// nur sinnvoll bei Karten auf dem Tisch -> Einschränkung einbauen
int x, y, x_initial = 0, y_initial = 0;
QList<QGraphicsItem *> list = scene->selectedItems();
// Find coordinates of leftmost card
for (int i = 0; i < list.size(); i++) {
CardItem *temp = (CardItem *) list.at(i);
if ((temp->pos().x() < x_initial) || (x_initial == 0)) {
x_initial = (int) temp->pos().x();
y_initial = (int) temp->pos().y();
}
}
x = x_initial;
y = y_initial;
for (int i = 0; i < list.size(); i++) {
CardItem *temp = (CardItem *) list.at(i);
QString zoneName = qgraphicsitem_cast<CardZone *>(temp->parentItem())->getName();
x = x_initial + i * RASTER_WIDTH;
y = y_initial + (i % 3) * RASTER_HEIGHT;
client->moveCard(temp->getId(), zoneName, zoneName, x, y);
}
}
void Game::actSayMessage()
{
QAction *a = qobject_cast<QAction *>(sender());

View file

@ -15,7 +15,7 @@ class Game : public QObject {
Q_OBJECT
private:
QMenu *actionsMenu, *sayMenu, *cardMenu;
QAction *aTap, *aUntap, *aDoesntUntap, *aFlip, *aAddCounter, *aRemoveCounter, *aSetCounters, *aRearrange,
QAction *aTap, *aUntap, *aDoesntUntap, *aFlip, *aAddCounter, *aRemoveCounter, *aSetCounters,
*aUntapAll, *aDecLife, *aIncLife, *aSetLife, *aShuffle, *aDraw, *aDrawCards, *aRollDice, *aCreateToken, *aEditMessages;
DlgStartGame *dlgStartGame;
@ -45,7 +45,6 @@ private slots:
void actAddCounter();
void actRemoveCounter();
void actSetCounters();
void actRearrange();
void actSayMessage();

View file

@ -19,14 +19,18 @@ void TableZone::paint(QPainter *painter, const QStyleOptionGraphicsItem */*optio
painter->fillRect(boundingRect(), QColor(0, 0, 100));
}
void TableZone::addCardImpl(CardItem *card, int x, int y)
void TableZone::addCardImpl(CardItem *card, int _x, int _y)
{
QPointF mapPoint = mapFromGrid(QPoint(_x, _y));
qreal x = mapPoint.x();
qreal y = mapPoint.y();
cards->append(card);
if ((x != -1) && (y != -1)) {
// if ((x != -1) && (y != -1)) {
if (!player->getLocal())
y = height - CARD_HEIGHT - y;
card->setPos(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);
@ -36,17 +40,8 @@ void TableZone::addCardImpl(CardItem *card, int x, int y)
void TableZone::handleDropEvent(int cardId, CardZone *startZone, const QPoint &dropPoint, bool faceDown)
{
int x = dropPoint.x();
int y = dropPoint.y();
if (x < 0)
x = 0;
if (y < 0)
y = 0;
if (x > width - CARD_WIDTH)
x = width - CARD_WIDTH;
if (y > height - CARD_HEIGHT)
y = height - CARD_HEIGHT;
player->client->moveCard(cardId, startZone->getName(), getName(), x, y, faceDown);
QPoint gridPoint = mapToGrid(dropPoint);
player->client->moveCard(cardId, startZone->getName(), getName(), gridPoint.x(), gridPoint.y(), faceDown);
}
void TableZone::reorganizeCards()
@ -67,3 +62,30 @@ void TableZone::toggleTapped()
setCardAttr(temp->getId(), "tapped", (!temp->getTapped() || tapAll) ? "1" : "0");
}
}
CardItem *TableZone::getCardFromGrid(const QPoint &gridPoint) const
{
}
QPointF TableZone::mapFromGrid(const QPoint &gridPoint) const
{
return QPointF(gridPoint.x() * CARD_WIDTH / gridPointsPerCardX,
gridPoint.y() * CARD_HEIGHT / gridPointsPerCardY);
}
QPoint TableZone::mapToGrid(const QPointF &mapPoint) const
{
qreal x = mapPoint.x();
qreal y = mapPoint.y();
if (x < 0)
x = 0;
else if (x > width - CARD_WIDTH)
x = width - CARD_WIDTH;
if (y < 0)
y = 0;
else if (y > height - CARD_HEIGHT)
y = height - CARD_HEIGHT;
return QPoint(round(((double) x * gridPointsPerCardX) / CARD_WIDTH), round(((double) y * gridPointsPerCardY) / CARD_HEIGHT));
}

View file

@ -3,19 +3,22 @@
#include "cardzone.h"
const int GRID_WIDTH = 30;
const int GRID_HEIGHT = 30;
class TableZone : public CardZone {
private:
int width, height;
public:
static const int gridPointsPerCardX = 2;
static const int gridPointsPerCardY = 3;
TableZone(Player *_p, QGraphicsItem *parent = 0);
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
void reorganizeCards();
void toggleTapped();
void handleDropEvent(int cardId, CardZone *startZone, const QPoint &dropPoint, bool faceDown);
CardItem *getCardFromGrid(const QPoint &gridPoint) const;
QPointF mapFromGrid(const QPoint &gridPoint) const;
QPoint mapToGrid(const QPointF &mapPoint) const;
protected:
void addCardImpl(CardItem *card, int x, int y);
};