1/* Copyright 2013 Jens Bav
2 * Copyright 2016 Software Freedom Conservancy Inc.
3 *
4 * This software is licensed under the GNU Lesser General Public License
5 * (version 2.1 or later).  See the COPYING file in this distribution.
6 */
7
8using Spit;
9
10private class ChessEffectDescriptor : ShotwellTransitionDescriptor {
11    public ChessEffectDescriptor(GLib.File resource_directory) {
12        base(resource_directory);
13    }
14
15    public override unowned string get_id() {
16        return "org.yorba.shotwell.transitions.chess";
17    }
18
19    public override unowned string get_pluggable_name() {
20        return _("Chess");
21    }
22
23    public override Transitions.Effect create(HostInterface host) {
24        return new ChessEffect();
25    }
26}
27
28private class ChessEffect : Object, Transitions.Effect {
29    private const int DESIRED_FPS = 25;
30    private const int MIN_FPS = 10;
31    private const int SQUARE_SIZE = 100;
32    private double square_count_x;
33    private double square_count_y;
34
35    public ChessEffect() {
36    }
37
38    public void get_fps(out int desired_fps, out int min_fps) {
39        desired_fps = ChessEffect.DESIRED_FPS;
40        min_fps = ChessEffect.MIN_FPS;
41    }
42
43    public void start(Transitions.Visuals visuals, Transitions.Motion motion) {
44      square_count_y = visuals.to_pos.height / SQUARE_SIZE + 2;
45      square_count_x = visuals.to_pos.width / SQUARE_SIZE + 2;
46    }
47
48    public bool needs_clear_background() {
49        return true;
50    }
51
52    public void paint(Transitions.Visuals visuals, Transitions.Motion motion, Cairo.Context ctx,
53        int width, int height, int frame_number) {
54        double alpha = motion.get_alpha(frame_number);
55        double size = 2 * alpha * SQUARE_SIZE;
56        if (visuals.from_pixbuf != null) {
57            Gdk.cairo_set_source_pixbuf(ctx, visuals.from_pixbuf, visuals.from_pos.x,
58                visuals.from_pos.y);
59            ctx.paint_with_alpha(1 - alpha);
60        }
61
62        if (visuals.to_pixbuf != null) {
63            Gdk.cairo_set_source_pixbuf(ctx, visuals.to_pixbuf,visuals.to_pos.x, visuals.to_pos.y);
64            for (double y = 0; y <= square_count_y; y++) {
65                for (double x = 0; x <= square_count_x; x++) {
66                    double translation = (x+y) % 2 == 0 ? -1.5 * SQUARE_SIZE : 1.5 * SQUARE_SIZE;
67                    if (motion.direction == Transitions.Direction.FORWARD) {
68                        ctx.rectangle(visuals.to_pos.x + translation + x * SQUARE_SIZE,
69                        visuals.to_pos.y + y * SQUARE_SIZE, size, SQUARE_SIZE);
70                    } else {
71                        ctx.rectangle(visuals.to_pos.x + visuals.to_pos.width + translation - x
72                            * SQUARE_SIZE - size, visuals.to_pos.y + y * SQUARE_SIZE, size,
73                            SQUARE_SIZE);
74                    }
75                }
76            }
77
78            ctx.clip();
79            ctx.paint_with_alpha(alpha);
80        }
81    }
82
83    public void advance(Transitions.Visuals visuals, Transitions.Motion motion, int frame_number) {
84    }
85
86    public void cancel() {
87    }
88}
89