button.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON.GUI {
  3. export class Button extends Rectangle {
  4. public pointerEnterAnimation: () => void;
  5. public pointerOutAnimation: () => void;
  6. public pointerDownAnimation: () => void;
  7. public pointerUpAnimation: () => void;
  8. constructor(public name?: string) {
  9. super(name);
  10. this.thickness = 1;
  11. this.isPointerBlocker = true;
  12. this.pointerEnterAnimation = () => {
  13. this.alpha -= 0.1;
  14. }
  15. this.pointerOutAnimation = () => {
  16. this.alpha += 0.1;
  17. }
  18. this.pointerDownAnimation = () => {
  19. this.scaleX -= 0.05;
  20. this.scaleY -= 0.05;
  21. }
  22. this.pointerUpAnimation = () => {
  23. this.scaleX += 0.05;
  24. this.scaleY += 0.05;
  25. }
  26. }
  27. protected _getTypeName(): string {
  28. return "Button";
  29. }
  30. // While being a container, the button behaves like a control.
  31. public _processPicking(x: number, y: number, type: number, pointerId:number, buttonIndex: number): boolean {
  32. if (!this.isHitTestVisible || !this.isVisible || this.notRenderable) {
  33. return false;
  34. }
  35. if (!super.contains(x, y)) {
  36. return false;
  37. }
  38. this._processObservables(type, x, y, pointerId, buttonIndex);
  39. return true;
  40. }
  41. public _onPointerEnter(target: Control): boolean {
  42. if (!super._onPointerEnter(target)) {
  43. return false;
  44. }
  45. if (this.pointerEnterAnimation) {
  46. this.pointerEnterAnimation();
  47. }
  48. return true;
  49. }
  50. public _onPointerOut(target: Control): void {
  51. if (this.pointerOutAnimation) {
  52. this.pointerOutAnimation();
  53. }
  54. super._onPointerOut(target);
  55. }
  56. public _onPointerDown(target: Control, coordinates: Vector2, pointerId:number, buttonIndex: number): boolean {
  57. if (!super._onPointerDown(target, coordinates, pointerId, buttonIndex)) {
  58. return false;
  59. }
  60. if (this.pointerDownAnimation) {
  61. this.pointerDownAnimation();
  62. }
  63. return true;
  64. }
  65. public _onPointerUp(target: Control, coordinates: Vector2, pointerId:number, buttonIndex: number, notifyClick: boolean): void {
  66. if (this.pointerUpAnimation) {
  67. this.pointerUpAnimation();
  68. }
  69. super._onPointerUp(target, coordinates, pointerId, buttonIndex, notifyClick);
  70. }
  71. // Statics
  72. public static CreateImageButton(name: string, text: string, imageUrl: string): Button {
  73. var result = new Button(name);
  74. // Adding text
  75. var textBlock = new BABYLON.GUI.TextBlock(name + "_button", text);
  76. textBlock.textWrapping = true;
  77. textBlock.textHorizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_CENTER;
  78. textBlock.paddingLeft = "20%";
  79. result.addControl(textBlock);
  80. // Adding image
  81. var iconImage = new BABYLON.GUI.Image(name + "_icon", imageUrl);
  82. iconImage.width = "20%";
  83. iconImage.stretch = BABYLON.GUI.Image.STRETCH_UNIFORM;
  84. iconImage.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
  85. result.addControl(iconImage);
  86. return result;
  87. }
  88. public static CreateImageOnlyButton(name: string, imageUrl: string): Button {
  89. var result = new Button(name);
  90. // Adding image
  91. var iconImage = new BABYLON.GUI.Image(name + "_icon", imageUrl);
  92. iconImage.stretch = BABYLON.GUI.Image.STRETCH_FILL;
  93. iconImage.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
  94. result.addControl(iconImage);
  95. return result;
  96. }
  97. public static CreateSimpleButton(name: string, text: string): Button {
  98. var result = new Button(name);
  99. // Adding text
  100. var textBlock = new BABYLON.GUI.TextBlock(name + "_button", text);
  101. textBlock.textWrapping = true;
  102. textBlock.textHorizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_CENTER;
  103. result.addControl(textBlock);
  104. return result;
  105. }
  106. public static CreateImageWithCenterTextButton(name: string, text: string, imageUrl: string): Button {
  107. var result = new Button(name);
  108. // Adding image
  109. var iconImage = new BABYLON.GUI.Image(name + "_icon", imageUrl);
  110. iconImage.stretch = BABYLON.GUI.Image.STRETCH_FILL;
  111. result.addControl(iconImage);
  112. // Adding text
  113. var textBlock = new BABYLON.GUI.TextBlock(name + "_button", text);
  114. textBlock.textWrapping = true;
  115. textBlock.textHorizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_CENTER;
  116. result.addControl(textBlock);
  117. return result;
  118. }
  119. }
  120. }