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;
42 /// A callback called when the transit is deleted.
44 public event EventHandler Deleted;
47 /// Creates and initializes a new instance of Transit class.
51 _handle = Interop.Elementary.elm_transit_add();
52 DeletedCallback = (ptr1, ptr2) => {
53 Deleted?.Invoke(this, EventArgs.Empty);
56 Interop.Elementary.elm_transit_del_cb_set(_handle, DeletedCallback, IntPtr.Zero);
57 ((INotifyCollectionChanged)_objects).CollectionChanged += OnObjectCollectionChanged;
58 ((INotifyCollectionChanged)_chains).CollectionChanged += OnChaninCollectionChanged;
67 /// Gets or sets the transit animation time
69 public double Duration
73 return Interop.Elementary.elm_transit_duration_get(_handle);
77 Interop.Elementary.elm_transit_duration_set(_handle, value);
82 /// Gets or sets a value whether the objects states will be keep or not.
83 /// If it is not kept, the objects states will be reset when transition ends.
85 public bool ObjectStateKeep
89 return Interop.Elementary.elm_transit_objects_final_state_keep_get(_handle);
93 Interop.Elementary.elm_transit_objects_final_state_keep_set(_handle, value);
98 /// Gets or sets the transit animation acceleration type.
100 public TweenMode TweenMode
104 return (TweenMode)Interop.Elementary.elm_transit_tween_mode_get(_handle);
108 Interop.Elementary.elm_transit_tween_mode_set(_handle, (int)value);
113 /// Gets or sets the transit repeat count.
114 /// If the repeat is a negative number, it will repeat infinite times.
120 return Interop.Elementary.elm_transit_repeat_times_get(_handle);
124 Interop.Elementary.elm_transit_repeat_times_set(_handle, value);
129 /// Gets or sets if the auto reverse is on.
131 public bool AutoReverse
135 return Interop.Elementary.elm_transit_auto_reverse_get(_handle);
139 Interop.Elementary.elm_transit_auto_reverse_set(_handle, value);
144 /// Gets or sets the event enabled when transit is operating.
146 public bool EventEnabled
150 return Interop.Elementary.elm_transit_event_enabled_get(_handle);
154 Interop.Elementary.elm_transit_event_enabled_set(_handle, value);
159 /// Gets or sets the smooth scaling for transit map rendering
160 /// This gets smooth scaling for transit map rendering.
166 return Interop.Elementary.elm_transit_smooth_get(_handle);
170 Interop.Elementary.elm_transit_smooth_set(_handle, value);
175 /// Pause/Resume the transition.
181 return Interop.Elementary.elm_transit_paused_get(_handle);
185 Interop.Elementary.elm_transit_paused_set(_handle, value);
190 /// Get the time progression of the animation (a double value between 0.0 and 1.0).
191 /// The value returned is a fraction(current time / total time).
192 /// It represents the progression position relative to the total.
194 public double Progress
198 return Interop.Elementary.elm_transit_progress_value_get(_handle);
203 /// Gets or sets the transit animation tween mode acceleration factor.
205 /// <returns>A factor value from 0.0 to 1.0.</returns>
206 public double BeginAccelerationFactor
210 double begin = 1.0, end = 0.0;
211 Interop.Elementary.elm_transit_tween_mode_factor_get(_handle, out begin, out end);
216 Interop.Elementary.elm_transit_tween_mode_factor_set(_handle, value, EndAccelerationFactor);
221 /// Gets or sets the transit animation tween mode acceleration factor.
223 /// <returns>A factor value from 0.0 to 1.0.</returns>
224 public double EndAccelerationFactor
228 double begin = 1.0, end = 0.0;
229 Interop.Elementary.elm_transit_tween_mode_factor_get(_handle, out begin, out end);
234 Interop.Elementary.elm_transit_tween_mode_factor_set(_handle, BeginAccelerationFactor, value);
239 /// Starts the transition in given seconds.
240 /// Once this API is called, the transit begins to measure the time.
242 /// <param name="interval">The interval value in seconds</param>
243 public void Go(double interval = 0)
245 Interop.Elementary.elm_transit_go_in(_handle, interval);
249 /// Get the current chained transit list.
251 /// <remarks>Cannot add the duplicate transit.</remarks>
252 public IList<Transit> Chains
254 get { return _chains; }
258 /// Get the objects list of the transit.
260 /// <remarks>Cannot add the duplicate object.</remarks>
261 public IList<EvasObject> Objects
263 get { return _objects; }
269 /// <param name="effect">EffectBase object.</param>
270 public void AddEffect(EffectBase effect)
272 IntPtr _effect = effect.CreateEffect(_handle);
273 EffectEndCallback = (ptr1, ptr2) => { effect.SendEffectEnd(); };
274 Interop.Elementary.elm_transit_effect_add(_handle, null, _effect, EffectEndCallback);
277 public void Dispose()
280 GC.SuppressFinalize(this);
283 protected virtual void Dispose(bool isDisposing)
290 ((INotifyCollectionChanged)_chains).CollectionChanged -= OnChaninCollectionChanged;
292 ((INotifyCollectionChanged)_objects).CollectionChanged -= OnObjectCollectionChanged;
300 void OnObjectCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
302 if (e.Action == NotifyCollectionChangedAction.Add)
304 foreach (EvasObject item in e.NewItems)
307 else if (e.Action == NotifyCollectionChangedAction.Remove)
309 foreach (EvasObject item in e.OldItems)
314 void OnChaninCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
316 if (e.Action == NotifyCollectionChangedAction.Add)
318 foreach (Transit item in e.NewItems)
319 AddChainedTransit(item);
321 else if (e.Action == NotifyCollectionChangedAction.Remove)
323 foreach (Transit item in e.OldItems)
324 DeleteChainedTransit(item);
329 /// Add new object to apply the effects.
330 /// After the first addition of an object to transit, if its object list become empty again, the transit will be killed.
331 /// If the obj belongs to another transit, the obj will be removed from it and it will only belong to the other transit.
333 /// <remarks>It is not allowed to add a new object after transit begins.</remarks>
334 /// <param name="obj">Object to be animated.</param>
335 void AddObject(EvasObject obj)
337 if (_checker.Contains(obj))
338 throw new Exception("Cannot add the duplicate object.");
341 Interop.Elementary.elm_transit_object_add(_handle, obj);
345 /// Removes an added object from the transit.
347 /// <param name="obj">Object to be removed from transit.</param>
348 void RemoveObject(EvasObject obj)
350 if (_checker.Contains(obj))
351 _checker.Remove(obj);
353 Interop.Elementary.elm_transit_object_remove(_handle, obj);
357 /// Makes the chain relationship between two transits.
359 /// <param name="transit">The chain transit object. This transit will be operated after transit is done.</param>
360 void AddChainedTransit(Transit transit)
362 if (_checker.Contains(transit))
363 throw new Exception("Cannot add the duplicate transit.");
365 _checker.Add(transit);
366 Interop.Elementary.elm_transit_chain_transit_add(_handle, transit._handle);
370 /// Cut off the chain relationship between two transits.
372 /// <param name="transit">The chain transit object.</param>
373 void DeleteChainedTransit(Transit transit)
375 if (_checker.Contains(transit))
376 _checker.Remove(transit);
378 Interop.Elementary.elm_transit_chain_transit_del(_handle, transit._handle);