OSDN Git Service

Update niftygui 1.3.3
[mikumikustudio/MikuMikuStudio.git] / engine / src / niftygui / com / jme3 / niftygui / NiftyJmeDisplay.java
1 /*
2  * Copyright (c) 2009-2010 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
33 package com.jme3.niftygui;
34
35 import com.jme3.asset.AssetInfo;
36 import com.jme3.asset.AssetKey;
37 import com.jme3.asset.AssetManager;
38 import com.jme3.asset.AssetNotFoundException;
39 import com.jme3.audio.AudioRenderer;
40 import com.jme3.input.InputManager;
41 import com.jme3.post.SceneProcessor;
42 import com.jme3.renderer.RenderManager;
43 import com.jme3.renderer.Renderer;
44 import com.jme3.renderer.ViewPort;
45 import com.jme3.renderer.queue.RenderQueue;
46 import com.jme3.texture.FrameBuffer;
47 import de.lessvoid.nifty.Nifty;
48 import de.lessvoid.nifty.spi.time.impl.FastTimeProvider;
49 import de.lessvoid.nifty.tools.TimeProvider;
50 import de.lessvoid.nifty.tools.resourceloader.ResourceLocation;
51 import java.io.InputStream;
52 import java.net.URL;
53
54 public class NiftyJmeDisplay implements SceneProcessor {
55
56     protected boolean inited = false;
57     protected Nifty nifty;
58     protected AssetManager assetManager;
59     protected RenderManager renderManager;
60     protected RenderDeviceJme renderDev;
61     protected InputSystemJme inputSys;
62     protected SoundDeviceJme soundDev;
63     protected Renderer renderer;
64     protected ViewPort vp;
65     
66     protected ResourceLocationJme resourceLocation;
67
68     protected int w, h;
69
70     protected class ResourceLocationJme implements ResourceLocation {
71
72         public InputStream getResourceAsStream(String path) {
73             AssetKey<Object> key = new AssetKey<Object>(path);
74             AssetInfo info = assetManager.locateAsset(key);
75             if (info != null){
76                 return info.openStream();
77             }else{
78                 throw new AssetNotFoundException(path);
79             }
80         }
81
82         public URL getResource(String path) {
83             throw new UnsupportedOperationException();
84         }
85     }
86
87     //Empty constructor needed for jMP to create replacement input system
88     public NiftyJmeDisplay() {
89     }
90     
91     public NiftyJmeDisplay(AssetManager assetManager, 
92                            InputManager inputManager,
93                            AudioRenderer audioRenderer,
94                            ViewPort vp){
95         this.assetManager = assetManager;
96
97         w = vp.getCamera().getWidth();
98         h = vp.getCamera().getHeight();
99
100         soundDev = new SoundDeviceJme(assetManager, audioRenderer);
101         renderDev = new RenderDeviceJme(this);
102         inputSys = new InputSystemJme(inputManager);
103         if (inputManager != null)
104             inputManager.addRawInputListener(inputSys);
105         
106         nifty = new Nifty(renderDev, soundDev, inputSys, new FastTimeProvider());
107         inputSys.setNifty(nifty);
108
109         resourceLocation = new ResourceLocationJme();
110         nifty.getResourceLoader().removeAllResourceLocations();
111         nifty.getResourceLoader().addResourceLocation(resourceLocation);
112     }
113
114     public void initialize(RenderManager rm, ViewPort vp) {
115         this.renderManager = rm;
116         renderDev.setRenderManager(rm);
117         inited = true;
118         this.vp = vp;
119         this.renderer = rm.getRenderer();
120         
121         inputSys.setHeight(vp.getCamera().getHeight());
122     }
123
124     public Nifty getNifty() {
125         return nifty;
126     }
127
128     RenderDeviceJme getRenderDevice() {
129         return renderDev;
130     }
131
132     AssetManager getAssetManager() {
133         return assetManager;
134     }
135
136     RenderManager getRenderManager() {
137         return renderManager;
138     }
139
140     int getHeight() {
141         return h;
142     }
143
144     int getWidth() {
145         return w;
146     }
147
148     Renderer getRenderer(){
149         return renderer;
150     }
151
152     public void reshape(ViewPort vp, int w, int h) {
153         this.w = w;
154         this.h = h;
155         inputSys.setHeight(h);
156         nifty.resolutionChanged();
157     }
158
159     public boolean isInitialized() {
160         return inited;
161     }
162
163     public void preFrame(float tpf) {
164     }
165
166     public void postQueue(RenderQueue rq) {
167         // render nifty before anything else
168         renderManager.setCamera(vp.getCamera(), true);
169         //nifty.update();
170         nifty.render(false);
171         renderManager.setCamera(vp.getCamera(), false);
172     }
173
174     public void postFrame(FrameBuffer out) {
175     }
176
177     public void cleanup() {
178         inited = false;
179 //        nifty.exit();
180     }
181
182 }