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>
56 /* === U S I N G =========================================================== */
60 using namespace synfig;
61 using namespace studio;
63 /* === M A C R O S ========================================================= */
65 /* === G L O B A L S ======================================================= */
67 StateScale studio::state_scale;
69 /* === C L A S S E S & S T R U C T S ======================================= */
71 class DuckDrag_Scale : public DuckDrag_Base
74 synfig::Vector last_scale;
75 synfig::Vector drag_offset;
76 synfig::Vector center;
79 std::vector<synfig::Vector> positions;
87 void begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& begin);
88 bool end_duck_drag(Duckmatic* duckmatic);
89 void duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector);
93 class studio::StateScale_Context : public sigc::trackable
95 etl::handle<CanvasView> canvas_view_;
97 synfigapp::Settings& settings;
99 etl::handle<DuckDrag_Scale> duck_dragger_;
101 Gtk::Table options_table;
104 Gtk::CheckButton checkbutton_aspect_lock;
108 bool get_aspect_lock_flag()const { return checkbutton_aspect_lock.get_active(); }
109 void set_aspect_lock_flag(bool x) { return checkbutton_aspect_lock.set_active(x); refresh_aspect_lock_flag(); }
111 void refresh_aspect_lock_flag() { if(duck_dragger_)duck_dragger_->lock_aspect=get_aspect_lock_flag(); }
113 Smach::event_result event_refresh_tool_options(const Smach::event& x);
115 void refresh_tool_options();
117 StateScale_Context(CanvasView* canvas_view);
119 ~StateScale_Context();
121 const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
122 etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
123 synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
124 WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
126 void load_settings();
127 void save_settings();
128 }; // END of class StateScale_Context
130 /* === M E T H O D S ======================================================= */
132 StateScale::StateScale():
133 Smach::state<StateScale_Context>("scale")
135 insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateScale_Context::event_refresh_tool_options));
138 StateScale::~StateScale()
143 StateScale_Context::load_settings()
147 if(settings.get_value("scale.lock_aspect",value) && value=="0")
148 set_aspect_lock_flag(false);
150 set_aspect_lock_flag(true);
154 StateScale_Context::save_settings()
156 settings.set_value("scale.lock_aspect",get_aspect_lock_flag()?"1":"0");
159 StateScale_Context::StateScale_Context(CanvasView* canvas_view):
160 canvas_view_(canvas_view),
161 settings(synfigapp::Main::get_selected_input_device()->settings()),
162 duck_dragger_(new DuckDrag_Scale()),
163 checkbutton_aspect_lock(_("Lock Aspect Ratio"))
165 // Set up the tool options dialog
166 //options_table.attach(*manage(new Gtk::Label(_("Scale Tool"))), 0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
167 options_table.attach(checkbutton_aspect_lock, 0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
169 checkbutton_aspect_lock.signal_toggled().connect(sigc::mem_fun(*this,&StateScale_Context::refresh_aspect_lock_flag));
171 options_table.show_all();
172 refresh_tool_options();
173 App::dialog_tool_options->present();
175 get_work_area()->set_allow_layer_clicks(true);
176 get_work_area()->set_duck_dragger(duck_dragger_);
178 // get_canvas_view()->work_area->set_cursor(Gdk::CROSSHAIR);
179 get_canvas_view()->work_area->reset_cursor();
181 App::toolbox->refresh();
183 set_aspect_lock_flag(true);
188 StateScale_Context::refresh_tool_options()
190 App::dialog_tool_options->clear();
191 App::dialog_tool_options->set_widget(options_table);
192 App::dialog_tool_options->set_local_name(_("Scale Tool"));
193 App::dialog_tool_options->set_name("scale");
197 StateScale_Context::event_refresh_tool_options(const Smach::event& /*x*/)
199 refresh_tool_options();
200 return Smach::RESULT_ACCEPT;
203 StateScale_Context::~StateScale_Context()
207 get_work_area()->clear_duck_dragger();
208 get_canvas_view()->work_area->reset_cursor();
210 App::dialog_tool_options->clear();
212 App::toolbox->refresh();
218 DuckDrag_Scale::DuckDrag_Scale():
224 #define EPSILON 0.0000001
228 DuckDrag_Scale::begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& offset)
230 last_scale=Vector(1,1);
231 const DuckList selected_ducks(duckmatic->get_selected_ducks());
232 DuckList::const_iterator iter;
234 //if(duckmatic->get_selected_ducks().size()<2)
241 drag_offset=duckmatic->find_duck(offset)->get_trans_point();
243 //snap=drag_offset-duckmatic->snap_point_to_grid(drag_offset);
244 //snap=offset-drag_offset;
248 Point vmin(100000000,100000000);
249 Point vmax(-100000000,-100000000);
250 //std::set<etl::handle<Duck> >::iterator iter;
253 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
255 Point p((*iter)->get_trans_point());
256 vmin[0]=min(vmin[0],p[0]);
257 vmin[1]=min(vmin[1],p[1]);
258 vmax[0]=max(vmax[0],p[0]);
259 vmax[1]=max(vmax[1],p[1]);
260 positions.push_back(p);
262 if((vmin-vmax).mag()<=EPSILON)
267 center=(vmin+vmax)*0.5;
272 DuckDrag_Scale::duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector)
274 const DuckList selected_ducks(duckmatic->get_selected_ducks());
275 DuckList::const_iterator iter;
280 //std::set<etl::handle<Duck> >::iterator iter;
281 synfig::Vector vect(duckmatic->snap_point_to_grid(vector)-center);
287 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
289 if(((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION))continue;
291 Vector p(positions[i]);
295 (*iter)->set_trans_point(p);
297 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
299 if(!((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION))continue;
301 Vector p(positions[i]);
305 (*iter)->set_trans_point(p);
312 if(abs(drag_offset[0]-center[0])>EPSILON)
313 vect[0]/=drag_offset[0]-center[0];
316 if(abs(drag_offset[1]-center[1])>EPSILON)
317 vect[1]/=drag_offset[1]-center[1];
323 //vect[0]=vect[1]=vect.mag()*0.707106781;
324 Real amount(vect.mag()/(drag_offset-center).mag());
325 vect[0]=vect[1]=amount;
328 if(vect[0]<EPSILON && vect[0]>-EPSILON)
330 if(vect[1]<EPSILON && vect[1]>-EPSILON)
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 p(positions[i]-center);
343 (*iter)->set_trans_point(p);
345 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
347 if(!((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION))continue;
349 Vector p(positions[i]-center);
354 (*iter)->set_trans_point(p);
362 DuckDrag_Scale::end_duck_drag(Duckmatic* duckmatic)
364 if(bad_drag)return false;
366 if((last_scale-Vector(1,1)).mag()>0.0001)
368 duckmatic->signal_edited_selected_ducks();
373 duckmatic->signal_user_click_selected_ducks(0);