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
9 ** Copyright (c) 2008 Chris Moore
11 ** This package is free software; you can redistribute it and/or
12 ** modify it under the terms of the GNU General Public License as
13 ** published by the Free Software Foundation; either version 2 of
14 ** the License, or (at your option) any later version.
16 ** This package is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ** General Public License for more details.
22 /* ========================================================================= */
24 /* === H E A D E R S ======================================================= */
33 #include <gtkmm/dialog.h>
34 #include <gtkmm/entry.h>
36 #include <synfig/valuenode_dynamiclist.h>
37 #include <synfigapp/action_system.h>
39 #include "state_rotate.h"
40 #include "canvasview.h"
44 #include <synfigapp/action.h>
45 #include "event_mouse.h"
46 #include "event_layerclick.h"
48 #include "dialog_tooloptions.h"
49 #include <gtkmm/optionmenu.h>
51 #include <synfig/angle.h>
52 #include <synfigapp/main.h>
58 /* === U S I N G =========================================================== */
62 using namespace synfig;
63 using namespace studio;
65 /* === M A C R O S ========================================================= */
68 #define EPSILON 0.0000001
71 /* === G L O B A L S ======================================================= */
73 StateRotate studio::state_rotate;
75 /* === C L A S S E S & S T R U C T S ======================================= */
77 class DuckDrag_Rotate : public DuckDrag_Base
80 synfig::Vector last_rotate;
81 synfig::Vector drag_offset;
82 synfig::Vector center;
88 std::vector<synfig::Vector> positions;
95 etl::handle<CanvasView> canvas_view_;
98 void begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& begin);
99 bool end_duck_drag(Duckmatic* duckmatic);
100 void duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector);
102 etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
106 class studio::StateRotate_Context : public sigc::trackable
108 etl::handle<CanvasView> canvas_view_;
110 synfigapp::Settings& settings;
112 etl::handle<DuckDrag_Rotate> duck_dragger_;
114 Gtk::Table options_table;
116 Gtk::CheckButton checkbutton_scale;
120 bool get_scale_flag()const { return checkbutton_scale.get_active(); }
121 void set_scale_flag(bool x) { return checkbutton_scale.set_active(x); refresh_scale_flag(); }
124 Smach::event_result event_refresh_tool_options(const Smach::event& x);
126 void refresh_tool_options();
128 void refresh_scale_flag() { if(duck_dragger_)duck_dragger_->use_magnitude=get_scale_flag(); }
130 StateRotate_Context(CanvasView* canvas_view);
132 ~StateRotate_Context();
134 const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
135 etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
136 synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
137 WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
139 void load_settings();
140 void save_settings();
141 }; // END of class StateRotate_Context
143 /* === M E T H O D S ======================================================= */
145 StateRotate::StateRotate():
146 Smach::state<StateRotate_Context>("rotate")
148 insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateRotate_Context::event_refresh_tool_options));
151 StateRotate::~StateRotate()
156 StateRotate_Context::load_settings()
160 if(settings.get_value("rotate.scale",value) && value=="0")
161 set_scale_flag(false);
163 set_scale_flag(true);
167 StateRotate_Context::save_settings()
169 settings.set_value("rotate.scale",get_scale_flag()?"1":"0");
172 StateRotate_Context::StateRotate_Context(CanvasView* canvas_view):
173 canvas_view_(canvas_view),
174 settings(synfigapp::Main::get_selected_input_device()->settings()),
175 duck_dragger_(new DuckDrag_Rotate()),
176 checkbutton_scale(_("Allow Scale"))
178 duck_dragger_->canvas_view_=get_canvas_view();
180 // Set up the tool options dialog
181 options_table.attach(*manage(new Gtk::Label(_("Rotate Tool"))), 0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
182 options_table.attach(checkbutton_scale, 0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
184 checkbutton_scale.signal_toggled().connect(sigc::mem_fun(*this,&StateRotate_Context::refresh_scale_flag));
186 options_table.show_all();
187 refresh_tool_options();
188 //App::dialog_tool_options->set_widget(options_table);
189 App::dialog_tool_options->present();
191 get_work_area()->set_allow_layer_clicks(true);
192 get_work_area()->set_duck_dragger(duck_dragger_);
194 // get_canvas_view()->work_area->set_cursor(Gdk::CROSSHAIR);
195 get_canvas_view()->work_area->reset_cursor();
197 App::toolbox->refresh();
200 refresh_scale_flag();
204 StateRotate_Context::refresh_tool_options()
206 App::dialog_tool_options->clear();
207 App::dialog_tool_options->set_widget(options_table);
208 App::dialog_tool_options->set_local_name(_("Rotate Tool"));
209 App::dialog_tool_options->set_name("rotate");
213 StateRotate_Context::event_refresh_tool_options(const Smach::event& /*x*/)
215 refresh_tool_options();
216 return Smach::RESULT_ACCEPT;
219 StateRotate_Context::~StateRotate_Context()
223 get_work_area()->clear_duck_dragger();
224 get_canvas_view()->work_area->reset_cursor();
226 App::dialog_tool_options->clear();
228 App::toolbox->refresh();
234 DuckDrag_Rotate::DuckDrag_Rotate()
240 DuckDrag_Rotate::begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& offset)
242 last_rotate=Vector(1,1);
244 const DuckList selected_ducks(duckmatic->get_selected_ducks());
245 DuckList::const_iterator iter;
248 if(duckmatic->get_selected_ducks().size()<2)
256 drag_offset=duckmatic->find_duck(offset)->get_trans_point();
258 //snap=drag_offset-duckmatic->snap_point_to_grid(drag_offset);
259 //snap=offset-drag_offset;
263 Point vmin(100000000,100000000);
264 Point vmax(-100000000,-100000000);
265 //std::set<etl::handle<Duck> >::iterator iter;
268 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
270 Point p((*iter)->get_trans_point());
271 vmin[0]=min(vmin[0],p[0]);
272 vmin[1]=min(vmin[1],p[1]);
273 vmax[0]=max(vmax[0],p[0]);
274 vmax[1]=max(vmax[1],p[1]);
275 positions.push_back(p);
277 center=(vmin+vmax)*0.5;
278 if((vmin-vmax).mag()<=EPSILON)
284 synfig::Vector vect(offset-center);
285 original_angle=Angle::tan(vect[1],vect[0]);
286 original_mag=vect.mag();
291 DuckDrag_Rotate::duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector)
296 //std::set<etl::handle<Duck> >::iterator iter;
297 synfig::Vector vect(duckmatic->snap_point_to_grid(vector)-center+snap);
299 const DuckList selected_ducks(duckmatic->get_selected_ducks());
300 DuckList::const_iterator iter;
305 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
307 if((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION)continue;
309 Vector p(positions[i]);
313 (*iter)->set_trans_point(p);
315 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
317 if(!((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION))continue;
319 Vector p(positions[i]);
323 (*iter)->set_trans_point(p);
328 Angle::tan angle(vect[1],vect[0]);
329 angle=original_angle-angle;
330 Real mag(vect.mag()/original_mag);
331 Real sine(Angle::sin(angle).get());
332 Real cosine(Angle::cos(angle).get());
335 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
337 if((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION)continue;
339 Vector x(positions[i]-center),p;
341 p[0]=cosine*x[0]+sine*x[1];
342 p[1]=-sine*x[0]+cosine*x[1];
343 if(use_magnitude)p*=mag;
345 (*iter)->set_trans_point(p);
347 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
349 if(!((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION))continue;
351 Vector x(positions[i]-center),p;
353 p[0]=cosine*x[0]+sine*x[1];
354 p[1]=-sine*x[0]+cosine*x[1];
355 if(use_magnitude)p*=mag;
357 (*iter)->set_trans_point(p);
365 DuckDrag_Rotate::end_duck_drag(Duckmatic* duckmatic)
367 if(bad_drag)return false;
370 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Move Duck"));
371 duckmatic->signal_edited_selected_ducks();
375 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Rotate Ducks"));
377 if((last_rotate-Vector(1,1)).mag()>0.0001)
379 duckmatic->signal_edited_selected_ducks();
384 duckmatic->signal_user_click_selected_ducks(0);