MouseMapper & enableGLCanvasRenderer();

Started by draga, December 10, 2009, 09:17:11 PM

Previous topic - Next topic

draga

Hi,

I'm sitting infront of a problem:
If I want to draw the render in a JPanel and also want A MouseMapper it says:
"Display must be created."
The message comes always if I call enableGLCanvasRenderer() for a Canvas...
What do I make false?


EgonOlsen

Which MouseMapper are you using? When using the AWTGLRenderer, the mouse can be queried by using the standard methods from awt like MouseListener and MouseMotionListener. There's actually no real need to use a mapper unless you plan to support both type of renderers. However, i've coded one or two versions of a MouseMapper that support both ways, which is why i'm asking which one you are using right now.

draga

I used the MouseMapper of the wiki (advances example) page....
ok I try to get it running with the awt methodes.

EgonOlsen

I see. That's a stripped version of a more complex mouse mapper that i was using a project once. That one (the unstripped one) contains an awt related part too. I'll post that one later...

EgonOlsen

Here the "complete" version. I think it has a reference to a configuration class that you have to get rid of and it does some project specific stuff. However, it shows how to replace the mouse cursor in both modes and how to center the mouse when using AWT.


package naroth.util;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;

import org.lwjgl.input.*;
import com.threed.jpct.*;
import naroth.config.*;
import java.nio.*;

public class MouseMapper extends MouseMotionAdapter implements MouseListener {

   public final static int BUTTON1=0;
   public final static int BUTTON2=1;

   private boolean lwjgl;
   private Frame frame=null;
   private java.awt.Cursor oldCursor=null;
   private org.lwjgl.input.Cursor oldCursorGL=null;
   private Robot robot=null;
   private int mouseX=0;
   private int mouseY=0;
   private boolean button1=false;
   private boolean button2=false;
   private boolean button3=false;
   private boolean hidden=false;


   public MouseMapper() {
      init(null);
   }

   public MouseMapper(Frame comp) {
      try {
         robot=new Robot();
      } catch (Exception e) {
          Logger.log("Error initializing awt.Robot: "+e.getMessage(), Logger.ERROR);
      }
      init(comp);
   }

   public void mouseMoved(MouseEvent e) {
     mouseX=e.getX()-frame.getInsets().left;
     mouseY=e.getY()-frame.getInsets().top;
   }

   public void mouseDragged(MouseEvent e) {
      mouseX=e.getX()-frame.getInsets().left;
      mouseY=e.getY()-frame.getInsets().top;
   }

   public void mouseClicked(MouseEvent e) {}

   public void mousePressed(MouseEvent e) {
      if (e.getButton()==MouseEvent.BUTTON1) {
         button1=true;
      }
      if (e.getButton()==MouseEvent.BUTTON2) {
         button2=true;
      }
      if (e.getButton()==MouseEvent.BUTTON3) {
         button3=true;
      }
   }

   public void mouseReleased(MouseEvent e) {
     if (e.getButton()==MouseEvent.BUTTON1) {
        button1=false;
     }
     if (e.getButton()==MouseEvent.BUTTON2) {
        button2=false;
     }
     if (e.getButton()==MouseEvent.BUTTON3) {
       button3=false;
    }

  }

  public void mouseEntered(MouseEvent e) {}

  public void mouseExited(MouseEvent e) {}

   public void center() {
      if (frame!=null) {
         int x=frame.getX();
         int y=frame.getY();
         int width=frame.getWidth();
         int height=frame.getHeight();
         width=width>>1;
         height=height>>1;
         x+=width;
         y+=height;
         robot.mouseMove(x,y);
      }
   }

   public void hide() {
     if (frame!=null) {
        oldCursor=frame.getCursor();
        int[] pixels=new int[16*16];
        Image image=Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(16, 16, pixels, 0, 16));
        java.awt.Cursor transparentCursor=Toolkit.getDefaultToolkit().createCustomCursor(image, new Point(0, 0),"hidden");
        frame.setCursor(transparentCursor);
      } else {
         Mouse.setGrabbed(true);
      }
      hidden=true;
   }

   public void show() {
      if (frame!=null) {
         if (oldCursor!=null) {
            frame.setCursor(oldCursor);
            oldCursor=null;
         }
      } else {
         Mouse.setGrabbed(false);
      }
      hidden=false;
   }

   public boolean isVisible() {
      return !hidden;
   }

   public void destroy() {
      if (frame!=null) {
         frame.setCursor(oldCursor);
         frame.removeMouseMotionListener(this);
         frame.removeMouseListener(this);
      } else {
         if (Mouse.isCreated()) {
            try {
              Mouse.setNativeCursor(oldCursorGL);
           } catch (Exception e) {
              Logger.log("Error restoring cursor!", Logger.WARNING);
           }
           Mouse.destroy();
         }
      }
      show();
   }

   public boolean buttonDown(int button) {
      if (!lwjgl) {
         if (button==BUTTON1) return button1;
         if (button==BUTTON2) return button2|button3;
      }
      return Mouse.isButtonDown(button);
   }

   public int getMouseX() {
      if (!lwjgl) {
         return mouseX;
      } else {
         return Mouse.getX();
      }
   }

   public int getMouseY() {
      if (!lwjgl) {
         return mouseY;
      } else {
         return Configuration.height-Mouse.getY();
      }
   }

   public int getDeltaX() {
      if (!lwjgl) {
         int mouseXCenter=(frame.getWidth()>>1)-frame.getInsets().left; // Korrektur, da mouseMoved und mouseDragged dies auch abziehen
         return mouseX-mouseXCenter;
      } else {
         if (Mouse.isGrabbed()) {
            return Mouse.getDX();
         } else {
            return 0;
         }
      }
   }

   public int getDeltaY() {
      if (!lwjgl) {
         int mouseYCenter=(frame.getHeight()>>1)-frame.getInsets().top; // Korrektur, da mouseMoved und mouseDragged dies auch abziehen
         return mouseY-mouseYCenter;
      } else {
         if (Mouse.isGrabbed()) {
            return Mouse.getDY();
         } else {
            return 0;
         }
      }
   }

   private Image loadCursor() {
      // Yikes!! There has to be a cleaner solution than this...but no time now to investigate....:-)
      int bufferSize=100000;
      int anzAkt=0;
      int anz=0;
      int aktsize=bufferSize;
      byte[] puffer=new byte[bufferSize];
      SimpleStream ss=new SimpleStream("textures/crosshair.gif");
      InputStream is=ss.getStream();
      Image image=null;
      try {
         while ((anzAkt!=-1)) {
            anzAkt=is.read(puffer, anz, aktsize-anz);
            anz+=anzAkt;
            if ((anzAkt!=-1)&&(anz>=aktsize)) {
               aktsize+=100000;
               byte npuffer[]=new byte[aktsize];
               System.arraycopy(puffer, 0, npuffer, 0, aktsize-100000);
               puffer=npuffer;
               Logger.log("Expanding buffers..."+aktsize+" bytes", Logger.MESSAGE);
            }
         }
         anz++;
         ss.close();
         image=Toolkit.getDefaultToolkit().createImage(puffer, 0, anz);
      } catch (Exception e) {
         Logger.log("Couldn't get data from InputStream: "+e.toString(), Logger.ERROR);
      }

      MediaTracker myTracker=new MediaTracker(new Canvas());
      myTracker.addImage(image, 0);
      try {
         myTracker.waitForAll();
      } catch (Exception e) {
         Logger.log("Error loading cursor: "+e, Logger.ERROR);
      }
      return image;
   }

   private IntBuffer createBuffer(Image img) {
      int len=img.getHeight(null)*img.getWidth(null);
      ByteBuffer temp=ByteBuffer.allocateDirect(len<<2);;
      temp.order(ByteOrder.LITTLE_ENDIAN);

      int[] pixels=new int[len];

      PixelGrabber pg=new PixelGrabber(img, 0, 0, img.getWidth(null), img.getHeight(null), pixels, 0, img.getWidth(null));

      try {
         pg.grabPixels();
      } catch (InterruptedException e) {
         Logger.log("Could not grab pixels from image!", Logger.ERROR);
      }

      for (int i=0; i<len; i++) {
         int pos=i<<2;
         int texel=pixels[i];
         if (texel!=0) {
            texel|=0xff000000;
         }
         temp.putInt(pos, texel);
      }

      return temp.asIntBuffer();
   }

   private void init(Frame comp) {
      Image cursor=loadCursor();
      if (comp!=null) {
         this.frame=comp;
         comp.addMouseMotionListener(this);
         comp.addMouseListener(this);
         lwjgl=false;
         center();
         java.awt.Cursor cur=Toolkit.getDefaultToolkit().createCustomCursor(cursor, new Point(15, 15),"crosshair");
         frame.setCursor(cur);
      } else {
         lwjgl=true;
         try {
            if (!Mouse.isCreated()) {
               Mouse.create();
            }
            try {
               oldCursorGL=Mouse.getNativeCursor();
               int maxSize=org.lwjgl.input.Cursor.getMaxCursorSize();
               if (maxSize<=cursor.getWidth(null)) {
                  org.lwjgl.input.Cursor cur=new org.lwjgl.input.Cursor(32, 32, 15, 15, 1, createBuffer(cursor), null);
                  Mouse.setNativeCursor(cur);
               }
            } catch (Exception e) {
               Logger.log("Error setting cursor!", Logger.WARNING);
            }
         } catch (Exception e) {
            //e.printStackTrace();
            Logger.log("Couldn't create LWJGL-mouse - initialize the OpenGL renderer first!", Logger.WARNING);
         }
      }
   }
}