inputText.ts 19 KB

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