2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "selection.h"
23 #include "globaldefs.h"
25 #include "debugging/debugging.h"
31 #include "windowobserver.h"
35 #include "renderable.h"
36 #include "selectable.h"
39 #include "math/frustum.h"
40 #include "signal/signal.h"
41 #include "generic/object.h"
42 #include "selectionlib.h"
46 #include "stream/stringstream.h"
47 #include "eclasslib.h"
48 #include "generic/bitfield.h"
49 #include "generic/static.h"
52 #include "container/container.h"
56 TextOutputStream& ostream_write( TextOutputStream& t, const Vector4& v ){
57 return t << "[ " << v.x() << " " << v.y() << " " << v.z() << " " << v.w() << " ]";
60 TextOutputStream& ostream_write( TextOutputStream& t, const Matrix4& m ){
61 return t << "[ " << m.x() << " " << m.y() << " " << m.z() << " " << m.t() << " ]";
67 Matrix4 m_viewpointSpace;
68 Matrix4 m_viewplaneSpace;
69 Vector3 m_axis_screen;
71 void update( const Matrix4& pivot2world, const Matrix4& modelview, const Matrix4& projection, const Matrix4& viewport ){
72 Pivot2World_worldSpace( m_worldSpace, pivot2world, modelview, projection, viewport );
73 Pivot2World_viewpointSpace( m_viewpointSpace, m_axis_screen, pivot2world, modelview, projection, viewport );
74 Pivot2World_viewplaneSpace( m_viewplaneSpace, pivot2world, modelview, projection, viewport );
79 void point_for_device_point( Vector3& point, const Matrix4& device2object, const float x, const float y, const float z ){
80 // transform from normalised device coords to object coords
81 point = vector4_projected( matrix4_transformed_vector4( device2object, Vector4( x, y, z, 1 ) ) );
84 void ray_for_device_point( Ray& ray, const Matrix4& device2object, const float x, const float y ){
85 // point at x, y, zNear
86 point_for_device_point( ray.origin, device2object, x, y, -1 );
88 // point at x, y, zFar
89 point_for_device_point( ray.direction, device2object, x, y, 1 );
92 vector3_subtract( ray.direction, ray.origin );
93 vector3_normalise( ray.direction );
96 bool sphere_intersect_ray( const Vector3& origin, float radius, const Ray& ray, Vector3& intersection ){
97 intersection = vector3_subtracted( origin, ray.origin );
98 const double a = vector3_dot( intersection, ray.direction );
99 const double d = radius * radius - ( vector3_dot( intersection, intersection ) - a * a );
102 intersection = vector3_added( ray.origin, vector3_scaled( ray.direction, a - sqrt( d ) ) );
107 intersection = vector3_added( ray.origin, vector3_scaled( ray.direction, a ) );
112 void ray_intersect_ray( const Ray& ray, const Ray& other, Vector3& intersection ){
113 intersection = vector3_subtracted( ray.origin, other.origin );
114 //float a = 1;//vector3_dot(ray.direction, ray.direction); // always >= 0
115 double dot = vector3_dot( ray.direction, other.direction );
116 //float c = 1;//vector3_dot(other.direction, other.direction); // always >= 0
117 double d = vector3_dot( ray.direction, intersection );
118 double e = vector3_dot( other.direction, intersection );
119 double D = 1 - dot * dot; //a*c - dot*dot; // always >= 0
121 if ( D < 0.000001 ) {
122 // the lines are almost parallel
123 intersection = vector3_added( other.origin, vector3_scaled( other.direction, e ) );
127 intersection = vector3_added( other.origin, vector3_scaled( other.direction, ( e - dot * d ) / D ) );
131 const Vector3 g_origin( 0, 0, 0 );
132 const float g_radius = 64;
134 void point_on_sphere( Vector3& point, const Matrix4& device2object, const float x, const float y ){
136 ray_for_device_point( ray, device2object, x, y );
137 sphere_intersect_ray( g_origin, g_radius, ray, point );
140 void point_on_axis( Vector3& point, const Vector3& axis, const Matrix4& device2object, const float x, const float y ){
142 ray_for_device_point( ray, device2object, x, y );
143 ray_intersect_ray( ray, Ray( Vector3( 0, 0, 0 ), axis ), point );
146 void point_on_plane( Vector3& point, const Matrix4& device2object, const float x, const float y ){
147 Matrix4 object2device( matrix4_full_inverse( device2object ) );
148 point = vector4_projected( matrix4_transformed_vector4( device2object, Vector4( x, y, object2device[14] / object2device[15], 1 ) ) );
151 //! a and b are unit vectors .. returns angle in radians
152 inline float angle_between( const Vector3& a, const Vector3& b ){
153 return static_cast<float>( 2.0 * atan2(
154 vector3_length( vector3_subtracted( a, b ) ),
155 vector3_length( vector3_added( a, b ) )
164 test_quat( const Vector3& from, const Vector3& to ){
165 Vector4 quaternion( quaternion_for_unit_vectors( from, to ) );
166 Matrix4 matrix( matrix4_rotation_for_quaternion( quaternion_multiplied_by_quaternion( quaternion, c_quaternion_identity ) ) );
171 static test_quat bleh( g_vector3_axis_x, g_vector3_axis_y );
174 //! axis is a unit vector
175 inline void constrain_to_axis( Vector3& vec, const Vector3& axis ){
176 vec = vector3_normalised( vector3_added( vec, vector3_scaled( axis, -vector3_dot( vec, axis ) ) ) );
179 //! a and b are unit vectors .. a and b must be orthogonal to axis .. returns angle in radians
180 float angle_for_axis( const Vector3& a, const Vector3& b, const Vector3& axis ){
181 if ( vector3_dot( axis, vector3_cross( a, b ) ) > 0.0 ) {
182 return angle_between( a, b );
185 return -angle_between( a, b );
189 float distance_for_axis( const Vector3& a, const Vector3& b, const Vector3& axis ){
190 return static_cast<float>( vector3_dot( b, axis ) - vector3_dot( a, axis ) );
196 virtual void Construct( const Matrix4& device2manip, const float x, const float y, const AABB bounds, const Vector3 transform_origin ) = 0;
197 virtual void Transform( const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y, const bool snap, const bool snapbbox ) = 0;
200 void transform_local2object( Matrix4& object, const Matrix4& local, const Matrix4& local2object ){
201 object = matrix4_multiplied_by_matrix4(
202 matrix4_multiplied_by_matrix4( local2object, local ),
203 matrix4_full_inverse( local2object )
210 virtual ~Rotatable() = default;
211 virtual void rotate( const Quaternion& rotation ) = 0;
214 class RotateFree : public Manipulatable
217 Rotatable& m_rotatable;
219 RotateFree( Rotatable& rotatable )
220 : m_rotatable( rotatable ){
222 void Construct( const Matrix4& device2manip, const float x, const float y, const AABB bounds, const Vector3 transform_origin ){
223 point_on_sphere( m_start, device2manip, x, y );
224 vector3_normalise( m_start );
226 void Transform( const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y, const bool snap, const bool snapbbox ){
228 point_on_sphere( current, device2manip, x, y );
231 Vector3 axis( 0, 0, 0 );
232 for( std::size_t i = 0; i < 3; ++i ){
233 if( current[i] == 0.0f ){
238 if( vector3_length_squared( axis ) != 0 ){
239 constrain_to_axis( current, axis );
240 m_rotatable.rotate( quaternion_for_axisangle( axis, float_snapped( angle_for_axis( m_start, current, axis ), static_cast<float>( c_pi / 12.0 ) ) ) );
245 vector3_normalise( current );
246 m_rotatable.rotate( quaternion_for_unit_vectors( m_start, current ) );
250 class RotateAxis : public Manipulatable
254 Rotatable& m_rotatable;
256 RotateAxis( Rotatable& rotatable )
257 : m_rotatable( rotatable ){
259 void Construct( const Matrix4& device2manip, const float x, const float y, const AABB bounds, const Vector3 transform_origin ){
260 point_on_sphere( m_start, device2manip, x, y );
261 constrain_to_axis( m_start, m_axis );
263 /// \brief Converts current position to a normalised vector orthogonal to axis.
264 void Transform( const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y, const bool snap, const bool snapbbox ){
266 point_on_sphere( current, device2manip, x, y );
267 constrain_to_axis( current, m_axis );
270 m_rotatable.rotate( quaternion_for_axisangle( m_axis, float_snapped( angle_for_axis( m_start, current, m_axis ), static_cast<float>( c_pi / 12.0 ) ) ) );
273 m_rotatable.rotate( quaternion_for_axisangle( m_axis, angle_for_axis( m_start, current, m_axis ) ) );
277 void SetAxis( const Vector3& axis ){
282 void translation_local2object( Vector3& object, const Vector3& local, const Matrix4& local2object ){
283 object = matrix4_get_translation_vec3(
284 matrix4_multiplied_by_matrix4(
285 matrix4_translated_by_vec3( local2object, local ),
286 matrix4_full_inverse( local2object )
294 virtual ~Translatable() = default;
295 virtual void translate( const Vector3& translation ) = 0;
298 class TranslateAxis : public Manipulatable
302 Translatable& m_translatable;
305 TranslateAxis( Translatable& translatable )
306 : m_translatable( translatable ){
308 void Construct( const Matrix4& device2manip, const float x, const float y, const AABB bounds, const Vector3 transform_origin ){
309 point_on_axis( m_start, m_axis, device2manip, x, y );
312 void Transform( const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y, const bool snap, const bool snapbbox ){
314 point_on_axis( current, m_axis, device2manip, x, y );
315 current = vector3_scaled( m_axis, distance_for_axis( m_start, current, m_axis ) );
317 translation_local2object( current, current, manip2object );
319 float grid = GetSnapGridSize();
320 Vector3 maxs( m_bounds.origin + m_bounds.extents );
321 Vector3 mins( m_bounds.origin - m_bounds.extents );
322 // globalOutputStream() << "current: " << current << "\n";
323 for( std::size_t i = 0; i < 3; ++i ){
324 if( m_axis[i] != 0.f ){
325 float snapto1 = float_snapped( maxs[i] + current[i] , grid );
326 float snapto2 = float_snapped( mins[i] + current[i] , grid );
328 float dist1 = fabs( fabs( maxs[i] + current[i] ) - fabs( snapto1 ) );
329 float dist2 = fabs( fabs( mins[i] + current[i] ) - fabs( snapto2 ) );
331 // globalOutputStream() << "maxs[i] + current[i]: " << maxs[i] + current[i] << " snapto1: " << snapto1 << " dist1: " << dist1 << "\n";
332 // globalOutputStream() << "mins[i] + current[i]: " << mins[i] + current[i] << " snapto2: " << snapto2 << " dist2: " << dist2 << "\n";
333 current[i] = dist2 > dist1 ? snapto1 - maxs[i] : snapto2 - mins[i];
338 vector3_snap( current, GetSnapGridSize() );
341 m_translatable.translate( current );
344 void SetAxis( const Vector3& axis ){
349 class TranslateFree : public Manipulatable
353 Translatable& m_translatable;
356 TranslateFree( Translatable& translatable )
357 : m_translatable( translatable ){
359 void Construct( const Matrix4& device2manip, const float x, const float y, const AABB bounds, const Vector3 transform_origin ){
360 point_on_plane( m_start, device2manip, x, y );
363 void Transform( const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y, const bool snap, const bool snapbbox ){
365 point_on_plane( current, device2manip, x, y );
366 current = vector3_subtracted( current, m_start );
369 for ( std::size_t i = 0; i < 3 ; ++i ){
370 if( fabs( current[i] ) >= fabs( current[(i + 1) % 3] ) ){
371 current[(i + 1) % 3] = 0.0f;
379 translation_local2object( current, current, manip2object );
381 float grid = GetSnapGridSize();
382 Vector3 maxs( m_bounds.origin + m_bounds.extents );
383 Vector3 mins( m_bounds.origin - m_bounds.extents );
384 //globalOutputStream() << "current: " << current << "\n";
385 for( std::size_t i = 0; i < 3; ++i ){
386 if( fabs( current[i] ) > 0.000001f ){
387 float snapto1 = float_snapped( maxs[i] + current[i] , grid );
388 float snapto2 = float_snapped( mins[i] + current[i] , grid );
390 float dist1 = fabs( fabs( maxs[i] + current[i] ) - fabs( snapto1 ) );
391 float dist2 = fabs( fabs( mins[i] + current[i] ) - fabs( snapto2 ) );
393 current[i] = dist2 > dist1 ? snapto1 - maxs[i] : snapto2 - mins[i];
398 vector3_snap( current, GetSnapGridSize() );
401 m_translatable.translate( current );
408 virtual ~Scalable() = default;
409 virtual void scale( const Vector3& scaling ) = 0;
413 class ScaleAxis : public Manipulatable
418 Scalable& m_scalable;
420 Vector3 m_choosen_extent;
424 ScaleAxis( Scalable& scalable )
425 : m_scalable( scalable ){
427 void Construct( const Matrix4& device2manip, const float x, const float y, const AABB bounds, const Vector3 transform_origin ){
428 point_on_axis( m_start, m_axis, device2manip, x, y );
430 m_choosen_extent = Vector3(
431 std::max( bounds.origin[0] + bounds.extents[0] - transform_origin[0], - bounds.origin[0] + bounds.extents[0] + transform_origin[0] ),
432 std::max( bounds.origin[1] + bounds.extents[1] - transform_origin[1], - bounds.origin[1] + bounds.extents[1] + transform_origin[1] ),
433 std::max( bounds.origin[2] + bounds.extents[2] - transform_origin[2], - bounds.origin[2] + bounds.extents[2] + transform_origin[2] )
437 void Transform( const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y, const bool snap, const bool snapbbox ){
438 //globalOutputStream() << "manip2object: " << manip2object << " device2manip: " << device2manip << " x: " << x << " y:" << y <<"\n";
440 point_on_axis( current, m_axis, device2manip, x, y );
441 Vector3 delta = vector3_subtracted( current, m_start );
443 translation_local2object( delta, delta, manip2object );
444 vector3_snap( delta, GetSnapGridSize() );
446 Vector3 start( vector3_snapped( m_start, GetSnapGridSize() != 0.0f ? GetSnapGridSize() : 0.001f ) );
447 for ( std::size_t i = 0; i < 3 ; ++i ){ //prevent snapping to 0 with big gridsize
448 if( float_snapped( m_start[i], 0.001f ) != 0.0f && start[i] == 0.0f ){
449 start[i] = GetSnapGridSize();
452 //globalOutputStream() << "m_start: " << m_start << " start: " << start << " delta: " << delta <<"\n";
454 start[0] == 0 ? 1 : 1 + delta[0] / start[0],
455 start[1] == 0 ? 1 : 1 + delta[1] / start[1],
456 start[2] == 0 ? 1 : 1 + delta[2] / start[2]
459 for( std::size_t i = 0; i < 3; i++ ){
460 if( m_choosen_extent[i] > 0.0625f && m_axis[i] != 0.f ){ //epsilon to prevent super high scale for set of models, having really small extent, formed by origins
461 scale[i] = ( m_choosen_extent[i] + delta[i] ) / m_choosen_extent[i];
463 float snappdwidth = float_snapped( scale[i] * m_bounds.extents[i] * 2.f, GetSnapGridSize() );
464 scale[i] = snappdwidth / ( m_bounds.extents[i] * 2.f );
469 for( std::size_t i = 0; i < 3; i++ ){
470 if( scale[i] == 1.0f ){
471 scale[i] = vector3_dot( scale, m_axis );
475 //globalOutputStream() << "scale: " << scale <<"\n";
476 m_scalable.scale( scale );
479 void SetAxis( const Vector3& axis ){
484 class ScaleFree : public Manipulatable
488 Scalable& m_scalable;
490 Vector3 m_choosen_extent;
494 ScaleFree( Scalable& scalable )
495 : m_scalable( scalable ){
497 void Construct( const Matrix4& device2manip, const float x, const float y, const AABB bounds, const Vector3 transform_origin ){
498 point_on_plane( m_start, device2manip, x, y );
500 m_choosen_extent = Vector3(
501 std::max( bounds.origin[0] + bounds.extents[0] - transform_origin[0], - bounds.origin[0] + bounds.extents[0] + transform_origin[0] ),
502 std::max( bounds.origin[1] + bounds.extents[1] - transform_origin[1], - bounds.origin[1] + bounds.extents[1] + transform_origin[1] ),
503 std::max( bounds.origin[2] + bounds.extents[2] - transform_origin[2], - bounds.origin[2] + bounds.extents[2] + transform_origin[2] )
507 void Transform( const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y, const bool snap, const bool snapbbox ){
509 point_on_plane( current, device2manip, x, y );
510 Vector3 delta = vector3_subtracted( current, m_start );
512 translation_local2object( delta, delta, manip2object );
513 vector3_snap( delta, GetSnapGridSize() );
515 Vector3 start( vector3_snapped( m_start, GetSnapGridSize() != 0.0f ? GetSnapGridSize() : 0.001f ) );
516 for ( std::size_t i = 0; i < 3 ; ++i ){ //prevent snapping to 0 with big gridsize
517 if( float_snapped( m_start[i], 0.001f ) != 0.0f && start[i] == 0.0f ){
518 start[i] = GetSnapGridSize();
522 start[0] == 0 ? 1 : 1 + delta[0] / start[0],
523 start[1] == 0 ? 1 : 1 + delta[1] / start[1],
524 start[2] == 0 ? 1 : 1 + delta[2] / start[2]
527 //globalOutputStream() << "m_start: " << m_start << " start: " << start << " delta: " << delta <<"\n";
528 for( std::size_t i = 0; i < 3; i++ ){
529 if( m_choosen_extent[i] > 0.0625f ){
530 scale[i] = ( m_choosen_extent[i] + delta[i] ) / m_choosen_extent[i];
531 if( snapbbox && start[i] != 0.f ){
532 float snappdwidth = float_snapped( scale[i] * m_bounds.extents[i] * 2.f, GetSnapGridSize() );
533 scale[i] = snappdwidth / ( m_bounds.extents[i] * 2.f );
537 //globalOutputStream() << "pre snap scale: " << scale <<"\n";
539 float bestscale = scale[0];
540 for( std::size_t i = 1; i < 3; i++ ){
541 //if( fabs( 1.0f - fabs( scale[i] ) ) > fabs( 1.0f - fabs( bestscale ) ) ){
542 if( fabs( scale[i] ) > fabs( bestscale ) && scale[i] != 1.0f ){ //harder to scale down with this, but glitchier with upper one
543 bestscale = scale[i];
545 //globalOutputStream() << "bestscale: " << bestscale <<"\n";
547 for( std::size_t i = 0; i < 3; i++ ){
548 if( start[i] != 0.0f ){ // !!!!check grid == 0 case
549 scale[i] = ( scale[i] < 0.0f ) ? -fabs( bestscale ) : fabs( bestscale );
553 //globalOutputStream() << "scale: " << scale <<"\n";
554 m_scalable.scale( scale );
567 class RenderableClippedPrimitive : public OpenGLRenderable
571 PointVertex m_points[9];
575 std::vector<primitive_t> m_primitives;
579 void render( RenderStateFlags state ) const {
580 for ( std::size_t i = 0; i < m_primitives.size(); ++i )
582 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_primitives[i].m_points[0].colour );
583 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_primitives[i].m_points[0].vertex );
584 switch ( m_primitives[i].m_count )
587 case 2: glDrawArrays( GL_LINES, 0, GLsizei( m_primitives[i].m_count ) ); break;
588 default: glDrawArrays( GL_POLYGON, 0, GLsizei( m_primitives[i].m_count ) ); break;
593 void construct( const Matrix4& world2device ){
594 m_inverse = matrix4_full_inverse( world2device );
595 m_world = g_matrix4_identity;
598 void insert( const Vector4 clipped[9], std::size_t count ){
601 m_primitives.back().m_count = count;
602 for ( std::size_t i = 0; i < count; ++i )
604 Vector3 world_point( vector4_projected( matrix4_transformed_vector4( m_inverse, clipped[i] ) ) );
605 m_primitives.back().m_points[i].vertex = vertex3f_for_vector3( world_point );
610 m_primitives.clear();
614 m_primitives.push_back( primitive_t() );
616 const Colour4b colour_clipped( 255, 127, 0, 255 );
618 for ( std::size_t i = 0; i < 9; ++i )
619 m_primitives.back().m_points[i].colour = colour_clipped;
624 #define DEBUG_SELECTION
627 #if defined( DEBUG_SELECTION )
628 Shader* g_state_clipped;
629 RenderableClippedPrimitive g_render_clipped;
634 // dist_Point_to_Line(): get the distance of a point to a line.
635 // Input: a Point P and a Line L (in any dimension)
636 // Return: the shortest distance from P to L
638 dist_Point_to_Line( Point P, Line L ){
639 Vector v = L.P1 - L.P0;
642 double c1 = dot( w,v );
643 double c2 = dot( v,v );
646 Point Pb = L.P0 + b * v;
653 typedef Vector3 point_type;
655 Segment3D( const point_type& _p0, const point_type& _p1 )
656 : p0( _p0 ), p1( _p1 ){
662 typedef Vector3 Point3D;
664 inline double vector3_distance_squared( const Point3D& a, const Point3D& b ){
665 return vector3_length_squared( b - a );
668 // get the distance of a point to a segment.
669 Point3D segment_closest_point_to_point( const Segment3D& segment, const Point3D& point ){
670 Vector3 v = segment.p1 - segment.p0;
671 Vector3 w = point - segment.p0;
673 double c1 = vector3_dot( w,v );
678 double c2 = vector3_dot( v,v );
683 return Point3D( segment.p0 + v * ( c1 / c2 ) );
686 double segment_dist_to_point_3d( const Segment3D& segment, const Point3D& point ){
687 return vector3_distance_squared( point, segment_closest_point_to_point( segment, point ) );
690 typedef Vector3 point_t;
691 typedef const Vector3* point_iterator_t;
693 // crossing number test for a point in a polygon
694 // This code is patterned after [Franklin, 2000]
695 bool point_test_polygon_2d( const point_t& P, point_iterator_t start, point_iterator_t finish ){
696 std::size_t crossings = 0;
698 // loop through all edges of the polygon
699 for ( point_iterator_t prev = finish - 1, cur = start; cur != finish; prev = cur, ++cur )
700 { // edge from (*prev) to (*cur)
701 if ( ( ( ( *prev )[1] <= P[1] ) && ( ( *cur )[1] > P[1] ) ) // an upward crossing
702 || ( ( ( *prev )[1] > P[1] ) && ( ( *cur )[1] <= P[1] ) ) ) { // a downward crossing
703 // compute the actual edge-ray intersect x-coordinate
704 float vt = (float)( P[1] - ( *prev )[1] ) / ( ( *cur )[1] - ( *prev )[1] );
705 if ( P[0] < ( *prev )[0] + vt * ( ( *cur )[0] - ( *prev )[0] ) ) { // P[0] < intersect
706 ++crossings; // a valid crossing of y=P[1] right of P[0]
710 return ( crossings & 0x1 ) != 0; // 0 if even (out), and 1 if odd (in)
713 inline double triangle_signed_area_XY( const Vector3& p0, const Vector3& p1, const Vector3& p2 ){
714 return ( ( p1[0] - p0[0] ) * ( p2[1] - p0[1] ) ) - ( ( p2[0] - p0[0] ) * ( p1[1] - p0[1] ) );
725 inline SelectionIntersection select_point_from_clipped( Vector4& clipped ){
726 return SelectionIntersection( clipped[2] / clipped[3], static_cast<float>( vector3_length_squared( Vector3( clipped[0] / clipped[3], clipped[1] / clipped[3], 0 ) ) ) );
729 void BestPoint( std::size_t count, Vector4 clipped[9], SelectionIntersection& best, clipcull_t cull ){
730 Vector3 normalised[9];
733 for ( std::size_t i = 0; i < count; ++i )
735 normalised[i][0] = clipped[i][0] / clipped[i][3];
736 normalised[i][1] = clipped[i][1] / clipped[i][3];
737 normalised[i][2] = clipped[i][2] / clipped[i][3];
741 if ( cull != eClipCullNone && count > 2 ) {
742 double signed_area = triangle_signed_area_XY( normalised[0], normalised[1], normalised[2] );
744 if ( ( cull == eClipCullCW && signed_area > 0 )
745 || ( cull == eClipCullCCW && signed_area < 0 ) ) {
751 Segment3D segment( normalised[0], normalised[1] );
752 Point3D point = segment_closest_point_to_point( segment, Vector3( 0, 0, 0 ) );
753 assign_if_closer( best, SelectionIntersection( point.z(), 0 ) );
755 else if ( count > 2 && !point_test_polygon_2d( Vector3( 0, 0, 0 ), normalised, normalised + count ) ) {
756 point_iterator_t end = normalised + count;
757 for ( point_iterator_t previous = end - 1, current = normalised; current != end; previous = current, ++current )
759 Segment3D segment( *previous, *current );
760 Point3D point = segment_closest_point_to_point( segment, Vector3( 0, 0, 0 ) );
761 float depth = point.z();
763 float distance = static_cast<float>( vector3_length_squared( point ) );
765 assign_if_closer( best, SelectionIntersection( depth, distance ) );
768 else if ( count > 2 ) {
771 SelectionIntersection(
772 static_cast<float>( ray_distance_to_plane(
773 Ray( Vector3( 0, 0, 0 ), Vector3( 0, 0, 1 ) ),
774 plane3_for_points( normalised[0], normalised[1], normalised[2] )
781 #if defined( DEBUG_SELECTION )
783 g_render_clipped.insert( clipped, count );
788 void LineStrip_BestPoint( const Matrix4& local2view, const PointVertex* vertices, const std::size_t size, SelectionIntersection& best ){
790 for ( std::size_t i = 0; ( i + 1 ) < size; ++i )
792 const std::size_t count = matrix4_clip_line( local2view, vertex3f_to_vector3( vertices[i].vertex ), vertex3f_to_vector3( vertices[i + 1].vertex ), clipped );
793 BestPoint( count, clipped, best, eClipCullNone );
797 void LineLoop_BestPoint( const Matrix4& local2view, const PointVertex* vertices, const std::size_t size, SelectionIntersection& best ){
799 for ( std::size_t i = 0; i < size; ++i )
801 const std::size_t count = matrix4_clip_line( local2view, vertex3f_to_vector3( vertices[i].vertex ), vertex3f_to_vector3( vertices[( i + 1 ) % size].vertex ), clipped );
802 BestPoint( count, clipped, best, eClipCullNone );
806 void Line_BestPoint( const Matrix4& local2view, const PointVertex vertices[2], SelectionIntersection& best ){
808 const std::size_t count = matrix4_clip_line( local2view, vertex3f_to_vector3( vertices[0].vertex ), vertex3f_to_vector3( vertices[1].vertex ), clipped );
809 BestPoint( count, clipped, best, eClipCullNone );
812 void Circle_BestPoint( const Matrix4& local2view, clipcull_t cull, const PointVertex* vertices, const std::size_t size, SelectionIntersection& best ){
814 for ( std::size_t i = 0; i < size; ++i )
816 const std::size_t count = matrix4_clip_triangle( local2view, g_vector3_identity, vertex3f_to_vector3( vertices[i].vertex ), vertex3f_to_vector3( vertices[( i + 1 ) % size].vertex ), clipped );
817 BestPoint( count, clipped, best, cull );
821 void Quad_BestPoint( const Matrix4& local2view, clipcull_t cull, const PointVertex* vertices, SelectionIntersection& best ){
824 const std::size_t count = matrix4_clip_triangle( local2view, vertex3f_to_vector3( vertices[0].vertex ), vertex3f_to_vector3( vertices[1].vertex ), vertex3f_to_vector3( vertices[3].vertex ), clipped );
825 BestPoint( count, clipped, best, cull );
828 const std::size_t count = matrix4_clip_triangle( local2view, vertex3f_to_vector3( vertices[1].vertex ), vertex3f_to_vector3( vertices[2].vertex ), vertex3f_to_vector3( vertices[3].vertex ), clipped );
829 BestPoint( count, clipped, best, cull );
833 struct FlatShadedVertex
844 typedef FlatShadedVertex* FlatShadedVertexIterator;
845 void Triangles_BestPoint( const Matrix4& local2view, clipcull_t cull, FlatShadedVertexIterator first, FlatShadedVertexIterator last, SelectionIntersection& best ){
846 for ( FlatShadedVertexIterator x( first ), y( first + 1 ), z( first + 2 ); x != last; x += 3, y += 3, z += 3 )
850 matrix4_clip_triangle(
852 reinterpret_cast<const Vector3&>( ( *x ).vertex ),
853 reinterpret_cast<const Vector3&>( ( *y ).vertex ),
854 reinterpret_cast<const Vector3&>( ( *z ).vertex ),
865 typedef std::multimap<SelectionIntersection, Selectable*> SelectableSortedSet;
867 class SelectionPool : public Selector
869 SelectableSortedSet m_pool;
870 SelectionIntersection m_intersection;
871 Selectable* m_selectable;
874 void pushSelectable( Selectable& selectable ){
875 m_intersection = SelectionIntersection();
876 m_selectable = &selectable;
878 void popSelectable(){
879 addSelectable( m_intersection, m_selectable );
880 m_intersection = SelectionIntersection();
882 void addIntersection( const SelectionIntersection& intersection ){
883 assign_if_closer( m_intersection, intersection );
885 void addSelectable( const SelectionIntersection& intersection, Selectable* selectable ){
886 if ( intersection.valid() ) {
887 m_pool.insert( SelectableSortedSet::value_type( intersection, selectable ) );
891 typedef SelectableSortedSet::iterator iterator;
894 return m_pool.begin();
901 return m_pool.empty();
906 const Colour4b g_colour_sphere( 0, 0, 0, 255 );
907 const Colour4b g_colour_screen( 0, 255, 255, 255 );
908 const Colour4b g_colour_selected( 255, 255, 0, 255 );
910 inline const Colour4b& colourSelected( const Colour4b& colour, bool selected ){
911 return ( selected ) ? g_colour_selected : colour;
914 template<typename remap_policy>
915 inline void draw_semicircle( const std::size_t segments, const float radius, PointVertex* vertices, remap_policy remap ){
916 const double increment = c_pi / double(segments << 2);
918 std::size_t count = 0;
921 remap_policy::set( vertices[segments << 2].vertex, -radius, 0, 0 );
922 while ( count < segments )
924 PointVertex* i = vertices + count;
925 PointVertex* j = vertices + ( ( segments << 1 ) - ( count + 1 ) );
927 PointVertex* k = i + ( segments << 1 );
928 PointVertex* l = j + ( segments << 1 );
931 PointVertex* m = i + ( segments << 2 );
932 PointVertex* n = j + ( segments << 2 );
933 PointVertex* o = k + ( segments << 2 );
934 PointVertex* p = l + ( segments << 2 );
937 remap_policy::set( i->vertex, x,-y, 0 );
938 remap_policy::set( k->vertex,-y,-x, 0 );
940 remap_policy::set( m->vertex,-x, y, 0 );
941 remap_policy::set( o->vertex, y, x, 0 );
947 const double theta = increment * count;
948 x = static_cast<float>( radius * cos( theta ) );
949 y = static_cast<float>( radius * sin( theta ) );
952 remap_policy::set( j->vertex, y,-x, 0 );
953 remap_policy::set( l->vertex,-x,-y, 0 );
955 remap_policy::set( n->vertex,-y, x, 0 );
956 remap_policy::set( p->vertex, x, y, 0 );
964 virtual Manipulatable* GetManipulatable() = 0;
965 virtual void testSelect( const View& view, const Matrix4& pivot2world ){
967 virtual void render( Renderer& renderer, const VolumeTest& volume, const Matrix4& pivot2world ){
969 virtual void setSelected( bool select ) = 0;
970 virtual bool isSelected() const = 0;
974 inline Vector3 normalised_safe( const Vector3& self ){
975 if ( vector3_equal( self, g_vector3_identity ) ) {
976 return g_vector3_identity;
978 return vector3_normalised( self );
982 class RotateManipulator : public Manipulator
984 struct RenderableCircle : public OpenGLRenderable
986 Array<PointVertex> m_vertices;
988 RenderableCircle( std::size_t size ) : m_vertices( size ){
990 void render( RenderStateFlags state ) const {
991 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_vertices.data()->colour );
992 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_vertices.data()->vertex );
993 glDrawArrays( GL_LINE_LOOP, 0, GLsizei( m_vertices.size() ) );
995 void setColour( const Colour4b& colour ){
996 for ( Array<PointVertex>::iterator i = m_vertices.begin(); i != m_vertices.end(); ++i )
998 ( *i ).colour = colour;
1003 struct RenderableSemiCircle : public OpenGLRenderable
1005 Array<PointVertex> m_vertices;
1007 RenderableSemiCircle( std::size_t size ) : m_vertices( size ){
1009 void render( RenderStateFlags state ) const {
1010 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_vertices.data()->colour );
1011 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_vertices.data()->vertex );
1012 glDrawArrays( GL_LINE_STRIP, 0, GLsizei( m_vertices.size() ) );
1014 void setColour( const Colour4b& colour ){
1015 for ( Array<PointVertex>::iterator i = m_vertices.begin(); i != m_vertices.end(); ++i )
1017 ( *i ).colour = colour;
1024 Vector3 m_axis_screen;
1025 RenderableSemiCircle m_circle_x;
1026 RenderableSemiCircle m_circle_y;
1027 RenderableSemiCircle m_circle_z;
1028 RenderableCircle m_circle_screen;
1029 RenderableCircle m_circle_sphere;
1030 SelectableBool m_selectable_x;
1031 SelectableBool m_selectable_y;
1032 SelectableBool m_selectable_z;
1033 SelectableBool m_selectable_screen;
1034 SelectableBool m_selectable_sphere;
1035 Pivot2World m_pivot;
1036 Matrix4 m_local2world_x;
1037 Matrix4 m_local2world_y;
1038 Matrix4 m_local2world_z;
1039 bool m_circle_x_visible;
1040 bool m_circle_y_visible;
1041 bool m_circle_z_visible;
1043 static Shader* m_state_outer;
1045 RotateManipulator( Rotatable& rotatable, std::size_t segments, float radius ) :
1046 m_free( rotatable ),
1047 m_axis( rotatable ),
1048 m_circle_x( ( segments << 2 ) + 1 ),
1049 m_circle_y( ( segments << 2 ) + 1 ),
1050 m_circle_z( ( segments << 2 ) + 1 ),
1051 m_circle_screen( segments << 3 ),
1052 m_circle_sphere( segments << 3 ){
1053 draw_semicircle( segments, radius, m_circle_x.m_vertices.data(), RemapYZX() );
1054 draw_semicircle( segments, radius, m_circle_y.m_vertices.data(), RemapZXY() );
1055 draw_semicircle( segments, radius, m_circle_z.m_vertices.data(), RemapXYZ() );
1057 draw_circle( segments, radius * 1.15f, m_circle_screen.m_vertices.data(), RemapXYZ() );
1058 draw_circle( segments, radius, m_circle_sphere.m_vertices.data(), RemapXYZ() );
1060 m_selectable_sphere.setSelected( true );
1064 void UpdateColours(){
1065 m_circle_x.setColour( colourSelected( g_colour_x, m_selectable_x.isSelected() ) );
1066 m_circle_y.setColour( colourSelected( g_colour_y, m_selectable_y.isSelected() ) );
1067 m_circle_z.setColour( colourSelected( g_colour_z, m_selectable_z.isSelected() ) );
1068 m_circle_screen.setColour( colourSelected( g_colour_screen, m_selectable_screen.isSelected() ) );
1069 m_circle_sphere.setColour( colourSelected( g_colour_sphere, false ) );
1072 void updateCircleTransforms(){
1073 Vector3 localViewpoint( matrix4_transformed_direction( matrix4_transposed( m_pivot.m_worldSpace ), vector4_to_vector3( m_pivot.m_viewpointSpace.z() ) ) );
1075 m_circle_x_visible = !vector3_equal_epsilon( g_vector3_axis_x, localViewpoint, 1e-6f );
1076 if ( m_circle_x_visible ) {
1077 m_local2world_x = g_matrix4_identity;
1078 vector4_to_vector3( m_local2world_x.y() ) = normalised_safe(
1079 vector3_cross( g_vector3_axis_x, localViewpoint )
1081 vector4_to_vector3( m_local2world_x.z() ) = normalised_safe(
1082 vector3_cross( vector4_to_vector3( m_local2world_x.x() ), vector4_to_vector3( m_local2world_x.y() ) )
1084 matrix4_premultiply_by_matrix4( m_local2world_x, m_pivot.m_worldSpace );
1087 m_circle_y_visible = !vector3_equal_epsilon( g_vector3_axis_y, localViewpoint, 1e-6f );
1088 if ( m_circle_y_visible ) {
1089 m_local2world_y = g_matrix4_identity;
1090 vector4_to_vector3( m_local2world_y.z() ) = normalised_safe(
1091 vector3_cross( g_vector3_axis_y, localViewpoint )
1093 vector4_to_vector3( m_local2world_y.x() ) = normalised_safe(
1094 vector3_cross( vector4_to_vector3( m_local2world_y.y() ), vector4_to_vector3( m_local2world_y.z() ) )
1096 matrix4_premultiply_by_matrix4( m_local2world_y, m_pivot.m_worldSpace );
1099 m_circle_z_visible = !vector3_equal_epsilon( g_vector3_axis_z, localViewpoint, 1e-6f );
1100 if ( m_circle_z_visible ) {
1101 m_local2world_z = g_matrix4_identity;
1102 vector4_to_vector3( m_local2world_z.x() ) = normalised_safe(
1103 vector3_cross( g_vector3_axis_z, localViewpoint )
1105 vector4_to_vector3( m_local2world_z.y() ) = normalised_safe(
1106 vector3_cross( vector4_to_vector3( m_local2world_z.z() ), vector4_to_vector3( m_local2world_z.x() ) )
1108 matrix4_premultiply_by_matrix4( m_local2world_z, m_pivot.m_worldSpace );
1112 void render( Renderer& renderer, const VolumeTest& volume, const Matrix4& pivot2world ){
1113 m_pivot.update( pivot2world, volume.GetModelview(), volume.GetProjection(), volume.GetViewport() );
1114 updateCircleTransforms();
1119 renderer.SetState( m_state_outer, Renderer::eWireframeOnly );
1120 renderer.SetState( m_state_outer, Renderer::eFullMaterials );
1122 renderer.addRenderable( m_circle_screen, m_pivot.m_viewpointSpace );
1123 renderer.addRenderable( m_circle_sphere, m_pivot.m_viewpointSpace );
1125 if ( m_circle_x_visible ) {
1126 renderer.addRenderable( m_circle_x, m_local2world_x );
1128 if ( m_circle_y_visible ) {
1129 renderer.addRenderable( m_circle_y, m_local2world_y );
1131 if ( m_circle_z_visible ) {
1132 renderer.addRenderable( m_circle_z, m_local2world_z );
1135 void testSelect( const View& view, const Matrix4& pivot2world ){
1136 m_pivot.update( pivot2world, view.GetModelview(), view.GetProjection(), view.GetViewport() );
1137 updateCircleTransforms();
1139 SelectionPool selector;
1143 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_local2world_x ) );
1145 #if defined( DEBUG_SELECTION )
1146 g_render_clipped.construct( view.GetViewMatrix() );
1149 SelectionIntersection best;
1150 LineStrip_BestPoint( local2view, m_circle_x.m_vertices.data(), m_circle_x.m_vertices.size(), best );
1151 selector.addSelectable( best, &m_selectable_x );
1155 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_local2world_y ) );
1157 #if defined( DEBUG_SELECTION )
1158 g_render_clipped.construct( view.GetViewMatrix() );
1161 SelectionIntersection best;
1162 LineStrip_BestPoint( local2view, m_circle_y.m_vertices.data(), m_circle_y.m_vertices.size(), best );
1163 selector.addSelectable( best, &m_selectable_y );
1167 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_local2world_z ) );
1169 #if defined( DEBUG_SELECTION )
1170 g_render_clipped.construct( view.GetViewMatrix() );
1173 SelectionIntersection best;
1174 LineStrip_BestPoint( local2view, m_circle_z.m_vertices.data(), m_circle_z.m_vertices.size(), best );
1175 selector.addSelectable( best, &m_selectable_z );
1180 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_pivot.m_viewpointSpace ) );
1183 SelectionIntersection best;
1184 LineLoop_BestPoint( local2view, m_circle_screen.m_vertices.data(), m_circle_screen.m_vertices.size(), best );
1185 selector.addSelectable( best, &m_selectable_screen );
1189 SelectionIntersection best;
1190 Circle_BestPoint( local2view, eClipCullCW, m_circle_sphere.m_vertices.data(), m_circle_sphere.m_vertices.size(), best );
1191 selector.addSelectable( best, &m_selectable_sphere );
1195 m_axis_screen = m_pivot.m_axis_screen;
1197 if ( !selector.failed() ) {
1198 ( *selector.begin() ).second->setSelected( true );
1202 Manipulatable* GetManipulatable(){
1203 if ( m_selectable_x.isSelected() ) {
1204 m_axis.SetAxis( g_vector3_axis_x );
1207 else if ( m_selectable_y.isSelected() ) {
1208 m_axis.SetAxis( g_vector3_axis_y );
1211 else if ( m_selectable_z.isSelected() ) {
1212 m_axis.SetAxis( g_vector3_axis_z );
1215 else if ( m_selectable_screen.isSelected() ) {
1216 m_axis.SetAxis( m_axis_screen );
1224 void setSelected( bool select ){
1225 m_selectable_x.setSelected( select );
1226 m_selectable_y.setSelected( select );
1227 m_selectable_z.setSelected( select );
1228 m_selectable_screen.setSelected( select );
1230 bool isSelected() const {
1231 return m_selectable_x.isSelected()
1232 | m_selectable_y.isSelected()
1233 | m_selectable_z.isSelected()
1234 | m_selectable_screen.isSelected()
1235 | m_selectable_sphere.isSelected();
1239 Shader* RotateManipulator::m_state_outer;
1242 const float arrowhead_length = 16;
1243 const float arrowhead_radius = 4;
1245 inline void draw_arrowline( const float length, PointVertex* line, const std::size_t axis ){
1246 ( *line++ ).vertex = vertex3f_identity;
1247 ( *line ).vertex = vertex3f_identity;
1248 vertex3f_to_array( ( *line ).vertex )[axis] = length - arrowhead_length;
1251 template<typename VertexRemap, typename NormalRemap>
1252 inline void draw_arrowhead( const std::size_t segments, const float length, FlatShadedVertex* vertices, VertexRemap, NormalRemap ){
1253 std::size_t head_tris = ( segments << 3 );
1254 const double head_segment = c_2pi / head_tris;
1255 for ( std::size_t i = 0; i < head_tris; ++i )
1258 FlatShadedVertex& point = vertices[i * 6 + 0];
1259 VertexRemap::x( point.vertex ) = length - arrowhead_length;
1260 VertexRemap::y( point.vertex ) = arrowhead_radius * static_cast<float>( cos( i * head_segment ) );
1261 VertexRemap::z( point.vertex ) = arrowhead_radius * static_cast<float>( sin( i * head_segment ) );
1262 NormalRemap::x( point.normal ) = arrowhead_radius / arrowhead_length;
1263 NormalRemap::y( point.normal ) = static_cast<float>( cos( i * head_segment ) );
1264 NormalRemap::z( point.normal ) = static_cast<float>( sin( i * head_segment ) );
1267 FlatShadedVertex& point = vertices[i * 6 + 1];
1268 VertexRemap::x( point.vertex ) = length;
1269 VertexRemap::y( point.vertex ) = 0;
1270 VertexRemap::z( point.vertex ) = 0;
1271 NormalRemap::x( point.normal ) = arrowhead_radius / arrowhead_length;
1272 NormalRemap::y( point.normal ) = static_cast<float>( cos( ( i + 0.5 ) * head_segment ) );
1273 NormalRemap::z( point.normal ) = static_cast<float>( sin( ( i + 0.5 ) * head_segment ) );
1276 FlatShadedVertex& point = vertices[i * 6 + 2];
1277 VertexRemap::x( point.vertex ) = length - arrowhead_length;
1278 VertexRemap::y( point.vertex ) = arrowhead_radius * static_cast<float>( cos( ( i + 1 ) * head_segment ) );
1279 VertexRemap::z( point.vertex ) = arrowhead_radius * static_cast<float>( sin( ( i + 1 ) * head_segment ) );
1280 NormalRemap::x( point.normal ) = arrowhead_radius / arrowhead_length;
1281 NormalRemap::y( point.normal ) = static_cast<float>( cos( ( i + 1 ) * head_segment ) );
1282 NormalRemap::z( point.normal ) = static_cast<float>( sin( ( i + 1 ) * head_segment ) );
1286 FlatShadedVertex& point = vertices[i * 6 + 3];
1287 VertexRemap::x( point.vertex ) = length - arrowhead_length;
1288 VertexRemap::y( point.vertex ) = 0;
1289 VertexRemap::z( point.vertex ) = 0;
1290 NormalRemap::x( point.normal ) = -1;
1291 NormalRemap::y( point.normal ) = 0;
1292 NormalRemap::z( point.normal ) = 0;
1295 FlatShadedVertex& point = vertices[i * 6 + 4];
1296 VertexRemap::x( point.vertex ) = length - arrowhead_length;
1297 VertexRemap::y( point.vertex ) = arrowhead_radius * static_cast<float>( cos( i * head_segment ) );
1298 VertexRemap::z( point.vertex ) = arrowhead_radius * static_cast<float>( sin( i * head_segment ) );
1299 NormalRemap::x( point.normal ) = -1;
1300 NormalRemap::y( point.normal ) = 0;
1301 NormalRemap::z( point.normal ) = 0;
1304 FlatShadedVertex& point = vertices[i * 6 + 5];
1305 VertexRemap::x( point.vertex ) = length - arrowhead_length;
1306 VertexRemap::y( point.vertex ) = arrowhead_radius * static_cast<float>( cos( ( i + 1 ) * head_segment ) );
1307 VertexRemap::z( point.vertex ) = arrowhead_radius * static_cast<float>( sin( ( i + 1 ) * head_segment ) );
1308 NormalRemap::x( point.normal ) = -1;
1309 NormalRemap::y( point.normal ) = 0;
1310 NormalRemap::z( point.normal ) = 0;
1315 template<typename Triple>
1316 class TripleRemapXYZ
1319 static float& x( Triple& triple ){
1322 static float& y( Triple& triple ){
1325 static float& z( Triple& triple ){
1330 template<typename Triple>
1331 class TripleRemapYZX
1334 static float& x( Triple& triple ){
1337 static float& y( Triple& triple ){
1340 static float& z( Triple& triple ){
1345 template<typename Triple>
1346 class TripleRemapZXY
1349 static float& x( Triple& triple ){
1352 static float& y( Triple& triple ){
1355 static float& z( Triple& triple ){
1360 void vector3_print( const Vector3& v ){
1361 globalOutputStream() << "( " << v.x() << " " << v.y() << " " << v.z() << " )";
1364 class TranslateManipulator : public Manipulator
1366 struct RenderableArrowLine : public OpenGLRenderable
1368 PointVertex m_line[2];
1370 RenderableArrowLine(){
1372 void render( RenderStateFlags state ) const {
1373 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_line[0].colour );
1374 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_line[0].vertex );
1375 glDrawArrays( GL_LINES, 0, 2 );
1377 void setColour( const Colour4b& colour ){
1378 m_line[0].colour = colour;
1379 m_line[1].colour = colour;
1382 struct RenderableArrowHead : public OpenGLRenderable
1384 Array<FlatShadedVertex> m_vertices;
1386 RenderableArrowHead( std::size_t size )
1387 : m_vertices( size ){
1389 void render( RenderStateFlags state ) const {
1390 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( FlatShadedVertex ), &m_vertices.data()->colour );
1391 glVertexPointer( 3, GL_FLOAT, sizeof( FlatShadedVertex ), &m_vertices.data()->vertex );
1392 glNormalPointer( GL_FLOAT, sizeof( FlatShadedVertex ), &m_vertices.data()->normal );
1393 glDrawArrays( GL_TRIANGLES, 0, GLsizei( m_vertices.size() ) );
1395 void setColour( const Colour4b& colour ){
1396 for ( Array<FlatShadedVertex>::iterator i = m_vertices.begin(); i != m_vertices.end(); ++i )
1398 ( *i ).colour = colour;
1402 struct RenderableQuad : public OpenGLRenderable
1404 PointVertex m_quad[4];
1405 void render( RenderStateFlags state ) const {
1406 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_quad[0].colour );
1407 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_quad[0].vertex );
1408 glDrawArrays( GL_LINE_LOOP, 0, 4 );
1410 void setColour( const Colour4b& colour ){
1411 m_quad[0].colour = colour;
1412 m_quad[1].colour = colour;
1413 m_quad[2].colour = colour;
1414 m_quad[3].colour = colour;
1418 TranslateFree m_free;
1419 TranslateAxis m_axis;
1420 RenderableArrowLine m_arrow_x;
1421 RenderableArrowLine m_arrow_y;
1422 RenderableArrowLine m_arrow_z;
1423 RenderableArrowHead m_arrow_head_x;
1424 RenderableArrowHead m_arrow_head_y;
1425 RenderableArrowHead m_arrow_head_z;
1426 RenderableQuad m_quad_screen;
1427 SelectableBool m_selectable_x;
1428 SelectableBool m_selectable_y;
1429 SelectableBool m_selectable_z;
1430 SelectableBool m_selectable_screen;
1431 Pivot2World m_pivot;
1433 static Shader* m_state_wire;
1434 static Shader* m_state_fill;
1436 TranslateManipulator( Translatable& translatable, std::size_t segments, float length ) :
1437 m_free( translatable ),
1438 m_axis( translatable ),
1439 m_arrow_head_x( 3 * 2 * ( segments << 3 ) ),
1440 m_arrow_head_y( 3 * 2 * ( segments << 3 ) ),
1441 m_arrow_head_z( 3 * 2 * ( segments << 3 ) ){
1442 draw_arrowline( length, m_arrow_x.m_line, 0 );
1443 draw_arrowhead( segments, length, m_arrow_head_x.m_vertices.data(), TripleRemapXYZ<Vertex3f>(), TripleRemapXYZ<Normal3f>() );
1444 draw_arrowline( length, m_arrow_y.m_line, 1 );
1445 draw_arrowhead( segments, length, m_arrow_head_y.m_vertices.data(), TripleRemapYZX<Vertex3f>(), TripleRemapYZX<Normal3f>() );
1446 draw_arrowline( length, m_arrow_z.m_line, 2 );
1447 draw_arrowhead( segments, length, m_arrow_head_z.m_vertices.data(), TripleRemapZXY<Vertex3f>(), TripleRemapZXY<Normal3f>() );
1449 draw_quad( 16, m_quad_screen.m_quad );
1452 void UpdateColours(){
1453 m_arrow_x.setColour( colourSelected( g_colour_x, m_selectable_x.isSelected() ) );
1454 m_arrow_head_x.setColour( colourSelected( g_colour_x, m_selectable_x.isSelected() ) );
1455 m_arrow_y.setColour( colourSelected( g_colour_y, m_selectable_y.isSelected() ) );
1456 m_arrow_head_y.setColour( colourSelected( g_colour_y, m_selectable_y.isSelected() ) );
1457 m_arrow_z.setColour( colourSelected( g_colour_z, m_selectable_z.isSelected() ) );
1458 m_arrow_head_z.setColour( colourSelected( g_colour_z, m_selectable_z.isSelected() ) );
1459 m_quad_screen.setColour( colourSelected( g_colour_screen, m_selectable_screen.isSelected() ) );
1462 bool manipulator_show_axis( const Pivot2World& pivot, const Vector3& axis ){
1463 return fabs( vector3_dot( pivot.m_axis_screen, axis ) ) < 0.95;
1466 void render( Renderer& renderer, const VolumeTest& volume, const Matrix4& pivot2world ){
1467 m_pivot.update( pivot2world, volume.GetModelview(), volume.GetProjection(), volume.GetViewport() );
1472 Vector3 x = vector3_normalised( vector4_to_vector3( m_pivot.m_worldSpace.x() ) );
1473 bool show_x = manipulator_show_axis( m_pivot, x );
1475 Vector3 y = vector3_normalised( vector4_to_vector3( m_pivot.m_worldSpace.y() ) );
1476 bool show_y = manipulator_show_axis( m_pivot, y );
1478 Vector3 z = vector3_normalised( vector4_to_vector3( m_pivot.m_worldSpace.z() ) );
1479 bool show_z = manipulator_show_axis( m_pivot, z );
1481 renderer.SetState( m_state_wire, Renderer::eWireframeOnly );
1482 renderer.SetState( m_state_wire, Renderer::eFullMaterials );
1485 renderer.addRenderable( m_arrow_x, m_pivot.m_worldSpace );
1488 renderer.addRenderable( m_arrow_y, m_pivot.m_worldSpace );
1491 renderer.addRenderable( m_arrow_z, m_pivot.m_worldSpace );
1494 renderer.addRenderable( m_quad_screen, m_pivot.m_viewplaneSpace );
1496 renderer.SetState( m_state_fill, Renderer::eWireframeOnly );
1497 renderer.SetState( m_state_fill, Renderer::eFullMaterials );
1500 renderer.addRenderable( m_arrow_head_x, m_pivot.m_worldSpace );
1503 renderer.addRenderable( m_arrow_head_y, m_pivot.m_worldSpace );
1506 renderer.addRenderable( m_arrow_head_z, m_pivot.m_worldSpace );
1509 void testSelect( const View& view, const Matrix4& pivot2world ){
1510 m_pivot.update( pivot2world, view.GetModelview(), view.GetProjection(), view.GetViewport() );
1512 SelectionPool selector;
1514 Vector3 x = vector3_normalised( vector4_to_vector3( m_pivot.m_worldSpace.x() ) );
1515 bool show_x = manipulator_show_axis( m_pivot, x );
1517 Vector3 y = vector3_normalised( vector4_to_vector3( m_pivot.m_worldSpace.y() ) );
1518 bool show_y = manipulator_show_axis( m_pivot, y );
1520 Vector3 z = vector3_normalised( vector4_to_vector3( m_pivot.m_worldSpace.z() ) );
1521 bool show_z = manipulator_show_axis( m_pivot, z );
1524 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_pivot.m_viewpointSpace ) );
1527 SelectionIntersection best;
1528 Quad_BestPoint( local2view, eClipCullCW, m_quad_screen.m_quad, best );
1529 if ( best.valid() ) {
1530 best = SelectionIntersection( 0, 0 );
1531 selector.addSelectable( best, &m_selectable_screen );
1537 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_pivot.m_worldSpace ) );
1539 #if defined( DEBUG_SELECTION )
1540 g_render_clipped.construct( view.GetViewMatrix() );
1544 SelectionIntersection best;
1545 Line_BestPoint( local2view, m_arrow_x.m_line, best );
1546 Triangles_BestPoint( local2view, eClipCullCW, m_arrow_head_x.m_vertices.begin(), m_arrow_head_x.m_vertices.end(), best );
1547 selector.addSelectable( best, &m_selectable_x );
1551 SelectionIntersection best;
1552 Line_BestPoint( local2view, m_arrow_y.m_line, best );
1553 Triangles_BestPoint( local2view, eClipCullCW, m_arrow_head_y.m_vertices.begin(), m_arrow_head_y.m_vertices.end(), best );
1554 selector.addSelectable( best, &m_selectable_y );
1558 SelectionIntersection best;
1559 Line_BestPoint( local2view, m_arrow_z.m_line, best );
1560 Triangles_BestPoint( local2view, eClipCullCW, m_arrow_head_z.m_vertices.begin(), m_arrow_head_z.m_vertices.end(), best );
1561 selector.addSelectable( best, &m_selectable_z );
1565 if ( !selector.failed() ) {
1566 ( *selector.begin() ).second->setSelected( true );
1570 Manipulatable* GetManipulatable(){
1571 if ( m_selectable_x.isSelected() ) {
1572 m_axis.SetAxis( g_vector3_axis_x );
1575 else if ( m_selectable_y.isSelected() ) {
1576 m_axis.SetAxis( g_vector3_axis_y );
1579 else if ( m_selectable_z.isSelected() ) {
1580 m_axis.SetAxis( g_vector3_axis_z );
1589 void setSelected( bool select ){
1590 m_selectable_x.setSelected( select );
1591 m_selectable_y.setSelected( select );
1592 m_selectable_z.setSelected( select );
1593 m_selectable_screen.setSelected( select );
1595 bool isSelected() const {
1596 return m_selectable_x.isSelected()
1597 | m_selectable_y.isSelected()
1598 | m_selectable_z.isSelected()
1599 | m_selectable_screen.isSelected();
1603 Shader* TranslateManipulator::m_state_wire;
1604 Shader* TranslateManipulator::m_state_fill;
1606 class ScaleManipulator : public Manipulator
1608 struct RenderableArrow : public OpenGLRenderable
1610 PointVertex m_line[2];
1612 void render( RenderStateFlags state ) const {
1613 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_line[0].colour );
1614 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_line[0].vertex );
1615 glDrawArrays( GL_LINES, 0, 2 );
1617 void setColour( const Colour4b& colour ){
1618 m_line[0].colour = colour;
1619 m_line[1].colour = colour;
1622 struct RenderableQuad : public OpenGLRenderable
1624 PointVertex m_quad[4];
1625 void render( RenderStateFlags state ) const {
1626 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_quad[0].colour );
1627 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_quad[0].vertex );
1628 glDrawArrays( GL_QUADS, 0, 4 );
1630 void setColour( const Colour4b& colour ){
1631 m_quad[0].colour = colour;
1632 m_quad[1].colour = colour;
1633 m_quad[2].colour = colour;
1634 m_quad[3].colour = colour;
1640 RenderableArrow m_arrow_x;
1641 RenderableArrow m_arrow_y;
1642 RenderableArrow m_arrow_z;
1643 RenderableQuad m_quad_screen;
1644 SelectableBool m_selectable_x;
1645 SelectableBool m_selectable_y;
1646 SelectableBool m_selectable_z;
1647 SelectableBool m_selectable_screen;
1648 Pivot2World m_pivot;
1650 ScaleManipulator( Scalable& scalable, std::size_t segments, float length ) :
1653 draw_arrowline( length, m_arrow_x.m_line, 0 );
1654 draw_arrowline( length, m_arrow_y.m_line, 1 );
1655 draw_arrowline( length, m_arrow_z.m_line, 2 );
1657 draw_quad( 16, m_quad_screen.m_quad );
1660 Pivot2World& getPivot(){
1664 void UpdateColours(){
1665 m_arrow_x.setColour( colourSelected( g_colour_x, m_selectable_x.isSelected() ) );
1666 m_arrow_y.setColour( colourSelected( g_colour_y, m_selectable_y.isSelected() ) );
1667 m_arrow_z.setColour( colourSelected( g_colour_z, m_selectable_z.isSelected() ) );
1668 m_quad_screen.setColour( colourSelected( g_colour_screen, m_selectable_screen.isSelected() ) );
1671 void render( Renderer& renderer, const VolumeTest& volume, const Matrix4& pivot2world ){
1672 m_pivot.update( pivot2world, volume.GetModelview(), volume.GetProjection(), volume.GetViewport() );
1677 renderer.addRenderable( m_arrow_x, m_pivot.m_worldSpace );
1678 renderer.addRenderable( m_arrow_y, m_pivot.m_worldSpace );
1679 renderer.addRenderable( m_arrow_z, m_pivot.m_worldSpace );
1681 renderer.addRenderable( m_quad_screen, m_pivot.m_viewpointSpace );
1683 void testSelect( const View& view, const Matrix4& pivot2world ){
1684 m_pivot.update( pivot2world, view.GetModelview(), view.GetProjection(), view.GetViewport() );
1686 SelectionPool selector;
1689 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_pivot.m_worldSpace ) );
1691 #if defined( DEBUG_SELECTION )
1692 g_render_clipped.construct( view.GetViewMatrix() );
1696 SelectionIntersection best;
1697 Line_BestPoint( local2view, m_arrow_x.m_line, best );
1698 selector.addSelectable( best, &m_selectable_x );
1702 SelectionIntersection best;
1703 Line_BestPoint( local2view, m_arrow_y.m_line, best );
1704 selector.addSelectable( best, &m_selectable_y );
1708 SelectionIntersection best;
1709 Line_BestPoint( local2view, m_arrow_z.m_line, best );
1710 selector.addSelectable( best, &m_selectable_z );
1715 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_pivot.m_viewpointSpace ) );
1718 SelectionIntersection best;
1719 Quad_BestPoint( local2view, eClipCullCW, m_quad_screen.m_quad, best );
1720 selector.addSelectable( best, &m_selectable_screen );
1724 if ( !selector.failed() ) {
1725 ( *selector.begin() ).second->setSelected( true );
1729 Manipulatable* GetManipulatable(){
1730 if ( m_selectable_x.isSelected() ) {
1731 m_axis.SetAxis( g_vector3_axis_x );
1734 else if ( m_selectable_y.isSelected() ) {
1735 m_axis.SetAxis( g_vector3_axis_y );
1738 else if ( m_selectable_z.isSelected() ) {
1739 m_axis.SetAxis( g_vector3_axis_z );
1747 void setSelected( bool select ){
1748 m_selectable_x.setSelected( select );
1749 m_selectable_y.setSelected( select );
1750 m_selectable_z.setSelected( select );
1751 m_selectable_screen.setSelected( select );
1753 bool isSelected() const {
1754 return m_selectable_x.isSelected()
1755 | m_selectable_y.isSelected()
1756 | m_selectable_z.isSelected()
1757 | m_selectable_screen.isSelected();
1762 inline PlaneSelectable* Instance_getPlaneSelectable( scene::Instance& instance ){
1763 return InstanceTypeCast<PlaneSelectable>::cast( instance );
1766 class PlaneSelectableSelectPlanes : public scene::Graph::Walker
1768 Selector& m_selector;
1769 SelectionTest& m_test;
1770 PlaneCallback m_selectedPlaneCallback;
1772 PlaneSelectableSelectPlanes( Selector& selector, SelectionTest& test, const PlaneCallback& selectedPlaneCallback )
1773 : m_selector( selector ), m_test( test ), m_selectedPlaneCallback( selectedPlaneCallback ){
1775 bool pre( const scene::Path& path, scene::Instance& instance ) const {
1776 if ( path.top().get().visible() ) {
1777 Selectable* selectable = Instance_getSelectable( instance );
1778 if ( selectable != 0 && selectable->isSelected() ) {
1779 PlaneSelectable* planeSelectable = Instance_getPlaneSelectable( instance );
1780 if ( planeSelectable != 0 ) {
1781 planeSelectable->selectPlanes( m_selector, m_test, m_selectedPlaneCallback );
1789 class PlaneSelectableSelectReversedPlanes : public scene::Graph::Walker
1791 Selector& m_selector;
1792 const SelectedPlanes& m_selectedPlanes;
1794 PlaneSelectableSelectReversedPlanes( Selector& selector, const SelectedPlanes& selectedPlanes )
1795 : m_selector( selector ), m_selectedPlanes( selectedPlanes ){
1797 bool pre( const scene::Path& path, scene::Instance& instance ) const {
1798 if ( path.top().get().visible() ) {
1799 Selectable* selectable = Instance_getSelectable( instance );
1800 if ( selectable != 0 && selectable->isSelected() ) {
1801 PlaneSelectable* planeSelectable = Instance_getPlaneSelectable( instance );
1802 if ( planeSelectable != 0 ) {
1803 planeSelectable->selectReversedPlanes( m_selector, m_selectedPlanes );
1811 void Scene_forEachPlaneSelectable_selectPlanes( scene::Graph& graph, Selector& selector, SelectionTest& test, const PlaneCallback& selectedPlaneCallback ){
1812 graph.traverse( PlaneSelectableSelectPlanes( selector, test, selectedPlaneCallback ) );
1815 void Scene_forEachPlaneSelectable_selectReversedPlanes( scene::Graph& graph, Selector& selector, const SelectedPlanes& selectedPlanes ){
1816 graph.traverse( PlaneSelectableSelectReversedPlanes( selector, selectedPlanes ) );
1823 bool operator()( const Plane3& plane, const Plane3& other ) const {
1824 if ( plane.a < other.a ) {
1827 if ( other.a < plane.a ) {
1831 if ( plane.b < other.b ) {
1834 if ( other.b < plane.b ) {
1838 if ( plane.c < other.c ) {
1841 if ( other.c < plane.c ) {
1845 if ( plane.d < other.d ) {
1848 if ( other.d < plane.d ) {
1856 typedef std::set<Plane3, PlaneLess> PlaneSet;
1858 inline void PlaneSet_insert( PlaneSet& self, const Plane3& plane ){
1859 self.insert( plane );
1862 inline bool PlaneSet_contains( const PlaneSet& self, const Plane3& plane ){
1863 return self.find( plane ) != self.end();
1867 class SelectedPlaneSet : public SelectedPlanes
1869 PlaneSet m_selectedPlanes;
1871 bool empty() const {
1872 return m_selectedPlanes.empty();
1875 void insert( const Plane3& plane ){
1876 PlaneSet_insert( m_selectedPlanes, plane );
1878 bool contains( const Plane3& plane ) const {
1879 return PlaneSet_contains( m_selectedPlanes, plane );
1881 typedef MemberCaller<SelectedPlaneSet, void(const Plane3&), &SelectedPlaneSet::insert> InsertCaller;
1885 bool Scene_forEachPlaneSelectable_selectPlanes( scene::Graph& graph, Selector& selector, SelectionTest& test ){
1886 SelectedPlaneSet selectedPlanes;
1888 Scene_forEachPlaneSelectable_selectPlanes( graph, selector, test, SelectedPlaneSet::InsertCaller( selectedPlanes ) );
1889 Scene_forEachPlaneSelectable_selectReversedPlanes( graph, selector, selectedPlanes );
1891 return !selectedPlanes.empty();
1897 class TestedBrushPlanesSelectVeritces : public scene::Graph::Walker
1899 SelectionTest& m_test;
1901 TestedBrushPlanesSelectVeritces( SelectionTest& test )
1904 bool pre( const scene::Path& path, scene::Instance& instance ) const {
1905 if ( path.top().get().visible() ) {
1906 Selectable* selectable = Instance_getSelectable( instance );
1907 if ( selectable != 0 && selectable->isSelected() ) {
1908 BrushInstance* brushInstance = Instance_getBrush( instance );
1909 if ( brushInstance != 0 ) {
1910 brushInstance->selectVerticesOnPlanes( m_test );
1918 void Scene_forEachTestedBrushPlane_selectVertices( scene::Graph& graph, SelectionTest& test ){
1919 graph.traverse( TestedBrushPlanesSelectVeritces( test ) );
1922 class BrushPlanesSelectVeritces : public scene::Graph::Walker
1924 SelectionTest& m_test;
1926 BrushPlanesSelectVeritces( SelectionTest& test )
1929 bool pre( const scene::Path& path, scene::Instance& instance ) const {
1930 if ( path.top().get().visible() ) {
1931 Selectable* selectable = Instance_getSelectable( instance );
1932 if ( selectable != 0 && selectable->isSelected() ) {
1933 BrushInstance* brushInstance = Instance_getBrush( instance );
1934 if ( brushInstance != 0 ) {
1935 brushInstance->selectVerticesOnPlanes( m_test );
1943 void Scene_forEachBrushPlane_selectVertices( scene::Graph& graph, SelectionTest& test ){
1944 graph.traverse( BrushPlanesSelectVeritces( test ) );
1947 void Scene_Translate_Component_Selected( scene::Graph& graph, const Vector3& translation );
1948 void Scene_Translate_Selected( scene::Graph& graph, const Vector3& translation );
1949 void Scene_TestSelect_Primitive( Selector& selector, SelectionTest& test, const VolumeTest& volume );
1950 void Scene_TestSelect_Component( Selector& selector, SelectionTest& test, const VolumeTest& volume, SelectionSystem::EComponentMode componentMode );
1951 void Scene_TestSelect_Component_Selected( Selector& selector, SelectionTest& test, const VolumeTest& volume, SelectionSystem::EComponentMode componentMode );
1952 void Scene_SelectAll_Component( bool select, SelectionSystem::EComponentMode componentMode );
1954 class ResizeTranslatable : public Translatable
1956 void translate( const Vector3& translation ){
1957 Scene_Translate_Component_Selected( GlobalSceneGraph(), translation );
1961 class DragTranslatable : public Translatable
1963 void translate( const Vector3& translation ){
1964 if ( GlobalSelectionSystem().Mode() == SelectionSystem::eComponent ) {
1965 Scene_Translate_Component_Selected( GlobalSceneGraph(), translation );
1969 Scene_Translate_Selected( GlobalSceneGraph(), translation );
1974 class SelectionVolume : public SelectionTest
1976 Matrix4 m_local2view;
1982 SelectionVolume( const View& view )
1986 const VolumeTest& getVolume() const {
1990 const Vector3& getNear() const {
1993 const Vector3& getFar() const {
1997 void BeginMesh( const Matrix4& localToWorld, bool twoSided ){
1998 m_local2view = matrix4_multiplied_by_matrix4( m_view.GetViewMatrix(), localToWorld );
2000 // Cull back-facing polygons based on winding being clockwise or counter-clockwise.
2001 // Don't cull if the view is wireframe and the polygons are two-sided.
2002 m_cull = twoSided && !m_view.fill() ? eClipCullNone : ( matrix4_handedness( localToWorld ) == MATRIX4_RIGHTHANDED ) ? eClipCullCW : eClipCullCCW;
2005 Matrix4 screen2world( matrix4_full_inverse( m_local2view ) );
2007 m_near = vector4_projected(
2008 matrix4_transformed_vector4(
2010 Vector4( 0, 0, -1, 1 )
2014 m_far = vector4_projected(
2015 matrix4_transformed_vector4(
2017 Vector4( 0, 0, 1, 1 )
2022 #if defined( DEBUG_SELECTION )
2023 g_render_clipped.construct( m_view.GetViewMatrix() );
2026 void TestPoint( const Vector3& point, SelectionIntersection& best ){
2028 if ( matrix4_clip_point( m_local2view, point, clipped ) == c_CLIP_PASS ) {
2029 best = select_point_from_clipped( clipped );
2032 void TestPolygon( const VertexPointer& vertices, std::size_t count, SelectionIntersection& best ){
2034 for ( std::size_t i = 0; i + 2 < count; ++i )
2037 matrix4_clip_triangle(
2039 reinterpret_cast<const Vector3&>( vertices[0] ),
2040 reinterpret_cast<const Vector3&>( vertices[i + 1] ),
2041 reinterpret_cast<const Vector3&>( vertices[i + 2] ),
2050 void TestLineLoop( const VertexPointer& vertices, std::size_t count, SelectionIntersection& best ){
2055 for ( VertexPointer::iterator i = vertices.begin(), end = i + count, prev = i + ( count - 1 ); i != end; prev = i, ++i )
2060 reinterpret_cast<const Vector3&>( ( *prev ) ),
2061 reinterpret_cast<const Vector3&>( ( *i ) ),
2070 void TestLineStrip( const VertexPointer& vertices, std::size_t count, SelectionIntersection& best ){
2075 for ( VertexPointer::iterator i = vertices.begin(), end = i + count, next = i + 1; next != end; i = next, ++next )
2080 reinterpret_cast<const Vector3&>( ( *i ) ),
2081 reinterpret_cast<const Vector3&>( ( *next ) ),
2090 void TestLines( const VertexPointer& vertices, std::size_t count, SelectionIntersection& best ){
2095 for ( VertexPointer::iterator i = vertices.begin(), end = i + count; i != end; i += 2 )
2100 reinterpret_cast<const Vector3&>( ( *i ) ),
2101 reinterpret_cast<const Vector3&>( ( *( i + 1 ) ) ),
2110 void TestTriangles( const VertexPointer& vertices, const IndexPointer& indices, SelectionIntersection& best ){
2112 for ( IndexPointer::iterator i( indices.begin() ); i != indices.end(); i += 3 )
2115 matrix4_clip_triangle(
2117 reinterpret_cast<const Vector3&>( vertices[*i] ),
2118 reinterpret_cast<const Vector3&>( vertices[*( i + 1 )] ),
2119 reinterpret_cast<const Vector3&>( vertices[*( i + 2 )] ),
2128 void TestQuads( const VertexPointer& vertices, const IndexPointer& indices, SelectionIntersection& best ){
2130 for ( IndexPointer::iterator i( indices.begin() ); i != indices.end(); i += 4 )
2133 matrix4_clip_triangle(
2135 reinterpret_cast<const Vector3&>( vertices[*i] ),
2136 reinterpret_cast<const Vector3&>( vertices[*( i + 1 )] ),
2137 reinterpret_cast<const Vector3&>( vertices[*( i + 3 )] ),
2145 matrix4_clip_triangle(
2147 reinterpret_cast<const Vector3&>( vertices[*( i + 1 )] ),
2148 reinterpret_cast<const Vector3&>( vertices[*( i + 2 )] ),
2149 reinterpret_cast<const Vector3&>( vertices[*( i + 3 )] ),
2158 void TestQuadStrip( const VertexPointer& vertices, const IndexPointer& indices, SelectionIntersection& best ){
2160 for ( IndexPointer::iterator i( indices.begin() ); i + 2 != indices.end(); i += 2 )
2163 matrix4_clip_triangle(
2165 reinterpret_cast<const Vector3&>( vertices[*i] ),
2166 reinterpret_cast<const Vector3&>( vertices[*( i + 1 )] ),
2167 reinterpret_cast<const Vector3&>( vertices[*( i + 2 )] ),
2175 matrix4_clip_triangle(
2177 reinterpret_cast<const Vector3&>( vertices[*( i + 2 )] ),
2178 reinterpret_cast<const Vector3&>( vertices[*( i + 1 )] ),
2179 reinterpret_cast<const Vector3&>( vertices[*( i + 3 )] ),
2190 class SelectionCounter
2193 using func = void(const Selectable &);
2195 SelectionCounter( const SelectionChangeCallback& onchanged )
2196 : m_count( 0 ), m_onchanged( onchanged ){
2198 void operator()( const Selectable& selectable ){
2199 if ( selectable.isSelected() ) {
2204 ASSERT_MESSAGE( m_count != 0, "selection counter underflow" );
2208 m_onchanged( selectable );
2210 bool empty() const {
2211 return m_count == 0;
2213 std::size_t size() const {
2217 std::size_t m_count;
2218 SelectionChangeCallback m_onchanged;
2221 inline void ConstructSelectionTest( View& view, const rect_t selection_box ){
2222 view.EnableScissor( selection_box.min[0], selection_box.max[0], selection_box.min[1], selection_box.max[1] );
2225 inline const rect_t SelectionBoxForPoint( const float device_point[2], const float device_epsilon[2] ){
2226 rect_t selection_box;
2227 selection_box.min[0] = device_point[0] - device_epsilon[0];
2228 selection_box.min[1] = device_point[1] - device_epsilon[1];
2229 selection_box.max[0] = device_point[0] + device_epsilon[0];
2230 selection_box.max[1] = device_point[1] + device_epsilon[1];
2231 return selection_box;
2234 inline const rect_t SelectionBoxForArea( const float device_point[2], const float device_delta[2] ){
2235 rect_t selection_box;
2236 selection_box.min[0] = ( device_delta[0] < 0 ) ? ( device_point[0] + device_delta[0] ) : ( device_point[0] );
2237 selection_box.min[1] = ( device_delta[1] < 0 ) ? ( device_point[1] + device_delta[1] ) : ( device_point[1] );
2238 selection_box.max[0] = ( device_delta[0] > 0 ) ? ( device_point[0] + device_delta[0] ) : ( device_point[0] );
2239 selection_box.max[1] = ( device_delta[1] > 0 ) ? ( device_point[1] + device_delta[1] ) : ( device_point[1] );
2240 return selection_box;
2243 Quaternion construct_local_rotation( const Quaternion& world, const Quaternion& localToWorld ){
2244 return quaternion_normalised( quaternion_multiplied_by_quaternion(
2245 quaternion_normalised( quaternion_multiplied_by_quaternion(
2246 quaternion_inverse( localToWorld ),
2253 inline void matrix4_assign_rotation( Matrix4& matrix, const Matrix4& other ){
2254 matrix[0] = other[0];
2255 matrix[1] = other[1];
2256 matrix[2] = other[2];
2257 matrix[4] = other[4];
2258 matrix[5] = other[5];
2259 matrix[6] = other[6];
2260 matrix[8] = other[8];
2261 matrix[9] = other[9];
2262 matrix[10] = other[10];
2265 void matrix4_assign_rotation_for_pivot( Matrix4& matrix, scene::Instance& instance ){
2266 Editable* editable = Node_getEditable( instance.path().top() );
2267 if ( editable != 0 ) {
2268 matrix4_assign_rotation( matrix, matrix4_multiplied_by_matrix4( instance.localToWorld(), editable->getLocalPivot() ) );
2272 matrix4_assign_rotation( matrix, instance.localToWorld() );
2276 inline bool Instance_isSelectedComponents( scene::Instance& instance ){
2277 ComponentSelectionTestable* componentSelectionTestable = Instance_getComponentSelectionTestable( instance );
2278 return componentSelectionTestable != 0
2279 && componentSelectionTestable->isSelectedComponents();
2282 class TranslateSelected : public SelectionSystem::Visitor
2284 const Vector3& m_translate;
2286 TranslateSelected( const Vector3& translate )
2287 : m_translate( translate ){
2289 void visit( scene::Instance& instance ) const {
2290 Transformable* transform = Instance_getTransformable( instance );
2291 if ( transform != 0 ) {
2292 transform->setType( TRANSFORM_PRIMITIVE );
2293 transform->setTranslation( m_translate );
2298 void Scene_Translate_Selected( scene::Graph& graph, const Vector3& translation ){
2299 if ( GlobalSelectionSystem().countSelected() != 0 ) {
2300 GlobalSelectionSystem().foreachSelected( TranslateSelected( translation ) );
2304 Vector3 get_local_pivot( const Vector3& world_pivot, const Matrix4& localToWorld ){
2306 matrix4_transformed_point(
2307 matrix4_full_inverse( localToWorld ),
2313 void translation_for_pivoted_matrix_transform( Vector3& parent_translation, const Matrix4& local_transform, const Vector3& world_pivot, const Matrix4& localToWorld, const Matrix4& localToParent ){
2314 // we need a translation inside the parent system to move the origin of this object to the right place
2316 // mathematically, it must fulfill:
2318 // local_translation local_transform local_pivot = local_pivot
2319 // local_translation = local_pivot - local_transform local_pivot
2322 // local_transform local_translation local_pivot = local_pivot
2323 // local_translation local_pivot = local_transform^-1 local_pivot
2324 // local_translation + local_pivot = local_transform^-1 local_pivot
2325 // local_translation = local_transform^-1 local_pivot - local_pivot
2327 Vector3 local_pivot( get_local_pivot( world_pivot, localToWorld ) );
2329 Vector3 local_translation(
2332 matrix4_transformed_point(
2337 matrix4_transformed_point(
2338 matrix4_full_inverse(local_transform),
2346 translation_local2object( parent_translation, local_translation, localToParent );
2350 globalOutputStream() << "World pivot is at " << world_pivot << "\n";
2351 globalOutputStream() << "Local pivot is at " << local_pivot << "\n";
2352 globalOutputStream() << "Transformation " << local_transform << " moves it to: " << matrix4_transformed_point(local_transform, local_pivot) << "\n";
2353 globalOutputStream() << "Must move by " << local_translation << " in the local system" << "\n";
2354 globalOutputStream() << "Must move by " << parent_translation << " in the parent system" << "\n";
2358 void translation_for_pivoted_rotation( Vector3& parent_translation, const Quaternion& local_rotation, const Vector3& world_pivot, const Matrix4& localToWorld, const Matrix4& localToParent ){
2359 translation_for_pivoted_matrix_transform( parent_translation, matrix4_rotation_for_quaternion_quantised( local_rotation ), world_pivot, localToWorld, localToParent );
2362 void translation_for_pivoted_scale( Vector3& parent_translation, const Vector3& world_scale, const Vector3& world_pivot, const Matrix4& localToWorld, const Matrix4& localToParent ){
2363 Matrix4 local_transform(
2364 matrix4_multiplied_by_matrix4(
2365 matrix4_full_inverse( localToWorld ),
2366 matrix4_multiplied_by_matrix4(
2367 matrix4_scale_for_vec3( world_scale ),
2372 local_transform.tx() = local_transform.ty() = local_transform.tz() = 0; // cancel translation parts
2373 translation_for_pivoted_matrix_transform( parent_translation, local_transform, world_pivot, localToWorld, localToParent );
2376 class rotate_selected : public SelectionSystem::Visitor
2378 const Quaternion& m_rotate;
2379 const Vector3& m_world_pivot;
2381 rotate_selected( const Quaternion& rotation, const Vector3& world_pivot )
2382 : m_rotate( rotation ), m_world_pivot( world_pivot ){
2384 void visit( scene::Instance& instance ) const {
2385 TransformNode* transformNode = Node_getTransformNode( instance.path().top() );
2386 if ( transformNode != 0 ) {
2387 Transformable* transform = Instance_getTransformable( instance );
2388 if ( transform != 0 ) {
2389 transform->setType( TRANSFORM_PRIMITIVE );
2390 transform->setScale( c_scale_identity );
2391 transform->setTranslation( c_translation_identity );
2393 transform->setType( TRANSFORM_PRIMITIVE );
2394 transform->setRotation( m_rotate );
2397 Editable* editable = Node_getEditable( instance.path().top() );
2398 const Matrix4& localPivot = editable != 0 ? editable->getLocalPivot() : g_matrix4_identity;
2400 Vector3 parent_translation;
2401 translation_for_pivoted_rotation(
2405 matrix4_multiplied_by_matrix4( instance.localToWorld(), localPivot ),
2406 matrix4_multiplied_by_matrix4( transformNode->localToParent(), localPivot )
2409 transform->setTranslation( parent_translation );
2416 void Scene_Rotate_Selected( scene::Graph& graph, const Quaternion& rotation, const Vector3& world_pivot ){
2417 if ( GlobalSelectionSystem().countSelected() != 0 ) {
2418 GlobalSelectionSystem().foreachSelected( rotate_selected( rotation, world_pivot ) );
2422 class scale_selected : public SelectionSystem::Visitor
2424 const Vector3& m_scale;
2425 const Vector3& m_world_pivot;
2427 scale_selected( const Vector3& scaling, const Vector3& world_pivot )
2428 : m_scale( scaling ), m_world_pivot( world_pivot ){
2430 void visit( scene::Instance& instance ) const {
2431 TransformNode* transformNode = Node_getTransformNode( instance.path().top() );
2432 if ( transformNode != 0 ) {
2433 Transformable* transform = Instance_getTransformable( instance );
2434 if ( transform != 0 ) {
2435 transform->setType( TRANSFORM_PRIMITIVE );
2436 transform->setScale( c_scale_identity );
2437 transform->setTranslation( c_translation_identity );
2439 transform->setType( TRANSFORM_PRIMITIVE );
2440 transform->setScale( m_scale );
2442 Editable* editable = Node_getEditable( instance.path().top() );
2443 const Matrix4& localPivot = editable != 0 ? editable->getLocalPivot() : g_matrix4_identity;
2445 Vector3 parent_translation;
2446 translation_for_pivoted_scale(
2450 matrix4_multiplied_by_matrix4( instance.localToWorld(), localPivot ),
2451 matrix4_multiplied_by_matrix4( transformNode->localToParent(), localPivot )
2454 transform->setTranslation( parent_translation );
2461 void Scene_Scale_Selected( scene::Graph& graph, const Vector3& scaling, const Vector3& world_pivot ){
2462 if ( GlobalSelectionSystem().countSelected() != 0 ) {
2463 GlobalSelectionSystem().foreachSelected( scale_selected( scaling, world_pivot ) );
2468 class translate_component_selected : public SelectionSystem::Visitor
2470 const Vector3& m_translate;
2472 translate_component_selected( const Vector3& translate )
2473 : m_translate( translate ){
2475 void visit( scene::Instance& instance ) const {
2476 Transformable* transform = Instance_getTransformable( instance );
2477 if ( transform != 0 ) {
2478 transform->setType( TRANSFORM_COMPONENT );
2479 transform->setTranslation( m_translate );
2484 void Scene_Translate_Component_Selected( scene::Graph& graph, const Vector3& translation ){
2485 if ( GlobalSelectionSystem().countSelected() != 0 ) {
2486 GlobalSelectionSystem().foreachSelectedComponent( translate_component_selected( translation ) );
2490 class rotate_component_selected : public SelectionSystem::Visitor
2492 const Quaternion& m_rotate;
2493 const Vector3& m_world_pivot;
2495 rotate_component_selected( const Quaternion& rotation, const Vector3& world_pivot )
2496 : m_rotate( rotation ), m_world_pivot( world_pivot ){
2498 void visit( scene::Instance& instance ) const {
2499 Transformable* transform = Instance_getTransformable( instance );
2500 if ( transform != 0 ) {
2501 Vector3 parent_translation;
2502 translation_for_pivoted_rotation( parent_translation, m_rotate, m_world_pivot, instance.localToWorld(), Node_getTransformNode( instance.path().top() )->localToParent() );
2504 transform->setType( TRANSFORM_COMPONENT );
2505 transform->setRotation( m_rotate );
2506 transform->setTranslation( parent_translation );
2511 void Scene_Rotate_Component_Selected( scene::Graph& graph, const Quaternion& rotation, const Vector3& world_pivot ){
2512 if ( GlobalSelectionSystem().countSelectedComponents() != 0 ) {
2513 GlobalSelectionSystem().foreachSelectedComponent( rotate_component_selected( rotation, world_pivot ) );
2517 class scale_component_selected : public SelectionSystem::Visitor
2519 const Vector3& m_scale;
2520 const Vector3& m_world_pivot;
2522 scale_component_selected( const Vector3& scaling, const Vector3& world_pivot )
2523 : m_scale( scaling ), m_world_pivot( world_pivot ){
2525 void visit( scene::Instance& instance ) const {
2526 Transformable* transform = Instance_getTransformable( instance );
2527 if ( transform != 0 ) {
2528 Vector3 parent_translation;
2529 translation_for_pivoted_scale( parent_translation, m_scale, m_world_pivot, instance.localToWorld(), Node_getTransformNode( instance.path().top() )->localToParent() );
2531 transform->setType( TRANSFORM_COMPONENT );
2532 transform->setScale( m_scale );
2533 transform->setTranslation( parent_translation );
2538 void Scene_Scale_Component_Selected( scene::Graph& graph, const Vector3& scaling, const Vector3& world_pivot ){
2539 if ( GlobalSelectionSystem().countSelectedComponents() != 0 ) {
2540 GlobalSelectionSystem().foreachSelectedComponent( scale_component_selected( scaling, world_pivot ) );
2545 class BooleanSelector : public Selector
2548 SelectionIntersection m_intersection;
2549 Selectable* m_selectable;
2551 BooleanSelector() : m_selected( false ){
2554 void pushSelectable( Selectable& selectable ){
2555 m_intersection = SelectionIntersection();
2556 m_selectable = &selectable;
2558 void popSelectable(){
2559 if ( m_intersection.valid() ) {
2562 m_intersection = SelectionIntersection();
2564 void addIntersection( const SelectionIntersection& intersection ){
2565 if ( m_selectable->isSelected() ) {
2566 assign_if_closer( m_intersection, intersection );
2575 class BestSelector : public Selector
2577 SelectionIntersection m_intersection;
2578 Selectable* m_selectable;
2579 SelectionIntersection m_bestIntersection;
2580 std::list<Selectable*> m_bestSelectable;
2582 BestSelector() : m_bestIntersection( SelectionIntersection() ), m_bestSelectable( 0 ){
2585 void pushSelectable( Selectable& selectable ){
2586 m_intersection = SelectionIntersection();
2587 m_selectable = &selectable;
2589 void popSelectable(){
2590 if ( m_intersection.equalEpsilon( m_bestIntersection, 0.25f, 0.001f ) ) {
2591 m_bestSelectable.push_back( m_selectable );
2592 m_bestIntersection = m_intersection;
2594 else if ( m_intersection < m_bestIntersection ) {
2595 m_bestSelectable.clear();
2596 m_bestSelectable.push_back( m_selectable );
2597 m_bestIntersection = m_intersection;
2599 m_intersection = SelectionIntersection();
2601 void addIntersection( const SelectionIntersection& intersection ){
2602 assign_if_closer( m_intersection, intersection );
2605 std::list<Selectable*>& best(){
2606 return m_bestSelectable;
2610 class DeepBestSelector : public Selector
2612 SelectionIntersection m_intersection;
2613 Selectable* m_selectable;
2614 SelectionIntersection m_bestIntersection;
2615 std::list<Selectable*> m_bestSelectable;
2617 DeepBestSelector() : m_bestIntersection( SelectionIntersection() ), m_bestSelectable( 0 ){
2620 void pushSelectable( Selectable& selectable ){
2621 m_intersection = SelectionIntersection();
2622 m_selectable = &selectable;
2624 void popSelectable(){
2625 if ( m_intersection.equalEpsilon( m_bestIntersection, 0.25f, 2.f ) ) {
2626 m_bestSelectable.push_back( m_selectable );
2627 m_bestIntersection = m_intersection;
2629 else if ( m_intersection < m_bestIntersection ) {
2630 m_bestSelectable.clear();
2631 m_bestSelectable.push_back( m_selectable );
2632 m_bestIntersection = m_intersection;
2634 m_intersection = SelectionIntersection();
2636 void addIntersection( const SelectionIntersection& intersection ){
2637 assign_if_closer( m_intersection, intersection );
2640 std::list<Selectable*>& best(){
2641 return m_bestSelectable;
2645 bool g_bAltDragManipulatorResize = false;
2646 bool g_bTmpComponentMode = false;
2648 class DragManipulator : public Manipulator
2650 TranslateFree m_freeResize;
2651 TranslateFree m_freeDrag;
2652 ResizeTranslatable m_resize;
2653 DragTranslatable m_drag;
2654 SelectableBool m_dragSelectable; //drag already selected stuff
2657 bool m_selected; //selected temporally for drag
2659 DragManipulator() : m_freeResize( m_resize ), m_freeDrag( m_drag ), m_selected( false ){
2662 Manipulatable* GetManipulatable(){
2663 return m_dragSelectable.isSelected() ? &m_freeDrag : &m_freeResize;
2666 void testSelect( const View& view, const Matrix4& pivot2world ){
2667 SelectionPool selector;
2669 SelectionVolume test( view );
2671 if ( GlobalSelectionSystem().Mode() == SelectionSystem::ePrimitive ) {
2672 BooleanSelector booleanSelector;
2674 Scene_TestSelect_Primitive( booleanSelector, test, view );
2676 if ( booleanSelector.isSelected() ) {
2677 if( g_bAltDragManipulatorResize ){
2678 DeepBestSelector deepSelector;
2679 Scene_TestSelect_Component_Selected( deepSelector, test, view, SelectionSystem::eVertex );
2680 for ( std::list<Selectable*>::iterator i = deepSelector.best().begin(); i != deepSelector.best().end(); ++i )
2682 if ( !( *i )->isSelected() ) {
2683 GlobalSelectionSystem().setSelectedAllComponents( false );
2685 selector.addSelectable( SelectionIntersection( 0, 0 ), ( *i ) );
2687 m_dragSelectable.setSelected( false );
2689 if( deepSelector.best().empty() ){
2690 //Scene_forEachTestedBrushPlane_selectVertices( GlobalSceneGraph(), test ); //todo? drag clicked face
2691 Scene_forEachBrushPlane_selectVertices( GlobalSceneGraph(), test );
2696 selector.addSelectable( SelectionIntersection( 0, 0 ), &m_dragSelectable );
2702 if( g_bAltDragManipulatorResize ){
2703 Scene_forEachBrushPlane_selectVertices( GlobalSceneGraph(), test );
2707 m_selected = Scene_forEachPlaneSelectable_selectPlanes( GlobalSceneGraph(), selector, test );
2713 BestSelector bestSelector;
2714 Scene_TestSelect_Component_Selected( bestSelector, test, view, GlobalSelectionSystem().ComponentMode() );
2715 for ( std::list<Selectable*>::iterator i = bestSelector.best().begin(); i != bestSelector.best().end(); ++i )
2717 if ( !( *i )->isSelected() ) {
2718 GlobalSelectionSystem().setSelectedAllComponents( false );
2721 selector.addSelectable( SelectionIntersection( 0, 0 ), ( *i ) );
2722 m_dragSelectable.setSelected( true );
2724 if( GlobalSelectionSystem().countSelectedComponents() != 0 ){
2725 m_dragSelectable.setSelected( true );
2729 for ( SelectionPool::iterator i = selector.begin(); i != selector.end(); ++i )
2731 ( *i ).second->setSelected( true );
2733 g_bTmpComponentMode = m_selected;
2736 void setSelected( bool select ){
2737 m_selected = select;
2738 m_dragSelectable.setSelected( select );
2740 bool isSelected() const {
2741 return m_selected || m_dragSelectable.isSelected();
2745 class ClipManipulator : public Manipulator
2749 Manipulatable* GetManipulatable(){
2750 ERROR_MESSAGE( "clipper is not manipulatable" );
2754 void setSelected( bool select ){
2756 bool isSelected() const {
2761 class select_all : public scene::Graph::Walker
2765 select_all( bool select )
2766 : m_select( select ){
2768 bool pre( const scene::Path& path, scene::Instance& instance ) const {
2769 Selectable* selectable = Instance_getSelectable( instance );
2770 if ( selectable != 0 ) {
2771 selectable->setSelected( m_select );
2777 class select_all_component : public scene::Graph::Walker
2780 SelectionSystem::EComponentMode m_mode;
2782 select_all_component( bool select, SelectionSystem::EComponentMode mode )
2783 : m_select( select ), m_mode( mode ){
2785 bool pre( const scene::Path& path, scene::Instance& instance ) const {
2786 ComponentSelectionTestable* componentSelectionTestable = Instance_getComponentSelectionTestable( instance );
2787 if ( componentSelectionTestable ) {
2788 componentSelectionTestable->setSelectedComponents( m_select, m_mode );
2794 void Scene_SelectAll_Component( bool select, SelectionSystem::EComponentMode componentMode ){
2795 GlobalSceneGraph().traverse( select_all_component( select, componentMode ) );
2799 // RadiantSelectionSystem
2800 class RadiantSelectionSystem :
2801 public SelectionSystem,
2802 public Translatable,
2807 mutable Matrix4 m_pivot2world;
2808 Matrix4 m_pivot2world_start;
2809 Matrix4 m_manip2pivot_start;
2810 Translation m_translation;
2811 Rotation m_rotation;
2814 static Shader* m_state;
2815 bool m_bPreferPointEntsIn2D;
2817 EManipulatorMode m_manipulator_mode;
2818 Manipulator* m_manipulator;
2823 EComponentMode m_componentmode;
2825 SelectionCounter m_count_primitive;
2826 SelectionCounter m_count_component;
2828 TranslateManipulator m_translate_manipulator;
2829 RotateManipulator m_rotate_manipulator;
2830 ScaleManipulator m_scale_manipulator;
2831 DragManipulator m_drag_manipulator;
2832 ClipManipulator m_clip_manipulator;
2834 typedef SelectionList<scene::Instance> selection_t;
2835 selection_t m_selection;
2836 selection_t m_component_selection;
2838 Signal1<const Selectable&> m_selectionChanged_callbacks;
2840 void ConstructPivot() const;
2841 void setCustomPivotOrigin( Vector3& point ) const;
2843 AABB getSelectionAABB() const;
2845 mutable bool m_pivotChanged;
2846 bool m_pivot_moving;
2847 mutable bool m_pivotIsCustom;
2849 void Scene_TestSelect( Selector& selector, SelectionTest& test, const View& view, SelectionSystem::EMode mode, SelectionSystem::EComponentMode componentMode );
2851 bool nothingSelected() const {
2852 return ( Mode() == eComponent && m_count_component.empty() )
2853 || ( Mode() == ePrimitive && m_count_primitive.empty() );
2868 RadiantSelectionSystem() :
2869 m_bPreferPointEntsIn2D( true ),
2870 m_undo_begun( false ),
2871 m_mode( ePrimitive ),
2872 m_componentmode( eDefault ),
2873 m_count_primitive( SelectionChangedCaller( *this ) ),
2874 m_count_component( SelectionChangedCaller( *this ) ),
2875 m_translate_manipulator( *this, 2, 64 ),
2876 m_rotate_manipulator( *this, 8, 64 ),
2877 m_scale_manipulator( *this, 0, 64 ),
2878 m_pivotChanged( false ),
2879 m_pivot_moving( false ),
2880 m_pivotIsCustom( false ){
2881 SetManipulatorMode( eTranslate );
2883 addSelectionChangeCallback( PivotChangedSelectionCaller( *this ) );
2884 AddGridChangeCallback( PivotChangedCaller( *this ) );
2886 void pivotChanged() const {
2887 m_pivotChanged = true;
2888 SceneChangeNotify();
2890 typedef ConstMemberCaller<RadiantSelectionSystem, void(), &RadiantSelectionSystem::pivotChanged> PivotChangedCaller;
2891 void pivotChangedSelection( const Selectable& selectable ){
2894 typedef MemberCaller<RadiantSelectionSystem, void(const Selectable&), &RadiantSelectionSystem::pivotChangedSelection> PivotChangedSelectionCaller;
2896 void SetMode( EMode mode ){
2897 if ( m_mode != mode ) {
2902 EMode Mode() const {
2905 void SetComponentMode( EComponentMode mode ){
2906 m_componentmode = mode;
2908 EComponentMode ComponentMode() const {
2909 return m_componentmode;
2911 void SetManipulatorMode( EManipulatorMode mode ){
2912 m_pivotIsCustom = false;
2913 m_manipulator_mode = mode;
2914 switch ( m_manipulator_mode )
2916 case eTranslate: m_manipulator = &m_translate_manipulator; break;
2917 case eRotate: m_manipulator = &m_rotate_manipulator; break;
2918 case eScale: m_manipulator = &m_scale_manipulator; break;
2919 case eDrag: m_manipulator = &m_drag_manipulator; break;
2920 case eClip: m_manipulator = &m_clip_manipulator; break;
2924 EManipulatorMode ManipulatorMode() const {
2925 return m_manipulator_mode;
2928 SelectionChangeCallback getObserver( EMode mode ){
2929 if ( mode == ePrimitive ) {
2930 return makeCallback( m_count_primitive );
2934 return makeCallback( m_count_component );
2937 std::size_t countSelected() const {
2938 return m_count_primitive.size();
2940 std::size_t countSelectedComponents() const {
2941 return m_count_component.size();
2943 void onSelectedChanged( scene::Instance& instance, const Selectable& selectable ){
2944 if ( selectable.isSelected() ) {
2945 m_selection.append( instance );
2949 m_selection.erase( instance );
2952 ASSERT_MESSAGE( m_selection.size() == m_count_primitive.size(), "selection-tracking error" );
2954 void onComponentSelection( scene::Instance& instance, const Selectable& selectable ){
2955 if ( selectable.isSelected() ) {
2956 m_component_selection.append( instance );
2960 m_component_selection.erase( instance );
2963 ASSERT_MESSAGE( m_component_selection.size() == m_count_component.size(), "selection-tracking error" );
2965 scene::Instance& ultimateSelected() const {
2966 ASSERT_MESSAGE( m_selection.size() > 0, "no instance selected" );
2967 return m_selection.back();
2969 scene::Instance& penultimateSelected() const {
2970 ASSERT_MESSAGE( m_selection.size() > 1, "only one instance selected" );
2971 return *( *( --( --m_selection.end() ) ) );
2973 void setSelectedAll( bool selected ){
2974 GlobalSceneGraph().traverse( select_all( selected ) );
2976 m_manipulator->setSelected( selected );
2978 void setSelectedAllComponents( bool selected ){
2979 Scene_SelectAll_Component( selected, SelectionSystem::eVertex );
2980 Scene_SelectAll_Component( selected, SelectionSystem::eEdge );
2981 Scene_SelectAll_Component( selected, SelectionSystem::eFace );
2983 m_manipulator->setSelected( selected );
2986 void foreachSelected( const Visitor& visitor ) const {
2987 selection_t::const_iterator i = m_selection.begin();
2988 while ( i != m_selection.end() )
2990 visitor.visit( *( *( i++ ) ) );
2993 void foreachSelectedComponent( const Visitor& visitor ) const {
2994 selection_t::const_iterator i = m_component_selection.begin();
2995 while ( i != m_component_selection.end() )
2997 visitor.visit( *( *( i++ ) ) );
3001 void addSelectionChangeCallback( const SelectionChangeHandler& handler ){
3002 m_selectionChanged_callbacks.connectLast( handler );
3004 void selectionChanged( const Selectable& selectable ){
3005 m_selectionChanged_callbacks( selectable );
3007 typedef MemberCaller<RadiantSelectionSystem, void(const Selectable&), &RadiantSelectionSystem::selectionChanged> SelectionChangedCaller;
3011 m_pivot2world_start = GetPivot2World();
3014 bool SelectManipulator( const View& view, const float device_point[2], const float device_epsilon[2] ){
3015 if ( !nothingSelected() || ( ManipulatorMode() == eDrag && Mode() == eComponent ) ) {
3016 #if defined ( DEBUG_SELECTION )
3017 g_render_clipped.destroy();
3020 m_manipulator->setSelected( false );
3022 if ( !nothingSelected() || ( ManipulatorMode() == eDrag && Mode() == eComponent ) ) {
3023 View scissored( view );
3024 ConstructSelectionTest( scissored, SelectionBoxForPoint( device_point, device_epsilon ) );
3025 m_manipulator->testSelect( scissored, GetPivot2World() );
3030 m_pivot_moving = m_manipulator->isSelected();
3032 if ( m_pivot_moving ) {
3034 pivot.update( GetPivot2World(), view.GetModelview(), view.GetProjection(), view.GetViewport() );
3036 m_manip2pivot_start = matrix4_multiplied_by_matrix4( matrix4_full_inverse( m_pivot2world_start ), pivot.m_worldSpace );
3038 Matrix4 device2manip;
3039 ConstructDevice2Manip( device2manip, m_pivot2world_start, view.GetModelview(), view.GetProjection(), view.GetViewport() );
3040 m_manipulator->GetManipulatable()->Construct( device2manip, device_point[0], device_point[1], getSelectionAABB(), vector4_to_vector3( GetPivot2World().t() ) );
3042 m_undo_begun = false;
3045 SceneChangeNotify();
3048 return m_pivot_moving;
3052 if ( Mode() == eComponent ) {
3053 setSelectedAllComponents( false );
3057 setSelectedAll( false );
3061 void deselectComponentsOrAll( bool components ){
3063 setSelectedAllComponents( false );
3071 void SelectPoint( const View& view, const float device_point[2], const float device_epsilon[2], RadiantSelectionSystem::EModifier modifier, bool face ){
3072 //globalOutputStream() << device_point[0] << " " << device_point[1] << "\n";
3073 ASSERT_MESSAGE( fabs( device_point[0] ) <= 1.0f && fabs( device_point[1] ) <= 1.0f, "point-selection error" );
3075 if ( modifier == eReplace ) {
3076 deselectComponentsOrAll( face );
3079 //nothingSelected() doesn't consider faces, selected in non-component mode, m
3080 if ( modifier == eCycle && nothingSelected() ){
3081 modifier = eReplace;
3084 #if defined ( DEBUG_SELECTION )
3085 g_render_clipped.destroy();
3089 View scissored( view );
3090 ConstructSelectionTest( scissored, SelectionBoxForPoint( device_point, device_epsilon ) );
3092 SelectionVolume volume( scissored );
3093 SelectionPool selector;
3094 SelectionPool selector_point_ents;
3095 const bool prefer_point_ents = m_bPreferPointEntsIn2D && Mode() == ePrimitive && !view.fill() && !face
3096 && ( modifier == RadiantSelectionSystem::eReplace || modifier == RadiantSelectionSystem::eSelect || modifier == RadiantSelectionSystem::eDeselect );
3098 if( prefer_point_ents ){
3099 Scene_TestSelect( selector_point_ents, volume, scissored, eEntity, ComponentMode() );
3101 if( prefer_point_ents && !selector_point_ents.failed() ){
3104 // if cycle mode not enabled, enable it
3105 case RadiantSelectionSystem::eReplace:
3108 ( *selector_point_ents.begin() ).second->setSelected( true );
3111 case RadiantSelectionSystem::eSelect:
3113 SelectionPool::iterator best = selector_point_ents.begin();
3114 if( !( *best ).second->isSelected() ){
3115 ( *best ).second->setSelected( true );
3117 SelectionPool::iterator i = best;
3119 while ( i != selector_point_ents.end() )
3121 if( ( *i ).first.equalEpsilon( ( *best ).first, 0.25f, 0.000001f ) ){
3122 if( !( *i ).second->isSelected() ){
3123 ( *i ).second->setSelected( true );
3133 case RadiantSelectionSystem::eDeselect:
3135 SelectionPool::iterator best = selector_point_ents.begin();
3136 if( ( *best ).second->isSelected() ){
3137 ( *best ).second->setSelected( false );
3139 SelectionPool::iterator i = best;
3141 while ( i != selector_point_ents.end() )
3143 if( ( *i ).first.equalEpsilon( ( *best ).first, 0.25f, 0.000001f ) ){
3144 if( ( *i ).second->isSelected() ){
3145 ( *i ).second->setSelected( false );
3161 Scene_TestSelect_Component( selector, volume, scissored, eFace );
3164 Scene_TestSelect( selector, volume, scissored, Mode(), ComponentMode() );
3167 if ( !selector.failed() ) {
3170 case RadiantSelectionSystem::eToggle:
3172 SelectableSortedSet::iterator best = selector.begin();
3173 // toggle selection of the object with least depth
3174 if ( ( *best ).second->isSelected() ) {
3175 ( *best ).second->setSelected( false );
3178 ( *best ).second->setSelected( true );
3182 // if cycle mode not enabled, enable it
3183 case RadiantSelectionSystem::eReplace:
3186 ( *selector.begin() ).second->setSelected( true );
3189 // select the next object in the list from the one already selected
3190 case RadiantSelectionSystem::eCycle:
3192 bool CycleSelectionOccured = false;
3193 SelectionPool::iterator i = selector.begin();
3194 while ( i != selector.end() )
3196 if ( ( *i ).second->isSelected() ) {
3197 deselectComponentsOrAll( face );
3199 if ( i != selector.end() ) {
3200 i->second->setSelected( true );
3204 selector.begin()->second->setSelected( true );
3206 CycleSelectionOccured = true;
3211 if( !CycleSelectionOccured ){
3212 deselectComponentsOrAll( face );
3213 ( *selector.begin() ).second->setSelected( true );
3217 case RadiantSelectionSystem::eSelect:
3219 SelectionPool::iterator best = selector.begin();
3220 if( !( *best ).second->isSelected() ){
3221 ( *best ).second->setSelected( true );
3223 SelectionPool::iterator i = best;
3225 while ( i != selector.end() )
3227 if( ( *i ).first.equalEpsilon( ( *best ).first, 0.25f, 0.000001f ) ){
3228 if( !( *i ).second->isSelected() ){
3229 ( *i ).second->setSelected( true );
3239 case RadiantSelectionSystem::eDeselect:
3241 SelectionPool::iterator best = selector.begin();
3242 if( ( *best ).second->isSelected() ){
3243 ( *best ).second->setSelected( false );
3245 SelectionPool::iterator i = best;
3247 while ( i != selector.end() )
3249 if( ( *i ).first.equalEpsilon( ( *best ).first, 0.25f, 0.000001f ) ){
3250 if( ( *i ).second->isSelected() ){
3251 ( *i ).second->setSelected( false );
3265 else if( modifier == eCycle ){
3266 deselectComponentsOrAll( face );
3272 bool SelectPoint_InitPaint( const View& view, const float device_point[2], const float device_epsilon[2], bool face ){
3273 ASSERT_MESSAGE( fabs( device_point[0] ) <= 1.0f && fabs( device_point[1] ) <= 1.0f, "point-selection error" );
3274 #if defined ( DEBUG_SELECTION )
3275 g_render_clipped.destroy();
3279 View scissored( view );
3280 ConstructSelectionTest( scissored, SelectionBoxForPoint( device_point, device_epsilon ) );
3282 SelectionVolume volume( scissored );
3283 SelectionPool selector;
3284 SelectionPool selector_point_ents;
3285 const bool prefer_point_ents = m_bPreferPointEntsIn2D && Mode() == ePrimitive && !view.fill() && !face;
3287 if( prefer_point_ents ){
3288 Scene_TestSelect( selector_point_ents, volume, scissored, eEntity, ComponentMode() );
3290 if( prefer_point_ents && !selector_point_ents.failed() ){
3291 SelectableSortedSet::iterator best = selector_point_ents.begin();
3292 const bool wasSelected = ( *best ).second->isSelected();
3293 ( *best ).second->setSelected( !wasSelected );
3294 SelectableSortedSet::iterator i = best;
3296 while ( i != selector_point_ents.end() )
3298 if( ( *i ).first.equalEpsilon( ( *best ).first, 0.25f, 0.000001f ) ){
3299 ( *i ).second->setSelected( !wasSelected );
3306 return !wasSelected;
3308 else{//do primitives, if ents failed
3310 Scene_TestSelect_Component( selector, volume, scissored, eFace );
3313 Scene_TestSelect( selector, volume, scissored, Mode(), ComponentMode() );
3315 if ( !selector.failed() ){
3316 SelectableSortedSet::iterator best = selector.begin();
3317 const bool wasSelected = ( *best ).second->isSelected();
3318 ( *best ).second->setSelected( !wasSelected );
3319 SelectableSortedSet::iterator i = best;
3321 while ( i != selector.end() )
3323 if( ( *i ).first.equalEpsilon( ( *best ).first, 0.25f, 0.000001f ) ){
3324 ( *i ).second->setSelected( !wasSelected );
3331 return !wasSelected;
3340 void SelectArea( const View& view, const float device_point[2], const float device_delta[2], RadiantSelectionSystem::EModifier modifier, bool face ){
3341 if ( modifier == eReplace ) {
3342 deselectComponentsOrAll( face );
3345 #if defined ( DEBUG_SELECTION )
3346 g_render_clipped.destroy();
3350 View scissored( view );
3351 ConstructSelectionTest( scissored, SelectionBoxForArea( device_point, device_delta ) );
3353 SelectionVolume volume( scissored );
3356 Scene_TestSelect_Component( pool, volume, scissored, eFace );
3360 Scene_TestSelect( pool, volume, scissored, Mode(), ComponentMode() );
3363 for ( SelectionPool::iterator i = pool.begin(); i != pool.end(); ++i )
3365 ( *i ).second->setSelected( !( modifier == RadiantSelectionSystem::eToggle && ( *i ).second->isSelected() ) );
3371 void translate( const Vector3& translation ){
3372 if ( !nothingSelected() ) {
3373 //ASSERT_MESSAGE(!m_pivotChanged, "pivot is invalid");
3375 m_translation = translation;
3377 m_pivot2world = m_pivot2world_start;
3378 matrix4_translate_by_vec3( m_pivot2world, translation );
3380 if ( Mode() == eComponent ) {
3381 Scene_Translate_Component_Selected( GlobalSceneGraph(), m_translation );
3385 Scene_Translate_Selected( GlobalSceneGraph(), m_translation );
3388 SceneChangeNotify();
3391 void outputTranslation( TextOutputStream& ostream ){
3392 ostream << " -xyz " << m_translation.x() << " " << m_translation.y() << " " << m_translation.z();
3394 void rotate( const Quaternion& rotation ){
3395 if ( !nothingSelected() ) {
3396 //ASSERT_MESSAGE(!m_pivotChanged, "pivot is invalid");
3398 m_rotation = rotation;
3400 if ( Mode() == eComponent ) {
3401 Scene_Rotate_Component_Selected( GlobalSceneGraph(), m_rotation, vector4_to_vector3( m_pivot2world.t() ) );
3403 matrix4_assign_rotation_for_pivot( m_pivot2world, m_component_selection.back() );
3407 Scene_Rotate_Selected( GlobalSceneGraph(), m_rotation, vector4_to_vector3( m_pivot2world.t() ) );
3409 matrix4_assign_rotation_for_pivot( m_pivot2world, m_selection.back() );
3412 SceneChangeNotify();
3415 void outputRotation( TextOutputStream& ostream ){
3416 ostream << " -eulerXYZ " << m_rotation.x() << " " << m_rotation.y() << " " << m_rotation.z();
3418 void scale( const Vector3& scaling ){
3419 if ( !nothingSelected() ) {
3422 if ( Mode() == eComponent ) {
3423 Scene_Scale_Component_Selected( GlobalSceneGraph(), m_scale, vector4_to_vector3( m_pivot2world.t() ) );
3427 Scene_Scale_Selected( GlobalSceneGraph(), m_scale, vector4_to_vector3( m_pivot2world.t() ) );
3430 SceneChangeNotify();
3433 void outputScale( TextOutputStream& ostream ){
3434 ostream << " -scale " << m_scale.x() << " " << m_scale.y() << " " << m_scale.z();
3437 void rotateSelected( const Quaternion& rotation, bool snapOrigin ){
3438 if( snapOrigin && !m_pivotIsCustom ){
3439 m_pivot2world.tx() = float_snapped( m_pivot2world.tx(), GetSnapGridSize() );
3440 m_pivot2world.ty() = float_snapped( m_pivot2world.ty(), GetSnapGridSize() );
3441 m_pivot2world.tz() = float_snapped( m_pivot2world.tz(), GetSnapGridSize() );
3447 void translateSelected( const Vector3& translation ){
3449 translate( translation );
3452 void scaleSelected( const Vector3& scaling ){
3458 void MoveSelected( const View& view, const float device_point[2], bool snap, bool snapbbox ){
3459 if ( m_manipulator->isSelected() ) {
3460 if ( !m_undo_begun ) {
3461 m_undo_begun = true;
3462 GlobalUndoSystem().start();
3465 Matrix4 device2manip;
3466 ConstructDevice2Manip( device2manip, m_pivot2world_start, view.GetModelview(), view.GetProjection(), view.GetViewport() );
3467 m_manipulator->GetManipulatable()->Transform( m_manip2pivot_start, device2manip, device_point[0], device_point[1], snap, snapbbox );
3471 /// \todo Support view-dependent nudge.
3472 void NudgeManipulator( const Vector3& nudge, const Vector3& view ){
3473 // if ( ManipulatorMode() == eTranslate || ManipulatorMode() == eDrag ) {
3474 translateSelected( nudge );
3479 void freezeTransforms();
3481 void renderSolid( Renderer& renderer, const VolumeTest& volume ) const;
3482 void renderWireframe( Renderer& renderer, const VolumeTest& volume ) const {
3483 renderSolid( renderer, volume );
3486 const Matrix4& GetPivot2World() const {
3488 return m_pivot2world;
3491 static void constructStatic(){
3492 m_state = GlobalShaderCache().capture( "$POINT" );
3493 #if defined( DEBUG_SELECTION )
3494 g_state_clipped = GlobalShaderCache().capture( "$DEBUG_CLIPPED" );
3496 TranslateManipulator::m_state_wire = GlobalShaderCache().capture( "$WIRE_OVERLAY" );
3497 TranslateManipulator::m_state_fill = GlobalShaderCache().capture( "$FLATSHADE_OVERLAY" );
3498 RotateManipulator::m_state_outer = GlobalShaderCache().capture( "$WIRE_OVERLAY" );
3501 static void destroyStatic(){
3502 #if defined( DEBUG_SELECTION )
3503 GlobalShaderCache().release( "$DEBUG_CLIPPED" );
3505 GlobalShaderCache().release( "$WIRE_OVERLAY" );
3506 GlobalShaderCache().release( "$FLATSHADE_OVERLAY" );
3507 GlobalShaderCache().release( "$WIRE_OVERLAY" );
3508 GlobalShaderCache().release( "$POINT" );
3512 Shader* RadiantSelectionSystem::m_state = 0;
3517 RadiantSelectionSystem* g_RadiantSelectionSystem;
3519 inline RadiantSelectionSystem& getSelectionSystem(){
3520 return *g_RadiantSelectionSystem;
3526 class testselect_entity_visible : public scene::Graph::Walker
3528 Selector& m_selector;
3529 SelectionTest& m_test;
3531 testselect_entity_visible( Selector& selector, SelectionTest& test )
3532 : m_selector( selector ), m_test( test ){
3534 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3535 if( path.top().get_pointer() == Map_GetWorldspawn( g_map ) ||
3536 node_is_group( path.top().get() ) ){
3539 Selectable* selectable = Instance_getSelectable( instance );
3540 if ( selectable != 0
3541 && Node_isEntity( path.top() ) ) {
3542 m_selector.pushSelectable( *selectable );
3545 SelectionTestable* selectionTestable = Instance_getSelectionTestable( instance );
3546 if ( selectionTestable ) {
3547 selectionTestable->testSelect( m_selector, m_test );
3552 void post( const scene::Path& path, scene::Instance& instance ) const {
3553 Selectable* selectable = Instance_getSelectable( instance );
3554 if ( selectable != 0
3555 && Node_isEntity( path.top() ) ) {
3556 m_selector.popSelectable();
3561 class testselect_primitive_visible : public scene::Graph::Walker
3563 Selector& m_selector;
3564 SelectionTest& m_test;
3566 testselect_primitive_visible( Selector& selector, SelectionTest& test )
3567 : m_selector( selector ), m_test( test ){
3569 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3570 Selectable* selectable = Instance_getSelectable( instance );
3571 if ( selectable != 0 ) {
3572 m_selector.pushSelectable( *selectable );
3575 SelectionTestable* selectionTestable = Instance_getSelectionTestable( instance );
3576 if ( selectionTestable ) {
3577 selectionTestable->testSelect( m_selector, m_test );
3582 void post( const scene::Path& path, scene::Instance& instance ) const {
3583 Selectable* selectable = Instance_getSelectable( instance );
3584 if ( selectable != 0 ) {
3585 m_selector.popSelectable();
3590 class testselect_component_visible : public scene::Graph::Walker
3592 Selector& m_selector;
3593 SelectionTest& m_test;
3594 SelectionSystem::EComponentMode m_mode;
3596 testselect_component_visible( Selector& selector, SelectionTest& test, SelectionSystem::EComponentMode mode )
3597 : m_selector( selector ), m_test( test ), m_mode( mode ){
3599 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3600 ComponentSelectionTestable* componentSelectionTestable = Instance_getComponentSelectionTestable( instance );
3601 if ( componentSelectionTestable ) {
3602 componentSelectionTestable->testSelectComponents( m_selector, m_test, m_mode );
3610 class testselect_component_visible_selected : public scene::Graph::Walker
3612 Selector& m_selector;
3613 SelectionTest& m_test;
3614 SelectionSystem::EComponentMode m_mode;
3616 testselect_component_visible_selected( Selector& selector, SelectionTest& test, SelectionSystem::EComponentMode mode )
3617 : m_selector( selector ), m_test( test ), m_mode( mode ){
3619 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3620 Selectable* selectable = Instance_getSelectable( instance );
3621 if ( selectable != 0 && selectable->isSelected() ) {
3622 ComponentSelectionTestable* componentSelectionTestable = Instance_getComponentSelectionTestable( instance );
3623 if ( componentSelectionTestable ) {
3624 componentSelectionTestable->testSelectComponents( m_selector, m_test, m_mode );
3632 void Scene_TestSelect_Primitive( Selector& selector, SelectionTest& test, const VolumeTest& volume ){
3633 Scene_forEachVisible( GlobalSceneGraph(), volume, testselect_primitive_visible( selector, test ) );
3636 void Scene_TestSelect_Component_Selected( Selector& selector, SelectionTest& test, const VolumeTest& volume, SelectionSystem::EComponentMode componentMode ){
3637 Scene_forEachVisible( GlobalSceneGraph(), volume, testselect_component_visible_selected( selector, test, componentMode ) );
3640 void Scene_TestSelect_Component( Selector& selector, SelectionTest& test, const VolumeTest& volume, SelectionSystem::EComponentMode componentMode ){
3641 Scene_forEachVisible( GlobalSceneGraph(), volume, testselect_component_visible( selector, test, componentMode ) );
3644 void RadiantSelectionSystem::Scene_TestSelect( Selector& selector, SelectionTest& test, const View& view, SelectionSystem::EMode mode, SelectionSystem::EComponentMode componentMode ){
3649 Scene_forEachVisible( GlobalSceneGraph(), view, testselect_entity_visible( selector, test ) );
3653 Scene_TestSelect_Primitive( selector, test, view );
3656 Scene_TestSelect_Component_Selected( selector, test, view, componentMode );
3661 class FreezeTransforms : public scene::Graph::Walker
3664 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3665 TransformNode* transformNode = Node_getTransformNode( path.top() );
3666 if ( transformNode != 0 ) {
3667 Transformable* transform = Instance_getTransformable( instance );
3668 if ( transform != 0 ) {
3669 transform->freezeTransform();
3676 void RadiantSelectionSystem::freezeTransforms(){
3677 GlobalSceneGraph().traverse( FreezeTransforms() );
3681 void RadiantSelectionSystem::endMove(){
3684 if ( Mode() == ePrimitive ) {
3685 if ( ManipulatorMode() == eDrag ) {
3686 g_bTmpComponentMode = false;
3687 if( g_bAltDragManipulatorResize ){
3688 Scene_SelectAll_Component( false, SelectionSystem::eVertex );
3691 Scene_SelectAll_Component( false, SelectionSystem::eFace );
3696 m_pivot_moving = false;
3699 SceneChangeNotify();
3701 if ( m_undo_begun ) {
3702 StringOutputStream command;
3704 if ( ManipulatorMode() == eTranslate ) {
3705 command << "translateTool";
3706 outputTranslation( command );
3708 else if ( ManipulatorMode() == eRotate ) {
3709 command << "rotateTool";
3710 outputRotation( command );
3712 else if ( ManipulatorMode() == eScale ) {
3713 command << "scaleTool";
3714 outputScale( command );
3716 else if ( ManipulatorMode() == eDrag ) {
3717 command << "dragTool";
3720 GlobalUndoSystem().finish( command.c_str() );
3725 inline AABB Instance_getPivotBounds( scene::Instance& instance ){
3726 Entity* entity = Node_getEntity( instance.path().top() );
3728 && ( entity->getEntityClass().fixedsize
3729 || !node_is_group( instance.path().top() ) ) ) {
3730 Editable* editable = Node_getEditable( instance.path().top() );
3731 if ( editable != 0 ) {
3732 return AABB( vector4_to_vector3( matrix4_multiplied_by_matrix4( instance.localToWorld(), editable->getLocalPivot() ).t() ), Vector3( 0, 0, 0 ) );
3736 return AABB( vector4_to_vector3( instance.localToWorld().t() ), Vector3( 0, 0, 0 ) );
3740 return instance.worldAABB();
3743 class bounds_selected : public scene::Graph::Walker
3747 bounds_selected( AABB& bounds )
3748 : m_bounds( bounds ){
3751 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3752 Selectable* selectable = Instance_getSelectable( instance );
3753 if ( selectable != 0
3754 && selectable->isSelected() ) {
3755 aabb_extend_by_aabb_safe( m_bounds, Instance_getPivotBounds( instance ) );
3761 class bounds_selected_component : public scene::Graph::Walker
3765 bounds_selected_component( AABB& bounds )
3766 : m_bounds( bounds ){
3769 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3770 Selectable* selectable = Instance_getSelectable( instance );
3771 if ( selectable != 0
3772 && selectable->isSelected() ) {
3773 ComponentEditable* componentEditable = Instance_getComponentEditable( instance );
3774 if ( componentEditable ) {
3775 aabb_extend_by_aabb_safe( m_bounds, aabb_for_oriented_aabb_safe( componentEditable->getSelectedComponentsBounds(), instance.localToWorld() ) );
3782 void Scene_BoundsSelected( scene::Graph& graph, AABB& bounds ){
3783 graph.traverse( bounds_selected( bounds ) );
3786 void Scene_BoundsSelectedComponent( scene::Graph& graph, AABB& bounds ){
3787 graph.traverse( bounds_selected_component( bounds ) );
3791 inline void pivot_for_node( Matrix4& pivot, scene::Node& node, scene::Instance& instance ){
3792 ComponentEditable* componentEditable = Instance_getComponentEditable( instance );
3793 if ( GlobalSelectionSystem().Mode() == SelectionSystem::eComponent
3794 && componentEditable != 0 ) {
3795 pivot = matrix4_translation_for_vec3( componentEditable->getSelectedComponentsBounds().origin );
3799 Bounded* bounded = Instance_getBounded( instance );
3800 if ( bounded != 0 ) {
3801 pivot = matrix4_translation_for_vec3( bounded->localAABB().origin );
3805 pivot = g_matrix4_identity;
3811 void RadiantSelectionSystem::ConstructPivot() const {
3812 if ( !m_pivotChanged || m_pivot_moving || m_pivotIsCustom ) {
3815 m_pivotChanged = false;
3817 Vector3 m_object_pivot;
3819 if ( !nothingSelected() ) {
3822 if ( Mode() == eComponent ) {
3823 Scene_BoundsSelectedComponent( GlobalSceneGraph(), bounds );
3827 Scene_BoundsSelected( GlobalSceneGraph(), bounds );
3829 m_object_pivot = bounds.origin;
3832 //vector3_snap( m_object_pivot, GetSnapGridSize() );
3833 //globalOutputStream() << m_object_pivot << "\n";
3834 m_pivot2world = matrix4_translation_for_vec3( m_object_pivot );
3836 switch ( m_manipulator_mode )
3841 if ( Mode() == eComponent ) {
3842 matrix4_assign_rotation_for_pivot( m_pivot2world, m_component_selection.back() );
3846 matrix4_assign_rotation_for_pivot( m_pivot2world, m_selection.back() );
3850 if ( Mode() == eComponent ) {
3851 matrix4_assign_rotation_for_pivot( m_pivot2world, m_component_selection.back() );
3855 matrix4_assign_rotation_for_pivot( m_pivot2world, m_selection.back() );
3864 void RadiantSelectionSystem::setCustomPivotOrigin( Vector3& point ) const {
3865 if ( !nothingSelected() && ( m_manipulator_mode == eTranslate || m_manipulator_mode == eRotate || m_manipulator_mode == eScale ) ) {
3867 if ( Mode() == eComponent ) {
3868 Scene_BoundsSelectedComponent( GlobalSceneGraph(), bounds );
3872 Scene_BoundsSelected( GlobalSceneGraph(), bounds );
3874 //globalOutputStream() << point << "\n";
3875 for( std::size_t i = 0; i < 3; i++ ){
3876 if( point[i] < 900000.0f ){
3877 float bestsnapDist = fabs( bounds.origin[i] - point[i] );
3878 float bestsnapTo = bounds.origin[i];
3879 float othersnapDist = fabs( bounds.origin[i] + bounds.extents[i] - point[i] );
3880 if( othersnapDist < bestsnapDist ){
3881 bestsnapDist = othersnapDist;
3882 bestsnapTo = bounds.origin[i] + bounds.extents[i];
3884 othersnapDist = fabs( bounds.origin[i] - bounds.extents[i] - point[i] );
3885 if( othersnapDist < bestsnapDist ){
3886 bestsnapDist = othersnapDist;
3887 bestsnapTo = bounds.origin[i] - bounds.extents[i];
3889 othersnapDist = fabs( float_snapped( point[i], GetSnapGridSize() ) - point[i] );
3890 if( othersnapDist < bestsnapDist ){
3891 bestsnapDist = othersnapDist;
3892 bestsnapTo = float_snapped( point[i], GetSnapGridSize() );
3894 point[i] = bestsnapTo;
3896 m_pivot2world[i + 12] = point[i]; //m_pivot2world.tx() .ty() .tz()
3900 switch ( m_manipulator_mode )
3905 if ( Mode() == eComponent ) {
3906 matrix4_assign_rotation_for_pivot( m_pivot2world, m_component_selection.back() );
3910 matrix4_assign_rotation_for_pivot( m_pivot2world, m_selection.back() );
3914 if ( Mode() == eComponent ) {
3915 matrix4_assign_rotation_for_pivot( m_pivot2world, m_component_selection.back() );
3919 matrix4_assign_rotation_for_pivot( m_pivot2world, m_selection.back() );
3926 m_pivotIsCustom = true;
3930 AABB RadiantSelectionSystem::getSelectionAABB() const {
3932 if ( !nothingSelected() ) {
3933 if ( Mode() == eComponent || g_bTmpComponentMode ) {
3934 Scene_BoundsSelectedComponent( GlobalSceneGraph(), bounds );
3938 Scene_BoundsSelected( GlobalSceneGraph(), bounds );
3944 void RadiantSelectionSystem::renderSolid( Renderer& renderer, const VolumeTest& volume ) const {
3945 //if(view->TestPoint(m_object_pivot))
3946 if ( !nothingSelected() ) {
3947 renderer.Highlight( Renderer::ePrimitive, false );
3948 renderer.Highlight( Renderer::eFace, false );
3950 renderer.SetState( m_state, Renderer::eWireframeOnly );
3951 renderer.SetState( m_state, Renderer::eFullMaterials );
3953 m_manipulator->render( renderer, volume, GetPivot2World() );
3956 #if defined( DEBUG_SELECTION )
3957 renderer.SetState( g_state_clipped, Renderer::eWireframeOnly );
3958 renderer.SetState( g_state_clipped, Renderer::eFullMaterials );
3959 renderer.addRenderable( g_render_clipped, g_render_clipped.m_world );
3963 #include "preferencesystem.h"
3964 #include "preferences.h"
3966 bool g_bLeftMouseClickSelector = true;
3968 void SelectionSystem_constructPreferences( PreferencesPage& page ){
3969 page.appendCheckBox( "", "Prefer point entities in 2D", getSelectionSystem().m_bPreferPointEntsIn2D );
3970 page.appendCheckBox( "", "Left mouse click tunnel selector", g_bLeftMouseClickSelector );
3972 void SelectionSystem_constructPage( PreferenceGroup& group ){
3973 PreferencesPage page( group.createPage( "Selection", "Selection System Settings" ) );
3974 SelectionSystem_constructPreferences( page );
3976 void SelectionSystem_registerPreferencesPage(){
3977 PreferencesDialog_addSettingsPage( FreeCaller<void(PreferenceGroup&), SelectionSystem_constructPage>() );
3982 void SelectionSystem_OnBoundsChanged(){
3983 getSelectionSystem().pivotChanged();
3986 SignalHandlerId SelectionSystem_boundsChanged;
3988 void SelectionSystem_Construct(){
3989 RadiantSelectionSystem::constructStatic();
3991 g_RadiantSelectionSystem = new RadiantSelectionSystem;
3993 SelectionSystem_boundsChanged = GlobalSceneGraph().addBoundsChangedCallback( FreeCaller<void(), SelectionSystem_OnBoundsChanged>() );
3995 GlobalShaderCache().attachRenderable( getSelectionSystem() );
3997 GlobalPreferenceSystem().registerPreference( "PreferPointEntsIn2D", make_property_string( getSelectionSystem().m_bPreferPointEntsIn2D ) );
3998 GlobalPreferenceSystem().registerPreference( "LeftMouseClickSelector", make_property_string( g_bLeftMouseClickSelector ) );
3999 SelectionSystem_registerPreferencesPage();
4002 void SelectionSystem_Destroy(){
4003 GlobalShaderCache().detachRenderable( getSelectionSystem() );
4005 GlobalSceneGraph().removeBoundsChangedCallback( SelectionSystem_boundsChanged );
4007 delete g_RadiantSelectionSystem;
4009 RadiantSelectionSystem::destroyStatic();
4015 inline float screen_normalised( float pos, std::size_t size ){
4016 return ( ( 2.0f * pos ) / size ) - 1.0f;
4019 typedef Vector2 DeviceVector;
4021 inline DeviceVector window_to_normalised_device( WindowVector window, std::size_t width, std::size_t height ){
4022 return DeviceVector( screen_normalised( window.x(), width ), screen_normalised( height - 1 - window.y(), height ) );
4025 inline float device_constrained( float pos ){
4026 return std::min( 1.0f, std::max( -1.0f, pos ) );
4029 inline DeviceVector device_constrained( DeviceVector device ){
4030 return DeviceVector( device_constrained( device.x() ), device_constrained( device.y() ) );
4033 inline float window_constrained( float pos, std::size_t origin, std::size_t size ){
4034 return std::min( static_cast<float>( origin + size ), std::max( static_cast<float>( origin ), pos ) );
4037 inline WindowVector window_constrained( WindowVector window, std::size_t x, std::size_t y, std::size_t width, std::size_t height ){
4038 return WindowVector( window_constrained( window.x(), x, width ), window_constrained( window.y(), y, height ) );
4041 typedef Callback<void(DeviceVector)> MouseEventCallback;
4043 Single<MouseEventCallback> g_mouseMovedCallback;
4044 Single<MouseEventCallback> g_mouseUpCallback;
4047 const ButtonIdentifier c_button_select = c_buttonLeft;
4048 const ButtonIdentifier c_button_select2 = c_buttonRight;
4049 const ModifierFlags c_modifier_manipulator = c_modifierNone;
4050 const ModifierFlags c_modifier_toggle = c_modifierShift;
4051 const ModifierFlags c_modifier_replace = c_modifierShift | c_modifierAlt;
4052 const ModifierFlags c_modifier_face = c_modifierControl;
4054 const ButtonIdentifier c_button_select = c_buttonLeft;
4055 const ModifierFlags c_modifier_manipulator = c_modifierNone;
4056 const ModifierFlags c_modifier_toggle = c_modifierControl;
4057 const ModifierFlags c_modifier_replace = c_modifierNone;
4058 const ModifierFlags c_modifier_face = c_modifierShift;
4060 const ModifierFlags c_modifier_toggle_face = c_modifier_toggle | c_modifier_face;
4061 const ModifierFlags c_modifier_replace_face = c_modifier_replace | c_modifier_face;
4063 const ButtonIdentifier c_button_texture = c_buttonMiddle;
4064 const ModifierFlags c_modifier_apply_texture1 = c_modifierControl | c_modifierShift;
4065 const ModifierFlags c_modifier_apply_texture2 = c_modifierControl;
4066 const ModifierFlags c_modifier_apply_texture3 = c_modifierShift;
4067 const ModifierFlags c_modifier_copy_texture = c_modifierNone;
4071 RadiantSelectionSystem::EModifier modifier_for_state( ModifierFlags state ){
4072 if ( ( state == c_modifier_toggle || state == c_modifier_toggle_face || state == c_modifier_face ) ) {
4074 return RadiantSelectionSystem::eReplace;
4077 return RadiantSelectionSystem::eToggle;
4080 return RadiantSelectionSystem::eManipulator;
4083 rect_t getDeviceArea() const {
4084 DeviceVector delta( m_current - m_start );
4085 if ( selecting() && fabs( delta.x() ) > m_epsilon.x() && fabs( delta.y() ) > m_epsilon.y() ) {
4086 return SelectionBoxForArea( &m_start[0], &delta[0] );
4090 rect_t default_area = { { 0, 0, }, { 0, 0, }, };
4091 return default_area;
4096 DeviceVector m_start;
4097 DeviceVector m_current;
4098 DeviceVector m_epsilon;
4099 ModifierFlags m_state;
4102 bool m_mouseMovedWhilePressed;
4105 RectangleCallback m_window_update;
4107 Selector_() : m_start( 0.0f, 0.0f ), m_current( 0.0f, 0.0f ), m_state( c_modifierNone ), m_mouse2( false ), m_mouseMoved( false ), m_mouseMovedWhilePressed( false ){
4111 m_window_update( getDeviceArea() );
4114 void testSelect( DeviceVector position ){
4115 RadiantSelectionSystem::EModifier modifier = modifier_for_state( m_state );
4116 if ( modifier != RadiantSelectionSystem::eManipulator ) {
4117 DeviceVector delta( position - m_start );
4118 if ( fabs( delta.x() ) > m_epsilon.x() && fabs( delta.y() ) > m_epsilon.y() ) {
4119 DeviceVector delta( position - m_start );
4120 //getSelectionSystem().SelectArea( *m_view, &m_start[0], &delta[0], modifier, ( m_state & c_modifier_face ) != c_modifierNone );
4121 getSelectionSystem().SelectArea( *m_view, &m_start[0], &delta[0], RadiantSelectionSystem::eToggle, ( m_state & c_modifier_face ) != c_modifierNone );
4123 else if( !m_mouseMovedWhilePressed ){
4124 if ( modifier == RadiantSelectionSystem::eReplace && !m_mouseMoved ) {
4125 modifier = RadiantSelectionSystem::eCycle;
4127 getSelectionSystem().SelectPoint( *m_view, &position[0], &m_epsilon[0], modifier, ( m_state & c_modifier_face ) != c_modifierNone );
4131 m_start = m_current = DeviceVector( 0.0f, 0.0f );
4135 void testSelect_simpleM1( DeviceVector position ){
4136 /*RadiantSelectionSystem::EModifier modifier = RadiantSelectionSystem::eReplace;
4137 DeviceVector delta( position - m_start );
4138 if ( fabs( delta.x() ) < m_epsilon.x() && fabs( delta.y() ) < m_epsilon.y() ) {
4139 modifier = RadiantSelectionSystem::eCycle;
4141 getSelectionSystem().SelectPoint( *m_view, &position[0], &m_epsilon[0], modifier, false );*/
4142 if( g_bLeftMouseClickSelector ){
4143 getSelectionSystem().SelectPoint( *m_view, &position[0], &m_epsilon[0], m_mouseMoved ? RadiantSelectionSystem::eReplace : RadiantSelectionSystem::eCycle, false );
4145 m_start = m_current = device_constrained( position );
4149 bool selecting() const {
4150 return m_state != c_modifier_manipulator && m_mouse2;
4153 void setState( ModifierFlags state ){
4154 bool was_selecting = selecting();
4156 if ( was_selecting ^ selecting() ) {
4161 ModifierFlags getState() const {
4165 void modifierEnable( ModifierFlags type ){
4166 setState( bitfield_enable( getState(), type ) );
4168 void modifierDisable( ModifierFlags type ){
4169 setState( bitfield_disable( getState(), type ) );
4172 void mouseDown( DeviceVector position ){
4173 m_start = m_current = device_constrained( position );
4174 if( !m_mouse2 && m_state != c_modifierNone ){
4175 m_paintSelect = getSelectionSystem().SelectPoint_InitPaint( *m_view, &position[0], &m_epsilon[0], ( m_state & c_modifier_face ) != c_modifierNone );
4179 void mouseMoved( DeviceVector position ){
4180 m_current = device_constrained( position );
4181 m_mouseMovedWhilePressed = true;
4185 else if( m_state != c_modifier_manipulator ){
4186 getSelectionSystem().SelectPoint( *m_view, &m_current[0], &m_epsilon[0],
4187 m_paintSelect ? RadiantSelectionSystem::eSelect : RadiantSelectionSystem::eDeselect,
4188 ( m_state & c_modifier_face ) != c_modifierNone );
4191 typedef MemberCaller<Selector_, void(DeviceVector), &Selector_::mouseMoved> MouseMovedCaller;
4193 void mouseUp( DeviceVector position ){
4195 testSelect( device_constrained( position ) );
4198 m_start = m_current = DeviceVector( 0.0f, 0.0f );
4201 g_mouseMovedCallback.clear();
4202 g_mouseUpCallback.clear();
4204 typedef MemberCaller<Selector_, void(DeviceVector), &Selector_::mouseUp> MouseUpCaller;
4211 DeviceVector m_epsilon;
4213 ModifierFlags m_state;
4215 Manipulator_() : m_state( c_modifierNone ){
4218 bool mouseDown( DeviceVector position ){
4219 return getSelectionSystem().SelectManipulator( *m_view, &position[0], &m_epsilon[0] );
4222 void mouseMoved( DeviceVector position ){
4223 getSelectionSystem().MoveSelected( *m_view, &position[0], ( m_state & c_modifierShift ) == c_modifierShift, ( m_state & c_modifierControl ) == c_modifierControl );
4225 typedef MemberCaller<Manipulator_, void(DeviceVector), &Manipulator_::mouseMoved> MouseMovedCaller;
4227 void mouseUp( DeviceVector position ){
4228 getSelectionSystem().endMove();
4229 g_mouseMovedCallback.clear();
4230 g_mouseUpCallback.clear();
4232 typedef MemberCaller<Manipulator_, void(DeviceVector), &Manipulator_::mouseUp> MouseUpCaller;
4234 void setState( ModifierFlags state ){
4238 ModifierFlags getState() const {
4242 void modifierEnable( ModifierFlags type ){
4243 setState( bitfield_enable( getState(), type ) );
4245 void modifierDisable( ModifierFlags type ){
4246 setState( bitfield_disable( getState(), type ) );
4250 void Scene_copyClosestTexture( SelectionTest& test );
4251 void Scene_applyClosestTexture( SelectionTest& test );
4253 class RadiantWindowObserver : public SelectionSystemWindowObserver
4266 Selector_ m_selector;
4267 Manipulator_ m_manipulator;
4269 RadiantWindowObserver() : m_mouse_down( false ){
4274 void setView( const View& view ){
4275 m_selector.m_view = &view;
4276 m_manipulator.m_view = &view;
4278 void setRectangleDrawCallback( const RectangleCallback& callback ){
4279 m_selector.m_window_update = callback;
4281 void onSizeChanged( int width, int height ){
4284 DeviceVector epsilon( SELECT_EPSILON / static_cast<float>( m_width ), SELECT_EPSILON / static_cast<float>( m_height ) );
4285 m_selector.m_epsilon = m_manipulator.m_epsilon = epsilon;
4287 void onMouseDown( const WindowVector& position, ButtonIdentifier button, ModifierFlags modifiers ){
4288 if ( button == c_button_select || ( button == c_button_select2 && modifiers != c_modifierNone ) ) {
4289 m_mouse_down = true;
4290 //m_selector.m_mouseMoved = false;
4292 DeviceVector devicePosition( window_to_normalised_device( position, m_width, m_height ) );
4293 g_bAltDragManipulatorResize = ( modifiers == c_modifierAlt ) ? true : false;
4294 if ( ( modifiers == c_modifier_manipulator || modifiers == c_modifierAlt ) && m_manipulator.mouseDown( devicePosition ) ) {
4295 g_mouseMovedCallback.insert( MouseEventCallback( Manipulator_::MouseMovedCaller( m_manipulator ) ) );
4296 g_mouseUpCallback.insert( MouseEventCallback( Manipulator_::MouseUpCaller( m_manipulator ) ) );
4300 m_selector.m_mouse2 = ( button == c_button_select ) ? false : true;
4301 m_selector.mouseDown( devicePosition );
4302 g_mouseMovedCallback.insert( MouseEventCallback( Selector_::MouseMovedCaller( m_selector ) ) );
4303 g_mouseUpCallback.insert( MouseEventCallback( Selector_::MouseUpCaller( m_selector ) ) );
4306 else if ( button == c_button_texture ) {
4307 DeviceVector devicePosition( device_constrained( window_to_normalised_device( position, m_width, m_height ) ) );
4309 View scissored( *m_selector.m_view );
4310 ConstructSelectionTest( scissored, SelectionBoxForPoint( &devicePosition[0], &m_selector.m_epsilon[0] ) );
4311 SelectionVolume volume( scissored );
4313 if ( modifiers == c_modifier_apply_texture1 || modifiers == c_modifier_apply_texture2 || modifiers == c_modifier_apply_texture3 ) {
4314 Scene_applyClosestTexture( volume );
4316 else if ( modifiers == c_modifier_copy_texture ) {
4317 Scene_copyClosestTexture( volume );
4321 void onMouseMotion( const WindowVector& position, ModifierFlags modifiers ){
4322 m_selector.m_mouseMoved = true;
4323 if ( m_mouse_down && !g_mouseMovedCallback.empty() ) {
4324 m_selector.m_mouseMovedWhilePressed = true;
4325 g_mouseMovedCallback.get() ( window_to_normalised_device( position, m_width, m_height ) );
4328 void onMouseUp( const WindowVector& position, ButtonIdentifier button, ModifierFlags modifiers ){
4329 if ( ( button == c_button_select || button == c_button_select2 ) && !g_mouseUpCallback.empty() ) {
4330 m_mouse_down = false;
4332 g_mouseUpCallback.get() ( window_to_normalised_device( position, m_width, m_height ) );
4334 //L button w/o scene changed = tunnel selection
4335 if( // !getSelectionSystem().m_undo_begun &&
4336 modifiers == c_modifierNone && button == c_button_select &&
4337 //( !m_selector.m_mouseMoved || !m_mouse_down ) &&
4338 !m_selector.m_mouseMovedWhilePressed &&
4339 ( getSelectionSystem().Mode() != SelectionSystem::eComponent || getSelectionSystem().ManipulatorMode() != SelectionSystem::eDrag ) ){
4340 m_selector.testSelect_simpleM1( device_constrained( window_to_normalised_device( position, m_width, m_height ) ) );
4342 //getSelectionSystem().m_undo_begun = false;
4343 m_selector.m_mouseMoved = false;
4344 m_selector.m_mouseMovedWhilePressed = false;
4346 void onModifierDown( ModifierFlags type ){
4347 m_selector.modifierEnable( type );
4348 m_manipulator.modifierEnable( type );
4350 void onModifierUp( ModifierFlags type ){
4351 m_selector.modifierDisable( type );
4352 m_manipulator.modifierDisable( type );
4358 SelectionSystemWindowObserver* NewWindowObserver(){
4359 return new RadiantWindowObserver;
4364 #include "modulesystem/singletonmodule.h"
4365 #include "modulesystem/moduleregistry.h"
4367 class SelectionDependencies :
4368 public GlobalSceneGraphModuleRef,
4369 public GlobalShaderCacheModuleRef,
4370 public GlobalOpenGLModuleRef
4374 class SelectionAPI : public TypeSystemRef
4376 SelectionSystem* m_selection;
4378 typedef SelectionSystem Type;
4379 STRING_CONSTANT( Name, "*" );
4382 SelectionSystem_Construct();
4384 m_selection = &getSelectionSystem();
4387 SelectionSystem_Destroy();
4389 SelectionSystem* getTable(){
4394 typedef SingletonModule<SelectionAPI, SelectionDependencies> SelectionModule;
4395 typedef Static<SelectionModule> StaticSelectionModule;
4396 StaticRegisterModule staticRegisterSelection( StaticSelectionModule::instance() );