textBlock.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON.GUI {
  3. export class TextBlock extends Control {
  4. private _text: string;
  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. private _drawText(text: string, textWidth: number, y: number, context: CanvasRenderingContext2D): void {
  56. var width = this._currentMeasure.width;
  57. var x = 0;
  58. switch (this._textHorizontalAlignment) {
  59. case Control.HORIZONTAL_ALIGNMENT_LEFT:
  60. x = 0
  61. break;
  62. case Control.HORIZONTAL_ALIGNMENT_RIGHT:
  63. x = width - textWidth;
  64. break;
  65. case Control.HORIZONTAL_ALIGNMENT_CENTER:
  66. x = (width - textWidth) / 2;
  67. break;
  68. }
  69. context.fillText(text, this._currentMeasure.left + x, y);
  70. }
  71. public _draw(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  72. context.save();
  73. this._applyStates(context);
  74. super._processMeasures(parentMeasure, context);
  75. // Render lines
  76. this._renderLines(context);
  77. context.restore();
  78. }
  79. protected _additionalProcessing(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  80. this._lines = [];
  81. if (this._textWrapping) {
  82. var words = this.text.split(' ');
  83. var line = '';
  84. var width = this._currentMeasure.width;
  85. var lineWidth = 0;
  86. for(var n = 0; n < words.length; n++) {
  87. var testLine = line + words[n] + ' ';
  88. var metrics = context.measureText(testLine);
  89. var testWidth = metrics.width;
  90. if (testWidth > width && n > 0) {
  91. this._lines.push({text: line, width: lineWidth});
  92. line = words[n] + ' ';
  93. lineWidth = context.measureText(line).width;
  94. }
  95. else {
  96. lineWidth = testWidth;
  97. line = testLine;
  98. }
  99. }
  100. this._lines.push({text: line, width: lineWidth});
  101. } else {
  102. this._lines.push({text: this.text, width: context.measureText(this.text).width});
  103. }
  104. }
  105. protected _renderLines(context: CanvasRenderingContext2D): void {
  106. var width = this._currentMeasure.width;
  107. var height = this._currentMeasure.height;
  108. if (!this._fontOffset) {
  109. this._fontOffset = Control._GetFontOffset(context.font);
  110. }
  111. var rootY = 0;
  112. switch (this._textVerticalAlignment) {
  113. case Control.VERTICAL_ALIGNMENT_TOP:
  114. rootY = this._fontOffset.ascent;
  115. break;
  116. case Control.VERTICAL_ALIGNMENT_BOTTOM:
  117. rootY = height - this._fontOffset.height * (this._lines.length - 1) - this._fontOffset.descent;
  118. break;
  119. case Control.VERTICAL_ALIGNMENT_CENTER:
  120. rootY = this._fontOffset.ascent + (height - this._fontOffset.height * this._lines.length) / 2;
  121. break;
  122. }
  123. rootY += this._currentMeasure.top;
  124. for (var line of this._lines) {
  125. this._drawText(line.text, line.width, rootY, context);
  126. rootY += this._fontOffset.height;
  127. }
  128. }
  129. }
  130. }