inputText.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON.GUI {
  3. export class InputText extends Control implements IFocusableControl {
  4. private _text = "";
  5. private _background = "black";
  6. private _focusedBackground = "black";
  7. private _thickness = 1;
  8. private _margin = new ValueAndUnit(10, ValueAndUnit.UNITMODE_PIXEL);
  9. private _autoStretchWidth = true;
  10. private _maxWidth = new ValueAndUnit(1, ValueAndUnit.UNITMODE_PERCENTAGE, false);
  11. private _isFocused = false;
  12. private _blinkTimeout: number;
  13. private _blinkIsEven = false;
  14. private _cursorOffset = 0;
  15. private _scrollLeft: number;
  16. public promptMessage = "Please enter text:";
  17. public onTextChangedObservable = new Observable<InputText>();
  18. public onFocusObservable = new Observable<InputText>();
  19. public onBlurObservable = new Observable<InputText>();
  20. public get maxWidth(): string | number {
  21. return this._maxWidth.toString(this._host);
  22. }
  23. public set maxWidth(value: string | number ) {
  24. if (this._maxWidth.toString(this._host) === value) {
  25. return;
  26. }
  27. if (this._maxWidth.fromString(value)) {
  28. this._markAsDirty();
  29. }
  30. }
  31. public get margin(): string {
  32. return this._margin.toString(this._host);
  33. }
  34. public set margin(value: string) {
  35. if (this._margin.toString(this._host) === value) {
  36. return;
  37. }
  38. if (this._margin.fromString(value)) {
  39. this._markAsDirty();
  40. }
  41. }
  42. public get autoStretchWidth(): boolean {
  43. return this._autoStretchWidth;
  44. }
  45. public set autoStretchWidth(value: boolean) {
  46. if (this._autoStretchWidth === value) {
  47. return;
  48. }
  49. this._autoStretchWidth = value;
  50. this._markAsDirty();
  51. }
  52. public get thickness(): number {
  53. return this._thickness;
  54. }
  55. public set thickness(value: number) {
  56. if (this._thickness === value) {
  57. return;
  58. }
  59. this._thickness = value;
  60. this._markAsDirty();
  61. }
  62. public get focusedBackground(): string {
  63. return this._focusedBackground;
  64. }
  65. public set focusedBackground(value: string) {
  66. if (this._focusedBackground === value) {
  67. return;
  68. }
  69. this._focusedBackground = value;
  70. this._markAsDirty();
  71. }
  72. public get background(): string {
  73. return this._background;
  74. }
  75. public set background(value: string) {
  76. if (this._background === value) {
  77. return;
  78. }
  79. this._background = value;
  80. this._markAsDirty();
  81. }
  82. public get text(): string {
  83. return this._text;
  84. }
  85. public set text(value: string) {
  86. if (this._text === value) {
  87. return;
  88. }
  89. this._text = value;
  90. this._markAsDirty();
  91. this.onTextChangedObservable.notifyObservers(this);
  92. }
  93. constructor(public name?: string, text: string = "") {
  94. super(name);
  95. this.text = text;
  96. }
  97. public onBlur(): void {
  98. this._isFocused = false;
  99. this._scrollLeft = null;
  100. this._cursorOffset = 0;
  101. clearTimeout(this._blinkTimeout);
  102. this._markAsDirty();
  103. this.onBlurObservable.notifyObservers(this);
  104. }
  105. public onFocus(): void {
  106. this._scrollLeft = null;
  107. this._isFocused = true;
  108. this._blinkIsEven = false;
  109. this._cursorOffset = 0;
  110. this._markAsDirty();
  111. this.onFocusObservable.notifyObservers(this);
  112. if (navigator.userAgent.indexOf("Mobile") !== -1) {
  113. this.text = prompt(this.promptMessage);
  114. this._host.focusedControl = null;
  115. return;
  116. }
  117. }
  118. protected _getTypeName(): string {
  119. return "InputText";
  120. }
  121. public processKey(keyCode: number, key?: string) {
  122. // Specific cases
  123. switch (keyCode) {
  124. case 8: // BACKSPACE
  125. if (this._text && this._text.length > 0) {
  126. if (this._cursorOffset === 0) {
  127. this.text = this._text.substr(0, this._text.length - 1);
  128. } else {
  129. let deletePosition = this._text.length - this._cursorOffset;
  130. if (deletePosition > 0) {
  131. this.text = this._text.slice(0, deletePosition - 1) + this._text.slice(deletePosition);
  132. }
  133. }
  134. }
  135. return;
  136. case 46: // DELETE
  137. if (this._text && this._text.length > 0) {
  138. let deletePosition = this._text.length - this._cursorOffset;
  139. this.text = this._text.slice(0, deletePosition) + this._text.slice(deletePosition + 1);
  140. this._cursorOffset--;
  141. }
  142. return;
  143. case 13: // RETURN
  144. this._host.focusedControl = null;
  145. return;
  146. case 35: // END
  147. this._cursorOffset = 0;
  148. this._blinkIsEven = false;
  149. this._markAsDirty();
  150. return;
  151. case 36: // HOME
  152. this._cursorOffset = this._text.length;
  153. this._blinkIsEven = false;
  154. this._markAsDirty();
  155. return;
  156. case 37: // LEFT
  157. this._cursorOffset++;
  158. if (this._cursorOffset > this._text.length) {
  159. this._cursorOffset = this._text.length;
  160. }
  161. this._blinkIsEven = false;
  162. this._markAsDirty();
  163. return;
  164. case 39: // RIGHT
  165. this._cursorOffset--;
  166. if (this._cursorOffset < 0) {
  167. this._cursorOffset = 0;
  168. }
  169. this._blinkIsEven = false;
  170. this._markAsDirty();
  171. return;
  172. }
  173. // Printable characters
  174. if (
  175. (keyCode === -1) || // Direct access
  176. (keyCode === 32) || // Space
  177. (keyCode > 47 && keyCode < 58) || // Numbers
  178. (keyCode > 64 && keyCode < 91) || // Letters
  179. (keyCode > 185 && keyCode < 193) || // Special characters
  180. (keyCode > 218 && keyCode < 223) || // Special characters
  181. (keyCode > 95 && keyCode < 112)) { // Numpad
  182. if (this._cursorOffset === 0) {
  183. this.text += key;
  184. } else {
  185. let insertPosition = this._text.length - this._cursorOffset;
  186. this.text = this._text.slice(0, insertPosition) + key + this._text.slice(insertPosition);
  187. }
  188. }
  189. }
  190. public processKeyboard(evt: KeyboardEvent): void {
  191. this.processKey(evt.keyCode, evt.key);
  192. }
  193. public _draw(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  194. context.save();
  195. this._applyStates(context);
  196. if (this._processMeasures(parentMeasure, context)) {
  197. // Background
  198. if (this._isFocused) {
  199. if (this._focusedBackground) {
  200. context.fillStyle = this._focusedBackground;
  201. context.fillRect(this._currentMeasure.left, this._currentMeasure.top, this._currentMeasure.width, this._currentMeasure.height);
  202. }
  203. } else if (this._background) {
  204. context.fillStyle = this._background;
  205. context.fillRect(this._currentMeasure.left, this._currentMeasure.top, this._currentMeasure.width, this._currentMeasure.height);
  206. }
  207. if (!this._fontOffset) {
  208. this._fontOffset = Control._GetFontOffset(context.font);
  209. }
  210. // Text
  211. let clipTextLeft = this._currentMeasure.left + this._margin.getValueInPixel(this._host, parentMeasure.width);
  212. if (this.color) {
  213. context.fillStyle = this.color;
  214. }
  215. let textWidth = context.measureText(this._text).width;
  216. let marginWidth = this._margin.getValueInPixel(this._host, parentMeasure.width) * 2;
  217. if (this._autoStretchWidth) {
  218. this.width = Math.min(this._maxWidth.getValueInPixel(this._host, parentMeasure.width), textWidth + marginWidth) + "px";
  219. }
  220. let rootY = this._fontOffset.ascent + (this._currentMeasure.height - this._fontOffset.height) / 2;
  221. let availableWidth = this._width.getValueInPixel(this._host, parentMeasure.width) - marginWidth;
  222. context.save();
  223. context.beginPath();
  224. context.rect(clipTextLeft, this._currentMeasure.top + (this._currentMeasure.height - this._fontOffset.height) / 2, availableWidth + 2, this._currentMeasure.height);
  225. context.clip();
  226. if (this._isFocused && textWidth > availableWidth) {
  227. let textLeft = clipTextLeft - textWidth + availableWidth;
  228. if (!this._scrollLeft) {
  229. this._scrollLeft = textLeft;
  230. }
  231. } else {
  232. this._scrollLeft = clipTextLeft;
  233. }
  234. context.fillText(this._text, this._scrollLeft, this._currentMeasure.top + rootY);
  235. // Cursor
  236. if (this._isFocused) {
  237. if (!this._blinkIsEven) {
  238. let cursorOffsetText = this.text.substr(this._text.length - this._cursorOffset);
  239. let cursorOffsetWidth = context.measureText(cursorOffsetText).width;
  240. let cursorLeft = this._scrollLeft + textWidth - cursorOffsetWidth;
  241. if (cursorLeft < clipTextLeft) {
  242. this._scrollLeft += (clipTextLeft - cursorLeft);
  243. cursorLeft = clipTextLeft;
  244. this._markAsDirty();
  245. } else if (cursorLeft > clipTextLeft + availableWidth) {
  246. this._scrollLeft += (clipTextLeft + availableWidth - cursorLeft);
  247. cursorLeft = clipTextLeft + availableWidth;
  248. this._markAsDirty();
  249. }
  250. context.fillRect(cursorLeft, this._currentMeasure.top + (this._currentMeasure.height - this._fontOffset.height) / 2, 2, this._fontOffset.height);
  251. }
  252. clearTimeout(this._blinkTimeout);
  253. this._blinkTimeout = setTimeout(() => {
  254. this._blinkIsEven = !this._blinkIsEven;
  255. this._markAsDirty();
  256. }, 500);
  257. }
  258. context.restore();
  259. // Border
  260. if (this._thickness) {
  261. if (this.color) {
  262. context.strokeStyle = this.color;
  263. }
  264. context.lineWidth = this._thickness;
  265. context.strokeRect(this._currentMeasure.left + this._thickness / 2, this._currentMeasure.top + this._thickness / 2,
  266. this._currentMeasure.width - this._thickness, this._currentMeasure.height - this._thickness);
  267. }
  268. }
  269. context.restore();
  270. }
  271. protected _onPointerDown(coordinates: Vector2): boolean {
  272. if (!super._onPointerDown(coordinates)) {
  273. return false;
  274. }
  275. this._host.focusedControl = this;
  276. return true;
  277. }
  278. protected _onPointerUp(coordinates: Vector2): void {
  279. super._onPointerUp(coordinates);
  280. }
  281. public dispose() {
  282. super.dispose();
  283. this.onBlurObservable.clear();
  284. this.onFocusObservable.clear();
  285. this.onTextChangedObservable.clear();
  286. }
  287. }
  288. }