1 /*
2 * createtextobjecttool.cpp
3 * Copyright 2017, Thorbjørn Lindeijer <bjorn@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 #include "createtextobjecttool.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 namespace Tiled {
32
CreateTextObjectTool(QObject * parent)33 CreateTextObjectTool::CreateTextObjectTool(QObject *parent)
34 : CreateObjectTool("CreateTextObjectTool", parent)
35 {
36 QIcon icon(QLatin1String(":images/24/insert-text.png"));
37 icon.addFile(QLatin1String(":images/48/insert-text.png"));
38 setIcon(icon);
39 setShortcut(Qt::Key_E);
40 Utils::setThemeIcon(this, "insert-text");
41 languageChangedImpl();
42 }
43
mouseMovedWhileCreatingObject(const QPointF & pos,Qt::KeyboardModifiers modifiers)44 void CreateTextObjectTool::mouseMovedWhileCreatingObject(const QPointF &pos, Qt::KeyboardModifiers modifiers)
45 {
46 MapObject *newMapObject = mNewMapObjectItem->mapObject();
47 const QPointF halfSize(newMapObject->width() / 2, newMapObject->height() / 2);
48 const QRectF screenBounds { pos - halfSize, newMapObject->size() };
49
50 // These screenBounds assume TopLeft alignment, but the map's object alignment might be different.
51 const QPointF offset = alignmentOffset(screenBounds, newMapObject->alignment(mapDocument()->map()));
52
53 const MapRenderer *renderer = mapDocument()->renderer();
54 QPointF pixelCoords = renderer->screenToPixelCoords(screenBounds.topLeft() + offset);
55
56 SnapHelper(renderer, modifiers).snap(pixelCoords);
57
58 newMapObject->setPosition(pixelCoords);
59 mNewMapObjectItem->syncWithMapObject();
60 }
61
languageChanged()62 void CreateTextObjectTool::languageChanged()
63 {
64 CreateObjectTool::languageChanged();
65 languageChangedImpl();
66 }
67
languageChangedImpl()68 void CreateTextObjectTool::languageChangedImpl()
69 {
70 setName(tr("Insert Text"));
71 }
72
createNewMapObject()73 MapObject *CreateTextObjectTool::createNewMapObject()
74 {
75 TextData textData;
76 textData.text = tr("Hello World");
77
78 MapObject *newMapObject = new MapObject;
79 newMapObject->setShape(MapObject::Text);
80 newMapObject->setTextData(textData);
81 newMapObject->setSize(textData.textSize());
82 return newMapObject;
83 }
84
85 } // namespace Tiled
86
87 #include "moc_createtextobjecttool.cpp"
88