--Destroy dialog if it already exists.
try(destroyDialog theRollout)catch()

--Create a rollout
rollout theRollout "The Rollout" width:300
(
	local rootObjs=#()		--Will hold all the root nodes found in the scene. 
	struct objectNode (obj,children=#())
	local objectTree=#()
	
	--Create the dotNet treeView control
	dotNetControl tv "system.windows.forms.treeView" height:200
	
	--Create a button for testing. 
	button testButton "Test"
	on testButton pressed do
	(
		clearListener()			--Clear the listener.
		format "Props\n"			--Format a header so we know what we are looking at below. 
		showProperties tv		--Show the properties of the treeView control.
		format "\nMethods\n"			--Format a header so we know what we are looking at below. 
		showMethods tv		--Show the properties of the treeView control.
		format "\nEvents\n"			--Format a header so we know what we are looking at below. 
		showEvents tv		--Show the properties of the treeView control.
	)
	
	--Find all the root objects in the scene and append them to rootObjs array
	fn findRootObjs =
	(
		rootObjs=#()
		for x in objects do 
		(
			if x.parent==undefined then append rootObjs x
		)
	)
	
	--Recurse hierarchy and add treeview nodes.
	fn recurseHierarchy obj theTvNode objNode=
	(
		for i = 1 to obj.children.count do		--Loop through each of the children
		(
			n=(dotNetObject "System.Windows.Forms.TreeNode" obj.children[i].name)
			theTvNode.nodes.add n
			
			objChildNode=objectNode()	--Create instance of the struct
			objChildNode.obj=obj.children[i]	--Set the object member
			append objNode.children objChildNode	--Append the struct instance with the child struct instance. 
			
			recurseHierarchy obj.children[i] n objChildNode	--Call recursion on each of the children. 
		)
	)
	
	--Adds root nodes to treeView.
	fn populateTreeView theTv=
	(
		findRootObjs()		--Collect all the root objects.
		
		--Loop through all the objects in the scene. 
		for x in rootObjs do 
		(
			--Create a treeViewNode and add it to the treeView control
			n=(dotNetObject "System.Windows.Forms.TreeNode" x.name) 
			theTv.nodes.add n
			
			objNode=objectNode()	--Create instance of the struct
			objNode.obj=x	--Set the object member
			append objectTree objNode	--Append the objectTree array with the instance of the struct.
			
			recurseHierarchy x n objNode		--Call recursive function on each of the root nodes. 
		)
	)
	
	--Select objects function. 
	fn selectObject theTvNode=
	(
		local indexPath=#()	--Array to hold the index of each node in the path.
		while theTvNode!=undefined do	--While loop to walk backwards from the selected node to root. 
		(
			insertItem theTvNode.index indexPath 1	--Insert the index of each node into array at at start.
			theTvNode=theTvNode.parent	--Set theTvNode to the parent node. 
		)
		
		ot=objectTree[indexPath[1]+1]	--Get the root node in objectTree to start with
		for i = 2 to indexPath.count do	--Loop trough all the index values accept the first.
		(
			ot=ot.children[indexPath[i]+1]	--Set ot to each of the structs by index.
		)
		if isValidNode ot.obj	then select ot.obj--Select the object that is left. 
	)
	
	on tv mouseDown arg do
	(
-- 		showProperties arg		--Show the properties for the argument
-- 		print arg.location			--Instead of using the X and Y value to create a point the location returns on. 
-- 		print (tv.GetNodeAt arg.location) --Get the node under the mouse
-- 		showProperties (tv.GetNodeAt arg.location)
-- 		print (tv.GetNodeAt arg.location).text	--Get the text
-- 		print (tv.GetNodeAt arg.location).Index	--The index of the node.
		
		selectObject (tv.GetNodeAt arg.location)
	)
	
	on theRollout open do
	(
		populateTreeView tv
	)
)
createDialog theRollout
