2 * Copyright (c) 2009-2010 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.
33 package com.jme3.niftygui;
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;
54 public class NiftyJmeDisplay implements SceneProcessor {
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;
66 protected ResourceLocationJme resourceLocation;
70 protected class ResourceLocationJme implements ResourceLocation {
72 public InputStream getResourceAsStream(String path) {
73 AssetKey<Object> key = new AssetKey<Object>(path);
74 AssetInfo info = assetManager.locateAsset(key);
76 return info.openStream();
78 throw new AssetNotFoundException(path);
82 public URL getResource(String path) {
83 throw new UnsupportedOperationException();
87 //Empty constructor needed for jMP to create replacement input system
88 public NiftyJmeDisplay() {
91 public NiftyJmeDisplay(AssetManager assetManager,
92 InputManager inputManager,
93 AudioRenderer audioRenderer,
95 this.assetManager = assetManager;
97 w = vp.getCamera().getWidth();
98 h = vp.getCamera().getHeight();
100 soundDev = new SoundDeviceJme(assetManager, audioRenderer);
101 renderDev = new RenderDeviceJme(this);
102 inputSys = new InputSystemJme(inputManager);
103 if (inputManager != null)
104 inputManager.addRawInputListener(inputSys);
106 nifty = new Nifty(renderDev, soundDev, inputSys, new FastTimeProvider());
107 inputSys.setNifty(nifty);
109 resourceLocation = new ResourceLocationJme();
110 nifty.getResourceLoader().removeAllResourceLocations();
111 nifty.getResourceLoader().addResourceLocation(resourceLocation);
114 public void initialize(RenderManager rm, ViewPort vp) {
115 this.renderManager = rm;
116 renderDev.setRenderManager(rm);
119 this.renderer = rm.getRenderer();
121 inputSys.setHeight(vp.getCamera().getHeight());
124 public Nifty getNifty() {
128 RenderDeviceJme getRenderDevice() {
132 AssetManager getAssetManager() {
136 RenderManager getRenderManager() {
137 return renderManager;
148 Renderer getRenderer(){
152 public void reshape(ViewPort vp, int w, int h) {
155 inputSys.setHeight(h);
156 nifty.resolutionChanged();
159 public boolean isInitialized() {
163 public void preFrame(float tpf) {
166 public void postQueue(RenderQueue rq) {
167 // render nifty before anything else
168 renderManager.setCamera(vp.getCamera(), true);
171 renderManager.setCamera(vp.getCamera(), false);
174 public void postFrame(FrameBuffer out) {
177 public void cleanup() {