button.ts 7.9 KB

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