#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=Binäruhr.ico
#AutoIt3Wrapper_Outfile_x64=Binäruhr.exe
#AutoIt3Wrapper_Res_Fileversion=1.0.0.2
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <GDIPlus.au3>


HotKeySet("{ESC}", "_Binaeruhr_Terminate")


Global $x_schwarz = 0x000000, $x_on = 0xF61A00, $x_off = 0xF0F0F0, $s_type = _FileGetType()
Global $a_ellipse_hour[5][2] = [[115, 380], [115, 300], [115, 220], [35, 380], _
								[35, 300]]
Global $a_ellipse_min[6][2] = [[300, 380], [300, 300], [300, 220], [220, 380], _
								[220, 300], [220, 220]]
Global $a_ellipse_sec[6][2] = [[480, 380], [480, 300], [480, 220], [400, 380], _
								[400, 300], [400, 220]]


Global $a_length[3] = [6, 6, 5]
Global $a_SMHellipse[3] = [$a_ellipse_sec, $a_ellipse_min, $a_ellipse_hour]

_GDIPlus_StartUp()

Global $h_stift = _GDIPlus_PenCreate(0xFF000000 + $x_schwarz, 1)
Global $h_on = _GDIPlus_BrushCreateSolid(0xFF000000 + $x_on)
Global $h_off = _GDIPlus_BrushCreateSolid(0xFF000000 + $x_off)

Global $h_GUI = GUICreate("Binäruhr", 600, 480, -1, -1, BitOR($WS_MINIMIZEBOX,$WS_POPUP,$WS_GROUP),$WS_EX_TOPMOST)
GUISetOnEvent(-3, "_Binaeruhr_Terminate")
GUISetBkColor($x_schwarz, $h_GUI)
Global $h_graphic =  _GDIPlus_GraphicsCreateFromHWND($h_GUI)
_GDIPlus_GraphicsSetSmoothingMode($h_graphic, 2)
Global $h_UhrzeitDigital = GUICtrlCreateLabel(@HOUR & ":" & @MIN & ":" & @SEC, 45, 50, 500, 150)
GUICtrlSetBkColor($h_UhrzeitDigital, $GUI_BKCOLOR_TRANSPARENT)
GUICtrlSetFont($h_UhrzeitDigital, 95, 400, 0, "Arial")
GUICtrlSetColor($h_UhrzeitDigital, $x_on)
GUISetCursor(16, 1, $h_GUI)

Global $h_trans = GUICreate("", @DesktopWidth + 5, @DesktopHeight + 5, -5, -5, $WS_POPUP, $DS_MODALFRAME)
GUISetBkColor($x_schwarz)
WinSetTrans($h_trans, "", 100)
GUISetCursor(16, 1, $h_trans)

GUISetState(@SW_SHOW, $h_trans)
GUISetState(@SW_SHOW, $h_GUI)


If $s_type = "scr" Then
	Global $a_pos_start = MouseGetPos(), $a_pos_run
	While True
		_Binaeruhr_Update()
		Sleep(100)
		$a_pos_run = MouseGetPos()
		If $a_pos_run[0] <> $a_pos_start[0] Or $a_pos_run[1] <> $a_pos_start[1] Then _Binaeruhr_Terminate()
	WEnd
Else
	While True
		_Binaeruhr_Update()
		Sleep(100)
	WEnd
EndIf

Func _Binaeruhr_Terminate()
	_GUIFadeInOut($h_GUI, False, 8)
	GUIDelete($h_GUI)
	GUIDelete($h_trans)
	_GDIPlus_GraphicsDispose($h_graphic)
	_GDIPlus_PenDispose($h_stift)
	_GDIPlus_BrushDispose($h_on)
	_GDIPlus_BrushDispose($h_off)
	_GDIPlus_Shutdown()
	Exit
EndFunc

Func _Binaeruhr_Update()
	If GUICtrlRead($h_UhrzeitDigital) <> @HOUR & ":" & @MIN & ":" & @SEC Then GUICtrlSetData($h_UhrzeitDigital, @HOUR & ":" & @MIN & ":" & @SEC)

	Local $a_SMH[3] = [@SEC, @MIN, @HOUR]
	Local $b_time, $a_time

	For $x = 0 To 2
		$b_time = _IntegerToBinary($a_SMH[$x])
		$b_time = _BinaerstringAuffuellen($b_time, $a_length[$x])
		$a_time =  StringSplit($b_time, "")
		$a_time = _ArrayElementeTauschen($a_time)
		_PunkteFuellen($a_time, $a_SMHellipse[$x])
	Next
EndFunc

Func _PunkteFuellen($a_array, $a_ellipse)
	For $i = 1 To $a_array[0]
		If $a_array[$i] = 1 Then
			Local $h_farbe = $h_on
		Else
			Local $h_farbe = $h_off
		EndIf
		_GDIPlus_GraphicsFillEllipse($h_graphic, $a_ellipse[$i - 1][0], $a_ellipse[$i - 1][1], 70, 70, $h_farbe)
		_GDIPlus_GraphicsDrawEllipse($h_graphic, $a_ellipse[$i - 1][0], $a_ellipse[$i - 1][1], 70, 70, $h_stift)
	Next
EndFunc

Func _IntegerToBinary($v_number)
	$b_binary = ""
	Do
		$b_binary = Mod($v_number, 2) & $b_binary
		$v_number = Floor($v_number/2)
	Until $v_number = 0
	Return $b_binary
EndFunc

Func _ArrayElementeTauschen($a_array)
	Local $v_rubbish, $i_ubound

	If Mod($a_array[0], 2) = 0 Then
		$i_ubound = ($a_array[0] / 2)
	Else
		$i_ubound = (($a_array[0] - 1) / 2)
	EndIf

	For $i = 1 To $i_ubound
		$v_rubbish = $a_array[$i]
		$a_array[$i] = $a_array[$a_array[0] - $i + 1]
		$a_array[$a_array[0] - $i + 1] = $v_rubbish
	Next
	Return $a_array
EndFunc

Func _BinaerstringAuffuellen($s_binary, $i_length)
	If StringLen($s_binary) < $i_length Then
		Do
			$s_binary = "0" & $s_binary
		Until StringLen($s_binary) = $i_length
	EndIf
	Return $s_binary
EndFunc

Func _GUIFadeInOut($hGUI, $bFadeIn = True, $iSpeed = 12) ; by Marsi
    If Not (IsHWnd($hGUI) And IsBool($bFadeIn) And ($iSpeed > 0)) Then Return SetError(-1 * (Not IsHWnd($hGUI)) - 2 * (Not IsBool($bFadeIn)) - 4 * (Not ($iSpeed > 0)))
    If $bFadeIn Then GUISetState(@SW_SHOW + 0 * WinSetTrans($hGUI, "", 255 * (Not $bFadeIn)), $hGUI)
    For $i = 0 To 255 Step $iSpeed
        WinSetTrans($hGUI, "", (2 * $i - 255) * $bFadeIn + (255 - $i) + 0 * Sleep(10))
    Next
    WinSetTrans($hGUI, "", 255 * $bFadeIn) ; Korrigiert Fehler, wenn die Schleife nicht bei 0 oder 255 endet.
    If Not $bFadeIn Then GUISetState(@SW_HIDE, $hGUI)
EndFunc

Func _FileGetType($s_path = @ScriptFullPath)
	If StringRegExp(StringLeft($s_path, 1), "[a-zA-Z]") And StringMid($s_path, 2, 1) = ":" Then
		Local $a_path = StringSplit($s_path, ".")
		Return $a_path[$a_path[0]]
	Else
		Return SetError(-1)
	EndIf
EndFunc


