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