1 /* === S Y N F I G ========================================================= */
2 /*! \file state_circle.cpp
3 ** \brief Template File
5 ** $Id: state_circle.cpp,v 1.1.1.1 2005/01/07 03:34:36 darco Exp $
8 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
10 ** This software and associated documentation
11 ** are CONFIDENTIAL and PROPRIETARY property of
12 ** the above-mentioned copyright holder.
14 ** You may not copy, print, publish, or in any
15 ** other way distribute this software without
16 ** a prior written agreement with
17 ** the copyright holder.
20 /* ========================================================================= */
22 /* === H E A D E R S ======================================================= */
31 #include <gtkmm/dialog.h>
32 #include <gtkmm/entry.h>
34 #include <synfig/valuenode_dynamiclist.h>
35 #include <synfigapp/action_system.h>
37 #include "state_circle.h"
38 #include "canvasview.h"
42 #include <synfigapp/action.h>
43 #include "event_mouse.h"
44 #include "event_layerclick.h"
46 #include "dialog_tooloptions.h"
47 #include <gtkmm/optionmenu.h>
49 #include "widget_enum.h"
50 #include <synfigapp/main.h>
54 /* === U S I N G =========================================================== */
58 using namespace synfig;
59 using namespace studio;
61 /* === M A C R O S ========================================================= */
65 CIRCLE_INTERPOLATION_LINEAR =1,
72 /* === G L O B A L S ======================================================= */
74 StateCircle studio::state_circle;
76 /* === C L A S S E S & S T R U C T S ======================================= */
78 class studio::StateCircle_Context : public sigc::trackable
80 etl::handle<CanvasView> canvas_view_;
81 CanvasView::IsWorking is_working;
83 Duckmatic::Push duckmatic_push;
87 etl::handle<Duck> point2_duck;
91 bool prev_workarea_layer_status_;
94 synfigapp::Settings& settings;
97 Gtk::Table options_table;
99 Gtk::Entry entry_id; //what to name the layer
101 Widget_Enum enum_falloff;
102 Widget_Enum enum_blend;
104 Gtk::Adjustment adj_feather;
105 Gtk::SpinButton spin_feather;
107 Gtk::CheckButton check_invert;
111 synfig::String get_id()const { return entry_id.get_text(); }
112 void set_id(const synfig::String& x) { return entry_id.set_text(x); }
114 int get_falloff()const { return enum_falloff.get_value(); }
115 void set_falloff(int x) { return enum_falloff.set_value(x); }
117 int get_blend()const { return enum_blend.get_value(); }
118 void set_blend(int x) { return enum_blend.set_value(x); }
120 Real get_feather()const { return adj_feather.get_value(); }
121 void set_feather(Real f) { adj_feather.set_value(f); }
123 bool get_invert()const { return check_invert.get_active(); }
124 void set_invert(bool i) { check_invert.set_active(i); }
126 void refresh_tool_options(); //to refresh the toolbox
129 Smach::event_result event_stop_handler(const Smach::event& x);
130 Smach::event_result event_refresh_handler(const Smach::event& x);
131 Smach::event_result event_mouse_click_handler(const Smach::event& x);
132 Smach::event_result event_refresh_tool_options(const Smach::event& x);
134 //constructor destructor
135 StateCircle_Context(CanvasView* canvas_view);
136 ~StateCircle_Context();
139 const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
140 etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
141 synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
142 WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
144 //Modifying settings etc.
145 void load_settings();
146 void save_settings();
149 bool no_egress_on_selection_change;
150 Smach::event_result event_layer_selection_changed_handler(const Smach::event& x)
152 if(!no_egress_on_selection_change)
153 throw Smach::egress_exception();
154 return Smach::RESULT_OK;
157 void make_circle(const Point& p1, const Point& p2);
159 }; // END of class StateGradient_Context
161 /* === M E T H O D S ======================================================= */
163 StateCircle::StateCircle():
164 Smach::state<StateCircle_Context>("circle")
166 insert(event_def(EVENT_LAYER_SELECTION_CHANGED,&StateCircle_Context::event_layer_selection_changed_handler));
167 insert(event_def(EVENT_STOP,&StateCircle_Context::event_stop_handler));
168 insert(event_def(EVENT_REFRESH,&StateCircle_Context::event_refresh_handler));
169 insert(event_def(EVENT_REFRESH_DUCKS,&StateCircle_Context::event_refresh_handler));
170 insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,&StateCircle_Context::event_mouse_click_handler));
171 insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DRAG,&StateCircle_Context::event_mouse_click_handler));
172 insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_UP,&StateCircle_Context::event_mouse_click_handler));
173 insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateCircle_Context::event_refresh_tool_options));
176 StateCircle::~StateCircle()
181 StateCircle_Context::load_settings()
185 //parse the arguments yargh!
186 if(settings.get_value("circle.id",value))
191 if(settings.get_value("circle.fallofftype",value) && value != "")
192 set_falloff(atoi(value.c_str()));
196 if(settings.get_value("circle.blend",value) && value != "")
197 set_blend(atoi(value.c_str()));
199 set_blend(0);//(int)Color::BLEND_COMPOSITE); //0 should be blend composites value
201 if(settings.get_value("circle.feather",value))
202 set_feather(atof(value.c_str()));
206 if(settings.get_value("circle.invert",value) && value != "0")
213 StateCircle_Context::save_settings()
215 settings.set_value("circle.id",get_id());
216 settings.set_value("circle.fallofftype",strprintf("%d",get_falloff()));
217 settings.set_value("circle.blend",strprintf("%d",get_blend()));
218 settings.set_value("circle.feather",strprintf("%f",(float)get_feather()));
219 settings.set_value("circle.invert",get_invert()?"1":"0");
223 StateCircle_Context::reset()
229 StateCircle_Context::increment_id()
238 // If there is a number
239 // already at the end of the
240 // id, then remove it.
241 if(id[id.size()-1]<='9' && id[id.size()-1]>='0')
243 // figure out how many digits it is
244 for(digits=0;(int)id.size()-1>=digits && id[id.size()-1-digits]<='9' && id[id.size()-1-digits]>='0';digits++)while(false);
247 str_number=String(id,id.size()-digits,id.size());
248 id=String(id,0,id.size()-digits);
250 number=atoi(str_number.c_str());
260 // Add the number back onto the id
262 const String format(strprintf("%%0%dd",digits));
263 id+=strprintf(format.c_str(),number);
270 StateCircle_Context::StateCircle_Context(CanvasView* canvas_view):
271 canvas_view_(canvas_view),
272 is_working(*canvas_view),
273 duckmatic_push(get_work_area()),
274 prev_workarea_layer_status_(get_work_area()->allow_layer_clicks),
275 settings(synfigapp::Main::get_selected_input_device()->settings()),
277 adj_feather(0,0,1,0.01,0.1),
278 spin_feather(adj_feather,0.1,3),
279 check_invert(_("Invert"))
281 no_egress_on_selection_change=false;
282 // Set up the tool options dialog
283 //options_table.attach(*manage(new Gtk::Label(_("Circle Tool"))), 0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
284 options_table.attach(entry_id, 0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
286 enum_falloff.set_param_desc(ParamDesc("falloff")
287 .set_local_name(_("Falloff"))
288 .set_description(_("Determines the falloff function for the feather"))
290 .add_enum_value(CIRCLE_INTERPOLATION_LINEAR,"linear",_("Linear"))
291 .add_enum_value(CIRCLE_SQUARED,"squared",_("Squared"))
292 .add_enum_value(CIRCLE_SQRT,"sqrt",_("Square Root"))
293 .add_enum_value(CIRCLE_SIGMOND,"sigmond",_("Sigmond"))
294 .add_enum_value(CIRCLE_COSINE,"cosine",_("Cosine")));
296 enum_blend.set_param_desc(ParamDesc(Color::BLEND_COMPOSITE,"blend_method")
297 .set_local_name(_("Blend Method"))
298 .set_description(_("Defines the blend method to be used for circles")));
303 options_table.attach(*manage(new Gtk::Label(_("Feather:"))), 0, 1, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
304 options_table.attach(spin_feather, 1, 2, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
305 options_table.attach(enum_falloff, 0, 2, 4, 5, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
306 options_table.attach(enum_blend, 0, 2, 5, 6, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
309 options_table.attach(check_invert, 0, 2, 6, 7, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
311 options_table.show_all();
313 refresh_tool_options();
314 App::dialog_tool_options->present();
316 // Turn off layer clicking
317 get_work_area()->allow_layer_clicks=false;
319 // clear out the ducks
320 get_work_area()->clear_ducks();
322 // Refresh the work area
323 get_work_area()->queue_draw();
325 // Hide the tables if they are showing
326 //prev_table_status=get_canvas_view()->tables_are_visible();
327 //if(prev_table_status)get_canvas_view()->hide_tables();
330 //get_canvas_view()->hide_timebar();
333 //get_work_area()->signal_user_click().connect(sigc::mem_fun(*this,&studio::StateCircle_Context::on_user_click));
334 get_canvas_view()->work_area->set_cursor(Gdk::CROSSHAIR);
336 App::toolbox->refresh();
340 StateCircle_Context::refresh_tool_options()
342 App::dialog_tool_options->clear();
343 App::dialog_tool_options->set_widget(options_table);
344 App::dialog_tool_options->set_local_name(_("Circle Tool"));
345 App::dialog_tool_options->set_name("circle");
349 StateCircle_Context::event_refresh_tool_options(const Smach::event& x)
351 refresh_tool_options();
352 return Smach::RESULT_ACCEPT;
355 StateCircle_Context::~StateCircle_Context()
359 // Restore layer clicking
360 get_work_area()->allow_layer_clicks=prev_workarea_layer_status_;
361 get_canvas_view()->work_area->reset_cursor();
363 App::dialog_tool_options->clear();
366 if(get_canvas_view()->get_canvas()->rend_desc().get_time_start()!=get_canvas_view()->get_canvas()->rend_desc().get_time_end())
367 get_canvas_view()->show_timebar();
369 // Bring back the tables if they were out before
370 //if(prev_table_status)get_canvas_view()->show_tables();
372 // Refresh the work area
373 get_work_area()->queue_draw();
375 App::toolbox->refresh();
379 StateCircle_Context::event_stop_handler(const Smach::event& x)
381 throw Smach::egress_exception();
385 StateCircle_Context::event_refresh_handler(const Smach::event& x)
388 return Smach::RESULT_ACCEPT;
392 StateCircle_Context::make_circle(const Point& _p1, const Point& _p2)
394 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("New Circle"));
395 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
399 Canvas::Handle canvas(get_canvas_view()->get_canvas());
402 // we are temporarily using the layer to hold something
403 layer=get_canvas_view()->get_selection_manager()->get_selected_layer();
406 depth=layer->get_depth();
407 canvas=layer->get_canvas();
410 const synfig::TransformStack& transform(get_canvas_view()->get_curr_transform_stack());
411 const Point p1(transform.unperform(_p1));
412 const Point p2(transform.unperform(_p2));
414 if(get_falloff() >= 0 && get_falloff() < CIRCLE_NUM_FALLOFF)
417 layer=get_canvas_interface()->add_layer_to("circle",canvas,depth);
419 layer->set_param("pos",p1);
420 get_canvas_interface()->signal_layer_param_changed()(layer,"pos");
422 layer->set_param("radius",(p2-p1).mag());
423 get_canvas_interface()->signal_layer_param_changed()(layer,"radius");
425 layer->set_param("falloff",get_falloff());
426 get_canvas_interface()->signal_layer_param_changed()(layer,"falloff");
428 layer->set_param("feather",get_feather());
429 get_canvas_interface()->signal_layer_param_changed()(layer,"feather");
431 layer->set_param("invert",get_invert());
432 get_canvas_interface()->signal_layer_param_changed()(layer,"invert");
434 layer->set_param("blend_method",get_blend());
435 get_canvas_interface()->signal_layer_param_changed()(layer,"blend_method");
437 layer->set_description(get_id());
438 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
440 no_egress_on_selection_change=true;
441 get_canvas_interface()->get_selection_manager()->clear_selected_layers();
442 get_canvas_interface()->get_selection_manager()->set_selected_layer(layer);
443 no_egress_on_selection_change=false;
451 StateCircle_Context::event_mouse_click_handler(const Smach::event& x)
453 const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
455 if(event.key==EVENT_WORKAREA_MOUSE_BUTTON_DOWN && event.button==BUTTON_LEFT)
457 point_holder=get_work_area()->snap_point_to_grid(event.pos);
458 etl::handle<Duck> duck=new Duck();
459 duck->set_point(point_holder);
460 duck->set_name("p1");
461 duck->set_type(Duck::TYPE_POSITION);
462 duck->set_editable(false);
463 get_work_area()->add_duck(duck);
465 point2_duck=new Duck();
466 point2_duck->set_point(Vector(0,0));
467 point2_duck->set_name("radius");
468 point2_duck->set_origin(duck);
469 point2_duck->set_radius(true);
470 point2_duck->set_scalar(-1);
471 point2_duck->set_type(Duck::TYPE_RADIUS);
472 get_work_area()->add_duck(point2_duck);
474 return Smach::RESULT_ACCEPT;
477 if(event.key==EVENT_WORKAREA_MOUSE_BUTTON_DRAG && event.button==BUTTON_LEFT)
479 point2_duck->set_point(point_holder-get_work_area()->snap_point_to_grid(event.pos));
480 get_work_area()->queue_draw();
481 return Smach::RESULT_ACCEPT;
484 if(event.key==EVENT_WORKAREA_MOUSE_BUTTON_UP && event.button==BUTTON_LEFT)
486 make_circle(point_holder, get_work_area()->snap_point_to_grid(event.pos));
487 get_work_area()->clear_ducks();
488 return Smach::RESULT_ACCEPT;
491 return Smach::RESULT_OK;
496 StateCircle_Context::refresh_ducks()
498 get_work_area()->clear_ducks();
499 get_work_area()->queue_draw();