UIEventAdapter Usage

I notice that the UIEVentAdapter receives all Mouse and Keyboard events. It then dispatches the event to the proper overlay (i.e. the one with focus) However, the main canvas is not in the overlay list. How do we set up the mouse and keyboard listeners for the main Canvas3D so that it is also considered an overlay.

IN other words, right now, when I input text into a textfield overlay my keyboard inputs are also effecting the main cavas3D.

Am I supposed to add the Cavnas3D as an overlay?? Any example code would be greatly appreciated. Thank you for the help. ~shochu

There is no solution for this currently. Let me propose the following:

Allow for default listeners to be registered with the UIWindowManager. We can call this listener for mouse events which happen outside an overlay window and for keyboard events when there is not an overlay window in focus.

Question is: Would you rather receive all the events with a flag set if they were also sent to a window? This way you could still have function keys, etc which couldbe active even if you had a chat box highlighted. If we didn’t do that then the user would have to click on a non-window in order to get keyboard events to the new listeners?

Or do you want the new listeners to have first crack at the event with the option to consume it? Maybe we combine them and pass in the window which we are planning to send the event to (if there is one) and if the event comes back consumed then we don’t send it to the window?

Both ideas sound good to me. Both of them sound easy enough to use. The flag seems very easy to implement. Having xith pass the event first to to the overlay with focus and then if the event is not consumed passing the event to the main window is probably the best. I’m not sure if this is technically possible though. Until a fix is made I’ll just hack the source and add a flag myself. Thanks for the info David!

~shochu

I just added a flag to UIWindow which tells me if the window is in focus. For now I just check that before doing anything in my main window with key inputs.

However, I’m having another problem. I think this one is similar to Jyang81’s questions about scrollPanes. I have a scrollpane and it works only if you are scolling up and down in the textarea with your keyboard arrow keys. If you try to click on the scollpane buttons (the up and down arrow) using your mouse nothing happens. Also, if you try to scoll the scoll pane bar nothing happens.

Any idea how to fix this or is this a problem with the UI system in xith??

On the scroll pane issue: The problem there is the method that Swing uses to scroll is bizzare. I have traced the code back and found it uses a timer to scroll and that it absolutely requires that the component be active and attached to a “real window” which of course UIWindow is not. So the solution is to setup a listener in your code on the scrollbar and force a redraw of the pane when the scrollbar changes. Thats what I do in my code and it works great.

Are you talking about adding this listener?


            scrollPaneScreen.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener(){
                  public void adjustmentValueChanged(AdjustmentEvent e){
                        scrollPaneScreen.repaint();
                  }
            }
            );

I added that and you can scroll with the keyboard but not by using the VerticalScrollBar. I tried adding listeners for mouse events and repaint on those but that didn’t do anything. Any ideas?

Also, I don’t think the layoutManagers are working. At least not in the way you can use them in normal SWing.

Hmmm, can you be more specific? UIWindow.pack() does a layout on all components. I have some screens with many layers of nested panes and controls using flow layout and border layout without issues.

Here’s my test program. I want to have three panels. One panel is the main panel and it has two subpanels. The chat panel and the button Panel. I use BorderLayout. At first I didn’t set any sizes because I thought the componenets would all expand to the right size, but that didn’t work. I explicitly set the sizes of everything below and that still doesn’t work.


import java.awt.*; 
import java.awt.event.*; 
import com.xith3d.scenegraph.*; 
import com.xith3d.render.*; 
import com.xith3d.render.jogl.*; 
import javax.vecmath.*; 
import javax.swing.*; 
import com.xith3d.userinterface.*;

 
public class TryUI{ 
 UIWindowManager windowMgr; 
 View view; 
 String inputText = ""; 
 
 public void start(){
       while(true){
             view.renderOnce();
       }
 }
 
 
 public void init() throws Exception 
  { 
   Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
 
   VirtualUniverse universe = new VirtualUniverse(); 
   view = new View(); 
   view.setBackClipDistance(100f); 
   view.setProjectionPolicy(View.PERSPECTIVE_PROJECTION); 
   view.getTransform().lookAt(new Vector3f(0, 0, 3), new Vector3f(0, 0, 0), new Vector3f(0, 1, 3)); 
   Locale locale = new Locale(); 
   universe.addLocale(locale); 
   universe.addView(view); 
   RenderPeer renderPeer = new RenderPeerImpl(); 
   CanvasPeer canvasPeer = renderPeer.makeCanvas(null, 
                                                                            screenSize.width - 200,
                                                                            screenSize.height - 200,
                                                                            16, false); 
   canvasPeer.getWindow().setLocation(100, 100); 
   Canvas3D canvas = new Canvas3D(); 
   canvas.set3DPeer(canvasPeer); 
   view.addCanvas3D(canvas); 
 
   // construct a window manager for this canvas 
   windowMgr = new UIWindowManager(canvas); 
   TestWindow w = new TestWindow(800,200); 
   windowMgr.addOverlay(w); 
   windowMgr.setPosition(w,25,600); 
   windowMgr.setVisible(w,true); 
    
   UIEventAdapter eventAdapter = new UIEventAdapter(windowMgr); 
   canvas.get3DPeer().getComponent().addKeyListener(eventAdapter); 
   canvas.get3DPeer().getComponent().addMouseListener(eventAdapter); 
   canvas.get3DPeer().getComponent().addMouseMotionListener(eventAdapter); 
   canvas.get3DPeer().getComponent().setFocusable(true); 
 
   start(); 
 } 
  
 class TestWindow extends UIWindow{ 
 
  public TestWindow(int width, int height) { 
     super(width, height, false, false ); 
     setRoot(buildGUI(width,height)); 
     this.pack();
     
  } 
 
  private JComponent buildGUI (int width, int height) { 
   JPanel mainPanel = new JPanel();
   JPanel chatPanel = new JPanel();
   JPanel buttonPanel = new JPanel();
   
   
   
   
   mainPanel.setDoubleBuffered(true); 
   mainPanel.setLocation(0,0); 
   mainPanel.setBackground(new Color(1.0f,1.0f,1.0f,0.5f)); //the 4th parameter changes the transparency setting 
       
   //TextArea  
   final JTextArea textAreaScreen = new JTextArea(); 
   textAreaScreen.setEditable(false); 
   textAreaScreen.setBackground(new Color(1f,1f,1f,.5f)); 
   textAreaScreen.setLineWrap(true); 
   
   //ScrollPane
   final JScrollPane scrollPaneScreen = new JScrollPane(textAreaScreen); 
   scrollPaneScreen.setBackground(new Color(1f,1f,1f, 0.5f));
   scrollPaneScreen.setSize(width, 100);
     
   scrollPaneScreen.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() { 
        public void adjustmentValueChanged(AdjustmentEvent e) { 
              scrollPaneScreen.repaint(); 
        } 
   }); 

   //TextField 
   final JTextField textFieldInput = new JTextField(); 
   textFieldInput.setSize(width, 100);
   textFieldInput.setBackground(new Color(1f,1f,1f,0.5f)); //the 4th parameter changes the transparency settings 
   textFieldInput.addKeyListener(new KeyListener(){ 
       public void keyPressed(KeyEvent e) {} 
         
       public void keyReleased(KeyEvent e) {} 
 
       public void keyTyped(KeyEvent e) { 
            if(e.getKeyChar() == 10){ 
                  textAreaScreen.append("\n" + textFieldInput.getText());
                  textFieldInput.setText("");
            }   
       } 
    });
   
   //Button 
   JButton logoutButton = new JButton(); 
   logoutButton.setText("Logout"); 
   logoutButton.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent e){ 
        System.exit(0); 
      } 
   });  
   //Set Panel and Component SIze
   mainPanel.setSize(new Dimension(width,height));
   chatPanel.setSize(new Dimension(width, height/2));
   buttonPanel.setSize(new Dimension(width, height/2));
   scrollPaneScreen.setSize(new Dimension(width, height/4));
   textAreaScreen.setSize(new Dimension(width, height/4));
   textFieldInput.setSize(new Dimension(width, height/4));
   logoutButton.setSize(new Dimension(100,25));
   
   chatPanel.add(scrollPaneScreen, BorderLayout.CENTER);
   chatPanel.add(textFieldInput, BorderLayout.SOUTH);
   buttonPanel.add(logoutButton, BorderLayout.SOUTH);
   mainPanel.add(chatPanel, BorderLayout.CENTER);
   mainPanel.add(buttonPanel, BorderLayout.SOUTH);
 
   return mainPanel; 
    
  } 
   
 
 } 
  
 public static void main(String[] args) 
  { 
   try 
   { 
      TryUI test = new TryUI(); 
      test.init(); 
   } 
   catch (Exception e) 
   { 
      e.printStackTrace(); 
   } 
 } 
} 

WHat could be causing the problem?

When using layout managers you have to setPreferredSize() instead of setSize(). You can only use setSize() and setLocation() if you use the null layout manager.

Hi David …
Do you have any examples of a complex GUI which uses things like text fields and text areas? I’m stil having problems and I think if I had a more complex example to look at I’d be able to figure it out. The UI example’s already available are all too simple to help me. Thanks you. ~shochu

I doubt anyone has the time to make one, but I would really love to see a simple demo with a few odd shaped transparent GUI components in action (the screenshots of Magicosm are awesome).

The GUI library of Xith3D is very powerful as the Magicosm screen shots show and it’s a shame there’s no public example which shows off the best of it’s capabilities.

Will.

Yes I agree hehe. It’s frustrating to know that it can be done but I can’t figure out how :-/

The magicosm UI makes me so jealous
hahah

looks like we got our wish ;D ;D

Will.