Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Measurement Mouse Tool to orthoviews (#2303)
- Loading branch information
1 parent
e1b95d2
commit 4811361
Showing
8 changed files
with
249 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
#include "MeasurementTool.h" | ||
|
||
#include "iclipper.h" | ||
#include "iuimanager.h" | ||
#include "XYMouseToolEvent.h" | ||
#include "selectionlib.h" | ||
#include "string/convert.h" | ||
#include <boost/format.hpp> | ||
|
||
namespace ui | ||
{ | ||
|
||
MeasurementTool::MeasurementTool() : | ||
_points(GL_POINTS), | ||
_lines(GL_LINE_STRIP) | ||
{} | ||
|
||
const std::string& MeasurementTool::getName() | ||
{ | ||
static std::string name("MeasurementTool"); | ||
return name; | ||
} | ||
|
||
const std::string& MeasurementTool::getDisplayName() | ||
{ | ||
static std::string displayName(_("Measure")); | ||
return displayName; | ||
} | ||
|
||
MouseTool::Result MeasurementTool::onMouseDown(Event& ev) | ||
{ | ||
try | ||
{ | ||
if (GlobalClipper().clipMode()) | ||
{ | ||
return Result::Ignored; // no measurement in clip mode | ||
} | ||
|
||
// We only operate on XY view events, so attempt to cast | ||
XYMouseToolEvent& xyEvent = dynamic_cast<XYMouseToolEvent&>(ev); | ||
|
||
Vector3 clickPos = xyEvent.getWorldPos(); | ||
xyEvent.getView().snapToGrid(clickPos); | ||
|
||
assert(_lines.size() == _points.size()); | ||
|
||
VertexCb clickVertex(clickPos, Colour4b()); | ||
|
||
if (_points.empty()) | ||
{ | ||
// If we just started, allocate two points | ||
_points.push_back(clickVertex); | ||
_points.push_back(clickVertex); | ||
|
||
// Copy the vertices to the lines vector | ||
_lines.push_back(clickVertex); | ||
_lines.push_back(clickVertex); | ||
} | ||
else | ||
{ | ||
// Store the click position and add a new vertex | ||
_points[_points.size() - 1].vertex = clickVertex.vertex; | ||
_lines[_lines.size() - 1].vertex = clickVertex.vertex; | ||
|
||
// Add one additional point to the chain | ||
_points.push_back(clickVertex); | ||
_lines.push_back(clickVertex); | ||
} | ||
|
||
return Result::Activated; | ||
} | ||
catch (std::bad_cast&) | ||
{ | ||
} | ||
|
||
return Result::Ignored; // not handled | ||
} | ||
|
||
MouseTool::Result MeasurementTool::onMouseMove(Event& ev) | ||
{ | ||
try | ||
{ | ||
// We only operate on XY view events, so attempt to cast | ||
XYMouseToolEvent& xyEvent = dynamic_cast<XYMouseToolEvent&>(ev); | ||
|
||
Vector3 endPos = xyEvent.getWorldPos(); | ||
xyEvent.getView().snapToGrid(endPos); | ||
|
||
assert(!_points.empty()); | ||
|
||
_points[_points.size() - 1].vertex = endPos; | ||
_points.setColour(Colour4b(0, 0, 0, 0)); | ||
|
||
_lines[_lines.size() -1] = _points[_points.size() - 1]; | ||
} | ||
catch (std::bad_cast&) | ||
{ | ||
return Result::Ignored; | ||
} | ||
|
||
return Result::Continued; | ||
} | ||
|
||
MouseTool::Result MeasurementTool::onMouseUp(Event& ev) | ||
{ | ||
try | ||
{ | ||
// We only operate on XY view events, so attempt to cast | ||
dynamic_cast<XYMouseToolEvent&>(ev); | ||
|
||
// We continue until the user hits ESC | ||
return Result::Continued; | ||
} | ||
catch (std::bad_cast&) | ||
{ | ||
return Result::Ignored; | ||
} | ||
} | ||
|
||
MeasurementTool::Result MeasurementTool::onCancel(IInteractiveView& view) | ||
{ | ||
_points.clear(); | ||
_lines.clear(); | ||
|
||
return Result::Finished; | ||
} | ||
|
||
void MeasurementTool::onMouseCaptureLost(IInteractiveView& view) | ||
{ | ||
onCancel(view); | ||
} | ||
|
||
unsigned int MeasurementTool::getPointerMode() | ||
{ | ||
return PointerMode::Normal; | ||
} | ||
|
||
unsigned int MeasurementTool::getRefreshMode() | ||
{ | ||
return RefreshMode::Force | RefreshMode::ActiveView; | ||
} | ||
|
||
void MeasurementTool::ensureShaders(RenderSystem& renderSystem) | ||
{ | ||
if (!_wireShader) | ||
{ | ||
Vector3 colour = ColourSchemes().getColour("drag_selection"); | ||
|
||
_colour.x() = colour.x(); | ||
_colour.y() = colour.y(); | ||
_colour.z() = colour.z(); | ||
_colour.w() = 1; | ||
|
||
_wireShader = renderSystem.capture((boost::format("<%f %f %f>") % _colour[0] % _colour[1] % _colour[2]).str()); | ||
} | ||
|
||
if (!_pointShader) | ||
{ | ||
_pointShader = renderSystem.capture("$POINT"); | ||
} | ||
} | ||
|
||
void MeasurementTool::render(RenderSystem& renderSystem, RenderableCollector& collector, const VolumeTest& volume) | ||
{ | ||
ensureShaders(renderSystem); | ||
|
||
// Render lines | ||
collector.SetState(_wireShader, RenderableCollector::eWireframeOnly); | ||
collector.addRenderable(_lines, Matrix4::getIdentity()); | ||
|
||
// Render points | ||
collector.SetState(_pointShader, RenderableCollector::eWireframeOnly); | ||
collector.addRenderable(_points, Matrix4::getIdentity()); | ||
|
||
// Render distance string | ||
for (std::size_t i = 1; i < _points.size(); ++i) | ||
{ | ||
const Vector3& a = _points[i-1].vertex; | ||
const Vector3& b = _points[i].vertex; | ||
|
||
glColor4dv(_colour); | ||
glRasterPos3dv((a+b)*0.5); | ||
GlobalOpenGL().drawString(string::to_string((a-b).getLength())); | ||
} | ||
} | ||
|
||
} // namespace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#pragma once | ||
|
||
#include "imousetool.h" | ||
#include "iorthoview.h" | ||
#include "math/Vector3.h" | ||
#include "render.h" | ||
#include "render/Colour4.h" | ||
|
||
namespace ui | ||
{ | ||
|
||
/** | ||
* This tool can be used to measure distances. | ||
* It is working on the orthoviews only. | ||
*/ | ||
class MeasurementTool : | ||
public MouseTool | ||
{ | ||
private: | ||
RenderablePointVector _points; | ||
RenderablePointVector _lines; | ||
|
||
ShaderPtr _pointShader; | ||
ShaderPtr _wireShader; | ||
|
||
Colour4 _colour; | ||
|
||
public: | ||
MeasurementTool(); | ||
|
||
const std::string& getName() override; | ||
const std::string& getDisplayName() override; | ||
|
||
Result onMouseDown(Event& ev) override; | ||
Result onMouseMove(Event& ev) override; | ||
Result onMouseUp(Event& ev) override; | ||
|
||
unsigned int getPointerMode() override; | ||
unsigned int getRefreshMode() override; | ||
Result onCancel(IInteractiveView& view) override; | ||
void onMouseCaptureLost(IInteractiveView& view) override; | ||
|
||
void render(RenderSystem& renderSystem, RenderableCollector& collector, const VolumeTest& volume) override; | ||
|
||
private: | ||
void ensureShaders(RenderSystem& renderSystem); | ||
}; | ||
|
||
} // namespace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters