/examples/advanced/puzzle/PuzzleScene.java

http://mt4j.googlecode.com/ · Java · 247 lines · 191 code · 29 blank · 27 comment · 7 complexity · 4f10a9eb5e54e12fb77ac3d0731d4cbc MD5 · raw file

  1. /***********************************************************************
  2. * mt4j Copyright (c) 2008 - 2010 Christopher Ruff, Fraunhofer-Gesellschaft All rights reserved.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. ***********************************************************************/
  18. package advanced.puzzle;
  19. import org.mt4j.MTApplication;
  20. import org.mt4j.components.MTComponent;
  21. import org.mt4j.components.TransformSpace;
  22. import org.mt4j.components.visibleComponents.font.FontManager;
  23. import org.mt4j.components.visibleComponents.font.IFont;
  24. import org.mt4j.components.visibleComponents.shapes.AbstractShape;
  25. import org.mt4j.components.visibleComponents.shapes.MTRoundRectangle;
  26. import org.mt4j.components.visibleComponents.shapes.MTRectangle.PositionAnchor;
  27. import org.mt4j.components.visibleComponents.widgets.MTBackgroundImage;
  28. import org.mt4j.components.visibleComponents.widgets.MTList;
  29. import org.mt4j.components.visibleComponents.widgets.MTListCell;
  30. import org.mt4j.components.visibleComponents.widgets.MTTextArea;
  31. import org.mt4j.input.gestureAction.DefaultButtonClickAction;
  32. import org.mt4j.input.inputProcessors.IGestureEventListener;
  33. import org.mt4j.input.inputProcessors.MTGestureEvent;
  34. import org.mt4j.input.inputProcessors.componentProcessors.tapProcessor.TapEvent;
  35. import org.mt4j.input.inputProcessors.componentProcessors.tapProcessor.TapProcessor;
  36. import org.mt4j.input.inputProcessors.globalProcessors.CursorTracer;
  37. import org.mt4j.sceneManagement.AbstractScene;
  38. import org.mt4j.sceneManagement.IPreDrawAction;
  39. import org.mt4j.util.MT4jSettings;
  40. import org.mt4j.util.MTColor;
  41. import org.mt4j.util.animation.ani.AniAnimation;
  42. import org.mt4j.util.math.ToolsMath;
  43. import org.mt4j.util.math.Vector3D;
  44. import processing.core.PImage;
  45. public class PuzzleScene extends AbstractScene{
  46. private PuzzleFactory pf;
  47. /** The images path. */
  48. private String imagesPath = "advanced"+MTApplication.separator+"puzzle"+MTApplication.separator+"data"+MTApplication.separator;
  49. /** The images names. */
  50. private String[] imagesNames = new String[]{
  51. "Pyramids.jpg",
  52. "Grass.jpg",
  53. "Heidelberg.jpg"
  54. };
  55. private MTComponent puzzleGroup;
  56. private int horizontalTiles = 4;
  57. private int verticalTiles = 3;
  58. private MTList list;
  59. private MTRoundRectangle loadingScreen;
  60. public PuzzleScene(MTApplication mtApplication, String name) {
  61. super(mtApplication, name);
  62. if (!MT4jSettings.getInstance().isOpenGlMode()){
  63. System.err.println(this.getName() + " is only usable with the OpenGL renderer.");
  64. }
  65. this.setClearColor(new MTColor(55,55,55));
  66. this.registerGlobalInputProcessor(new CursorTracer(mtApplication, this));
  67. this.getCanvas().setDepthBufferDisabled(true); //to avoid display errors because everything is 2D
  68. MTBackgroundImage background = new MTBackgroundImage(mtApplication, mtApplication.loadImage(imagesPath + "webtreats_wood-pattern1-512d.jpg") , true);
  69. this.getCanvas().addChild(background);
  70. this.puzzleGroup = new MTComponent(mtApplication);
  71. this.getCanvas().addChild(puzzleGroup);
  72. //Puzzle tile factory
  73. this.pf = new PuzzleFactory(getMTApplication());
  74. IFont font = FontManager.getInstance().createFont(mtApplication, "SansSerif", 16, MTColor.WHITE, false);
  75. //New Puzzle button
  76. MTRoundRectangle r = getRoundRectWithText(0, 0, 120, 35, "New Puzzle", font);
  77. r.registerInputProcessor(new TapProcessor(getMTApplication()));
  78. r.addGestureListener(TapProcessor.class, new DefaultButtonClickAction(r));
  79. r.addGestureListener(TapProcessor.class, new IGestureEventListener() {
  80. public boolean processGestureEvent(MTGestureEvent ge) {
  81. TapEvent te = (TapEvent)ge;
  82. if (te.isTapped()){
  83. if (list.isVisible()){
  84. list.setVisible(false);
  85. }else{
  86. list.setVisible(true);
  87. }
  88. }
  89. return false;
  90. }
  91. });
  92. r.setPositionGlobal(new Vector3D(r.getWidthXY(TransformSpace.GLOBAL)/2f + 3 , r.getHeightXY(TransformSpace.GLOBAL)/2f + 3));
  93. this.getCanvas().addChild(r);
  94. //Image list
  95. float cellWidth = 180;
  96. float cellHeight = 40;
  97. MTColor cellFillColor = new MTColor(MTColor.BLACK);
  98. MTColor cellPressedFillColor = new MTColor(new MTColor(105,105,105));
  99. list = new MTList(getMTApplication(), r.getWidthXY(TransformSpace.GLOBAL) + 5, 0, cellWidth+2, imagesNames.length* cellHeight + imagesNames.length*3);
  100. list.setNoFill(true);
  101. list.setNoStroke(true);
  102. list.unregisterAllInputProcessors();
  103. list.setAnchor(PositionAnchor.UPPER_LEFT);
  104. // list.setPositionGlobal(Vector3D.ZERO_VECTOR);
  105. list.setVisible(false);
  106. for (String imageName : imagesNames) {
  107. list.addListElement(this.createListCell(imageName, font, cellWidth, cellHeight, cellFillColor, cellPressedFillColor));
  108. }
  109. this.getCanvas().addChild(list);
  110. //Loading window
  111. this.loadingScreen = getRoundRectWithText(0, 0, 130, 45, " Loading...", font);
  112. this.loadingScreen.setFillColor(new MTColor(0,0,0,200));
  113. this.loadingScreen.setStrokeColor(new MTColor(0,0,0,200));
  114. this.loadingScreen.setPickable(false);
  115. this.loadingScreen.setPositionGlobal(MT4jSettings.getInstance().getWindowCenter());
  116. this.loadingScreen.setVisible(false);
  117. this.getCanvas().addChild(loadingScreen);
  118. }
  119. private MTRoundRectangle getRoundRectWithText(float x, float y, float width, float height, String text, IFont font){
  120. MTRoundRectangle r = new MTRoundRectangle(getMTApplication(), x, y, 0, width, height, 12, 12);
  121. r.unregisterAllInputProcessors();
  122. r.setFillColor(MTColor.BLACK);
  123. r.setStrokeColor(MTColor.BLACK);
  124. MTTextArea rText = new MTTextArea(getMTApplication(), font);
  125. rText.unregisterAllInputProcessors();
  126. rText.setPickable(false);
  127. rText.setNoFill(true);
  128. rText.setNoStroke(true);
  129. rText.setText(text);
  130. r.addChild(rText);
  131. rText.setPositionRelativeToParent(r.getCenterPointLocal());
  132. return r;
  133. }
  134. private MTListCell createListCell(final String imageName, IFont font, float cellWidth, float cellHeight, final MTColor cellFillColor, final MTColor cellPressedFillColor){
  135. final MTListCell cell = new MTListCell(getMTApplication(), cellWidth, cellHeight);
  136. cell.setFillColor(cellFillColor);
  137. MTTextArea listLabel = new MTTextArea(getMTApplication(), font);
  138. listLabel.setNoFill(true);
  139. listLabel.setNoStroke(true);
  140. listLabel.setText(imageName);
  141. cell.addChild(listLabel);
  142. listLabel.setPositionRelativeToParent(cell.getCenterPointLocal());
  143. cell.unregisterAllInputProcessors();
  144. cell.registerInputProcessor(new TapProcessor(getMTApplication(), 15));
  145. cell.addGestureListener(TapProcessor.class, new IGestureEventListener() {
  146. public boolean processGestureEvent(MTGestureEvent ge) {
  147. TapEvent te = (TapEvent)ge;
  148. switch (te.getTapID()) {
  149. case TapEvent.TAP_DOWN:
  150. cell.setFillColor(cellPressedFillColor);
  151. break;
  152. case TapEvent.TAP_UP:
  153. cell.setFillColor(cellFillColor);
  154. break;
  155. case TapEvent.TAPPED:
  156. //System.out.println("Button clicked: " + label);
  157. cell.setFillColor(cellFillColor);
  158. list.setVisible(false);
  159. loadingScreen.setVisible(true);
  160. registerPreDrawAction(new IPreDrawAction() {
  161. public void processAction() {
  162. getMTApplication().invokeLater(new Runnable() {
  163. public void run() {
  164. loadNewPuzzle(imageName, horizontalTiles, verticalTiles);
  165. loadingScreen.setVisible(false);
  166. }
  167. });
  168. }
  169. public boolean isLoop() {return false;}
  170. });
  171. break;
  172. }
  173. return false;
  174. }
  175. });
  176. return cell;
  177. }
  178. private void loadNewPuzzle(String imageName, int horizontalTiles, int verticalTiles){
  179. for (MTComponent c : puzzleGroup.getChildren()){
  180. c.destroy();
  181. }
  182. PImage p = getMTApplication().loadImage(imagesPath + imageName);
  183. AbstractShape[] tiles = pf.createTiles(p, this.horizontalTiles, this.verticalTiles);
  184. for (final AbstractShape sh : tiles) {
  185. //Delay to smooth the animation because of loading hickups
  186. final float x = ToolsMath.getRandom(0, MT4jSettings.getInstance().getWindowWidth());
  187. final float y = ToolsMath.getRandom(0, MT4jSettings.getInstance().getWindowHeight());
  188. registerPreDrawAction(new IPreDrawAction() {
  189. public void processAction() {
  190. getMTApplication().invokeLater(new Runnable() {
  191. public void run() {
  192. registerPreDrawAction(new IPreDrawAction() {
  193. public void processAction() {
  194. getMTApplication().invokeLater(new Runnable() {
  195. public void run() {
  196. puzzleGroup.addChild(sh);
  197. // sh.tweenTranslateTo(x, y, 0, 400, 0f, 1.0f);
  198. sh.tweenTranslateTo(x, y, 0, 400, AniAnimation.CIRC_OUT, 0);
  199. }
  200. });
  201. }
  202. public boolean isLoop() {
  203. return false;
  204. }
  205. });
  206. }
  207. });
  208. }
  209. public boolean isLoop() {
  210. return false;
  211. }
  212. });
  213. sh.rotateZ(sh.getCenterPointRelativeToParent(), ToolsMath.getRandom(0, 359));
  214. }
  215. }
  216. public void onEnter() { }
  217. public void onLeave() { }
  218. }