1 /* === S Y N F I G ========================================================= */
3 ** \brief Template Header
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007 Chris Moore
11 ** This package is free software; you can redistribute it and/or
12 ** modify it under the terms of the GNU General Public License as
13 ** published by the Free Software Foundation; either version 2 of
14 ** the License, or (at your option) any later version.
16 ** This package is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ** General Public License for more details.
22 /* ========================================================================= */
24 /* === S T A R T =========================================================== */
26 #ifndef __SYNFIG_STUDIO_DUCKMATIC_H
27 #define __SYNFIG_STUDIO_DUCKMATIC_H
29 /* === H E A D E R S ======================================================= */
35 #include <ETL/smart_ptr>
38 #include <synfig/vector.h>
39 #include <synfig/string.h>
40 #include <synfig/real.h>
41 #include <sigc++/signal.h>
42 #include <sigc++/object.h>
43 #include <synfig/time.h>
44 #include <synfig/color.h>
45 #include <ETL/smart_ptr>
48 #include <synfig/color.h>
49 #include <synfig/guidset.h>
51 /* === M A C R O S ========================================================= */
57 #ifndef __STRING_HASH__
58 #define __STRING_HASH__
61 # ifdef FUNCTIONAL_HASH_ON_STRING
62 HASH_MAP_NAMESPACE::hash<synfig::String> hasher_;
63 # else // FUNCTIONAL_HASH_ON_STRING
64 HASH_MAP_NAMESPACE::hash<const char*> hasher_;
65 # endif // FUNCTIONAL_HASH_ON_STRING
67 size_t operator()(const synfig::String& x)const
69 # ifdef FUNCTIONAL_HASH_ON_STRING
71 # else // FUNCTIONAL_HASH_ON_STRING
72 return hasher_(x.c_str());
73 # endif // FUNCTIONAL_HASH_ON_STRING
81 //! If defined, only allow radius ducks to be in the top right
82 //! quadrant of the plane. This makes it very easy to set the
83 //! corresponding parameter to zero (by dragging down and left)
84 #define RESTRICT_RADIUS_DUCKS_TO_ONE_QUARTER
86 /* === T Y P E D E F S ===================================================== */
88 /* === C L A S S E S & S T R U C T S ======================================= */
90 namespace synfigapp { class ValueDesc; }
91 namespace synfig { class ParamDesc; }
99 class DuckDrag_Base : public etl::shared_object
102 virtual void begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& begin)=0;
103 virtual bool end_duck_drag(Duckmatic* duckmatic)=0;
104 virtual void duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector)=0;
107 class DuckDrag_Translate : public DuckDrag_Base
109 synfig::Vector last_translate_;
110 synfig::Vector drag_offset_;
112 std::vector<synfig::Vector> positions;
115 void begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& begin);
116 bool end_duck_drag(Duckmatic* duckmatic);
117 void duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector);
122 ** This class helps organize any of the devices displayed in
123 ** the work area that the user may want to interact with.
124 ** This includes ducks, beziers, and strokes
126 ** \note At some point I'll probably rename this class to "DuckOMatic".
130 friend class DuckDrag_Base;
131 friend class DuckDrag_Translate;
134 -- ** -- P U B L I C T Y P E S ---------------------------------------------
140 typedef HASH_MAP_CLASS<synfig::GUID,etl::smart_ptr<synfig::Point>,synfig::GUIDHash> DuckDataMap;
142 typedef std::map<synfig::GUID,etl::smart_ptr<synfig::Point> > DuckDataMap;
145 typedef studio::DuckMap DuckMap;
147 typedef studio::Duck Duck;
157 typedef Duck::Type Type;
159 typedef std::list<float> GuideList;
162 -- ** -- P R I V A T E D A T A ---------------------------------------------
171 DuckDataMap duck_data_share_map;
173 std::list<etl::handle<Stroke> > stroke_list_;
175 std::list<etl::handle<Stroke> > persistent_stroke_list_;
177 synfig::GUIDSet selected_ducks;
179 synfig::GUID last_duck_guid;
181 std::list<etl::handle<Bezier> > bezier_list_;
183 //! I cannot recall what this is for
184 //synfig::Vector snap;
186 etl::handle<DuckDrag_Base> duck_dragger_;
188 sigc::signal<void> signal_duck_selection_changed_;
190 sigc::signal<void> signal_strokes_changed_;
192 sigc::signal<void> signal_grid_changed_;
194 mutable sigc::signal<void> signal_sketch_saved_;
196 GuideList guide_list_x_;
197 GuideList guide_list_y_;
199 mutable synfig::String sketch_filename_;
201 //! whether the display is flipped vertically or horizontally
202 bool flip_x_, flip_y_;
205 -- ** -- P R O T E C T E D D A T A -----------------------------------------
210 etl::handle<Bezier> selected_bezier;
212 synfig::Time cur_time;
214 //! This flag is set if operations should snap to the grid
215 /*! \todo perhaps there should be two of these flags, one for each axis?
216 ** \see show_grid, grid_size */
221 //! This vector describes the grid size.
222 /*! \see grid_snap, show_grid */
223 synfig::Vector grid_size;
225 bool show_persistent_strokes;
230 -- ** -- P R I V A T E M E T H O D S ---------------------------------------
235 synfig::Vector last_translate_;
236 synfig::Vector drag_offset_;
238 //etl::handle<Duck> selected_duck;
242 -- ** -- P U B L I C M E T H O D S -----------------------------------------
248 virtual ~Duckmatic();
250 void set_flip_x(bool flip_x);
251 void set_flip_y(bool flip_y);
253 sigc::signal<void>& signal_duck_selection_changed() { return signal_duck_selection_changed_; }
254 sigc::signal<void>& signal_strokes_changed() { return signal_strokes_changed_; }
255 sigc::signal<void>& signal_grid_changed() { return signal_grid_changed_; }
256 sigc::signal<void>& signal_sketch_saved() { return signal_sketch_saved_; }
258 GuideList& get_guide_list_x() { return guide_list_x_; }
259 GuideList& get_guide_list_y() { return guide_list_y_; }
260 const GuideList& get_guide_list_x()const { return guide_list_x_; }
261 const GuideList& get_guide_list_y()const { return guide_list_y_; }
263 void set_guide_snap(bool x=true);
264 bool get_guide_snap()const { return guide_snap; }
265 void toggle_guide_snap() { set_guide_snap(!get_guide_snap()); }
267 //! Sets the state of the grid snap flag
268 void set_grid_snap(bool x=true);
270 //! Gets the state of the grid snap flag
271 bool get_grid_snap()const { return grid_snap; }
273 void enable_grid_snap() { set_grid_snap(true); }
275 void disable_grid_snap() { set_grid_snap(false); }
277 void toggle_grid_snap() { set_grid_snap(!grid_snap); }
279 synfig::Point snap_point_to_grid(const synfig::Point& x, float radius=0.1)const;
281 bool get_show_persistent_strokes()const { return show_persistent_strokes; }
282 void set_show_persistent_strokes(bool x);
284 //! Sets the size of the grid
285 void set_grid_size(const synfig::Vector &s);
287 //! Returns the size of the grid
288 const synfig::Vector &get_grid_size()const { return grid_size; }
291 const synfig::Time &get_time()const { return cur_time; }
293 bool get_axis_lock()const { return axis_lock; }
294 void set_axis_lock(bool x) { axis_lock=x; }
296 void set_time(synfig::Time x) { cur_time=x; }
298 bool is_duck_group_selectable(const etl::handle<Duck>& x)const;
300 //const DuckMap& duck_map()const { return duck_map; }
301 DuckList get_duck_list()const;
303 const std::list<etl::handle<Bezier> >& bezier_list()const { return bezier_list_; }
305 const std::list<etl::handle<Stroke> >& stroke_list()const { return stroke_list_; }
307 const std::list<etl::handle<Stroke> >& persistent_stroke_list()const { return persistent_stroke_list_; }
309 std::list<etl::handle<Stroke> >& persistent_stroke_list() { return persistent_stroke_list_; }
311 //! \todo We should modify this to support multiple selections
312 etl::handle<Duck> get_selected_duck()const;
314 DuckList get_selected_ducks()const;
316 //! Returns \a true if the given duck is currently selected
317 bool duck_is_selected(const etl::handle<Duck> &duck)const;
320 void refresh_selected_ducks();
322 void clear_selected_ducks();
324 int count_selected_ducks()const;
326 void toggle_select_duck(const etl::handle<Duck> &duck);
328 void select_duck(const etl::handle<Duck> &duck);
330 void toggle_select_ducks_in_box(const synfig::Vector& tl,const synfig::Vector& br);
332 void select_ducks_in_box(const synfig::Vector& tl,const synfig::Vector& br);
334 void unselect_duck(const etl::handle<Duck> &duck);
336 void start_duck_drag(const synfig::Vector& offset);
337 void translate_selected_ducks(const synfig::Vector& vector);
338 bool end_duck_drag();
340 void signal_edited_selected_ducks();
342 void signal_user_click_selected_ducks(int button);
345 etl::handle<Duck> find_similar_duck(etl::handle<Duck> duck);
346 etl::handle<Duck> add_similar_duck(etl::handle<Duck> duck);
348 void add_stroke(etl::smart_ptr<std::list<synfig::Point> > stroke_point_list, const synfig::Color& color=synfig::Color(0,0,0));
350 void add_persistent_stroke(etl::smart_ptr<std::list<synfig::Point> > stroke_point_list, const synfig::Color& color=synfig::Color(0,0,0));
352 void clear_persistent_strokes();
354 void add_duck(const etl::handle<Duck> &duck);
356 void add_bezier(const etl::handle<Bezier> &bezier);
358 void erase_duck(const etl::handle<Duck> &duck);
360 void erase_bezier(const etl::handle<Bezier> &bezier);
362 //! Returns the last duck added
363 etl::handle<Duck> last_duck()const;
365 etl::handle<Bezier> last_bezier()const;
367 //! \note parameter is in canvas coordinates
368 /*! A radius of "zero" will have an unlimited radius */
369 etl::handle<Duck> find_duck(synfig::Point pos, synfig::Real radius=0, Duck::Type type=Duck::TYPE_DEFAULT);
371 GuideList::iterator find_guide_x(synfig::Point pos, float radius=0.1);
372 GuideList::iterator find_guide_y(synfig::Point pos, float radius=0.1);
373 GuideList::const_iterator find_guide_x(synfig::Point pos, float radius=0.1)const { return const_cast<Duckmatic*>(this)->find_guide_x(pos,radius); }
374 GuideList::const_iterator find_guide_y(synfig::Point pos, float radius=0.1)const { return const_cast<Duckmatic*>(this)->find_guide_y(pos,radius); }
376 //! \note parameter is in canvas coordinates
377 /*! A radius of "zero" will have an unlimited radius */
378 //etl::handle<Bezier> find_bezier(synfig::Point pos, synfig::Real radius=0);
380 //! \note parameter is in canvas coordinates
381 /*! A radius of "zero" will have an unlimited radius */
382 etl::handle<Bezier> find_bezier(synfig::Point pos, synfig::Real radius=0, float* location=0);
384 etl::handle<Bezier> find_bezier(synfig::Point pos, synfig::Real scale, synfig::Real radius, float* location=0);
386 bool add_to_ducks(const synfigapp::ValueDesc& value_desc,etl::handle<CanvasView> canvas_view, const synfig::TransformStack& transform_stack_, synfig::ParamDesc *param_desc=0, int multiple=0);
389 void set_type_mask(Type x) { type_mask=x; }
392 Type get_type_mask()const { return type_mask; }
394 void select_all_ducks();
398 bool save_sketch(const synfig::String& filename)const;
399 bool load_sketch(const synfig::String& filename);
400 const synfig::String& get_sketch_filename()const { return sketch_filename_; }
402 void set_duck_dragger(etl::handle<DuckDrag_Base> x) { duck_dragger_=x; }
403 etl::handle<DuckDrag_Base> get_duck_dragger()const { return duck_dragger_; }
404 void clear_duck_dragger() { duck_dragger_=new DuckDrag_Translate(); }
405 }; // END of class Duckmatic
408 /*! \class Duckmatic::Push
410 class Duckmatic::Push
412 Duckmatic *duckmatic_;
414 std::list<etl::handle<Bezier> > bezier_list_;
415 std::list<etl::handle<Stroke> > stroke_list_;
416 DuckDataMap duck_data_share_map;
417 etl::handle<DuckDrag_Base> duck_dragger_;
422 Push(Duckmatic *duckmatic_);
425 }; // END of class Duckmatic::Push
427 /*! \struct Duckmatic::Bezier
429 struct Duckmatic::Bezier : public etl::shared_object
432 sigc::signal<void,float> signal_user_click_[5];
435 etl::handle<Duck> p1,p2,c1,c2;
436 bool is_valid()const { return p1 && p2 && c1 && c2; }
438 sigc::signal<void,float> &signal_user_click(int i=0) { assert(i>=0); assert(i<5); return signal_user_click_[i]; }
439 }; // END of struct Duckmatic::Bezier
441 /*! \struct Duckmatic::Stroke
443 struct Duckmatic::Stroke : public etl::shared_object
446 sigc::signal<void,float> signal_user_click_[5];
449 etl::smart_ptr<std::list<synfig::Point> > stroke_data;
453 bool is_valid()const { return (bool)stroke_data; }
455 sigc::signal<void,float> &signal_user_click(int i=0) { assert(i>=0); assert(i<5); return signal_user_click_[i]; }
456 }; // END of struct Duckmatic::Stroke
458 }; // END of namespace studio
460 /* === E N D =============================================================== */