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