;Author: Kanashius@autoitscript.com, Kanashius@autoit.de, https://www.kanashius.com
#include "WallpaperUDF.au3"
#include <StaticConstants.au3>

; initialize own tray
__Wallpaper_InitDefaultTray()

; animation variables
Global Const $fDeg = ACos(-1) / 180 ;ACos(-1) is nearly pi
Global Const $iDots = 7, $fAngelDist = 360 / $iDots, $iRadius = 250
Global $aCoordinates[$iDots][2], $bIsFirst = True
Global $iX_Center, $iY_Center, $fAngle = 0

; fps-counter variables
Global $iFPSTextWidth = 100, $iFPSTextSize = 16, $iTimer, $iFrames = 0, $sFps = "FPS: 0"
Global $hPen, $hFamily, $hFont, $hFormat, $hBrush

; create gui
Global $iWidth = 180, $iHeight = 125
$hGui = GUICreate("Wallpaper UDF example", $iWidth, $iHeight, 10, 10)
$idLabelFps = GUICtrlCreateLabel($sFps, 10, 10, $iWidth-20, Default, $SS_CENTER)
$idBtnPR = GUICtrlCreateButton("Pause/Resume", 10, 30, $iWidth-20)
$idBtnClear = GUICtrlCreateButton("Clear", 10, 60, $iWidth-20)
$idBtnExit = GUICtrlCreateButton("Exit", 10, 90, $iWidth-20)
GUISetState(@SW_SHOW, $hGui)

; start the wallpaper drawing loop
__Wallpaper_Start(_draw, _init, _shutdown, _process)
If @error Then MsgBox(16, "Error", "Something went wrong starting initializing the wallpaper: "&@error)

Func _draw(ByRef $hGraphics, $iWidth, $iHeight, ByRef $arScreenInfo) ; will be called for each drawing cicle.
	; Draw the animation
	For $i = 0 To $iDots - 1
			$aCoordinates[$i][0] = $iX_Center+Cos($fAngle*$fDeg)*$iRadius
			$aCoordinates[$i][1] = $iY_Center+Sin($fAngle*$fDeg)*$iRadius
			If Not $bIsFirst Then
				_GDIPlus_PenSetColor($hPen, 0xFFFFFF00)
				_GDIPlus_PenSetWidth($hPen, 2)
				_GDIPlus_GraphicsDrawLine($hGraphics, $aCoordinates[$i][0], $aCoordinates[$i][1], _
								$aCoordinates[Mod(($i + $iDots / 2), $iDots)][0], $aCoordinates[Mod(($i + $iDots / 2), $iDots)][1], $hPen)
				_GDIPlus_PenSetColor($hPen, 0xFFFF8000)
				_GDIPlus_PenSetWidth($hPen, 3)
				If $i < $iDots - 1 Then _GDIPlus_GraphicsDrawLine($hGraphics, $aCoordinates[$i][0], $aCoordinates[$i][1], $aCoordinates[$i + 1][0], $aCoordinates[$i + 1][1], $hPen) ;;draw outer lines
			EndIf
			$fAngle += $fAngelDist
	Next
	If Not $bIsFirst Then _GDIPlus_GraphicsDrawLine($hGraphics, $aCoordinates[$i - 1][0], $aCoordinates[$i - 1][1], $aCoordinates[0][0], $aCoordinates[0][1], $hPen)
	$bIsFirst = False
	$fAngle -= 0.5

	; update fps
	If TimerDiff($iTimer)>1000 Then
		_updateFps($iFrames)
		$iFrames = 0
		$iTimer = TimerInit()
	EndIf
	$iFrames += 1
	; draw fps
	$tLayout = _GDIPlus_RectFCreate($iX_Center - $iFPSTextWidth, $iY_Center - 4, $iFPSTextWidth*2, $iFPSTextSize+4)
	$aInfo = _GDIPlus_GraphicsMeasureString($hGraphics, $sFps, $hFont, $tLayout, $hFormat)
	_GDIPlus_GraphicsDrawStringEx($hGraphics, $sFps, $hFont, $aInfo[0], $hFormat, $hBrush)

	; Return redraw areas
	Local $arAreas = [[$iX_Center-$iRadius,$iY_Center-$iRadius,$iX_Center+$iRadius,$iY_Center+$iRadius]] ; Set the areas that were redrawn and should be replaced
	return SetExtended(0, $arAreas) ; Change extended to the (min) time in ms to wait between each redraw. (This does not influence _process)
EndFunc

Func _updateFps($iFps)
	$sFps = "FPS: "&$iFps
	GUICtrlSetData($idLabelFps, $sFps)
EndFunc

Func _process()
	$iMsg = GUIGetMsg()
	Switch $iMsg
		Case -3, $idBtnExit
			GUIDelete($hGui)
			__Wallpaper_Exit()
		Case $idBtnPR
			__Wallpaper_PauseResume()
			_updateFps(0)
		Case $idBtnClear
			__Wallpaper_Clear()
			_updateFps(0)
	EndSwitch
EndFunc

Func _init($iWidth, $iHeight, ByRef $arScreenInfo) ; The initialization method is called once before the draw loop starts.
	If UBound($arScreenInfo) = 0 Then
		MsgBox(16, "Error", "It is difficult to draw a wallpaper without a screen.")
		Exit
	EndIf
	$iTimer = TimerInit()
	$iX_Center = $arScreenInfo[0][0]+$arScreenInfo[0][2]/2
	$iY_Center = $arScreenInfo[0][1]+$arScreenInfo[0][3]/2
	$hPen = _GDIPlus_PenCreate(0xFFFF0000, 4)
	$hFamily = _GDIPlus_FontFamilyCreate("Arial")
	$hFont = _GDIPlus_FontCreate($hFamily, $iFPSTextSize)
	$hFormat = _GDIPlus_StringFormatCreate(0x4000)
	_GDIPlus_StringFormatSetAlign($hFormat, 1)
	$hBrush = _GDIPlus_BrushCreateSolid(0xFFFF0000)
EndFunc

Func _shutdown() ; The shutdown method is called at the end of the draw loop, after __Wallpaper_Exit was called.
    _GDIPlus_BrushDispose($hBrush)
    _GDIPlus_StringFormatDispose($hFormat)
    _GDIPlus_FontDispose($hFont)
    _GDIPlus_FontFamilyDispose($hFamily)
	_GDIPlus_PenDispose($hPen)
EndFunc