Scriptname RaceMenuPluginCitrus extends RaceMenuBase

;Citrus
string Property NINODE_LEFT_SCALEBREAST = "NPC L ScaleBreast" AutoReadOnly
string Property NINODE_RIGHT_SCALEBREAST = "NPC R ScaleBreast" AutoReadOnly
string Property NINODE_LEFT_SCALEBUTT = "NPC R ScaleButt" AutoReadOnly
string Property NINODE_RIGHT_SCALEBUTT = "NPC L ScaleButt" AutoReadOnly

;Callbacks Table
string Property CALLBACK_LEFT_SCALEBREAST = "CitrusChangeLeftScaleBreast" AutoReadOnly
string Property CALLBACK_RIGHT_SCALEBREAST = "CitrusChangeRightScaleBreast" AutoReadOnly
string Property CALLBACK_LEFT_SCALEBUTT = "CitrusChangeLeftScaleButt" AutoReadOnly
string Property CALLBACK_RIGHT_SCALEBUTT = "CitrusChangeRightScaleButt" AutoReadOnly

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

string Property CATEGORY_KEY = "rsm_citrus_scale" AutoReadOnly

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

bool _versionValid = false

Event OnCategoryRequest()
	AddCategory(CATEGORY_KEY, "$CATEGORY CITRUS")
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_LEFT_SCALEBREAST, false)
		AddSliderEx("$Citrus Left Breast", CATEGORY_KEY, CALLBACK_LEFT_SCALEBREAST, 0.1, 3.00, 0.01, GetNodeScale(target, isFemale, NINODE_LEFT_SCALEBREAST))
	Endif
	If NetImmerse.HasNode(target, NINODE_RIGHT_SCALEBREAST, false)
		AddSliderEx("$Citrus Right Breast", CATEGORY_KEY, CALLBACK_RIGHT_SCALEBREAST, 0.1, 3.00, 0.01, GetNodeScale(target, isFemale, NINODE_RIGHT_SCALEBREAST))
	Endif
	If NetImmerse.HasNode(target, NINODE_LEFT_SCALEBUTT, false)
		AddSliderEx("$Citrus Left Butt", CATEGORY_KEY, CALLBACK_LEFT_SCALEBUTT, 0.1, 3.00, 0.01, GetNodeScale(target, isFemale, NINODE_LEFT_SCALEBUTT))
	Endif
	If NetImmerse.HasNode(target, NINODE_RIGHT_SCALEBUTT, false)
		AddSliderEx("$Citrus Right Butt", CATEGORY_KEY, CALLBACK_RIGHT_SCALEBUTT, 0.1, 3.00, 0.01, GetNodeScale(target, isFemale, NINODE_RIGHT_SCALEBUTT))
	Endif
EndEvent

Event OnSliderChanged(string callback, float value)
	bool isFemale = _targetActorBase.GetSex() as bool
	If _versionValid
		If callback == CALLBACK_LEFT_SCALEBREAST
			SetNodeScale(_targetActor, isFemale, NINODE_LEFT_SCALEBREAST, value)
		Elseif callback == CALLBACK_RIGHT_SCALEBREAST
			SetNodeScale(_targetActor, isFemale, NINODE_RIGHT_SCALEBREAST, value)
		Elseif callback == CALLBACK_LEFT_SCALEBUTT
			SetNodeScale(_targetActor, isFemale, NINODE_LEFT_SCALEBUTT, value)
		Elseif callback == CALLBACK_RIGHT_SCALEBUTT
			SetNodeScale(_targetActor, isFemale, NINODE_RIGHT_SCALEBUTT, value)
		Endif	
	Endif
EndEvent

Function SetNodeScale(Actor akActor, bool isFemale, string nodeName, float value)
	If value != 1.0
		NiOverride.AddNodeTransformScale(akActor, false, isFemale, nodeName, MOD_OVERRIDE_KEY, value)
		NiOverride.AddNodeTransformScale(akActor, true, isFemale, nodeName, MOD_OVERRIDE_KEY, value)
	Else
		NiOverride.RemoveNodeTransformScale(akActor, false, isFemale, nodeName, MOD_OVERRIDE_KEY)
		NiOverride.RemoveNodeTransformScale(akActor, true, isFemale, nodeName, MOD_OVERRIDE_KEY)
	Endif
	NiOverride.UpdateNodeTransform(akActor, false, isFemale, nodeName)
	NiOverride.UpdateNodeTransform(akActor, true, isFemale, nodeName)
EndFunction

float Function GetNodeScale(Actor akActor, bool isFemale, string nodeName)
	return NiOverride.GetNodeTransformScale(akActor, false, isFemale, nodeName, MOD_OVERRIDE_KEY)
EndFunction