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.

DSimpleTooltipComponent.java 3.4KB

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