button.ts 7.1 KB

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