#include <GUIConstants.au3>
#Include <GuiEdit.au3>
#include <Misc.au3>
#include <ButtonConstants.au3>

HotKeySet("^+h","_ShowHide")
GUICreate("LumosCalc by Chesstiger",500,250)
$INPUT = GUICtrlCreateEdit("",0,0,500,50,BitOr($ES_AUTOVSCROLL,$ES_RIGHT,$ES_READONLY))
$MEM_A_OUTPUT = GUICtrlCreateInput("$A=",0,150,100,25,BitOr($ES_AUTOVSCROLL,$ES_READONLY))
$MEM_B_OUTPUT = GUICtrlCreateInput("$B=",0,175,100,25,BitOr($ES_AUTOVSCROLL,$ES_READONLY))
GUICtrlSetFont($INPUT,28)
$TEST_FSIZE = 1
$HIDE = 0
Global $MEM_A, $MEM_B
$CALC_PLUS = GUICtrlCreateButton("+",300,50,50,50)
$CALC_MINUS = GUICtrlCreateButton("-",300,100,50,50)
$CALC_MAL = GUICtrlCreateButton("×",300,150,50,50)
$CALC_DURCH = GUICtrlCreateButton("÷",300,200,50,50)
$NUMBER_1 = GUICtrlCreateButton("1",350,50,50,50)
$NUMBER_2 = GUICtrlCreateButton("2",400,50,50,50)
$NUMBER_3 = GUICtrlCreateButton("3",450,50,50,50)
$NUMBER_4 = GUICtrlCreateButton("4",350,100,50,50)
$NUMBER_5 = GUICtrlCreateButton("5",400,100,50,50)
$NUMBER_6 = GUICtrlCreateButton("6",450,100,50,50)
$NUMBER_7 = GUICtrlCreateButton("7",350,150,50,50)
$NUMBER_8 = GUICtrlCreateButton("8",400,150,50,50)
$NUMBER_9 = GUICtrlCreateButton("9",450,150,50,50)
$NUMBER_0 = GUICtrlCreateButton("0",350,200,50,50)
$NUMBER_KOMMA = GUICtrlCreateButton(",",400,200,50,50)
$CALC_GLEICH = GUICtrlCreateButton("=",450,200,50,50)
$CLEAR = GUICtrlCreateButton("CE",250,150,50,100)
$CALC_WURZEL = GUICtrlCreateButton("\/¯(x)",200,100,50,50)
$CALC_POTENZ = GUICtrlCreateButton("x^(y)",200,150,50,50)
$CALC_KLM_L = GUICtrlCreateButton("(",150,50,50,50)
$CALC_KLM_R = GUICtrlCreateButton(")",200,50,50,50)
$CTRL_ML = GUICtrlCreateButton("<-",250,50,50,100)
$CALC_LOG = GUICtrlCreateButton("log(x)",150,100,50,50)
$CALC_SIN = GUICtrlCreateButton("sin(x)",150,150,50,50)
$CALC_COS = GUICtrlCreateButton("cos(x)",150,200,50,50)
$CALC_TAN = GUICtrlCreateButton("tan(x)",200,200,50,50)
$MEM_A_ADD = GUICtrlCreateButton("Set" & @CR & "A",0,50,50,50,$BS_MULTILINE)
$MEM_A_PAS = GUICtrlCreateButton("Insert" & @CR & "A",50,50,50,50,$BS_MULTILINE)
$MEM_A_DEL = GUICtrlCreateButton("Delete" & @CR & "A",100,50,50,50,$BS_MULTILINE)
$MEM_B_ADD = GUICtrlCreateButton("Set" & @CR & "B",0,100,50,50,$BS_MULTILINE)
$MEM_B_PAS = GUICtrlCreateButton("Insert" & @CR & "B",50,100,50,50,$BS_MULTILINE)
$MEM_B_DEL = GUICtrlCreateButton("Delete" & @CR & "B",100,100,50,50,$BS_MULTILINE)
GUISetState()

While 1
	_LivingFont()
	$msg = GUIGetMsg()
	#region IsPressed-Controls
	If WinActive("LumosCalc") <> 0 Then
	Select
	Case _IsPressed("0D")
		_Calc()	
		Sleep(200)
	Case _IsPressed(08) 
		GUICtrlSetData($INPUT,StringTrimRight(GUICtrlRead($INPUT),1))
		Sleep(200)		
	Case _IsPressed("6B")
		_Add2Input("+")
		Sleep(200)
	Case _IsPressed("6A")
		_Add2Input("×")
		Sleep(200)
	Case _IsPressed("6D")
		_Add2Input("-")
		Sleep(200)
	Case _IsPressed("6F")
		_Add2Input("÷")
		Sleep(200)	
	Case _IsPressed("6E")
		_Add2Input(".")
		Sleep(200)			
	Case _IsPressed("2E")
		GUICtrlSetData($INPUT,"")				
	Case _IsPressed(30) Or _IsPressed(60)
		_Add2Input(0)
		Sleep(200)
	Case _IsPressed(31) Or _IsPressed(61) 
		_Add2Input(1)
		Sleep(200)
	Case _IsPressed(32) Or _IsPressed(62) 
		_Add2Input(2)
		Sleep(200)		
	Case _IsPressed(33) Or _IsPressed(63) 
		_Add2Input(3)
		Sleep(200)	
	Case _IsPressed(34) Or _IsPressed(64) 
		_Add2Input(4)
		Sleep(200)	
	Case _IsPressed(35) Or _IsPressed(65) 
		_Add2Input(5)
		Sleep(200)	
	Case _IsPressed(36) Or _IsPressed(66) 
		_Add2Input(6)
		Sleep(200)	
	Case _IsPressed(37) Or _IsPressed(67) 
		_Add2Input(7)
		Sleep(200)	
	Case _IsPressed(38) Or _IsPressed(68) 
		_Add2Input(8)
		Sleep(200)	
	Case _IsPressed(39) Or _IsPressed(69) 
		_Add2Input(9)
		Sleep(200)			
	Case _IsPressed(52)
		GUICtrlSetData($INPUT,Round(GUICtrlRead($INPUT)))
		Sleep(200)
	EndSelect		
	EndIf
	#endregion IsPressed-Controls
	Select
	Case $msg = $GUI_EVENT_CLOSE
		Exit	
	Case $msg = $CALC_LOG
		_Add2Input("log(")
	Case $msg = $CALC_SIN
		_Add2Input("sin(")
	Case $msg = $CALC_COS
		_Add2Input("cos(")	
	Case $msg = $CALC_TAN
		_Add2Input("tan(")			
	Case $msg = $CTRL_ML		
		GUICtrlSetData($INPUT,StringTrimRight(GUICtrlRead($INPUT),1))	
	Case $msg = $CLEAR
		GUICtrlSetData($INPUT,"")	
	Case $msg = $CALC_GLEICH
		_Calc()
	Case $msg = $NUMBER_1
		_Add2Input(1)
	Case $msg = $NUMBER_2
		_Add2Input(2)
	Case $msg = $NUMBER_3
		_Add2Input(3)
	Case $msg = $NUMBER_4
		_Add2Input(4)
	Case $msg = $NUMBER_5
		_Add2Input(5)		
	Case $msg = $NUMBER_6
		_Add2Input(6)
	Case $msg = $NUMBER_7
		_Add2Input(7)
	Case $msg = $NUMBER_8
		_Add2Input(8)
	Case $msg = $NUMBER_9
		_Add2Input(9)
	Case $msg = $NUMBER_0
		_Add2Input(0)
	Case $msg = $NUMBER_KOMMA
		_Add2Input(".")	
	Case $msg = $CALC_PLUS
		_Add2Input("+")
	Case $msg = $CALC_MINUS
		_Add2Input("-")
	Case $msg = $CALC_MAL
		_Add2Input("×")
	Case $msg = $CALC_DURCH
		_Add2Input("÷")		
	Case $msg = $CALC_WURZEL
		_Add2Input("\/¯(")
	Case $msg = $CALC_WURZEL
		_Add2Input("\/¯(")		
	Case $msg = $CALC_POTENZ
		_Add2Input("^(")			
	Case $msg = $CALC_KLM_L
		_Add2Input("(")
	Case $msg = $CALC_KLM_R
		_Add2Input(")")	
	Case $msg = $MEM_A_ADD	
		_AddMemory("A")
	Case $msg = $MEM_A_PAS
		_PasteMemory("A")
	Case $msg = $MEM_A_DEL
		_DeleteMemory("A")
	Case $msg = $MEM_B_ADD	
		_AddMemory("B")
	Case $msg = $MEM_B_PAS
		_PasteMemory("B")
	Case $msg = $MEM_B_DEL
		_DeleteMemory("B")		
EndSelect		
WEnd	

Func _Add2Input($VALUE)
GUICtrlSetData($INPUT,GUICtrlRead($INPUT) & $VALUE)
EndFunc

Func _User2PC($VALUE)
	$STRING = $VALUE
	;$STRING = StringReplace($STRING,",",".")
	$STRING = StringReplace($STRING,"×","*")
	$STRING = StringReplace($STRING,"÷","/")
	$STRING = StringReplace($STRING,"\/¯","Sqrt")
	$STRING = StringReplace($STRING,"$A",$MEM_A)
	$STRING = StringReplace($STRING,"$B",$MEM_B)	
	Return $STRING
EndFunc	

Func _PC2User($VALUE)
	$STRING = $VALUE
	;$STRING = StringReplace($STRING,".",",")
	$STRING = StringReplace($STRING,"*","×")
	$STRING = StringReplace($STRING,"/","÷")
	$STRING = StringReplace($STRING,"sqrt","\/¯")
	
	Return $STRING
EndFunc	

Func _Calc()
	FileWrite(@TempDir & "\LumosCalc.log",GUICtrlRead($INPUT))
	$CALCED = _PC2User(Execute(_User2PC(GUICtrlRead($INPUT))))
	If $CALCED = "1.#INF" Or $CALCED = "-1.#IND" Then $CALCED = "MATH ERROR"
	GUICtrlSetData($INPUT,$CALCED)	
	FileWrite(@TempDir & "\LumosCalc.log","=" & GUICtrlRead($INPUT) & ";" & @CRLF)	
EndFunc		


Func _LivingFont()
	If $TEST_FSIZE = 0 Then 
		If StringLen(GUICtrlRead($INPUT)) >= 22 Then
			GUICtrlSetFont($INPUT,14)
			$TEST_FSIZE = 1
		EndIf
	ElseIf $TEST_FSIZE = 1 Then 	
		If StringLen(GUICtrlRead($INPUT)) < 22 Then
			GUICtrlSetFont($INPUT,28)
			$TEST_FSIZE = 0
		EndIf		
	EndIf
EndFunc	

#cs
Func _Round($VALUE)
	$aVALUE = StringSplit($VALUE,".")
	;msgbox(0,"",$aVALUE[2])
	$NUMBER = Execute($aVALUE[2])
	$NUMBER = StringTrimRight($NUMBER,StringLen($NUMBER) - 1)
	;MSGBOX(0,"",$NUMBER)
	If $NUMBER >= 5 Then Return Ceiling($VALUE)
	If $NUMBER < 5 Then Return Floor($VALUE)
	EndFunc		
#ce

Func _ShowHide()
		If $HIDE = 1 Then
			GUISetState(@SW_SHOW)
			$HIDE = 0
		ElseIf $HIDE = 0 Then	
			GUISetState(@SW_HIDE)
			$HIDE = 1
		EndIf
		Sleep(200)
	EndFunc		
	
	
Func _AddMemory($MEMORY)
	If $MEMORY = "A" Then
		Global $MEM_A = Execute(GUICtrlRead($INPUT))
		GUICtrlSetData($MEM_A_OUTPUT,"$A=" & $MEM_A)
	ElseIf $MEMORY = "B" Then
		Global $MEM_B = Execute(GUICtrlRead($INPUT))
		GUICtrlSetData($MEM_B_OUTPUT,"$B=" & $MEM_B)		
	EndIf
EndFunc	




Func _PasteMemory($MEMORY)
	If $MEMORY = "A" Then
		If IsDeclared("MEM_A") Then _Add2Input("$A")
	ElseIf $MEMORY = "B" Then
		If IsDeclared("MEM_B") Then _Add2Input("$B")
	EndIf
EndFunc		


	
	
	
Func _DeleteMemory($MEMORY)
	If $MEMORY = "A" Then
		If IsDeclared("MEM_A") Then $MEM_A = ""
		GUICtrlSetData($MEM_A_OUTPUT,"$A=" & $MEM_A)	
	ElseIf $MEMORY = "B" Then
		If IsDeclared("MEM_B") Then $MEM_B = ""
		GUICtrlSetData($MEM_B_OUTPUT,"$B=" & $MEM_B)				
	EndIf
EndFunc	