27 #include "WOnscreenSelection.h"
30 m_projection( new osg::Projection() ),
33 m_isSelecting( false ),
34 m_shader( new
WGEShader(
"WOnscreenSelection" ) ),
84 case WSelectionType::BRUSH:
87 case WSelectionType::LINELOOP:
89 case WSelectionType::BOX:
113 case WSelectionType::BRUSH:
114 case WSelectionType::LINELOOP:
117 case WSelectionType::BOX:
150 m_projection->setMatrix( osg::Matrix::ortho2D( 0, 1.0, 0, 1.0 ) );
151 m_projection->getOrCreateStateSet()->setRenderBinDetails( 15,
"RenderBin" );
152 m_projection->getOrCreateStateSet()->setDataVariance( osg::Object::DYNAMIC );
153 m_projection->getOrCreateStateSet()->setMode( GL_DEPTH_TEST, osg::StateAttribute::OFF );
154 m_projection->getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
155 m_projection->getOrCreateStateSet()->setRenderingHint( osg::StateSet::TRANSPARENT_BIN );
156 m_projection->getOrCreateStateSet()->setMode( GL_BLEND, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE );
158 osg::ref_ptr< osg::Geode > lines =
new osg::Geode();
159 osg::ref_ptr< osg::Geometry > geometry =
new osg::Geometry();
160 osg::ref_ptr< osg::Vec3Array > vertices = osg::ref_ptr< osg::Vec3Array >(
new osg::Vec3Array() );
167 vertices->push_back( osg::Vec3( pos1.x(), pos2.y(), 0 ) );
169 vertices->push_back( osg::Vec3( pos1.x(), pos1.y(), 0 ) );
170 vertices->push_back( osg::Vec3( pos2.x(), pos1.y(), 0 ) );
171 vertices->push_back( osg::Vec3( pos2.x(), pos2.y(), 0 ) );
172 vertices->push_back( osg::Vec3( pos1.x(), pos2.y(), 0 ) );
173 vertices->push_back( osg::Vec3( pos1.x(), pos1.y(), 0 ) );
175 vertices->push_back( osg::Vec3( pos2.x(), pos1.y(), 0 ) );
177 geometry->addPrimitiveSet(
new osg::DrawArrays( osg::PrimitiveSet::LINE_STRIP_ADJACENCY, 0, 7 ) );
188 vertices->push_back( osg::Vec3( last.x(), last.y(), last.z() ) );
192 vertices->push_back( osg::Vec3( first.x(), first.y(), first.z() ) );
196 for(
size_t i = 0; i <
m_line.size(); i++ )
199 vertices->push_back( osg::Vec3( pos.x(), pos.y(), pos.z() ) );
205 vertices->push_back( osg::Vec3( first.x(), first.y(), first.z() ) );
206 vertices->push_back( osg::Vec3(
m_line.at( 1 ).x(),
m_line.at( 1 ).y(),
m_line.at( 1 ).z() ) );
211 vertices->push_back( osg::Vec3( last.x(), last.y(), last.z() ) );
214 geometry->addPrimitiveSet(
new osg::DrawArrays( osg::PrimitiveSet::LINE_STRIP_ADJACENCY, 0,
m_line.size() + add ) );
217 geometry->setVertexArray( vertices );
219 lines->addDrawable( geometry );
223 osg::StateSet* state = lines->getOrCreateStateSet();
225 state->addUniform(
new osg::Uniform(
"u_viewport", osg::Vec2( camera->getViewport()->width(), camera->getViewport()->height() ) ) );
226 state->addUniform(
new osg::Uniform(
"u_thickness", thickness ) );
227 state->addUniform(
new osg::Uniform(
"u_clickType",
m_clickType ) );
229 osg::ref_ptr< osg::MatrixTransform > matrix =
new osg::MatrixTransform();
230 matrix->setMatrix( osg::Matrix::identity() );
231 matrix->setReferenceFrame( osg::Transform::ABSOLUTE_RF );
234 matrix->addChild( lines );
275 osg::Matrix viewMatrix = camera->getViewMatrix();
276 osg::Matrix projectionMatrix = camera->getProjectionMatrix();
277 osg::Matrix cameraMatrix = viewMatrix * projectionMatrix;
278 osg::Vec3 point = osg::Vec3( x, y, z ) * cameraMatrix;
279 float xPos = ( point.x() + 1.0 ) / 2.0;
280 float yPos = ( point.y() + 1.0 ) / 2.0;
282 if( xPos < 0 || xPos > 1 || yPos < 0 || yPos > 1 )
289 case WSelectionType::BRUSH:
291 case WSelectionType::LINELOOP:
293 case WSelectionType::BOX:
302 std::vector< WPosition > out;
303 for(
size_t i = 0; i < positions.size(); i++ )
308 out.push_back( pos );
321 float maxX = fmax( pos1.x(), pos2.x() );
322 float maxY = fmax( pos1.y(), pos2.y() );
323 float minX = fmin( pos1.x(), pos2.x() );
324 float minY = fmin( pos1.y(), pos2.y() );
326 return x >= minX && x <= maxX && y >= minY && y <= maxY;
333 float width = camera->getViewport()->width();
334 float height = camera->getViewport()->height();
339 for(
size_t i = 0; i <
m_line.size() - 1; i++ )
345 pos1 =
WPosition( pos1.x() * width, pos1.y() * height, 0.0 );
346 pos2 =
WPosition( pos2.x() * width, pos2.y() * height, 0.0 );
349 float segLen2 = pow( pos1.x() - pos2.x(), 2.0 ) + pow( pos1.y() - pos2.y(), 2.0 );
355 float segParam = ( ( x - pos1.x() ) * ( pos2.x() - pos1.x() ) + ( y - pos1.y() ) * ( pos2.y() - pos1.y() ) ) / segLen2;
356 segParam = fmax( 0.0, fmin( 1.0, segParam ) );
358 float xPos = pos1.x() + segParam * ( pos2.x() - pos1.x() );
359 float yPos = pos1.y() + segParam * ( pos2.y() - pos1.y() );
361 float dis2 = pow( x - xPos, 2.0 ) + pow( y - yPos, 2.0 );
374 for(
size_t i = 0; i <
m_line.size(); i++ )
390 if( y == b.y() && y == c.y() )
392 if( ( b.x() <= x && x <= c.y() ) || ( c.x() <= x && x <= b.x() ) )
401 if( y == b.y() && x == b.x() )
412 if( y <= b.y() || y > c.y() )
417 float delta = ( b.x() - x ) * ( c.y() - y ) - ( b.y() - y ) * ( c.x() - x );
418 return delta > 0 ? -1 : delta < 0 ? 1 : 0;
Class encapsulating the OSG Program class for a more convenient way of adding and modifying shader.
static WKernel * getRunningKernel()
Returns pointer to the currently running kernel.
std::shared_ptr< WGraphicsEngine > getGraphicsEngine() const
Returns pointer to currently running instance of graphics engine.
Handles the GUIEvents in context of the WOnscreenSelection.
std::vector< WPosition > m_line
The points that are used for the selection.
bool lineloopCheck(float x, float y)
Check for the lineloop selection.
osg::ref_ptr< osg::Projection > m_projection
The Projection to draw on.
void setSelectionType(enum WSelectionType selectionType)
Sets the current selection type.
void clear()
Clears the current selection.
void start(float x, float y)
Starts the selection.
CallbackType m_onend
The Callback for the end function.
bool brushCheck(float x, float y)
Check for the brush selection.
osg::ref_ptr< WGEShader > m_shader
The shader for the selection.
int crossingNumberProduct(float x, float y, WPosition b, WPosition c)
Calculates the crossing number product.
void setOnmove(CallbackType onmove)
Sets the callback for the onmove function.
void setOnend(CallbackType onend)
Sets the callback for the onend function.
float m_thickness
The thickness of the brush.
CallbackType m_onmove
The Callback for the move function.
bool getClickType()
Gets the click type.
CallbackType m_onstart
The Callback for the start function.
void setClickType(bool clickType)
Sets the click type.
~WOnscreenSelection()
Cleans up all the now unneeded drawing context stuff.
bool m_clickType
Whether this is a left click or not.
bool isSelected(float x, float y, float z)
Whether a point is selected or not.
void move(float x, float y)
Handles mouse move while selecting.
bool m_moved
Whether the mouse has been moved.
WOnscreenSelection()
Not only creates the Object but also sets up the drawing context.
bool m_isSelecting
Whether a selection has been started or not.
enum WSelectionType getSelectionType()
Gets the current selection type.
void setOnstart(CallbackType onstart)
Sets the callback for the onstart function.
bool boxCheck(float x, float y)
Check for the box selection.
void updateDisplay()
Updates the current rendered data.
WSelectionType
The different types of selection.
bool isSelecting()
Returns whether this manager is currently creating a selection.
void end(float x, float y)
Ends the selection.
osg::ref_ptr< WOnscreenSelectionHandler > m_selectionHandler
The SelectionHandler for this object.
boost::function< void(enum WSelectionType, float, float) > CallbackType
A typedef for the type of the callbacks.
enum WSelectionType m_selectionType
The current selection type.
This only is a 3d double vector.