inputText.ts 14 KB

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