Using the mouse when using OpenGL

Started by Melssj5, September 04, 2005, 08:11:21 AM

Previous topic - Next topic

Melssj5

Well, I have a pair of questions:

First is about using the openGL rendering, it opens a new Frame, how can I change the properties of that frame created when enabling the openGL render, for example, changing the title or things like that?

Second: KeyMapper is used to manage key events, but what about the mouse events?
Nada por ahora

MFJ

Don't know about OpenGl, sorry. But for mouse events you want to register an AWT mouselistener on your drawing frame. Eg:


//Get the graphics context from the applet
Graphics g =mainPanel.getGraphics();

//Setup the mouse listenage
mainPanel.addMouseListener(this);
mainPanel.addMouseMotionListener(this);


Assuming that your program is implementing those two listeners, you can then listen to events from them, see


http://java.sun.com/j2se/1.5.0/docs/api/java/awt/event/package-summary.html


For more details.

EgonOlsen

Quote from: "Melssj5"
First is about using the openGL rendering, it opens a new Frame, how can I change the properties of that frame created when enabling the openGL render, for example, changing the title or things like that?

Second: KeyMapper is used to manage key events, but what about the mouse events?
Some properties (like the title) can be configured in Config. Have a look at glWindowName for example.
There is no MouseMapper in the package. I've written a very simple one for Paradroidz. I can post the code on monday, when i'm back home.

EgonOlsen

Here it is. It may contain some Paradroidz specific stuff, but it should help to get you started.

package naroth.util;

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

import org.lwjgl.input.*;
import com.threed.jpct.*;

import naroth.config.*;

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;
  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);
     } else {
        // Tu nix...dies ist schon automatisch durch das Mouse.setGrabbed() erledigt.
     }
  }

  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.removeMouseMotionListener(this);
        frame.removeMouseListener(this);
     } else {
        if (Mouse.isCreated()) {
           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 void init(Frame comp) {
     if (comp!=null) {
        this.frame=comp;
        comp.addMouseMotionListener(this);
        comp.addMouseListener(this);
        lwjgl=false;
        center();
     } else {
        lwjgl=true;
        try {
           if (!Mouse.isCreated()) {
              Mouse.create();
           }
        } catch (Exception e) {
           //e.printStackTrace();
           Logger.log("Couldn't create LWJGL-mouse - initialize the OpenGL renderer first!", Logger.WARNING);
        }
     }
  }
}

Melssj5

:cry:

:cry:

:cry:

I see that it seems to be a class that implements a MouseListener and have some methods to handle the events but I cant find where it is set to be used with the OpenGl frame opened, I see that in the constructor it calls to init (null) and it just set the value of a boolean and checks if Mouse is created, if not, it creates it, but I dont see the joint with OpenGl feature. Maybe in the Mouse class that is not posted, because I could find any variable called Mouse.

I tried to use standard MouseListener in the main class, but it doesnt work.

Please can you post the other class to have things a clearlier?  :P
Nada por ahora

EgonOlsen

Mouse is provided by LWJGL. It is "automagically" associated with the OpenGL frame. You simply create the OpenGL frame, then the Mouse and it should work fine.

Anonymous