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 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 org.openzen.drawablegui.live.LiveBool;
  8. import org.openzen.drawablegui.live.LiveObject;
  9. import org.openzen.drawablegui.live.LiveString;
  10. import org.openzen.drawablegui.live.MutableLiveObject;
  11. import org.openzen.drawablegui.style.DShadow;
  12. import org.openzen.drawablegui.style.DStyleClass;
  13. import org.openzen.drawablegui.style.DStylePath;
  14. /**
  15. *
  16. * @author Hoofdgebruiker
  17. */
  18. public class DButton implements DComponent {
  19. private final DStyleClass styleClass;
  20. private final LiveString label;
  21. private final MutableLiveObject<DSizing> sizing = DSizing.create();
  22. private final LiveBool disabled;
  23. private final Runnable action;
  24. private DUIContext context;
  25. private DIRectangle bounds;
  26. private DButtonStyle style;
  27. private DFontMetrics fontMetrics;
  28. private boolean hovering = false;
  29. private boolean pressing = false;
  30. public DButton(DStyleClass styleClass, LiveString label, LiveBool disabled, Runnable action) {
  31. this.styleClass = styleClass;
  32. this.label = label;
  33. this.disabled = disabled;
  34. this.action = action;
  35. }
  36. @Override
  37. public void setContext(DStylePath parent, DUIContext context) {
  38. this.context = context;
  39. DStylePath path = parent.getChild("Button", styleClass);
  40. this.style = new DButtonStyle(context.getStylesheets().get(context, path));
  41. fontMetrics = context.getFontMetrics(style.font);
  42. sizing.setValue(new DSizing(
  43. style.paddingLeft + style.paddingRight + fontMetrics.getWidth(label.getValue()),
  44. style.paddingTop + style.paddingBottom + fontMetrics.getAscent() + fontMetrics.getDescent()));
  45. }
  46. @Override
  47. public LiveObject<DSizing> getSizing() {
  48. return sizing;
  49. }
  50. @Override
  51. public DIRectangle getBounds() {
  52. return bounds;
  53. }
  54. @Override
  55. public int getBaselineY() {
  56. return style.paddingTop + fontMetrics.getAscent();
  57. }
  58. @Override
  59. public void setBounds(DIRectangle bounds) {
  60. this.bounds = bounds;
  61. }
  62. @Override
  63. public void paint(DCanvas canvas) {
  64. int backgroundColor = style.backgroundColorNormal;
  65. DShadow shadow = style.shadowNormal;
  66. if (hovering) {
  67. backgroundColor = style.backgroundColorHover;
  68. shadow = style.shadowNormal;
  69. }
  70. if (pressing) {
  71. backgroundColor = style.backgroundColorPress;
  72. shadow = style.shadowPress;
  73. }
  74. if (disabled.getValue()) {
  75. backgroundColor = style.backgroundColorDisabled;
  76. shadow = style.shadowDisabled;
  77. }
  78. DPath shape = DPath.roundedRectangle(bounds.x, bounds.y, bounds.width, bounds.height, 2 * context.getScale());
  79. canvas.shadowPath(shape, DTransform2D.IDENTITY, backgroundColor, shadow);
  80. canvas.drawText(style.font, style.textColor, bounds.x + style.paddingLeft, bounds.y + style.paddingTop + fontMetrics.getAscent(), label.getValue());
  81. }
  82. @Override
  83. public void close() {
  84. }
  85. @Override
  86. public void onMouseEnter(DMouseEvent e) {
  87. hovering = true;
  88. repaint();
  89. }
  90. @Override
  91. public void onMouseExit(DMouseEvent e) {
  92. hovering = false;
  93. pressing = false;
  94. repaint();
  95. }
  96. @Override
  97. public void onMouseDown(DMouseEvent e) {
  98. pressing = true;
  99. repaint();
  100. }
  101. @Override
  102. public void onMouseRelease(DMouseEvent e) {
  103. pressing = false;
  104. if (!disabled.getValue()) {
  105. action.run();
  106. }
  107. repaint();
  108. }
  109. private void repaint() {
  110. if (context == null || bounds == null)
  111. return;
  112. context.repaint(bounds);
  113. }
  114. }