inputText.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. constructor(public name?: string, text: string = "") {
  124. super(name);
  125. this.text = text;
  126. }
  127. public onBlur(): void {
  128. this._isFocused = false;
  129. this._scrollLeft = null;
  130. this._cursorOffset = 0;
  131. clearTimeout(this._blinkTimeout);
  132. this._markAsDirty();
  133. this.onBlurObservable.notifyObservers(this);
  134. }
  135. public onFocus(): void {
  136. this._scrollLeft = null;
  137. this._isFocused = true;
  138. this._blinkIsEven = false;
  139. this._cursorOffset = 0;
  140. this._markAsDirty();
  141. this.onFocusObservable.notifyObservers(this);
  142. if (navigator.userAgent.indexOf("Mobile") !== -1) {
  143. let value = prompt(this.promptMessage);
  144. if (value !== null) {
  145. this.text = value;
  146. }
  147. this._host.focusedControl = null;
  148. return;
  149. }
  150. }
  151. protected _getTypeName(): string {
  152. return "InputText";
  153. }
  154. public processKey(keyCode: number, key?: string) {
  155. // Specific cases
  156. switch (keyCode) {
  157. case 8: // BACKSPACE
  158. if (this._text && this._text.length > 0) {
  159. if (this._cursorOffset === 0) {
  160. this.text = this._text.substr(0, this._text.length - 1);
  161. } else {
  162. let deletePosition = this._text.length - this._cursorOffset;
  163. if (deletePosition > 0) {
  164. this.text = this._text.slice(0, deletePosition - 1) + this._text.slice(deletePosition);
  165. }
  166. }
  167. }
  168. return;
  169. case 46: // DELETE
  170. if (this._text && this._text.length > 0) {
  171. let deletePosition = this._text.length - this._cursorOffset;
  172. this.text = this._text.slice(0, deletePosition) + this._text.slice(deletePosition + 1);
  173. this._cursorOffset--;
  174. }
  175. return;
  176. case 13: // RETURN
  177. this._host.focusedControl = null;
  178. return;
  179. case 35: // END
  180. this._cursorOffset = 0;
  181. this._blinkIsEven = false;
  182. this._markAsDirty();
  183. return;
  184. case 36: // HOME
  185. this._cursorOffset = this._text.length;
  186. this._blinkIsEven = false;
  187. this._markAsDirty();
  188. return;
  189. case 37: // LEFT
  190. this._cursorOffset++;
  191. if (this._cursorOffset > this._text.length) {
  192. this._cursorOffset = this._text.length;
  193. }
  194. this._blinkIsEven = false;
  195. this._markAsDirty();
  196. return;
  197. case 39: // RIGHT
  198. this._cursorOffset--;
  199. if (this._cursorOffset < 0) {
  200. this._cursorOffset = 0;
  201. }
  202. this._blinkIsEven = false;
  203. this._markAsDirty();
  204. return;
  205. }
  206. // Printable characters
  207. if (
  208. (keyCode === -1) || // Direct access
  209. (keyCode === 32) || // Space
  210. (keyCode > 47 && keyCode < 58) || // Numbers
  211. (keyCode > 64 && keyCode < 91) || // Letters
  212. (keyCode > 185 && keyCode < 193) || // Special characters
  213. (keyCode > 218 && keyCode < 223) || // Special characters
  214. (keyCode > 95 && keyCode < 112)) { // Numpad
  215. if (this._cursorOffset === 0) {
  216. this.text += key;
  217. } else {
  218. let insertPosition = this._text.length - this._cursorOffset;
  219. this.text = this._text.slice(0, insertPosition) + key + this._text.slice(insertPosition);
  220. }
  221. }
  222. }
  223. public processKeyboard(evt: KeyboardEvent): void {
  224. this.processKey(evt.keyCode, evt.key);
  225. }
  226. public _draw(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  227. context.save();
  228. this._applyStates(context);
  229. if (this._processMeasures(parentMeasure, context)) {
  230. if(this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY){
  231. context.shadowColor = this.shadowColor;
  232. context.shadowBlur = this.shadowBlur;
  233. context.shadowOffsetX = this.shadowOffsetX;
  234. context.shadowOffsetY = this.shadowOffsetY;
  235. }
  236. // Background
  237. if (this._isFocused) {
  238. if (this._focusedBackground) {
  239. context.fillStyle = this._focusedBackground;
  240. context.fillRect(this._currentMeasure.left, this._currentMeasure.top, this._currentMeasure.width, this._currentMeasure.height);
  241. }
  242. } else if (this._background) {
  243. context.fillStyle = this._background;
  244. context.fillRect(this._currentMeasure.left, this._currentMeasure.top, this._currentMeasure.width, this._currentMeasure.height);
  245. }
  246. if(this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY){
  247. context.shadowBlur = 0;
  248. context.shadowOffsetX = 0;
  249. context.shadowOffsetY = 0;
  250. }
  251. if (!this._fontOffset) {
  252. this._fontOffset = Control._GetFontOffset(context.font);
  253. }
  254. // Text
  255. let clipTextLeft = this._currentMeasure.left + this._margin.getValueInPixel(this._host, parentMeasure.width);
  256. if (this.color) {
  257. context.fillStyle = this.color;
  258. }
  259. let text = this._text;
  260. if (!this._isFocused && !this._text && this._placeholderText) {
  261. text = this._placeholderText;
  262. if (this._placeholderColor) {
  263. context.fillStyle = this._placeholderColor;
  264. }
  265. }
  266. this._textWidth = context.measureText(text).width;
  267. let marginWidth = this._margin.getValueInPixel(this._host, parentMeasure.width) * 2;
  268. if (this._autoStretchWidth) {
  269. this.width = Math.min(this._maxWidth.getValueInPixel(this._host, parentMeasure.width), this._textWidth + marginWidth) + "px";
  270. }
  271. let rootY = this._fontOffset.ascent + (this._currentMeasure.height - this._fontOffset.height) / 2;
  272. let availableWidth = this._width.getValueInPixel(this._host, parentMeasure.width) - marginWidth;
  273. context.save();
  274. context.beginPath();
  275. context.rect(clipTextLeft, this._currentMeasure.top + (this._currentMeasure.height - this._fontOffset.height) / 2, availableWidth + 2, this._currentMeasure.height);
  276. context.clip();
  277. if (this._isFocused && this._textWidth > availableWidth) {
  278. let textLeft = clipTextLeft - this._textWidth + availableWidth;
  279. if (!this._scrollLeft) {
  280. this._scrollLeft = textLeft;
  281. }
  282. } else {
  283. this._scrollLeft = clipTextLeft;
  284. }
  285. context.fillText(text, this._scrollLeft, this._currentMeasure.top + rootY);
  286. // Cursor
  287. if (this._isFocused) {
  288. // Need to move cursor
  289. if (this._clickedCoordinate) {
  290. var rightPosition = this._scrollLeft + this._textWidth;
  291. var absoluteCursorPosition = rightPosition - this._clickedCoordinate;
  292. var currentSize = 0;
  293. this._cursorOffset = 0;
  294. var previousDist = 0;
  295. do {
  296. if (this._cursorOffset) {
  297. previousDist = Math.abs(absoluteCursorPosition - currentSize);
  298. }
  299. this._cursorOffset++;
  300. currentSize = context.measureText(text.substr(text.length - this._cursorOffset, this._cursorOffset)).width;
  301. } while(currentSize < absoluteCursorPosition);
  302. // Find closest move
  303. if (Math.abs(absoluteCursorPosition - currentSize) > previousDist) {
  304. this._cursorOffset--;
  305. }
  306. this._blinkIsEven = false;
  307. this._clickedCoordinate = null;
  308. }
  309. // Render cursor
  310. if (!this._blinkIsEven) {
  311. let cursorOffsetText = this.text.substr(this._text.length - this._cursorOffset);
  312. let cursorOffsetWidth = context.measureText(cursorOffsetText).width;
  313. let cursorLeft = this._scrollLeft + this._textWidth - cursorOffsetWidth;
  314. if (cursorLeft < clipTextLeft) {
  315. this._scrollLeft += (clipTextLeft - cursorLeft);
  316. cursorLeft = clipTextLeft;
  317. this._markAsDirty();
  318. } else if (cursorLeft > clipTextLeft + availableWidth) {
  319. this._scrollLeft += (clipTextLeft + availableWidth - cursorLeft);
  320. cursorLeft = clipTextLeft + availableWidth;
  321. this._markAsDirty();
  322. }
  323. context.fillRect(cursorLeft, this._currentMeasure.top + (this._currentMeasure.height - this._fontOffset.height) / 2, 2, this._fontOffset.height);
  324. }
  325. clearTimeout(this._blinkTimeout);
  326. this._blinkTimeout = setTimeout(() => {
  327. this._blinkIsEven = !this._blinkIsEven;
  328. this._markAsDirty();
  329. }, 500);
  330. }
  331. context.restore();
  332. // Border
  333. if (this._thickness) {
  334. if (this.color) {
  335. context.strokeStyle = this.color;
  336. }
  337. context.lineWidth = this._thickness;
  338. context.strokeRect(this._currentMeasure.left + this._thickness / 2, this._currentMeasure.top + this._thickness / 2,
  339. this._currentMeasure.width - this._thickness, this._currentMeasure.height - this._thickness);
  340. }
  341. }
  342. context.restore();
  343. }
  344. public _onPointerDown(target: Control, coordinates: Vector2, buttonIndex: number): boolean {
  345. if (!super._onPointerDown(target, coordinates, buttonIndex)) {
  346. return false;
  347. }
  348. this._clickedCoordinate = coordinates.x;
  349. if (this._host.focusedControl === this) {
  350. // Move cursor
  351. clearTimeout(this._blinkTimeout);
  352. this._markAsDirty();
  353. return true;
  354. }
  355. this._host.focusedControl = this;
  356. return true;
  357. }
  358. public _onPointerUp(target: Control, coordinates: Vector2, buttonIndex: number): void {
  359. super._onPointerUp(target, coordinates, buttonIndex);
  360. }
  361. public dispose() {
  362. super.dispose();
  363. this.onBlurObservable.clear();
  364. this.onFocusObservable.clear();
  365. this.onTextChangedObservable.clear();
  366. }
  367. }
  368. }