File indexing completed on 2024-05-12 04:04:35

0001 /*
0002     Copyright (C) 2002-2005, Jason Katz-Brown <jasonkb@mit.edu>
0003     Copyright 2010 Stefan Majewsky <majewsky@gmx.net>
0004 
0005     This program is free software; you can redistribute it and/or modify
0006     it under the terms of the GNU General Public License as published by
0007     the Free Software Foundation; either version 2 of the License, or
0008     (at your option) any later version.
0009 
0010     This program is distributed in the hope that it will be useful,
0011     but WITHOUT ANY WARRANTY; without even the implied warranty of
0012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013     GNU General Public License for more details.
0014 
0015     You should have received a copy of the GNU General Public License
0016     along with this program; if not, write to the Free Software
0017     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
0018 */
0019 
0020 #ifndef KOLF_OBSTACLES_H
0021 #define KOLF_OBSTACLES_H
0022 
0023 //NOTE: Only refactored stuff goes into this header.
0024 
0025 #include <config.h>
0026 #include "canvasitem.h"
0027 #include "overlay.h"
0028 
0029 #include <QPen>
0030 
0031 class QCheckBox;
0032 class QGridLayout;
0033 
0034 namespace Kolf
0035 {
0036     class LineShape;
0037     class RectShape;
0038 
0039     class Bumper : public EllipticalCanvasItem
0040     {
0041         Q_OBJECT
0042         public:
0043             Bumper(QGraphicsItem* parent, b2World* world);
0044 
0045             bool collision(Ball* ball) override;
0046         protected:
0047             Kolf::Overlay* createOverlay() override;
0048         public Q_SLOTS:
0049             void turnBumperOff();
0050     };
0051 
0052     class Wall : public QGraphicsLineItem, public CanvasItem
0053     {
0054         public:
0055             Wall(QGraphicsItem* parent, b2World* world);
0056 
0057             void load(KConfigGroup* cfgGroup) override;
0058             void save(KConfigGroup* cfgGroup) override;
0059             void setVisible(bool visible);
0060 
0061             virtual void setLine(const QLineF& line);
0062             void moveBy(double dx, double dy) override;
0063             QPointF getPosition() const override;
0064         protected:
0065             Kolf::Overlay* createOverlay() override;
0066         private:
0067             Kolf::LineShape* m_shape;
0068     };
0069 
0070     class WallOverlay : public Kolf::Overlay
0071     {
0072         Q_OBJECT
0073         public:
0074             explicit WallOverlay(Kolf::Wall* wall);
0075             void update() override;
0076         private Q_SLOTS:
0077             //interface to handles
0078             void moveHandle(const QPointF& handleScenePos);
0079         private:
0080             Kolf::OverlayHandle* m_handle1;
0081             Kolf::OverlayHandle* m_handle2;
0082     };
0083 
0084     //WARNING: WallIndex and WallFlag have to stay in sync like they are now!
0085     enum WallIndex {
0086         TopWallIndex = 0,
0087         LeftWallIndex = 1,
0088         RightWallIndex = 2,
0089         BottomWallIndex = 3,
0090         RectangleWallCount = 4
0091     };
0092 
0093     class RectangleItem : public Tagaro::SpriteObjectItem, public CanvasItem
0094     {
0095         Q_OBJECT
0096         public:
0097             RectangleItem(const QString& type, QGraphicsItem* parent, b2World* world);
0098             ~RectangleItem() override;
0099 
0100             bool hasWall(Kolf::WallIndex index) const;
0101             bool isWallAllowed(Kolf::WallIndex index) const;
0102             void setWall(Kolf::WallIndex index, bool hasWall);
0103             void setWallAllowed(Kolf::WallIndex index, bool wallAllowed);
0104             void setSize(const QSizeF& size) override;
0105             QPointF getPosition() const override;
0106             void moveBy(double dx, double dy) override;
0107 
0108             void setWallColor(const QColor& color);
0109             void applyWallStyle(Kolf::Wall* wall, bool adjustPainting = true);
0110 
0111             void load(KConfigGroup* group) override;
0112             void save(KConfigGroup* group) override;
0113 
0114             Config* config(QWidget* parent) override;
0115         Q_SIGNALS:
0116             void wallChanged(Kolf::WallIndex index, bool hasWall, bool wallAllowed);
0117         protected:
0118             Kolf::Overlay* createOverlay() override;
0119             virtual void updateWallPosition();
0120         private:
0121             QPen m_wallPen;
0122             QList<bool> m_wallAllowed;
0123             QList<Kolf::Wall*> m_walls;
0124             Kolf::RectShape* m_shape;
0125     };
0126 
0127     class RectangleOverlay : public Kolf::Overlay
0128     {
0129         Q_OBJECT
0130         public:
0131             explicit RectangleOverlay(Kolf::RectangleItem* item);
0132             void update() override;
0133         private Q_SLOTS:
0134             //interface to handles
0135             void moveHandle(const QPointF& handleScenePos);
0136         private:
0137             QList<Kolf::OverlayHandle*> m_handles;
0138     };
0139 
0140     class RectangleConfig : public Config
0141     {
0142         Q_OBJECT
0143         public:
0144             RectangleConfig(Kolf::RectangleItem* item, QWidget* parent);
0145         protected Q_SLOTS:
0146             void setWall(bool hasWall);
0147             void wallChanged(Kolf::WallIndex index, bool hasWall, bool wallAllowed);
0148         protected:
0149             QGridLayout* m_layout;
0150             QList<QCheckBox*> m_wallCheckBoxes;
0151             Kolf::RectangleItem* const m_item;
0152     };
0153 
0154     class Bridge : public Kolf::RectangleItem
0155     {
0156         public:
0157             Bridge(QGraphicsItem* parent, b2World* world);
0158             bool collision(Ball* ball) override;
0159     };
0160 
0161     class Floater : public Kolf::RectangleItem
0162     {
0163         Q_OBJECT
0164         public:
0165             Floater(QGraphicsItem* parent, b2World* world);
0166             void editModeChanged(bool changed) override;
0167             void moveBy(double dx, double dy) override;
0168 
0169             QLineF motionLine() const;
0170             void setMotionLine(const QLineF& motionLine);
0171             int speed() const;
0172             void advance(int phase) override;
0173 
0174             void load(KConfigGroup* group) override;
0175             void save(KConfigGroup* group) override;
0176         public Q_SLOTS:
0177             void setSpeed(int speed);
0178         protected:
0179             Kolf::Overlay* createOverlay() override;
0180         private:
0181             void setMlPosition(qreal position);
0182 
0183             QLineF m_motionLine;
0184             int m_speed;
0185             qreal m_velocity;
0186             qreal m_position; //parameter on motion line (see QLineF::pointAt)
0187             bool m_moveByMovesMotionLine, m_animated;
0188     };
0189 
0190     class FloaterOverlay : public Kolf::RectangleOverlay
0191     {
0192         Q_OBJECT
0193         public:
0194             explicit FloaterOverlay(Kolf::Floater* floater);
0195             void update() override;
0196         private Q_SLOTS:
0197             //interface to handles
0198             void moveMotionLineHandle(const QPointF& handleScenePos);
0199         private:
0200             Kolf::OverlayHandle* m_handle1;
0201             Kolf::OverlayHandle* m_handle2;
0202             QGraphicsLineItem* m_motionLineItem;
0203     };
0204 
0205     class Sign : public Kolf::RectangleItem
0206     {
0207         Q_OBJECT
0208         public:
0209             Sign(QGraphicsItem* parent, b2World* world);
0210 
0211             QString text() const;
0212             void setSize(const QSizeF& size) override;
0213 
0214             void load(KConfigGroup* group) override;
0215             void save(KConfigGroup* group) override;
0216         public Q_SLOTS:
0217             void setText(const QString& text);
0218         private:
0219             QString m_text;
0220             QGraphicsTextItem* m_textItem;
0221     };
0222 
0223     class Windmill : public Kolf::RectangleItem
0224     {
0225         Q_OBJECT
0226         public:
0227             Windmill(QGraphicsItem* parent, b2World* world);
0228             ~Windmill() override;
0229 
0230             bool guardAtTop() const;
0231             int speed() const;
0232             void advance(int phase) override;
0233             void moveBy(double dx, double dy) override;
0234 
0235             void load(KConfigGroup* group) override;
0236             void save(KConfigGroup* group) override;
0237         public Q_SLOTS:
0238             void setGuardAtTop(bool guardAtTop);
0239             void setSpeed(int speed);
0240         protected:
0241             void updateWallPosition() override;
0242         private:
0243             Kolf::Wall* m_leftWall;
0244             Kolf::Wall* m_rightWall;
0245             Kolf::Wall* m_guardWall;
0246             bool m_guardAtTop;
0247             int m_speed;
0248             double m_velocity;
0249     };
0250 }
0251 
0252 #endif // KOLF_OBSTACLES_H