1 /* === S Y N F I G ========================================================= */
2 /*! \file state_smooth_move.cpp
3 ** \brief Template File
5 ** $Id: state_smoothmove.cpp,v 1.1.1.1 2005/01/07 03:34:37 darco Exp $
8 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
10 ** This software and associated documentation
11 ** are CONFIDENTIAL and PROPRIETARY property of
12 ** the above-mentioned copyright holder.
14 ** You may not copy, print, publish, or in any
15 ** other way distribute this software without
16 ** a prior written agreement with
17 ** the copyright holder.
20 /* ========================================================================= */
22 /* === H E A D E R S ======================================================= */
31 #include <gtkmm/dialog.h>
32 #include <gtkmm/entry.h>
34 #include <synfig/valuenode_dynamiclist.h>
35 #include <synfigapp/action_system.h>
37 #include "state_smoothmove.h"
38 #include "canvasview.h"
42 #include <synfigapp/action.h>
43 #include "event_mouse.h"
44 #include "event_layerclick.h"
46 #include "dialog_tooloptions.h"
47 #include <gtkmm/optionmenu.h>
49 #include "onemoment.h"
50 #include <synfigapp/main.h>
54 /* === U S I N G =========================================================== */
58 using namespace synfig;
59 using namespace studio;
61 /* === M A C R O S ========================================================= */
63 /* === G L O B A L S ======================================================= */
65 StateSmoothMove studio::state_smooth_move;
67 /* === C L A S S E S & S T R U C T S ======================================= */
69 class DuckDrag_SmoothMove : public DuckDrag_Base
73 synfig::Vector last_translate_;
74 synfig::Vector drag_offset_;
77 std::vector<synfig::Vector> last_;
78 std::vector<synfig::Vector> positions;
81 DuckDrag_SmoothMove();
82 void begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& begin);
83 bool end_duck_drag(Duckmatic* duckmatic);
84 void duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector);
86 void set_radius(float x) { radius=x; }
87 float get_radius()const { return radius; }
91 class studio::StateSmoothMove_Context : public sigc::trackable
93 etl::handle<CanvasView> canvas_view_;
95 //Duckmatic::Push duckmatic_push;
97 synfigapp::Settings& settings;
99 etl::handle<DuckDrag_SmoothMove> duck_dragger_;
101 Gtk::Table options_table;
103 Gtk::Adjustment adj_radius;
104 Gtk::SpinButton spin_radius;
109 float get_radius()const { return adj_radius.get_value(); }
110 void set_radius(float x) { return adj_radius.set_value(x); }
112 void refresh_radius() { duck_dragger_->set_radius(get_radius()*pressure); }
114 Smach::event_result event_stop_handler(const Smach::event& x);
116 Smach::event_result event_refresh_tool_options(const Smach::event& x);
118 void refresh_tool_options();
120 StateSmoothMove_Context(CanvasView* canvas_view);
122 ~StateSmoothMove_Context();
124 const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
125 etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
126 synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
127 WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
129 void load_settings();
130 void save_settings();
131 }; // END of class StateSmoothMove_Context
133 /* === M E T H O D S ======================================================= */
135 StateSmoothMove::StateSmoothMove():
136 Smach::state<StateSmoothMove_Context>("smooth_move")
138 insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateSmoothMove_Context::event_refresh_tool_options));
141 StateSmoothMove::~StateSmoothMove()
146 StateSmoothMove_Context::load_settings()
150 if(settings.get_value("smooth_move.radius",value))
151 set_radius(atof(value.c_str()));
157 StateSmoothMove_Context::save_settings()
159 settings.set_value("smooth_move.radius",strprintf("%f",get_radius()));
162 StateSmoothMove_Context::StateSmoothMove_Context(CanvasView* canvas_view):
163 canvas_view_(canvas_view),
164 // duckmatic_push(get_work_area()),
165 settings(synfigapp::Main::get_selected_input_device()->settings()),
166 duck_dragger_(new DuckDrag_SmoothMove()),
167 adj_radius(1,0,100000,0.01,0.1),
168 spin_radius(adj_radius,0.1,3)
172 // Set up the tool options dialog
173 //options_table.attach(*manage(new Gtk::Label(_("SmoothMove Tool"))), 0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
175 options_table.attach(*manage(new Gtk::Label(_("Radius"))), 0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
176 options_table.attach(spin_radius, 0, 2, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
178 spin_radius.signal_value_changed().connect(sigc::mem_fun(*this,&StateSmoothMove_Context::refresh_radius));
180 options_table.show_all();
181 refresh_tool_options();
182 //App::dialog_tool_options->set_widget(options_table);
183 App::dialog_tool_options->present();
185 get_work_area()->allow_layer_clicks=true;
186 get_work_area()->set_duck_dragger(duck_dragger_);
188 App::toolbox->refresh();
190 // get_canvas_view()->work_area->set_cursor(Gdk::CROSSHAIR);
191 get_canvas_view()->work_area->reset_cursor();
197 StateSmoothMove_Context::refresh_tool_options()
199 App::dialog_tool_options->clear();
200 App::dialog_tool_options->set_widget(options_table);
201 App::dialog_tool_options->set_local_name(_("Smooth Move"));
202 App::dialog_tool_options->set_name("smooth_move");
206 StateSmoothMove_Context::event_refresh_tool_options(const Smach::event& x)
208 refresh_tool_options();
209 return Smach::RESULT_ACCEPT;
212 StateSmoothMove_Context::~StateSmoothMove_Context()
216 get_work_area()->clear_duck_dragger();
217 get_canvas_view()->work_area->reset_cursor();
219 App::dialog_tool_options->clear();
221 App::toolbox->refresh();
227 DuckDrag_SmoothMove::DuckDrag_SmoothMove():radius(1.0f)
232 DuckDrag_SmoothMove::begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& offset)
234 last_translate_=Vector(0,0);
235 drag_offset_=duckmatic->find_duck(offset)->get_trans_point();
237 //snap=drag_offset-duckmatic->snap_point_to_grid(drag_offset);
238 //snap=offset-drag_offset_;
243 const DuckList selected_ducks(duckmatic->get_selected_ducks());
244 DuckList::const_iterator iter;
246 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
248 last_.push_back(Vector(0,0));
249 positions.push_back((*iter)->get_trans_point());
254 DuckDrag_SmoothMove::duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector)
256 const DuckList selected_ducks(duckmatic->get_selected_ducks());
257 DuckList::const_iterator iter;
258 synfig::Vector vect(duckmatic->snap_point_to_grid(vector)-drag_offset_+snap);
262 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
264 if(((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION))continue;
265 Point p(positions[i]);
267 float dist(1.0f-(p-drag_offset_).mag()/get_radius());
272 (*iter)->set_trans_point(p+last_[i]);
275 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
277 if(!((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION))continue;
278 Point p(positions[i]);
280 float dist(1.0f-(p-drag_offset_).mag()/get_radius());
285 (*iter)->set_trans_point(p+last_[i]);
288 last_translate_=vect;
293 DuckDrag_SmoothMove::end_duck_drag(Duckmatic* duckmatic)
295 //synfig::info("end_duck_drag(): Diff= %f",last_translate_.mag());
296 if(last_translate_.mag()>0.0001)
298 const DuckList selected_ducks(duckmatic->get_selected_ducks());
299 DuckList::const_iterator iter;
303 smart_ptr<OneMoment> wait;if(selected_ducks.size()>20)wait.spawn();
305 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
307 if(last_[i].mag()>0.0001)
308 if(!(*iter)->signal_edited()((*iter)->get_point()))
310 throw String("Bad Move");
313 //duckmatic->get_selected_ducks()=new_set;
314 //duckmatic->refresh_selected_ducks();
319 duckmatic->signal_user_click_selected_ducks(0);