Changed the "tagrelease" and "tagstable" make targets to use subversion. Also increme...
[synfig.git] / synfig-studio / tags / stable / src / gtkmm / state_smoothmove.cpp
1 /* === S I N F G =========================================================== */
2 /*!     \file state_smooth_move.cpp
3 **      \brief Template File
4 **
5 **      $Id: state_smoothmove.cpp,v 1.1.1.1 2005/01/07 03:34:37 darco Exp $
6 **
7 **      \legal
8 **      Copyright (c) 2002 Robert B. Quattlebaum Jr.
9 **
10 **      This software and associated documentation
11 **      are CONFIDENTIAL and PROPRIETARY property of
12 **      the above-mentioned copyright holder.
13 **
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.
18 **      \endlegal
19 */
20 /* ========================================================================= */
21
22 /* === H E A D E R S ======================================================= */
23
24 #ifdef USING_PCH
25 #       include "pch.h"
26 #else
27 #ifdef HAVE_CONFIG_H
28 #       include <config.h>
29 #endif
30
31 #include <gtkmm/dialog.h>
32 #include <gtkmm/entry.h>
33
34 #include <sinfg/valuenode_dynamiclist.h>
35 #include <sinfgapp/action_system.h>
36
37 #include "state_smoothmove.h"
38 #include "canvasview.h"
39 #include "workarea.h"
40 #include "app.h"
41
42 #include <sinfgapp/action.h>
43 #include "event_mouse.h"
44 #include "event_layerclick.h"
45 #include "toolbox.h"
46 #include "dialog_tooloptions.h"
47 #include <gtkmm/optionmenu.h>
48 #include "duck.h"
49 #include "onemoment.h"
50 #include <sinfgapp/main.h>
51
52 #endif
53
54 /* === U S I N G =========================================================== */
55
56 using namespace std;
57 using namespace etl;
58 using namespace sinfg;
59 using namespace studio;
60
61 /* === M A C R O S ========================================================= */
62
63 /* === G L O B A L S ======================================================= */
64
65 StateSmoothMove studio::state_smooth_move;
66
67 /* === C L A S S E S & S T R U C T S ======================================= */
68
69 class DuckDrag_SmoothMove : public DuckDrag_Base
70 {
71         float radius;
72
73         sinfg::Vector last_translate_;
74         sinfg::Vector drag_offset_;
75         sinfg::Vector snap;
76         
77         std::vector<sinfg::Vector> last_;
78         std::vector<sinfg::Vector> positions;
79         
80 public:
81         DuckDrag_SmoothMove();
82         void begin_duck_drag(Duckmatic* duckmatic, const sinfg::Vector& begin);
83         bool end_duck_drag(Duckmatic* duckmatic);
84         void duck_drag(Duckmatic* duckmatic, const sinfg::Vector& vector);
85
86         void set_radius(float x) { radius=x; }
87         float get_radius()const { return radius; }      
88 };
89
90
91 class studio::StateSmoothMove_Context : public sigc::trackable
92 {
93         etl::handle<CanvasView> canvas_view_;
94         
95         //Duckmatic::Push duckmatic_push;
96         
97         sinfgapp::Settings& settings;
98
99         etl::handle<DuckDrag_SmoothMove> duck_dragger_;
100
101         Gtk::Table options_table;
102
103         Gtk::Adjustment  adj_radius;
104         Gtk::SpinButton  spin_radius;
105         
106         float pressure;
107         
108 public:
109         float get_radius()const { return adj_radius.get_value(); }
110         void set_radius(float x) { return adj_radius.set_value(x); }
111         
112         void refresh_radius() { duck_dragger_->set_radius(get_radius()*pressure); }
113         
114         Smach::event_result event_stop_handler(const Smach::event& x);
115
116         Smach::event_result event_refresh_tool_options(const Smach::event& x);
117
118         void refresh_tool_options();
119
120         StateSmoothMove_Context(CanvasView* canvas_view);
121
122         ~StateSmoothMove_Context();
123
124         const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
125         etl::handle<sinfgapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
126         sinfg::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
127         WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
128         
129         void load_settings();
130         void save_settings();
131 };      // END of class StateSmoothMove_Context
132
133 /* === M E T H O D S ======================================================= */
134
135 StateSmoothMove::StateSmoothMove():
136         Smach::state<StateSmoothMove_Context>("smooth_move")
137 {
138         insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateSmoothMove_Context::event_refresh_tool_options));
139 }       
140
141 StateSmoothMove::~StateSmoothMove()
142 {
143 }
144
145 void
146 StateSmoothMove_Context::load_settings()
147 {       
148         String value;
149
150         if(settings.get_value("smooth_move.radius",value))
151                 set_radius(atof(value.c_str()));
152         else
153                 set_radius(1.0f);
154 }
155
156 void
157 StateSmoothMove_Context::save_settings()
158 {       
159         settings.set_value("smooth_move.radius",strprintf("%f",get_radius()));
160 }
161
162 StateSmoothMove_Context::StateSmoothMove_Context(CanvasView* canvas_view):
163         canvas_view_(canvas_view),
164 //      duckmatic_push(get_work_area()),
165         settings(sinfgapp::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)
169 {
170         pressure=1.0f;
171         
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);  
174         
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);
177
178         spin_radius.signal_value_changed().connect(sigc::mem_fun(*this,&StateSmoothMove_Context::refresh_radius));
179         
180         options_table.show_all();
181         refresh_tool_options();
182         //App::dialog_tool_options->set_widget(options_table);
183         App::dialog_tool_options->present();
184         
185         get_work_area()->allow_layer_clicks=true;
186         get_work_area()->set_duck_dragger(duck_dragger_);
187
188         App::toolbox->refresh();
189
190 //      get_canvas_view()->work_area->set_cursor(Gdk::CROSSHAIR);
191         get_canvas_view()->work_area->reset_cursor();
192
193         load_settings();
194 }
195
196 void
197 StateSmoothMove_Context::refresh_tool_options()
198 {
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");
203 }
204
205 Smach::event_result
206 StateSmoothMove_Context::event_refresh_tool_options(const Smach::event& x)
207 {
208         refresh_tool_options();
209         return Smach::RESULT_ACCEPT;
210 }
211
212 StateSmoothMove_Context::~StateSmoothMove_Context()
213 {
214         save_settings();
215
216         get_work_area()->clear_duck_dragger();
217         get_canvas_view()->work_area->reset_cursor();
218
219         App::dialog_tool_options->clear();
220
221         App::toolbox->refresh();
222 }
223
224
225
226
227 DuckDrag_SmoothMove::DuckDrag_SmoothMove():radius(1.0f)
228 {
229 }
230
231 void
232 DuckDrag_SmoothMove::begin_duck_drag(Duckmatic* duckmatic, const sinfg::Vector& offset)
233 {
234         last_translate_=Vector(0,0);
235                 drag_offset_=duckmatic->find_duck(offset)->get_trans_point();
236
237                 //snap=drag_offset-duckmatic->snap_point_to_grid(drag_offset);
238                 //snap=offset-drag_offset_;
239                 snap=Vector(0,0);
240
241         last_.clear();
242         positions.clear();
243         const DuckList selected_ducks(duckmatic->get_selected_ducks());
244         DuckList::const_iterator iter;
245         int i;
246         for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
247         {
248                 last_.push_back(Vector(0,0));
249                 positions.push_back((*iter)->get_trans_point());
250         }
251 }
252
253 void
254 DuckDrag_SmoothMove::duck_drag(Duckmatic* duckmatic, const sinfg::Vector& vector)
255 {
256         const DuckList selected_ducks(duckmatic->get_selected_ducks());
257         DuckList::const_iterator iter;
258         sinfg::Vector vect(duckmatic->snap_point_to_grid(vector)-drag_offset_+snap);
259         
260         int i;
261         
262         for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
263         {
264                 if(((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION))continue;
265                 Point p(positions[i]);
266
267                 float dist(1.0f-(p-drag_offset_).mag()/get_radius());
268                 if(dist<0)
269                         dist=0;
270                 
271                 last_[i]=vect*dist;
272                 (*iter)->set_trans_point(p+last_[i]);
273         }
274
275         for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
276         {
277                 if(!((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION))continue;
278                 Point p(positions[i]);
279
280                 float dist(1.0f-(p-drag_offset_).mag()/get_radius());
281                 if(dist<0)
282                         dist=0;
283                 
284                 last_[i]=vect*dist;
285                 (*iter)->set_trans_point(p+last_[i]);
286         }
287         
288         last_translate_=vect;
289         //snap=Vector(0,0);
290 }
291
292 bool
293 DuckDrag_SmoothMove::end_duck_drag(Duckmatic* duckmatic)
294 {
295         //sinfg::info("end_duck_drag(): Diff= %f",last_translate_.mag());
296         if(last_translate_.mag()>0.0001)
297         {
298                 const DuckList selected_ducks(duckmatic->get_selected_ducks());
299                 DuckList::const_iterator iter;
300
301                 int i;
302                 
303                 smart_ptr<OneMoment> wait;if(selected_ducks.size()>20)wait.spawn();
304                         
305                 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
306                 {
307                         if(last_[i].mag()>0.0001)
308                                 if(!(*iter)->signal_edited()((*iter)->get_point()))
309                                 {
310                                         throw String("Bad Move");
311                                 }
312                 }
313                 //duckmatic->get_selected_ducks()=new_set;
314                 //duckmatic->refresh_selected_ducks();
315                 return true;
316         }
317         else
318         {
319                 duckmatic->signal_user_click_selected_ducks(0);
320                 return false;
321         }
322 }