1 /* === S Y N F I G ========================================================= */
2 /*! \file state_circle.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_circle.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>
50 #include "widget_enum.h"
51 #include <synfigapp/main.h>
55 /* === U S I N G =========================================================== */
59 using namespace synfig;
60 using namespace studio;
62 /* === M A C R O S ========================================================= */
66 CIRCLE_INTERPOLATION_LINEAR =1,
73 /* === G L O B A L S ======================================================= */
75 StateCircle studio::state_circle;
77 /* === C L A S S E S & S T R U C T S ======================================= */
79 class studio::StateCircle_Context : public sigc::trackable
81 etl::handle<CanvasView> canvas_view_;
82 CanvasView::IsWorking is_working;
84 Duckmatic::Push duckmatic_push;
88 etl::handle<Duck> point2_duck;
92 bool prev_workarea_layer_status_;
95 synfigapp::Settings& settings;
98 Gtk::Table options_table;
100 Gtk::Entry entry_id; //what to name the layer
102 Widget_Enum enum_falloff;
103 Widget_Enum enum_blend;
105 Gtk::Adjustment adj_feather;
106 Gtk::SpinButton spin_feather;
108 Gtk::CheckButton check_invert;
112 synfig::String get_id()const { return entry_id.get_text(); }
113 void set_id(const synfig::String& x) { return entry_id.set_text(x); }
115 int get_falloff()const { return enum_falloff.get_value(); }
116 void set_falloff(int x) { return enum_falloff.set_value(x); }
118 int get_blend()const { return enum_blend.get_value(); }
119 void set_blend(int x) { return enum_blend.set_value(x); }
121 Real get_feather()const { return adj_feather.get_value(); }
122 void set_feather(Real f) { adj_feather.set_value(f); }
124 bool get_invert()const { return check_invert.get_active(); }
125 void set_invert(bool i) { check_invert.set_active(i); }
127 void refresh_tool_options(); //to refresh the toolbox
130 Smach::event_result event_stop_handler(const Smach::event& x);
131 Smach::event_result event_refresh_handler(const Smach::event& x);
132 Smach::event_result event_mouse_click_handler(const Smach::event& x);
133 Smach::event_result event_refresh_tool_options(const Smach::event& x);
135 //constructor destructor
136 StateCircle_Context(CanvasView* canvas_view);
137 ~StateCircle_Context();
140 const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
141 etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
142 synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
143 WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
145 //Modifying settings etc.
146 void load_settings();
147 void save_settings();
150 bool no_egress_on_selection_change;
151 Smach::event_result event_layer_selection_changed_handler(const Smach::event& x)
153 if(!no_egress_on_selection_change)
154 throw Smach::egress_exception();
155 return Smach::RESULT_OK;
158 void make_circle(const Point& p1, const Point& p2);
160 }; // END of class StateGradient_Context
162 /* === M E T H O D S ======================================================= */
164 StateCircle::StateCircle():
165 Smach::state<StateCircle_Context>("circle")
167 insert(event_def(EVENT_LAYER_SELECTION_CHANGED,&StateCircle_Context::event_layer_selection_changed_handler));
168 insert(event_def(EVENT_STOP,&StateCircle_Context::event_stop_handler));
169 insert(event_def(EVENT_REFRESH,&StateCircle_Context::event_refresh_handler));
170 insert(event_def(EVENT_REFRESH_DUCKS,&StateCircle_Context::event_refresh_handler));
171 insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,&StateCircle_Context::event_mouse_click_handler));
172 insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DRAG,&StateCircle_Context::event_mouse_click_handler));
173 insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_UP,&StateCircle_Context::event_mouse_click_handler));
174 insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateCircle_Context::event_refresh_tool_options));
177 StateCircle::~StateCircle()
182 StateCircle_Context::load_settings()
186 //parse the arguments yargh!
187 if(settings.get_value("circle.id",value))
192 if(settings.get_value("circle.fallofftype",value) && value != "")
193 set_falloff(atoi(value.c_str()));
197 if(settings.get_value("circle.blend",value) && value != "")
198 set_blend(atoi(value.c_str()));
200 set_blend(0);//(int)Color::BLEND_COMPOSITE); //0 should be blend composites value
202 if(settings.get_value("circle.feather",value))
203 set_feather(atof(value.c_str()));
207 if(settings.get_value("circle.invert",value) && value != "0")
214 StateCircle_Context::save_settings()
216 settings.set_value("circle.id",get_id());
217 settings.set_value("circle.fallofftype",strprintf("%d",get_falloff()));
218 settings.set_value("circle.blend",strprintf("%d",get_blend()));
219 settings.set_value("circle.feather",strprintf("%f",(float)get_feather()));
220 settings.set_value("circle.invert",get_invert()?"1":"0");
224 StateCircle_Context::reset()
230 StateCircle_Context::increment_id()
239 // If there is a number
240 // already at the end of the
241 // id, then remove it.
242 if(id[id.size()-1]<='9' && id[id.size()-1]>='0')
244 // figure out how many digits it is
245 for(digits=0;(int)id.size()-1>=digits && id[id.size()-1-digits]<='9' && id[id.size()-1-digits]>='0';digits++)while(false);
248 str_number=String(id,id.size()-digits,id.size());
249 id=String(id,0,id.size()-digits);
251 number=atoi(str_number.c_str());
261 // Add the number back onto the id
263 const String format(strprintf("%%0%dd",digits));
264 id+=strprintf(format.c_str(),number);
271 StateCircle_Context::StateCircle_Context(CanvasView* canvas_view):
272 canvas_view_(canvas_view),
273 is_working(*canvas_view),
274 duckmatic_push(get_work_area()),
275 prev_workarea_layer_status_(get_work_area()->allow_layer_clicks),
276 settings(synfigapp::Main::get_selected_input_device()->settings()),
278 adj_feather(0,0,1,0.01,0.1),
279 spin_feather(adj_feather,0.1,3),
280 check_invert(_("Invert"))
282 no_egress_on_selection_change=false;
283 // Set up the tool options dialog
284 //options_table.attach(*manage(new Gtk::Label(_("Circle Tool"))), 0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
285 options_table.attach(entry_id, 0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
287 enum_falloff.set_param_desc(ParamDesc("falloff")
288 .set_local_name(_("Falloff"))
289 .set_description(_("Determines the falloff function for the feather"))
291 .add_enum_value(CIRCLE_INTERPOLATION_LINEAR,"linear",_("Linear"))
292 .add_enum_value(CIRCLE_SQUARED,"squared",_("Squared"))
293 .add_enum_value(CIRCLE_SQRT,"sqrt",_("Square Root"))
294 .add_enum_value(CIRCLE_SIGMOND,"sigmond",_("Sigmond"))
295 .add_enum_value(CIRCLE_COSINE,"cosine",_("Cosine")));
297 enum_blend.set_param_desc(ParamDesc(Color::BLEND_COMPOSITE,"blend_method")
298 .set_local_name(_("Blend Method"))
299 .set_description(_("Defines the blend method to be used for circles")));
304 options_table.attach(*manage(new Gtk::Label(_("Feather:"))), 0, 1, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
305 options_table.attach(spin_feather, 1, 2, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
306 options_table.attach(enum_falloff, 0, 2, 4, 5, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
307 options_table.attach(enum_blend, 0, 2, 5, 6, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
310 options_table.attach(check_invert, 0, 2, 6, 7, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
312 options_table.show_all();
314 refresh_tool_options();
315 App::dialog_tool_options->present();
317 // Turn off layer clicking
318 get_work_area()->allow_layer_clicks=false;
320 // clear out the ducks
321 get_work_area()->clear_ducks();
323 // Refresh the work area
324 get_work_area()->queue_draw();
326 // Hide the tables if they are showing
327 //prev_table_status=get_canvas_view()->tables_are_visible();
328 //if(prev_table_status)get_canvas_view()->hide_tables();
331 //get_canvas_view()->hide_timebar();
334 //get_work_area()->signal_user_click().connect(sigc::mem_fun(*this,&studio::StateCircle_Context::on_user_click));
335 get_canvas_view()->work_area->set_cursor(Gdk::CROSSHAIR);
337 App::toolbox->refresh();
341 StateCircle_Context::refresh_tool_options()
343 App::dialog_tool_options->clear();
344 App::dialog_tool_options->set_widget(options_table);
345 App::dialog_tool_options->set_local_name(_("Circle Tool"));
346 App::dialog_tool_options->set_name("circle");
350 StateCircle_Context::event_refresh_tool_options(const Smach::event& x)
352 refresh_tool_options();
353 return Smach::RESULT_ACCEPT;
356 StateCircle_Context::~StateCircle_Context()
360 // Restore layer clicking
361 get_work_area()->allow_layer_clicks=prev_workarea_layer_status_;
362 get_canvas_view()->work_area->reset_cursor();
364 App::dialog_tool_options->clear();
367 if(get_canvas_view()->get_canvas()->rend_desc().get_time_start()!=get_canvas_view()->get_canvas()->rend_desc().get_time_end())
368 get_canvas_view()->show_timebar();
370 // Bring back the tables if they were out before
371 //if(prev_table_status)get_canvas_view()->show_tables();
373 // Refresh the work area
374 get_work_area()->queue_draw();
376 App::toolbox->refresh();
380 StateCircle_Context::event_stop_handler(const Smach::event& x)
382 throw Smach::egress_exception();
386 StateCircle_Context::event_refresh_handler(const Smach::event& x)
389 return Smach::RESULT_ACCEPT;
393 StateCircle_Context::make_circle(const Point& _p1, const Point& _p2)
395 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("New Circle"));
396 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
400 Canvas::Handle canvas(get_canvas_view()->get_canvas());
403 // we are temporarily using the layer to hold something
404 layer=get_canvas_view()->get_selection_manager()->get_selected_layer();
407 depth=layer->get_depth();
408 canvas=layer->get_canvas();
411 const synfig::TransformStack& transform(get_canvas_view()->get_curr_transform_stack());
412 const Point p1(transform.unperform(_p1));
413 const Point p2(transform.unperform(_p2));
415 if(get_falloff() >= 0 && get_falloff() < CIRCLE_NUM_FALLOFF)
418 layer=get_canvas_interface()->add_layer_to("circle",canvas,depth);
420 layer->set_param("pos",p1);
421 get_canvas_interface()->signal_layer_param_changed()(layer,"pos");
423 layer->set_param("radius",(p2-p1).mag());
424 get_canvas_interface()->signal_layer_param_changed()(layer,"radius");
426 layer->set_param("falloff",get_falloff());
427 get_canvas_interface()->signal_layer_param_changed()(layer,"falloff");
429 layer->set_param("feather",get_feather());
430 get_canvas_interface()->signal_layer_param_changed()(layer,"feather");
432 layer->set_param("invert",get_invert());
433 get_canvas_interface()->signal_layer_param_changed()(layer,"invert");
435 layer->set_param("blend_method",get_blend());
436 get_canvas_interface()->signal_layer_param_changed()(layer,"blend_method");
438 layer->set_description(get_id());
439 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
441 no_egress_on_selection_change=true;
442 get_canvas_interface()->get_selection_manager()->clear_selected_layers();
443 get_canvas_interface()->get_selection_manager()->set_selected_layer(layer);
444 no_egress_on_selection_change=false;
452 StateCircle_Context::event_mouse_click_handler(const Smach::event& x)
454 const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
456 if(event.key==EVENT_WORKAREA_MOUSE_BUTTON_DOWN && event.button==BUTTON_LEFT)
458 point_holder=get_work_area()->snap_point_to_grid(event.pos);
459 etl::handle<Duck> duck=new Duck();
460 duck->set_point(point_holder);
461 duck->set_name("p1");
462 duck->set_type(Duck::TYPE_POSITION);
463 duck->set_editable(false);
464 get_work_area()->add_duck(duck);
466 point2_duck=new Duck();
467 point2_duck->set_point(Vector(0,0));
468 point2_duck->set_name("radius");
469 point2_duck->set_origin(duck);
470 point2_duck->set_radius(true);
471 point2_duck->set_scalar(-1);
472 point2_duck->set_type(Duck::TYPE_RADIUS);
473 get_work_area()->add_duck(point2_duck);
475 return Smach::RESULT_ACCEPT;
478 if(event.key==EVENT_WORKAREA_MOUSE_BUTTON_DRAG && event.button==BUTTON_LEFT)
480 point2_duck->set_point(point_holder-get_work_area()->snap_point_to_grid(event.pos));
481 get_work_area()->queue_draw();
482 return Smach::RESULT_ACCEPT;
485 if(event.key==EVENT_WORKAREA_MOUSE_BUTTON_UP && event.button==BUTTON_LEFT)
487 make_circle(point_holder, get_work_area()->snap_point_to_grid(event.pos));
488 get_work_area()->clear_ducks();
489 return Smach::RESULT_ACCEPT;
492 return Smach::RESULT_OK;
497 StateCircle_Context::refresh_ducks()
499 get_work_area()->clear_ducks();
500 get_work_area()->queue_draw();