OSDN Git Service

c9fc6085e4852d5601581fef0cb8ab9214396645
[mikumikustudio/MikuMikuStudio.git] / src / com / jmex / awt / swingui / JMEDesktopState.java
1 /*
2  * Copyright (c) 2003-2009 jMonkeyEngine
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  *
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.
15  *
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.
19  *
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.
31  */
32 package com.jmex.awt.swingui;
33
34 import java.awt.Color;
35 import java.lang.reflect.InvocationTargetException;
36 import java.util.logging.Level;
37 import java.util.logging.Logger;
38
39 import javax.swing.SwingUtilities;
40
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;
48
49 /**
50  * @author Matthew D. Hicks
51  */
52 public class JMEDesktopState extends BasicGameState {
53     private static final Logger logger = Logger.getLogger(JMEDesktopState.class.getName());
54     
55     private boolean variableDesktopSize;
56     private Node guiNode;
57     private InputHandler guiInput;
58     private JMEDesktop desktop;
59     
60     private int width;
61     private int height;
62     
63     public JMEDesktopState() {
64         this(800, 600);
65     }
66     
67     public JMEDesktopState(int width, int height) {
68         super("jMEDesktop");
69         this.width = width;
70         this.height = height;
71         init();
72     }
73     
74     public JMEDesktopState(boolean variableDesktopSize) {
75         super("jMEDesktop");
76         this.variableDesktopSize = variableDesktopSize;
77         init();
78     }
79     
80     private void init() {
81         guiNode = new Node("GUI");
82         guiNode.setCullHint(CullHint.Never);
83         guiNode.setLightCombineMode(LightCombineMode.Off);
84         
85         rootNode = new Node("RootNode");
86         
87         guiInput = new InputHandler();
88         guiInput.setEnabled(true);
89
90         if (variableDesktopSize) {
91             desktop = new JMEDesktop("Desktop", DisplaySystem.getDisplaySystem().getWidth(), DisplaySystem.getDisplaySystem().getHeight(), guiInput);
92         } else {
93             desktop = new JMEDesktop("Desktop", width, height, guiInput);
94         }
95
96         try {
97             SwingUtilities.invokeAndWait(new Runnable() {
98
99                 public void run() {
100                     desktop.getJDesktop().setName("Desktop");
101                     desktop.getJDesktop().setBackground(new Color(0.0f, 0.0f, 0.0f, 0.0f));
102                     desktop.getJDesktop().setOpaque(true);
103
104                     buildUI();
105                 }});
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);
110         }
111         
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();
120     }
121     
122     protected void buildUI() {
123     }
124     
125     public void update(float tpf) {
126         guiInput.update(tpf);
127         guiNode.updateGeometricState(tpf, true);
128         super.update(tpf);
129     }
130     
131     public void render(float tpf) {
132         DisplaySystem.getDisplaySystem().getRenderer().draw(guiNode);
133     }
134     
135     public void cleanup() {
136         desktop.dispose();
137     }
138     
139     public Node getGUINode() {
140         return guiNode;
141     }
142     
143     public JMEDesktop getDesktop() {
144         return desktop;
145     }
146     
147     public InputHandler getInputHandler() {
148         return guiInput;
149     }
150 }