'dont touch
'==============================================================
'timer reference
'	1 = clock tick

'standard references
Dim dxs
Dim dxEdit
Dim strScriptsPath

'theme init and exit
Dim strThemeRootName, strClockRootName
Dim arrToggleButtons, arrSliders, arrHide, arrShow
Dim optCPUInfo
'==============================================================

Sub dxInit()
	'dont touch
	'==========================================================
	strScriptsPath = Object.Directory
	'strScriptsPath = "D:\DesktopX\2012 scripts\"
	
	Set dxs = DesktopX.ScriptObject("dxScriptor")
	Set dxEdit = DesktopX.ScriptObject("dxEdit_script")
	strThemeRootName = Object.Name
	'==========================================================
	'set active theme options
	optCPUInfo = True
	
	'name objects which are controls - leave of theme name
	arrToggleButtons = Array("toggleLeftArm","toggleRightArm","toggleInternet")
	arrSliders = Array("rightArm","leftArm")
	
	'name of objects to hide until initialised - leave of theme name
	arrHide = Array("leftArm","rightArm")
	arrShow = Array("clock","leftArm","rightArm")
	
	'dont touch
	'==========================================================
	Call theme_hide_init()
	Call theme_controls_init()
	Call theme_clock_init()
	Call theme_info_init()
	Call theme_show_init()
End Sub

Sub dxExit()
	'toggle buttons
	For Each strToggleButton in arrToggleButtons
		strObjectName = strThemeRootName & "_" & strToggleButton
		If DesktopX.IsObject(strObjectName) Then
			DesktopX.Object(strObjectName).SetScript("")
		End If
	Next
	
	'sliders
	For Each strSlider in arrSliders
		strObjectName = strThemeRootName & "_" & strSlider
		DesktopX.Object(strObjectName).SetScript("")
	Next
	
	'clock
	Call DesktopX.Object(strClockRootName & "_secondHand").SetScript("")
	Call DesktopX.Object(strClockRootName & "_minuteHand").SetScript("")
	Call DesktopX.Object(strClockRootName & "_hourHand").SetScript("")
End Sub

Sub Object_OnScriptEnter()
	Call dxInit()
End Sub

'hide/show objects until initialised
Sub theme_hide_init()
	For each strObjectName in arrHide
		If DesktopX.IsObject(strThemeRootName & "_" & strObjectName) Then
			DesktopX.Object(strThemeRootName & "_" & strObjectName).Visible = False
		End If
	Next
End Sub

Sub theme_show_init()
	For each strObjectName in arrHide
		If DesktopX.IsObject(strThemeRootName & "_" & strObjectName) Then
			DesktopX.Object(strThemeRootName & "_" & strObjectName).Visible = True
		End If
	Next
	For each strObjectName in arrShow
		If DesktopX.IsObject(strThemeRootName & "_" & strObjectName) Then
			DesktopX.Object(strThemeRootName & "_" & strObjectName).Visible = True
		End If
	Next
End Sub

'controls init
Sub theme_controls_init()
	'toggle buttons
	For Each strToggleButton in arrToggleButtons
		strObjectName = strThemeRootName & "_" & strToggleButton
		If DesktopX.IsObject(strObjectName) Then
			DesktopX.Object(strObjectName).SetScript(strScriptsPath & "dxToggleButton.txt")
			Call DesktopX.ScriptObject(strObjectName).dxInit()
		End If
	Next
	
	'sliders
	For Each strSlider in arrSliders
		strObjectName = strThemeRootName & "_" & strSlider
		DesktopX.Object(strObjectName).SetScript(strScriptsPath & "dxSlider.txt")
		Call DesktopX.ScriptObject(strObjectName).dxInit()
	Next
End Sub

'piped events
Function Object_OnLButtonDownEx(obj,x,y)
	'kill 'bring to top' of mouse click
	Object_OnLButtonDownEx = True
End Function

'theme plugin/components
'===========================================================
'analog clock
Dim lastSecondsAngle
Dim objSecondHand,objMinuteHand,objHourHand
Sub theme_clock_init()
	strClockRootName = strThemeRootName & "_clock"
	
	On Error Resume Next
	Set objSecondHand = DesktopX.Object(strClockRootName & "_secondHand")
	Set objMinuteHand = DesktopX.Object(strClockRootName & "_minuteHand")
	Set objHourHand = DesktopX.Object(strClockRootName & "_hourHand")
	Call dxEdit.dxPipeEvents(strClockRootName & "_secondHand",strThemeRootName)
	Call dxEdit.dxPipeEvents(strClockRootName & "_minuteHand",strThemeRootName)
	Call dxEdit.dxPipeEvents(strClockRootName & "_hourHand",strThemeRootName)
	
	nowTime = Time()
	nowHours = Hour(nowTime)
	nowMinutes = Minute(nowTime)
	nowSeconds = Second(nowTime)
	nowTimer = Timer
	nowMilliseconds = (nowTimer - Int(nowTimer)) * 1000
	
	angleHours = nowHours * 360/12
	angleMinutes = nowMinutes * 360/60
	angleSeconds = nowSeconds * 360/60
	
	objMinuteHand.Rotation = angleMinutes
	objHourHand.Rotation = angleHours
	objMinuteHand.Rotation = angleSeconds
	
	'synchronize to system clock ticks
	nextWholeSecond = Int(Timer)+2
	While Timer < nextWholeSecond
	Wend
	
	Object.SetTimer 1,100
End Sub

Sub Object_OnTimer1		'clock tick
	On Error Resume Next
	nowTime = Time()
	nowHours = Hour(nowTime)
	nowMinutes = Minute(nowTime)
	nowSeconds = Second(nowTime)
	
	nowTimer = Timer
	nowMilliseconds = (nowTimer - Int(nowTimer)) * 1000
	
	angleHours = nowHours * 360/12 + nowMinutes * 30/60
	angleMinutes = nowMinutes * 360/60
	angleSeconds = nowSeconds * 360/60
	angleMilliseconds = nowMilliseconds * 360/60000
	
	'objMinuteHand.Rotation = angleMinutes
	'objHourHand.Rotation = angleHours
	newSecondsAngle = angleSeconds + angleMilliseconds
	
	If newSecondsAngle > 360 Then
		lastSecondsAngle = -2
		newSecondsAngle = newSecondsAngle - 360
	End If
	
	If newSecondsAngle > lastSecondsAngle Then
		'objSecondHand.Rotation = newSecondsAngle
	End If
	
	lastSecondsAngle = newSecondsAngle
End Sub

'various information text piping
Dim cpuCoreCount
Sub theme_info_init()
	If optCPUInfo = True Then
		cpuCoreCount = 4
	End If
	Object.SetTimer 2,200
	
End Sub

Sub Object_OnTimer2
	'refresh cpu readouts
	If optCPUInfo = True Then
		For coreCount = 1 to 8
			strObjectCoreName = strThemeRootName & "_cpu" & coreCount
			If coreCount <= cpuCoreCount Then
				If DesktopX.IsObject(strObjectCoreName) Then
					Set objCore = DesktopX.Object(strObjectCoreName)
					objCore.Text = Rnd(1)
				End If
			Else
				If DesktopX.IsObject(strObjectCoreName) Then
					Set objCore = DesktopX.Object(strObjectCoreName)
					objCore.Text = "boo"
				End If
			End If
		Next
	End If
	
	If optRAMPercentage = True Then
	End If
End Sub
