scrollText.ts 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. import { Measure } from "../measure";
  2. import { Rectangle } from "./rectangle";
  3. import { Grid } from "./grid";
  4. import { Control } from "./control";
  5. import { TextBlock } from "./textBlock";
  6. import { Slider } from "./slider";
  7. import { ValueAndUnit } from "../valueAndUnit";
  8. /** Class used to hold the textBlock and slider in a grid
  9. */
  10. export class ScrollText extends Rectangle {
  11. private _grid: Grid;
  12. private _textBlock: TextBlock;
  13. private _text: string = "";
  14. private _fontColor: string = "white";
  15. private _bar: Slider;
  16. private _barColor: string = "grey";
  17. private _barBorderColor: string = "#444444";
  18. private _barBackground: string = "white";
  19. private _endTop: number;
  20. private _scrollGridWidth: number = 30;
  21. /**
  22. * Gets or sets the text to display
  23. */
  24. public get text(): string {
  25. return this._text;
  26. }
  27. public set text(value: string) {
  28. if (this._text === value) {
  29. return;
  30. }
  31. this._text = value;
  32. this._textBlock.text = value;
  33. }
  34. /**
  35. * Gets or sets the fontColor
  36. */
  37. public get fontColor(): string {
  38. return this._fontColor;
  39. }
  40. public set fontColor(color: string) {
  41. if (this._fontColor === color) {
  42. return;
  43. }
  44. this._fontColor = color;
  45. this._textBlock.color = color;
  46. }
  47. /**
  48. * Sets the width of the scrollText, the left grid column and the textBlock
  49. */
  50. public set width(value: string | number) {
  51. if (this._width.toString(this._host) === value) {
  52. return;
  53. }
  54. if (this._width.fromString(value)) {
  55. this._markAsDirty();
  56. this._textBlock.width = (this.widthInPixels - this._scrollGridWidth) + "px";
  57. this._grid.setColumnDefinition(0, this._textBlock.widthInPixels, true);
  58. }
  59. }
  60. /**
  61. * Sets the height of the scrollText, the grid and the textBlock
  62. */
  63. public set height(value: string | number) {
  64. if (this._height.toString(this._host) === value) {
  65. return;
  66. }
  67. if (this._height.fromString(value)) {
  68. this._grid.setRowDefinition(0, this.heightInPixels, true);
  69. this._markAsDirty();
  70. }
  71. }
  72. /**
  73. * Gets or sets a value indicating the padding to use on the left of the text
  74. */
  75. public get paddingLeft(): string | number {
  76. return this._textBlock.paddingLeft;
  77. }
  78. public set paddingLeft(value: string | number) {
  79. this._textBlock.paddingLeft = value;
  80. }
  81. /**
  82. * Gets a value indicating the padding in pixels to use on the left of the text
  83. */
  84. public get paddingLeftInPixels(): number {
  85. return this._textBlock.paddingLeftInPixels;
  86. }
  87. /**
  88. * Gets or sets a value indicating the padding to use on the right of the text
  89. */
  90. public get paddingRight(): string | number {
  91. return this._textBlock.paddingRight;
  92. }
  93. public set paddingRight(value: string | number) {
  94. this._textBlock.paddingRight = value;
  95. }
  96. /**
  97. * Gets a value indicating the padding in pixels to use on the right of the text
  98. */
  99. public get paddingRightInPixels(): number {
  100. return this._textBlock.paddingRightInPixels;
  101. }
  102. /**
  103. * Gets or sets a value indicating the padding to use at the top of the text
  104. */
  105. public get paddingTop(): string | number {
  106. return this._textBlock.paddingTop;
  107. }
  108. public set paddingTop(value: string | number) {
  109. this._textBlock.paddingTop = value;
  110. }
  111. /**
  112. * Gets a value indicating the padding in pixels to use at the top of the text
  113. */
  114. public get paddingTopInPixels(): number {
  115. return this._textBlock.paddingTopInPixels;
  116. }
  117. /**
  118. * Gets or sets a value indicating the padding to use on the bottom of the text
  119. */
  120. public get paddingBottom(): string | number {
  121. return this._textBlock.paddingBottom;
  122. }
  123. public set paddingBottom(value: string | number) {
  124. this._textBlock.paddingBottom = value;
  125. }
  126. /**
  127. * Gets a value indicating the padding in pixels to use on the bottom of the text
  128. */
  129. public get paddingBottomInPixels(): number {
  130. return this._textBlock.paddingBottomInPixels;
  131. }
  132. /**
  133. * Creates a new ScrollText
  134. * @param name of ScrollText
  135. * @param text of ScrollText
  136. */
  137. constructor(
  138. /** name of ScrollText */
  139. public name?: string,
  140. text: string = "") {
  141. super(name);
  142. this._text = text;
  143. this._grid = new Grid();
  144. this._textBlock = new TextBlock();
  145. this._bar = new Slider();
  146. this._width = new ValueAndUnit(0.25, ValueAndUnit.UNITMODE_PERCENTAGE, false);
  147. this._height = new ValueAndUnit(0.25, ValueAndUnit.UNITMODE_PERCENTAGE, false);
  148. this._background = "black";
  149. this.fontSize = "16px";
  150. this._grid.addColumnDefinition(1, true);
  151. this._grid.addColumnDefinition(this._scrollGridWidth, true);
  152. this.addControl(this._grid);
  153. this._textBlock.textWrapping = true;
  154. this._textBlock.color = this._fontColor;
  155. this._textBlock.top = "0px";
  156. this._textBlock.paddingLeft = "5px";
  157. this._textBlock.paddingRight = "5px";
  158. this._textBlock.paddingTop = "5px";
  159. this._textBlock.paddingBottom = "5px";
  160. this._textBlock.horizontalAlignment = Control.HORIZONTAL_ALIGNMENT_LEFT;
  161. this._textBlock.verticalAlignment = Control.VERTICAL_ALIGNMENT_TOP;
  162. this._textBlock.textHorizontalAlignment = Control.HORIZONTAL_ALIGNMENT_LEFT;
  163. this._textBlock.textVerticalAlignment = Control.VERTICAL_ALIGNMENT_TOP;
  164. this._textBlock.onLinesReadyObservable.add(() => {
  165. let textPadding = this._textBlock.paddingTopInPixels + this._textBlock.paddingBottomInPixels + 2 * this.thickness;
  166. let textBlockHeight = (this.fontOffset.height) * this._textBlock.lines.length + textPadding;
  167. this._textBlock.height = textBlockHeight + "px";
  168. this._endTop = this.heightInPixels - textBlockHeight - textPadding;
  169. this._bar.height = ((this.heightInPixels - textPadding) * 0.85) + "px";
  170. this._grid.setRowDefinition(0, this.heightInPixels - textPadding, true);
  171. if (textBlockHeight > this.heightInPixels) {
  172. this._bar.isVisible = true;
  173. this._grid.setColumnDefinition(1, this._scrollGridWidth, true);
  174. }
  175. else {
  176. this._bar.isVisible = false;
  177. this._grid.setColumnDefinition(1, 1, true);
  178. }
  179. });
  180. this._bar.paddingLeft = 0;
  181. this._bar.width = "25px";
  182. this._bar.value = 0;
  183. this._bar.maximum = 100;
  184. this._bar.horizontalAlignment = Control.VERTICAL_ALIGNMENT_CENTER;
  185. this._bar.verticalAlignment = Control.VERTICAL_ALIGNMENT_CENTER;
  186. this._bar.left = 0.05;
  187. this._bar.isThumbClamped = true;
  188. this._bar.color = "grey";
  189. this._bar.borderColor = "#444444";
  190. this._bar.background = "white";
  191. this._bar.isVisible = false;
  192. this._bar.isVertical = true;
  193. this._bar.rotation = Math.PI;
  194. this._grid.addControl(this._bar, 0, 1);
  195. this._bar.onValueChangedObservable.add((value) => {
  196. this._textBlock.top = (value * this._endTop / this._bar.maximum) + "px";
  197. });
  198. }
  199. /** Gets or sets the bar color */
  200. public get barColor(): string {
  201. return this._barColor;
  202. }
  203. public set barColor(color: string) {
  204. if (this._barColor === color) {
  205. return;
  206. }
  207. this._barColor = color;
  208. this._bar.color = color;
  209. }
  210. /** Gets or sets the bar color */
  211. public get barBorderColor(): string {
  212. return this._barBorderColor;
  213. }
  214. public set barBorderColor(color: string) {
  215. if (this._barBorderColor === color) {
  216. return;
  217. }
  218. this._barBorderColor = color;
  219. this._bar.borderColor = color;
  220. }
  221. /** Gets or sets the bar background */
  222. public get barBackground(): string {
  223. return this._barBackground;
  224. }
  225. public set barBackground(color: string) {
  226. if (this._barBackground === color) {
  227. return;
  228. }
  229. this._barBackground = color;
  230. this._bar.background = color;
  231. }
  232. protected _additionalProcessing(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  233. super._additionalProcessing(parentMeasure, context);
  234. this._measureForChildren.width -= 2 * this.thickness;
  235. this._measureForChildren.height -= 2 * this.thickness;
  236. this._measureForChildren.left += this.thickness;
  237. this._measureForChildren.top += this.thickness;
  238. let innerWidth = this._width.getValueInPixel(this._host, parentMeasure.width) - this._scrollGridWidth - 4 * this.thickness;
  239. this._textBlock.width = innerWidth + "px";
  240. this._grid.setColumnDefinition(0, innerWidth, true);
  241. this._grid.setRowDefinition(0, this._height.getValueInPixel(this._host, parentMeasure.height));
  242. this._grid.addControl(this._textBlock, 0, 0);
  243. this._textBlock.text = this._text;
  244. }
  245. }