//--------------------------------------
//               PowerUI
//
//        For documentation or 
//    if you have any issues, visit
//        powerUI.kulestar.com
//
//    Copyright  2013 Kulestar Ltd
//          www.kulestar.com
//--------------------------------------

using System;


namespace Css{
	
	/// <summary>
	/// Represents a stack of transformations (e.g. scale, translate, rotate).
	/// <see cref="PowerUI.Renderman.Transformations"/> The renderman maintains
	/// a stack of transformations. The frame on the top of the stack is applied
	/// to each element being renderered as a post process.
	/// </summary>
	
	public class TransformationStack{
		
		/// <summary>The last element on the stack - it's at the top.</summary>
		public Transformation Last;
		/// <summary>The first element on the stack - it's at the bottom.</summary>
		public Transformation First;
		
		/// <summary>Add a transformation to the top of the stack.</summary>
		/// <param name="transform">The transformation to add.</param>
		public void Push(Transformation transform){
			if(First==null){
				First=Last=transform;
			}else{
				transform.Parent=Last;
				Last=transform;
			}
		}
		
		/// <summary>Add a raw matrix to the top of the stack.</summary>
		/// <param name="transform">The matrix to add.</param>
		public void Push(UnityEngine.Matrix4x4 matrix){	
			
			// Create:
			Transformation transform=new Transformation(matrix);
			
			if(First==null){
				First=Last=transform;
			}else{
				transform.Parent=Last;
				Last=transform;
			}
		}
		
		/// <summary>Removes a transformation from the top of the stack.</summary>
		public void Pop(){
			if(Last==null){
				return;
			}
			Last=Last.Parent;
		}
		
		/// <summary>Clears all transformations from the stack.</summary>
		public void Clear(){
			First=Last=null;
		}
		
	}
	
}