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) => {
54 Deleted?.Invoke(this, EventArgs.Empty);
57 Interop.Elementary.elm_transit_del_cb_set(_handle, DeletedCallback, IntPtr.Zero);
58 ((INotifyCollectionChanged)_objects).CollectionChanged += OnObjectCollectionChanged;
59 ((INotifyCollectionChanged)_chains).CollectionChanged += OnChaninCollectionChanged;
68 /// Gets or sets the transit animation time
70 public double Duration
74 return Interop.Elementary.elm_transit_duration_get(_handle);
78 Interop.Elementary.elm_transit_duration_set(_handle, value);
83 /// Gets or sets a value whether the objects states will be keep or not.
84 /// If it is not kept, the objects states will be reset when transition ends.
86 public bool ObjectStateKeep
90 return Interop.Elementary.elm_transit_objects_final_state_keep_get(_handle);
94 Interop.Elementary.elm_transit_objects_final_state_keep_set(_handle, value);
99 /// Gets or sets the transit animation acceleration type.
101 public TweenMode TweenMode
105 return (TweenMode)Interop.Elementary.elm_transit_tween_mode_get(_handle);
109 Interop.Elementary.elm_transit_tween_mode_set(_handle, (int)value);
114 /// Gets or sets the transit repeat count.
115 /// If the repeat is a negative number, it will repeat infinite times.
121 return Interop.Elementary.elm_transit_repeat_times_get(_handle);
125 Interop.Elementary.elm_transit_repeat_times_set(_handle, value);
130 /// Gets or sets if the auto reverse is on.
132 public bool AutoReverse
136 return Interop.Elementary.elm_transit_auto_reverse_get(_handle);
140 Interop.Elementary.elm_transit_auto_reverse_set(_handle, value);
145 /// Gets or sets the event enabled when transit is operating.
147 public bool EventEnabled
151 return Interop.Elementary.elm_transit_event_enabled_get(_handle);
155 Interop.Elementary.elm_transit_event_enabled_set(_handle, value);
160 /// Gets or sets the smooth scaling for transit map rendering
161 /// This gets smooth scaling for transit map rendering.
167 return Interop.Elementary.elm_transit_smooth_get(_handle);
171 Interop.Elementary.elm_transit_smooth_set(_handle, value);
176 /// Get the time progression of the animation (a double value between 0.0 and 1.0).
177 /// The value returned is a fraction(current time / total time).
178 /// It represents the progression position relative to the total.
180 public double Progress
184 return Interop.Elementary.elm_transit_progress_value_get(_handle);
189 /// Gets or sets the transit animation tween mode acceleration factor.
191 /// <returns>A factor value from 0.0 to 1.0.</returns>
192 public double BeginAccelerationFactor
196 double begin = 1.0, end = 0.0;
197 Interop.Elementary.elm_transit_tween_mode_factor_get(_handle, out begin, out end);
202 Interop.Elementary.elm_transit_tween_mode_factor_set(_handle, value, EndAccelerationFactor);
207 /// Gets or sets the transit animation tween mode acceleration factor.
209 /// <returns>A factor value from 0.0 to 1.0.</returns>
210 public double EndAccelerationFactor
214 double begin = 1.0, end = 0.0;
215 Interop.Elementary.elm_transit_tween_mode_factor_get(_handle, out begin, out end);
220 Interop.Elementary.elm_transit_tween_mode_factor_set(_handle, BeginAccelerationFactor, value);
225 /// Starts the transition in given seconds.
226 /// Once this API is called, the transit begins to measure the time.
228 /// <param name="interval">The interval value in seconds</param>
229 public void Go(double interval = 0)
231 Interop.Elementary.elm_transit_go_in(_handle, interval);
235 /// Pause the transition.
239 if (Interop.Elementary.elm_transit_paused_get(_handle) == false)
240 Interop.Elementary.elm_transit_paused_set(_handle, true);
244 /// Resume the transition.
248 if (Interop.Elementary.elm_transit_paused_get(_handle) == true)
249 Interop.Elementary.elm_transit_paused_set(_handle, false);
253 /// Get the current chained transit list.
255 /// <remarks>Cannot add the duplicate transit.</remarks>
256 public IList<Transit> Chains
258 get { return _chains; }
262 /// Get the objects list of the transit.
264 /// <remarks>Cannot add the duplicate object.</remarks>
265 public IList<EvasObject> Objects
267 get { return _objects; }
273 /// <param name="effect">EffectBase object.</param>
274 public void AddEffect(EffectBase effect)
276 IntPtr _effect = effect.CreateEffect(_handle);
277 EffectEndCallback = (effectPtr, transitPtr) => { effect.SendEffectEnd(); };
278 EffectTransitionCallback = (effectPtr, transitPtr, progress) => { };
279 Interop.Elementary.elm_transit_effect_add(_handle, EffectTransitionCallback, _effect, EffectEndCallback);
282 public void Dispose()
285 GC.SuppressFinalize(this);
288 protected virtual void Dispose(bool isDisposing)
295 ((INotifyCollectionChanged)_chains).CollectionChanged -= OnChaninCollectionChanged;
297 ((INotifyCollectionChanged)_objects).CollectionChanged -= OnObjectCollectionChanged;
305 void OnObjectCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
307 if (e.Action == NotifyCollectionChangedAction.Add)
309 foreach (EvasObject item in e.NewItems)
312 else if (e.Action == NotifyCollectionChangedAction.Remove)
314 foreach (EvasObject item in e.OldItems)
319 void OnChaninCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
321 if (e.Action == NotifyCollectionChangedAction.Add)
323 foreach (Transit item in e.NewItems)
324 AddChainedTransit(item);
326 else if (e.Action == NotifyCollectionChangedAction.Remove)
328 foreach (Transit item in e.OldItems)
329 DeleteChainedTransit(item);
334 /// Add new object to apply the effects.
335 /// After the first addition of an object to transit, if its object list become empty again, the transit will be killed.
336 /// If the obj belongs to another transit, the obj will be removed from it and it will only belong to the other transit.
338 /// <remarks>It is not allowed to add a new object after transit begins.</remarks>
339 /// <param name="obj">Object to be animated.</param>
340 void AddObject(EvasObject obj)
342 if (_checker.Contains(obj))
343 throw new Exception("Cannot add the duplicate object.");
346 Interop.Elementary.elm_transit_object_add(_handle, obj);
350 /// Removes an added object from the transit.
352 /// <param name="obj">Object to be removed from transit.</param>
353 void RemoveObject(EvasObject obj)
355 if (_checker.Contains(obj))
356 _checker.Remove(obj);
358 Interop.Elementary.elm_transit_object_remove(_handle, obj);
362 /// Makes the chain relationship between two transits.
364 /// <param name="transit">The chain transit object. This transit will be operated after transit is done.</param>
365 void AddChainedTransit(Transit transit)
367 if (_checker.Contains(transit))
368 throw new Exception("Cannot add the duplicate transit.");
370 _checker.Add(transit);
371 Interop.Elementary.elm_transit_chain_transit_add(_handle, transit._handle);
375 /// Cut off the chain relationship between two transits.
377 /// <param name="transit">The chain transit object.</param>
378 void DeleteChainedTransit(Transit transit)
380 if (_checker.Contains(transit))
381 _checker.Remove(transit);
383 Interop.Elementary.elm_transit_chain_transit_del(_handle, transit._handle);