Merge branch 'eldruin_ffmpeg' of ssh://synfig.git.sourceforge.net/gitroot/synfig...
[synfig.git] / synfig-studio / src / gtkmm / mod_mirror / state_mirror.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file state_mirror.cpp
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2009 Nikita Kitaev
10 **
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.
15 **
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.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === H E A D E R S ======================================================= */
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include <gtkmm/dialog.h>
34 #include <gtkmm/entry.h>
35
36 #include <synfig/valuenode_dynamiclist.h>
37 #include <synfigapp/action_system.h>
38
39 #include "state_mirror.h"
40 #include "../state_normal.h"
41 #include "../canvasview.h"
42 #include "../workarea.h"
43 #include "../app.h"
44
45 #include <synfigapp/action.h>
46 #include "../event_mouse.h"
47 #include "../event_layerclick.h"
48 #include "../toolbox.h"
49 #include "../dialog_tooloptions.h"
50 #include <gtkmm/optionmenu.h>
51 #include "../duck.h"
52 #include <synfigapp/main.h>
53
54 #include "../general.h"
55
56 #endif
57
58 /* === U S I N G =========================================================== */
59
60 using namespace std;
61 using namespace etl;
62 using namespace synfig;
63 using namespace studio;
64
65 /* === M A C R O S ========================================================= */
66
67 enum Axis {
68         AXIS_X,
69         AXIS_Y
70 } ;
71
72 /* === G L O B A L S ======================================================= */
73
74 StateMirror studio::state_mirror;
75
76 /* === C L A S S E S & S T R U C T S ======================================= */
77
78 class DuckDrag_Mirror : public DuckDrag_Base
79 {
80         synfig::Vector center;
81
82         std::vector<synfig::Vector> positions;
83
84 public:
85         Axis axis;
86
87         DuckDrag_Mirror();
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);
91 };
92
93 class studio::StateMirror_Context : public sigc::trackable
94 {
95         etl::handle<CanvasView> canvas_view_;
96         CanvasView::IsWorking is_working;
97
98         synfigapp::Settings& settings;
99
100         sigc::connection keypress_connect;
101         sigc::connection keyrelease_connect;
102
103         etl::handle<DuckDrag_Mirror> duck_dragger_;
104
105         Gtk::Table options_table;
106
107         Gtk::RadioButton::Group radiobutton_group;
108         Gtk::RadioButton radiobutton_axis_x;
109         Gtk::RadioButton radiobutton_axis_y;
110
111 public:
112
113         Axis get_axis()const { return radiobutton_axis_x.get_active()?AXIS_X:AXIS_Y; }
114         void set_axis(Axis a)
115         {
116                 if(a==AXIS_X)
117                         radiobutton_axis_x.set_active(true);
118                 else
119                         radiobutton_axis_y.set_active(true);
120
121                 duck_dragger_->axis=get_axis();
122         }
123
124         void update_axes()
125         {
126                 duck_dragger_->axis=get_axis();
127                 get_work_area()->set_cursor(get_axis() == AXIS_X?Gdk::SB_H_DOUBLE_ARROW:Gdk::SB_V_DOUBLE_ARROW);
128         }
129
130         Smach::event_result event_stop_handler(const Smach::event& x);
131         Smach::event_result event_refresh_tool_options(const Smach::event& x);
132
133         void refresh_tool_options();
134
135         StateMirror_Context(CanvasView* canvas_view);
136
137         ~StateMirror_Context();
138
139         const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
140         etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
141         synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
142         WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
143
144         void load_settings();
145         void save_settings();
146
147         bool key_event(GdkEventKey *event);
148 };      // END of class StateMirror_Context
149
150 /* === M E T H O D S ======================================================= */
151
152 StateMirror::StateMirror():
153         Smach::state<StateMirror_Context>("mirror")
154 {
155         insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateMirror_Context::event_refresh_tool_options));
156         insert(event_def(EVENT_STOP,&StateMirror_Context::event_stop_handler));
157 }
158
159 StateMirror::~StateMirror()
160 {
161 }
162
163 void
164 StateMirror_Context::load_settings()
165 {
166         String value;
167
168         settings.get_value("mirror.axis",value);
169         set_axis((Axis)atoi(value.c_str()));
170 }
171
172 void
173 StateMirror_Context::save_settings()
174 {
175         settings.set_value("mirror.lock_aspect",strprintf("%d",(int)get_axis()));
176 }
177
178 StateMirror_Context::StateMirror_Context(CanvasView* canvas_view):
179         canvas_view_(canvas_view),
180         is_working(*canvas_view),
181         settings(synfigapp::Main::get_selected_input_device()->settings()),
182         duck_dragger_(new DuckDrag_Mirror()),
183         radiobutton_axis_x(radiobutton_group,_("Horizontal")),
184         radiobutton_axis_y(radiobutton_group,_("Vertical"))
185 {
186         // Set up the tool options dialog
187         options_table.attach(*manage(new Gtk::Label(_("Mirror Tool"))), 0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
188         options_table.attach(radiobutton_axis_x, 0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
189         options_table.attach(radiobutton_axis_y, 0, 2, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
190         options_table.attach(*manage(new Gtk::Label(_("(Shift key toggles axis)"))), 0, 2, 3, 4, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
191
192         radiobutton_axis_x.signal_toggled().connect(sigc::mem_fun(*this,&StateMirror_Context::update_axes));
193         radiobutton_axis_y.signal_toggled().connect(sigc::mem_fun(*this,&StateMirror_Context::update_axes));
194
195         options_table.show_all();
196         refresh_tool_options();
197         App::dialog_tool_options->present();
198
199         get_work_area()->set_allow_layer_clicks(true);
200         get_work_area()->set_duck_dragger(duck_dragger_);
201
202         keypress_connect=get_work_area()->signal_key_press_event().connect(sigc::mem_fun(*this,&StateMirror_Context::key_event),false);
203         keyrelease_connect=get_work_area()->signal_key_release_event().connect(sigc::mem_fun(*this,&StateMirror_Context::key_event),false);
204
205         get_work_area()->set_cursor(Gdk::SB_H_DOUBLE_ARROW);
206 //      get_work_area()->reset_cursor();
207
208         App::toolbox->refresh();
209
210         set_axis(AXIS_X);
211         load_settings();
212 }
213
214 bool
215 StateMirror_Context::key_event(GdkEventKey *event)
216 {
217         if (event->keyval==GDK_Shift_L || event->keyval==GDK_Shift_R )
218         {
219                 set_axis(get_axis()==AXIS_X ? AXIS_Y:AXIS_X);
220                 get_work_area()->set_cursor(get_axis() == AXIS_X?Gdk::SB_H_DOUBLE_ARROW:Gdk::SB_V_DOUBLE_ARROW);
221         }
222
223         return false; //Pass on the event to other handlers, just in case
224 }
225
226 void
227 StateMirror_Context::refresh_tool_options()
228 {
229         App::dialog_tool_options->clear();
230         App::dialog_tool_options->set_widget(options_table);
231         App::dialog_tool_options->set_local_name(_("Mirror Tool"));
232         App::dialog_tool_options->set_name("mirror");
233 }
234
235 Smach::event_result
236 StateMirror_Context::event_refresh_tool_options(const Smach::event& /*x*/)
237 {
238         refresh_tool_options();
239         return Smach::RESULT_ACCEPT;
240 }
241
242 Smach::event_result
243 StateMirror_Context::event_stop_handler(const Smach::event& /*x*/)
244 {
245         throw &state_normal;
246         return Smach::RESULT_OK;
247 }
248
249 StateMirror_Context::~StateMirror_Context()
250 {
251         save_settings();
252
253         get_work_area()->clear_duck_dragger();
254         get_work_area()->reset_cursor();
255
256         keypress_connect.disconnect();
257         keyrelease_connect.disconnect();
258
259         App::dialog_tool_options->clear();
260
261         App::toolbox->refresh();
262 }
263
264 DuckDrag_Mirror::DuckDrag_Mirror():
265         axis(AXIS_X)
266 {
267 }
268
269 #ifndef EPSILON
270 #define EPSILON 0.0000001
271 #endif
272
273 void
274 DuckDrag_Mirror::begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& /*offset*/)
275 {
276         const DuckList selected_ducks(duckmatic->get_selected_ducks());
277         DuckList::const_iterator iter;
278
279         positions.clear();
280         int i;
281         for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
282         {
283                 Point p((*iter)->get_trans_point());
284                 positions.push_back(p);
285         }
286
287 }
288
289 void
290 DuckDrag_Mirror::duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector)
291 {
292         center=vector;
293         int i;
294
295         const DuckList selected_ducks(duckmatic->get_selected_ducks());
296         DuckList::const_iterator iter;
297
298         Time time(duckmatic->get_time());
299
300         // do the Vertex and Position ducks first
301         for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
302                 if ((*iter)->get_type() == Duck::TYPE_VERTEX ||
303                         (*iter)->get_type() == Duck::TYPE_POSITION)
304                 {
305                         Vector p(positions[i]);
306
307                         if              (axis==AXIS_X) p[0] = -(p[0]-center[0]) + center[0];
308                         else if (axis==AXIS_Y) p[1] = -(p[1]-center[1]) + center[1];
309
310                         (*iter)->set_trans_point(p);
311                 }
312
313         // then do the other ducks
314         for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
315                 if ((*iter)->get_type() != Duck::TYPE_VERTEX &&
316                         (*iter)->get_type() != Duck::TYPE_POSITION)
317                 {
318                         // we don't need to mirror radius ducks - they're one-dimensional
319                         if ((*iter)->is_radius())
320                                 continue;
321
322                         Vector p(positions[i]);
323
324                         if              (axis==AXIS_X) p[0] = -(p[0]-center[0]) + center[0];
325                         else if (axis==AXIS_Y) p[1] = -(p[1]-center[1]) + center[1];
326
327                         (*iter)->set_trans_point(p);
328                 }
329 }
330
331 bool
332 DuckDrag_Mirror::end_duck_drag(Duckmatic* duckmatic)
333 {
334         duckmatic->signal_edited_selected_ducks();
335         return true;
336 }