<#
.SYNOPSIS
  main effects library for PowerPresentation script
.DESCRIPTION
  
.NOTES
author: nExoR 2o14
.LINK
http://w-files.pl
#>


<#
.SYNOPSIS
  displays 'to be continue' and wait for the input. 
#>
function nextPage {
    $host.ui.rawui.flushInputBuffer()
    $poz=New-Object System.Management.Automation.Host.Coordinates
    $mws=$host.ui.RawUI.MaxWindowSize
    $poz.X=2
    $poz.Y=$mws.Height-1
    $host.ui.RawUI.CursorPosition=$poz
    write-host -noNewLine -foregroundColor "white" -BackgroundColor "darkBlue" "....to be continued"  
    $key=$host.ui.RawUI.readKey('AllowCtrlC,NoEcho,IncludeKeyUp')
    $host.ui.rawui.flushInputBuffer()
    return $key  

}


###############################################################################
#                              TEXT EFFECTS                                   #
###############################################################################


function writeTextXY {
  param( 
     [parameter(Mandatory=$true,position=0)][System.Xml.XmlElement]$element
  )
  
  $host.ui.rawui.cursorposition=New-Object System.Management.Automation.Host.Coordinates $element.x,$element.y
  write-host $element.innerText
}

function typeText {
  param( 
     [parameter(Mandatory=$true,position=0)][System.Xml.XmlElement]$element
  )

  $s=0
  $timeout=100
  do {
    $host.ui.rawui.cursorposition=New-Object System.Management.Automation.Host.Coordinates $([int]$element.x+$s),$element.y
    write-host $element.innerText[$s++]
    start-sleep -milliseconds $timeout
    if ($host.ui.RawUi.KeyAvailable) {
      $key = $host.ui.RawUI.ReadKey('AllowCtrlC,NoEcho,IncludeKeyUp')
      $host.ui.rawui.flushInputBuffer()
      switch($key.VirtualKeyCode) {
        27 {endPresentation}
        13 {$timeout=0}
      }
          
    }
  } while($s -lt ($element.innerText).length)
}

function flyIn {
  param( 
     [parameter(Mandatory=$true,position=0)][System.Xml.XmlElement]$element
  )

  $rightEdge=[int]$element.x
  $coordinates= New-Object System.Management.Automation.Host.Coordinates 0,$element.y
  $foreground=$host.ui.rawui.ForegroundColor
  $background=$host.ui.rawui.backgroundColor
  $position=0
  while($position++ -lt $rightEdge) {
    $host.ui.rawui.foregroundColor=$background
    $host.ui.rawui.CursorPosition=$coordinates
    write-host $element.innerText
    #placement is not accidental. after clearing old text and before putting a new one on the screen
    if ($host.ui.RawUi.KeyAvailable) {
      $key = $host.ui.RawUI.ReadKey('AllowCtrlC,NoEcho,IncludeKeyUp')
      $host.ui.rawui.flushInputBuffer()
      if($key.VirtualKeyCode -eq 13) {
        $position=$rightEdge
        $coordinates.X=$position
      }
    }
    $host.ui.rawui.foregroundColor=$foreground
    $coordinates.X++
    $host.ui.rawui.CursorPosition=$coordinates
    write-host $element.innerText
    start-sleep -milliseconds 100
  }
}

function colors {
  param( 
    [parameter(Mandatory=$true,position=0)][System.Xml.XmlElement]$element
  )
  $bgcolor="darkgreen"
  $fgcolor="yellow"
  if($element.hasAttribute('bgcolor')) { $bgcolor=$element.bgcolor }
  if($element.hasAttribute('fgcolor')) { $fgcolor=$element.fgcolor }
  $host.ui.rawui.CursorPosition= New-Object System.Management.Automation.Host.Coordinates $element.x,$element.y
  write-host -back $bgcolor -foregroundColor $fgcolor $element.innerText
}

###############################################################################
#                              PAGE EFFECTS                                   #
###############################################################################
function scrollOut {
  param(
    [parameter(Mandatory=$true,position=0)][System.Xml.XmlElement]$pageInfo
  )
  if($pageInfo.hasAttribute('speed')) { $speed=$pageInfo.speed} else {$speed=10}
  $xpos=1
  $ypos=1
  $mod=$script:WindowWidth/$script:windowHeight
  while($xpos++ -lt $script:WindowWidth) {
    if($ypos -lt $script:windowHeight) {$ypos+=$xpos%$mod}
    $host.ui.rawui.WindowSize=new-object System.Management.Automation.Host.Size($xpos,$ypos)
    start-sleep -milliseconds $speed
  }  
   #just in case...  
  $host.ui.rawui.WindowSize=new-object System.Management.Automation.Host.Size($script:WindowWidth,$script:windowHeight)
  
}

function curtainDown {
  param(
    [parameter(Mandatory=$true,position=0)][System.Xml.XmlElement]$pageInfo
  )
  if($pageInfo.hasAttribute('speed')) { $speed=$pageInfo.speed} else {$speed=10}
  $ypos=1
  while($ypos++ -lt $script:WindowHeight) {
    $host.ui.rawui.WindowSize=new-object System.Management.Automation.Host.Size($script:windowWidth,$ypos)
    start-sleep -milliseconds $speed
  }
   #just in case...  
  $host.ui.rawui.WindowSize=new-object System.Management.Automation.Host.Size($script:WindowWidth,$script:windowHeight)
}

#EFFECT OUT - LEAVING CLOSED PRESENTATION WINDOW
function curtainUp {
  param(
    [parameter(Mandatory=$true,position=0)][System.Xml.XmlElement]$pageInfo
  )
  if($pageInfo.hasAttribute('speed')) { $speed=$pageInfo.speed} else {$speed=10}
  $ypos=$script:windowHeight
  while($ypos-- -gt 0) {
    $host.ui.rawui.WindowSize=new-object System.Management.Automation.Host.Size($script:windowWidth,$ypos)
    start-sleep -milliseconds $speed
  }
}
