textBlock.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON.GUI {
  3. /**
  4. * Enum that determines the text-wrapping mode to use.
  5. */
  6. export enum TextWrapping {
  7. /**
  8. * Clip the text when it's larger than Control.width; this is the default mode.
  9. */
  10. Clip = 0,
  11. /**
  12. * Wrap the text word-wise, i.e. try to add line-breaks at word boundary to fit within Control.width.
  13. */
  14. WordWrap = 1,
  15. /**
  16. * Ellipsize the text, i.e. shrink with trailing … when text is larger than Control.width.
  17. */
  18. Ellipsis,
  19. }
  20. /**
  21. * Class used to create text block control
  22. */
  23. export class TextBlock extends Control {
  24. private _text = "";
  25. private _textWrapping = TextWrapping.Clip;
  26. private _textHorizontalAlignment = Control.HORIZONTAL_ALIGNMENT_CENTER;
  27. private _textVerticalAlignment = Control.VERTICAL_ALIGNMENT_CENTER;
  28. private _lines: any[];
  29. private _resizeToFit: boolean = false;
  30. private _lineSpacing: ValueAndUnit = new ValueAndUnit(0);
  31. private _outlineWidth: number = 0;
  32. private _outlineColor: string = "white";
  33. /**
  34. * An event triggered after the text is changed
  35. */
  36. public onTextChangedObservable = new Observable<TextBlock>();
  37. /**
  38. * An event triggered after the text was broken up into lines
  39. */
  40. public onLinesReadyObservable = new Observable<TextBlock>();
  41. /**
  42. * Return the line list (you may need to use the onLinesReadyObservable to make sure the list is ready)
  43. */
  44. public get lines(): any[] {
  45. return this._lines;
  46. }
  47. /**
  48. * Gets or sets an boolean indicating that the TextBlock will be resized to fit container
  49. */
  50. public get resizeToFit(): boolean {
  51. return this._resizeToFit;
  52. }
  53. /**
  54. * Gets or sets an boolean indicating that the TextBlock will be resized to fit container
  55. */
  56. public set resizeToFit(value: boolean) {
  57. this._resizeToFit = value;
  58. if (this._resizeToFit) {
  59. this._width.ignoreAdaptiveScaling = true;
  60. this._height.ignoreAdaptiveScaling = true;
  61. }
  62. }
  63. /**
  64. * Gets or sets a boolean indicating if text must be wrapped
  65. */
  66. public get textWrapping(): TextWrapping | boolean {
  67. return this._textWrapping;
  68. }
  69. /**
  70. * Gets or sets a boolean indicating if text must be wrapped
  71. */
  72. public set textWrapping(value: TextWrapping | boolean) {
  73. if (this._textWrapping === value) {
  74. return;
  75. }
  76. this._textWrapping = +value;
  77. this._markAsDirty();
  78. }
  79. /**
  80. * Gets or sets text to display
  81. */
  82. public get text(): string {
  83. return this._text;
  84. }
  85. /**
  86. * Gets or sets text to display
  87. */
  88. public set text(value: string) {
  89. if (this._text === value) {
  90. return;
  91. }
  92. this._text = value;
  93. this._markAsDirty();
  94. this.onTextChangedObservable.notifyObservers(this);
  95. }
  96. /**
  97. * Gets or sets text horizontal alignment (BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_CENTER by default)
  98. */
  99. public get textHorizontalAlignment(): number {
  100. return this._textHorizontalAlignment;
  101. }
  102. /**
  103. * Gets or sets text horizontal alignment (BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_CENTER by default)
  104. */
  105. public set textHorizontalAlignment(value: number) {
  106. if (this._textHorizontalAlignment === value) {
  107. return;
  108. }
  109. this._textHorizontalAlignment = value;
  110. this._markAsDirty();
  111. }
  112. /**
  113. * Gets or sets text vertical alignment (BABYLON.GUI.Control.VERTICAL_ALIGNMENT_CENTER by default)
  114. */
  115. public get textVerticalAlignment(): number {
  116. return this._textVerticalAlignment;
  117. }
  118. /**
  119. * Gets or sets text vertical alignment (BABYLON.GUI.Control.VERTICAL_ALIGNMENT_CENTER by default)
  120. */
  121. public set textVerticalAlignment(value: number) {
  122. if (this._textVerticalAlignment === value) {
  123. return;
  124. }
  125. this._textVerticalAlignment = value;
  126. this._markAsDirty();
  127. }
  128. /**
  129. * Gets or sets line spacing value
  130. */
  131. public set lineSpacing(value: string | number) {
  132. if (this._lineSpacing.fromString(value)) {
  133. this._markAsDirty();
  134. }
  135. }
  136. /**
  137. * Gets or sets line spacing value
  138. */
  139. public get lineSpacing(): string | number {
  140. return this._lineSpacing.toString(this._host);
  141. }
  142. /**
  143. * Gets or sets outlineWidth of the text to display
  144. */
  145. public get outlineWidth(): number {
  146. return this._outlineWidth;
  147. }
  148. /**
  149. * Gets or sets outlineWidth of the text to display
  150. */
  151. public set outlineWidth(value: number) {
  152. if (this._outlineWidth === value) {
  153. return;
  154. }
  155. this._outlineWidth = value;
  156. this._markAsDirty();
  157. }
  158. /**
  159. * Gets or sets outlineColor of the text to display
  160. */
  161. public get outlineColor(): string {
  162. return this._outlineColor;
  163. }
  164. /**
  165. * Gets or sets outlineColor of the text to display
  166. */
  167. public set outlineColor(value: string) {
  168. if (this._outlineColor === value) {
  169. return;
  170. }
  171. this._outlineColor = value;
  172. this._markAsDirty();
  173. }
  174. /**
  175. * Creates a new TextBlock object
  176. * @param name defines the name of the control
  177. * @param text defines the text to display (emptry string by default)
  178. */
  179. constructor(
  180. /**
  181. * Defines the name of the control
  182. */
  183. public name?: string,
  184. text: string = "") {
  185. super(name);
  186. this.text = text;
  187. }
  188. protected _getTypeName(): string {
  189. return "TextBlock";
  190. }
  191. private _drawText(text: string, textWidth: number, y: number, context: CanvasRenderingContext2D): void {
  192. var width = this._currentMeasure.width;
  193. var x = 0;
  194. switch (this._textHorizontalAlignment) {
  195. case Control.HORIZONTAL_ALIGNMENT_LEFT:
  196. x = 0
  197. break;
  198. case Control.HORIZONTAL_ALIGNMENT_RIGHT:
  199. x = width - textWidth;
  200. break;
  201. case Control.HORIZONTAL_ALIGNMENT_CENTER:
  202. x = (width - textWidth) / 2;
  203. break;
  204. }
  205. if (this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY) {
  206. context.shadowColor = this.shadowColor;
  207. context.shadowBlur = this.shadowBlur;
  208. context.shadowOffsetX = this.shadowOffsetX;
  209. context.shadowOffsetY = this.shadowOffsetY;
  210. }
  211. if (this.outlineWidth) {
  212. context.strokeText(text, this._currentMeasure.left + x, y);
  213. }
  214. context.fillText(text, this._currentMeasure.left + x, y);
  215. }
  216. /** @hidden */
  217. public _draw(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  218. context.save();
  219. this._applyStates(context);
  220. if (this._processMeasures(parentMeasure, context)) {
  221. // Render lines
  222. this._renderLines(context);
  223. }
  224. context.restore();
  225. }
  226. protected _applyStates(context: CanvasRenderingContext2D): void {
  227. super._applyStates(context);
  228. if (this.outlineWidth) {
  229. context.lineWidth = this.outlineWidth;
  230. context.strokeStyle = this.outlineColor;
  231. }
  232. }
  233. protected _additionalProcessing(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  234. this._lines = this._breakLines(this._currentMeasure.width, context);
  235. this.onLinesReadyObservable.notifyObservers(this);
  236. }
  237. protected _breakLines(refWidth: number, context: CanvasRenderingContext2D): object[] {
  238. var lines = [];
  239. var _lines = this.text.split("\n");
  240. if (this._textWrapping === TextWrapping.Ellipsis && !this._resizeToFit) {
  241. for (var _line of _lines) {
  242. lines.push(this._parseLineEllipsis(_line, refWidth, context));
  243. }
  244. } else if (this._textWrapping === TextWrapping.WordWrap && !this._resizeToFit) {
  245. for (var _line of _lines) {
  246. lines.push(...this._parseLineWordWrap(_line, refWidth, context));
  247. }
  248. } else {
  249. for (var _line of _lines) {
  250. lines.push(this._parseLine(_line, context));
  251. }
  252. }
  253. return lines;
  254. }
  255. protected _parseLine(line: string = '', context: CanvasRenderingContext2D): object {
  256. return { text: line, width: context.measureText(line).width };
  257. }
  258. protected _parseLineEllipsis(line: string = '', width: number,
  259. context: CanvasRenderingContext2D): object {
  260. var lineWidth = context.measureText(line).width;
  261. if (lineWidth > width) {
  262. line += '…';
  263. }
  264. while (line.length > 2 && lineWidth > width) {
  265. line = line.slice(0, -2) + '…';
  266. lineWidth = context.measureText(line).width;
  267. }
  268. return { text: line, width: lineWidth };
  269. }
  270. protected _parseLineWordWrap(line: string = '', width: number,
  271. context: CanvasRenderingContext2D): object[] {
  272. var lines = [];
  273. var words = line.split(' ');
  274. var lineWidth = 0;
  275. for (var n = 0; n < words.length; n++) {
  276. var testLine = n > 0 ? line + " " + words[n] : words[0];
  277. var metrics = context.measureText(testLine);
  278. var testWidth = metrics.width;
  279. if (testWidth > width && n > 0) {
  280. lines.push({ text: line, width: lineWidth });
  281. line = words[n];
  282. lineWidth = context.measureText(line).width;
  283. }
  284. else {
  285. lineWidth = testWidth;
  286. line = testLine;
  287. }
  288. }
  289. lines.push({ text: line, width: lineWidth });
  290. return lines;
  291. }
  292. protected _renderLines(context: CanvasRenderingContext2D): void {
  293. var height = this._currentMeasure.height;
  294. if (!this._fontOffset) {
  295. this._fontOffset = Control._GetFontOffset(context.font);
  296. }
  297. var rootY = 0;
  298. switch (this._textVerticalAlignment) {
  299. case Control.VERTICAL_ALIGNMENT_TOP:
  300. rootY = this._fontOffset.ascent;
  301. break;
  302. case Control.VERTICAL_ALIGNMENT_BOTTOM:
  303. rootY = height - this._fontOffset.height * (this._lines.length - 1) - this._fontOffset.descent;
  304. break;
  305. case Control.VERTICAL_ALIGNMENT_CENTER:
  306. rootY = this._fontOffset.ascent + (height - this._fontOffset.height * this._lines.length) / 2;
  307. break;
  308. }
  309. rootY += this._currentMeasure.top;
  310. var maxLineWidth: number = 0;
  311. for (let i = 0; i < this._lines.length; i++) {
  312. const line = this._lines[i];
  313. if (i !== 0 && this._lineSpacing.internalValue !== 0) {
  314. if (this._lineSpacing.isPixel) {
  315. rootY += this._lineSpacing.getValue(this._host);
  316. } else {
  317. rootY = rootY + (this._lineSpacing.getValue(this._host) * this._height.getValueInPixel(this._host, this._cachedParentMeasure.height));
  318. }
  319. }
  320. this._drawText(line.text, line.width, rootY, context);
  321. rootY += this._fontOffset.height;
  322. if (line.width > maxLineWidth) maxLineWidth = line.width;
  323. }
  324. if (this._resizeToFit) {
  325. this.width = this.paddingLeftInPixels + this.paddingRightInPixels + maxLineWidth + 'px';
  326. this.height = this.paddingTopInPixels + this.paddingBottomInPixels + this._fontOffset.height * this._lines.length + 'px';
  327. }
  328. }
  329. /**
  330. * Given a width constraint applied on the text block, find the expected height
  331. * @returns expected height
  332. */
  333. public computeExpectedHeight(): number {
  334. if (this.text && this.widthInPixels) {
  335. const context = document.createElement('canvas').getContext('2d');
  336. if (context) {
  337. this._applyStates(context);
  338. if (!this._fontOffset) {
  339. this._fontOffset = Control._GetFontOffset(context.font);
  340. }
  341. const lines = this._lines ? this._lines : this._breakLines(
  342. this.widthInPixels - this.paddingLeftInPixels - this.paddingRightInPixels, context);
  343. return this.paddingTopInPixels + this.paddingBottomInPixels + this._fontOffset.height * lines.length;
  344. }
  345. }
  346. return 0;
  347. }
  348. dispose(): void {
  349. super.dispose();
  350. this.onTextChangedObservable.clear();
  351. }
  352. }
  353. }