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