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 "MapViewToolBox.h" 21 #include "Model/EditorContext.h" 22 #include "View/ClipTool.h" 23 #include "View/CreateComplexBrushTool.h" 24 #include "View/CreateEntityTool.h" 25 #include "View/CreateSimpleBrushTool.h" 26 #include "View/MapDocument.h" 27 #include "View/MoveObjectsTool.h" 28 #include "View/ResizeBrushesTool.h" 29 #include "View/RotateObjectsTool.h" 30 #include "View/SelectionTool.h" 31 #include "View/VertexTool.h" 32 33 namespace TrenchBroom { 34 namespace View { MapViewToolBox(MapDocumentWPtr document,wxBookCtrlBase * bookCtrl)35 MapViewToolBox::MapViewToolBox(MapDocumentWPtr document, wxBookCtrlBase* bookCtrl) : 36 m_document(document), 37 m_clipTool(NULL), 38 m_createComplexBrushTool(NULL), 39 m_createEntityTool(NULL), 40 m_createSimpleBrushTool(NULL), 41 m_moveObjectsTool(NULL), 42 m_resizeBrushesTool(NULL), 43 m_rotateObjectsTool(NULL), 44 m_vertexTool(NULL) { 45 createTools(document, bookCtrl); 46 bindObservers(); 47 } 48 ~MapViewToolBox()49 MapViewToolBox::~MapViewToolBox() { 50 unbindObservers(); 51 destroyTools(); 52 } 53 clipTool()54 ClipTool* MapViewToolBox::clipTool() { 55 return m_clipTool; 56 } 57 createComplexBrushTool()58 CreateComplexBrushTool* MapViewToolBox::createComplexBrushTool() { 59 return m_createComplexBrushTool; 60 } 61 createEntityTool()62 CreateEntityTool* MapViewToolBox::createEntityTool() { 63 return m_createEntityTool; 64 } 65 createSimpleBrushTool()66 CreateSimpleBrushTool* MapViewToolBox::createSimpleBrushTool() { 67 return m_createSimpleBrushTool; 68 } 69 moveObjectsTool()70 MoveObjectsTool* MapViewToolBox::moveObjectsTool() { 71 return m_moveObjectsTool; 72 } 73 resizeBrushesTool()74 ResizeBrushesTool* MapViewToolBox::resizeBrushesTool() { 75 return m_resizeBrushesTool; 76 } 77 rotateObjectsTool()78 RotateObjectsTool* MapViewToolBox::rotateObjectsTool() { 79 return m_rotateObjectsTool; 80 } 81 vertexTool()82 VertexTool* MapViewToolBox::vertexTool() { 83 return m_vertexTool; 84 } 85 toggleCreateComplexBrushTool()86 void MapViewToolBox::toggleCreateComplexBrushTool() { 87 toggleTool(m_createComplexBrushTool); 88 } 89 createComplexBrushToolActive() const90 bool MapViewToolBox::createComplexBrushToolActive() const { 91 return toolActive(m_createComplexBrushTool); 92 } 93 performCreateComplexBrush()94 void MapViewToolBox::performCreateComplexBrush() { 95 m_createComplexBrushTool->createBrush(); 96 } 97 toggleClipTool()98 void MapViewToolBox::toggleClipTool() { 99 toggleTool(m_clipTool); 100 } 101 clipToolActive() const102 bool MapViewToolBox::clipToolActive() const { 103 return toolActive(m_clipTool); 104 } 105 toggleClipSide()106 void MapViewToolBox::toggleClipSide() { 107 assert(clipToolActive()); 108 m_clipTool->toggleSide(); 109 } 110 performClip()111 void MapViewToolBox::performClip() { 112 assert(clipToolActive()); 113 m_clipTool->performClip(); 114 } 115 removeLastClipPoint()116 void MapViewToolBox::removeLastClipPoint() { 117 assert(clipToolActive()); 118 m_clipTool->removeLastPoint(); 119 } 120 toggleRotateObjectsTool()121 void MapViewToolBox::toggleRotateObjectsTool() { 122 toggleTool(m_rotateObjectsTool); 123 } 124 rotateObjectsToolActive() const125 bool MapViewToolBox::rotateObjectsToolActive() const { 126 return toolActive(m_rotateObjectsTool); 127 } 128 rotateToolAngle() const129 double MapViewToolBox::rotateToolAngle() const { 130 assert(rotateObjectsToolActive()); 131 return m_rotateObjectsTool->angle(); 132 } 133 rotateToolCenter() const134 const Vec3 MapViewToolBox::rotateToolCenter() const { 135 assert(rotateObjectsToolActive()); 136 return m_rotateObjectsTool->rotationCenter(); 137 } 138 moveRotationCenter(const Vec3 & delta)139 void MapViewToolBox::moveRotationCenter(const Vec3& delta) { 140 assert(rotateObjectsToolActive()); 141 const Vec3 center = m_rotateObjectsTool->rotationCenter(); 142 m_rotateObjectsTool->setRotationCenter(center + delta); 143 } 144 toggleVertexTool()145 void MapViewToolBox::toggleVertexTool() { 146 toggleTool(m_vertexTool); 147 } 148 vertexToolActive() const149 bool MapViewToolBox::vertexToolActive() const { 150 return toolActive(m_vertexTool); 151 } 152 moveVertices(const Vec3 & delta)153 void MapViewToolBox::moveVertices(const Vec3& delta) { 154 assert(vertexToolActive()); 155 m_vertexTool->moveVerticesAndRebuildBrushGeometry(delta); 156 } 157 createTools(MapDocumentWPtr document,wxBookCtrlBase * bookCtrl)158 void MapViewToolBox::createTools(MapDocumentWPtr document, wxBookCtrlBase* bookCtrl) { 159 m_clipTool = new ClipTool(document); 160 m_createComplexBrushTool = new CreateComplexBrushTool(document); 161 m_createEntityTool = new CreateEntityTool(document); 162 m_createSimpleBrushTool = new CreateSimpleBrushTool(document); 163 m_moveObjectsTool = new MoveObjectsTool(document); 164 m_resizeBrushesTool = new ResizeBrushesTool(document); 165 m_rotateObjectsTool = new RotateObjectsTool(document); 166 m_vertexTool = new VertexTool(document); 167 168 deactivateWhen(m_createComplexBrushTool, m_moveObjectsTool); 169 deactivateWhen(m_createComplexBrushTool, m_resizeBrushesTool); 170 deactivateWhen(m_createComplexBrushTool, m_createSimpleBrushTool); 171 deactivateWhen(m_rotateObjectsTool, m_moveObjectsTool); 172 deactivateWhen(m_rotateObjectsTool, m_resizeBrushesTool); 173 deactivateWhen(m_rotateObjectsTool, m_createSimpleBrushTool); 174 deactivateWhen(m_vertexTool, m_moveObjectsTool); 175 deactivateWhen(m_vertexTool, m_resizeBrushesTool); 176 deactivateWhen(m_vertexTool, m_createSimpleBrushTool); 177 deactivateWhen(m_clipTool, m_moveObjectsTool); 178 deactivateWhen(m_clipTool, m_resizeBrushesTool); 179 deactivateWhen(m_clipTool, m_createSimpleBrushTool); 180 181 registerTool(m_moveObjectsTool, bookCtrl); 182 registerTool(m_rotateObjectsTool, bookCtrl); 183 registerTool(m_resizeBrushesTool, bookCtrl); 184 registerTool(m_createComplexBrushTool, bookCtrl); 185 registerTool(m_clipTool, bookCtrl); 186 registerTool(m_vertexTool, bookCtrl); 187 registerTool(m_createEntityTool, bookCtrl); 188 registerTool(m_createSimpleBrushTool, bookCtrl); 189 } 190 destroyTools()191 void MapViewToolBox::destroyTools() { 192 delete m_vertexTool; 193 delete m_rotateObjectsTool; 194 delete m_resizeBrushesTool; 195 delete m_moveObjectsTool; 196 delete m_createSimpleBrushTool; 197 delete m_createEntityTool; 198 delete m_createComplexBrushTool; 199 delete m_clipTool; 200 } 201 registerTool(Tool * tool,wxBookCtrlBase * bookCtrl)202 void MapViewToolBox::registerTool(Tool* tool, wxBookCtrlBase* bookCtrl) { 203 tool->createPage(bookCtrl); 204 addTool(tool); 205 } 206 bindObservers()207 void MapViewToolBox::bindObservers() { 208 toolActivatedNotifier.addObserver(this, &MapViewToolBox::toolActivated); 209 toolDeactivatedNotifier.addObserver(this, &MapViewToolBox::toolDeactivated); 210 } 211 unbindObservers()212 void MapViewToolBox::unbindObservers() { 213 toolActivatedNotifier.removeObserver(this, &MapViewToolBox::toolActivated); 214 toolDeactivatedNotifier.removeObserver(this, &MapViewToolBox::toolDeactivated); 215 } 216 toolActivated(Tool * tool)217 void MapViewToolBox::toolActivated(Tool* tool) { 218 updateEditorContext(); 219 tool->showPage(); 220 } 221 toolDeactivated(Tool * tool)222 void MapViewToolBox::toolDeactivated(Tool* tool) { 223 updateEditorContext(); 224 m_moveObjectsTool->showPage(); 225 } 226 updateEditorContext()227 void MapViewToolBox::updateEditorContext() { 228 MapDocumentSPtr document = lock(m_document); 229 Model::EditorContext& editorContext = document->editorContext(); 230 editorContext.setBlockSelection(createComplexBrushToolActive()); 231 } 232 } 233 } 234