]> git.tdb.fi Git - r2c2.git/commitdiff
Move common accept/cancel handling to the Tool class
authorMikko Rasa <tdb@tdb.fi>
Sat, 24 Aug 2013 08:14:21 +0000 (11:14 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sat, 24 Aug 2013 08:14:21 +0000 (11:14 +0300)
13 files changed:
source/designer/extendtool.cpp
source/designer/extendtool.h
source/designer/manipulator.cpp
source/designer/manipulator.h
source/designer/measure.cpp
source/designer/measure.h
source/designer/movetool.cpp
source/designer/movetool.h
source/designer/rotatetool.cpp
source/designer/rotatetool.h
source/designer/slopetool.cpp
source/designer/tool.cpp
source/designer/tool.h

index e35b4abcf13a43c46a53861472cb127a7466bb49..a5dea819a192132aca5454864203bcc7dddf8bb1 100644 (file)
@@ -8,8 +8,7 @@ using namespace Msp;
 using namespace R2C2;
 
 ExtendTool::ExtendTool(Designer &d, Input::Mouse &m, const set<Object *> &objects):
-       Tool(d, m),
-       accepted(false)
+       Tool(d, m)
 {
        for(set<Object *>::const_iterator i=objects.begin(); i!=objects.end(); ++i)
                if(Track *t = dynamic_cast<Track *>(*i))
@@ -93,28 +92,11 @@ void ExtendTool::connect()
        extend_tracks.front()->link_to(*start_track);
        extend_tracks.back()->link_to(*end_track);
 
-       accepted = true;
-       set_done();
+       set_done(true);
 }
 
-void ExtendTool::button_press(unsigned btn)
+void ExtendTool::pointer_motion()
 {
-       if(btn==1)
-       {
-               for(vector<TrackIter>::const_iterator i=unlinked_endpoints.begin(); i!=unlinked_endpoints.end(); ++i)
-                       if(extend_tracks.front()->link_to(**i))
-                               break;
-               accepted = true;
-               set_done();
-       }
-       else if(btn==3)
-               set_done();
-}
-
-void ExtendTool::axis_motion(unsigned axis, float value, float rel)
-{
-       Tool::axis_motion(axis, value, rel);
-
        Vector pos;
        Angle dir;
        float length = 0;
@@ -163,6 +145,13 @@ void ExtendTool::axis_motion(unsigned axis, float value, float rel)
        }
 }
 
+void ExtendTool::finish()
+{
+       for(vector<TrackIter>::const_iterator i=unlinked_endpoints.begin(); i!=unlinked_endpoints.end(); ++i)
+               if(extend_tracks.front()->link_to(**i))
+                       break;
+}
+
 vector<Track *> ExtendTool::create_straight(const Vector &start, const Angle &dir, float length, float limit)
 {
        const Catalogue::TrackMap &track_types = designer.get_catalogue().get_tracks();
index 8e14fcf89a0cf07ea8bdd8b9bb9e064da5a39602..8a7ebebe6fadd75de56549756c0e61bba6acb2b0 100644 (file)
@@ -12,7 +12,6 @@ class ExtendTool: public Tool
 private:
        std::vector<R2C2::TrackIter> unlinked_endpoints;
        std::vector<R2C2::Track *> extend_tracks;
-       bool accepted;
 
 public:
        ExtendTool(Designer &, Msp::Input::Mouse &, const std::set<R2C2::Object *> &);
@@ -20,10 +19,10 @@ public:
 
        void connect();
 
-       virtual void button_press(unsigned);
-       virtual void axis_motion(unsigned, float, float);
-
 private:
+       virtual void pointer_motion();
+       virtual void finish();
+
        std::vector<R2C2::Track *> create_straight(const R2C2::Vector &, const R2C2::Angle &, float, float);
 
 public:
index 8ac35e82274f58a5c913fa7e346a946d8ed01446..6c917b1824b9c585039fad2d24119e2ca3a8661a 100644 (file)
@@ -11,8 +11,7 @@ using namespace R2C2;
 using namespace Msp;
 
 Manipulator::Manipulator(Designer &d, Input::Mouse &m, const set<Object *> &objs):
-       Tool(d, m),
-       accepted(false)
+       Tool(d, m)
 {
        set<Object *> pending = objs;
        while(!pending.empty())
@@ -57,38 +56,30 @@ Manipulator::~Manipulator()
        }
 }
 
-void Manipulator::button_press(unsigned btn)
+void Manipulator::finish()
 {
-       if(btn==3)
-               set_done();
-       else if(btn==1)
+       set<Object *> object_set;
+       for(ObjectArray::iterator i=objects.begin(); i!=objects.end(); ++i)
+               object_set.insert(i->object);
+       for(ObjectArray::iterator i=objects.begin(); i!=objects.end(); ++i)
        {
-               set<Object *> object_set;
-               for(ObjectArray::iterator i=objects.begin(); i!=objects.end(); ++i)
-                       object_set.insert(i->object);
-               for(ObjectArray::iterator i=objects.begin(); i!=objects.end(); ++i)
-               {
-                       unsigned nls = i->object->get_n_link_slots();
-                       for(unsigned j=0; j<nls; ++j)
-                               if(Object *link = i->object->get_link(j))
-                                       if(!object_set.count(link))
-                                               i->object->break_link(j);
-               }
-
-               const set<Track *> &ltracks = designer.get_layout().get_all<Track>();
-               for(set<Track *>::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
-               {
-                       bool ok = true;
-                       for(vector<MObject>::iterator j=objects.begin(); (j!=objects.end() && ok); ++j)
-                               ok = (j->object!=*i);
-                       if(!ok) continue;
+               unsigned nls = i->object->get_n_link_slots();
+               for(unsigned j=0; j<nls; ++j)
+                       if(Object *link = i->object->get_link(j))
+                               if(!object_set.count(link))
+                                       i->object->break_link(j);
+       }
 
-                       for(vector<MObject>::iterator j=objects.begin(); j!=objects.end(); ++j)
-                               j->object->link_to(**i);
-               }
+       const set<Track *> &ltracks = designer.get_layout().get_all<Track>();
+       for(set<Track *>::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
+       {
+               bool ok = true;
+               for(vector<MObject>::iterator j=objects.begin(); (j!=objects.end() && ok); ++j)
+                       ok = (j->object!=*i);
+               if(!ok) continue;
 
-               accepted = true;
-               set_done();
+               for(vector<MObject>::iterator j=objects.begin(); j!=objects.end(); ++j)
+                       j->object->link_to(**i);
        }
 }
 
index d48645c743ddcbcbe5e5001f0038bed1b805870e..efbdfa5fba351bf02bfff552e315a06d22f25ccb 100644 (file)
@@ -28,14 +28,13 @@ protected:
 
        ObjectArray objects;
        R2C2::Vector center;
-       bool accepted;
 
 public:
        Manipulator(Designer &, Msp::Input::Mouse &, const std::set<R2C2::Object *> &);
        virtual ~Manipulator();
 
 private:
-       virtual void button_press(unsigned);
+       virtual void finish();
 };
 
 #endif
index 5c2d84938a1c19c3c3a50156f4ed3547eb9d190b..2857b0d7f631834c177a464a1788d71b787f01b6 100644 (file)
@@ -43,17 +43,12 @@ void Measure::button_press(unsigned btn)
                        update_mesh();
                }
                else
-               {
-                       done = true;
-                       signal_done.emit();
-               }
+                       set_done(true);
        }
 }
 
-void Measure::axis_motion(unsigned axis, float x, float y)
+void Measure::pointer_motion()
 {
-       Tool::axis_motion(axis, x, y);
-
        Snap sn = start;
        sn.position = ground_pointer;
        snap_to_tracks(sn);
index e5f109181e379d92927f352ea06f20dd0e4a3dfa..9a270d94bea3cfeedb038c6cd2075d315206fd47 100644 (file)
@@ -26,9 +26,11 @@ public:
        float get_perpendicular_distance() const { return perp_dist; }
        const R2C2::Angle &get_angle_difference() const { return adiff; }
 
+private:
        virtual void button_press(unsigned);
-       virtual void axis_motion(unsigned, float, float);
+       virtual void pointer_motion();
 
+public:
        virtual void render(Msp::GL::Renderer &, const Msp::GL::Tag &) const;
 private:
        void update_mesh();
index 57d72928612e95792e4c5801c349ac97deb70b17..29d2857faab119edc68603746e58849b64b32173 100644 (file)
@@ -23,10 +23,8 @@ MoveTool::MoveTool(Designer &d, Input::Mouse &m, const set<Object *> &o):
                snap_targets.erase(i->object);
 }
 
-void MoveTool::axis_motion(unsigned axis, float value, float rel)
+void MoveTool::pointer_motion()
 {
-       Manipulator::axis_motion(axis, value, rel);
-
        Vector offset = center+ground_pointer-origin;
        for(vector<MObject>::iterator i=objects.begin(); i!=objects.end(); ++i)
        {
index 2f243fefdd5228b3b2a630a610c0f7a751565a54..40665c5bfa5eba6f15b1dcc15fca1a3d166f32ea 100644 (file)
@@ -23,7 +23,7 @@ private:
 public:
        MoveTool(Designer &, Msp::Input::Mouse &, const std::set<R2C2::Object *> &);
 
-       virtual void axis_motion(unsigned, float, float);
+       virtual void pointer_motion();
 };
 
 #endif
index 5931e831d3cc17d829841026497f5cb03cdb97d2..63f237197ccf96680289418a38510a0d66221b8c 100644 (file)
@@ -9,10 +9,8 @@ RotateTool::RotateTool(Designer &d, Input::Mouse &m, const set<Object *> &o):
        origin(Geometry::atan2(ground_pointer.y-center.y, ground_pointer.x-center.x))
 { }
 
-void RotateTool::axis_motion(unsigned axis, float value, float rel)
+void RotateTool::pointer_motion()
 {
-       Manipulator::axis_motion(axis, value, rel);
-
        Angle a = Geometry::atan2(ground_pointer.y-center.y, ground_pointer.x-center.x);
        angle += a-origin;
        origin = a;
index dceaa80d031ae1a8b8c8bfd4b0aab44bc7ab87cb..3a2ce5dd27258adf916b50416e6ba913d155dcf9 100644 (file)
@@ -12,7 +12,7 @@ private:
 public:
        RotateTool(Designer &, Msp::Input::Mouse &, const std::set<R2C2::Object *> &);
 
-       virtual void axis_motion(unsigned, float, float);
+       virtual void pointer_motion();
 };
 
 #endif
index 869724e68da578e2a7552f6f56fb81ddd13a0744..96a78bf6aaefdb1f845e42f3154808d5deefcddc 100644 (file)
@@ -13,7 +13,7 @@ SlopeTool::SlopeTool(Designer &d, Input::Mouse &m, const set<Object *> &objects)
                if(!dynamic_cast<Track *>(*i) || (*i)->get_n_link_slots()!=2)
                {
                        set_status("Must have linear tracks only");
-                       set_done();
+                       set_done(false);
                        return;
                }
 
index a0419d04f8699018a6ac7c23998a4ea1c6b2f022..ffd6e6b6c5385201a1f5041eab1bbd27a71aafb5 100644 (file)
@@ -6,7 +6,8 @@ using namespace Msp;
 
 Tool::Tool(Designer &d, Input::Mouse &mouse):
        designer(d),
-       done(false)
+       done(false),
+       accepted(false)
 {
        mouse.signal_button_press.connect(sigc::bind_return(sigc::mem_fun(this, &Tool::button_press), false));
        mouse.signal_axis_motion.connect(sigc::bind_return(sigc::mem_fun(this, &Tool::axis_motion), false));
@@ -22,12 +23,24 @@ void Tool::set_status(const std::string &s)
        signal_status.emit(status);
 }
 
-void Tool::set_done()
+void Tool::set_done(bool a)
 {
+       accepted = a;
        done = true;
        signal_done.emit();
 }
 
+void Tool::button_press(unsigned btn)
+{
+       if(btn==1)
+       {
+               finish();
+               set_done(true);
+       }
+       else if(btn==3)
+               set_done(false);
+}
+
 void Tool::axis_motion(unsigned axis, float value, float)
 {
        if(axis==0)
@@ -35,4 +48,6 @@ void Tool::axis_motion(unsigned axis, float value, float)
        else if(axis==1)
                pointer.y = value;
        ground_pointer = designer.map_pointer_to_ground(pointer);
+
+       pointer_motion();
 }
index bfadfc03feae9154b1ab1215e103add88ed03d7e..137ac5c326e26fd767fd14decc604a26a0eeae65 100644 (file)
@@ -22,6 +22,7 @@ protected:
        R2C2::Vector ground_pointer;
        std::string status;
        bool done;
+       bool accepted;
 
        Tool(Designer &, Msp::Input::Mouse &);
 public:
@@ -29,14 +30,18 @@ public:
 
 protected:
        void set_status(const std::string &);
-       void set_done();
+       void set_done(bool);
 public:
        const std::string &get_status() const { return status; }
        bool is_done() const { return done; }
 
-       virtual void button_press(unsigned) { }
+protected:
+       virtual void button_press(unsigned);
        virtual void axis_motion(unsigned, float, float);
+       virtual void pointer_motion() { }
+       virtual void finish() { }
 
+public:
        virtual void update_selection(Selection &) const { }
 };