Scriptname RaceMenuPluginCamera extends RaceMenuBase

;Camera
string Property NINODE_CAMERA1 = "CME Camera1st [Cam1]" AutoReadOnly
string Property NINODE_CAMERA3 = "CME Camera3rd [Cam3]" AutoReadOnly

;Callbacks Table
string Property CALLBACK_CAMERA1Y = "XPMSEChangeCamera1stPersonY" AutoReadOnly
string Property CALLBACK_CAMERA1Z = "XPMSEChangeCamera1stPersonZ" AutoReadOnly
string Property CALLBACK_CAMERA3X = "XPMSEChangeCamera3rdPersonX" AutoReadOnly
string Property CALLBACK_CAMERA3Y = "XPMSEChangeCamera3rdPersonY" AutoReadOnly
string Property CALLBACK_CAMERA3Z = "XPMSEChangeCamera3rdPersonZ" AutoReadOnly

;Arrays
float[] _camera1_Position
float[] _camera3_Position

; If you are making your own scaling mod, use your own key name
string Property MOD_OVERRIDE_KEY = "RMCPlugin" AutoReadOnly

string Property CATEGORY_CAMERA = "rsm_camera" AutoReadOnly

; NiOverride version data
int Property NIOVERRIDE_VERSION = 3 AutoReadOnly
int Property NIOVERRIDE_SCRIPT_VERSION = 2 AutoReadOnly

bool _versionValid = false

Event OnCategoryRequest()
	AddCategory(CATEGORY_CAMERA, "$CATEGORY CAMERA")
EndEvent

Event OnStartup()
	parent.OnStartup()

	int nioverrideVersion = SKSE.GetPluginVersion("NiOverride")
	int nioverrideScriptVersion = NiOverride.GetScriptVersion()

	; Check NiOverride version, disable most features if this fails
	if nioverrideVersion >= NIOVERRIDE_VERSION && nioverrideScriptVersion >= NIOVERRIDE_SCRIPT_VERSION
		_versionValid = true
	Else
		_versionValid = false
	Endif
EndEvent

; Add Custom sliders here
Event OnSliderRequest(Actor target, ActorBase targetBase, Race actorRace, bool isFemale)
	If NetImmerse.HasNode(target, NINODE_CAMERA1, true)
		_camera1_Position = GetNodePositionV(target, isFemale, NINODE_CAMERA1, true)
		AddSliderEx("$Camera1 Pos Y", CATEGORY_CAMERA, CALLBACK_CAMERA1Y, -20.00, 20.00, 0.01, _camera1_Position[1])
		AddSliderEx("$Camera1 Pos Z", CATEGORY_CAMERA, CALLBACK_CAMERA1Z, -20.00, 20.00, 0.01, _camera1_Position[2])
	Endif
	If NetImmerse.HasNode(target, NINODE_CAMERA3, false)
		_camera3_Position = GetNodePositionV(target, isFemale, NINODE_CAMERA3, false)
		;AddSliderEx("$Camera3 Pos X", CATEGORY_CAMERA, CALLBACK_CAMERA3X, -20.00, 20.00, 0.01, _camera3_Position[0])
		;AddSliderEx("$Camera3 Pos Y", CATEGORY_CAMERA, CALLBACK_CAMERA3Y, -20.00, 20.00, 0.01, _camera3_Position[1])
		;AddSliderEx("$Camera3 Pos Z", CATEGORY_CAMERA, CALLBACK_CAMERA3Z, -20.00, 20.00, 0.01, _camera3_Position[2])
	Endif
EndEvent

Event OnSliderChanged(string callback, float value)
	bool isFemale = _targetActorBase.GetSex() as bool
	If _versionValid
		If callback == CALLBACK_CAMERA1Y || callback == CALLBACK_CAMERA1Z
			If callback == CALLBACK_CAMERA1Y
				_camera1_Position[1] = value
			ElseIf callback == CALLBACK_CAMERA1Z
				_camera1_Position[2] = value
			EndIf
			SetNodePositionV(_targetActor, isFemale, NINODE_CAMERA1, _camera1_Position, true)
		Elseif callback == CALLBACK_CAMERA3X || callback == CALLBACK_CAMERA3Y || callback == CALLBACK_CAMERA3Z
			If callback == CALLBACK_CAMERA3X
				_camera3_Position[0] = value
			ElseIf callback == CALLBACK_CAMERA3Y
				_camera3_Position[1] = value
			ElseIf callback == CALLBACK_CAMERA3Z
				_camera3_Position[2] = value
			EndIf
			SetNodePositionV(_targetActor, isFemale, NINODE_CAMERA3, _camera3_Position, false)
		Endif	
	Endif
EndEvent

Function SetNodePositionV(Actor akActor, bool isFemale, string nodeName, float[] values, bool isFirstPerson)
	If values[0] != 0.0 || values[1] != 0.0 || values[2] != 0.0
		NiOverride.AddNodeTransformPosition(akActor, isFirstPerson, isFemale, nodeName, MOD_OVERRIDE_KEY, values)
	Else
		NiOverride.RemoveNodeTransformPosition(akActor, isFirstPerson, isFemale, nodeName, MOD_OVERRIDE_KEY)
	Endif
	NiOverride.UpdateNodeTransform(akActor, isFirstPerson, isFemale, nodeName)
EndFunction

float[] Function GetNodePositionV(Actor akActor, bool isFemale, string nodeName, bool isFirstPerson)
	return NiOverride.GetNodeTransformPosition(akActor, isFirstPerson, isFemale, nodeName, MOD_OVERRIDE_KEY)
EndFunction