Skip to content

Instantly share code, notes, and snippets.

@y-ack y-ack/EditGrid.ps1
Created Mar 14, 2019

Embed
What would you like to do?
TextADinosaur
using namespace System.Collections.Generic
using namespace System.Management.Automation.Host
using module .\Widget.psm1
class EditGrid : Widget
{
[BufferCell[,]]$Grid
[Coordinates]$Cursor
[Bool]$CursorOn = $True
#region constructor
EditGrid([UInt32]$X, [UInt32]$Y, [UInt32]$Width, [UInt32]$Height)
{
$This.init([Rectangle]::new($X, $Y, $X + $Width, $Y + $Height))
}
EditGrid([Rectangle]$Position)
{
$This.init($Position)
}
EditGrid([Widget]$Clone)
{
$This = $Clone
$This.GUID = New-Guid
}
EditGrid() # PowerShell 5.1.14393.1944 bug: inheritance requires default constructor
{
}
init([Rectangle]$Position)
{
$This.Position = $Position
$This.GUID = New-Guid
$This.Children = [List[Widget]]::new()
$this.Grid = $global:Host.Ui.RawUI.NewBufferCellArray($This.GetWidth(),$This.GetHeight(),$This.BlankChar)
$This.Cursor = [Coordinates]::new(0,0)
}
#endregion
[BufferCell[,]]GetBufferCellArray()
{
[BufferCell[,]]$buffer = $This.Grid.Clone()
$IsCursor = if($This.CursorOn){[ConsoleColor]::DarkCyan}else{$This.BackgroundColor}
Set-BufferCell $This.Grid $This.Cursor.X $This.Cursor.Y -BackgroundColor $IsCursor
$This.CopyChildBuffers($buffer)
return $buffer
}
[void]Background([guid]$focused)
{
# Write-Host "got background call " + ([datetime]::Now)
}
[void]HandleKey([ConsoleKeyInfo]$Key, [guid]$focused)
{
if ($This.guid -eq $focused)
{
if (('LeftArrow','RightArrow','UpArrow','DownArrow') -contains $Key.Key)
{
$This.MoveCursor($Key.Key)
} else
{
$This.SetChar($Key.KeyChar)
}
}
# Detect rising focus edge
if ($This.LastFocusState -eq $false)
{
$this.OnFocus()
}
if ($This.Children.Count -gt 0 -and $This.guid -ne $focused)
{
$This.Children[$This.SelectFocusedChild($focused)].HandleKey($Key, $focused)
}
$This.FocusEdge = $false
}
[void]MoveCursor([ConsoleKey]$Key)
{
Set-BufferCell $This.Grid $This.Cursor.X $This.Cursor.Y -BackgroundColor $This.BackgroundColor
if($Key -eq 'UpArrow')
{
$This.Cursor = [Coordinates]::new(
$This.Cursor.X,
[Math]::Max(0,$This.Cursor.Y-1))
}
elseif($Key -eq 'DownArrow')
{
$This.Cursor = [Coordinates]::new(
$This.Cursor.X,
[Math]::Min($This.GetHeight()-1,$This.Cursor.Y+1))
}
elseif($Key -eq 'LeftArrow')
{
$This.Cursor = [Coordinates]::new(
[Math]::Max(0,$This.Cursor.X-1),
$This.Cursor.Y)
}
elseif($Key -eq 'RightArrow')
{
$This.Cursor = [Coordinates]::new(
[Math]::Min($This.GetWidth()-1,$This.Cursor.X+1),
$This.Cursor.Y)
}
}
[void]SetChar([Char]$Character)
{
Set-BufferCell $This.Grid $This.Cursor.X $This.Cursor.Y $Character
}
[void]OnFocus()
{
#$this.CursorOn = $true
$this.OnFocusCallbacks | ForEach-Object {
$_.Invoke()
}
}
[void]OnUnFocus()
{
#$this.CursorOn = $false
$this.OnUnfocusCallbacks | ForEach-Object {
$_.Invoke()
}
}
}
using module ..\..\Aster\
using module ..\..\Aster\Classes\EditGrid.psm1
using module ..\..\Aster\Classes\Controller.psm1
using module ..\..\Aster\Classes\Window.psm1
$Controller = [Controller]::new()
$PaintContainer = [Window]::new(0,0,52,26,"Color A Dinosaur")
$Paint = [EditGrid]::new(1,1,50,24)
$PaintContainer.AddWidget($Paint)
$Controller.AddWidget(@($PaintContainer), $true)
$Controller.focused = 1
$Controller.Start({})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.