Fix 1866422: the polygon tool was broken by r1230. This fixes it.
[synfig.git] / synfig-studio / trunk / src / gtkmm / state_polygon.cpp
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 **
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.
14 **
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.
19 **      \endlegal
20 */
21 /* ========================================================================= */
22
23 /* === H E A D E R S ======================================================= */
24
25 #ifdef USING_PCH
26 #       include "pch.h"
27 #else
28 #ifdef HAVE_CONFIG_H
29 #       include <config.h>
30 #endif
31
32 #include <gtkmm/dialog.h>
33 #include <gtkmm/entry.h>
34
35 #include <synfig/valuenode_dynamiclist.h>
36 #include <synfigapp/action_system.h>
37
38 #include "state_polygon.h"
39 #include "canvasview.h"
40 #include "workarea.h"
41 #include "app.h"
42
43 #include <synfigapp/action.h>
44 #include "event_mouse.h"
45 #include "event_layerclick.h"
46 #include "toolbox.h"
47 #include "dialog_tooloptions.h"
48 #include <synfigapp/main.h>
49
50 #include "general.h"
51
52 #endif
53
54 /* === U S I N G =========================================================== */
55
56 using namespace std;
57 using namespace etl;
58 using namespace synfig;
59 using namespace studio;
60
61 /* === M A C R O S ========================================================= */
62
63 /* === G L O B A L S ======================================================= */
64
65 StatePolygon studio::state_polygon;
66
67 /* === C L A S S E S & S T R U C T S ======================================= */
68
69 class studio::StatePolygon_Context : public sigc::trackable
70 {
71         etl::handle<CanvasView> canvas_view_;
72         CanvasView::IsWorking is_working;
73
74         bool prev_table_status;
75         bool prev_workarea_layer_status_;
76
77         Gtk::Menu menu;
78
79         Duckmatic::Push duckmatic_push;
80
81         std::list<synfig::Point> polygon_point_list;
82         synfigapp::Settings& settings;
83
84
85         bool on_polygon_duck_change(const synfig::Point &point, std::list<synfig::Point>::iterator iter);
86
87
88         void popup_handle_menu(synfigapp::ValueDesc value_desc);
89
90
91         void refresh_ducks();
92
93         Gtk::Table options_table;
94         Gtk::Entry entry_id;
95         Gtk::Button button_make;
96
97 public:
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); }
100
101         Smach::event_result event_stop_handler(const Smach::event& x);
102
103         Smach::event_result event_refresh_handler(const Smach::event& x);
104
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();
108
109         StatePolygon_Context(CanvasView* canvas_view);
110
111         ~StatePolygon_Context();
112
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();}
117
118         //void on_user_click(synfig::Point point);
119         void load_settings();
120         void save_settings();
121         void reset();
122         void increment_id();
123         bool egress_on_selection_change;
124         Smach::event_result event_layer_selection_changed_handler(const Smach::event& /*x*/)
125         {
126                 if(egress_on_selection_change)
127                         throw Smach::egress_exception();
128                 return Smach::RESULT_OK;
129         }
130
131         void run();
132 };      // END of class StatePolygon_Context
133
134 /* === M E T H O D S ======================================================= */
135
136 StatePolygon::StatePolygon():
137         Smach::state<StatePolygon_Context>("polygon")
138 {
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));
145 }
146
147 StatePolygon::~StatePolygon()
148 {
149 }
150
151 void
152 StatePolygon_Context::load_settings()
153 {
154         String value;
155
156         if(settings.get_value("polygon.id",value))
157                 set_id(value);
158         else
159                 set_id("Polygon");
160 }
161
162 void
163 StatePolygon_Context::save_settings()
164 {
165         settings.set_value("polygon.id",get_id().c_str());
166 }
167
168 void
169 StatePolygon_Context::reset()
170 {
171         polygon_point_list.clear();
172         refresh_ducks();
173 }
174
175 void
176 StatePolygon_Context::increment_id()
177 {
178         String id(get_id());
179         int number=1;
180         int digits=0;
181
182         if(id.empty())
183                 id="Polygon";
184
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')
189         {
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);
192
193                 String str_number;
194                 str_number=String(id,id.size()-digits,id.size());
195                 id=String(id,0,id.size()-digits);
196
197                 number=atoi(str_number.c_str());
198         }
199         else
200         {
201                 number=1;
202                 digits=3;
203         }
204
205         number++;
206
207         // Add the number back onto the id
208         {
209                 const String format(strprintf("%%0%dd",digits));
210                 id+=strprintf(format.c_str(),number);
211         }
212
213         // Set the ID
214         set_id(id);
215 }
216
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()),
223         entry_id(),
224         button_make(_("Make"))
225 {
226         egress_on_selection_change=true;
227         load_settings();
228
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();
237
238
239         // Turn off layer clicking
240         get_work_area()->set_allow_layer_clicks(false);
241
242         // clear out the ducks
243         get_work_area()->clear_ducks();
244
245         // Refresh the work area
246         get_work_area()->queue_draw();
247
248         get_canvas_view()->work_area->set_cursor(Gdk::CROSSHAIR);
249
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();
253
254         // Disable the time bar
255         get_canvas_view()->set_sensitive_timebar(false);
256
257         // Connect a signal
258         //get_work_area()->signal_user_click().connect(sigc::mem_fun(*this,&studio::StatePolygon_Context::on_user_click));
259
260         App::toolbox->refresh();
261 }
262
263 void
264 StatePolygon_Context::refresh_tool_options()
265 {
266         App::dialog_tool_options->clear();
267         App::dialog_tool_options->set_widget(options_table);
268
269         App::dialog_tool_options->set_local_name(_("Polygon Tool"));
270         App::dialog_tool_options->set_name("polygon");
271
272         App::dialog_tool_options->add_button(
273                 Gtk::StockID("gtk-execute"),
274                 _("Make Polygon")
275         )->signal_clicked().connect(
276                 sigc::mem_fun(
277                         *this,
278                         &StatePolygon_Context::run
279                 )
280         );
281
282         App::dialog_tool_options->add_button(
283                 Gtk::StockID("gtk-clear"),
284                 _("Clear current Polygon")
285         )->signal_clicked().connect(
286                 sigc::mem_fun(
287                         *this,
288                         &StatePolygon_Context::reset
289                 )
290         );
291 }
292
293 Smach::event_result
294 StatePolygon_Context::event_refresh_tool_options(const Smach::event& /*x*/)
295 {
296         refresh_tool_options();
297         return Smach::RESULT_ACCEPT;
298 }
299
300 StatePolygon_Context::~StatePolygon_Context()
301 {
302         run();
303
304         save_settings();
305         // Restore layer clicking
306         get_work_area()->set_allow_layer_clicks(prev_workarea_layer_status_);
307
308         App::dialog_tool_options->clear();
309
310         get_canvas_view()->work_area->reset_cursor();
311
312         // Enable the time bar
313         get_canvas_view()->set_sensitive_timebar(true);
314
315         // Bring back the tables if they were out before
316         if(prev_table_status)get_canvas_view()->show_tables();
317
318         // Refresh the work area
319         get_work_area()->queue_draw();
320
321         App::toolbox->refresh();
322 }
323
324 Smach::event_result
325 StatePolygon_Context::event_stop_handler(const Smach::event& /*x*/)
326 {
327         synfig::info("STATE RotoPolygon: Received Stop Event");
328         //throw Smach::egress_exception();
329         reset();
330         return Smach::RESULT_ACCEPT;
331
332 }
333
334 Smach::event_result
335 StatePolygon_Context::event_refresh_handler(const Smach::event& /*x*/)
336 {
337         synfig::info("STATE RotoPolygon: Received Refresh Event");
338         refresh_ducks();
339         return Smach::RESULT_ACCEPT;
340 }
341
342 void
343 StatePolygon_Context::run()
344 {
345         if(polygon_point_list.empty())
346                 return;
347
348         if(polygon_point_list.size()<3)
349         {
350                 get_canvas_view()->get_ui_interface()->error("You need at least 3 points to create a polygon");
351                 return;
352         }
353                 Layer::Handle layer;
354                 Canvas::Handle canvas(get_canvas_view()->get_canvas());
355                 int depth(0);
356
357                 // we are temporarily using the layer to hold something
358                 layer=get_canvas_view()->get_selection_manager()->get_selected_layer();
359                 if(layer)
360                 {
361                         depth=layer->get_depth();
362                         canvas=layer->get_canvas();
363                 }
364
365                 {
366                         synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("New Polygon"));
367                         synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
368
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());
372
373                         layer->disconnect_dynamic_param("vector_list");
374                         if(!layer->set_param("vector_list",polygon_point_list))
375                         {
376                                 group.cancel();
377                                 get_canvas_view()->get_ui_interface()->error("Unable to set layer parameter");
378                                 return;
379                         }
380
381                         {
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))
390                                 {
391                                         group.cancel();
392                                         get_canvas_view()->get_ui_interface()->error("Unable to execute action \"value_desc_convert\"");
393                                         return;
394                                 }
395                         }
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()();
401                 }
402 /*
403                 else
404                 {
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))
409                                         return true;
410                 }
411 */
412         reset();
413         increment_id();
414 }
415
416 Smach::event_result
417 StatePolygon_Context::event_mouse_click_handler(const Smach::event& x)
418 {
419         synfig::info("STATE ROTOPOLYGON: Received mouse button down Event");
420         const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
421         switch(event.button)
422         {
423         case BUTTON_LEFT:
424                 polygon_point_list.push_back(get_work_area()->snap_point_to_grid(event.pos));
425                 refresh_ducks();
426                 return Smach::RESULT_ACCEPT;
427
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;
431
432         default:
433                 return Smach::RESULT_OK;
434         }
435 }
436
437
438 void
439 StatePolygon_Context::refresh_ducks()
440 {
441         get_work_area()->clear_ducks();
442
443         if(polygon_point_list.empty()) return;
444
445         std::list<synfig::Point>::iterator iter=polygon_point_list.begin();
446
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)
452         );
453         duck->signal_user_click(0).connect(sigc::mem_fun(*this,&StatePolygon_Context::run));
454
455         get_work_area()->add_duck(duck);
456
457         for(++iter;iter!=polygon_point_list.end();++iter)
458         {
459                 etl::handle<WorkArea::Bezier> bezier(new WorkArea::Bezier());
460                 bezier->p1=bezier->c1=duck;
461
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)
467                 );
468
469                 get_work_area()->add_duck(duck);
470
471                 bezier->p2=bezier->c2=duck;
472                 get_work_area()->add_bezier(bezier);
473         }
474         get_work_area()->queue_draw();
475 }
476
477
478 bool
479 StatePolygon_Context::on_polygon_duck_change(const synfig::Point &point, std::list<synfig::Point>::iterator iter)
480 {
481         *iter=point;
482         return true;
483 }