1 /* === S Y N F I G ========================================================= */
2 /*! \file state_gradient.cpp
3 ** \brief Template File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
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.
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.
21 /* ========================================================================= */
23 /* === H E A D E R S ======================================================= */
32 #include <gtkmm/dialog.h>
33 #include <gtkmm/entry.h>
35 #include <synfig/valuenode_dynamiclist.h>
36 #include <synfigapp/action_system.h>
38 #include "state_gradient.h"
39 #include "canvasview.h"
43 #include <synfigapp/action.h>
44 #include "event_mouse.h"
45 #include "event_layerclick.h"
47 #include "dialog_tooloptions.h"
48 #include <gtkmm/optionmenu.h>
51 #include "widget_enum.h"
52 #include <synfigapp/main.h>
56 /* === U S I N G =========================================================== */
60 using namespace synfig;
61 using namespace studio;
63 /* === M A C R O S ========================================================= */
67 GRADIENT_INTERPOLATION_LINEAR=0,
73 /* === G L O B A L S ======================================================= */
75 StateGradient studio::state_gradient;
77 /* === C L A S S E S & S T R U C T S ======================================= */
79 class studio::StateGradient_Context : public sigc::trackable
81 etl::handle<CanvasView> canvas_view_;
82 CanvasView::IsWorking is_working;
84 Duckmatic::Push duckmatic_push;
86 synfigapp::Settings& settings;
90 etl::handle<Duck> point2_duck;
94 bool prev_workarea_layer_status_;
96 Gtk::Table options_table;
98 Widget_Enum enum_type;
99 Widget_Enum enum_blend;
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); }
105 int get_type()const { return enum_type.get_value(); }
106 void set_type(int x) { return enum_type.set_value(x); }
108 int get_blend()const { return enum_blend.get_value(); }
109 void set_blend(int x) { return enum_blend.set_value(x); }
111 Smach::event_result event_stop_handler(const Smach::event& x);
113 Smach::event_result event_refresh_handler(const Smach::event& x);
115 Smach::event_result event_mouse_click_handler(const Smach::event& x);
116 Smach::event_result event_refresh_tool_options(const Smach::event& x);
118 void refresh_tool_options();
120 StateGradient_Context(CanvasView* canvas_view);
122 ~StateGradient_Context();
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();}
129 //void on_user_click(synfig::Point point);
130 void load_settings();
131 void save_settings();
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)
139 if(!no_egress_on_selection_change)
140 throw Smach::egress_exception();
141 return Smach::RESULT_OK;
144 }; // END of class StateGradient_Context
146 /* === M E T H O D S ======================================================= */
148 StateGradient::StateGradient():
149 Smach::state<StateGradient_Context>("gradient")
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));
162 StateGradient::~StateGradient()
167 StateGradient_Context::load_settings()
171 if(settings.get_value("gradient.id",value))
176 if(settings.get_value("gradient.type",value))
177 set_type(atoi(value.c_str()));
179 set_type(GRADIENT_INTERPOLATION_LINEAR);
181 if(settings.get_value("gradient.blend",value))
182 set_blend(atoi(value.c_str()));
184 set_blend(Color::BLEND_COMPOSITE);
188 StateGradient_Context::save_settings()
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()));
196 StateGradient_Context::reset()
202 StateGradient_Context::increment_id()
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')
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);
220 str_number=String(id,id.size()-digits,id.size());
221 id=String(id,0,id.size()-digits);
223 number=atoi(str_number.c_str());
233 // Add the number back onto the id
235 const String format(strprintf("%%0%dd",digits));
236 id+=strprintf(format.c_str(),number);
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),
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);
256 enum_type.set_param_desc(ParamDesc("type")
257 .set_local_name(_("Gradient Type"))
258 .set_description(_("Determines the type of Gradient used"))
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")));
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")));
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);
274 options_table.show_all();
275 refresh_tool_options();
276 App::dialog_tool_options->present();
279 // Turn off layer clicking
280 get_work_area()->allow_layer_clicks=false;
282 get_canvas_view()->work_area->set_cursor(Gdk::CROSSHAIR);
284 // clear out the ducks
285 get_work_area()->clear_ducks();
287 // Refresh the work area
288 get_work_area()->queue_draw();
290 get_work_area()->refresh_cursor();
292 // Hide the tables if they are showing
293 get_canvas_view()->hide_tables();
296 //get_canvas_view()->hide_timebar();
299 //get_work_area()->signal_user_click().connect(sigc::mem_fun(*this,&studio::StateGradient_Context::on_user_click));
301 App::toolbox->refresh();
305 StateGradient_Context::refresh_tool_options()
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");
314 StateGradient_Context::event_refresh_tool_options(const Smach::event& x)
316 refresh_tool_options();
317 return Smach::RESULT_ACCEPT;
320 StateGradient_Context::~StateGradient_Context()
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();
329 App::dialog_tool_options->clear();
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();
335 // Bring back the tables if they were out before
336 //if(prev_table_status)get_canvas_view()->show_tables();
338 // Refresh the work area
339 get_work_area()->queue_draw();
341 //get_canvas_view()->show_tables();
343 get_work_area()->refresh_cursor();
345 App::toolbox->refresh();
349 StateGradient_Context::event_stop_handler(const Smach::event& x)
351 throw Smach::egress_exception();
355 StateGradient_Context::event_refresh_handler(const Smach::event& x)
358 return Smach::RESULT_ACCEPT;
362 StateGradient_Context::make_gradient(const Point& _p1, const Point& _p2)
364 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("New Gradient"));
365 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
369 Canvas::Handle canvas(get_canvas_view()->get_canvas());
372 // we are temporarily using the layer to hold something
373 layer=get_canvas_view()->get_selection_manager()->get_selected_layer();
376 depth=layer->get_depth();
377 canvas=layer->get_canvas();
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));
385 case GRADIENT_INTERPOLATION_LINEAR:
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");
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");
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");
406 layer->set_param("angle",Angle::tan(diff[1],diff[0]));
407 get_canvas_interface()->signal_layer_param_changed()(layer,"angle");
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");
418 layer->set_param("angle",Angle::tan(diff[1],diff[0]));
419 get_canvas_interface()->signal_layer_param_changed()(layer,"angle");
427 layer->set_param("blend_method",get_blend());
428 get_canvas_interface()->signal_layer_param_changed()(layer,"blend_method");
430 layer->set_description(get_id());
431 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
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;
443 StateGradient_Context::event_mouse_click_handler(const Smach::event& x)
445 const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
447 if(event.key==EVENT_WORKAREA_MOUSE_BUTTON_DOWN && event.button==BUTTON_LEFT)
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);
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);
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);
467 return Smach::RESULT_ACCEPT;
470 if(event.key==EVENT_WORKAREA_MOUSE_BUTTON_DRAG && event.button==BUTTON_LEFT)
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;
477 if(event.key==EVENT_WORKAREA_MOUSE_BUTTON_UP && event.button==BUTTON_LEFT)
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;
484 return Smach::RESULT_OK;
489 StateGradient_Context::refresh_ducks()
491 get_work_area()->clear_ducks();
492 get_work_area()->queue_draw();