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.

DButton.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 live.LiveBool;
  8. import live.LiveObject;
  9. import live.LiveString;
  10. import live.MutableLiveObject;
  11. import org.openzen.drawablegui.draw.DDrawnShape;
  12. import org.openzen.drawablegui.draw.DDrawnText;
  13. import org.openzen.drawablegui.style.DShadow;
  14. import org.openzen.drawablegui.style.DStyleClass;
  15. /**
  16. *
  17. * @author Hoofdgebruiker
  18. */
  19. public class DButton implements DComponent {
  20. private final DStyleClass styleClass;
  21. private final LiveString label;
  22. private final MutableLiveObject<DSizing> sizing = DSizing.create();
  23. private final LiveBool disabled;
  24. private final Runnable action;
  25. private DComponentContext context;
  26. private DIRectangle bounds;
  27. private DButtonStyle style;
  28. private DFontMetrics fontMetrics;
  29. private boolean hovering = false;
  30. private boolean pressing = false;
  31. private DDrawnShape shape;
  32. private DDrawnText text;
  33. private DShadow currentShadow;
  34. public DButton(DStyleClass styleClass, LiveString label, LiveBool disabled, Runnable action) {
  35. this.styleClass = styleClass;
  36. this.label = label;
  37. this.disabled = disabled;
  38. this.action = action;
  39. }
  40. @Override
  41. public void mount(DComponentContext parent) {
  42. context = parent.getChildContext("button", styleClass);
  43. style = context.getStyle(DButtonStyle::new);
  44. fontMetrics = context.getFontMetrics(style.font);
  45. sizing.setValue(new DSizing(
  46. style.paddingLeft + style.paddingRight + fontMetrics.getWidth(label.getValue()),
  47. style.paddingTop + style.paddingBottom + fontMetrics.getAscent() + fontMetrics.getDescent()));
  48. currentShadow = getShadow();
  49. }
  50. @Override
  51. public void unmount() {
  52. context = null;
  53. if (shape != null)
  54. shape.close();
  55. if (text != null)
  56. text.close();
  57. }
  58. @Override
  59. public LiveObject<DSizing> getSizing() {
  60. return sizing;
  61. }
  62. @Override
  63. public DIRectangle getBounds() {
  64. return bounds;
  65. }
  66. @Override
  67. public int getBaselineY() {
  68. return style.paddingTop + fontMetrics.getAscent();
  69. }
  70. @Override
  71. public void setBounds(DIRectangle bounds) {
  72. this.bounds = bounds;
  73. if (shape != null)
  74. shape.close();
  75. if (text != null)
  76. text.close();
  77. shape = context.shadowPath(
  78. 0,
  79. DPath.roundedRectangle(bounds.x, bounds.y, bounds.width, bounds.height, 2 * context.getScale()),
  80. DTransform2D.IDENTITY,
  81. getBackgroundColor(),
  82. currentShadow);
  83. text = context.drawText(
  84. 1,
  85. style.font,
  86. style.textColor,
  87. bounds.x + style.paddingLeft,
  88. bounds.y + style.paddingTop + fontMetrics.getAscent(),
  89. label.getValue());
  90. }
  91. @Override
  92. public void close() {
  93. unmount();
  94. }
  95. @Override
  96. public void onMouseEnter(DMouseEvent e) {
  97. hovering = true;
  98. update();
  99. }
  100. @Override
  101. public void onMouseExit(DMouseEvent e) {
  102. hovering = false;
  103. pressing = false;
  104. update();
  105. }
  106. @Override
  107. public void onMouseDown(DMouseEvent e) {
  108. pressing = true;
  109. update();
  110. }
  111. @Override
  112. public void onMouseRelease(DMouseEvent e) {
  113. pressing = false;
  114. if (!disabled.getValue()) {
  115. action.run();
  116. }
  117. update();
  118. }
  119. private void update() {
  120. DShadow newShadow = getShadow();
  121. if (newShadow != currentShadow) {
  122. currentShadow = newShadow;
  123. if (shape != null)
  124. shape.close();
  125. shape = context.shadowPath(
  126. 0,
  127. DPath.roundedRectangle(bounds.x, bounds.y, bounds.width, bounds.height, 2 * context.getScale()),
  128. DTransform2D.IDENTITY,
  129. getBackgroundColor(),
  130. currentShadow);
  131. } else {
  132. shape.setColor(getBackgroundColor());
  133. }
  134. }
  135. private DShadow getShadow() {
  136. if (disabled.getValue())
  137. return style.shadowDisabled;
  138. if (pressing)
  139. return style.shadowPress;
  140. if (hovering)
  141. return style.shadowHover;
  142. return style.shadowNormal;
  143. }
  144. private int getBackgroundColor() {
  145. if (disabled.getValue())
  146. return style.backgroundColorDisabled;
  147. if (pressing)
  148. return style.backgroundColorPress;
  149. if (hovering)
  150. return style.backgroundColorHover;
  151. return style.backgroundColorNormal;
  152. }
  153. }