##// END OF EJS Templates
Implab.Fx: implemented animation object...
cin -
r4:381095ad0a69 default
parent child
Show More
@@ -0,0 +1,41
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Windows.Forms;
6 using System.Diagnostics;
7
8 namespace Implab.Fx
9 {
10 public static class AnimationHelpers
11 {
12 public static Animation<TTarget> AnimateProperty<TTarget,TVal>(this Animation<TTarget> animation, Action<TTarget,TVal> setter, Func<TTarget,TVal> getter, TVal newValue, Func<TVal,TVal,int,int,TVal> fx) where TTarget: class
13 {
14 if (animation == null)
15 throw new ArgumentNullException("animation");
16
17 TVal oldValue = getter(animation.Traget);
18
19 animation.Step += (target, elaped, duration) =>
20 {
21 var value = fx(oldValue, newValue, elaped, duration);
22 setter(target, value);
23 };
24
25 return animation;
26 }
27
28 public static Animation<Form> AnimateTransparency(this Form ctl, float newValue)
29 {
30 var anim = new Animation<Form>(ctl);
31
32 anim.AnimateProperty(
33 (target, value) => target.Opacity = value,
34 target => target.Opacity,
35 newValue,
36 (ov, nv, el, du) => ov + ((float)el / du) * (nv - ov)
37 );
38 return anim;
39 }
40 }
41 }
@@ -0,0 +1,95
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Windows.Forms;
6 using System.Threading;
7
8 namespace Implab.Fx
9 {
10 public static class PromiseHelpers
11 {
12 /// <summary>
13 /// Перенаправляет обработку обещания в поток указанного элемента управления.
14 /// </summary>
15 /// <typeparam name="T">Тип результата обещания</typeparam>
16 /// <param name="that">Исходное обещание</param>
17 /// <param name="ctl">Элемент управления</param>
18 /// <returns>Новое обещание, обработчики которого будут выполнены в потоке элемента управления.</returns>
19 /// <exception cref="ArgumentNullException">Параметр не может быть <c>null</c>.</exception>
20 /// <example>
21 /// client
22 /// .Get("description.txt") // returns a promise
23 /// .DirectToControl(m_ctl) // handle the promise in the thread of the control
24 /// .Then(
25 /// description => m_ctl.Text = description // now it's safe
26 /// )
27 /// </example>
28 public static Promise<T> DispatchToControl<T>(this Promise<T> that, Control ctl)
29 {
30 if (that == null)
31 throw new ArgumentNullException("that");
32 if (ctl == null)
33 throw new ArgumentNullException("ctl");
34
35 var directed = new Promise<T>();
36
37 that.Then(
38 res =>
39 {
40 if (ctl.InvokeRequired)
41 ctl.Invoke(new Action<T>(directed.Resolve), res);
42 else
43 directed.Resolve(res);
44 },
45 err =>
46 {
47 if (ctl.InvokeRequired)
48 ctl.Invoke(new Action<Exception>(directed.Reject), err);
49 else
50 directed.Reject(err);
51 }
52 );
53
54 return directed;
55 }
56
57 /// <summary>
58 /// Направляет обработку обещания в текущий поток, если у него существует контекст синхронизации.
59 /// </summary>
60 /// <typeparam name="T">Тип результата обещания.</typeparam>
61 /// <param name="that">Обещание которое нужно обработать в текущем потоке.</param>
62 /// <returns>Перенаправленное обещание.</returns>
63 public static Promise<T> DispatchToCurrentThread<T>(this Promise<T> that)
64 {
65 var sync = SynchronizationContext.Current;
66 if (sync == null)
67 throw new InvalidOperationException("The current thread doesn't have a syncronization context");
68 return DispatchToSyncContext(that, sync);
69 }
70
71 /// <summary>
72 /// Направляет обработку обещания в указанный контекст синхронизации.
73 /// </summary>
74 /// <typeparam name="T">Тип результата обещания.</typeparam>
75 /// <param name="that">Обещание, которое требуется обработать в указанном контексте синхронизации.</param>
76 /// <param name="sync">Контекст синхронизации в который будет направлено обещание.</param>
77 /// <returns>Новое обещание, которое будет обрабатываться в указанном контексте.</returns>
78 public static Promise<T> DispatchToSyncContext<T>(this Promise<T> that, SynchronizationContext sync)
79 {
80 if (that == null)
81 throw new ArgumentNullException("that");
82 if (sync == null)
83 throw new ArgumentNullException("sync");
84
85 var d = new Promise<T>();
86
87 that.Then(
88 res => sync.Post(state => d.Resolve(res), null),
89 err => sync.Post(state => d.Reject(err), null)
90 );
91
92 return d;
93 }
94 }
95 }
@@ -1,9 +1,10
1 1 syntax: glob
2 2 Implab.Test/bin/
3 3 *.user
4 4 Implab.Test/obj/
5 5 *.userprefs
6 6 Implab/bin/
7 7 Implab/obj/
8 8 TestResults/
9 9 Implab.Fx/obj/
10 Implab.Fx/bin/
@@ -1,15 +1,97
1 1 using System;
2 2 using System.Collections.Generic;
3 3 using System.Linq;
4 4 using System.Text;
5 using System.Timers;
6 using System.ComponentModel;
7 using System.Diagnostics;
5 8
6 9 namespace Implab.Fx
7 10 {
8 public class Animation
11 public delegate void AnimationStep<T>(T target, int elapsed, int duration);
12
13 public class Animation<TArg> where TArg: class
9 14 {
10 15 int m_duration;
11 int m_fps;
16 int m_delay;
17 int m_elapsed;
18 int m_prevTicks;
19 TArg m_arg;
20 ISynchronizeInvoke m_syncronizationObject;
21
22 public event AnimationStep<TArg> Step;
23
24 Promise<TArg> m_promise;
25
26 public Animation(TArg target, int duration, int delay)
27 {
28 if (duration <= 0)
29 throw new ArgumentOutOfRangeException("duration");
30 if (delay <= 0)
31 throw new ArgumentOutOfRangeException("delay");
32
33 m_arg = target;
34 m_syncronizationObject = target as ISynchronizeInvoke;
35 m_duration = duration;
36 m_delay = delay;
37 m_promise = new Promise<TArg>();
38 }
39
40 public Animation(TArg target)
41 : this(target, 500, 30)
42 {
43 }
44
45 public TArg Traget
46 {
47 get { return m_arg; }
48 }
12 49
50 public Promise<TArg> Play()
51 {
52 var timer = new Timer(m_delay);
53
54 timer.AutoReset = true;
55 timer.SynchronizingObject = m_syncronizationObject;
56 timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
13 57
58 m_prevTicks = Environment.TickCount;
59
60 timer.Start();
61
62 return m_promise;
63 }
64
65 void timer_Elapsed(object sender, ElapsedEventArgs args)
66 {
67 var timer = sender as Timer;
68
69 var dt = Environment.TickCount - m_prevTicks;
70 m_prevTicks = Environment.TickCount;
71
72 m_elapsed += dt;
73
74 if (m_elapsed > m_duration)
75 m_elapsed = m_duration;
76
77 try
78 {
79 var handler = Step;
80 if (handler != null)
81 handler(m_arg, m_elapsed, m_duration);
82 }
83 catch (Exception e)
84 {
85 Trace.TraceError(e.ToString());
86 }
87
88 if (m_elapsed < m_duration)
89 timer.Start();
90 else
91 {
92 timer.Dispose();
93 m_promise.Resolve(m_arg);
94 }
95 }
14 96 }
15 97 }
@@ -1,61 +1,63
1 1 <?xml version="1.0" encoding="utf-8"?>
2 2 <Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3 3 <PropertyGroup>
4 4 <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5 5 <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6 6 <ProductVersion>8.0.30703</ProductVersion>
7 7 <SchemaVersion>2.0</SchemaVersion>
8 8 <ProjectGuid>{06E706F8-6881-43EB-927E-FFC503AF6ABC}</ProjectGuid>
9 9 <OutputType>Library</OutputType>
10 10 <AppDesignerFolder>Properties</AppDesignerFolder>
11 11 <RootNamespace>Implab.Fx</RootNamespace>
12 12 <AssemblyName>Implab.Fx</AssemblyName>
13 13 <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
14 14 <FileAlignment>512</FileAlignment>
15 15 </PropertyGroup>
16 16 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17 17 <DebugSymbols>true</DebugSymbols>
18 18 <DebugType>full</DebugType>
19 19 <Optimize>false</Optimize>
20 20 <OutputPath>bin\Debug\</OutputPath>
21 21 <DefineConstants>DEBUG;TRACE</DefineConstants>
22 22 <ErrorReport>prompt</ErrorReport>
23 23 <WarningLevel>4</WarningLevel>
24 24 </PropertyGroup>
25 25 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26 26 <DebugType>pdbonly</DebugType>
27 27 <Optimize>true</Optimize>
28 28 <OutputPath>bin\Release\</OutputPath>
29 29 <DefineConstants>TRACE</DefineConstants>
30 30 <ErrorReport>prompt</ErrorReport>
31 31 <WarningLevel>4</WarningLevel>
32 32 </PropertyGroup>
33 33 <ItemGroup>
34 34 <Reference Include="System" />
35 35 <Reference Include="System.Core" />
36 36 <Reference Include="System.Windows.Forms" />
37 37 <Reference Include="System.Xml.Linq" />
38 38 <Reference Include="System.Data.DataSetExtensions" />
39 39 <Reference Include="Microsoft.CSharp" />
40 40 <Reference Include="System.Data" />
41 41 <Reference Include="System.Xml" />
42 42 </ItemGroup>
43 43 <ItemGroup>
44 <Compile Include="ControlHelpers.cs" />
44 <Compile Include="Animation.cs" />
45 <Compile Include="AnimationHelpers.cs" />
46 <Compile Include="PromiseHelpers.cs" />
45 47 <Compile Include="Properties\AssemblyInfo.cs" />
46 48 </ItemGroup>
47 49 <ItemGroup>
48 50 <ProjectReference Include="..\Implab\Implab.csproj">
49 51 <Project>{99B95D0D-9CF9-4F70-8ADF-F4D0AA5CB0D9}</Project>
50 52 <Name>Implab</Name>
51 53 </ProjectReference>
52 54 </ItemGroup>
53 55 <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
54 56 <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
55 57 Other similar extension points exist, see Microsoft.Common.targets.
56 58 <Target Name="BeforeBuild">
57 59 </Target>
58 60 <Target Name="AfterBuild">
59 61 </Target>
60 62 -->
61 63 </Project> No newline at end of file
@@ -1,101 +1,101
1 using System;
2 using Microsoft.VisualStudio.TestTools.UnitTesting;
3 using Implab;
4 using System.Reflection;
5 using System.Threading;
6
7 namespace Implab.Tests
8 {
9 [TestClass]
10 public class AsyncTests
11 {
12 [TestMethod]
13 public void ResolveTest ()
14 {
15 int res = -1;
16 var p = new Promise<int> ();
17 p.Then (x => res = x);
18 p.Resolve (100);
19
20 Assert.AreEqual (res, 100);
1 using System;
2 using Microsoft.VisualStudio.TestTools.UnitTesting;
3 using Implab;
4 using System.Reflection;
5 using System.Threading;
6
7 namespace Implab.Tests
8 {
9 [TestClass]
10 public class AsyncTests
11 {
12 [TestMethod]
13 public void ResolveTest ()
14 {
15 int res = -1;
16 var p = new Promise<int> ();
17 p.Then (x => res = x);
18 p.Resolve (100);
19
20 Assert.AreEqual (res, 100);
21 21 }
22 22
23 [TestMethod]
24 public void RejectTest ()
25 {
26 int res = -1;
27 Exception err = null;
28
29 var p = new Promise<int> ();
30 p.Then (x => res = x, e => err = e);
31 p.Reject (new ApplicationException ("error"));
32
33 Assert.AreEqual (res, -1);
34 Assert.AreEqual (err.Message, "error");
35
23 [TestMethod]
24 public void RejectTest ()
25 {
26 int res = -1;
27 Exception err = null;
28
29 var p = new Promise<int> ();
30 p.Then (x => res = x, e => err = e);
31 p.Reject (new ApplicationException ("error"));
32
33 Assert.AreEqual (res, -1);
34 Assert.AreEqual (err.Message, "error");
35
36 36 }
37 37
38 [TestMethod]
39 public void JoinSuccessTest ()
40 {
41 var p = new Promise<int> ();
42 p.Resolve (100);
43 Assert.AreEqual (p.Join (), 100);
38 [TestMethod]
39 public void JoinSuccessTest ()
40 {
41 var p = new Promise<int> ();
42 p.Resolve (100);
43 Assert.AreEqual (p.Join (), 100);
44 44 }
45 45
46 [TestMethod]
47 public void JoinFailTest ()
48 {
49 var p = new Promise<int> ();
50 p.Reject (new ApplicationException ("failed"));
51
52 try {
53 p.Join ();
54 throw new ApplicationException ("WRONG!");
55 } catch (TargetInvocationException err) {
56 Assert.AreEqual (err.InnerException.Message, "failed");
57 } catch {
58 Assert.Fail ("Got wrong excaption");
59 }
46 [TestMethod]
47 public void JoinFailTest ()
48 {
49 var p = new Promise<int> ();
50 p.Reject (new ApplicationException ("failed"));
51
52 try {
53 p.Join ();
54 throw new ApplicationException ("WRONG!");
55 } catch (TargetInvocationException err) {
56 Assert.AreEqual (err.InnerException.Message, "failed");
57 } catch {
58 Assert.Fail ("Got wrong excaption");
59 }
60 60 }
61 61
62 [TestMethod]
63 public void MapTest ()
64 {
65 var p = new Promise<int> ();
66
67 var p2 = p.Map (x => x.ToString ());
68 p.Resolve (100);
69
70 Assert.AreEqual (p2.Join (), "100");
62 [TestMethod]
63 public void MapTest ()
64 {
65 var p = new Promise<int> ();
66
67 var p2 = p.Map (x => x.ToString ());
68 p.Resolve (100);
69
70 Assert.AreEqual (p2.Join (), "100");
71 71 }
72 72
73 [TestMethod]
74 public void ChainTest ()
75 {
76 var p1 = new Promise<int> ();
77
78 var p3 = p1.Chain (x => {
79 var p2 = new Promise<string> ();
80 p2.Resolve (x.ToString ());
81 return p2;
82 });
83
84 p1.Resolve (100);
85
86 Assert.AreEqual (p3.Join (), "100");
73 [TestMethod]
74 public void ChainTest ()
75 {
76 var p1 = new Promise<int> ();
77
78 var p3 = p1.Chain (x => {
79 var p2 = new Promise<string> ();
80 p2.Resolve (x.ToString ());
81 return p2;
82 });
83
84 p1.Resolve (100);
85
86 Assert.AreEqual (p3.Join (), "100");
87 87 }
88 88
89 [TestMethod]
90 public void PoolTest ()
91 {
92 var pid = Thread.CurrentThread.ManagedThreadId;
93 var p = AsyncPool.Invoke (() => {
94 return Thread.CurrentThread.ManagedThreadId;
95 });
96
97 Assert.AreNotEqual (pid, p.Join ());
98 }
99 }
100 }
101
89 [TestMethod]
90 public void PoolTest ()
91 {
92 var pid = Thread.CurrentThread.ManagedThreadId;
93 var p = AsyncPool.Invoke (() => {
94 return Thread.CurrentThread.ManagedThreadId;
95 });
96
97 Assert.AreNotEqual (pid, p.Join ());
98 }
99 }
100 }
101
@@ -1,45 +1,45
1 1 
2 2 Microsoft Visual Studio Solution File, Format Version 11.00
3 3 # Visual Studio 2010
4 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Implab", "Implab\Implab.csproj", "{99B95D0D-9CF9-4F70-8ADF-F4D0AA5CB0D9}"
4 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Implab", "Implab\Implab.csproj", "{F550F1F8-8746-4AD0-9614-855F4C4B7F05}"
5 5 EndProject
6 6 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{CE8D8D18-437A-445C-B662-4C2CE79A76F6}"
7 7 ProjectSection(SolutionItems) = preProject
8 8 Implab.vsmdi = Implab.vsmdi
9 9 Local.testsettings = Local.testsettings
10 10 TraceAndTestImpact.testsettings = TraceAndTestImpact.testsettings
11 11 EndProjectSection
12 12 EndProject
13 13 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Implab.Test", "Implab.Test\Implab.Test.csproj", "{63F92C0C-61BF-48C0-A377-8D67C3C661D0}"
14 14 EndProject
15 15 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Implab.Fx", "Implab.Fx\Implab.Fx.csproj", "{06E706F8-6881-43EB-927E-FFC503AF6ABC}"
16 16 EndProject
17 17 Global
18 18 GlobalSection(TestCaseManagementSettings) = postSolution
19 19 CategoryFile = Implab.vsmdi
20 20 EndGlobalSection
21 21 GlobalSection(SolutionConfigurationPlatforms) = preSolution
22 22 Debug|Any CPU = Debug|Any CPU
23 23 Release|Any CPU = Release|Any CPU
24 24 EndGlobalSection
25 25 GlobalSection(ProjectConfigurationPlatforms) = postSolution
26 {99B95D0D-9CF9-4F70-8ADF-F4D0AA5CB0D9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27 {99B95D0D-9CF9-4F70-8ADF-F4D0AA5CB0D9}.Debug|Any CPU.Build.0 = Debug|Any CPU
28 {99B95D0D-9CF9-4F70-8ADF-F4D0AA5CB0D9}.Release|Any CPU.ActiveCfg = Release|Any CPU
29 {99B95D0D-9CF9-4F70-8ADF-F4D0AA5CB0D9}.Release|Any CPU.Build.0 = Release|Any CPU
26 {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27 {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Debug|Any CPU.Build.0 = Debug|Any CPU
28 {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Release|Any CPU.ActiveCfg = Release|Any CPU
29 {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Release|Any CPU.Build.0 = Release|Any CPU
30 30 {63F92C0C-61BF-48C0-A377-8D67C3C661D0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
31 31 {63F92C0C-61BF-48C0-A377-8D67C3C661D0}.Debug|Any CPU.Build.0 = Debug|Any CPU
32 32 {63F92C0C-61BF-48C0-A377-8D67C3C661D0}.Release|Any CPU.ActiveCfg = Release|Any CPU
33 33 {63F92C0C-61BF-48C0-A377-8D67C3C661D0}.Release|Any CPU.Build.0 = Release|Any CPU
34 34 {06E706F8-6881-43EB-927E-FFC503AF6ABC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
35 35 {06E706F8-6881-43EB-927E-FFC503AF6ABC}.Debug|Any CPU.Build.0 = Debug|Any CPU
36 36 {06E706F8-6881-43EB-927E-FFC503AF6ABC}.Release|Any CPU.ActiveCfg = Release|Any CPU
37 37 {06E706F8-6881-43EB-927E-FFC503AF6ABC}.Release|Any CPU.Build.0 = Release|Any CPU
38 38 EndGlobalSection
39 39 GlobalSection(SolutionProperties) = preSolution
40 40 HideSolutionNode = FALSE
41 41 EndGlobalSection
42 42 GlobalSection(MonoDevelopProperties) = preSolution
43 43 StartupItem = Implab\Implab.csproj
44 44 EndGlobalSection
45 45 EndGlobal
1 NO CONTENT: modified file, binary diff hidden
@@ -1,44 +1,44
1 1 <?xml version="1.0" encoding="utf-8"?>
2 2 <Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3 3 <PropertyGroup>
4 4 <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5 5 <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6 6 <ProductVersion>10.0.0</ProductVersion>
7 7 <SchemaVersion>2.0</SchemaVersion>
8 <ProjectGuid>{99B95D0D-9CF9-4F70-8ADF-F4D0AA5CB0D9}</ProjectGuid>
8 <ProjectGuid>{F550F1F8-8746-4AD0-9614-855F4C4B7F05}</ProjectGuid>
9 9 <OutputType>Library</OutputType>
10 10 <RootNamespace>Implab</RootNamespace>
11 11 <AssemblyName>Implab</AssemblyName>
12 12 </PropertyGroup>
13 13 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
14 14 <DebugSymbols>true</DebugSymbols>
15 15 <DebugType>full</DebugType>
16 16 <Optimize>false</Optimize>
17 17 <OutputPath>bin\Debug</OutputPath>
18 18 <DefineConstants>DEBUG;</DefineConstants>
19 19 <ErrorReport>prompt</ErrorReport>
20 20 <WarningLevel>4</WarningLevel>
21 21 <ConsolePause>false</ConsolePause>
22 22 </PropertyGroup>
23 23 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
24 24 <DebugType>full</DebugType>
25 25 <Optimize>true</Optimize>
26 26 <OutputPath>bin\Release</OutputPath>
27 27 <ErrorReport>prompt</ErrorReport>
28 28 <WarningLevel>4</WarningLevel>
29 29 <ConsolePause>false</ConsolePause>
30 30 </PropertyGroup>
31 31 <ItemGroup>
32 32 <Reference Include="System" />
33 33 </ItemGroup>
34 34 <ItemGroup>
35 35 <Compile Include="Properties\AssemblyInfo.cs" />
36 36 <Compile Include="Promise.cs" />
37 37 <Compile Include="AsyncPool.cs" />
38 38 <Compile Include="Safe.cs" />
39 39 </ItemGroup>
40 40 <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
41 41 <ItemGroup>
42 42 <Folder Include="Parallels\" />
43 43 </ItemGroup>
44 44 </Project> No newline at end of file
1 NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now