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>
58 /* === U S I N G =========================================================== */
62 using namespace synfig;
63 using namespace studio;
65 /* === M A C R O S ========================================================= */
69 GRADIENT_INTERPOLATION_LINEAR=0,
75 /* === G L O B A L S ======================================================= */
77 StateGradient studio::state_gradient;
79 /* === C L A S S E S & S T R U C T S ======================================= */
81 class studio::StateGradient_Context : public sigc::trackable
83 etl::handle<CanvasView> canvas_view_;
84 CanvasView::IsWorking is_working;
86 Duckmatic::Push duckmatic_push;
88 synfigapp::Settings& settings;
92 etl::handle<Duck> point2_duck;
96 bool prev_workarea_layer_status_;
98 Gtk::Table options_table;
100 Widget_Enum enum_type;
101 Widget_Enum enum_blend;
104 synfig::String get_id()const { return entry_id.get_text(); }
105 void set_id(const synfig::String& x) { return entry_id.set_text(x); }
107 int get_type()const { return enum_type.get_value(); }
108 void set_type(int x) { return enum_type.set_value(x); }
110 int get_blend()const { return enum_blend.get_value(); }
111 void set_blend(int x) { return enum_blend.set_value(x); }
113 Smach::event_result event_stop_handler(const Smach::event& x);
115 Smach::event_result event_refresh_handler(const Smach::event& x);
117 Smach::event_result event_mouse_click_handler(const Smach::event& x);
118 Smach::event_result event_refresh_tool_options(const Smach::event& x);
120 void refresh_tool_options();
122 StateGradient_Context(CanvasView* canvas_view);
124 ~StateGradient_Context();
126 const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
127 etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
128 synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
129 WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
131 //void on_user_click(synfig::Point point);
132 void load_settings();
133 void save_settings();
137 void make_gradient(const Point& p1, const Point& p2);
138 bool egress_on_selection_change;
139 Smach::event_result event_layer_selection_changed_handler(const Smach::event& /*x*/)
141 if(egress_on_selection_change)
142 throw Smach::egress_exception();
143 return Smach::RESULT_OK;
146 }; // END of class StateGradient_Context
148 /* === M E T H O D S ======================================================= */
150 StateGradient::StateGradient():
151 Smach::state<StateGradient_Context>("gradient")
153 insert(event_def(EVENT_LAYER_SELECTION_CHANGED,&StateGradient_Context::event_layer_selection_changed_handler));
154 insert(event_def(EVENT_STOP,&StateGradient_Context::event_stop_handler));
155 insert(event_def(EVENT_TABLES_SHOW,&StateGradient_Context::event_stop_handler));
156 insert(event_def(EVENT_REFRESH,&StateGradient_Context::event_refresh_handler));
157 insert(event_def(EVENT_REFRESH_DUCKS,&StateGradient_Context::event_refresh_handler));
158 insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,&StateGradient_Context::event_mouse_click_handler));
159 insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DRAG,&StateGradient_Context::event_mouse_click_handler));
160 insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_UP,&StateGradient_Context::event_mouse_click_handler));
161 insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateGradient_Context::event_refresh_tool_options));
164 StateGradient::~StateGradient()
169 StateGradient_Context::load_settings()
173 if(settings.get_value("gradient.id",value))
178 if(settings.get_value("gradient.type",value))
179 set_type(atoi(value.c_str()));
181 set_type(GRADIENT_INTERPOLATION_LINEAR);
183 if(settings.get_value("gradient.blend",value))
184 set_blend(atoi(value.c_str()));
186 set_blend(Color::BLEND_COMPOSITE);
190 StateGradient_Context::save_settings()
192 settings.set_value("gradient.id",get_id().c_str());
193 settings.set_value("gradient.type",strprintf("%d",get_type()));
194 settings.set_value("gradient.blend",strprintf("%d",get_blend()));
198 StateGradient_Context::reset()
204 StateGradient_Context::increment_id()
213 // If there is a number
214 // already at the end of the
215 // id, then remove it.
216 if(id[id.size()-1]<='9' && id[id.size()-1]>='0')
218 // figure out how many digits it is
220 (int)id.size()-1 >= digits && id[id.size()-1-digits] <= '9' && id[id.size()-1-digits] >= '0';
225 str_number=String(id,id.size()-digits,id.size());
226 id=String(id,0,id.size()-digits);
228 number=atoi(str_number.c_str());
238 // Add the number back onto the id
240 const String format(strprintf("%%0%dd",digits));
241 id+=strprintf(format.c_str(),number);
248 StateGradient_Context::StateGradient_Context(CanvasView* canvas_view):
249 canvas_view_(canvas_view),
250 is_working(*canvas_view),
251 duckmatic_push(get_work_area()),
252 settings(synfigapp::Main::get_selected_input_device()->settings()),
253 prev_workarea_layer_status_(get_work_area()->get_allow_layer_clicks()),
256 egress_on_selection_change=true;
257 // Set up the tool options dialog
258 ///options_table.attach(*manage(new Gtk::Label(_("Gradient Tool"))), 0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
259 options_table.attach(entry_id, 0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
261 enum_type.set_param_desc(ParamDesc("type")
262 .set_local_name(_("Gradient Type"))
263 .set_description(_("Determines the type of Gradient used"))
265 .add_enum_value(GRADIENT_INTERPOLATION_LINEAR,"linear",_("Linear"))
266 .add_enum_value(GRADIENT_RADIAL,"radial",_("Radial"))
267 .add_enum_value(GRADIENT_CONICAL,"conical",_("Conical"))
268 .add_enum_value(GRADIENT_SPIRAL,"spiral",_("Spiral")));
270 enum_blend.set_param_desc(ParamDesc(Color::BLEND_COMPOSITE,"blend_method")
271 .set_local_name(_("Blend Method"))
272 .set_description(_("The blend method the gradient will use")));
276 options_table.attach(enum_type, 0, 2, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
277 options_table.attach(enum_blend, 0, 2, 3, 4, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
279 options_table.show_all();
280 refresh_tool_options();
281 App::dialog_tool_options->present();
284 // Turn off layer clicking
285 get_work_area()->set_allow_layer_clicks(false);
287 get_canvas_view()->work_area->set_cursor(Gdk::CROSSHAIR);
289 // clear out the ducks
290 get_work_area()->clear_ducks();
292 // Refresh the work area
293 get_work_area()->queue_draw();
295 get_work_area()->refresh_cursor();
297 // Hide the tables if they are showing
298 get_canvas_view()->hide_tables();
300 // Disable the time bar
301 //get_canvas_view()->set_sensitive_timebar(false);
304 //get_work_area()->signal_user_click().connect(sigc::mem_fun(*this,&studio::StateGradient_Context::on_user_click));
306 App::toolbox->refresh();
310 StateGradient_Context::refresh_tool_options()
312 App::dialog_tool_options->clear();
313 App::dialog_tool_options->set_widget(options_table);
314 App::dialog_tool_options->set_local_name(_("Gradient Tool"));
315 App::dialog_tool_options->set_name("gradient");
319 StateGradient_Context::event_refresh_tool_options(const Smach::event& /*x*/)
321 refresh_tool_options();
322 return Smach::RESULT_ACCEPT;
325 StateGradient_Context::~StateGradient_Context()
329 // Restore layer clicking
330 // get_work_area()->set_allow_layer_clicks(prev_workarea_layer_status_);
331 get_work_area()->set_allow_layer_clicks(true);
332 get_canvas_view()->work_area->reset_cursor();
334 App::dialog_tool_options->clear();
336 // Enable the time bar
337 //get_canvas_view()->set_sensitive_timebar(true);
339 // Bring back the tables if they were out before
340 //if(prev_table_status)get_canvas_view()->show_tables();
342 // Refresh the work area
343 get_work_area()->queue_draw();
345 //get_canvas_view()->show_tables();
347 get_work_area()->refresh_cursor();
349 App::toolbox->refresh();
353 StateGradient_Context::event_stop_handler(const Smach::event& /*x*/)
355 throw Smach::egress_exception();
359 StateGradient_Context::event_refresh_handler(const Smach::event& /*x*/)
362 return Smach::RESULT_ACCEPT;
366 StateGradient_Context::make_gradient(const Point& _p1, const Point& _p2)
368 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("New Gradient"));
369 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
373 Canvas::Handle canvas(get_canvas_view()->get_canvas());
376 // we are temporarily using the layer to hold something
377 layer=get_canvas_view()->get_selection_manager()->get_selected_layer();
380 depth=layer->get_depth();
381 canvas=layer->get_canvas();
383 const synfig::TransformStack& transform(get_canvas_view()->get_curr_transform_stack());
384 const Point p1(transform.unperform(_p1));
385 const Point p2(transform.unperform(_p2));
389 case GRADIENT_INTERPOLATION_LINEAR:
391 layer=get_canvas_interface()->add_layer_to("linear_gradient",canvas,depth);
392 layer->set_param("p1",p1);
393 get_canvas_interface()->signal_layer_param_changed()(layer,"p1");
394 layer->set_param("p2",p2);
395 get_canvas_interface()->signal_layer_param_changed()(layer,"p2");
397 case GRADIENT_RADIAL:
398 layer=get_canvas_interface()->add_layer_to("radial_gradient",canvas,depth);
399 layer->set_param("center",p1);
400 get_canvas_interface()->signal_layer_param_changed()(layer,"center");
401 layer->set_param("radius",(p2-p1).mag());
402 get_canvas_interface()->signal_layer_param_changed()(layer,"radius");
404 case GRADIENT_CONICAL:
405 layer=get_canvas_interface()->add_layer_to("conical_gradient",canvas,depth);
406 layer->set_param("center",p1);
407 get_canvas_interface()->signal_layer_param_changed()(layer,"center");
410 layer->set_param("angle",Angle::tan(diff[1],diff[0]));
411 get_canvas_interface()->signal_layer_param_changed()(layer,"angle");
414 case GRADIENT_SPIRAL:
415 layer=get_canvas_interface()->add_layer_to("spiral_gradient",canvas,depth);
416 layer->set_param("center",p1);
417 get_canvas_interface()->signal_layer_param_changed()(layer,"center");
418 layer->set_param("radius",(p2-p1).mag());
419 get_canvas_interface()->signal_layer_param_changed()(layer,"radius");
422 layer->set_param("angle",Angle::tan(diff[1],diff[0]));
423 get_canvas_interface()->signal_layer_param_changed()(layer,"angle");
431 layer->set_param("blend_method",get_blend());
432 get_canvas_interface()->signal_layer_param_changed()(layer,"blend_method");
434 layer->set_description(get_id());
435 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
437 egress_on_selection_change=false;
438 get_canvas_interface()->get_selection_manager()->clear_selected_layers();
439 get_canvas_interface()->get_selection_manager()->set_selected_layer(layer);
440 egress_on_selection_change=true;
447 StateGradient_Context::event_mouse_click_handler(const Smach::event& x)
449 const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
451 if(event.key==EVENT_WORKAREA_MOUSE_BUTTON_DOWN && event.button==BUTTON_LEFT)
453 point_holder=get_work_area()->snap_point_to_grid(event.pos);
454 etl::handle<Duck> duck=new Duck();
455 duck->set_point(point_holder);
456 duck->set_name("p1");
457 duck->set_type(Duck::TYPE_POSITION);
458 get_work_area()->add_duck(duck);
460 point2_duck=new Duck();
461 point2_duck->set_point(point_holder);
462 point2_duck->set_name("p2");
463 point2_duck->set_type(Duck::TYPE_POSITION);
464 get_work_area()->add_duck(point2_duck);
466 handle<Duckmatic::Bezier> bezier(new Duckmatic::Bezier());
467 bezier->p1=bezier->c1=duck;
468 bezier->p2=bezier->c2=point2_duck;
469 get_work_area()->add_bezier(bezier);
471 return Smach::RESULT_ACCEPT;
474 if(event.key==EVENT_WORKAREA_MOUSE_BUTTON_DRAG && event.button==BUTTON_LEFT)
476 point2_duck->set_point(get_work_area()->snap_point_to_grid(event.pos));
477 get_work_area()->queue_draw();
478 return Smach::RESULT_ACCEPT;
481 if(event.key==EVENT_WORKAREA_MOUSE_BUTTON_UP && event.button==BUTTON_LEFT)
483 make_gradient(point_holder, get_work_area()->snap_point_to_grid(event.pos));
484 get_work_area()->clear_ducks();
485 return Smach::RESULT_ACCEPT;
488 return Smach::RESULT_OK;
493 StateGradient_Context::refresh_ducks()
495 get_work_area()->clear_ducks();
496 get_work_area()->queue_draw();