Debian Codegraph @

radiant/xyview/tools/BrushCreatorTool.cpp

raw
1 #include "BrushCreatorTool.h"
2
3 #include "i18n.h"
4 #include "igrid.h"
5 #include "iclipper.h"
6 #include "icommandsystem.h"
7 #include "scenelib.h"
8 #include "selectionlib.h"
9 #include "command/ExecutionNotPossible.h"
10 #include "XYMouseToolEvent.h"
11 #include "ui/texturebrowser/TextureBrowserPanel.h"
12 #include "ui/texturebrowser/TextureBrowserManager.h"
13
14 namespace ui
15 {
16
17 const std::string& BrushCreatorTool::getName()
18 {
19 static std::string name("BrushCreatorTool");
20 return name;
21 }
22
23 const std::string& BrushCreatorTool::getDisplayName()
24 {
25 static std::string displayName(_("Drag-create Brush"));
26 return displayName;
27 }
28
29 MouseTool::Result BrushCreatorTool::onMouseDown(Event& ev)
30 {
31 try
32 {
33 if (GlobalClipper().clipMode() || GlobalMapModule().getEditMode() == IMap::EditMode::Merge)
34 {
35 return Result::Ignored; // no brush creation in clip or merge mode
36 }
37
38 // We only operate on XY view events, so attempt to cast
39 XYMouseToolEvent& xyEvent = dynamic_cast<XYMouseToolEvent&>(ev);
40
41 if (GlobalSelectionSystem().countSelected() == 0)
42 {
43 _brush.reset();
44 _startPos = xyEvent.getWorldPos();
45 GlobalUndoSystem().start();
46
47 return Result::Activated;
48 }
49 }
50 catch (std::bad_cast&)
51 {
52 }
53
54 return Result::Ignored; // not handled
55 }
56
57 MouseTool::Result BrushCreatorTool::onMouseMove(Event& ev)
58 {
59 try
60 {
61 // We only operate on XY view events, so attempt to cast
62 XYMouseToolEvent& xyEvent = dynamic_cast<XYMouseToolEvent&>(ev);
63
64 Vector3 startPos = _startPos;
65 xyEvent.getView().snapToGrid(startPos);
66
67 Vector3 endPos = xyEvent.getWorldPos();
68 xyEvent.getView().snapToGrid(endPos);
69
70 int nDim = (xyEvent.getViewType() == OrthoOrientation::XY) ? 2 : (xyEvent.getViewType() == OrthoOrientation::YZ) ? 0 : 1;
71
72 const selection::WorkZone& wz = GlobalSelectionSystem().getWorkZone();
73
74 startPos[nDim] = float_snapped(wz.min[nDim], GlobalGrid().getGridSize());
75 endPos[nDim] = float_snapped(wz.max[nDim], GlobalGrid().getGridSize());
76
77 if (endPos[nDim] <= startPos[nDim])
78 {
79 endPos[nDim] = startPos[nDim] + GlobalGrid().getGridSize();
80 }
81
82 for (int i = 0; i < 3; i++)
83 {
84 if (startPos[i] == endPos[i])
85 {
86 return Result::Continued; // don't create a degenerate brush
87 }
88
89 if (startPos[i] > endPos[i])
90 {
91 std::swap(startPos[i], endPos[i]);
92 }
93 }
94
95 if (!_brush)
96 {
97 // greebo: Create a new brush
98 _brush = GlobalBrushCreator().createBrush();
99
100 if (_brush)
101 {
102 // Brush was created
103 // Insert the brush into worldspawn
104 auto worldspawn = GlobalMapModule().findOrInsertWorldspawn();
105 scene::addNodeToContainer(_brush, worldspawn);
106 }
107
108 // Make sure the brush is selected
109 Node_setSelected(_brush, true);
110 }
111
112 // It's possible that the brush we created has been deleted in the meantime
113 // check the brush object for validity
114 if (!_brush->getParent() || GlobalSelectionSystem().countSelected() == 0)
115 {
116 _brush.reset();
117 return Result::Finished;
118 }
119
120 // Dispatch the command
121 GlobalCommandSystem().executeCommand(
122 "ResizeSelectedBrushesToBounds",
123 {startPos, endPos, GlobalTextureBrowser().getSelectedShader()}
124 );
125 }
126 catch (cmd::ExecutionNotPossible&)
127 {
128 return Result::Ignored;
129 }
130 catch (std::bad_cast&)
131 {
132 return Result::Ignored;
133 }
134
135 return Result::Continued;
136 }
137
138 MouseTool::Result BrushCreatorTool::onMouseUp(Event& ev)
139 {
140 try
141 {
142 // We only operate on XY view events, so attempt to cast
143 dynamic_cast<XYMouseToolEvent&>(ev).getScale();
144
145 GlobalUndoSystem().finish("brushDragNew");
146 _brush.reset();
147
148 return Result::Finished;
149 }
150 catch (std::bad_cast&)
151 {
152 return Result::Ignored;
153 }
154 }
155
156 BrushCreatorTool::Result BrushCreatorTool::onCancel(IInteractiveView& view)
157 {
158 if (_brush)
159 {
160 // We have a WIP brush object, kill it
161 scene::removeNodeFromParent(_brush);
162 GlobalUndoSystem().cancel();
163
164 _brush.reset();
165 }
166
167 return Result::Finished;
168 }
169
170 void BrushCreatorTool::onMouseCaptureLost(IInteractiveView& view)
171 {
172 onCancel(view);
173 }
174
175 unsigned int BrushCreatorTool::getPointerMode()
176 {
177 return PointerMode::Capture;
178 }
179
180 unsigned int BrushCreatorTool::getRefreshMode()
181 {
182 return RefreshMode::Force | RefreshMode::AllViews; // update cam view too
183 }
184
185 } // namespace