#include <GDIPlus.au3>
#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <Misc.au3>

Opt("GUIOnEventMode", 1)

$iGUIColorBG = 0xFFFFFFFF
$iGUIWidth = 400
$iGUIHeight = 400

$hWndMain = GUICreate("Springy by name22 (autoit.de)", $iGUIWidth, $iGUIHeight)
GUISetState()

$hWndSettings = GUICreate("Einstellungen", 190, 180, 192, 124)
$cSpringNr = GUICtrlCreateInput("Feder Nr", 8, 8, 81, 21, 0x2000)
GUICtrlSetLimit($cSpringNr, 2, 1)
$cSpringNrUD = GUICtrlCreateUpdown($cSpringNr)
GUICtrlSetLimit($cSpringNrUD, 10, 1)
$cLabel1 = GUICtrlCreateLabel("Farbe:", 8, 48, 34, 17)
$cColorShow = GUICtrlCreateLabel("", 8, 72, 71, 25)
$cState = GUICtrlCreateCheckbox("Aktiv", 136, 16, 49, 17)
$cStrength = GUICtrlCreateInput("", 8, 136, 81, 21, 0x2000)
GUICtrlSetLimit($cStrength, 3, 1)
$cLabel2 = GUICtrlCreateLabel("Stärke:", 8, 112, 36, 17)

_GDIPlus_Startup()

$hGraphic = _GDIPlus_GraphicsCreateFromHWND($hWndMain)
$hBitmap = _GDIPlus_BitmapCreateFromGraphics($iGUIWidth, $iGUIHeight, $hGraphic)
$hBuffer = _GDIPlus_ImageGetGraphicsContext($hBitmap)
_GDIPlus_GraphicsSetSmoothingMode($hBuffer, 2)

$hFormatNumber = _GDIPlus_StringFormatCreate()
$hFamilyNumbers = _GDIPlus_FontFamilyCreate("Arial")
$hFontNumbers = _GDIPlus_FontCreate($hFamilyNumbers, 14)

$hBrushNumbers = _GDIPlus_BrushCreateSolid(0xFF000000)

GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit", $hWndMain)
GUISetOnEvent($GUI_EVENT_RESTORE, "WM_PAINT", $hWndMain)

GUIRegisterMsg($WM_PAINT, "WM_PAINT")

$iX = 200
$iY = 200
$vX = 0
$vY = 0
$Resistance = 0.93

If FileExists(@ScriptDir & "\Data") Then
	$aSpringCoords = _LoadFromFile()
Else
	Global $aSpringCoords[11][5]
	$aSpringCoords[0][0] = 10

	For $i = 1 To $aSpringCoords[0][0]
		$aSpringCoords[$i][0] = Random(0, $iGUIWidth, 1)
		$aSpringCoords[$i][1] = Random(0, $iGUIHeight, 1)
		$aSpringCoords[$i][2] = _GDIPlus_PenCreate(0xFF000000, 2)
		$aSpringCoords[$i][3] = True
		$aSpringCoords[$i][4] = 30
	Next
EndIf

HotKeySet("^s", "_ShowSettings")

While Sleep(10)
	$iX += $vX
	$iY += $vY
	$vX *= $Resistance
	$vY *= $Resistance

	_GDIPlus_GraphicsClear($hBuffer, $iGUIColorBG)

	$aMousePos = GUIGetCursorInfo($hWndMain)
	If _IsPressed("01") Then
		$iX = $aMousePos[0]
		$iY = $aMousePos[1]
	EndIf
	For $i = 30 To 39
		If _IsPressed($i) Then
			$aSpringCoords[$i - 29][0] = $aMousePos[0]
			$aSpringCoords[$i - 29][1] = $aMousePos[1]
		EndIf
	Next
	For $i = 1 To $aSpringCoords[0][0]
		If $aSpringCoords[$i][3] Then
			$vX += ($aSpringCoords[$i][0] - $iX) / (100 - $aSpringCoords[$i][4])
			$vY += ($aSpringCoords[$i][1] - $iY) / (100 - $aSpringCoords[$i][4])
			_GDIPlus_GraphicsDrawLine($hBuffer, $aSpringCoords[$i][0], $aSpringCoords[$i][1], $iX, $iY, $aSpringCoords[$i][2])
			$aInfo = _GDIPlus_GraphicsMeasureString($hBuffer, $i - 1, $hFontNumbers, _GDIPlus_RectFCreate(), $hFormatNumber)
			$iWidthTmp = DllStructGetData($aInfo[0], "width")
			$iHeightTmp = DllStructGetData($aInfo[0], "height")
			Select
				Case $aSpringCoords[$i][0] <= $iX And $aSpringCoords[$i][1] <= $iY
					$tRectF = _GDIPlus_RectFCreate($aSpringCoords[$i][0] - $iWidthTmp, $aSpringCoords[$i][1] - $iHeightTmp)
				Case $aSpringCoords[$i][0] >= $iX And $aSpringCoords[$i][1] <= $iY
					$tRectF = _GDIPlus_RectFCreate($aSpringCoords[$i][0], $aSpringCoords[$i][1] - $iHeightTmp)
				Case $aSpringCoords[$i][0] <= $iX And $aSpringCoords[$i][1] >= $iY
					$tRectF = _GDIPlus_RectFCreate($aSpringCoords[$i][0] - $iWidthTmp, $aSpringCoords[$i][1])
				Case $aSpringCoords[$i][0] >= $iX And $aSpringCoords[$i][1] >= $iY
					$tRectF = _GDIPlus_RectFCreate($aSpringCoords[$i][0], $aSpringCoords[$i][1])
			EndSelect
			_GDIPlus_GraphicsDrawStringEx($hBuffer, $i - 1, $hFontNumbers, $tRectF, $hFormatNumber, $hBrushNumbers)
			_GDIPlus_GraphicsDrawRect($hBuffer, DllStructGetData($tRectF, "x"), DllStructGetData($tRectF, "y"), $iWidthTmp, $iHeightTmp, $aSpringCoords[$i][2])
		EndIf
	Next

	_GDIPlus_GraphicsFillEllipse($hBuffer, $iX - 20, $iY - 20, 40, 40)
	_GDIPlus_GraphicsDrawImageRect($hGraphic, $hBitmap, 0, 0, $iGUIWidth, $iGUIHeight)
WEnd

Func _ShowSettings()
	Opt("GUIOnEventMode", 0)
	GUISetState(@SW_SHOW, $hWndSettings)
	$iSpringNrTmp = 1

	While 1
		Switch GUIGetMsg()
			Case $GUI_EVENT_CLOSE
				Opt("GUIOnEventMode", 1)
				GUISetState(@SW_HIDE, $hWndSettings)
				Return _SaveToFile()
			Case $cSpringNr
				$iSpringNrTmp = GUICtrlRead($cSpringNr)
				Select
					Case $iSpringNrTmp > 10
						$iSpringNrTmp = 10
					Case $iSpringNrTmp < 1
						$iSpringNrTmp = 1
				EndSelect
				GUICtrlSetBkColor($cColorShow, StringTrimLeft(Hex(_GDIPlus_PenGetColor($aSpringCoords[$iSpringNrTmp][2])), 3))
				GUICtrlSetData($cStrength, $aSpringCoords[$iSpringNrTmp][4])
				If $aSpringCoords[$iSpringNrTmp][3] Then
					GUICtrlSetState($cState, $GUI_CHECKED)
				Else
					GUICtrlSetState($cState, $GUI_UNCHECKED)
				EndIf
			Case $cColorShow
				$iColorRGB = StringTrimLeft(_ChooseColor(2), 2)
				_GDIPlus_PenSetColor($aSpringCoords[$iSpringNrTmp][2], "0xFF" & $iColorRGB)
				GUICtrlSetBkColor($cColorShow, "0x" & $iColorRGB)
			Case $cState
				If $aSpringCoords[$iSpringNrTmp][3] Then
					$aSpringCoords[$iSpringNrTmp][3] = False
				Else
					$aSpringCoords[$iSpringNrTmp][3] = True
				EndIf
			Case $cStrength
				$iStrengthTmp = GUICtrlRead($cStrength)
				Select
					Case $iStrengthTmp > 80
						$iStrengthTmp = 80
					Case $iStrengthTmp < 1
						$iStrengthTmp = 1
				EndSelect
				GUICtrlSetData($cStrength, $iStrengthTmp)
				$aSpringCoords[$iSpringNrTmp][4] = $iStrengthTmp
		EndSwitch
	WEnd
EndFunc   ;==>_ShowSettings

Func WM_PAINT()
	_GDIPlus_GraphicsDrawImageRect($hGraphic, $hBitmap, 0, 0, $iGUIWidth, $iGUIHeight)
EndFunc   ;==>WM_PAINT

Func _LoadFromFile()
	$sText = FileRead(@ScriptDir & "\Data")
	$aSplitTmp = StringSplit($sText, "|")

	Local $aReturn[$aSplitTmp[0] + 1][5]
	$aReturn[0][0] = $aSplitTmp[0]

	For $i = 1 To $aSplitTmp[0]
		$aSplitTmp2 = StringSplit($aSplitTmp[$i], ",", 2)
		$aReturn[$i][0] = $aSplitTmp2[0]
		$aReturn[$i][1] = $aSplitTmp2[1]
		$aReturn[$i][2] = "0x" & Hex(_GDIPlus_PenCreate($aSplitTmp2[2], 2))
		Switch $aSplitTmp2[3]
			Case "True"
			    $aReturn[$i][3] = True
		    Case "False"
				$aReturn[$i][3] = False
		EndSwitch
		$aReturn[$i][4] = $aSplitTmp2[4]
	Next
	Return $aReturn
EndFunc   ;==>_LoadFromFile

Func _SaveToFile()
	$sString = ""

	For $i = 1 To $aSpringCoords[0][0]
		$sString &= $aSpringCoords[$i][0] & "," & $aSpringCoords[$i][1] & "," & _GDIPlus_PenGetColor($aSpringCoords[$i][2]) & "," & $aSpringCoords[$i][3] & "," & $aSpringCoords[$i][4] & "|"
	Next
	$hFile = FileOpen(@ScriptDir & "\Data", 2)
	FileWrite($hFile, StringTrimRight($sString, 2))
	FileClose($hFile)
EndFunc

Func _Exit()
	_GDIPlus_GraphicsDispose($hGraphic)
	_GDIPlus_GraphicsDispose($hBuffer)
	_GDIPlus_BitmapDispose($hBitmap)
	For $i = 1 To $aSpringCoords[0][0]
		_GDIPlus_PenDispose($aSpringCoords[$i][2])
	Next
	_GDIPlus_StringFormatDispose($hFormatNumber)
	_GDIPlus_FontDispose($hFontNumbers)
	_GDIPlus_FontFamilyDispose($hFamilyNumbers)
	_GDIPlus_Shutdown()
	Exit
EndFunc   ;==>_Exit