📄 root.java
字号:
/* Ogre4J
Copyright (C) 2002 macross
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.ogre4j;
import java.util.Iterator;
import java.util.Vector;
import org.ogre4j.event.FrameEvent;
import org.ogre4j.event.IEventHub;
import org.ogre4j.event.IFrameListener;
import org.ogre4j.event.IKeyListener;
import org.ogre4j.event.IMouseListener;
import org.ogre4j.event.IMouseMotionListener;
import org.ogre4j.event.KeyEvent;
import org.ogre4j.event.MouseEvent;
/**
* Root
*
* @author Ivica Aracic <ivica.aracic@bytelords.de>
*/
public final class Root {
public static final String
DEF_PLUGIN_FILE = "Plugins.cfg",
DEF_RESSOURCES_FILE = "resources.cfg";
/*
* static class code
*/
static {
System.loadLibrary("ogre4j");
}
private EventHub eventHub = new EventHub();
private InputState inputState = new InputState();
private IOgreApp app;
public InputState getInputState() {
return inputState;
}
public void run(IOgreApp app) {
this.app = app;
if(!start())
return;
startRendering(eventHub);
}
/*
* STARTUP/SHUTDOWN
*/
/** start up OGRE with default plugin file name */
private boolean start() {
return
start(
DEF_PLUGIN_FILE,
DEF_RESSOURCES_FILE,
true
);
}
/** start up OGRE */
private native boolean start(
String pluginFile,
String ressourceFile,
boolean showConfigDialog
);
/** shutdown OGRE */
public native void exit();
// singleton managers
public LogManager getLogManager() {
return LogManager.instance();
}
/** returns scene manager */
public native SceneManager getSceneManager(int sceneType);
/**
* there is only one render window,
* therefore everything goes here
*/
public native Viewport addViewport(Camera cam);
public native void writeContentsToFile(String filename);
private native void startRendering(IEventHub listener);
public native void showDebugOverlay(boolean show);
/*
* LISTENERS
*/
public final static int
BLOCK_KEY_EVENTS = 0,
BLOCK_MOUSE_EVENTS = 1,
BLOCK_MOUSE_MOTION_EVENTS = 2;
boolean blockFlags[] = new boolean[3];
Vector frameListeners = new Vector();
Vector renderLoopListeners = new Vector();
Vector keyListeners = new Vector();
Vector mouseListeners = new Vector();
Vector motionListeners = new Vector();
public void addFrameListener(IFrameListener listener) {
frameListeners.add(listener);
}
public void removeFrameListener(IFrameListener listener) {
frameListeners.remove(listener);
}
public void addOgreAppListener(IOgreApp listener) {
renderLoopListeners.add(listener);
}
public void removeOgreAppListener(IOgreApp listener) {
renderLoopListeners.remove(listener);
}
public void addMouseListener(IMouseListener listener) {
mouseListeners.add(listener);
}
public void removeMouseListener(IMouseListener listener) {
mouseListeners.remove(listener);
}
public void addMouseMotionListener(IMouseMotionListener listener) {
motionListeners.add(listener);
}
public void removeMouseMotionListener(IMouseMotionListener listener) {
motionListeners.remove(listener);
}
public void addKeyListener(IKeyListener listener) {
keyListeners.add(listener);
}
public void removeKeyListener(IKeyListener listener) {
keyListeners.remove(listener);
}
public final class EventHub implements IEventHub {
// render loop
public void appStarted() {
app.appStarted();
}
public void appEnded() {
app.appEnded();
}
// frame listener
public void frameStarted(FrameEvent e) {
//System.out.println("frameStarted");
for(Iterator it=frameListeners.iterator(); it.hasNext();) {
((IFrameListener)it.next()).frameStarted(e);
}
//System.out.println(inputState.toString());
}
public void frameEnded(FrameEvent e) {
//System.out.println("frameEnded");
for(Iterator it=frameListeners.iterator(); it.hasNext();) {
((IFrameListener)it.next()).frameEnded(e);
}
inputState.resetMouseState();
}
// keys
public void keyClicked (KeyEvent e) {
for(Iterator it=keyListeners.iterator(); it.hasNext();) {
((IKeyListener)it.next()).keyClicked(e);
}
}
public void keyPressed (KeyEvent e) {
inputState.updateKeyState(e.getKey(), true);
for(Iterator it=keyListeners.iterator(); it.hasNext();) {
((IKeyListener)it.next()).keyPressed(e);
}
}
public void keyReleased (KeyEvent e) {
inputState.updateKeyState(e.getKey(), false);
for(Iterator it=keyListeners.iterator(); it.hasNext();) {
((IKeyListener)it.next()).keyReleased(e);
}
}
// mouse
public void mouseClicked (MouseEvent e) {
for(Iterator it=mouseListeners.iterator(); it.hasNext();) {
((IMouseListener)it.next()).mouseClicked(e);
}
}
public void mouseEntered (MouseEvent e) {
for(Iterator it=mouseListeners.iterator(); it.hasNext();) {
((IMouseListener)it.next()).mouseEntered(e);
}
}
public void mouseExited (MouseEvent e) {
for(Iterator it=mouseListeners.iterator(); it.hasNext();) {
((IMouseListener)it.next()).mouseExited(e);
}
}
public void mousePressed (MouseEvent e) {
inputState.updateButtonState(e.getButtonId(), true);
for(Iterator it=mouseListeners.iterator(); it.hasNext();) {
((IMouseListener)it.next()).mousePressed(e);
}
}
public void mouseReleased (MouseEvent e) {
inputState.updateButtonState(e.getButtonId(), false);
for(Iterator it=mouseListeners.iterator(); it.hasNext();) {
((IMouseListener)it.next()).mouseReleased(e);
}
}
// mouse motion
public void mouseMoved (MouseEvent e) {
inputState.updatePositionState(e);
for(Iterator it=motionListeners.iterator(); it.hasNext();) {
((IMouseMotionListener)it.next()).mouseMoved(e);
}
}
public void mouseDragged (MouseEvent e) {
inputState.updatePositionState(e);
for(Iterator it=motionListeners.iterator(); it.hasNext();) {
((IMouseMotionListener)it.next()).mouseDragged(e);
}
}
}
/*
* SINGLETON
*/
public static Root instance() {
return singleton;
}
private static Root singleton = new Root();
private Root() {
}
/*
* TESTCODE
*/
public static void main(String[] args) {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -