Restrict 'radius' ducks to the top right corner of the plane. This allows us to...
[synfig.git] / synfig-studio / trunk / src / gtkmm / duckmatic.h
1 /* === S Y N F I G ========================================================= */
2 /*!     \file duckmatic.h
3 **      \brief Template Header
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007 Chris Moore
10 **
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.
15 **
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.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === S T A R T =========================================================== */
25
26 #ifndef __SYNFIG_STUDIO_DUCKMATIC_H
27 #define __SYNFIG_STUDIO_DUCKMATIC_H
28
29 /* === H E A D E R S ======================================================= */
30
31 #include <list>
32 #include <map>
33 #include <set>
34
35 #include <ETL/smart_ptr>
36 #include <ETL/handle>
37
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>
46
47 #include "duck.h"
48 #include <synfig/color.h>
49 #include <synfig/guidset.h>
50
51 /* === M A C R O S ========================================================= */
52
53 #ifdef HASH_MAP_H
54 #include HASH_MAP_H
55 #include FUNCTIONAL_H
56
57 #ifndef __STRING_HASH__
58 #define __STRING_HASH__
59 class StringHash
60 {
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
66 public:
67         size_t operator()(const synfig::String& x)const
68         {
69 # ifdef FUNCTIONAL_HASH_ON_STRING
70                 return hasher_(x);
71 # else  // FUNCTIONAL_HASH_ON_STRING
72                 return hasher_(x.c_str());
73 # endif  // FUNCTIONAL_HASH_ON_STRING
74         }
75 };
76 #endif
77 #else
78 #include <map>
79 #endif
80
81 #define RESTRICT_RADIUS_DUCKS_TO_ONE_QUARTER
82
83 /* === T Y P E D E F S ===================================================== */
84
85 /* === C L A S S E S & S T R U C T S ======================================= */
86
87 namespace synfigapp { class ValueDesc; }
88 namespace synfig { class ParamDesc; }
89
90 namespace studio
91 {
92
93 class CanvasView;
94 class Duckmatic;
95
96 class DuckDrag_Base : public etl::shared_object
97 {
98 public:
99         virtual void begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& begin)=0;
100         virtual bool end_duck_drag(Duckmatic* duckmatic)=0;
101         virtual void duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector)=0;
102 };
103
104 class DuckDrag_Translate : public DuckDrag_Base
105 {
106         synfig::Vector last_translate_;
107         synfig::Vector drag_offset_;
108         synfig::Vector snap;
109         std::vector<synfig::Vector> positions;
110
111 public:
112         void begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& begin);
113         bool end_duck_drag(Duckmatic* duckmatic);
114         void duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector);
115 };
116
117 /*! \class Duckmatic
118 **
119 **      This class helps organize any of the devices displayed in
120 **      the work area that the user may want to interact with.
121 **      This includes ducks, beziers, and strokes
122 **
123 **      \note At some point I'll probably rename this class to "DuckOMatic".
124 */
125 class Duckmatic
126 {
127         friend class DuckDrag_Base;
128         friend class DuckDrag_Translate;
129
130         /*
131  -- ** -- P U B L I C   T Y P E S ---------------------------------------------
132         */
133
134 public:
135
136 #ifdef HASH_MAP_H
137 typedef HASH_MAP_CLASS<synfig::GUID,etl::smart_ptr<synfig::Point>,synfig::GUIDHash> DuckDataMap;
138 #else
139 typedef std::map<synfig::GUID,etl::smart_ptr<synfig::Point> > DuckDataMap;
140 #endif
141
142         typedef studio::DuckMap DuckMap;
143
144         typedef studio::Duck Duck;
145
146         struct Stroke;
147
148         struct Bezier;
149
150         class Push;
151
152         friend class Push;
153
154         typedef Duck::Type Type;
155
156         typedef std::list<float> GuideList;
157
158         /*
159  -- ** -- P R I V A T E   D A T A ---------------------------------------------
160         */
161
162 private:
163
164         Type type_mask;
165
166         DuckMap duck_map;
167
168         DuckDataMap duck_data_share_map;
169
170         std::list<etl::handle<Stroke> > stroke_list_;
171
172         std::list<etl::handle<Stroke> > persistent_stroke_list_;
173
174         synfig::GUIDSet selected_ducks;
175
176         synfig::GUID last_duck_guid;
177
178         std::list<etl::handle<Bezier> > bezier_list_;
179
180         //! I cannot recall what this is for
181         //synfig::Vector snap;
182
183         etl::handle<DuckDrag_Base> duck_dragger_;
184
185         sigc::signal<void> signal_duck_selection_changed_;
186
187         sigc::signal<void> signal_strokes_changed_;
188
189         sigc::signal<void> signal_grid_changed_;
190
191         mutable sigc::signal<void> signal_sketch_saved_;
192
193         GuideList guide_list_x_;
194         GuideList guide_list_y_;
195
196         mutable synfig::String sketch_filename_;
197
198         //! whether the display is flipped vertically or horizontally
199         bool flip_x_, flip_y_;
200
201         /*
202  -- ** -- P R O T E C T E D   D A T A -----------------------------------------
203         */
204
205 protected:
206
207         etl::handle<Bezier> selected_bezier;
208
209         synfig::Time cur_time;
210
211         //! This flag is set if operations should snap to the grid
212         /*! \todo perhaps there should be two of these flags, one for each axis?
213         **      \see show_grid, grid_size */
214         bool grid_snap;
215
216         bool guide_snap;
217
218         //! This vector describes the grid size.
219         /*! \see grid_snap, show_grid */
220         synfig::Vector grid_size;
221
222         bool show_persistent_strokes;
223
224         bool axis_lock;
225
226         /*
227  -- ** -- P R I V A T E   M E T H O D S ---------------------------------------
228         */
229
230 private:
231
232         synfig::Vector last_translate_;
233         synfig::Vector drag_offset_;
234
235         //etl::handle<Duck> selected_duck;
236
237
238         /*
239  -- ** -- P U B L I C   M E T H O D S -----------------------------------------
240         */
241
242 public:
243
244         Duckmatic();
245         virtual ~Duckmatic();
246
247         void set_flip_x(bool flip_x);
248         void set_flip_y(bool flip_y);
249
250         sigc::signal<void>& signal_duck_selection_changed() { return signal_duck_selection_changed_; }
251         sigc::signal<void>& signal_strokes_changed() { return signal_strokes_changed_; }
252         sigc::signal<void>& signal_grid_changed() { return signal_grid_changed_; }
253         sigc::signal<void>& signal_sketch_saved() { return signal_sketch_saved_; }
254
255         GuideList& get_guide_list_x() { return guide_list_x_; }
256         GuideList& get_guide_list_y() { return guide_list_y_; }
257         const GuideList& get_guide_list_x()const { return guide_list_x_; }
258         const GuideList& get_guide_list_y()const { return guide_list_y_; }
259
260         void set_guide_snap(bool x=true);
261         bool get_guide_snap()const { return guide_snap; }
262         void toggle_guide_snap() { set_guide_snap(!get_guide_snap()); }
263
264         //! Sets the state of the grid snap flag
265         void set_grid_snap(bool x=true);
266
267         //! Gets the state of the grid snap flag
268         bool get_grid_snap()const { return grid_snap; }
269
270         void enable_grid_snap() { set_grid_snap(true); }
271
272         void disable_grid_snap() { set_grid_snap(false); }
273
274         void toggle_grid_snap() { set_grid_snap(!grid_snap); }
275
276         synfig::Point snap_point_to_grid(const synfig::Point& x, float radius=0.1)const;
277
278         bool get_show_persistent_strokes()const { return show_persistent_strokes; }
279         void set_show_persistent_strokes(bool x);
280
281         //! Sets the size of the grid
282         void set_grid_size(const synfig::Vector &s);
283
284         //! Returns the size of the grid
285         const synfig::Vector &get_grid_size()const { return grid_size; }
286
287
288         const synfig::Time &get_time()const { return cur_time; }
289
290         bool get_axis_lock()const { return axis_lock; }
291         void set_axis_lock(bool x) { axis_lock=x; }
292
293         void set_time(synfig::Time x) { cur_time=x; }
294
295         bool is_duck_group_selectable(const etl::handle<Duck>& x)const;
296
297         //const DuckMap& duck_map()const { return duck_map; }
298         DuckList get_duck_list()const;
299
300         const std::list<etl::handle<Bezier> >& bezier_list()const { return bezier_list_; }
301
302         const std::list<etl::handle<Stroke> >& stroke_list()const { return stroke_list_; }
303
304         const std::list<etl::handle<Stroke> >& persistent_stroke_list()const { return persistent_stroke_list_; }
305
306         std::list<etl::handle<Stroke> >& persistent_stroke_list() { return persistent_stroke_list_; }
307
308         //! \todo We should modify this to support multiple selections
309         etl::handle<Duck> get_selected_duck()const;
310
311         DuckList get_selected_ducks()const;
312
313         //! Returns \a true if the given duck is currently selected
314         bool duck_is_selected(const etl::handle<Duck> &duck)const;
315
316
317         void refresh_selected_ducks();
318
319         void clear_selected_ducks();
320
321         int count_selected_ducks()const;
322
323         void toggle_select_duck(const etl::handle<Duck> &duck);
324
325         void select_duck(const etl::handle<Duck> &duck);
326
327         void toggle_select_ducks_in_box(const synfig::Vector& tl,const synfig::Vector& br);
328
329         void select_ducks_in_box(const synfig::Vector& tl,const synfig::Vector& br);
330
331         void unselect_duck(const etl::handle<Duck> &duck);
332
333         void start_duck_drag(const synfig::Vector& offset);
334         void translate_selected_ducks(const synfig::Vector& vector);
335         bool end_duck_drag();
336
337         void signal_edited_selected_ducks();
338
339         void signal_user_click_selected_ducks(int button);
340
341
342         etl::handle<Duck> find_similar_duck(etl::handle<Duck> duck);
343         etl::handle<Duck> add_similar_duck(etl::handle<Duck> duck);
344
345         void add_stroke(etl::smart_ptr<std::list<synfig::Point> > stroke_point_list, const synfig::Color& color=synfig::Color(0,0,0));
346
347         void add_persistent_stroke(etl::smart_ptr<std::list<synfig::Point> > stroke_point_list, const synfig::Color& color=synfig::Color(0,0,0));
348
349         void clear_persistent_strokes();
350
351         void add_duck(const etl::handle<Duck> &duck);
352
353         void add_bezier(const etl::handle<Bezier> &bezier);
354
355         void erase_duck(const etl::handle<Duck> &duck);
356
357         void erase_bezier(const etl::handle<Bezier> &bezier);
358
359         //! Returns the last duck added
360         etl::handle<Duck> last_duck()const;
361
362         etl::handle<Bezier> last_bezier()const;
363
364         //! \note parameter is in canvas coordinates
365         /*!     A radius of "zero" will have an unlimited radius */
366         etl::handle<Duck> find_duck(synfig::Point pos, synfig::Real radius=0, Duck::Type type=Duck::TYPE_DEFAULT);
367
368         GuideList::iterator find_guide_x(synfig::Point pos, float radius=0.1);
369         GuideList::iterator find_guide_y(synfig::Point pos, float radius=0.1);
370         GuideList::const_iterator find_guide_x(synfig::Point pos, float radius=0.1)const { return const_cast<Duckmatic*>(this)->find_guide_x(pos,radius); }
371         GuideList::const_iterator find_guide_y(synfig::Point pos, float radius=0.1)const { return const_cast<Duckmatic*>(this)->find_guide_y(pos,radius); }
372
373         //! \note parameter is in canvas coordinates
374         /*!     A radius of "zero" will have an unlimited radius */
375         //etl::handle<Bezier> find_bezier(synfig::Point pos, synfig::Real radius=0);
376
377         //! \note parameter is in canvas coordinates
378         /*!     A radius of "zero" will have an unlimited radius */
379         etl::handle<Bezier> find_bezier(synfig::Point pos, synfig::Real radius=0, float* location=0);
380
381         etl::handle<Bezier> find_bezier(synfig::Point pos, synfig::Real scale, synfig::Real radius, float* location=0);
382
383         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);
384
385         //! \writeme
386         void set_type_mask(Type x) { type_mask=x; }
387
388         //! \writeme
389         Type get_type_mask()const { return type_mask; }
390
391         void select_all_ducks();
392
393         void clear_ducks();
394
395         bool save_sketch(const synfig::String& filename)const;
396         bool load_sketch(const synfig::String& filename);
397         const synfig::String& get_sketch_filename()const { return sketch_filename_; }
398
399         void set_duck_dragger(etl::handle<DuckDrag_Base> x) { duck_dragger_=x; }
400         etl::handle<DuckDrag_Base> get_duck_dragger()const { return duck_dragger_; }
401         void clear_duck_dragger() { duck_dragger_=new DuckDrag_Translate(); }
402 }; // END of class Duckmatic
403
404
405 /*! \class Duckmatic::Push
406 **      \writeme */
407 class Duckmatic::Push
408 {
409         Duckmatic *duckmatic_;
410         DuckMap duck_map;
411         std::list<etl::handle<Bezier> > bezier_list_;
412         std::list<etl::handle<Stroke> > stroke_list_;
413         DuckDataMap duck_data_share_map;
414         etl::handle<DuckDrag_Base> duck_dragger_;
415
416         bool needs_restore;
417
418 public:
419         Push(Duckmatic *duckmatic_);
420         ~Push();
421         void restore();
422 }; // END of class Duckmatic::Push
423
424 /*! \struct Duckmatic::Bezier
425 **      \writeme */
426 struct Duckmatic::Bezier : public etl::shared_object
427 {
428 private:
429         sigc::signal<void,float> signal_user_click_[5];
430 public:
431
432         etl::handle<Duck> p1,p2,c1,c2;
433         bool is_valid()const { return p1 && p2 && c1 && c2; }
434
435         sigc::signal<void,float> &signal_user_click(int i=0) { assert(i>=0); assert(i<5); return signal_user_click_[i]; }
436 }; // END of struct Duckmatic::Bezier
437
438 /*! \struct Duckmatic::Stroke
439 **      \writeme */
440 struct Duckmatic::Stroke : public etl::shared_object
441 {
442 private:
443         sigc::signal<void,float> signal_user_click_[5];
444 public:
445
446         etl::smart_ptr<std::list<synfig::Point> > stroke_data;
447
448         synfig::Color color;
449
450         bool is_valid()const { return (bool)stroke_data; }
451
452         sigc::signal<void,float> &signal_user_click(int i=0) { assert(i>=0); assert(i<5); return signal_user_click_[i]; }
453 }; // END of struct Duckmatic::Stroke
454
455 }; // END of namespace studio
456
457 /* === E N D =============================================================== */
458
459 #endif