/android/LGame-Android-0.2.95/src/org/loon/framework/android/game/core/graphics/window/LButton.java

http://loon-simple.googlecode.com/ · Java · 249 lines · 176 code · 48 blank · 25 comment · 19 complexity · c93db64392669b65534fb194264f655a MD5 · raw file

  1. package org.loon.framework.android.game.core.graphics.window;
  2. import org.loon.framework.android.game.core.LSystem;
  3. import org.loon.framework.android.game.core.graphics.LColor;
  4. import org.loon.framework.android.game.core.graphics.LComponent;
  5. import org.loon.framework.android.game.core.graphics.LFont;
  6. import org.loon.framework.android.game.core.graphics.LImage;
  7. import org.loon.framework.android.game.core.graphics.filter.ImageFilterFactory;
  8. import org.loon.framework.android.game.utils.GraphicsUtils;
  9. import android.view.KeyEvent;
  10. import android.view.MotionEvent;
  11. /**
  12. *
  13. * Copyright 2008 - 2009
  14. *
  15. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  16. * use this file except in compliance with the License. You may obtain a copy of
  17. * the License at
  18. *
  19. * http://www.apache.org/licenses/LICENSE-2.0
  20. *
  21. * Unless required by applicable law or agreed to in writing, software
  22. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  23. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  24. * License for the specific language governing permissions and limitations under
  25. * the License.
  26. *
  27. * @project loonframework
  28. * @author chenpeng
  29. * @email?ceponline@yahoo.com.cn
  30. * @version 0.1
  31. */
  32. public class LButton extends LComponent {
  33. private String text = "";
  34. private boolean over, pressed, exception;
  35. private int pressedTime, offsetLeft, offsetTop;
  36. private LFont font = LFont.getFont(LSystem.FONT_NAME, 0, 20);
  37. private LColor fontColor = LColor.white;
  38. public LButton(String fileName) {
  39. this(fileName, null, 0, 0);
  40. }
  41. public LButton(String fileName, String text, int x, int y) {
  42. this(GraphicsUtils.loadImage(fileName, true), text, x, y);
  43. }
  44. public LButton(LImage img, String text, int x, int y) {
  45. this(img, text, img.getWidth(), img.getHeight(), x, y);
  46. }
  47. public LButton(String fileName, int row, int col) {
  48. this(fileName, null, row, col, 0, 0);
  49. }
  50. public LButton(String fileName, String text, int row, int col, int x, int y) {
  51. this(GraphicsUtils.loadImage(fileName, true), text, row, col, x, y);
  52. }
  53. public LButton(LImage img, String text, int row, int col, int x, int y) {
  54. this(GraphicsUtils.getSplitImages(img, row, col), text, row, col, x, y);
  55. }
  56. public LButton(LImage[] img, String text, int row, int col, int x, int y) {
  57. super(x, y, row, col);
  58. this.setImages(img);
  59. this.text = text;
  60. }
  61. public LButton(String text, int x, int y, int w, int h) {
  62. super(x, y, w, h);
  63. this.text = text;
  64. }
  65. public void setImages(LImage[] images) {
  66. LImage[] buttons = new LImage[4];
  67. if (images != null) {
  68. int size = images.length;
  69. switch (size) {
  70. case 1:
  71. buttons[0] = ImageFilterFactory.getGray(images[0]);
  72. buttons[1] = images[0];
  73. buttons[2] = images[0];
  74. buttons[3] = images[0];
  75. break;
  76. case 2:
  77. buttons[0] = images[0];
  78. buttons[1] = images[1];
  79. buttons[2] = images[0];
  80. buttons[3] = images[0];
  81. break;
  82. case 3:
  83. buttons[0] = images[0];
  84. buttons[1] = images[1];
  85. buttons[2] = images[2];
  86. buttons[3] = images[0];
  87. break;
  88. case 4:
  89. buttons = images;
  90. break;
  91. default:
  92. exception = true;
  93. break;
  94. }
  95. }
  96. if (!exception) {
  97. this.setImageUI(buttons, true);
  98. }
  99. }
  100. public void update(long timer) {
  101. if (this.pressedTime > 0 && --this.pressedTime <= 0) {
  102. this.pressed = false;
  103. }
  104. }
  105. public boolean isTouchOver() {
  106. return this.over;
  107. }
  108. public boolean isTouchPressed() {
  109. return this.pressed;
  110. }
  111. public String getText() {
  112. return this.text;
  113. }
  114. public void setText(String st) {
  115. this.text = st;
  116. this.createUI();
  117. }
  118. protected void processTouchDragged() {
  119. if (this.input.isTouchDown(MotionEvent.ACTION_MOVE)) {
  120. this.over = this.pressed = this.intersects(this.input.getTouchX(),
  121. this.input.getTouchY());
  122. }
  123. }
  124. /**
  125. * ?????????????
  126. *
  127. */
  128. public void doClick() {
  129. }
  130. public void downClick() {
  131. }
  132. public void upClick() {
  133. }
  134. protected void processTouchClicked() {
  135. if (this.input.getTouchReleased() == MotionEvent.ACTION_UP) {
  136. this.doClick();
  137. }
  138. }
  139. protected void processTouchPressed() {
  140. if (this.input.getTouchPressed() == MotionEvent.ACTION_DOWN) {
  141. this.downClick();
  142. this.pressed = true;
  143. }
  144. }
  145. protected void processTouchReleased() {
  146. if (this.input.getTouchReleased() == MotionEvent.ACTION_UP) {
  147. this.upClick();
  148. this.pressed = false;
  149. }
  150. }
  151. protected void processTouchEntered() {
  152. this.over = true;
  153. }
  154. protected void processTouchExited() {
  155. this.over = this.pressed = false;
  156. }
  157. protected void processKeyPressed() {
  158. if (this.isSelected()
  159. && this.input.getKeyPressed() == KeyEvent.KEYCODE_ENTER) {
  160. this.pressedTime = 5;
  161. this.pressed = true;
  162. this.doClick();
  163. }
  164. }
  165. protected void processKeyReleased() {
  166. if (this.isSelected()
  167. && this.input.getKeyReleased() == KeyEvent.KEYCODE_ENTER) {
  168. this.pressed = false;
  169. }
  170. }
  171. public boolean isException() {
  172. return exception;
  173. }
  174. public String getUIName() {
  175. return "Button";
  176. }
  177. public LFont getFont() {
  178. return font;
  179. }
  180. public void setFont(LFont font) {
  181. this.font = font;
  182. }
  183. public LColor getFontColor() {
  184. return fontColor;
  185. }
  186. public void setFontColor(LColor fontColor) {
  187. this.fontColor = fontColor;
  188. }
  189. public int getOffsetLeft() {
  190. return offsetLeft;
  191. }
  192. public void setOffsetLeft(int offsetLeft) {
  193. this.offsetLeft = offsetLeft;
  194. }
  195. public int getOffsetTop() {
  196. return offsetTop;
  197. }
  198. public void setOffsetTop(int offsetTop) {
  199. this.offsetTop = offsetTop;
  200. }
  201. }