WIP desktop client for Chatto reimplemented in ScalaFX and Sapphire Framework
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.

178 lines
4.3 KiB

4 years ago
  1. package wow.doge.chatto.messagebuble;
  2. import javafx.beans.InvalidationListener;
  3. import javafx.beans.Observable;
  4. import javafx.beans.value.ChangeListener;
  5. import javafx.beans.value.ObservableValue;
  6. import javafx.geometry.Insets;
  7. import javafx.scene.Node;
  8. import javafx.scene.control.Label;
  9. import javafx.scene.effect.DropShadow;
  10. import javafx.scene.paint.Color;
  11. import javafx.scene.shape.Shape;
  12. public class BubbledLabel extends Label {
  13. /*
  14. * Copyright {2015} {Terah Laweh}
  15. *
  16. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  17. * use this file except in compliance with the License. You may obtain a copy of
  18. * the License at
  19. *
  20. * http://www.apache.org/licenses/LICENSE-2.0
  21. *
  22. * Unless required by applicable law or agreed to in writing, software
  23. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  24. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  25. * License for the specific language governing permissions and limitations under
  26. * the License.
  27. */
  28. private BubbleSpec bs = BubbleSpec.FACE_LEFT_CENTER;
  29. private double pading = 5.0;
  30. private boolean systemCall = false;
  31. public BubbledLabel() {
  32. super();
  33. init();
  34. }
  35. public BubbledLabel(String arg0, Node arg1) {
  36. super(arg0, arg1);
  37. init();
  38. }
  39. public BubbledLabel(String arg0) {
  40. super(arg0);
  41. init();
  42. }
  43. public BubbledLabel(BubbleSpec bubbleSpec) {
  44. super();
  45. this.bs = bubbleSpec;
  46. init();
  47. }
  48. public BubbledLabel(String arg0, Node arg1, BubbleSpec bubbleSpec) {
  49. super(arg0, arg1);
  50. this.bs = bubbleSpec;
  51. init();
  52. }
  53. public BubbledLabel(String arg0, BubbleSpec bubbleSpec) {
  54. super(arg0);
  55. this.bs = bubbleSpec;
  56. init();
  57. }
  58. private void init() {
  59. DropShadow ds = new DropShadow();
  60. ds.setOffsetX(1.3);
  61. ds.setOffsetY(1.3);
  62. ds.setColor(Color.DARKGRAY);
  63. setPrefSize(Label.USE_COMPUTED_SIZE, Label.USE_COMPUTED_SIZE);
  64. shapeProperty().addListener(new ChangeListener<Shape>() {
  65. @Override
  66. public void changed(ObservableValue<? extends Shape> arg0, Shape arg1, Shape arg2) {
  67. if (systemCall) {
  68. systemCall = false;
  69. } else {
  70. shapeIt();
  71. }
  72. /*
  73. * if(arg2.getClass().isAssignableFrom(Bubble.class)){ // i do no need to check
  74. * for this actuall is waste of time systemCall = false; return; }else{ // not
  75. * the required shape systemCall = true; setShape(new Bubble(bs)); System.gc();
  76. * }
  77. */
  78. }
  79. });
  80. heightProperty().addListener(new InvalidationListener() {
  81. @Override
  82. public void invalidated(Observable arg0) {
  83. if (!systemCall)
  84. setPrefHeight(Label.USE_COMPUTED_SIZE);
  85. }
  86. });
  87. widthProperty().addListener(new InvalidationListener() {
  88. @Override
  89. public void invalidated(Observable observable) {
  90. if (!systemCall)
  91. setPrefHeight(Label.USE_COMPUTED_SIZE);
  92. }
  93. });
  94. shapeIt();
  95. }
  96. @Override
  97. protected void updateBounds() {
  98. super.updateBounds();
  99. // top right bottom left
  100. switch (bs) {
  101. case FACE_LEFT_BOTTOM:
  102. setPadding(new Insets(pading, pading,
  103. (this.getBoundsInLocal().getWidth() * ((Bubble) getShape()).drawRectBubbleIndicatorRule) / 2
  104. + pading,
  105. pading));
  106. break;
  107. case FACE_LEFT_CENTER:
  108. setPadding(new Insets(pading, pading, pading,
  109. (this.getBoundsInLocal().getWidth() * ((Bubble) getShape()).drawRectBubbleIndicatorRule) / 2
  110. + pading));
  111. break;
  112. case FACE_RIGHT_BOTTOM:
  113. setPadding(new Insets(pading,
  114. (this.getBoundsInLocal().getWidth() * ((Bubble) getShape()).drawRectBubbleIndicatorRule) / 2
  115. + pading,
  116. pading, pading));
  117. break;
  118. case FACE_RIGHT_CENTER:
  119. setPadding(new Insets(pading,
  120. (this.getBoundsInLocal().getWidth() * ((Bubble) getShape()).drawRectBubbleIndicatorRule) / 2
  121. + pading,
  122. pading, pading));
  123. break;
  124. case FACE_TOP:
  125. setPadding(new Insets(
  126. (this.getBoundsInLocal().getWidth() * ((Bubble) getShape()).drawRectBubbleIndicatorRule) / 2
  127. + pading,
  128. pading, pading, pading));
  129. break;
  130. default:
  131. break;
  132. }
  133. }
  134. public final double getPading() {
  135. return pading;
  136. }
  137. public void setPading(double pading) {
  138. if (pading > 25.0)
  139. return;
  140. this.pading = pading;
  141. }
  142. public BubbleSpec getBubbleSpec() {
  143. return bs;
  144. }
  145. public void setBubbleSpec(BubbleSpec bubbleSpec) {
  146. this.bs = bubbleSpec;
  147. shapeIt();
  148. }
  149. private final void shapeIt() {
  150. systemCall = true;
  151. setShape(new Bubble(bs));
  152. System.gc();
  153. }
  154. }