Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. package utilities.input;
  2.  
  3. import java.awt.event.MouseAdapter;
  4. import java.awt.event.MouseEvent;
  5.  
  6. public class MouseInput extends MouseAdapter {
  7.  
  8. private static final int NUM_BUTTONS = 10;
  9.  
  10. private static final boolean[] BUTTONS = new boolean[NUM_BUTTONS];
  11. private static final boolean[] LAST_BUTTONS = new boolean[NUM_BUTTONS];
  12.  
  13. private static int x = -1, y = -1;
  14. private static int lastX = x, lastY = y;
  15. private static boolean moving = false;
  16.  
  17. @Override
  18. public void mousePressed(MouseEvent me) {
  19. BUTTONS[me.getButton()] = true;
  20. }
  21.  
  22. @Override
  23. public void mouseReleased(MouseEvent me) {
  24. BUTTONS[me.getButton()] = false;
  25. }
  26.  
  27. @Override
  28. public void mouseMoved(MouseEvent me) {
  29. x = me.getX();
  30. y = me.getY();
  31.  
  32. moving = true;
  33. }
  34.  
  35. public static void update() {
  36. for (int i = 0; i < NUM_BUTTONS; i++) {
  37. LAST_BUTTONS[i] = BUTTONS[i];
  38. }
  39. if (x == lastX && y == lastY)
  40. moving = false;
  41. lastX = x;
  42. lastY = y;
  43.  
  44. }
  45.  
  46. public static boolean isButtonPressed(int button) {
  47. return BUTTONS[button];
  48. }
  49.  
  50. public static boolean wasButtonPressed(int button) {
  51. return isButtonPressed(button) && !LAST_BUTTONS[button];
  52. }
  53.  
  54. public static boolean wasButtonReleased(int button) {
  55. return !isButtonPressed(button) && LAST_BUTTONS[button];
  56. }
  57.  
  58. public static int getX() {
  59. return x;
  60. }
  61.  
  62. public static int getY() {
  63. return y;
  64. }
  65.  
  66. public static boolean isMoving() {
  67. return moving;
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement