/*
Blueprint Manager v1.00
Copyright 2010 Anton Vasilescu

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see http://www.gnu.org/licenses/.


*/

if blueprintManagerFloater != undefined then CloseRolloutFloater blueprintManagerFloater
(
	function fn_findPlane thePlane = 
	(
		tp = for o in objects where matchpattern o.name pattern:thePlane collect o
		return tp
	)
	
	function fn_findAllPlanes = 
	(
		tp = for o in objects where matchpattern o.name pattern:"XX_BP_*" collect o
		return tp
	)
	
	function fn_createPlanes topPlane bottomPlane frontPlane backPlane leftPlane rightPlane =
	(
		planesArray = #(topPlane, bottomPlane, frontPlane, backPlane, leftPlane, rightPlane)
		
		for i = 1 to planesArray.count do
		(
			namePrefix = #("XX_BP_TOP_","XX_BP_BOTTOM_","XX_BP_FRONT_","XX_BP_BACK_","XX_BP_LEFT_","XX_BP_RIGHT_" )
			if planesArray[i] != "" then
			(
				imgFile = planesArray[i]
				tempMap = openBitMap imgFile
				
				imgPlane = plane name:(namePrefix[i] + (getfilenameFile imgFile)) length:(tempMap.height) width:(tempMap.width) lengthsegs:1 widthsegs:1 mapCoords:true isselected:true
				
				imgPlane.renderable = off
				imgPlane.showFrozenInGray = off
				imgPlane.backfacecull = on
				
				imgPlane.wirecolor = color 88 177 26				
				imgMaterial = standard()
				imgMaterial.name = imgPlane.name
				imgMaterial.selfIllumAmount = 100
				
				imgTexture = bitmaptexture()
				imgTexture.bitmap = tempMap
				
				imgMaterial.diffuseMap = imgTexture
				imgPlane.material = imgMaterial
				imgMaterial.twoSided = on
				showTextureMap imgMaterial imgMaterial.diffuseMap on
				close tempMap
				
				case of
				(
					(i == 1):  -- top plane
						(
							imgPlane.rotation = (eulerangles 0 0 0)
							imgPlane.pivot = ((imgPlane.max + imgPlane.min)/2)
							imgPlane.pos = [0,0,0]
						)
					(i == 2):  -- bottom plane
						(
							imgPlane.rotation = (eulerangles 0 180 0)
							imgPlane.pivot = ((imgPlane.max + imgPlane.min)/2)
							imgPlane.pos = [0,0,0]
						)
					(i == 3):  -- front plane
						(
							imgPlane.rotation = (eulerangles -90 0 0)
							imgPlane.pivot = ((imgPlane.max + imgPlane.min)/2)
							imgPlane.pivot.z = imgPlane.min.z
							imgPlane.pos = [0,0,0]
						)
					(i == 4):  -- back plane
						(
							imgPlane.rotation = (eulerangles -90 180 0)
							imgPlane.pivot = ((imgPlane.max + imgPlane.min)/2)
							imgPlane.pivot.z = imgPlane.min.z
							imgPlane.pos = [0,0,0]
						)
					(i == 5):  -- left plane
						(
							imgPlane.rotation = (eulerangles 0 90 90)
							imgPlane.pivot = ((imgPlane.max + imgPlane.min)/2)
							imgPlane.pivot.z = imgPlane.min.z
							imgPlane.pos = [0,0,0]
						)
					(i == 6):  -- right plane
						(
							imgPlane.rotation = (eulerangles 0 -90 -90)
							imgPlane.pivot = ((imgPlane.max + imgPlane.min)/2)
							imgPlane.pivot.z = imgPlane.min.z
							imgPlane.pos = [0,0,0]
						)
				)
				max views redraw
			)
		)

		clearselection()
		for p in fn_findAllPlanes() do selectmore p
		
		try (fn_findPlane "XX_BP_BOTTOM_*")[1].pos.z = selection.max.z catch()
		try (fn_findPlane "XX_BP_FRONT_*")[1].pos.y = selection.max.y catch()
		try (fn_findPlane "XX_BP_BACK_*")[1].pos.y = selection.min.y catch()
		try (fn_findPlane "XX_BP_LEFT_*")[1].pos.x = selection.max.x catch()
		try (fn_findPlane "XX_BP_RIGHT_*")[1].pos.x = selection.min.x catch()

	)
	
	rollout rlt_planeCreation "Plane Creation"
	(
		group "Source Images"
		(
			label lbl_imgTop "Top Image: " align:#left
			edittext txt_imgTop "" width:195 align:#left offset:[-2,0] across:2
			button btn_browseTop "Browse" width:60 align:#right offset:[0,-2]

			label lbl_imgBottom "Bottom Image: " align:#left
			edittext txt_imgBottom "" width:195 align:#left offset:[-2,0] across:2
			button btn_browseBottom "Browse" width:60 align:#right offset:[0,-2]
			
			label lbl_imgFront "Front Image: " align:#left
			edittext txt_imgFront "" width:195 align:#left offset:[-2,0] across:2
			button btn_browseFront "Browse" width:60 align:#right offset:[0,-2]

			label lbl_imgBack "Back Image: " align:#left
			edittext txt_imgBack "" width:195 align:#left offset:[-2,0] across:2
			button btn_browseBack "Browse" width:60 align:#right offset:[0,-2]
			
			label lbl_imgLeft "Left Image: " align:#left
			edittext txt_imgLeft "" width:195 align:#left offset:[-2,0] across:2
			button btn_browseLeft "Browse" width:60 align:#right offset:[0,-2]
			
			label lbl_imgRight "Right Image: " align:#left
			edittext txt_imgRight "" width:195 align:#left offset:[-2,0] across:2
			button btn_browseright "Browse" width:60 align:#right offset:[0,-2]
		)

		button btn_createPlanes "Create Blueprint Planes" width:278 height:40
		
		on btn_browseTop pressed do
		(
			theFile = getOpenFileName()
			if theFile != undefined do txt_imgTop.text = theFile
		)
		
		on btn_browseBottom pressed do
		(
			theFile = getOpenFileName()
			if theFile != undefined do txt_imgBottom.text = theFile
		)

		on btn_browseFront pressed do
		(
			theFile = getOpenFileName()
			if theFile != undefined do txt_imgFront.text = theFile
		)
		
		on btn_browseBack pressed do
		(
			theFile = getOpenFileName()
			if theFile != undefined do txt_imgBack.text = theFile
		)
		
		on btn_browseLeft pressed do
		(
			theFile = getOpenFileName()
			if theFile != undefined do txt_imgLeft.text = theFile
		)
		
		on btn_browseRight pressed do
		(
			theFile = getOpenFileName()
			if theFile != undefined do txt_imgRight.text = theFile
		)
		
		on btn_createPlanes pressed do
		(
			existingPlanes = fn_findAllPlanes()
			if existingPlanes.count == 0 then
			(
				fn_createPlanes txt_imgTop.text txt_imgBottom.text txt_imgFront.text txt_imgBack.text txt_imgLeft.text txt_imgRight.text
				
				txt_imgTop.text = ""
				txt_imgBottom.text = ""
				txt_imgFront.text = ""
				txt_imgBack.text = ""
				txt_imgLeft.text = ""
				txt_imgRight.text = ""
			
				clearSelection()
			)
			else messagebox "The scene already contains a set of Blueprints.\nPlease delete or rename them before trying to create a new set."
		)
	)
	
	rollout rlt_planeManagement "Plane Management"
	(
		group "By Selection"
		(
			checkbox chk_top "Top" across:3
			checkbox chk_front "Front"
			checkbox chk_left "Left"
			checkbox chk_bottom "Bottom" across:3
			checkbox chk_back "Back"
			checkbox chk_right "Right"
			
			button btn_hideSelected "Hide Selected" across:2 align:#left width:130 offset:[-3,0]
			button btn_unhideSelected "Unhide Selected" width:130 offset:[3,0]
			
			button btn_freezeSelected "Freeze Selected" across:2 align:#left width:130 offset:[-3,0]
			button btn_unfreezeSelected "Unfreeze Selected" width:130 offset:[3,0]
		)
		
		group "Global"
		(		
			button btn_hidePlanes "Hide All Planes" across:2 align:#left width:130 offset:[-3,0]
			button btn_unhidePlanes "Unhide All Planes" width:130 offset:[3,0]
			
			button btn_freezePlanes "Freeze All Planes" across:2 align:#left width:130 offset:[-3,0]
			button btn_unfreezePlanes "Unfreeze All Planes" width:130 offset:[3,0]
		)
		
		on btn_hideSelected pressed do
		(
			if (chk_top.checked == true) then hide (fn_findPlane "XX_BP_TOP_*")[1]
			if (chk_front.checked == true) then hide (fn_findPlane "XX_BP_FRONT_*")[1]
			if (chk_left.checked == true) then hide (fn_findPlane "XX_BP_LEFT_*")[1]
			if (chk_bottom.checked == true) then hide (fn_findPlane "XX_BP_BOTTOM_*")[1]
			if (chk_back.checked == true) then hide (fn_findPlane "XX_BP_BACK_*")[1]
			if (chk_right.checked == true) then hide (fn_findPlane "XX_BP_RIGHT_*")[1]
		)
		on btn_unhideSelected pressed do
		(
			if (chk_top.checked == true) then unhide (fn_findPlane "XX_BP_TOP_*")[1]
			if (chk_front.checked == true) then unhide (fn_findPlane "XX_BP_FRONT_*")[1]
			if (chk_left.checked == true) then unhide (fn_findPlane "XX_BP_LEFT_*")[1]
			if (chk_bottom.checked == true) then unhide (fn_findPlane "XX_BP_BOTTOM_*")[1]
			if (chk_back.checked == true) then unhide (fn_findPlane "XX_BP_BACK_*")[1]
			if (chk_right.checked == true) then unhide (fn_findPlane "XX_BP_RIGHT_*")[1]
		)
		on btn_freezeSelected pressed do
		(
			if (chk_top.checked == true) then freeze (fn_findPlane "XX_BP_TOP_*")[1]
			if (chk_front.checked == true) then freeze (fn_findPlane "XX_BP_FRONT_*")[1]
			if (chk_left.checked == true) then freeze (fn_findPlane "XX_BP_LEFT_*")[1]
			if (chk_bottom.checked == true) then freeze (fn_findPlane "XX_BP_BOTTOM_*")[1]
			if (chk_back.checked == true) then freeze (fn_findPlane "XX_BP_BACK_*")[1]
			if (chk_right.checked == true) then freeze (fn_findPlane "XX_BP_RIGHT_*")[1]
		)
		on btn_unfreezeSelected pressed do
		(
			if (chk_top.checked == true) then unfreeze (fn_findPlane "XX_BP_TOP_*")[1]
			if (chk_front.checked == true) then unfreeze (fn_findPlane "XX_BP_FRONT_*")[1]
			if (chk_left.checked == true) then unfreeze (fn_findPlane "XX_BP_LEFT_*")[1]
			if (chk_bottom.checked == true) then unfreeze (fn_findPlane "XX_BP_BOTTOM_*")[1]
			if (chk_back.checked == true) then unfreeze (fn_findPlane "XX_BP_BACK_*")[1]
			if (chk_right.checked == true) then unfreeze (fn_findPlane "XX_BP_RIGHT_*")[1]
		)
		on btn_hidePlanes pressed do for i in fn_findAllPlanes() do hide i
		on btn_unhidePlanes pressed do for i in fn_findAllPlanes() do unhide i
		on btn_freezePlanes pressed do for i in fn_findAllPlanes() do freeze i
		on btn_unfreezePlanes pressed do for i in fn_findAllPlanes() do unfreeze i
		
	)
	
	rollout rlt_about "About"
	(
		label lbl_version "Version 1.00"
		label lbl_written_for "Written for " across:2 offset:[42,0]
		hyperLink lnk_cgTutsPlus "CG TUTS+" address:"http://cg.tutsplus.com" offset:[8,0] visitedColor:(color 200 200 200)

		label lbl_date "Date released: October 24rd, 2010"
		label lbl_spacer ""
		label lbl_writtenBy "Written by: Anton Vasilescu"
		hyperLink lnk_75ive "http://75ive.com" address:"http://75ive.com" align:#center visitedColor:(color 200 200 200)
	)	
	
	
	blueprintManagerFloater = newRolloutFloater "Blueprint Manager" 300 610
	addRollout rlt_planeCreation blueprintManagerFloater rolledUp:false
	addRollout rlt_planeManagement blueprintManagerFloater rolledUp:false
	addRollout rlt_about blueprintManagerFloater rolledUp:true
)
