PageRenderTime 4568ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/ardor3d-examples/src/main/java/com/ardor3d/example/canvas/LwjglAwtExample.java

https://github.com/thecookie/Ardor3D
Java | 217 lines | 164 code | 43 blank | 10 comment | 7 complexity | 85ebb1f7a9f8d2c98a59dc2e3668e995 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /**
  2. * Copyright (c) 2008-2009 Ardor Labs, Inc.
  3. *
  4. * This file is part of Ardor3D.
  5. *
  6. * Ardor3D is free software: you can redistribute it and/or modify it
  7. * under the terms of its license which may be found in the accompanying
  8. * LICENSE file or at <http://www.ardor3d.com/LICENSE>.
  9. */
  10. package com.ardor3d.example.canvas;
  11. import java.awt.Dimension;
  12. import java.awt.GridLayout;
  13. import java.awt.event.WindowAdapter;
  14. import java.awt.event.WindowEvent;
  15. import java.io.IOException;
  16. import java.net.URISyntaxException;
  17. import java.util.HashMap;
  18. import java.util.Map;
  19. import javax.swing.JFrame;
  20. import javax.swing.JLabel;
  21. import javax.swing.SwingConstants;
  22. import com.ardor3d.example.ExampleBase;
  23. import com.ardor3d.example.Exit;
  24. import com.ardor3d.framework.ArdorModule;
  25. import com.ardor3d.framework.Canvas;
  26. import com.ardor3d.framework.DisplaySettings;
  27. import com.ardor3d.framework.FrameHandler;
  28. import com.ardor3d.framework.lwjgl.LwjglAwtCanvas;
  29. import com.ardor3d.framework.lwjgl.LwjglCanvasRenderer;
  30. import com.ardor3d.image.util.AWTImageLoader;
  31. import com.ardor3d.input.ControllerWrapper;
  32. import com.ardor3d.input.Key;
  33. import com.ardor3d.input.MouseCursor;
  34. import com.ardor3d.input.PhysicalLayer;
  35. import com.ardor3d.input.awt.AwtFocusWrapper;
  36. import com.ardor3d.input.awt.AwtKeyboardWrapper;
  37. import com.ardor3d.input.awt.AwtMouseManager;
  38. import com.ardor3d.input.awt.AwtMouseWrapper;
  39. import com.ardor3d.input.logical.DummyControllerWrapper;
  40. import com.ardor3d.input.logical.InputTrigger;
  41. import com.ardor3d.input.logical.KeyPressedCondition;
  42. import com.ardor3d.input.logical.LogicalLayer;
  43. import com.ardor3d.input.logical.TriggerAction;
  44. import com.ardor3d.input.logical.TwoInputStates;
  45. import com.ardor3d.util.resource.ResourceLocatorTool;
  46. import com.ardor3d.util.resource.SimpleResourceLocator;
  47. import com.google.inject.Guice;
  48. import com.google.inject.Injector;
  49. import com.google.inject.Module;
  50. import com.google.inject.Stage;
  51. public class LwjglAwtExample {
  52. static MouseCursor _cursor1;
  53. static MouseCursor _cursor2;
  54. static Map<Canvas, Boolean> _showCursor1 = new HashMap<Canvas, Boolean>();
  55. public static void main(final String[] args) throws Exception {
  56. System.setProperty("ardor3d.useMultipleContexts", "true");
  57. final Module ardorModule = new ArdorModule();
  58. // final Module systemModule = new LwjglAwtModule();
  59. final Injector injector = Guice.createInjector(Stage.PRODUCTION, ardorModule);
  60. final FrameHandler frameWork = injector.getInstance(FrameHandler.class);
  61. final MyExit exit = new MyExit();
  62. final LogicalLayer logicalLayer = injector.getInstance(LogicalLayer.class);
  63. final ExampleScene scene1 = new ExampleScene();
  64. final RotatingCubeGame game1 = new RotatingCubeGame(scene1, exit, logicalLayer, Key.T);
  65. final ExampleScene scene2 = new ExampleScene();
  66. final RotatingCubeGame game2 = new RotatingCubeGame(scene2, exit, logicalLayer, Key.G);
  67. frameWork.addUpdater(game1);
  68. frameWork.addUpdater(game2);
  69. final JFrame frame = new JFrame("AWT Example");
  70. frame.addWindowListener(new WindowAdapter() {
  71. @Override
  72. public void windowClosing(final WindowEvent e) {
  73. exit.exit();
  74. }
  75. });
  76. frame.setLayout(new GridLayout(2, 3));
  77. AWTImageLoader.registerLoader();
  78. try {
  79. final SimpleResourceLocator srl = new SimpleResourceLocator(ExampleBase.class.getClassLoader().getResource(
  80. "com/ardor3d/example/media/"));
  81. ResourceLocatorTool.addResourceLocator(ResourceLocatorTool.TYPE_TEXTURE, srl);
  82. } catch (final URISyntaxException ex) {
  83. ex.printStackTrace();
  84. }
  85. final AWTImageLoader awtImageLoader = new AWTImageLoader();
  86. _cursor1 = createMouseCursor(awtImageLoader, "/com/ardor3d/example/media/input/wait_cursor.png");
  87. _cursor2 = createMouseCursor(awtImageLoader, "/com/ardor3d/example/media/input/movedata.gif");
  88. addCanvas(frame, scene1, logicalLayer, frameWork);
  89. frame
  90. .add(new JLabel(
  91. "<html>"
  92. + "<table>"
  93. + "<tr><th align=\"left\" style=\"font-size: 16\">Action</th><th align=\"left\" style=\"font-size: 16\">Command</th></tr>"
  94. + "<tr><td>WS</td><td>Move camera position forward/back</td></tr>"
  95. + "<tr><td>AD</td><td>Turn camera left/right</td></tr>"
  96. + "<tr><td>QE</td><td>Strafe camera left/right</td></tr>"
  97. + "<tr><td>T</td><td>Toggle cube rotation for scene 1 on press</td></tr>"
  98. + "<tr><td>G</td><td>Toggle cube rotation for scene 2 on press</td></tr>"
  99. + "<tr><td>U</td><td>Toggle both cube rotations on release</td></tr>"
  100. + "<tr><td>0 (zero)</td><td>Reset camera position</td></tr>"
  101. + "<tr><td>9</td><td>Face camera towards cube without changing position</td></tr>"
  102. + "<tr><td>ESC</td><td>Quit</td></tr>"
  103. + "<tr><td>Mouse</td><td>Press left button to rotate camera.</td></tr>" + "</table>"
  104. + "</html>", SwingConstants.CENTER));
  105. addCanvas(frame, scene1, logicalLayer, frameWork);
  106. frame.add(new JLabel("", SwingConstants.CENTER));
  107. addCanvas(frame, scene2, logicalLayer, frameWork);
  108. frame.add(new JLabel("", SwingConstants.CENTER));
  109. frame.pack();
  110. frame.setVisible(true);
  111. game1.init();
  112. game2.init();
  113. while (!exit.isExit()) {
  114. frameWork.updateFrame();
  115. Thread.yield();
  116. }
  117. frame.dispose();
  118. System.exit(0);
  119. }
  120. private static MouseCursor createMouseCursor(final AWTImageLoader awtImageLoader, final String resourceName)
  121. throws IOException {
  122. final com.ardor3d.image.Image image = awtImageLoader.load(LwjglAwtExample.class
  123. .getResourceAsStream(resourceName), false);
  124. return new MouseCursor("cursor1", image, 0, image.getHeight() - 1);
  125. }
  126. private static void addCanvas(final JFrame frame, final ExampleScene scene, final LogicalLayer logicalLayer,
  127. final FrameHandler frameWork) throws Exception {
  128. final LwjglCanvasRenderer canvasRenderer = new LwjglCanvasRenderer(scene);
  129. final DisplaySettings settings = new DisplaySettings(400, 300, 24, 0, 0, 16, 0, 0, false, false);
  130. final LwjglAwtCanvas theCanvas = new LwjglAwtCanvas(settings, canvasRenderer);
  131. frame.add(theCanvas);
  132. _showCursor1.put(theCanvas, true);
  133. theCanvas.setSize(new Dimension(400, 300));
  134. theCanvas.setVisible(true);
  135. final AwtKeyboardWrapper keyboardWrapper = new AwtKeyboardWrapper(theCanvas);
  136. final AwtFocusWrapper focusWrapper = new AwtFocusWrapper(theCanvas);
  137. final AwtMouseManager mouseManager = new AwtMouseManager(theCanvas);
  138. final AwtMouseWrapper mouseWrapper = new AwtMouseWrapper(theCanvas, mouseManager);
  139. final ControllerWrapper controllerWrapper = new DummyControllerWrapper();
  140. final PhysicalLayer pl = new PhysicalLayer(keyboardWrapper, mouseWrapper, controllerWrapper, focusWrapper);
  141. logicalLayer.registerInput(theCanvas, pl);
  142. logicalLayer.registerTrigger(new InputTrigger(new KeyPressedCondition(Key.H), new TriggerAction() {
  143. public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
  144. if (source != theCanvas) {
  145. return;
  146. }
  147. if (_showCursor1.get(theCanvas)) {
  148. mouseManager.setCursor(_cursor1);
  149. } else {
  150. mouseManager.setCursor(_cursor2);
  151. }
  152. _showCursor1.put(theCanvas, !_showCursor1.get(theCanvas));
  153. }
  154. }));
  155. logicalLayer.registerTrigger(new InputTrigger(new KeyPressedCondition(Key.J), new TriggerAction() {
  156. public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
  157. if (source != theCanvas) {
  158. return;
  159. }
  160. mouseManager.setCursor(MouseCursor.SYSTEM_DEFAULT);
  161. }
  162. }));
  163. frameWork.addCanvas(theCanvas);
  164. }
  165. private static class MyExit implements Exit {
  166. private volatile boolean exit = false;
  167. public void exit() {
  168. exit = true;
  169. }
  170. public boolean isExit() {
  171. return exit;
  172. }
  173. }
  174. }