2 * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using System.Collections;
19 using System.Collections.Generic;
20 using System.Collections.ObjectModel;
21 using System.Collections.Specialized;
22 using static Interop.Elementary;
27 /// Transit is designed to apply various animated transition effects, such like translation, rotation, etc.
28 /// For using these effects, create an Transit and add the desired transition effects.
30 /// <remarks>Transit is not reusable. If the effect ends, the transit is destroyed automatically.</remarks>
31 public class Transit : IDisposable
33 IntPtr _handle = IntPtr.Zero;
34 bool _isDisposed = false;
35 ObservableCollection<EvasObject> _objects = new ObservableCollection<EvasObject>();
36 ObservableCollection<Transit> _chains = new ObservableCollection<Transit>();
37 HashSet<object> _checker = new HashSet<object>();
38 Elm_Transit_Del_Cb DeletedCallback;
39 Elm_Transit_Effect_End_Cb EffectEndCallback;
40 Elm_Transit_Effect_Transition_Cb EffectTransitionCallback;
43 /// A callback called when the transit is deleted.
45 public event EventHandler Deleted;
48 /// Creates and initializes a new instance of Transit class.
52 _handle = Interop.Elementary.elm_transit_add();
53 DeletedCallback = (ptr1, ptr2) =>
55 Deleted?.Invoke(this, EventArgs.Empty);
58 Interop.Elementary.elm_transit_del_cb_set(_handle, DeletedCallback, IntPtr.Zero);
59 ((INotifyCollectionChanged)_objects).CollectionChanged += OnObjectCollectionChanged;
60 ((INotifyCollectionChanged)_chains).CollectionChanged += OnChaninCollectionChanged;
64 /// Destroy the Transit object.
72 /// Gets or sets the transit animation time
74 public double Duration
78 return Interop.Elementary.elm_transit_duration_get(_handle);
82 Interop.Elementary.elm_transit_duration_set(_handle, value);
87 /// Gets or sets a value whether the objects states will be keep or not.
88 /// If it is not kept, the objects states will be reset when transition ends.
90 public bool ObjectStateKeep
94 return Interop.Elementary.elm_transit_objects_final_state_keep_get(_handle);
98 Interop.Elementary.elm_transit_objects_final_state_keep_set(_handle, value);
103 /// Gets or sets the transit animation acceleration type.
105 public TweenMode TweenMode
109 return (TweenMode)Interop.Elementary.elm_transit_tween_mode_get(_handle);
113 Interop.Elementary.elm_transit_tween_mode_set(_handle, (int)value);
118 /// Gets or sets the transit repeat count.
119 /// If the repeat is a negative number, it will repeat infinite times.
125 return Interop.Elementary.elm_transit_repeat_times_get(_handle);
129 Interop.Elementary.elm_transit_repeat_times_set(_handle, value);
134 /// Gets or sets if the auto reverse is on.
136 public bool AutoReverse
140 return Interop.Elementary.elm_transit_auto_reverse_get(_handle);
144 Interop.Elementary.elm_transit_auto_reverse_set(_handle, value);
149 /// Gets or sets the event enabled when transit is operating.
151 public bool EventEnabled
155 return Interop.Elementary.elm_transit_event_enabled_get(_handle);
159 Interop.Elementary.elm_transit_event_enabled_set(_handle, value);
164 /// Gets or sets the smooth scaling for transit map rendering
165 /// This gets smooth scaling for transit map rendering.
171 return Interop.Elementary.elm_transit_smooth_get(_handle);
175 Interop.Elementary.elm_transit_smooth_set(_handle, value);
180 /// Get the time progression of the animation (a double value between 0.0 and 1.0).
181 /// The value returned is a fraction(current time / total time).
182 /// It represents the progression position relative to the total.
184 public double Progress
188 return Interop.Elementary.elm_transit_progress_value_get(_handle);
193 /// Gets or sets the transit animation tween mode acceleration factor.
195 /// <returns>A factor value from 0.0 to 1.0.</returns>
196 public double BeginAccelerationFactor
200 double begin = 1.0, end = 0.0;
201 Interop.Elementary.elm_transit_tween_mode_factor_get(_handle, out begin, out end);
206 Interop.Elementary.elm_transit_tween_mode_factor_set(_handle, value, EndAccelerationFactor);
211 /// Gets or sets the transit animation tween mode acceleration factor.
213 /// <returns>A factor value from 0.0 to 1.0.</returns>
214 public double EndAccelerationFactor
218 double begin = 1.0, end = 0.0;
219 Interop.Elementary.elm_transit_tween_mode_factor_get(_handle, out begin, out end);
224 Interop.Elementary.elm_transit_tween_mode_factor_set(_handle, BeginAccelerationFactor, value);
229 /// Starts the transition in given seconds.
230 /// Once this API is called, the transit begins to measure the time.
232 /// <param name="interval">The interval value in seconds</param>
233 public void Go(double interval = 0)
235 Interop.Elementary.elm_transit_go_in(_handle, interval);
239 /// Pause the transition.
243 if (Interop.Elementary.elm_transit_paused_get(_handle) == false)
244 Interop.Elementary.elm_transit_paused_set(_handle, true);
248 /// Resume the transition.
252 if (Interop.Elementary.elm_transit_paused_get(_handle) == true)
253 Interop.Elementary.elm_transit_paused_set(_handle, false);
257 /// Get the current chained transit list.
259 /// <remarks>Cannot add the duplicate transit.</remarks>
260 public IList<Transit> Chains
262 get { return _chains; }
266 /// Get the objects list of the transit.
268 /// <remarks>Cannot add the duplicate object.</remarks>
269 public IList<EvasObject> Objects
271 get { return _objects; }
277 /// <param name="effect">EffectBase object.</param>
278 public void AddEffect(EffectBase effect)
280 IntPtr _effect = effect.CreateEffect(_handle);
281 EffectEndCallback = (effectPtr, transitPtr) => { effect.SendEffectEnd(); };
282 EffectTransitionCallback = (effectPtr, transitPtr, progress) => { };
283 Interop.Elementary.elm_transit_effect_add(_handle, EffectTransitionCallback, _effect, EffectEndCallback);
287 /// Destroy current object
289 public void Dispose()
292 GC.SuppressFinalize(this);
296 /// Releases all resources currently used by this instance.
298 /// <param name="disposing">
299 /// true if managed resources should be disposed
300 /// otherwise, false.
302 protected virtual void Dispose(bool disposing)
309 ((INotifyCollectionChanged)_chains).CollectionChanged -= OnChaninCollectionChanged;
311 ((INotifyCollectionChanged)_objects).CollectionChanged -= OnObjectCollectionChanged;
319 void OnObjectCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
321 if (e.Action == NotifyCollectionChangedAction.Add)
323 foreach (EvasObject item in e.NewItems)
326 else if (e.Action == NotifyCollectionChangedAction.Remove)
328 foreach (EvasObject item in e.OldItems)
333 void OnChaninCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
335 if (e.Action == NotifyCollectionChangedAction.Add)
337 foreach (Transit item in e.NewItems)
338 AddChainedTransit(item);
340 else if (e.Action == NotifyCollectionChangedAction.Remove)
342 foreach (Transit item in e.OldItems)
343 DeleteChainedTransit(item);
348 /// Add new object to apply the effects.
349 /// After the first addition of an object to transit, if its object list become empty again, the transit will be killed.
350 /// If the obj belongs to another transit, the obj will be removed from it and it will only belong to the other transit.
352 /// <remarks>It is not allowed to add a new object after transit begins.</remarks>
353 /// <param name="obj">Object to be animated.</param>
354 void AddObject(EvasObject obj)
356 if (_checker.Contains(obj))
357 throw new Exception("Cannot add the duplicate object.");
360 Interop.Elementary.elm_transit_object_add(_handle, obj);
364 /// Removes an added object from the transit.
366 /// <param name="obj">Object to be removed from transit.</param>
367 void RemoveObject(EvasObject obj)
369 if (_checker.Contains(obj))
370 _checker.Remove(obj);
372 Interop.Elementary.elm_transit_object_remove(_handle, obj);
376 /// Makes the chain relationship between two transits.
378 /// <param name="transit">The chain transit object. This transit will be operated after transit is done.</param>
379 void AddChainedTransit(Transit transit)
381 if (_checker.Contains(transit))
382 throw new Exception("Cannot add the duplicate transit.");
384 _checker.Add(transit);
385 Interop.Elementary.elm_transit_chain_transit_add(_handle, transit._handle);
389 /// Cut off the chain relationship between two transits.
391 /// <param name="transit">The chain transit object.</param>
392 void DeleteChainedTransit(Transit transit)
394 if (_checker.Contains(transit))
395 _checker.Remove(transit);
397 Interop.Elementary.elm_transit_chain_transit_del(_handle, transit._handle);