~ubuntu-branches/ubuntu/karmic/moon/karmic

« back to all changes in this revision

Viewing changes to src/transform.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2009-02-14 12:01:08 UTC
  • Revision ID: james.westby@ubuntu.com-20090214120108-06539vb25vhbd8bn
Tags: upstream-1.0
ImportĀ upstreamĀ versionĀ 1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
 
2
/*
 
3
 * transform.cpp: 
 
4
 *
 
5
 * Contact:
 
6
 *   Moonlight List (moonlight-list@lists.ximian.com)
 
7
 *
 
8
 * Copyright 2007 Novell, Inc. (http://www.novell.com)
 
9
 *
 
10
 * See the LICENSE file included with the distribution for details.
 
11
 */
 
12
 
 
13
#include <config.h>
 
14
 
 
15
#include "transform.h"
 
16
#include "math.h"
 
17
 
 
18
 
 
19
//
 
20
// GeneralTransform
 
21
//
 
22
 
 
23
void
 
24
GeneralTransform::OnPropertyChanged (PropertyChangedEventArgs *args)
 
25
{
 
26
        if (args->property->GetOwnerType() == Type::DEPENDENCY_OBJECT) {
 
27
                DependencyObject::OnPropertyChanged (args);
 
28
                return;
 
29
        }
 
30
 
 
31
        need_update = true;
 
32
        //
 
33
        // If the transform changes, we need to notify our owners
 
34
        // that they must repaint (all of our properties have
 
35
        // a visible effect.
 
36
        //
 
37
        // There is no need to override this on the base classes
 
38
        // as they are sealed, so no new properties can be added
 
39
        // and I do not believe that we can create new instances
 
40
        // of transform from C#, and in that case, we would only
 
41
        // be slower.
 
42
        //
 
43
        NotifyListenersOfPropertyChange (args);
 
44
}
 
45
 
 
46
void
 
47
GeneralTransform::UpdateTransform ()
 
48
{
 
49
        g_warning ("GeneralTransform::UpdateTransform has been called. The derived class should have overridden it.");
 
50
}
 
51
 
 
52
void
 
53
GeneralTransform::MaybeUpdateTransform ()
 
54
{
 
55
        if (need_update) {
 
56
                UpdateTransform ();
 
57
                need_update = false;
 
58
        }
 
59
}
 
60
 
 
61
void
 
62
GeneralTransform::GetTransform (cairo_matrix_t *value)
 
63
{
 
64
        MaybeUpdateTransform ();
 
65
        *value = _matrix;
 
66
}
 
67
 
 
68
Point
 
69
GeneralTransform::Transform (Point point)
 
70
{
 
71
        MaybeUpdateTransform ();
 
72
        return point.Transform (&_matrix);
 
73
}
 
74
 
 
75
void
 
76
general_transform_transform_point (GeneralTransform *t, Point *p, Point *r)
 
77
{
 
78
        *r = t->Transform (*p);
 
79
}
 
80
 
 
81
 
 
82
//
 
83
// RotateTransform
 
84
//
 
85
 
 
86
void
 
87
RotateTransform::UpdateTransform ()
 
88
{
 
89
        double angle, center_x, center_y;
 
90
        double radians;
 
91
 
 
92
        angle = GetAngle ();
 
93
        center_x = GetCenterX ();
 
94
        center_y = GetCenterY ();
 
95
        
 
96
        radians = angle / 180.0 * M_PI;
 
97
 
 
98
        if (center_x == 0.0 && center_y == 0.0) {
 
99
                cairo_matrix_init_rotate (&_matrix, radians);
 
100
        }
 
101
        else {
 
102
                cairo_matrix_init_translate (&_matrix, center_x, center_y);
 
103
                cairo_matrix_rotate (&_matrix, radians);
 
104
                cairo_matrix_translate (&_matrix, -center_x, -center_y);
 
105
        }
 
106
        //printf ("Returning2 %g %g %g %g %g %g\n", value->xx, value->yx, value->xy, value->yy, value->x0, value->y0);
 
107
}
 
108
 
 
109
 
 
110
//
 
111
// TranslateTransform
 
112
//
 
113
 
 
114
void
 
115
TranslateTransform::UpdateTransform ()
 
116
{
 
117
        double x = GetX ();
 
118
        double y = GetY ();
 
119
 
 
120
        cairo_matrix_init_translate (&_matrix, x, y);
 
121
        //printf ("translating dx %g dy %g", x, y);
 
122
        //printf ("TranslateTransform %g %g %g %g %g %g\n", value->xx, value->yx, value->xy, value->yy, value->x0, value->y0);
 
123
}
 
124
 
 
125
//
 
126
// ScaleTransform
 
127
//
 
128
void
 
129
ScaleTransform::UpdateTransform ()
 
130
{
 
131
        double sx = GetScaleX ();
 
132
        double sy = GetScaleY ();
 
133
 
 
134
        // XXX you don't want to know.  don't make these 0.00001, or
 
135
        // else cairo spits out errors about non-invertable matrices
 
136
        // (or worse, crashes)
 
137
        //
 
138
        // the 0.0 scales are caused in at least one instance by us
 
139
        // being too aggressive at starting animations at time=0 when
 
140
        // they're supposed to (unset, or 0:0:0 BeginTime)
 
141
        //
 
142
        if (sx == 0.0) sx = 0.00002;
 
143
        if (sy == 0.0) sy = 0.00002;
 
144
 
 
145
        double cx = GetCenterX ();
 
146
        double cy = GetCenterY ();
 
147
 
 
148
        if (cx == 0.0 && cy == 0.0) {
 
149
                cairo_matrix_init_scale (&_matrix, sx, sy);
 
150
        }
 
151
        else {
 
152
                cairo_matrix_init_translate (&_matrix, cx, cy);
 
153
                cairo_matrix_scale (&_matrix, sx, sy);
 
154
                cairo_matrix_translate (&_matrix, -cx, -cy);
 
155
        }
 
156
        //printf ("scaling sx %g sy %g at center cx %g cy %g\n", sx, sy, cx, cy);
 
157
        //printf ("ScaleTransform %g %g %g %g %g %g\n", value->xx, value->yx, value->xy, value->yy, value->x0, value->y0);
 
158
}
 
159
 
 
160
//
 
161
// SkewTransform
 
162
//
 
163
 
 
164
void
 
165
SkewTransform::UpdateTransform ()
 
166
{
 
167
        double cx = GetCenterX ();
 
168
        double cy = GetCenterY ();
 
169
 
 
170
        bool translation = ((cx != 0.0) || (cy != 0.0));
 
171
        if (translation)
 
172
                cairo_matrix_init_translate (&_matrix, cx, cy);
 
173
        else
 
174
                cairo_matrix_init_identity (&_matrix);
 
175
 
 
176
        double ax = GetAngleX ();
 
177
        if (ax != 0.0)
 
178
                _matrix.xy = tan (ax * M_PI / 180);
 
179
 
 
180
        double ay = GetAngleY ();
 
181
        if (ay != 0.0)
 
182
                _matrix.yx = tan (ay * M_PI / 180);
 
183
 
 
184
        if (translation)
 
185
                cairo_matrix_translate (&_matrix, -cx, -cy);
 
186
 
 
187
        //printf ("SkewTransform %g %g %g %g %g %g\n", value->xx, value->yx, value->xy, value->yy, value->x0, value->y0);
 
188
}
 
189
 
 
190
//
 
191
// Matrix
 
192
//
 
193
 
 
194
Matrix::Matrix ()
 
195
{
 
196
        cairo_matrix_init_identity (&matrix);
 
197
}
 
198
 
 
199
Matrix::Matrix(cairo_matrix_t *m)
 
200
{
 
201
        matrix = *m;
 
202
}
 
203
 
 
204
void
 
205
Matrix::OnPropertyChanged (PropertyChangedEventArgs *args)
 
206
{
 
207
        if (args->property->GetOwnerType() != Type::MATRIX) {
 
208
                DependencyObject::OnPropertyChanged (args);
 
209
                return;
 
210
        }
 
211
        
 
212
        if (args->property == Matrix::M11Property)
 
213
                matrix.xx = args->new_value->AsDouble ();
 
214
        else if (args->property == Matrix::M12Property)
 
215
                matrix.yx = args->new_value->AsDouble ();
 
216
        else if (args->property == Matrix::M21Property)
 
217
                matrix.xy = args->new_value->AsDouble ();
 
218
        else if (args->property == Matrix::M22Property)
 
219
                matrix.yy = args->new_value->AsDouble ();
 
220
        else if (args->property == Matrix::OffsetXProperty)
 
221
                matrix.x0 = args->new_value->AsDouble ();
 
222
        else if (args->property == Matrix::OffsetYProperty)
 
223
                matrix.y0 = args->new_value->AsDouble ();
 
224
 
 
225
        NotifyListenersOfPropertyChange (args);
 
226
}
 
227
 
 
228
cairo_matrix_t
 
229
Matrix::GetUnderlyingMatrix ()
 
230
{
 
231
        return matrix;
 
232
}
 
233
 
 
234
//
 
235
// MatrixTransform
 
236
//
 
237
 
 
238
void
 
239
MatrixTransform::OnSubPropertyChanged (DependencyProperty *prop, DependencyObject *obj, PropertyChangedEventArgs *subobj_args)
 
240
{
 
241
        need_update = true;
 
242
 
 
243
        DependencyObject::OnSubPropertyChanged (prop, obj, subobj_args);
 
244
 
 
245
        NotifyListenersOfPropertyChange (MatrixTransform::MatrixProperty);
 
246
}
 
247
 
 
248
void
 
249
MatrixTransform::UpdateTransform ()
 
250
{
 
251
        Matrix *matrix = GetMatrix ();
 
252
        if (matrix)
 
253
                _matrix = matrix->GetUnderlyingMatrix();
 
254
        else
 
255
                cairo_matrix_init_identity (&_matrix);
 
256
}
 
257
 
 
258
//
 
259
// TransformGroup
 
260
//
 
261
 
 
262
TransformGroup::TransformGroup ()
 
263
{
 
264
        SetValue (TransformGroup::ChildrenProperty, Value::CreateUnref (new TransformCollection ()));
 
265
}
 
266
 
 
267
TransformGroup::~TransformGroup ()
 
268
{
 
269
}
 
270
 
 
271
void
 
272
TransformGroup::OnPropertyChanged (PropertyChangedEventArgs *args)
 
273
{
 
274
        if (args->property->GetOwnerType() != Type::TRANSFORMGROUP) {
 
275
                Transform::OnPropertyChanged (args);
 
276
                return;
 
277
        }
 
278
 
 
279
        if (args->property == TransformGroup::ChildrenProperty) {
 
280
                need_update = true;
 
281
        }
 
282
 
 
283
        NotifyListenersOfPropertyChange (args);
 
284
}
 
285
 
 
286
void
 
287
TransformGroup::OnCollectionChanged (Collection *col, CollectionChangedEventArgs *args)
 
288
{
 
289
        if (col != GetChildren ()) {
 
290
                Transform::OnCollectionChanged (col, args);
 
291
                return;
 
292
        }
 
293
        
 
294
        need_update = true;
 
295
        NotifyListenersOfPropertyChange (TransformGroup::ChildrenProperty);
 
296
}
 
297
 
 
298
void
 
299
TransformGroup::OnCollectionItemChanged (Collection *col, DependencyObject *obj, PropertyChangedEventArgs *args)
 
300
{
 
301
        if (col != GetChildren ()) {
 
302
                Transform::OnCollectionItemChanged (col, obj, args);
 
303
                return;
 
304
        }
 
305
        
 
306
        need_update = true;
 
307
        NotifyListenersOfPropertyChange (TransformGroup::ChildrenProperty);
 
308
}
 
309
 
 
310
void
 
311
TransformGroup::UpdateTransform ()
 
312
{
 
313
        TransformCollection *children = GetChildren ();
 
314
        
 
315
        cairo_matrix_init_identity (&_matrix);
 
316
        
 
317
        for (int i = 0; i < children->GetCount (); i++) {
 
318
                Transform *transform = children->GetValueAt (i)->AsTransform ();
 
319
                cairo_matrix_t matrix;
 
320
                
 
321
                transform->GetTransform (&matrix);
 
322
                cairo_matrix_multiply (&_matrix, &_matrix, &matrix);
 
323
        }
 
324
}