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
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_scale.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 <synfigapp/main.h>
54 /* === U S I N G =========================================================== */
58 using namespace synfig;
59 using namespace studio;
61 /* === M A C R O S ========================================================= */
63 /* === G L O B A L S ======================================================= */
65 StateScale studio::state_scale;
67 /* === C L A S S E S & S T R U C T S ======================================= */
69 class DuckDrag_Scale : public DuckDrag_Base
72 synfig::Vector last_scale;
73 synfig::Vector drag_offset;
74 synfig::Vector center;
77 std::vector<synfig::Vector> positions;
85 void begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& begin);
86 bool end_duck_drag(Duckmatic* duckmatic);
87 void duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector);
91 class studio::StateScale_Context : public sigc::trackable
93 etl::handle<CanvasView> canvas_view_;
95 synfigapp::Settings& settings;
97 etl::handle<DuckDrag_Scale> duck_dragger_;
99 Gtk::Table options_table;
102 Gtk::CheckButton checkbutton_aspect_lock;
106 bool get_aspect_lock_flag()const { return checkbutton_aspect_lock.get_active(); }
107 void set_aspect_lock_flag(bool x) { return checkbutton_aspect_lock.set_active(x); refresh_aspect_lock_flag(); }
109 void refresh_aspect_lock_flag() { if(duck_dragger_)duck_dragger_->lock_aspect=get_aspect_lock_flag(); }
111 Smach::event_result event_refresh_tool_options(const Smach::event& x);
113 void refresh_tool_options();
115 StateScale_Context(CanvasView* canvas_view);
117 ~StateScale_Context();
119 const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
120 etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
121 synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
122 WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
124 void load_settings();
125 void save_settings();
126 }; // END of class StateScale_Context
128 /* === M E T H O D S ======================================================= */
130 StateScale::StateScale():
131 Smach::state<StateScale_Context>("scale")
133 insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateScale_Context::event_refresh_tool_options));
136 StateScale::~StateScale()
141 StateScale_Context::load_settings()
145 if(settings.get_value("scale.lock_aspect",value) && value=="0")
146 set_aspect_lock_flag(false);
148 set_aspect_lock_flag(true);
152 StateScale_Context::save_settings()
154 settings.set_value("scale.lock_aspect",get_aspect_lock_flag()?"1":"0");
157 StateScale_Context::StateScale_Context(CanvasView* canvas_view):
158 canvas_view_(canvas_view),
159 settings(synfigapp::Main::get_selected_input_device()->settings()),
160 duck_dragger_(new DuckDrag_Scale()),
161 checkbutton_aspect_lock(_("Lock Aspect Ratio"))
163 // Set up the tool options dialog
164 //options_table.attach(*manage(new Gtk::Label(_("Scale Tool"))), 0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
165 options_table.attach(checkbutton_aspect_lock, 0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
167 checkbutton_aspect_lock.signal_toggled().connect(sigc::mem_fun(*this,&StateScale_Context::refresh_aspect_lock_flag));
169 options_table.show_all();
170 refresh_tool_options();
171 App::dialog_tool_options->present();
173 get_work_area()->set_allow_layer_clicks(true);
174 get_work_area()->set_duck_dragger(duck_dragger_);
176 // get_canvas_view()->work_area->set_cursor(Gdk::CROSSHAIR);
177 get_canvas_view()->work_area->reset_cursor();
179 App::toolbox->refresh();
181 set_aspect_lock_flag(true);
186 StateScale_Context::refresh_tool_options()
188 App::dialog_tool_options->clear();
189 App::dialog_tool_options->set_widget(options_table);
190 App::dialog_tool_options->set_local_name(_("Scale Tool"));
191 App::dialog_tool_options->set_name("scale");
195 StateScale_Context::event_refresh_tool_options(const Smach::event& /*x*/)
197 refresh_tool_options();
198 return Smach::RESULT_ACCEPT;
201 StateScale_Context::~StateScale_Context()
205 get_work_area()->clear_duck_dragger();
206 get_canvas_view()->work_area->reset_cursor();
208 App::dialog_tool_options->clear();
210 App::toolbox->refresh();
216 DuckDrag_Scale::DuckDrag_Scale():
222 #define EPSILON 0.0000001
226 DuckDrag_Scale::begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& offset)
228 last_scale=Vector(1,1);
229 const DuckList selected_ducks(duckmatic->get_selected_ducks());
230 DuckList::const_iterator iter;
232 //if(duckmatic->get_selected_ducks().size()<2)
239 drag_offset=duckmatic->find_duck(offset)->get_trans_point();
241 //snap=drag_offset-duckmatic->snap_point_to_grid(drag_offset);
242 //snap=offset-drag_offset;
246 Point vmin(100000000,100000000);
247 Point vmax(-100000000,-100000000);
248 //std::set<etl::handle<Duck> >::iterator iter;
251 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
253 Point p((*iter)->get_trans_point());
254 vmin[0]=min(vmin[0],p[0]);
255 vmin[1]=min(vmin[1],p[1]);
256 vmax[0]=max(vmax[0],p[0]);
257 vmax[1]=max(vmax[1],p[1]);
258 positions.push_back(p);
260 if((vmin-vmax).mag()<=EPSILON)
265 center=(vmin+vmax)*0.5;
270 DuckDrag_Scale::duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector)
272 const DuckList selected_ducks(duckmatic->get_selected_ducks());
273 DuckList::const_iterator iter;
278 //std::set<etl::handle<Duck> >::iterator iter;
279 synfig::Vector vect(duckmatic->snap_point_to_grid(vector)-center);
285 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
287 if(((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION))continue;
289 Vector p(positions[i]);
293 (*iter)->set_trans_point(p);
295 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
297 if(!((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION))continue;
299 Vector p(positions[i]);
303 (*iter)->set_trans_point(p);
310 if(abs(drag_offset[0]-center[0])>EPSILON)
311 vect[0]/=drag_offset[0]-center[0];
314 if(abs(drag_offset[1]-center[1])>EPSILON)
315 vect[1]/=drag_offset[1]-center[1];
321 //vect[0]=vect[1]=vect.mag()*0.707106781;
322 Real amount(vect.mag()/(drag_offset-center).mag());
323 vect[0]=vect[1]=amount;
326 if(vect[0]<EPSILON && vect[0]>-EPSILON)
328 if(vect[1]<EPSILON && vect[1]>-EPSILON)
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 p(positions[i]-center);
341 (*iter)->set_trans_point(p);
343 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
345 if(!((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION))continue;
347 Vector p(positions[i]-center);
352 (*iter)->set_trans_point(p);
360 DuckDrag_Scale::end_duck_drag(Duckmatic* duckmatic)
362 if(bad_drag)return false;
364 if((last_scale-Vector(1,1)).mag()>0.0001)
366 duckmatic->signal_edited_selected_ducks();
371 duckmatic->signal_user_click_selected_ducks(0);