]> git.tdb.fi Git - r2c2.git/blobdiff - source/designer/manipulator.cpp
Rename the project to R²C²
[r2c2.git] / source / designer / manipulator.cpp
index aad20a084b84bcd0511192e25d2f7fb087502821..23ea138623c42809726c0df07c644ad15ba52f99 100644 (file)
@@ -1,6 +1,6 @@
 /* $Id$
 
-This file is part of the MSP Märklin suite
+This file is part of R²C²
 Copyright © 2006-2010  Mikkosoft Productions, Mikko Rasa
 Distributed under the GPL
 */
@@ -8,21 +8,24 @@ Distributed under the GPL
 #include <algorithm>
 #include <cmath>
 #include <msp/strings/formatter.h>
-#include "libmarklin/tracktype.h"
+#include "libr2c2/tracktype.h"
 #include "designer.h"
 #include "manipulator.h"
 #include "selection.h"
 
 using namespace std;
-using namespace Marklin;
+using namespace R2C2;
 using namespace Msp;
 
-Manipulator::Manipulator(Designer &d, Selection &s):
+Manipulator::Manipulator(Designer &d, Graphics::EventSource &es, Selection &s):
        designer(d),
+       event_source(es),
        selection(s),
        mode(NONE),
        angle(0)
 {
+       event_source.signal_button_press.connect(sigc::mem_fun(this, &Manipulator::button_press));
+       event_source.signal_pointer_motion.connect(sigc::mem_fun(this, &Manipulator::pointer_motion));
        selection.signal_changed.connect(sigc::mem_fun(this, &Manipulator::selection_changed));
 }
 
@@ -208,7 +211,7 @@ void Manipulator::connect()
                return;
        }
 
-       float limit = 0.001;
+       float limit = designer.get_layout().get_catalogue().get_gauge()/10;
 
        Track *track1 = tracks.front().track;
        Point pos1;
@@ -306,11 +309,14 @@ void Manipulator::cancel()
        signal_done.emit(false);
 }
 
-void Manipulator::button_press(int, int, float, float, unsigned btn)
+void Manipulator::button_press(int, int, unsigned btn, unsigned)
 {
+       if(!mode)
+               return;
+
        if(btn==3)
                cancel();
-       else if(btn==1 && mode)
+       else if(btn==1)
        {
                Mode m = mode;
                mode = NONE;
@@ -350,10 +356,10 @@ void Manipulator::button_press(int, int, float, float, unsigned btn)
        }
 }
 
-void Manipulator::pointer_motion(int, int y, float gx, float gy)
+void Manipulator::pointer_motion(int x, int y)
 {
        pointer_y = y;
-       gpointer = Point(gx, gy, 0);
+       gpointer = designer.map_pointer_to_ground(x, event_source.get_height()-1-y);
 
        if(mode==MOVE)
        {
@@ -366,6 +372,8 @@ void Manipulator::pointer_motion(int, int y, float gx, float gy)
                }
 
                const set<Track *> &ltracks = designer.get_layout().get_tracks();
+               float limit = max(designer.get_layout().get_catalogue().get_gauge(),
+                       designer.get_camera_controller().get_view_scale()*5/event_source.get_height());
                MTrack *snapped = 0;
                for(set<Track *>::const_iterator i=ltracks.begin(); (i!=ltracks.end() && !snapped); ++i)
                {
@@ -375,7 +383,7 @@ void Manipulator::pointer_motion(int, int y, float gx, float gy)
                        if(!ok) continue;
 
                        for(vector<MTrack>::iterator j=tracks.begin(); (j!=tracks.end() && !snapped); ++j)
-                               if(j->track->snap_to(**i, false))
+                               if(j->track->snap_to(**i, false, limit))
                                        snapped = &*j;
                }
 
@@ -439,8 +447,8 @@ void Manipulator::pointer_motion(int, int y, float gx, float gy)
                                float ep_dir = i->track->get_endpoint_direction(j);
                                float c = cos(ep_dir);
                                float s = sin(ep_dir);
-                               float dx = gx-ep_pos.x;
-                               float dy = gy-ep_pos.y;
+                               float dx = gpointer.x-ep_pos.x;
+                               float dy = gpointer.y-ep_pos.y;
 
                                float len = dx*c+dy*s;
                                if(len<length)
@@ -554,7 +562,7 @@ void Manipulator::set_slope(TrackOrder &track, float z, float dz)
        }
 }
 
-vector<Track *> Manipulator::create_straight(const Marklin::Point &start, float dir, float length, float limit)
+vector<Track *> Manipulator::create_straight(const R2C2::Point &start, float dir, float length, float limit)
 {
        const Catalogue::TrackMap &track_types = designer.get_catalogue().get_tracks();
        std::map<float, const TrackType *> types_by_length;