virtualKeyboard.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON.GUI {
  3. export class KeyPropertySet {
  4. width?: string;
  5. height?: string;
  6. paddingLeft?: string;
  7. paddingRight?: string;
  8. paddingTop?: string;
  9. paddingBottom?: string;
  10. color?: string;
  11. background?: string;
  12. }
  13. export class VirtualKeyboard extends StackPanel {
  14. public onKeyPressObservable = new Observable<string>();
  15. public defaultButtonWidth = "40px";
  16. public defaultButtonHeight = "40px";
  17. public defaultButtonPaddingLeft= "2px";
  18. public defaultButtonPaddingRight = "2px";
  19. public defaultButtonPaddingTop = "2px";
  20. public defaultButtonPaddingBottom = "2px";
  21. public defaultButtonColor = "#DDD";
  22. public defaultButtonBackground = "#070707";
  23. protected _getTypeName(): string {
  24. return "VirtualKeyboard";
  25. }
  26. private _createKey(key: string, propertySet: Nullable<KeyPropertySet>) {
  27. var button = Button.CreateSimpleButton(key, key);
  28. button.width = propertySet && propertySet.width ? propertySet.width : this.defaultButtonWidth;
  29. button.height = propertySet && propertySet.height ? propertySet.height : this.defaultButtonHeight;
  30. button.color = propertySet && propertySet.color ? propertySet.color : this.defaultButtonColor;
  31. button.background = propertySet && propertySet.background ? propertySet.background : this.defaultButtonBackground;
  32. button.paddingLeft = propertySet && propertySet.paddingLeft ? propertySet.paddingLeft : this.defaultButtonPaddingLeft;
  33. button.paddingRight = propertySet && propertySet.paddingRight ? propertySet.paddingRight : this.defaultButtonPaddingRight;
  34. button.paddingTop = propertySet && propertySet.paddingTop ? propertySet.paddingTop : this.defaultButtonPaddingTop;
  35. button.paddingBottom = propertySet && propertySet.paddingBottom ? propertySet.paddingBottom : this.defaultButtonPaddingBottom;
  36. button.thickness = 0;
  37. button.isFocusInvisible = true;
  38. button.shadowColor = this.shadowColor;
  39. button.shadowBlur = this.shadowBlur;
  40. button.shadowOffsetX = this.shadowOffsetX;
  41. button.shadowOffsetY = this.shadowOffsetY;
  42. button.onPointerUpObservable.add(() => {
  43. this.onKeyPressObservable.notifyObservers(key);
  44. });
  45. return button;
  46. }
  47. public addKeysRow(keys: Array<string>, propertySets?: Array<KeyPropertySet>): void {
  48. let panel = new StackPanel();
  49. panel.isVertical = false;
  50. panel.isFocusInvisible = true;
  51. for(var i = 0; i < keys.length; i++) {
  52. let properties = null;
  53. if (propertySets && propertySets.length === keys.length) {
  54. properties = propertySets[i];
  55. }
  56. panel.addControl(this._createKey(keys[i], properties));
  57. }
  58. this.addControl(panel);
  59. }
  60. private _connectedInputText: Nullable<InputText>;
  61. private _onFocusObserver: Nullable<Observer<InputText>>;
  62. private _onBlurObserver: Nullable<Observer<InputText>>;
  63. private _onKeyPressObserver: Nullable<Observer<string>>;
  64. public get connectedInputText(): Nullable<InputText> {
  65. return this._connectedInputText;
  66. }
  67. public connect(input: InputText): void {
  68. this.isVisible = false;
  69. this._connectedInputText = input;
  70. // Events hooking
  71. this._onFocusObserver = input.onFocusObservable.add(() => {
  72. this.isVisible = true;
  73. });
  74. this._onBlurObserver = input.onBlurObservable.add(() => {
  75. this.isVisible = false;
  76. });
  77. this._onKeyPressObserver = this.onKeyPressObservable.add((key) => {
  78. if (!this._connectedInputText) {
  79. return;
  80. }
  81. switch (key) {
  82. case "\u2190":
  83. this._connectedInputText.processKey(8);
  84. return;
  85. case "\u21B5":
  86. this._connectedInputText.processKey(13);
  87. return;
  88. }
  89. this._connectedInputText.processKey(-1, key);
  90. });
  91. }
  92. public disconnect(): void {
  93. if (!this._connectedInputText) {
  94. return;
  95. }
  96. this._connectedInputText.onFocusObservable.remove(this._onFocusObserver);
  97. this._connectedInputText.onBlurObservable.remove(this._onBlurObserver);
  98. this.onKeyPressObservable.remove(this._onKeyPressObserver);
  99. this._connectedInputText = null;
  100. }
  101. // Statics
  102. public static CreateDefaultLayout(): VirtualKeyboard {
  103. let returnValue = new VirtualKeyboard();
  104. returnValue.addKeysRow(["1", "2", "3", "4", "5", "6", "7", "8", "9", "0","\u2190"]);
  105. returnValue.addKeysRow(["q", "w", "e", "r", "t", "y", "u", "i", "o", "p"]);
  106. returnValue.addKeysRow(["a", "s", "d", "f", "g", "h", "j", "k", "l",";","'","\u21B5"]);
  107. returnValue.addKeysRow(["z", "x", "c", "v", "b", "n", "m", ",", ".", "/"]);
  108. returnValue.addKeysRow([" "], [{ width: "200px"}]);
  109. return returnValue;
  110. }
  111. }
  112. }