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.

DLabel.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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.draw.DDrawSurface;
  8. import org.openzen.drawablegui.draw.DDrawnText;
  9. import org.openzen.drawablegui.listeners.ListenerHandle;
  10. import org.openzen.drawablegui.live.LiveObject;
  11. import org.openzen.drawablegui.live.LiveString;
  12. import org.openzen.drawablegui.live.MutableLiveObject;
  13. import org.openzen.drawablegui.style.DStyleClass;
  14. import org.openzen.drawablegui.style.DStylePath;
  15. /**
  16. *
  17. * @author Hoofdgebruiker
  18. */
  19. public class DLabel implements DComponent {
  20. private final LiveString label;
  21. private final DStyleClass styleClass;
  22. private final MutableLiveObject<DSizing> sizing = DSizing.create();
  23. private final ListenerHandle<LiveString.Listener> labelListener;
  24. private DDrawSurface surface;
  25. private int z;
  26. private DIRectangle bounds;
  27. private DLabelStyle style;
  28. private DFontMetrics fontMetrics;
  29. private DDrawnText text;
  30. public DLabel(DStyleClass styleClass, LiveString label) {
  31. this.styleClass = styleClass;
  32. this.label = label;
  33. labelListener = label.addListener(this::onLabelChanged);
  34. }
  35. @Override
  36. public void mount(DStylePath parent, int z, DDrawSurface surface) {
  37. this.surface = surface;
  38. this.z = z;
  39. DStylePath path = parent.getChild("label", styleClass);
  40. style = new DLabelStyle(surface.getStylesheet(path));
  41. fontMetrics = surface.getFontMetrics(style.font);
  42. calculateDimension();
  43. if (text != null)
  44. text.close();
  45. text = surface.drawText(z, style.font, style.color, 0, 0, label.getValue());
  46. }
  47. @Override
  48. public void unmount() {
  49. if (style != null)
  50. style.border.close();
  51. if (text != null)
  52. text.close();
  53. }
  54. @Override
  55. public LiveObject<DSizing> getSizing() {
  56. return sizing;
  57. }
  58. @Override
  59. public DIRectangle getBounds() {
  60. return bounds;
  61. }
  62. @Override
  63. public int getBaselineY() {
  64. return style.border.getPaddingTop() + fontMetrics.getAscent();
  65. }
  66. @Override
  67. public void setBounds(DIRectangle bounds) {
  68. this.bounds = bounds;
  69. style.border.update(surface, z + 1, bounds);
  70. text.setPosition(
  71. bounds.x + style.border.getPaddingLeft(),
  72. bounds.y + style.border.getPaddingTop() + fontMetrics.getAscent());
  73. }
  74. @Override
  75. public void close() {
  76. labelListener.close();
  77. unmount();
  78. }
  79. private void onLabelChanged(String oldValue, String newValue) {
  80. calculateDimension();
  81. if (text != null)
  82. text.close();
  83. text = surface.drawText(
  84. z,
  85. style.font,
  86. style.color,
  87. bounds.x + style.border.getPaddingLeft(),
  88. bounds.y + style.border.getPaddingTop() + fontMetrics.getAscent(),
  89. newValue);
  90. }
  91. private void calculateDimension() {
  92. sizing.setValue(new DSizing(
  93. style.border.getPaddingLeft() + fontMetrics.getWidth(label.getValue()) + style.border.getPaddingRight(),
  94. style.border.getPaddingTop() + fontMetrics.getAscent() + fontMetrics.getDescent() + style.border.getPaddingTop()));
  95. }
  96. }