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