1 /*
2  Copyright (C) 2010-2014 Kristian Duske
3 
4  This file is part of TrenchBroom.
5 
6  TrenchBroom is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  TrenchBroom is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with TrenchBroom. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "MoveObjectsTool.h"
21 
22 #include "Model/Brush.h"
23 #include "Model/ComputeNodeBoundsVisitor.h"
24 #include "Model/Entity.h"
25 #include "Model/Group.h"
26 #include "Model/HitAdapter.h"
27 #include "Model/HitQuery.h"
28 #include "Model/PickResult.h"
29 #include "Renderer/RenderContext.h"
30 #include "View/Grid.h"
31 #include "View/InputState.h"
32 #include "View/MapDocument.h"
33 #include "View/MoveObjectsToolPage.h"
34 
35 #include <cassert>
36 
37 namespace TrenchBroom {
38     namespace View {
MoveObjectsTool(MapDocumentWPtr document)39         MoveObjectsTool::MoveObjectsTool(MapDocumentWPtr document) :
40         Tool(true),
41         m_document(document),
42         m_duplicateObjects(false) {}
43 
grid() const44         const Grid& MoveObjectsTool::grid() const {
45             return lock(m_document)->grid();
46         }
47 
startMove(const InputState & inputState)48         bool MoveObjectsTool::startMove(const InputState& inputState) {
49             MapDocumentSPtr document = lock(m_document);
50             document->beginTransaction(duplicateObjects(inputState) ? "Duplicate Objects" : "Move Objects");
51             m_duplicateObjects = duplicateObjects(inputState);
52             return true;
53         }
54 
move(const InputState & inputState,const Vec3 & delta)55         MoveObjectsTool::MoveResult MoveObjectsTool::move(const InputState& inputState, const Vec3& delta) {
56             MapDocumentSPtr document = lock(m_document);
57             const BBox3& worldBounds = document->worldBounds();
58             const BBox3 bounds = document->selectionBounds();
59             if (!worldBounds.contains(bounds.translated(delta)))
60                 return MR_Deny;
61 
62             if (m_duplicateObjects) {
63                 m_duplicateObjects = false;
64                 if (!document->duplicateObjects())
65                     return MR_Cancel;
66             }
67 
68             if (!document->translateObjects(delta))
69                 return MR_Deny;
70             return MR_Continue;
71         }
72 
endMove(const InputState & inputState)73         void MoveObjectsTool::endMove(const InputState& inputState) {
74             MapDocumentSPtr document = lock(m_document);
75             document->commitTransaction();
76         }
77 
cancelMove()78         void MoveObjectsTool::cancelMove() {
79             MapDocumentSPtr document = lock(m_document);
80             document->cancelTransaction();
81         }
82 
duplicateObjects(const InputState & inputState) const83         bool MoveObjectsTool::duplicateObjects(const InputState& inputState) const {
84             return inputState.modifierKeysDown(ModifierKeys::MKCtrlCmd);
85         }
86 
doCreatePage(wxWindow * parent)87         wxWindow* MoveObjectsTool::doCreatePage(wxWindow* parent) {
88             return new MoveObjectsToolPage(parent, m_document);
89         }
90     }
91 }
92