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 #ifndef TrenchBroom_MapDocumentCommandFacade
21 #define TrenchBroom_MapDocumentCommandFacade
22 
23 #include "TrenchBroom.h"
24 #include "VecMath.h"
25 #include "Model/EntityAttributeSnapshot.h"
26 #include "Model/EntityColor.h"
27 #include "Model/Node.h"
28 #include "View/CommandProcessor.h"
29 #include "View/MapDocument.h"
30 #include "View/UndoableCommand.h"
31 
32 namespace TrenchBroom {
33     namespace Model {
34         class ChangeBrushFaceAttributesRequest;
35         class Snapshot;
36     }
37 
38     namespace View {
39         class VertexHandleManager;
40 
41         class MapDocumentCommandFacade : public MapDocument {
42         private:
43             CommandProcessor m_commandProcessor;
44         public:
45             static MapDocumentSPtr newMapDocument();
46         private:
47             MapDocumentCommandFacade();
48         public: // selection modification
49             void performSelect(const Model::NodeList& nodes);
50             void performSelect(const Model::BrushFaceList& faces);
51             void performSelectAllNodes();
52             void performSelectAllBrushFaces();
53             void performConvertToBrushFaceSelection();
54 
55             void performDeselect(const Model::NodeList& nodes);
56             void performDeselect(const Model::BrushFaceList& faces);
57             void performDeselectAll();
58         private:
59             void deselectAllNodes();
60             void deselectAllBrushFaces();
61         public: // adding and removing nodes
62             Model::NodeList performAddNodes(const Model::ParentChildrenMap& nodes);
63             Model::ParentChildrenMap performRemoveNodes(const Model::NodeList& nodes);
64         private:
65             void addEmptyNodes(Model::ParentChildrenMap& nodes) const;
66             Model::NodeList collectEmptyNodes(const Model::ParentChildrenMap& nodes) const;
67             void removeEmptyNodes(Model::ParentChildrenMap& nodes, const Model::NodeList& emptyNodes) const;
68         public: // reparenting nodes
69             struct ReparentResult {
70                 Model::ParentChildrenMap movedNodes;
71                 Model::ParentChildrenMap removedNodes;
72                 ReparentResult(const Model::ParentChildrenMap& i_movedNodes, const Model::ParentChildrenMap& i_removedNodes);
73             };
74 
75             typedef enum {
76                 RemoveEmptyNodes,
77                 KeepEmptyNodes
78             } EmptyNodePolicy;
79 
80             ReparentResult performReparentNodes(const Model::ParentChildrenMap& nodes, EmptyNodePolicy emptyNodePolicy);
81         private:
82             Model::NodeList findRemovableEmptyParentNodes(const Model::ParentChildrenMap& nodes) const;
83         public: // Node Visibility
84             Model::VisibilityMap setVisibilityState(const Model::NodeList& nodes, Model::VisibilityState visibilityState);
85             Model::VisibilityMap setVisibilityEnsured(const Model::NodeList& nodes);
86             void restoreVisibilityState(const Model::VisibilityMap& nodes);
87             Model::LockStateMap setLockState(const Model::NodeList& nodes, Model::LockState lockState);
88             void restoreLockState(const Model::LockStateMap& nodes);
89         private:  // groups
90             class RenameGroupsVisitor;
91             class UndoRenameGroupsVisitor;
92         public:
93             Model::GroupNameMap performRenameGroups(const String& newName);
94             void performUndoRenameGroups(const Model::GroupNameMap& newNames);
95 
96             void performPushGroup(Model::Group* group);
97             void performPopGroup();
98         public: // transformation
99             void performTransform(const Mat4x4& transform, bool lockTextures);
100         public: // entity attributes
101             Model::EntityAttributeSnapshot::Map performSetAttribute(const Model::AttributeName& name, const Model::AttributeValue& value);
102             Model::EntityAttributeSnapshot::Map performRemoveAttribute(const Model::AttributeName& name);
103             Model::EntityAttributeSnapshot::Map performConvertColorRange(const Model::AttributeName& name, Assets::ColorRange::Type colorRange);
104             void performRenameAttribute(const Model::AttributeName& oldName, const Model::AttributeName& newName);
105             void restoreAttributes(const Model::EntityAttributeSnapshot::Map& attributes);
106         public: // brush resizing
107             bool performResizeBrushes(const Model::BrushFaceList& faces, const Vec3& delta);
108         public: // brush face attributes
109             void performMoveTextures(const Vec3f& cameraUp, const Vec3f& cameraRight, const Vec2f& delta);
110             void performRotateTextures(float angle);
111             void performShearTextures(const Vec2f& factors);
112             void performChangeBrushFaceAttributes(const Model::ChangeBrushFaceAttributesRequest& request);
113         public: // vertices
114             Model::Snapshot* performFindPlanePoints();
115             Vec3::List performSnapVertices(const Model::BrushVerticesMap& vertices, size_t snapTo);
116             Vec3::List performMoveVertices(const Model::BrushVerticesMap& vertices, const Vec3& delta);
117             Edge3::List performMoveEdges(const Model::BrushEdgesMap& edges, const Vec3& delta);
118             Polygon3::List performMoveFaces(const Model::BrushFacesMap& faces, const Vec3& delta);
119             Vec3::List performSplitEdges(const Model::BrushEdgesMap& edges, const Vec3& delta);
120             Vec3::List performSplitFaces(const Model::BrushFacesMap& faces, const Vec3& delta);
121         private: // implement MapDocument operations
122             void performRebuildBrushGeometry(const Model::BrushList& brushes);
123         public: // snapshots and restoration
124             void restoreSnapshot(Model::Snapshot* snapshot);
125         public: // entity definition file management
126             void performSetEntityDefinitionFile(const Assets::EntityDefinitionFileSpec& spec);
127         public: // texture collection management
128             void performAddExternalTextureCollections(const StringList& names);
129             void performRemoveExternalTextureCollections(const StringList& names);
130             void performMoveExternalTextureCollectionUp(const String& name);
131             void performMoveExternalTextureCollectionDown(const String& name);
132         public: // mods management
133             void performSetMods(const StringList& mods);
134         private:
135             void doSetIssueHidden(Model::Issue* issue, bool hidden);
136         public: // modification count
137             void incModificationCount(size_t delta = 1);
138             void decModificationCount(size_t delta = 1);
139         private: // notification
140             void bindObservers();
141             void documentWasNewed(MapDocument* document);
142             void documentWasLoaded(MapDocument* document);
143         private: // implement MapDocument interface
144             bool doCanUndoLastCommand() const;
145             bool doCanRedoNextCommand() const;
146             const String& doGetLastCommandName() const;
147             const String& doGetNextCommandName() const;
148             void doUndoLastCommand();
149             void doRedoNextCommand();
150             bool doRepeatLastCommands();
151             void doClearRepeatableCommands();
152 
153             void doBeginTransaction(const String& name);
154             void doEndTransaction();
155             void doRollbackTransaction();
156 
157             bool doSubmit(UndoableCommand::Ptr command);
158         };
159     }
160 }
161 
162 #endif /* defined(TrenchBroom_MapDocumentCommandFacade) */
163