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