summaryrefslogtreecommitdiff
path: root/src/Orientation.vala
blob: b31c22db17e3acab80bdefe0ceeb0d78f7863c35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/* Copyright 2009-2014 Yorba Foundation
 *
 * This software is licensed under the GNU LGPL (version 2.1 or later).
 * See the COPYING file in this distribution.
 */

public enum Orientation {
    MIN = 1,
    TOP_LEFT = 1,
    TOP_RIGHT = 2,
    BOTTOM_RIGHT = 3,
    BOTTOM_LEFT = 4,
    LEFT_TOP = 5,
    RIGHT_TOP = 6,
    RIGHT_BOTTOM = 7,
    LEFT_BOTTOM = 8,
    MAX = 8;

    public string to_string() {
        switch (this) {
            case TOP_LEFT:
                return "top-left";
                
            case TOP_RIGHT:
                return "top-right";
                
            case BOTTOM_RIGHT:
                return "bottom-right";
                
            case BOTTOM_LEFT:
                return "bottom-left";
                
            case LEFT_TOP:
                return "left-top";
                
            case RIGHT_TOP:
                return "right-top";
                
            case RIGHT_BOTTOM:
                return "right-bottom";
                
            case LEFT_BOTTOM:
                return "left-bottom";
                
            default:
                return "unknown orientation %d".printf((int) this);
        }
    }
    
    public Orientation rotate_clockwise() {
        switch (this) {
            case TOP_LEFT:
                return RIGHT_TOP;
                
            case TOP_RIGHT:
                return RIGHT_BOTTOM;
                
            case BOTTOM_RIGHT:
                return LEFT_BOTTOM;
                
            case BOTTOM_LEFT:
                return LEFT_TOP;
                
            case LEFT_TOP:
                return TOP_RIGHT;
                
            case RIGHT_TOP:
                return BOTTOM_RIGHT;
                
            case RIGHT_BOTTOM:
                return BOTTOM_LEFT;
                
            case LEFT_BOTTOM:
                return TOP_LEFT;
                
            default:
                error("rotate_clockwise: %d", this);
        }
    }
    
    public Orientation rotate_counterclockwise() {
        switch (this) {
            case TOP_LEFT:
                return LEFT_BOTTOM;
                
            case TOP_RIGHT:
                return LEFT_TOP;
                
            case BOTTOM_RIGHT:
                return RIGHT_TOP;
                
            case BOTTOM_LEFT:
                return RIGHT_BOTTOM;
                
            case LEFT_TOP:
                return BOTTOM_LEFT;
                
            case RIGHT_TOP:
                return TOP_LEFT;
                
            case RIGHT_BOTTOM:
                return TOP_RIGHT;
                
            case LEFT_BOTTOM:
                return BOTTOM_RIGHT;
                
            default:
                error("rotate_counterclockwise: %d", this);
        }
    }
    
    public Orientation flip_top_to_bottom() {
        switch (this) {
            case TOP_LEFT:
                return BOTTOM_LEFT;
                
            case TOP_RIGHT:
                return BOTTOM_RIGHT;
                
            case BOTTOM_RIGHT:
                return TOP_RIGHT;
                
            case BOTTOM_LEFT:
                return TOP_LEFT;
                
            case LEFT_TOP:
                return LEFT_BOTTOM;
                
            case RIGHT_TOP:
                return RIGHT_BOTTOM;
                
            case RIGHT_BOTTOM:
                return RIGHT_TOP;
                
            case LEFT_BOTTOM:
                return LEFT_TOP;
                
            default:
                error("flip_top_to_bottom: %d", this);
        }
    }
    
    public Orientation flip_left_to_right() {
        switch (this) {
            case TOP_LEFT:
                return TOP_RIGHT;
                
            case TOP_RIGHT:
                return TOP_LEFT;
                
            case BOTTOM_RIGHT:
                return BOTTOM_LEFT;
                
            case BOTTOM_LEFT:
                return BOTTOM_RIGHT;
                
            case LEFT_TOP:
                return RIGHT_TOP;
                
            case RIGHT_TOP:
                return LEFT_TOP;
                
            case RIGHT_BOTTOM:
                return LEFT_BOTTOM;
                
            case LEFT_BOTTOM:
                return RIGHT_BOTTOM;
                
            default:
                error("flip_left_to_right: %d", this);
        }
    }
    
    public Orientation perform(Rotation rotation) {
        switch (rotation) {
            case Rotation.CLOCKWISE:
                return rotate_clockwise();
            
            case Rotation.COUNTERCLOCKWISE:
                return rotate_counterclockwise();
            
            case Rotation.MIRROR:
                return flip_left_to_right();
            
            case Rotation.UPSIDE_DOWN:
                return flip_top_to_bottom();
            
            default:
                error("perform: %d", (int) rotation);
        }
    }
    
    public Rotation[] to_rotations() {
        switch (this) {
            case TOP_LEFT:
                // identity orientation
                return { };
            
            case TOP_RIGHT:
                return { Rotation.MIRROR };
            
            case BOTTOM_RIGHT:
                return { Rotation.UPSIDE_DOWN };
            
            case BOTTOM_LEFT:
                // flip top-to-bottom
                return { Rotation.MIRROR, Rotation.UPSIDE_DOWN };
            
            case LEFT_TOP:
                return { Rotation.COUNTERCLOCKWISE, Rotation.UPSIDE_DOWN };
            
            case RIGHT_TOP:
                return { Rotation.CLOCKWISE };
            
            case RIGHT_BOTTOM:
                return { Rotation.CLOCKWISE, Rotation.UPSIDE_DOWN };
            
            case LEFT_BOTTOM:
                return { Rotation.COUNTERCLOCKWISE };
            
            default:
                error("to_rotations: %d", this);
        }
    }
    
    public Dimensions rotate_dimensions(Dimensions dim) {
        switch (this) {
            case Orientation.TOP_LEFT:
            case Orientation.TOP_RIGHT:
            case Orientation.BOTTOM_RIGHT:
            case Orientation.BOTTOM_LEFT:
                // fine just as it is
                return dim;

            case Orientation.LEFT_TOP:
            case Orientation.RIGHT_TOP:
            case Orientation.RIGHT_BOTTOM:
            case Orientation.LEFT_BOTTOM:
                // swap
                return Dimensions(dim.height, dim.width);

            default:
                error("rotate_dimensions: %d", this);
        }
    }
    
    public Dimensions derotate_dimensions(Dimensions dim) {
        return rotate_dimensions(dim);
    }

    public Gdk.Pixbuf rotate_pixbuf(Gdk.Pixbuf pixbuf) {
        Gdk.Pixbuf rotated;
        switch (this) {
            case TOP_LEFT:
                // fine just as it is
                rotated = pixbuf;
            break;
            
            case TOP_RIGHT:
                // mirror
                rotated = pixbuf.flip(true);
            break;
            
            case BOTTOM_RIGHT:
                rotated = pixbuf.rotate_simple(Gdk.PixbufRotation.UPSIDEDOWN);
            break;
            
            case BOTTOM_LEFT:
                // flip top-to-bottom
                rotated = pixbuf.flip(false);
            break;
            
            case LEFT_TOP:
                rotated = pixbuf.rotate_simple(Gdk.PixbufRotation.COUNTERCLOCKWISE).flip(false);
            break;
            
            case RIGHT_TOP:
                rotated = pixbuf.rotate_simple(Gdk.PixbufRotation.CLOCKWISE);
            break;
            
            case RIGHT_BOTTOM:
                rotated = pixbuf.rotate_simple(Gdk.PixbufRotation.CLOCKWISE).flip(false);
            break;
            
            case LEFT_BOTTOM:
                rotated = pixbuf.rotate_simple(Gdk.PixbufRotation.COUNTERCLOCKWISE);
            break;
            
            default:
                error("rotate_pixbuf: %d", this);
        }
        
        return rotated;
    }
    
    // space is the unrotated dimensions the point is rotating with
    public Gdk.Point rotate_point(Dimensions space, Gdk.Point point) {
        assert(space.has_area());
        assert(point.x >= 0);
        assert(point.x < space.width);
        assert(point.y >= 0);
        assert(point.y < space.height);
        
        Gdk.Point rotated = Gdk.Point();
        
        switch (this) {
            case TOP_LEFT:
                // fine as-is
                rotated = point;
            break;
                
            case TOP_RIGHT:
                // mirror
                rotated.x = space.width - point.x - 1;
                rotated.y = point.y;
            break;
                
            case BOTTOM_RIGHT:
                // rotate 180
                rotated.x = space.width - point.x - 1;
                rotated.y = space.height - point.y - 1;
            break;
                
            case BOTTOM_LEFT:
                // flip top-to-bottom
                rotated.x = point.x;
                rotated.y = space.height - point.y - 1;
            break;
                
            case LEFT_TOP:
                // rotate 90, flip top-to-bottom
                rotated.x = point.y;
                rotated.y = point.x;
            break;
                
            case RIGHT_TOP:
                // rotate 270
                rotated.x = space.height - point.y - 1;
                rotated.y = point.x;
            break;
                
            case RIGHT_BOTTOM:
                // rotate 270, flip top-to-bottom
                rotated.x = space.height - point.y - 1;
                rotated.y = space.width - point.x - 1;
            break;
                
            case LEFT_BOTTOM:
                // rotate 90
                rotated.x = point.y;
                rotated.y = space.width - point.x - 1;
            break;
                
            default:
                error("rotate_point: %d", this);
        }
        
        return rotated;
    }
    
    // space is the unrotated dimensions the point is return to
    public Gdk.Point derotate_point(Dimensions space, Gdk.Point point) {
        assert(space.has_area());
        
        Gdk.Point derotated = Gdk.Point();
        
        switch (this) {
            case TOP_LEFT:
                // fine as-is
                derotated = point;
            break;
                
            case TOP_RIGHT:
                // mirror
                derotated.x = space.width - point.x - 1;
                derotated.y = point.y;
            break;
                
            case BOTTOM_RIGHT:
                // rotate 180
                derotated.x = space.width - point.x - 1;
                derotated.y = space.height - point.y - 1;
            break;
                
            case BOTTOM_LEFT:
                // flip top-to-bottom
                derotated.x = point.x;
                derotated.y = space.height - point.y - 1;
            break;
                
            case LEFT_TOP:
                // rotate 90, flip top-to-bottom
                derotated.x = point.y;
                derotated.y = point.x;
            break;
                
            case RIGHT_TOP:
                // rotate 270
                derotated.x = point.y;
                derotated.y = space.height - point.x - 1;
            break;
                
            case RIGHT_BOTTOM:
                // rotate 270, flip top-to-bottom
                derotated.x = space.width - point.y - 1;
                derotated.y = space.height - point.x - 1;
            break;
                
            case LEFT_BOTTOM:
                // rotate 90
                derotated.x = space.width - point.y - 1;
                derotated.y = point.x;
            break;
                
            default:
                error("rotate_point: %d", this);
        }
        
        return derotated;
    }
    
    // space is the unrotated dimensions the point is rotating with
    public Box rotate_box(Dimensions space, Box box) {
        Gdk.Point top_left, bottom_right;
        box.get_points(out top_left, out bottom_right);
        
        top_left.x = top_left.x.clamp(0, space.width - 1);
        top_left.y = top_left.y.clamp(0, space.height - 1);

        bottom_right.x = bottom_right.x.clamp(0, space.width - 1);
        bottom_right.y = bottom_right.y.clamp(0, space.height - 1);
        
        top_left = rotate_point(space, top_left);
        bottom_right = rotate_point(space, bottom_right);
        
        return Box.from_points(top_left, bottom_right);
    }
    
    // space is the unrotated dimensions the point is return to
    public Box derotate_box(Dimensions space, Box box) {
        Gdk.Point top_left, bottom_right;
        box.get_points(out top_left, out bottom_right);
        
        top_left = derotate_point(space, top_left);
        bottom_right = derotate_point(space, bottom_right);
        
        return Box.from_points(top_left, bottom_right);
    }
}

public enum Rotation {
    CLOCKWISE,
    COUNTERCLOCKWISE,
    MIRROR,
    UPSIDE_DOWN;
    
    public Gdk.Pixbuf perform(Gdk.Pixbuf pixbuf) {
        switch (this) {
            case CLOCKWISE:
                return pixbuf.rotate_simple(Gdk.PixbufRotation.CLOCKWISE);
            
            case COUNTERCLOCKWISE:
                return pixbuf.rotate_simple(Gdk.PixbufRotation.COUNTERCLOCKWISE);
            
            case MIRROR:
                return pixbuf.flip(true);
            
            case UPSIDE_DOWN:
                return pixbuf.flip(false);
            
            default:
                error("Unknown rotation: %d", (int) this);
        }
    }
    
    public Rotation opposite() {
        switch (this) {
            case CLOCKWISE:
                return COUNTERCLOCKWISE;
            
            case COUNTERCLOCKWISE:
                return CLOCKWISE;
            
            case MIRROR:
            case UPSIDE_DOWN:
                return this;
            
            default:
                error("Unknown rotation: %d", (int) this);
        }
    }
}