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 as translation, rotation, etc.
28 /// For using these effects, create a transit and add the desired transition effects.
30 /// <remarks>Transit is not reusable. If the effect ends, the transit is destroyed automatically.</remarks>
31 /// <since_tizen> preview </since_tizen>
32 public class Transit : IDisposable
34 IntPtr _handle = IntPtr.Zero;
35 bool _isDisposed = false;
36 ObservableCollection<EvasObject> _objects = new ObservableCollection<EvasObject>();
37 ObservableCollection<Transit> _chains = new ObservableCollection<Transit>();
38 HashSet<object> _checker = new HashSet<object>();
39 Elm_Transit_Del_Cb DeletedCallback;
40 Elm_Transit_Effect_End_Cb EffectEndCallback;
41 Elm_Transit_Effect_Transition_Cb EffectTransitionCallback;
44 /// A callback is called when the transit is deleted.
46 /// <since_tizen> preview </since_tizen>
47 public event EventHandler Deleted;
50 /// Creates and initializes a new instance of the Transit class.
52 /// <since_tizen> preview </since_tizen>
55 _handle = Interop.Elementary.elm_transit_add();
56 DeletedCallback = (ptr1, ptr2) =>
58 Deleted?.Invoke(this, EventArgs.Empty);
61 Interop.Elementary.elm_transit_del_cb_set(_handle, DeletedCallback, IntPtr.Zero);
62 ((INotifyCollectionChanged)_objects).CollectionChanged += OnObjectCollectionChanged;
63 ((INotifyCollectionChanged)_chains).CollectionChanged += OnChaninCollectionChanged;
67 /// Destroys the transit object.
75 /// Gets or sets the transit animation time.
77 /// <since_tizen> preview </since_tizen>
78 public double Duration
82 return Interop.Elementary.elm_transit_duration_get(_handle);
86 Interop.Elementary.elm_transit_duration_set(_handle, value);
91 /// Gets or sets a value whether the objects states will be kept or not.
92 /// If it is not kept, the objects states will be reset when the transition ends.
94 /// <since_tizen> preview </since_tizen>
95 public bool ObjectStateKeep
99 return Interop.Elementary.elm_transit_objects_final_state_keep_get(_handle);
103 Interop.Elementary.elm_transit_objects_final_state_keep_set(_handle, value);
108 /// Gets or sets the transit animation acceleration type.
110 /// <since_tizen> preview </since_tizen>
111 public TweenMode TweenMode
115 return (TweenMode)Interop.Elementary.elm_transit_tween_mode_get(_handle);
119 Interop.Elementary.elm_transit_tween_mode_set(_handle, (int)value);
124 /// Gets or sets the transit repeat count.
125 /// If the repeat is a negative number, it will repeat infinite times.
127 /// <since_tizen> preview </since_tizen>
132 return Interop.Elementary.elm_transit_repeat_times_get(_handle);
136 Interop.Elementary.elm_transit_repeat_times_set(_handle, value);
141 /// Gets or sets if auto reverse is on.
143 /// <since_tizen> preview </since_tizen>
144 public bool AutoReverse
148 return Interop.Elementary.elm_transit_auto_reverse_get(_handle);
152 Interop.Elementary.elm_transit_auto_reverse_set(_handle, value);
157 /// Gets or sets the event enabled when transit is operating.
159 /// <since_tizen> preview </since_tizen>
160 public bool EventEnabled
164 return Interop.Elementary.elm_transit_event_enabled_get(_handle);
168 Interop.Elementary.elm_transit_event_enabled_set(_handle, value);
173 /// Gets or sets the smooth scaling for transit map rendering.
174 /// This gets the smooth scaling for transit map rendering.
176 /// <since_tizen> preview </since_tizen>
181 return Interop.Elementary.elm_transit_smooth_get(_handle);
185 Interop.Elementary.elm_transit_smooth_set(_handle, value);
190 /// Gets 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 /// <since_tizen> preview </since_tizen>
195 public double Progress
199 return Interop.Elementary.elm_transit_progress_value_get(_handle);
204 /// Gets or sets the transit animation tween mode acceleration factor.
206 /// <returns>A factor value from 0.0 to 1.0.</returns>
207 /// <since_tizen> preview </since_tizen>
208 public double BeginAccelerationFactor
212 double begin = 1.0, end = 0.0;
213 Interop.Elementary.elm_transit_tween_mode_factor_get(_handle, out begin, out end);
218 Interop.Elementary.elm_transit_tween_mode_factor_set(_handle, value, EndAccelerationFactor);
223 /// Gets or sets the transit animation tween mode acceleration factor.
225 /// <returns>A factor value from 0.0 to 1.0.</returns>
226 /// <since_tizen> preview </since_tizen>
227 public double EndAccelerationFactor
231 double begin = 1.0, end = 0.0;
232 Interop.Elementary.elm_transit_tween_mode_factor_get(_handle, out begin, out end);
237 Interop.Elementary.elm_transit_tween_mode_factor_set(_handle, BeginAccelerationFactor, value);
242 /// Starts the transition in given seconds.
243 /// Once this API is called, the transit begins to measure the time.
245 /// <param name="interval">The interval value in seconds.</param>
246 /// <since_tizen> preview </since_tizen>
247 public void Go(double interval = 0)
249 Interop.Elementary.elm_transit_go_in(_handle, interval);
253 /// Pauses the transition.
255 /// <since_tizen> preview </since_tizen>
258 if (Interop.Elementary.elm_transit_paused_get(_handle) == false)
259 Interop.Elementary.elm_transit_paused_set(_handle, true);
263 /// Resumes the transition.
265 /// <since_tizen> preview </since_tizen>
268 if (Interop.Elementary.elm_transit_paused_get(_handle) == true)
269 Interop.Elementary.elm_transit_paused_set(_handle, false);
273 /// Gets the current chained transit list.
275 /// <remarks>Cannot add the duplicate transit.</remarks>
276 /// <since_tizen> preview </since_tizen>
277 public IList<Transit> Chains
279 get { return _chains; }
283 /// Gets the objects list of the transit.
285 /// <remarks>Cannot add the duplicate object.</remarks>
286 /// <since_tizen> preview </since_tizen>
287 public IList<EvasObject> Objects
289 get { return _objects; }
295 /// <param name="effect">EffectBase object.</param>
296 /// <since_tizen> preview </since_tizen>
297 public void AddEffect(EffectBase effect)
299 IntPtr _effect = effect.CreateEffect(_handle);
300 EffectEndCallback = (effectPtr, transitPtr) => { effect.SendEffectEnd(); };
301 EffectTransitionCallback = (effectPtr, transitPtr, progress) => { };
302 Interop.Elementary.elm_transit_effect_add(_handle, EffectTransitionCallback, _effect, EffectEndCallback);
306 /// Destroys the current object.
308 /// <since_tizen> preview </since_tizen>
309 public void Dispose()
312 GC.SuppressFinalize(this);
316 /// Releases all the resources currently used by this instance.
318 /// <param name="disposing">
319 /// true if the managed resources should be disposed,
322 /// <since_tizen> preview </since_tizen>
323 protected virtual void Dispose(bool disposing)
330 ((INotifyCollectionChanged)_chains).CollectionChanged -= OnChaninCollectionChanged;
332 ((INotifyCollectionChanged)_objects).CollectionChanged -= OnObjectCollectionChanged;
340 void OnObjectCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
342 if (e.Action == NotifyCollectionChangedAction.Add)
344 foreach (EvasObject item in e.NewItems)
347 else if (e.Action == NotifyCollectionChangedAction.Remove)
349 foreach (EvasObject item in e.OldItems)
354 void OnChaninCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
356 if (e.Action == NotifyCollectionChangedAction.Add)
358 foreach (Transit item in e.NewItems)
359 AddChainedTransit(item);
361 else if (e.Action == NotifyCollectionChangedAction.Remove)
363 foreach (Transit item in e.OldItems)
364 DeleteChainedTransit(item);
369 /// Adds a new object to apply the effects.
370 /// After the first addition of an object to transit, if its object list becomes empty again, the transit will be killed.
371 /// If the object belongs to another transit, the object will be removed from it and it will only belong to the other transit.
373 /// <remarks>It is not allowed to add a new object after the transit begins.</remarks>
374 /// <param name="obj">Object to be animated.</param>
375 void AddObject(EvasObject obj)
377 if (_checker.Contains(obj))
378 throw new InvalidOperationException("Cannot add the duplicate object.");
381 Interop.Elementary.elm_transit_object_add(_handle, obj);
385 /// Removes an added object from the transit.
387 /// <param name="obj">Object to be removed from transit.</param>
388 void RemoveObject(EvasObject obj)
390 if (_checker.Contains(obj))
391 _checker.Remove(obj);
393 Interop.Elementary.elm_transit_object_remove(_handle, obj);
397 /// Makes the chain relationship between two transits.
399 /// <param name="transit">The chain transit object. This transit will be operated after the transit is done.</param>
400 void AddChainedTransit(Transit transit)
402 if (_checker.Contains(transit))
403 throw new InvalidOperationException("Cannot add the duplicate transit.");
405 _checker.Add(transit);
406 Interop.Elementary.elm_transit_chain_transit_add(_handle, transit._handle);
410 /// Cuts off the chain relationship between two transits.
412 /// <param name="transit">The chain transit object.</param>
413 void DeleteChainedTransit(Transit transit)
415 if (_checker.Contains(transit))
416 _checker.Remove(transit);
418 Interop.Elementary.elm_transit_chain_transit_del(_handle, transit._handle);