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