textBlock.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = Object.setPrototypeOf ||
  4. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  5. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  6. return function (d, b) {
  7. extendStatics(d, b);
  8. function __() { this.constructor = d; }
  9. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  10. };
  11. })();
  12. var BABYLON;
  13. (function (BABYLON) {
  14. var GUI;
  15. (function (GUI) {
  16. var TextBlock = (function (_super) {
  17. __extends(TextBlock, _super);
  18. function TextBlock(name, text) {
  19. var _this = _super.call(this, name) || this;
  20. _this.name = name;
  21. _this.text = text;
  22. return _this;
  23. }
  24. Object.defineProperty(TextBlock.prototype, "text", {
  25. get: function () {
  26. return this._text;
  27. },
  28. set: function (value) {
  29. if (this._text === value) {
  30. return;
  31. }
  32. this._text = value;
  33. this._markAsDirty();
  34. },
  35. enumerable: true,
  36. configurable: true
  37. });
  38. TextBlock.prototype._draw = function (parentMeasure, context) {
  39. context.save();
  40. this.applyStates(context);
  41. this._prepare(parentMeasure, context);
  42. context.fillText(this.text, this._currentMeasure.left, this._currentMeasure.top);
  43. context.restore();
  44. };
  45. TextBlock.prototype._prepare = function (parentMeasure, context) {
  46. var width = parentMeasure.width;
  47. var height = parentMeasure.height;
  48. var x = 0;
  49. var y = 0;
  50. var textSize = context.measureText(this.text);
  51. switch (this.horizontalAlignment) {
  52. case GUI.Control.HORIZONTAL_ALIGNMENT_LEFT:
  53. x = 0;
  54. break;
  55. case GUI.Control.HORIZONTAL_ALIGNMENT_RIGHT:
  56. x = width - textSize.width;
  57. break;
  58. case GUI.Control.HORIZONTAL_ALIGNMENT_CENTER:
  59. x = (width - textSize.width) / 2;
  60. break;
  61. }
  62. if (!this._fontOffset) {
  63. this._fontOffset = GUI.Control._GetFontOffset(context.font);
  64. }
  65. switch (this.verticalAlignment) {
  66. case GUI.Control.VERTICAL_ALIGNMENT_TOP:
  67. y = this._fontOffset.ascent;
  68. break;
  69. case GUI.Control.VERTICAL_ALIGNMENT_BOTTOM:
  70. y = height - this._fontOffset.descent;
  71. break;
  72. case GUI.Control.VERTICAL_ALIGNMENT_CENTER:
  73. y = (height / 2) + (this._fontOffset.ascent - this._fontOffset.height / 2);
  74. break;
  75. }
  76. this._currentMeasure = new GUI.Measure(parentMeasure.left + x, parentMeasure.top + y, width, height);
  77. };
  78. return TextBlock;
  79. }(GUI.Control));
  80. GUI.TextBlock = TextBlock;
  81. })(GUI = BABYLON.GUI || (BABYLON.GUI = {}));
  82. })(BABYLON || (BABYLON = {}));
  83. //# sourceMappingURL=textBlock.js.map