textBlock.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON.GUI {
  3. export class TextBlock extends Control {
  4. private _text = "";
  5. private _textY: number;
  6. private _textWrapping = false;
  7. private _textHorizontalAlignment = Control.HORIZONTAL_ALIGNMENT_CENTER;
  8. private _textVerticalAlignment = Control.VERTICAL_ALIGNMENT_CENTER;
  9. private _lines: any[];
  10. private _totalHeight: number;
  11. public get textWrapping(): boolean {
  12. return this._textWrapping;
  13. }
  14. public set textWrapping(value: boolean) {
  15. if (this._textWrapping === value) {
  16. return;
  17. }
  18. this._textWrapping = value;
  19. this._markAsDirty();
  20. }
  21. public get text(): string {
  22. return this._text;
  23. }
  24. public set text(value: string) {
  25. if (this._text === value) {
  26. return;
  27. }
  28. this._text = value;
  29. this._markAsDirty();
  30. }
  31. public get textHorizontalAlignment(): number {
  32. return this._textHorizontalAlignment;
  33. }
  34. public set textHorizontalAlignment(value: number) {
  35. if (this._textHorizontalAlignment === value) {
  36. return;
  37. }
  38. this._textHorizontalAlignment = value;
  39. this._markAsDirty();
  40. }
  41. public get textVerticalAlignment(): number {
  42. return this._textVerticalAlignment;
  43. }
  44. public set textVerticalAlignment(value: number) {
  45. if (this._textVerticalAlignment === value) {
  46. return;
  47. }
  48. this._textVerticalAlignment = value;
  49. this._markAsDirty();
  50. }
  51. constructor(public name?: string, text: string = "") {
  52. super(name);
  53. this.text = text;
  54. }
  55. protected _getTypeName(): string {
  56. return "TextBlock";
  57. }
  58. private _drawText(text: string, textWidth: number, y: number, context: CanvasRenderingContext2D): void {
  59. var width = this._currentMeasure.width;
  60. var x = 0;
  61. switch (this._textHorizontalAlignment) {
  62. case Control.HORIZONTAL_ALIGNMENT_LEFT:
  63. x = 0
  64. break;
  65. case Control.HORIZONTAL_ALIGNMENT_RIGHT:
  66. x = width - textWidth;
  67. break;
  68. case Control.HORIZONTAL_ALIGNMENT_CENTER:
  69. x = (width - textWidth) / 2;
  70. break;
  71. }
  72. context.fillText(text, this._currentMeasure.left + x, y);
  73. }
  74. public _draw(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  75. context.save();
  76. this._applyStates(context);
  77. if (this._processMeasures(parentMeasure, context)) {
  78. // Render lines
  79. this._renderLines(context);
  80. }
  81. context.restore();
  82. }
  83. protected _additionalProcessing(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  84. this._lines = [];
  85. var _lines = this.text.split("\n");
  86. if (this._textWrapping) {
  87. for(var _line of _lines) {
  88. this._lines.push(this._parseLineWithTextWrapping(_line, context));
  89. }
  90. } else {
  91. for(var _line of _lines) {
  92. this._lines.push(this._parseLine(_line, context));
  93. }
  94. }
  95. }
  96. protected _parseLine(line: string='', context: CanvasRenderingContext2D): object {
  97. return {text: line, width: context.measureText(line).width};
  98. }
  99. protected _parseLineWithTextWrapping(line: string='', context: CanvasRenderingContext2D): object {
  100. var words = line.split(' ');
  101. var width = this._currentMeasure.width;
  102. var lineWidth = 0;
  103. for(var n = 0; n < words.length; n++) {
  104. var testLine = n > 0 ? line + " " + words[n] : words[0];
  105. var metrics = context.measureText(testLine);
  106. var testWidth = metrics.width;
  107. if (testWidth > width && n > 0) {
  108. this._lines.push({text: line, width: lineWidth});
  109. line = words[n];
  110. lineWidth = context.measureText(line).width;
  111. }
  112. else {
  113. lineWidth = testWidth;
  114. line = testLine;
  115. }
  116. }
  117. return {text: line, width: lineWidth};
  118. }
  119. protected _renderLines(context: CanvasRenderingContext2D): void {
  120. var width = this._currentMeasure.width;
  121. var height = this._currentMeasure.height;
  122. if (!this._fontOffset) {
  123. this._fontOffset = Control._GetFontOffset(context.font);
  124. }
  125. var rootY = 0;
  126. switch (this._textVerticalAlignment) {
  127. case Control.VERTICAL_ALIGNMENT_TOP:
  128. rootY = this._fontOffset.ascent;
  129. break;
  130. case Control.VERTICAL_ALIGNMENT_BOTTOM:
  131. rootY = height - this._fontOffset.height * (this._lines.length - 1) - this._fontOffset.descent;
  132. break;
  133. case Control.VERTICAL_ALIGNMENT_CENTER:
  134. rootY = this._fontOffset.ascent + (height - this._fontOffset.height * this._lines.length) / 2;
  135. break;
  136. }
  137. rootY += this._currentMeasure.top;
  138. for (var line of this._lines) {
  139. this._drawText(line.text, line.width, rootY, context);
  140. rootY += this._fontOffset.height;
  141. }
  142. }
  143. }
  144. }