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

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