DPad.js 6.5 KB

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