001/** 002 * Copyright (c) 2008-2014 Ardor Labs, Inc. 003 * 004 * This file is part of Ardor3D. 005 * 006 * Ardor3D is free software: you can redistribute it and/or modify it 007 * under the terms of its license which may be found in the accompanying 008 * LICENSE file or at <http://www.ardor3d.com/LICENSE>. 009 */ 010 011package com.ardor3d.example.canvas; 012 013import java.awt.Dimension; 014import java.awt.GridLayout; 015import java.awt.event.WindowAdapter; 016import java.awt.event.WindowEvent; 017import java.io.IOException; 018import java.net.URISyntaxException; 019import java.util.HashMap; 020import java.util.Map; 021 022import javax.swing.JFrame; 023import javax.swing.JLabel; 024import javax.swing.SwingConstants; 025 026import com.ardor3d.example.Purpose; 027import com.ardor3d.framework.Canvas; 028import com.ardor3d.framework.DisplaySettings; 029import com.ardor3d.framework.FrameHandler; 030import com.ardor3d.framework.jogl.JoglCanvasRenderer; 031import com.ardor3d.framework.jogl.awt.JoglNewtAwtCanvas; 032import com.ardor3d.image.util.ImageLoaderUtil; 033import com.ardor3d.image.util.jogl.JoglImageLoader; 034import com.ardor3d.input.ControllerWrapper; 035import com.ardor3d.input.Key; 036import com.ardor3d.input.MouseCursor; 037import com.ardor3d.input.PhysicalLayer; 038import com.ardor3d.input.jogl.JoglNewtFocusWrapper; 039import com.ardor3d.input.jogl.JoglNewtKeyboardWrapper; 040import com.ardor3d.input.jogl.JoglNewtMouseManager; 041import com.ardor3d.input.jogl.JoglNewtMouseWrapper; 042import com.ardor3d.input.logical.DummyControllerWrapper; 043import com.ardor3d.input.logical.InputTrigger; 044import com.ardor3d.input.logical.KeyPressedCondition; 045import com.ardor3d.input.logical.LogicalLayer; 046import com.ardor3d.input.logical.TriggerAction; 047import com.ardor3d.input.logical.TwoInputStates; 048import com.ardor3d.util.Timer; 049import com.ardor3d.util.resource.ResourceLocatorTool; 050import com.ardor3d.util.resource.SimpleResourceLocator; 051import com.ardor3d.util.resource.URLResourceSource; 052 053/** 054 * This examples demonstrates how to render OpenGL (via JOGL) on a NEWT AWT canvas. FIXME update the thumbnail and the 055 * description 056 */ 057@Purpose(htmlDescriptionKey = "com.ardor3d.example.canvas.JoglAwtExample", // 058thumbnailPath = "com/ardor3d/example/media/thumbnails/canvas_JoglAwtExample.jpg", // 059maxHeapMemory = 64) 060public class JoglNewtAwtExample { 061 static MouseCursor _cursor1; 062 static MouseCursor _cursor2; 063 064 static Map<Canvas, Boolean> _showCursor1 = new HashMap<>(); 065 066 public static void main(final String[] args) throws Exception { 067 System.setProperty("ardor3d.useMultipleContexts", "true"); 068 069 final Timer timer = new Timer(); 070 final FrameHandler frameWork = new FrameHandler(timer); 071 072 final MyExit exit = new MyExit(); 073 final LogicalLayer logicalLayer = new LogicalLayer(); 074 075 final ExampleScene scene1 = new ExampleScene(); 076 final RotatingCubeGame game1 = new RotatingCubeGame(scene1, exit, logicalLayer, Key.T); 077 078 final ExampleScene scene2 = new ExampleScene(); 079 final RotatingCubeGame game2 = new RotatingCubeGame(scene2, exit, logicalLayer, Key.G); 080 081 frameWork.addUpdater(game1); 082 frameWork.addUpdater(game2); 083 084 final JFrame frame = new JFrame("NEWT AWT Example"); 085 frame.addWindowListener(new WindowAdapter() { 086 @Override 087 public void windowClosing(final WindowEvent e) { 088 exit.exit(); 089 } 090 }); 091 092 frame.setLayout(new GridLayout(2, 3)); 093 094 JoglImageLoader.registerLoader(); 095 096 try { 097 final SimpleResourceLocator srl = new SimpleResourceLocator(ResourceLocatorTool.getClassPathResource( 098 JoglNewtAwtExample.class, "com/ardor3d/example/media/")); 099 ResourceLocatorTool.addResourceLocator(ResourceLocatorTool.TYPE_TEXTURE, srl); 100 } catch (final URISyntaxException ex) { 101 ex.printStackTrace(); 102 } 103 104 _cursor1 = createMouseCursor("com/ardor3d/example/media/input/wait_cursor.png"); 105 _cursor2 = createMouseCursor("com/ardor3d/example/media/input/movedata.gif"); 106 107 addCanvas(frame, scene1, logicalLayer, frameWork); 108 frame.add(new JLabel( 109 "<html>" 110 + "<table>" 111 + "<tr><th align=\"left\" style=\"font-size: 16\">Action</th><th align=\"left\" style=\"font-size: 16\">Command</th></tr>" 112 + "<tr><td>WS</td><td>Move camera position forward/back</td></tr>" 113 + "<tr><td>AD</td><td>Turn camera left/right</td></tr>" 114 + "<tr><td>QE</td><td>Strafe camera left/right</td></tr>" 115 + "<tr><td>T</td><td>Toggle cube rotation for scene 1 on press</td></tr>" 116 + "<tr><td>G</td><td>Toggle cube rotation for scene 2 on press</td></tr>" 117 + "<tr><td>U</td><td>Toggle both cube rotations on release</td></tr>" 118 + "<tr><td>0 (zero)</td><td>Reset camera position</td></tr>" 119 + "<tr><td>9</td><td>Face camera towards cube without changing position</td></tr>" 120 + "<tr><td>ESC</td><td>Quit</td></tr>" 121 + "<tr><td>Mouse</td><td>Press left button to rotate camera.</td></tr>" + "</table>" 122 + "</html>", SwingConstants.CENTER)); 123 addCanvas(frame, scene1, logicalLayer, frameWork); 124 frame.add(new JLabel("", SwingConstants.CENTER)); 125 addCanvas(frame, scene2, logicalLayer, frameWork); 126 frame.add(new JLabel("", SwingConstants.CENTER)); 127 128 frame.pack(); 129 frame.setVisible(true); 130 131 game1.init(); 132 game2.init(); 133 134 while (!exit.isExit()) { 135 frameWork.updateFrame(); 136 Thread.yield(); 137 } 138 139 frame.dispose(); 140 System.exit(0); 141 } 142 143 private static MouseCursor createMouseCursor(final String resourceName) throws IOException { 144 final com.ardor3d.image.Image image = ImageLoaderUtil 145 .loadImage( 146 new URLResourceSource(ResourceLocatorTool.getClassPathResource(JoglNewtAwtExample.class, 147 resourceName)), false); 148 149 return new MouseCursor("cursor1", image, 0, image.getHeight() - 1); 150 } 151 152 private static void addCanvas(final JFrame frame, final ExampleScene scene, final LogicalLayer logicalLayer, 153 final FrameHandler frameWork) throws Exception { 154 final JoglCanvasRenderer canvasRenderer = new JoglCanvasRenderer(scene); 155 156 final DisplaySettings settings = new DisplaySettings(400, 300, 24, 0, 0, 16, 0, 0, false, false); 157 final JoglNewtAwtCanvas theCanvas = new JoglNewtAwtCanvas(settings, canvasRenderer); 158 159 frame.add(theCanvas); 160 161 _showCursor1.put(theCanvas, true); 162 163 theCanvas.setSize(new Dimension(400, 300)); 164 theCanvas.setVisible(true); 165 166 final JoglNewtKeyboardWrapper keyboardWrapper = new JoglNewtKeyboardWrapper(theCanvas); 167 final JoglNewtFocusWrapper focusWrapper = new JoglNewtFocusWrapper(theCanvas); 168 final JoglNewtMouseManager mouseManager = new JoglNewtMouseManager(theCanvas); 169 final JoglNewtMouseWrapper mouseWrapper = new JoglNewtMouseWrapper(theCanvas, mouseManager); 170 theCanvas.setMouseManager(mouseManager); 171 final ControllerWrapper controllerWrapper = new DummyControllerWrapper(); 172 173 final PhysicalLayer pl = new PhysicalLayer(keyboardWrapper, mouseWrapper, controllerWrapper, focusWrapper); 174 175 logicalLayer.registerInput(theCanvas, pl); 176 177 logicalLayer.registerTrigger(new InputTrigger(new KeyPressedCondition(Key.H), new TriggerAction() { 178 @Override 179 public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) { 180 if (source != theCanvas) { 181 return; 182 } 183 184 if (_showCursor1.get(theCanvas)) { 185 mouseManager.setCursor(_cursor1); 186 } else { 187 mouseManager.setCursor(_cursor2); 188 } 189 190 _showCursor1.put(theCanvas, !_showCursor1.get(theCanvas)); 191 } 192 })); 193 logicalLayer.registerTrigger(new InputTrigger(new KeyPressedCondition(Key.J), new TriggerAction() { 194 @Override 195 public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) { 196 if (source != theCanvas) { 197 return; 198 } 199 200 mouseManager.setCursor(MouseCursor.SYSTEM_DEFAULT); 201 } 202 })); 203 204 frameWork.addCanvas(theCanvas); 205 206 } 207 208 private static class MyExit implements Exit { 209 private volatile boolean exit = false; 210 211 @Override 212 public void exit() { 213 exit = true; 214 } 215 216 public boolean isExit() { 217 return exit; 218 } 219 } 220}