Implements PXEGeek's http://wiki.synfig.com/Wish_list entry: "Optionally display...
[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 #endif
51
52 /* === U S I N G =========================================================== */
53
54 using namespace std;
55 using namespace etl;
56 using namespace synfig;
57 using namespace studio;
58
59 /* === M A C R O S ========================================================= */
60
61 /* === G L O B A L S ======================================================= */
62
63 StatePolygon studio::state_polygon;
64
65 /* === C L A S S E S & S T R U C T S ======================================= */
66
67 class studio::StatePolygon_Context : public sigc::trackable
68 {
69         etl::handle<CanvasView> canvas_view_;
70         CanvasView::IsWorking is_working;
71
72         bool prev_table_status;
73         bool prev_workarea_layer_status_;
74
75         Gtk::Menu menu;
76
77         Duckmatic::Push duckmatic_push;
78
79         std::list<synfig::Point> polygon_point_list;
80         synfigapp::Settings& settings;
81
82
83         bool on_polygon_duck_change(const synfig::Point &point, std::list<synfig::Point>::iterator iter);
84
85
86         void popup_handle_menu(synfigapp::ValueDesc value_desc);
87
88
89         void refresh_ducks();
90
91         Gtk::Table options_table;
92         Gtk::Entry entry_id;
93         Gtk::Button button_make;
94
95 public:
96         synfig::String get_id()const { return entry_id.get_text(); }
97         void set_id(const synfig::String& x) { return entry_id.set_text(x); }
98
99         Smach::event_result event_stop_handler(const Smach::event& x);
100
101         Smach::event_result event_refresh_handler(const Smach::event& x);
102
103         Smach::event_result event_mouse_click_handler(const Smach::event& x);
104         Smach::event_result event_refresh_tool_options(const Smach::event& x);
105         void refresh_tool_options();
106
107         StatePolygon_Context(CanvasView* canvas_view);
108
109         ~StatePolygon_Context();
110
111         const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
112         etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
113         synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
114         WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
115
116         //void on_user_click(synfig::Point point);
117         void load_settings();
118         void save_settings();
119         void reset();
120         void increment_id();
121         bool no_egress_on_selection_change;
122         Smach::event_result event_layer_selection_changed_handler(const Smach::event& x)
123         {
124                 if(!no_egress_on_selection_change)
125                         throw Smach::egress_exception();
126                 return Smach::RESULT_OK;
127         }
128
129         void run();
130 };      // END of class StatePolygon_Context
131
132 /* === M E T H O D S ======================================================= */
133
134 StatePolygon::StatePolygon():
135         Smach::state<StatePolygon_Context>("polygon")
136 {
137         insert(event_def(EVENT_LAYER_SELECTION_CHANGED,&StatePolygon_Context::event_layer_selection_changed_handler));
138         insert(event_def(EVENT_STOP,&StatePolygon_Context::event_stop_handler));
139         insert(event_def(EVENT_REFRESH,&StatePolygon_Context::event_refresh_handler));
140         insert(event_def(EVENT_REFRESH_DUCKS,&StatePolygon_Context::event_refresh_handler));
141         insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,&StatePolygon_Context::event_mouse_click_handler));
142         insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StatePolygon_Context::event_refresh_tool_options));
143 }
144
145 StatePolygon::~StatePolygon()
146 {
147 }
148
149 void
150 StatePolygon_Context::load_settings()
151 {
152         String value;
153
154         if(settings.get_value("polygon.id",value))
155                 set_id(value);
156         else
157                 set_id("Polygon");
158 }
159
160 void
161 StatePolygon_Context::save_settings()
162 {
163         settings.set_value("polygon.id",get_id().c_str());
164 }
165
166 void
167 StatePolygon_Context::reset()
168 {
169         polygon_point_list.clear();
170         refresh_ducks();
171 }
172
173 void
174 StatePolygon_Context::increment_id()
175 {
176         String id(get_id());
177         int number=1;
178         int digits=0;
179
180         if(id.empty())
181                 id="Polygon";
182
183         // If there is a number
184         // already at the end of the
185         // id, then remove it.
186         if(id[id.size()-1]<='9' && id[id.size()-1]>='0')
187         {
188                 // figure out how many digits it is
189                 for(digits=0;(int)id.size()-1>=digits && id[id.size()-1-digits]<='9' && id[id.size()-1-digits]>='0';digits++)while(false);
190
191                 String str_number;
192                 str_number=String(id,id.size()-digits,id.size());
193                 id=String(id,0,id.size()-digits);
194
195                 number=atoi(str_number.c_str());
196         }
197         else
198         {
199                 number=1;
200                 digits=3;
201         }
202
203         number++;
204
205         // Add the number back onto the id
206         {
207                 const String format(strprintf("%%0%dd",digits));
208                 id+=strprintf(format.c_str(),number);
209         }
210
211         // Set the ID
212         set_id(id);
213 }
214
215 StatePolygon_Context::StatePolygon_Context(CanvasView* canvas_view):
216         canvas_view_(canvas_view),
217         is_working(*canvas_view),
218         prev_workarea_layer_status_(get_work_area()->allow_layer_clicks),
219         duckmatic_push(get_work_area()),
220         settings(synfigapp::Main::get_selected_input_device()->settings()),
221         entry_id(),
222         button_make(_("Make"))
223 {
224         no_egress_on_selection_change=false;
225         load_settings();
226
227         // Set up the tool options dialog
228         //options_table.attach(*manage(new Gtk::Label(_("Polygon Tool"))), 0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
229         options_table.attach(entry_id, 0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
230         //options_table.attach(button_make, 0, 2, 4, 5, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
231         button_make.signal_pressed().connect(sigc::mem_fun(*this,&StatePolygon_Context::run));
232         options_table.show_all();
233         refresh_tool_options();
234         App::dialog_tool_options->present();
235
236
237         // Turn off layer clicking
238         get_work_area()->allow_layer_clicks=false;
239
240         // clear out the ducks
241         get_work_area()->clear_ducks();
242
243         // Refresh the work area
244         get_work_area()->queue_draw();
245
246         get_canvas_view()->work_area->set_cursor(Gdk::CROSSHAIR);
247
248         // Hide the tables if they are showing
249         prev_table_status=get_canvas_view()->tables_are_visible();
250         if(prev_table_status)get_canvas_view()->hide_tables();
251
252         // Hide the time bar
253         get_canvas_view()->hide_timebar();
254
255         // Connect a signal
256         //get_work_area()->signal_user_click().connect(sigc::mem_fun(*this,&studio::StatePolygon_Context::on_user_click));
257
258         App::toolbox->refresh();
259 }
260
261 void
262 StatePolygon_Context::refresh_tool_options()
263 {
264         App::dialog_tool_options->clear();
265         App::dialog_tool_options->set_widget(options_table);
266
267         App::dialog_tool_options->set_local_name(_("Polygon Tool"));
268         App::dialog_tool_options->set_name("polygon");
269
270         App::dialog_tool_options->add_button(
271                 Gtk::StockID("gtk-execute"),
272                 _("Make Polygon")
273         )->signal_clicked().connect(
274                 sigc::mem_fun(
275                         *this,
276                         &StatePolygon_Context::run
277                 )
278         );
279
280         App::dialog_tool_options->add_button(
281                 Gtk::StockID("gtk-clear"),
282                 _("Clear current Polygon")
283         )->signal_clicked().connect(
284                 sigc::mem_fun(
285                         *this,
286                         &StatePolygon_Context::reset
287                 )
288         );
289 }
290
291 Smach::event_result
292 StatePolygon_Context::event_refresh_tool_options(const Smach::event& x)
293 {
294         refresh_tool_options();
295         return Smach::RESULT_ACCEPT;
296 }
297
298 StatePolygon_Context::~StatePolygon_Context()
299 {
300         run();
301
302         save_settings();
303         // Restore layer clicking
304         get_work_area()->allow_layer_clicks=prev_workarea_layer_status_;
305
306         App::dialog_tool_options->clear();
307
308         get_canvas_view()->work_area->reset_cursor();
309
310         // Show the time bar
311         if(get_canvas_view()->get_canvas()->rend_desc().get_time_start()!=get_canvas_view()->get_canvas()->rend_desc().get_time_end())
312                 get_canvas_view()->show_timebar();
313
314         // Bring back the tables if they were out before
315         if(prev_table_status)get_canvas_view()->show_tables();
316
317         // Refresh the work area
318         get_work_area()->queue_draw();
319
320         App::toolbox->refresh();
321 }
322
323 Smach::event_result
324 StatePolygon_Context::event_stop_handler(const Smach::event& x)
325 {
326         synfig::info("STATE RotoPolygon: Received Stop Event");
327         //throw Smach::egress_exception();
328         reset();
329         return Smach::RESULT_ACCEPT;
330
331 }
332
333 Smach::event_result
334 StatePolygon_Context::event_refresh_handler(const Smach::event& x)
335 {
336         synfig::info("STATE RotoPolygon: Received Refresh Event");
337         refresh_ducks();
338         return Smach::RESULT_ACCEPT;
339 }
340
341 void
342 StatePolygon_Context::run()
343 {
344         if(polygon_point_list.empty())
345                 return;
346
347         if(polygon_point_list.size()<3)
348         {
349                 get_canvas_view()->get_ui_interface()->error("You need at least 3 points to create a polygon");
350                 return;
351         }
352                 Layer::Handle layer;
353                 Canvas::Handle canvas(get_canvas_view()->get_canvas());
354                 int depth(0);
355
356                 // we are temporarily using the layer to hold something
357                 layer=get_canvas_view()->get_selection_manager()->get_selected_layer();
358                 if(layer)
359                 {
360                         depth=layer->get_depth();
361                         canvas=layer->get_canvas();
362                 }
363
364                 {
365                         synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("New Polygon"));
366                         synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
367
368                         Layer::Handle layer(get_canvas_interface()->add_layer_to("polygon",canvas,depth));
369                         layer->set_description(get_id());
370                         get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
371
372                         layer->disconnect_dynamic_param("vector_list");
373                         if(!layer->set_param("vector_list",polygon_point_list))
374                         {
375                                 group.cancel();
376                                 get_canvas_view()->get_ui_interface()->error("Unable to set layer parameter");
377                                 return;
378                         }
379
380                         {
381                                 synfigapp::Action::Handle action(synfigapp::Action::create("value_desc_convert"));
382                                 synfigapp::ValueDesc value_desc(layer,"vector_list");
383                                 action->set_param("canvas",get_canvas());
384                                 action->set_param("canvas_interface",get_canvas_interface());
385                                 action->set_param("value_desc",value_desc);
386                                 action->set_param("type","dynamic_list");
387                                 if(!get_canvas_interface()->get_instance()->perform_action(action))
388                                 {
389                                         group.cancel();
390                                         get_canvas_view()->get_ui_interface()->error("Unable to execute action \"value_desc_convert\"");
391                                         return;
392                                 }
393                         }
394                         no_egress_on_selection_change=true;
395                         get_canvas_interface()->get_selection_manager()->clear_selected_layers();
396                         get_canvas_interface()->get_selection_manager()->set_selected_layer(layer);
397                         no_egress_on_selection_change=false;
398                         //get_canvas_interface()->signal_dirty_preview()();
399                 }
400 /*
401                 else
402                 {
403                         ValueNode::Handle value_node=(ValueNode_Const::create(polygon_point_list));
404                         std::string valuenode_name="Poly";
405                         while(studio::App::dialog_entry("New Polygon", "Please enter the new ID for this value_node",valuenode_name))
406                                 if(get_canvas_interface()->add_value_node(value_node,valuenode_name))
407                                         return true;
408                 }
409 */
410         reset();
411         increment_id();
412 }
413
414 Smach::event_result
415 StatePolygon_Context::event_mouse_click_handler(const Smach::event& x)
416 {
417         synfig::info("STATE ROTOPOLYGON: Received mouse button down Event");
418         const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
419         switch(event.button)
420         {
421         case BUTTON_LEFT:
422                 polygon_point_list.push_back(get_work_area()->snap_point_to_grid(event.pos));
423                 refresh_ducks();
424                 return Smach::RESULT_ACCEPT;
425
426         case BUTTON_RIGHT: // Intercept the right-button click to short-circut the pop-up menu
427                 return Smach::RESULT_ACCEPT;
428
429         default:
430                 return Smach::RESULT_OK;
431         }
432 }
433
434
435 void
436 StatePolygon_Context::refresh_ducks()
437 {
438         get_work_area()->clear_ducks();
439
440         if(polygon_point_list.empty()) return;
441
442         std::list<synfig::Point>::iterator iter=polygon_point_list.begin();
443
444         etl::handle<WorkArea::Duck> duck;
445         duck=new WorkArea::Duck(*iter);
446         duck->set_editable(true);
447         duck->signal_edited().connect(
448                 sigc::bind(sigc::mem_fun(*this,&studio::StatePolygon_Context::on_polygon_duck_change),iter)
449         );
450         duck->signal_user_click(0).connect(sigc::mem_fun(*this,&StatePolygon_Context::run));
451
452         get_work_area()->add_duck(duck);
453
454         for(++iter;iter!=polygon_point_list.end();++iter)
455         {
456                 etl::handle<WorkArea::Bezier> bezier(new WorkArea::Bezier());
457                 bezier->p1=bezier->c1=duck;
458
459                 duck=new WorkArea::Duck(*iter);
460                 duck->set_editable(true);
461                 duck->set_name(strprintf("%x",&*iter));
462                 duck->signal_edited().connect(
463                         sigc::bind(sigc::mem_fun(*this,&studio::StatePolygon_Context::on_polygon_duck_change),iter)
464                 );
465
466                 get_work_area()->add_duck(duck);
467
468                 bezier->p2=bezier->c2=duck;
469                 get_work_area()->add_bezier(bezier);
470         }
471         get_work_area()->queue_draw();
472 }
473
474
475 bool
476 StatePolygon_Context::on_polygon_duck_change(const synfig::Point &point, std::list<synfig::Point>::iterator iter)
477 {
478         *iter=point;
479         return true;
480 }