Orientation.vala 14 KB
Newer Older
1
/* Copyright 2016 Software Freedom Conservancy Inc.
2 3
 *
 * This software is licensed under the GNU LGPL (version 2.1 or later).
4
 * See the COPYING file in this distribution.
5
 */
6 7

public enum Orientation {
Jim Nelson's avatar
Jim Nelson committed
8
    MIN = 1,
9 10 11 12 13 14 15 16
    TOP_LEFT = 1,
    TOP_RIGHT = 2,
    BOTTOM_RIGHT = 3,
    BOTTOM_LEFT = 4,
    LEFT_TOP = 5,
    RIGHT_TOP = 6,
    RIGHT_BOTTOM = 7,
    LEFT_BOTTOM = 8,
Jim Nelson's avatar
Jim Nelson committed
17
    MAX = 8;
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

    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;
                
76
            default:
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
                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;
                
107
            default:
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
                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:
127
                return LEFT_BOTTOM;
128 129
                
            case RIGHT_TOP:
130
                return RIGHT_BOTTOM;
131 132
                
            case RIGHT_BOTTOM:
133
                return RIGHT_TOP;
134 135
                
            case LEFT_BOTTOM:
136
                return LEFT_TOP;
137
                
138
            default:
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
                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;
                
169
            default:
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
                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);
        }
    }
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
    
    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);
        }
    }
    
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
    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);
        }
    }
246 247 248 249
    
    public Dimensions derotate_dimensions(Dimensions dim) {
        return rotate_dimensions(dim);
    }
250

251 252
    public Gdk.Pixbuf rotate_pixbuf(Gdk.Pixbuf pixbuf) {
        Gdk.Pixbuf rotated;
253 254 255
        switch (this) {
            case TOP_LEFT:
                // fine just as it is
256
                rotated = pixbuf;
257 258 259 260
            break;
            
            case TOP_RIGHT:
                // mirror
261
                rotated = pixbuf.flip(true);
262 263 264
            break;
            
            case BOTTOM_RIGHT:
265
                rotated = pixbuf.rotate_simple(Gdk.PixbufRotation.UPSIDEDOWN);
266 267 268 269
            break;
            
            case BOTTOM_LEFT:
                // flip top-to-bottom
270
                rotated = pixbuf.flip(false);
271 272 273
            break;
            
            case LEFT_TOP:
274
                rotated = pixbuf.rotate_simple(Gdk.PixbufRotation.COUNTERCLOCKWISE).flip(false);
275 276 277
            break;
            
            case RIGHT_TOP:
278
                rotated = pixbuf.rotate_simple(Gdk.PixbufRotation.CLOCKWISE);
279 280 281
            break;
            
            case RIGHT_BOTTOM:
282
                rotated = pixbuf.rotate_simple(Gdk.PixbufRotation.CLOCKWISE).flip(false);
283 284 285
            break;
            
            case LEFT_BOTTOM:
286
                rotated = pixbuf.rotate_simple(Gdk.PixbufRotation.COUNTERCLOCKWISE);
287 288 289 290 291 292
            break;
            
            default:
                error("rotate_pixbuf: %d", this);
        }
        
293
        return rotated;
294 295 296 297
    }
    
    // space is the unrotated dimensions the point is rotating with
    public Gdk.Point rotate_point(Dimensions space, Gdk.Point point) {
298
        assert(space.has_area());
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
        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
314
                rotated.x = space.width - point.x - 1;
315 316 317 318 319
                rotated.y = point.y;
            break;
                
            case BOTTOM_RIGHT:
                // rotate 180
320 321
                rotated.x = space.width - point.x - 1;
                rotated.y = space.height - point.y - 1;
322 323 324 325 326
            break;
                
            case BOTTOM_LEFT:
                // flip top-to-bottom
                rotated.x = point.x;
327
                rotated.y = space.height - point.y - 1;
328 329 330 331 332 333 334 335 336 337
            break;
                
            case LEFT_TOP:
                // rotate 90, flip top-to-bottom
                rotated.x = point.y;
                rotated.y = point.x;
            break;
                
            case RIGHT_TOP:
                // rotate 270
338
                rotated.x = space.height - point.y - 1;
339 340 341 342 343
                rotated.y = point.x;
            break;
                
            case RIGHT_BOTTOM:
                // rotate 270, flip top-to-bottom
344 345
                rotated.x = space.height - point.y - 1;
                rotated.y = space.width - point.x - 1;
346 347 348 349 350
            break;
                
            case LEFT_BOTTOM:
                // rotate 90
                rotated.x = point.y;
351
                rotated.y = space.width - point.x - 1;
352 353 354 355 356 357 358 359 360 361 362
            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) {
363 364
        assert(space.has_area());
        
365 366 367 368 369 370 371 372 373 374
        Gdk.Point derotated = Gdk.Point();
        
        switch (this) {
            case TOP_LEFT:
                // fine as-is
                derotated = point;
            break;
                
            case TOP_RIGHT:
                // mirror
375
                derotated.x = space.width - point.x - 1;
376 377 378 379 380
                derotated.y = point.y;
            break;
                
            case BOTTOM_RIGHT:
                // rotate 180
381 382
                derotated.x = space.width - point.x - 1;
                derotated.y = space.height - point.y - 1;
383 384 385 386 387
            break;
                
            case BOTTOM_LEFT:
                // flip top-to-bottom
                derotated.x = point.x;
388
                derotated.y = space.height - point.y - 1;
389 390 391 392 393 394 395 396 397 398 399
            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;
400
                derotated.y = space.height - point.x - 1;
401 402 403 404
            break;
                
            case RIGHT_BOTTOM:
                // rotate 270, flip top-to-bottom
405 406
                derotated.x = space.width - point.y - 1;
                derotated.y = space.height - point.x - 1;
407 408 409 410
            break;
                
            case LEFT_BOTTOM:
                // rotate 90
411
                derotated.x = space.width - point.y - 1;
412 413 414 415 416 417 418 419 420 421
                derotated.y = point.x;
            break;
                
            default:
                error("rotate_point: %d", this);
        }
        
        return derotated;
    }
    
422
    // space is the unrotated dimensions the point is rotating with
423 424 425 426
    public Box rotate_box(Dimensions space, Box box) {
        Gdk.Point top_left, bottom_right;
        box.get_points(out top_left, out bottom_right);
        
427 428 429 430 431 432
        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);
        
433 434 435 436 437 438
        top_left = rotate_point(space, top_left);
        bottom_right = rotate_point(space, bottom_right);
        
        return Box.from_points(top_left, bottom_right);
    }
    
439
    // space is the unrotated dimensions the point is return to
440 441 442 443 444 445 446 447 448 449 450
    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);
    }
}

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
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);
        }
    }
}