my log
[synfig.git] / synfig-studio / trunk / src / gtkmm / state_circle.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file state_circle.cpp
3 **      \brief Template File
4 **
5 **      $Id: state_circle.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 <synfig/valuenode_dynamiclist.h>
35 #include <synfigapp/action_system.h>
36
37 #include "state_circle.h"
38 #include "canvasview.h"
39 #include "workarea.h"
40 #include "app.h"
41
42 #include <synfigapp/action.h>
43 #include "event_mouse.h"
44 #include "event_layerclick.h"
45 #include "toolbox.h"
46 #include "dialog_tooloptions.h"
47 #include <gtkmm/optionmenu.h>
48 #include "duck.h"
49 #include "widget_enum.h"
50 #include <synfigapp/main.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 enum CircleFalloff
63 {
64         CIRCLE_SQUARED  =0,
65         CIRCLE_INTERPOLATION_LINEAR     =1,
66         CIRCLE_COSINE   =2,
67         CIRCLE_SIGMOND  =3,
68         CIRCLE_SQRT             =4,
69         CIRCLE_NUM_FALLOFF
70 };
71
72 /* === G L O B A L S ======================================================= */
73
74 StateCircle studio::state_circle;
75
76 /* === C L A S S E S & S T R U C T S ======================================= */
77
78 class studio::StateCircle_Context : public sigc::trackable
79 {
80         etl::handle<CanvasView> canvas_view_;
81         CanvasView::IsWorking is_working;
82         
83         Duckmatic::Push duckmatic_push;
84         
85         Point point_holder;
86         
87         etl::handle<Duck> point2_duck;
88
89         void refresh_ducks();
90         
91         bool prev_workarea_layer_status_;
92                 
93         //Toolbox settings
94         synfigapp::Settings& settings;
95         
96         //Toolbox display
97         Gtk::Table options_table;
98         
99         Gtk::Entry              entry_id; //what to name the layer
100         
101         Widget_Enum             enum_falloff;
102         Widget_Enum             enum_blend;
103         
104         Gtk::Adjustment adj_feather;
105         Gtk::SpinButton spin_feather;
106         
107         Gtk::CheckButton check_invert;
108         
109 public:
110
111         synfig::String get_id()const { return entry_id.get_text(); }
112         void set_id(const synfig::String& x) { return entry_id.set_text(x); }
113
114         int get_falloff()const { return enum_falloff.get_value(); }
115         void set_falloff(int x) { return enum_falloff.set_value(x); }
116         
117         int get_blend()const { return enum_blend.get_value(); }
118         void set_blend(int x) { return enum_blend.set_value(x); }
119         
120         Real get_feather()const { return adj_feather.get_value(); }
121         void set_feather(Real f) { adj_feather.set_value(f); }
122         
123         bool get_invert()const { return check_invert.get_active(); }
124         void set_invert(bool i) { check_invert.set_active(i); }
125         
126         void refresh_tool_options(); //to refresh the toolbox   
127
128         //events
129         Smach::event_result event_stop_handler(const Smach::event& x);
130         Smach::event_result event_refresh_handler(const Smach::event& x);
131         Smach::event_result event_mouse_click_handler(const Smach::event& x);
132         Smach::event_result event_refresh_tool_options(const Smach::event& x);
133
134         //constructor destructor
135         StateCircle_Context(CanvasView* canvas_view);
136         ~StateCircle_Context();
137
138         //Canvas interaction
139         const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
140         etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
141         synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
142         WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
143         
144         //Modifying settings etc.
145         void load_settings();
146         void save_settings();
147         void reset();
148         void increment_id();
149         bool no_egress_on_selection_change;
150         Smach::event_result event_layer_selection_changed_handler(const Smach::event& x)
151         {
152                 if(!no_egress_on_selection_change)
153                         throw Smach::egress_exception();
154                 return Smach::RESULT_OK;
155         }
156
157         void make_circle(const Point& p1, const Point& p2);
158         
159 };      // END of class StateGradient_Context
160
161 /* === M E T H O D S ======================================================= */
162
163 StateCircle::StateCircle():
164         Smach::state<StateCircle_Context>("circle")
165 {
166         insert(event_def(EVENT_LAYER_SELECTION_CHANGED,&StateCircle_Context::event_layer_selection_changed_handler));
167         insert(event_def(EVENT_STOP,&StateCircle_Context::event_stop_handler));
168         insert(event_def(EVENT_REFRESH,&StateCircle_Context::event_refresh_handler));
169         insert(event_def(EVENT_REFRESH_DUCKS,&StateCircle_Context::event_refresh_handler));
170         insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,&StateCircle_Context::event_mouse_click_handler));
171         insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DRAG,&StateCircle_Context::event_mouse_click_handler));
172         insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_UP,&StateCircle_Context::event_mouse_click_handler));
173         insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateCircle_Context::event_refresh_tool_options));
174 }       
175
176 StateCircle::~StateCircle()
177 {
178 }
179
180 void
181 StateCircle_Context::load_settings()
182 {       
183         String value;
184         
185         //parse the arguments yargh!
186         if(settings.get_value("circle.id",value))
187                 set_id(value);
188         else
189                 set_id("Circle");
190
191         if(settings.get_value("circle.fallofftype",value) && value != "")
192                 set_falloff(atoi(value.c_str()));
193         else
194                 set_falloff(2);
195         
196         if(settings.get_value("circle.blend",value) && value != "")
197                 set_blend(atoi(value.c_str()));
198         else
199                 set_blend(0);//(int)Color::BLEND_COMPOSITE); //0 should be blend composites value
200         
201         if(settings.get_value("circle.feather",value))
202                 set_feather(atof(value.c_str()));
203         else
204                 set_feather(0);
205         
206         if(settings.get_value("circle.invert",value) && value != "0")
207                 set_invert(true);
208         else
209                 set_invert(false);
210 }
211
212 void
213 StateCircle_Context::save_settings()
214 {       
215         settings.set_value("circle.id",get_id());
216         settings.set_value("circle.fallofftype",strprintf("%d",get_falloff()));
217         settings.set_value("circle.blend",strprintf("%d",get_blend()));
218         settings.set_value("circle.feather",strprintf("%f",(float)get_feather()));
219         settings.set_value("circle.invert",get_invert()?"1":"0");
220 }
221
222 void
223 StateCircle_Context::reset()
224 {
225         refresh_ducks();
226 }
227
228 void
229 StateCircle_Context::increment_id()
230 {
231         String id(get_id());
232         int number=1;
233         int digits=0;
234         
235         if(id.empty())
236                 id="Circle";
237         
238         // If there is a number
239         // already at the end of the
240         // id, then remove it.
241         if(id[id.size()-1]<='9' && id[id.size()-1]>='0')
242         {
243                 // figure out how many digits it is
244                 for(digits=0;(int)id.size()-1>=digits && id[id.size()-1-digits]<='9' && id[id.size()-1-digits]>='0';digits++)while(false);
245                 
246                 String str_number;
247                 str_number=String(id,id.size()-digits,id.size());
248                 id=String(id,0,id.size()-digits);
249                 
250                 number=atoi(str_number.c_str());
251         }
252         else
253         {
254                 number=1;
255                 digits=3;
256         }
257         
258         number++;
259         
260         // Add the number back onto the id
261         {
262                 const String format(strprintf("%%0%dd",digits));
263                 id+=strprintf(format.c_str(),number);
264         }
265         
266         // Set the ID
267         set_id(id);
268 }
269
270 StateCircle_Context::StateCircle_Context(CanvasView* canvas_view):
271         canvas_view_(canvas_view),
272         is_working(*canvas_view),
273         duckmatic_push(get_work_area()),
274         prev_workarea_layer_status_(get_work_area()->allow_layer_clicks),
275         settings(synfigapp::Main::get_selected_input_device()->settings()),
276         entry_id(),
277         adj_feather(0,0,1,0.01,0.1),
278         spin_feather(adj_feather,0.1,3),
279         check_invert(_("Invert"))
280 {
281         no_egress_on_selection_change=false;
282         // Set up the tool options dialog
283         //options_table.attach(*manage(new Gtk::Label(_("Circle Tool"))), 0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);      
284         options_table.attach(entry_id, 0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
285
286         enum_falloff.set_param_desc(ParamDesc("falloff")
287                 .set_local_name(_("Falloff"))
288                 .set_description(_("Determines the falloff function for the feather"))
289                 .set_hint("enum")
290                 .add_enum_value(CIRCLE_INTERPOLATION_LINEAR,"linear",_("Linear"))
291                 .add_enum_value(CIRCLE_SQUARED,"squared",_("Squared"))
292                 .add_enum_value(CIRCLE_SQRT,"sqrt",_("Square Root"))
293                 .add_enum_value(CIRCLE_SIGMOND,"sigmond",_("Sigmond"))
294                 .add_enum_value(CIRCLE_COSINE,"cosine",_("Cosine")));
295         
296         enum_blend.set_param_desc(ParamDesc(Color::BLEND_COMPOSITE,"blend_method")
297                 .set_local_name(_("Blend Method"))
298                 .set_description(_("Defines the blend method to be used for circles")));
299         
300         load_settings();
301
302         //feather stuff
303         options_table.attach(*manage(new Gtk::Label(_("Feather:"))), 0, 1, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
304         options_table.attach(spin_feather, 1, 2, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
305         options_table.attach(enum_falloff, 0, 2, 4, 5, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
306         options_table.attach(enum_blend, 0, 2, 5, 6, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
307         
308         //invert flag
309         options_table.attach(check_invert, 0, 2, 6, 7, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
310         
311         options_table.show_all();
312         
313         refresh_tool_options();
314         App::dialog_tool_options->present();
315
316         // Turn off layer clicking
317         get_work_area()->allow_layer_clicks=false;
318         
319         // clear out the ducks
320         get_work_area()->clear_ducks();
321         
322         // Refresh the work area
323         get_work_area()->queue_draw();
324         
325         // Hide the tables if they are showing
326         //prev_table_status=get_canvas_view()->tables_are_visible();
327         //if(prev_table_status)get_canvas_view()->hide_tables();
328                 
329         // Hide the time bar
330         //get_canvas_view()->hide_timebar();
331         
332         // Connect a signal
333         //get_work_area()->signal_user_click().connect(sigc::mem_fun(*this,&studio::StateCircle_Context::on_user_click));
334         get_canvas_view()->work_area->set_cursor(Gdk::CROSSHAIR);
335
336         App::toolbox->refresh();
337 }
338
339 void
340 StateCircle_Context::refresh_tool_options()
341 {
342         App::dialog_tool_options->clear();
343         App::dialog_tool_options->set_widget(options_table);
344         App::dialog_tool_options->set_local_name(_("Circle Tool"));
345         App::dialog_tool_options->set_name("circle");
346 }
347
348 Smach::event_result
349 StateCircle_Context::event_refresh_tool_options(const Smach::event& x)
350 {
351         refresh_tool_options();
352         return Smach::RESULT_ACCEPT;
353 }
354
355 StateCircle_Context::~StateCircle_Context()
356 {
357         save_settings();
358
359         // Restore layer clicking
360         get_work_area()->allow_layer_clicks=prev_workarea_layer_status_;
361         get_canvas_view()->work_area->reset_cursor();
362
363         App::dialog_tool_options->clear();
364
365         // Show the time bar
366         if(get_canvas_view()->get_canvas()->rend_desc().get_time_start()!=get_canvas_view()->get_canvas()->rend_desc().get_time_end())
367                 get_canvas_view()->show_timebar();
368
369         // Bring back the tables if they were out before
370         //if(prev_table_status)get_canvas_view()->show_tables();
371                         
372         // Refresh the work area
373         get_work_area()->queue_draw();
374
375         App::toolbox->refresh();
376 }
377
378 Smach::event_result
379 StateCircle_Context::event_stop_handler(const Smach::event& x)
380 {
381         throw Smach::egress_exception();
382 }
383
384 Smach::event_result
385 StateCircle_Context::event_refresh_handler(const Smach::event& x)
386 {
387         refresh_ducks();
388         return Smach::RESULT_ACCEPT;
389 }
390
391 void
392 StateCircle_Context::make_circle(const Point& _p1, const Point& _p2)
393 {
394         synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("New Circle"));
395         synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
396
397         Layer::Handle layer;
398         
399         Canvas::Handle canvas(get_canvas_view()->get_canvas());
400         int depth(0);
401         
402         // we are temporarily using the layer to hold something
403         layer=get_canvas_view()->get_selection_manager()->get_selected_layer();
404         if(layer)
405         {
406                 depth=layer->get_depth();
407                 canvas=layer->get_canvas();
408         }
409
410         const synfig::TransformStack& transform(get_canvas_view()->get_curr_transform_stack());
411         const Point p1(transform.unperform(_p1));
412         const Point p2(transform.unperform(_p2));
413         
414         if(get_falloff() >= 0 && get_falloff() < CIRCLE_NUM_FALLOFF)
415         {
416                 
417                 layer=get_canvas_interface()->add_layer_to("circle",canvas,depth);
418                 
419                 layer->set_param("pos",p1);
420                 get_canvas_interface()->signal_layer_param_changed()(layer,"pos");
421                 
422                 layer->set_param("radius",(p2-p1).mag());
423                 get_canvas_interface()->signal_layer_param_changed()(layer,"radius");
424                 
425                 layer->set_param("falloff",get_falloff());
426                 get_canvas_interface()->signal_layer_param_changed()(layer,"falloff");
427                 
428                 layer->set_param("feather",get_feather());
429                 get_canvas_interface()->signal_layer_param_changed()(layer,"feather");
430
431                 layer->set_param("invert",get_invert());
432                 get_canvas_interface()->signal_layer_param_changed()(layer,"invert");
433                 
434                 layer->set_param("blend_method",get_blend());
435                 get_canvas_interface()->signal_layer_param_changed()(layer,"blend_method");
436                 
437                 layer->set_description(get_id());
438                 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
439
440                 no_egress_on_selection_change=true;
441                 get_canvas_interface()->get_selection_manager()->clear_selected_layers();
442                 get_canvas_interface()->get_selection_manager()->set_selected_layer(layer);
443                 no_egress_on_selection_change=false;
444         }
445         
446         reset();
447         increment_id();
448 }
449
450 Smach::event_result
451 StateCircle_Context::event_mouse_click_handler(const Smach::event& x)
452 {
453         const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
454         
455         if(event.key==EVENT_WORKAREA_MOUSE_BUTTON_DOWN && event.button==BUTTON_LEFT)
456         {
457                 point_holder=get_work_area()->snap_point_to_grid(event.pos);
458                 etl::handle<Duck> duck=new Duck();
459                 duck->set_point(point_holder);
460                 duck->set_name("p1");
461                 duck->set_type(Duck::TYPE_POSITION);
462                 duck->set_editable(false);
463                 get_work_area()->add_duck(duck);
464
465                 point2_duck=new Duck();
466                 point2_duck->set_point(Vector(0,0));
467                 point2_duck->set_name("radius");
468                 point2_duck->set_origin(duck);
469                 point2_duck->set_radius(true);
470                 point2_duck->set_scalar(-1);
471                 point2_duck->set_type(Duck::TYPE_RADIUS);
472                 get_work_area()->add_duck(point2_duck);
473
474                 return Smach::RESULT_ACCEPT;
475         }
476
477         if(event.key==EVENT_WORKAREA_MOUSE_BUTTON_DRAG && event.button==BUTTON_LEFT)
478         {
479                 point2_duck->set_point(point_holder-get_work_area()->snap_point_to_grid(event.pos));
480                 get_work_area()->queue_draw();                  
481                 return Smach::RESULT_ACCEPT;
482         }
483
484         if(event.key==EVENT_WORKAREA_MOUSE_BUTTON_UP && event.button==BUTTON_LEFT)
485         {
486                 make_circle(point_holder, get_work_area()->snap_point_to_grid(event.pos));
487                 get_work_area()->clear_ducks();
488                 return Smach::RESULT_ACCEPT;
489         }
490
491         return Smach::RESULT_OK;
492 }
493
494
495 void
496 StateCircle_Context::refresh_ducks()
497 {
498         get_work_area()->clear_ducks();
499         get_work_area()->queue_draw();
500 }