1 /*
2 * createscalableobjecttool.cpp
3 * Copyright 2014, Martin Ziel <martin.ziel.com>
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 #include "createscalableobjecttool.h"
22
23 #include "mapdocument.h"
24 #include "mapobject.h"
25 #include "mapobjectitem.h"
26 #include "maprenderer.h"
27 #include "objectgroup.h"
28 #include "snaphelper.h"
29 #include "utils.h"
30
31 using namespace Tiled;
32
CreateScalableObjectTool(Id id,QObject * parent)33 CreateScalableObjectTool::CreateScalableObjectTool(Id id, QObject *parent)
34 : CreateObjectTool(id, parent)
35 {
36 }
37
startNewMapObject(const QPointF & pos,ObjectGroup * objectGroup)38 bool CreateScalableObjectTool::startNewMapObject(const QPointF &pos, ObjectGroup *objectGroup)
39 {
40 mStartPos = pos;
41 return CreateObjectTool::startNewMapObject(pos, objectGroup);
42 }
43
sign(qreal value)44 static qreal sign(qreal value)
45 {
46 return value < 0 ? -1 : 1;
47 }
48
mouseMovedWhileCreatingObject(const QPointF & pos,Qt::KeyboardModifiers modifiers)49 void CreateScalableObjectTool::mouseMovedWhileCreatingObject(const QPointF &pos, Qt::KeyboardModifiers modifiers)
50 {
51 const MapRenderer *renderer = mapDocument()->renderer();
52 QPointF pixelCoords = renderer->screenToPixelCoords(pos);
53
54 if (state() == Preview) {
55 SnapHelper(renderer, modifiers).snap(pixelCoords);
56 mStartPos = pixelCoords;
57 }
58
59 QRectF objectArea(mStartPos, pixelCoords);
60
61 // Holding shift creates circle or square
62 if (modifiers & Qt::ShiftModifier) {
63 qreal max = qMax(qAbs(objectArea.width()), qAbs(objectArea.height()));
64 objectArea.setWidth(max * sign(objectArea.width()));
65 objectArea.setHeight(max * sign(objectArea.height()));
66 }
67
68 // Update the position and size of the new map object
69 QPointF snapSize(objectArea.width(), objectArea.height());
70 SnapHelper(renderer, modifiers).snap(snapSize);
71 objectArea.setWidth(snapSize.x());
72 objectArea.setHeight(snapSize.y());
73
74 objectArea = objectArea.normalized();
75
76 // This objectArea assumes TopLeft alignment, but the map's object alignment might be different.
77 MapObject *newMapObject = mNewMapObjectItem->mapObject();
78 const auto offset = alignmentOffset(objectArea, newMapObject->alignment(mapDocument()->map()));
79 objectArea.translate(offset);
80
81 // Not using the MapObjectModel because the object is not actually part of
82 // the map yet
83 newMapObject->setBounds(objectArea);
84 mNewMapObjectItem->syncWithMapObject();
85 }
86
87 #include "moc_createscalableobjecttool.cpp"
88