inputText.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON.GUI {
  3. export class InputText extends Control {
  4. private _text = "";
  5. private _background = "black";
  6. private _thickness = 1;
  7. private _margin = new ValueAndUnit(10, ValueAndUnit.UNITMODE_PIXEL);
  8. private _autoStretchWidth = true;
  9. private _maxWidth = new ValueAndUnit(1, ValueAndUnit.UNITMODE_PERCENTAGE, false);
  10. public get maxWidth(): string | number {
  11. return this._maxWidth.toString(this._host);
  12. }
  13. public set maxWidth(value: string | number ) {
  14. if (this._maxWidth.toString(this._host) === value) {
  15. return;
  16. }
  17. if (this._maxWidth.fromString(value)) {
  18. this._markAsDirty();
  19. }
  20. }
  21. public get margin(): string {
  22. return this._margin.toString(this._host);
  23. }
  24. public set margin(value: string) {
  25. if (this._margin.toString(this._host) === value) {
  26. return;
  27. }
  28. if (this._margin.fromString(value)) {
  29. this._markAsDirty();
  30. }
  31. }
  32. public get autoStretchWidth(): boolean {
  33. return this._autoStretchWidth;
  34. }
  35. public set autoStretchWidth(value: boolean) {
  36. if (this._autoStretchWidth === value) {
  37. return;
  38. }
  39. this._autoStretchWidth = value;
  40. this._markAsDirty();
  41. }
  42. public get thickness(): number {
  43. return this._thickness;
  44. }
  45. public set thickness(value: number) {
  46. if (this._thickness === value) {
  47. return;
  48. }
  49. this._thickness = value;
  50. this._markAsDirty();
  51. }
  52. public get background(): string {
  53. return this._background;
  54. }
  55. public set background(value: string) {
  56. if (this._background === value) {
  57. return;
  58. }
  59. this._background = value;
  60. this._markAsDirty();
  61. }
  62. public get text(): string {
  63. return this._text;
  64. }
  65. public set text(value: string) {
  66. if (this._text === value) {
  67. return;
  68. }
  69. this._text = value;
  70. this._markAsDirty();
  71. }
  72. constructor(public name?: string, text: string = "") {
  73. super(name);
  74. this.text = text;
  75. }
  76. protected _getTypeName(): string {
  77. return "InputText";
  78. }
  79. public _draw(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  80. context.save();
  81. this._applyStates(context);
  82. if (this._processMeasures(parentMeasure, context)) {
  83. // Background
  84. if (this._background) {
  85. context.fillStyle = this._background;
  86. context.fillRect(this._currentMeasure.left, this._currentMeasure.top, this._currentMeasure.width, this._currentMeasure.height);
  87. }
  88. // Text
  89. if (this._text) {
  90. if (this.color) {
  91. context.fillStyle = this.color;
  92. }
  93. let rootY = this._fontOffset.ascent + (this._currentMeasure.height - this._fontOffset.height) / 2;
  94. context.fillText(this._text, this._currentMeasure.left + this._margin.getValueInPixel(this._host, parentMeasure.width), this._currentMeasure.top + rootY);
  95. if (this._autoStretchWidth) {
  96. this.width = Math.min(this._maxWidth.getValueInPixel(this._host, parentMeasure.width), context.measureText(this._text).width + this._margin.getValueInPixel(this._host, parentMeasure.width) * 2) + "px";
  97. }
  98. }
  99. // Border
  100. if (this._thickness) {
  101. if (this.color) {
  102. context.strokeStyle = this.color;
  103. }
  104. context.lineWidth = this._thickness;
  105. context.strokeRect(this._currentMeasure.left + this._thickness / 2, this._currentMeasure.top + this._thickness / 2,
  106. this._currentMeasure.width - this._thickness, this._currentMeasure.height - this._thickness);
  107. }
  108. }
  109. context.restore();
  110. }
  111. }
  112. }