Rev 835 | Details | Compare with Previous | 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.graphics.sprite.Sprite; |
||
| 5 | import com.gebauz.bauzoid.math.Rect; |
||
| 6 | import com.gebauz.bauzoid.math.Vector4; |
||
| 7 | import com.gebauz.bauzoid.parser.ScanException; |
||
| 8 | import com.gebauz.bauzoid.parser.Tokenizer; |
||
| 9 | |||
| 10 | /** Button that uses a Frame as image. |
||
| 11 | * |
||
| 12 | * @author chiu |
||
| 13 | * |
||
| 14 | */ |
||
| 15 | public class SegmentButton extends Button |
||
| 16 | { |
||
| 17 | // Constants======================================================================================== |
||
| 18 | |||
| 19 | |||
| 20 | // Embedded Types=================================================================================== |
||
| 21 | |||
| 22 | // Fields=========================================================================================== |
||
| 23 | |||
| 24 | private String mTextureName = ""; |
||
| 25 | private String mPushTextureName = ""; |
||
| 26 | private String mButtonImageName = ""; |
||
| 27 | |||
| 28 | private FrameElement mImage = null; |
||
| 29 | private FrameElement mPushImage = null; |
||
| 30 | |||
| 31 | private Sprite mButtonImage = null; |
||
| 32 | private float mButtonImageScale = 1.0f; |
||
| 33 | private HorizontalAlign mButtonImageAlignH = HorizontalAlign.CENTER; |
||
| 34 | private VerticalAlign mButtonImageAlignV = VerticalAlign.CENTER; |
||
| 35 | |||
| 36 | // Methods========================================================================================== |
||
| 37 | |||
| 38 | public SegmentButton(Game game, Menu parent, String name) |
||
| 39 | { |
||
| 40 | super(game, parent, name); |
||
| 41 | } |
||
| 42 | |||
| 43 | @Override |
||
| 44 | public void init() |
||
| 45 | { |
||
| 46 | super.init(); |
||
| 47 | |||
| 48 | mImage = new FrameElement(getParent().getGame()); |
||
| 49 | mImage.setInnerOffset(new Rect(FrameElement.CELL_SIZE, FrameElement.CELL_SIZE, FrameElement.CELL_SIZE, FrameElement.CELL_SIZE)); |
||
| 50 | //mImage.init(new Texture(Gdx.files.internal(mTextureName)), true); |
||
| 51 | mImage.init(mTextureName, true); |
||
| 52 | Frame.setFrameProperties(mImage, this); |
||
| 53 | |||
| 54 | if (!mPushTextureName.isEmpty()) |
||
| 55 | { |
||
| 56 | mPushImage = new FrameElement(getParent().getGame()); |
||
| 57 | if (mPushImage != null) |
||
| 58 | { |
||
| 59 | mPushImage.setInnerOffset(new Rect(FrameElement.CELL_SIZE, FrameElement.CELL_SIZE, FrameElement.CELL_SIZE, FrameElement.CELL_SIZE)); |
||
| 60 | mPushImage.init(mPushTextureName, true); |
||
| 61 | //mPushImage.init(new Texture(Gdx.files.internal(mPushTextureName)), true); |
||
| 62 | Frame.setFrameProperties(mPushImage, this); |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | if (!mButtonImageName.isEmpty()) |
||
| 67 | { |
||
| 68 | //mButtonImage = new Sprite(getParent().getGame().getGraphics(), new Texture(Gdx.files.internal(mButtonImageName))); |
||
| 69 | mButtonImage = new Sprite(getGame().getGraphics(), mButtonImageName); |
||
| 70 | mButtonImage.init(); |
||
| 71 | } |
||
| 72 | } |
||
| 73 | |||
| 74 | @Override |
||
| 75 | public void exit() |
||
| 76 | { |
||
| 77 | if (mImage != null) |
||
| 78 | { |
||
| 79 | mImage.exit(); |
||
| 80 | mImage = null; |
||
| 81 | } |
||
| 82 | |||
| 83 | if (mPushImage != null) |
||
| 84 | { |
||
| 85 | mPushImage.exit(); |
||
| 86 | mPushImage = null; |
||
| 87 | } |
||
| 88 | |||
| 89 | if (mButtonImage != null) |
||
| 90 | { |
||
| 91 | mButtonImage.dispose(); |
||
| 92 | mButtonImage = null; |
||
| 93 | } |
||
| 94 | |||
| 95 | super.exit(); |
||
| 96 | } |
||
| 97 | |||
| 98 | @Override |
||
| 99 | public void update(float deltaTime) |
||
| 100 | { |
||
| 101 | super.update(deltaTime); |
||
| 102 | |||
| 103 | mImage.update(deltaTime); |
||
| 104 | Frame.setFrameProperties(mImage, this); |
||
| 105 | if (mPushImage != null) |
||
| 106 | { |
||
| 107 | Frame.setFrameProperties(mPushImage, this); |
||
| 108 | mPushImage.update(deltaTime); |
||
| 109 | } |
||
| 110 | |||
| 111 | if (mButtonImage != null) |
||
| 112 | { |
||
| 113 | // treat like text |
||
| 114 | float w = getRight() - getLeft(); |
||
| 115 | float h = getBottom() - getTop(); |
||
| 116 | |||
| 900 | chris | 117 | mButtonImage.param.x = getLeft(); |
| 118 | mButtonImage.param.y = getTop(); |
||
| 119 | mButtonImage.param.w = mButtonImage.getTextureWidth() * mButtonImageScale; |
||
| 120 | mButtonImage.param.h = mButtonImage.getTextureHeight() * mButtonImageScale; |
||
| 121 | mButtonImage.param.pivotX = 0; |
||
| 122 | mButtonImage.param.pivotY = 0; |
||
| 835 | chris | 123 | |
| 124 | if (mButtonImageAlignH == HorizontalAlign.RIGHT) |
||
| 125 | { |
||
| 900 | chris | 126 | mButtonImage.param.x = getRight(); |
| 127 | mButtonImage.param.pivotX = mButtonImage.param.w; |
||
| 835 | chris | 128 | } |
| 129 | else if (mButtonImageAlignH == HorizontalAlign.CENTER) |
||
| 130 | { |
||
| 900 | chris | 131 | mButtonImage.param.x = getLeft() + w/2; |
| 132 | mButtonImage.param.pivotX = mButtonImage.param.w/2; |
||
| 835 | chris | 133 | } |
| 134 | |||
| 135 | if (mButtonImageAlignV == VerticalAlign.BOTTOM) |
||
| 136 | { |
||
| 900 | chris | 137 | mButtonImage.param.y = getBottom(); |
| 138 | mButtonImage.param.pivotY = mButtonImage.param.h; |
||
| 835 | chris | 139 | } |
| 140 | else if (mButtonImageAlignV == VerticalAlign.CENTER) |
||
| 141 | { |
||
| 900 | chris | 142 | mButtonImage.param.y = getTop() + h/2; |
| 143 | mButtonImage.param.pivotY = mButtonImage.param.h/2; |
||
| 835 | chris | 144 | } |
| 145 | |||
| 146 | if (isButtonDown()) |
||
| 147 | { |
||
| 900 | chris | 148 | mButtonImage.param.x += getPushOffset().x; |
| 149 | mButtonImage.param.y += getPushOffset().y; |
||
| 835 | chris | 150 | } |
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | @Override |
||
| 155 | public void render() |
||
| 156 | { |
||
| 157 | if (isButtonDown() && (mPushImage != null)) |
||
| 158 | mPushImage.render(); |
||
| 159 | else |
||
| 160 | mImage.render(); |
||
| 161 | |||
| 162 | if (mButtonImage != null) |
||
| 163 | { |
||
| 900 | chris | 164 | float oldAlpha = mButtonImage.param.alpha; |
| 165 | mButtonImage.param.alpha *= getParent().getFadeAlpha(); |
||
| 835 | chris | 166 | |
| 167 | if (getTextElement().isShadow()) |
||
| 168 | { |
||
| 900 | chris | 169 | Vector4 oldColor = mButtonImage.param.color; |
| 170 | mButtonImage.param.color = getTextElement().getShadowColor(); |
||
| 171 | mButtonImage.param.x += getTextElement().getShadowOffset().x; |
||
| 172 | mButtonImage.param.y += getTextElement().getShadowOffset().y; |
||
| 835 | chris | 173 | mButtonImage.render(); |
| 900 | chris | 174 | mButtonImage.param.x -= getTextElement().getShadowOffset().x; |
| 175 | mButtonImage.param.y -= getTextElement().getShadowOffset().y; |
||
| 176 | mButtonImage.param.color = oldColor; |
||
| 835 | chris | 177 | } |
| 178 | |||
| 179 | |||
| 180 | mButtonImage.render(); |
||
| 900 | chris | 181 | mButtonImage.param.alpha = oldAlpha; |
| 835 | chris | 182 | } |
| 183 | |||
| 184 | super.render(); |
||
| 185 | } |
||
| 186 | |||
| 187 | public boolean parseLine(String identifier, Tokenizer tokenizer) throws ScanException |
||
| 188 | { |
||
| 189 | if (identifier.equalsIgnoreCase("texture")) |
||
| 190 | { |
||
| 191 | mTextureName = MenuUtil.parseString(tokenizer); |
||
| 192 | } |
||
| 193 | else if (identifier.equalsIgnoreCase("pushTexture")) |
||
| 194 | { |
||
| 195 | mPushTextureName = MenuUtil.parseString(tokenizer); |
||
| 196 | } |
||
| 197 | else if (identifier.equalsIgnoreCase("buttonImage")) |
||
| 198 | { |
||
| 199 | mButtonImageName = MenuUtil.parseString(tokenizer); |
||
| 200 | } |
||
| 201 | else if (identifier.equalsIgnoreCase("buttonImageScale")) |
||
| 202 | { |
||
| 203 | mButtonImageScale = MenuUtil.parseNumber(tokenizer); |
||
| 204 | } |
||
| 205 | else if (identifier.equalsIgnoreCase("buttonImageAlign")) |
||
| 206 | { |
||
| 207 | mButtonImageAlignH = MenuUtil.stringToHorizontalAlignment(tokenizer.readIdentifier()); |
||
| 208 | tokenizer.readToken(","); |
||
| 209 | mButtonImageAlignV = MenuUtil.stringToVerticalAlignment(tokenizer.readIdentifier()); |
||
| 210 | tokenizer.readToken(";"); |
||
| 211 | } |
||
| 212 | else |
||
| 213 | { |
||
| 214 | return super.parseLine(identifier, tokenizer); |
||
| 215 | } |
||
| 216 | return true; |
||
| 217 | } |
||
| 218 | |||
| 219 | // Getters/Setters================================================================================== |
||
| 220 | |||
| 221 | } |