942d6d865efa6b7ee3255be56daf48f8243c328f
[synfig.git] /
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
10 **
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.
15 **
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
21 */
22 /* ========================================================================= */
23
24 /* === H E A D E R S ======================================================= */
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include <gtkmm/dialog.h>
34 #include <gtkmm/entry.h>
35
36 #include <synfig/valuenode_dynamiclist.h>
37 #include <synfigapp/action_system.h>
38
39 #include "state_polygon.h"
40 #include "canvasview.h"
41 #include "workarea.h"
42 #include "app.h"
43
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>
50
51 #include "general.h"
52
53 #endif
54
55 /* === U S I N G =========================================================== */
56
57 using namespace std;
58 using namespace etl;
59 using namespace synfig;
60 using namespace studio;
61
62 /* === M A C R O S ========================================================= */
63
64 /* === G L O B A L S ======================================================= */
65
66 StatePolygon studio::state_polygon;
67
68 /* === C L A S S E S & S T R U C T S ======================================= */
69
70 class studio::StatePolygon_Context : public sigc::trackable
71 {
72         etl::handle<CanvasView> canvas_view_;
73         CanvasView::IsWorking is_working;
74
75         bool prev_table_status;
76         bool prev_workarea_layer_status_;
77
78         Gtk::Menu menu;
79
80         Duckmatic::Push duckmatic_push;
81
82         std::list<synfig::Point> polygon_point_list;
83         synfigapp::Settings& settings;
84
85
86         bool on_polygon_duck_change(const synfig::Point &point, std::list<synfig::Point>::iterator iter);
87
88
89         void popup_handle_menu(synfigapp::ValueDesc value_desc);
90
91
92         void refresh_ducks();
93
94         Gtk::Table options_table;
95         Gtk::Entry entry_id;
96         Gtk::Button button_make;
97
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); }
101
102         Smach::event_result event_stop_handler(const Smach::event& x);
103
104         Smach::event_result event_refresh_handler(const Smach::event& x);
105
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();
109
110         StatePolygon_Context(CanvasView* canvas_view);
111
112         ~StatePolygon_Context();
113
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();}
118
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*/)
126         {
127                 if(egress_on_selection_change)
128                         throw Smach::egress_exception();
129                 return Smach::RESULT_OK;
130         }
131
132         void run();
133 };      // END of class StatePolygon_Context
134
135 /* === M E T H O D S ======================================================= */
136
137 StatePolygon::StatePolygon():
138         Smach::state<StatePolygon_Context>("polygon")
139 {
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));
146 }
147
148 StatePolygon::~StatePolygon()
149 {
150 }
151
152 void
153 StatePolygon_Context::load_settings()
154 {
155         String value;
156
157         if(settings.get_value("polygon.id",value))
158                 set_id(value);
159         else
160                 set_id("Polygon");
161 }
162
163 void
164 StatePolygon_Context::save_settings()
165 {
166         settings.set_value("polygon.id",get_id().c_str());
167 }
168
169 void
170 StatePolygon_Context::reset()
171 {
172         polygon_point_list.clear();
173         refresh_ducks();
174 }
175
176 void
177 StatePolygon_Context::increment_id()
178 {
179         String id(get_id());
180         int number=1;
181         int digits=0;
182
183         if(id.empty())
184                 id="Polygon";
185
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')
190         {
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++)
195                         ;
196
197                 String str_number;
198                 str_number=String(id,id.size()-digits,id.size());
199                 id=String(id,0,id.size()-digits);
200
201                 number=atoi(str_number.c_str());
202         }
203         else
204         {
205                 number=1;
206                 digits=3;
207         }
208
209         number++;
210
211         // Add the number back onto the id
212         {
213                 const String format(strprintf("%%0%dd",digits));
214                 id+=strprintf(format.c_str(),number);
215         }
216
217         // Set the ID
218         set_id(id);
219 }
220
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"))
229 {
230         egress_on_selection_change=true;
231         load_settings();
232
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();
241
242
243         // Turn off layer clicking
244         get_work_area()->set_allow_layer_clicks(false);
245
246         // clear out the ducks
247         get_work_area()->clear_ducks();
248
249         // Refresh the work area
250         get_work_area()->queue_draw();
251
252         get_canvas_view()->work_area->set_cursor(Gdk::CROSSHAIR);
253
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();
257
258         // Disable the time bar
259         get_canvas_view()->set_sensitive_timebar(false);
260
261         // Connect a signal
262         //get_work_area()->signal_user_click().connect(sigc::mem_fun(*this,&studio::StatePolygon_Context::on_user_click));
263
264         App::toolbox->refresh();
265 }
266
267 void
268 StatePolygon_Context::refresh_tool_options()
269 {
270         App::dialog_tool_options->clear();
271         App::dialog_tool_options->set_widget(options_table);
272
273         App::dialog_tool_options->set_local_name(_("Polygon Tool"));
274         App::dialog_tool_options->set_name("polygon");
275
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
283                 )
284         );
285
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
293                 )
294         );
295 }
296
297 Smach::event_result
298 StatePolygon_Context::event_refresh_tool_options(const Smach::event& /*x*/)
299 {
300         refresh_tool_options();
301         return Smach::RESULT_ACCEPT;
302 }
303
304 StatePolygon_Context::~StatePolygon_Context()
305 {
306         run();
307
308         save_settings();
309         // Restore layer clicking
310         get_work_area()->set_allow_layer_clicks(prev_workarea_layer_status_);
311
312         App::dialog_tool_options->clear();
313
314         get_canvas_view()->work_area->reset_cursor();
315
316         // Enable the time bar
317         get_canvas_view()->set_sensitive_timebar(true);
318
319         // Bring back the tables if they were out before
320         if(prev_table_status)get_canvas_view()->show_tables();
321
322         // Refresh the work area
323         get_work_area()->queue_draw();
324
325         App::toolbox->refresh();
326 }
327
328 Smach::event_result
329 StatePolygon_Context::event_stop_handler(const Smach::event& /*x*/)
330 {
331         synfig::info("STATE RotoPolygon: Received Stop Event");
332         //throw Smach::egress_exception();
333         reset();
334         return Smach::RESULT_ACCEPT;
335
336 }
337
338 Smach::event_result
339 StatePolygon_Context::event_refresh_handler(const Smach::event& /*x*/)
340 {
341         synfig::info("STATE RotoPolygon: Received Refresh Event");
342         refresh_ducks();
343         return Smach::RESULT_ACCEPT;
344 }
345
346 void
347 StatePolygon_Context::run()
348 {
349         if(polygon_point_list.empty())
350                 return;
351
352         if(polygon_point_list.size()<3)
353         {
354                 get_canvas_view()->get_ui_interface()->error("You need at least 3 points to create a polygon");
355                 return;
356         }
357                 Layer::Handle layer;
358                 Canvas::Handle canvas(get_canvas_view()->get_canvas());
359                 int depth(0);
360
361                 // we are temporarily using the layer to hold something
362                 layer=get_canvas_view()->get_selection_manager()->get_selected_layer();
363                 if(layer)
364                 {
365                         depth=layer->get_depth();
366                         canvas=layer->get_canvas();
367                 }
368
369                 {
370                         synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("New Polygon"));
371                         synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
372
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());
376
377                         layer->disconnect_dynamic_param("vector_list");
378                         if(!layer->set_param("vector_list",polygon_point_list))
379                         {
380                                 group.cancel();
381                                 get_canvas_view()->get_ui_interface()->error("Unable to set layer parameter");
382                                 return;
383                         }
384
385                         {
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))
394                                 {
395                                         group.cancel();
396                                         get_canvas_view()->get_ui_interface()->error("Unable to execute action \"value_desc_convert\"");
397                                         return;
398                                 }
399                         }
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()();
409                 }
410 /*
411                 else
412                 {
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))
417                                         return true;
418                 }
419 */
420         reset();
421         increment_id();
422 }
423
424 Smach::event_result
425 StatePolygon_Context::event_mouse_click_handler(const Smach::event& x)
426 {
427         synfig::info("STATE ROTOPOLYGON: Received mouse button down Event");
428         const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
429         switch(event.button)
430         {
431         case BUTTON_LEFT:
432                 polygon_point_list.push_back(get_work_area()->snap_point_to_grid(event.pos));
433                 refresh_ducks();
434                 return Smach::RESULT_ACCEPT;
435
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;
439
440         default:
441                 return Smach::RESULT_OK;
442         }
443 }
444
445
446 void
447 StatePolygon_Context::refresh_ducks()
448 {
449         get_work_area()->clear_ducks();
450
451         if(polygon_point_list.empty()) return;
452
453         std::list<synfig::Point>::iterator iter=polygon_point_list.begin();
454
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)
460         );
461         duck->signal_user_click(0).connect(sigc::mem_fun(*this,&StatePolygon_Context::run));
462
463         get_work_area()->add_duck(duck);
464
465         for(++iter;iter!=polygon_point_list.end();++iter)
466         {
467                 etl::handle<WorkArea::Bezier> bezier(new WorkArea::Bezier());
468                 bezier->p1=bezier->c1=duck;
469
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)
475                 );
476
477                 get_work_area()->add_duck(duck);
478
479                 bezier->p2=bezier->c2=duck;
480                 get_work_area()->add_bezier(bezier);
481         }
482         get_work_area()->queue_draw();
483 }
484
485
486 bool
487 StatePolygon_Context::on_polygon_duck_change(const synfig::Point &point, std::list<synfig::Point>::iterator iter)
488 {
489         *iter=point;
490         return true;
491 }