PageRenderTime 48ms CodeModel.GetById 31ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/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 ***********************************************************************/
 18package advanced.puzzle;
 19
 20import org.mt4j.MTApplication;
 21import org.mt4j.components.MTComponent;
 22import org.mt4j.components.TransformSpace;
 23import org.mt4j.components.visibleComponents.font.FontManager;
 24import org.mt4j.components.visibleComponents.font.IFont;
 25import org.mt4j.components.visibleComponents.shapes.AbstractShape;
 26import org.mt4j.components.visibleComponents.shapes.MTRoundRectangle;
 27import org.mt4j.components.visibleComponents.shapes.MTRectangle.PositionAnchor;
 28import org.mt4j.components.visibleComponents.widgets.MTBackgroundImage;
 29import org.mt4j.components.visibleComponents.widgets.MTList;
 30import org.mt4j.components.visibleComponents.widgets.MTListCell;
 31import org.mt4j.components.visibleComponents.widgets.MTTextArea;
 32import org.mt4j.input.gestureAction.DefaultButtonClickAction;
 33import org.mt4j.input.inputProcessors.IGestureEventListener;
 34import org.mt4j.input.inputProcessors.MTGestureEvent;
 35import org.mt4j.input.inputProcessors.componentProcessors.tapProcessor.TapEvent;
 36import org.mt4j.input.inputProcessors.componentProcessors.tapProcessor.TapProcessor;
 37import org.mt4j.input.inputProcessors.globalProcessors.CursorTracer;
 38import org.mt4j.sceneManagement.AbstractScene;
 39import org.mt4j.sceneManagement.IPreDrawAction;
 40import org.mt4j.util.MT4jSettings;
 41import org.mt4j.util.MTColor;
 42import org.mt4j.util.animation.ani.AniAnimation;
 43import org.mt4j.util.math.ToolsMath;
 44import org.mt4j.util.math.Vector3D;
 45
 46import processing.core.PImage;
 47
 48public class PuzzleScene extends AbstractScene{
 49
 50	private PuzzleFactory pf;
 51	
 52	/** The images path. */
 53	private String imagesPath = "advanced"+MTApplication.separator+"puzzle"+MTApplication.separator+"data"+MTApplication.separator;
 54	
 55	/** The images names. */
 56	private String[] imagesNames = new String[]{
 57			"Pyramids.jpg", 
 58			"Grass.jpg",
 59			"Heidelberg.jpg"
 60			};
 61	
 62	private MTComponent puzzleGroup;
 63	private int horizontalTiles = 4;
 64	private int verticalTiles = 3;
 65	private MTList list;
 66	private MTRoundRectangle loadingScreen;
 67
 68	public PuzzleScene(MTApplication mtApplication, String name) {
 69		super(mtApplication, name);
 70		if (!MT4jSettings.getInstance().isOpenGlMode()){
 71			System.err.println(this.getName() + " is only usable with the OpenGL renderer.");
 72		}
 73
 74		this.setClearColor(new MTColor(55,55,55));
 75		this.registerGlobalInputProcessor(new CursorTracer(mtApplication, this));
 76		this.getCanvas().setDepthBufferDisabled(true); //to avoid display errors because everything is 2D
 77		
 78		MTBackgroundImage background = new MTBackgroundImage(mtApplication, mtApplication.loadImage(imagesPath + "webtreats_wood-pattern1-512d.jpg") , true);
 79		this.getCanvas().addChild(background);
 80		
 81		this.puzzleGroup = new MTComponent(mtApplication);
 82		this.getCanvas().addChild(puzzleGroup);
 83		
 84		//Puzzle tile factory
 85		this.pf = new PuzzleFactory(getMTApplication());
 86		
 87		IFont font = FontManager.getInstance().createFont(mtApplication, "SansSerif", 16, MTColor.WHITE, false);
 88		
 89		//New Puzzle button
 90		MTRoundRectangle r = getRoundRectWithText(0, 0, 120, 35, "New Puzzle", font);
 91		r.registerInputProcessor(new TapProcessor(getMTApplication()));
 92		r.addGestureListener(TapProcessor.class, new DefaultButtonClickAction(r));
 93		r.addGestureListener(TapProcessor.class, new IGestureEventListener() {
 94			public boolean processGestureEvent(MTGestureEvent ge) {
 95				TapEvent te = (TapEvent)ge;
 96				if (te.isTapped()){
 97					if (list.isVisible()){
 98						list.setVisible(false);
 99					}else{
100						list.setVisible(true);
101					}
102				}
103				return false;
104			}
105		});
106		r.setPositionGlobal(new Vector3D(r.getWidthXY(TransformSpace.GLOBAL)/2f + 3 , r.getHeightXY(TransformSpace.GLOBAL)/2f + 3));
107		this.getCanvas().addChild(r);
108		
109		//Image list
110		float cellWidth = 180;
111		float cellHeight = 40;
112		MTColor cellFillColor = new MTColor(MTColor.BLACK);
113		MTColor cellPressedFillColor = new MTColor(new MTColor(105,105,105));
114		list = new MTList(getMTApplication(), r.getWidthXY(TransformSpace.GLOBAL) + 5, 0, cellWidth+2, imagesNames.length* cellHeight + imagesNames.length*3);
115		list.setNoFill(true);
116		list.setNoStroke(true);
117		list.unregisterAllInputProcessors();
118		list.setAnchor(PositionAnchor.UPPER_LEFT);
119//		list.setPositionGlobal(Vector3D.ZERO_VECTOR);
120		list.setVisible(false);
121        for (String imageName : imagesNames) {
122            list.addListElement(this.createListCell(imageName, font, cellWidth, cellHeight, cellFillColor, cellPressedFillColor));
123        }
124		this.getCanvas().addChild(list);
125		
126		//Loading window
127		this.loadingScreen = getRoundRectWithText(0, 0, 130, 45, "  Loading...", font);
128		this.loadingScreen.setFillColor(new MTColor(0,0,0,200));
129		this.loadingScreen.setStrokeColor(new MTColor(0,0,0,200));
130		this.loadingScreen.setPickable(false);
131		this.loadingScreen.setPositionGlobal(MT4jSettings.getInstance().getWindowCenter());
132		this.loadingScreen.setVisible(false);
133		this.getCanvas().addChild(loadingScreen);
134	}
135	
136	
137	private MTRoundRectangle getRoundRectWithText(float x, float y, float width, float height, String text, IFont font){
138		MTRoundRectangle r = new MTRoundRectangle(getMTApplication(), x, y, 0, width, height, 12, 12);
139		r.unregisterAllInputProcessors();
140		r.setFillColor(MTColor.BLACK);
141		r.setStrokeColor(MTColor.BLACK);
142		MTTextArea rText = new MTTextArea(getMTApplication(), font);
143		rText.unregisterAllInputProcessors();
144		rText.setPickable(false);
145		rText.setNoFill(true);
146		rText.setNoStroke(true);
147		rText.setText(text);
148		r.addChild(rText);
149		rText.setPositionRelativeToParent(r.getCenterPointLocal());
150		return r;
151	}
152	
153	
154	private MTListCell createListCell(final String imageName, IFont font, float cellWidth, float cellHeight, final MTColor cellFillColor, final MTColor cellPressedFillColor){
155		final MTListCell cell = new MTListCell(getMTApplication(), cellWidth, cellHeight);
156		cell.setFillColor(cellFillColor);
157		MTTextArea listLabel = new MTTextArea(getMTApplication(), font);
158		listLabel.setNoFill(true);
159		listLabel.setNoStroke(true);
160		listLabel.setText(imageName);
161		cell.addChild(listLabel);
162		listLabel.setPositionRelativeToParent(cell.getCenterPointLocal());
163		cell.unregisterAllInputProcessors();
164		cell.registerInputProcessor(new TapProcessor(getMTApplication(), 15));
165		cell.addGestureListener(TapProcessor.class, new IGestureEventListener() {
166			public boolean processGestureEvent(MTGestureEvent ge) {
167				TapEvent te = (TapEvent)ge;
168				switch (te.getTapID()) { 
169				case TapEvent.TAP_DOWN:
170					cell.setFillColor(cellPressedFillColor);
171					break;
172				case TapEvent.TAP_UP:
173					cell.setFillColor(cellFillColor);
174					break;
175				case TapEvent.TAPPED:
176					//System.out.println("Button clicked: " + label);
177					cell.setFillColor(cellFillColor);
178					list.setVisible(false);
179					loadingScreen.setVisible(true);
180					registerPreDrawAction(new IPreDrawAction() {
181						public void processAction() {
182							getMTApplication().invokeLater(new Runnable() {
183								public void run() {
184									loadNewPuzzle(imageName, horizontalTiles, verticalTiles);
185									loadingScreen.setVisible(false);
186								}
187							});
188						}
189						public boolean isLoop() {return false;}
190					});
191					break;
192				}
193				return false;
194			}
195		});
196		return cell;
197	}
198	
199	private void loadNewPuzzle(String imageName, int horizontalTiles, int verticalTiles){
200		for (MTComponent c : puzzleGroup.getChildren()){
201			c.destroy();
202		}
203		PImage p = getMTApplication().loadImage(imagesPath + imageName);
204		AbstractShape[] tiles = pf.createTiles(p, this.horizontalTiles, this.verticalTiles);
205        for (final AbstractShape sh : tiles) {
206            //Delay to smooth the animation because of loading hickups
207            final float x = ToolsMath.getRandom(0, MT4jSettings.getInstance().getWindowWidth());
208            final float y = ToolsMath.getRandom(0, MT4jSettings.getInstance().getWindowHeight());
209            registerPreDrawAction(new IPreDrawAction() {
210                public void processAction() {
211                    getMTApplication().invokeLater(new Runnable() {
212                        public void run() {
213                            registerPreDrawAction(new IPreDrawAction() {
214                                public void processAction() {
215                                    getMTApplication().invokeLater(new Runnable() {
216                                        public void run() {
217                                            puzzleGroup.addChild(sh);
218//											sh.tweenTranslateTo(x, y, 0, 400, 0f, 1.0f);
219                                            sh.tweenTranslateTo(x, y, 0, 400, AniAnimation.CIRC_OUT, 0);
220                                        }
221                                    });
222                                }
223
224                                public boolean isLoop() {
225                                    return false;
226                                }
227                            });
228                        }
229                    });
230                }
231
232                public boolean isLoop() {
233                    return false;
234                }
235            });
236            sh.rotateZ(sh.getCenterPointRelativeToParent(), ToolsMath.getRandom(0, 359));
237        }
238	}
239	
240
241	public void onEnter() {	}
242	
243	public void onLeave() {	}
244	
245	
246
247}