Select TextBox on hover

master
John 2022-06-08 12:35:37 -04:00
parent d5bf288c26
commit 948756a1c4
2 changed files with 25 additions and 1 deletions

View File

@ -66,7 +66,15 @@ public class MenuPanel extends JPanel implements Runnable, KeyListener{
//add the MousePressed method from the MouseAdapter - by doing this we can listen for mouse input. We do this differently from the KeyListener because MouseAdapter has SEVEN mandatory methods - we only need one of them, and we don't want to make 6 empty methods
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
//player.mousePressed(e);
if (hoverCheck(e)) {
keyPressed(new KeyEvent(new Component() {
}, 0, 0, 0, KeyEvent.VK_ENTER));
}
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseMoved(MouseEvent e) {
hoverCheck(e);
}
});
this.setPreferredSize(new Dimension(GAME_WIDTH, GAME_HEIGHT));
@ -125,6 +133,17 @@ public class MenuPanel extends JPanel implements Runnable, KeyListener{
}
}
public boolean hoverCheck(MouseEvent e) {
for (TextBox t: textBoxArray) {
if (t.isHover(e.getX(), e.getY())) {
currentBox = textBoxArray.indexOf(t);
System.out.println(t.id);
return true;
}
}
return false;
}
//if a key is pressed, we'll send it over to the Player class for processing
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {

View File

@ -53,4 +53,9 @@ public class TextBox extends Rectangle {
g.setFont(font);
drawCenteredTextBox(g, text, backgroundColor, textColor);
}
@Override
public boolean equals(Object o) {
return this == o;
}
}