DougFinke

TurtleGraphics

Jul 29th, 2011
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. New-Window -AsJob -Title "ShowUI - Turtle Graphics" -WindowStartupLocation "CenterScreen" -Width 500 -Height 500 -On_Loaded {
  2.     . $Window.Resources.Code | Import-Module
  3.     Set-Window $Window
  4. } -Resource @{
  5.     Code = {
  6.         New-Module TurtleGraphics {
  7.             [double] $Script:X = 0
  8.             [double] $Script:Y = 0
  9.             [bool]   $Script:PenDown = $false
  10.            
  11.             $Script:Stroke = [System.Windows.Media.Brushes]::Violet
  12.             $Script:StrokeThickness = 2
  13.             $Script:Fill = [System.Windows.Media.Brushes]::LightBlue
  14.             $Script:Window = ""
  15.            
  16.             $Script:PointCollection = New-Object System.Windows.Media.PointCollection
  17.  
  18.             function Set-Window    ($Window) {$Script:Window = $Window}
  19.             function Set-PenWidth  ($StrokeThickness) {$Script:StrokeThickness = $StrokeThickness}
  20.             function Set-PenColor  ($Color) { $Script:Stroke = [System.Windows.Media.Brushes]::$color }
  21.             function Set-FillColor ($Color) { $Script:Fill = [System.Windows.Media.Brushes]::$color }
  22.            
  23.             function New-Point {
  24.                 param([double]$x, [double]$y)
  25.                 New-Object System.Windows.Point $x, $y
  26.             }
  27.  
  28.             function PenDown {
  29.                 $Script:PointCollection.Add((New-Point $X $y))
  30.                 $Script:PenDown = $true
  31.             }
  32.  
  33.             function PenUp {
  34.                 if($Script:PointCollection.Count -ge 2) {
  35.                     $Polyline = New-Polyline
  36.  
  37.                     $PointCollection | ForEach { $Polyline.Points.Add($_) }
  38.  
  39.                     $Polyline.Stroke = $Script:Stroke
  40.                     $Polyline.StrokeThickness = $Script:StrokeThickness
  41.                     $Polyline.Fill = $Script:Fill
  42.  
  43.                     $TheCanvas = $Script:Window | Get-ChildControl -ByName TheCanvas
  44.                     $TheCanvas.Children.Add($Polyline)
  45.                 }
  46.  
  47.                 $Script:PointCollection.Clear()
  48.                 $Script:PenDown = $false
  49.             }
  50.  
  51.             function MoveTo ($X, $Y) {    
  52.                 if($Script:PenDown) {
  53.                     $Script:PointCollection.Add((New-Point $X $y))
  54.                 }
  55.  
  56.                 $Script:X = $X
  57.                 $Script:Y = $Y
  58.             }
  59.  
  60.             function ClearCanvas {
  61.                 $TheCanvas = $Script:Window | Get-ChildControl -ByName TheCanvas
  62.                 $TheCanvas.Children.Clear()
  63.                 $Script:X = 0
  64.                 $Script:Y = 0
  65.             }    
  66.         }
  67.     }
  68. } {    
  69.     $tbStype = @{
  70.         Margin = 5
  71.         VerticalScrollBarVisibility = "Auto"
  72.         AcceptsReturn = $true
  73.         HorizontalScrollBarVisibility = "Auto"
  74.     }
  75.    
  76.     $targetScript = @"
  77. ClearCanvas
  78.  
  79. Set-PenWidth 3
  80.  
  81. function Do-It (`$x, `$y, `$max) {
  82.    PenUp
  83.    MoveTo `$x `$y
  84.  
  85.    PenDown
  86.    MoveTo `$max `$y
  87.    MoveTo `$max `$max
  88.    MoveTo `$x `$max
  89.    MoveTo `$x `$y
  90.  
  91.    PenUp
  92. }
  93.  
  94. 1..10 | ForEach { `$y=`$x=10; `$max=230 } {
  95.    `$y=`$x+=10; `$max-=10
  96.    Do-It `$x `$y `$max
  97. }
  98.  
  99. "@    
  100.     Grid -Columns 2 -Rows 100*, Auto {
  101.         Canvas  -Row 0 -Column 1 -RowSpan 2 -Name TheCanvas
  102.         TextBox -Row 0 -Column 0 -Name TB -Text $targetScript @tbStype
  103.        
  104.         StackPanel -Orientation Horizontal -Row 1 -Column 0 {
  105.             Button _Run -Margin 5 -MinWidth 75 -On_Click {
  106.                 $TB.Text | Invoke-Expression
  107.             }
  108.            
  109.             Button _Clear -Margin 5 -MinWidth 75 -On_Click { ClearCanvas }
  110.         }
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment