Subversion Repositories AndroidProjects

Rev

Rev 106 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
104 chris 1
package com.gebauz.pingK.game;
2
 
107 chris 3
import com.gebauz.pingK.MultitouchInput;
4
import com.gebauz.pingK.MultitouchInput.Finger;
5
 
104 chris 6
public class TextButton
7
{
107 chris 8
        public static class TouchListener
9
        {
10
                public void onTouched()
11
                {                      
12
                }
13
        }
14
 
15
        public enum HorizontalAlignment
16
        {
17
                LEFT,
18
                CENTER,
19
                RIGHT
20
        };
21
 
22
        public enum VerticalAlignment
23
        {
24
                TOP,
25
                MIDDLE,
26
                BOTTOM
27
        };
28
 
29
        public enum Style
30
        {
31
                PUSHBUTTON,
32
                TOGGLEBUTTON           
33
        };
34
 
104 chris 35
        private Font mFont = null;
36
        private String mText;
37
 
107 chris 38
        public float x = 0.0f;
39
        public float y = 0.0f;
40
        public float scale = 1.0f;
104 chris 41
 
107 chris 42
        private HorizontalAlignment mHorizontalAlignment = HorizontalAlignment.LEFT;
43
        private VerticalAlignment mVerticalAlignment = VerticalAlignment.TOP;
44
        private Style mStyle = Style.PUSHBUTTON;
45
 
46
        private boolean mWasTouched = false;
47
        private boolean mActivated = false;
48
 
49
        private TouchListener mTouchListener = null;
50
 
104 chris 51
        public TextButton(Font font, String text)
52
        {
53
                mFont = font;
54
                mText = text;
55
        }
56
 
57
        public void update(float deltaTime)
58
        {
107 chris 59
                float topLeftX = getTopLeftX();
60
                float topLeftY = getTopLeftY();
61
                float bottomRightX = topLeftX + getWidth();
62
                float bottomRightY = topLeftY + getHeight();
104 chris 63
 
107 chris 64
                Finger finger = MultitouchInput.getInstance().getTouchPointInside(topLeftX, topLeftY, bottomRightX, bottomRightY);
65
                if ((finger != null) && (!mWasTouched))
66
                {
67
                        // activate
68
                        if (mTouchListener != null)
69
                        {
70
                                mTouchListener.onTouched();
71
 
72
                                if (mStyle == Style.PUSHBUTTON)
73
                                        mActivated = true;
74
                        }
75
                }
76
 
77
                mWasTouched = (finger != null);
104 chris 78
        }
79
 
80
        public void render()
81
        {
107 chris 82
                if (mText.length() > 0)
83
                {                                              
84
                        if (mActivated)
85
                        {
86
                                float offsetX = Font.CHARACTER_SIZE * scale;
87
                                mFont.drawText("=" + mText + "=", getTopLeftX() - offsetX, getTopLeftY(), scale);
88
                        }
89
                        else
90
                        {
91
                                mFont.drawText(mText, getTopLeftX(), getTopLeftY(), scale);
92
                        }
93
                }
104 chris 94
        }
95
 
96
        public void setText(String text)
97
        {
98
                mText = text;
99
        }
100
 
101
        public String getText()
102
        {
103
                return mText;
104
        }
107 chris 105
 
106
        public float getTopLeftX()
107
        {
108
                switch (mHorizontalAlignment)
109
                {
110
                case LEFT:
111
                        return x;
112
                case CENTER:
113
                        return (x - (getWidth() / 2.0f));
114
                case RIGHT:
115
                        return (x - getWidth());
116
                }
117
 
118
                return x;
119
        }
120
 
121
        public float getTopLeftY()
122
        {
123
                switch (mVerticalAlignment)
124
                {
125
                case TOP:
126
                        return y;
127
                case MIDDLE:
128
                        return (y - (getHeight() / 2.0f));
129
                case BOTTOM:
130
                        return (y - getHeight());
131
                }
132
 
133
                return y;
134
        }
104 chris 135
 
136
        public float getWidth()
137
        {
138
                return ((float)mText.length() * (float)Font.CHARACTER_SIZE * scale);
139
        }
140
 
141
        public float getHeight()
142
        {
143
                return ((float)Font.CHARACTER_SIZE * scale);
144
        }
107 chris 145
 
146
        public void setAlignment(HorizontalAlignment horizontal, VerticalAlignment vertical)
147
        {
148
                mHorizontalAlignment = horizontal;
149
                mVerticalAlignment = vertical;
150
        }
151
 
152
        public void setTouchListener(TouchListener listener)
153
        {
154
                mTouchListener = listener;
155
        }
156
 
157
        public void setStyle(Style style)
158
        {
159
                mStyle = style;
160
        }
104 chris 161
}
106 chris 162