]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/selection.cpp
Merge commit '3a78d902017a780e65f21f12c709aa746dfcab84' into garux-merge
[xonotic/netradiant.git] / radiant / selection.cpp
1 /*
2    Copyright (C) 2001-2006, William Joseph.
3    All Rights Reserved.
4
5    This file is part of GtkRadiant.
6
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.
11
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.
16
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
20  */
21
22 #include "selection.h"
23 #include "globaldefs.h"
24
25 #include "debugging/debugging.h"
26
27 #include <map>
28 #include <list>
29 #include <set>
30
31 #include "windowobserver.h"
32 #include "iundo.h"
33 #include "ientity.h"
34 #include "cullable.h"
35 #include "renderable.h"
36 #include "selectable.h"
37 #include "editable.h"
38
39 #include "math/frustum.h"
40 #include "signal/signal.h"
41 #include "generic/object.h"
42 #include "selectionlib.h"
43 #include "render.h"
44 #include "view.h"
45 #include "renderer.h"
46 #include "stream/stringstream.h"
47 #include "eclasslib.h"
48 #include "generic/bitfield.h"
49 #include "generic/static.h"
50 #include "pivot.h"
51 #include "stringio.h"
52 #include "container/container.h"
53
54 #include "grid.h"
55
56 TextOutputStream& ostream_write( TextOutputStream& t, const Vector4& v ){
57         return t << "[ " << v.x() << " " << v.y() << " " << v.z() << " " << v.w() << " ]";
58 }
59
60 TextOutputStream& ostream_write( TextOutputStream& t, const Matrix4& m ){
61         return t << "[ " << m.x() << " " << m.y() << " " << m.z() << " " << m.t() << " ]";
62 }
63
64 struct Pivot2World
65 {
66         Matrix4 m_worldSpace;
67         Matrix4 m_viewpointSpace;
68         Matrix4 m_viewplaneSpace;
69         Vector3 m_axis_screen;
70
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 );
75         }
76 };
77
78
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 ) ) );
82 }
83
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 );
87
88         // point at x, y, zFar
89         point_for_device_point( ray.direction, device2object, x, y, 1 );
90
91         // construct ray
92         vector3_subtract( ray.direction, ray.origin );
93         vector3_normalise( ray.direction );
94 }
95
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 );
100
101         if ( d > 0 ) {
102                 intersection = vector3_added( ray.origin, vector3_scaled( ray.direction, a - sqrt( d ) ) );
103                 return true;
104         }
105         else
106         {
107                 intersection = vector3_added( ray.origin, vector3_scaled( ray.direction, a ) );
108                 return false;
109         }
110 }
111
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
120
121         if ( D < 0.000001 ) {
122                 // the lines are almost parallel
123                 intersection = vector3_added( other.origin, vector3_scaled( other.direction, e ) );
124         }
125         else
126         {
127                 intersection = vector3_added( other.origin, vector3_scaled( other.direction, ( e - dot * d ) / D ) );
128         }
129 }
130
131 const Vector3 g_origin( 0, 0, 0 );
132 const float g_radius = 64;
133
134 void point_on_sphere( Vector3& point, const Matrix4& device2object, const float x, const float y ){
135         Ray ray;
136         ray_for_device_point( ray, device2object, x, y );
137         sphere_intersect_ray( g_origin, g_radius, ray, point );
138 }
139
140 void point_on_axis( Vector3& point, const Vector3& axis, const Matrix4& device2object, const float x, const float y ){
141         Ray ray;
142         ray_for_device_point( ray, device2object, x, y );
143         ray_intersect_ray( ray, Ray( Vector3( 0, 0, 0 ), axis ), point );
144 }
145
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 ) ) );
149 }
150
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 ) )
156                                                                    ) );
157 }
158
159
160 #if GDEF_DEBUG
161 class test_quat
162 {
163 public:
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 ) ) );
167 }
168 private:
169 };
170
171 static test_quat bleh( g_vector3_axis_x, g_vector3_axis_y );
172 #endif
173
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 ) ) ) );
177 }
178
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 );
183         }
184         else{
185                 return -angle_between( a, b );
186         }
187 }
188
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 ) );
191 }
192
193 class Manipulatable
194 {
195 public:
196 virtual void Construct( const Matrix4& device2manip, const float x, const float y ) = 0;
197 virtual void Transform( const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y, const bool snap ) = 0;
198 };
199
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 )
204                 );
205 }
206
207 class Rotatable
208 {
209 public:
210 virtual ~Rotatable() = default;
211 virtual void rotate( const Quaternion& rotation ) = 0;
212 };
213
214 class RotateFree : public Manipulatable
215 {
216 Vector3 m_start;
217 Rotatable& m_rotatable;
218 public:
219 RotateFree( Rotatable& rotatable )
220         : m_rotatable( rotatable ){
221 }
222 void Construct( const Matrix4& device2manip, const float x, const float y ){
223         point_on_sphere( m_start, device2manip, x, y );
224         vector3_normalise( m_start );
225 }
226 void Transform( const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y, const bool snap ){
227         Vector3 current;
228         point_on_sphere( current, device2manip, x, y );
229
230         if( snap ){
231                 Vector3 axis( 0, 0, 0 );
232                 for( std::size_t i = 0; i < 3; ++i ){
233                         if( current[i] == 0.0f ){
234                                 axis[i] = 1.0f;
235                                 break;
236                         }
237                 }
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 ) ) ) );
241                         return;
242                 }
243         }
244
245         vector3_normalise( current );
246         m_rotatable.rotate( quaternion_for_unit_vectors( m_start, current ) );
247 }
248 };
249
250 class RotateAxis : public Manipulatable
251 {
252 Vector3 m_axis;
253 Vector3 m_start;
254 Rotatable& m_rotatable;
255 public:
256 RotateAxis( Rotatable& rotatable )
257         : m_rotatable( rotatable ){
258 }
259 void Construct( const Matrix4& device2manip, const float x, const float y ){
260         point_on_sphere( m_start, device2manip, x, y );
261         constrain_to_axis( m_start, m_axis );
262 }
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 ){
265         Vector3 current;
266         point_on_sphere( current, device2manip, x, y );
267         constrain_to_axis( current, m_axis );
268
269         if( snap ){
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 ) ) ) );
271         }
272         else{
273                 m_rotatable.rotate( quaternion_for_axisangle( m_axis, angle_for_axis( m_start, current, m_axis ) ) );
274         }
275 }
276
277 void SetAxis( const Vector3& axis ){
278         m_axis = axis;
279 }
280 };
281
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 )
287                         )
288                 );
289 }
290
291 class Translatable
292 {
293 public:
294 virtual ~Translatable() = default;
295 virtual void translate( const Vector3& translation ) = 0;
296 };
297
298 class TranslateAxis : public Manipulatable
299 {
300 Vector3 m_start;
301 Vector3 m_axis;
302 Translatable& m_translatable;
303 public:
304 TranslateAxis( Translatable& translatable )
305         : m_translatable( translatable ){
306 }
307 void Construct( const Matrix4& device2manip, const float x, const float y ){
308         point_on_axis( m_start, m_axis, device2manip, x, y );
309 }
310 void Transform( const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y, const bool snap ){
311         Vector3 current;
312         point_on_axis( current, m_axis, device2manip, x, y );
313         current = vector3_scaled( m_axis, distance_for_axis( m_start, current, m_axis ) );
314
315         translation_local2object( current, current, manip2object );
316         vector3_snap( current, GetSnapGridSize() );
317
318         m_translatable.translate( current );
319 }
320
321 void SetAxis( const Vector3& axis ){
322         m_axis = axis;
323 }
324 };
325
326 class TranslateFree : public Manipulatable
327 {
328 private:
329 Vector3 m_start;
330 Translatable& m_translatable;
331 public:
332 TranslateFree( Translatable& translatable )
333         : m_translatable( translatable ){
334 }
335 void Construct( const Matrix4& device2manip, const float x, const float y ){
336         point_on_plane( m_start, device2manip, x, y );
337 }
338 void Transform( const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y, const bool snap ){
339         Vector3 current;
340         point_on_plane( current, device2manip, x, y );
341         current = vector3_subtracted( current, m_start );
342
343         if( snap ){
344                 for ( std::size_t i = 0; i < 3 ; ++i ){
345                         if( fabs( current[i] ) >= fabs( current[(i + 1) % 3] ) ){
346                                 current[(i + 1) % 3] = 0.0f;
347                         }
348                         else{
349                                 current[i] = 0.0f;
350                         }
351                 }
352         }
353
354         translation_local2object( current, current, manip2object );
355         vector3_snap( current, GetSnapGridSize() );
356
357         m_translatable.translate( current );
358 }
359 };
360
361 void GetSelectionAABB( AABB& bounds );
362 const Matrix4& SelectionSystem_GetPivot2World();
363
364 class Scalable
365 {
366 public:
367 virtual ~Scalable() = default;
368 virtual void scale( const Vector3& scaling ) = 0;
369 };
370
371
372 class ScaleAxis : public Manipulatable
373 {
374 private:
375 Vector3 m_start;
376 Vector3 m_axis;
377 Scalable& m_scalable;
378
379 Vector3 m_choosen_extent;
380
381 public:
382 ScaleAxis( Scalable& scalable )
383         : m_scalable( scalable ){
384 }
385 void Construct( const Matrix4& device2manip, const float x, const float y ){
386         point_on_axis( m_start, m_axis, device2manip, x, y );
387
388         AABB aabb;
389         GetSelectionAABB( aabb );
390         Vector3 transform_origin = vector4_to_vector3( SelectionSystem_GetPivot2World().t() );
391         m_choosen_extent = Vector3(
392                                         std::max( aabb.origin[0] + aabb.extents[0] - transform_origin[0], - aabb.origin[0] + aabb.extents[0] + transform_origin[0] ),
393                                         std::max( aabb.origin[1] + aabb.extents[1] - transform_origin[1], - aabb.origin[1] + aabb.extents[1] + transform_origin[1] ),
394                                         std::max( aabb.origin[2] + aabb.extents[2] - transform_origin[2], - aabb.origin[2] + aabb.extents[2] + transform_origin[2] )
395                                                         );
396
397 }
398 void Transform( const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y, const bool snap ){
399         //globalOutputStream() << "manip2object: " << manip2object << "  device2manip: " << device2manip << "  x: " << x << "  y:" << y <<"\n";
400         Vector3 current;
401         point_on_axis( current, m_axis, device2manip, x, y );
402         Vector3 delta = vector3_subtracted( current, m_start );
403
404         translation_local2object( delta, delta, manip2object );
405         vector3_snap( delta, GetSnapGridSize() );
406
407         Vector3 start( vector3_snapped( m_start, GetSnapGridSize() != 0.0f ? GetSnapGridSize() : 0.001f ) );
408         for ( std::size_t i = 0; i < 3 ; ++i ){ //prevent snapping to 0 with big gridsize
409                 if( float_snapped( m_start[i], 0.001f ) != 0.0f && start[i] == 0.0f ){
410                         start[i] = GetSnapGridSize();
411                 }
412         }
413         //globalOutputStream() << "m_start: " << m_start << "   start: " << start << "   delta: " << delta <<"\n";
414         Vector3 scale(
415                 start[0] == 0 ? 1 : 1 + delta[0] / start[0],
416                 start[1] == 0 ? 1 : 1 + delta[1] / start[1],
417                 start[2] == 0 ? 1 : 1 + delta[2] / start[2]
418                 );
419
420         for( std::size_t i = 0; i < 3; i++ ){
421                 if( m_choosen_extent[i] > 0.0625f ){ //epsilon to prevent super high scale for set of models, having really small extent, formed by origins
422                         scale[i] = ( m_choosen_extent[i] + delta[i] ) / m_choosen_extent[i];
423                 }
424         }
425         if( snap ){
426                 for( std::size_t i = 0; i < 3; i++ ){
427                         if( scale[i] == 1.0f ){
428                                 scale[i] = vector3_dot( scale, m_axis );
429                         }
430                 }
431         }
432         //globalOutputStream() << "scale: " << scale <<"\n";
433         m_scalable.scale( scale );
434 }
435
436 void SetAxis( const Vector3& axis ){
437         m_axis = axis;
438 }
439 };
440
441 class ScaleFree : public Manipulatable
442 {
443 private:
444 Vector3 m_start;
445 Scalable& m_scalable;
446
447 Vector3 m_choosen_extent;
448
449 public:
450 ScaleFree( Scalable& scalable )
451         : m_scalable( scalable ){
452 }
453 void Construct( const Matrix4& device2manip, const float x, const float y ){
454         point_on_plane( m_start, device2manip, x, y );
455
456         AABB aabb;
457         GetSelectionAABB( aabb );
458         Vector3 transform_origin = vector4_to_vector3( SelectionSystem_GetPivot2World().t() );
459         m_choosen_extent = Vector3(
460                                         std::max( aabb.origin[0] + aabb.extents[0] - transform_origin[0], - aabb.origin[0] + aabb.extents[0] + transform_origin[0] ),
461                                         std::max( aabb.origin[1] + aabb.extents[1] - transform_origin[1], - aabb.origin[1] + aabb.extents[1] + transform_origin[1] ),
462                                         std::max( aabb.origin[2] + aabb.extents[2] - transform_origin[2], - aabb.origin[2] + aabb.extents[2] + transform_origin[2] )
463                                                         );
464 }
465 void Transform( const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y, const bool snap ){
466         Vector3 current;
467         point_on_plane( current, device2manip, x, y );
468         Vector3 delta = vector3_subtracted( current, m_start );
469
470         translation_local2object( delta, delta, manip2object );
471         vector3_snap( delta, GetSnapGridSize() );
472
473         Vector3 start( vector3_snapped( m_start, GetSnapGridSize() != 0.0f ? GetSnapGridSize() : 0.001f ) );
474         for ( std::size_t i = 0; i < 3 ; ++i ){ //prevent snapping to 0 with big gridsize
475                 if( float_snapped( m_start[i], 0.001f ) != 0.0f && start[i] == 0.0f ){
476                         start[i] = GetSnapGridSize();
477                 }
478         }
479         Vector3 scale(
480                 start[0] == 0 ? 1 : 1 + delta[0] / start[0],
481                 start[1] == 0 ? 1 : 1 + delta[1] / start[1],
482                 start[2] == 0 ? 1 : 1 + delta[2] / start[2]
483                 );
484
485         //globalOutputStream() << "m_start: " << m_start << "   start: " << start << "   delta: " << delta <<"\n";
486         for( std::size_t i = 0; i < 3; i++ ){
487                 if( m_choosen_extent[i] > 0.0625f ){
488                         scale[i] = ( m_choosen_extent[i] + delta[i] ) / m_choosen_extent[i];
489                 }
490         }
491         //globalOutputStream() << "pre snap scale: " << scale <<"\n";
492         if( snap ){
493                 float bestscale = scale[0];
494                 for( std::size_t i = 1; i < 3; i++ ){
495                         //if( fabs( 1.0f - fabs( scale[i] ) ) > fabs( 1.0f - fabs( bestscale ) ) ){
496                         if( fabs( scale[i] ) > fabs( bestscale ) && scale[i] != 1.0f ){ //harder to scale down with this, but glitchier with upper one
497                                 bestscale = scale[i];
498                         }
499                         //globalOutputStream() << "bestscale: " << bestscale <<"\n";
500                 }
501                 for( std::size_t i = 0; i < 3; i++ ){
502                         if( start[i] != 0.0f ){ // !!!!check grid == 0 case
503                                 scale[i] = ( scale[i] < 0.0f ) ? -fabs( bestscale ) : fabs( bestscale );
504                         }
505                 }
506         }
507         //globalOutputStream() << "scale: " << scale <<"\n";
508         m_scalable.scale( scale );
509 }
510 };
511
512
513
514
515
516
517
518
519
520
521 class RenderableClippedPrimitive : public OpenGLRenderable
522 {
523 struct primitive_t
524 {
525         PointVertex m_points[9];
526         std::size_t m_count;
527 };
528 Matrix4 m_inverse;
529 std::vector<primitive_t> m_primitives;
530 public:
531 Matrix4 m_world;
532
533 void render( RenderStateFlags state ) const {
534         for ( std::size_t i = 0; i < m_primitives.size(); ++i )
535         {
536                 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_primitives[i].m_points[0].colour );
537                 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_primitives[i].m_points[0].vertex );
538                 switch ( m_primitives[i].m_count )
539                 {
540                 case 1: break;
541                 case 2: glDrawArrays( GL_LINES, 0, GLsizei( m_primitives[i].m_count ) ); break;
542                 default: glDrawArrays( GL_POLYGON, 0, GLsizei( m_primitives[i].m_count ) ); break;
543                 }
544         }
545 }
546
547 void construct( const Matrix4& world2device ){
548         m_inverse = matrix4_full_inverse( world2device );
549         m_world = g_matrix4_identity;
550 }
551
552 void insert( const Vector4 clipped[9], std::size_t count ){
553         add_one();
554
555         m_primitives.back().m_count = count;
556         for ( std::size_t i = 0; i < count; ++i )
557         {
558                 Vector3 world_point( vector4_projected( matrix4_transformed_vector4( m_inverse, clipped[i] ) ) );
559                 m_primitives.back().m_points[i].vertex = vertex3f_for_vector3( world_point );
560         }
561 }
562
563 void destroy(){
564         m_primitives.clear();
565 }
566 private:
567 void add_one(){
568         m_primitives.push_back( primitive_t() );
569
570         const Colour4b colour_clipped( 255, 127, 0, 255 );
571
572         for ( std::size_t i = 0; i < 9; ++i )
573                 m_primitives.back().m_points[i].colour = colour_clipped;
574 }
575 };
576
577 #if GDEF_DEBUG
578 #define DEBUG_SELECTION
579 #endif
580
581 #if defined( DEBUG_SELECTION )
582 Shader* g_state_clipped;
583 RenderableClippedPrimitive g_render_clipped;
584 #endif
585
586
587 #if 0
588 // dist_Point_to_Line(): get the distance of a point to a line.
589 //    Input:  a Point P and a Line L (in any dimension)
590 //    Return: the shortest distance from P to L
591 float
592 dist_Point_to_Line( Point P, Line L ){
593         Vector v = L.P1 - L.P0;
594         Vector w = P - L.P0;
595
596         double c1 = dot( w,v );
597         double c2 = dot( v,v );
598         double b = c1 / c2;
599
600         Point Pb = L.P0 + b * v;
601         return d( P, Pb );
602 }
603 #endif
604
605 class Segment3D
606 {
607 typedef Vector3 point_type;
608 public:
609 Segment3D( const point_type& _p0, const point_type& _p1 )
610         : p0( _p0 ), p1( _p1 ){
611 }
612
613 point_type p0, p1;
614 };
615
616 typedef Vector3 Point3D;
617
618 inline double vector3_distance_squared( const Point3D& a, const Point3D& b ){
619         return vector3_length_squared( b - a );
620 }
621
622 // get the distance of a point to a segment.
623 Point3D segment_closest_point_to_point( const Segment3D& segment, const Point3D& point ){
624         Vector3 v = segment.p1 - segment.p0;
625         Vector3 w = point - segment.p0;
626
627         double c1 = vector3_dot( w,v );
628         if ( c1 <= 0 ) {
629                 return segment.p0;
630         }
631
632         double c2 = vector3_dot( v,v );
633         if ( c2 <= c1 ) {
634                 return segment.p1;
635         }
636
637         return Point3D( segment.p0 + v * ( c1 / c2 ) );
638 }
639
640 double segment_dist_to_point_3d( const Segment3D& segment, const Point3D& point ){
641         return vector3_distance_squared( point, segment_closest_point_to_point( segment, point ) );
642 }
643
644 typedef Vector3 point_t;
645 typedef const Vector3* point_iterator_t;
646
647 // crossing number test for a point in a polygon
648 // This code is patterned after [Franklin, 2000]
649 bool point_test_polygon_2d( const point_t& P, point_iterator_t start, point_iterator_t finish ){
650         std::size_t crossings = 0;
651
652         // loop through all edges of the polygon
653         for ( point_iterator_t prev = finish - 1, cur = start; cur != finish; prev = cur, ++cur )
654         {  // edge from (*prev) to (*cur)
655                 if ( ( ( ( *prev )[1] <= P[1] ) && ( ( *cur )[1] > P[1] ) ) // an upward crossing
656                          || ( ( ( *prev )[1] > P[1] ) && ( ( *cur )[1] <= P[1] ) ) ) { // a downward crossing
657                                                                                       // compute the actual edge-ray intersect x-coordinate
658                         float vt = (float)( P[1] - ( *prev )[1] ) / ( ( *cur )[1] - ( *prev )[1] );
659                         if ( P[0] < ( *prev )[0] + vt * ( ( *cur )[0] - ( *prev )[0] ) ) { // P[0] < intersect
660                                 ++crossings; // a valid crossing of y=P[1] right of P[0]
661                         }
662                 }
663         }
664         return ( crossings & 0x1 ) != 0; // 0 if even (out), and 1 if odd (in)
665 }
666
667 inline double triangle_signed_area_XY( const Vector3& p0, const Vector3& p1, const Vector3& p2 ){
668         return ( ( p1[0] - p0[0] ) * ( p2[1] - p0[1] ) ) - ( ( p2[0] - p0[0] ) * ( p1[1] - p0[1] ) );
669 }
670
671 enum clipcull_t
672 {
673         eClipCullNone,
674         eClipCullCW,
675         eClipCullCCW,
676 };
677
678
679 inline SelectionIntersection select_point_from_clipped( Vector4& clipped ){
680         return SelectionIntersection( clipped[2] / clipped[3], static_cast<float>( vector3_length_squared( Vector3( clipped[0] / clipped[3], clipped[1] / clipped[3], 0 ) ) ) );
681 }
682
683 void BestPoint( std::size_t count, Vector4 clipped[9], SelectionIntersection& best, clipcull_t cull ){
684         Vector3 normalised[9];
685
686         {
687                 for ( std::size_t i = 0; i < count; ++i )
688                 {
689                         normalised[i][0] = clipped[i][0] / clipped[i][3];
690                         normalised[i][1] = clipped[i][1] / clipped[i][3];
691                         normalised[i][2] = clipped[i][2] / clipped[i][3];
692                 }
693         }
694
695         if ( cull != eClipCullNone && count > 2 ) {
696                 double signed_area = triangle_signed_area_XY( normalised[0], normalised[1], normalised[2] );
697
698                 if ( ( cull == eClipCullCW && signed_area > 0 )
699                          || ( cull == eClipCullCCW && signed_area < 0 ) ) {
700                         return;
701                 }
702         }
703
704         if ( count == 2 ) {
705                 Segment3D segment( normalised[0], normalised[1] );
706                 Point3D point = segment_closest_point_to_point( segment, Vector3( 0, 0, 0 ) );
707                 assign_if_closer( best, SelectionIntersection( point.z(), 0 ) );
708         }
709         else if ( count > 2 && !point_test_polygon_2d( Vector3( 0, 0, 0 ), normalised, normalised + count ) ) {
710                 point_iterator_t end = normalised + count;
711                 for ( point_iterator_t previous = end - 1, current = normalised; current != end; previous = current, ++current )
712                 {
713                         Segment3D segment( *previous, *current );
714                         Point3D point = segment_closest_point_to_point( segment, Vector3( 0, 0, 0 ) );
715                         float depth = point.z();
716                         point.z() = 0;
717                         float distance = static_cast<float>( vector3_length_squared( point ) );
718
719                         assign_if_closer( best, SelectionIntersection( depth, distance ) );
720                 }
721         }
722         else if ( count > 2 ) {
723                 assign_if_closer(
724                         best,
725                         SelectionIntersection(
726                                 static_cast<float>( ray_distance_to_plane(
727                                                                                 Ray( Vector3( 0, 0, 0 ), Vector3( 0, 0, 1 ) ),
728                                                                                 plane3_for_points( normalised[0], normalised[1], normalised[2] )
729                                                                                 ) ),
730                                 0
731                                 )
732                         );
733         }
734
735 #if defined( DEBUG_SELECTION )
736         if ( count >= 2 ) {
737                 g_render_clipped.insert( clipped, count );
738         }
739 #endif
740 }
741
742 void LineStrip_BestPoint( const Matrix4& local2view, const PointVertex* vertices, const std::size_t size, SelectionIntersection& best ){
743         Vector4 clipped[2];
744         for ( std::size_t i = 0; ( i + 1 ) < size; ++i )
745         {
746                 const std::size_t count = matrix4_clip_line( local2view, vertex3f_to_vector3( vertices[i].vertex ), vertex3f_to_vector3( vertices[i + 1].vertex ), clipped );
747                 BestPoint( count, clipped, best, eClipCullNone );
748         }
749 }
750
751 void LineLoop_BestPoint( const Matrix4& local2view, const PointVertex* vertices, const std::size_t size, SelectionIntersection& best ){
752         Vector4 clipped[2];
753         for ( std::size_t i = 0; i < size; ++i )
754         {
755                 const std::size_t count = matrix4_clip_line( local2view, vertex3f_to_vector3( vertices[i].vertex ), vertex3f_to_vector3( vertices[( i + 1 ) % size].vertex ), clipped );
756                 BestPoint( count, clipped, best, eClipCullNone );
757         }
758 }
759
760 void Line_BestPoint( const Matrix4& local2view, const PointVertex vertices[2], SelectionIntersection& best ){
761         Vector4 clipped[2];
762         const std::size_t count = matrix4_clip_line( local2view, vertex3f_to_vector3( vertices[0].vertex ), vertex3f_to_vector3( vertices[1].vertex ), clipped );
763         BestPoint( count, clipped, best, eClipCullNone );
764 }
765
766 void Circle_BestPoint( const Matrix4& local2view, clipcull_t cull, const PointVertex* vertices, const std::size_t size, SelectionIntersection& best ){
767         Vector4 clipped[9];
768         for ( std::size_t i = 0; i < size; ++i )
769         {
770                 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 );
771                 BestPoint( count, clipped, best, cull );
772         }
773 }
774
775 void Quad_BestPoint( const Matrix4& local2view, clipcull_t cull, const PointVertex* vertices, SelectionIntersection& best ){
776         Vector4 clipped[9];
777         {
778                 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 );
779                 BestPoint( count, clipped, best, cull );
780         }
781         {
782                 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 );
783                 BestPoint( count, clipped, best, cull );
784         }
785 }
786
787 struct FlatShadedVertex
788 {
789         Vertex3f vertex;
790         Colour4b colour;
791         Normal3f normal;
792
793         FlatShadedVertex(){
794         }
795 };
796
797
798 typedef FlatShadedVertex* FlatShadedVertexIterator;
799 void Triangles_BestPoint( const Matrix4& local2view, clipcull_t cull, FlatShadedVertexIterator first, FlatShadedVertexIterator last, SelectionIntersection& best ){
800         for ( FlatShadedVertexIterator x( first ), y( first + 1 ), z( first + 2 ); x != last; x += 3, y += 3, z += 3 )
801         {
802                 Vector4 clipped[9];
803                 BestPoint(
804                         matrix4_clip_triangle(
805                                 local2view,
806                                 reinterpret_cast<const Vector3&>( ( *x ).vertex ),
807                                 reinterpret_cast<const Vector3&>( ( *y ).vertex ),
808                                 reinterpret_cast<const Vector3&>( ( *z ).vertex ),
809                                 clipped
810                                 ),
811                         clipped,
812                         best,
813                         cull
814                         );
815         }
816 }
817
818
819 typedef std::multimap<SelectionIntersection, Selectable*> SelectableSortedSet;
820
821 class SelectionPool : public Selector
822 {
823 SelectableSortedSet m_pool;
824 SelectionIntersection m_intersection;
825 Selectable* m_selectable;
826
827 public:
828 void pushSelectable( Selectable& selectable ){
829         m_intersection = SelectionIntersection();
830         m_selectable = &selectable;
831 }
832 void popSelectable(){
833         addSelectable( m_intersection, m_selectable );
834         m_intersection = SelectionIntersection();
835 }
836 void addIntersection( const SelectionIntersection& intersection ){
837         assign_if_closer( m_intersection, intersection );
838 }
839 void addSelectable( const SelectionIntersection& intersection, Selectable* selectable ){
840         if ( intersection.valid() ) {
841                 m_pool.insert( SelectableSortedSet::value_type( intersection, selectable ) );
842         }
843 }
844
845 typedef SelectableSortedSet::iterator iterator;
846
847 iterator begin(){
848         return m_pool.begin();
849 }
850 iterator end(){
851         return m_pool.end();
852 }
853
854 bool failed(){
855         return m_pool.empty();
856 }
857 };
858
859
860 const Colour4b g_colour_sphere( 0, 0, 0, 255 );
861 const Colour4b g_colour_screen( 0, 255, 255, 255 );
862 const Colour4b g_colour_selected( 255, 255, 0, 255 );
863
864 inline const Colour4b& colourSelected( const Colour4b& colour, bool selected ){
865         return ( selected ) ? g_colour_selected : colour;
866 }
867
868 template<typename remap_policy>
869 inline void draw_semicircle( const std::size_t segments, const float radius, PointVertex* vertices, remap_policy remap ){
870         const double increment = c_pi / double(segments << 2);
871
872         std::size_t count = 0;
873         float x = radius;
874         float y = 0;
875         remap_policy::set( vertices[segments << 2].vertex, -radius, 0, 0 );
876         while ( count < segments )
877         {
878                 PointVertex* i = vertices + count;
879                 PointVertex* j = vertices + ( ( segments << 1 ) - ( count + 1 ) );
880
881                 PointVertex* k = i + ( segments << 1 );
882                 PointVertex* l = j + ( segments << 1 );
883
884 #if 0
885                 PointVertex* m = i + ( segments << 2 );
886                 PointVertex* n = j + ( segments << 2 );
887                 PointVertex* o = k + ( segments << 2 );
888                 PointVertex* p = l + ( segments << 2 );
889 #endif
890
891                 remap_policy::set( i->vertex, x,-y, 0 );
892                 remap_policy::set( k->vertex,-y,-x, 0 );
893 #if 0
894                 remap_policy::set( m->vertex,-x, y, 0 );
895                 remap_policy::set( o->vertex, y, x, 0 );
896 #endif
897
898                 ++count;
899
900                 {
901                         const double theta = increment * count;
902                         x = static_cast<float>( radius * cos( theta ) );
903                         y = static_cast<float>( radius * sin( theta ) );
904                 }
905
906                 remap_policy::set( j->vertex, y,-x, 0 );
907                 remap_policy::set( l->vertex,-x,-y, 0 );
908 #if 0
909                 remap_policy::set( n->vertex,-y, x, 0 );
910                 remap_policy::set( p->vertex, x, y, 0 );
911 #endif
912         }
913 }
914
915 class Manipulator
916 {
917 public:
918 virtual Manipulatable* GetManipulatable() = 0;
919 virtual void testSelect( const View& view, const Matrix4& pivot2world ){
920 }
921 virtual void render( Renderer& renderer, const VolumeTest& volume, const Matrix4& pivot2world ){
922 }
923 virtual void setSelected( bool select ) = 0;
924 virtual bool isSelected() const = 0;
925 };
926
927
928 inline Vector3 normalised_safe( const Vector3& self ){
929         if ( vector3_equal( self, g_vector3_identity ) ) {
930                 return g_vector3_identity;
931         }
932         return vector3_normalised( self );
933 }
934
935
936 class RotateManipulator : public Manipulator
937 {
938 struct RenderableCircle : public OpenGLRenderable
939 {
940         Array<PointVertex> m_vertices;
941
942         RenderableCircle( std::size_t size ) : m_vertices( size ){
943         }
944         void render( RenderStateFlags state ) const {
945                 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_vertices.data()->colour );
946                 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_vertices.data()->vertex );
947                 glDrawArrays( GL_LINE_LOOP, 0, GLsizei( m_vertices.size() ) );
948         }
949         void setColour( const Colour4b& colour ){
950                 for ( Array<PointVertex>::iterator i = m_vertices.begin(); i != m_vertices.end(); ++i )
951                 {
952                         ( *i ).colour = colour;
953                 }
954         }
955 };
956
957 struct RenderableSemiCircle : public OpenGLRenderable
958 {
959         Array<PointVertex> m_vertices;
960
961         RenderableSemiCircle( std::size_t size ) : m_vertices( size ){
962         }
963         void render( RenderStateFlags state ) const {
964                 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_vertices.data()->colour );
965                 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_vertices.data()->vertex );
966                 glDrawArrays( GL_LINE_STRIP, 0, GLsizei( m_vertices.size() ) );
967         }
968         void setColour( const Colour4b& colour ){
969                 for ( Array<PointVertex>::iterator i = m_vertices.begin(); i != m_vertices.end(); ++i )
970                 {
971                         ( *i ).colour = colour;
972                 }
973         }
974 };
975
976 RotateFree m_free;
977 RotateAxis m_axis;
978 Vector3 m_axis_screen;
979 RenderableSemiCircle m_circle_x;
980 RenderableSemiCircle m_circle_y;
981 RenderableSemiCircle m_circle_z;
982 RenderableCircle m_circle_screen;
983 RenderableCircle m_circle_sphere;
984 SelectableBool m_selectable_x;
985 SelectableBool m_selectable_y;
986 SelectableBool m_selectable_z;
987 SelectableBool m_selectable_screen;
988 SelectableBool m_selectable_sphere;
989 Pivot2World m_pivot;
990 Matrix4 m_local2world_x;
991 Matrix4 m_local2world_y;
992 Matrix4 m_local2world_z;
993 bool m_circle_x_visible;
994 bool m_circle_y_visible;
995 bool m_circle_z_visible;
996 public:
997 static Shader* m_state_outer;
998
999 RotateManipulator( Rotatable& rotatable, std::size_t segments, float radius ) :
1000         m_free( rotatable ),
1001         m_axis( rotatable ),
1002         m_circle_x( ( segments << 2 ) + 1 ),
1003         m_circle_y( ( segments << 2 ) + 1 ),
1004         m_circle_z( ( segments << 2 ) + 1 ),
1005         m_circle_screen( segments << 3 ),
1006         m_circle_sphere( segments << 3 ){
1007         draw_semicircle( segments, radius, m_circle_x.m_vertices.data(), RemapYZX() );
1008         draw_semicircle( segments, radius, m_circle_y.m_vertices.data(), RemapZXY() );
1009         draw_semicircle( segments, radius, m_circle_z.m_vertices.data(), RemapXYZ() );
1010
1011         draw_circle( segments, radius * 1.15f, m_circle_screen.m_vertices.data(), RemapXYZ() );
1012         draw_circle( segments, radius, m_circle_sphere.m_vertices.data(), RemapXYZ() );
1013
1014         m_selectable_sphere.setSelected( true );
1015 }
1016
1017
1018 void UpdateColours(){
1019         m_circle_x.setColour( colourSelected( g_colour_x, m_selectable_x.isSelected() ) );
1020         m_circle_y.setColour( colourSelected( g_colour_y, m_selectable_y.isSelected() ) );
1021         m_circle_z.setColour( colourSelected( g_colour_z, m_selectable_z.isSelected() ) );
1022         m_circle_screen.setColour( colourSelected( g_colour_screen, m_selectable_screen.isSelected() ) );
1023         m_circle_sphere.setColour( colourSelected( g_colour_sphere, false ) );
1024 }
1025
1026 void updateCircleTransforms(){
1027         Vector3 localViewpoint( matrix4_transformed_direction( matrix4_transposed( m_pivot.m_worldSpace ), vector4_to_vector3( m_pivot.m_viewpointSpace.z() ) ) );
1028
1029         m_circle_x_visible = !vector3_equal_epsilon( g_vector3_axis_x, localViewpoint, 1e-6f );
1030         if ( m_circle_x_visible ) {
1031                 m_local2world_x = g_matrix4_identity;
1032                 vector4_to_vector3( m_local2world_x.y() ) = normalised_safe(
1033                         vector3_cross( g_vector3_axis_x, localViewpoint )
1034                         );
1035                 vector4_to_vector3( m_local2world_x.z() ) = normalised_safe(
1036                         vector3_cross( vector4_to_vector3( m_local2world_x.x() ), vector4_to_vector3( m_local2world_x.y() ) )
1037                         );
1038                 matrix4_premultiply_by_matrix4( m_local2world_x, m_pivot.m_worldSpace );
1039         }
1040
1041         m_circle_y_visible = !vector3_equal_epsilon( g_vector3_axis_y, localViewpoint, 1e-6f );
1042         if ( m_circle_y_visible ) {
1043                 m_local2world_y = g_matrix4_identity;
1044                 vector4_to_vector3( m_local2world_y.z() ) = normalised_safe(
1045                         vector3_cross( g_vector3_axis_y, localViewpoint )
1046                         );
1047                 vector4_to_vector3( m_local2world_y.x() ) = normalised_safe(
1048                         vector3_cross( vector4_to_vector3( m_local2world_y.y() ), vector4_to_vector3( m_local2world_y.z() ) )
1049                         );
1050                 matrix4_premultiply_by_matrix4( m_local2world_y, m_pivot.m_worldSpace );
1051         }
1052
1053         m_circle_z_visible = !vector3_equal_epsilon( g_vector3_axis_z, localViewpoint, 1e-6f );
1054         if ( m_circle_z_visible ) {
1055                 m_local2world_z = g_matrix4_identity;
1056                 vector4_to_vector3( m_local2world_z.x() ) = normalised_safe(
1057                         vector3_cross( g_vector3_axis_z, localViewpoint )
1058                         );
1059                 vector4_to_vector3( m_local2world_z.y() ) = normalised_safe(
1060                         vector3_cross( vector4_to_vector3( m_local2world_z.z() ), vector4_to_vector3( m_local2world_z.x() ) )
1061                         );
1062                 matrix4_premultiply_by_matrix4( m_local2world_z, m_pivot.m_worldSpace );
1063         }
1064 }
1065
1066 void render( Renderer& renderer, const VolumeTest& volume, const Matrix4& pivot2world ){
1067         m_pivot.update( pivot2world, volume.GetModelview(), volume.GetProjection(), volume.GetViewport() );
1068         updateCircleTransforms();
1069
1070         // temp hack
1071         UpdateColours();
1072
1073         renderer.SetState( m_state_outer, Renderer::eWireframeOnly );
1074         renderer.SetState( m_state_outer, Renderer::eFullMaterials );
1075
1076         renderer.addRenderable( m_circle_screen, m_pivot.m_viewpointSpace );
1077         renderer.addRenderable( m_circle_sphere, m_pivot.m_viewpointSpace );
1078
1079         if ( m_circle_x_visible ) {
1080                 renderer.addRenderable( m_circle_x, m_local2world_x );
1081         }
1082         if ( m_circle_y_visible ) {
1083                 renderer.addRenderable( m_circle_y, m_local2world_y );
1084         }
1085         if ( m_circle_z_visible ) {
1086                 renderer.addRenderable( m_circle_z, m_local2world_z );
1087         }
1088 }
1089 void testSelect( const View& view, const Matrix4& pivot2world ){
1090         m_pivot.update( pivot2world, view.GetModelview(), view.GetProjection(), view.GetViewport() );
1091         updateCircleTransforms();
1092
1093         SelectionPool selector;
1094
1095         {
1096                 {
1097                         Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_local2world_x ) );
1098
1099 #if defined( DEBUG_SELECTION )
1100                         g_render_clipped.construct( view.GetViewMatrix() );
1101 #endif
1102
1103                         SelectionIntersection best;
1104                         LineStrip_BestPoint( local2view, m_circle_x.m_vertices.data(), m_circle_x.m_vertices.size(), best );
1105                         selector.addSelectable( best, &m_selectable_x );
1106                 }
1107
1108                 {
1109                         Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_local2world_y ) );
1110
1111 #if defined( DEBUG_SELECTION )
1112                         g_render_clipped.construct( view.GetViewMatrix() );
1113 #endif
1114
1115                         SelectionIntersection best;
1116                         LineStrip_BestPoint( local2view, m_circle_y.m_vertices.data(), m_circle_y.m_vertices.size(), best );
1117                         selector.addSelectable( best, &m_selectable_y );
1118                 }
1119
1120                 {
1121                         Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_local2world_z ) );
1122
1123 #if defined( DEBUG_SELECTION )
1124                         g_render_clipped.construct( view.GetViewMatrix() );
1125 #endif
1126
1127                         SelectionIntersection best;
1128                         LineStrip_BestPoint( local2view, m_circle_z.m_vertices.data(), m_circle_z.m_vertices.size(), best );
1129                         selector.addSelectable( best, &m_selectable_z );
1130                 }
1131         }
1132
1133         {
1134                 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_pivot.m_viewpointSpace ) );
1135
1136                 {
1137                         SelectionIntersection best;
1138                         LineLoop_BestPoint( local2view, m_circle_screen.m_vertices.data(), m_circle_screen.m_vertices.size(), best );
1139                         selector.addSelectable( best, &m_selectable_screen );
1140                 }
1141
1142                 {
1143                         SelectionIntersection best;
1144                         Circle_BestPoint( local2view, eClipCullCW, m_circle_sphere.m_vertices.data(), m_circle_sphere.m_vertices.size(), best );
1145                         selector.addSelectable( best, &m_selectable_sphere );
1146                 }
1147         }
1148
1149         m_axis_screen = m_pivot.m_axis_screen;
1150
1151         if ( !selector.failed() ) {
1152                 ( *selector.begin() ).second->setSelected( true );
1153         }
1154 }
1155
1156 Manipulatable* GetManipulatable(){
1157         if ( m_selectable_x.isSelected() ) {
1158                 m_axis.SetAxis( g_vector3_axis_x );
1159                 return &m_axis;
1160         }
1161         else if ( m_selectable_y.isSelected() ) {
1162                 m_axis.SetAxis( g_vector3_axis_y );
1163                 return &m_axis;
1164         }
1165         else if ( m_selectable_z.isSelected() ) {
1166                 m_axis.SetAxis( g_vector3_axis_z );
1167                 return &m_axis;
1168         }
1169         else if ( m_selectable_screen.isSelected() ) {
1170                 m_axis.SetAxis( m_axis_screen );
1171                 return &m_axis;
1172         }
1173         else{
1174                 return &m_free;
1175         }
1176 }
1177
1178 void setSelected( bool select ){
1179         m_selectable_x.setSelected( select );
1180         m_selectable_y.setSelected( select );
1181         m_selectable_z.setSelected( select );
1182         m_selectable_screen.setSelected( select );
1183 }
1184 bool isSelected() const {
1185         return m_selectable_x.isSelected()
1186                    | m_selectable_y.isSelected()
1187                    | m_selectable_z.isSelected()
1188                    | m_selectable_screen.isSelected()
1189                    | m_selectable_sphere.isSelected();
1190 }
1191 };
1192
1193 Shader* RotateManipulator::m_state_outer;
1194
1195
1196 const float arrowhead_length = 16;
1197 const float arrowhead_radius = 4;
1198
1199 inline void draw_arrowline( const float length, PointVertex* line, const std::size_t axis ){
1200         ( *line++ ).vertex = vertex3f_identity;
1201         ( *line ).vertex = vertex3f_identity;
1202         vertex3f_to_array( ( *line ).vertex )[axis] = length - arrowhead_length;
1203 }
1204
1205 template<typename VertexRemap, typename NormalRemap>
1206 inline void draw_arrowhead( const std::size_t segments, const float length, FlatShadedVertex* vertices, VertexRemap, NormalRemap ){
1207         std::size_t head_tris = ( segments << 3 );
1208         const double head_segment = c_2pi / head_tris;
1209         for ( std::size_t i = 0; i < head_tris; ++i )
1210         {
1211                 {
1212                         FlatShadedVertex& point = vertices[i * 6 + 0];
1213                         VertexRemap::x( point.vertex ) = length - arrowhead_length;
1214                         VertexRemap::y( point.vertex ) = arrowhead_radius * static_cast<float>( cos( i * head_segment ) );
1215                         VertexRemap::z( point.vertex ) = arrowhead_radius * static_cast<float>( sin( i * head_segment ) );
1216                         NormalRemap::x( point.normal ) = arrowhead_radius / arrowhead_length;
1217                         NormalRemap::y( point.normal ) = static_cast<float>( cos( i * head_segment ) );
1218                         NormalRemap::z( point.normal ) = static_cast<float>( sin( i * head_segment ) );
1219                 }
1220                 {
1221                         FlatShadedVertex& point = vertices[i * 6 + 1];
1222                         VertexRemap::x( point.vertex ) = length;
1223                         VertexRemap::y( point.vertex ) = 0;
1224                         VertexRemap::z( point.vertex ) = 0;
1225                         NormalRemap::x( point.normal ) = arrowhead_radius / arrowhead_length;
1226                         NormalRemap::y( point.normal ) = static_cast<float>( cos( ( i + 0.5 ) * head_segment ) );
1227                         NormalRemap::z( point.normal ) = static_cast<float>( sin( ( i + 0.5 ) * head_segment ) );
1228                 }
1229                 {
1230                         FlatShadedVertex& point = vertices[i * 6 + 2];
1231                         VertexRemap::x( point.vertex ) = length - arrowhead_length;
1232                         VertexRemap::y( point.vertex ) = arrowhead_radius * static_cast<float>( cos( ( i + 1 ) * head_segment ) );
1233                         VertexRemap::z( point.vertex ) = arrowhead_radius * static_cast<float>( sin( ( i + 1 ) * head_segment ) );
1234                         NormalRemap::x( point.normal ) = arrowhead_radius / arrowhead_length;
1235                         NormalRemap::y( point.normal ) = static_cast<float>( cos( ( i + 1 ) * head_segment ) );
1236                         NormalRemap::z( point.normal ) = static_cast<float>( sin( ( i + 1 ) * head_segment ) );
1237                 }
1238
1239                 {
1240                         FlatShadedVertex& point = vertices[i * 6 + 3];
1241                         VertexRemap::x( point.vertex ) = length - arrowhead_length;
1242                         VertexRemap::y( point.vertex ) = 0;
1243                         VertexRemap::z( point.vertex ) = 0;
1244                         NormalRemap::x( point.normal ) = -1;
1245                         NormalRemap::y( point.normal ) = 0;
1246                         NormalRemap::z( point.normal ) = 0;
1247                 }
1248                 {
1249                         FlatShadedVertex& point = vertices[i * 6 + 4];
1250                         VertexRemap::x( point.vertex ) = length - arrowhead_length;
1251                         VertexRemap::y( point.vertex ) = arrowhead_radius * static_cast<float>( cos( i * head_segment ) );
1252                         VertexRemap::z( point.vertex ) = arrowhead_radius * static_cast<float>( sin( i * head_segment ) );
1253                         NormalRemap::x( point.normal ) = -1;
1254                         NormalRemap::y( point.normal ) = 0;
1255                         NormalRemap::z( point.normal ) = 0;
1256                 }
1257                 {
1258                         FlatShadedVertex& point = vertices[i * 6 + 5];
1259                         VertexRemap::x( point.vertex ) = length - arrowhead_length;
1260                         VertexRemap::y( point.vertex ) = arrowhead_radius * static_cast<float>( cos( ( i + 1 ) * head_segment ) );
1261                         VertexRemap::z( point.vertex ) = arrowhead_radius * static_cast<float>( sin( ( i + 1 ) * head_segment ) );
1262                         NormalRemap::x( point.normal ) = -1;
1263                         NormalRemap::y( point.normal ) = 0;
1264                         NormalRemap::z( point.normal ) = 0;
1265                 }
1266         }
1267 }
1268
1269 template<typename Triple>
1270 class TripleRemapXYZ
1271 {
1272 public:
1273 static float& x( Triple& triple ){
1274         return triple.x();
1275 }
1276 static float& y( Triple& triple ){
1277         return triple.y();
1278 }
1279 static float& z( Triple& triple ){
1280         return triple.z();
1281 }
1282 };
1283
1284 template<typename Triple>
1285 class TripleRemapYZX
1286 {
1287 public:
1288 static float& x( Triple& triple ){
1289         return triple.y();
1290 }
1291 static float& y( Triple& triple ){
1292         return triple.z();
1293 }
1294 static float& z( Triple& triple ){
1295         return triple.x();
1296 }
1297 };
1298
1299 template<typename Triple>
1300 class TripleRemapZXY
1301 {
1302 public:
1303 static float& x( Triple& triple ){
1304         return triple.z();
1305 }
1306 static float& y( Triple& triple ){
1307         return triple.x();
1308 }
1309 static float& z( Triple& triple ){
1310         return triple.y();
1311 }
1312 };
1313
1314 void vector3_print( const Vector3& v ){
1315         globalOutputStream() << "( " << v.x() << " " << v.y() << " " << v.z() << " )";
1316 }
1317
1318 class TranslateManipulator : public Manipulator
1319 {
1320 struct RenderableArrowLine : public OpenGLRenderable
1321 {
1322         PointVertex m_line[2];
1323
1324         RenderableArrowLine(){
1325         }
1326         void render( RenderStateFlags state ) const {
1327                 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_line[0].colour );
1328                 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_line[0].vertex );
1329                 glDrawArrays( GL_LINES, 0, 2 );
1330         }
1331         void setColour( const Colour4b& colour ){
1332                 m_line[0].colour = colour;
1333                 m_line[1].colour = colour;
1334         }
1335 };
1336 struct RenderableArrowHead : public OpenGLRenderable
1337 {
1338         Array<FlatShadedVertex> m_vertices;
1339
1340         RenderableArrowHead( std::size_t size )
1341                 : m_vertices( size ){
1342         }
1343         void render( RenderStateFlags state ) const {
1344                 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( FlatShadedVertex ), &m_vertices.data()->colour );
1345                 glVertexPointer( 3, GL_FLOAT, sizeof( FlatShadedVertex ), &m_vertices.data()->vertex );
1346                 glNormalPointer( GL_FLOAT, sizeof( FlatShadedVertex ), &m_vertices.data()->normal );
1347                 glDrawArrays( GL_TRIANGLES, 0, GLsizei( m_vertices.size() ) );
1348         }
1349         void setColour( const Colour4b& colour ){
1350                 for ( Array<FlatShadedVertex>::iterator i = m_vertices.begin(); i != m_vertices.end(); ++i )
1351                 {
1352                         ( *i ).colour = colour;
1353                 }
1354         }
1355 };
1356 struct RenderableQuad : public OpenGLRenderable
1357 {
1358         PointVertex m_quad[4];
1359         void render( RenderStateFlags state ) const {
1360                 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_quad[0].colour );
1361                 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_quad[0].vertex );
1362                 glDrawArrays( GL_LINE_LOOP, 0, 4 );
1363         }
1364         void setColour( const Colour4b& colour ){
1365                 m_quad[0].colour = colour;
1366                 m_quad[1].colour = colour;
1367                 m_quad[2].colour = colour;
1368                 m_quad[3].colour = colour;
1369         }
1370 };
1371
1372 TranslateFree m_free;
1373 TranslateAxis m_axis;
1374 RenderableArrowLine m_arrow_x;
1375 RenderableArrowLine m_arrow_y;
1376 RenderableArrowLine m_arrow_z;
1377 RenderableArrowHead m_arrow_head_x;
1378 RenderableArrowHead m_arrow_head_y;
1379 RenderableArrowHead m_arrow_head_z;
1380 RenderableQuad m_quad_screen;
1381 SelectableBool m_selectable_x;
1382 SelectableBool m_selectable_y;
1383 SelectableBool m_selectable_z;
1384 SelectableBool m_selectable_screen;
1385 Pivot2World m_pivot;
1386 public:
1387 static Shader* m_state_wire;
1388 static Shader* m_state_fill;
1389
1390 TranslateManipulator( Translatable& translatable, std::size_t segments, float length ) :
1391         m_free( translatable ),
1392         m_axis( translatable ),
1393         m_arrow_head_x( 3 * 2 * ( segments << 3 ) ),
1394         m_arrow_head_y( 3 * 2 * ( segments << 3 ) ),
1395         m_arrow_head_z( 3 * 2 * ( segments << 3 ) ){
1396         draw_arrowline( length, m_arrow_x.m_line, 0 );
1397         draw_arrowhead( segments, length, m_arrow_head_x.m_vertices.data(), TripleRemapXYZ<Vertex3f>(), TripleRemapXYZ<Normal3f>() );
1398         draw_arrowline( length, m_arrow_y.m_line, 1 );
1399         draw_arrowhead( segments, length, m_arrow_head_y.m_vertices.data(), TripleRemapYZX<Vertex3f>(), TripleRemapYZX<Normal3f>() );
1400         draw_arrowline( length, m_arrow_z.m_line, 2 );
1401         draw_arrowhead( segments, length, m_arrow_head_z.m_vertices.data(), TripleRemapZXY<Vertex3f>(), TripleRemapZXY<Normal3f>() );
1402
1403         draw_quad( 16, m_quad_screen.m_quad );
1404 }
1405
1406 void UpdateColours(){
1407         m_arrow_x.setColour( colourSelected( g_colour_x, m_selectable_x.isSelected() ) );
1408         m_arrow_head_x.setColour( colourSelected( g_colour_x, m_selectable_x.isSelected() ) );
1409         m_arrow_y.setColour( colourSelected( g_colour_y, m_selectable_y.isSelected() ) );
1410         m_arrow_head_y.setColour( colourSelected( g_colour_y, m_selectable_y.isSelected() ) );
1411         m_arrow_z.setColour( colourSelected( g_colour_z, m_selectable_z.isSelected() ) );
1412         m_arrow_head_z.setColour( colourSelected( g_colour_z, m_selectable_z.isSelected() ) );
1413         m_quad_screen.setColour( colourSelected( g_colour_screen, m_selectable_screen.isSelected() ) );
1414 }
1415
1416 bool manipulator_show_axis( const Pivot2World& pivot, const Vector3& axis ){
1417         return fabs( vector3_dot( pivot.m_axis_screen, axis ) ) < 0.95;
1418 }
1419
1420 void render( Renderer& renderer, const VolumeTest& volume, const Matrix4& pivot2world ){
1421         m_pivot.update( pivot2world, volume.GetModelview(), volume.GetProjection(), volume.GetViewport() );
1422
1423         // temp hack
1424         UpdateColours();
1425
1426         Vector3 x = vector3_normalised( vector4_to_vector3( m_pivot.m_worldSpace.x() ) );
1427         bool show_x = manipulator_show_axis( m_pivot, x );
1428
1429         Vector3 y = vector3_normalised( vector4_to_vector3( m_pivot.m_worldSpace.y() ) );
1430         bool show_y = manipulator_show_axis( m_pivot, y );
1431
1432         Vector3 z = vector3_normalised( vector4_to_vector3( m_pivot.m_worldSpace.z() ) );
1433         bool show_z = manipulator_show_axis( m_pivot, z );
1434
1435         renderer.SetState( m_state_wire, Renderer::eWireframeOnly );
1436         renderer.SetState( m_state_wire, Renderer::eFullMaterials );
1437
1438         if ( show_x ) {
1439                 renderer.addRenderable( m_arrow_x, m_pivot.m_worldSpace );
1440         }
1441         if ( show_y ) {
1442                 renderer.addRenderable( m_arrow_y, m_pivot.m_worldSpace );
1443         }
1444         if ( show_z ) {
1445                 renderer.addRenderable( m_arrow_z, m_pivot.m_worldSpace );
1446         }
1447
1448         renderer.addRenderable( m_quad_screen, m_pivot.m_viewplaneSpace );
1449
1450         renderer.SetState( m_state_fill, Renderer::eWireframeOnly );
1451         renderer.SetState( m_state_fill, Renderer::eFullMaterials );
1452
1453         if ( show_x ) {
1454                 renderer.addRenderable( m_arrow_head_x, m_pivot.m_worldSpace );
1455         }
1456         if ( show_y ) {
1457                 renderer.addRenderable( m_arrow_head_y, m_pivot.m_worldSpace );
1458         }
1459         if ( show_z ) {
1460                 renderer.addRenderable( m_arrow_head_z, m_pivot.m_worldSpace );
1461         }
1462 }
1463 void testSelect( const View& view, const Matrix4& pivot2world ){
1464         m_pivot.update( pivot2world, view.GetModelview(), view.GetProjection(), view.GetViewport() );
1465
1466         SelectionPool selector;
1467
1468         Vector3 x = vector3_normalised( vector4_to_vector3( m_pivot.m_worldSpace.x() ) );
1469         bool show_x = manipulator_show_axis( m_pivot, x );
1470
1471         Vector3 y = vector3_normalised( vector4_to_vector3( m_pivot.m_worldSpace.y() ) );
1472         bool show_y = manipulator_show_axis( m_pivot, y );
1473
1474         Vector3 z = vector3_normalised( vector4_to_vector3( m_pivot.m_worldSpace.z() ) );
1475         bool show_z = manipulator_show_axis( m_pivot, z );
1476
1477         {
1478                 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_pivot.m_viewpointSpace ) );
1479
1480                 {
1481                         SelectionIntersection best;
1482                         Quad_BestPoint( local2view, eClipCullCW, m_quad_screen.m_quad, best );
1483                         if ( best.valid() ) {
1484                                 best = SelectionIntersection( 0, 0 );
1485                                 selector.addSelectable( best, &m_selectable_screen );
1486                         }
1487                 }
1488         }
1489
1490         {
1491                 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_pivot.m_worldSpace ) );
1492
1493 #if defined( DEBUG_SELECTION )
1494                 g_render_clipped.construct( view.GetViewMatrix() );
1495 #endif
1496
1497                 if ( show_x ) {
1498                         SelectionIntersection best;
1499                         Line_BestPoint( local2view, m_arrow_x.m_line, best );
1500                         Triangles_BestPoint( local2view, eClipCullCW, m_arrow_head_x.m_vertices.begin(), m_arrow_head_x.m_vertices.end(), best );
1501                         selector.addSelectable( best, &m_selectable_x );
1502                 }
1503
1504                 if ( show_y ) {
1505                         SelectionIntersection best;
1506                         Line_BestPoint( local2view, m_arrow_y.m_line, best );
1507                         Triangles_BestPoint( local2view, eClipCullCW, m_arrow_head_y.m_vertices.begin(), m_arrow_head_y.m_vertices.end(), best );
1508                         selector.addSelectable( best, &m_selectable_y );
1509                 }
1510
1511                 if ( show_z ) {
1512                         SelectionIntersection best;
1513                         Line_BestPoint( local2view, m_arrow_z.m_line, best );
1514                         Triangles_BestPoint( local2view, eClipCullCW, m_arrow_head_z.m_vertices.begin(), m_arrow_head_z.m_vertices.end(), best );
1515                         selector.addSelectable( best, &m_selectable_z );
1516                 }
1517         }
1518
1519         if ( !selector.failed() ) {
1520                 ( *selector.begin() ).second->setSelected( true );
1521         }
1522 }
1523
1524 Manipulatable* GetManipulatable(){
1525         if ( m_selectable_x.isSelected() ) {
1526                 m_axis.SetAxis( g_vector3_axis_x );
1527                 return &m_axis;
1528         }
1529         else if ( m_selectable_y.isSelected() ) {
1530                 m_axis.SetAxis( g_vector3_axis_y );
1531                 return &m_axis;
1532         }
1533         else if ( m_selectable_z.isSelected() ) {
1534                 m_axis.SetAxis( g_vector3_axis_z );
1535                 return &m_axis;
1536         }
1537         else
1538         {
1539                 return &m_free;
1540         }
1541 }
1542
1543 void setSelected( bool select ){
1544         m_selectable_x.setSelected( select );
1545         m_selectable_y.setSelected( select );
1546         m_selectable_z.setSelected( select );
1547         m_selectable_screen.setSelected( select );
1548 }
1549 bool isSelected() const {
1550         return m_selectable_x.isSelected()
1551                    | m_selectable_y.isSelected()
1552                    | m_selectable_z.isSelected()
1553                    | m_selectable_screen.isSelected();
1554 }
1555 };
1556
1557 Shader* TranslateManipulator::m_state_wire;
1558 Shader* TranslateManipulator::m_state_fill;
1559
1560 class ScaleManipulator : public Manipulator
1561 {
1562 struct RenderableArrow : public OpenGLRenderable
1563 {
1564         PointVertex m_line[2];
1565
1566         void render( RenderStateFlags state ) const {
1567                 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_line[0].colour );
1568                 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_line[0].vertex );
1569                 glDrawArrays( GL_LINES, 0, 2 );
1570         }
1571         void setColour( const Colour4b& colour ){
1572                 m_line[0].colour = colour;
1573                 m_line[1].colour = colour;
1574         }
1575 };
1576 struct RenderableQuad : public OpenGLRenderable
1577 {
1578         PointVertex m_quad[4];
1579         void render( RenderStateFlags state ) const {
1580                 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_quad[0].colour );
1581                 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_quad[0].vertex );
1582                 glDrawArrays( GL_QUADS, 0, 4 );
1583         }
1584         void setColour( const Colour4b& colour ){
1585                 m_quad[0].colour = colour;
1586                 m_quad[1].colour = colour;
1587                 m_quad[2].colour = colour;
1588                 m_quad[3].colour = colour;
1589         }
1590 };
1591
1592 ScaleFree m_free;
1593 ScaleAxis m_axis;
1594 RenderableArrow m_arrow_x;
1595 RenderableArrow m_arrow_y;
1596 RenderableArrow m_arrow_z;
1597 RenderableQuad m_quad_screen;
1598 SelectableBool m_selectable_x;
1599 SelectableBool m_selectable_y;
1600 SelectableBool m_selectable_z;
1601 SelectableBool m_selectable_screen;
1602 Pivot2World m_pivot;
1603 public:
1604 ScaleManipulator( Scalable& scalable, std::size_t segments, float length ) :
1605         m_free( scalable ),
1606         m_axis( scalable ){
1607         draw_arrowline( length, m_arrow_x.m_line, 0 );
1608         draw_arrowline( length, m_arrow_y.m_line, 1 );
1609         draw_arrowline( length, m_arrow_z.m_line, 2 );
1610
1611         draw_quad( 16, m_quad_screen.m_quad );
1612 }
1613
1614 Pivot2World& getPivot(){
1615         return m_pivot;
1616 }
1617
1618 void UpdateColours(){
1619         m_arrow_x.setColour( colourSelected( g_colour_x, m_selectable_x.isSelected() ) );
1620         m_arrow_y.setColour( colourSelected( g_colour_y, m_selectable_y.isSelected() ) );
1621         m_arrow_z.setColour( colourSelected( g_colour_z, m_selectable_z.isSelected() ) );
1622         m_quad_screen.setColour( colourSelected( g_colour_screen, m_selectable_screen.isSelected() ) );
1623 }
1624
1625 void render( Renderer& renderer, const VolumeTest& volume, const Matrix4& pivot2world ){
1626         m_pivot.update( pivot2world, volume.GetModelview(), volume.GetProjection(), volume.GetViewport() );
1627
1628         // temp hack
1629         UpdateColours();
1630
1631         renderer.addRenderable( m_arrow_x, m_pivot.m_worldSpace );
1632         renderer.addRenderable( m_arrow_y, m_pivot.m_worldSpace );
1633         renderer.addRenderable( m_arrow_z, m_pivot.m_worldSpace );
1634
1635         renderer.addRenderable( m_quad_screen, m_pivot.m_viewpointSpace );
1636 }
1637 void testSelect( const View& view, const Matrix4& pivot2world ){
1638         m_pivot.update( pivot2world, view.GetModelview(), view.GetProjection(), view.GetViewport() );
1639
1640         SelectionPool selector;
1641
1642         {
1643                 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_pivot.m_worldSpace ) );
1644
1645 #if defined( DEBUG_SELECTION )
1646                 g_render_clipped.construct( view.GetViewMatrix() );
1647 #endif
1648
1649                 {
1650                         SelectionIntersection best;
1651                         Line_BestPoint( local2view, m_arrow_x.m_line, best );
1652                         selector.addSelectable( best, &m_selectable_x );
1653                 }
1654
1655                 {
1656                         SelectionIntersection best;
1657                         Line_BestPoint( local2view, m_arrow_y.m_line, best );
1658                         selector.addSelectable( best, &m_selectable_y );
1659                 }
1660
1661                 {
1662                         SelectionIntersection best;
1663                         Line_BestPoint( local2view, m_arrow_z.m_line, best );
1664                         selector.addSelectable( best, &m_selectable_z );
1665                 }
1666         }
1667
1668         {
1669                 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_pivot.m_viewpointSpace ) );
1670
1671                 {
1672                         SelectionIntersection best;
1673                         Quad_BestPoint( local2view, eClipCullCW, m_quad_screen.m_quad, best );
1674                         selector.addSelectable( best, &m_selectable_screen );
1675                 }
1676         }
1677
1678         if ( !selector.failed() ) {
1679                 ( *selector.begin() ).second->setSelected( true );
1680         }
1681 }
1682
1683 Manipulatable* GetManipulatable(){
1684         if ( m_selectable_x.isSelected() ) {
1685                 m_axis.SetAxis( g_vector3_axis_x );
1686                 return &m_axis;
1687         }
1688         else if ( m_selectable_y.isSelected() ) {
1689                 m_axis.SetAxis( g_vector3_axis_y );
1690                 return &m_axis;
1691         }
1692         else if ( m_selectable_z.isSelected() ) {
1693                 m_axis.SetAxis( g_vector3_axis_z );
1694                 return &m_axis;
1695         }
1696         else{
1697                 return &m_free;
1698         }
1699 }
1700
1701 void setSelected( bool select ){
1702         m_selectable_x.setSelected( select );
1703         m_selectable_y.setSelected( select );
1704         m_selectable_z.setSelected( select );
1705         m_selectable_screen.setSelected( select );
1706 }
1707 bool isSelected() const {
1708         return m_selectable_x.isSelected()
1709                    | m_selectable_y.isSelected()
1710                    | m_selectable_z.isSelected()
1711                    | m_selectable_screen.isSelected();
1712 }
1713 };
1714
1715
1716 inline PlaneSelectable* Instance_getPlaneSelectable( scene::Instance& instance ){
1717         return InstanceTypeCast<PlaneSelectable>::cast( instance );
1718 }
1719
1720 class PlaneSelectableSelectPlanes : public scene::Graph::Walker
1721 {
1722 Selector& m_selector;
1723 SelectionTest& m_test;
1724 PlaneCallback m_selectedPlaneCallback;
1725 public:
1726 PlaneSelectableSelectPlanes( Selector& selector, SelectionTest& test, const PlaneCallback& selectedPlaneCallback )
1727         : m_selector( selector ), m_test( test ), m_selectedPlaneCallback( selectedPlaneCallback ){
1728 }
1729 bool pre( const scene::Path& path, scene::Instance& instance ) const {
1730         if ( path.top().get().visible() ) {
1731                 Selectable* selectable = Instance_getSelectable( instance );
1732                 if ( selectable != 0 && selectable->isSelected() ) {
1733                         PlaneSelectable* planeSelectable = Instance_getPlaneSelectable( instance );
1734                         if ( planeSelectable != 0 ) {
1735                                 planeSelectable->selectPlanes( m_selector, m_test, m_selectedPlaneCallback );
1736                         }
1737                 }
1738         }
1739         return true;
1740 }
1741 };
1742
1743 class PlaneSelectableSelectReversedPlanes : public scene::Graph::Walker
1744 {
1745 Selector& m_selector;
1746 const SelectedPlanes& m_selectedPlanes;
1747 public:
1748 PlaneSelectableSelectReversedPlanes( Selector& selector, const SelectedPlanes& selectedPlanes )
1749         : m_selector( selector ), m_selectedPlanes( selectedPlanes ){
1750 }
1751 bool pre( const scene::Path& path, scene::Instance& instance ) const {
1752         if ( path.top().get().visible() ) {
1753                 Selectable* selectable = Instance_getSelectable( instance );
1754                 if ( selectable != 0 && selectable->isSelected() ) {
1755                         PlaneSelectable* planeSelectable = Instance_getPlaneSelectable( instance );
1756                         if ( planeSelectable != 0 ) {
1757                                 planeSelectable->selectReversedPlanes( m_selector, m_selectedPlanes );
1758                         }
1759                 }
1760         }
1761         return true;
1762 }
1763 };
1764
1765 void Scene_forEachPlaneSelectable_selectPlanes( scene::Graph& graph, Selector& selector, SelectionTest& test, const PlaneCallback& selectedPlaneCallback ){
1766         graph.traverse( PlaneSelectableSelectPlanes( selector, test, selectedPlaneCallback ) );
1767 }
1768
1769 void Scene_forEachPlaneSelectable_selectReversedPlanes( scene::Graph& graph, Selector& selector, const SelectedPlanes& selectedPlanes ){
1770         graph.traverse( PlaneSelectableSelectReversedPlanes( selector, selectedPlanes ) );
1771 }
1772
1773
1774 class PlaneLess
1775 {
1776 public:
1777 bool operator()( const Plane3& plane, const Plane3& other ) const {
1778         if ( plane.a < other.a ) {
1779                 return true;
1780         }
1781         if ( other.a < plane.a ) {
1782                 return false;
1783         }
1784
1785         if ( plane.b < other.b ) {
1786                 return true;
1787         }
1788         if ( other.b < plane.b ) {
1789                 return false;
1790         }
1791
1792         if ( plane.c < other.c ) {
1793                 return true;
1794         }
1795         if ( other.c < plane.c ) {
1796                 return false;
1797         }
1798
1799         if ( plane.d < other.d ) {
1800                 return true;
1801         }
1802         if ( other.d < plane.d ) {
1803                 return false;
1804         }
1805
1806         return false;
1807 }
1808 };
1809
1810 typedef std::set<Plane3, PlaneLess> PlaneSet;
1811
1812 inline void PlaneSet_insert( PlaneSet& self, const Plane3& plane ){
1813         self.insert( plane );
1814 }
1815
1816 inline bool PlaneSet_contains( const PlaneSet& self, const Plane3& plane ){
1817         return self.find( plane ) != self.end();
1818 }
1819
1820
1821 class SelectedPlaneSet : public SelectedPlanes
1822 {
1823 PlaneSet m_selectedPlanes;
1824 public:
1825 bool empty() const {
1826         return m_selectedPlanes.empty();
1827 }
1828
1829 void insert( const Plane3& plane ){
1830         PlaneSet_insert( m_selectedPlanes, plane );
1831 }
1832 bool contains( const Plane3& plane ) const {
1833         return PlaneSet_contains( m_selectedPlanes, plane );
1834 }
1835 typedef MemberCaller<SelectedPlaneSet, void(const Plane3&), &SelectedPlaneSet::insert> InsertCaller;
1836 };
1837
1838
1839 bool Scene_forEachPlaneSelectable_selectPlanes( scene::Graph& graph, Selector& selector, SelectionTest& test ){
1840         SelectedPlaneSet selectedPlanes;
1841
1842         Scene_forEachPlaneSelectable_selectPlanes( graph, selector, test, SelectedPlaneSet::InsertCaller( selectedPlanes ) );
1843         Scene_forEachPlaneSelectable_selectReversedPlanes( graph, selector, selectedPlanes );
1844
1845         return !selectedPlanes.empty();
1846 }
1847
1848 void Scene_Translate_Component_Selected( scene::Graph& graph, const Vector3& translation );
1849 void Scene_Translate_Selected( scene::Graph& graph, const Vector3& translation );
1850 void Scene_TestSelect_Primitive( Selector& selector, SelectionTest& test, const VolumeTest& volume );
1851 void Scene_TestSelect_Component( Selector& selector, SelectionTest& test, const VolumeTest& volume, SelectionSystem::EComponentMode componentMode );
1852 void Scene_TestSelect_Component_Selected( Selector& selector, SelectionTest& test, const VolumeTest& volume, SelectionSystem::EComponentMode componentMode );
1853 void Scene_SelectAll_Component( bool select, SelectionSystem::EComponentMode componentMode );
1854
1855 class ResizeTranslatable : public Translatable
1856 {
1857 void translate( const Vector3& translation ){
1858         Scene_Translate_Component_Selected( GlobalSceneGraph(), translation );
1859 }
1860 };
1861
1862 class DragTranslatable : public Translatable
1863 {
1864 void translate( const Vector3& translation ){
1865         if ( GlobalSelectionSystem().Mode() == SelectionSystem::eComponent ) {
1866                 Scene_Translate_Component_Selected( GlobalSceneGraph(), translation );
1867         }
1868         else
1869         {
1870                 Scene_Translate_Selected( GlobalSceneGraph(), translation );
1871         }
1872 }
1873 };
1874
1875 class SelectionVolume : public SelectionTest
1876 {
1877 Matrix4 m_local2view;
1878 const View& m_view;
1879 clipcull_t m_cull;
1880 Vector3 m_near;
1881 Vector3 m_far;
1882 public:
1883 SelectionVolume( const View& view )
1884         : m_view( view ){
1885 }
1886
1887 const VolumeTest& getVolume() const {
1888         return m_view;
1889 }
1890
1891 const Vector3& getNear() const {
1892         return m_near;
1893 }
1894 const Vector3& getFar() const {
1895         return m_far;
1896 }
1897
1898 void BeginMesh( const Matrix4& localToWorld, bool twoSided ){
1899         m_local2view = matrix4_multiplied_by_matrix4( m_view.GetViewMatrix(), localToWorld );
1900
1901         // Cull back-facing polygons based on winding being clockwise or counter-clockwise.
1902         // Don't cull if the view is wireframe and the polygons are two-sided.
1903         m_cull = twoSided && !m_view.fill() ? eClipCullNone : ( matrix4_handedness( localToWorld ) == MATRIX4_RIGHTHANDED ) ? eClipCullCW : eClipCullCCW;
1904
1905         {
1906                 Matrix4 screen2world( matrix4_full_inverse( m_local2view ) );
1907
1908                 m_near = vector4_projected(
1909                         matrix4_transformed_vector4(
1910                                 screen2world,
1911                                 Vector4( 0, 0, -1, 1 )
1912                                 )
1913                         );
1914
1915                 m_far = vector4_projected(
1916                         matrix4_transformed_vector4(
1917                                 screen2world,
1918                                 Vector4( 0, 0, 1, 1 )
1919                                 )
1920                         );
1921         }
1922
1923 #if defined( DEBUG_SELECTION )
1924         g_render_clipped.construct( m_view.GetViewMatrix() );
1925 #endif
1926 }
1927 void TestPoint( const Vector3& point, SelectionIntersection& best ){
1928         Vector4 clipped;
1929         if ( matrix4_clip_point( m_local2view, point, clipped ) == c_CLIP_PASS ) {
1930                 best = select_point_from_clipped( clipped );
1931         }
1932 }
1933 void TestPolygon( const VertexPointer& vertices, std::size_t count, SelectionIntersection& best ){
1934         Vector4 clipped[9];
1935         for ( std::size_t i = 0; i + 2 < count; ++i )
1936         {
1937                 BestPoint(
1938                         matrix4_clip_triangle(
1939                                 m_local2view,
1940                                 reinterpret_cast<const Vector3&>( vertices[0] ),
1941                                 reinterpret_cast<const Vector3&>( vertices[i + 1] ),
1942                                 reinterpret_cast<const Vector3&>( vertices[i + 2] ),
1943                                 clipped
1944                                 ),
1945                         clipped,
1946                         best,
1947                         m_cull
1948                         );
1949         }
1950 }
1951 void TestLineLoop( const VertexPointer& vertices, std::size_t count, SelectionIntersection& best ){
1952         if ( count == 0 ) {
1953                 return;
1954         }
1955         Vector4 clipped[9];
1956         for ( VertexPointer::iterator i = vertices.begin(), end = i + count, prev = i + ( count - 1 ); i != end; prev = i, ++i )
1957         {
1958                 BestPoint(
1959                         matrix4_clip_line(
1960                                 m_local2view,
1961                                 reinterpret_cast<const Vector3&>( ( *prev ) ),
1962                                 reinterpret_cast<const Vector3&>( ( *i ) ),
1963                                 clipped
1964                                 ),
1965                         clipped,
1966                         best,
1967                         m_cull
1968                         );
1969         }
1970 }
1971 void TestLineStrip( const VertexPointer& vertices, std::size_t count, SelectionIntersection& best ){
1972         if ( count == 0 ) {
1973                 return;
1974         }
1975         Vector4 clipped[9];
1976         for ( VertexPointer::iterator i = vertices.begin(), end = i + count, next = i + 1; next != end; i = next, ++next )
1977         {
1978                 BestPoint(
1979                         matrix4_clip_line(
1980                                 m_local2view,
1981                                 reinterpret_cast<const Vector3&>( ( *i ) ),
1982                                 reinterpret_cast<const Vector3&>( ( *next ) ),
1983                                 clipped
1984                                 ),
1985                         clipped,
1986                         best,
1987                         m_cull
1988                         );
1989         }
1990 }
1991 void TestLines( const VertexPointer& vertices, std::size_t count, SelectionIntersection& best ){
1992         if ( count == 0 ) {
1993                 return;
1994         }
1995         Vector4 clipped[9];
1996         for ( VertexPointer::iterator i = vertices.begin(), end = i + count; i != end; i += 2 )
1997         {
1998                 BestPoint(
1999                         matrix4_clip_line(
2000                                 m_local2view,
2001                                 reinterpret_cast<const Vector3&>( ( *i ) ),
2002                                 reinterpret_cast<const Vector3&>( ( *( i + 1 ) ) ),
2003                                 clipped
2004                                 ),
2005                         clipped,
2006                         best,
2007                         m_cull
2008                         );
2009         }
2010 }
2011 void TestTriangles( const VertexPointer& vertices, const IndexPointer& indices, SelectionIntersection& best ){
2012         Vector4 clipped[9];
2013         for ( IndexPointer::iterator i( indices.begin() ); i != indices.end(); i += 3 )
2014         {
2015                 BestPoint(
2016                         matrix4_clip_triangle(
2017                                 m_local2view,
2018                                 reinterpret_cast<const Vector3&>( vertices[*i] ),
2019                                 reinterpret_cast<const Vector3&>( vertices[*( i + 1 )] ),
2020                                 reinterpret_cast<const Vector3&>( vertices[*( i + 2 )] ),
2021                                 clipped
2022                                 ),
2023                         clipped,
2024                         best,
2025                         m_cull
2026                         );
2027         }
2028 }
2029 void TestQuads( const VertexPointer& vertices, const IndexPointer& indices, SelectionIntersection& best ){
2030         Vector4 clipped[9];
2031         for ( IndexPointer::iterator i( indices.begin() ); i != indices.end(); i += 4 )
2032         {
2033                 BestPoint(
2034                         matrix4_clip_triangle(
2035                                 m_local2view,
2036                                 reinterpret_cast<const Vector3&>( vertices[*i] ),
2037                                 reinterpret_cast<const Vector3&>( vertices[*( i + 1 )] ),
2038                                 reinterpret_cast<const Vector3&>( vertices[*( i + 3 )] ),
2039                                 clipped
2040                                 ),
2041                         clipped,
2042                         best,
2043                         m_cull
2044                         );
2045                 BestPoint(
2046                         matrix4_clip_triangle(
2047                                 m_local2view,
2048                                 reinterpret_cast<const Vector3&>( vertices[*( i + 1 )] ),
2049                                 reinterpret_cast<const Vector3&>( vertices[*( i + 2 )] ),
2050                                 reinterpret_cast<const Vector3&>( vertices[*( i + 3 )] ),
2051                                 clipped
2052                                 ),
2053                         clipped,
2054                         best,
2055                         m_cull
2056                         );
2057         }
2058 }
2059 void TestQuadStrip( const VertexPointer& vertices, const IndexPointer& indices, SelectionIntersection& best ){
2060         Vector4 clipped[9];
2061         for ( IndexPointer::iterator i( indices.begin() ); i + 2 != indices.end(); i += 2 )
2062         {
2063                 BestPoint(
2064                         matrix4_clip_triangle(
2065                                 m_local2view,
2066                                 reinterpret_cast<const Vector3&>( vertices[*i] ),
2067                                 reinterpret_cast<const Vector3&>( vertices[*( i + 1 )] ),
2068                                 reinterpret_cast<const Vector3&>( vertices[*( i + 2 )] ),
2069                                 clipped
2070                                 ),
2071                         clipped,
2072                         best,
2073                         m_cull
2074                         );
2075                 BestPoint(
2076                         matrix4_clip_triangle(
2077                                 m_local2view,
2078                                 reinterpret_cast<const Vector3&>( vertices[*( i + 2 )] ),
2079                                 reinterpret_cast<const Vector3&>( vertices[*( i + 1 )] ),
2080                                 reinterpret_cast<const Vector3&>( vertices[*( i + 3 )] ),
2081                                 clipped
2082                                 ),
2083                         clipped,
2084                         best,
2085                         m_cull
2086                         );
2087         }
2088 }
2089 };
2090
2091 class SelectionCounter
2092 {
2093 public:
2094 using func = void(const Selectable &);
2095
2096 SelectionCounter( const SelectionChangeCallback& onchanged )
2097         : m_count( 0 ), m_onchanged( onchanged ){
2098 }
2099 void operator()( const Selectable& selectable ){
2100         if ( selectable.isSelected() ) {
2101                 ++m_count;
2102         }
2103         else
2104         {
2105                 ASSERT_MESSAGE( m_count != 0, "selection counter underflow" );
2106                 --m_count;
2107         }
2108
2109         m_onchanged( selectable );
2110 }
2111 bool empty() const {
2112         return m_count == 0;
2113 }
2114 std::size_t size() const {
2115         return m_count;
2116 }
2117 private:
2118 std::size_t m_count;
2119 SelectionChangeCallback m_onchanged;
2120 };
2121
2122 inline void ConstructSelectionTest( View& view, const rect_t selection_box ){
2123         view.EnableScissor( selection_box.min[0], selection_box.max[0], selection_box.min[1], selection_box.max[1] );
2124 }
2125
2126 inline const rect_t SelectionBoxForPoint( const float device_point[2], const float device_epsilon[2] ){
2127         rect_t selection_box;
2128         selection_box.min[0] = device_point[0] - device_epsilon[0];
2129         selection_box.min[1] = device_point[1] - device_epsilon[1];
2130         selection_box.max[0] = device_point[0] + device_epsilon[0];
2131         selection_box.max[1] = device_point[1] + device_epsilon[1];
2132         return selection_box;
2133 }
2134
2135 inline const rect_t SelectionBoxForArea( const float device_point[2], const float device_delta[2] ){
2136         rect_t selection_box;
2137         selection_box.min[0] = ( device_delta[0] < 0 ) ? ( device_point[0] + device_delta[0] ) : ( device_point[0] );
2138         selection_box.min[1] = ( device_delta[1] < 0 ) ? ( device_point[1] + device_delta[1] ) : ( device_point[1] );
2139         selection_box.max[0] = ( device_delta[0] > 0 ) ? ( device_point[0] + device_delta[0] ) : ( device_point[0] );
2140         selection_box.max[1] = ( device_delta[1] > 0 ) ? ( device_point[1] + device_delta[1] ) : ( device_point[1] );
2141         return selection_box;
2142 }
2143
2144 Quaternion construct_local_rotation( const Quaternion& world, const Quaternion& localToWorld ){
2145         return quaternion_normalised( quaternion_multiplied_by_quaternion(
2146                                                                           quaternion_normalised( quaternion_multiplied_by_quaternion(
2147                                                                                                                                  quaternion_inverse( localToWorld ),
2148                                                                                                                                  world
2149                                                                                                                                  ) ),
2150                                                                           localToWorld
2151                                                                           ) );
2152 }
2153
2154 inline void matrix4_assign_rotation( Matrix4& matrix, const Matrix4& other ){
2155         matrix[0] = other[0];
2156         matrix[1] = other[1];
2157         matrix[2] = other[2];
2158         matrix[4] = other[4];
2159         matrix[5] = other[5];
2160         matrix[6] = other[6];
2161         matrix[8] = other[8];
2162         matrix[9] = other[9];
2163         matrix[10] = other[10];
2164 }
2165
2166 void matrix4_assign_rotation_for_pivot( Matrix4& matrix, scene::Instance& instance ){
2167         Editable* editable = Node_getEditable( instance.path().top() );
2168         if ( editable != 0 ) {
2169                 matrix4_assign_rotation( matrix, matrix4_multiplied_by_matrix4( instance.localToWorld(), editable->getLocalPivot() ) );
2170         }
2171         else
2172         {
2173                 matrix4_assign_rotation( matrix, instance.localToWorld() );
2174         }
2175 }
2176
2177 inline bool Instance_isSelectedComponents( scene::Instance& instance ){
2178         ComponentSelectionTestable* componentSelectionTestable = Instance_getComponentSelectionTestable( instance );
2179         return componentSelectionTestable != 0
2180                    && componentSelectionTestable->isSelectedComponents();
2181 }
2182
2183 class TranslateSelected : public SelectionSystem::Visitor
2184 {
2185 const Vector3& m_translate;
2186 public:
2187 TranslateSelected( const Vector3& translate )
2188         : m_translate( translate ){
2189 }
2190 void visit( scene::Instance& instance ) const {
2191         Transformable* transform = Instance_getTransformable( instance );
2192         if ( transform != 0 ) {
2193                 transform->setType( TRANSFORM_PRIMITIVE );
2194                 transform->setTranslation( m_translate );
2195         }
2196 }
2197 };
2198
2199 void Scene_Translate_Selected( scene::Graph& graph, const Vector3& translation ){
2200         if ( GlobalSelectionSystem().countSelected() != 0 ) {
2201                 GlobalSelectionSystem().foreachSelected( TranslateSelected( translation ) );
2202         }
2203 }
2204
2205 Vector3 get_local_pivot( const Vector3& world_pivot, const Matrix4& localToWorld ){
2206         return Vector3(
2207                            matrix4_transformed_point(
2208                                    matrix4_full_inverse( localToWorld ),
2209                                    world_pivot
2210                                    )
2211                            );
2212 }
2213
2214 void translation_for_pivoted_matrix_transform( Vector3& parent_translation, const Matrix4& local_transform, const Vector3& world_pivot, const Matrix4& localToWorld, const Matrix4& localToParent ){
2215         // we need a translation inside the parent system to move the origin of this object to the right place
2216
2217         // mathematically, it must fulfill:
2218         //
2219         //   local_translation local_transform local_pivot = local_pivot
2220         //   local_translation = local_pivot - local_transform local_pivot
2221         //
2222         //   or maybe?
2223         //   local_transform local_translation local_pivot = local_pivot
2224         //                   local_translation local_pivot = local_transform^-1 local_pivot
2225         //                 local_translation + local_pivot = local_transform^-1 local_pivot
2226         //                   local_translation             = local_transform^-1 local_pivot - local_pivot
2227
2228         Vector3 local_pivot( get_local_pivot( world_pivot, localToWorld ) );
2229
2230         Vector3 local_translation(
2231                 vector3_subtracted(
2232                         local_pivot,
2233                         matrix4_transformed_point(
2234                                 local_transform,
2235                                 local_pivot
2236                                 )
2237                 /*
2238                     matrix4_transformed_point(
2239                         matrix4_full_inverse(local_transform),
2240                         local_pivot
2241                     ),
2242                     local_pivot
2243                  */
2244                         )
2245                 );
2246
2247         translation_local2object( parent_translation, local_translation, localToParent );
2248
2249         /*
2250            // verify it!
2251            globalOutputStream() << "World pivot is at " << world_pivot << "\n";
2252            globalOutputStream() << "Local pivot is at " << local_pivot << "\n";
2253            globalOutputStream() << "Transformation " << local_transform << " moves it to: " << matrix4_transformed_point(local_transform, local_pivot) << "\n";
2254            globalOutputStream() << "Must move by " << local_translation << " in the local system" << "\n";
2255            globalOutputStream() << "Must move by " << parent_translation << " in the parent system" << "\n";
2256          */
2257 }
2258
2259 void translation_for_pivoted_rotation( Vector3& parent_translation, const Quaternion& local_rotation, const Vector3& world_pivot, const Matrix4& localToWorld, const Matrix4& localToParent ){
2260         translation_for_pivoted_matrix_transform( parent_translation, matrix4_rotation_for_quaternion_quantised( local_rotation ), world_pivot, localToWorld, localToParent );
2261 }
2262
2263 void translation_for_pivoted_scale( Vector3& parent_translation, const Vector3& world_scale, const Vector3& world_pivot, const Matrix4& localToWorld, const Matrix4& localToParent ){
2264         Matrix4 local_transform(
2265                 matrix4_multiplied_by_matrix4(
2266                         matrix4_full_inverse( localToWorld ),
2267                         matrix4_multiplied_by_matrix4(
2268                                 matrix4_scale_for_vec3( world_scale ),
2269                                 localToWorld
2270                                 )
2271                         )
2272                 );
2273         local_transform.tx() = local_transform.ty() = local_transform.tz() = 0; // cancel translation parts
2274         translation_for_pivoted_matrix_transform( parent_translation, local_transform, world_pivot, localToWorld, localToParent );
2275 }
2276
2277 class rotate_selected : public SelectionSystem::Visitor
2278 {
2279 const Quaternion& m_rotate;
2280 const Vector3& m_world_pivot;
2281 public:
2282 rotate_selected( const Quaternion& rotation, const Vector3& world_pivot )
2283         : m_rotate( rotation ), m_world_pivot( world_pivot ){
2284 }
2285 void visit( scene::Instance& instance ) const {
2286         TransformNode* transformNode = Node_getTransformNode( instance.path().top() );
2287         if ( transformNode != 0 ) {
2288                 Transformable* transform = Instance_getTransformable( instance );
2289                 if ( transform != 0 ) {
2290                         transform->setType( TRANSFORM_PRIMITIVE );
2291                         transform->setScale( c_scale_identity );
2292                         transform->setTranslation( c_translation_identity );
2293
2294                         transform->setType( TRANSFORM_PRIMITIVE );
2295                         transform->setRotation( m_rotate );
2296
2297                         {
2298                                 Editable* editable = Node_getEditable( instance.path().top() );
2299                                 const Matrix4& localPivot = editable != 0 ? editable->getLocalPivot() : g_matrix4_identity;
2300
2301                                 Vector3 parent_translation;
2302                                 translation_for_pivoted_rotation(
2303                                         parent_translation,
2304                                         m_rotate,
2305                                         m_world_pivot,
2306                                         matrix4_multiplied_by_matrix4( instance.localToWorld(), localPivot ),
2307                                         matrix4_multiplied_by_matrix4( transformNode->localToParent(), localPivot )
2308                                         );
2309
2310                                 transform->setTranslation( parent_translation );
2311                         }
2312                 }
2313         }
2314 }
2315 };
2316
2317 void Scene_Rotate_Selected( scene::Graph& graph, const Quaternion& rotation, const Vector3& world_pivot ){
2318         if ( GlobalSelectionSystem().countSelected() != 0 ) {
2319                 GlobalSelectionSystem().foreachSelected( rotate_selected( rotation, world_pivot ) );
2320         }
2321 }
2322
2323 class scale_selected : public SelectionSystem::Visitor
2324 {
2325 const Vector3& m_scale;
2326 const Vector3& m_world_pivot;
2327 public:
2328 scale_selected( const Vector3& scaling, const Vector3& world_pivot )
2329         : m_scale( scaling ), m_world_pivot( world_pivot ){
2330 }
2331 void visit( scene::Instance& instance ) const {
2332         TransformNode* transformNode = Node_getTransformNode( instance.path().top() );
2333         if ( transformNode != 0 ) {
2334                 Transformable* transform = Instance_getTransformable( instance );
2335                 if ( transform != 0 ) {
2336                         transform->setType( TRANSFORM_PRIMITIVE );
2337                         transform->setScale( c_scale_identity );
2338                         transform->setTranslation( c_translation_identity );
2339
2340                         transform->setType( TRANSFORM_PRIMITIVE );
2341                         transform->setScale( m_scale );
2342                         {
2343                                 Editable* editable = Node_getEditable( instance.path().top() );
2344                                 const Matrix4& localPivot = editable != 0 ? editable->getLocalPivot() : g_matrix4_identity;
2345
2346                                 Vector3 parent_translation;
2347                                 translation_for_pivoted_scale(
2348                                         parent_translation,
2349                                         m_scale,
2350                                         m_world_pivot,
2351                                         matrix4_multiplied_by_matrix4( instance.localToWorld(), localPivot ),
2352                                         matrix4_multiplied_by_matrix4( transformNode->localToParent(), localPivot )
2353                                         );
2354
2355                                 transform->setTranslation( parent_translation );
2356                         }
2357                 }
2358         }
2359 }
2360 };
2361
2362 void Scene_Scale_Selected( scene::Graph& graph, const Vector3& scaling, const Vector3& world_pivot ){
2363         if ( GlobalSelectionSystem().countSelected() != 0 ) {
2364                 GlobalSelectionSystem().foreachSelected( scale_selected( scaling, world_pivot ) );
2365         }
2366 }
2367
2368
2369 class translate_component_selected : public SelectionSystem::Visitor
2370 {
2371 const Vector3& m_translate;
2372 public:
2373 translate_component_selected( const Vector3& translate )
2374         : m_translate( translate ){
2375 }
2376 void visit( scene::Instance& instance ) const {
2377         Transformable* transform = Instance_getTransformable( instance );
2378         if ( transform != 0 ) {
2379                 transform->setType( TRANSFORM_COMPONENT );
2380                 transform->setTranslation( m_translate );
2381         }
2382 }
2383 };
2384
2385 void Scene_Translate_Component_Selected( scene::Graph& graph, const Vector3& translation ){
2386         if ( GlobalSelectionSystem().countSelected() != 0 ) {
2387                 GlobalSelectionSystem().foreachSelectedComponent( translate_component_selected( translation ) );
2388         }
2389 }
2390
2391 class rotate_component_selected : public SelectionSystem::Visitor
2392 {
2393 const Quaternion& m_rotate;
2394 const Vector3& m_world_pivot;
2395 public:
2396 rotate_component_selected( const Quaternion& rotation, const Vector3& world_pivot )
2397         : m_rotate( rotation ), m_world_pivot( world_pivot ){
2398 }
2399 void visit( scene::Instance& instance ) const {
2400         Transformable* transform = Instance_getTransformable( instance );
2401         if ( transform != 0 ) {
2402                 Vector3 parent_translation;
2403                 translation_for_pivoted_rotation( parent_translation, m_rotate, m_world_pivot, instance.localToWorld(), Node_getTransformNode( instance.path().top() )->localToParent() );
2404
2405                 transform->setType( TRANSFORM_COMPONENT );
2406                 transform->setRotation( m_rotate );
2407                 transform->setTranslation( parent_translation );
2408         }
2409 }
2410 };
2411
2412 void Scene_Rotate_Component_Selected( scene::Graph& graph, const Quaternion& rotation, const Vector3& world_pivot ){
2413         if ( GlobalSelectionSystem().countSelectedComponents() != 0 ) {
2414                 GlobalSelectionSystem().foreachSelectedComponent( rotate_component_selected( rotation, world_pivot ) );
2415         }
2416 }
2417
2418 class scale_component_selected : public SelectionSystem::Visitor
2419 {
2420 const Vector3& m_scale;
2421 const Vector3& m_world_pivot;
2422 public:
2423 scale_component_selected( const Vector3& scaling, const Vector3& world_pivot )
2424         : m_scale( scaling ), m_world_pivot( world_pivot ){
2425 }
2426 void visit( scene::Instance& instance ) const {
2427         Transformable* transform = Instance_getTransformable( instance );
2428         if ( transform != 0 ) {
2429                 Vector3 parent_translation;
2430                 translation_for_pivoted_scale( parent_translation, m_scale, m_world_pivot, instance.localToWorld(), Node_getTransformNode( instance.path().top() )->localToParent() );
2431
2432                 transform->setType( TRANSFORM_COMPONENT );
2433                 transform->setScale( m_scale );
2434                 transform->setTranslation( parent_translation );
2435         }
2436 }
2437 };
2438
2439 void Scene_Scale_Component_Selected( scene::Graph& graph, const Vector3& scaling, const Vector3& world_pivot ){
2440         if ( GlobalSelectionSystem().countSelectedComponents() != 0 ) {
2441                 GlobalSelectionSystem().foreachSelectedComponent( scale_component_selected( scaling, world_pivot ) );
2442         }
2443 }
2444
2445
2446 class BooleanSelector : public Selector
2447 {
2448 bool m_selected;
2449 SelectionIntersection m_intersection;
2450 Selectable* m_selectable;
2451 public:
2452 BooleanSelector() : m_selected( false ){
2453 }
2454
2455 void pushSelectable( Selectable& selectable ){
2456         m_intersection = SelectionIntersection();
2457         m_selectable = &selectable;
2458 }
2459 void popSelectable(){
2460         if ( m_intersection.valid() ) {
2461                 m_selected = true;
2462         }
2463         m_intersection = SelectionIntersection();
2464 }
2465 void addIntersection( const SelectionIntersection& intersection ){
2466         if ( m_selectable->isSelected() ) {
2467                 assign_if_closer( m_intersection, intersection );
2468         }
2469 }
2470
2471 bool isSelected(){
2472         return m_selected;
2473 }
2474 };
2475
2476 class BestSelector : public Selector
2477 {
2478 SelectionIntersection m_intersection;
2479 Selectable* m_selectable;
2480 SelectionIntersection m_bestIntersection;
2481 std::list<Selectable*> m_bestSelectable;
2482 public:
2483 BestSelector() : m_bestIntersection( SelectionIntersection() ), m_bestSelectable( 0 ){
2484 }
2485
2486 void pushSelectable( Selectable& selectable ){
2487         m_intersection = SelectionIntersection();
2488         m_selectable = &selectable;
2489 }
2490 void popSelectable(){
2491         if ( m_intersection.equalEpsilon( m_bestIntersection, 0.25f, 0.001f ) ) {
2492                 m_bestSelectable.push_back( m_selectable );
2493                 m_bestIntersection = m_intersection;
2494         }
2495         else if ( m_intersection < m_bestIntersection ) {
2496                 m_bestSelectable.clear();
2497                 m_bestSelectable.push_back( m_selectable );
2498                 m_bestIntersection = m_intersection;
2499         }
2500         m_intersection = SelectionIntersection();
2501 }
2502 void addIntersection( const SelectionIntersection& intersection ){
2503         assign_if_closer( m_intersection, intersection );
2504 }
2505
2506 std::list<Selectable*>& best(){
2507         return m_bestSelectable;
2508 }
2509 };
2510
2511 class DragManipulator : public Manipulator
2512 {
2513 TranslateFree m_freeResize;
2514 TranslateFree m_freeDrag;
2515 ResizeTranslatable m_resize;
2516 DragTranslatable m_drag;
2517 SelectableBool m_dragSelectable;
2518 public:
2519
2520 bool m_selected;
2521
2522 DragManipulator() : m_freeResize( m_resize ), m_freeDrag( m_drag ), m_selected( false ){
2523 }
2524
2525 Manipulatable* GetManipulatable(){
2526         return m_dragSelectable.isSelected() ? &m_freeDrag : &m_freeResize;
2527 }
2528
2529 void testSelect( const View& view, const Matrix4& pivot2world ){
2530         SelectionPool selector;
2531
2532         SelectionVolume test( view );
2533
2534         if ( GlobalSelectionSystem().Mode() == SelectionSystem::ePrimitive ) {
2535                 BooleanSelector booleanSelector;
2536
2537                 Scene_TestSelect_Primitive( booleanSelector, test, view );
2538
2539                 if ( booleanSelector.isSelected() ) {
2540                         selector.addSelectable( SelectionIntersection( 0, 0 ), &m_dragSelectable );
2541                         m_selected = false;
2542                 }
2543                 else
2544                 {
2545                         m_selected = Scene_forEachPlaneSelectable_selectPlanes( GlobalSceneGraph(), selector, test );
2546                 }
2547         }
2548         else
2549         {
2550                 BestSelector bestSelector;
2551                 Scene_TestSelect_Component_Selected( bestSelector, test, view, GlobalSelectionSystem().ComponentMode() );
2552                 for ( std::list<Selectable*>::iterator i = bestSelector.best().begin(); i != bestSelector.best().end(); ++i )
2553                 {
2554                         if ( !( *i )->isSelected() ) {
2555                                 GlobalSelectionSystem().setSelectedAllComponents( false );
2556                         }
2557                         m_selected = false;
2558                         selector.addSelectable( SelectionIntersection( 0, 0 ), ( *i ) );
2559                         m_dragSelectable.setSelected( true );
2560                 }
2561         }
2562
2563         for ( SelectionPool::iterator i = selector.begin(); i != selector.end(); ++i )
2564         {
2565                 ( *i ).second->setSelected( true );
2566         }
2567 }
2568
2569 void setSelected( bool select ){
2570         m_selected = select;
2571         m_dragSelectable.setSelected( select );
2572 }
2573 bool isSelected() const {
2574         return m_selected || m_dragSelectable.isSelected();
2575 }
2576 };
2577
2578 class ClipManipulator : public Manipulator
2579 {
2580 public:
2581
2582 Manipulatable* GetManipulatable(){
2583         ERROR_MESSAGE( "clipper is not manipulatable" );
2584         return 0;
2585 }
2586
2587 void setSelected( bool select ){
2588 }
2589 bool isSelected() const {
2590         return false;
2591 }
2592 };
2593
2594 class select_all : public scene::Graph::Walker
2595 {
2596 bool m_select;
2597 public:
2598 select_all( bool select )
2599         : m_select( select ){
2600 }
2601 bool pre( const scene::Path& path, scene::Instance& instance ) const {
2602         Selectable* selectable = Instance_getSelectable( instance );
2603         if ( selectable != 0 ) {
2604                 selectable->setSelected( m_select );
2605         }
2606         return true;
2607 }
2608 };
2609
2610 class select_all_component : public scene::Graph::Walker
2611 {
2612 bool m_select;
2613 SelectionSystem::EComponentMode m_mode;
2614 public:
2615 select_all_component( bool select, SelectionSystem::EComponentMode mode )
2616         : m_select( select ), m_mode( mode ){
2617 }
2618 bool pre( const scene::Path& path, scene::Instance& instance ) const {
2619         ComponentSelectionTestable* componentSelectionTestable = Instance_getComponentSelectionTestable( instance );
2620         if ( componentSelectionTestable ) {
2621                 componentSelectionTestable->setSelectedComponents( m_select, m_mode );
2622         }
2623         return true;
2624 }
2625 };
2626
2627 void Scene_SelectAll_Component( bool select, SelectionSystem::EComponentMode componentMode ){
2628         GlobalSceneGraph().traverse( select_all_component( select, componentMode ) );
2629 }
2630
2631
2632 // RadiantSelectionSystem
2633 class RadiantSelectionSystem :
2634         public SelectionSystem,
2635         public Translatable,
2636         public Rotatable,
2637         public Scalable,
2638         public Renderable
2639 {
2640 mutable Matrix4 m_pivot2world;
2641 Matrix4 m_pivot2world_start;
2642 Matrix4 m_manip2pivot_start;
2643 Translation m_translation;
2644 Rotation m_rotation;
2645 Scale m_scale;
2646 public:
2647 static Shader* m_state;
2648 bool m_bPreferPointEntsIn2D;
2649 private:
2650 EManipulatorMode m_manipulator_mode;
2651 Manipulator* m_manipulator;
2652
2653 // state
2654 bool m_undo_begun;
2655 EMode m_mode;
2656 EComponentMode m_componentmode;
2657
2658 SelectionCounter m_count_primitive;
2659 SelectionCounter m_count_component;
2660
2661 TranslateManipulator m_translate_manipulator;
2662 RotateManipulator m_rotate_manipulator;
2663 ScaleManipulator m_scale_manipulator;
2664 DragManipulator m_drag_manipulator;
2665 ClipManipulator m_clip_manipulator;
2666
2667 typedef SelectionList<scene::Instance> selection_t;
2668 selection_t m_selection;
2669 selection_t m_component_selection;
2670
2671 Signal1<const Selectable&> m_selectionChanged_callbacks;
2672
2673 void ConstructPivot() const;
2674 void setCustomPivotOrigin( Vector3& point ) const;
2675 public:
2676 void getSelectionAABB( AABB& bounds ) const;
2677 private:
2678 mutable bool m_pivotChanged;
2679 bool m_pivot_moving;
2680 mutable bool m_pivotIsCustom;
2681
2682 void Scene_TestSelect( Selector& selector, SelectionTest& test, const View& view, SelectionSystem::EMode mode, SelectionSystem::EComponentMode componentMode );
2683
2684 bool nothingSelected() const {
2685         return ( Mode() == eComponent && m_count_component.empty() )
2686                    || ( Mode() == ePrimitive && m_count_primitive.empty() );
2687 }
2688
2689
2690 public:
2691 enum EModifier
2692 {
2693         eManipulator,
2694         eToggle,
2695         eReplace,
2696         eCycle,
2697         eSelect,
2698         eDeselect,
2699 };
2700
2701 RadiantSelectionSystem() :
2702         m_bPreferPointEntsIn2D( true ),
2703         m_undo_begun( false ),
2704         m_mode( ePrimitive ),
2705         m_componentmode( eDefault ),
2706         m_count_primitive( SelectionChangedCaller( *this ) ),
2707         m_count_component( SelectionChangedCaller( *this ) ),
2708         m_translate_manipulator( *this, 2, 64 ),
2709         m_rotate_manipulator( *this, 8, 64 ),
2710         m_scale_manipulator( *this, 0, 64 ),
2711         m_pivotChanged( false ),
2712         m_pivot_moving( false ),
2713         m_pivotIsCustom( false ){
2714         SetManipulatorMode( eTranslate );
2715         pivotChanged();
2716         addSelectionChangeCallback( PivotChangedSelectionCaller( *this ) );
2717         AddGridChangeCallback( PivotChangedCaller( *this ) );
2718 }
2719 void pivotChanged() const {
2720         m_pivotChanged = true;
2721         SceneChangeNotify();
2722 }
2723 typedef ConstMemberCaller<RadiantSelectionSystem, void(), &RadiantSelectionSystem::pivotChanged> PivotChangedCaller;
2724 void pivotChangedSelection( const Selectable& selectable ){
2725         pivotChanged();
2726 }
2727 typedef MemberCaller<RadiantSelectionSystem, void(const Selectable&), &RadiantSelectionSystem::pivotChangedSelection> PivotChangedSelectionCaller;
2728
2729 void SetMode( EMode mode ){
2730         if ( m_mode != mode ) {
2731                 m_mode = mode;
2732                 pivotChanged();
2733         }
2734 }
2735 EMode Mode() const {
2736         return m_mode;
2737 }
2738 void SetComponentMode( EComponentMode mode ){
2739         m_componentmode = mode;
2740 }
2741 EComponentMode ComponentMode() const {
2742         return m_componentmode;
2743 }
2744 void SetManipulatorMode( EManipulatorMode mode ){
2745         m_pivotIsCustom = false;
2746         m_manipulator_mode = mode;
2747         switch ( m_manipulator_mode )
2748         {
2749         case eTranslate: m_manipulator = &m_translate_manipulator; break;
2750         case eRotate: m_manipulator = &m_rotate_manipulator; break;
2751         case eScale: m_manipulator = &m_scale_manipulator; break;
2752         case eDrag: m_manipulator = &m_drag_manipulator; break;
2753         case eClip: m_manipulator = &m_clip_manipulator; break;
2754         }
2755         pivotChanged();
2756 }
2757 EManipulatorMode ManipulatorMode() const {
2758         return m_manipulator_mode;
2759 }
2760
2761 SelectionChangeCallback getObserver( EMode mode ){
2762         if ( mode == ePrimitive ) {
2763                 return makeCallback( m_count_primitive );
2764         }
2765         else
2766         {
2767                 return makeCallback( m_count_component );
2768         }
2769 }
2770 std::size_t countSelected() const {
2771         return m_count_primitive.size();
2772 }
2773 std::size_t countSelectedComponents() const {
2774         return m_count_component.size();
2775 }
2776 void onSelectedChanged( scene::Instance& instance, const Selectable& selectable ){
2777         if ( selectable.isSelected() ) {
2778                 m_selection.append( instance );
2779         }
2780         else
2781         {
2782                 m_selection.erase( instance );
2783         }
2784
2785         ASSERT_MESSAGE( m_selection.size() == m_count_primitive.size(), "selection-tracking error" );
2786 }
2787 void onComponentSelection( scene::Instance& instance, const Selectable& selectable ){
2788         if ( selectable.isSelected() ) {
2789                 m_component_selection.append( instance );
2790         }
2791         else
2792         {
2793                 m_component_selection.erase( instance );
2794         }
2795
2796         ASSERT_MESSAGE( m_component_selection.size() == m_count_component.size(), "selection-tracking error" );
2797 }
2798 scene::Instance& ultimateSelected() const {
2799         ASSERT_MESSAGE( m_selection.size() > 0, "no instance selected" );
2800         return m_selection.back();
2801 }
2802 scene::Instance& penultimateSelected() const {
2803         ASSERT_MESSAGE( m_selection.size() > 1, "only one instance selected" );
2804         return *( *( --( --m_selection.end() ) ) );
2805 }
2806 void setSelectedAll( bool selected ){
2807         GlobalSceneGraph().traverse( select_all( selected ) );
2808
2809         m_manipulator->setSelected( selected );
2810 }
2811 void setSelectedAllComponents( bool selected ){
2812         Scene_SelectAll_Component( selected, SelectionSystem::eVertex );
2813         Scene_SelectAll_Component( selected, SelectionSystem::eEdge );
2814         Scene_SelectAll_Component( selected, SelectionSystem::eFace );
2815
2816         m_manipulator->setSelected( selected );
2817 }
2818
2819 void foreachSelected( const Visitor& visitor ) const {
2820         selection_t::const_iterator i = m_selection.begin();
2821         while ( i != m_selection.end() )
2822         {
2823                 visitor.visit( *( *( i++ ) ) );
2824         }
2825 }
2826 void foreachSelectedComponent( const Visitor& visitor ) const {
2827         selection_t::const_iterator i = m_component_selection.begin();
2828         while ( i != m_component_selection.end() )
2829         {
2830                 visitor.visit( *( *( i++ ) ) );
2831         }
2832 }
2833
2834 void addSelectionChangeCallback( const SelectionChangeHandler& handler ){
2835         m_selectionChanged_callbacks.connectLast( handler );
2836 }
2837 void selectionChanged( const Selectable& selectable ){
2838         m_selectionChanged_callbacks( selectable );
2839 }
2840 typedef MemberCaller<RadiantSelectionSystem, void(const Selectable&), &RadiantSelectionSystem::selectionChanged> SelectionChangedCaller;
2841
2842
2843 void startMove(){
2844         m_pivot2world_start = GetPivot2World();
2845 }
2846
2847 bool SelectManipulator( const View& view, const float device_point[2], const float device_epsilon[2] ){
2848         if ( !nothingSelected() || ( ManipulatorMode() == eDrag && Mode() == eComponent ) ) {
2849 #if defined ( DEBUG_SELECTION )
2850                 g_render_clipped.destroy();
2851 #endif
2852
2853                 m_manipulator->setSelected( false );
2854
2855                 if ( !nothingSelected() || ( ManipulatorMode() == eDrag && Mode() == eComponent ) ) {
2856                         View scissored( view );
2857                         ConstructSelectionTest( scissored, SelectionBoxForPoint( device_point, device_epsilon ) );
2858                         m_manipulator->testSelect( scissored, GetPivot2World() );
2859                 }
2860
2861                 startMove();
2862
2863                 m_pivot_moving = m_manipulator->isSelected();
2864
2865                 if ( m_pivot_moving ) {
2866                         Pivot2World pivot;
2867                         pivot.update( GetPivot2World(), view.GetModelview(), view.GetProjection(), view.GetViewport() );
2868
2869                         m_manip2pivot_start = matrix4_multiplied_by_matrix4( matrix4_full_inverse( m_pivot2world_start ), pivot.m_worldSpace );
2870
2871                         Matrix4 device2manip;
2872                         ConstructDevice2Manip( device2manip, m_pivot2world_start, view.GetModelview(), view.GetProjection(), view.GetViewport() );
2873                         m_manipulator->GetManipulatable()->Construct( device2manip, device_point[0], device_point[1] );
2874
2875                         m_undo_begun = false;
2876                 }
2877
2878                 SceneChangeNotify();
2879         }
2880
2881         return m_pivot_moving;
2882 }
2883
2884 void deselectAll(){
2885         if ( Mode() == eComponent ) {
2886                 setSelectedAllComponents( false );
2887         }
2888         else
2889         {
2890                 setSelectedAll( false );
2891         }
2892 }
2893
2894 void deselectComponentsOrAll( bool components ){
2895         if ( components ) {
2896                 setSelectedAllComponents( false );
2897         }
2898         else
2899         {
2900                 deselectAll();
2901         }
2902 }
2903
2904 void SelectPoint( const View& view, const float device_point[2], const float device_epsilon[2], RadiantSelectionSystem::EModifier modifier, bool face ){
2905         //globalOutputStream() << device_point[0] << "   " << device_point[1] << "\n";
2906         ASSERT_MESSAGE( fabs( device_point[0] ) <= 1.0f && fabs( device_point[1] ) <= 1.0f, "point-selection error" );
2907
2908         if ( modifier == eReplace ) {
2909                 deselectComponentsOrAll( face );
2910         }
2911 /*
2912 //nothingSelected() doesn't consider faces, selected in non-component mode, m
2913         if ( modifier == eCycle && nothingSelected() ){
2914                 modifier = eReplace;
2915         }
2916 */
2917   #if defined ( DEBUG_SELECTION )
2918         g_render_clipped.destroy();
2919   #endif
2920
2921         {
2922                 View scissored( view );
2923                 ConstructSelectionTest( scissored, SelectionBoxForPoint( device_point, device_epsilon ) );
2924
2925                 SelectionVolume volume( scissored );
2926                 SelectionPool selector;
2927                 SelectionPool selector_point_ents;
2928                 const bool prefer_point_ents = m_bPreferPointEntsIn2D && Mode() == ePrimitive && !view.fill() && !face
2929                         && ( modifier == RadiantSelectionSystem::eReplace || modifier == RadiantSelectionSystem::eSelect || modifier == RadiantSelectionSystem::eDeselect );
2930
2931                 if( prefer_point_ents ){
2932                         Scene_TestSelect( selector_point_ents, volume, scissored, eEntity, ComponentMode() );
2933                 }
2934                 if( prefer_point_ents && !selector_point_ents.failed() ){
2935                         switch ( modifier )
2936                         {
2937                         // if cycle mode not enabled, enable it
2938                         case RadiantSelectionSystem::eReplace:
2939                         {
2940                                 // select closest
2941                                 ( *selector_point_ents.begin() ).second->setSelected( true );
2942                         }
2943                         break;
2944                         case RadiantSelectionSystem::eSelect:
2945                         {
2946                                 SelectionPool::iterator best = selector_point_ents.begin();
2947                                 if( !( *best ).second->isSelected() ){
2948                                         ( *best ).second->setSelected( true );
2949                                 }
2950                                 SelectionPool::iterator i = best;
2951                                 ++i;
2952                                 while ( i != selector_point_ents.end() )
2953                                 {
2954                                         if( ( *i ).first.equalEpsilon( ( *best ).first, 0.25f, 0.000001f ) ){
2955                                                 if( !( *i ).second->isSelected() ){
2956                                                         ( *i ).second->setSelected( true );
2957                                                 }
2958                                         }
2959                                         else{
2960                                                 break;
2961                                         }
2962                                         ++i;
2963                                 }
2964                         }
2965                         break;
2966                         case RadiantSelectionSystem::eDeselect:
2967                         {
2968                                 SelectionPool::iterator best = selector_point_ents.begin();
2969                                 if( ( *best ).second->isSelected() ){
2970                                         ( *best ).second->setSelected( false );
2971                                 }
2972                                 SelectionPool::iterator i = best;
2973                                 ++i;
2974                                 while ( i != selector_point_ents.end() )
2975                                 {
2976                                         if( ( *i ).first.equalEpsilon( ( *best ).first, 0.25f, 0.000001f ) ){
2977                                                 if( ( *i ).second->isSelected() ){
2978                                                         ( *i ).second->setSelected( false );
2979                                                 }
2980                                         }
2981                                         else{
2982                                                 break;
2983                                         }
2984                                         ++i;
2985                                 }
2986                         }
2987                         break;
2988                         default:
2989                                 break;
2990                         }
2991                 }
2992                 else{
2993                         if ( face ){
2994                                 Scene_TestSelect_Component( selector, volume, scissored, eFace );
2995                         }
2996                         else{
2997                                 Scene_TestSelect( selector, volume, scissored, Mode(), ComponentMode() );
2998                         }
2999
3000                         if ( !selector.failed() ) {
3001                                 switch ( modifier )
3002                                 {
3003                                 case RadiantSelectionSystem::eToggle:
3004                                 {
3005                                         SelectableSortedSet::iterator best = selector.begin();
3006                                         // toggle selection of the object with least depth
3007                                         if ( ( *best ).second->isSelected() ) {
3008                                                 ( *best ).second->setSelected( false );
3009                                         }
3010                                         else{
3011                                                 ( *best ).second->setSelected( true );
3012                                         }
3013                                 }
3014                                 break;
3015                                 // if cycle mode not enabled, enable it
3016                                 case RadiantSelectionSystem::eReplace:
3017                                 {
3018                                         // select closest
3019                                         ( *selector.begin() ).second->setSelected( true );
3020                                 }
3021                                 break;
3022                                 // select the next object in the list from the one already selected
3023                                 case RadiantSelectionSystem::eCycle:
3024                                 {
3025                                         bool CycleSelectionOccured = false;
3026                                         SelectionPool::iterator i = selector.begin();
3027                                         while ( i != selector.end() )
3028                                         {
3029                                                 if ( ( *i ).second->isSelected() ) {
3030                                                         deselectComponentsOrAll( face );
3031                                                         ++i;
3032                                                         if ( i != selector.end() ) {
3033                                                                 i->second->setSelected( true );
3034                                                         }
3035                                                         else
3036                                                         {
3037                                                                 selector.begin()->second->setSelected( true );
3038                                                         }
3039                                                         CycleSelectionOccured = true;
3040                                                         break;
3041                                                 }
3042                                                 ++i;
3043                                         }
3044                                         if( !CycleSelectionOccured ){
3045                                                 deselectComponentsOrAll( face );
3046                                                 ( *selector.begin() ).second->setSelected( true );
3047                                         }
3048                                 }
3049                                 break;
3050                                 case RadiantSelectionSystem::eSelect:
3051                                 {
3052                                         SelectionPool::iterator best = selector.begin();
3053                                         if( !( *best ).second->isSelected() ){
3054                                                 ( *best ).second->setSelected( true );
3055                                         }
3056                                         SelectionPool::iterator i = best;
3057                                         ++i;
3058                                         while ( i != selector.end() )
3059                                         {
3060                                                 if( ( *i ).first.equalEpsilon( ( *best ).first, 0.25f, 0.000001f ) ){
3061                                                         if( !( *i ).second->isSelected() ){
3062                                                                 ( *i ).second->setSelected( true );
3063                                                         }
3064                                                 }
3065                                                 else{
3066                                                         break;
3067                                                 }
3068                                                 ++i;
3069                                         }
3070                                 }
3071                                 break;
3072                                 case RadiantSelectionSystem::eDeselect:
3073                                 {
3074                                         SelectionPool::iterator best = selector.begin();
3075                                         if( ( *best ).second->isSelected() ){
3076                                                 ( *best ).second->setSelected( false );
3077                                         }
3078                                         SelectionPool::iterator i = best;
3079                                         ++i;
3080                                         while ( i != selector.end() )
3081                                         {
3082                                                 if( ( *i ).first.equalEpsilon( ( *best ).first, 0.25f, 0.000001f ) ){
3083                                                         if( ( *i ).second->isSelected() ){
3084                                                                 ( *i ).second->setSelected( false );
3085                                                         }
3086                                                 }
3087                                                 else{
3088                                                         break;
3089                                                 }
3090                                                 ++i;
3091                                         }
3092                                 }
3093                                 break;
3094                                 default:
3095                                         break;
3096                                 }
3097                         }
3098                         else if( modifier == eCycle ){
3099                                 deselectComponentsOrAll( face );
3100                         }
3101                 }
3102         }
3103 }
3104
3105 bool SelectPoint_InitPaint( const View& view, const float device_point[2], const float device_epsilon[2], bool face ){
3106         ASSERT_MESSAGE( fabs( device_point[0] ) <= 1.0f && fabs( device_point[1] ) <= 1.0f, "point-selection error" );
3107   #if defined ( DEBUG_SELECTION )
3108         g_render_clipped.destroy();
3109   #endif
3110
3111         {
3112                 View scissored( view );
3113                 ConstructSelectionTest( scissored, SelectionBoxForPoint( device_point, device_epsilon ) );
3114
3115                 SelectionVolume volume( scissored );
3116                 SelectionPool selector;
3117                 SelectionPool selector_point_ents;
3118                 const bool prefer_point_ents = m_bPreferPointEntsIn2D && Mode() == ePrimitive && !view.fill() && !face;
3119
3120                 if( prefer_point_ents ){
3121                         Scene_TestSelect( selector_point_ents, volume, scissored, eEntity, ComponentMode() );
3122                 }
3123                 if( prefer_point_ents && !selector_point_ents.failed() ){
3124                         SelectableSortedSet::iterator best = selector_point_ents.begin();
3125                         const bool wasSelected = ( *best ).second->isSelected();
3126                         ( *best ).second->setSelected( !wasSelected );
3127                         SelectableSortedSet::iterator i = best;
3128                         ++i;
3129                         while ( i != selector_point_ents.end() )
3130                         {
3131                                 if( ( *i ).first.equalEpsilon( ( *best ).first, 0.25f, 0.000001f ) ){
3132                                         ( *i ).second->setSelected( !wasSelected );
3133                                 }
3134                                 else{
3135                                         break;
3136                                 }
3137                                 ++i;
3138                         }
3139                         return !wasSelected;
3140                 }
3141                 else{//do primitives, if ents failed
3142                         if ( face ){
3143                                 Scene_TestSelect_Component( selector, volume, scissored, eFace );
3144                         }
3145                         else{
3146                                 Scene_TestSelect( selector, volume, scissored, Mode(), ComponentMode() );
3147                         }
3148                         if ( !selector.failed() ){
3149                                 SelectableSortedSet::iterator best = selector.begin();
3150                                 const bool wasSelected = ( *best ).second->isSelected();
3151                                 ( *best ).second->setSelected( !wasSelected );
3152                                 SelectableSortedSet::iterator i = best;
3153                                 ++i;
3154                                 while ( i != selector.end() )
3155                                 {
3156                                         if( ( *i ).first.equalEpsilon( ( *best ).first, 0.25f, 0.000001f ) ){
3157                                                 ( *i ).second->setSelected( !wasSelected );
3158                                         }
3159                                         else{
3160                                                 break;
3161                                         }
3162                                         ++i;
3163                                 }
3164                                 return !wasSelected;
3165                         }
3166                         else{
3167                                 return true;
3168                         }
3169                 }
3170         }
3171 }
3172
3173 void SelectArea( const View& view, const float device_point[2], const float device_delta[2], RadiantSelectionSystem::EModifier modifier, bool face ){
3174         if ( modifier == eReplace ) {
3175                 deselectComponentsOrAll( face );
3176         }
3177
3178   #if defined ( DEBUG_SELECTION )
3179         g_render_clipped.destroy();
3180   #endif
3181
3182         {
3183                 View scissored( view );
3184                 ConstructSelectionTest( scissored, SelectionBoxForArea( device_point, device_delta ) );
3185
3186                 SelectionVolume volume( scissored );
3187                 SelectionPool pool;
3188                 if ( face ) {
3189                         Scene_TestSelect_Component( pool, volume, scissored, eFace );
3190                 }
3191                 else
3192                 {
3193                         Scene_TestSelect( pool, volume, scissored, Mode(), ComponentMode() );
3194                 }
3195
3196                 for ( SelectionPool::iterator i = pool.begin(); i != pool.end(); ++i )
3197                 {
3198                         ( *i ).second->setSelected( !( modifier == RadiantSelectionSystem::eToggle && ( *i ).second->isSelected() ) );
3199                 }
3200         }
3201 }
3202
3203
3204 void translate( const Vector3& translation ){
3205         if ( !nothingSelected() ) {
3206                 //ASSERT_MESSAGE(!m_pivotChanged, "pivot is invalid");
3207
3208                 m_translation = translation;
3209
3210                 m_pivot2world = m_pivot2world_start;
3211                 matrix4_translate_by_vec3( m_pivot2world, translation );
3212
3213                 if ( Mode() == eComponent ) {
3214                         Scene_Translate_Component_Selected( GlobalSceneGraph(), m_translation );
3215                 }
3216                 else
3217                 {
3218                         Scene_Translate_Selected( GlobalSceneGraph(), m_translation );
3219                 }
3220
3221                 SceneChangeNotify();
3222         }
3223 }
3224 void outputTranslation( TextOutputStream& ostream ){
3225         ostream << " -xyz " << m_translation.x() << " " << m_translation.y() << " " << m_translation.z();
3226 }
3227 void rotate( const Quaternion& rotation ){
3228         if ( !nothingSelected() ) {
3229                 //ASSERT_MESSAGE(!m_pivotChanged, "pivot is invalid");
3230
3231                 m_rotation = rotation;
3232
3233                 if ( Mode() == eComponent ) {
3234                         Scene_Rotate_Component_Selected( GlobalSceneGraph(), m_rotation, vector4_to_vector3( m_pivot2world.t() ) );
3235
3236                         matrix4_assign_rotation_for_pivot( m_pivot2world, m_component_selection.back() );
3237                 }
3238                 else
3239                 {
3240                         Scene_Rotate_Selected( GlobalSceneGraph(), m_rotation, vector4_to_vector3( m_pivot2world.t() ) );
3241
3242                         matrix4_assign_rotation_for_pivot( m_pivot2world, m_selection.back() );
3243                 }
3244
3245                 SceneChangeNotify();
3246         }
3247 }
3248 void outputRotation( TextOutputStream& ostream ){
3249         ostream << " -eulerXYZ " << m_rotation.x() << " " << m_rotation.y() << " " << m_rotation.z();
3250 }
3251 void scale( const Vector3& scaling ){
3252         if ( !nothingSelected() ) {
3253                 m_scale = scaling;
3254
3255                 if ( Mode() == eComponent ) {
3256                         Scene_Scale_Component_Selected( GlobalSceneGraph(), m_scale, vector4_to_vector3( m_pivot2world.t() ) );
3257                 }
3258                 else
3259                 {
3260                         Scene_Scale_Selected( GlobalSceneGraph(), m_scale, vector4_to_vector3( m_pivot2world.t() ) );
3261                 }
3262
3263                 SceneChangeNotify();
3264         }
3265 }
3266 void outputScale( TextOutputStream& ostream ){
3267         ostream << " -scale " << m_scale.x() << " " << m_scale.y() << " " << m_scale.z();
3268 }
3269
3270 void rotateSelected( const Quaternion& rotation, bool snapOrigin ){
3271         if( snapOrigin && !m_pivotIsCustom ){
3272                 m_pivot2world.tx() = float_snapped( m_pivot2world.tx(), GetSnapGridSize() );
3273                 m_pivot2world.ty() = float_snapped( m_pivot2world.ty(), GetSnapGridSize() );
3274                 m_pivot2world.tz() = float_snapped( m_pivot2world.tz(), GetSnapGridSize() );
3275         }
3276         startMove();
3277         rotate( rotation );
3278         freezeTransforms();
3279 }
3280 void translateSelected( const Vector3& translation ){
3281         startMove();
3282         translate( translation );
3283         freezeTransforms();
3284 }
3285 void scaleSelected( const Vector3& scaling ){
3286         startMove();
3287         scale( scaling );
3288         freezeTransforms();
3289 }
3290
3291 void MoveSelected( const View& view, const float device_point[2], bool snap ){
3292         if ( m_manipulator->isSelected() ) {
3293                 if ( !m_undo_begun ) {
3294                         m_undo_begun = true;
3295                         GlobalUndoSystem().start();
3296                 }
3297
3298                 Matrix4 device2manip;
3299                 ConstructDevice2Manip( device2manip, m_pivot2world_start, view.GetModelview(), view.GetProjection(), view.GetViewport() );
3300                 m_manipulator->GetManipulatable()->Transform( m_manip2pivot_start, device2manip, device_point[0], device_point[1], snap );
3301         }
3302 }
3303
3304 /// \todo Support view-dependent nudge.
3305 void NudgeManipulator( const Vector3& nudge, const Vector3& view ){
3306         if ( ManipulatorMode() == eTranslate || ManipulatorMode() == eDrag ) {
3307                 translateSelected( nudge );
3308         }
3309 }
3310
3311 void endMove();
3312 void freezeTransforms();
3313
3314 void renderSolid( Renderer& renderer, const VolumeTest& volume ) const;
3315 void renderWireframe( Renderer& renderer, const VolumeTest& volume ) const {
3316         renderSolid( renderer, volume );
3317 }
3318
3319 const Matrix4& GetPivot2World() const {
3320         ConstructPivot();
3321         return m_pivot2world;
3322 }
3323
3324 static void constructStatic(){
3325         m_state = GlobalShaderCache().capture( "$POINT" );
3326   #if defined( DEBUG_SELECTION )
3327         g_state_clipped = GlobalShaderCache().capture( "$DEBUG_CLIPPED" );
3328   #endif
3329         TranslateManipulator::m_state_wire = GlobalShaderCache().capture( "$WIRE_OVERLAY" );
3330         TranslateManipulator::m_state_fill = GlobalShaderCache().capture( "$FLATSHADE_OVERLAY" );
3331         RotateManipulator::m_state_outer = GlobalShaderCache().capture( "$WIRE_OVERLAY" );
3332 }
3333
3334 static void destroyStatic(){
3335   #if defined( DEBUG_SELECTION )
3336         GlobalShaderCache().release( "$DEBUG_CLIPPED" );
3337   #endif
3338         GlobalShaderCache().release( "$WIRE_OVERLAY" );
3339         GlobalShaderCache().release( "$FLATSHADE_OVERLAY" );
3340         GlobalShaderCache().release( "$WIRE_OVERLAY" );
3341         GlobalShaderCache().release( "$POINT" );
3342 }
3343 };
3344
3345 Shader* RadiantSelectionSystem::m_state = 0;
3346
3347
3348 namespace
3349 {
3350 RadiantSelectionSystem* g_RadiantSelectionSystem;
3351
3352 inline RadiantSelectionSystem& getSelectionSystem(){
3353         return *g_RadiantSelectionSystem;
3354 }
3355 }
3356
3357 #include "map.h"
3358
3359 class testselect_entity_visible : public scene::Graph::Walker
3360 {
3361 Selector& m_selector;
3362 SelectionTest& m_test;
3363 public:
3364 testselect_entity_visible( Selector& selector, SelectionTest& test )
3365         : m_selector( selector ), m_test( test ){
3366 }
3367 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3368         if( path.top().get_pointer() == Map_GetWorldspawn( g_map ) ||
3369                 node_is_group( path.top().get() ) ){
3370                 return false;
3371         }
3372         Selectable* selectable = Instance_getSelectable( instance );
3373         if ( selectable != 0
3374                  && Node_isEntity( path.top() ) ) {
3375                 m_selector.pushSelectable( *selectable );
3376         }
3377
3378         SelectionTestable* selectionTestable = Instance_getSelectionTestable( instance );
3379         if ( selectionTestable ) {
3380                 selectionTestable->testSelect( m_selector, m_test );
3381         }
3382
3383         return true;
3384 }
3385 void post( const scene::Path& path, scene::Instance& instance ) const {
3386         Selectable* selectable = Instance_getSelectable( instance );
3387         if ( selectable != 0
3388                  && Node_isEntity( path.top() ) ) {
3389                 m_selector.popSelectable();
3390         }
3391 }
3392 };
3393
3394 class testselect_primitive_visible : public scene::Graph::Walker
3395 {
3396 Selector& m_selector;
3397 SelectionTest& m_test;
3398 public:
3399 testselect_primitive_visible( Selector& selector, SelectionTest& test )
3400         : m_selector( selector ), m_test( test ){
3401 }
3402 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3403         Selectable* selectable = Instance_getSelectable( instance );
3404         if ( selectable != 0 ) {
3405                 m_selector.pushSelectable( *selectable );
3406         }
3407
3408         SelectionTestable* selectionTestable = Instance_getSelectionTestable( instance );
3409         if ( selectionTestable ) {
3410                 selectionTestable->testSelect( m_selector, m_test );
3411         }
3412
3413         return true;
3414 }
3415 void post( const scene::Path& path, scene::Instance& instance ) const {
3416         Selectable* selectable = Instance_getSelectable( instance );
3417         if ( selectable != 0 ) {
3418                 m_selector.popSelectable();
3419         }
3420 }
3421 };
3422
3423 class testselect_component_visible : public scene::Graph::Walker
3424 {
3425 Selector& m_selector;
3426 SelectionTest& m_test;
3427 SelectionSystem::EComponentMode m_mode;
3428 public:
3429 testselect_component_visible( Selector& selector, SelectionTest& test, SelectionSystem::EComponentMode mode )
3430         : m_selector( selector ), m_test( test ), m_mode( mode ){
3431 }
3432 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3433         ComponentSelectionTestable* componentSelectionTestable = Instance_getComponentSelectionTestable( instance );
3434         if ( componentSelectionTestable ) {
3435                 componentSelectionTestable->testSelectComponents( m_selector, m_test, m_mode );
3436         }
3437
3438         return true;
3439 }
3440 };
3441
3442
3443 class testselect_component_visible_selected : public scene::Graph::Walker
3444 {
3445 Selector& m_selector;
3446 SelectionTest& m_test;
3447 SelectionSystem::EComponentMode m_mode;
3448 public:
3449 testselect_component_visible_selected( Selector& selector, SelectionTest& test, SelectionSystem::EComponentMode mode )
3450         : m_selector( selector ), m_test( test ), m_mode( mode ){
3451 }
3452 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3453         Selectable* selectable = Instance_getSelectable( instance );
3454         if ( selectable != 0 && selectable->isSelected() ) {
3455                 ComponentSelectionTestable* componentSelectionTestable = Instance_getComponentSelectionTestable( instance );
3456                 if ( componentSelectionTestable ) {
3457                         componentSelectionTestable->testSelectComponents( m_selector, m_test, m_mode );
3458                 }
3459         }
3460
3461         return true;
3462 }
3463 };
3464
3465 void Scene_TestSelect_Primitive( Selector& selector, SelectionTest& test, const VolumeTest& volume ){
3466         Scene_forEachVisible( GlobalSceneGraph(), volume, testselect_primitive_visible( selector, test ) );
3467 }
3468
3469 void Scene_TestSelect_Component_Selected( Selector& selector, SelectionTest& test, const VolumeTest& volume, SelectionSystem::EComponentMode componentMode ){
3470         Scene_forEachVisible( GlobalSceneGraph(), volume, testselect_component_visible_selected( selector, test, componentMode ) );
3471 }
3472
3473 void Scene_TestSelect_Component( Selector& selector, SelectionTest& test, const VolumeTest& volume, SelectionSystem::EComponentMode componentMode ){
3474         Scene_forEachVisible( GlobalSceneGraph(), volume, testselect_component_visible( selector, test, componentMode ) );
3475 }
3476
3477 void RadiantSelectionSystem::Scene_TestSelect( Selector& selector, SelectionTest& test, const View& view, SelectionSystem::EMode mode, SelectionSystem::EComponentMode componentMode ){
3478         switch ( mode )
3479         {
3480         case eEntity:
3481         {
3482                 Scene_forEachVisible( GlobalSceneGraph(), view, testselect_entity_visible( selector, test ) );
3483         }
3484         break;
3485         case ePrimitive:
3486                 Scene_TestSelect_Primitive( selector, test, view );
3487                 break;
3488         case eComponent:
3489                 Scene_TestSelect_Component_Selected( selector, test, view, componentMode );
3490                 break;
3491         }
3492 }
3493
3494 class FreezeTransforms : public scene::Graph::Walker
3495 {
3496 public:
3497 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3498         TransformNode* transformNode = Node_getTransformNode( path.top() );
3499         if ( transformNode != 0 ) {
3500                 Transformable* transform = Instance_getTransformable( instance );
3501                 if ( transform != 0 ) {
3502                         transform->freezeTransform();
3503                 }
3504         }
3505         return true;
3506 }
3507 };
3508
3509 void RadiantSelectionSystem::freezeTransforms(){
3510         GlobalSceneGraph().traverse( FreezeTransforms() );
3511 }
3512
3513
3514 void RadiantSelectionSystem::endMove(){
3515         freezeTransforms();
3516
3517         if ( Mode() == ePrimitive ) {
3518                 if ( ManipulatorMode() == eDrag ) {
3519                         Scene_SelectAll_Component( false, SelectionSystem::eFace );
3520                 }
3521         }
3522
3523         m_pivot_moving = false;
3524         pivotChanged();
3525
3526         SceneChangeNotify();
3527
3528         if ( m_undo_begun ) {
3529                 StringOutputStream command;
3530
3531                 if ( ManipulatorMode() == eTranslate ) {
3532                         command << "translateTool";
3533                         outputTranslation( command );
3534                 }
3535                 else if ( ManipulatorMode() == eRotate ) {
3536                         command << "rotateTool";
3537                         outputRotation( command );
3538                 }
3539                 else if ( ManipulatorMode() == eScale ) {
3540                         command << "scaleTool";
3541                         outputScale( command );
3542                 }
3543                 else if ( ManipulatorMode() == eDrag ) {
3544                         command << "dragTool";
3545                 }
3546
3547                 GlobalUndoSystem().finish( command.c_str() );
3548         }
3549
3550 }
3551
3552 inline AABB Instance_getPivotBounds( scene::Instance& instance ){
3553         Entity* entity = Node_getEntity( instance.path().top() );
3554         if ( entity != 0
3555                  && ( entity->getEntityClass().fixedsize
3556                           || !node_is_group( instance.path().top() ) ) ) {
3557                 Editable* editable = Node_getEditable( instance.path().top() );
3558                 if ( editable != 0 ) {
3559                         return AABB( vector4_to_vector3( matrix4_multiplied_by_matrix4( instance.localToWorld(), editable->getLocalPivot() ).t() ), Vector3( 0, 0, 0 ) );
3560                 }
3561                 else
3562                 {
3563                         return AABB( vector4_to_vector3( instance.localToWorld().t() ), Vector3( 0, 0, 0 ) );
3564                 }
3565         }
3566
3567         return instance.worldAABB();
3568 }
3569
3570 class bounds_selected : public scene::Graph::Walker
3571 {
3572 AABB& m_bounds;
3573 public:
3574 bounds_selected( AABB& bounds )
3575         : m_bounds( bounds ){
3576         m_bounds = AABB();
3577 }
3578 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3579         Selectable* selectable = Instance_getSelectable( instance );
3580         if ( selectable != 0
3581                  && selectable->isSelected() ) {
3582                 aabb_extend_by_aabb_safe( m_bounds, Instance_getPivotBounds( instance ) );
3583         }
3584         return true;
3585 }
3586 };
3587
3588 class bounds_selected_component : public scene::Graph::Walker
3589 {
3590 AABB& m_bounds;
3591 public:
3592 bounds_selected_component( AABB& bounds )
3593         : m_bounds( bounds ){
3594         m_bounds = AABB();
3595 }
3596 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3597         Selectable* selectable = Instance_getSelectable( instance );
3598         if ( selectable != 0
3599                  && selectable->isSelected() ) {
3600                 ComponentEditable* componentEditable = Instance_getComponentEditable( instance );
3601                 if ( componentEditable ) {
3602                         aabb_extend_by_aabb_safe( m_bounds, aabb_for_oriented_aabb_safe( componentEditable->getSelectedComponentsBounds(), instance.localToWorld() ) );
3603                 }
3604         }
3605         return true;
3606 }
3607 };
3608
3609 void Scene_BoundsSelected( scene::Graph& graph, AABB& bounds ){
3610         graph.traverse( bounds_selected( bounds ) );
3611 }
3612
3613 void Scene_BoundsSelectedComponent( scene::Graph& graph, AABB& bounds ){
3614         graph.traverse( bounds_selected_component( bounds ) );
3615 }
3616
3617 #if 0
3618 inline void pivot_for_node( Matrix4& pivot, scene::Node& node, scene::Instance& instance ){
3619         ComponentEditable* componentEditable = Instance_getComponentEditable( instance );
3620         if ( GlobalSelectionSystem().Mode() == SelectionSystem::eComponent
3621                  && componentEditable != 0 ) {
3622                 pivot = matrix4_translation_for_vec3( componentEditable->getSelectedComponentsBounds().origin );
3623         }
3624         else
3625         {
3626                 Bounded* bounded = Instance_getBounded( instance );
3627                 if ( bounded != 0 ) {
3628                         pivot = matrix4_translation_for_vec3( bounded->localAABB().origin );
3629                 }
3630                 else
3631                 {
3632                         pivot = g_matrix4_identity;
3633                 }
3634         }
3635 }
3636 #endif
3637
3638 void RadiantSelectionSystem::ConstructPivot() const {
3639         if ( !m_pivotChanged || m_pivot_moving || m_pivotIsCustom ) {
3640                 return;
3641         }
3642         m_pivotChanged = false;
3643
3644         Vector3 m_object_pivot;
3645
3646         if ( !nothingSelected() ) {
3647                 {
3648                         AABB bounds;
3649                         if ( Mode() == eComponent ) {
3650                                 Scene_BoundsSelectedComponent( GlobalSceneGraph(), bounds );
3651                         }
3652                         else
3653                         {
3654                                 Scene_BoundsSelected( GlobalSceneGraph(), bounds );
3655                         }
3656                         m_object_pivot = bounds.origin;
3657                 }
3658
3659                 //vector3_snap( m_object_pivot, GetSnapGridSize() );
3660                 //globalOutputStream() << m_object_pivot << "\n";
3661                 m_pivot2world = matrix4_translation_for_vec3( m_object_pivot );
3662
3663                 switch ( m_manipulator_mode )
3664                 {
3665                 case eTranslate:
3666                         break;
3667                 case eRotate:
3668                         if ( Mode() == eComponent ) {
3669                                 matrix4_assign_rotation_for_pivot( m_pivot2world, m_component_selection.back() );
3670                         }
3671                         else
3672                         {
3673                                 matrix4_assign_rotation_for_pivot( m_pivot2world, m_selection.back() );
3674                         }
3675                         break;
3676                 case eScale:
3677                         if ( Mode() == eComponent ) {
3678                                 matrix4_assign_rotation_for_pivot( m_pivot2world, m_component_selection.back() );
3679                         }
3680                         else
3681                         {
3682                                 matrix4_assign_rotation_for_pivot( m_pivot2world, m_selection.back() );
3683                         }
3684                         break;
3685                 default:
3686                         break;
3687                 }
3688         }
3689 }
3690
3691 void RadiantSelectionSystem::setCustomPivotOrigin( Vector3& point ) const {
3692         if ( !nothingSelected() && ( m_manipulator_mode == eTranslate || m_manipulator_mode == eRotate || m_manipulator_mode == eScale ) ) {
3693                 AABB bounds;
3694                 if ( Mode() == eComponent ) {
3695                         Scene_BoundsSelectedComponent( GlobalSceneGraph(), bounds );
3696                 }
3697                 else
3698                 {
3699                         Scene_BoundsSelected( GlobalSceneGraph(), bounds );
3700                 }
3701                 //globalOutputStream() << point << "\n";
3702                 for( std::size_t i = 0; i < 3; i++ ){
3703                         if( point[i] < 900000.0f ){
3704                                 float bestsnapDist = fabs( bounds.origin[i] - point[i] );
3705                                 float bestsnapTo = bounds.origin[i];
3706                                 float othersnapDist = fabs( bounds.origin[i] + bounds.extents[i] - point[i] );
3707                                 if( othersnapDist < bestsnapDist ){
3708                                         bestsnapDist = othersnapDist;
3709                                         bestsnapTo = bounds.origin[i] + bounds.extents[i];
3710                                 }
3711                                 othersnapDist = fabs( bounds.origin[i] - bounds.extents[i] - point[i] );
3712                                 if( othersnapDist < bestsnapDist ){
3713                                         bestsnapDist = othersnapDist;
3714                                         bestsnapTo = bounds.origin[i] - bounds.extents[i];
3715                                 }
3716                                 othersnapDist = fabs( float_snapped( point[i], GetSnapGridSize() ) - point[i] );
3717                                 if( othersnapDist < bestsnapDist ){
3718                                         bestsnapDist = othersnapDist;
3719                                         bestsnapTo = float_snapped( point[i], GetSnapGridSize() );
3720                                 }
3721                                 point[i] = bestsnapTo;
3722
3723                                 m_pivot2world[i + 12] = point[i]; //m_pivot2world.tx() .ty() .tz()
3724                         }
3725                 }
3726
3727                 switch ( m_manipulator_mode )
3728                 {
3729                 case eTranslate:
3730                         break;
3731                 case eRotate:
3732                         if ( Mode() == eComponent ) {
3733                                 matrix4_assign_rotation_for_pivot( m_pivot2world, m_component_selection.back() );
3734                         }
3735                         else
3736                         {
3737                                 matrix4_assign_rotation_for_pivot( m_pivot2world, m_selection.back() );
3738                         }
3739                         break;
3740                 case eScale:
3741                         if ( Mode() == eComponent ) {
3742                                 matrix4_assign_rotation_for_pivot( m_pivot2world, m_component_selection.back() );
3743                         }
3744                         else
3745                         {
3746                                 matrix4_assign_rotation_for_pivot( m_pivot2world, m_selection.back() );
3747                         }
3748                         break;
3749                 default:
3750                         break;
3751                 }
3752
3753                 m_pivotIsCustom = true;
3754         }
3755 }
3756
3757 void RadiantSelectionSystem::getSelectionAABB( AABB& bounds ) const {
3758         if ( !nothingSelected() ) {
3759                 if ( Mode() == eComponent ) {
3760                         Scene_BoundsSelectedComponent( GlobalSceneGraph(), bounds );
3761                 }
3762                 else
3763                 {
3764                         Scene_BoundsSelected( GlobalSceneGraph(), bounds );
3765                 }
3766         }
3767 }
3768
3769 void GetSelectionAABB( AABB& bounds ){
3770         getSelectionSystem().getSelectionAABB( bounds );
3771 }
3772
3773 const Matrix4& SelectionSystem_GetPivot2World(){
3774         return getSelectionSystem().GetPivot2World();
3775 }
3776
3777 void RadiantSelectionSystem::renderSolid( Renderer& renderer, const VolumeTest& volume ) const {
3778         //if(view->TestPoint(m_object_pivot))
3779         if ( !nothingSelected() ) {
3780                 renderer.Highlight( Renderer::ePrimitive, false );
3781                 renderer.Highlight( Renderer::eFace, false );
3782
3783                 renderer.SetState( m_state, Renderer::eWireframeOnly );
3784                 renderer.SetState( m_state, Renderer::eFullMaterials );
3785
3786                 m_manipulator->render( renderer, volume, GetPivot2World() );
3787         }
3788
3789 #if defined( DEBUG_SELECTION )
3790         renderer.SetState( g_state_clipped, Renderer::eWireframeOnly );
3791         renderer.SetState( g_state_clipped, Renderer::eFullMaterials );
3792         renderer.addRenderable( g_render_clipped, g_render_clipped.m_world );
3793 #endif
3794 }
3795
3796 #include "preferencesystem.h"
3797 #include "preferences.h"
3798
3799 void SelectionSystem_constructPreferences( PreferencesPage& page ){
3800         page.appendCheckBox( "", "Prefer point entities in 2D", getSelectionSystem().m_bPreferPointEntsIn2D );
3801 }
3802 void SelectionSystem_constructPage( PreferenceGroup& group ){
3803         PreferencesPage page( group.createPage( "Selection", "Selection System Settings" ) );
3804         SelectionSystem_constructPreferences( page );
3805 }
3806 void SelectionSystem_registerPreferencesPage(){
3807         PreferencesDialog_addSettingsPage( FreeCaller<void(PreferenceGroup&), SelectionSystem_constructPage>() );
3808 }
3809
3810
3811
3812 void SelectionSystem_OnBoundsChanged(){
3813         getSelectionSystem().pivotChanged();
3814 }
3815
3816 SignalHandlerId SelectionSystem_boundsChanged;
3817
3818 void SelectionSystem_Construct(){
3819         RadiantSelectionSystem::constructStatic();
3820
3821         g_RadiantSelectionSystem = new RadiantSelectionSystem;
3822
3823         SelectionSystem_boundsChanged = GlobalSceneGraph().addBoundsChangedCallback( FreeCaller<void(), SelectionSystem_OnBoundsChanged>() );
3824
3825         GlobalShaderCache().attachRenderable( getSelectionSystem() );
3826
3827         GlobalPreferenceSystem().registerPreference( "PreferPointEntsIn2D", make_property_string( getSelectionSystem().m_bPreferPointEntsIn2D ) );
3828         SelectionSystem_registerPreferencesPage();
3829 }
3830
3831 void SelectionSystem_Destroy(){
3832         GlobalShaderCache().detachRenderable( getSelectionSystem() );
3833
3834         GlobalSceneGraph().removeBoundsChangedCallback( SelectionSystem_boundsChanged );
3835
3836         delete g_RadiantSelectionSystem;
3837
3838         RadiantSelectionSystem::destroyStatic();
3839 }
3840
3841
3842
3843
3844 inline float screen_normalised( float pos, std::size_t size ){
3845         return ( ( 2.0f * pos ) / size ) - 1.0f;
3846 }
3847
3848 typedef Vector2 DeviceVector;
3849
3850 inline DeviceVector window_to_normalised_device( WindowVector window, std::size_t width, std::size_t height ){
3851         return DeviceVector( screen_normalised( window.x(), width ), screen_normalised( height - 1 - window.y(), height ) );
3852 }
3853
3854 inline float device_constrained( float pos ){
3855         return std::min( 1.0f, std::max( -1.0f, pos ) );
3856 }
3857
3858 inline DeviceVector device_constrained( DeviceVector device ){
3859         return DeviceVector( device_constrained( device.x() ), device_constrained( device.y() ) );
3860 }
3861
3862 inline float window_constrained( float pos, std::size_t origin, std::size_t size ){
3863         return std::min( static_cast<float>( origin + size ), std::max( static_cast<float>( origin ), pos ) );
3864 }
3865
3866 inline WindowVector window_constrained( WindowVector window, std::size_t x, std::size_t y, std::size_t width, std::size_t height ){
3867         return WindowVector( window_constrained( window.x(), x, width ), window_constrained( window.y(), y, height ) );
3868 }
3869
3870 typedef Callback<void(DeviceVector)> MouseEventCallback;
3871
3872 Single<MouseEventCallback> g_mouseMovedCallback;
3873 Single<MouseEventCallback> g_mouseUpCallback;
3874
3875 #if 1
3876 const ButtonIdentifier c_button_select = c_buttonLeft;
3877 const ButtonIdentifier c_button_select2 = c_buttonRight;
3878 const ModifierFlags c_modifier_manipulator = c_modifierNone;
3879 const ModifierFlags c_modifier_toggle = c_modifierShift;
3880 const ModifierFlags c_modifier_replace = c_modifierShift | c_modifierAlt;
3881 const ModifierFlags c_modifier_face = c_modifierControl;
3882 #else
3883 const ButtonIdentifier c_button_select = c_buttonLeft;
3884 const ModifierFlags c_modifier_manipulator = c_modifierNone;
3885 const ModifierFlags c_modifier_toggle = c_modifierControl;
3886 const ModifierFlags c_modifier_replace = c_modifierNone;
3887 const ModifierFlags c_modifier_face = c_modifierShift;
3888 #endif
3889 const ModifierFlags c_modifier_toggle_face = c_modifier_toggle | c_modifier_face;
3890 const ModifierFlags c_modifier_replace_face = c_modifier_replace | c_modifier_face;
3891
3892 const ButtonIdentifier c_button_texture = c_buttonMiddle;
3893 const ModifierFlags c_modifier_apply_texture1 = c_modifierControl | c_modifierShift;
3894 const ModifierFlags c_modifier_apply_texture2 = c_modifierControl;
3895 const ModifierFlags c_modifier_apply_texture3 =                     c_modifierShift;
3896 const ModifierFlags c_modifier_copy_texture = c_modifierNone;
3897
3898 class Selector_
3899 {
3900 RadiantSelectionSystem::EModifier modifier_for_state( ModifierFlags state ){
3901         if ( ( state == c_modifier_toggle || state == c_modifier_toggle_face || state == c_modifier_face ) ) {
3902                 if( m_mouse2 ){
3903                         return RadiantSelectionSystem::eReplace;
3904                 }
3905                 else{
3906                         return RadiantSelectionSystem::eToggle;
3907                 }
3908         }
3909         return RadiantSelectionSystem::eManipulator;
3910 }
3911
3912 rect_t getDeviceArea() const {
3913         DeviceVector delta( m_current - m_start );
3914         if ( selecting() && fabs( delta.x() ) > m_epsilon.x() && fabs( delta.y() ) > m_epsilon.y() ) {
3915                 return SelectionBoxForArea( &m_start[0], &delta[0] );
3916         }
3917         else
3918         {
3919                 rect_t default_area = { { 0, 0, }, { 0, 0, }, };
3920                 return default_area;
3921         }
3922 }
3923
3924 public:
3925 DeviceVector m_start;
3926 DeviceVector m_current;
3927 DeviceVector m_epsilon;
3928 ModifierFlags m_state;
3929 bool m_mouse2;
3930 bool m_mouseMoved;
3931 bool m_mouseMovedWhilePressed;
3932 bool m_paintSelect;
3933 const View* m_view;
3934 RectangleCallback m_window_update;
3935
3936 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 ){
3937 }
3938
3939 void draw_area(){
3940         m_window_update( getDeviceArea() );
3941 }
3942
3943 void testSelect( DeviceVector position ){
3944         RadiantSelectionSystem::EModifier modifier = modifier_for_state( m_state );
3945         if ( modifier != RadiantSelectionSystem::eManipulator ) {
3946                 DeviceVector delta( position - m_start );
3947                 if ( fabs( delta.x() ) > m_epsilon.x() && fabs( delta.y() ) > m_epsilon.y() ) {
3948                         DeviceVector delta( position - m_start );
3949                         //getSelectionSystem().SelectArea( *m_view, &m_start[0], &delta[0], modifier, ( m_state & c_modifier_face ) != c_modifierNone );
3950                         getSelectionSystem().SelectArea( *m_view, &m_start[0], &delta[0], RadiantSelectionSystem::eToggle, ( m_state & c_modifier_face ) != c_modifierNone );
3951                 }
3952                 else if( !m_mouseMovedWhilePressed ){
3953                         if ( modifier == RadiantSelectionSystem::eReplace && !m_mouseMoved ) {
3954                                 modifier = RadiantSelectionSystem::eCycle;
3955                         }
3956                         getSelectionSystem().SelectPoint( *m_view, &position[0], &m_epsilon[0], modifier, ( m_state & c_modifier_face ) != c_modifierNone );
3957                 }
3958         }
3959
3960         m_start = m_current = DeviceVector( 0.0f, 0.0f );
3961         draw_area();
3962 }
3963
3964 void testSelect_simpleM1( DeviceVector position ){
3965         /*RadiantSelectionSystem::EModifier modifier = RadiantSelectionSystem::eReplace;
3966         DeviceVector delta( position - m_start );
3967         if ( fabs( delta.x() ) < m_epsilon.x() && fabs( delta.y() ) < m_epsilon.y() ) {
3968                 modifier = RadiantSelectionSystem::eCycle;
3969         }
3970         getSelectionSystem().SelectPoint( *m_view, &position[0], &m_epsilon[0], modifier, false );*/
3971         getSelectionSystem().SelectPoint( *m_view, &position[0], &m_epsilon[0], m_mouseMoved ? RadiantSelectionSystem::eReplace : RadiantSelectionSystem::eCycle, false );
3972         m_start = m_current = device_constrained( position );
3973 }
3974
3975
3976 bool selecting() const {
3977         return m_state != c_modifier_manipulator && m_mouse2;
3978 }
3979
3980 void setState( ModifierFlags state ){
3981         bool was_selecting = selecting();
3982         m_state = state;
3983         if ( was_selecting ^ selecting() ) {
3984                 draw_area();
3985         }
3986 }
3987
3988 ModifierFlags getState() const {
3989         return m_state;
3990 }
3991
3992 void modifierEnable( ModifierFlags type ){
3993         setState( bitfield_enable( getState(), type ) );
3994 }
3995 void modifierDisable( ModifierFlags type ){
3996         setState( bitfield_disable( getState(), type ) );
3997 }
3998
3999 void mouseDown( DeviceVector position ){
4000         m_start = m_current = device_constrained( position );
4001         if( !m_mouse2 && m_state != c_modifierNone ){
4002                 m_paintSelect = getSelectionSystem().SelectPoint_InitPaint( *m_view, &position[0], &m_epsilon[0], ( m_state & c_modifier_face ) != c_modifierNone );
4003         }
4004 }
4005
4006 void mouseMoved( DeviceVector position ){
4007         m_current = device_constrained( position );
4008         m_mouseMovedWhilePressed = true;
4009         if( m_mouse2 ){
4010                 draw_area();
4011         }
4012         else if( m_state != c_modifier_manipulator ){
4013                 getSelectionSystem().SelectPoint( *m_view, &m_current[0], &m_epsilon[0],
4014                                                                                 m_paintSelect ? RadiantSelectionSystem::eSelect : RadiantSelectionSystem::eDeselect,
4015                                                                                 ( m_state & c_modifier_face ) != c_modifierNone );
4016         }
4017 }
4018 typedef MemberCaller<Selector_, void(DeviceVector), &Selector_::mouseMoved> MouseMovedCaller;
4019
4020 void mouseUp( DeviceVector position ){
4021         if( m_mouse2 ){
4022                 testSelect( device_constrained( position ) );
4023         }
4024         else{
4025                 m_start = m_current = DeviceVector( 0.0f, 0.0f );
4026         }
4027
4028         g_mouseMovedCallback.clear();
4029         g_mouseUpCallback.clear();
4030 }
4031 typedef MemberCaller<Selector_, void(DeviceVector), &Selector_::mouseUp> MouseUpCaller;
4032 };
4033
4034
4035 class Manipulator_
4036 {
4037 public:
4038 DeviceVector m_epsilon;
4039 const View* m_view;
4040 ModifierFlags m_state;
4041
4042 Manipulator_() : m_state( c_modifierNone ){
4043 }
4044
4045 bool mouseDown( DeviceVector position ){
4046         return getSelectionSystem().SelectManipulator( *m_view, &position[0], &m_epsilon[0] );
4047 }
4048
4049 void mouseMoved( DeviceVector position ){
4050         getSelectionSystem().MoveSelected( *m_view, &position[0], ( m_state & c_modifierShift ) == c_modifierShift );
4051 }
4052 typedef MemberCaller<Manipulator_, void(DeviceVector), &Manipulator_::mouseMoved> MouseMovedCaller;
4053
4054 void mouseUp( DeviceVector position ){
4055         getSelectionSystem().endMove();
4056         g_mouseMovedCallback.clear();
4057         g_mouseUpCallback.clear();
4058 }
4059 typedef MemberCaller<Manipulator_, void(DeviceVector), &Manipulator_::mouseUp> MouseUpCaller;
4060
4061 void setState( ModifierFlags state ){
4062         m_state = state;
4063 }
4064
4065 ModifierFlags getState() const {
4066         return m_state;
4067 }
4068
4069 void modifierEnable( ModifierFlags type ){
4070         setState( bitfield_enable( getState(), type ) );
4071 }
4072 void modifierDisable( ModifierFlags type ){
4073         setState( bitfield_disable( getState(), type ) );
4074 }
4075 };
4076
4077 void Scene_copyClosestTexture( SelectionTest& test );
4078 void Scene_applyClosestTexture( SelectionTest& test );
4079
4080 class RadiantWindowObserver : public SelectionSystemWindowObserver
4081 {
4082 enum
4083 {
4084         SELECT_EPSILON = 8,
4085 };
4086
4087 int m_width;
4088 int m_height;
4089
4090 bool m_mouse_down;
4091
4092 public:
4093 Selector_ m_selector;
4094 Manipulator_ m_manipulator;
4095
4096 RadiantWindowObserver() : m_mouse_down( false ){
4097 }
4098 void release(){
4099         delete this;
4100 }
4101 void setView( const View& view ){
4102         m_selector.m_view = &view;
4103         m_manipulator.m_view = &view;
4104 }
4105 void setRectangleDrawCallback( const RectangleCallback& callback ){
4106         m_selector.m_window_update = callback;
4107 }
4108 void onSizeChanged( int width, int height ){
4109         m_width = width;
4110         m_height = height;
4111         DeviceVector epsilon( SELECT_EPSILON / static_cast<float>( m_width ), SELECT_EPSILON / static_cast<float>( m_height ) );
4112         m_selector.m_epsilon = m_manipulator.m_epsilon = epsilon;
4113 }
4114 void onMouseDown( const WindowVector& position, ButtonIdentifier button, ModifierFlags modifiers ){
4115         if ( button == c_button_select || ( button == c_button_select2 && modifiers != c_modifierNone ) ) {
4116                 m_mouse_down = true;
4117                 //m_selector.m_mouseMoved = false;
4118
4119                 DeviceVector devicePosition( window_to_normalised_device( position, m_width, m_height ) );
4120                 if ( modifiers == c_modifier_manipulator && m_manipulator.mouseDown( devicePosition ) ) {
4121                         g_mouseMovedCallback.insert( MouseEventCallback( Manipulator_::MouseMovedCaller( m_manipulator ) ) );
4122                         g_mouseUpCallback.insert( MouseEventCallback( Manipulator_::MouseUpCaller( m_manipulator ) ) );
4123                 }
4124                 else
4125                 {
4126                         if ( button == c_button_select ) {
4127                                 m_selector.m_mouse2 = false;
4128                         }
4129                         else{
4130                                 m_selector.m_mouse2 = true;
4131                         }
4132                         m_selector.mouseDown( devicePosition );
4133                         g_mouseMovedCallback.insert( MouseEventCallback( Selector_::MouseMovedCaller( m_selector ) ) );
4134                         g_mouseUpCallback.insert( MouseEventCallback( Selector_::MouseUpCaller( m_selector ) ) );
4135                 }
4136         }
4137         else if ( button == c_button_texture ) {
4138                 DeviceVector devicePosition( device_constrained( window_to_normalised_device( position, m_width, m_height ) ) );
4139
4140                 View scissored( *m_selector.m_view );
4141                 ConstructSelectionTest( scissored, SelectionBoxForPoint( &devicePosition[0], &m_selector.m_epsilon[0] ) );
4142                 SelectionVolume volume( scissored );
4143
4144                 if ( modifiers == c_modifier_apply_texture1 || modifiers == c_modifier_apply_texture2 || modifiers == c_modifier_apply_texture3 ) {
4145                         Scene_applyClosestTexture( volume );
4146                 }
4147                 else if ( modifiers == c_modifier_copy_texture ) {
4148                         Scene_copyClosestTexture( volume );
4149                 }
4150         }
4151 }
4152 void onMouseMotion( const WindowVector& position, ModifierFlags modifiers ){
4153         m_selector.m_mouseMoved = true;
4154         if ( m_mouse_down && !g_mouseMovedCallback.empty() ) {
4155                 m_selector.m_mouseMovedWhilePressed = true;
4156                 g_mouseMovedCallback.get() ( window_to_normalised_device( position, m_width, m_height ) );
4157         }
4158 }
4159 void onMouseUp( const WindowVector& position, ButtonIdentifier button, ModifierFlags modifiers ){
4160         if ( ( button == c_button_select || button == c_button_select2 ) && !g_mouseUpCallback.empty() ) {
4161                 m_mouse_down = false;
4162
4163                 g_mouseUpCallback.get() ( window_to_normalised_device( position, m_width, m_height ) );
4164         }
4165         //L button w/o scene changed = tunnel selection
4166         if( // !getSelectionSystem().m_undo_begun &&
4167                 modifiers == c_modifierNone && button == c_button_select &&
4168                 //( !m_selector.m_mouseMoved || !m_mouse_down ) &&
4169                 !m_selector.m_mouseMovedWhilePressed &&
4170                 ( getSelectionSystem().Mode() != SelectionSystem::eComponent || getSelectionSystem().ManipulatorMode() != SelectionSystem::eDrag ) ){
4171                 m_selector.testSelect_simpleM1( device_constrained( window_to_normalised_device( position, m_width, m_height ) ) );
4172         }
4173         //getSelectionSystem().m_undo_begun = false;
4174         m_selector.m_mouseMoved = false;
4175         m_selector.m_mouseMovedWhilePressed = false;
4176 }
4177 void onModifierDown( ModifierFlags type ){
4178         m_selector.modifierEnable( type );
4179         m_manipulator.modifierEnable( type );
4180 }
4181 void onModifierUp( ModifierFlags type ){
4182         m_selector.modifierDisable( type );
4183         m_manipulator.modifierDisable( type );
4184 }
4185 };
4186
4187
4188
4189 SelectionSystemWindowObserver* NewWindowObserver(){
4190         return new RadiantWindowObserver;
4191 }
4192
4193
4194
4195 #include "modulesystem/singletonmodule.h"
4196 #include "modulesystem/moduleregistry.h"
4197
4198 class SelectionDependencies :
4199         public GlobalSceneGraphModuleRef,
4200         public GlobalShaderCacheModuleRef,
4201         public GlobalOpenGLModuleRef
4202 {
4203 };
4204
4205 class SelectionAPI : public TypeSystemRef
4206 {
4207 SelectionSystem* m_selection;
4208 public:
4209 typedef SelectionSystem Type;
4210 STRING_CONSTANT( Name, "*" );
4211
4212 SelectionAPI(){
4213         SelectionSystem_Construct();
4214
4215         m_selection = &getSelectionSystem();
4216 }
4217 ~SelectionAPI(){
4218         SelectionSystem_Destroy();
4219 }
4220 SelectionSystem* getTable(){
4221         return m_selection;
4222 }
4223 };
4224
4225 typedef SingletonModule<SelectionAPI, SelectionDependencies> SelectionModule;
4226 typedef Static<SelectionModule> StaticSelectionModule;
4227 StaticRegisterModule staticRegisterSelection( StaticSelectionModule::instance() );