Subversion Repositories AndroidProjects

Rev

Blame | Last modification | View Log | RSS feed

package com.gebauz.bauzoid.graphics.spritex;

import com.gebauz.bauzoid.math.Matrix4;
import com.gebauz.bauzoid.math.Vector2;
import com.gebauz.bauzoid.math.Vector4;
import com.gebauz.bauzoid.math.collisionx.AABoundingBox;

public class SpriteParameters
{

        // Constants========================================================================================

        // Embedded Types===================================================================================

        // Fields===========================================================================================

        public float x = 0.0f;
        public float y = 0.0f;
        public float w = 0.0f;
        public float h = 0.0f;
        public float angle = 0.0f;
        public float alpha = 1.0f;
        public Vector4 color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f);
       
        public boolean mirrorX = false;
        public boolean mirrorY = false;
       
        /** Rotation and scaling pivot point in absolute coordinates. */
        public float pivotX = 0.0f;
        public float pivotY = 0.0f;
       
        // Methods==========================================================================================

        public SpriteParameters()
        {

        }
       
        /** Get the upper left corner as a Vector2. */
        public Vector2 getTopLeft()
        {
                return new Vector2(x - pivotX, y - pivotY);
        }
       
        /** Get the upper left corner as a Vector2. */
        public Vector2 getTopRight()
        {
                return new Vector2(x - pivotX + w, y - pivotY);
        }
       
        /** Get the upper left corner as a Vector2. */
        public Vector2 getBottomLeft()
        {
                return new Vector2(x - pivotX, y - pivotY + h);
        }
       
        /** Get the upper left corner as a Vector2. */
        public Vector2 getBottomRight()
        {
                return new Vector2(x - pivotX + w, y - pivotY + h);
        }
       
        /** Check if the point is inside the sprite. */
        public boolean isInside(float x, float y)
        {
                Vector2 topLeft = getTopLeft();
                Vector2 bottomRight = getBottomRight();
               
                return ( (x >= topLeft.x) && (y >= topLeft.y) &&
                                 (x <= bottomRight.x) && (y <= bottomRight.y) );
        }
       
        public void centerPivot()
        {
                pivotX = w/2.0f;
                pivotY = h/2.0f;
        }
       
        public void apply(SpriteParameters param)
        {
                x = param.x;
                y = param.y;
                w = param.w;
                h = param.h;
                alpha = param.alpha;
                color = param.color.copy();
                pivotX = param.pivotX;
                pivotY = param.pivotY;
                angle = param.angle;
                mirrorX = param.mirrorX;
                mirrorY = param.mirrorY;
        }
       
        public final Matrix4 getTransform()
        {
                Matrix4 result = Matrix4.createIdentity();

                //Matrix4.multiply(result, result, Matrix4.createScale((mirrorX ? -1 : 1), (mirrorY ? -1 : 1), 1));
               
                Matrix4.multiply(result,  result, Matrix4.createTranslation(-0.5f, -0.5f, 0));
                Matrix4.multiply(result, result, Matrix4.createScale((mirrorX ? -1 : 1), (mirrorY ? -1 : 1), 1));
                Matrix4.multiply(result,  result, Matrix4.createTranslation(0.5f, 0.5f, 0));
                Matrix4.multiply(result, result, Matrix4.createScale(w, h, 1.0f));
                Matrix4.multiply(result,  result, Matrix4.createTranslation(-pivotX, -pivotY, 0));
                Matrix4.multiply(result, result, Matrix4.createRotationZ(angle));
                Matrix4.multiply(result,  result, Matrix4.createTranslation(x, y, 0));
               
               
                //Matrix4.multiply(result,  result, Matrix4.createTranslation(-pivotX, -pivotY, 0));
                //Matrix4.multiply(result,  result, Matrix4.createTranslation(-pivotX, -pivotY, 0));
                //Matrix4.multiply(result, result, Matrix4.createRotationZ(angle));
                //Matrix4.multiply(result, result, Matrix4.createTranslation(x, y, 0));

               
/*              Matrix4.multiply(result, result, Matrix4.createScale((mirrorX ? -1 : 1), (mirrorY ? -1 : 1), 1));
                Matrix4.multiply(result, result, Matrix4.createScale(w/2, h/2, 1.0f));
                Matrix4.multiply(result,  result, Matrix4.createTranslation(-pivotX+w/2, -pivotY+h/2, 0));
                Matrix4.multiply(result, result, Matrix4.createRotationZ(angle));
                Matrix4.multiply(result, result, Matrix4.createTranslation(x, y, 0));*/

               
                return result;
        }
       
        /** Transform a point that is specified in sprite space. */
/*      public Vector2 transformPoint(float pX, float pY)
        {
                // bottom left and top right corner points relative to origin
                final float worldOriginX = x + pivotX;
                final float worldOriginY = y + pivotY;
               
                float fx = -pivotX;
                float fy = -pivotY;

                if (mirrorX)
                {
                        pX = w - pX;
                }
               
                if (mirrorY)
                {
                        pY = h - pY;                   
                }
               
                // point in world space
                final float p1x = fx + pX;
                final float p1y = fy + pY;
               
                float x1;
                float y1;
               
                // rotate
                if (angle != 0)
                {
                        final float cos = MathUtil.cos(angle);
                       
                        // TODO: changing this sign might require changes elsewhere - currently angle is CCW defined!
                        final float sin = -MathUtil.sin(angle);

                        x1 = cos * p1x - sin * p1y;
                        y1 = sin * p1x + cos * p1y;
                }
                else
                {
                        x1 = p1x;
                        y1 = p1y;
                }
               
                x1 += worldOriginX - pivotX;
                y1 += worldOriginY - pivotY;
               
                return new Vector2(x1, y1);            
        }*/

       
        public AABoundingBox getBoundingBox()
        {
                // get a rough bounding box
                float maxWidth = Math.max(pivotX, w - pivotX)*1.5f;
                float maxHeight = Math.max(pivotY, h - pivotY)*1.5f;
               
                return new AABoundingBox(x - maxWidth, y - maxHeight, x + maxWidth, y + maxHeight);
        }
       
        // Getters/Setters==================================================================================

}