ZenScript main repository
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BaseComponentGroup.java 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package org.openzen.drawablegui;
  7. import java.util.function.Consumer;
  8. import java.util.function.Predicate;
  9. /**
  10. *
  11. * @author Hoofdgebruiker
  12. */
  13. public abstract class BaseComponentGroup implements DComponent {
  14. private DComponent hovering = null;
  15. protected abstract void forEachChild(Consumer<DComponent> children);
  16. protected abstract DComponent findChild(Predicate<DComponent> predicate);
  17. @Override
  18. public void onMouseEnter(DMouseEvent e) {
  19. DComponent target = getComponent(e.x, e.y);
  20. setHovering(target, e);
  21. }
  22. @Override
  23. public void onMouseExit(DMouseEvent e) {
  24. setHovering(null, e);
  25. }
  26. @Override
  27. public void onMouseMove(DMouseEvent e) {
  28. DComponent target = getComponent(e.x, e.y);
  29. if (target == hovering) {
  30. if (target != null)
  31. target.onMouseMove(e);
  32. } else {
  33. setHovering(target, e);
  34. }
  35. }
  36. @Override
  37. public void onMouseDrag(DMouseEvent e) {
  38. if (hovering != null)
  39. hovering.onMouseDrag(e);
  40. }
  41. @Override
  42. public void onMouseClick(DMouseEvent e) {
  43. if (hovering != null)
  44. hovering.onMouseClick(e);
  45. }
  46. @Override
  47. public void onMouseDown(DMouseEvent e) {
  48. if (hovering != null)
  49. hovering.onMouseDown(e);
  50. }
  51. @Override
  52. public void onMouseRelease(DMouseEvent e) {
  53. if (hovering != null)
  54. hovering.onMouseRelease(e);
  55. onMouseMove(e);
  56. }
  57. @Override
  58. public void onMouseScroll(DMouseEvent e) {
  59. if (hovering != null)
  60. hovering.onMouseScroll(e);
  61. }
  62. private void setHovering(DComponent component, DMouseEvent e) {
  63. if (component == hovering)
  64. return;
  65. if (hovering != null)
  66. hovering.onMouseExit(e);
  67. hovering = component;
  68. if (hovering != null)
  69. hovering.onMouseEnter(e);
  70. }
  71. private DComponent getComponent(int x, int y) {
  72. return findChild(child -> child.getBounds().contains(x, y));
  73. }
  74. }