button.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. // While being a container, the button behaves like a control.
  28. public _processPicking(x: number, y: number, type: number): boolean {
  29. if (!this.contains(x, y)) {
  30. return false;
  31. }
  32. this._processObservables(type);
  33. return true;
  34. }
  35. protected _onPointerEnter(): void {
  36. if (this.pointerEnterAnimation) {
  37. this.pointerEnterAnimation();
  38. }
  39. super._onPointerEnter();
  40. }
  41. protected _onPointerOut(): void {
  42. if (this.pointerOutAnimation) {
  43. this.pointerOutAnimation();
  44. }
  45. super._onPointerOut();
  46. }
  47. protected _onPointerDown(): void {
  48. if (this.pointerDownAnimation) {
  49. this.pointerDownAnimation();
  50. }
  51. super._onPointerDown();
  52. }
  53. protected _onPointerUp (): void {
  54. if (this.pointerUpAnimation) {
  55. this.pointerUpAnimation();
  56. }
  57. super._onPointerUp();
  58. }
  59. // Statics
  60. public static CreateImageButton(name: string, text: string, imageUrl: string): Button {
  61. var result = new Button(name);
  62. // Adding text
  63. var textBlock = new BABYLON.GUI.TextBlock(name + "_button", text);
  64. textBlock.textWrapping = true;
  65. textBlock.textHorizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_CENTER;
  66. textBlock.marginLeft = "20%";
  67. result.addControl(textBlock);
  68. // Adding image
  69. var iconImage = new BABYLON.GUI.Image(name + "_icon", imageUrl);
  70. iconImage.width = "20%";
  71. iconImage.stretch = BABYLON.GUI.Image.STRETCH_UNIFORM;
  72. iconImage.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
  73. result.addControl(iconImage);
  74. return result;
  75. }
  76. public static CreateImageOnlyButton(name: string, imageUrl: string): Button {
  77. var result = new Button(name);
  78. // Adding image
  79. var iconImage = new BABYLON.GUI.Image(name + "_icon", imageUrl);
  80. iconImage.stretch = BABYLON.GUI.Image.STRETCH_FILL;
  81. iconImage.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
  82. result.addControl(iconImage);
  83. return result;
  84. }
  85. public static CreateSimpleButton(name: string, text: string): Button {
  86. var result = new Button(name);
  87. // Adding text
  88. var textBlock = new BABYLON.GUI.TextBlock(name + "_button", text);
  89. textBlock.textWrapping = true;
  90. textBlock.textHorizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_CENTER;
  91. result.addControl(textBlock);
  92. return result;
  93. }
  94. }
  95. }