Debian Codegraph @

src/utils/animation/GtkAnimation.vala

raw
1 /*
2 * Copyright (c) 2017-2019 José Amuedo (https://github.com/spheras)
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 namespace DesktopFolder.UtilGtkAnimation {
19 /**
20 * @name AnimateFn
21 * @description Animate Function to be executed en each frame
22 * @param {double} ellapsed the time ellapsed since the start of the animation
23 */
24 protected delegate void AnimateFn (double ellapsed);
25
26 public void animate_move (Gtk.Widget widget, Gdk.Point to, double duration, UtilFx.AnimationMode mode = UtilFx.AnimationMode.EASE_OUT_BACK) {
27 WidgetAnimationMove animation = new WidgetAnimationMove (widget, to, duration, mode);
28 animation.start ();
29 }
30
31 /**
32 * @class
33 * @name WidgetAnimation
34 * @description util abstract class to animate widgets, each implementation should implement the animation itself
35 */
36 public abstract class WidgetAnimation {
37 /** the time when the animation was started, in ms */
38 protected double _start_time;
39 /** the duration of the animation, ms */
40 protected double _duration;
41 /** the widget to be moved */
42 protected Gtk.Widget _widget;
43 /** the animation function to be executed in each frame */
44 protected unowned AnimateFn _animation_fn;
45 /** the uid reference for the animation */
46 private uint _animation_ref;
47
48 /**
49 * @constructor
50 * @param {Gtk.Widget} widget the widget to animate
51 * @param {double} duration the duration of the animation
52 * @param {AnimateFn} animation_fn the animation function
53 */
54 protected WidgetAnimation (Gtk.Widget widget, double duration, AnimateFn animation_fn) {
55 this._widget = widget;
56 this._animation_fn = animation_fn;
57 this._duration = duration;
58 }
59
60 /**
61 * @name get_pending_animations
62 * @description return the pending animations for this animation
63 * @return {List<WidgetAnimation>} the list of widget animations that are pending
64 */
65 protected abstract unowned Gee.List <WidgetAnimation> get_pending_animations ();
66
67 /**
68 * @name find_pending_animations
69 * @description util method to find a pending animation for the widget
70 * @return {WidgetAnimation} the Widget animation found or null
71 */
72 private WidgetAnimation ? find_pending_animations () {
73 unowned Gee.List <WidgetAnimation> pending_animations = this.get_pending_animations ();
74 for (var i = 0; i < pending_animations.size; i++) {
75 WidgetAnimation anim = pending_animations.@get (i);
76 if (anim.get_widget () != null && anim.get_widget () == this._widget) {
77 return anim;
78 }
79 }
80 return null;
81 }
82
83 /**
84 * @name finish_pending_animations
85 * @description finish all pending animations for the widget
86 */
87 private void finish_pending_animations () {
88 WidgetAnimation found = this.find_pending_animations ();
89 if (found != null) {
90 found.stop ();
91 }
92 unowned Gee.List <WidgetAnimation> pending_animations = this.get_pending_animations ();
93 pending_animations.remove (found);
94 }
95
96 /**
97 * @name stop
98 * @description stop the current animation, simulating the last frame
99 */
100 public void stop () {
101 // removing the pending threads
102 try {
103 Source.remove (this._animation_ref);
104 } catch (Error err) {
105
106 }
107 // executing the last frame
108 // this._animation_fn (this._duration);
109 }
110
111 /**
112 * @name start
113 * @description start the animation
114 */
115 public void start () {
116 // first we finish all the pending animations for the widget
117 this.finish_pending_animations ();
118
119 // adding a new pending animation
120 unowned Gee.List <WidgetAnimation> pending_animations = this.get_pending_animations ();
121 pending_animations.add (this);
122
123 // and starting a new one
124 this._start_time = (double) GLib.get_real_time () / 1000;
125 this._animation_ref = GLib.Timeout.add (40, () => {
126 double current = (double) GLib.get_real_time () / 1000;
127 double ellapsed = current - this._start_time;
128 this._animation_fn (ellapsed);
129 if (ellapsed < this._duration) {
130 return true;
131 } else {
132 WidgetAnimation found = this.find_pending_animations ();
133 pending_animations.remove (found);
134 return false;
135 }
136 });
137 }
138
139 /**
140 * @Getter
141 */
142 public Gtk.Widget ? get_widget () {
143 return this._widget;
144 }
145
146 /**
147 * @Getter
148 */
149 public double get_duration () {
150 return this._duration;
151 }
152
153 }
154
155 /**
156 * @name WidgetAnimationMove
157 * @description util class to move a Gtk.Widget from its current place to a different place
158 */
159 public class WidgetAnimationMove : WidgetAnimation {
160 /** list of pending move animations */
161 private static Gee.List <WidgetAnimation> move_animations = null;
162
163 static construct {
164 WidgetAnimationMove.move_animations = new Gee.ArrayList <WidgetAnimation>();
165 }
166
167 /**
168 * @override
169 */
170 protected override unowned Gee.List <WidgetAnimation> get_pending_animations () {
171 // WidgetAnimationMove.move_animations.append(this);
172 return WidgetAnimationMove.move_animations;
173 }
174
175 /**
176 * @constructor
177 * @param {Gtk.Widget} widget the widget to animate
178 * @param {Gdk.Point} to the point x,y where the widget will be placed at the end of the animation
179 * @param {double} duration the duration of the animation
180 * @param {UtilFx.AnimationMode} mode (optional) the animation to perform @see UtilFx.AnimationMode, by default EASE_OUT_BACK
181 */
182 public WidgetAnimationMove (Gtk.Widget ? widget, Gdk.Point ? to, double ? duration, UtilFx.AnimationMode mode = UtilFx.AnimationMode.EASE_OUT_BACK) {
183 // calculating the distances and positions to animate
184 Gtk.Fixed parent = (Gtk.Fixed)widget.get_parent ();
185 Gtk.Allocation widget_alloc;
186 widget.get_allocation (out widget_alloc);
187 int x = widget_alloc.x;
188 int y = widget_alloc.y;
189 int x_dist = to.x - x;
190 int y_dist = to.y - y;
191 int x_dist_curr = 0;
192 int y_dist_curr = 0;
193
194 // the animation function
195 AnimateFn anim_fn = (ellapsed) => {
196 if (ellapsed < duration) {
197 double ease = UtilFx.easing_for_mode (mode, ellapsed, duration);
198 x_dist_curr = (int) (x_dist * ease);
199 y_dist_curr = (int) (y_dist * ease);
200 parent.move (widget, x + x_dist_curr, y + y_dist_curr);
201 } else {
202 // last frame
203 parent.move (widget, to.x, to.y);
204 }
205 };
206
207 base (widget, duration, (ell) => {
208 // trick to allow access to widget!
209 // help: why widget is null inside this closure when called from the Glib.Timeout.add??
210 anim_fn (ell);
211 });
212 }
213
214 }
215
216 }