From kde-commits Sun Oct 31 23:23:09 2010 From: Stefan Majewsky Date: Sun, 31 Oct 2010 23:23:09 +0000 To: kde-commits Subject: KDE/kdegames/kolf Message-Id: <20101031232309.B639AAC8A7 () svn ! kde ! org> X-MARC-Message: https://marc.info/?l=kde-commits&m=128856754812917 SVN commit 1191708 by majewsky: Adapt Kolf::Overlay and Kolf::Shape from playground/games/kolf2/base. I'm aiming to restrict CanvasItem subclasses to those which are actually relevant for the gameplay (e.g. Bumper, Cup, Bridge; e.g. NOT RectPoint and WallPoint). The first step is to introduce a means of displaying editing interfaces without the need for additional CanvasItems, i.e. Kolf::Overlay. The Overlay class relies on the Shape class which determines the outlines, but Shape will also be useful for the upcoming step to Box2D. That code is already available in playground/games/kolf2/base, but I've kept things simple in this first step. The aim is to replace the editing interface first, and further simplify datastructures in the various CanvasItem subclasses in the process. Currently, an overlay is only installed on Cup and Wall. I plan to stabilize those first before I install overlays on all items. M +3 -0 CMakeLists.txt M +47 -0 canvasitem.cpp M +24 -4 canvasitem.h M +24 -2 game.cpp M +11 -1 game.h A overlay.cpp [License: GPL (v2+)] A overlay.h [License: GPL (v2+)] A shape.cpp [License: GPL (v2+)] A shape.h [License: GPL (v2+)] A utils-animateditem.cpp [License: GPL (v2+)] A utils-animateditem.h [License: GPL (v2+)] --- trunk/KDE/kdegames/kolf/CMakeLists.txt #1191707:1191708 @@ -27,6 +27,9 @@ floater.cpp slope.cpp itemfactory.cpp + overlay.cpp + shape.cpp + utils-animateditem.cpp tagaro/board.cpp tagaro/scene.cpp tagaro/spriteobjectitem.cpp --- trunk/KDE/kdegames/kolf/canvasitem.cpp #1191707:1191708 @@ -19,7 +19,18 @@ #include "canvasitem.h" #include "game.h" +#include "overlay.h" +#include "shape.h" +CanvasItem::~CanvasItem() +{ + //The overlay is deleted first, because it might interact with all other parts of the object. + delete m_overlay; + //NOTE: Box2D objects will need to be destroyed in the following order: + //subobjects, shapes, own b2Body + qDeleteAll(m_shapes); +} + QGraphicsRectItem *CanvasItem::onVStrut() { QGraphicsItem *qthis = dynamic_cast(this); @@ -60,6 +71,42 @@ game->playSound(file, vol); } +void CanvasItem::editModeChanged(bool editing) +{ + Kolf::Overlay* overlay = this->overlay(); + if (overlay) + overlay->setVisible(editing); +} + +void CanvasItem::addShape(Kolf::Shape* shape) +{ + if (shape->attach(this)) //this will fail if the shape is already attached to some object + m_shapes << shape; +} + +Kolf::Overlay* CanvasItem::overlay(bool createIfNecessary) +{ + //the overlay is created once it is requested + if (!m_overlay && createIfNecessary) + { + m_overlay = createOverlay(); + if (m_overlay) + { + //should be above object representation + m_overlay->setZValue(m_overlay->qitem()->zValue() + 100); + //initialize the overlay's parameters + m_overlay->update(); + } + } + return m_overlay; +} + +void CanvasItem::propagateUpdate() +{ + if (m_overlay) + m_overlay->update(); +} + //BEGIN EllipticalCanvasItem EllipticalCanvasItem::EllipticalCanvasItem(bool withEllipse, const QString& spriteKey, QGraphicsItem* parent) --- trunk/KDE/kdegames/kolf/canvasitem.h #1191707:1191708 @@ -32,11 +32,17 @@ class KConfigGroup; class KolfGame; +namespace Kolf +{ + class Overlay; + class Shape; +} + class CanvasItem { public: - CanvasItem() { game = 0; } - virtual ~CanvasItem() {} + CanvasItem() : game(0), m_overlay(0) { } + virtual ~CanvasItem(); ///load your settings from the KConfigGroup, which represents a course. virtual void load(KConfigGroup *) {} ///returns a bool that is true if your item needs to load after other items @@ -52,7 +58,7 @@ ///called right after all items are saved. virtual void savingDone() {} ///called when the edit mode has been changed. - virtual void editModeChanged(bool /*editing*/) {} + virtual void editModeChanged(bool editing); ///The item should delete any other objects it's created. DO NOT DO THIS KIND OF STUFF IN THE DESTRUCTOR! virtual void aboutToDie() {} ///Returns the object to get rid of when the delete button is pressed on this item. @@ -109,7 +115,6 @@ virtual void setVelocity(const Vector& velocity) { m_velocity = velocity; } Vector velocity() const { return m_velocity; } virtual void moveBy(double , double) { kDebug(12007) << "Warning, empty moveBy used";} //needed so that float can call the own custom moveBy()s of everything on it - protected: ///pointer to main KolfGame KolfGame *game; @@ -121,6 +126,21 @@ ///custom animation code bool m_animated; Vector m_velocity; + +//AFTER THIS LINE follows what I have inserted during the refactoring + public: + QList shapes() const { return m_shapes; } + Kolf::Overlay* overlay(bool createIfNecessary = true); + protected: + void addShape(Kolf::Shape* shape); + ///Creates the optimal overlay for this object. The implementation does not have to propagate its properties to the overlay, as the overlay is updated just after it has been created. + ///@warning Do not actually call this function from subclass implementations. Use overlay() instead. + virtual Kolf::Overlay* createOverlay() { return 0; } //TODO: make this pure virtual when all CanvasItems are QGraphicsItems and implement createOverlay() (and then disallow createOverlay() == 0) + ///This function should be called whenever the value of an object's property changes. This will most prominently cause the overlay to be updated (if it exists). + void propagateUpdate(); + private: + Kolf::Overlay* m_overlay; + QList m_shapes; }; //WARNING: pos() is at center (not at top-left edge of bounding rect!) --- trunk/KDE/kdegames/kolf/game.cpp #1191707:1191708 @@ -20,7 +20,9 @@ #include "game.h" #include "itemfactory.h" #include "kcomboboxdialog.h" +#include "overlay.h" #include "rtti.h" +#include "shape.h" #include "tagaro/board.h" @@ -1071,10 +1073,16 @@ { const int diameter = 16; setSize(QSizeF(diameter, diameter)); + addShape(new Kolf::CircleShape(diameter / 2)); setZValue(998.1); } +Kolf::Overlay* Cup::createOverlay() +{ + return new Kolf::Overlay(this, this); +} + bool Cup::place(Ball *ball, bool /*wasCenter*/) { ball->setState(Holed); @@ -1674,7 +1682,9 @@ endItem->setVisible(true); setPen(QPen(Qt::darkRed, 3)); - setLine(-15, 10, 15, -5); + HintedLineItem::setLine(QLineF(-15, 10, 15, -5)); + shape = new Kolf::LineShape(line()); + addShape(shape); moveBy(0, 0); @@ -1777,6 +1787,7 @@ const bool debugPoints = false; editing = changed; + CanvasItem::editModeChanged(editing); startItem->setZValue(zValue() + .002); endItem->setZValue(zValue() + .001); @@ -1795,6 +1806,17 @@ moveBy(0, 0); } +void Wall::setLine(const QLineF& line) +{ + HintedLineItem::setLine(line); + shape->setLine(line); +} + +Kolf::Overlay* Wall::createOverlay() +{ + return new Kolf::Overlay(this, this); +} + bool Wall::collision(Ball *ball, long int id) { if (ball->curVector().magnitude() <= 0) @@ -2300,7 +2322,7 @@ void KolfGame::addBorderWall(const QPoint &start, const QPoint &end) { Wall *wall = new Wall(courseBoard); - wall->setLine(start.x(), start.y(), end.x(), end.y()); + wall->setLine(QLineF(start, end)); wall->setVisible(true); wall->setGame(this); wall->setZValue(998.7); --- trunk/KDE/kdegames/kolf/game.h #1191707:1191708 @@ -37,6 +37,10 @@ class KolfGame; class KGameRenderer; +namespace Kolf +{ + class LineShape; +}; namespace Tagaro { class Board; @@ -264,6 +268,7 @@ public: Cup(QGraphicsItem *parent); + virtual Kolf::Overlay* createOverlay(); virtual bool place(Ball *ball, bool wasCenter); virtual bool canBeMovedByOthers() const { return true; } virtual bool collision(Ball *ball, long int id); @@ -401,7 +406,7 @@ // must reimp because we gotta move the end items, // and we do that in moveBy() - virtual void setPoints(double xa, double ya, double xb, double yb) { setLine(xa, ya, xb, yb); moveBy(0, 0); } + virtual void setPoints(double xa, double ya, double xb, double yb) { setLine(QLineF(xa, ya, xb, yb)); moveBy(0, 0); } virtual QList moveableItems() const; virtual void setGame(KolfGame *game); @@ -413,11 +418,16 @@ QPoint endPoint() const { return line().p2().toPoint(); } void doAdvance(); + + void setLine(const QLineF& line); + protected: WallPoint *startItem; WallPoint *endItem; bool editing; + Kolf::LineShape* shape; + virtual Kolf::Overlay* createOverlay(); private: long int lastId;