DPad.js 5.8 KB

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