# HG changeset patch # User cin # Date 1379377650 -14400 # Node ID 381095ad0a6990c59e6371ff5a073f230602ce14 # Parent 1e9583086e99e025f68820763c3bc1014ff54d68 Implab.Fx: implemented animation object Implab.Fx: implemented transparency animation helper diff -r 1e9583086e99 -r 381095ad0a69 .hgignore --- a/.hgignore Fri Sep 13 12:54:28 2013 +0400 +++ b/.hgignore Tue Sep 17 04:27:30 2013 +0400 @@ -7,3 +7,4 @@ Implab/obj/ TestResults/ Implab.Fx/obj/ +Implab.Fx/bin/ diff -r 1e9583086e99 -r 381095ad0a69 Implab.Fx/Animation.cs --- a/Implab.Fx/Animation.cs Fri Sep 13 12:54:28 2013 +0400 +++ b/Implab.Fx/Animation.cs Tue Sep 17 04:27:30 2013 +0400 @@ -2,14 +2,96 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using System.Timers; +using System.ComponentModel; +using System.Diagnostics; namespace Implab.Fx { - public class Animation + public delegate void AnimationStep(T target, int elapsed, int duration); + + public class Animation where TArg: class { int m_duration; - int m_fps; + int m_delay; + int m_elapsed; + int m_prevTicks; + TArg m_arg; + ISynchronizeInvoke m_syncronizationObject; + + public event AnimationStep Step; + + Promise m_promise; + + public Animation(TArg target, int duration, int delay) + { + if (duration <= 0) + throw new ArgumentOutOfRangeException("duration"); + if (delay <= 0) + throw new ArgumentOutOfRangeException("delay"); + + m_arg = target; + m_syncronizationObject = target as ISynchronizeInvoke; + m_duration = duration; + m_delay = delay; + m_promise = new Promise(); + } + + public Animation(TArg target) + : this(target, 500, 30) + { + } + + public TArg Traget + { + get { return m_arg; } + } + public Promise Play() + { + var timer = new Timer(m_delay); + + timer.AutoReset = true; + timer.SynchronizingObject = m_syncronizationObject; + timer.Elapsed += new ElapsedEventHandler(timer_Elapsed); + m_prevTicks = Environment.TickCount; + + timer.Start(); + + return m_promise; + } + + void timer_Elapsed(object sender, ElapsedEventArgs args) + { + var timer = sender as Timer; + + var dt = Environment.TickCount - m_prevTicks; + m_prevTicks = Environment.TickCount; + + m_elapsed += dt; + + if (m_elapsed > m_duration) + m_elapsed = m_duration; + + try + { + var handler = Step; + if (handler != null) + handler(m_arg, m_elapsed, m_duration); + } + catch (Exception e) + { + Trace.TraceError(e.ToString()); + } + + if (m_elapsed < m_duration) + timer.Start(); + else + { + timer.Dispose(); + m_promise.Resolve(m_arg); + } + } } } diff -r 1e9583086e99 -r 381095ad0a69 Implab.Fx/AnimationHelpers.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Implab.Fx/AnimationHelpers.cs Tue Sep 17 04:27:30 2013 +0400 @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Windows.Forms; +using System.Diagnostics; + +namespace Implab.Fx +{ + public static class AnimationHelpers + { + public static Animation AnimateProperty(this Animation animation, Action setter, Func getter, TVal newValue, Func fx) where TTarget: class + { + if (animation == null) + throw new ArgumentNullException("animation"); + + TVal oldValue = getter(animation.Traget); + + animation.Step += (target, elaped, duration) => + { + var value = fx(oldValue, newValue, elaped, duration); + setter(target, value); + }; + + return animation; + } + + public static Animation
AnimateTransparency(this Form ctl, float newValue) + { + var anim = new Animation(ctl); + + anim.AnimateProperty( + (target, value) => target.Opacity = value, + target => target.Opacity, + newValue, + (ov, nv, el, du) => ov + ((float)el / du) * (nv - ov) + ); + return anim; + } + } +} diff -r 1e9583086e99 -r 381095ad0a69 Implab.Fx/ControlHelpers.cs --- a/Implab.Fx/ControlHelpers.cs Fri Sep 13 12:54:28 2013 +0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,56 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Windows.Forms; - -namespace Implab.Fx -{ - public static class ControlHelpers - { - /// - /// Переключает обработку обещания в поток указанного элемента управления. - /// - /// Тип результата обещания - /// Исходное обещание - /// Элемент управления - /// Новое обещание, обработчики которого будут выполнены в потоке элемента управления. - /// Параметр не может быть null. - /// - /// client - /// .Get("description.txt") // returns a promise - /// .DirectToControl(m_ctl) // handle the promise in the thread of the control - /// .Then( - /// description => m_ctl.Text = description // now it's safe - /// ) - /// - public static Promise DirectToControl(this Promise that, Control ctl) - { - if (that == null) - throw new ArgumentNullException("that"); - if (ctl == null) - throw new ArgumentNullException("ctl"); - - var directed = new Promise(); - - that.Then( - res => - { - if (ctl.InvokeRequired) - ctl.Invoke(new Action(directed.Resolve),res); - else - directed.Resolve(res); - }, - err => - { - if (ctl.InvokeRequired) - ctl.Invoke(new Action(directed.Reject), err); - else - directed.Reject(err); - } - ); - - return directed; - } - } -} diff -r 1e9583086e99 -r 381095ad0a69 Implab.Fx/Implab.Fx.csproj --- a/Implab.Fx/Implab.Fx.csproj Fri Sep 13 12:54:28 2013 +0400 +++ b/Implab.Fx/Implab.Fx.csproj Tue Sep 17 04:27:30 2013 +0400 @@ -41,7 +41,9 @@ - + + + diff -r 1e9583086e99 -r 381095ad0a69 Implab.Fx/PromiseHelpers.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Implab.Fx/PromiseHelpers.cs Tue Sep 17 04:27:30 2013 +0400 @@ -0,0 +1,95 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Windows.Forms; +using System.Threading; + +namespace Implab.Fx +{ + public static class PromiseHelpers + { + /// + /// Перенаправляет обработку обещания в поток указанного элемента управления. + /// + /// Тип результата обещания + /// Исходное обещание + /// Элемент управления + /// Новое обещание, обработчики которого будут выполнены в потоке элемента управления. + /// Параметр не может быть null. + /// + /// client + /// .Get("description.txt") // returns a promise + /// .DirectToControl(m_ctl) // handle the promise in the thread of the control + /// .Then( + /// description => m_ctl.Text = description // now it's safe + /// ) + /// + public static Promise DispatchToControl(this Promise that, Control ctl) + { + if (that == null) + throw new ArgumentNullException("that"); + if (ctl == null) + throw new ArgumentNullException("ctl"); + + var directed = new Promise(); + + that.Then( + res => + { + if (ctl.InvokeRequired) + ctl.Invoke(new Action(directed.Resolve), res); + else + directed.Resolve(res); + }, + err => + { + if (ctl.InvokeRequired) + ctl.Invoke(new Action(directed.Reject), err); + else + directed.Reject(err); + } + ); + + return directed; + } + + /// + /// Направляет обработку обещания в текущий поток, если у него существует контекст синхронизации. + /// + /// Тип результата обещания. + /// Обещание которое нужно обработать в текущем потоке. + /// Перенаправленное обещание. + public static Promise DispatchToCurrentThread(this Promise that) + { + var sync = SynchronizationContext.Current; + if (sync == null) + throw new InvalidOperationException("The current thread doesn't have a syncronization context"); + return DispatchToSyncContext(that, sync); + } + + /// + /// Направляет обработку обещания в указанный контекст синхронизации. + /// + /// Тип результата обещания. + /// Обещание, которое требуется обработать в указанном контексте синхронизации. + /// Контекст синхронизации в который будет направлено обещание. + /// Новое обещание, которое будет обрабатываться в указанном контексте. + public static Promise DispatchToSyncContext(this Promise that, SynchronizationContext sync) + { + if (that == null) + throw new ArgumentNullException("that"); + if (sync == null) + throw new ArgumentNullException("sync"); + + var d = new Promise(); + + that.Then( + res => sync.Post(state => d.Resolve(res), null), + err => sync.Post(state => d.Reject(err), null) + ); + + return d; + } + } +} diff -r 1e9583086e99 -r 381095ad0a69 Implab.Test/AsyncTests.cs --- a/Implab.Test/AsyncTests.cs Fri Sep 13 12:54:28 2013 +0400 +++ b/Implab.Test/AsyncTests.cs Tue Sep 17 04:27:30 2013 +0400 @@ -1,101 +1,101 @@ -using System; -using Microsoft.VisualStudio.TestTools.UnitTesting; -using Implab; -using System.Reflection; -using System.Threading; - -namespace Implab.Tests -{ - [TestClass] - public class AsyncTests - { - [TestMethod] - public void ResolveTest () - { - int res = -1; - var p = new Promise (); - p.Then (x => res = x); - p.Resolve (100); - - Assert.AreEqual (res, 100); +using System; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Implab; +using System.Reflection; +using System.Threading; + +namespace Implab.Tests +{ + [TestClass] + public class AsyncTests + { + [TestMethod] + public void ResolveTest () + { + int res = -1; + var p = new Promise (); + p.Then (x => res = x); + p.Resolve (100); + + Assert.AreEqual (res, 100); } - [TestMethod] - public void RejectTest () - { - int res = -1; - Exception err = null; - - var p = new Promise (); - p.Then (x => res = x, e => err = e); - p.Reject (new ApplicationException ("error")); - - Assert.AreEqual (res, -1); - Assert.AreEqual (err.Message, "error"); - + [TestMethod] + public void RejectTest () + { + int res = -1; + Exception err = null; + + var p = new Promise (); + p.Then (x => res = x, e => err = e); + p.Reject (new ApplicationException ("error")); + + Assert.AreEqual (res, -1); + Assert.AreEqual (err.Message, "error"); + } - [TestMethod] - public void JoinSuccessTest () - { - var p = new Promise (); - p.Resolve (100); - Assert.AreEqual (p.Join (), 100); + [TestMethod] + public void JoinSuccessTest () + { + var p = new Promise (); + p.Resolve (100); + Assert.AreEqual (p.Join (), 100); } - [TestMethod] - public void JoinFailTest () - { - var p = new Promise (); - p.Reject (new ApplicationException ("failed")); - - try { - p.Join (); - throw new ApplicationException ("WRONG!"); - } catch (TargetInvocationException err) { - Assert.AreEqual (err.InnerException.Message, "failed"); - } catch { - Assert.Fail ("Got wrong excaption"); - } + [TestMethod] + public void JoinFailTest () + { + var p = new Promise (); + p.Reject (new ApplicationException ("failed")); + + try { + p.Join (); + throw new ApplicationException ("WRONG!"); + } catch (TargetInvocationException err) { + Assert.AreEqual (err.InnerException.Message, "failed"); + } catch { + Assert.Fail ("Got wrong excaption"); + } } - [TestMethod] - public void MapTest () - { - var p = new Promise (); - - var p2 = p.Map (x => x.ToString ()); - p.Resolve (100); - - Assert.AreEqual (p2.Join (), "100"); + [TestMethod] + public void MapTest () + { + var p = new Promise (); + + var p2 = p.Map (x => x.ToString ()); + p.Resolve (100); + + Assert.AreEqual (p2.Join (), "100"); } - [TestMethod] - public void ChainTest () - { - var p1 = new Promise (); - - var p3 = p1.Chain (x => { - var p2 = new Promise (); - p2.Resolve (x.ToString ()); - return p2; - }); - - p1.Resolve (100); - - Assert.AreEqual (p3.Join (), "100"); + [TestMethod] + public void ChainTest () + { + var p1 = new Promise (); + + var p3 = p1.Chain (x => { + var p2 = new Promise (); + p2.Resolve (x.ToString ()); + return p2; + }); + + p1.Resolve (100); + + Assert.AreEqual (p3.Join (), "100"); } - [TestMethod] - public void PoolTest () - { - var pid = Thread.CurrentThread.ManagedThreadId; - var p = AsyncPool.Invoke (() => { - return Thread.CurrentThread.ManagedThreadId; - }); - - Assert.AreNotEqual (pid, p.Join ()); - } - } -} - + [TestMethod] + public void PoolTest () + { + var pid = Thread.CurrentThread.ManagedThreadId; + var p = AsyncPool.Invoke (() => { + return Thread.CurrentThread.ManagedThreadId; + }); + + Assert.AreNotEqual (pid, p.Join ()); + } + } +} + diff -r 1e9583086e99 -r 381095ad0a69 Implab.sln --- a/Implab.sln Fri Sep 13 12:54:28 2013 +0400 +++ b/Implab.sln Tue Sep 17 04:27:30 2013 +0400 @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 11.00 # Visual Studio 2010 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Implab", "Implab\Implab.csproj", "{99B95D0D-9CF9-4F70-8ADF-F4D0AA5CB0D9}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Implab", "Implab\Implab.csproj", "{F550F1F8-8746-4AD0-9614-855F4C4B7F05}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{CE8D8D18-437A-445C-B662-4C2CE79A76F6}" ProjectSection(SolutionItems) = preProject @@ -23,10 +23,10 @@ Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {99B95D0D-9CF9-4F70-8ADF-F4D0AA5CB0D9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {99B95D0D-9CF9-4F70-8ADF-F4D0AA5CB0D9}.Debug|Any CPU.Build.0 = Debug|Any CPU - {99B95D0D-9CF9-4F70-8ADF-F4D0AA5CB0D9}.Release|Any CPU.ActiveCfg = Release|Any CPU - {99B95D0D-9CF9-4F70-8ADF-F4D0AA5CB0D9}.Release|Any CPU.Build.0 = Release|Any CPU + {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Release|Any CPU.Build.0 = Release|Any CPU {63F92C0C-61BF-48C0-A377-8D67C3C661D0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {63F92C0C-61BF-48C0-A377-8D67C3C661D0}.Debug|Any CPU.Build.0 = Debug|Any CPU {63F92C0C-61BF-48C0-A377-8D67C3C661D0}.Release|Any CPU.ActiveCfg = Release|Any CPU diff -r 1e9583086e99 -r 381095ad0a69 Implab.suo Binary file Implab.suo has changed diff -r 1e9583086e99 -r 381095ad0a69 Implab/Implab.csproj --- a/Implab/Implab.csproj Fri Sep 13 12:54:28 2013 +0400 +++ b/Implab/Implab.csproj Tue Sep 17 04:27:30 2013 +0400 @@ -5,7 +5,7 @@ AnyCPU 10.0.0 2.0 - {99B95D0D-9CF9-4F70-8ADF-F4D0AA5CB0D9} + {F550F1F8-8746-4AD0-9614-855F4C4B7F05} Library Implab Implab