1 /* === S Y N F I G ========================================================= */
2 /*! \file state_rotate.cpp
3 ** \brief Template File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
10 ** This package is free software; you can redistribute it and/or
11 ** modify it under the terms of the GNU General Public License as
12 ** published by the Free Software Foundation; either version 2 of
13 ** the License, or (at your option) any later version.
15 ** This package is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ** General Public License for more details.
21 /* ========================================================================= */
23 /* === H E A D E R S ======================================================= */
32 #include <gtkmm/dialog.h>
33 #include <gtkmm/entry.h>
35 #include <synfig/valuenode_dynamiclist.h>
36 #include <synfigapp/action_system.h>
38 #include "state_rotate.h"
39 #include "canvasview.h"
43 #include <synfigapp/action.h>
44 #include "event_mouse.h"
45 #include "event_layerclick.h"
47 #include "dialog_tooloptions.h"
48 #include <gtkmm/optionmenu.h>
50 #include <synfig/angle.h>
51 #include <synfigapp/main.h>
55 /* === U S I N G =========================================================== */
59 using namespace synfig;
60 using namespace studio;
62 /* === M A C R O S ========================================================= */
65 #define EPSILON 0.0000001
68 /* === G L O B A L S ======================================================= */
70 StateRotate studio::state_rotate;
72 /* === C L A S S E S & S T R U C T S ======================================= */
74 class DuckDrag_Rotate : public DuckDrag_Base
77 synfig::Vector last_rotate;
78 synfig::Vector drag_offset;
79 synfig::Vector center;
85 std::vector<synfig::Vector> positions;
92 etl::handle<CanvasView> canvas_view_;
95 void begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& begin);
96 bool end_duck_drag(Duckmatic* duckmatic);
97 void duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector);
99 etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
103 class studio::StateRotate_Context : public sigc::trackable
105 etl::handle<CanvasView> canvas_view_;
107 synfigapp::Settings& settings;
109 etl::handle<DuckDrag_Rotate> duck_dragger_;
111 Gtk::Table options_table;
113 Gtk::CheckButton checkbutton_scale;
117 bool get_scale_flag()const { return checkbutton_scale.get_active(); }
118 void set_scale_flag(bool x) { return checkbutton_scale.set_active(x); refresh_scale_flag(); }
121 Smach::event_result event_refresh_tool_options(const Smach::event& x);
123 void refresh_tool_options();
125 void refresh_scale_flag() { if(duck_dragger_)duck_dragger_->use_magnitude=get_scale_flag(); }
127 StateRotate_Context(CanvasView* canvas_view);
129 ~StateRotate_Context();
131 const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
132 etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
133 synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
134 WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
136 void load_settings();
137 void save_settings();
138 }; // END of class StateRotate_Context
140 /* === M E T H O D S ======================================================= */
142 StateRotate::StateRotate():
143 Smach::state<StateRotate_Context>("rotate")
145 insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateRotate_Context::event_refresh_tool_options));
148 StateRotate::~StateRotate()
153 StateRotate_Context::load_settings()
157 if(settings.get_value("rotate.scale",value) && value=="0")
158 set_scale_flag(false);
160 set_scale_flag(true);
164 StateRotate_Context::save_settings()
166 settings.set_value("rotate.scale",get_scale_flag()?"1":"0");
169 StateRotate_Context::StateRotate_Context(CanvasView* canvas_view):
170 canvas_view_(canvas_view),
171 settings(synfigapp::Main::get_selected_input_device()->settings()),
172 duck_dragger_(new DuckDrag_Rotate()),
173 checkbutton_scale(_("Allow Scale"))
175 duck_dragger_->canvas_view_=get_canvas_view();
177 // Set up the tool options dialog
178 //options_table.attach(*manage(new Gtk::Label(_("Rotate Tool"))), 0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
179 options_table.attach(checkbutton_scale, 0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
181 checkbutton_scale.signal_toggled().connect(sigc::mem_fun(*this,&StateRotate_Context::refresh_scale_flag));
183 options_table.show_all();
184 refresh_tool_options();
185 //App::dialog_tool_options->set_widget(options_table);
186 App::dialog_tool_options->present();
188 get_work_area()->set_allow_layer_clicks(true);
189 get_work_area()->set_duck_dragger(duck_dragger_);
191 // get_canvas_view()->work_area->set_cursor(Gdk::CROSSHAIR);
192 get_canvas_view()->work_area->reset_cursor();
194 App::toolbox->refresh();
197 refresh_scale_flag();
201 StateRotate_Context::refresh_tool_options()
203 App::dialog_tool_options->clear();
204 App::dialog_tool_options->set_widget(options_table);
205 App::dialog_tool_options->set_local_name(_("Rotate Tool"));
206 App::dialog_tool_options->set_name("rotate");
210 StateRotate_Context::event_refresh_tool_options(const Smach::event& /*x*/)
212 refresh_tool_options();
213 return Smach::RESULT_ACCEPT;
216 StateRotate_Context::~StateRotate_Context()
220 get_work_area()->clear_duck_dragger();
221 get_canvas_view()->work_area->reset_cursor();
223 App::dialog_tool_options->clear();
225 App::toolbox->refresh();
231 DuckDrag_Rotate::DuckDrag_Rotate()
237 DuckDrag_Rotate::begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& offset)
239 last_rotate=Vector(1,1);
241 const DuckList selected_ducks(duckmatic->get_selected_ducks());
242 DuckList::const_iterator iter;
245 if(duckmatic->get_selected_ducks().size()<2)
253 drag_offset=duckmatic->find_duck(offset)->get_trans_point();
255 //snap=drag_offset-duckmatic->snap_point_to_grid(drag_offset);
256 //snap=offset-drag_offset;
260 Point vmin(100000000,100000000);
261 Point vmax(-100000000,-100000000);
262 //std::set<etl::handle<Duck> >::iterator iter;
265 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
267 Point p((*iter)->get_trans_point());
268 vmin[0]=min(vmin[0],p[0]);
269 vmin[1]=min(vmin[1],p[1]);
270 vmax[0]=max(vmax[0],p[0]);
271 vmax[1]=max(vmax[1],p[1]);
272 positions.push_back(p);
274 center=(vmin+vmax)*0.5;
275 if((vmin-vmax).mag()<=EPSILON)
281 synfig::Vector vect(offset-center);
282 original_angle=Angle::tan(vect[1],vect[0]);
283 original_mag=vect.mag();
288 DuckDrag_Rotate::duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector)
293 //std::set<etl::handle<Duck> >::iterator iter;
294 synfig::Vector vect(duckmatic->snap_point_to_grid(vector)-center+snap);
296 const DuckList selected_ducks(duckmatic->get_selected_ducks());
297 DuckList::const_iterator iter;
302 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
304 if((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION)continue;
306 Vector p(positions[i]);
310 (*iter)->set_trans_point(p);
312 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
314 if(!((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION))continue;
316 Vector p(positions[i]);
320 (*iter)->set_trans_point(p);
325 Angle::tan angle(vect[1],vect[0]);
326 angle=original_angle-angle;
327 Real mag(vect.mag()/original_mag);
328 Real sine(Angle::sin(angle).get());
329 Real cosine(Angle::cos(angle).get());
332 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
334 if((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION)continue;
336 Vector x(positions[i]-center),p;
338 p[0]=cosine*x[0]+sine*x[1];
339 p[1]=-sine*x[0]+cosine*x[1];
340 if(use_magnitude)p*=mag;
342 (*iter)->set_trans_point(p);
344 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
346 if(!((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION))continue;
348 Vector x(positions[i]-center),p;
350 p[0]=cosine*x[0]+sine*x[1];
351 p[1]=-sine*x[0]+cosine*x[1];
352 if(use_magnitude)p*=mag;
354 (*iter)->set_trans_point(p);
362 DuckDrag_Rotate::end_duck_drag(Duckmatic* duckmatic)
364 if(bad_drag)return false;
367 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Move Duck"));
368 duckmatic->signal_edited_selected_ducks();
372 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Rotate Ducks"));
374 if((last_rotate-Vector(1,1)).mag()>0.0001)
376 duckmatic->signal_edited_selected_ducks();
381 duckmatic->signal_user_click_selected_ducks(0);