1 /* === S Y N F I G ========================================================= */
2 /*! \file state_scale.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_scale.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 <synfigapp/main.h>
57 /* === U S I N G =========================================================== */
61 using namespace synfig;
62 using namespace studio;
64 /* === M A C R O S ========================================================= */
66 /* === G L O B A L S ======================================================= */
68 StateScale studio::state_scale;
70 /* === C L A S S E S & S T R U C T S ======================================= */
72 class DuckDrag_Scale : public DuckDrag_Base
75 synfig::Vector last_scale;
76 synfig::Vector drag_offset;
77 synfig::Vector center;
80 std::vector<synfig::Vector> positions;
88 void begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& begin);
89 bool end_duck_drag(Duckmatic* duckmatic);
90 void duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector);
94 class studio::StateScale_Context : public sigc::trackable
96 etl::handle<CanvasView> canvas_view_;
98 synfigapp::Settings& settings;
100 etl::handle<DuckDrag_Scale> duck_dragger_;
102 Gtk::Table options_table;
105 Gtk::CheckButton checkbutton_aspect_lock;
109 bool get_aspect_lock_flag()const { return checkbutton_aspect_lock.get_active(); }
110 void set_aspect_lock_flag(bool x) { return checkbutton_aspect_lock.set_active(x); refresh_aspect_lock_flag(); }
112 void refresh_aspect_lock_flag() { if(duck_dragger_)duck_dragger_->lock_aspect=get_aspect_lock_flag(); }
114 Smach::event_result event_refresh_tool_options(const Smach::event& x);
116 void refresh_tool_options();
118 StateScale_Context(CanvasView* canvas_view);
120 ~StateScale_Context();
122 const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
123 etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
124 synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
125 WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
127 void load_settings();
128 void save_settings();
129 }; // END of class StateScale_Context
131 /* === M E T H O D S ======================================================= */
133 StateScale::StateScale():
134 Smach::state<StateScale_Context>("scale")
136 insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateScale_Context::event_refresh_tool_options));
139 StateScale::~StateScale()
144 StateScale_Context::load_settings()
148 if(settings.get_value("scale.lock_aspect",value) && value=="0")
149 set_aspect_lock_flag(false);
151 set_aspect_lock_flag(true);
155 StateScale_Context::save_settings()
157 settings.set_value("scale.lock_aspect",get_aspect_lock_flag()?"1":"0");
160 StateScale_Context::StateScale_Context(CanvasView* canvas_view):
161 canvas_view_(canvas_view),
162 settings(synfigapp::Main::get_selected_input_device()->settings()),
163 duck_dragger_(new DuckDrag_Scale()),
164 checkbutton_aspect_lock(_("Lock Aspect Ratio"))
166 // Set up the tool options dialog
167 options_table.attach(*manage(new Gtk::Label(_("Scale Tool"))), 0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
168 options_table.attach(checkbutton_aspect_lock, 0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
170 checkbutton_aspect_lock.signal_toggled().connect(sigc::mem_fun(*this,&StateScale_Context::refresh_aspect_lock_flag));
172 options_table.show_all();
173 refresh_tool_options();
174 App::dialog_tool_options->present();
176 get_work_area()->set_allow_layer_clicks(true);
177 get_work_area()->set_duck_dragger(duck_dragger_);
179 // get_canvas_view()->work_area->set_cursor(Gdk::CROSSHAIR);
180 get_canvas_view()->work_area->reset_cursor();
182 App::toolbox->refresh();
184 set_aspect_lock_flag(true);
189 StateScale_Context::refresh_tool_options()
191 App::dialog_tool_options->clear();
192 App::dialog_tool_options->set_widget(options_table);
193 App::dialog_tool_options->set_local_name(_("Scale Tool"));
194 App::dialog_tool_options->set_name("scale");
198 StateScale_Context::event_refresh_tool_options(const Smach::event& /*x*/)
200 refresh_tool_options();
201 return Smach::RESULT_ACCEPT;
204 StateScale_Context::~StateScale_Context()
208 get_work_area()->clear_duck_dragger();
209 get_canvas_view()->work_area->reset_cursor();
211 App::dialog_tool_options->clear();
213 App::toolbox->refresh();
219 DuckDrag_Scale::DuckDrag_Scale():
225 #define EPSILON 0.0000001
229 DuckDrag_Scale::begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& offset)
231 last_scale=Vector(1,1);
232 const DuckList selected_ducks(duckmatic->get_selected_ducks());
233 DuckList::const_iterator iter;
235 //if(duckmatic->get_selected_ducks().size()<2)
242 drag_offset=duckmatic->find_duck(offset)->get_trans_point();
244 //snap=drag_offset-duckmatic->snap_point_to_grid(drag_offset);
245 //snap=offset-drag_offset;
249 Point vmin(100000000,100000000);
250 Point vmax(-100000000,-100000000);
251 //std::set<etl::handle<Duck> >::iterator iter;
254 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
256 Point p((*iter)->get_trans_point());
257 vmin[0]=min(vmin[0],p[0]);
258 vmin[1]=min(vmin[1],p[1]);
259 vmax[0]=max(vmax[0],p[0]);
260 vmax[1]=max(vmax[1],p[1]);
261 positions.push_back(p);
263 if((vmin-vmax).mag()<=EPSILON)
268 center=(vmin+vmax)*0.5;
273 DuckDrag_Scale::duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector)
275 const DuckList selected_ducks(duckmatic->get_selected_ducks());
276 DuckList::const_iterator iter;
281 //std::set<etl::handle<Duck> >::iterator iter;
282 synfig::Vector vect(duckmatic->snap_point_to_grid(vector)-center);
288 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
290 if(((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION))continue;
292 Vector p(positions[i]);
296 (*iter)->set_trans_point(p);
298 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
300 if(!((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION))continue;
302 Vector p(positions[i]);
306 (*iter)->set_trans_point(p);
313 if(abs(drag_offset[0]-center[0])>EPSILON)
314 vect[0]/=drag_offset[0]-center[0];
317 if(abs(drag_offset[1]-center[1])>EPSILON)
318 vect[1]/=drag_offset[1]-center[1];
324 //vect[0]=vect[1]=vect.mag()*0.707106781;
325 Real amount(vect.mag()/(drag_offset-center).mag());
326 vect[0]=vect[1]=amount;
329 if(vect[0]<EPSILON && vect[0]>-EPSILON)
331 if(vect[1]<EPSILON && vect[1]>-EPSILON)
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 p(positions[i]-center);
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 p(positions[i]-center);
355 (*iter)->set_trans_point(p);
363 DuckDrag_Scale::end_duck_drag(Duckmatic* duckmatic)
365 if(bad_drag)return false;
367 if((last_scale-Vector(1,1)).mag()>0.0001)
369 duckmatic->signal_edited_selected_ducks();
374 duckmatic->signal_user_click_selected_ducks(0);