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
191 for(digits=0;(int)id.size()-1>=digits && id[id.size()-1-digits]<='9' && id[id.size()-1-digits]>='0';digits++)while(false);
194 str_number=String(id,id.size()-digits,id.size());
195 id=String(id,0,id.size()-digits);
197 number=atoi(str_number.c_str());
207 // Add the number back onto the id
209 const String format(strprintf("%%0%dd",digits));
210 id+=strprintf(format.c_str(),number);
217 StatePolygon_Context::StatePolygon_Context(CanvasView* canvas_view):
218 canvas_view_(canvas_view),
219 is_working(*canvas_view),
220 prev_workarea_layer_status_(get_work_area()->get_allow_layer_clicks()),
221 duckmatic_push(get_work_area()),
222 settings(synfigapp::Main::get_selected_input_device()->settings()),
224 button_make(_("Make"))
226 egress_on_selection_change=true;
229 // Set up the tool options dialog
230 //options_table.attach(*manage(new Gtk::Label(_("Polygon Tool"))), 0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
231 options_table.attach(entry_id, 0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
232 //options_table.attach(button_make, 0, 2, 4, 5, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
233 button_make.signal_pressed().connect(sigc::mem_fun(*this,&StatePolygon_Context::run));
234 options_table.show_all();
235 refresh_tool_options();
236 App::dialog_tool_options->present();
239 // Turn off layer clicking
240 get_work_area()->set_allow_layer_clicks(false);
242 // clear out the ducks
243 get_work_area()->clear_ducks();
245 // Refresh the work area
246 get_work_area()->queue_draw();
248 get_canvas_view()->work_area->set_cursor(Gdk::CROSSHAIR);
250 // Hide the tables if they are showing
251 prev_table_status=get_canvas_view()->tables_are_visible();
252 if(prev_table_status)get_canvas_view()->hide_tables();
254 // Disable the time bar
255 get_canvas_view()->set_sensitive_timebar(false);
258 //get_work_area()->signal_user_click().connect(sigc::mem_fun(*this,&studio::StatePolygon_Context::on_user_click));
260 App::toolbox->refresh();
264 StatePolygon_Context::refresh_tool_options()
266 App::dialog_tool_options->clear();
267 App::dialog_tool_options->set_widget(options_table);
269 App::dialog_tool_options->set_local_name(_("Polygon Tool"));
270 App::dialog_tool_options->set_name("polygon");
272 App::dialog_tool_options->add_button(
273 Gtk::StockID("gtk-execute"),
275 )->signal_clicked().connect(
278 &StatePolygon_Context::run
282 App::dialog_tool_options->add_button(
283 Gtk::StockID("gtk-clear"),
284 _("Clear current Polygon")
285 )->signal_clicked().connect(
288 &StatePolygon_Context::reset
294 StatePolygon_Context::event_refresh_tool_options(const Smach::event& /*x*/)
296 refresh_tool_options();
297 return Smach::RESULT_ACCEPT;
300 StatePolygon_Context::~StatePolygon_Context()
305 // Restore layer clicking
306 get_work_area()->set_allow_layer_clicks(prev_workarea_layer_status_);
308 App::dialog_tool_options->clear();
310 get_canvas_view()->work_area->reset_cursor();
312 // Enable the time bar
313 get_canvas_view()->set_sensitive_timebar(true);
315 // Bring back the tables if they were out before
316 if(prev_table_status)get_canvas_view()->show_tables();
318 // Refresh the work area
319 get_work_area()->queue_draw();
321 App::toolbox->refresh();
325 StatePolygon_Context::event_stop_handler(const Smach::event& /*x*/)
327 synfig::info("STATE RotoPolygon: Received Stop Event");
328 //throw Smach::egress_exception();
330 return Smach::RESULT_ACCEPT;
335 StatePolygon_Context::event_refresh_handler(const Smach::event& /*x*/)
337 synfig::info("STATE RotoPolygon: Received Refresh Event");
339 return Smach::RESULT_ACCEPT;
343 StatePolygon_Context::run()
345 if(polygon_point_list.empty())
348 if(polygon_point_list.size()<3)
350 get_canvas_view()->get_ui_interface()->error("You need at least 3 points to create a polygon");
354 Canvas::Handle canvas(get_canvas_view()->get_canvas());
357 // we are temporarily using the layer to hold something
358 layer=get_canvas_view()->get_selection_manager()->get_selected_layer();
361 depth=layer->get_depth();
362 canvas=layer->get_canvas();
366 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("New Polygon"));
367 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
369 Layer::Handle layer(get_canvas_interface()->add_layer_to("polygon",canvas,depth));
370 layer->set_description(get_id());
371 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
373 layer->disconnect_dynamic_param("vector_list");
374 if(!layer->set_param("vector_list",polygon_point_list))
377 get_canvas_view()->get_ui_interface()->error("Unable to set layer parameter");
382 synfigapp::Action::Handle action(synfigapp::Action::create("value_desc_convert"));
383 synfigapp::ValueDesc value_desc(layer,"vector_list");
384 action->set_param("canvas",get_canvas());
385 action->set_param("canvas_interface",get_canvas_interface());
386 action->set_param("value_desc",value_desc);
387 action->set_param("type","dynamic_list");
388 action->set_param("time",get_canvas_interface()->get_time());
389 if(!get_canvas_interface()->get_instance()->perform_action(action))
392 get_canvas_view()->get_ui_interface()->error("Unable to execute action \"value_desc_convert\"");
396 egress_on_selection_change=false;
397 get_canvas_interface()->get_selection_manager()->clear_selected_layers();
398 get_canvas_interface()->get_selection_manager()->set_selected_layer(layer);
399 egress_on_selection_change=true;
400 //get_canvas_interface()->signal_dirty_preview()();
405 ValueNode::Handle value_node=(ValueNode_Const::create(polygon_point_list));
406 std::string valuenode_name="Poly";
407 while(studio::App::dialog_entry("New Polygon", "Please enter the new ID for this value_node",valuenode_name))
408 if(get_canvas_interface()->add_value_node(value_node,valuenode_name))
417 StatePolygon_Context::event_mouse_click_handler(const Smach::event& x)
419 synfig::info("STATE ROTOPOLYGON: Received mouse button down Event");
420 const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
424 polygon_point_list.push_back(get_work_area()->snap_point_to_grid(event.pos));
426 return Smach::RESULT_ACCEPT;
428 case BUTTON_RIGHT: // Intercept the right-button click to short-circuit the pop-up menu
429 if (!getenv("SYNFIG_ENABLE_POPUP_MENU_IN_ALL_TOOLS"))
430 return Smach::RESULT_ACCEPT;
433 return Smach::RESULT_OK;
439 StatePolygon_Context::refresh_ducks()
441 get_work_area()->clear_ducks();
443 if(polygon_point_list.empty()) return;
445 std::list<synfig::Point>::iterator iter=polygon_point_list.begin();
447 etl::handle<WorkArea::Duck> duck;
448 duck=new WorkArea::Duck(*iter);
449 duck->set_editable(true);
450 duck->signal_edited().connect(
451 sigc::bind(sigc::mem_fun(*this,&studio::StatePolygon_Context::on_polygon_duck_change),iter)
453 duck->signal_user_click(0).connect(sigc::mem_fun(*this,&StatePolygon_Context::run));
455 get_work_area()->add_duck(duck);
457 for(++iter;iter!=polygon_point_list.end();++iter)
459 etl::handle<WorkArea::Bezier> bezier(new WorkArea::Bezier());
460 bezier->p1=bezier->c1=duck;
462 duck=new WorkArea::Duck(*iter);
463 duck->set_editable(true);
464 duck->set_name(strprintf("%x",&*iter));
465 duck->signal_edited().connect(
466 sigc::bind(sigc::mem_fun(*this,&studio::StatePolygon_Context::on_polygon_duck_change),iter)
469 get_work_area()->add_duck(duck);
471 bezier->p2=bezier->c2=duck;
472 get_work_area()->add_bezier(bezier);
474 get_work_area()->queue_draw();
479 StatePolygon_Context::on_polygon_duck_change(const synfig::Point &point, std::list<synfig::Point>::iterator iter)