1 /*
2  * createobjecttool.h
3  * Copyright 2010, Thorbjørn Lindeijer <thorbjorn@lindeijer.nl>
4  *
5  * This file is part of Tiled.
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the Free
9  * Software Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #pragma once
22 
23 #include "abstractobjecttool.h"
24 
25 #include <memory>
26 
27 namespace Tiled {
28 
29 class Tile;
30 
31 class MapObjectItem;
32 class ObjectGroupItem;
33 
34 class CreateObjectTool : public AbstractObjectTool
35 {
36     Q_OBJECT
37 
38 public:
39     CreateObjectTool(Id id, QObject *parent = nullptr);
40     ~CreateObjectTool() override;
41 
42     void activate(MapScene *scene) override;
43     void deactivate(MapScene *scene) override;
44 
45     void keyPressed(QKeyEvent *event) override;
46     void mouseEntered() override;
47     void mouseLeft() override;
48     void mouseMoved(const QPointF &pos,
49                     Qt::KeyboardModifiers modifiers) override;
50     void mousePressed(QGraphicsSceneMouseEvent *event) override;
51     void mouseReleased(QGraphicsSceneMouseEvent *event) override;
52     void modifiersChanged(Qt::KeyboardModifiers modifiers) override;
53 
54 protected:
55     void changeEvent(const ChangeEvent &event) override;
56 
57     void updateEnabledState() override;
58 
59     enum State {
60         Idle,
61         Preview,
62         CreatingObject,
63     };
64 
65     virtual void mouseMovedWhileCreatingObject(const QPointF &pos,
66                                                Qt::KeyboardModifiers modifiers);
67 
68     virtual bool startNewMapObject(const QPointF &pos, ObjectGroup *objectGroup);
69     virtual MapObject *createNewMapObject() = 0;
70     virtual void cancelNewMapObject();
71     virtual void finishNewMapObject();
72     virtual std::unique_ptr<MapObject> clearNewMapObjectItem();
73 
state()74     State state() const { return mState; }
setState(State state)75     void setState(State state) { mState = state; }
76 
newMapObjectGroup()77     ObjectGroup *newMapObjectGroup() { return mNewMapObjectGroup.get(); }
objectGroupItem()78     ObjectGroupItem *objectGroupItem() { return mObjectGroupItem.get(); }
79 
80     MapObjectItem *mNewMapObjectItem;   // owned by mObjectGroupItem if set
81 
82 private:
83     void objectGroupChanged(const ObjectGroupChangeEvent &event);
84     void updateNewObjectGroupItemPos();
85 
86     void tryCreatePreview(const QPointF &scenePos,
87                           Qt::KeyboardModifiers modifiers);
88 
89     State mState = Idle;
90     QPointF mLastScenePos;
91     Qt::KeyboardModifiers mLastModifiers = Qt::NoModifier;
92     std::unique_ptr<ObjectGroup> mNewMapObjectGroup;
93     std::unique_ptr<ObjectGroupItem> mObjectGroupItem;
94 };
95 
96 } // namespace Tiled
97