myBlog

IronPython, Silverlight, WPF, XAML, HTML5, ...

IronPythonで、動的にアニメーションの動作を変える!

2011-07-22 23:57:47 | Animation
IronPythonで、Storyboard.Begin メソッド の HandoffBehavior を使って 動的にアニメーションを変化させます。
MSDNを調べていると、面白いサンプルがあったので、IronPython に変換して実行してみました。
http://207.46.16.248/ja-jp/library/ms605722(VS.90).aspx

ユーザーがクリックしたときに SnapshotAndReplace の HandoffBehavior を使用してアニメーション化し、
ユーザーが右クリックしたときには Compose の HandoffBehavior を使用する例を次に示します。
微妙に動作に違いがあります。

Interactiveanimationexample

#
# InteractiveAnimationExample.py
#   This sample animates the position of an ellipse when 
#   the user clicks within the main border. If the user
#   left-clicks, the SnapshotAndReplace HandoffBehavior
#   is used when applying the animations. If the user
#   right-clicks, the Compose HandoffBehavior is used
#   instead.
#
import clr
clr.AddReferenceByPartialName("PresentationFramework")
clr.AddReferenceByPartialName("PresentationCore")
clr.AddReference('WindowsBase')  # for Point
from System import TimeSpan
from System.Windows import( Window, Application, NameScope, Thickness, Point,
        PropertyPath, HorizontalAlignment, VerticalAlignment )
from System.Windows.Controls import DockPanel, Border
from System.Windows.Media import Brushes, TranslateTransform
from System.Windows.Media.Animation import Storyboard, DoubleAnimation, HandoffBehavior
from System.Windows.Shapes import Ellipse
from System.Windows.Input import Mouse

class ExWindow(Window):
    def __init__(self):
        self.Title = "InteractiveAnimationExample.py"
        self.Width = 600
        self.Height = 400
        #// Create a name scope for the page.
        NameScope.SetNameScope(self, NameScope())

        myPanel = DockPanel()
        myPanel.Margin = Thickness(20.0)          

        containerBorder = Border();
        containerBorder.Background = Brushes.White
        containerBorder.BorderBrush = Brushes.Black
        containerBorder.BorderThickness = Thickness(2.0)
        containerBorder.VerticalAlignment = VerticalAlignment.Stretch

        interactiveEllipse = Ellipse()
        interactiveEllipse.Fill = Brushes.Lime
        interactiveEllipse.Stroke = Brushes.Black
        interactiveEllipse.StrokeThickness = 2.0
        interactiveEllipse.Width = 25
        interactiveEllipse.Height = 25
        interactiveEllipse.HorizontalAlignment = HorizontalAlignment.Left
        interactiveEllipse.VerticalAlignment = VerticalAlignment.Top

        interactiveTranslateTransform = TranslateTransform()      
        self.RegisterName("InteractiveTranslateTransform", interactiveTranslateTransform)

        interactiveEllipse.RenderTransform = interactiveTranslateTransform

        xAnimation = DoubleAnimation()
        xAnimation.Duration = TimeSpan.FromSeconds(4)
        yAnimation = xAnimation.Clone()
        Storyboard.SetTargetName(xAnimation, "InteractiveTranslateTransform")
        Storyboard.SetTargetProperty(xAnimation, PropertyPath(TranslateTransform.XProperty))
        Storyboard.SetTargetName(yAnimation, "InteractiveTranslateTransform")
        Storyboard.SetTargetProperty(yAnimation, PropertyPath(TranslateTransform.YProperty))           

        theStoryboard = Storyboard()
        theStoryboard.Children.Add(xAnimation)
        theStoryboard.Children.Add(yAnimation)

        containerBorder.MouseLeftButtonDown += self.border_mouseLeftButtonDown
        containerBorder.MouseRightButtonDown += self.border_mouseRightButtonDown             

        containerBorder.Child = interactiveEllipse
        myPanel.Children.Add(containerBorder)
        self.Content = myPanel

        self.containerBorder = containerBorder
        self.interactiveEllipse = interactiveEllipse
        self.theStoryboard = theStoryboard
        self.xAnimation = xAnimation
        self.yAnimation = yAnimation

    #// When the user left-clicks, use the 
    #// SnapshotAndReplace HandoffBehavior when applying the animation.        
    def border_mouseLeftButtonDown(self, sender, e):
        clickPoint = Mouse.GetPosition(self.containerBorder)

        #// Set the target point so the center of the ellipse
        #// ends up at the clicked point.
        targetPoint = Point()
        targetPoint.X = clickPoint.X - self.interactiveEllipse.Width / 2
        targetPoint.Y = clickPoint.Y - self.interactiveEllipse.Height / 2  

        #// Animate to the target point.
        self.xAnimation.To = targetPoint.X
        self.yAnimation.To = targetPoint.Y
        self.theStoryboard.Begin(self, HandoffBehavior.SnapshotAndReplace)

        #// Change the color of the ellipse.
        self.interactiveEllipse.Fill = Brushes.Lime

    #// When the user right-clicks, use the 
    #// Compose HandoffBehavior when applying the animation.
    def border_mouseRightButtonDown(self, sender, e):
        #// Find the point where the use clicked.
        clickPoint = Mouse.GetPosition(self.containerBorder)

        #// Set the target point so the center of the ellipse
        #// ends up at the clicked point.
        targetPoint = Point()
        targetPoint.X = clickPoint.X - self.interactiveEllipse.Width / 2
        targetPoint.Y = clickPoint.Y - self.interactiveEllipse.Height / 2

        #// Animate to the target point.
        self.xAnimation.To = targetPoint.X
        self.yAnimation.To = targetPoint.Y
        self.theStoryboard.Begin(self, HandoffBehavior.Compose)

        #// Change the color of the ellipse.
        self.interactiveEllipse.Fill = Brushes.Orange

if __name__ == "__main__":
    win = ExWindow()
    Application().Run(win)

IronPythonの世界 (Windows Script Programming)
荒井 省三
ソフトバンク クリエイティブ
エキスパートPythonプログラミング
Tarek Ziade
アスキー・メディアワークス
Pythonスタートブック
辻 真吾
技術評論社

最新の画像もっと見る

コメントを投稿