#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <AVIWriter.au3>
#include <File.au3>
#include <ScreenCapture.au3>
#include <SliderConstants.au3>
#include <GUIListBox.au3>
#include <EditConstants.au3>




Global $__SpeedCtrl_Timer = 0
Global $__SpeedCtrl_SleepTime = 0
Global $__SpeedCtrl_Speed = 0

Global $Capture
Global $FramesPerSecMax = 10
Global $_Counter = 0

Global $avi

HotKeySet("^v", "_startvideo")
HotKeySet("{ESC}", "_close")

;_StartAviLibrary()
;$avi = _CreateAvi("C:\Crocoframe\Videos\" & "\crocoframe.avi", 5,200,200)
;Break (0)

DirCreate("C:\Crocoframe\")
DirCreate("C:\Crocoframe\Videos\")
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("CrocoFrame   © CrocoJunk ", 548, 250, 192, 124)
;~ GUISetOnEvent($GUI_EVENT_CLOSE, "_exit")
$Pic1 = GUICtrlCreatePic("pic.jpg", 0, 0, 545, 89, BitOR($SS_NOTIFY, $WS_GROUP, $WS_CLIPSIBLINGS))
$Label1 = GUICtrlCreateLabel("Video aufnehmen", 8, 112, 87, 17)
$Button1 = GUICtrlCreateButton("Play  (STRG+V) ", 120, 112, 100, 25, $WS_GROUP)
;~ GUICtrlSetOnEvent(-1, "_Startvideo")
$Label3 = GUICtrlCreateLabel("Video stoppen", 8, 192, 72, 17)
$Button3 = GUICtrlCreateButton("Video Beenden  (ESC)", 120, 200, 120, 25, $WS_GROUP)
;~ GUICtrlSetOnEvent(-1, "_close")
$check = GUICtrlCreateCheckbox("Video starten nachdem aufgenommen", 256, 120, 199, 17)
$check2 = GUICtrlCreateCheckbox("Video starten nach", 256, 168, 113, 17)
;~ $menu2 = GUICtrlCreateMenu ("Hilfe")
;~ $credits = GUICtrlCreateMenuItem ("Mitwirkende", $menu2)
$input = GUICtrlCreateInput("", 376, 168, 57, 21)
$Label4 = GUICtrlCreateLabel("Sekunden", 448, 168, 53, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
	$nMsg = GUIGetMsg()
	Switch $nMsg
		Case $GUI_EVENT_CLOSE
			Exit

		Case $Button1

			If Not @error Then
				MsgBox(64, "Video", "Das Video startet jetzt!")
			EndIf
			$Capture = True
			_StartAviLibrary()
			$start = String (@HOUR &"_"&@MIN &"_" &  @SEC & ".avi")
			$avi = _CreateAvi("C:\Crocoframe\Videos\" & "\crocoframe" & $start, 24, @DesktopWidth, @DesktopHeight)
;~ 			AdlibRegister("_close", 30000) ;vorsichtshalber Buttons funktionieren nicht Hotkey ESC klappt aber
			ConsoleWrite("STARTED" & @CRLF)
			While $Capture
				If GUIGetMsg() = $Button3 Then
					_close()
				EndIf
 				$m = MouseGetPos()
				$hBmp = _ScreenCapture_Capture("", 0, 0, -1, -1)
				_AddHBitmapToAvi($avi, $hBmp)
				_WinAPI_DeleteObject($hBmp)
 				ConsoleWrite(_SpeedCtrl_Regulate($_Counter) & " FPS" & @CRLF)
 				$_Counter += 1
			WEnd
			$_Counter = 0
			_SpeedCtrl_Clear()
			ConsoleWrite("STOPPED" & @CRLF)
			$Read = GUICtrlRead($check)
			If $Read = 1 Then
				ShellExecute("C:\Crocoframe\Videos\crocoframe"  &$start)
				MsgBox(64, "Achtung", "Video wird abgespielt", 1)

			EndIf

			$2read = Guictrlread ($check2)
			if $2read = 4 Then
			sleep (Guictrlread ($input))
			_startvideo()
			EndIf
;~ 			AdlibUnRegister("_close")



;~ 		Case $item1
;~ 			ShellExecute ("NEU.au3")

;~ 		Case $credits
;~ 			MsgBox (64, "About", " Crocoframe 1.2 "& @CRLF &" Skripted by Crocojunk©")

	EndSwitch
WEnd


Func _startvideo()
	FileDelete("C:\Crocoframe\Videos\crocoframe.avi")
	If Not @error Then
		MsgBox(64, "Video", "Das Video startet jetz!")
	EndIf
	$Capture = True
	_StartAviLibrary()
	$avi = _CreateAvi("C:\Crocoframe\Videos\" & "\crocoframe.avi", 24, @DesktopWidth, @DesktopHeight)
;~     AdlibRegister("_close", 30000) ;vorsichtshalber Buttons funktionieren nicht Hotkey ESC klappt aber
	ConsoleWrite("STARTED" & @CRLF)
	_SpeedCtrl_Start($FramesPerSecMax)
	While $Capture
		If GUIGetMsg() = $Button3 Then
			_close()
		EndIf
 		$m = MouseGetPos()
		$hBmp = _ScreenCapture_Capture("", 0, 0, -1, -1)
		_AddHBitmapToAvi($avi, $hBmp)
		_WinAPI_DeleteObject($hBmp)
;~ 		Sleep(200)
	WEnd
	ConsoleWrite("STOPPED" & @CRLF)
;~     AdlibUnRegister("_close")
EndFunc   ;==>_startvideo





Func _close()
	If IsDeclared("avi") Then
		_CloseAvi($avi)
		_StopAviLibrary()
	EndIf
	ConsoleWrite("Stopped")
	$Capture = False
EndFunc   ;==>_close
Func _exit()
	If IsDeclared("avi") Then
		_CloseAvi($avi)
		_StopAviLibrary()
	EndIf
	$Capture = False
	Exit 0
EndFunc   ;==>_exit


Func _SpeedCtrl_Start($iSpeed)
	If $__SpeedCtrl_Timer <> 0 Then Return SetError(1, 0, -1)
	$__SpeedCtrl_Timer = TimerInit()
	$__SpeedCtrl_SleepTime = 0
	$__SpeedCtrl_Speed = $iSpeed
EndFunc   ;==>_SpeedCtrl_Start

Func _SpeedCtrl_Regulate($sAkPos)
	If Not IsInt($sAkPos) Then Return SetError(1, 0, -1)
	$_Fort = Int(($sAkPos / (TimerDiff($__SpeedCtrl_Timer) / 1000)))

	If $_Fort < $__SpeedCtrl_Speed Then ; Zu kleiner Speed
		$__SpeedCtrl_SleepTime -= 5
	ElseIf $_Fort > $__SpeedCtrl_Speed Then ; Zu großer Speed
		$__SpeedCtrl_SleepTime += 5
	EndIf

	Sleep($__SpeedCtrl_SleepTime)

	If $__SpeedCtrl_SleepTime < 0 Then $__SpeedCtrl_SleepTime = 0
	Return $_Fort
EndFunc   ;==>_SpeedCtrl_Regulate

Func _SpeedCtrl_Clear()
	$__SpeedCtrl_Timer = 0
	$__SpeedCtrl_SleepTime = 0
	$__SpeedCtrl_Speed = 0
EndFunc   ;==>_SpeedCtrl_Clear


