textBlock.ts 12 KB

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