2 * Copyright (c) 2003-2009 jMonkeyEngine
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 package com.jmex.awt.swingui;
34 import java.awt.Color;
35 import java.lang.reflect.InvocationTargetException;
36 import java.util.logging.Level;
37 import java.util.logging.Logger;
39 import javax.swing.SwingUtilities;
41 import com.jme.input.InputHandler;
42 import com.jme.renderer.Renderer;
43 import com.jme.scene.Node;
44 import com.jme.scene.Spatial.CullHint;
45 import com.jme.scene.Spatial.LightCombineMode;
46 import com.jme.system.DisplaySystem;
47 import com.jmex.game.state.BasicGameState;
50 * @author Matthew D. Hicks
52 public class JMEDesktopState extends BasicGameState {
53 private static final Logger logger = Logger.getLogger(JMEDesktopState.class.getName());
55 private boolean variableDesktopSize;
57 private InputHandler guiInput;
58 private JMEDesktop desktop;
63 public JMEDesktopState() {
67 public JMEDesktopState(int width, int height) {
74 public JMEDesktopState(boolean variableDesktopSize) {
76 this.variableDesktopSize = variableDesktopSize;
81 guiNode = new Node("GUI");
82 guiNode.setCullHint(CullHint.Never);
83 guiNode.setLightCombineMode(LightCombineMode.Off);
85 rootNode = new Node("RootNode");
87 guiInput = new InputHandler();
88 guiInput.setEnabled(true);
90 if (variableDesktopSize) {
91 desktop = new JMEDesktop("Desktop", DisplaySystem.getDisplaySystem().getWidth(), DisplaySystem.getDisplaySystem().getHeight(), guiInput);
93 desktop = new JMEDesktop("Desktop", width, height, guiInput);
97 SwingUtilities.invokeAndWait(new Runnable() {
100 desktop.getJDesktop().setName("Desktop");
101 desktop.getJDesktop().setBackground(new Color(0.0f, 0.0f, 0.0f, 0.0f));
102 desktop.getJDesktop().setOpaque(true);
106 } catch(InvocationTargetException exc) {
107 logger.logp(Level.SEVERE, this.getClass().toString(), "init()", "Exception", exc);
108 } catch(InterruptedException exc) {
109 logger.logp(Level.SEVERE, this.getClass().toString(), "init()", "Exception", exc);
112 guiNode.attachChild(desktop);
113 guiNode.getLocalTranslation().set(DisplaySystem.getDisplaySystem().getWidth() / 2, DisplaySystem.getDisplaySystem().getHeight() / 2, 0.0f);
114 guiNode.getLocalScale().set(1.0f, 1.0f, 1.0f);
115 guiNode.updateRenderState();
116 guiNode.updateGeometricState(0.0f, true);
117 guiNode.setRenderQueueMode(Renderer.QUEUE_ORTHO);
118 guiNode.updateGeometricState(0.0f, true);
119 guiNode.updateRenderState();
122 protected void buildUI() {
125 public void update(float tpf) {
126 guiInput.update(tpf);
127 guiNode.updateGeometricState(tpf, true);
131 public void render(float tpf) {
132 DisplaySystem.getDisplaySystem().getRenderer().draw(guiNode);
135 public void cleanup() {
139 public Node getGUINode() {
143 public JMEDesktop getDesktop() {
147 public InputHandler getInputHandler() {