inputText.ts 17 KB

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