Leave previously selected layers selected, and refresh ducks to ensure newly created...
[synfig.git] / synfig-studio / trunk / src / gtkmm / state_polygon.cpp
blob10c152adc5e449fabccd00a14242523099cad00b
1 /* === S Y N F I G ========================================================= */
2 /*! \file state_polygon.cpp
3 ** \brief Template File
4 **
5 ** $Id$
6 **
7 ** \legal
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.
20 ** \endlegal
22 /* ========================================================================= */
24 /* === H E A D E R S ======================================================= */
26 #ifdef USING_PCH
27 # include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 # include <config.h>
31 #endif
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"
41 #include "workarea.h"
42 #include "app.h"
44 #include <synfigapp/action.h>
45 #include "event_mouse.h"
46 #include "event_layerclick.h"
47 #include "toolbox.h"
48 #include "dialog_tooloptions.h"
49 #include <synfigapp/main.h>
51 #include "general.h"
53 #endif
55 /* === U S I N G =========================================================== */
57 using namespace std;
58 using namespace etl;
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_;
78 Gtk::Menu menu;
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);
92 void refresh_ducks();
94 Gtk::Table options_table;
95 Gtk::Entry entry_id;
96 Gtk::Button button_make;
98 public:
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();
122 void reset();
123 void increment_id();
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;
132 void run();
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()
152 void
153 StatePolygon_Context::load_settings()
155 String value;
157 if(settings.get_value("polygon.id",value))
158 set_id(value);
159 else
160 set_id("Polygon");
163 void
164 StatePolygon_Context::save_settings()
166 settings.set_value("polygon.id",get_id().c_str());
169 void
170 StatePolygon_Context::reset()
172 polygon_point_list.clear();
173 refresh_ducks();
176 void
177 StatePolygon_Context::increment_id()
179 String id(get_id());
180 int number=1;
181 int digits=0;
183 if(id.empty())
184 id="Polygon";
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
192 for (digits = 0;
193 (int)id.size()-1 >= digits && id[id.size()-1-digits] <= '9' && id[id.size()-1-digits] >= '0';
194 digits++)
197 String str_number;
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());
203 else
205 number=1;
206 digits=3;
209 number++;
211 // Add the number back onto the id
213 const String format(strprintf("%%0%dd",digits));
214 id+=strprintf(format.c_str(),number);
217 // Set the ID
218 set_id(id);
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()),
227 entry_id(),
228 button_make(_("Make"))
230 egress_on_selection_change=true;
231 load_settings();
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);
261 // Connect a signal
262 //get_work_area()->signal_user_click().connect(sigc::mem_fun(*this,&studio::StatePolygon_Context::on_user_click));
264 App::toolbox->refresh();
267 void
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"),
278 _("Make Polygon")
279 )->signal_clicked().connect(
280 sigc::mem_fun(
281 *this,
282 &StatePolygon_Context::run
286 App::dialog_tool_options->add_button(
287 Gtk::StockID("gtk-clear"),
288 _("Clear current Polygon")
289 )->signal_clicked().connect(
290 sigc::mem_fun(
291 *this,
292 &StatePolygon_Context::reset
297 Smach::event_result
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()
306 run();
308 save_settings();
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();
328 Smach::event_result
329 StatePolygon_Context::event_stop_handler(const Smach::event& /*x*/)
331 synfig::info("STATE RotoPolygon: Received Stop Event");
332 //throw Smach::egress_exception();
333 reset();
334 return Smach::RESULT_ACCEPT;
338 Smach::event_result
339 StatePolygon_Context::event_refresh_handler(const Smach::event& /*x*/)
341 synfig::info("STATE RotoPolygon: Received Refresh Event");
342 refresh_ducks();
343 return Smach::RESULT_ACCEPT;
346 void
347 StatePolygon_Context::run()
349 if(polygon_point_list.empty())
350 return;
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");
355 return;
357 Layer::Handle layer;
358 Canvas::Handle canvas(get_canvas_view()->get_canvas());
359 int depth(0);
361 // we are temporarily using the layer to hold something
362 layer=get_canvas_view()->get_selection_manager()->get_selected_layer();
363 if(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))
380 group.cancel();
381 get_canvas_view()->get_ui_interface()->error("Unable to set layer parameter");
382 return;
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))
395 group.cancel();
396 get_canvas_view()->get_ui_interface()->error("Unable to execute action \"value_desc_convert\"");
397 return;
400 egress_on_selection_change=false;
401 synfigapp::SelectionManager::LayerList layer_selection(get_canvas_view()->get_selection_manager()->get_selected_layers());
402 get_canvas_interface()->get_selection_manager()->clear_selected_layers();
403 layer_selection.push_back(layer);
404 get_canvas_interface()->get_selection_manager()->set_selected_layers(layer_selection);
405 egress_on_selection_change=true;
406 //get_canvas_interface()->signal_dirty_preview()();
409 else
411 ValueNode::Handle value_node=(ValueNode_Const::create(polygon_point_list));
412 std::string valuenode_name="Poly";
413 while(studio::App::dialog_entry("New Polygon", "Please enter the new ID for this value_node",valuenode_name))
414 if(get_canvas_interface()->add_value_node(value_node,valuenode_name))
415 return true;
418 reset();
419 increment_id();
422 Smach::event_result
423 StatePolygon_Context::event_mouse_click_handler(const Smach::event& x)
425 synfig::info("STATE ROTOPOLYGON: Received mouse button down Event");
426 const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
427 switch(event.button)
429 case BUTTON_LEFT:
430 polygon_point_list.push_back(get_work_area()->snap_point_to_grid(event.pos));
431 refresh_ducks();
432 return Smach::RESULT_ACCEPT;
434 case BUTTON_RIGHT: // Intercept the right-button click to short-circuit the pop-up menu
435 if (!getenv("SYNFIG_ENABLE_POPUP_MENU_IN_ALL_TOOLS"))
436 return Smach::RESULT_ACCEPT;
438 default:
439 return Smach::RESULT_OK;
444 void
445 StatePolygon_Context::refresh_ducks()
447 get_work_area()->clear_ducks();
449 if(polygon_point_list.empty()) return;
451 std::list<synfig::Point>::iterator iter=polygon_point_list.begin();
453 etl::handle<WorkArea::Duck> duck;
454 duck=new WorkArea::Duck(*iter);
455 duck->set_editable(true);
456 duck->signal_edited().connect(
457 sigc::bind(sigc::mem_fun(*this,&studio::StatePolygon_Context::on_polygon_duck_change),iter)
459 duck->signal_user_click(0).connect(sigc::mem_fun(*this,&StatePolygon_Context::run));
461 get_work_area()->add_duck(duck);
463 for(++iter;iter!=polygon_point_list.end();++iter)
465 etl::handle<WorkArea::Bezier> bezier(new WorkArea::Bezier());
466 bezier->p1=bezier->c1=duck;
468 duck=new WorkArea::Duck(*iter);
469 duck->set_editable(true);
470 duck->set_name(strprintf("%x",&*iter));
471 duck->signal_edited().connect(
472 sigc::bind(sigc::mem_fun(*this,&studio::StatePolygon_Context::on_polygon_duck_change),iter)
475 get_work_area()->add_duck(duck);
477 bezier->p2=bezier->c2=duck;
478 get_work_area()->add_bezier(bezier);
480 get_work_area()->queue_draw();
484 bool
485 StatePolygon_Context::on_polygon_duck_change(const synfig::Point &point, std::list<synfig::Point>::iterator iter)
487 *iter=point;
488 return true;