/android/LGame-Android-0.2.95/src/org/loon/framework/android/game/action/avg/MessageDialog.java

http://loon-simple.googlecode.com/ · Java · 263 lines · 194 code · 49 blank · 20 comment · 24 complexity · a86a87944600c8ac8249098cd7deed39 MD5 · raw file

  1. package org.loon.framework.android.game.action.avg;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import org.loon.framework.android.game.core.graphics.LImage;
  5. import org.loon.framework.android.game.core.graphics.device.LGraphics;
  6. import org.loon.framework.android.game.utils.GraphicsUtils;
  7. /**
  8. * Copyright 2008 - 2010
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  11. * use this file except in compliance with the License. You may obtain a copy of
  12. * the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  18. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  19. * License for the specific language governing permissions and limitations under
  20. * the License.
  21. *
  22. * @project loonframework
  23. * @author chenpeng
  24. * @email ceponline@yahoo.com.cn
  25. * @version 0.1.1
  26. */
  27. final public class MessageDialog {
  28. private static Map<String, LImage> lazyImages;
  29. final static private int objWidth = 64;
  30. final static private int objHeight = 64;
  31. final static private int x1 = 128;
  32. final static private int x2 = 192;
  33. final static private int y1 = 0;
  34. final static private int y2 = 64;
  35. public final static LImage getRMXPDialog(String fileName, int width,
  36. int height) {
  37. if (lazyImages == null) {
  38. lazyImages = new HashMap<String, LImage>(10);
  39. }
  40. LImage dialog = GraphicsUtils.load8888Image(fileName);
  41. int w = dialog.getWidth();
  42. int[] pixels = dialog.getPixels();
  43. int index = -1;
  44. int count = 0;
  45. int pixel;
  46. for (int i = 0; i < 5; i++) {
  47. pixel = pixels[(141 + i) + w * 12];
  48. if (index == -1) {
  49. index = pixel;
  50. }
  51. if (index == pixel) {
  52. count++;
  53. }
  54. }
  55. if (count == 5) {
  56. return getRMXPDialog(dialog, width, height, 16, 5);
  57. } else if (count == 1) {
  58. return getRMXPDialog(dialog, width, height, 27, 5);
  59. } else if (count == 2) {
  60. return getRMXPDialog(dialog, width, height, 20, 5);
  61. } else {
  62. return getRMXPDialog(dialog, width, height, 27, 5);
  63. }
  64. }
  65. public final static LImage getRMXPloadBuoyage(String fileName, int width,
  66. int height) {
  67. return getRMXPloadBuoyage(GraphicsUtils.load8888Image(fileName), width,
  68. height);
  69. }
  70. public final static LImage getRMXPloadBuoyage(LImage rmxpImage, int width,
  71. int height) {
  72. if (lazyImages == null) {
  73. lazyImages = new HashMap<String, LImage>(10);
  74. }
  75. String keyName = ("buoyage" + width + "|" + height).intern();
  76. LImage lazyImage = (LImage) lazyImages.get(keyName);
  77. if (lazyImage == null) {
  78. LImage image, left, right, center, up, down = null;
  79. int objWidth = 32;
  80. int objHeight = 32;
  81. int x1 = 128;
  82. int x2 = 160;
  83. int y1 = 64;
  84. int y2 = 96;
  85. int k = 1;
  86. try {
  87. image = GraphicsUtils.drawClipImage(rmxpImage, objWidth,
  88. objHeight, x1, y1, x2, y2);
  89. lazyImage = LImage.createImage(width, height, false);
  90. LGraphics g = lazyImage.getLGraphics();
  91. left = GraphicsUtils.drawClipImage(image, k, height, 0, 0, k,
  92. objHeight);
  93. right = GraphicsUtils.drawClipImage(image, k, height, objWidth
  94. - k, 0, objWidth, objHeight);
  95. center = GraphicsUtils.drawClipImage(image, width, height, k,
  96. k, objWidth - k, objHeight - k);
  97. up = GraphicsUtils.drawClipImage(image, width, k, 0, 0,
  98. objWidth, k);
  99. down = GraphicsUtils.drawClipImage(image, width, k, 0,
  100. objHeight - k, objWidth, objHeight);
  101. g.drawImage(center, 0, 0);
  102. g.drawImage(left, 0, 0);
  103. g.drawImage(right, width - k, 0);
  104. g.drawImage(up, 0, 0);
  105. g.drawImage(down, 0, height - k);
  106. g.dispose();
  107. lazyImages.put(keyName, lazyImage);
  108. } catch (Exception e) {
  109. return null;
  110. } finally {
  111. left = null;
  112. right = null;
  113. center = null;
  114. up = null;
  115. down = null;
  116. image = null;
  117. }
  118. }
  119. return lazyImage;
  120. }
  121. private final static LImage getRMXPDialog(LImage rmxpImage, int width,
  122. int height, int size, int offset) {
  123. if (lazyImages == null) {
  124. lazyImages = new HashMap<String, LImage>(10);
  125. }
  126. String keyName = "dialog" + width + "|" + height;
  127. LImage lazyImage = (LImage) lazyImages.get(keyName);
  128. if (lazyImage == null) {
  129. try {
  130. int center_size = objHeight - size * 2;
  131. LImage image = null;
  132. LImage messageImage = null;
  133. image = GraphicsUtils.drawClipImage(rmxpImage, objWidth,
  134. objHeight, x1, y1, x2, y2);
  135. LImage centerTop = GraphicsUtils.drawClipImage(image,
  136. center_size, size, size, 0);
  137. LImage centerDown = GraphicsUtils.drawClipImage(image,
  138. center_size, size, size, objHeight - size);
  139. LImage leftTop = GraphicsUtils.drawClipImage(image, size, size,
  140. 0, 0);
  141. LImage leftCenter = GraphicsUtils.drawClipImage(image, size,
  142. center_size, 0, size);
  143. LImage leftDown = GraphicsUtils.drawClipImage(image, size,
  144. size, 0, objHeight - size);
  145. LImage rightTop = GraphicsUtils.drawClipImage(image, size,
  146. size, objWidth - size, 0);
  147. LImage rightCenter = GraphicsUtils.drawClipImage(image, size,
  148. center_size, objWidth - size, size);
  149. LImage rightDown = GraphicsUtils.drawClipImage(image, size,
  150. size, objWidth - size, objHeight - size);
  151. lazyImage = LImage.createImage(width, height, rmxpImage
  152. .getConfig());
  153. messageImage = GraphicsUtils.drawClipImage(rmxpImage, 128, 128,
  154. 0, 0, 128, 128, false);
  155. LGraphics g = lazyImage.getLGraphics();
  156. g.setAlpha(0.5f);
  157. messageImage = GraphicsUtils.getResize(messageImage, width
  158. - offset, height - offset);
  159. g.drawImage(messageImage, (lazyImage.getWidth() - messageImage
  160. .getWidth()) / 2, (lazyImage.getHeight() - messageImage
  161. .getHeight()) / 2);
  162. g.setAlpha(1.0f);
  163. LImage tmp = GraphicsUtils.getResize(centerTop, width
  164. - (size * 2), size);
  165. g.drawImage(tmp, size, 0);
  166. tmp = null;
  167. tmp = GraphicsUtils.getResize(centerDown, width - (size * 2),
  168. size);
  169. g.drawImage(tmp, size, height - size);
  170. tmp = null;
  171. g.drawImage(leftTop, 0, 0);
  172. tmp = GraphicsUtils.getResize(leftCenter,
  173. leftCenter.getWidth(), width - (size * 2));
  174. g.drawImage(tmp, 0, size);
  175. tmp = null;
  176. g.drawImage(leftDown, 0, height - size);
  177. int right = width - size;
  178. g.drawImage(rightTop, right, 0);
  179. tmp = GraphicsUtils.getResize(rightCenter, leftCenter
  180. .getWidth(), width - (size * 2));
  181. g.drawImage(tmp, right, size);
  182. tmp = null;
  183. g.drawImage(rightDown, right, height - size);
  184. g.dispose();
  185. lazyImages.put(keyName, lazyImage);
  186. image.dispose();
  187. messageImage.dispose();
  188. centerTop.dispose();
  189. centerDown.dispose();
  190. leftTop.dispose();
  191. leftCenter.dispose();
  192. leftDown.dispose();
  193. rightTop.dispose();
  194. rightCenter.dispose();
  195. rightDown.dispose();
  196. image = null;
  197. messageImage = null;
  198. centerTop = null;
  199. centerDown = null;
  200. leftTop = null;
  201. leftCenter = null;
  202. leftDown = null;
  203. rightTop = null;
  204. rightCenter = null;
  205. rightDown = null;
  206. } catch (Exception e) {
  207. }
  208. }
  209. return lazyImage;
  210. }
  211. public static void clear() {
  212. lazyImages.clear();
  213. }
  214. }