Newt event handling

classic Classic list List threaded Threaded
10 messages Options
ac
Reply | Threaded
Open this post in threaded view
|

Newt event handling

ac
Hi, I'm trying to get my head around the event handling mechanism in NEWT, but haven't been very successful so far. The general situation is as follows: I create a GLWindow, and then wrap it with a NewtCanvasAWT in order to add it to an AWT frame. My problem is how to implement working mouse and key listeners.

I went through different examples included in the tests cases that appear to be relevant to this problem. I also read the javadoc of AWTAdapter: http://jogamp.org/deployment/jogamp-next/javadoc/jogl/javadoc/com/jogamp/newt/event/awt/AWTAdapter.html

 The simplest code I could come up that condenses my current understanding is the following:

public void run() throws InterruptedException, InvocationTargetException {
  frame = new Frame("AWT Frame");
   
  GLProfile profile = GLProfile.getDefault();
  GLCapabilities capabilities = new GLCapabilities(profile);
   
  window = GLWindow.create(capabilities);    
  canvas = new NewtCanvasAWT(window);
  canvas.setBounds(0, 0, 300, 300);
  canvas.setFocusable(true);
   
  MouseListener mouseListener = new TestMouseAdapter();
  window.addMouseListener(mouseListener);    
   
  frame.setLayout(new BorderLayout());
  frame.add(canvas, BorderLayout.CENTER);
   
  TestGLListener glListener = new TestGLListener();
  window.addGLEventListener(glListener);    
  final GLAnimatorControl animator = new Animator(window);
  animator.start();
   
  frame.setLocation(0, 0);
  frame.setSize(300, 300);
  frame.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
      animator.stop();
      System.exit(0);
    }
  });
   
  EventQueue.invokeAndWait(new Runnable() {
    public void run() {
      frame.validate();                
      frame.setVisible(true);
  }});
}

The TestMouseAdapter is very simple, is only supposed to print the events:

class TestMouseAdapter extends MouseAdapter {    
  public void mousePressed(MouseEvent e) {
    System.out.println("mouse pressed event: " + e);        
  }
  public void mouseReleased(MouseEvent e) {
    System.out.println("mouse released event: " + e);
  }
  public void mouseMoved(MouseEvent e) {          
    System.out.println("mouse moved event: " + e);  
  }    
  public void mouseDragged(MouseEvent e) {
    System.out.println("mouse dragged event: " + e);  
  }    
}  

The program renders normally, but no events appear to be detected. This happens both on Windows 7 and OSX 10.8.2, Java 1.6. I will test on Linux shortly.

The full code of my test application is here:

http://pastebin.com/wJ3wHFU1

Any help will be greatly appreciated.
Reply | Threaded
Open this post in threaded view
|

Re: Newt event handling

Sven Gothel
Administrator
On 11/12/2012 03:17 AM, ac [via jogamp] wrote:
> Hi, I'm trying to get my head around the event handling mechanism in NEWT, but
> haven't been very successful so far. The general situation is as follows: I
> create a GLWindow, and then wrap it with a NewtCanvasAWT in order to add it to
> an AWT frame. My problem is how to implement working mouse and key listeners.
>
> I went through different examples included in the tests cases that appear to
> be relevant to this problem. I also read the javadoc of AWTAdapter:
> http://jogamp.org/deployment/jogamp-next/javadoc/jogl/javadoc/com/jogamp/newt/event/awt/AWTAdapter.html
>

NewtCanvasAWT already produces NEWT mouse/key events,
intercepted from the AWT component, translated to NEWT and dispatched
for the user.

Hence no further action is required, i.e. treat a NewtCanvasAWT wrapped
NEWT Windows as it were 'standing alone'.

I will look at your test code in a bit.

~Sven

> The full code of my test application is here:
>
> http://pastebin.com/wJ3wHFU1
>
> Any help will be greatly appreciated.
>
~Sven


signature.asc (907 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Newt event handling

Sven Gothel
Administrator
In reply to this post by ac
On 11/12/2012 05:40 AM, Sven Gothel wrote:

> On 11/12/2012 03:17 AM, ac [via jogamp] wrote:
>> Hi, I'm trying to get my head around the event handling mechanism in NEWT, but
>> haven't been very successful so far. The general situation is as follows: I
>> create a GLWindow, and then wrap it with a NewtCanvasAWT in order to add it to
>> an AWT frame. My problem is how to implement working mouse and key listeners.
>>
>> I went through different examples included in the tests cases that appear to
>> be relevant to this problem. I also read the javadoc of AWTAdapter:
>> http://jogamp.org/deployment/jogamp-next/javadoc/jogl/javadoc/com/jogamp/newt/event/awt/AWTAdapter.html
>>
>
> NewtCanvasAWT already produces NEWT mouse/key events,
> intercepted from the AWT component, translated to NEWT and dispatched
> for the user.
>
> Hence no further action is required, i.e. treat a NewtCanvasAWT wrapped
> NEWT Windows as it were 'standing alone'.
>
> I will look at your test code in a bit.
>
> ~Sven
>
>> The full code of my test application is here:
>>
>> http://pastebin.com/wJ3wHFU1
>>
>> Any help will be greatly appreciated.
>>

You just became a victim of your favorite IDE :)

Instead of importing NEWT's MouseEvent,
you imported AWT's MouseEvent.

Hence you didn't use the @Override decoration
of your MouseAdapter specialization, the compiler/IDE didn't warn you.

Attached is a diff which fixes the issue at hand.

> ~Sven
>


EventTest2.diff (1K) Download Attachment
signature.asc (907 bytes) Download Attachment
ac
Reply | Threaded
Open this post in threaded view
|

Re: Newt event handling

ac
it works now, thanks a lot for your help!
ac
Reply | Threaded
Open this post in threaded view
|

Re: Newt event handling

ac
quick question: what would be the equivalent of java.awt.event.MouseEvent.getModifiersEx() in com.jogamp.newt.event.MouseEvent?
Reply | Threaded
Open this post in threaded view
|

Re: Newt event handling

Sven Gothel
Administrator
On 11/12/2012 04:45 PM, ac [via jogamp] wrote:
> quick question: what would be the equivalent of
> java.awt.event.MouseEvent.getModifiersEx() in com.jogamp.newt.event.MouseEvent?
>

NEWT's InputEvent getModifiers() maybe, I don't know the *Ex() thingy.

~Sven



signature.asc (907 bytes) Download Attachment
ac
Reply | Threaded
Open this post in threaded view
|

Re: Newt event handling

ac
I'm using com.jogamp.newt.event.KeyEvent.getModifiers() for the time being, and appears to work but haven't tested extensively.

But there might be problems, from the AWT's documentation:

"When a mouse button is clicked, events are generated and sent to the registered MouseListeners. The state of modal keys can be retrieved using InputEvent.getModifiers() and InputEvent.getModifiersEx(). The button mask returned by InputEvent.getModifiers() reflects only the button that changed state, not the current state of all buttons. (Note: Due to overlap in the values of ALT_MASK/BUTTON2_MASK and META_MASK/BUTTON3_MASK, this is not always true for mouse events involving modifier keys). To get the state of all buttons and modifier keys, use InputEvent.getModifiersEx(). The button which has changed state is returned by getButton()"

(quoted from http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/event/MouseEvent.html)

Also:

http://docs.oracle.com/javase/6/docs/api/java/awt/event/InputEvent.html#getModifiersEx()
Reply | Threaded
Open this post in threaded view
|

Re: Newt event handling

Sven Gothel
Administrator
On 11/14/2012 04:10 PM, ac [via jogamp] wrote:

> I'm using com.jogamp.newt.event.KeyEvent.getModifiers() for the time being,
> and appears to work but haven't tested extensively.
>
> But there might be problems, from the AWT's documentation:
>
> "When a mouse button is clicked, events are generated and sent to the
> registered MouseListeners. The state of modal keys can be retrieved using
> InputEvent.getModifiers() and InputEvent.getModifiersEx(). The button mask
> returned by InputEvent.getModifiers() reflects only the button that changed
> state, not the current state of all buttons. (Note: Due to overlap in the
> values of ALT_MASK/BUTTON2_MASK and META_MASK/BUTTON3_MASK, this is not always
> true for mouse events involving modifier keys). To get the state of all
> buttons and modifier keys, use InputEvent.getModifiersEx(). The button which
> has changed state is returned by getButton()"
>
> (quoted from
> http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/event/MouseEvent.html)
>
> Also:
>
> http://docs.oracle.com/javase/6/docs/api/java/awt/event/InputEvent.html#getModifiersEx(
> <http://docs.oracle.com/javase/6/docs/api/java/awt/event/InputEvent.html#getModifiersEx%28>)
We have a bug entry discussing the API differences of NEWT and AWT.
  <https://jogamp.org/bugzilla/show_bug.cgi?id=629>

Still: NEWT != AWT.

~Sven



signature.asc (907 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Newt event handling

elect
Stupid question, why can't I use the AWT mouseListener?

glWindow require the Newt..

Moreover, what about if I also need a mouseWheelListener?
Reply | Threaded
Open this post in threaded view
|

Re: Newt event handling

gouessej
Administrator
We need to go beyond some limitations of AWT, that's why NEWT exists (which is more cross-platform, generally faster, it has a smaller memory footprint, it preserves the OpenGL context more easily in some cases, ...).

Please look at the API documentation for the mouse wheel:
http://jogamp.org/deployment/jogamp-next/javadoc/jogl/javadoc/com/jogamp/newt/event/MouseListener.html#mouseWheelMoved(com.jogamp.newt.event.MouseEvent)
Julien Gouesse | Personal blog | Website