Skip to content

Commit

Permalink
Merge pull request #146 from Clancey/Animations
Browse files Browse the repository at this point in the history
Animations
  • Loading branch information
Clancey committed Dec 5, 2019
2 parents 4cb52eb + 8b91f44 commit e57accb
Show file tree
Hide file tree
Showing 41 changed files with 1,227 additions and 205 deletions.
2 changes: 1 addition & 1 deletion sample/Comet.Mac.Sample/ViewController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public ViewController(IntPtr handle) : base(handle)
public override void ViewDidLoad()
{
base.ViewDidLoad();

UI.Init();
var hotViewController = new MainPage().ToViewController();
AddChildViewController(hotViewController);
var view = hotViewController.View;
Expand Down
59 changes: 47 additions & 12 deletions sample/Comet.Samples/AnimationSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,58 @@ public AnimationSample()
{
Body = Build;
}

readonly State<bool> shouldAnimate = true;
Text animatedText;
Button button;
View Build() =>
new VStack
{
new Text("Regular Text Behind..."),
new Text("Text to Animate!")
(animatedText = new Text("Text to Animate!")
.Background(Color.Orange)
.Animate(new Animation
{
Duration = 2000,
Delay = 500,
Options = AnimationOptions.CurveEaseOut | AnimationOptions.Repeat,
TranslateTo = new PointF(100, 50),
RotateTo = 30,
ScaleTo = new PointF(2f, 2f),
}),
new Text("Regular Text Above..."),
.Color(Color.Blue)
.FontSize(10)
.Animate(duration: 3,repeats:true, autoReverses:true, action: (text) => {
text.Background(Color.Blue)
.Color(Color.Orange);
text.FontSize(30);
})

//new Animation
//{
// Duration = 2000,
// Delay = 500,
// Options = AnimationOptions.CurveEaseOut | AnimationOptions.Repeat,
// TranslateTo = new PointF(100, 50),
// RotateTo = 30,
// ScaleTo = new PointF(2f, 2f),
//}
),
new Text("Regular Text Above...")
.BeginAnimationSequence(repeats:true)
.Animate(duration:1,action:(text)=>{
text.Background(Color.Fuchsia);
button.Background(Color.Green);
}).Animate(duration:1,action:(text)=>{
text.Background(Color.AliceBlue);
}).Animate(duration:1,action:(text)=>{
text.Background(Color.Beige);
button.Background(Color.Blue);
}).Animate(duration:1,action:(text)=>{
text.Background(Color.BlueViolet);
}).Animate(duration:1,action:(text)=>{
text.Background(Color.Lavender);
}).Animate(duration:1,action:(text)=>{
text.Background(Color.Fuchsia);
button.Background(Color.Green);
}).EndAnimationSequence(),
(button = new Button("Animate", () => {
shouldAnimate.Value = !shouldAnimate;
if(shouldAnimate)
this.ResumeAnimations();
else
this.PauseAnimations();
}))
};
}
}
2 changes: 1 addition & 1 deletion sample/Comet.Samples/Comet.Samples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<ItemGroup>
<PackageReference Include="SkiaSharp" Version="1.68.1" />
<PackageReference Include="Clancey.Comet.Reload" Version="0.0.9-alpha" />
<PackageReference Include="Microsoft.VisualStudio.Threading" Version="16.4.16" />
<PackageReference Include="Microsoft.VisualStudio.Threading" Version="16.4.33" />
<PackageReference Include="Topten.RichTextKit" Version="0.1.116" />
</ItemGroup>
<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions sample/Comet.Samples/MainPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class MainPage : View
new MenuItem("Virtual Sectioned List View",()=>new VirtualSectionedListViewSample()),
new MenuItem("Virtual List View",()=>new VirtualListViewSample()),
new MenuItem("Binding Sample!",()=> new BindingSample()),
new MenuItem("Animated Skia Sample",()=> new SkiaButtonSample()),
new MenuItem("Animation Sample",()=> new AnimationSample()),
new MenuItem("TabView",()=> new TabViewSample()),
new MenuItem("BasicTestView",()=> new BasicTestView()),
Expand Down
31 changes: 31 additions & 0 deletions sample/Comet.Samples/Skia/SkiaButtonSample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using Comet.Skia;
namespace Comet.Samples
{
public class SkiaButtonSample : View
{
[Body]
View body() => new VStack
{
new SKText("Hello").Color(Color.Black)
.BeginAnimationSequence(repeats:true)
.Animate(duration:1,action:(text)=>{
text.Color(Color.Fuchsia);
}).Animate(duration:1,action:(text)=>{
text.Color(Color.AliceBlue);
}).Animate(duration:1,action:(text)=>{
text.Color(Color.Beige);
}).Animate(duration:1,action:(text)=>{
text.Color(Color.BlueViolet);
}).Animate(duration:1,action:(text)=>{
text.Color(Color.Lavender);
}).Animate(duration:1,action:(text)=>{
text.Color(Color.Fuchsia);
}).EndAnimationSequence(),
//TODO: Figure out why animation doesn't work
new SKButton("Hello").Background(Color.Black).Color(Color.White).Animate(x => {
x.Background(Color.White).Color(Color.Black);
},duration:3)
};
}
}
1 change: 1 addition & 0 deletions src/Comet.Android/Comet.Android.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
<Compile Include="Controls\CUITabView.cs" />
<Compile Include="Controls\CUINavigationView.cs" />
<Compile Include="Handlers\NavigationViewHandler.cs" />
<Compile Include="Services\AndroidTicker.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Resources\AboutResources.txt" />
Expand Down
1 change: 1 addition & 0 deletions src/Comet.Android/CometActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
AndroidContext.CurrentContext = this;
UI.Init();
Xamarin.Essentials.Platform.Init(this, savedInstanceState);

SupportFragmentManager.AddOnBackStackChangedListener(this);
Expand Down
39 changes: 0 additions & 39 deletions src/Comet.Android/Handlers/ViewHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public class ViewHandler : AbstractHandler<View, AView>
[nameof(EnvironmentKeys.Colors.BackgroundColor)] = MapBackgroundColorProperty,
[nameof(EnvironmentKeys.View.Shadow)] = MapShadowProperty,
[nameof(EnvironmentKeys.View.ClipShape)] = MapClipShapeProperty,
[nameof(EnvironmentKeys.Animations.Animation)] = MapAnimationProperty,
};

public ViewHandler() : base(Mapper)
Expand Down Expand Up @@ -76,43 +75,5 @@ public static void RemoveGesture(AndroidViewHandler handler, Gesture gesture)
var listner = handler.GetGestureListener();
listner.RemoveGesture(gesture);
}

public static void MapAnimationProperty(IViewHandler handler, View virtualView)
{
var nativeView = (AView)handler.NativeView;
var animation = virtualView.GetAnimation();
if (animation != null)
{
System.Diagnostics.Debug.WriteLine($"Starting animation [{animation}] on [{virtualView.GetType().Name}/{nativeView.GetType().Name}]");

var duration = Convert.ToInt64(animation.Duration ?? 1000);
var delay = Convert.ToInt64(animation.Delay ?? 0);
var animator = nativeView.Animate();
animator.SetStartDelay(delay);
animator.SetDuration(duration);

if (animation.TranslateTo != null)
{
animator.TranslationX(animation.TranslateTo.Value.X);
animator.TranslationY(animation.TranslateTo.Value.Y);
}

if (animation.RotateTo != null)
{
var angle = Convert.ToInt16(animation.RotateTo.Value);
animator.Rotation(angle);
}

if (animation.ScaleTo != null)
{
animator.ScaleX(animation.ScaleTo.Value.X);
animator.ScaleY(animation.ScaleTo.Value.Y);
}

animator.Start();

// TODO: implement other properties
}
}
}
}
55 changes: 55 additions & 0 deletions src/Comet.Android/Services/AndroidTicker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using Android.Animation;
using Android.Content;
using Android.OS;

namespace Comet.Android.Services
{
public class AndroidTicker : Ticker
{
ValueAnimator _val;
bool _systemEnabled;
public AndroidTicker()
{
_val = new ValueAnimator();
_val.SetIntValues(0, 100); // avoid crash
_val.RepeatCount = ValueAnimator.Infinite;
_val.Update += (s, e) => Fire?.Invoke(); ;
CheckPowerSaveModeStatus();
}

internal void CheckPowerSaveModeStatus()
{
// Android disables animations when it's in power save mode
// So we need to keep track of whether we're in that mode and handle animations accordingly
// We can't just check ValueAnimator.AreAnimationsEnabled() because there's no event for that, and it's
// only supported on API >= 26

//if (!Forms.IsLollipopOrNewer)
//{
// _systemEnabled = true;
// return;
//}

var powerManager = (PowerManager)AndroidContext.CurrentContext.GetSystemService(Context.PowerService);

var powerSaveOn = powerManager.IsPowerSaveMode;

// If power saver is active, then animations will not run
_systemEnabled = !powerSaveOn;

}

public override bool IsRunning => _val.IsStarted;
public override bool SystemEnabled { get => _systemEnabled; }
public override void Start()
{
_val?.Start();

}
public override void Stop()
{
ThreadHelper.FireOnMainThread(() => _val.Cancel());
}
}
}
3 changes: 3 additions & 0 deletions src/Comet.Android/UI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public static void Init()
ThreadHelper.JoinableTaskContext = new Microsoft.VisualStudio.Threading.JoinableTaskContext();
Device.GraphicsService = new AndroidGraphicsService();
Device.BitmapService = new AndroidBitmapService();


AnimationManger.SetTicker(new AndroidTicker());
}
}
}
1 change: 1 addition & 0 deletions src/Comet.Mac/Comet.Mac.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
<Compile Include="Controls\CUITableViewSource.cs" />
<Compile Include="Handlers\ListViewHandler.cs" />
<Compile Include="Controls\CUITableViewCell.cs" />
<Compile Include="Services\MacTicker.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Comet\Comet.csproj">
Expand Down
2 changes: 1 addition & 1 deletion src/Comet.Mac/Services/MacBitmapService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Comet.Map.Graphics;
using Comet.Services;

namespace Comet.iOS.Services
namespace Comet.Mac.Services
{
public class MacBitmapService : AbstractBitmapService
{
Expand Down
40 changes: 40 additions & 0 deletions src/Comet.Mac/Services/MacTicker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using CoreAnimation;
using CoreVideo;
using Foundation;

namespace Comet.Mac
{
public class MacTicker : Ticker
{
public MacTicker()
{
}
CVDisplayLink link;

public override bool IsRunning => link != null;
public override void Start()
{
if (link != null)
return;
link = new CVDisplayLink();
link.SetOutputCallback(DisplayLinkOutputCallback);
link.Start();

}
public CVReturn DisplayLinkOutputCallback(CVDisplayLink displayLink, ref CVTimeStamp inNow,
ref CVTimeStamp inOutputTime, CVOptionFlags flagsIn, ref CVOptionFlags flagsOut)
{
Fire?.Invoke();
return CVReturn.Success;
}
public override void Stop()
{
if (link == null)
return;
link?.Stop();
link?.Dispose();
link = null;
}
}
}
4 changes: 3 additions & 1 deletion src/Comet.Mac/UI.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Foundation;
using Comet.iOS.Services;
using Comet.Mac.Handlers;
using Comet.Mac.Services;

Expand Down Expand Up @@ -45,6 +44,9 @@ public static void Init()
Device.FontService = new MacFontService();
Device.GraphicsService = new MacGraphicsService();
Device.BitmapService = new MacBitmapService();


AnimationManger.SetTicker(new MacTicker());
}
}
}
2 changes: 1 addition & 1 deletion src/Comet.iOS/Comet.iOS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
<Compile Include="Controls\CUITapGestures.cs" />
<Compile Include="Handlers\ActivityIndicatorHandler.cs" />
<Compile Include="Handlers\ProgressBarHandler.cs" />
<Compile Include="Extensions\AnimationExtensions.cs" />
<Compile Include="Services\iOSTicker.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Comet\Comet.csproj">
Expand Down
31 changes: 0 additions & 31 deletions src/Comet.iOS/Extensions/AnimationExtensions.cs

This file was deleted.

Loading

0 comments on commit e57accb

Please sign in to comment.