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