Commit 0f272a2f authored by brukie's avatar brukie
Browse files

improved card dragging

parent 680d5ab8
...@@ -88,19 +88,22 @@ QPixmap *CardInfo::loadPixmap() ...@@ -88,19 +88,22 @@ QPixmap *CardInfo::loadPixmap()
if (pixmap->load(QString("../pics/%1/%2%3.full.jpg").arg(editions.at(i)).arg(correctedName).arg(1))) if (pixmap->load(QString("../pics/%1/%2%3.full.jpg").arg(editions.at(i)).arg(correctedName).arg(1)))
return pixmap; return pixmap;
} }
pixmap->load("../pics/none.jpg");
return pixmap; return pixmap;
} }
QPixmap *CardInfo::getPixmap(QSize size) QPixmap *CardInfo::getPixmap(QSize size)
{ {
qDebug(QString("CardInfo::getPixmap(%1, %2) for %3").arg(size.width()).arg(size.height()).arg(getName()).toLatin1()); qDebug(QString("CardInfo::getPixmap(%1, %2) for %3").arg(size.width()).arg(size.height()).arg(getName()).toLatin1());
if (QPixmap *result = scaledPixmapCache.value(size.width())) { QPixmap *cachedPixmap = scaledPixmapCache.value(size.width());
if (cachedPixmap) {
qDebug("cache HIT"); qDebug("cache HIT");
return result; return cachedPixmap;
} }
qDebug("cache MISS"); qDebug("cache MISS");
QPixmap *result = new QPixmap(loadPixmap()->scaled(size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation)); QPixmap *bigPixmap = loadPixmap();
if (bigPixmap->isNull())
return 0;
QPixmap *result = new QPixmap(bigPixmap->scaled(size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
scaledPixmapCache.insert(size.width(), result); scaledPixmapCache.insert(size.width(), result);
return result; return result;
} }
......
...@@ -3,90 +3,104 @@ ...@@ -3,90 +3,104 @@
#include "carddatabase.h" #include "carddatabase.h"
#include <QtGui> #include <QtGui>
CardDragItem::CardDragItem(QGraphicsScene *scene, CardZone *_startZone, CardInfo *_info, int _id, const QPointF &_hotSpot, bool _faceDown, QGraphicsItem *parent) CardDragItem::CardDragItem(CardItem *_item, int _id, const QPointF &_hotSpot, bool _faceDown, CardDragItem *parentDrag)
: QGraphicsItem(parent), id(_id), info(_info), hotSpot(_hotSpot), startZone(_startZone), faceDown(_faceDown) : QGraphicsItem(), id(_id), item(_item), hotSpot(_hotSpot), faceDown(_faceDown)
{ {
if ((hotSpot.x() < 0) || (hotSpot.y() < 0)) { if (parentDrag)
qDebug(QString("CardDragItem: coordinate overflow: x = %1, y = %2").arg(hotSpot.x()).arg(hotSpot.y()).toLatin1()); parentDrag->addChildDrag(this);
hotSpot = QPointF(); else {
} else if ((hotSpot.x() > CARD_WIDTH) || (hotSpot.y() > CARD_HEIGHT)) { if ((hotSpot.x() < 0) || (hotSpot.y() < 0)) {
qDebug(QString("CardDragItem: coordinate overflow: x = %1, y = %2").arg(hotSpot.x()).arg(hotSpot.y()).toLatin1()); qDebug(QString("CardDragItem: coordinate overflow: x = %1, y = %2").arg(hotSpot.x()).arg(hotSpot.y()).toLatin1());
hotSpot = QPointF(CARD_WIDTH, CARD_HEIGHT); hotSpot = QPointF();
} else if ((hotSpot.x() > CARD_WIDTH) || (hotSpot.y() > CARD_HEIGHT)) {
qDebug(QString("CardDragItem: coordinate overflow: x = %1, y = %2").arg(hotSpot.x()).arg(hotSpot.y()).toLatin1());
hotSpot = QPointF(CARD_WIDTH, CARD_HEIGHT);
}
setCursor(Qt::ClosedHandCursor);
} }
if (item->getTapped())
setTransform(QTransform().translate((float) CARD_WIDTH / 2, (float) CARD_HEIGHT / 2).rotate(90).translate((float) -CARD_WIDTH / 2, (float) -CARD_HEIGHT / 2));
setZValue(2000000000); setZValue(2000000000);
setCacheMode(DeviceCoordinateCache); setCacheMode(DeviceCoordinateCache);
setCursor(Qt::ClosedHandCursor);
if (!parent)
scene->addItem(this);
} }
CardDragItem::~CardDragItem() CardDragItem::~CardDragItem()
{ {
qDebug("CardDragItem destructor"); qDebug("CardDragItem destructor");
for (int i = 0; i < childDrags.size(); i++)
delete childDrags[i];
} }
QRectF CardDragItem::boundingRect() const void CardDragItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
return QRectF(0, 0, CARD_WIDTH, CARD_HEIGHT);
}
void CardDragItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget */*widget*/)
{ {
QSizeF translatedSize = option->matrix.mapRect(boundingRect()).size(); item->paint(painter, option, widget);
QPixmap *translatedPixmap = info->getPixmap(translatedSize.toSize());
painter->drawPixmap(boundingRect(), *translatedPixmap, translatedPixmap->rect());
} }
void CardDragItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) void CardDragItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{ {
event->accept();
QPointF sp = event->scenePos(); QPointF sp = event->scenePos();
QList<QGraphicsItem *> colliding = scene()->items(sp); QList<QGraphicsItem *> colliding = scene()->items(sp);
CardZone *cursorZone = 0; CardZone *cursorZone = 0;
for (int i = colliding.size() - 1; i >= 0; i--) { for (int i = colliding.size() - 1; i >= 0; i--)
if ((cursorZone = qgraphicsitem_cast<CardZone *>(colliding.at(i)))) { if ((cursorZone = qgraphicsitem_cast<CardZone *>(colliding.at(i))))
if (cursorZone->getName() == "table") {
QPointF cp = cursorZone->scenePos();
QPointF localpos = sp - hotSpot - cp;
setPos(QPointF(round(localpos.x() / RASTER_WIDTH) * RASTER_WIDTH, round(localpos.y() / RASTER_HEIGHT) * RASTER_HEIGHT) + cp);
} else
setPos(sp - hotSpot);
break; break;
}
QPointF newPos;
if (!cursorZone)
return;
else if (cursorZone->getName() == "table") {
QPointF cp = cursorZone->scenePos();
QPointF localpos = sp - hotSpot - cp;
newPos = QPointF(round(localpos.x() / RASTER_WIDTH) * RASTER_WIDTH, round(localpos.y() / RASTER_HEIGHT) * RASTER_HEIGHT) + cp;
} else
newPos = sp - hotSpot;
if (newPos != pos()) {
for (int i = 0; i < childDrags.size(); i++)
childDrags[i]->setPos(newPos + childDrags[i]->getHotSpot());
// qDebug(QString("setPos: x=%1, y=%2").arg(newPos.x()).arg(newPos.y()).toLatin1());
setPos(newPos);
} }
event->accept();
} }
void CardDragItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) void CardDragItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{ {
setCursor(Qt::OpenHandCursor); setCursor(Qt::OpenHandCursor);
QGraphicsScene *sc = scene(); QGraphicsScene *sc = scene();
QPointF sp = scenePos(); QPointF sp = pos();
qDebug(QString("sp: x=%1, y=%2").arg(sp.x()).arg(sp.y()).toLatin1());
sc->removeItem(this); sc->removeItem(this);
QList<QGraphicsItem *> colliding = sc->items(event->scenePos()); QList<QGraphicsItem *> colliding = sc->items(event->scenePos());
qDebug(QString("drop: %1 collisions").arg(colliding.size()).toLatin1()); // qDebug(QString("drop: %1 collisions").arg(colliding.size()).toLatin1());
CardZone *dropZone = 0; CardZone *dropZone = 0;
for (int i = colliding.size() - 1; i >= 0; i--) { for (int i = colliding.size() - 1; i >= 0; i--) {
QRectF bbox = colliding.at(i)->boundingRect(); 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()); // 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)))) { if ((dropZone = qgraphicsitem_cast<CardZone *>(colliding.at(i)))) {
qDebug("zone found"); // qDebug("zone found");
break; break;
} }
} }
if (dropZone) { if (dropZone) {
CardZone *startZone = qgraphicsitem_cast<CardZone *>(item->parentItem());
dropZone->handleDropEvent(id, startZone, (sp - dropZone->scenePos()).toPoint(), faceDown); dropZone->handleDropEvent(id, startZone, (sp - dropZone->scenePos()).toPoint(), faceDown);
QList<QGraphicsItem *> childList = childItems(); for (int i = 0; i < childDrags.size(); i++) {
for (int i = 0; i < childList.size(); i++) { CardDragItem *c = childDrags[i];
CardDragItem *c = qgraphicsitem_cast<CardDragItem *>(childList.at(i)); dropZone->handleDropEvent(c->id, startZone, (sp - dropZone->scenePos() + c->getHotSpot()).toPoint(), faceDown);
dropZone->handleDropEvent(c->id, startZone, (sp - dropZone->scenePos() + c->pos()).toPoint(), faceDown); sc->removeItem(c);
} }
} }
event->accept(); event->accept();
} }
void CardDragItem::addChildDrag(CardDragItem *child)
{
childDrags << child;
}
...@@ -10,18 +10,19 @@ class CardInfo; ...@@ -10,18 +10,19 @@ class CardInfo;
class CardDragItem : public QGraphicsItem { class CardDragItem : public QGraphicsItem {
private: private:
int id; int id;
CardInfo *info; CardItem *item;
QPointF hotSpot; QPointF hotSpot;
CardZone *startZone;
bool faceDown; bool faceDown;
QList<CardDragItem *> childDrags;
public: public:
enum { Type = typeCardDrag }; enum { Type = typeCardDrag };
int type() const { return Type; } int type() const { return Type; }
CardDragItem(QGraphicsScene *scene, CardZone *_startZone, CardInfo *_info, int _id, const QPointF &_hotSpot, bool _faceDown, QGraphicsItem *parent = 0); CardDragItem(CardItem *_item, int _id, const QPointF &_hotSpot, bool _faceDown, CardDragItem *parentDrag = 0);
~CardDragItem(); ~CardDragItem();
QRectF boundingRect() const; QRectF boundingRect() const { return QRectF(0, 0, CARD_WIDTH, CARD_HEIGHT); }
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
QPointF getHotSpot() const { return hotSpot; } QPointF getHotSpot() const { return hotSpot; }
void addChildDrag(CardDragItem *child);
protected: protected:
void mouseMoveEvent(QGraphicsSceneMouseEvent *event); void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
......
...@@ -2,9 +2,10 @@ ...@@ -2,9 +2,10 @@
#include <QGridLayout> #include <QGridLayout>
#include <QLabel> #include <QLabel>
#include <QTextEdit> #include <QTextEdit>
#include <QMessageBox>
CardInfoWidget::CardInfoWidget(CardDatabase *_db, QWidget *parent) CardInfoWidget::CardInfoWidget(CardDatabase *_db, QWidget *parent)
: QFrame(parent), db(_db), pixmapHeight(0) : QFrame(parent), db(_db), pixmapHeight(pixmapWidth)
{ {
cardPicture = new QLabel(); cardPicture = new QLabel();
cardPicture->setAlignment(Qt::AlignCenter); cardPicture->setAlignment(Qt::AlignCenter);
...@@ -50,8 +51,15 @@ CardInfoWidget::CardInfoWidget(CardDatabase *_db, QWidget *parent) ...@@ -50,8 +51,15 @@ CardInfoWidget::CardInfoWidget(CardDatabase *_db, QWidget *parent)
grid->setRowStretch(5, 1); grid->setRowStretch(5, 1);
grid->setColumnStretch(1, 1); grid->setColumnStretch(1, 1);
CardInfo *cardBack = db->getCard();
QPixmap *bigPixmap = cardBack->loadPixmap();
if (bigPixmap->isNull())
QMessageBox::critical(this, tr("Error"), tr("Unable to load pixmap for card back."));
else
pixmapHeight = pixmapWidth * bigPixmap->height() / bigPixmap->width();
setCard(cardBack);
setFrameStyle(QFrame::Panel | QFrame::Raised); setFrameStyle(QFrame::Panel | QFrame::Raised);
setCard(db->getCard());
setFixedSize(sizeHint()); setFixedSize(sizeHint());
} }
...@@ -60,11 +68,11 @@ void CardInfoWidget::setCard(CardInfo *card) ...@@ -60,11 +68,11 @@ void CardInfoWidget::setCard(CardInfo *card)
if (!card) if (!card)
return; return;
if (pixmapHeight == 0) { QPixmap *resizedPixmap = card->getPixmap(QSize(pixmapWidth, pixmapHeight));
QPixmap *bigPixmap = card->loadPixmap(); if (resizedPixmap)
pixmapHeight = pixmapWidth * bigPixmap->height() / bigPixmap->width(); cardPicture->setPixmap(*resizedPixmap);
} else
cardPicture->setPixmap(*card->getPixmap(QSize(pixmapWidth, pixmapHeight))); cardPicture->setPixmap(*(db->getCard()->getPixmap(QSize(pixmapWidth, pixmapHeight))));
nameLabel2->setText(card->getName()); nameLabel2->setText(card->getName());
manacostLabel2->setText(card->getManacost()); manacostLabel2->setText(card->getManacost());
......
...@@ -35,15 +35,35 @@ void CardItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, ...@@ -35,15 +35,35 @@ void CardItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
if (tapped) if (tapped)
translatedSize.transpose(); translatedSize.transpose();
QPixmap *translatedPixmap = db->getCard(name)->getPixmap(translatedSize.toSize()); QPixmap *translatedPixmap = db->getCard(name)->getPixmap(translatedSize.toSize());
painter->drawPixmap(boundingRect(), *translatedPixmap, translatedPixmap->rect()); painter->save();
if (translatedPixmap) {
painter->resetTransform();
if (tapped) {
painter->translate(((qreal) translatedSize.height()) / 2, ((qreal) translatedSize.width()) / 2);
painter->rotate(90);
painter->translate(-((qreal) translatedSize.width()) / 2, -((qreal) translatedSize.height()) / 2);
}
painter->drawPixmap(translatedPixmap->rect(), *translatedPixmap, translatedPixmap->rect());
} else {
QFont f;
f.setStyleHint(QFont::Serif);
f.setPointSize(8);
f.setWeight(QFont::Bold);
painter->setFont(f);
painter->setBrush(QColor(200, 200, 200));
painter->setPen(QPen(Qt::black));
painter->drawRect(0.5, 0.5, CARD_WIDTH - 1, CARD_HEIGHT - 1);
painter->drawText(QRectF(5, 5, CARD_WIDTH - 15, CARD_HEIGHT - 15), Qt::AlignTop | Qt::AlignLeft | Qt::TextWordWrap, name);
}
painter->restore();
if (isSelected()) { if (isSelected()) {
painter->setPen(QPen(QColor("red"))); painter->setPen(Qt::red);
painter->drawRect(QRectF(1, 1, CARD_WIDTH - 2, CARD_HEIGHT - 2)); painter->drawRect(QRectF(0.5, 0.5, CARD_WIDTH - 1, CARD_HEIGHT - 1));
} }
if (counters) { if (counters) {
painter->setFont(QFont("Times", 32, QFont::Bold)); painter->setFont(QFont("Times", 32, QFont::Bold));
painter->setPen(QPen(QColor("black"))); painter->setPen(QPen(Qt::black));
painter->setBackground(QBrush(QColor(255, 255, 255, 100))); painter->setBackground(QBrush(QColor(255, 255, 255, 100)));
painter->setBackgroundMode(Qt::OpaqueMode); painter->setBackgroundMode(Qt::OpaqueMode);
painter->drawText(boundingRect(), Qt::AlignCenter, QString::number(counters)); painter->drawText(boundingRect(), Qt::AlignCenter, QString::number(counters));
...@@ -107,10 +127,11 @@ void CardItem::resetState() ...@@ -107,10 +127,11 @@ void CardItem::resetState()
update(boundingRect()); update(boundingRect());
} }
CardDragItem *CardItem::createDragItem(CardZone *startZone, int _id, const QPointF &_pos, const QPointF &_scenePos, bool faceDown) CardDragItem *CardItem::createDragItem(int _id, const QPointF &_pos, const QPointF &_scenePos, bool faceDown)
{ {
deleteDragItem(); deleteDragItem();
dragItem = new CardDragItem(scene(), startZone, db->getCard(name), _id, _pos, faceDown); dragItem = new CardDragItem(this, _id, _pos, faceDown);
scene()->addItem(dragItem);
dragItem->setPos(_scenePos - dragItem->getHotSpot()); dragItem->setPos(_scenePos - dragItem->getHotSpot());
return dragItem; return dragItem;
...@@ -142,7 +163,7 @@ void CardItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) ...@@ -142,7 +163,7 @@ void CardItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
return; return;
bool faceDown = event->modifiers().testFlag(Qt::ShiftModifier) || facedown; bool faceDown = event->modifiers().testFlag(Qt::ShiftModifier) || facedown;
createDragItem((CardZone *) parentItem(), id, event->pos(), event->scenePos(), faceDown); createDragItem(id, event->pos(), event->scenePos(), faceDown);
dragItem->grabMouse(); dragItem->grabMouse();
QList<QGraphicsItem *> sel = scene()->selectedItems(); QList<QGraphicsItem *> sel = scene()->selectedItems();
...@@ -150,8 +171,9 @@ void CardItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) ...@@ -150,8 +171,9 @@ void CardItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
CardItem *c = (CardItem *) sel.at(i); CardItem *c = (CardItem *) sel.at(i);
if (c == this) if (c == this)
continue; continue;
CardDragItem *drag = new CardDragItem(scene(), (CardZone *) parentItem(), db->getCard(c->getName()), c->getId(), QPointF(), false, dragItem); CardDragItem *drag = new CardDragItem(c, c->getId(), c->pos() - pos(), false, dragItem);
drag->setPos(c->pos() - pos()); drag->setPos(dragItem->pos() + c->pos() - pos());
scene()->addItem(drag);
} }
setCursor(Qt::OpenHandCursor); setCursor(Qt::OpenHandCursor);
} }
......
...@@ -13,13 +13,6 @@ const int CARD_HEIGHT = 102; ...@@ -13,13 +13,6 @@ const int CARD_HEIGHT = 102;
const int RASTER_WIDTH = 36; const int RASTER_WIDTH = 36;
const int RASTER_HEIGHT = 34; const int RASTER_HEIGHT = 34;
/*
const int CARD_WIDTH = 72;
const int CARD_HEIGHT = 108;
const int RASTER_WIDTH = 36;
const int RASTER_HEIGHT = 36;
*/
const int MAX_COUNTERS_ON_CARD = 999; const int MAX_COUNTERS_ON_CARD = 999;
enum CardItemType { enum CardItemType {
...@@ -66,7 +59,7 @@ public: ...@@ -66,7 +59,7 @@ public:
void setDoesntUntap(bool _doesntUntap); void setDoesntUntap(bool _doesntUntap);
void resetState(); void resetState();
CardDragItem *createDragItem(CardZone *startZone, int _id, const QPointF &_pos, const QPointF &_scenePos, bool faceDown); CardDragItem *createDragItem(int _id, const QPointF &_pos, const QPointF &_scenePos, bool faceDown);
void deleteDragItem(); void deleteDragItem();
protected: protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event); void mousePressEvent(QGraphicsSceneMouseEvent *event);
......
...@@ -75,7 +75,7 @@ void GraveZone::mouseMoveEvent(QGraphicsSceneMouseEvent *event) ...@@ -75,7 +75,7 @@ void GraveZone::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
bool faceDown = event->modifiers().testFlag(Qt::ShiftModifier); bool faceDown = event->modifiers().testFlag(Qt::ShiftModifier);
CardItem *card = cards->at(0); CardItem *card = cards->at(0);
CardDragItem *drag = card->createDragItem(this, card->getId(), event->pos(), event->scenePos(), faceDown); CardDragItem *drag = card->createDragItem(card->getId(), event->pos(), event->scenePos(), faceDown);
drag->grabMouse(); drag->grabMouse();
setCursor(Qt::OpenHandCursor); setCursor(Qt::OpenHandCursor);
} }
......
...@@ -76,7 +76,7 @@ void LibraryZone::mouseMoveEvent(QGraphicsSceneMouseEvent *event) ...@@ -76,7 +76,7 @@ void LibraryZone::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
bool faceDown = event->modifiers().testFlag(Qt::ShiftModifier); bool faceDown = event->modifiers().testFlag(Qt::ShiftModifier);
CardItem *card = cards->at(0); CardItem *card = cards->at(0);
CardDragItem *drag = card->createDragItem(this, 0, event->pos(), event->scenePos(), faceDown); CardDragItem *drag = card->createDragItem(0, event->pos(), event->scenePos(), faceDown);
drag->grabMouse(); drag->grabMouse();
setCursor(Qt::OpenHandCursor); setCursor(Qt::OpenHandCursor);
} }
......
...@@ -73,7 +73,7 @@ void RfgZone::mouseMoveEvent(QGraphicsSceneMouseEvent *event) ...@@ -73,7 +73,7 @@ void RfgZone::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
bool faceDown = event->modifiers().testFlag(Qt::ShiftModifier); bool faceDown = event->modifiers().testFlag(Qt::ShiftModifier);
CardItem *card = cards->at(0); CardItem *card = cards->at(0);
CardDragItem *drag = card->createDragItem(this, card->getId(), event->pos(), event->scenePos(), faceDown); CardDragItem *drag = card->createDragItem(card->getId(), event->pos(), event->scenePos(), faceDown);
drag->grabMouse(); drag->grabMouse();
setCursor(Qt::OpenHandCursor); setCursor(Qt::OpenHandCursor);
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment