filein "biped functions.ms"

fn getIKval NodeName = 
	(
	despace = filterstring NodeName " "
	NodeF = despace[1]
	for q = 2 to (despace.count) do
				(
				NodeF += "_"
				NodeF += despace[q]
				)
	tv = trackviewnodes
	subs = getsubanimnames tv
	for z = 1 to subs.count do
		(if (NodeF as name) == subs[z] do return tv[z].IK_state.controller.value)
	)
fn getTVnode NodeName =
	(
	global mytv
	TV = trackviewnodes
	despace = filterstring NodeName " "
	NodeF = despace[1]
	for q = 2 to (despace.count) do
				(
				NodeF += "_"
				NodeF += despace[q]
				)
	execme = "" as stringstream
	format "mytv =  TV.% " NodeF to:execme
	seek execme 0
	execute execme
	return mytv
	)

fn WhoAmI =
	(
	checklist = getCurrentSelection()
	try (join checklist selection[1].children)
	catch()
	Iam = undefined
	for items in checklist do 
		(
		if items.name == (items.parent.name + " anchor") do
			Iam = items
		)
	return Iam
	)
				
RcMenu Footsie
(
fn HasLock = (	checklist = getCurrentSelection()
				try (join checklist selection[1].children)
				catch()
				for item in checklist do
					(
					if item.name == (item.parent.name + " anchor") do return true
					)		
				return false
			)
fn IsVirgin = (selection.count == 1 and \
				HasLock() == false and \
				findstring (bipstrip selection[1]) "foot" != undefined
				)
				
menuitem 	ff_abt "About footsie..."
menuitem	ff_add	"Add footsie" filter:IsVirgin
menuitem	ff_del	"Delete footsie" filter:HasLock
submenu "Footsie Keys" filter:Haslock
	(
	menuitem 	ff_setin	"set startTouch key" 
	menuitem	ff_setlock	"set nowLocked key" 
	menuitem	ff_setunlock "set startTakeoff key" 
	menuitem	ff_setfree	"set nowFree key" 
	)
menuitem 	ff_save 	"save *.fts file" 

on 	ff_abt picked do (
	messagebox "Tool for tagging when feet are locked to ground."
	)

on	ff_add	 picked do 
	(
	target = selection[1]
	namestring = target.name + " anchor"
	isthere = (for items in objects where items.name == namestring collect items)
	if isthere.count > 0 then
		 return "already got one, you see"
	else
		(
		newanchor = text font:"WebDings" \
		text:"Þ" \
		size:10 \
		rotation:(quat 0.707107 0 0 0.707107)\
		name:namestring
		newanchor.parent = target
		testname = bipstrip target
		offset = (if (testname[1] == "L") then  -1 else 1)
		newanchor.position = [0,0,offset] * target.objecttransform
		newanchor.wirecolor = target.wirecolor
		TVN = newTrackViewNode namestring
		TVC = AddTrackViewController TVN (Bezier_float()) "IK State"
		setuserprop newanchor "TrackNode" TVN
		newanchor.scale.controller = scale_script()
		nascript = stringstream ""
		format "myTrack = getTVNode \"%\"\n" namestring to:nascript
		format "$'%'.wirecolor = ($'%'.parent.wirecolor) * getIKval \"%\"\n" namestring namestring namestring to:nascript
		format "[1,1,1]\n" to:nascript		
		newanchor.scale.controller.script = nascript as string
		)
		
	)
	
on	ff_del picked do 
		(	checklist = getCurrentSelection()
				try (join checklist selection[1].children)
				catch()
				killme = WhoAmI()	
				if killme != undefined do 
					(
					mytracknode = GetTVnode killme.name
					delete killme
					deletetrackviewnode mytracknode
					
					)
			)
on	ff_setin picked do 
	(
	me = WhoAmI()
	mytrack = getTVNode me.name
	thiskey = AddNewKey Mytrack.IK_state.keys slidertime #select
	thiskey.value = 1
	thiskey.outtangenttype = #slow
	thiskey.intangenttype = #step
	
	)	
on	ff_setlock picked do 
	(
	me = WhoAmI()
	mytrack = getTVNode me.name
	thiskey = AddNewKey Mytrack.IK_state.keys slidertime #select
	thiskey.value = 0
	thiskey.outtangenttype = #step
	thiskey.intangenttype = #slow
	
	)
on	ff_setunlock picked do 
	(
	me = WhoAmI()
	mytrack = getTVNode me.name
	thiskey = AddNewKey Mytrack.IK_state.keys slidertime #select
	thiskey.value = 0
	thiskey.outtangenttype = #slow
	thiskey.intangenttype = #linear
	
	)
on	ff_setfree picked do 
	(
	me = WhoAmI()
	mytrack = getTVNode me.name
	thiskey = AddNewKey Mytrack.IK_state.keys slidertime #select
	thiskey.value = 1
	thiskey.outtangenttype = #step
	thiskey.intangenttype = #slow
	
	)

on ff_save picked do
	(
	
	targetfile = getSaveFilename caption:"Save Footsie Data" filename:(getfilenamefile maxfilename) types:"Footsie file|*.fts|Text file|*.txt|"
	if targetfile != undefined then
		(
		setwaitcursor()
		oput = createfile targetfile
		anchorlist = for items in objects where (findstring items.name " anchor") != undefined collect items
		tracklist = #()
		for q = 1 to anchorlist.count do
			(
			MyTrack = getTVNode anchorlist[q].name
			MyController = MyTrack.IK_state
			append tracklist MyController 
			)
		format "FOOTSIE v 0.1\n" to:oput
		format "format: time <frame> { <bone> -tab- <freedom> -tab- [<matrix 3 x 4>] -tab- <state> } \n" to:oput
		for t = (animationrange.start.frame) to (animationrange.end.frame) do
			(
			slidertime = t
			format "time %\n\t{\n" (t as integer)  to:oput
			for q = 1 to anchorlist.count do
				(format "\t\"%\" " anchorlist[q].parent.name to:oput
				 format "%" tracklist[q].value to:oput
				 tf = anchorlist[q].parent.transform
				 format "\t[ % % % " tf.row1.x tf.row1.y tf.row1.z to:oput
								format "% % % " tf.row2.x tf.row2.y tf.row2.z to:oput
								format "% % % " tf.row3.x tf.row3.y tf.row3.z to:oput
								format "% % % ]" tf.row4.x tf.row4.y tf.row4.z to:oput
				case tracklist[q].value of 
					(
					0.0:(if (at time (slidertime - 1) tracklist[q].value > 0.0 ) do format " locked" to:oput)
					1.0:(if (at time (slidertime - 1) tracklist[q].value < 1.0 ) do format " free" to:oput)	 
					default:()
					
					)
				format "\n" to:oput
				)
				
			format "\t}\n" to:oput
			)
		setarrowcursor()
		flush oput
		close oput
		messagebox ("Saved file " + targetfile)
		)
	)
on Footsie open do
	(
	tv = getsubanimnames trackviewnodes
	anchors = for items in tv where findstring (items as string) "anchor" != undefined collect items
	ff_save.enabled = (anchors.count > 0)
	)

)


registerRightClickMenu Footsie