410947a4d550edf8130646a30ca88bbdb85beb7d
[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;
192                          (int)id.size()-1 >= digits && id[id.size()-1-digits] <= '9' && id[id.size()-1-digits] >= '0';
193                          digits++)
194                         ;
195
196                 String str_number;
197                 str_number=String(id,id.size()-digits,id.size());
198                 id=String(id,0,id.size()-digits);
199
200                 number=atoi(str_number.c_str());
201         }
202         else
203         {
204                 number=1;
205                 digits=3;
206         }
207
208         number++;
209
210         // Add the number back onto the id
211         {
212                 const String format(strprintf("%%0%dd",digits));
213                 id+=strprintf(format.c_str(),number);
214         }
215
216         // Set the ID
217         set_id(id);
218 }
219
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()),
226         entry_id(),
227         button_make(_("Make"))
228 {
229         egress_on_selection_change=true;
230         load_settings();
231
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();
240
241
242         // Turn off layer clicking
243         get_work_area()->set_allow_layer_clicks(false);
244
245         // clear out the ducks
246         get_work_area()->clear_ducks();
247
248         // Refresh the work area
249         get_work_area()->queue_draw();
250
251         get_canvas_view()->work_area->set_cursor(Gdk::CROSSHAIR);
252
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();
256
257         // Disable the time bar
258         get_canvas_view()->set_sensitive_timebar(false);
259
260         // Connect a signal
261         //get_work_area()->signal_user_click().connect(sigc::mem_fun(*this,&studio::StatePolygon_Context::on_user_click));
262
263         App::toolbox->refresh();
264 }
265
266 void
267 StatePolygon_Context::refresh_tool_options()
268 {
269         App::dialog_tool_options->clear();
270         App::dialog_tool_options->set_widget(options_table);
271
272         App::dialog_tool_options->set_local_name(_("Polygon Tool"));
273         App::dialog_tool_options->set_name("polygon");
274
275         App::dialog_tool_options->add_button(
276                 Gtk::StockID("gtk-execute"),
277                 _("Make Polygon")
278         )->signal_clicked().connect(
279                 sigc::mem_fun(
280                         *this,
281                         &StatePolygon_Context::run
282                 )
283         );
284
285         App::dialog_tool_options->add_button(
286                 Gtk::StockID("gtk-clear"),
287                 _("Clear current Polygon")
288         )->signal_clicked().connect(
289                 sigc::mem_fun(
290                         *this,
291                         &StatePolygon_Context::reset
292                 )
293         );
294 }
295
296 Smach::event_result
297 StatePolygon_Context::event_refresh_tool_options(const Smach::event& /*x*/)
298 {
299         refresh_tool_options();
300         return Smach::RESULT_ACCEPT;
301 }
302
303 StatePolygon_Context::~StatePolygon_Context()
304 {
305         run();
306
307         save_settings();
308         // Restore layer clicking
309         get_work_area()->set_allow_layer_clicks(prev_workarea_layer_status_);
310
311         App::dialog_tool_options->clear();
312
313         get_canvas_view()->work_area->reset_cursor();
314
315         // Enable the time bar
316         get_canvas_view()->set_sensitive_timebar(true);
317
318         // Bring back the tables if they were out before
319         if(prev_table_status)get_canvas_view()->show_tables();
320
321         // Refresh the work area
322         get_work_area()->queue_draw();
323
324         App::toolbox->refresh();
325 }
326
327 Smach::event_result
328 StatePolygon_Context::event_stop_handler(const Smach::event& /*x*/)
329 {
330         synfig::info("STATE RotoPolygon: Received Stop Event");
331         //throw Smach::egress_exception();
332         reset();
333         return Smach::RESULT_ACCEPT;
334
335 }
336
337 Smach::event_result
338 StatePolygon_Context::event_refresh_handler(const Smach::event& /*x*/)
339 {
340         synfig::info("STATE RotoPolygon: Received Refresh Event");
341         refresh_ducks();
342         return Smach::RESULT_ACCEPT;
343 }
344
345 void
346 StatePolygon_Context::run()
347 {
348         if(polygon_point_list.empty())
349                 return;
350
351         if(polygon_point_list.size()<3)
352         {
353                 get_canvas_view()->get_ui_interface()->error("You need at least 3 points to create a polygon");
354                 return;
355         }
356                 Layer::Handle layer;
357                 Canvas::Handle canvas(get_canvas_view()->get_canvas());
358                 int depth(0);
359
360                 // we are temporarily using the layer to hold something
361                 layer=get_canvas_view()->get_selection_manager()->get_selected_layer();
362                 if(layer)
363                 {
364                         depth=layer->get_depth();
365                         canvas=layer->get_canvas();
366                 }
367
368                 {
369                         synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("New Polygon"));
370                         synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
371
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());
375
376                         layer->disconnect_dynamic_param("vector_list");
377                         if(!layer->set_param("vector_list",polygon_point_list))
378                         {
379                                 group.cancel();
380                                 get_canvas_view()->get_ui_interface()->error("Unable to set layer parameter");
381                                 return;
382                         }
383
384                         {
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))
393                                 {
394                                         group.cancel();
395                                         get_canvas_view()->get_ui_interface()->error("Unable to execute action \"value_desc_convert\"");
396                                         return;
397                                 }
398                         }
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()();
404                 }
405 /*
406                 else
407                 {
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))
412                                         return true;
413                 }
414 */
415         reset();
416         increment_id();
417 }
418
419 Smach::event_result
420 StatePolygon_Context::event_mouse_click_handler(const Smach::event& x)
421 {
422         synfig::info("STATE ROTOPOLYGON: Received mouse button down Event");
423         const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
424         switch(event.button)
425         {
426         case BUTTON_LEFT:
427                 polygon_point_list.push_back(get_work_area()->snap_point_to_grid(event.pos));
428                 refresh_ducks();
429                 return Smach::RESULT_ACCEPT;
430
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;
434
435         default:
436                 return Smach::RESULT_OK;
437         }
438 }
439
440
441 void
442 StatePolygon_Context::refresh_ducks()
443 {
444         get_work_area()->clear_ducks();
445
446         if(polygon_point_list.empty()) return;
447
448         std::list<synfig::Point>::iterator iter=polygon_point_list.begin();
449
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)
455         );
456         duck->signal_user_click(0).connect(sigc::mem_fun(*this,&StatePolygon_Context::run));
457
458         get_work_area()->add_duck(duck);
459
460         for(++iter;iter!=polygon_point_list.end();++iter)
461         {
462                 etl::handle<WorkArea::Bezier> bezier(new WorkArea::Bezier());
463                 bezier->p1=bezier->c1=duck;
464
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)
470                 );
471
472                 get_work_area()->add_duck(duck);
473
474                 bezier->p2=bezier->c2=duck;
475                 get_work_area()->add_bezier(bezier);
476         }
477         get_work_area()->queue_draw();
478 }
479
480
481 bool
482 StatePolygon_Context::on_polygon_duck_change(const synfig::Point &point, std::list<synfig::Point>::iterator iter)
483 {
484         *iter=point;
485         return true;
486 }