PageRenderTime 2959ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/run/automenta/spacenet/run/old/ArdorPanel.java

https://github.com/automenta/spacegraph1
Java | 292 lines | 207 code | 72 blank | 13 comment | 2 complexity | 1e79efa01f6e36a184976ce8ab0dbe82 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;
  6. import automenta.spacenet.run.ArdorSpacetime;
  7. import automenta.spacenet.run.ArdorSpacetime;
  8. import automenta.spacenet.run.widget.DemoButton;
  9. import automenta.spacenet.space.geom.ProcessBox;
  10. import automenta.spacenet.space.video.Exit;
  11. import com.ardor3d.annotation.MainThread;
  12. import com.ardor3d.framework.ArdorModule;
  13. import com.ardor3d.framework.CanvasRenderer;
  14. import com.ardor3d.framework.DisplaySettings;
  15. import com.ardor3d.framework.FrameHandler;
  16. import com.ardor3d.framework.NativeCanvas;
  17. import com.ardor3d.framework.Scene;
  18. import com.ardor3d.framework.jogl.JoglAwtCanvas;
  19. import com.ardor3d.framework.jogl.JoglCanvasRenderer;
  20. import com.ardor3d.image.Image;
  21. import com.ardor3d.image.util.AWTImageLoader;
  22. import com.ardor3d.input.ControllerWrapper;
  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.DummyControllerWrapper;
  29. import com.ardor3d.input.logical.LogicalLayer;
  30. import com.ardor3d.intersection.PickResults;
  31. import com.ardor3d.math.Ray3;
  32. import com.ardor3d.renderer.Renderer;
  33. import com.ardor3d.scenegraph.Node;
  34. import com.ardor3d.util.Timer;
  35. import com.google.inject.Guice;
  36. import com.google.inject.Injector;
  37. import com.google.inject.Module;
  38. import com.google.inject.Stage;
  39. import java.awt.BorderLayout;
  40. import java.awt.Dimension;
  41. import java.awt.event.ComponentEvent;
  42. import java.awt.event.ComponentListener;
  43. import java.util.concurrent.CountDownLatch;
  44. import java.util.logging.Level;
  45. import java.util.logging.Logger;
  46. import javax.swing.JButton;
  47. import javax.swing.JFrame;
  48. import javax.swing.JPanel;
  49. import javax.swing.event.AncestorEvent;
  50. import javax.swing.event.AncestorListener;
  51. /**
  52. *
  53. * @author seh
  54. */
  55. public class ArdorPanel extends JPanel implements Runnable {
  56. private final MyExit exit;
  57. private final FrameHandler frameWork;
  58. private ArdorSpacetime root;
  59. private JoglAwtCanvas theCanvas;
  60. static {
  61. //System.setProperty("ardor3d.useMultipleContexts", "true");
  62. }
  63. private NativeCanvas nativeCanvas;
  64. public ArdorPanel(ProcessBox content) {
  65. super(new BorderLayout());
  66. final Module ardorModule = new ArdorModule();
  67. final Injector injector = Guice.createInjector(Stage.PRODUCTION, ardorModule);
  68. frameWork = injector.getInstance(FrameHandler.class);
  69. exit = new MyExit();
  70. final LogicalLayer logicalLayer = injector.getInstance(LogicalLayer.class);
  71. //AWTImageLoader.registerLoader();
  72. try {
  73. addCanvas(new AwtScene(content, logicalLayer, frameWork), logicalLayer, frameWork);
  74. } catch (Exception ex) {
  75. Logger.getLogger(ArdorPanel.class.getName()).log(Level.SEVERE, null, ex);
  76. }
  77. start();
  78. }
  79. protected void start() {
  80. new Thread(this).start();
  81. }
  82. public void run() {
  83. root.init();
  84. addComponentListener(new ComponentListener() {
  85. @Override
  86. public void componentResized(ComponentEvent e) {
  87. updateSize();
  88. }
  89. @Override
  90. public void componentMoved(ComponentEvent e) {
  91. }
  92. @Override
  93. public void componentShown(ComponentEvent e) {
  94. }
  95. @Override
  96. public void componentHidden(ComponentEvent e) {
  97. }
  98. });
  99. addAncestorListener(new AncestorListener() {
  100. @Override
  101. public void ancestorRemoved(AncestorEvent event) {
  102. exit.exit = true;
  103. }
  104. @Override
  105. public void ancestorAdded(AncestorEvent event) {
  106. }
  107. @Override
  108. public void ancestorMoved(AncestorEvent event) {
  109. }
  110. });
  111. Timer timer = new Timer();
  112. //TODO un-hack this
  113. boolean updateSizeOnce = false;
  114. while (!exit.isExit()) {
  115. timer.update();
  116. frameWork.updateFrame();
  117. //root.update must be called after frameWork.updateFrame()!
  118. root.update(timer);
  119. Thread.yield();
  120. if (!updateSizeOnce) {
  121. updateSize();
  122. updateSizeOnce = true;
  123. }
  124. }
  125. }
  126. public final class AwtScene implements Scene {
  127. private AwtScene(final ProcessBox content, LogicalLayer logicalLayer, FrameHandler frameWork) {
  128. root = new ArdorSpacetime(logicalLayer, frameWork) {
  129. @Override protected void initWindow() {
  130. root.add(content);
  131. }
  132. public NativeCanvas getCanvas() {
  133. return nativeCanvas;
  134. }
  135. @Override
  136. public NativeCanvas getVideo() {
  137. return nativeCanvas;
  138. }
  139. };
  140. }
  141. public Node getRoot() {
  142. return root;
  143. }
  144. @MainThread
  145. public boolean renderUnto(final Renderer renderer) {
  146. renderer.draw(root);
  147. return true;
  148. }
  149. public PickResults doPick(final Ray3 pickRay) {
  150. // does nothing.
  151. return null;
  152. }
  153. }
  154. private void addCanvas(final AwtScene scene, final LogicalLayer logicalLayer, 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. theCanvas = new JoglAwtCanvas(settings, canvasRenderer);
  158. nativeCanvas = new NativeCanvas() {
  159. @Override public void close() {
  160. }
  161. @Override
  162. public boolean isActive() {
  163. return true;
  164. }
  165. @Override
  166. public boolean isClosing() {
  167. return false;
  168. }
  169. @Override
  170. public void setVSyncEnabled(boolean arg0) {
  171. }
  172. @Override
  173. public void setTitle(String arg0) {
  174. }
  175. @Override
  176. public void setIcon(Image[] arg0) {
  177. }
  178. @Override
  179. public void moveWindowTo(int arg0, int arg1) {
  180. }
  181. @Override
  182. public void init() {
  183. }
  184. @Override
  185. public void draw(CountDownLatch arg0) {
  186. }
  187. @Override
  188. public CanvasRenderer getCanvasRenderer() {
  189. return canvasRenderer;
  190. }
  191. };
  192. add(theCanvas, BorderLayout.CENTER);
  193. theCanvas.setVisible(true);
  194. final AwtKeyboardWrapper keyboardWrapper = new AwtKeyboardWrapper(theCanvas);
  195. final AwtFocusWrapper focusWrapper = new AwtFocusWrapper(theCanvas);
  196. final AwtMouseManager mouseManager = new AwtMouseManager(theCanvas);
  197. final AwtMouseWrapper mouseWrapper = new AwtMouseWrapper(theCanvas, mouseManager);
  198. final ControllerWrapper controllerWrapper = new DummyControllerWrapper();
  199. final PhysicalLayer pl = new PhysicalLayer(keyboardWrapper, mouseWrapper, controllerWrapper, focusWrapper);
  200. logicalLayer.registerInput(theCanvas, pl);
  201. frameWork.addCanvas(theCanvas);
  202. }
  203. protected void updateSize() {
  204. Dimension d = ArdorPanel.this.getSize();
  205. try {
  206. theCanvas.setSize(d);
  207. theCanvas.getCanvasRenderer().getCamera().resize(d.width, d.height);
  208. }
  209. catch (NullPointerException e) { }
  210. }
  211. private static class MyExit implements Exit {
  212. private volatile boolean exit = false;
  213. public void exit() {
  214. exit = true;
  215. }
  216. public boolean isExit() {
  217. return exit;
  218. }
  219. }
  220. }