summaryrefslogtreecommitdiff
path: root/src/utilities/animatedValue.vala
blob: 5bb46f5d1ce843b54f737ceb38d4643039bcefe1 (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
/////////////////////////////////////////////////////////////////////////
// Copyright (c) 2011-2016 by Simon Schneegans
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or (at
// your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
/////////////////////////////////////////////////////////////////////////

namespace GnomePie {

/////////////////////////////////////////////////////////////////////////
/// A class which interpolates smoothly between to given values.
/// Duration and interpolation mode can be specified.
/////////////////////////////////////////////////////////////////////////

public class AnimatedValue : GLib.Object {

    /////////////////////////////////////////////////////////////////////
    /// The direction of the interpolation.
    /////////////////////////////////////////////////////////////////////

    public enum Direction { IN, OUT, IN_OUT, OUT_IN }

    /////////////////////////////////////////////////////////////////////
    /// Type of the interpolation, linear or cubic.
    /////////////////////////////////////////////////////////////////////

    private enum Type { LINEAR, CUBIC }

    /////////////////////////////////////////////////////////////////////
    /// Current value, interpolated.
    /////////////////////////////////////////////////////////////////////

    public double val { get; private set; }

    /////////////////////////////////////////////////////////////////////
    /// Starting value of the interpolation.
    /////////////////////////////////////////////////////////////////////

    public double start { get; private set; default=0.0; }

    /////////////////////////////////////////////////////////////////////
    /// Final value of the interpolation.
    /////////////////////////////////////////////////////////////////////

    public double end { get; private set; default=0.0; }

    /////////////////////////////////////////////////////////////////////
    /// The current state. In range 0 .. 1
    /////////////////////////////////////////////////////////////////////

    private double state = 0.0;

    /////////////////////////////////////////////////////////////////////
    /// Duration of the interpolation. Should be in the same unit as
    /// taken for the update() method.
    /////////////////////////////////////////////////////////////////////

    private double duration = 0.0;

    /////////////////////////////////////////////////////////////////////
    /// The amount of over-shooting of the cubicly interpolated value.
    /////////////////////////////////////////////////////////////////////

    private double multiplier = 0.0;

    /////////////////////////////////////////////////////////////////////
    /// Type of the interpolation, linear or cubic.
    /////////////////////////////////////////////////////////////////////

    private Type type = Type.LINEAR;

    /////////////////////////////////////////////////////////////////////
    /// The direction of the interpolation.
    /////////////////////////////////////////////////////////////////////

    private Direction direction = Direction.IN;

    /////////////////////////////////////////////////////////////////////
    /// Creates a new linearly interpolated value.
    /////////////////////////////////////////////////////////////////////

    public AnimatedValue.linear(double start, double end, double duration) {
        this.val = start;
        this.start = start;
        this.end = end;
        this.duration = duration;
    }

    /////////////////////////////////////////////////////////////////////
    /// Creates a new cubicly interpolated value.
    /////////////////////////////////////////////////////////////////////

    public AnimatedValue.cubic(Direction direction, double start, double end, double duration, double multiplier = 0) {
        this.val = start;
        this.start = start;
        this.end = end;
        this.duration = duration;
        this.direction = direction;
        this.type = Type.CUBIC;
        this.multiplier = multiplier;
    }

    /////////////////////////////////////////////////////////////////////
    /// Resets the final value of the interpolation to a new value. The
    /// current state is taken for the beginning from now.
    /////////////////////////////////////////////////////////////////////

    public void reset_target(double end, double duration) {
        this.end = end;
        this.duration = duration;
        this.start = this.val;

        if (duration == 0.0) {
            this.val = end;
            this.state = 1.0;
        } else {
            this.state = 0.0;
        }
    }

    /////////////////////////////////////////////////////////////////////
    /// Updates the interpolated value according to it's type.
    /////////////////////////////////////////////////////////////////////

    public void update(double time) {
        this.state += time/this.duration;

        if (this.state < 1) {

            switch (this.type) {
                case Type.LINEAR:
                    this.val = update_linear();
                    break;
                case Type.CUBIC:
                    switch (this.direction) {
                        case Direction.IN:
                            this.val = update_ease_in();
                            return;
                        case Direction.OUT:
                            this.val = update_ease_out();
                            return;
                        case Direction.IN_OUT:
                            this.val = update_ease_in_out();
                            return;
                        case Direction.OUT_IN:
                            this.val = update_ease_out_in();
                            return;
                }
                break;
            }

        } else if (this.val != this.end) {
             this.val = this.end;
        }
    }

    /////////////////////////////////////////////////////////////////////
    /// The following equations are based on Robert Penner's easing
    /// equations. See (http://www.robertpenner.com/easing/) and their
    /// adaption by Zeh Fernando, Nate Chatellier and Arthur Debert for
    /// the Tweener class. See (http://code.google.com/p/tweener/).
    /////////////////////////////////////////////////////////////////////

    private double update_linear(double t = this.state, double s = this.start, double e = this.end) {
        return (s + t*(e - s));
    }

    private double update_ease_in(double t = this.state, double s = this.start, double e = this.end) {
        return (s + (t*t*((multiplier+1)*t-multiplier))*(e - s));
    }

    private double update_ease_out(double t = this.state, double s = this.start, double e = this.end) {
        return (s + ((t-1) * (t-1) * ((multiplier+1)*(t-1)+multiplier) + 1) * (e - s));
    }

    private double update_ease_in_out(double t = this.state, double s = this.start, double e = this.end) {
        if (this.state < 0.5) return update_ease_in(t*2, s, e - (e-s)*0.5);
        else                  return update_ease_out(t*2-1, s + (e-s)*0.5, e);
    }

    private double update_ease_out_in(double t = this.state, double s = this.start, double e = this.end) {
        if (this.state < 0.5) return update_ease_out(t*2, s, e - (e-s)*0.5);
        else                  return update_ease_in(t*2-1, s + (e-s)*0.5, e);
    }
}

}