1 /* === S Y N F I G ========================================================= */
2 /*! \file state_polygon.cpp
3 ** \brief Template File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007, 2008 Chris Moore
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.
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.
22 /* ========================================================================= */
24 /* === H E A D E R S ======================================================= */
33 #include <gtkmm/dialog.h>
34 #include <gtkmm/entry.h>
36 #include <synfig/valuenode_dynamiclist.h>
37 #include <synfigapp/action_system.h>
39 #include "state_polygon.h"
40 #include "canvasview.h"
44 #include <synfigapp/action.h>
45 #include "event_mouse.h"
46 #include "event_layerclick.h"
48 #include "dialog_tooloptions.h"
49 #include <synfigapp/main.h>
55 /* === U S I N G =========================================================== */
59 using namespace synfig;
60 using namespace studio;
62 /* === M A C R O S ========================================================= */
64 /* === G L O B A L S ======================================================= */
66 StatePolygon studio::state_polygon;
68 /* === C L A S S E S & S T R U C T S ======================================= */
70 class studio::StatePolygon_Context : public sigc::trackable
72 etl::handle<CanvasView> canvas_view_;
73 CanvasView::IsWorking is_working;
75 bool prev_table_status;
76 bool prev_workarea_layer_status_;
80 Duckmatic::Push duckmatic_push;
82 std::list<synfig::Point> polygon_point_list;
83 synfigapp::Settings& settings;
86 bool on_polygon_duck_change(const synfig::Point &point, std::list<synfig::Point>::iterator iter);
89 void popup_handle_menu(synfigapp::ValueDesc value_desc);
94 Gtk::Table options_table;
96 Gtk::Button button_make;
99 synfig::String get_id()const { return entry_id.get_text(); }
100 void set_id(const synfig::String& x) { return entry_id.set_text(x); }
102 Smach::event_result event_stop_handler(const Smach::event& x);
104 Smach::event_result event_refresh_handler(const Smach::event& x);
106 Smach::event_result event_mouse_click_handler(const Smach::event& x);
107 Smach::event_result event_refresh_tool_options(const Smach::event& x);
108 void refresh_tool_options();
110 StatePolygon_Context(CanvasView* canvas_view);
112 ~StatePolygon_Context();
114 const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
115 etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
116 synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
117 WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
119 //void on_user_click(synfig::Point point);
120 void load_settings();
121 void save_settings();
124 bool egress_on_selection_change;
125 Smach::event_result event_layer_selection_changed_handler(const Smach::event& /*x*/)
127 if(egress_on_selection_change)
128 throw Smach::egress_exception();
129 return Smach::RESULT_OK;
133 }; // END of class StatePolygon_Context
135 /* === M E T H O D S ======================================================= */
137 StatePolygon::StatePolygon():
138 Smach::state<StatePolygon_Context>("polygon")
140 insert(event_def(EVENT_LAYER_SELECTION_CHANGED,&StatePolygon_Context::event_layer_selection_changed_handler));
141 insert(event_def(EVENT_STOP,&StatePolygon_Context::event_stop_handler));
142 insert(event_def(EVENT_REFRESH,&StatePolygon_Context::event_refresh_handler));
143 insert(event_def(EVENT_REFRESH_DUCKS,&StatePolygon_Context::event_refresh_handler));
144 insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,&StatePolygon_Context::event_mouse_click_handler));
145 insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StatePolygon_Context::event_refresh_tool_options));
148 StatePolygon::~StatePolygon()
153 StatePolygon_Context::load_settings()
157 if(settings.get_value("polygon.id",value))
164 StatePolygon_Context::save_settings()
166 settings.set_value("polygon.id",get_id().c_str());
170 StatePolygon_Context::reset()
172 polygon_point_list.clear();
177 StatePolygon_Context::increment_id()
186 // If there is a number
187 // already at the end of the
188 // id, then remove it.
189 if(id[id.size()-1]<='9' && id[id.size()-1]>='0')
191 // figure out how many digits it is
193 (int)id.size()-1 >= digits && id[id.size()-1-digits] <= '9' && id[id.size()-1-digits] >= '0';
198 str_number=String(id,id.size()-digits,id.size());
199 id=String(id,0,id.size()-digits);
201 number=atoi(str_number.c_str());
211 // Add the number back onto the id
213 const String format(strprintf("%%0%dd",digits));
214 id+=strprintf(format.c_str(),number);
221 StatePolygon_Context::StatePolygon_Context(CanvasView* canvas_view):
222 canvas_view_(canvas_view),
223 is_working(*canvas_view),
224 prev_workarea_layer_status_(get_work_area()->get_allow_layer_clicks()),
225 duckmatic_push(get_work_area()),
226 settings(synfigapp::Main::get_selected_input_device()->settings()),
228 button_make(_("Make"))
230 egress_on_selection_change=true;
233 // Set up the tool options dialog
234 options_table.attach(*manage(new Gtk::Label(_("Polygon Tool"))), 0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
235 options_table.attach(entry_id, 0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
236 //options_table.attach(button_make, 0, 2, 4, 5, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
237 button_make.signal_pressed().connect(sigc::mem_fun(*this,&StatePolygon_Context::run));
238 options_table.show_all();
239 refresh_tool_options();
240 App::dialog_tool_options->present();
243 // Turn off layer clicking
244 get_work_area()->set_allow_layer_clicks(false);
246 // clear out the ducks
247 get_work_area()->clear_ducks();
249 // Refresh the work area
250 get_work_area()->queue_draw();
252 get_canvas_view()->work_area->set_cursor(Gdk::CROSSHAIR);
254 // Hide the tables if they are showing
255 prev_table_status=get_canvas_view()->tables_are_visible();
256 if(prev_table_status)get_canvas_view()->hide_tables();
258 // Disable the time bar
259 get_canvas_view()->set_sensitive_timebar(false);
262 //get_work_area()->signal_user_click().connect(sigc::mem_fun(*this,&studio::StatePolygon_Context::on_user_click));
264 App::toolbox->refresh();
268 StatePolygon_Context::refresh_tool_options()
270 App::dialog_tool_options->clear();
271 App::dialog_tool_options->set_widget(options_table);
273 App::dialog_tool_options->set_local_name(_("Polygon Tool"));
274 App::dialog_tool_options->set_name("polygon");
276 App::dialog_tool_options->add_button(
277 Gtk::StockID("gtk-execute"),
279 )->signal_clicked().connect(
282 &StatePolygon_Context::run
286 App::dialog_tool_options->add_button(
287 Gtk::StockID("gtk-clear"),
288 _("Clear current Polygon")
289 )->signal_clicked().connect(
292 &StatePolygon_Context::reset
298 StatePolygon_Context::event_refresh_tool_options(const Smach::event& /*x*/)
300 refresh_tool_options();
301 return Smach::RESULT_ACCEPT;
304 StatePolygon_Context::~StatePolygon_Context()
309 // Restore layer clicking
310 get_work_area()->set_allow_layer_clicks(prev_workarea_layer_status_);
312 App::dialog_tool_options->clear();
314 get_canvas_view()->work_area->reset_cursor();
316 // Enable the time bar
317 get_canvas_view()->set_sensitive_timebar(true);
319 // Bring back the tables if they were out before
320 if(prev_table_status)get_canvas_view()->show_tables();
322 // Refresh the work area
323 get_work_area()->queue_draw();
325 App::toolbox->refresh();
329 StatePolygon_Context::event_stop_handler(const Smach::event& /*x*/)
331 synfig::info("STATE RotoPolygon: Received Stop Event");
332 //throw Smach::egress_exception();
334 return Smach::RESULT_ACCEPT;
339 StatePolygon_Context::event_refresh_handler(const Smach::event& /*x*/)
341 synfig::info("STATE RotoPolygon: Received Refresh Event");
343 return Smach::RESULT_ACCEPT;
347 StatePolygon_Context::run()
349 if(polygon_point_list.empty())
352 if(polygon_point_list.size()<3)
354 get_canvas_view()->get_ui_interface()->error("You need at least 3 points to create a polygon");
358 Canvas::Handle canvas(get_canvas_view()->get_canvas());
361 // we are temporarily using the layer to hold something
362 layer=get_canvas_view()->get_selection_manager()->get_selected_layer();
365 depth=layer->get_depth();
366 canvas=layer->get_canvas();
370 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("New Polygon"));
371 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
373 Layer::Handle layer(get_canvas_interface()->add_layer_to("polygon",canvas,depth));
374 layer->set_description(get_id());
375 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
377 layer->disconnect_dynamic_param("vector_list");
378 if(!layer->set_param("vector_list",polygon_point_list))
381 get_canvas_view()->get_ui_interface()->error("Unable to set layer parameter");
386 synfigapp::Action::Handle action(synfigapp::Action::create("value_desc_convert"));
387 synfigapp::ValueDesc value_desc(layer,"vector_list");
388 action->set_param("canvas",get_canvas());
389 action->set_param("canvas_interface",get_canvas_interface());
390 action->set_param("value_desc",value_desc);
391 action->set_param("type","dynamic_list");
392 action->set_param("time",get_canvas_interface()->get_time());
393 if(!get_canvas_interface()->get_instance()->perform_action(action))
396 get_canvas_view()->get_ui_interface()->error("Unable to execute action \"value_desc_convert\"");
400 egress_on_selection_change=false;
401 synfigapp::SelectionManager::LayerList layer_selection;
402 if (!getenv("SYNFIG_TOOLS_CLEAR_SELECTION"))
403 layer_selection = get_canvas_view()->get_selection_manager()->get_selected_layers();
404 get_canvas_interface()->get_selection_manager()->clear_selected_layers();
405 layer_selection.push_back(layer);
406 get_canvas_interface()->get_selection_manager()->set_selected_layers(layer_selection);
407 egress_on_selection_change=true;
408 //get_canvas_interface()->signal_dirty_preview()();
413 ValueNode::Handle value_node=(ValueNode_Const::create(polygon_point_list));
414 std::string valuenode_name="Poly";
415 while(studio::App::dialog_entry("New Polygon", "Please enter the new ID for this value_node",valuenode_name))
416 if(get_canvas_interface()->add_value_node(value_node,valuenode_name))
425 StatePolygon_Context::event_mouse_click_handler(const Smach::event& x)
427 synfig::info("STATE ROTOPOLYGON: Received mouse button down Event");
428 const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
432 polygon_point_list.push_back(get_work_area()->snap_point_to_grid(event.pos));
434 return Smach::RESULT_ACCEPT;
436 case BUTTON_RIGHT: // Intercept the right-button click to short-circuit the pop-up menu
437 if (!getenv("SYNFIG_ENABLE_POPUP_MENU_IN_ALL_TOOLS"))
438 return Smach::RESULT_ACCEPT;
441 return Smach::RESULT_OK;
447 StatePolygon_Context::refresh_ducks()
449 get_work_area()->clear_ducks();
451 if(polygon_point_list.empty()) return;
453 std::list<synfig::Point>::iterator iter=polygon_point_list.begin();
455 etl::handle<WorkArea::Duck> duck;
456 duck=new WorkArea::Duck(*iter);
457 duck->set_editable(true);
458 duck->signal_edited().connect(
459 sigc::bind(sigc::mem_fun(*this,&studio::StatePolygon_Context::on_polygon_duck_change),iter)
461 duck->signal_user_click(0).connect(sigc::mem_fun(*this,&StatePolygon_Context::run));
463 get_work_area()->add_duck(duck);
465 for(++iter;iter!=polygon_point_list.end();++iter)
467 etl::handle<WorkArea::Bezier> bezier(new WorkArea::Bezier());
468 bezier->p1=bezier->c1=duck;
470 duck=new WorkArea::Duck(*iter);
471 duck->set_editable(true);
472 duck->set_name(strprintf("%x",&*iter));
473 duck->signal_edited().connect(
474 sigc::bind(sigc::mem_fun(*this,&studio::StatePolygon_Context::on_polygon_duck_change),iter)
477 get_work_area()->add_duck(duck);
479 bezier->p2=bezier->c2=duck;
480 get_work_area()->add_bezier(bezier);
482 get_work_area()->queue_draw();
487 StatePolygon_Context::on_polygon_duck_change(const synfig::Point &point, std::list<synfig::Point>::iterator iter)