/*

		Isometric Block Raster Script
		by Håkan Lundgren
		© 2011 Monovektor.com
	
		For more information on how it works visit:
			http://monovektor.com/2012/02/block-raster-revisited-revived-revised/

*/
script.coordinateSystem = 'bottom-up'
script.angleUnits = 'radians'

var block, blockStyle, shading, leftSide, rightSide, topSide

var values			=	{ sqrValue: 10, heightMultiplier: 40, widthMultiplier: 30, topLight: 1, topLightNumber: 100, leftLight: 0.5, leftLightNumber: 50, rightLight: 0.75, rightLightNumber: 75, elevate: 'Light', drawFlats: true, outlines: true, render: 'Render' }
var items			=	{ ruler0: { type: 'ruler', label: 'SETTINGS' },
						sqrValue: { type: 'number', label: 'Raster Square Size', steppers: true, units: 'point' },
						heightMultiplier: { type: 'number', label: 'Max Height', units: 'point', onChange: function(value){ items.drawFlatsThreshold.max = value } },
						widthMultiplier: { type: 'number', label: 'Block Size', units: 'point' },
						ruler1: { type: 'ruler', label: 'LIGHTING' },
						topLight: { type: 'slider', label: 'Top', range: [0, 1], increments: 0.05, onChange: function(value){ items.topLightNumber.value = value * 100 } },
						topLightNumber: { type: 'number', steppers: true, range: [0, 100], units: 'percent', onChange: function(value){ items.topLight.value = value / 100 } },
						leftLight: { type: 'slider', label: 'Left', range: [0, 1], increments: 0.05, onChange: function(value){ items.leftLightNumber.value = value * 100 } },
						leftLightNumber: { type: 'number', steppers: true, range: [0, 100], units: 'percent', onChange: function(value){ items.leftLight.value = value / 100 } },
						rightLight: { type: 'slider', label: 'Right', range: [0, 1], increments: 0.05, onChange: function(value){ items.rightLightNumber.value = value * 100 } },
						rightLightNumber: { type: 'number', steppers: true, range: [0, 100], units: 'percent', onChange: function(value){ items.rightLight.value = value / 100 } },
						ruler2: { type: 'ruler', label: 'DETAILS' },
						elevate: { type: 'list', label: 'Elevate', options: ['Light', 'Dark'] },
						drawFlats: { type: 'checkbox', label: 'Draw Non-Elevated', onChange: function(value){ items.drawFlatsThreshold.enabled = !value } },
						drawFlatsThreshold: { type: 'number', label: 'Threshold', steppers: true, min: 0, max: 40, enabled: false},
						outlines: { type: 'checkbox', label: 'Outlines' },
						render: { type: 'button', value: 'Render', onClick: function(){ blockRaster(values.sqrValue, values.widthMultiplier) } } }
var palette			=	new Palette('Block Raster v1.5', items, values)


function blockRaster(sqrValue, widthMultiplier){
			var getRasters		=	document.getItems([Raster, PlacedFile], {selected: true})

			if(!getRasters.length){ Dialog.alert('Select an image first.'); return false }

			raster				=	getRasters.first

			if(raster instanceof PlacedFile && !raster.eps){ raster = raster.embed(false) }

			var blockRasterGrp	=	new Group()
			blockRasterGrp.name	=	'Block Raster'

			var rectSize		=	new Size(sqrValue, sqrValue)
			var rasterWidth		=	raster.bounds.width
			var rasterHeight	=	raster.bounds.height
			var columns			=	Math.round(rasterWidth / sqrValue)
			var rows			=	Math.round(rasterHeight / sqrValue)
			var offset			=	raster.bounds.bottomLeft

			var threshold		=	items.drawFlatsThreshold.value
			lightArray			=	[items.topLight.value, items.leftLight.value, items.rightLight.value]
			blockStyle			=	{ strokeWidth: widthMultiplier / 100,
									strokeJoin: 'round',
									strokeColor: items.outlines.value ? '#000000' : null }

			for(var i = 0; i < rows; i++){
					var row = new Group()
					row.name = 'Row ' + (i + 1)

					for(var j = 0; j < columns; j++){
							block				=	new Group
							block.name			=	'Block ' + (j + 1)
			
					        var bottomLeft		=	offset + new Point(j, i) * rectSize
							var rectangle		=	new Rectangle(bottomLeft, rectSize)
			
							var color			=	raster.getAverageColor(rectangle)
							shading				=	{ topSide: [color.red * lightArray[0], color.green * lightArray[0], color.blue * lightArray[0]],
													leftSide: [color.red * lightArray[1], color.green * lightArray[1], color.blue * lightArray[1]],
													rightSide: [color.red * lightArray[2], color.green * lightArray[2], color.blue * lightArray[2]] }
			
							var fullHeight		=	(items.elevate.value == 'Light' ? (1 - color.gray) : color.gray) * items.heightMultiplier.value
							var fullWidth		=	widthMultiplier
							var halfWidth		=	widthMultiplier / 2
							var quarterWidth	=	widthMultiplier / 4
							var constantX 		=	-(i * halfWidth - j * halfWidth)
							var constantY 		=	j * quarterWidth + i * quarterWidth
							var points			=	[ new Point(constantX, constantY),											//		Point 0
													new Point(constantX, constantY + fullHeight),								//		Point 1
													new Point(constantX + halfWidth, constantY + fullHeight - quarterWidth),	//		Point 2
													new Point(constantX + halfWidth, constantY - quarterWidth),					//		Point 3
													new Point(constantX + fullWidth, constantY + fullHeight),					//		Point 4
													new Point(constantX + fullWidth, constantY),								//		Point 5
													new Point(constantX + halfWidth, constantY + fullHeight + quarterWidth)	]	//		Point 6

							if(fullHeight > threshold){																			//			     6
									leftSide = new Path([ points[0], points[1], points[2], points[3] ])							//		     ===   ===
									addStyle(leftSide)																			//		1 ==  topSide  == 4
																																//	   	 |   ===   ===   |
									rightSide = new Path([ points[3], points[2], points[4], points[5] ])						//	 	 |		 2		 |
									addStyle(rightSide)																			//	 	 | 		 | 		 |
							}																									//	 	 | left  | right |
																																//	 	 | side	 | side	 |
							if(items.drawFlats.value || fullHeight > threshold){												//	 	 |		 |		 |
									topSide = new Path([ points[1], points[6], points[4], points[2] ])							//		0 ==	 |	   == 5
									addStyle(topSide)																			//			 === | ===
							}																									//				 3

							row.appendBottom(block)
					}

					blockRasterGrp.appendBottom(row)
			}
}


function addStyle(obj){
			if(obj == leftSide) blockStyle.fillColor = shading.leftSide
			if(obj == rightSide) blockStyle.fillColor = shading.rightSide
			if(obj == topSide) blockStyle.fillColor = shading.topSide
			obj.style = blockStyle
			obj.closed = true
			block.appendChild(obj)
}