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

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