using UnityEngine;
using System.Collections;

/*
 * Convert real world distances to pixels
 */

namespace com.deeperbeige.lib
{
	public class ScreenSize
	{
		private static int   _pixelsPerCentimeter = -1;
		private const  int   minimumDPI           = 72;
		private const  float centimetersPerInch   = 2.5441f;
		
		// Converts given length in real world centimeters to equivelent length in screen pixels
		public static float centimetersToPixels(float cm)
		{
			return cm * pixelsPerCentimeter;
		}

		// Converts given length in screen pixels into real world centimeters
		public static float pixelsToCentimeters(float px)
		{
			return px / pixelsPerCentimeter;
		}

		// Work out pixels per centimeter and cache the result
		public static float pixelsPerCentimeter
		{
			get
			{
				if (_pixelsPerCentimeter >= 0) return _pixelsPerCentimeter;
			
				_pixelsPerCentimeter = Mathf.RoundToInt(Screen.dpi / centimetersPerInch);
				if (pixelsPerInch < minimumDPI) _pixelsPerCentimeter = Mathf.RoundToInt(minimumDPI / centimetersPerInch);
				
				return _pixelsPerCentimeter;
			}
		}
		
		public static float pixelsPerInch
		{
			get
			{
				return (float)pixelsPerCentimeter * centimetersPerInch;
			}
		}
	}
}
