Subversion Repositories AndroidProjects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
835 chris 1
package com.gebauz.bauzoid.menu;
2
 
3
import com.gebauz.bauzoid.game.Game;
4
import com.gebauz.bauzoid.game.GameObject;
5
import com.gebauz.bauzoid.math.Vector2;
6
import com.gebauz.bauzoid.math.Vector4;
7
import com.gebauz.bauzoid.parser.ScanException;
8
import com.gebauz.bauzoid.parser.Tokenizer;
9
 
10
 
11
/** Base class for all Menu items.
12
 * MenuItem has the following additional properties:
13
 * - position
14
 * - size
15
 * - align
16
 *
17
 * Note: different actual menu items might interpret these properties differently.
18
 *
19
 * @author chiu
20
 *
21
 */
22
public abstract class MenuItem extends GameObject
23
{
24
        // Constants========================================================================================
25
 
26
        // Embedded Types===================================================================================
27
 
28
        public enum HorizontalAlign
29
        {
30
                LEFT,
31
                CENTER,
32
                RIGHT,
33
        }
34
 
35
        public enum VerticalAlign
36
        {
37
                TOP,
38
                CENTER,
39
                BOTTOM,
40
        }
41
 
42
        public static interface UpdateListener
43
        {
44
                public void onUpdate(MenuItem sender, float deltaTime);
45
        }
46
 
47
        // Fields===========================================================================================
48
        private Menu mParentMenu = null;
49
        private String mName = "";
50
 
51
        private Vector2 mPosition = new Vector2(0, 0);
52
        private Vector2 mSize = new Vector2(0, 0);
53
        private HorizontalAlign mHorizontalAlignment = HorizontalAlign.LEFT;
54
        private VerticalAlign mVerticalAlignment = VerticalAlign.TOP;
55
        private Vector4 mColor = new Vector4(1, 1, 1, 1);
56
 
57
        /** Whether this item should be rendered. */
58
        private boolean mIsVisible = true;
59
 
60
        /** Whether this item should react to input. */
61
        private boolean mIsEnabled = true;
62
 
63
        private UpdateListener mUpdateListener = null;
64
 
65
        // Methods==========================================================================================
66
 
67
        public MenuItem(Game game, Menu parent, String name)
68
        {
69
                super(game);
70
                mParentMenu = parent;
71
                mName = name;
72
        }
73
 
74
        /** Perform initialization. */
75
        public abstract void init();
76
 
77
        /** Destroy resources. */
78
        public abstract void exit();
79
 
80
        /** Update internal structures. */
81
        public abstract void update(float deltaTime);
82
 
83
        /** Handle input if applicable. */
84
        public void handleInput(float deltaTime) {}
85
 
86
        /** Render the UI element. */
87
        public abstract void render();
88
 
89
        /** Parse an instruction line in the body of the MenuItem.
90
         * Returns true if the identifier has been processed, or false if not. The caller should then
91
         * appropiately process the identifier in another way.
92
         * @param identifier Identifier of statement to be processed
93
         * @param tokenizer Tokenizer parsing the statement
94
         * @return true if the statement has been processed.
95
         * @throws ScanException
96
         */
97
        public boolean parseLine(String identifier, Tokenizer tokenizer) throws ScanException  
98
        {
99
                if (identifier.equalsIgnoreCase("position"))
100
                {
101
                        setPosition(MenuUtil.parseVector2(tokenizer));
102
                }
103
                else if (identifier.equalsIgnoreCase("size"))
104
                {
105
                        setSize(MenuUtil.parseVector2(tokenizer));
106
                }
107
                else if (identifier.equalsIgnoreCase("align"))
108
                {
109
                        mHorizontalAlignment = MenuUtil.stringToHorizontalAlignment(tokenizer.readIdentifier());
110
                        tokenizer.readToken(",");
111
                        mVerticalAlignment = MenuUtil.stringToVerticalAlignment(tokenizer.readIdentifier());
112
                        tokenizer.readToken(";");
113
                }
114
                else if (identifier.equalsIgnoreCase("color"))
115
                {
116
                        setColor(MenuUtil.parseVector4(tokenizer));
117
                }
118
                else if (identifier.equalsIgnoreCase("visible"))
119
                {
120
                        mIsVisible = MenuUtil.parseBool(tokenizer);
121
                }
122
                else if (identifier.equalsIgnoreCase("enabled"))
123
                {
124
                        mIsEnabled = MenuUtil.parseBool(tokenizer);
125
                }
126
                else
127
                {
128
                        // When reached here, nothing interpreted the identifer, so we can throw a ScanException
129
                        throw new ScanException("Syntax error", tokenizer.getSurroundings());
130
                }
131
 
132
                return true;
133
        }
134
 
135
        // Getters/Setters==================================================================================
136
 
137
        public Menu getParent() { return mParentMenu; }
138
 
139
        public String getName() { return mName; }
140
 
141
        public Vector2 getPosition() { return mPosition; }
142
        public void setPosition(Vector2 position) { mPosition = position; }
143
        public void setPosition(float x, float y) { mPosition.x = x; mPosition.y = y; }
144
 
145
        public float getPositionX() { return mPosition.x; }
146
        public void setPositionX(float positionX) { mPosition.x = positionX; }
147
 
148
        public float getPositionY() { return mPosition.y;}
149
        public void setPositionY(float positionY) { mPosition.y = positionY; }
150
 
151
        public HorizontalAlign getHorizontalAlignment() { return mHorizontalAlignment;}
152
        public void getHorizontalAlignment(HorizontalAlign horizonalAlignment) { mHorizontalAlignment = horizonalAlignment;}
153
 
154
        public VerticalAlign getVerticalAlignment() { return mVerticalAlignment; }
155
        public void setVerticalAlignment(VerticalAlign verticalAlignment) { mVerticalAlignment = verticalAlignment;}
156
 
157
        public Vector4 getColor() { return mColor; }
158
        public void setColor(Vector4 color) { mColor = color; }
159
 
160
        public Vector2 getSize() { return mSize; }
161
        public void setSize(Vector2 size) { mSize = size; }
162
 
163
        public float getWidth() { return mSize.x; }
164
        public void setWidth(float width) { mSize.x = width; }
165
 
166
        public final float getHeight() { return mSize.y; }
167
        public void setHeight(float height) { mSize.y = height; }
168
 
169
        public boolean isVisible() { return mIsVisible; }
170
        public void setVisible(boolean visible) { mIsVisible = visible; }
171
 
172
        public boolean isEnabled() { return mIsEnabled; }
173
        public void setEnabled(boolean enabled) { mIsEnabled = enabled; }
174
 
175
        public void setUpdateListener(UpdateListener listener) { mUpdateListener = listener; }
176
        public UpdateListener getUpdateListener() { return mUpdateListener; }
177
 
178
        public float getLeft()
179
        {
180
                if (mHorizontalAlignment == HorizontalAlign.RIGHT)
181
                        return mPosition.x - mSize.x;
182
                else if (mHorizontalAlignment == HorizontalAlign.CENTER)
183
                        return mPosition.x - mSize.x/2;
184
                else //if (mHorizontalAlignment == HorizontalAlignment.LEFT)
185
                        return mPosition.x;
186
        }
187
 
188
        public float getTop()
189
        {
190
                if (mVerticalAlignment == VerticalAlign.BOTTOM)
191
                        return mPosition.y - mSize.y;
192
                else if (mVerticalAlignment == VerticalAlign.CENTER)
193
                        return mPosition.y - mSize.y/2;
194
                else //if (mHorizontalAlignment == HorizontalAlignment.LEFT)
195
                        return mPosition.y;
196
        }
197
 
198
        public float getRight()
199
        {
200
                if (mHorizontalAlignment == HorizontalAlign.RIGHT)
201
                        return mPosition.x;
202
                else if (mHorizontalAlignment == HorizontalAlign.CENTER)
203
                        return mPosition.x + mSize.x/2;
204
                else //if (mHorizontalAlignment == HorizontalAlignment.LEFT)
205
                        return mPosition.x + mSize.x;
206
        }
207
 
208
        public float getBottom()
209
        {
210
                if (mVerticalAlignment == VerticalAlign.BOTTOM)
211
                        return mPosition.y;
212
                else if (mVerticalAlignment == VerticalAlign.CENTER)
213
                        return mPosition.y + mSize.y/2;
214
                else //if (mHorizontalAlignment == HorizontalAlignment.LEFT)
215
                        return mPosition.y + mSize.y;
216
        }
217
 
218
 
219
}