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