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

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