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>
57 /* === U S I N G =========================================================== */
61 using namespace synfig;
62 using namespace studio;
64 /* === M A C R O S ========================================================= */
67 #define EPSILON 0.0000001
70 /* === G L O B A L S ======================================================= */
72 StateRotate studio::state_rotate;
74 /* === C L A S S E S & S T R U C T S ======================================= */
76 class DuckDrag_Rotate : public DuckDrag_Base
79 synfig::Vector last_rotate;
80 synfig::Vector drag_offset;
81 synfig::Vector center;
87 std::vector<synfig::Vector> positions;
94 etl::handle<CanvasView> canvas_view_;
97 void begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& begin);
98 bool end_duck_drag(Duckmatic* duckmatic);
99 void duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector);
101 etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
105 class studio::StateRotate_Context : public sigc::trackable
107 etl::handle<CanvasView> canvas_view_;
109 synfigapp::Settings& settings;
111 etl::handle<DuckDrag_Rotate> duck_dragger_;
113 Gtk::Table options_table;
115 Gtk::CheckButton checkbutton_scale;
119 bool get_scale_flag()const { return checkbutton_scale.get_active(); }
120 void set_scale_flag(bool x) { return checkbutton_scale.set_active(x); refresh_scale_flag(); }
123 Smach::event_result event_refresh_tool_options(const Smach::event& x);
125 void refresh_tool_options();
127 void refresh_scale_flag() { if(duck_dragger_)duck_dragger_->use_magnitude=get_scale_flag(); }
129 StateRotate_Context(CanvasView* canvas_view);
131 ~StateRotate_Context();
133 const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
134 etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
135 synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
136 WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
138 void load_settings();
139 void save_settings();
140 }; // END of class StateRotate_Context
142 /* === M E T H O D S ======================================================= */
144 StateRotate::StateRotate():
145 Smach::state<StateRotate_Context>("rotate")
147 insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateRotate_Context::event_refresh_tool_options));
150 StateRotate::~StateRotate()
155 StateRotate_Context::load_settings()
159 if(settings.get_value("rotate.scale",value) && value=="0")
160 set_scale_flag(false);
162 set_scale_flag(true);
166 StateRotate_Context::save_settings()
168 settings.set_value("rotate.scale",get_scale_flag()?"1":"0");
171 StateRotate_Context::StateRotate_Context(CanvasView* canvas_view):
172 canvas_view_(canvas_view),
173 settings(synfigapp::Main::get_selected_input_device()->settings()),
174 duck_dragger_(new DuckDrag_Rotate()),
175 checkbutton_scale(_("Allow Scale"))
177 duck_dragger_->canvas_view_=get_canvas_view();
179 // Set up the tool options dialog
180 options_table.attach(*manage(new Gtk::Label(_("Rotate Tool"))), 0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
181 options_table.attach(checkbutton_scale, 0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
183 checkbutton_scale.signal_toggled().connect(sigc::mem_fun(*this,&StateRotate_Context::refresh_scale_flag));
185 options_table.show_all();
186 refresh_tool_options();
187 //App::dialog_tool_options->set_widget(options_table);
188 App::dialog_tool_options->present();
190 get_work_area()->set_allow_layer_clicks(true);
191 get_work_area()->set_duck_dragger(duck_dragger_);
193 // get_canvas_view()->work_area->set_cursor(Gdk::CROSSHAIR);
194 get_canvas_view()->work_area->reset_cursor();
196 App::toolbox->refresh();
199 refresh_scale_flag();
203 StateRotate_Context::refresh_tool_options()
205 App::dialog_tool_options->clear();
206 App::dialog_tool_options->set_widget(options_table);
207 App::dialog_tool_options->set_local_name(_("Rotate Tool"));
208 App::dialog_tool_options->set_name("rotate");
212 StateRotate_Context::event_refresh_tool_options(const Smach::event& /*x*/)
214 refresh_tool_options();
215 return Smach::RESULT_ACCEPT;
218 StateRotate_Context::~StateRotate_Context()
222 get_work_area()->clear_duck_dragger();
223 get_canvas_view()->work_area->reset_cursor();
225 App::dialog_tool_options->clear();
227 App::toolbox->refresh();
233 DuckDrag_Rotate::DuckDrag_Rotate()
239 DuckDrag_Rotate::begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& offset)
241 last_rotate=Vector(1,1);
243 const DuckList selected_ducks(duckmatic->get_selected_ducks());
244 DuckList::const_iterator iter;
247 if(duckmatic->get_selected_ducks().size()<2)
255 drag_offset=duckmatic->find_duck(offset)->get_trans_point();
257 //snap=drag_offset-duckmatic->snap_point_to_grid(drag_offset);
258 //snap=offset-drag_offset;
262 Point vmin(100000000,100000000);
263 Point vmax(-100000000,-100000000);
264 //std::set<etl::handle<Duck> >::iterator iter;
267 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
269 Point p((*iter)->get_trans_point());
270 vmin[0]=min(vmin[0],p[0]);
271 vmin[1]=min(vmin[1],p[1]);
272 vmax[0]=max(vmax[0],p[0]);
273 vmax[1]=max(vmax[1],p[1]);
274 positions.push_back(p);
276 center=(vmin+vmax)*0.5;
277 if((vmin-vmax).mag()<=EPSILON)
283 synfig::Vector vect(offset-center);
284 original_angle=Angle::tan(vect[1],vect[0]);
285 original_mag=vect.mag();
290 DuckDrag_Rotate::duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector)
295 //std::set<etl::handle<Duck> >::iterator iter;
296 synfig::Vector vect(duckmatic->snap_point_to_grid(vector)-center+snap);
298 const DuckList selected_ducks(duckmatic->get_selected_ducks());
299 DuckList::const_iterator iter;
304 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
306 if((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION)continue;
308 Vector p(positions[i]);
312 (*iter)->set_trans_point(p);
314 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
316 if(!((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION))continue;
318 Vector p(positions[i]);
322 (*iter)->set_trans_point(p);
327 Angle::tan angle(vect[1],vect[0]);
328 angle=original_angle-angle;
329 Real mag(vect.mag()/original_mag);
330 Real sine(Angle::sin(angle).get());
331 Real cosine(Angle::cos(angle).get());
334 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
336 if((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION)continue;
338 Vector x(positions[i]-center),p;
340 p[0]=cosine*x[0]+sine*x[1];
341 p[1]=-sine*x[0]+cosine*x[1];
342 if(use_magnitude)p*=mag;
344 (*iter)->set_trans_point(p);
346 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
348 if(!((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION))continue;
350 Vector x(positions[i]-center),p;
352 p[0]=cosine*x[0]+sine*x[1];
353 p[1]=-sine*x[0]+cosine*x[1];
354 if(use_magnitude)p*=mag;
356 (*iter)->set_trans_point(p);
364 DuckDrag_Rotate::end_duck_drag(Duckmatic* duckmatic)
366 if(bad_drag)return false;
369 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Move Duck"));
370 duckmatic->signal_edited_selected_ducks();
374 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Rotate Ducks"));
376 if((last_rotate-Vector(1,1)).mag()>0.0001)
378 duckmatic->signal_edited_selected_ducks();
383 duckmatic->signal_user_click_selected_ducks(0);