| 1 |
#include "MeasurementTool.h" |
| 2 |
|
| 3 |
#include "i18n.h" |
| 4 |
#include "iclipper.h" |
| 5 |
#include "icolourscheme.h" |
| 6 |
#include "math/Matrix4.h" |
| 7 |
#include "XYMouseToolEvent.h" |
| 8 |
#include "selectionlib.h" |
| 9 |
#include "string/convert.h" |
| 10 |
#include <fmt/format.h> |
| 11 |
|
| 12 |
namespace ui |
| 13 |
{ |
| 14 |
|
| 15 |
MeasurementTool::MeasurementTool() : |
| 16 |
_points(_vertices), |
| 17 |
_line(_vertices) |
| 18 |
{} |
| 19 |
|
| 20 |
const std::string& MeasurementTool::getName() |
| 21 |
{ |
| 22 |
static std::string name("MeasurementTool"); |
| 23 |
return name; |
| 24 |
} |
| 25 |
|
| 26 |
const std::string& MeasurementTool::getDisplayName() |
| 27 |
{ |
| 28 |
static std::string displayName(_("Measure")); |
| 29 |
return displayName; |
| 30 |
} |
| 31 |
|
| 32 |
MouseTool::Result MeasurementTool::onMouseDown(Event& ev) |
| 33 |
{ |
| 34 |
try |
| 35 |
{ |
| 36 |
if (GlobalClipper().clipMode()) |
| 37 |
{ |
| 38 |
return Result::Ignored; // no measurement in clip mode |
| 39 |
} |
| 40 |
|
| 41 |
// We only operate on XY view events, so attempt to cast |
| 42 |
XYMouseToolEvent& xyEvent = dynamic_cast<XYMouseToolEvent&>(ev); |
| 43 |
|
| 44 |
Vector3 clickPos = xyEvent.getWorldPos(); |
| 45 |
xyEvent.getView().snapToGrid(clickPos); |
| 46 |
|
| 47 |
Vertex3 clickVertex(clickPos); |
| 48 |
|
| 49 |
if (_vertices.empty()) |
| 50 |
{ |
| 51 |
// If we just started, allocate two points |
| 52 |
_vertices.push_back(clickVertex); |
| 53 |
_vertices.push_back(clickVertex); |
| 54 |
} |
| 55 |
else |
| 56 |
{ |
| 57 |
// Store the click position and add a new vertex |
| 58 |
_vertices[_vertices.size() - 1] = clickVertex; |
| 59 |
|
| 60 |
// Add one additional point to the chain |
| 61 |
_vertices.push_back(clickVertex); |
| 62 |
} |
| 63 |
|
| 64 |
_points.queueUpdate(); |
| 65 |
_line.queueUpdate(); |
| 66 |
|
| 67 |
return Result::Activated; |
| 68 |
} |
| 69 |
catch (std::bad_cast&) |
| 70 |
{ |
| 71 |
} |
| 72 |
|
| 73 |
return Result::Ignored; // not handled |
| 74 |
} |
| 75 |
|
| 76 |
MouseTool::Result MeasurementTool::onMouseMove(Event& ev) |
| 77 |
{ |
| 78 |
try |
| 79 |
{ |
| 80 |
// We only operate on XY view events, so attempt to cast |
| 81 |
XYMouseToolEvent& xyEvent = dynamic_cast<XYMouseToolEvent&>(ev); |
| 82 |
|
| 83 |
Vector3 endPos = xyEvent.getWorldPos(); |
| 84 |
xyEvent.getView().snapToGrid(endPos); |
| 85 |
|
| 86 |
assert(!_vertices.empty()); |
| 87 |
|
| 88 |
_vertices[_vertices.size() - 1] = endPos; |
| 89 |
_line.setColour({ 0, 0, 0, 0 }); |
| 90 |
_points.setColour({ 0, 0, 0, 0 }); |
| 91 |
} |
| 92 |
catch (std::bad_cast&) |
| 93 |
{ |
| 94 |
return Result::Ignored; |
| 95 |
} |
| 96 |
|
| 97 |
return Result::Continued; |
| 98 |
} |
| 99 |
|
| 100 |
MouseTool::Result MeasurementTool::onMouseUp(Event& ev) |
| 101 |
{ |
| 102 |
try |
| 103 |
{ |
| 104 |
// We only operate on XY view events, so attempt to cast |
| 105 |
dynamic_cast<XYMouseToolEvent&>(ev).getScale(); |
| 106 |
|
| 107 |
// We continue until the user hits ESC |
| 108 |
return Result::Continued; |
| 109 |
} |
| 110 |
catch (std::bad_cast&) |
| 111 |
{ |
| 112 |
return Result::Ignored; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
MeasurementTool::Result MeasurementTool::onCancel(IInteractiveView& view) |
| 117 |
{ |
| 118 |
_vertices.clear(); |
| 119 |
_line.clear(); |
| 120 |
_points.clear(); |
| 121 |
_texts.clear(); |
| 122 |
|
| 123 |
return Result::Finished; |
| 124 |
} |
| 125 |
|
| 126 |
void MeasurementTool::onMouseCaptureLost(IInteractiveView& view) |
| 127 |
{ |
| 128 |
onCancel(view); |
| 129 |
} |
| 130 |
|
| 131 |
unsigned int MeasurementTool::getPointerMode() |
| 132 |
{ |
| 133 |
return PointerMode::Normal; |
| 134 |
} |
| 135 |
|
| 136 |
unsigned int MeasurementTool::getRefreshMode() |
| 137 |
{ |
| 138 |
return RefreshMode::Force | RefreshMode::ActiveView; |
| 139 |
} |
| 140 |
|
| 141 |
void MeasurementTool::ensureShaders(RenderSystem& renderSystem) |
| 142 |
{ |
| 143 |
if (!_wireShader) |
| 144 |
{ |
| 145 |
Vector3 colour = GlobalColourSchemeManager().getColour("drag_selection"); |
| 146 |
|
| 147 |
_colour.x() = colour.x(); |
| 148 |
_colour.y() = colour.y(); |
| 149 |
_colour.z() = colour.z(); |
| 150 |
_colour.w() = 1; |
| 151 |
|
| 152 |
_wireShader = renderSystem.capture(ColourShaderType::OrthoviewSolid, _colour); |
| 153 |
} |
| 154 |
|
| 155 |
if (!_pointShader) |
| 156 |
{ |
| 157 |
_pointShader = renderSystem.capture(BuiltInShaderType::Point); |
| 158 |
} |
| 159 |
|
| 160 |
if (!_textRenderer) |
| 161 |
{ |
| 162 |
_textRenderer = renderSystem.captureTextRenderer(IGLFont::Style::Mono, 14); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
void MeasurementTool::render(RenderSystem& renderSystem, IRenderableCollector& collector, const VolumeTest& volume) |
| 167 |
{ |
| 168 |
ensureShaders(renderSystem); |
| 169 |
|
| 170 |
_points.update(_pointShader); |
| 171 |
_line.update(_wireShader); |
| 172 |
|
| 173 |
_texts.resize(_vertices.size() - 1); |
| 174 |
|
| 175 |
// Render distance string |
| 176 |
for (std::size_t i = 1; i < _vertices.size(); ++i) |
| 177 |
{ |
| 178 |
auto& text = _texts.at(i - 1); |
| 179 |
|
| 180 |
if (!text) |
| 181 |
{ |
| 182 |
text.reset(new render::StaticRenderableText("", { 0,0,0 }, _colour)); |
| 183 |
text->update(_textRenderer); |
| 184 |
} |
| 185 |
|
| 186 |
const auto& a = _vertices[i - 1]; |
| 187 |
const auto& b = _vertices[i]; |
| 188 |
|
| 189 |
text->setColour(_colour); |
| 190 |
text->setText(string::to_string((a - b).getLength())); |
| 191 |
text->setWorldPosition((a + b) * 0.5); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
} // namespace |