/*
* Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
namespace ElmSharp
{
///
/// EcoreTimelineAnimator is a helper class, it provides functions to manager animations.
///
public class EcoreTimelineAnimator
{
double _runtime;
IntPtr _animator;
Action _timelineCallback;
Interop.Ecore.EcoreTimelineCallback _nativeCallback;
double _position;
///
/// It occurs when the animator is complete.
///
public event EventHandler Finished;
///
/// Creates and initializes a new instance of the EcoreTimelineAnimator class.
///
/// The time to run in seconds
/// Functions called at each time line
public EcoreTimelineAnimator(double runtime, Action timelineCallback)
{
_runtime = runtime;
_nativeCallback = NativeCallback;
_timelineCallback = timelineCallback;
_position = 0;
}
///
/// Gets whether the animation is running.
///
public bool IsRunning { get; private set; }
///
/// Gets the current position of the animation.
///
public double Position => _position;
///
/// Starts an animator that runs for a limited time.for a limited time.
///
public void Start()
{
IsRunning = true;
_animator = Interop.Ecore.ecore_animator_timeline_add(_runtime, _nativeCallback, IntPtr.Zero);
}
///
/// Stops an animator that running.
///
public void Stop()
{
IsRunning = false;
_animator = IntPtr.Zero;
}
///
/// Suspends the specified animator.
///
public void Freeze()
{
Interop.Ecore.ecore_animator_freeze(_animator);
}
///
/// Restores execution of the specified animator.
///
public void Thaw()
{
Interop.Ecore.ecore_animator_thaw(_animator);
}
///
/// Callback that is called when ticks off
///
protected void OnTimeline()
{
_timelineCallback();
}
bool NativeCallback(IntPtr data, double pos)
{
_position = pos;
if (!IsRunning) return false;
OnTimeline();
if (pos == 1.0)
{
IsRunning = false;
_animator = IntPtr.Zero;
Finished?.Invoke(this, EventArgs.Empty);
}
return true;
}
}
}