myBlog

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

IronPythonで、今度は ViewboxUnits を Absolute で Tile を実行する!

2011-07-17 23:44:49 | Animation
WPF/IronPythonで、ViewboxUnits を Absolute で Tile を実行する。
ImageBrush のViewBoxの使い方に慣れてきました。
つぎのBlogを参考にさせて頂きました。

川西 裕幸のブログ ⇒ WPFでテクスチャ移動アニメーション
http://blogs.msdn.com/b/hiroyuk/archive/2007/09/14/4905174.aspx

創造的プログラミングと粘土細工 ⇒ 【WPF】Visibilityってご存じですよね?
http://pro.art55.jp/?eid=969079

画像の上で、マウスを押しながら移動すると、画像がそれに合わせて移動します。
マウス右クリックで、Coverを表示しません( Visibility.Collapsed )
今度は、Tile の上でマウスのホイールを回すと Coverが表示されます( Visibility.Visible )
もう一度、マウス右クリックで Coverが表示されます( Visibility.Visible )
マウスカーソルの形も変化します。

Viewboxtile_absolute

#
# ViewBoxTile_Absolute.py
#
import clr
clr.AddReferenceByPartialName("PresentationFramework")
clr.AddReferenceByPartialName("PresentationCore")
clr.AddReference('WindowsBase')  # for Point, Rect
from System import Convert, Math, Uri
from System.Windows import Window, Application, Rect, Point, Visibility
from System.Windows.Markup import XamlReader
from System.Windows.Controls import Canvas, UserControl
from System.Windows.Media import( ImageBrush, AlignmentX, AlignmentY, Stretch, 
        BrushMappingMode, Brushes, SolidColorBrush, Colors, Color )
from System.Windows.Media.Imaging import BitmapImage
from System.Windows.Shapes import Rectangle
from System.Windows.Input import Mouse, Cursors

class Tile(Canvas):
    def __init__(self):
        self.targetAngle = 0
        self.layoutRoot = Canvas()
        self.Children.Add(self.layoutRoot)
              
        self.bkgBrush = ImageBrush(
            ImageSource = BitmapImage(Uri("http://softgarden.lovepop.jp/myBlog/image/pic3.png")))
        self.bkgBrush.AlignmentX = AlignmentX.Left
        self.bkgBrush.AlignmentY = AlignmentY.Top
        self.bkgBrush.Stretch = Stretch.None 
        #self.bkgBrush.Viewport = Rect(0,0,1.0,1.0)
        self.bkgBrush.ViewboxUnits = BrushMappingMode.Absolute # New
        self.bkgBrush.Viewbox = Rect(0,0,640,400) # New

        self.bkg = Rectangle()
        self.bkg.Width = 250
        self.bkg.Height = 250
        self.bkg.Fill = self.bkgBrush
        self.layoutRoot.Children.Add(self.bkg)

        self.isMouseDown = False
        self.currentPoint = Point(0,0)
        self.oldPoint = Point(0,0)
        self.layoutRoot.MouseDown += self.LayoutRoot_MouseDown
        self.layoutRoot.MouseUp += self.LayoutRoot_MouseUp
        self.layoutRoot.MouseMove += self.LayoutRoot_MouseMove
        #self.layoutRoot.MouseWheel += self.LayoutRoot_MouseWheel
        self.layoutRoot.MouseRightButtonDown += self.LayoutRoot_MouseRightButtonDown
        self.win = None
  
    def LayoutRoot_MouseDown(self, sender, e):
        self.isMouseDown = True
        self.oldPoint = e.GetPosition(self.layoutRoot)
        self.currentPoint = Point(self.bkgBrush.Viewbox.X, self.bkgBrush.Viewbox.Y)
        self.layoutRoot.CaptureMouse()
        Mouse.OverrideCursor = Cursors.Hand

    def LayoutRoot_MouseUp(self, sender, e):
        self.isMouseDown = False
        self.layoutRoot.ReleaseMouseCapture()
        Mouse.OverrideCursor = Cursors.Arrow

    def LayoutRoot_MouseMove(self, sender, e):
        if (self.isMouseDown):
            newPoint = e.GetPosition(self.layoutRoot)
            r = Rect(self.currentPoint.X - (newPoint.X - self.oldPoint.X) ,
                     self.currentPoint.Y - (newPoint.Y - self.oldPoint.Y), 0 ,0)
            self.bkgBrush.Viewbox = r

#   def LayoutRoot_MouseWheel(self, sender, e):
    def LayoutRoot_MouseRightButtonDown(self, sender, e):
        self.win.cover.Visibility = Visibility.Visible
        self.win.isMouseDown = False
        Mouse.OverrideCursor = Cursors.Arrow

    def SetX(self, value):
        self.SetValue(Canvas.LeftProperty, Convert.ToDouble(value))
    def GetX(self):
        return self.GetValue(Canvas.LeftProperty)
    X = property(GetX, SetX)
          
    def SetY(self, value):
        self.SetValue(Canvas.TopProperty, Convert.ToDouble(value))
    def GetY(self):
        return self.GetValue(Canvas.TopProperty)
    Y = property(GetY, SetY)

class TileWindow(Window):
    def __init__(self):
        self.Title="ViewBoxTile_Absolute.py"
        self.Width = 600
        self.Height= 330
        self.Background = Brushes.Black
        xaml_str="""  
            <UserControl 
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:xaml="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
                Width="600" Height="300">
                <Canvas x:Name="LayoutRoot" Background="LightGray"> <!-- "#222222" -->
                    <Canvas x:Name="tiles"></Canvas>
                    <Image x:Name="cover" 
                        Source="http://softgarden.lovepop.jp/myBlog/image/cover.png" 
                        Width="600" Height="330" Stretch="Fill" Visibility="Collapsed">
                    </Image>
                 </Canvas>
            </UserControl>"""
        userControl = XamlReader.Parse(xaml_str)
        self.layoutRoot = userControl.FindName("LayoutRoot")
        self.tiles = userControl.FindName("tiles")
        self.cover = userControl.FindName("cover")
        self.Content = userControl
      
        self.tile = s = Tile()
        s.X = 160
        s.Y = 25
        s.bkgBrush.Viewbox = Rect(s.X + 40, s.Y + 25, 0, 0 ) # New
        s.win = self # New
        self.tiles.Children.Add(s)

        self.isMouseDown = False
        self.currentPoint = Point(0,0)
        self.oldPoint = Point(0,0)
        self.cover.MouseDown += self.Cover_MouseDown
        self.cover.MouseUp += self.Cover_MouseUp
        self.cover.MouseMove += self.Cover_MouseMove
        self.cover.MouseRightButtonDown += self.Cover_MouseRightButtonDown
        self.cover.Visibility = Visibility.Visible
        self.rectTiles = Rect(166, 33, 409, 276)

    def Cover_MouseDown(self, sender, e):
        position = e.GetPosition(self.cover) #Mouse.GetPosition(sender)
        if self.rectTiles.Contains(position): 
            self.isMouseDown = True
            self.oldPoint = position
            self.currentPoint = Point(self.tile.bkgBrush.Viewbox.X, self.tile.bkgBrush.Viewbox.Y)
            self.cover.CaptureMouse()
            Mouse.OverrideCursor = Cursors.Hand

    def Cover_MouseUp(self, sender, e):
        self.isMouseDown = False
        #self.currentPoint = Point(self.tile.bkgBrush.Viewbox.X, self.tile.bkgBrush.Viewbox.Y)
        self.cover.ReleaseMouseCapture()
        Mouse.OverrideCursor = Cursors.Arrow

    def  Cover_MouseMove(self, sender, e):
        if (self.isMouseDown):
            newPoint = e.GetPosition(self.layoutRoot)
            r = Rect(self.currentPoint.X - (newPoint.X - self.oldPoint.X) ,
                     self.currentPoint.Y - (newPoint.Y - self.oldPoint.Y), 0 ,0)
            self.tile.bkgBrush.Viewbox = r

    def Cover_MouseRightButtonDown(self, sender, e):
        self.cover.Visibility = Visibility.Collapsed

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

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

IronPythonで、advanced Tile (Gestalt Transformations Pack) を実行する!

2011-07-16 14:59:00 | Animation
ひき続き、WPF/IronPythonで、Gestalt advanced Tile(Transformations Pack)サンプルを実行する。
Gestalt ⇒ samples ⇒ Transformations Pack ⇒ view 2
http://www.visitmix.com/labs/gestalt/samples/
ImageBrush の変換に少し苦労しました。
WPFとSilverlightの違いがわかった。
Silverlightは、ほぼ WPFのサブセットだ。
ぜひ、実行してみてください、ダイナッミクに画面が変わる、おもしろいプログラムです。

Advanced_tile

#
# advanced.tile.py
#   from:transform.advanced.python.html
#
import clr
clr.AddReferenceByPartialName("PresentationFramework")
clr.AddReferenceByPartialName("PresentationCore")
clr.AddReference('WindowsBase')  # for Point, Rect
from System import Convert, Math, Uri
from System.Windows import Window, Application, Rect, Point, Visibility
from System.Windows.Markup import XamlReader
from System.Windows.Controls import Canvas, UserControl
from System.Windows.Media import( ImageBrush, TranslateTransform, AlignmentX, AlignmentY, Stretch,
    CompositionTarget, Brushes, ScaleTransform, RotateTransform, TransformGroup )
from System.Windows.Media.Imaging import BitmapImage
from System.Windows.Shapes import Rectangle

class Tile(Canvas):
    def __init__(self):
        self.targetAngle = 0
        layoutRoot = Canvas()
        self.Children.Add(layoutRoot)
        #self.bkgBrushPosition = TranslateTransform()
        #self.bkgBrushPosition = Point(0, 0)
        #self.bkgBrushPosition.X = 0
        #self.bkgBrushPosition.Y = 0
        self.bkgBrush = ImageBrush(
            ImageSource = BitmapImage(Uri("http://softgarden.lovepop.jp/myBlog/image/pic3.png")))
        self.bkgBrush.AlignmentX = AlignmentX.Left
        self.bkgBrush.AlignmentY = AlignmentY.Top
        self.bkgBrush.Stretch = Stretch.None 
        #self.bkgBrush.Transform = self.bkgBrushPosition
        #self.bkgBrush.Viewport = Rect(0,0,1.0,1.0) # New
 
        self.bkg = Rectangle()
        self.bkg.Width = 30
        self.bkg.Height = 30
        self.bkg.Fill = self.bkgBrush
        self.bkg.RenderTransformOrigin = Point (0.45,0.45)
        layoutRoot.Children.Add(self.bkg)
      
        self.scale = ScaleTransform()
        self.scale.ScaleX = 1.0
        self.scale.ScaleY = 1.0
        self.rotation = RotateTransform()
        self.rotation.Angle = 0
        transformGroup = TransformGroup()
        transformGroup.Children.Add(self.scale)
        transformGroup.Children.Add(self.rotation)
        self.bkg.RenderTransform = transformGroup

        CompositionTarget.Rendering += self.compositionTarget_Rendering
                       
    def compositionTarget_Rendering(self, sender, e):
        self.rotation.Angle = self.rotation.Angle + ( self.targetAngle - self.rotation.Angle) * .13

    def SetBkgBrushXY(self, x, y):
        self.bkgBrush.Viewbox = Rect(x/640.0, y/400.0, 0, 0) # New

    def SetX(self, value):
        self.SetValue(Canvas.LeftProperty, Convert.ToDouble(value))
    def GetX(self):
        return self.GetValue(Canvas.LeftProperty)
    X = property(GetX, SetX)
          
    def SetY(self, value):
        self.SetValue(Canvas.TopProperty, Convert.ToDouble(value))
    def GetY(self):
        return self.GetValue(Canvas.TopProperty)
    Y = property(GetY, SetY)

class TileWindow(Window):
    def __init__(self):
        self.Title = "advanced.tile.py" #"Advanced Python Transforms"
        self.Width = 600
        self.Height= 350
        self.Background = Brushes.Black
        xaml_str="""  
            <UserControl 
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:xaml="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
                Width="600" Height="300">
                <Canvas x:Name="LayoutRoot" Background="#222222">
                    <Canvas x:Name="tiles"></Canvas>
                    <Image x:Name="cover" 
                     Source="http://softgarden.lovepop.jp/myBlog/image/cover.png" 
                     Width="600" Height="330" Stretch="Fill" Visibility="Collapsed">
                    </Image>
                </Canvas>
            </UserControl>"""
        userControl = XamlReader.Parse(xaml_str)
        self.layoutRoot = userControl.FindName("LayoutRoot")
        self.tiles = userControl.FindName("tiles")
        self.cover = userControl.FindName("cover")
        self.Content = userControl

        for x in range(0,10):
            for y in range(0, 10):
                s = Tile()
                s.X = 160 + (x * 25)
                s.Y = 25 + (y * 25)
                #s.bkgBrushPosition.X = -s.X - 45;
                #s.bkgBrushPosition.Y = -s.Y - 30;
                s.SetBkgBrushXY(s.X + 40  , s.Y + 25  ) # New
                self.tiles.Children.Add(s)
        self.mouse = Point(0, 0)
        self.layoutRoot.MouseMove += self.LayoutRoot_MouseMove
        self.cover.Visibility = Visibility.Visible
        CompositionTarget.Rendering += self.compositionTarget_Rendering
   
    def LayoutRoot_MouseMove(self,sender, e):
        self.mouse = e.GetPosition(self.layoutRoot)
          
    def compositionTarget_Rendering(self, sender, e):
        for s in self.tiles.Children:
            _y = s.Y - self.mouse.Y
            _x = s.X - self.mouse.X
            distance = Math.Sqrt((_y * _y) + (_x * _x))
            rad = Math.Atan2(_y, _x)
            angle = rad * (180 / Math.PI)
            newScale = .3 + ((distance / 100.0));
            if newScale > 1.2:
                newScale = 1.2
            s.scale.ScaleX = s.scale.ScaleY = newScale
            if distance < 100:
                s.targetAngle = angle - 180
            else: 
                s.targetAngle = 0

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

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

IronPythonで、Tile (Gestalt Transformations Pack) を実行する!

2011-07-15 15:25:20 | Animation
WPF/IronPythonで、Gestalt Tile(Transformations Pack)サンプルを実行する。
Gestalt ⇒ samples ⇒ Transformations Pack ⇒ view 1
http://www.visitmix.com/labs/gestalt/samples/
変換に苦労しました。難解でした。
しかし、次のページを見つけて。すんなり、変換できました!
Flash to SilverlighA Fun Interactive Example
http://visitmix.com/labs/rosetta/FTSL/GetStarted/ColorScale/Page3/
参考にしてください。Tile の Silverlightアニメーションがあります。

Tile_2

#
# tile.py
#  
import clr
clr.AddReferenceByPartialName("PresentationFramework")
clr.AddReferenceByPartialName("PresentationCore")
clr.AddReference('WindowsBase')  # for Point
from System import Convert, Math, TimeSpan
from System.Windows import Window, Application, Point
from System.Windows.Markup import XamlReader
from System.Windows.Controls import Canvas, UserControl
from System.Windows.Media import( Colors, Color, SolidColorBrush, Brushes,
    CompositionTarget, DoubleCollection, ScaleTransform, RotateTransform, TransformGroup )
from System.Windows.Shapes import Ellipse
 
class Tile(Canvas):
    def __init__(self):
        self.targetAngle = 0
        layoutRoot = Canvas()
        self.Children.Add(layoutRoot)
        self.bkg = Canvas()
        self.bkg.Width = 30
        self.bkg.Height = 10
        self.bkg.Background = SolidColorBrush(Colors.White)
        self.bkg.RenderTransformOrigin = Point (0.5,0.5)
        Canvas.SetLeft(self.bkg, -15)
        Canvas.SetTop (self.bkg, -5)
        layoutRoot.Children.Add(self.bkg)

        self.scale = ScaleTransform()
        self.scale.ScaleX = 1.0
        self.scale.ScaleY = 1.0
        self.rotation = RotateTransform()
        self.rotation.Angle = 0

        transformGroup = TransformGroup()
        transformGroup.Children.Add(self.scale)
        transformGroup.Children.Add(self.rotation)
        self.bkg.RenderTransform = transformGroup

        dc = DoubleCollection();
        dc.Add(1);
        dc.Add(2);
        ellipse = Ellipse()
        ellipse.Stroke = SolidColorBrush(Color.FromArgb(0xFF,0xFF,0x00,0xFF))
        ellipse.StrokeDashArray = dc
        ellipse.Width = 10
        ellipse.Height = 10
        Canvas.SetLeft(ellipse, -5)
        Canvas.SetTop (ellipse, -5)
        layoutRoot.Children.Add(ellipse)

        CompositionTarget.Rendering += self.compositionTarget_Rendering

    def compositionTarget_Rendering(self, sender, e):
        self.rotation.Angle += (self.targetAngle - self.rotation.Angle) * 0.08

    def SetX(self, value):
        self.SetValue(Canvas.LeftProperty, Convert.ToDouble(value))
    def GetX(self):
        return self.GetValue(Canvas.LeftProperty)
    X = property(GetX, SetX)

    def SetY(self, value):
        self.SetValue(Canvas.TopProperty, Convert.ToDouble(value))
    def GetY(self):
        return self.GetValue(Canvas.TopProperty)
    Y = property(GetY, SetY)

    def SetBackground(self, value):
        self.bkg.Background = value
    def GetBackground(self):
        return self.bkg.Background
    background = property(GetBackground, SetBackground)

class Window1(Window):
    def __init__(self):
        self.Title="tile.py"
        self.Width = 600
        self.Height= 400
        self.Background = Brushes.Black
        xaml_str="""  
            <UserControl 
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
                Width="600" Height="300">
                <Canvas x:Name="LayoutRoot" Background="#222222">
                    <Canvas x:Name="tiles"></Canvas>
                </Canvas>
            </UserControl> """
        userControl = XamlReader.Parse(xaml_str)
        self.layoutRoot = userControl.FindName("LayoutRoot")
        self.tiles = userControl.FindName("tiles")
        self.Content = userControl

        for x in range(0,15):
          for y in range(0, 10):
              s = Tile()
              s.background = SolidColorBrush(
                  Color.FromArgb(0xcc,Convert.ToByte((100 + (x*10))),0,Convert.ToByte((100 + (y*15)))))
              s.X = 115 + (x * 25)
              s.Y = 25 + (y * 25)
              self.tiles.Children.Add(s)

        self.mouse = Point(0, 0)
        self.layoutRoot.MouseMove += self.LayoutRoot_MouseMove
        CompositionTarget.Rendering += self.compositionTarget_Rendering

    def LayoutRoot_MouseMove(self,sender, e):
        self.mouse = e.GetPosition(self.layoutRoot)

    def compositionTarget_Rendering(self, sender, e):
        for s in self.tiles.Children:
            _y = s.Y - self.mouse.Y
            _x = s.X - self.mouse.X
            distance = Math.Sqrt((_y * _y) + (_x * _x))
            rad = Math.Atan2(_y, _x)
            angle = rad * (180/Math.PI)
            s.scale.ScaleX = .6 + ((distance / 300.0))
            s.scale.ScaleY = s.scale.ScaleX
            s.angle = angle
            if distance < 200:
                s.targetAngle = angle - 180
            else:
                s.targetAngle = 0

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

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

IronPythonで、Snow ( Gastaltのサンプル ) を実行する!

2011-07-13 13:25:42 | Animation

再び WPF/IronPythonで、 Snow (Gestalt Particles Pack)サンプルを実行する。

Gestalt ⇒ samples ⇒ Particles Pack ⇒ view 1
http://www.visitmix.com/labs/gestalt/samples/
暑い夏に、清涼感を感じてください...

Let it grow, let it grow,
Let it blossom, let it flow.
In the sun, the rain, the snow,
Love is lovely, let it grow.

Snow


# snow.py
# from: particles.simple.python.html
#
import clr
clr.AddReferenceByPartialName("PresentationFramework")
clr.AddReferenceByPartialName("PresentationCore")
clr.AddReference('WindowsBase') # for Point
from System.Windows.Markup import XamlReader
from System import Object, Random, Math
from System.Windows import Window, Application, Point
from System.Windows.Controls import Canvas, UserControl
from System.Windows.Media import CompositionTarget, Brushes
#from System.Windows.Media.Animation import Storyboard 
    
xaml_SnowFlake_str = """
<UserControl 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="20" Height="20">
    <Grid x:Name="LayoutRoot">
        <Ellipse Fill="#FFFFFFFF"></Ellipse>
    </Grid>
</UserControl>
"""
class Snowflake(UserControl):
    def __init__(self, xaml, randomNumber):
        self.xaml = xaml
        self.randomNumber = randomNumber
        self.x = 0
        self.y = 0
        self.xSpeed = 0
        self.ySpeed = 0
        self.radius = 0
        self.scale = 0
        self.alpha = 0
        self.stageSize = Point()
        self.Content = XamlReader.Parse(xaml)
            
        # This method gets called many times a second and is responsible for moving your snowflake around
        def MoveSnowFlake(sender, e):
            self.x = self.x + self.xSpeed
            self.y = self.y + self.ySpeed
    
            Canvas.SetTop(self, self.y)
            Canvas.SetLeft(self, Canvas.GetLeft(self) + self.radius * Math.Cos(self.x))
    
            # Reset the position to go back to the top when the bottom boundary is reached
            if (Canvas.GetTop(self) > self.stageSize.Y):
                Canvas.SetTop(self, - self.ActualHeight - 10)
                self.y = Canvas.GetTop(self)

        CompositionTarget.Rendering += MoveSnowFlake # !
                     
    def SetInitialProperties(self, stageWidth, stageHeight):
        self.xSpeed = self.randomNumber.NextDouble() / 20
        self.ySpeed = .01 + self.randomNumber.NextDouble() * 2
        self.radius = self.randomNumber.NextDouble()
        self.scale = .01 + self.randomNumber.NextDouble() * 2
        self.alpha = .1 + self.randomNumber.NextDouble()
    
        # Setting initial position
        Canvas.SetLeft(self, self.randomNumber.Next(stageWidth))
        Canvas.SetTop(self, self.randomNumber.Next(stageHeight))
    
        self.stageSize = Point(stageWidth, stageHeight)
        self.y = Canvas.GetTop(self)
    
        # Setting initial size and opacity
        self.Content.Width = 5 * self.scale
        self.Content.Height = 5 * self.scale
        self.Content.Opacity = self.alpha

class SnowWindow(Window):
    def __init__(self):
        self.Title="Let It Grow"
        self.Width = 600
        self.Height= 300
        self.Background = Brushes.Black
        xaml_str="""  
            <Canvas x:Name="LayoutRoot"
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
              <Canvas.Background>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                  <GradientStop Color="#FF00202A" Offset="1"></GradientStop>
                  <GradientStop Color="#FF000000" Offset="0"></GradientStop>
                </LinearGradientBrush>
              </Canvas.Background>
            </Canvas>"""
        canvas = XamlReader.Parse(xaml_str)
        self.layoutRoot = canvas.FindName("LayoutRoot")
        canvas.Width = 600
        canvas.Height = 300
        self.Content = canvas  

        randomNumber = Random()
        for i in range(0, 200):
            snowflake = Snowflake(xaml_SnowFlake_str, randomNumber)
            # 600 and 300 is the width/height of the application
            snowflake.SetInitialProperties(600, 300)
            self.layoutRoot.Children.Add(snowflake)

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

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

IronPythonで、お掃除ロボット( Xamloomba )を動かす!

2011-07-12 15:53:17 | Animation
WPF/IronPythonで、Silverlightアニメーション(お掃除ロボット)を実行できるようにする。

ネットを検索していると、このSilverlightプログラムが目にとまった。
さっそく、IronPythonで、動かすことにした。少しアレンジしています。

デザインツールで始めるSilverlightアニメ/グラフィック
Silverlight 2で.NET技術をカッコよく使おう(3)
http://www.atmarkit.co.jp/fwcr/rensai2/silverlight2_03/silverlight2_03_3.html

お部屋のXAML ⇒ room.xaml ⇒ フローリング
http://softgarden.lovepop.jp/myBlog/xaml/room.xaml
お掃除ロボットXAML ⇒ loomba.xaml ⇒ ザムルンバ(Xamloomba)君
http://softgarden.lovepop.jp/myBlog/xaml/loomba.xaml

フローリングの好きな位置をクリックすると、Xamloomba君がその方向に回転した後に目的地まで移動します。
この処理はStoryboardを動的に生成して実行して実現していますので、詳しくはソースを確認してください .…

Myloomba    


# myLoomba.py
#
import clr
clr.AddReferenceByPartialName("PresentationFramework")
clr.AddReferenceByPartialName("PresentationCore")
clr.AddReference('WindowsBase')  # for Point
from System import Object, TimeSpan, Math
from System.Windows.Markup import XamlReader
from System.Windows import ( 
        Window, Application, Point, Duration, PropertyPath,
        HorizontalAlignment, VerticalAlignment )
from System.Windows.Controls import Canvas, StackPanel
from System.Windows.Media.Animation import ( 
        Storyboard, RepeatBehavior, DoubleAnimation, 
        DoubleAnimationUsingKeyFrames, SplineDoubleKeyFrame, KeyTime )
from System.Windows.Media import RotateTransform

def LoadXamlNet(strUrl):
    import System
    request = System.Net.WebRequest.Create(strUrl)
    response = request.GetResponse()
    dataStream = response.GetResponseStream()
    try:
        element = XamlReader.Load(dataStream)
    finally:
        dataStream.Close()
        response.Close()
    return element

class Loomba(Object):
    SpeedPerSec = 100
    SpeedTurn = 2
    def __init__(self):
        self.Content = uc =  LoadXamlNet("http://softgarden.lovepop.jp/myBlog/xaml/loomba.xaml")
        self.loomba = uc.FindName('loomba')    
        setattr(self, "turnLeft", uc.FindName('turnLeft'))   
        setattr(self, "turnRight", uc.FindName('turnRight'))  
        setattr(self, "flushlight", uc.FindName('flushlight'))
        setattr(self, "loombaRotate", uc.FindName('loombaRotate'))

    def StopCurrentAction(self):
        self.turnLeft.Pause()
        self.turnRight.Pause()

    def Move(self, dest):
        self.StopCurrentAction()
        original = self.GetOriginalPosition()
        current = self.GetCurrentPosition()
        distance = self.GetDistanceTo(dest)
        angle = self.GetAngleTo(dest)
        currentAngle = self.GetCurrentAngle()

        angle2 = angle # 0.0 ~ 360.0 へ
        if (angle < 0.0 ):
            angle2 = 360.0 + angle
        currentAngle2 = currentAngle % 360.0
        by = angle2 - currentAngle2
        if by > 180.0 :
            by -= 360.0
        elif by < -180.0 :
            by += 360.0

        #//回転に要する時間
        #turnTime = TimeSpan.FromSeconds(Math.Abs(self.GetCurrentAngle() - angle) * 2 / 360)
        turnTime = TimeSpan.FromSeconds( ( Math.Abs(by) *  2.0 ) / 360.0 )

        #//目的地までの所要時間
        time = TimeSpan.FromSeconds(distance / self.SpeedPerSec)

        #//ストーリーボードを作成
        sb = Storyboard()

        #//ストーリーボードの所要時間を設定
        sb.Duration = time.Add(turnTime)

        #//回転用のアニメーション
        turnAnimation = DoubleAnimation()
        #turnAnimation.To = angle
        turnAnimation.By = by
        turnAnimation.Duration = Duration(turnTime)
        sb.Children.Add(turnAnimation)

        Storyboard.SetTarget(turnAnimation, self.loomba)
        Storyboard.SetTargetProperty(turnAnimation, 
            PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)"))
  
        #//縦方向移動アニメーション
        topAnimation = DoubleAnimationUsingKeyFrames()
        topAnimation.BeginTime = turnTime #//回転終了後に開始
        topKf = SplineDoubleKeyFrame()
        topKf.KeyTime = KeyTime.FromTimeSpan(time)
        topKf.Value = dest.Y - original.Y
        topAnimation.KeyFrames.Add(topKf)
        sb.Children.Add(topAnimation)

        Storyboard.SetTarget(topAnimation, self.loomba);
        Storyboard.SetTargetProperty(topAnimation,
            PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)"))

        #//横方向移動アニメーション
        leftAnimation = DoubleAnimationUsingKeyFrames()
        leftAnimation.BeginTime = turnTime #//回転終了後に開始
        leftKf = SplineDoubleKeyFrame()
        leftKf.KeyTime = KeyTime.FromTimeSpan(time)
        leftKf.Value = dest.X - original.X
        leftAnimation.KeyFrames.Add(leftKf)
        sb.Children.Add(leftAnimation)

        Storyboard.SetTarget(leftAnimation, self.loomba)
        Storyboard.SetTargetProperty(leftAnimation, 
            PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)"))
        #//ストーリーボードを実行
        sb.Begin();

    def GetOriginalPosition(self):
        this=self.Content 
        original = Point(Canvas.GetLeft(this), Canvas.GetTop(this))
        return original
    def GetCurrentPosition(self):
        original = self.GetOriginalPosition()
        trans = (self.loomba.RenderTransform).Children[3]
        current = Point(original.X + trans.X, original.Y + trans.Y)
        return current
    def GetCurrentAngle(self):
        trans = (self.loomba.RenderTransform).Children[2]
        return trans.Angle
    def GetDistanceTo(self,dest):
        current = self.GetCurrentPosition()
        dx = current.X - dest.X;
        dy = current.Y - dest.Y;
        return Math.Sqrt(dx * dx + dy * dy)
    def GetAngleTo(self,dest):
            current = self.GetCurrentPosition()
            dx = current.X - dest.X
            dy = current.Y - dest.Y
            return Math.Atan2(dy, dx) * 180 / Math.PI - 90

class ExWindow(Object):
    def __init__(self, Content=None, **keywords):
        self.Root = win = Window(**keywords)
        aUserControl = LoadXamlNet("http://softgarden.lovepop.jp/myBlog/xaml/room.xaml")
        aUserControl.FindName('floor').MouseLeftButtonDown+=self.Border_MouseLeftButtonDown
        aUserControl.FindName('leftButton').Click+=self.leftButton_Click
        aUserControl.FindName('startFlush').Click+=self.startFlush_Click
        aUserControl.FindName('rightButton').Click+=self.rightButton_Click
        setattr(self, "floor", aUserControl.FindName('floor'))
        self.swFlush = 0
        
        self.myLoomba = Loomba()
        self.myLoomba.Content.RenderTransformOrigin = Point(0.5,0.5)
        self.myLoomba.Content.HorizontalAlignment = HorizontalAlignment.Center
        self.myLoomba.Content.VerticalAlignment = VerticalAlignment.Center
 
        self.myLoomba.Content.Width = 100
        self.myLoomba.Content.Height = 100
        Canvas.SetLeft(self.myLoomba.Content,250)
        Canvas.SetTop(self.myLoomba.Content,200)

        canvas=aUserControl.FindName('myLoombaCanvas')
        canvas.AddChild(self.myLoomba.Content)

        win.Content = aUserControl

    def Border_MouseLeftButtonDown(self,sender, e):
        #//マウス位置を取得
        mouse = e.GetPosition(self.floor)
        #//マウス位置は左上のため、中心点を取得
        dest = Point(mouse.X - 50, mouse.Y - 50)
        #//XamLoombaの移動
        self.myLoomba.Move(dest)
 
    def leftButton_Click(self,sender, e):
        #//左へ回転
        self.myLoomba.turnLeft.Begin()

    def rightButton_Click(self,sender, e):
        #//右へ回転
        self.myLoomba.turnRight.Begin()

    def startFlush_Click(self,sender, e):
        #//RepeatBehavior.Foreverを設定することにより、動作を永遠に続けます
        self.myLoomba.flushlight.RepeatBehavior = RepeatBehavior.Forever
        if self.swFlush == 0:
            #//フラッシュを開始
            self.myLoomba.flushlight.Begin()
            self.swFlush = 3
        elif self.swFlush == 1:
            #//フラッシュを中断
            self.myLoomba.flushlight.Pause()
            self.swFlush = 2
        elif self.swFlush == 2:
            #//フラッシュを再開
            self.myLoomba.flushlight.Resume()
            self.swFlush = 1
        else:
            #//フラッシュを停止
            self.myLoomba.flushlight.Stop()
            self.swFlush = 0

if __name__ == "__main__":
    win = ExWindow( Width=608, Height=432 ,Title="myLoomba.py")
    Application().Run(win.Root)




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

IronPythonで、Gestalt の Sample を動かす! 《 WPF/IronPython 》

2011-07-11 18:33:05 | Animation
WPF/IronPythonで、Gestalt の マッシュルームのサンプルを実行する。

久しぶりに、ネットからGestaltのSampleを動かそうとしたが、起動できなかった。
そこで、IronPythonで、動かすことにした。無事、完成しました!

Gestalt ⇒ samples ⇒ Getting Started Pack ⇒ view 5
http://www.visitmix.com/labs/gestalt/samples/

blinking_mushroo XAML ⇒ mushroom.xaml ⇒ マッシュルーム
http://softgarden.lovepop.jp/myBlog/xaml/mushroom.xaml

Gestaltsample_2

#
# GestaltSample.py
#
import clr
clr.AddReferenceByPartialName("PresentationFramework")
clr.AddReferenceByPartialName("PresentationCore")
clr.AddReference('WindowsBase')
from System.Windows.Markup import XamlReader
from System.Windows import Window, Application
from System.Windows.Controls import Canvas, Label, Button, TextBox

def LoadXamlNet(strUrl):
    import System
    request = System.Net.WebRequest.Create(strUrl)
    #request.Credentials = System.Net.CredentialCache.DefaultCredentials
    response = request.GetResponse()
    dataStream = response.GetResponseStream()
    try:
        element = XamlReader.Load(dataStream)
    finally:
        dataStream.Close()
        response.Close()
    return element

class ExWindow(Window):
    def __init__(self):
        self.Title="GestaltSample.py"
        self.Width = 235
        self.Height= 360
        self.Content = canvas = Canvas()
        self.say_hello_lbl = Label(Content="Say hello to: " ,FontSize = 16 )
        self.say_hello_txt = TextBox(Width=90.0,FontSize = 16)
        self.say_hello_btn = Button(Content="Go",FontSize = 16, Width = 30)
        canvas.AddChild(self.say_hello_lbl)
        canvas.AddChild(self.say_hello_txt)
        canvas.AddChild(self.say_hello_btn)
        #bm = me.blinking_mushroom
        #bm.left_eye_blink.Begin()
        #bm.right_eye_blink.Begin()
        self.blinking_mushroom = bm = LoadXamlNet("http://softgarden.lovepop.jp/myBlog/xaml/mushroom.xaml")
        canvas.AddChild(bm)
        bm.FindName("left_eye_blink").Begin()
        bm.FindName("right_eye_blink").Begin()
        self.show_bubble = bm.FindName("show_bubble")
        self.float_bubble = bm.FindName("float_bubble")
        self.bubble_text = bm.FindName("bubble_text")
        self.say_hello_btn.Click += self.OnClick
        self.show_bubble.Completed += self.Completed

        self.SetPosition(self.say_hello_lbl, 5, 8) 
        self.SetPosition(self.say_hello_txt, 95, 10) 
        self.SetPosition(self.say_hello_btn, 190, 9)
        self.SetPosition(self.blinking_mushroom, 10, 35) 

    def SetPosition(self, obj, x, y):
        try:
            Canvas.SetTop(obj, y)
        except: pass
        try:
            Canvas.SetLeft(obj, x)
        except: pass
    def OnClick(self, s, e):
        self.bubble_text.Text = "Hello, " + self.say_hello_txt.Text + "!"
        self.show_bubble.Begin()
    def Completed(self, s, e):
        self.float_bubble.Begin()

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

IronPythonで、自由にドラッグできる、時計を作りました! 《 Drag clock 》

2011-07-09 14:07:48 | Animation
Dynamic Sliverlight サンプルの clock を WPF-IronPythonに変換しました。
下記のBlogを参考にして、Drag可能な、時計にしました。
Blog: jimmy.thinking ⇒ Dragging elements in Silverlight with DLRConsole
http://blog.jimmy.schementi.com/2008/08/dragging-elements-in-silverlight-with.html

MSDN Blogs ⇒ 荒井省三のBlog ⇒ DLR Console を使って時計をドラッグするサンプル
http://blogs.msdn.com/b/shozoa/archive/2008/09/03/dragging-clock-on-dlr-console.aspx

clock XAML⇒clock.xaml ⇒ アナログ時計

Clock

#
# clock.py
# ipy.exe clock.py
#
import clr
clr.AddReferenceByPartialName("PresentationFramework")
clr.AddReferenceByPartialName("PresentationCore")
clr.AddReference('WindowsBase')
from System import Object
from System.Windows.Markup import XamlReader
from System.Windows import Window, Application
from System.Windows.Controls import Canvas
from System.Windows.Media import Brushes
from datetime import datetime

def LoadXamlNet(strUrl):
    import System
    request = System.Net.WebRequest.Create(strUrl)
    response = request.GetResponse()
    dataStream = response.GetResponseStream()
    try:
        element = XamlReader.Load(dataStream)
    finally:
        dataStream.Close()
        response.Close()
    return element

class Clock(Object):
    def __init__(self):
        self.scene = LoadXamlNet("http://softgarden.lovepop.jp/myBlog/xaml/clock.xaml")
        self.start()
    def _fromAngle(self, time, divisor = 5, offset = 0):
        return ((time / (12.0 * divisor)) * 360) + offset + 180
    def _toAngle(self, time):
        return self._fromAngle(time) + 360
    def start(self):
        d = datetime.now()
        #self.scene.FindName('hourAnimation').From = self._fromAngle(d.hour, 1, d.minute/2)
        #self.scene.FindName('hourAnimation').To = self._toAngle(d.hour)
        from_h = self._fromAngle(d.hour, 1, d.minute/2)
        to_h = from_h + 360.0
        self.scene.FindName('hourAnimation').From = from_h
        self.scene.FindName('hourAnimation').To = to_h
        self.scene.FindName('minuteAnimation').From = self._fromAngle(d.minute)
        self.scene.FindName('minuteAnimation').To = self._toAngle(d.minute)
        self.scene.FindName('secondAnimation').From = self._fromAngle(d.second)
        self.scene.FindName('secondAnimation').To = self._toAngle(d.second)

class Drag(object):
    def __init__(self, root, obj):
        self.click = None
        self.obj = obj
        self.root = root
    def OnMouseLeftButtonDown(self, sender, e):
        self.click = e.GetPosition(self.root)
        self.sx = Canvas.GetLeft(self.obj)
        self.sy = Canvas.GetTop(self.obj)
        if (self.sx.IsNaN(self.sx)): self.sx = 0.0
        if (self.sy.IsNaN(self.sy)): self.sy = 0.0
        self.obj.CaptureMouse()
    def OnMouseLeftButtonUp(self, sender, e):
        if(self.click != None):
            self.obj.ReleaseMouseCapture()
            self.click = None
    def OnMouseMove(self, sender, e):
        if(self.click != None):
            mouse_pos = e.GetPosition(self.root)
            Canvas.SetLeft(self.obj, (self.sx + mouse_pos.X - self.click.X))
            Canvas.SetTop(self.obj, (self.sy + mouse_pos.Y - self.click.Y))
    def enable(self):
         self.obj.MouseLeftButtonDown += self.OnMouseLeftButtonDown
         self.obj.MouseLeftButtonUp += self.OnMouseLeftButtonUp
         self.obj.MouseMove += self.OnMouseMove
    def disable(self):
         self.obj.MouseLeftButtonDown -= self.OnMouseLeftButtonDown
         self.obj.MouseLeftButtonUp -= self.OnMouseLeftButtonUp
         self.obj.MouseMove -= self.OnMouseMove

class ExWindow(Window):
    def __init__(self):
        self.Title = "Dynamic Languages Rock!"
        self.Width = 640
        self.Height = 480
        self.Content = canvas = Canvas(Background = Brushes.LightGray)
        self.clock = Clock().scene
        canvas.Children.Add (self.clock)
        #self.drag = Drag(root=canvas, obj=self.clock.FindName('parentCanvas'))
        self.drag = Drag(root=canvas, obj=self.clock)
        self.drag.enable()

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

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

IronPythonで、Loose XAML を実行する for Google Chrome12

2011-07-08 13:03:44 | DataBinding
IronPythonで、Loose XAMLファイルを実行する。
Google Chrome12 では、読み込むとエラーになるので、作ってみました。

Loose XAML⇒DropShadowInk.xamlDrop-shadows are cool!

LooseXaml.py: LoosXAMLをIronPythonで、ラップしました。
LooseXaml2.py: ローカルファイルのLooseXAMLをIronPythonで、読み込み実行。
LooseXaml3.py: インターネットにあるLooseXAMLをIronPythonで、読み込み実行。

Loosexaml   

#
# LooseXaml.py
# ipy.exe LooseXaml.py
#
import clr
clr.AddReferenceByPartialName("PresentationFramework")
clr.AddReferenceByPartialName("PresentationCore")
from System.Windows import Window, Application
from System.Windows.Markup import XamlReader
xaml_str="""
<Page WindowTitle = "Binding ( Slider, Rectangle )"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid
        Background="OrangeRed" Width="300" Height="300" >
        <Slider x:Name = "MySlider" 
            VerticalAlignment = "Top" 
            Minimum = "10" 
            Maximum = "200" 
            Value = "50" 
            Margin = "10" />
        <Rectangle 
            Width = "{Binding ElementName = MySlider, Path = Value}" 
            Height = "{Binding ElementName = MySlider, Path = Value}" 
            Fill = "Orange"
            VerticalAlignment = "Center"
            HorizontalAlignment = "Center" />
    </Grid>
</Page>
"""
class Window1(Window):
    def __init__(self):
       self.Content = XamlReader.Parse(xaml_str)
if __name__ == "__main__":
    win = Window1()
    Application().Run(win)

#
# LooseXaml2.py
# ipy.exe LooseXaml2.py
#
import clr
clr.AddReferenceByPartialName("PresentationFramework")
clr.AddReferenceByPartialName("PresentationCore")
from System.Windows import Window, Application
from System.Windows.Markup import XamlReader

def LoadXaml(filename):
    from System.IO import *
    from System.Windows.Markup import XamlReader
    f = FileStream(filename, FileMode.Open, FileAccess.Read)
    try:
        element = XamlReader.Load(f)
    finally:
        f.Close()
    return element
class Window1(Window):
    def __init__(self):
       self.Content = LoadXaml("LooseXaml.xaml")
if __name__ == "__main__":
    win = Window1()
    Application().Run(win)

#
# LooseXaml3.py
# ipy.exe LooseXaml3.py
#
import clr
clr.AddReferenceByPartialName("PresentationFramework")
clr.AddReferenceByPartialName("PresentationCore")
from System import Uri, UriKind
from System.Windows import Window, Application
from System.Windows.Markup import XamlReader

def LoadXaml3(strUrl):
    import System
    # Create a request for the URL. 
    request = System.Net.WebRequest.Create(strUrl)
    # If required by the server, set the credentials.
    #request.Credentials = System.Net.CredentialCache.DefaultCredentials
    # Get the response.
    response = request.GetResponse()
    # Get the stream containing content returned by the server.
    dataStream = response.GetResponseStream()
    try:
        element = XamlReader.Load(dataStream)
    finally:
        # Cleanup the streams and the response.
        dataStream.Close()
        response.Close()
    return element

class Window1(Window):
    def __init__(self):
      self.Content = LoadXaml3("http://softgarden.lovepop.jp/myBlog/xaml/dropshadowink.xaml")

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

GitHubにプログラムを投稿しています。参考にしてください。
https://github.com/softgarden137/LooseXAML

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

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スタートブック
辻 真吾
技術評論社