DPad.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. function DPad() {
  2. var width = Atomic.graphics.width;
  3. var height = Atomic.graphics.height;
  4. //create a new view for ours dpad
  5. this.view = new Atomic.UIView();
  6. //horizontal buttons sizeX, sizeY
  7. var horizButtonsSize = [75, 61];
  8. var verticButtonsSize = [61, 75];
  9. //init function should be called adding vertical/horizontal buttons
  10. //it's like we are commiting ours buttons
  11. this.init = function() {
  12. //if touch buttons skin is not loaded
  13. if(!DPad.skinLoaded) {
  14. //load skin
  15. Atomic.ui.loadSkin("Ui/touchButtonsSkin.ui");
  16. DPad.skinLoaded = true;
  17. }
  18. //sets its position by default
  19. this.view.setPosition(-width/8, height/10);
  20. //create a dpad layout
  21. this.dpad = new Atomic.UILayout();
  22. this.dpad.rect = this.view.rect;
  23. //if layouts are exists, add them
  24. if(this.leftLayout)
  25. this.dpad.addChild(this.leftLayout);
  26. if(this.upDownLayout)
  27. this.dpad.addChild(this.upDownLayout);
  28. if(this.rightLayout)
  29. this.dpad.addChild(this.rightLayout);
  30. //ok, add ours dpad to the view
  31. this.view.addChild(this.dpad);
  32. }
  33. //adds horizontal and vertical buttons
  34. this.addAll = function() {
  35. //adds horizontal buttons
  36. this.addHorizontal();
  37. //adds vertical buttons
  38. this.addVertical();
  39. }
  40. //adds horizontal buttons
  41. this.addHorizontal = function() {
  42. //if layout params doesn't exist create a new one
  43. if(!this.layoutParamsLeftRight) this.initLeftRightLayoutParams();
  44. //new layout for left button
  45. this.leftLayout = new Atomic.UILayout();
  46. this.leftLayout.rect = this.view.rect;
  47. //create a left button
  48. this.leftButton = new Atomic.UIButton();
  49. this.leftButton.skinBg = "TouchButtonLeft";
  50. this.leftButton.layoutParams = this.layoutParamsLeftRight;
  51. this.leftLayout.addChild(this.leftButton);
  52. //new layout for right button
  53. this.rightLayout = new Atomic.UILayout();
  54. this.rightLayout.rect = this.view.rect;
  55. //create a right button
  56. this.rightButton = new Atomic.UIButton();
  57. this.rightButton.skinBg = "TouchButtonRight";
  58. this.rightButton.layoutParams = this.layoutParamsLeftRight;
  59. this.rightLayout.addChild(this.rightButton);
  60. this.rightLayout.layoutSize = Atomic.UI_LAYOUT_SIZE_PREFERRED;
  61. this.leftLayout.layoutSize = Atomic.UI_LAYOUT_SIZE_PREFERRED;
  62. //it makes ours buttons uncaptured, this is used for the multiTouch, to don't `concentrate` only on one button
  63. this.leftButton.setCapturing(false);
  64. this.rightButton.setCapturing(false);
  65. //bind our ui button to the specified Keyboard Key
  66. Atomic.input.bindButton(this.rightButton, Atomic.KEY_RIGHT);
  67. Atomic.input.bindButton(this.leftButton, Atomic.KEY_LEFT);
  68. }
  69. //adds vertical buttons
  70. this.addVertical = function() {
  71. //if layout params doesn't exist create a new one
  72. if(!this.layoutParamsUpDown) this.initUpDownLayoutParams();
  73. //create a new layout for up and down buttons
  74. this.upDownLayout = new Atomic.UILayout();
  75. this.upDownLayout.rect = this.view.rect;
  76. this.upDownLayout.axis = Atomic.UI_AXIS_Y;
  77. this.upDownLayout.spacing = 50;
  78. //create an up buttons
  79. this.upButton = new Atomic.UIButton();
  80. this.upButton.skinBg = "TouchButtonUp";
  81. this.upButton.layoutParams = this.layoutParamsUpDown;
  82. this.upDownLayout.addChild(this.upButton);
  83. //create a down button
  84. this.downButton = new Atomic.UIButton();
  85. this.downButton.skinBg = "TouchButtonDown";
  86. this.downButton.layoutParams = this.layoutParamsUpDown;
  87. this.upDownLayout.addChild(this.downButton);
  88. this.upDownLayout.layoutSize = Atomic.UI_LAYOUT_SIZE_PREFERRED;
  89. //it makes ours buttons uncaptured, this is used for the multiTouch, to don't `concentrate` only on one button
  90. this.upButton.setCapturing(false);
  91. this.downButton.setCapturing(false);
  92. //bind our ui button to the specified Keyboard Button
  93. Atomic.input.bindButton(this.upButton, Atomic.KEY_UP);
  94. Atomic.input.bindButton(this.downButton, Atomic.KEY_DOWN);
  95. }
  96. //inits layout prams for up/down buttons
  97. this.initUpDownLayoutParams = function() {
  98. this.layoutParamsUpDown = new Atomic.UILayoutParams();
  99. this.layoutParamsUpDown.minWidth = verticButtonsSize[0];
  100. this.layoutParamsUpDown.minHeight = verticButtonsSize[1];
  101. this.layoutParamsUpDown.width = verticButtonsSize[0]*2;
  102. this.layoutParamsUpDown.height = verticButtonsSize[1]*2;
  103. this.layoutParamsUpDown.maxWidth = verticButtonsSize[0]*6;
  104. this.layoutParamsUpDown.maxHeight = verticButtonsSize[1]*6;
  105. }
  106. //inits layout params for left/right buttons
  107. this.initLeftRightLayoutParams = function() {
  108. this.layoutParamsLeftRight = new Atomic.UILayoutParams();
  109. this.layoutParamsLeftRight.minWidth = horizButtonsSize[0];
  110. this.layoutParamsLeftRight.minHeight = horizButtonsSize[1];
  111. this.layoutParamsLeftRight.width = horizButtonsSize[0]*2;
  112. this.layoutParamsLeftRight.height = horizButtonsSize[1]*2;
  113. this.layoutParamsLeftRight.maxWidth = horizButtonsSize[0]*6;
  114. this.layoutParamsLeftRight.maxHeight = horizButtonsSize[1]*6;
  115. }
  116. //set horizontal spacing
  117. this.setSpacingX = function(spacing) {
  118. this.dpad.spacing = spacing;
  119. }
  120. //set vertical spacing
  121. this.setSpacingY = function(spacing) {
  122. this.upDownLayout.spacing = spacing;
  123. }
  124. //set view position
  125. this.setPosition = function(x, y) {
  126. this.view.setPosition(x, y);
  127. }
  128. }
  129. module.exports = DPad;