DPad.js 5.6 KB

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