button.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON.GUI {
  3. /**
  4. * Class used to create 2D buttons
  5. */
  6. export class Button extends Rectangle {
  7. /**
  8. * Function called to generate a pointer enter animation
  9. */
  10. public pointerEnterAnimation: () => void;
  11. /**
  12. * Function called to generate a pointer out animation
  13. */
  14. public pointerOutAnimation: () => void;
  15. /**
  16. * Function called to generate a pointer down animation
  17. */
  18. public pointerDownAnimation: () => void;
  19. /**
  20. * Function called to generate a pointer up animation
  21. */
  22. public pointerUpAnimation: () => void;
  23. /**
  24. * Creates a new Button
  25. * @param name defines the name of the button
  26. */
  27. constructor(public name?: string) {
  28. super(name);
  29. this.thickness = 1;
  30. this.isPointerBlocker = true;
  31. this.pointerEnterAnimation = () => {
  32. this.alpha -= 0.1;
  33. }
  34. this.pointerOutAnimation = () => {
  35. this.alpha += 0.1;
  36. }
  37. this.pointerDownAnimation = () => {
  38. this.scaleX -= 0.05;
  39. this.scaleY -= 0.05;
  40. }
  41. this.pointerUpAnimation = () => {
  42. this.scaleX += 0.05;
  43. this.scaleY += 0.05;
  44. }
  45. }
  46. protected _getTypeName(): string {
  47. return "Button";
  48. }
  49. // While being a container, the button behaves like a control.
  50. /** @hidden */
  51. public _processPicking(x: number, y: number, type: number, pointerId:number, buttonIndex: number): boolean {
  52. if (!this.isHitTestVisible || !this.isVisible || this.notRenderable) {
  53. return false;
  54. }
  55. if (!super.contains(x, y)) {
  56. return false;
  57. }
  58. this._processObservables(type, x, y, pointerId, buttonIndex);
  59. return true;
  60. }
  61. /** @hidden */
  62. public _onPointerEnter(target: Control): boolean {
  63. if (!super._onPointerEnter(target)) {
  64. return false;
  65. }
  66. if (this.pointerEnterAnimation) {
  67. this.pointerEnterAnimation();
  68. }
  69. return true;
  70. }
  71. /** @hidden */
  72. public _onPointerOut(target: Control): void {
  73. if (this.pointerOutAnimation) {
  74. this.pointerOutAnimation();
  75. }
  76. super._onPointerOut(target);
  77. }
  78. /** @hidden */
  79. public _onPointerDown(target: Control, coordinates: Vector2, pointerId:number, buttonIndex: number): boolean {
  80. if (!super._onPointerDown(target, coordinates, pointerId, buttonIndex)) {
  81. return false;
  82. }
  83. if (this.pointerDownAnimation) {
  84. this.pointerDownAnimation();
  85. }
  86. return true;
  87. }
  88. /** @hidden */
  89. public _onPointerUp(target: Control, coordinates: Vector2, pointerId:number, buttonIndex: number, notifyClick: boolean): void {
  90. if (this.pointerUpAnimation) {
  91. this.pointerUpAnimation();
  92. }
  93. super._onPointerUp(target, coordinates, pointerId, buttonIndex, notifyClick);
  94. }
  95. // Statics
  96. /**
  97. * Creates a new button made with an image and a text
  98. * @param name defines the name of the button
  99. * @param text defines the text of the button
  100. * @param imageUrl defines the url of the image
  101. * @returns a new Button
  102. */
  103. public static CreateImageButton(name: string, text: string, imageUrl: string): Button {
  104. var result = new Button(name);
  105. // Adding text
  106. var textBlock = new BABYLON.GUI.TextBlock(name + "_button", text);
  107. textBlock.textWrapping = true;
  108. textBlock.textHorizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_CENTER;
  109. textBlock.paddingLeft = "20%";
  110. result.addControl(textBlock);
  111. // Adding image
  112. var iconImage = new BABYLON.GUI.Image(name + "_icon", imageUrl);
  113. iconImage.width = "20%";
  114. iconImage.stretch = BABYLON.GUI.Image.STRETCH_UNIFORM;
  115. iconImage.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
  116. result.addControl(iconImage);
  117. return result;
  118. }
  119. /**
  120. * Creates a new button made with an image
  121. * @param name defines the name of the button
  122. * @param imageUrl defines the url of the image
  123. * @returns a new Button
  124. */
  125. public static CreateImageOnlyButton(name: string, imageUrl: string): Button {
  126. var result = new Button(name);
  127. // Adding image
  128. var iconImage = new BABYLON.GUI.Image(name + "_icon", imageUrl);
  129. iconImage.stretch = BABYLON.GUI.Image.STRETCH_FILL;
  130. iconImage.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
  131. result.addControl(iconImage);
  132. return result;
  133. }
  134. /**
  135. * Creates a new button made with a text
  136. * @param name defines the name of the button
  137. * @param text defines the text of the button
  138. * @returns a new Button
  139. */
  140. public static CreateSimpleButton(name: string, text: string): Button {
  141. var result = new Button(name);
  142. // Adding text
  143. var textBlock = new BABYLON.GUI.TextBlock(name + "_button", text);
  144. textBlock.textWrapping = true;
  145. textBlock.textHorizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_CENTER;
  146. result.addControl(textBlock);
  147. return result;
  148. }
  149. /**
  150. * Creates a new button made with an image and a centered text
  151. * @param name defines the name of the button
  152. * @param text defines the text of the button
  153. * @param imageUrl defines the url of the image
  154. * @returns a new Button
  155. */
  156. public static CreateImageWithCenterTextButton(name: string, text: string, imageUrl: string): Button {
  157. var result = new Button(name);
  158. // Adding image
  159. var iconImage = new BABYLON.GUI.Image(name + "_icon", imageUrl);
  160. iconImage.stretch = BABYLON.GUI.Image.STRETCH_FILL;
  161. result.addControl(iconImage);
  162. // Adding text
  163. var textBlock = new BABYLON.GUI.TextBlock(name + "_button", text);
  164. textBlock.textWrapping = true;
  165. textBlock.textHorizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_CENTER;
  166. result.addControl(textBlock);
  167. return result;
  168. }
  169. }
  170. }