From: Mikko Rasa Date: Sat, 24 Aug 2013 08:14:21 +0000 (+0300) Subject: Move common accept/cancel handling to the Tool class X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=0c0a16e5f0f41efddb6f3ff920e54a5a11e1841d;p=r2c2.git Move common accept/cancel handling to the Tool class --- diff --git a/source/designer/extendtool.cpp b/source/designer/extendtool.cpp index e35b4ab..a5dea81 100644 --- a/source/designer/extendtool.cpp +++ b/source/designer/extendtool.cpp @@ -8,8 +8,7 @@ using namespace Msp; using namespace R2C2; ExtendTool::ExtendTool(Designer &d, Input::Mouse &m, const set &objects): - Tool(d, m), - accepted(false) + Tool(d, m) { for(set::const_iterator i=objects.begin(); i!=objects.end(); ++i) if(Track *t = dynamic_cast(*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::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::const_iterator i=unlinked_endpoints.begin(); i!=unlinked_endpoints.end(); ++i) + if(extend_tracks.front()->link_to(**i)) + break; +} + vector ExtendTool::create_straight(const Vector &start, const Angle &dir, float length, float limit) { const Catalogue::TrackMap &track_types = designer.get_catalogue().get_tracks(); diff --git a/source/designer/extendtool.h b/source/designer/extendtool.h index 8e14fcf..8a7ebeb 100644 --- a/source/designer/extendtool.h +++ b/source/designer/extendtool.h @@ -12,7 +12,6 @@ class ExtendTool: public Tool private: std::vector unlinked_endpoints; std::vector extend_tracks; - bool accepted; public: ExtendTool(Designer &, Msp::Input::Mouse &, const std::set &); @@ -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 create_straight(const R2C2::Vector &, const R2C2::Angle &, float, float); public: diff --git a/source/designer/manipulator.cpp b/source/designer/manipulator.cpp index 8ac35e8..6c917b1 100644 --- a/source/designer/manipulator.cpp +++ b/source/designer/manipulator.cpp @@ -11,8 +11,7 @@ using namespace R2C2; using namespace Msp; Manipulator::Manipulator(Designer &d, Input::Mouse &m, const set &objs): - Tool(d, m), - accepted(false) + Tool(d, m) { set 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_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_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; jobject->get_link(j)) - if(!object_set.count(link)) - i->object->break_link(j); - } - - const set <racks = designer.get_layout().get_all(); - for(set::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i) - { - bool ok = true; - for(vector::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; jobject->get_link(j)) + if(!object_set.count(link)) + i->object->break_link(j); + } - for(vector::iterator j=objects.begin(); j!=objects.end(); ++j) - j->object->link_to(**i); - } + const set <racks = designer.get_layout().get_all(); + for(set::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i) + { + bool ok = true; + for(vector::iterator j=objects.begin(); (j!=objects.end() && ok); ++j) + ok = (j->object!=*i); + if(!ok) continue; - accepted = true; - set_done(); + for(vector::iterator j=objects.begin(); j!=objects.end(); ++j) + j->object->link_to(**i); } } diff --git a/source/designer/manipulator.h b/source/designer/manipulator.h index d48645c..efbdfa5 100644 --- a/source/designer/manipulator.h +++ b/source/designer/manipulator.h @@ -28,14 +28,13 @@ protected: ObjectArray objects; R2C2::Vector center; - bool accepted; public: Manipulator(Designer &, Msp::Input::Mouse &, const std::set &); virtual ~Manipulator(); private: - virtual void button_press(unsigned); + virtual void finish(); }; #endif diff --git a/source/designer/measure.cpp b/source/designer/measure.cpp index 5c2d849..2857b0d 100644 --- a/source/designer/measure.cpp +++ b/source/designer/measure.cpp @@ -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); diff --git a/source/designer/measure.h b/source/designer/measure.h index e5f1091..9a270d9 100644 --- a/source/designer/measure.h +++ b/source/designer/measure.h @@ -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(); diff --git a/source/designer/movetool.cpp b/source/designer/movetool.cpp index 57d7292..29d2857 100644 --- a/source/designer/movetool.cpp +++ b/source/designer/movetool.cpp @@ -23,10 +23,8 @@ MoveTool::MoveTool(Designer &d, Input::Mouse &m, const set &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::iterator i=objects.begin(); i!=objects.end(); ++i) { diff --git a/source/designer/movetool.h b/source/designer/movetool.h index 2f243fe..40665c5 100644 --- a/source/designer/movetool.h +++ b/source/designer/movetool.h @@ -23,7 +23,7 @@ private: public: MoveTool(Designer &, Msp::Input::Mouse &, const std::set &); - virtual void axis_motion(unsigned, float, float); + virtual void pointer_motion(); }; #endif diff --git a/source/designer/rotatetool.cpp b/source/designer/rotatetool.cpp index 5931e83..63f2371 100644 --- a/source/designer/rotatetool.cpp +++ b/source/designer/rotatetool.cpp @@ -9,10 +9,8 @@ RotateTool::RotateTool(Designer &d, Input::Mouse &m, const set &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; diff --git a/source/designer/rotatetool.h b/source/designer/rotatetool.h index dceaa80..3a2ce5d 100644 --- a/source/designer/rotatetool.h +++ b/source/designer/rotatetool.h @@ -12,7 +12,7 @@ private: public: RotateTool(Designer &, Msp::Input::Mouse &, const std::set &); - virtual void axis_motion(unsigned, float, float); + virtual void pointer_motion(); }; #endif diff --git a/source/designer/slopetool.cpp b/source/designer/slopetool.cpp index 869724e..96a78bf 100644 --- a/source/designer/slopetool.cpp +++ b/source/designer/slopetool.cpp @@ -13,7 +13,7 @@ SlopeTool::SlopeTool(Designer &d, Input::Mouse &m, const set &objects) if(!dynamic_cast(*i) || (*i)->get_n_link_slots()!=2) { set_status("Must have linear tracks only"); - set_done(); + set_done(false); return; } diff --git a/source/designer/tool.cpp b/source/designer/tool.cpp index a0419d0..ffd6e6b 100644 --- a/source/designer/tool.cpp +++ b/source/designer/tool.cpp @@ -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(); } diff --git a/source/designer/tool.h b/source/designer/tool.h index bfadfc0..137ac5c 100644 --- a/source/designer/tool.h +++ b/source/designer/tool.h @@ -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 { } };