WPF/IronPythonで、ViewboxUnits を Absolute で Tile を実行する。
ImageBrush のViewBoxの使い方に慣れてきました。
つぎのBlogを参考にさせて頂きました。
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.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)
# 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スタートブック | |
辻 真吾 | |
技術評論社 |