myBlog

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

IronPythonで、MouseEnterで図形が動きだすAnimation

2011-07-07 12:30:13 | Animation
IronPythonで、MouseEnterで図形が動きだすAnimationを紹介します。
Googleで検索していると、おもしろいアニメーションXAMLを見つけました。
Blog: "++C++;// 未確認飛行 C" ⇒ アニメーション(WPF)⇒ MouseEnter.xaml
http://ufcpp.net/study/dotnet/wpf_xamlani.html
さっそく、IronPythonで実行できるようにしてみました。

#
# MouseEnterAnimation.py

import clr
clr.AddReferenceByPartialName("PresentationFramework")
clr.AddReferenceByPartialName("PresentationCore")
from System.Windows import Window, Application
from System.Windows.Markup import XamlReader
xaml_str="""
<WrapPanel
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="200" Height="200">
    <WrapPanel.Resources>
        <Style TargetType="{x:Type Rectangle}">
            <Setter Property="Fill" Value="#8080ff"/>
            <Setter Property="Width" Value="50"/>
            <Setter Property="Height" Value="50"/>
            <Setter Property="RenderTransform">
                <Setter.Value>
                    <RotateTransform CenterX="25" CenterY="25" Angle="0"/>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <EventTrigger RoutedEvent="Mouse.MouseEnter">
                    <BeginStoryboard>
                        <Storyboard>
                            <ColorAnimation
                                Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"
                                To="#ff8080"
                                Duration="0:0:0"/>
                            <DoubleAnimation
                                Storyboard.TargetProperty="RenderTransform.Angle"
                                To="0"
                                Duration="0:0:0"/>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
                <EventTrigger RoutedEvent="Mouse.MouseLeave">
                    <BeginStoryboard>
                        <Storyboard>
                            <ColorAnimation
                                Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"
                                To="#8080ff"
                                Duration="0:0:1"/>
                            <DoubleAnimation
                                Storyboard.TargetProperty="RenderTransform.Angle"
                                To="360"
                                Duration="0:0:3"/>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Style.Triggers>
        </Style>
    </WrapPanel.Resources>
    <Rectangle/><Rectangle/><Rectangle/><Rectangle/>
    <Rectangle/><Rectangle/><Rectangle/><Rectangle/>
    <Rectangle/><Rectangle/><Rectangle/><Rectangle/>
    <Rectangle/><Rectangle/><Rectangle/><Rectangle/>
</WrapPanel>
"""
class Window1(Window):
    def __init__(self):
        self.Title = "MouseEnter.py"
        self.Width = 300
        self.Height = 300
        self.Content = XamlReader.Parse(xaml_str)

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

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

IronPythonで、Windowの背景をグラデーション表示 《 IMultiValueConverter 使用 》

2011-07-07 01:56:00 | DataBinding
IronPythonで、Windowの背景をグラデーション表示しました。MultiBinding を使って。
Blog: メモ ⇒ [WPF]LinearGradientBrushのGradientStopの色はBindingできない。
http://d.hatena.ne.jp/kn000/20100812/1281632098
を参考にしました。C# から IronPython へ変換しました。
色指定は、Colorの名前だけでなく、16進数でも入力できます。

Lineargradientvalueconverter_2

#
# LinearGradientValueConverter.py
#
import clr
clr.AddReferenceByPartialName("PresentationFramework")
clr.AddReferenceByPartialName("PresentationCore")
clr.AddReference('WindowsBase') # for Point
from System import Object
from System.Windows.Markup import XamlReader
from System.Windows import Window, Application, PropertyPath, Point
from System.Windows.Controls import TextBox
from System.Windows.Media import(
        Color,ColorConverter,LinearGradientBrush,GradientStop,Brushes )
from System.Windows.Data import( 
        Binding,BindingOperations, MultiBinding, IMultiValueConverter )

xaml_str="""
<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="LinearGradientValueConverter" Height="300" Width="320">
    <Canvas>  <!-- Text="#FFFFFFFF" or "White"  "#FF000000" or "Black" -->
        <TextBox x:Name="textBox1" Width ="80" Text="#FFFFFFFF" 
                 Canvas.Top = "5" Canvas.Left="8" />
        <TextBox x:Name="textBox2" Width ="80" Text="Black" 
                 Canvas.Top = "25" Canvas.Left="8" />
    </Canvas>
</Window>
"""
class LinearGradientValueConverter(IMultiValueConverter):
    def Convert(self, values, targetType, parameter, culture):
        try:
            color1 = ColorConverter.ConvertFromString(values[0])
            color2 = ColorConverter.ConvertFromString(values[1])
            brush = LinearGradientBrush(StartPoint=Point(0.5, 0.0),EndPoint=Point(0.5, 1.0))
            brush.GradientStops.Add( GradientStop(color1, 0.0) )
            brush.GradientStops.Add( GradientStop(color2, 1.0) )
            result = brush
        except:
            return None
        return result
    def ConvertBack(self, value, targetTypes, parameter, culture):
        return None

class ExWindow(Object):
    def __init__(self):
        self.Root = win = XamlReader.Parse(xaml_str)
        self.textBox1 = win.FindName("textBox1")
        self.textBox2 = win.FindName("textBox2")
        binding = MultiBinding()
        binding.Bindings.Add( Binding(Source = self.textBox1,
                                      Path = PropertyPath(TextBox.TextProperty.Name) ) )
        binding.Bindings.Add( Binding(Source = self.textBox2,
                                      Path = PropertyPath(TextBox.TextProperty.Name) ) )
        binding.Converter = LinearGradientValueConverter()
        #BindingOperations.SetBinding(win, win.BackgroundProperty, binding)
        win.SetBinding(win.BackgroundProperty, binding)

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


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