Some refactoring for the Skia Demos

Now to add a new demo method you only need to add it to the
SkiaSharp.Demos file and update the sampleList member at the bottom
of the file.
This commit is contained in:
Bill Holmes 2015-11-24 13:33:27 -05:00
Родитель 7edfd3c4fa
Коммит dc4ef53286
7 изменённых файлов: 51 добавлений и 38 удалений

Просмотреть файл

@ -1,9 +1,10 @@
using System;
using SkiaSharp;
using System.Linq;
namespace Skia.Forms.Demo
namespace SkiaSharp
{
public class DrawHelpers
public class Demos
{
#pragma warning disable 414
static readonly SKColor XamLtBlue = new SKColor (0x34, 0x98, 0xdb);
@ -171,6 +172,38 @@ namespace Skia.Forms.Demo
}
}
}
[Flags]
public enum Platform
{
iOS = 1,
Android = 2,
OSX = 4,
All = 0xFFFF,
}
class Sample
{
public string Title { get; set; }
public Action <SKCanvas, int, int> Method { get; set; }
public Platform Platform { get; set; }
}
public static string [] SamplesForPlatform (Platform platform)
{
return sampleList.Where (s => 0 != (s.Platform & platform)).Select (s => s.Title).ToArray ();
}
public static Action <SKCanvas, int, int> MethodForSample (string title)
{
return sampleList.Where (s => s.Title == title).First ().Method;
}
static Sample [] sampleList = new Sample[] {
new Sample {Title="Xamagon", Method = DrawXamagon, Platform = Platform.All},
new Sample {Title="Text Sample", Method = TextSample, Platform = Platform.All},
new Sample {Title="Gradient Sample", Method = DrawGradient, Platform = Platform.All},
};
}
}

Просмотреть файл

@ -21,33 +21,18 @@ namespace Skia.Forms.Demo
BarTextColor = Xamarin.Forms.Color.White,
};
listView.ItemsSource = new [] {"Xamagon", "Text Sample", "Gradient Sample"};
listView.ItemsSource = Demos.SamplesForPlatform (Demos.Platform.iOS | Demos.Platform.Android);
listView.ItemSelected += (sender, e) => {
if (e.SelectedItem == null) return;
listView.SelectedItem = null;
navPage.PushAsync (new ContentPage {
Content = new SkiaView (GetDrawHandler (e.SelectedItem.ToString ())),
Content = new SkiaView (SkiaSharp.Demos.MethodForSample (e.SelectedItem.ToString ())),
});
};
}
Action <SKCanvas, int, int> GetDrawHandler (string selectedItem)
{
switch (selectedItem) {
case "Xamagon":
return DrawHelpers.DrawXamagon;
case "Text Sample":
return DrawHelpers.TextSample;
case "Gradient Sample":
return DrawHelpers.DrawGradient;
}
throw new NotImplementedException ();
}
protected override void OnStart ()
{
// Handle when your app starts

Просмотреть файл

@ -32,9 +32,11 @@
<ItemGroup>
<Compile Include="Skia.Forms.Demo.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="DrawHelpers.cs" />
<Compile Include="ISkiaViewController.cs" />
<Compile Include="SkiaView.cs" />
<Compile Include="..\SharedDemo\SkiaSharp.Demos.cs">
<Link>SkiaSharp.Demos.cs</Link>
</Compile>
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
<Import Project="packages\Xamarin.Forms.1.3.5.6335\build\portable-win+net45+wp80+MonoAndroid10+MonoTouch10+Xamarin.iOS10\Xamarin.Forms.targets" Condition="Exists('packages\Xamarin.Forms.1.3.5.6335\build\portable-win+net45+wp80+MonoAndroid10+MonoTouch10+Xamarin.iOS10\Xamarin.Forms.targets')" />

Просмотреть файл

@ -85,9 +85,9 @@
<autoresizingMask key="autoresizingMask"/>
<subviews>
<popUpButton verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="mnJ-8z-WRI">
<rect key="frame" x="334" y="17" width="129" height="26"/>
<rect key="frame" x="258" y="17" width="205" height="26"/>
<constraints>
<constraint firstAttribute="width" constant="124" id="3Ya-mE-NFc"/>
<constraint firstAttribute="width" constant="200" id="3Ya-mE-NFc"/>
<constraint firstAttribute="height" constant="21" id="PW1-2o-QII"/>
</constraints>
<popUpButtonCell key="cell" type="push" bezelStyle="rounded" alignment="left" lineBreakMode="truncatingTail" borderStyle="borderAndBezel" imageScaling="proportionallyDown" inset="2" id="5te-c2-WKW">

Просмотреть файл

@ -14,13 +14,15 @@ namespace Skia.OSX.Demo
public SkaiView (IntPtr handle) : base (handle)
{
onDrawCallback = Skia.Forms.Demo.DrawHelpers.DrawXamagon;
}
public override void DrawRect (CoreGraphics.CGRect dirtyRect)
{
base.DrawRect (dirtyRect);
if (onDrawCallback == null)
return;
// Just not that sharp using the scale pixel scale only
// Going going 2x Pixel Scale
var screenScale = (int)NSScreen.MainScreen.BackingScaleFactor * 2;

Просмотреть файл

@ -80,12 +80,12 @@
<Compile Include="SkaiView.designer.cs">
<DependentUpon>SkaiView.cs</DependentUpon>
</Compile>
<Compile Include="..\Skia.Forms.Demo\DrawHelpers.cs">
<Link>DrawHelpers.cs</Link>
</Compile>
<Compile Include="AppDelegate.designer.cs">
<DependentUpon>AppDelegate.cs</DependentUpon>
</Compile>
<Compile Include="..\SharedDemo\SkiaSharp.Demos.cs">
<Link>SkiaSharp.Demos.cs</Link>
</Compile>
</ItemGroup>
<ItemGroup>
<InterfaceDefinition Include="Main.storyboard" />

Просмотреть файл

@ -14,23 +14,14 @@ namespace Skia.OSX.Demo
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
PopUpButton.AddItems (new [] {"Xamagon", "Text", "Gradient"});
PopUpButton.AddItems (SkiaSharp.Demos.SamplesForPlatform (SkiaSharp.Demos.Platform.OSX));
PopUpButton.SelectItem (0);
SkiaView.OnDrawCallback = SkiaSharp.Demos.MethodForSample (PopUpButton.SelectedItem.Title);
}
partial void PopUpButtonAction (NSObject sender)
{
switch (PopUpButton.SelectedItem.Title) {
case "Xamagon":
SkiaView.OnDrawCallback = Skia.Forms.Demo.DrawHelpers.DrawXamagon;
break;
case "Text":
SkiaView.OnDrawCallback = Skia.Forms.Demo.DrawHelpers.TextSample;
break;
case "Gradient":
SkiaView.OnDrawCallback = Skia.Forms.Demo.DrawHelpers.DrawGradient;
break;
}
SkiaView.OnDrawCallback = SkiaSharp.Demos.MethodForSample (PopUpButton.SelectedItem.Title);
}
public override NSObject RepresentedObject {