/android/Android(LGame-0.3.2&LAE-1.1)/LAE-1.1(Canvas)/src/core/org/loon/framework/android/game/core/graphics/component/LMessage.java

http://loon-simple.googlecode.com/ · Java · 285 lines · 208 code · 53 blank · 24 comment · 24 complexity · 1caa7185f994e08a9e211e47f9f15d64 MD5 · raw file

  1. package org.loon.framework.android.game.core.graphics.component;
  2. import org.loon.framework.android.game.action.sprite.Animation;
  3. import org.loon.framework.android.game.action.sprite.SpriteImage;
  4. import org.loon.framework.android.game.core.LSystem;
  5. import org.loon.framework.android.game.core.graphics.LColor;
  6. import org.loon.framework.android.game.core.graphics.LComponent;
  7. import org.loon.framework.android.game.core.graphics.LContainer;
  8. import org.loon.framework.android.game.core.graphics.LFont;
  9. import org.loon.framework.android.game.core.graphics.LImage;
  10. import org.loon.framework.android.game.core.graphics.device.LGraphics;
  11. import org.loon.framework.android.game.utils.GraphicsUtils;
  12. import android.view.KeyEvent;
  13. /**
  14. * Copyright 2008 - 2009
  15. *
  16. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  17. * use this file except in compliance with the License. You may obtain a copy of
  18. * the License at
  19. *
  20. * http://www.apache.org/licenses/LICENSE-2.0
  21. *
  22. * Unless required by applicable law or agreed to in writing, software
  23. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  24. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  25. * License for the specific language governing permissions and limitations under
  26. * the License.
  27. *
  28. * @project loonframework
  29. * @author chenpeng
  30. * @email?ceponline@yahoo.com.cn
  31. * @version 0.1
  32. */
  33. public class LMessage extends LContainer {
  34. private Animation animation;
  35. private LFont messageFont = LFont.getFont(LSystem.FONT_NAME, 20);
  36. private LColor fontColor = LColor.white;
  37. private long printTime, totalDuration;
  38. private int dx, dy, dw, dh;
  39. private Print print;
  40. public LMessage(int width, int height) {
  41. this(0, 0, width, height);
  42. }
  43. public LMessage(int x, int y, int width, int height) {
  44. this((LImage) null, x, y, width, height);
  45. }
  46. public LMessage(String fileName, int x, int y) {
  47. this(GraphicsUtils.loadImage(fileName), x, y);
  48. }
  49. public LMessage(LImage formImage, int x, int y) {
  50. this(formImage, x, y, formImage.getWidth(), formImage.getHeight());
  51. }
  52. public LMessage(LImage formImage, int x, int y, int width, int height) {
  53. super(x, y, width, height);
  54. this.animation = new Animation();
  55. if (formImage == null) {
  56. this.setBackground(new LImage(width, height, true));
  57. this.setAlpha(0.3F);
  58. } else {
  59. this.setBackground(formImage);
  60. if (width == -1) {
  61. width = formImage.getWidth();
  62. }
  63. if (height == -1) {
  64. height = formImage.getHeight();
  65. }
  66. }
  67. this.print = new Print(getLocation(), width, height);
  68. this.setTipIcon(LSystem.FRAMEWORK_IMG_NAME+"creese.png");
  69. this.totalDuration = 80;
  70. this.customRendering = true;
  71. this.setElastic(true);
  72. this.setLocked(true);
  73. this.setLayer(100);
  74. }
  75. public void complete() {
  76. print.complete();
  77. }
  78. public void setLeftOffset(int left) {
  79. print.setLeftOffset(left);
  80. }
  81. public void setTopOffset(int top) {
  82. print.setTopOffset(top);
  83. }
  84. public int getLeftOffset() {
  85. return print.getLeftOffset();
  86. }
  87. public int getTopOffset() {
  88. return print.getTopOffset();
  89. }
  90. public int getMessageLength() {
  91. return print.getMessageLength();
  92. }
  93. public void setMessageLength(int messageLength) {
  94. print.setMessageLength(messageLength);
  95. }
  96. public void setTipIcon(String fileName) {
  97. print.setCreeseIcon(GraphicsUtils.loadImage(fileName));
  98. }
  99. public void setTipIcon(LImage icon) {
  100. print.setCreeseIcon(icon);
  101. }
  102. public void setNotTipIcon() {
  103. print.setCreeseIcon(null);
  104. }
  105. public void setEnglish(boolean e) {
  106. print.setEnglish(true);
  107. }
  108. public boolean isEnglish() {
  109. return print.isEnglish();
  110. }
  111. public void setDelay(long delay) {
  112. this.totalDuration = (delay < 1 ? 1 : delay);
  113. }
  114. public long getDelay() {
  115. return totalDuration;
  116. }
  117. public boolean isComplete() {
  118. return print.isComplete();
  119. }
  120. public void setPauseIconAnimationLocation(int dx, int dy) {
  121. this.dx = dx;
  122. this.dy = dy;
  123. }
  124. public void setMessage(String context, boolean isComplete) {
  125. print.setMessage(context, isComplete);
  126. }
  127. public void setMessage(String context) {
  128. print.setMessage(context);
  129. }
  130. public String getMessage() {
  131. return print.getMessage();
  132. }
  133. /**
  134. * ?????????????
  135. *
  136. */
  137. public void doClick() {
  138. }
  139. protected void processTouchClicked() {
  140. this.doClick();
  141. }
  142. protected void processKeyPressed() {
  143. if (this.isSelected()
  144. && this.input.getKeyPressed() == KeyEvent.KEYCODE_ENTER) {
  145. this.doClick();
  146. }
  147. }
  148. public void update(long elapsedTime) {
  149. if (!visible) {
  150. return;
  151. }
  152. super.update(elapsedTime);
  153. if (print.isComplete()) {
  154. animation.update(elapsedTime);
  155. }
  156. printTime += elapsedTime;
  157. if (printTime >= totalDuration) {
  158. printTime = printTime % totalDuration;
  159. print.next();
  160. }
  161. }
  162. protected void createCustomUI(LGraphics g, int x, int y, int w, int h) {
  163. if (!visible) {
  164. return;
  165. }
  166. LColor oldColor = g.getColor();
  167. LFont oldFont = g.getFont();
  168. g.setColor(fontColor);
  169. g.setFont(messageFont);
  170. print.draw(g, fontColor);
  171. g.setColor(oldColor);
  172. g.setFont(oldFont);
  173. if (print.isComplete()) {
  174. if (animation.getSpriteImage() != null) {
  175. g.setAlpha(1.0F);
  176. updateIcon();
  177. g.drawImage(animation.getSpriteImage().getImage(), dx, dy);
  178. }
  179. }
  180. }
  181. protected void processTouchDragged() {
  182. if (!locked) {
  183. if (getContainer() != null) {
  184. getContainer().sendToFront(this);
  185. }
  186. this.move(this.input.getTouchDX(), this.input.getTouchDY());
  187. this.updateIcon();
  188. }
  189. }
  190. public void setPauseIconAnimation(Animation animation) {
  191. this.animation = animation;
  192. if (animation != null) {
  193. SpriteImage image = animation.getSpriteImage(0);
  194. if (image != null) {
  195. this.dw = image.getWidth();
  196. this.dh = image.getHeight();
  197. this.updateIcon();
  198. }
  199. }
  200. }
  201. private void updateIcon() {
  202. this.setPauseIconAnimationLocation(getScreenX() + getWidth() - dw / 2
  203. - 20, getScreenY() + getHeight() - dh - 10);
  204. }
  205. public LColor getFontColor() {
  206. return fontColor;
  207. }
  208. public void setFontColor(LColor fontColor) {
  209. this.fontColor = fontColor;
  210. }
  211. public LFont getMessageFont() {
  212. return messageFont;
  213. }
  214. public void setMessageFont(LFont messageFont) {
  215. this.messageFont = messageFont;
  216. }
  217. public boolean isLocked() {
  218. return locked;
  219. }
  220. public void setLocked(boolean locked) {
  221. this.locked = locked;
  222. }
  223. protected void validateSize() {
  224. super.validateSize();
  225. }
  226. public String getUIName() {
  227. return "Message";
  228. }
  229. public void createUI(LGraphics g, int x, int y, LComponent component,
  230. LImage[] buttonImage) {
  231. }
  232. }