1 /* === S Y N F I G ========================================================= */
2 /*! \file state_mirror.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_mirror.h"
39 #include "../canvasview.h"
40 #include "../workarea.h"
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>
50 #include <synfigapp/main.h>
52 #include "../general.h"
56 /* === U S I N G =========================================================== */
60 using namespace synfig;
61 using namespace studio;
63 /* === M A C R O S ========================================================= */
70 /* === G L O B A L S ======================================================= */
72 StateMirror studio::state_mirror;
74 /* === C L A S S E S & S T R U C T S ======================================= */
76 class DuckDrag_Mirror : public DuckDrag_Base
78 synfig::Vector center;
80 std::vector<synfig::Vector> positions;
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);
91 class studio::StateMirror_Context : public sigc::trackable
93 etl::handle<CanvasView> canvas_view_;
95 synfigapp::Settings& settings;
97 etl::handle<DuckDrag_Mirror> duck_dragger_;
99 Gtk::Table options_table;
101 Gtk::CheckButton checkbutton_axis_x;
102 Gtk::CheckButton checkbutton_axis_y;
106 Axis get_axis()const { return checkbutton_axis_x.get_active()?AXIS_X:AXIS_Y; }
107 void set_axis(Axis a)
111 checkbutton_axis_x.set_active(true);
112 checkbutton_axis_y.set_active(false);
116 checkbutton_axis_y.set_active(true);
117 checkbutton_axis_x.set_active(false);
120 duck_dragger_->axis=get_axis();
125 checkbutton_axis_x.set_active(!checkbutton_axis_y.get_active());
126 duck_dragger_->axis=get_axis();
130 checkbutton_axis_y.set_active(!checkbutton_axis_x.get_active());
131 duck_dragger_->axis=get_axis();
133 Smach::event_result event_refresh_tool_options(const Smach::event& x);
135 void refresh_tool_options();
137 StateMirror_Context(CanvasView* canvas_view);
139 ~StateMirror_Context();
141 const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
142 etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
143 synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
144 WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
146 void load_settings();
147 void save_settings();
148 }; // END of class StateMirror_Context
150 /* === M E T H O D S ======================================================= */
152 StateMirror::StateMirror():
153 Smach::state<StateMirror_Context>("mirror")
155 insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateMirror_Context::event_refresh_tool_options));
158 StateMirror::~StateMirror()
163 StateMirror_Context::load_settings()
167 settings.get_value("mirror.axis",value);
168 set_axis((Axis)atoi(value.c_str()));
172 StateMirror_Context::save_settings()
174 settings.set_value("mirror.lock_aspect",strprintf("%d",(int)get_axis()));
177 StateMirror_Context::StateMirror_Context(CanvasView* canvas_view):
178 canvas_view_(canvas_view),
179 settings(synfigapp::Main::get_selected_input_device()->settings()),
180 duck_dragger_(new DuckDrag_Mirror()),
181 checkbutton_axis_x(_("Horizontal")),
182 checkbutton_axis_y(_("Vertical"))
184 // Set up the tool options dialog
185 options_table.attach(*manage(new Gtk::Label(_("Mirror Tool"))), 0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
186 options_table.attach(checkbutton_axis_x, 0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
187 options_table.attach(checkbutton_axis_y, 0, 2, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
189 checkbutton_axis_x.signal_toggled().connect(sigc::mem_fun(*this,&StateMirror_Context::update_axis_x));
190 checkbutton_axis_y.signal_toggled().connect(sigc::mem_fun(*this,&StateMirror_Context::update_axis_y));
192 options_table.show_all();
193 refresh_tool_options();
194 App::dialog_tool_options->present();
196 get_work_area()->set_allow_layer_clicks(true);
197 get_work_area()->set_duck_dragger(duck_dragger_);
199 // get_canvas_view()->work_area->set_cursor(Gdk::CROSSHAIR);
200 get_canvas_view()->work_area->reset_cursor();
202 App::toolbox->refresh();
209 StateMirror_Context::refresh_tool_options()
211 App::dialog_tool_options->clear();
212 App::dialog_tool_options->set_widget(options_table);
213 App::dialog_tool_options->set_local_name(_("Mirror Tool"));
214 App::dialog_tool_options->set_name("mirror");
218 StateMirror_Context::event_refresh_tool_options(const Smach::event& /*x*/)
220 refresh_tool_options();
221 return Smach::RESULT_ACCEPT;
224 StateMirror_Context::~StateMirror_Context()
228 get_work_area()->clear_duck_dragger();
229 get_canvas_view()->work_area->reset_cursor();
231 App::dialog_tool_options->clear();
233 App::toolbox->refresh();
236 DuckDrag_Mirror::DuckDrag_Mirror():
242 #define EPSILON 0.0000001
246 DuckDrag_Mirror::begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& /*offset*/)
248 const DuckList selected_ducks(duckmatic->get_selected_ducks());
249 DuckList::const_iterator iter;
253 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
255 Point p((*iter)->get_trans_point());
256 positions.push_back(p);
262 DuckDrag_Mirror::duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector)
267 const DuckList selected_ducks(duckmatic->get_selected_ducks());
268 DuckList::const_iterator iter;
270 // do the Vertex and Position ducks first
271 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
272 if ((*iter)->get_type() == Duck::TYPE_VERTEX ||
273 (*iter)->get_type() == Duck::TYPE_POSITION)
275 Vector p(positions[i]);
277 if (axis==AXIS_X) p[0] = -(p[0]-center[0]) + center[0];
278 else if (axis==AXIS_Y) p[1] = -(p[1]-center[1]) + center[1];
280 (*iter)->set_trans_point(p);
283 // then do the other ducks
284 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
285 if ((*iter)->get_type() != Duck::TYPE_VERTEX &&
286 (*iter)->get_type() != Duck::TYPE_POSITION)
288 Vector p(positions[i]);
290 if (axis==AXIS_X) p[0] = -(p[0]-center[0]) + center[0];
291 else if (axis==AXIS_Y) p[1] = -(p[1]-center[1]) + center[1];
293 (*iter)->set_trans_point(p);
298 DuckDrag_Mirror::end_duck_drag(Duckmatic* duckmatic)
300 duckmatic->signal_edited_selected_ducks();