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
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_polygon.h"
39 #include "canvasview.h"
43 #include <synfigapp/action.h>
44 #include "event_mouse.h"
45 #include "event_layerclick.h"
47 #include "dialog_tooloptions.h"
48 #include <synfigapp/main.h>
54 /* === U S I N G =========================================================== */
58 using namespace synfig;
59 using namespace studio;
61 /* === M A C R O S ========================================================= */
63 /* === G L O B A L S ======================================================= */
65 StatePolygon studio::state_polygon;
67 /* === C L A S S E S & S T R U C T S ======================================= */
69 class studio::StatePolygon_Context : public sigc::trackable
71 etl::handle<CanvasView> canvas_view_;
72 CanvasView::IsWorking is_working;
74 bool prev_table_status;
75 bool prev_workarea_layer_status_;
79 Duckmatic::Push duckmatic_push;
81 std::list<synfig::Point> polygon_point_list;
82 synfigapp::Settings& settings;
85 bool on_polygon_duck_change(const synfig::Point &point, std::list<synfig::Point>::iterator iter);
88 void popup_handle_menu(synfigapp::ValueDesc value_desc);
93 Gtk::Table options_table;
95 Gtk::Button button_make;
98 synfig::String get_id()const { return entry_id.get_text(); }
99 void set_id(const synfig::String& x) { return entry_id.set_text(x); }
101 Smach::event_result event_stop_handler(const Smach::event& x);
103 Smach::event_result event_refresh_handler(const Smach::event& x);
105 Smach::event_result event_mouse_click_handler(const Smach::event& x);
106 Smach::event_result event_refresh_tool_options(const Smach::event& x);
107 void refresh_tool_options();
109 StatePolygon_Context(CanvasView* canvas_view);
111 ~StatePolygon_Context();
113 const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
114 etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
115 synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
116 WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
118 //void on_user_click(synfig::Point point);
119 void load_settings();
120 void save_settings();
123 bool egress_on_selection_change;
124 Smach::event_result event_layer_selection_changed_handler(const Smach::event& /*x*/)
126 if(egress_on_selection_change)
127 throw Smach::egress_exception();
128 return Smach::RESULT_OK;
132 }; // END of class StatePolygon_Context
134 /* === M E T H O D S ======================================================= */
136 StatePolygon::StatePolygon():
137 Smach::state<StatePolygon_Context>("polygon")
139 insert(event_def(EVENT_LAYER_SELECTION_CHANGED,&StatePolygon_Context::event_layer_selection_changed_handler));
140 insert(event_def(EVENT_STOP,&StatePolygon_Context::event_stop_handler));
141 insert(event_def(EVENT_REFRESH,&StatePolygon_Context::event_refresh_handler));
142 insert(event_def(EVENT_REFRESH_DUCKS,&StatePolygon_Context::event_refresh_handler));
143 insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,&StatePolygon_Context::event_mouse_click_handler));
144 insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StatePolygon_Context::event_refresh_tool_options));
147 StatePolygon::~StatePolygon()
152 StatePolygon_Context::load_settings()
156 if(settings.get_value("polygon.id",value))
163 StatePolygon_Context::save_settings()
165 settings.set_value("polygon.id",get_id().c_str());
169 StatePolygon_Context::reset()
171 polygon_point_list.clear();
176 StatePolygon_Context::increment_id()
185 // If there is a number
186 // already at the end of the
187 // id, then remove it.
188 if(id[id.size()-1]<='9' && id[id.size()-1]>='0')
190 // figure out how many digits it is
192 (int)id.size()-1 >= digits && id[id.size()-1-digits] <= '9' && id[id.size()-1-digits] >= '0';
197 str_number=String(id,id.size()-digits,id.size());
198 id=String(id,0,id.size()-digits);
200 number=atoi(str_number.c_str());
210 // Add the number back onto the id
212 const String format(strprintf("%%0%dd",digits));
213 id+=strprintf(format.c_str(),number);
220 StatePolygon_Context::StatePolygon_Context(CanvasView* canvas_view):
221 canvas_view_(canvas_view),
222 is_working(*canvas_view),
223 prev_workarea_layer_status_(get_work_area()->get_allow_layer_clicks()),
224 duckmatic_push(get_work_area()),
225 settings(synfigapp::Main::get_selected_input_device()->settings()),
227 button_make(_("Make"))
229 egress_on_selection_change=true;
232 // Set up the tool options dialog
233 //options_table.attach(*manage(new Gtk::Label(_("Polygon Tool"))), 0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
234 options_table.attach(entry_id, 0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
235 //options_table.attach(button_make, 0, 2, 4, 5, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
236 button_make.signal_pressed().connect(sigc::mem_fun(*this,&StatePolygon_Context::run));
237 options_table.show_all();
238 refresh_tool_options();
239 App::dialog_tool_options->present();
242 // Turn off layer clicking
243 get_work_area()->set_allow_layer_clicks(false);
245 // clear out the ducks
246 get_work_area()->clear_ducks();
248 // Refresh the work area
249 get_work_area()->queue_draw();
251 get_canvas_view()->work_area->set_cursor(Gdk::CROSSHAIR);
253 // Hide the tables if they are showing
254 prev_table_status=get_canvas_view()->tables_are_visible();
255 if(prev_table_status)get_canvas_view()->hide_tables();
257 // Disable the time bar
258 get_canvas_view()->set_sensitive_timebar(false);
261 //get_work_area()->signal_user_click().connect(sigc::mem_fun(*this,&studio::StatePolygon_Context::on_user_click));
263 App::toolbox->refresh();
267 StatePolygon_Context::refresh_tool_options()
269 App::dialog_tool_options->clear();
270 App::dialog_tool_options->set_widget(options_table);
272 App::dialog_tool_options->set_local_name(_("Polygon Tool"));
273 App::dialog_tool_options->set_name("polygon");
275 App::dialog_tool_options->add_button(
276 Gtk::StockID("gtk-execute"),
278 )->signal_clicked().connect(
281 &StatePolygon_Context::run
285 App::dialog_tool_options->add_button(
286 Gtk::StockID("gtk-clear"),
287 _("Clear current Polygon")
288 )->signal_clicked().connect(
291 &StatePolygon_Context::reset
297 StatePolygon_Context::event_refresh_tool_options(const Smach::event& /*x*/)
299 refresh_tool_options();
300 return Smach::RESULT_ACCEPT;
303 StatePolygon_Context::~StatePolygon_Context()
308 // Restore layer clicking
309 get_work_area()->set_allow_layer_clicks(prev_workarea_layer_status_);
311 App::dialog_tool_options->clear();
313 get_canvas_view()->work_area->reset_cursor();
315 // Enable the time bar
316 get_canvas_view()->set_sensitive_timebar(true);
318 // Bring back the tables if they were out before
319 if(prev_table_status)get_canvas_view()->show_tables();
321 // Refresh the work area
322 get_work_area()->queue_draw();
324 App::toolbox->refresh();
328 StatePolygon_Context::event_stop_handler(const Smach::event& /*x*/)
330 synfig::info("STATE RotoPolygon: Received Stop Event");
331 //throw Smach::egress_exception();
333 return Smach::RESULT_ACCEPT;
338 StatePolygon_Context::event_refresh_handler(const Smach::event& /*x*/)
340 synfig::info("STATE RotoPolygon: Received Refresh Event");
342 return Smach::RESULT_ACCEPT;
346 StatePolygon_Context::run()
348 if(polygon_point_list.empty())
351 if(polygon_point_list.size()<3)
353 get_canvas_view()->get_ui_interface()->error("You need at least 3 points to create a polygon");
357 Canvas::Handle canvas(get_canvas_view()->get_canvas());
360 // we are temporarily using the layer to hold something
361 layer=get_canvas_view()->get_selection_manager()->get_selected_layer();
364 depth=layer->get_depth();
365 canvas=layer->get_canvas();
369 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("New Polygon"));
370 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
372 Layer::Handle layer(get_canvas_interface()->add_layer_to("polygon",canvas,depth));
373 layer->set_description(get_id());
374 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
376 layer->disconnect_dynamic_param("vector_list");
377 if(!layer->set_param("vector_list",polygon_point_list))
380 get_canvas_view()->get_ui_interface()->error("Unable to set layer parameter");
385 synfigapp::Action::Handle action(synfigapp::Action::create("value_desc_convert"));
386 synfigapp::ValueDesc value_desc(layer,"vector_list");
387 action->set_param("canvas",get_canvas());
388 action->set_param("canvas_interface",get_canvas_interface());
389 action->set_param("value_desc",value_desc);
390 action->set_param("type","dynamic_list");
391 action->set_param("time",get_canvas_interface()->get_time());
392 if(!get_canvas_interface()->get_instance()->perform_action(action))
395 get_canvas_view()->get_ui_interface()->error("Unable to execute action \"value_desc_convert\"");
399 egress_on_selection_change=false;
400 get_canvas_interface()->get_selection_manager()->clear_selected_layers();
401 get_canvas_interface()->get_selection_manager()->set_selected_layer(layer);
402 egress_on_selection_change=true;
403 //get_canvas_interface()->signal_dirty_preview()();
408 ValueNode::Handle value_node=(ValueNode_Const::create(polygon_point_list));
409 std::string valuenode_name="Poly";
410 while(studio::App::dialog_entry("New Polygon", "Please enter the new ID for this value_node",valuenode_name))
411 if(get_canvas_interface()->add_value_node(value_node,valuenode_name))
420 StatePolygon_Context::event_mouse_click_handler(const Smach::event& x)
422 synfig::info("STATE ROTOPOLYGON: Received mouse button down Event");
423 const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
427 polygon_point_list.push_back(get_work_area()->snap_point_to_grid(event.pos));
429 return Smach::RESULT_ACCEPT;
431 case BUTTON_RIGHT: // Intercept the right-button click to short-circuit the pop-up menu
432 if (!getenv("SYNFIG_ENABLE_POPUP_MENU_IN_ALL_TOOLS"))
433 return Smach::RESULT_ACCEPT;
436 return Smach::RESULT_OK;
442 StatePolygon_Context::refresh_ducks()
444 get_work_area()->clear_ducks();
446 if(polygon_point_list.empty()) return;
448 std::list<synfig::Point>::iterator iter=polygon_point_list.begin();
450 etl::handle<WorkArea::Duck> duck;
451 duck=new WorkArea::Duck(*iter);
452 duck->set_editable(true);
453 duck->signal_edited().connect(
454 sigc::bind(sigc::mem_fun(*this,&studio::StatePolygon_Context::on_polygon_duck_change),iter)
456 duck->signal_user_click(0).connect(sigc::mem_fun(*this,&StatePolygon_Context::run));
458 get_work_area()->add_duck(duck);
460 for(++iter;iter!=polygon_point_list.end();++iter)
462 etl::handle<WorkArea::Bezier> bezier(new WorkArea::Bezier());
463 bezier->p1=bezier->c1=duck;
465 duck=new WorkArea::Duck(*iter);
466 duck->set_editable(true);
467 duck->set_name(strprintf("%x",&*iter));
468 duck->signal_edited().connect(
469 sigc::bind(sigc::mem_fun(*this,&studio::StatePolygon_Context::on_polygon_duck_change),iter)
472 get_work_area()->add_duck(duck);
474 bezier->p2=bezier->c2=duck;
475 get_work_area()->add_bezier(bezier);
477 get_work_area()->queue_draw();
482 StatePolygon_Context::on_polygon_duck_change(const synfig::Point &point, std::list<synfig::Point>::iterator iter)