Subversion Repositories AndroidProjects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1051 chris 1
package com.gebauz.bauzoid.graphics.sprite;
2
 
3
import com.badlogic.gdx.graphics.Texture;
4
 
5
/** A region that addresses a single rectangular region inside a sprite's texture. */
6
public class SpriteRegion
7
{
8
        // Constants========================================================================================
9
 
10
        // Embedded Types===================================================================================
11
 
12
        // Fields===========================================================================================
13
 
14
        public float left = 0;
15
        public float top = 0;
16
        public float bottom = 0;
17
        public float right = 0;
18
 
19
        private Sprite mParentSprite = null;
20
        private int mRegionIndex = -1;
21
 
22
        // Methods==========================================================================================
23
 
24
        /** Constructor. */
25
        public SpriteRegion(Sprite parentSprite, int index)
26
        {
27
                mParentSprite = parentSprite;
28
                mRegionIndex = index;
29
        }
30
 
31
        /** Constructor - sets region by relative texture coordinates. */
32
        public SpriteRegion(Sprite parentSprite, int index, float relativeTop, float relativeLeft, float relativeRight, float relativeBottom)
33
        {
34
                this(parentSprite, index, relativeTop, relativeLeft, relativeRight - relativeTop, relativeBottom - relativeTop, false);        
35
        }
36
 
37
        /** Constructor - sets region by absolute texture coordinates in texels. Requires reference texture to calculate relative texture coordinates. */
38
        public SpriteRegion(Sprite parentSprite, int index, float x, float y, float w, float h, boolean absolute)
39
        {
40
                this(parentSprite, index);
41
                if (absolute)
42
                        setAbsolute(parentSprite.getTexture(), x, y, w, h);
43
                else
44
                        setRelative(x, y, w, h);
45
        }
46
 
47
        // Getters/Setters==================================================================================
48
 
49
        /** Set region by relative texel coordinates.
50
         * Coordinates are in relative space [0..1].
51
         */
52
        public void setRelative(float x, float y, float w, float h)
53
        {
54
                left = x;
55
                top = y;
56
                right = left + w;
57
                bottom = top + h;
58
        }
59
 
60
        /** Set region by absolute pixel coordinates relative to the specified texture.
61
         * Coordinates are in absolute texel space.
62
         */
63
        public void setAbsolute(Texture referenceTexture, float x, float y, float w, float h)
64
        {
65
                left = x / referenceTexture.getWidth();
66
                top = y / referenceTexture.getHeight();
67
                right = left + (w / referenceTexture.getWidth());
68
                bottom = top + (h / referenceTexture.getHeight());
69
        }
70
 
71
        /** Get the region's width in relative texel coordinates. */
72
        public float getWidth()
73
        {
74
                return (right-left);
75
        }
76
 
77
        /** Get the region's height in relative texel coordinates. */
78
        public float getHeight()
79
        {
80
                return (bottom-top);
81
        }
82
 
83
        /** Get the region's parent sprite. */
84
        public Sprite getSprite()
85
        {
86
                return mParentSprite;
87
        }
88
 
89
        /** Get the region's index. */
90
        public final int getRegionIndex()
91
        {
92
                return mRegionIndex;
93
        }
94
}
95
 
96