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