MouseOver UI thingy

After a couple of days of coding I must say thanks to all who have contributed to xith3d.

I spent quite a bit of time getting a pretty UI exit button for my app. The behaviour I want is a standard mouse over button that changes icon when you mouse over it. I extened UIWindow, made the root a JPanel, added a JButton to it and tried to use the standard setRolloverEnabled(true) and setRolloverIcon(icon) methods. This did not work.

After a bit of investigation it is apparant that the UIEventAdaptor was processing all events on the canvas and these were not being transferred to the various UIWindows. They were being transferred to the UIWindowManager however. Essentially what I had to do was fiddle with the UIWindowManager a bit so the MOUSE_MOVED events triggered MOUSED_ENTERED and MOUSE_EXITED events on the UIWindows. This still did not do the automatic rollover thing so I had to make my custom UIWindow a MouseListener and “manually” change the icons.

The really cool thing is that I now have pretty buttons that change when you mouse over them. Maybe there was an easier way…

There is still a bit of mystery. The mouse over looks to work about 10 pixels below the button?

I am not sure if someone wants to put this code in the repository. It is posted here just in case.

Regards,

Alistair

The changes to UIWindowManager:

near the top

//objects needed for enter/exit support
UIPositionedWindow inWindow = null;

the first bit of the processMouseEvent method with my changes in bold
public synchronized void processMouseEvent(MouseEvent me) {

        boolean rightButton = ((MouseEvent.BUTTON3_MASK & me.getModifiers()) != 0);
        boolean leftButton = ((MouseEvent.BUTTON1_MASK & me.getModifiers()) != 0);

        UIPositionedWindow w = getWindow(me.getPoint());


        boolean eventConsumed = false;

        if (me.getID() == MouseEvent.MOUSE_MOVED) {
            if (mode == OM_MOVE_WINDOW) {
                focus.setPosition(me.getX(), me.getY(), canvasDim);
                eventConsumed = true;
            }
            [b]else if(mode == OM_NONE){
              if(w == null && inWindow != null){
                //mouseExited inWindow
                UIOverlayInterface o = inWindow.getOverlay();
                if (o instanceof UIWindow) {
                    MouseEvent mec = new MouseEvent(me.getComponent(),
                            MouseEvent.MOUSE_EXITED,
                            me.getWhen(),
                            me.getModifiers(), me.getX(),
                            me.getY(),
                            me.getClickCount(), false);

                    ((UIWindow) o).dispatchEvent(mec);
                }
                inWindow = null;


              }
              else if(w != null && inWindow == null){
                //mouseEntered w

                UIOverlayInterface o = w.getOverlay();
                if (o instanceof UIWindow) {
                    MouseEvent mec = new MouseEvent(me.getComponent(),
                            MouseEvent.MOUSE_ENTERED,
                            me.getWhen(),
                            me.getModifiers(), me.getX(),
                            me.getY(),
                            me.getClickCount(), false);
                 
                    ((UIWindow) o).dispatchEvent(mec);

                }
                inWindow = w;
              }
              else if(w != null && inWindow != null && w != inWindow){
                //mouseEntered w and Exited inWindow

                UIOverlayInterface o = inWindow.getOverlay();
                if (o instanceof UIWindow) {
                    MouseEvent mec = new MouseEvent(me.getComponent(),
                            MouseEvent.MOUSE_EXITED,
                            me.getWhen(),
                            me.getModifiers(), me.getX(),
                            me.getY(),
                            me.getClickCount(), false);

                    ((UIWindow) o).dispatchEvent(mec);
                }

                o = w.getOverlay();
                if (o instanceof UIWindow) {
                    MouseEvent mec = new MouseEvent(me.getComponent(),
                            MouseEvent.MOUSE_ENTERED,
                            me.getWhen(),
                            me.getModifiers(), me.getX(),
                            me.getY(),
                            me.getClickCount(), false);

                    ((UIWindow) o).dispatchEvent(mec);
                }
                inWindow = w;
              }[/b]
              //else if (w==null) setOverlayFocus((UIPositionedWindow) null);
            }
        }

And here is my custom UIWindow that I use it with:

[b]package islandtest;

import com.xith3d.userinterface.UIWindow;

import javax.swing.;
import javax.imageio.ImageIO;
import java.awt.
;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;

public class IslandExitUIWindow extends UIWindow implements ActionListener, MouseListener {

private IslandMain main;
private Icon grey;
private Icon red;
private JButton button;

public IslandExitUIWindow(IslandMain main) {
super(50,50, true, true);

this.main = main;
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(1,1));
panel.setDoubleBuffered(true);
panel.setSize(new Dimension(40,40));
panel.setLocation(0,0);
panel.setBackground(new Color(0,0,0,0));

button = new JButton();

// button.setLayout(new BoxLayout(button,BoxLayout.X_AXIS));
button.setSize(40,40);
button.addActionListener(this);
button.setMargin(new Insets(0,0,0,0));
button.setBorder(BorderFactory.createEmptyBorder());
button.setLocation(0,0);
button.setBackground(new Color(0,0,0,0));

panel.addMouseListener(this);

try {
  grey = new ImageIcon(ImageIO.read(new File("d:/code/island/src/islandtest/data/exitgrey.png")));
  red = new ImageIcon(ImageIO.read(new File("d:/code/island/src/islandtest/data/exitred.png")));
  button.setIcon(grey);

}
catch (IOException e) {
  e.printStackTrace();
  button.setText("Exit");
}
panel.add(button);

this.setRoot(panel);

}

public void actionPerformed(ActionEvent e) {
main.doExit();

}

public void mouseClicked(MouseEvent e) {

}

public void mousePressed(MouseEvent e) {

}

public void mouseReleased(MouseEvent e) {

}

public void mouseEntered(MouseEvent e) {
button.setIcon(red);

}

public void mouseExited(MouseEvent e) {
button.setIcon(grey);
}

}[/b]