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 "state_normal.h"
41 #include "canvasview.h"
45 #include <synfigapp/action.h>
46 #include "event_mouse.h"
47 #include "event_layerclick.h"
49 #include "docks/dialog_tooloptions.h"
50 #include <gtkmm/optionmenu.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 ========================================================= */
67 /* === G L O B A L S ======================================================= */
69 StateScale studio::state_scale;
71 /* === C L A S S E S & S T R U C T S ======================================= */
73 class DuckDrag_Scale : public DuckDrag_Base
76 synfig::Vector last_scale;
77 synfig::Vector drag_offset;
78 synfig::Vector center;
81 std::vector<synfig::Vector> positions;
89 void begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& begin);
90 bool end_duck_drag(Duckmatic* duckmatic);
91 void duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector);
95 class studio::StateScale_Context : public sigc::trackable
97 etl::handle<CanvasView> canvas_view_;
98 CanvasView::IsWorking is_working;
100 synfigapp::Settings& settings;
102 etl::handle<DuckDrag_Scale> duck_dragger_;
104 Gtk::Table options_table;
107 Gtk::CheckButton checkbutton_aspect_lock;
111 bool get_aspect_lock_flag()const { return checkbutton_aspect_lock.get_active(); }
112 void set_aspect_lock_flag(bool x) { return checkbutton_aspect_lock.set_active(x); refresh_aspect_lock_flag(); }
114 void refresh_aspect_lock_flag() { if(duck_dragger_)duck_dragger_->lock_aspect=get_aspect_lock_flag(); }
116 Smach::event_result event_stop_handler(const Smach::event& x);
117 Smach::event_result event_refresh_tool_options(const Smach::event& x);
119 void refresh_tool_options();
121 StateScale_Context(CanvasView* canvas_view);
123 ~StateScale_Context();
125 const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
126 etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
127 synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
128 WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
130 void load_settings();
131 void save_settings();
132 }; // END of class StateScale_Context
134 /* === M E T H O D S ======================================================= */
136 StateScale::StateScale():
137 Smach::state<StateScale_Context>("scale")
139 insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateScale_Context::event_refresh_tool_options));
140 insert(event_def(EVENT_STOP,&StateScale_Context::event_stop_handler));
143 StateScale::~StateScale()
148 StateScale_Context::load_settings()
152 SETTINGS_LOCALE_SAFE_AND_BACKUP
155 if(settings.get_value("scale.lock_aspect",value) && value=="0")
156 set_aspect_lock_flag(false);
158 set_aspect_lock_flag(true);
159 SETTINGS_LOCALE_RESTORE
163 synfig::warning("State Scale: Caught exception when attempting to load settings.");
168 StateScale_Context::save_settings()
172 SETTINGS_LOCALE_SAFE_AND_BACKUP
173 settings.set_value("scale.lock_aspect",get_aspect_lock_flag()?"1":"0");
174 SETTINGS_LOCALE_RESTORE
178 synfig::warning("State Scale: Caught exception when attempting to save settings.");
182 StateScale_Context::StateScale_Context(CanvasView* canvas_view):
183 canvas_view_(canvas_view),
184 is_working(*canvas_view),
185 settings(synfigapp::Main::get_selected_input_device()->settings()),
186 duck_dragger_(new DuckDrag_Scale()),
187 checkbutton_aspect_lock(_("Lock Aspect Ratio"))
189 // Set up the tool options dialog
190 options_table.attach(*manage(new Gtk::Label(_("Scale Tool"))), 0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
191 options_table.attach(checkbutton_aspect_lock, 0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
193 checkbutton_aspect_lock.signal_toggled().connect(sigc::mem_fun(*this,&StateScale_Context::refresh_aspect_lock_flag));
195 options_table.show_all();
196 refresh_tool_options();
197 App::dialog_tool_options->present();
199 get_work_area()->set_allow_layer_clicks(true);
200 get_work_area()->set_duck_dragger(duck_dragger_);
202 get_work_area()->set_cursor(Gdk::SIZING);
203 // get_work_area()->reset_cursor();
205 App::toolbox->refresh();
207 set_aspect_lock_flag(true);
212 StateScale_Context::refresh_tool_options()
214 App::dialog_tool_options->clear();
215 App::dialog_tool_options->set_widget(options_table);
216 App::dialog_tool_options->set_local_name(_("Scale Tool"));
217 App::dialog_tool_options->set_name("scale");
221 StateScale_Context::event_refresh_tool_options(const Smach::event& /*x*/)
223 refresh_tool_options();
224 return Smach::RESULT_ACCEPT;
228 StateScale_Context::event_stop_handler(const Smach::event& /*x*/)
231 return Smach::RESULT_OK;
234 StateScale_Context::~StateScale_Context()
238 get_work_area()->clear_duck_dragger();
239 get_work_area()->reset_cursor();
241 App::dialog_tool_options->clear();
243 App::toolbox->refresh();
249 DuckDrag_Scale::DuckDrag_Scale():
255 #define EPSILON 0.0000001
259 DuckDrag_Scale::begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& offset)
261 last_scale=Vector(1,1);
262 const DuckList selected_ducks(duckmatic->get_selected_ducks());
263 DuckList::const_iterator iter;
265 //if(duckmatic->get_selected_ducks().size()<2)
272 drag_offset=duckmatic->find_duck(offset)->get_trans_point();
274 //snap=drag_offset-duckmatic->snap_point_to_grid(drag_offset);
275 //snap=offset-drag_offset;
279 Point vmin(100000000,100000000);
280 Point vmax(-100000000,-100000000);
281 //std::set<etl::handle<Duck> >::iterator iter;
284 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
286 Point p((*iter)->get_trans_point());
287 vmin[0]=min(vmin[0],p[0]);
288 vmin[1]=min(vmin[1],p[1]);
289 vmax[0]=max(vmax[0],p[0]);
290 vmax[1]=max(vmax[1],p[1]);
291 positions.push_back(p);
293 if((vmin-vmax).mag()<=EPSILON)
298 center=(vmin+vmax)*0.5;
303 DuckDrag_Scale::duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector)
305 const DuckList selected_ducks(duckmatic->get_selected_ducks());
306 DuckList::const_iterator iter;
311 //std::set<etl::handle<Duck> >::iterator iter;
312 synfig::Vector vect(duckmatic->snap_point_to_grid(vector)-center);
318 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
320 if(((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION))continue;
322 Vector p(positions[i]);
326 (*iter)->set_trans_point(p);
328 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
330 if(!((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION))continue;
332 Vector p(positions[i]);
336 (*iter)->set_trans_point(p);
343 if(abs(drag_offset[0]-center[0])>EPSILON)
344 vect[0]/=drag_offset[0]-center[0];
347 if(abs(drag_offset[1]-center[1])>EPSILON)
348 vect[1]/=drag_offset[1]-center[1];
354 //vect[0]=vect[1]=vect.mag()*0.707106781;
355 Real amount(vect.mag()/(drag_offset-center).mag());
356 vect[0]=vect[1]=amount;
359 if(vect[0]<EPSILON && vect[0]>-EPSILON)
361 if(vect[1]<EPSILON && vect[1]>-EPSILON)
365 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
367 if(((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION))continue;
369 Vector p(positions[i]-center);
374 (*iter)->set_trans_point(p);
376 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
378 if(!((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION))continue;
380 Vector p(positions[i]-center);
385 (*iter)->set_trans_point(p);
393 DuckDrag_Scale::end_duck_drag(Duckmatic* duckmatic)
395 if(bad_drag)return false;
397 if((last_scale-Vector(1,1)).mag()>0.0001)
399 duckmatic->signal_edited_selected_ducks();
404 duckmatic->signal_user_click_selected_ducks(0);