* Note: this disables focus traversal keys for the canvas it creates.
*/
void attachViewCanvas(JPanel panel) {
rb = ClientContextJME.getWorldManager().getRenderManager().createRenderBuffer(RenderBuffer.Target.ONSCREEN, width, height);
ClientContextJME.getWorldManager().getRenderManager().addRenderBuffer(rb);
final Canvas canvas = ((OnscreenRenderBuffer) rb).getCanvas();
canvas.setVisible(true);
canvas.setBounds(0, 0, width, height);
// Fix bug 884
canvas.setFocusTraversalKeysEnabled(false);
panel.addComponentListener(new ComponentAdapter() {
public void componentResized(final ComponentEvent e) {
// workaround for race condition between EDT and MTGame renderer thread
// when using JOGL 2.0. Need to stop MTGame renderer to update canvas
ClientContextJME.getWorldManager().getRenderManager().setRunning(false);
final int width = e.getComponent().getWidth();
final int height = e.getComponent().getHeight();
float aspectRatio = (float) width / (float) height;
logger.fine("Resizing " + e);
// ty to acquire synchronization semaphore
try{
ClientContextJME.getWorldManager().getRenderManager().getSynchronizer().acquire();
} catch(InterruptedException ex){
logger.severe("Interrupted while trying to acquire semaphore");
return;
}
getCanvas().setBounds(0, 0, width, height);
ClientContextJME.getWorldManager().getRenderManager().getSynchronizer().release();
cameraComponent.setViewport(width, height);
cameraComponent.setAspectRatio(aspectRatio);
viewProperties.setFieldOfView(viewProperties.getFieldOfView());
// start MTGame renderer again
ClientContextJME.getWorldManager().getRenderManager().setRunning(true);
}
});
// Listen for (de)iconification of root window and start/stop the renderer accordingly
Window w = SwingUtilities.getWindowAncestor(panel);
if (w != null) {
w.addWindowListener(new WindowAdapter() {
@Override
public void windowDeiconified(WindowEvent e) {
// OWL issue #22 -- restore the default frame rate
int desiredFrameRate = JmeClientMain.getDesiredFrameRate();
ClientContextJME.getWorldManager().getRenderManager().setDesiredFrameRate(desiredFrameRate);
}
@Override
public void windowIconified(WindowEvent e) {
// OWL issue #22 -- instead of stopping the renderer, set
// the framerate down to 1 fps. This will still allow the
// system to make progress on tasks that require the
// renderer to update, but should cut CPU usage way down.
ClientContextJME.getWorldManager().getRenderManager().setDesiredFrameRate(1);
}
});
}
final Semaphore waitForReady = new Semaphore(0);
// Wait for the renderer to become ready
rb.setBufferUpdater(new BufferUpdater() {
public void init(RenderBuffer arg0) {
logger.info("RENDERER IS READY !");
waitForReady.release();
// OWL issue #14: ignore repaints after the first to avoid
// flickering on Windows. The first paint is necessary to
// setup the canvas. Once we get to this point, the canvas
// is initialized, and we can ignore further repaints.
canvas.setIgnoreRepaint(true);
}
});
// issue 999: don't add the canvas until after the BufferUpdater is
// registered, to make sure we don't miss the initialization call. Also
// force a repaint to be sure the initialization call happens eventually,
// even on headless clients
panel.add(canvas, BorderLayout.CENTER);
canvas.repaint();
try {
waitForReady.acquire();
} catch (InterruptedException ex) {
Logger.getLogger(ViewManager.class.getName()).log(Level.SEVERE, null, ex);