Fix 1891491: Only show bline vertices large when the mouse is really hovering over...
[synfig.git] / synfig-studio / trunk / src / gtkmm / duck.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file duck.cpp
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007, 2008 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 /* === H E A D E R S ======================================================= */
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include "duck.h"
34 #include <ETL/misc>
35
36 #include "general.h"
37
38 #endif
39
40 /* === U S I N G =========================================================== */
41
42 using namespace std;
43 using namespace etl;
44 using namespace synfig;
45 using namespace studio;
46
47 /* === M A C R O S ========================================================= */
48
49 /* === G L O B A L S ======================================================= */
50
51 int studio::Duck::duck_count(0);
52
53 struct _DuckCounter
54 {
55         static int counter;
56         ~_DuckCounter()
57         {
58                 if(counter)
59                         synfig::error("%d ducks not yet deleted!",counter);
60         }
61 } _duck_counter;
62
63 int _DuckCounter::counter(0);
64
65
66 /* === P R O C E D U R E S ================================================= */
67
68 /* === M E T H O D S ======================================================= */
69
70 Duck::Duck():
71         rotations(synfig::Angle::deg(0)),
72         origin(0,0),
73         scalar(1),
74         editable(false),
75         radius_(false),
76         tangent_(false),
77         hover_(false)
78 { duck_count++; _DuckCounter::counter++; }
79
80 Duck::Duck(const synfig::Point &point):
81         type_(TYPE_NONE),
82         point(point),
83         rotations(synfig::Angle::deg(0)),
84         origin(0,0),
85         scalar(1),
86         guid_(0),
87         editable(false),
88         radius_(false),
89         tangent_(false),
90         hover_(false)
91 { duck_count++; _DuckCounter::counter++;}
92
93 Duck::Duck(const synfig::Point &point,const synfig::Point &origin):
94         point(point),
95         rotations(synfig::Angle::deg(0)),
96         origin(origin),
97         scalar(1),
98         guid_(0),
99         editable(false),
100         radius_(true),
101         tangent_(false),
102         hover_(false)
103 { duck_count++; _DuckCounter::counter++;}
104
105 Duck::~Duck() { duck_count--; _DuckCounter::counter--;}
106
107 synfig::GUID
108 Duck::get_data_guid()const
109 {
110         if(value_desc_.is_value_node())
111                 return value_desc_.get_value_node()->get_guid();
112         return GUID::hasher(get_name());
113 }
114
115 void
116 Duck::set_name(const synfig::String &x)
117 {
118         name=x;
119         if(guid_==GUID::zero())
120         {
121                 guid_=GUID::hasher(name);
122         }
123 }
124
125
126 bool
127 Duck::operator==(const Duck &rhs)const
128 {
129         if(this==&rhs)
130                 return true;
131         return
132                 name==rhs.name &&
133                 scalar==rhs.scalar &&
134                 type_==rhs.type_ &&
135                 transform_stack_.size()==rhs.transform_stack_.size();
136                 //true;
137                 //(origin_duck?*origin_duck==*rhs.origin_duck:origin==rhs.origin) &&
138                 //(shared_point?*shared_point==*rhs.shared_point:point==rhs.point) ;
139 }
140
141 synfig::Point
142 Duck::get_trans_point()const
143 {
144         return transform_stack_.perform(get_sub_trans_point());
145 }
146
147 void
148 Duck::set_trans_point(const synfig::Point &x)
149 {
150         set_sub_trans_point(transform_stack_.unperform(x));
151 }
152
153 //! Sets the origin point.
154 void
155 Duck::set_origin(const synfig::Point &x)
156 {
157         origin=x; origin_duck=0;
158 }
159
160 //! Sets the origin point as another duck
161 void
162 Duck::set_origin(const etl::handle<Duck> &x)
163 {
164         origin_duck=x;
165 }
166
167 //! Retrieves the origin location
168 synfig::Point
169 Duck::get_origin()const
170 {
171         return origin_duck?origin_duck->get_point():origin;
172 }
173
174 //! Retrieves the origin duck
175 const etl::handle<Duck> &
176 Duck::get_origin_duck() const
177 {
178         return origin_duck;
179 }
180
181 //! Retrieves the origin location
182 synfig::Point
183 Duck::get_trans_origin()const
184 {
185         return transform_stack_.perform(get_sub_trans_origin());
186 }
187
188 synfig::Point
189 Duck::get_sub_trans_point()const
190 {
191         return get_point()*get_scalar()+get_sub_trans_origin();
192 }
193
194 void
195 Duck::set_sub_trans_point(const synfig::Point &x)
196 {
197         if (get_type() == Duck::TYPE_TANGENT ||
198                 get_type() == Duck::TYPE_ANGLE)
199         {
200                 Angle old_angle = get_point().angle();
201                 set_point((x-get_sub_trans_origin())/get_scalar());
202                 Angle change = get_point().angle() - old_angle;
203                 while (change < Angle::deg(-180)) change += Angle::deg(360);
204                 while (change > Angle::deg(180)) change -= Angle::deg(360);
205                 int old_quarters = round_to_int(Angle::deg(rotations).get()/90);
206                 rotations += change;
207                 int new_quarters = round_to_int(Angle::deg(rotations).get()/90);
208                 if (old_quarters != new_quarters)
209                         synfig::info("rotation: %.2f turns", new_quarters/4.0);
210         }
211         else
212                 set_point((x-get_sub_trans_origin())/get_scalar());
213 }
214
215 synfig::Point
216 Duck::get_sub_trans_origin()const
217 {
218         return origin_duck?origin_duck->get_sub_trans_point():origin;
219 }