Subversion Repositories AndroidProjects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1051 chris 1
package com.gebauz.bauzoid.menu;
2
 
3
import com.gebauz.bauzoid.graphics.Font;
4
import com.gebauz.bauzoid.math.Vector2;
5
import com.gebauz.bauzoid.math.Vector4;
6
import com.gebauz.bauzoid.menu.MenuItem.HorizontalAlign;
7
import com.gebauz.bauzoid.menu.MenuItem.VerticalAlign;
8
import com.gebauz.bauzoid.parser.Preprocessor;
9
import com.gebauz.bauzoid.parser.ScanException;
10
import com.gebauz.bauzoid.parser.Tokenizer;
11
 
12
public class TextElement
13
{
14
 
15
        // Constants========================================================================================
16
 
17
        // Embedded Types===================================================================================
18
 
19
        // Members==========================================================================================
20
 
21
        private String mCaption = "";
22
        private float mScale = 1.0f;
23
        private HorizontalAlign mTextAlignH= HorizontalAlign.CENTER;
24
        private VerticalAlign mTextAlignV = VerticalAlign.CENTER;
25
        private String mFontName = null;
26
        private boolean mShadow = true;
27
        private Vector4 mShadowColor = new Vector4(0, 0, 0, 0.5f);
28
        private Vector2 mShadowOffset = new Vector2(2, 2);
29
 
30
        /** The owner of this element. */
31
        private MenuItem mOwner = null;
32
 
33
        private float mTextWidth = 0.0f;
34
        private float mTextHeight = 0.0f;
35
        private Font mFont = null;
36
        private float mLineBreak = 1.0f;
37
 
38
        // Methods==========================================================================================
39
 
40
        public TextElement(MenuItem owner)
41
        {
42
                mOwner = owner;
43
        }
44
 
45
        public void init()
46
        {
47
                // Get the first font that was added (typically the system font)
48
 
49
                if (mFontName != null)
50
                {
51
                        mFont = mOwner.getGame().getFont(mFontName);
52
                }
53
 
54
                if (mFont == null)
55
                {
56
                        mFont = mOwner.getGame().getFonts().getDefaultFont();
57
                }
58
 
59
                mFont.setLineBreak(mLineBreak);
60
 
61
                //mFont = PonPonChunCustomServices.getInstance().getInGameFont();
62
 
63
                /*if ((mFontName != null) && mFontName.equalsIgnoreCase("scoring"))
64
                {
65
                        mFont = PonPonChunCustomServices.getInstance().getScoringFont();
66
                }*/
67
 
68
                mTextWidth = mFont.getTextWidth(mCaption, mScale);
69
                mTextHeight = mFont.getTextHeight(mCaption, mScale);
70
        }
71
 
72
        public void exit()
73
        {
74
                mFont = null;
75
        }
76
 
77
        public void update(float deltaTime)
78
        {
79
 
80
        }
81
 
82
        public void render(float left, float top, float right, float bottom)
83
        {
84
                render(left, top, right, bottom, mOwner.getColor());
85
        }
86
 
87
        public void render(float left, float top, float right, float bottom, Vector4 color)
88
        {
89
                // Calculate position
90
                float x = left;
91
                float y = top;
92
                float w = right - left;
93
                float h = bottom - top;
94
 
95
                if (mTextAlignH == HorizontalAlign.RIGHT)
96
                        x = left + w - mTextWidth;
97
                else if (mTextAlignH == HorizontalAlign.CENTER)
98
                        x = left + w/2 - mTextWidth/2;
99
 
100
                if (mTextAlignV == VerticalAlign.BOTTOM)
101
                        y = top + h - mTextHeight;
102
                else if (mTextAlignV == VerticalAlign.CENTER)
103
                        y = top + h/2 - mTextHeight/2;
104
 
105
                if (mShadow)
106
                {
107
                        float oldAlpha = mShadowColor.w;
108
                        mShadowColor.w *= mOwner.getParent().getFadeAlpha();
109
                        mFont.drawText(mCaption, x + mShadowOffset.x, y + mShadowOffset.y, mShadowColor, mScale);
110
                        mShadowColor.w = oldAlpha;
111
                }
112
 
113
                float oldAlpha = mShadowColor.w;
114
                color.w *= mOwner.getParent().getFadeAlpha();          
115
                mFont.drawText(mCaption, x, y, color, mScale);
116
                color.w = oldAlpha;
117
        }
118
 
119
        public boolean parseLine(String identifier, Tokenizer tokenizer) throws ScanException
120
        {
121
                if (identifier.equalsIgnoreCase("caption"))
122
                {
123
                        mCaption = Preprocessor.escapeString(MenuUtil.parseString(tokenizer));
124
                }              
125
                else if (identifier.equalsIgnoreCase("scale"))
126
                {
127
                        mScale = MenuUtil.parseNumber(tokenizer);
128
                }
129
                else if (identifier.equalsIgnoreCase("textAlign"))
130
                {
131
                        mTextAlignH = MenuUtil.stringToHorizontalAlignment(tokenizer.readIdentifier());
132
                        tokenizer.readToken(",");
133
                        mTextAlignV = MenuUtil.stringToVerticalAlignment(tokenizer.readIdentifier());
134
                        tokenizer.readToken(";");
135
                }              
136
                else if (identifier.equalsIgnoreCase("font"))
137
                {
138
                        mFontName = MenuUtil.parseString(tokenizer);
139
                }
140
                else if (identifier.equalsIgnoreCase("lineBreak"))
141
                {
142
                        mLineBreak = MenuUtil.parseNumber(tokenizer);
143
                }
144
                else if (identifier.equalsIgnoreCase("shadow"))
145
                {
146
                        mShadow = MenuUtil.parseBool(tokenizer);
147
                }
148
                else if (identifier.equalsIgnoreCase("shadowOffset"))
149
                {
150
                        setShadowOffset(MenuUtil.parseVector2(tokenizer));
151
                }
152
                else if (identifier.equalsIgnoreCase("shadowColor"))
153
                {
154
                        setShadowColor(MenuUtil.parseVector4(tokenizer));
155
                }
156
                else
157
                {
158
                        return false;
159
                }
160
                return true;
161
        }
162
 
163
        // Getters/Setters==================================================================================
164
 
165
        public String getCaption() { return mCaption; }
166
        public void setCaption(String caption)
167
        {
168
                mCaption = caption;
169
                mTextWidth = mFont.getTextWidth(mCaption, mScale);
170
                mTextHeight = mFont.getTextHeight(mCaption, mScale);   
171
        }
172
 
173
        public float getScale() { return mScale; }
174
        public void setScale(float scale)
175
        {
176
                mScale = scale;
177
                mTextWidth = mFont.getTextWidth(mCaption, mScale);
178
                mTextHeight = mFont.getTextHeight(mCaption, mScale);
179
        }
180
 
181
        public float getLineBreak() { return mLineBreak; }
182
        public void setLineBreak(float lineBreak)
183
        {
184
                mLineBreak = lineBreak;
185
                mFont.setLineBreak(mLineBreak);
186
                mTextHeight = mFont.getTextHeight(mCaption, mScale);
187
        }
188
 
189
        public float getTextWidth() { return mTextWidth; }
190
        public float getTextHeight() { return mTextHeight; }
191
 
192
        public HorizontalAlign getTextAlignH() { return mTextAlignH; }
193
        public void setTextAlignH(HorizontalAlign textAlignH) { mTextAlignH = textAlignH; }
194
 
195
        public VerticalAlign getTextAlignV() { return mTextAlignV; }
196
        public void setTextAlignV(VerticalAlign textAlignV) { mTextAlignV = textAlignV; }
197
 
198
        public boolean isShadow() { return mShadow; }
199
        public void setShadow(boolean shadow) { mShadow = shadow; }
200
 
201
        public Vector4 getShadowColor() { return mShadowColor; }
202
        public void setShadowColor(Vector4 shadowColor) { mShadowColor = shadowColor; }
203
 
204
        public Vector2 getShadowOffset() { return mShadowOffset; }
205
        public void setShadowOffset(Vector2 shadowOffset) { mShadowOffset = shadowOffset; }
206
}