Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1051 | chris | 1 | package com.gebauz.bauzoid.menu; |
| 2 | |||
| 3 | import com.badlogic.gdx.audio.Sound; |
||
| 4 | import com.gebauz.bauzoid.game.Game; |
||
| 5 | import com.gebauz.bauzoid.input.Input; |
||
| 6 | import com.gebauz.bauzoid.input.Input.Finger; |
||
| 7 | import com.gebauz.bauzoid.math.Vector2; |
||
| 8 | import com.gebauz.bauzoid.math.Vector4; |
||
| 9 | import com.gebauz.bauzoid.parser.ScanException; |
||
| 10 | import com.gebauz.bauzoid.parser.Tokenizer; |
||
| 11 | |||
| 12 | |||
| 13 | /** Implements a Text Button Menu item. |
||
| 14 | * The Button has the following additional properties: |
||
| 15 | * - onTouch |
||
| 16 | * - pushColor |
||
| 17 | * |
||
| 18 | * The size parameter is unused/ignored. The text is aligned relative to position. |
||
| 19 | * |
||
| 20 | * @author chiu |
||
| 21 | * |
||
| 22 | */ |
||
| 23 | public class Button extends MenuItem |
||
| 24 | { |
||
| 25 | // Constants======================================================================================== |
||
| 26 | |||
| 27 | public static String EVENT_TOUCH = "TOUCH"; |
||
| 28 | |||
| 29 | // Embedded Types=================================================================================== |
||
| 30 | |||
| 31 | // Fields=========================================================================================== |
||
| 32 | |||
| 33 | private String mOnTouchParam = ""; |
||
| 34 | private Vector4 mPushColor = new Vector4(0.5f, 0.5f, 0.5f, 1.0f); |
||
| 35 | private Vector2 mPushOffset = new Vector2(1, 1); |
||
| 36 | |||
| 37 | private Vector2 mActiveMargin = new Vector2(0, 0); |
||
| 38 | |||
| 39 | /** Sound for button pushes. */ |
||
| 40 | private Sound mPushSound = null; |
||
| 41 | |||
| 42 | /** Sound for button clicks. */ |
||
| 43 | private Sound mClickSound = null; |
||
| 44 | |||
| 45 | private TextElement mText = null; |
||
| 46 | |||
| 47 | private boolean mIsTouching[] = new boolean[Input.NUM_FINGERS]; |
||
| 48 | private boolean mIsFingerInside[] = new boolean[Input.NUM_FINGERS]; |
||
| 49 | |||
| 50 | |||
| 51 | |||
| 52 | // Methods========================================================================================== |
||
| 53 | |||
| 54 | public Button(Game game, Menu parent, String name) |
||
| 55 | { |
||
| 56 | super(game, parent, name); |
||
| 57 | mText = new TextElement(this); |
||
| 58 | |||
| 59 | for (int i = 0; i < Input.NUM_FINGERS; i++) |
||
| 60 | mIsTouching[i] = false; |
||
| 61 | } |
||
| 62 | |||
| 63 | @Override |
||
| 64 | public void init() |
||
| 65 | { |
||
| 66 | mText.init(); |
||
| 67 | |||
| 68 | // default width/height is the text element |
||
| 69 | if (getWidth() == 0.0f) |
||
| 70 | setWidth(mText.getTextWidth()); |
||
| 71 | if (getHeight() == 0.0f) |
||
| 72 | setHeight(mText.getTextHeight()); |
||
| 73 | } |
||
| 74 | |||
| 75 | @Override |
||
| 76 | public void exit() |
||
| 77 | { |
||
| 78 | mText.exit(); |
||
| 79 | |||
| 80 | mPushSound = null; |
||
| 81 | mClickSound = null; |
||
| 82 | } |
||
| 83 | |||
| 84 | @Override |
||
| 85 | public void update(float deltaTime) |
||
| 86 | { |
||
| 87 | mText.update(deltaTime); |
||
| 88 | |||
| 89 | /* if (isEnabled() && getParent().isEnabled()) |
||
| 90 | handleInput(deltaTime);*/ |
||
| 91 | } |
||
| 92 | |||
| 93 | public void handleInput(float deltaTime) |
||
| 94 | { |
||
| 95 | Input input = getGame().getInput(); |
||
| 96 | |||
| 97 | boolean prevButtonDown = isButtonDown(); |
||
| 98 | |||
| 99 | for (int i = 0; i < Input.NUM_FINGERS; i++) |
||
| 100 | { |
||
| 101 | Finger finger = input.getFinger(i); |
||
| 102 | if (finger != null) |
||
| 103 | { |
||
| 104 | mIsFingerInside[i] = (finger.isInsideVirtual(getLeft() - mActiveMargin.x, getTop() - mActiveMargin.y, getRight() + mActiveMargin.x, getBottom() + mActiveMargin.y)); |
||
| 105 | |||
| 106 | if (finger.isJustTouched() && mIsFingerInside[i]) |
||
| 107 | { |
||
| 108 | mIsTouching[i] = true; |
||
| 109 | } |
||
| 110 | |||
| 111 | if (finger.isJustReleased() && mIsTouching[i]) |
||
| 112 | { |
||
| 113 | if (mIsFingerInside[i]) |
||
| 114 | { |
||
| 115 | if ((mClickSound != null) && (getAudio().isSoundEnabled())) |
||
| 116 | mClickSound.play(); |
||
| 117 | |||
| 118 | getParent().sendEvent(this, EVENT_TOUCH, mOnTouchParam); |
||
| 119 | } |
||
| 120 | |||
| 121 | mIsTouching[i] = false; |
||
| 122 | } |
||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | if (!prevButtonDown && isButtonDown()) |
||
| 127 | { |
||
| 128 | if ((mPushSound != null) && (getAudio().isSoundEnabled())) |
||
| 129 | mPushSound.play(); |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | @Override |
||
| 134 | public void render() |
||
| 135 | { |
||
| 136 | if (isButtonDown()) |
||
| 137 | { |
||
| 138 | mText.render(getLeft()+mPushOffset.x, getTop()+mPushOffset.y, getRight()+mPushOffset.x, getBottom()+mPushOffset.y, getPushColor()); |
||
| 139 | } |
||
| 140 | else |
||
| 141 | { |
||
| 142 | mText.render(getLeft(), getTop(), getRight(), getBottom()); |
||
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 146 | public boolean parseLine(String identifier, Tokenizer tokenizer) throws ScanException |
||
| 147 | { |
||
| 148 | if (identifier.equalsIgnoreCase("onTouch")) |
||
| 149 | { |
||
| 150 | mOnTouchParam = MenuUtil.parseString(tokenizer); |
||
| 151 | } |
||
| 152 | else if (identifier.equalsIgnoreCase("pushColor")) |
||
| 153 | { |
||
| 154 | setPushColor(MenuUtil.parseVector4(tokenizer)); |
||
| 155 | } |
||
| 156 | else if (identifier.equalsIgnoreCase("pushOffset")) |
||
| 157 | { |
||
| 158 | setPushOffset(MenuUtil.parseVector2(tokenizer)); |
||
| 159 | } |
||
| 160 | else if (identifier.equalsIgnoreCase("activeMargin")) |
||
| 161 | { |
||
| 162 | mActiveMargin = MenuUtil.parseVector2(tokenizer); |
||
| 163 | } |
||
| 164 | else if (identifier.equalsIgnoreCase("clickSound")) |
||
| 165 | { |
||
| 166 | String name = MenuUtil.parseString(tokenizer); |
||
| 167 | mClickSound = getParent().getGame().getAudio().getSound(name); |
||
| 168 | } |
||
| 169 | else if (identifier.equalsIgnoreCase("pushSound")) |
||
| 170 | { |
||
| 171 | String name = MenuUtil.parseString(tokenizer); |
||
| 172 | mPushSound = getParent().getGame().getAudio().getSound(name); |
||
| 173 | } |
||
| 174 | else if (!mText.parseLine(identifier, tokenizer)) |
||
| 175 | { |
||
| 176 | return super.parseLine(identifier, tokenizer); |
||
| 177 | } |
||
| 178 | return true; |
||
| 179 | } |
||
| 180 | |||
| 181 | // Getters/Setters================================================================================== |
||
| 182 | |||
| 183 | public Vector4 getPushColor() { return mPushColor; } |
||
| 184 | public void setPushColor(Vector4 pushColor) { mPushColor = pushColor; } |
||
| 185 | |||
| 186 | public Vector2 getPushOffset() { return mPushOffset; } |
||
| 187 | public void setPushOffset(Vector2 pushOffset) { mPushOffset = pushOffset; } |
||
| 188 | |||
| 189 | public Vector2 getActiveMargin() { return mActiveMargin; } |
||
| 190 | public void setActiveMargin(Vector2 activeMargin) { mActiveMargin = activeMargin; } |
||
| 191 | |||
| 192 | public Sound getButtonPushSound() { return mPushSound; } |
||
| 193 | public void setButtonPushSound(Sound buttonPushSound) { mPushSound = buttonPushSound; } |
||
| 194 | |||
| 195 | public Sound getButtonClickSound() { return mClickSound; } |
||
| 196 | public void setButtonClickSound(Sound buttonClickSound) { mClickSound = buttonClickSound; } |
||
| 197 | |||
| 198 | public TextElement getTextElement() { return mText; } |
||
| 199 | public void setTextElement(TextElement text) { mText = text; } |
||
| 200 | |||
| 201 | public final boolean isTouching() |
||
| 202 | { |
||
| 203 | for (int i = 0; i < Input.NUM_FINGERS; i++) |
||
| 204 | { |
||
| 205 | if (mIsTouching[i]) |
||
| 206 | return true; |
||
| 207 | } |
||
| 208 | return false; |
||
| 209 | } |
||
| 210 | |||
| 211 | public final boolean isFingerInside() |
||
| 212 | { |
||
| 213 | for (int i = 0; i < Input.NUM_FINGERS; i++) |
||
| 214 | { |
||
| 215 | if (mIsFingerInside[i]) |
||
| 216 | return true; |
||
| 217 | } |
||
| 218 | return false; |
||
| 219 | } |
||
| 220 | |||
| 221 | public final boolean isButtonDown() |
||
| 222 | { |
||
| 223 | for (int i = 0; i < Input.NUM_FINGERS; i++) |
||
| 224 | { |
||
| 225 | if (mIsFingerInside[i] && mIsTouching[i]) |
||
| 226 | return true; |
||
| 227 | } |
||
| 228 | return false; |
||
| 229 | } |
||
| 230 | |||
| 231 | |||
| 232 | } |