PageRenderTime 563ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/run/automenta/spacenet/run/old/swing/ArdorAwtExample.java

https://github.com/automenta/spacegraph1
Java | 566 lines | 411 code | 117 blank | 38 comment | 14 complexity | d18c432c7117731de946023f5cd1383b MD5 | raw file
Possible License(s): LGPL-3.0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package automenta.spacenet.run.old.swing;
  6. import automenta.spacenet.space.video.Exit;
  7. import com.ardor3d.annotation.MainThread;
  8. import com.ardor3d.framework.ArdorModule;
  9. import com.ardor3d.framework.Canvas;
  10. import com.ardor3d.framework.DisplaySettings;
  11. import com.ardor3d.framework.FrameHandler;
  12. import com.ardor3d.framework.Scene;
  13. import com.ardor3d.framework.Updater;
  14. import com.ardor3d.framework.jogl.JoglAwtCanvas;
  15. import com.ardor3d.framework.jogl.JoglCanvasRenderer;
  16. import com.ardor3d.image.util.AWTImageLoader;
  17. import com.ardor3d.input.ButtonState;
  18. import com.ardor3d.input.ControllerWrapper;
  19. import com.ardor3d.input.InputState;
  20. import com.ardor3d.input.Key;
  21. import com.ardor3d.input.MouseCursor;
  22. import com.ardor3d.input.MouseState;
  23. import com.ardor3d.input.PhysicalLayer;
  24. import com.ardor3d.input.awt.AwtFocusWrapper;
  25. import com.ardor3d.input.awt.AwtKeyboardWrapper;
  26. import com.ardor3d.input.awt.AwtMouseManager;
  27. import com.ardor3d.input.awt.AwtMouseWrapper;
  28. import com.ardor3d.input.logical.AnyKeyCondition;
  29. import com.ardor3d.input.logical.DummyControllerWrapper;
  30. import com.ardor3d.input.logical.InputTrigger;
  31. import com.ardor3d.input.logical.KeyHeldCondition;
  32. import com.ardor3d.input.logical.KeyPressedCondition;
  33. import com.ardor3d.input.logical.KeyReleasedCondition;
  34. import com.ardor3d.input.logical.LogicalLayer;
  35. import com.ardor3d.input.logical.MouseButtonCondition;
  36. import com.ardor3d.input.logical.TriggerAction;
  37. import com.ardor3d.input.logical.TriggerConditions;
  38. import com.ardor3d.input.logical.TwoInputStates;
  39. import com.ardor3d.intersection.PickResults;
  40. import com.ardor3d.light.PointLight;
  41. import com.ardor3d.math.ColorRGBA;
  42. import com.ardor3d.math.Matrix3;
  43. import com.ardor3d.math.Ray3;
  44. import com.ardor3d.math.Vector3;
  45. import com.ardor3d.renderer.Camera;
  46. import com.ardor3d.renderer.Camera.ProjectionMode;
  47. import com.ardor3d.renderer.Renderer;
  48. import com.ardor3d.renderer.state.LightState;
  49. import com.ardor3d.renderer.state.ZBufferState;
  50. import com.ardor3d.scenegraph.Mesh;
  51. import com.ardor3d.scenegraph.Node;
  52. import com.ardor3d.scenegraph.shape.Box;
  53. import com.ardor3d.util.ReadOnlyTimer;
  54. import com.google.common.base.Predicate;
  55. import com.google.common.base.Predicates;
  56. import com.google.inject.Guice;
  57. import com.google.inject.Inject;
  58. import com.google.inject.Injector;
  59. import com.google.inject.Module;
  60. import com.google.inject.Stage;
  61. import java.awt.Dimension;
  62. import java.awt.GridLayout;
  63. import java.awt.event.WindowAdapter;
  64. import java.awt.event.WindowEvent;
  65. import java.util.HashMap;
  66. import java.util.Map;
  67. import java.util.Random;
  68. import javax.swing.JFrame;
  69. import javax.swing.JLabel;
  70. import javax.swing.SwingConstants;
  71. /**
  72. *
  73. * @author seh
  74. */
  75. public class ArdorAwtExample {
  76. public final static class ExampleScene implements Scene {
  77. private final Node root;
  78. public ExampleScene() {
  79. root = new Node("root");
  80. }
  81. public Node getRoot() {
  82. return root;
  83. }
  84. @MainThread
  85. public boolean renderUnto(final Renderer renderer) {
  86. renderer.draw(root);
  87. return true;
  88. }
  89. public PickResults doPick(final Ray3 pickRay) {
  90. // does nothing.
  91. return null;
  92. }
  93. }
  94. static MouseCursor _cursor1;
  95. static MouseCursor _cursor2;
  96. static Map<Canvas, Boolean> _showCursor1 = new HashMap<Canvas, Boolean>();
  97. public static void main(final String[] args) throws Exception {
  98. System.setProperty("ardor3d.useMultipleContexts", "true");
  99. final Module ardorModule = new ArdorModule();
  100. // final Module systemModule = new LwjglAwtModule();
  101. final Injector injector = Guice.createInjector(Stage.PRODUCTION, ardorModule);
  102. final FrameHandler frameWork = injector.getInstance(FrameHandler.class);
  103. final MyExit exit = new MyExit();
  104. final LogicalLayer logicalLayer = injector.getInstance(LogicalLayer.class);
  105. final ExampleScene scene1 = new ExampleScene();
  106. final RotatingCubeGame game1 = new RotatingCubeGame(scene1, exit, logicalLayer, Key.T);
  107. frameWork.addUpdater(game1);
  108. final ExampleScene scene2 = new ExampleScene();
  109. final RotatingCubeGame game2 = new RotatingCubeGame(scene2, exit, logicalLayer, Key.G);
  110. frameWork.addUpdater(game2);
  111. final JFrame frame = new JFrame("AWT Example");
  112. frame.addWindowListener(new WindowAdapter() {
  113. @Override
  114. public void windowClosing(final WindowEvent e) {
  115. exit.exit();
  116. }
  117. });
  118. frame.setLayout(new GridLayout(2, 3));
  119. AWTImageLoader.registerLoader();
  120. // try {
  121. // final SimpleResourceLocator srl = new SimpleResourceLocator(ExampleBase.class.getClassLoader().getResource(
  122. // "com/ardor3d/example/media/"));
  123. // ResourceLocatorTool.addResourceLocator(ResourceLocatorTool.TYPE_TEXTURE, srl);
  124. // } catch (final URISyntaxException ex) {
  125. // ex.printStackTrace();
  126. // }
  127. final AWTImageLoader awtImageLoader = new AWTImageLoader();
  128. // _cursor1 = createMouseCursor(awtImageLoader, "/com/ardor3d/example/media/input/wait_cursor.png");
  129. // _cursor2 = createMouseCursor(awtImageLoader, "/com/ardor3d/example/media/input/movedata.gif");
  130. addCanvas(frame, scene1, logicalLayer, frameWork);
  131. frame.add(new JLabel(
  132. "<html>" + "<table>" + "<tr><th align=\"left\" style=\"font-size: 16\">Action</th><th align=\"left\" style=\"font-size: 16\">Command</th></tr>" + "<tr><td>WS</td><td>Move camera position forward/back</td></tr>" + "<tr><td>AD</td><td>Turn camera left/right</td></tr>" + "<tr><td>QE</td><td>Strafe camera left/right</td></tr>" + "<tr><td>T</td><td>Toggle cube rotation for scene 1 on press</td></tr>" + "<tr><td>G</td><td>Toggle cube rotation for scene 2 on press</td></tr>" + "<tr><td>U</td><td>Toggle both cube rotations on release</td></tr>" + "<tr><td>0 (zero)</td><td>Reset camera position</td></tr>" + "<tr><td>9</td><td>Face camera towards cube without changing position</td></tr>" + "<tr><td>ESC</td><td>Quit</td></tr>" + "<tr><td>Mouse</td><td>Press left button to rotate camera.</td></tr>" + "</table>" + "</html>", SwingConstants.CENTER));
  133. addCanvas(frame, scene1, logicalLayer, frameWork);
  134. frame.add(new JLabel("", SwingConstants.CENTER));
  135. addCanvas(frame, scene2, logicalLayer, frameWork);
  136. frame.add(new JLabel("", SwingConstants.CENTER));
  137. frame.pack();
  138. frame.setVisible(true);
  139. game1.init();
  140. game2.init();
  141. while (!exit.isExit()) {
  142. frameWork.updateFrame();
  143. Thread.yield();
  144. }
  145. frame.dispose();
  146. System.exit(0);
  147. }
  148. // private static MouseCursor createMouseCursor(final AWTImageLoader awtImageLoader, final String resourceName) throws IOException {
  149. // final com.ardor3d.image.Image image = awtImageLoader.load(JoglAwtExample.class.getResourceAsStream(resourceName), false);
  150. //
  151. // return new MouseCursor("cursor1", image, 0, image.getHeight() - 1);
  152. // }
  153. private static void addCanvas(final JFrame frame, final ExampleScene scene, final LogicalLayer logicalLayer,
  154. final FrameHandler frameWork) throws Exception {
  155. final JoglCanvasRenderer canvasRenderer = new JoglCanvasRenderer(scene);
  156. final DisplaySettings settings = new DisplaySettings(400, 300, 24, 0, 0, 16, 0, 0, false, false);
  157. final JoglAwtCanvas theCanvas = new JoglAwtCanvas(settings, canvasRenderer);
  158. frame.add(theCanvas);
  159. _showCursor1.put(theCanvas, true);
  160. theCanvas.setSize(new Dimension(400, 300));
  161. theCanvas.setVisible(true);
  162. final AwtKeyboardWrapper keyboardWrapper = new AwtKeyboardWrapper(theCanvas);
  163. final AwtFocusWrapper focusWrapper = new AwtFocusWrapper(theCanvas);
  164. final AwtMouseManager mouseManager = new AwtMouseManager(theCanvas);
  165. final AwtMouseWrapper mouseWrapper = new AwtMouseWrapper(theCanvas, mouseManager);
  166. final ControllerWrapper controllerWrapper = new DummyControllerWrapper();
  167. final PhysicalLayer pl = new PhysicalLayer(keyboardWrapper, mouseWrapper, controllerWrapper, focusWrapper);
  168. logicalLayer.registerInput(theCanvas, pl);
  169. logicalLayer.registerTrigger(new InputTrigger(new KeyPressedCondition(Key.H), new TriggerAction() {
  170. public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
  171. if (source != theCanvas) {
  172. return;
  173. }
  174. if (_showCursor1.get(theCanvas)) {
  175. mouseManager.setCursor(_cursor1);
  176. } else {
  177. mouseManager.setCursor(_cursor2);
  178. }
  179. _showCursor1.put(theCanvas, !_showCursor1.get(theCanvas));
  180. }
  181. }));
  182. logicalLayer.registerTrigger(new InputTrigger(new KeyPressedCondition(Key.J), new TriggerAction() {
  183. public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
  184. if (source != theCanvas) {
  185. return;
  186. }
  187. mouseManager.setCursor(MouseCursor.SYSTEM_DEFAULT);
  188. }
  189. }));
  190. frameWork.addCanvas(theCanvas);
  191. }
  192. private static class MyExit implements Exit {
  193. private volatile boolean exit = false;
  194. public void exit() {
  195. exit = true;
  196. }
  197. public boolean isExit() {
  198. return exit;
  199. }
  200. }
  201. public static class RotatingCubeGame implements Updater {
  202. // private final Canvas view;
  203. private final ExampleScene scene;
  204. private final Exit exit;
  205. private final LogicalLayer logicalLayer;
  206. private final Key toggleRotationKey;
  207. private final static float CUBE_ROTATE_SPEED = 1;
  208. private final Vector3 rotationAxis = new Vector3(1, 1, 0);
  209. private double angle = 0;
  210. private Mesh box = new Box("The cube", new Vector3(-1, -1, -1), new Vector3(1, 1, 1));
  211. private final Matrix3 rotation = new Matrix3();
  212. private static final int MOVE_SPEED = 4;
  213. private static final double TURN_SPEED = 0.5;
  214. private final Matrix3 _incr = new Matrix3();
  215. private static final double MOUSE_TURN_SPEED = 1;
  216. private int rotationSign = 1;
  217. @Inject
  218. public RotatingCubeGame(final ExampleScene scene, final Exit exit, final LogicalLayer logicalLayer,
  219. final Key toggleRotationKey) {
  220. this.scene = scene;
  221. this.exit = exit;
  222. this.logicalLayer = logicalLayer;
  223. this.toggleRotationKey = toggleRotationKey;
  224. }
  225. @MainThread
  226. public void init() {
  227. // add a cube to the scene
  228. // add a rotating controller to the cube
  229. // add a light
  230. // box = SimpleShapeFactory.createQuad("the 'box'", 1, 1);
  231. final ZBufferState buf = new ZBufferState();
  232. buf.setEnabled(true);
  233. buf.setFunction(ZBufferState.TestFunction.LessThanOrEqualTo);
  234. scene.getRoot().setRenderState(buf);
  235. // Add a texture to the box.
  236. // final TextureState ts = new TextureState();
  237. // ts.setTexture(TextureManager.load("images/ardor3d_white_256.jpg", Texture.MinificationFilter.Trilinear,
  238. // Format.GuessNoCompression, true));
  239. // box.setRenderState(ts);
  240. box.setRandomColors();
  241. final PointLight light = new PointLight();
  242. final Random random = new Random();
  243. final float r = random.nextFloat();
  244. final float g = random.nextFloat();
  245. final float b = random.nextFloat();
  246. final float a = random.nextFloat();
  247. light.setDiffuse(new ColorRGBA(r, g, b, a));
  248. light.setAmbient(new ColorRGBA(0.5f, 0.5f, 0.5f, 1.0f));
  249. light.setLocation(new Vector3(MOVE_SPEED, MOVE_SPEED, MOVE_SPEED));
  250. light.setEnabled(true);
  251. /** Attach the light to a lightState and the lightState to rootNode. */
  252. final LightState lightState = new LightState();
  253. lightState.setEnabled(true);
  254. lightState.attach(light);
  255. scene.getRoot().setRenderState(lightState);
  256. scene.getRoot().attachChild(box);
  257. registerInputTriggers();
  258. }
  259. private void registerInputTriggers() {
  260. logicalLayer.registerTrigger(new InputTrigger(new KeyHeldCondition(Key.W), new TriggerAction() {
  261. public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
  262. moveForward(source, tpf);
  263. }
  264. }));
  265. logicalLayer.registerTrigger(new InputTrigger(new KeyHeldCondition(Key.S), new TriggerAction() {
  266. public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
  267. moveBack(source, tpf);
  268. }
  269. }));
  270. logicalLayer.registerTrigger(new InputTrigger(new KeyHeldCondition(Key.A), new TriggerAction() {
  271. public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
  272. turnLeft(source, tpf);
  273. }
  274. }));
  275. logicalLayer.registerTrigger(new InputTrigger(new KeyHeldCondition(Key.D), new TriggerAction() {
  276. public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
  277. turnRight(source, tpf);
  278. }
  279. }));
  280. logicalLayer.registerTrigger(new InputTrigger(new KeyHeldCondition(Key.Q), new TriggerAction() {
  281. public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
  282. moveLeft(source, tpf);
  283. }
  284. }));
  285. logicalLayer.registerTrigger(new InputTrigger(new KeyHeldCondition(Key.E), new TriggerAction() {
  286. public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
  287. moveRight(source, tpf);
  288. }
  289. }));
  290. logicalLayer.registerTrigger(new InputTrigger(new KeyPressedCondition(Key.ESCAPE), new TriggerAction() {
  291. public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
  292. exit.exit();
  293. }
  294. }));
  295. logicalLayer.registerTrigger(new InputTrigger(new KeyPressedCondition(toggleRotationKey), new TriggerAction() {
  296. public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
  297. toggleRotation();
  298. }
  299. }));
  300. logicalLayer.registerTrigger(new InputTrigger(new KeyReleasedCondition(Key.U), new TriggerAction() {
  301. public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
  302. toggleRotation();
  303. }
  304. }));
  305. logicalLayer.registerTrigger(new InputTrigger(new KeyPressedCondition(Key.ZERO), new TriggerAction() {
  306. public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
  307. resetCamera(source);
  308. }
  309. }));
  310. logicalLayer.registerTrigger(new InputTrigger(new KeyPressedCondition(Key.NINE), new TriggerAction() {
  311. public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
  312. lookAtZero(source);
  313. }
  314. }));
  315. final Predicate<TwoInputStates> mouseMovedAndOneButtonPressed = Predicates.and(TriggerConditions.mouseMoved(),
  316. Predicates.or(TriggerConditions.leftButtonDown(), TriggerConditions.rightButtonDown()));
  317. logicalLayer.registerTrigger(new InputTrigger(mouseMovedAndOneButtonPressed, new TriggerAction() {
  318. public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
  319. final MouseState mouseState = inputStates.getCurrent().getMouseState();
  320. turn(source, mouseState.getDx() * tpf * -MOUSE_TURN_SPEED);
  321. rotateUpDown(source, mouseState.getDy() * tpf * -MOUSE_TURN_SPEED);
  322. }
  323. }));
  324. logicalLayer.registerTrigger(new InputTrigger(new MouseButtonCondition(ButtonState.DOWN, ButtonState.DOWN,
  325. ButtonState.UNDEFINED), new TriggerAction() {
  326. public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
  327. moveForward(source, tpf);
  328. }
  329. }));
  330. logicalLayer.registerTrigger(new InputTrigger(new AnyKeyCondition(), new TriggerAction() {
  331. public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
  332. final InputState current = inputStates.getCurrent();
  333. System.out.println("Key character pressed: " + current.getKeyboardState().getKeyEvent().getKeyChar());
  334. }
  335. }));
  336. }
  337. private void lookAtZero(final Canvas source) {
  338. source.getCanvasRenderer().getCamera().lookAt(Vector3.ZERO, Vector3.UNIT_Y);
  339. }
  340. private void resetCamera(final Canvas source) {
  341. final Vector3 loc = new Vector3(0.0f, 0.0f, 10.0f);
  342. final Vector3 left = new Vector3(-1.0f, 0.0f, 0.0f);
  343. final Vector3 up = new Vector3(0.0f, 1.0f, 0.0f);
  344. final Vector3 dir = new Vector3(0.0f, 0f, -1.0f);
  345. source.getCanvasRenderer().getCamera().setFrame(loc, left, up, dir);
  346. }
  347. private void toggleRotation() {
  348. rotationSign *= -1;
  349. }
  350. @MainThread
  351. public void update(final ReadOnlyTimer timer) {
  352. final double tpf = timer.getTimePerFrame();
  353. logicalLayer.checkTriggers(tpf);
  354. // rotate away
  355. angle += tpf * CUBE_ROTATE_SPEED * rotationSign;
  356. rotation.fromAngleAxis(angle, rotationAxis);
  357. if (box!=null) {
  358. box.setRotation(rotation);
  359. box.updateGeometricState(tpf, true);
  360. }
  361. }
  362. private void rotateUpDown(final Canvas canvas, final double speed) {
  363. final Camera camera = canvas.getCanvasRenderer().getCamera();
  364. final Vector3 temp = Vector3.fetchTempInstance();
  365. _incr.fromAngleNormalAxis(speed, camera.getLeft());
  366. _incr.applyPost(camera.getLeft(), temp);
  367. camera.setLeft(temp);
  368. _incr.applyPost(camera.getDirection(), temp);
  369. camera.setDirection(temp);
  370. _incr.applyPost(camera.getUp(), temp);
  371. camera.setUp(temp);
  372. Vector3.releaseTempInstance(temp);
  373. camera.normalize();
  374. }
  375. private void turnRight(final Canvas canvas, final double tpf) {
  376. turn(canvas, -TURN_SPEED * tpf);
  377. }
  378. private void turn(final Canvas canvas, final double speed) {
  379. final Camera camera = canvas.getCanvasRenderer().getCamera();
  380. final Vector3 temp = Vector3.fetchTempInstance();
  381. _incr.fromAngleNormalAxis(speed, camera.getUp());
  382. _incr.applyPost(camera.getLeft(), temp);
  383. camera.setLeft(temp);
  384. _incr.applyPost(camera.getDirection(), temp);
  385. camera.setDirection(temp);
  386. _incr.applyPost(camera.getUp(), temp);
  387. camera.setUp(temp);
  388. Vector3.releaseTempInstance(temp);
  389. camera.normalize();
  390. }
  391. private void turnLeft(final Canvas canvas, final double tpf) {
  392. turn(canvas, TURN_SPEED * tpf);
  393. }
  394. private void moveForward(final Canvas canvas, final double tpf) {
  395. final Camera camera = canvas.getCanvasRenderer().getCamera();
  396. final Vector3 loc = Vector3.fetchTempInstance().set(camera.getLocation());
  397. final Vector3 dir = Vector3.fetchTempInstance();
  398. if (camera.getProjectionMode() == ProjectionMode.Perspective) {
  399. dir.set(camera.getDirection());
  400. } else {
  401. // move up if in parallel mode
  402. dir.set(camera.getUp());
  403. }
  404. dir.multiplyLocal(MOVE_SPEED * tpf);
  405. loc.addLocal(dir);
  406. camera.setLocation(loc);
  407. Vector3.releaseTempInstance(loc);
  408. Vector3.releaseTempInstance(dir);
  409. }
  410. private void moveLeft(final Canvas canvas, final double tpf) {
  411. final Camera camera = canvas.getCanvasRenderer().getCamera();
  412. final Vector3 loc = Vector3.fetchTempInstance().set(camera.getLocation());
  413. final Vector3 dir = Vector3.fetchTempInstance();
  414. dir.set(camera.getLeft());
  415. dir.multiplyLocal(MOVE_SPEED * tpf);
  416. loc.addLocal(dir);
  417. camera.setLocation(loc);
  418. Vector3.releaseTempInstance(loc);
  419. Vector3.releaseTempInstance(dir);
  420. }
  421. private void moveRight(final Canvas canvas, final double tpf) {
  422. final Camera camera = canvas.getCanvasRenderer().getCamera();
  423. final Vector3 loc = Vector3.fetchTempInstance().set(camera.getLocation());
  424. final Vector3 dir = Vector3.fetchTempInstance();
  425. dir.set(camera.getLeft());
  426. dir.multiplyLocal(-MOVE_SPEED * tpf);
  427. loc.addLocal(dir);
  428. camera.setLocation(loc);
  429. Vector3.releaseTempInstance(loc);
  430. Vector3.releaseTempInstance(dir);
  431. }
  432. private void moveBack(final Canvas canvas, final double tpf) {
  433. final Camera camera = canvas.getCanvasRenderer().getCamera();
  434. final Vector3 loc = Vector3.fetchTempInstance().set(camera.getLocation());
  435. final Vector3 dir = Vector3.fetchTempInstance();
  436. if (camera.getProjectionMode() == ProjectionMode.Perspective) {
  437. dir.set(camera.getDirection());
  438. } else {
  439. // move up if in parallel mode
  440. dir.set(camera.getUp());
  441. }
  442. dir.multiplyLocal(-MOVE_SPEED * tpf);
  443. loc.addLocal(dir);
  444. camera.setLocation(loc);
  445. Vector3.releaseTempInstance(loc);
  446. Vector3.releaseTempInstance(dir);
  447. }
  448. }
  449. }