#include <WindowsConstants.au3>
#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
#include <WinAPIEx.au3>
#include <Sound.au3>
Global $aPoints[5][2], 	$MouseOldX = -10, $MouseOldY = -10, $Poly = 0, $freeze = False
Global $baseX, $baseY, $SetX = -10, $SetY = -10, $dh, $dw, $field_1x, $field_1y, $field_2x, $field_2y, $nx, $ny, $ncx, $ncy, $tree
Global $hGUI, $hGraphic, $hPenRed, $hPenMagentaBold, $hBrushBlue, $hFormat, $hFamily, $hFont
Global $wait = 200, $section, $music, $sSound, $aVual[4][2], $speed, $colortext, $hShem
Global $dl = 5					; размер "плюсика" прицела +-
Global $corX = 0, $corY = 0	; коррекция начала от курсора
FileInstall("Vint_avatar_11_64x64.jpg", @TempDir & "\Vint_avatar_11_64x64.jpg")
FileInstall("Схема.bmp", @TempDir & "\Схема.bmp")
If _WinAPI_GetAsyncKeyState(0x41) = True Then $MouseOldX = -10
If @Compiled Then
	If $CmdLine[0] = 0 Then Exit(1)
	$section = $CmdLine[1]
Else
	$section = 'TreePlant'
EndIf
HotKeySet('{ESC}', '_EndProgramm')
ReadingIni()
Vual()
Shem()
If $music Then
	FileInstall("Kin-dza-dza.mp3", @TempDir & "\Kin-dza-dza.mp3")
	$sSound = _SoundOpen (@TempDir & "\Kin-dza-dza.mp3")
	_SoundPlay($sSound,0)
EndIf
$hGUI = GUICreate('', $field_2x-$field_1x, $field_2y-$field_1y, $field_1x, $field_1y, BitOR($WS_DISABLED, $WS_POPUP), BitOR($WS_EX_TRANSPARENT, $WS_EX_LAYERED, $WS_EX_TOPMOST)) ; BitOR($WS_EX_TRANSPARENT, $WS_EX_LAYERED, $WS_EX_TOPMOST, $WS_EX_TOOLWINDOW)
GUISetBkColor(0xABABAB)
GUISetState(@SW_SHOWNOACTIVATE)
_WinAPI_SetLayeredWindowAttributes($hGUI, '0xABABAB', 0, $LWA_COLORKEY)
_GDIPlus_Startup()
$hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)
$hPenRed = _GDIPlus_PenCreate(0xFFFF0000) ; ручка для красной линии
$hPenMagentaBold = _GDIPlus_PenCreate(0xFFFF00FF, 2) ; ручка для красной точки
$hBrushBlue = _GDIPlus_BrushCreateSolid($colortext) ; синяя кисть
$hFormat = _GDIPlus_StringFormatCreate() ; формат текста
$hFamily = _GDIPlus_FontFamilyCreate('Tahoma') ; шрифт текста
$hFont = _GDIPlus_FontCreate($hFamily, 14) ; размер шрифта
$aPoints[0][0] = 4
Do
	$pos = MouseGetPos() ; получить текущие координыты указателя мыши
	$baseX = $pos[0] - $field_1x + $corX
	$baseY = $pos[1] - $field_1y + $corY
	If $freeze Then
		If _WinAPI_GetAsyncKeyState(0x02) = True Then _EndProgramm() ; Нажата правая кнопка мыши - выход
		If _WinAPI_GetAsyncKeyState(0x01) = False Then        ; Отпущена левая кнопка мыши
			If ($SetX = $baseX) And ($SetY = $baseY) Then
				ProClick()
				Sleep(300)
			EndIf
			$freeze = False
		EndIf
	EndIf
	If Not ($MouseOldX = $baseX And $MouseOldY = $baseY) Then
		$MouseOldX = $baseX
		$MouseOldY = $baseY
		If $Poly = 1 Then
			_WinAPI_RedrawWindow( $hGUI )
		Else
			$Poly = 1
		EndIf
		Draw()
	EndIf
	; проверяем нажатие клавы
	If _WinAPI_GetAsyncKeyState(0x02) = True Then _EndProgramm() ; Нажата правая кнопка мыши - выход
	If _WinAPI_GetAsyncKeyState(0x53) = True And Not $freeze Then IncX() ; Нажата S
	If _WinAPI_GetAsyncKeyState(0x57) = True And Not $freeze Then DecX() ; Нажата W
	If _WinAPI_GetAsyncKeyState(0x44) = True And Not $freeze Then IncY() ; Нажата D
	If _WinAPI_GetAsyncKeyState(0x41) = True And Not $freeze Then DecY() ; Нажата A
	If _WinAPI_GetAsyncKeyState(0x58) = True And Not $freeze Then Tree() ; Нажата X
	; проверяем нажатие мыши
	If _WinAPI_GetAsyncKeyState(0x01) = True And Not $freeze Then        ; Зажата левая кнопка мыши
		$SetX = $baseX
		$SetY = $baseY
		$freeze = True
	EndIf
Until Not Sleep(20)
_EndProgramm()
Func ProClick()
	Local $iX, $iY, $clX, $clY
	For $iY = 0 To $ncy
		For $iX = 0 To $ncx
			$clX = $baseX + ($iY-$iX)*$dw + $field_1x - $corX
			$clY = $baseY + ($iY+$iX)*$dh + $field_1y - $corY
			If $clX > $field_1x And $clX < $field_2x And $clY > $field_1y And $clY < $field_2y Then
				MouseClick("left", $clX, $clY, 1, $speed) ; клик ЛКМ
			EndIf
		Next
	Next
EndFunc   ;==>ProClick
Func ReadingIni()
	$field_1x = IniRead ('TreePlant.ini', $section, 'field_1x', '100')
	$field_1y = IniRead ('TreePlant.ini', $section, 'field_1y', '100')
	$field_2x = IniRead ('TreePlant.ini', $section, 'field_2x', '0')
	$field_2y = IniRead ('TreePlant.ini', $section, 'field_2y', '0')
	$tree = IniRead ('TreePlant.ini', $section, 'tree', 'small')
	$nx = IniRead ('TreePlant.ini', $section, 'nx', '3')
	$ny = IniRead ('TreePlant.ini', $section, 'ny', '4')
	$colortext = IniRead ('TreePlant.ini', $section, 'colortext', 0xFF0000FF)
	$speed = IniRead ('TreePlant.ini', $section, 'speed', '5')
	If $speed < 0 Then $speed = 0
	If $speed > 100 Then $speed = 100
	$run_count = IniRead ('TreePlant.ini', $section, 'run_count', '0')
	$run_count += 1
	IniWrite ('TreePlant.ini', $section, 'run_count', $run_count)
	$music = IniRead ('TreePlant.ini', $section, 'music', 'True')
	If $music And ($run_count/10 - Int($run_count/10) = 0) Then
		$music = True
	Else
		$music = False
	EndIf
	If $field_2x = 0 Then $field_2x = @DesktopWidth-100
	If $field_2y = 0 Then $field_2y = @DesktopHeight-100
	Nc()
	If $tree = 'big' Then
		$dh = 24
		$dw = 48
	Else
		$dh = 16
		$dw = 32
	EndIf
EndFunc   ;==>ReadingIni
Func Tree()
	If $tree = 'small' Then
		$tree = 'big'
		$dh = 24
		$dw = 48
	Else
		$tree = 'small'
		$dh = 16
		$dw = 32
	EndIf
	$MouseOldX = -10
	$MouseOldY = -10
	IniWrite ('TreePlant.ini', $section, 'tree', $tree)
	Sleep($wait)
EndFunc   ;==>Tree
Func IncX()
	If $nx < 18 Then
		$nx += 1
		Nc()
		IniWrite ('TreePlant.ini', $section, 'nx', $nx)
		Sleep($wait)
	EndIf
EndFunc   ;==>IncX
Func IncY()
	If $ny < 38 Then
		$ny += 1
		Nc()
		IniWrite ('TreePlant.ini', $section, 'ny', $ny)
		Sleep($wait)
	EndIf
EndFunc   ;==>IncY
Func DecX()
	If $nx > 1 Then
		$nx -= 1
		Nc()
		IniWrite ('TreePlant.ini', $section, 'nx', $nx)
		Sleep($wait)
	EndIf
EndFunc   ;==>DecX
Func DecY()
	If $ny  > 1 Then
		$ny -= 1
		Nc()
		IniWrite ('TreePlant.ini', $section, 'ny', $ny)
		Sleep($wait)
	EndIf
EndFunc   ;==>DecY
Func Nc()
	$ncx = $nx-1
	$ncy = $ny-1
	If $ncx < 0 Then
		$ncx = 0
		$nx = 1
	EndIf
	If $ncy < 0 Then
		$ncy = 0
		$ny = 1
	EndIf
	$MouseOldX = -10
	$MouseOldY = -10
EndFunc   ;==>Nc
Func Draw()
	Local $iX, $iY, $tLayout
	$aPoints[1][0] = $baseX
	$aPoints[1][1] = $baseY
	$aPoints[2][0] = $baseX - $ncx*$dw
	$aPoints[2][1] = $baseY + $ncx*$dh
	$aPoints[3][0] = $baseX + ($ncy-$ncx)*$dw
	$aPoints[3][1] = $baseY + ($ncx+$ncy)*$dh
	$aPoints[4][0] = $baseX + $ncy*$dw
	$aPoints[4][1] = $baseY + $ncy*$dh
	_GDIPlus_GraphicsDrawPolygon($hGraphic, $aPoints, $hPenRed)
	For $iY = 0 To $ncy
		For $iX = 0 To $ncx
			_GDIPlus_GraphicsDrawEllipse($hGraphic, ($baseX + ($iY-$iX)*$dw)-2, ($baseY + ($iY+$iX)*$dh)-2, 4, 4, $hPenMagentaBold) ; точка
		Next
	Next
	_GDIPlus_GraphicsDrawLine($hGraphic, $baseX, $baseY-$dl-$dh*2, $baseX, $baseY+$dl-$dh*2, $hPenRed)
	_GDIPlus_GraphicsDrawLine($hGraphic, $baseX-$dl, $baseY-$dh*2, $baseX+$dl, $baseY-$dh*2, $hPenRed)
	_GDIPlus_GraphicsDrawLine($hGraphic, $baseX-$dw, $baseY-$dl-$dh, $baseX-$dw, $baseY+$dl-$dh, $hPenRed)
	_GDIPlus_GraphicsDrawLine($hGraphic, $baseX-$dl-$dw, $baseY-$dh, $baseX+$dl-$dw, $baseY-$dh, $hPenRed)
	_GDIPlus_GraphicsDrawLine($hGraphic, $baseX+$dw, $baseY-$dl-$dh, $baseX+$dw, $baseY+$dl-$dh, $hPenRed)
	_GDIPlus_GraphicsDrawLine($hGraphic, $baseX-$dl+$dw, $baseY-$dh, $baseX+$dl+$dw, $baseY-$dh, $hPenRed)
	; координаты для надписи с длиной отрезка
	$tLayout = _GDIPlus_RectFCreate($baseX + 25, $baseY - 12, 0, 0)
	; вывод надписи с размерами
	_GDIPlus_GraphicsDrawStringEx($hGraphic, $nx & ' x ' & $ny & ' ' & $tree, $hFont, $tLayout, $hFormat, $hBrushBlue)
EndFunc   ;==>Draw
Func Vual()
	$aVual[0][0] = False
	$aVual[1][0] = False
	$aVual[2][0] = False
	$aVual[3][0] = False
	If $field_1y > 0 Then
		$aVual[0][0] = True
	EndIf
	If $field_2y < @DesktopHeight Then
		$aVual[1][0] = True
	EndIf
	If $field_1x > 0 Then
		$aVual[2][0] = True
	EndIf
	If $field_2x < @DesktopWidth Then
		$aVual[3][0] = True
	EndIf
	; MsgBox(4096,'@DesktopWidth', '@DesktopWidth  '&@DesktopWidth &@CRLF& '@DesktopHeight  '&@DesktopHeight)
	; BitOR($WS_EX_TRANSPARENT, $WS_EX_LAYERED, $WS_EX_TOPMOST)
	If $aVual[0][0] Then
		$aVual[0][1] = GUICreate("", @DesktopWidth, $field_1y, 0, 0, BitOR($WS_DISABLED, $WS_POPUP), BitOR($WS_EX_TRANSPARENT, $WS_EX_TOOLWINDOW, $WS_EX_TOPMOST))
		GUISetBkColor(0x000000, $aVual[0][1])
		WinSetTrans($aVual[0][1], '', 100)
		GUISetState(@SW_SHOWNOACTIVATE, $aVual[0][1])
	EndIf
	If $aVual[1][0] Then
		$aVual[1][1] = GUICreate("", @DesktopWidth, @DesktopHeight-$field_2y, 0, $field_2y, BitOR($WS_DISABLED, $WS_POPUP), BitOR($WS_EX_TRANSPARENT, $WS_EX_TOOLWINDOW, $WS_EX_TOPMOST))
		GUISetBkColor(0x000000, $aVual[1][1])
		WinSetTrans($aVual[1][1], '', 100)
		GUISetState(@SW_SHOWNOACTIVATE, $aVual[1][1])
	EndIf
	If $aVual[2][0] Then
		$aVual[2][1] = GUICreate("", $field_1x, $field_2y-$field_1y, 0, $field_1y, BitOR($WS_DISABLED, $WS_POPUP), BitOR($WS_EX_TRANSPARENT, $WS_EX_TOOLWINDOW, $WS_EX_TOPMOST))
		GUISetBkColor(0x000000, $aVual[2][1])
		WinSetTrans($aVual[2][1], '', 100)
		GUISetState(@SW_SHOWNOACTIVATE, $aVual[2][1])
	EndIf
	If $aVual[3][0] Then
		$aVual[3][1] = GUICreate("", @DesktopWidth-$field_2x, $field_2y-$field_1y, $field_2x, $field_1y, BitOR($WS_DISABLED, $WS_POPUP), BitOR($WS_EX_TRANSPARENT, $WS_EX_TOOLWINDOW, $WS_EX_TOPMOST))
		GUISetBkColor(0x000000, $aVual[3][1])
		WinSetTrans($aVual[3][1], '', 100)
		GUISetState(@SW_SHOWNOACTIVATE, $aVual[3][1])
	EndIf
	; MsgBox(4096,"Окна затемнения", @DesktopWidth&'  '&$field_1y&'  '&0&'  '&0 &@CRLF& @DesktopWidth&'  '&@DesktopHeight-$field_2y&'  '&0&'  '&$field_2y &@CRLF& $field_1x&'  '&$field_2y-$field_1y&'  '&0&'  '&$field_1y &@CRLF& @DesktopWidth-$field_2x&'  '&$field_2y-$field_1y&'  '&$field_2x&'  '&$field_1y)
EndFunc   ;==>Vual
Func Shem()
	;If @DesktopWidth-$field_2x < 130 Then Return
	$hShem = GUICreate("", 274, 130, @DesktopWidth-274, @DesktopHeight-130-30, BitOR($WS_DISABLED, $WS_POPUP), BitOR($WS_EX_TRANSPARENT, $WS_EX_TOOLWINDOW, $WS_EX_TOPMOST))
	WinSetTrans($hShem, '', 250)
	GUICtrlCreatePic(@TempDir & "\\Схема.bmp", 0, 0, 0, 0)
	GUISetState(@SW_SHOWNOACTIVATE, $hShem)
EndFunc   ;==>Shem
Func _EndProgramm()
	Local $FormEnd, $i, $hParent, $SoundCancel
	; Стоп и удаляем музыку
	If $music Then
		_SoundStop($sSound)
		_SoundClose($sSound)
		FileDelete(@TempDir & "\Kin-dza-dza.mp3")
	EndIf
	GUIDelete($hShem)
	FileInstall("Схема.bmp", @TempDir & "\Схема.bmp")
	; Удаляем затемнения
	For $i = 0 To 3
		If $aVual[$i][0] Then GUIDelete($aVual[$i][1])
	Next
	; удаление ресурсов
	FileDelete(@TempDir & "\Схема.bmp")
	_GDIPlus_FontDispose($hFont)
	_GDIPlus_FontFamilyDispose($hFamily)
	_GDIPlus_StringFormatDispose($hFormat)
	_GDIPlus_BrushDispose($hBrushBlue)
	_GDIPlus_PenDispose($hPenMagentaBold)
	_GDIPlus_GraphicsDispose($hGraphic)
	_GDIPlus_Shutdown()
	GUIDelete($hGUI)
	$hParent  = GUICreate('')
	$FormEnd = GUICreate("О программе", 200, 160, -1, -1, BitOR($WS_DISABLED, $WS_POPUP), $WS_EX_TOPMOST, $hParent)
	GUICtrlCreatePic(@TempDir & "\Vint_avatar_11_64x64.jpg", 68, 8, 64, 64)
	GUICtrlCreateLabel("Autor Vint", 68, 80, 70, 20)
	GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
	GUICtrlSetColor(-1, 0x800080)
	GUICtrlCreateLabel("version 3.0.3 of 13.02.2015", 46, 95, 125, 17)
	GUICtrlSetFont(-1, 8, 400, 0, "Tahoma")
	GUICtrlCreateLabel("Посадка деревьев", 54, 116, 100, 17)
	GUICtrlCreateLabel("для ZF", 84, 130, 82, 17)
	WinSetTrans($FormEnd, '', 0) ; прозрачность 30
	GUISetBkColor(0xDCF2CA, $FormEnd)
	GUISetState(@SW_SHOW)
	For $i = 0 To 255 Step 8
		WinSetTrans($FormEnd, '', $i)
		Sleep(1)
	Next
	$i = 1024
	While $i >= 0
		If $i < 256 Then WinSetTrans($FormEnd, '', $i)
		$nMsg = GUIGetMsg()
		Switch $nMsg
			Case $GUI_EVENT_CLOSE
				Exit
		EndSwitch
		Sleep(1)
		$i -= 8
	WEnd
	; удаление оставшихся ресурсов
	$SoundCancel = _SoundOpen (@TempDir & "\cancel4.mp3")
	_SoundPlay($SoundCancel,1)
	_SoundClose($SoundCancel)
	FileDelete(@TempDir & "\cancel4.mp3")
	FileDelete(@TempDir & "\Vint_avatar_11_64x64.jpg")
	Exit
EndFunc ;==>_EndProgramm