-- Vertex Color tagger
-- version 0.9
-- Oct 26 '99
-- for assigning vertex colors (and non-overlapping selection groups) to meshes
-- coming soon: loadable definition files

global Clist = #(color 75 75 75 ,  color 150 50 50 , color 50 50 150 , color 220 190 50 , color 50 200 225 , \
				color 220 100 75 , color 100 100 100 , color 50 50 50 , color 75 75 75 , color 100 100 100 , \
				color 125 125 125 , color 150 150 150, color 175 175 175, color 200 200 200, color 225 225 225, color 250 250 250) 

global namelist = #("Nose","Right Eye","Left Eye", "Right Eyebrow", "Left Eyebrow", "Right Cheek", "Left Cheek","Upper Lip", "Lower Lip","","","","","","","","")
global targetObj
global NumRegions = 9
global Hlist = #("false","false","false","false","false","false","false","false","false","false","false","false","false","false","false","false")

fn Showhide Hstate index = 
	(		
	hlist[index] = Hstate
	if keyboard.shiftpressed then
		(
		for k = 1 to Numregions do
			(
			if K != index then
				(
				Hlist[K] = not HState
				DummyS = "TagAreas.Hide" + ((K as integer)as string) +".checked = Hlist[" + ((K as integer)as string)+ "]"
				execute DummyS
				)
			else
				(
				Hlist[K] = Hstate
				)
			)
		)
			
	Meshops.unhideall targetobj
	for i = 1 to  NumRegions do
		(
		if Hlist[i] == true do
			(
			selectregion i
			Meshops.hide targetobj
			)		
		)
	)
		
fn ColorComp Color1 Color2 =
	(
	testcolor = Color1 - Color2
	TestSum = (testcolor.R as integer) + (testcolor.g as integer) + (testcolor.B) as integer
	if testsum == 0 then
		(return true)
	else 
		(return false)
	)

fn UpdateName name index = ( NameList[index] = name)

fn UpdateColor color index = 
	(
	selectregion index
 	Clist[index] = Color
	tagverts index
	 )

fn Selectregion index = 
	(
	newsel = #()
	for i = 1 to targetobj.numverts do 
		(
		if ColorComp (getvertcolor targetobj i)  Clist[index] do (append NewSel i)
		)
	setvertselection targetobj newsel
	update targetobj
	)
	
fn tagverts index = 
	(
	for i = 1 to targetobj.numverts do
		(
		if (getvertselection targetobj)[i] do (setvertcolor targetobj i Clist[index])
		)
	completeredraw()
	)

fn DTlist_entry number =
	(
	Cmdstring = "" as stringstream
	format "Checkbutton Hide% \"\" images:#(\"Facetagger_16i.bmp\",\"Facetagger_16a.bmp\",4,2,1,2,1) offset:[-20,0] across:5 tooltip:\"Lock/unlock tagged region\" \n" number to:CmdString
	format "colorpicker CP% \"\" color:Clist[%] offset:[-32,0] fieldwidth:44 height:22 \n" number  number to:CmdString
	format "EditText Rname% text:namelist[%]  FieldWidth:96 offset:[-36,2]\n" number number to:CmdString
	format "Button Add% Offset:[16,0] images:#(\"Facetagger_16i.bmp\",\"Facetagger_16a.bmp\",4,3,3,3,3) tooltip:\"Tag selected faces\" \n" number to:CmdString
	format "Button Sel% images:#(\"Facetagger_16i.bmp\",\"Facetagger_16a.bmp\",4,4,4,4,4) tooltip:\"select tagged vertices\" \n" number to:CmDstring
	format "on Hide% changed state do (ShowHide Hide%.checked %)\n" number number number to:CmDstring
	format "on Rname% entered text do (UpdateName text %)\n" number number to:CMDstring
	format "on CP% changed color do (updatecolor Color %)\n" number number to:CmdString
	format "on Sel% pressed do (selectregion %)\n" number number to:CMDstring
	format "on Add% pressed do (tagverts %)\n" number number to:CMDstring
	return CmdString
	)

fn Assemble_DTlist total =
	(
	RemoveRollout TagAreas TestWindow
	Rolloutstring = "" as stringstream
	format "Rollout TagAreas \"Regions\" \n" to:RolloutString
	format "(\nlabel sh \"Hide\" offset:[-20,0] Across:5 \n" to:RolloutString
	format "label cl \"Color\" offset:[-34,0]\n" to:RolloutString
	format "label rn \"Region name\" offset:[-20,0]\n" to:RolloutString
	format "label d1 \"Tag\" offset:[12,0]\n" to:RolloutString
	format "label d2 \"Select\"\n\n" to:RolloutString
	for i = 1 to total do
		(
		format "%" ((DTList_entry i) as string) to:Rolloutstring
		format "\n" to:RolloutString
		)
	format ")\n" to:RolloutString
	format "Addrollout TagAreas TestWindow" to:RolloutString
	seek Rolloutstring 0
	Execute (RolloutString)
	)


rollout Setup "Setup"
(	
	pickbutton PickMesh "Select Mesh" offset:[-10,-2] across:3
	Edittext ChosenMesh text:"<--- press to start" enabled:false offset:[-12,0] fieldwidth:98
	spinner HowManyRegions "Regions:" \
		fieldwidth:24
		range:[1,16,9] \
		type:#integer
	EditText Posename "Pose name:"
	label empty1 ""
	group "Files" 
		(
	Button LoadDef "Load definition file" across:2 enabled:false
	Button SaveDef "Save Definition File" enabled:false
		)
		group "Display" 
		(
		Checkbutton Vcolor "Vertex colors" across:3 checked:true
		Checkbutton Vshade "Shading"  
		Checkbutton TexMaps "Textures" enabled:false
		)
		
	on LoadDef pressed do
		(
		bob = getopenfilename caption:"Load VDef file" types:".VDF files|*.VDF|"
		)	
		
	on SaveDef pressed do
		(
		SaveName = getsavefilename caption:"Load VDef file" types:".VDF files|*.VDF|"
		SaveFile = createfile SaveName
		format "VDF vertex tag file\n"n to: SaveFile
		format "BEGIN HEADER\n" to:SaveFile
		format "MeshName: \"%\"\n" TargetObj to:SaveFile
		format "% Vertices\n" targetobj.numverts to:Savefile
		format "pose: \"%\"\n" Setup.Posename.text to:SaveFile
		format "BEGIN VERTICES\n" to:SaveFile
		
		for i = 1 to targetObj.numverts do
			(
			Vpos =  targetobj.verts[i].pos
			format "% % % %\n" i vpos.x vpos.y vpos.z to:SaveFile
			)
		format "BEGIN TRIANGLES\n" to:SaveFile
		for h = 1 to targetobj.numfaces do
			(
			ThisFace = (getface targetobj h) as point3
			format "% % %\n" (Thisface.x as integer) (Thisface.y as integer) (ThisFace.z as integer) to:SaveFile
			)
		for j = 1 to NumRegions do
			(
			format "BEGIN REGION %\n" j to:SaveFile
			format "Name: \"%\"\n" namelist[j] to:SaveFile
			format "Color: %\n" clist[j] to:SaveFile
			for K = 1 to targetobj.numverts do
				(
				if ColorComp (getvertcolor targetobj k)  Clist[j] do 
					(format "%\n" K to:SaveFile)
				)
			)
		format "END\n" to:SaveFile
		close SaveFile
		)
	on HowManyRegions changed value do 
		(
		NumRegions = value
		Assemble_DTlist value
		)
		
	on Vcolor changed state do 
		(
		setcvertmode $ Vcolor.checked
		completeredraw()
		)
		
	on Vshade changed state do 
		(
		SetShadeCverts $ Vshade.checked
		completeredraw()
		)
		
	on TexMaps changed state do 
		(
		if $.material != undefined do
			(
			ShowTextureMap  $.material $.material.diffusemap Texmaps.checked
			completeredraw()
			)
		)
		
	on PickMesh picked TheMesh do
		(
		ChosenMesh.text =  (TheMesh.name as string)
		TargetObj = TheMesh
		DefaultVCfaces TheMesh
		Setup.SaveDef.enabled = true
		Setup.ChosenMesh.enabled = true
		
		)
	on ChosenMesh entered text do
		(
		Targetobj.name = (text as string)
		update targetobj
		)
	)
	
Rollout TagAreas "Regions"
(
)


TestWindow = NewRolloutFloater "Arggh me hearties" 320 512 
AddRollout Setup TestWindow
AddRollout TagAreas Testwindow
Assemble_DTlist 9


