1 /*
2 * createtileobjecttool.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 "createtileobjecttool.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 "tile.h"
30 #include "utils.h"
31
32 using namespace Tiled;
33
CreateTileObjectTool(QObject * parent)34 CreateTileObjectTool::CreateTileObjectTool(QObject *parent)
35 : CreateObjectTool("CreateTileObjectTool", parent)
36 {
37 QIcon icon(QLatin1String(":images/24/insert-image.png"));
38 icon.addFile(QLatin1String(":images/48/insert-image.png"));
39 setIcon(icon);
40 setShortcut(Qt::Key_T);
41 Utils::setThemeIcon(this, "insert-image");
42 languageChangedImpl();
43 }
44
mouseMovedWhileCreatingObject(const QPointF & pos,Qt::KeyboardModifiers modifiers)45 void CreateTileObjectTool::mouseMovedWhileCreatingObject(const QPointF &pos, Qt::KeyboardModifiers modifiers)
46 {
47 const QSize imgSize = mNewMapObjectItem->mapObject()->cell().tile()->size();
48 const QPointF halfSize(imgSize.width() / 2, imgSize.height() / 2);
49 const QRectF screenBounds { pos - halfSize, imgSize };
50
51 // These screenBounds assume TopLeft alignment, but the map's object alignment might be different.
52 MapObject *newMapObject = mNewMapObjectItem->mapObject();
53 const QPointF offset = alignmentOffset(screenBounds, newMapObject->alignment(mapDocument()->map()));
54
55 const MapRenderer *renderer = mapDocument()->renderer();
56 QPointF pixelCoords = renderer->screenToPixelCoords(screenBounds.topLeft() + offset);
57
58 SnapHelper(renderer, modifiers).snap(pixelCoords);
59
60 newMapObject->setPosition(pixelCoords);
61 mNewMapObjectItem->syncWithMapObject();
62 }
63
languageChanged()64 void CreateTileObjectTool::languageChanged()
65 {
66 CreateObjectTool::languageChanged();
67 languageChangedImpl();
68 }
69
languageChangedImpl()70 void CreateTileObjectTool::languageChangedImpl()
71 {
72 setName(tr("Insert Tile"));
73 }
74
createNewMapObject()75 MapObject *CreateTileObjectTool::createNewMapObject()
76 {
77 if (!tile())
78 return nullptr;
79
80 MapObject *newMapObject = new MapObject;
81 newMapObject->setShape(MapObject::Rectangle);
82 newMapObject->setCell(Cell(tile()));
83 newMapObject->setSize(tile()->size());
84 return newMapObject;
85 }
86
87 #include "moc_createtileobjecttool.cpp"
88