inputText.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON.GUI {
  3. /**
  4. * Class used to create input text control
  5. */
  6. export class InputText extends Control implements IFocusableControl {
  7. private _text = "";
  8. private _placeholderText = "";
  9. private _background = "#222222";
  10. private _focusedBackground = "#000000";
  11. private _placeholderColor = "gray";
  12. private _thickness = 1;
  13. private _margin = new ValueAndUnit(10, ValueAndUnit.UNITMODE_PIXEL);
  14. private _autoStretchWidth = true;
  15. private _maxWidth = new ValueAndUnit(1, ValueAndUnit.UNITMODE_PERCENTAGE, false);
  16. private _isFocused = false;
  17. private _blinkTimeout: number;
  18. private _blinkIsEven = false;
  19. private _cursorOffset = 0;
  20. private _scrollLeft: Nullable<number>;
  21. private _textWidth: number;
  22. private _clickedCoordinate: Nullable<number>;
  23. private _deadKey = false;
  24. private _addKey = true;
  25. private _currentKey = "";
  26. /** Gets or sets a string representing the message displayed on mobile when the control gets the focus */
  27. public promptMessage = "Please enter text:";
  28. /** Observable raised when the text changes */
  29. public onTextChangedObservable = new Observable<InputText>();
  30. /** Observable raised just before an entered character is to be added */
  31. public onBeforeKeyAddObservable = new Observable<InputText>();
  32. /** Observable raised when the control gets the focus */
  33. public onFocusObservable = new Observable<InputText>();
  34. /** Observable raised when the control loses the focus */
  35. public onBlurObservable = new Observable<InputText>();
  36. /** Gets or sets the maximum width allowed by the control */
  37. public get maxWidth(): string | number {
  38. return this._maxWidth.toString(this._host);
  39. }
  40. /** Gets the maximum width allowed by the control in pixels */
  41. public get maxWidthInPixels(): number {
  42. return this._maxWidth.getValueInPixel(this._host, this._cachedParentMeasure.width);
  43. }
  44. public set maxWidth(value: string | number) {
  45. if (this._maxWidth.toString(this._host) === value) {
  46. return;
  47. }
  48. if (this._maxWidth.fromString(value)) {
  49. this._markAsDirty();
  50. }
  51. }
  52. /** Gets or sets control margin */
  53. public get margin(): string {
  54. return this._margin.toString(this._host);
  55. }
  56. /** Gets control margin in pixels */
  57. public get marginInPixels(): number {
  58. return this._margin.getValueInPixel(this._host, this._cachedParentMeasure.width);
  59. }
  60. public set margin(value: string) {
  61. if (this._margin.toString(this._host) === value) {
  62. return;
  63. }
  64. if (this._margin.fromString(value)) {
  65. this._markAsDirty();
  66. }
  67. }
  68. /** Gets or sets a boolean indicating if the control can auto stretch its width to adapt to the text */
  69. public get autoStretchWidth(): boolean {
  70. return this._autoStretchWidth;
  71. }
  72. public set autoStretchWidth(value: boolean) {
  73. if (this._autoStretchWidth === value) {
  74. return;
  75. }
  76. this._autoStretchWidth = value;
  77. this._markAsDirty();
  78. }
  79. /** Gets or sets border thickness */
  80. public get thickness(): number {
  81. return this._thickness;
  82. }
  83. public set thickness(value: number) {
  84. if (this._thickness === value) {
  85. return;
  86. }
  87. this._thickness = value;
  88. this._markAsDirty();
  89. }
  90. /** Gets or sets the background color when focused */
  91. public get focusedBackground(): string {
  92. return this._focusedBackground;
  93. }
  94. public set focusedBackground(value: string) {
  95. if (this._focusedBackground === value) {
  96. return;
  97. }
  98. this._focusedBackground = value;
  99. this._markAsDirty();
  100. }
  101. /** Gets or sets the background color */
  102. public get background(): string {
  103. return this._background;
  104. }
  105. public set background(value: string) {
  106. if (this._background === value) {
  107. return;
  108. }
  109. this._background = value;
  110. this._markAsDirty();
  111. }
  112. /** Gets or sets the placeholder color */
  113. public get placeholderColor(): string {
  114. return this._placeholderColor;
  115. }
  116. public set placeholderColor(value: string) {
  117. if (this._placeholderColor === value) {
  118. return;
  119. }
  120. this._placeholderColor = value;
  121. this._markAsDirty();
  122. }
  123. /** Gets or sets the text displayed when the control is empty */
  124. public get placeholderText(): string {
  125. return this._placeholderText;
  126. }
  127. public set placeholderText(value: string) {
  128. if (this._placeholderText === value) {
  129. return;
  130. }
  131. this._placeholderText = value;
  132. this._markAsDirty();
  133. }
  134. /** Gets or sets the dead key flag */
  135. public get deadKey(): boolean {
  136. return this._deadKey;
  137. }
  138. public set deadKey(flag: boolean) {
  139. this._deadKey = flag;
  140. }
  141. /** Gets or sets if the current key should be added */
  142. public get addKey(): boolean {
  143. return this._addKey;
  144. }
  145. public set addKey(flag: boolean) {
  146. this._addKey = flag;
  147. }
  148. /** Gets or sets the value of the current key being entered */
  149. public get currentKey(): string {
  150. return this._currentKey;
  151. }
  152. public set currentKey(key: string) {
  153. this._currentKey = key;
  154. }
  155. /** Gets or sets the text displayed in the control */
  156. public get text(): string {
  157. return this._text;
  158. }
  159. public set text(value: string) {
  160. if (this._text === value) {
  161. return;
  162. }
  163. this._text = value;
  164. this._markAsDirty();
  165. this.onTextChangedObservable.notifyObservers(this);
  166. }
  167. /** Gets or sets control width */
  168. public get width(): string | number {
  169. return this._width.toString(this._host);
  170. }
  171. public set width(value: string | number) {
  172. if (this._width.toString(this._host) === value) {
  173. return;
  174. }
  175. if (this._width.fromString(value)) {
  176. this._markAsDirty();
  177. }
  178. this.autoStretchWidth = false;
  179. }
  180. /**
  181. * Creates a new InputText
  182. * @param name defines the control name
  183. * @param text defines the text of the control
  184. */
  185. constructor(public name?: string, text: string = "") {
  186. super(name);
  187. this.text = text;
  188. }
  189. /** @hidden */
  190. public onBlur(): void {
  191. this._isFocused = false;
  192. this._scrollLeft = null;
  193. this._cursorOffset = 0;
  194. clearTimeout(this._blinkTimeout);
  195. this._markAsDirty();
  196. this.onBlurObservable.notifyObservers(this);
  197. }
  198. /** @hidden */
  199. public onFocus(): void {
  200. this._scrollLeft = null;
  201. this._isFocused = true;
  202. this._blinkIsEven = false;
  203. this._cursorOffset = 0;
  204. this._markAsDirty();
  205. this.onFocusObservable.notifyObservers(this);
  206. if (navigator.userAgent.indexOf("Mobile") !== -1) {
  207. let value = prompt(this.promptMessage);
  208. if (value !== null) {
  209. this.text = value;
  210. }
  211. this._host.focusedControl = null;
  212. return;
  213. }
  214. }
  215. protected _getTypeName(): string {
  216. return "InputText";
  217. }
  218. /** @hidden */
  219. public processKey(keyCode: number, key?: string) {
  220. // Specific cases
  221. switch (keyCode) {
  222. case 32: //SPACE
  223. key = " "; //ie11 key for space is "Spacebar"
  224. break;
  225. case 8: // BACKSPACE
  226. if (this._text && this._text.length > 0) {
  227. if (this._cursorOffset === 0) {
  228. this.text = this._text.substr(0, this._text.length - 1);
  229. } else {
  230. let deletePosition = this._text.length - this._cursorOffset;
  231. if (deletePosition > 0) {
  232. this.text = this._text.slice(0, deletePosition - 1) + this._text.slice(deletePosition);
  233. }
  234. }
  235. }
  236. return;
  237. case 46: // DELETE
  238. if (this._text && this._text.length > 0) {
  239. let deletePosition = this._text.length - this._cursorOffset;
  240. this.text = this._text.slice(0, deletePosition) + this._text.slice(deletePosition + 1);
  241. this._cursorOffset--;
  242. }
  243. return;
  244. case 13: // RETURN
  245. this._host.focusedControl = null;
  246. return;
  247. case 35: // END
  248. this._cursorOffset = 0;
  249. this._blinkIsEven = false;
  250. this._markAsDirty();
  251. return;
  252. case 36: // HOME
  253. this._cursorOffset = this._text.length;
  254. this._blinkIsEven = false;
  255. this._markAsDirty();
  256. return;
  257. case 37: // LEFT
  258. this._cursorOffset++;
  259. if (this._cursorOffset > this._text.length) {
  260. this._cursorOffset = this._text.length;
  261. }
  262. this._blinkIsEven = false;
  263. this._markAsDirty();
  264. return;
  265. case 39: // RIGHT
  266. this._cursorOffset--;
  267. if (this._cursorOffset < 0) {
  268. this._cursorOffset = 0;
  269. }
  270. this._blinkIsEven = false;
  271. this._markAsDirty();
  272. return;
  273. case 222: // Dead
  274. this.deadKey = true;
  275. return;
  276. }
  277. // Printable characters
  278. if (key &&
  279. ((keyCode === -1) || // Direct access
  280. (keyCode === 32) || // Space
  281. (keyCode > 47 && keyCode < 58) || // Numbers
  282. (keyCode > 64 && keyCode < 91) || // Letters
  283. (keyCode > 185 && keyCode < 193) || // Special characters
  284. (keyCode > 218 && keyCode < 223) || // Special characters
  285. (keyCode > 95 && keyCode < 112))) { // Numpad
  286. this._currentKey = key;
  287. this.onBeforeKeyAddObservable.notifyObservers(this);
  288. key = this._currentKey;
  289. if (this._addKey) {
  290. if (this._cursorOffset === 0) {
  291. this.text += key;
  292. } else {
  293. let insertPosition = this._text.length - this._cursorOffset;
  294. this.text = this._text.slice(0, insertPosition) + key + this._text.slice(insertPosition);
  295. }
  296. }
  297. }
  298. }
  299. /** @hidden */
  300. public processKeyboard(evt: KeyboardEvent): void {
  301. this.processKey(evt.keyCode, evt.key);
  302. }
  303. public _draw(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  304. context.save();
  305. this._applyStates(context);
  306. if (this._processMeasures(parentMeasure, context)) {
  307. if (this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY) {
  308. context.shadowColor = this.shadowColor;
  309. context.shadowBlur = this.shadowBlur;
  310. context.shadowOffsetX = this.shadowOffsetX;
  311. context.shadowOffsetY = this.shadowOffsetY;
  312. }
  313. // Background
  314. if (this._isFocused) {
  315. if (this._focusedBackground) {
  316. context.fillStyle = this._focusedBackground;
  317. context.fillRect(this._currentMeasure.left, this._currentMeasure.top, this._currentMeasure.width, this._currentMeasure.height);
  318. }
  319. } else if (this._background) {
  320. context.fillStyle = this._background;
  321. context.fillRect(this._currentMeasure.left, this._currentMeasure.top, this._currentMeasure.width, this._currentMeasure.height);
  322. }
  323. if (this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY) {
  324. context.shadowBlur = 0;
  325. context.shadowOffsetX = 0;
  326. context.shadowOffsetY = 0;
  327. }
  328. if (!this._fontOffset) {
  329. this._fontOffset = Control._GetFontOffset(context.font);
  330. }
  331. // Text
  332. let clipTextLeft = this._currentMeasure.left + this._margin.getValueInPixel(this._host, parentMeasure.width);
  333. if (this.color) {
  334. context.fillStyle = this.color;
  335. }
  336. let text = this._beforeRenderText(this._text);
  337. if (!this._isFocused && !this._text && this._placeholderText) {
  338. text = this._placeholderText;
  339. if (this._placeholderColor) {
  340. context.fillStyle = this._placeholderColor;
  341. }
  342. }
  343. this._textWidth = context.measureText(text).width;
  344. let marginWidth = this._margin.getValueInPixel(this._host, parentMeasure.width) * 2;
  345. if (this._autoStretchWidth) {
  346. this.width = Math.min(this._maxWidth.getValueInPixel(this._host, parentMeasure.width), this._textWidth + marginWidth) + "px";
  347. }
  348. let rootY = this._fontOffset.ascent + (this._currentMeasure.height - this._fontOffset.height) / 2;
  349. let availableWidth = this._width.getValueInPixel(this._host, parentMeasure.width) - marginWidth;
  350. context.save();
  351. context.beginPath();
  352. context.rect(clipTextLeft, this._currentMeasure.top + (this._currentMeasure.height - this._fontOffset.height) / 2, availableWidth + 2, this._currentMeasure.height);
  353. context.clip();
  354. if (this._isFocused && this._textWidth > availableWidth) {
  355. let textLeft = clipTextLeft - this._textWidth + availableWidth;
  356. if (!this._scrollLeft) {
  357. this._scrollLeft = textLeft;
  358. }
  359. } else {
  360. this._scrollLeft = clipTextLeft;
  361. }
  362. context.fillText(text, this._scrollLeft, this._currentMeasure.top + rootY);
  363. // Cursor
  364. if (this._isFocused) {
  365. // Need to move cursor
  366. if (this._clickedCoordinate) {
  367. var rightPosition = this._scrollLeft + this._textWidth;
  368. var absoluteCursorPosition = rightPosition - this._clickedCoordinate;
  369. var currentSize = 0;
  370. this._cursorOffset = 0;
  371. var previousDist = 0;
  372. do {
  373. if (this._cursorOffset) {
  374. previousDist = Math.abs(absoluteCursorPosition - currentSize);
  375. }
  376. this._cursorOffset++;
  377. currentSize = context.measureText(text.substr(text.length - this._cursorOffset, this._cursorOffset)).width;
  378. } while (currentSize < absoluteCursorPosition && (text.length >= this._cursorOffset));
  379. // Find closest move
  380. if (Math.abs(absoluteCursorPosition - currentSize) > previousDist) {
  381. this._cursorOffset--;
  382. }
  383. this._blinkIsEven = false;
  384. this._clickedCoordinate = null;
  385. }
  386. // Render cursor
  387. if (!this._blinkIsEven) {
  388. let cursorOffsetText = this.text.substr(this._text.length - this._cursorOffset);
  389. let cursorOffsetWidth = context.measureText(cursorOffsetText).width;
  390. let cursorLeft = this._scrollLeft + this._textWidth - cursorOffsetWidth;
  391. if (cursorLeft < clipTextLeft) {
  392. this._scrollLeft += (clipTextLeft - cursorLeft);
  393. cursorLeft = clipTextLeft;
  394. this._markAsDirty();
  395. } else if (cursorLeft > clipTextLeft + availableWidth) {
  396. this._scrollLeft += (clipTextLeft + availableWidth - cursorLeft);
  397. cursorLeft = clipTextLeft + availableWidth;
  398. this._markAsDirty();
  399. }
  400. context.fillRect(cursorLeft, this._currentMeasure.top + (this._currentMeasure.height - this._fontOffset.height) / 2, 2, this._fontOffset.height);
  401. }
  402. clearTimeout(this._blinkTimeout);
  403. this._blinkTimeout = setTimeout(() => {
  404. this._blinkIsEven = !this._blinkIsEven;
  405. this._markAsDirty();
  406. }, 500);
  407. }
  408. context.restore();
  409. // Border
  410. if (this._thickness) {
  411. if (this.color) {
  412. context.strokeStyle = this.color;
  413. }
  414. context.lineWidth = this._thickness;
  415. context.strokeRect(this._currentMeasure.left + this._thickness / 2, this._currentMeasure.top + this._thickness / 2,
  416. this._currentMeasure.width - this._thickness, this._currentMeasure.height - this._thickness);
  417. }
  418. }
  419. context.restore();
  420. }
  421. public _onPointerDown(target: Control, coordinates: Vector2, pointerId: number, buttonIndex: number): boolean {
  422. if (!super._onPointerDown(target, coordinates, pointerId, buttonIndex)) {
  423. return false;
  424. }
  425. this._clickedCoordinate = coordinates.x;
  426. if (this._host.focusedControl === this) {
  427. // Move cursor
  428. clearTimeout(this._blinkTimeout);
  429. this._markAsDirty();
  430. return true;
  431. }
  432. this._host.focusedControl = this;
  433. return true;
  434. }
  435. public _onPointerUp(target: Control, coordinates: Vector2, pointerId: number, buttonIndex: number, notifyClick: boolean): void {
  436. super._onPointerUp(target, coordinates, pointerId, buttonIndex, notifyClick);
  437. }
  438. protected _beforeRenderText(text: string): string {
  439. return text;
  440. }
  441. public dispose() {
  442. super.dispose();
  443. this.onBlurObservable.clear();
  444. this.onFocusObservable.clear();
  445. this.onTextChangedObservable.clear();
  446. }
  447. }
  448. }