Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- New-Window -AsJob -Title "ShowUI - Turtle Graphics" -WindowStartupLocation "CenterScreen" -Width 500 -Height 500 -On_Loaded {
- . $Window.Resources.Code | Import-Module
- Set-Window $Window
- } -Resource @{
- Code = {
- New-Module TurtleGraphics {
- [double] $Script:X = 0
- [double] $Script:Y = 0
- [bool] $Script:PenDown = $false
- $Script:Stroke = [System.Windows.Media.Brushes]::Violet
- $Script:StrokeThickness = 2
- $Script:Fill = [System.Windows.Media.Brushes]::LightBlue
- $Script:Window = ""
- $Script:PointCollection = New-Object System.Windows.Media.PointCollection
- function Set-Window ($Window) {$Script:Window = $Window}
- function Set-PenWidth ($StrokeThickness) {$Script:StrokeThickness = $StrokeThickness}
- function Set-PenColor ($Color) { $Script:Stroke = [System.Windows.Media.Brushes]::$color }
- function Set-FillColor ($Color) { $Script:Fill = [System.Windows.Media.Brushes]::$color }
- function New-Point {
- param([double]$x, [double]$y)
- New-Object System.Windows.Point $x, $y
- }
- function PenDown {
- $Script:PointCollection.Add((New-Point $X $y))
- $Script:PenDown = $true
- }
- function PenUp {
- if($Script:PointCollection.Count -ge 2) {
- $Polyline = New-Polyline
- $PointCollection | ForEach { $Polyline.Points.Add($_) }
- $Polyline.Stroke = $Script:Stroke
- $Polyline.StrokeThickness = $Script:StrokeThickness
- $Polyline.Fill = $Script:Fill
- $TheCanvas = $Script:Window | Get-ChildControl -ByName TheCanvas
- $TheCanvas.Children.Add($Polyline)
- }
- $Script:PointCollection.Clear()
- $Script:PenDown = $false
- }
- function MoveTo ($X, $Y) {
- if($Script:PenDown) {
- $Script:PointCollection.Add((New-Point $X $y))
- }
- $Script:X = $X
- $Script:Y = $Y
- }
- function ClearCanvas {
- $TheCanvas = $Script:Window | Get-ChildControl -ByName TheCanvas
- $TheCanvas.Children.Clear()
- $Script:X = 0
- $Script:Y = 0
- }
- }
- }
- } {
- $tbStype = @{
- Margin = 5
- VerticalScrollBarVisibility = "Auto"
- AcceptsReturn = $true
- HorizontalScrollBarVisibility = "Auto"
- }
- $targetScript = @"
- ClearCanvas
- Set-PenWidth 3
- function Do-It (`$x, `$y, `$max) {
- PenUp
- MoveTo `$x `$y
- PenDown
- MoveTo `$max `$y
- MoveTo `$max `$max
- MoveTo `$x `$max
- MoveTo `$x `$y
- PenUp
- }
- 1..10 | ForEach { `$y=`$x=10; `$max=230 } {
- `$y=`$x+=10; `$max-=10
- Do-It `$x `$y `$max
- }
- "@
- Grid -Columns 2 -Rows 100*, Auto {
- Canvas -Row 0 -Column 1 -RowSpan 2 -Name TheCanvas
- TextBox -Row 0 -Column 0 -Name TB -Text $targetScript @tbStype
- StackPanel -Orientation Horizontal -Row 1 -Column 0 {
- Button _Run -Margin 5 -MinWidth 75 -On_Click {
- $TB.Text | Invoke-Expression
- }
- Button _Clear -Margin 5 -MinWidth 75 -On_Click { ClearCanvas }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment