PageRenderTime 36ms CodeModel.GetById 14ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 1ms

/src/org/mt4j/components/visibleComponents/widgets/MTOverlayContainer.java

http://mt4j.googlecode.com/
Java | 133 lines | 54 code | 17 blank | 62 comment | 8 complexity | 37c515d658e87798b0d6cd428ba33073 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 org.mt4j.components.visibleComponents.widgets;
 19
 20import org.mt4j.MTApplication;
 21import org.mt4j.components.MTComponent;
 22import org.mt4j.components.StateChange;
 23import org.mt4j.components.StateChangeEvent;
 24import org.mt4j.components.StateChangeListener;
 25import org.mt4j.components.interfaces.IMTController;
 26import org.mt4j.util.camera.MTCamera;
 27
 28/**
 29 * This is a component that will try to always stay on top and will stay in place even
 30 * if the scene's camera is moved.
 31 * <br>Thus it can be used as a container for a GUI or a HUD (head up display). 
 32 * 
 33 * @author Christopher Ruff
 34 */
 35public class MTOverlayContainer extends MTComponent {
 36	
 37	/** The app. */
 38	private MTApplication app;
 39	
 40	//TODO overlay layer priorites / layer numbers to sort them by priority
 41	
 42	/**
 43	 * Instantiates a new mT overlay container.
 44	 * 
 45	 * @param applet the applet
 46	 */
 47	public MTOverlayContainer(MTApplication applet) {
 48		this(applet, "unnamed overlay container");
 49	}
 50
 51	/**
 52	 * Instantiates a new mT overlay container.
 53	 * 
 54	 * @param app the app
 55	 * @param name the name
 56	 */
 57	public MTOverlayContainer(MTApplication app, String name) {
 58		super(app, name, new MTCamera(app));
 59		this.app = app;
 60		
 61		this.setDepthBufferDisabled(true);
 62
 63		//Send overlay group to front again if it isnt - check each frame if its on front!
 64		this.setController(new IMTController() {
 65			public void update(long timeDelta) {
 66				putLastInParentList();
 67			}
 68		});
 69		
 70		//Always put last in parents children list if added to a parent,
 71		//so the chance is higher that this is really drawn ontop of other stuff
 72		this.addStateChangeListener(StateChange.ADDED_TO_PARENT, new StateChangeListener() {
 73			public void stateChanged(StateChangeEvent evt) {
 74				putLastInParentList();
 75			}
 76		});
 77	}
 78	
 79	
 80	private void putLastInParentList(){
 81		MTComponent parent = getParent();
 82		if (parent != null){
 83			int childCount = parent.getChildCount();
 84			
 85			if (	childCount > 0
 86				&& !parent.getChildByIndex(childCount-1).equals(MTOverlayContainer.this)
 87			){
 88				MTComponent lastChild = parent.getChildByIndex(childCount-1);
 89				if (	!(lastChild instanceof MTOverlayContainer)
 90					&& 	!(lastChild.getName().equalsIgnoreCase("Cursor Trace group"))
 91				){
 92					//last component in canvas child list is not a overlay container:
 93					MTOverlayContainer.this.app.invokeLater(new Runnable() {
 94						public void run(){
 95							MTComponent parent = getParent();
 96							if (parent != null){
 97								parent.removeChild(MTOverlayContainer.this);
 98								parent.addChild(MTOverlayContainer.this);
 99							}
100						}
101					});
102				}else{
103					//last component in canvas already is a different overlay container:
104//					int insertionIndex = getInsertionIndex(parent);
105					
106				}
107			}
108		}
109	}
110	
111	/*
112	private int getInsertionIndex(MTComponent parent){
113		int count = parent.getChildCount();
114		for (int i = 1; i <= count; i++) {
115//			int index = -i;
116			int checkIndex = count-i;
117			MTComponent child = parent.getChildByIndex(checkIndex);
118			System.out.println("Check at: " + checkIndex + " ->" + child);
119			
120			if (child instanceof MTOverlayContainer){
121				
122			}else{
123				System.out.println("Found insertion place for : " + this + " at: " + checkIndex);
124				return checkIndex;
125			}
126		}
127		return 0;
128	}
129	*/
130
131
132
133}