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