Remove debug info
[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         etl::handle<DuckDrag_Mirror> duck_dragger_;
99
100         Gtk::Table options_table;
101
102
103         sigc::connection keypress_connect;
104         sigc::connection keyrelease_connect;
105         bool shift_is_pressed;
106         Gtk::RadioButton::Group radiobutton_group;
107         Gtk::RadioButton radiobutton_axis_x;
108         Gtk::RadioButton radiobutton_axis_y;
109
110 public:
111
112         Axis get_axis()const { return radiobutton_axis_x.get_active()?AXIS_X:AXIS_Y; }
113         void set_axis(Axis a)
114         {
115                 if(a==AXIS_X)
116                         radiobutton_axis_x.set_active(true);
117                 else
118                         radiobutton_axis_y.set_active(true);
119
120                 duck_dragger_->axis=get_axis();
121         }
122
123         void update_axes()
124         {
125                 duck_dragger_->axis=get_axis();
126                 get_work_area()->set_cursor(get_axis() == AXIS_X?Gdk::SB_H_DOUBLE_ARROW:Gdk::SB_V_DOUBLE_ARROW);
127         }
128
129
130
131
132         Smach::event_result event_stop_handler(const Smach::event& x);
133         Smach::event_result event_refresh_tool_options(const Smach::event& x);
134         Smach::event_result event_mouse_motion_handler(const Smach::event& x);
135
136         void refresh_tool_options();
137
138         StateMirror_Context(CanvasView* canvas_view);
139
140         ~StateMirror_Context();
141
142         const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
143         etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
144         synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
145         WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
146
147         bool key_press_event(GdkEventKey *event);
148         bool key_release_event(GdkEventKey *event);
149
150
151 };      // END of class StateMirror_Context
152
153 /* === M E T H O D S ======================================================= */
154
155 StateMirror::StateMirror():
156         Smach::state<StateMirror_Context>("mirror")
157 {
158         insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateMirror_Context::event_refresh_tool_options));
159         insert(event_def(EVENT_STOP,&StateMirror_Context::event_stop_handler));
160         insert(event_def(EVENT_WORKAREA_MOUSE_MOTION,           &StateMirror_Context::event_mouse_motion_handler));
161         insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DRAG,      &StateMirror_Context::event_mouse_motion_handler));
162 }
163
164 StateMirror::~StateMirror()
165 {
166 }
167
168 StateMirror_Context::StateMirror_Context(CanvasView* canvas_view):
169         canvas_view_(canvas_view),
170         is_working(*canvas_view),
171         duck_dragger_(new DuckDrag_Mirror()),
172         radiobutton_axis_x(radiobutton_group,_("Horizontal")),
173         radiobutton_axis_y(radiobutton_group,_("Vertical"))
174 {
175         // Set up the tool options dialog
176         options_table.attach(*manage(new Gtk::Label(_("Mirror Tool"))), 0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
177         options_table.attach(radiobutton_axis_x, 0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
178         options_table.attach(radiobutton_axis_y, 0, 2, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
179         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);
180
181         radiobutton_axis_x.signal_toggled().connect(sigc::mem_fun(*this,&StateMirror_Context::update_axes));
182         radiobutton_axis_y.signal_toggled().connect(sigc::mem_fun(*this,&StateMirror_Context::update_axes));
183         shift_is_pressed=false;
184         keypress_connect=get_work_area()->signal_key_press_event().connect(sigc::mem_fun(*this,&StateMirror_Context::key_press_event),false);
185         keyrelease_connect=get_work_area()->signal_key_release_event().connect(sigc::mem_fun(*this,&StateMirror_Context::key_release_event),false);
186
187         options_table.show_all();
188         refresh_tool_options();
189         App::dialog_tool_options->present();
190
191         get_work_area()->set_allow_layer_clicks(true);
192         get_work_area()->set_duck_dragger(duck_dragger_);
193
194         get_work_area()->set_cursor(Gdk::SB_H_DOUBLE_ARROW);
195 //      get_work_area()->reset_cursor();
196
197         App::toolbox->refresh();
198
199         set_axis(AXIS_Y);
200 }
201
202 bool
203 StateMirror_Context::key_press_event(GdkEventKey *event)
204 {
205         if (event->keyval==GDK_Shift_L || event->keyval==GDK_Shift_R )
206         {
207                 if (shift_is_pressed) return false;
208                 shift_is_pressed=true;
209                 set_axis(get_axis()==AXIS_X ? AXIS_Y:AXIS_X);
210                 update_axes();
211         }
212
213         return false; //Pass on the event to other handlers, just in case
214 }
215
216 bool
217 StateMirror_Context::key_release_event(GdkEventKey *event)
218 {
219         if (event->keyval==GDK_Shift_L || event->keyval==GDK_Shift_R )
220         {
221                 if (!shift_is_pressed) return false;
222                 shift_is_pressed = false;
223                 set_axis(get_axis()==AXIS_X ? AXIS_Y:AXIS_X);
224                 update_axes();
225         }
226
227         return false; //Pass on the event to other handlers, just in case
228 }
229
230 void
231 StateMirror_Context::refresh_tool_options()
232 {
233         App::dialog_tool_options->clear();
234         App::dialog_tool_options->set_widget(options_table);
235         App::dialog_tool_options->set_local_name(_("Mirror Tool"));
236         App::dialog_tool_options->set_name("mirror");
237 }
238
239 Smach::event_result
240 StateMirror_Context::event_refresh_tool_options(const Smach::event& /*x*/)
241 {
242         refresh_tool_options();
243         return Smach::RESULT_ACCEPT;
244 }
245
246 Smach::event_result
247 StateMirror_Context::event_stop_handler(const Smach::event& /*x*/)
248 {
249         throw &state_normal;
250         return Smach::RESULT_OK;
251 }
252
253 Smach::event_result
254 StateMirror_Context::event_mouse_motion_handler(const Smach::event& x)
255 {
256         // synfig::info("STATE NORMAL: Received mouse button down Event");
257
258         const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
259         bool shift_state = event.modifier&GDK_SHIFT_MASK;
260         if (shift_state != shift_is_pressed)
261         {
262                 shift_is_pressed = !shift_is_pressed;
263                 set_axis(get_axis()==AXIS_X ? AXIS_Y:AXIS_X);
264         }
265
266         return Smach::RESULT_OK;
267 }
268
269 StateMirror_Context::~StateMirror_Context()
270 {
271         get_work_area()->clear_duck_dragger();
272         get_work_area()->reset_cursor();
273
274         keypress_connect.disconnect();
275         keyrelease_connect.disconnect();
276
277         App::dialog_tool_options->clear();
278
279         App::toolbox->refresh();
280 }
281
282 DuckDrag_Mirror::DuckDrag_Mirror():
283         axis(AXIS_X)
284 {
285 }
286
287 #ifndef EPSILON
288 #define EPSILON 0.0000001
289 #endif
290
291 void
292 DuckDrag_Mirror::begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& /*offset*/)
293 {
294         const DuckList selected_ducks(duckmatic->get_selected_ducks());
295         DuckList::const_iterator iter;
296
297         positions.clear();
298         int i;
299         for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
300         {
301                 Point p((*iter)->get_trans_point());
302                 positions.push_back(p);
303         }
304
305 }
306
307 void
308 DuckDrag_Mirror::duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector)
309 {
310         center=vector;
311         int i;
312
313         const DuckList selected_ducks(duckmatic->get_selected_ducks());
314         DuckList::const_iterator iter;
315
316         Time time(duckmatic->get_time());
317
318         // do the Vertex and Position ducks first
319         for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
320                 if ((*iter)->get_type() == Duck::TYPE_VERTEX ||
321                         (*iter)->get_type() == Duck::TYPE_POSITION)
322                 {
323                         Vector p(positions[i]);
324
325                         if              (axis==AXIS_X) p[0] = -(p[0]-center[0]) + center[0];
326                         else if (axis==AXIS_Y) p[1] = -(p[1]-center[1]) + center[1];
327
328                         (*iter)->set_trans_point(p);
329                 }
330
331         // then do the other ducks
332         for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
333                 if ((*iter)->get_type() != Duck::TYPE_VERTEX &&
334                         (*iter)->get_type() != Duck::TYPE_POSITION)
335                 {
336                         // we don't need to mirror radius ducks - they're one-dimensional
337                         if ((*iter)->is_radius())
338                                 continue;
339
340                         Vector p(positions[i]);
341
342                         if              (axis==AXIS_X) p[0] = -(p[0]-center[0]) + center[0];
343                         else if (axis==AXIS_Y) p[1] = -(p[1]-center[1]) + center[1];
344
345                         (*iter)->set_trans_point(p);
346                 }
347 }
348
349 bool
350 DuckDrag_Mirror::end_duck_drag(Duckmatic* duckmatic)
351 {
352         duckmatic->signal_edited_selected_ducks();
353         return true;
354 }