summaryrefslogtreecommitdiff
path: root/plugins/shotwell-transitions/CrumbleEffect.vala
blob: 0fe7cdc596d24815f398f285099a862c0c3f9aa0 (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
/* Copyright 2010 Maxim Kartashev
 * Copyright 2011-2015 Yorba Foundation
 *
 * This software is licensed under the GNU Lesser General Public License
 * (version 2.1 or later).  See the COPYING file in this distribution.
 */

using Spit;

private class CrumbleEffectDescriptor : ShotwellTransitionDescriptor {
    public CrumbleEffectDescriptor(GLib.File resource_directory) {
        base(resource_directory);
    }

    public override unowned string get_id() {
        return "org.yorba.shotwell.transitions.crumble";
    }
    
    public override unowned string get_pluggable_name() {
        return _("Crumble");
    }
    
    public override Transitions.Effect create(Spit.HostInterface host) {
        return new CrumbleEffect();
    }
}

private class CrumbleEffect : Object, Transitions.Effect {
    private const int DESIRED_FPS = 25;
    private const int MIN_FPS = 15;
    
    private const int STRIPE_WIDTH = 10;
    
    private Cairo.ImageSurface[] from_stripes;
    private double[] accelerations;
    private int stripes_count;
    
    public CrumbleEffect() {
    }
    
    public void get_fps(out int desired_fps, out int min_fps) {
        desired_fps = CrumbleEffect.DESIRED_FPS;
        min_fps = CrumbleEffect.MIN_FPS;
    }
    
    public bool needs_clear_background() {
        return true;
    }
    
    public void start(Transitions.Visuals visuals, Transitions.Motion motion) {
        Rand rand = new Rand();
        
        // Cut original image into stripes of STRIPE_WIDTH width; also prepare
        // acceleration for each stripe.
        if (visuals.from_pixbuf != null) {
            stripes_count = visuals.from_pixbuf.width / STRIPE_WIDTH;
            from_stripes = new Cairo.ImageSurface[stripes_count];
            accelerations = new double[stripes_count];
            for (int i = 0; i < stripes_count; ++i) {
                from_stripes[i] = new Cairo.ImageSurface(Cairo.Format.RGB24, STRIPE_WIDTH,
                    visuals.from_pixbuf.height);
                Cairo.Context ctx = new Cairo.Context(from_stripes[i]);
                Gdk.cairo_set_source_pixbuf(ctx, visuals.from_pixbuf, - i * STRIPE_WIDTH, 0);
                ctx.paint();
                accelerations[i] = rand.next_double();
            }
        }
    }
    
    public void paint(Transitions.Visuals visuals, Transitions.Motion motion, Cairo.Context ctx,
        int width, int height, int frame_number) {
        double alpha = motion.get_alpha(frame_number);
        
        if (alpha < 0.5) {
            // First part: draw stripes that go down with pre-calculated acceleration
            alpha = alpha * 2; // stretch alpha to [0, 1]
            
            // tear down from_pixbuf first 
            for (int i = 0; i < stripes_count; ++i) {
                int x = visuals.from_pos.x + i * STRIPE_WIDTH;
                double a = alpha + alpha * accelerations[i];
                int y = visuals.from_pos.y + (int) (visuals.from_pixbuf.height * a * a);
                
                ctx.set_source_surface(from_stripes[i], x, y);
                ctx.paint();
            }
        } else if (visuals.to_pixbuf != null) {
            // Second part: fade in next image ("to_pixbuf")
            alpha = (alpha - 0.5) * 2; // stretch alpha to [0, 1]
            Gdk.cairo_set_source_pixbuf(ctx, visuals.to_pixbuf, visuals.to_pos.x, visuals.to_pos.y);
            ctx.paint_with_alpha(alpha);
        } else {
            // TODO: fade in background color
        }
    }
    
    public void advance(Transitions.Visuals visuals, Transitions.Motion motion, int frame_number) {
    }
    
    public void cancel() {
    }
}