inputText.ts 19 KB

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