1 /* === S Y N F I G ========================================================= */
2 /*! \file state_normal.cpp
3 ** \brief Template File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007, 2008 Chris Moore
10 ** Copyright (c) 2009 Nikita Kitaev
12 ** This package is free software; you can redistribute it and/or
13 ** modify it under the terms of the GNU General Public License as
14 ** published by the Free Software Foundation; either version 2 of
15 ** the License, or (at your option) any later version.
17 ** This package is distributed in the hope that it will be useful,
18 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 ** General Public License for more details.
23 /* ========================================================================= */
25 /* === H E A D E R S ======================================================= */
34 #include <gtkmm/dialog.h>
35 #include <gtkmm/entry.h>
37 #include <synfig/valuenode_animated.h>
38 #include <synfig/valuenode_blinecalcvertex.h>
39 #include <synfig/valuenode_composite.h>
40 #include <synfig/valuenode_const.h>
41 #include <synfig/valuenode_dynamiclist.h>
42 #include <synfigapp/action_system.h>
44 #include "state_normal.h"
45 #include "canvasview.h"
49 #include <synfigapp/action.h>
50 #include "event_mouse.h"
51 #include "event_layerclick.h"
53 #include "docks/dialog_tooloptions.h"
54 #include <gtkmm/optionmenu.h>
56 #include <synfig/angle.h>
57 #include <synfigapp/main.h>
62 /* === U S I N G =========================================================== */
66 using namespace synfig;
67 using namespace studio;
69 /* === M A C R O S ========================================================= */
72 #define EPSILON 0.0000001
75 /* === G L O B A L S ======================================================= */
77 StateNormal studio::state_normal;
79 /* === C L A S S E S & S T R U C T S ======================================= */
81 class DuckDrag_Combo : public DuckDrag_Base
83 synfig::Vector last_move;
84 synfig::Vector drag_offset;
85 synfig::Vector center;
88 synfig::Angle original_angle;
89 synfig::Real original_mag;
91 std::vector<synfig::Vector> last_;
92 std::vector<synfig::Vector> positions;
99 CanvasView* canvas_view_;
104 void begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& begin);
105 bool end_duck_drag(Duckmatic* duckmatic);
106 void duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector);
108 etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
112 class studio::StateNormal_Context : public sigc::trackable
114 CanvasView* canvas_view_;
116 etl::handle<DuckDrag_Combo> duck_dragger_;
118 Gtk::Table options_table;
122 void refresh_cursor();
124 bool get_rotate_flag()const { if(duck_dragger_) return duck_dragger_->rotate; else return false; }
125 void set_rotate_flag(bool x) { if(duck_dragger_ && x!=duck_dragger_->rotate)
126 {duck_dragger_->rotate=x; refresh_cursor();} }
128 bool get_scale_flag()const { if(duck_dragger_) return duck_dragger_->scale; else return false; }
129 void set_scale_flag(bool x) { if(duck_dragger_ && x!=duck_dragger_->scale)
130 {duck_dragger_->scale=x; refresh_cursor();} }
132 bool get_constrain_flag()const { if(duck_dragger_) return duck_dragger_->constrain; else return false; }
133 void set_constrain_flag(bool x) { if(duck_dragger_ && x!=duck_dragger_->constrain)
134 {duck_dragger_->constrain=x; refresh_cursor();} }
136 StateNormal_Context(CanvasView* canvas_view);
138 ~StateNormal_Context();
140 CanvasView* get_canvas_view()const{return canvas_view_;}
141 etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
142 synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
143 WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
145 Smach::event_result event_stop_handler(const Smach::event& x);
146 Smach::event_result event_refresh_handler(const Smach::event& x);
147 Smach::event_result event_refresh_ducks_handler(const Smach::event& x);
148 Smach::event_result event_undo_handler(const Smach::event& x);
149 Smach::event_result event_redo_handler(const Smach::event& x);
150 Smach::event_result event_mouse_button_down_handler(const Smach::event& x);
151 Smach::event_result event_multiple_ducks_clicked_handler(const Smach::event& x);
152 Smach::event_result event_mouse_motion_handler(const Smach::event& x);
153 Smach::event_result event_refresh_tool_options(const Smach::event& x);
154 void refresh_tool_options();
155 Smach::event_result event_layer_click(const Smach::event& x);
158 }; // END of class StateNormal_Context
160 /* === M E T H O D S ======================================================= */
162 StateNormal::StateNormal():
163 Smach::state<StateNormal_Context>("normal")
165 insert(event_def(EVENT_STOP,&StateNormal_Context::event_stop_handler));
166 insert(event_def(EVENT_REFRESH,&StateNormal_Context::event_refresh_handler));
167 insert(event_def(EVENT_REFRESH_DUCKS,&StateNormal_Context::event_refresh_ducks_handler));
168 insert(event_def(EVENT_UNDO,&StateNormal_Context::event_undo_handler));
169 insert(event_def(EVENT_REDO,&StateNormal_Context::event_redo_handler));
170 insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,&StateNormal_Context::event_mouse_button_down_handler));
171 insert(event_def(EVENT_WORKAREA_MULTIPLE_DUCKS_CLICKED,&StateNormal_Context::event_multiple_ducks_clicked_handler));
172 insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateNormal_Context::event_refresh_tool_options));
173 insert(event_def(EVENT_WORKAREA_MOUSE_MOTION, &StateNormal_Context::event_mouse_motion_handler));
174 insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DRAG, &StateNormal_Context::event_mouse_motion_handler));
175 insert(event_def(EVENT_WORKAREA_LAYER_CLICKED,&StateNormal_Context::event_layer_click));
179 StateNormal::~StateNormal()
183 void StateNormal_Context::refresh_cursor()
185 // Check the current state and return when applicable
186 synfig::String sname;
187 sname=get_canvas_view()->get_smach().get_state_name();
191 // Change the cursor based on key flags
192 if(get_rotate_flag() && !get_scale_flag())
194 get_work_area()->set_cursor(Gdk::EXCHANGE);
197 if(!get_rotate_flag() && get_scale_flag())
199 get_work_area()->set_cursor(Gdk::SIZING);
202 if(get_rotate_flag() && get_scale_flag())
204 get_work_area()->set_cursor(Gdk::CROSSHAIR);
208 // Default cursor for Transform tool
209 get_work_area()->set_cursor(Gdk::ARROW);
213 StateNormal_Context::StateNormal_Context(CanvasView* canvas_view):
214 canvas_view_(canvas_view),
215 duck_dragger_(new DuckDrag_Combo())
217 duck_dragger_->canvas_view_=get_canvas_view();
219 // Set up the tool options dialog
220 options_table.attach(*manage(new Gtk::Label(_("Transform Tool"))), 0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
221 options_table.attach(*manage(new Gtk::Label(_("Ctrl to rotate"), Gtk::ALIGN_LEFT)), 0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
222 options_table.attach(*manage(new Gtk::Label(_("Alt to scale"), Gtk::ALIGN_LEFT)), 0, 2, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
223 options_table.attach(*manage(new Gtk::Label(_("Shift to constrain"), Gtk::ALIGN_LEFT)), 0, 2, 3, 4, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
225 options_table.show_all();
226 refresh_tool_options();
227 //App::dialog_tool_options->set_widget(options_table);
228 //App::dialog_tool_options->present();
230 get_work_area()->set_allow_layer_clicks(true);
231 get_work_area()->set_duck_dragger(duck_dragger_);
233 //these will segfault
234 // get_work_area()->set_cursor(Gdk::CROSSHAIR);
235 // get_work_area()->reset_cursor();
237 App::toolbox->refresh();
241 StateNormal_Context::refresh_tool_options()
243 App::dialog_tool_options->clear();
244 App::dialog_tool_options->set_widget(options_table);
245 App::dialog_tool_options->set_local_name(_("Transform Tool"));
246 App::dialog_tool_options->set_name("normal");
251 StateNormal_Context::~StateNormal_Context()
253 get_work_area()->clear_duck_dragger();
254 get_work_area()->reset_cursor();
256 App::dialog_tool_options->clear();
258 App::toolbox->refresh();
261 DuckDrag_Combo::DuckDrag_Combo():
264 constrain(false) // Lock aspect for scale
269 DuckDrag_Combo::begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& offset)
271 last_move=Vector(1,1);
273 const DuckList selected_ducks(duckmatic->get_selected_ducks());
274 DuckList::const_iterator iter;
278 drag_offset=duckmatic->find_duck(offset)->get_trans_point();
280 //snap=drag_offset-duckmatic->snap_point_to_grid(drag_offset);
281 //snap=offset-drag_offset_;
285 Point vmin(100000000,100000000);
286 Point vmax(-100000000,-100000000);
287 //std::set<etl::handle<Duck> >::iterator iter;
290 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
292 Point p((*iter)->get_trans_point());
293 vmin[0]=min(vmin[0],p[0]);
294 vmin[1]=min(vmin[1],p[1]);
295 vmax[0]=max(vmax[0],p[0]);
296 vmax[1]=max(vmax[1],p[1]);
297 positions.push_back(p);
299 center=(vmin+vmax)*0.5;
300 if((vmin-vmax).mag()<=EPSILON)
306 synfig::Vector vect(offset-center);
307 original_angle=Angle::tan(vect[1],vect[0]);
308 original_mag=vect.mag();
313 DuckDrag_Combo::duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector)
315 if (!duckmatic) return;
320 //Override axis lock set in workarea when holding down the shift key
321 if (!move_only && (scale || rotate))
322 duckmatic->set_axis_lock(false);
325 if (move_only || (!scale && !rotate))
326 vect= duckmatic->snap_point_to_grid(vector)-drag_offset+snap;
328 vect= duckmatic->snap_point_to_grid(vector)-center+snap;
332 const DuckList selected_ducks(duckmatic->get_selected_ducks());
333 DuckList::const_iterator iter;
335 Time time(duckmatic->get_time());
338 if( move_only || (!scale && !rotate) )
340 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
342 if((*iter)->get_type()==Duck::TYPE_VERTEX || (*iter)->get_type()==Duck::TYPE_POSITION)
343 (*iter)->set_trans_point(positions[i]+vect, time);
345 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
347 if((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION)
348 (*iter)->set_trans_point(positions[i]+vect, time);
354 Angle::deg angle(Angle::tan(vect[1],vect[0]));
355 angle=original_angle-angle;
358 float degrees = angle.get()/15;
359 angle= Angle::deg (degrees>0?std::floor(degrees)*15:std::ceil(degrees)*15);
361 Real mag(vect.mag()/original_mag);
362 Real sine(Angle::sin(angle).get());
363 Real cosine(Angle::cos(angle).get());
365 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
367 if((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION)continue;
369 Vector x(positions[i]-center),p;
371 p[0]=cosine*x[0]+sine*x[1];
372 p[1]=-sine*x[0]+cosine*x[1];
375 (*iter)->set_trans_point(p, time);
377 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
379 if(!((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION))continue;
381 Vector x(positions[i]-center),p;
383 p[0]=cosine*x[0]+sine*x[1];
384 p[1]=-sine*x[0]+cosine*x[1];
387 (*iter)->set_trans_point(p, time);
393 if(abs(drag_offset[0]-center[0])>EPSILON)
394 vect[0]/=drag_offset[0]-center[0];
397 if(abs(drag_offset[1]-center[1])>EPSILON)
398 vect[1]/=drag_offset[1]-center[1];
405 if((drag_offset-center).mag() < EPSILON)
408 amount = vect.mag()/(drag_offset-center).mag();
410 vect[0]=vect[1]=amount;
413 if(vect[0]<EPSILON && vect[0]>-EPSILON)
415 if(vect[1]<EPSILON && vect[1]>-EPSILON)
418 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
420 if(((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION))continue;
422 Vector p(positions[i]-center);
427 (*iter)->set_trans_point(p, time);
429 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
431 if(!((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION))continue;
433 Vector p(positions[i]-center);
438 (*iter)->set_trans_point(p, time);
443 // then patch up the tangents for the vertices we've moved
444 duckmatic->update_ducks();
450 DuckDrag_Combo::end_duck_drag(Duckmatic* duckmatic)
452 if(bad_drag)return false;
454 //synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Rotate Ducks"));
456 if((last_move-Vector(1,1)).mag()>0.0001)
458 duckmatic->signal_edited_selected_ducks();
463 duckmatic->signal_user_click_selected_ducks(0);
469 StateNormal_Context::event_refresh_tool_options(const Smach::event& /*x*/)
471 refresh_tool_options();
472 return Smach::RESULT_ACCEPT;
476 StateNormal_Context::event_stop_handler(const Smach::event& /*x*/)
478 // synfig::info("STATE NORMAL: Received Stop Event");
479 canvas_view_->stop();
480 return Smach::RESULT_ACCEPT;
484 StateNormal_Context::event_refresh_handler(const Smach::event& /*x*/)
486 // synfig::info("STATE NORMAL: Received Refresh Event");
487 canvas_view_->rebuild_tables();
488 canvas_view_->work_area->queue_render_preview();
489 return Smach::RESULT_ACCEPT;
493 StateNormal_Context::event_refresh_ducks_handler(const Smach::event& /*x*/)
495 // synfig::info("STATE NORMAL: Received Refresh Ducks");
496 canvas_view_->queue_rebuild_ducks();
497 return Smach::RESULT_ACCEPT;
501 StateNormal_Context::event_undo_handler(const Smach::event& /*x*/)
503 // synfig::info("STATE NORMAL: Received Undo Event");
504 canvas_view_->get_instance()->undo();
505 return Smach::RESULT_ACCEPT;
509 StateNormal_Context::event_redo_handler(const Smach::event& /*x*/)
511 // synfig::info("STATE NORMAL: Received Redo Event");
512 canvas_view_->get_instance()->redo();
513 return Smach::RESULT_ACCEPT;
517 StateNormal_Context::event_mouse_button_down_handler(const Smach::event& x)
519 // synfig::info("STATE NORMAL: Received mouse button down Event");
521 const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
526 canvas_view_->popup_main_menu();
527 return Smach::RESULT_ACCEPT;
529 return Smach::RESULT_OK;
534 StateNormal_Context::event_mouse_motion_handler(const Smach::event& x)
536 // synfig::info("STATE NORMAL: Received mouse button down Event");
538 const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
540 set_rotate_flag(event.modifier&GDK_CONTROL_MASK);
541 set_scale_flag(event.modifier&GDK_MOD1_MASK);
542 set_constrain_flag(event.modifier&GDK_SHIFT_MASK);
544 return Smach::RESULT_OK;
548 StateNormal_Context::event_layer_click(const Smach::event& x)
550 const EventLayerClick& event(*reinterpret_cast<const EventLayerClick*>(&x));
554 // synfig::info("STATE NORMAL: Received layer click Event, \"%s\"",event.layer->get_name().c_str());
558 // synfig::info("STATE NORMAL: Received layer click Event with an empty layer.");
564 if(!(event.modifier&Gdk::CONTROL_MASK))
565 canvas_view_->get_selection_manager()->clear_selected_layers();
568 std::list<Layer::Handle> layer_list(canvas_view_->get_selection_manager()->get_selected_layers());
569 std::set<Layer::Handle> layers(layer_list.begin(),layer_list.end());
570 if(layers.count(event.layer))
572 layers.erase(event.layer);
573 layer_list=std::list<Layer::Handle>(layers.begin(),layers.end());
574 canvas_view_->get_selection_manager()->clear_selected_layers();
575 canvas_view_->get_selection_manager()->set_selected_layers(layer_list);
579 canvas_view_->get_selection_manager()->set_selected_layer(event.layer);
582 return Smach::RESULT_ACCEPT;
584 canvas_view_->popup_layer_menu(event.layer);
585 return Smach::RESULT_ACCEPT;
587 return Smach::RESULT_OK;
593 StateNormal_Context::edit_several_waypoints(std::list<synfigapp::ValueDesc> value_desc_list)
596 "Edit Multiple Waypoints", // Title
598 true // use_separator
601 Widget_WaypointModel widget_waypoint_model;
602 widget_waypoint_model.show();
604 dialog.get_vbox()->pack_start(widget_waypoint_model);
607 dialog.add_button(Gtk::StockID("gtk-apply"),1);
608 dialog.add_button(Gtk::StockID("gtk-cancel"),0);
613 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Set Waypoints"));
615 std::list<synfigapp::ValueDesc>::iterator iter;
616 for(iter=value_desc_list.begin();iter!=value_desc_list.end();++iter)
618 synfigapp::ValueDesc value_desc(*iter);
620 if(!value_desc.is_valid())
623 ValueNode_Animated::Handle value_node;
625 // If this value isn't a ValueNode_Animated, but
626 // it is somewhat constant, then go ahead and convert
627 // it to a ValueNode_Animated.
628 if(!value_desc.is_value_node() || ValueNode_Const::Handle::cast_dynamic(value_desc.get_value_node()))
631 if(value_desc.is_value_node())
632 value=ValueNode_Const::Handle::cast_dynamic(value_desc.get_value_node())->get_value();
634 value=value_desc.get_value();
636 value_node=ValueNode_Animated::create(value,get_canvas()->get_time());
638 synfigapp::Action::Handle action;
640 if(!value_desc.is_value_node())
642 action=synfigapp::Action::create("ValueDescConnect");
643 action->set_param("dest",value_desc);
644 action->set_param("src",ValueNode::Handle(value_node));
648 action=synfigapp::Action::create("ValueNodeReplace");
649 action->set_param("dest",value_desc.get_value_node());
650 action->set_param("src",ValueNode::Handle(value_node));
653 action->set_param("canvas",get_canvas());
654 action->set_param("canvas_interface",get_canvas_interface());
657 if(!get_canvas_interface()->get_instance()->perform_action(action))
659 get_canvas_view()->get_ui_interface()->error(_("Unable to convert to animated waypoint"));
666 if(value_desc.is_value_node())
667 value_node=ValueNode_Animated::Handle::cast_dynamic(value_desc.get_value_node());
674 synfigapp::Action::Handle action(synfigapp::Action::create("WaypointSetSmart"));
678 get_canvas_view()->get_ui_interface()->error(_("Unable to find WaypointSetSmart action"));
684 action->set_param("canvas",get_canvas());
685 action->set_param("canvas_interface",get_canvas_interface());
686 action->set_param("value_node",ValueNode::Handle(value_node));
687 action->set_param("time",get_canvas()->get_time());
688 action->set_param("model",widget_waypoint_model.get_waypoint_model());
690 if(!get_canvas_interface()->get_instance()->perform_action(action))
692 get_canvas_view()->get_ui_interface()->error(_("Unable to set a specific waypoint"));
699 //get_canvas_view()->get_ui_interface()->error(_("Unable to animate a specific valuedesc"));
709 StateNormal_Context::event_multiple_ducks_clicked_handler(const Smach::event& /*x*/)
711 // synfig::info("STATE NORMAL: Received multiple duck click event");
713 //const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
715 std::list<synfigapp::ValueDesc> value_desc_list;
717 // Create a list of value_descs associated with selection
718 const DuckList selected_ducks(get_work_area()->get_selected_ducks());
719 DuckList::const_iterator iter;
720 for(iter=selected_ducks.begin();iter!=selected_ducks.end();++iter)
722 synfigapp::ValueDesc value_desc((*iter)->get_value_desc());
724 if(!value_desc.is_valid())
727 if(value_desc.get_value_type()==ValueBase::TYPE_BLINEPOINT && value_desc.is_value_node() && ValueNode_Composite::Handle::cast_dynamic(value_desc.get_value_node()))
729 value_desc_list.push_back(
730 synfigapp::ValueDesc(
731 ValueNode_Composite::Handle::cast_dynamic(value_desc.get_value_node())
732 ,ValueNode_Composite::Handle::cast_dynamic(value_desc.get_value_node())
733 ->get_link_index_from_name("point")
738 value_desc_list.push_back(value_desc);
741 Gtk::Menu *menu=manage(new Gtk::Menu());
742 menu->signal_hide().connect(sigc::bind(sigc::ptr_fun(&delete_widget), menu));
744 canvas_view_->get_instance()->make_param_menu(menu,canvas_view_->get_canvas(),value_desc_list);
747 synfigapp::Action::ParamList param_list;
748 param_list=get_canvas_interface()->generate_param_list(value_desc_list);
750 canvas_view_->add_actions_to_menu(menu, param_list,synfigapp::Action::CATEGORY_VALUEDESC|synfigapp::Action::CATEGORY_VALUENODE);
752 menu->items().push_back(Gtk::Menu_Helpers::MenuElem(_("Edit Waypoints"),
756 &studio::StateNormal_Context::edit_several_waypoints
762 menu->popup(3,gtk_get_current_event_time());
764 return Smach::RESULT_ACCEPT;