Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. SlickUI.namespace('Element');
  2.  
  3. /**
  4. * Create a Slider to control defined values
  5. *
  6. * @author Richard Snijders <richard@fizz.nl>
  7. * @param x
  8. * @param y
  9. * @param size
  10. * @param value
  11. * @param orientation
  12. * @constructor
  13. */
  14. SlickUI.Element.Slider = function (x, y, size, value, orientation) {
  15. this._x = x;
  16. this._y = y;
  17. this._size = size;
  18. this._value = value;
  19. this._orientation = orientation || "horizontal";
  20. this.container = null;
  21. if(typeof value == 'undefined') {
  22. this._value = 1;
  23. }
  24. };
  25.  
  26. /**
  27. * Internal Container handling.
  28. *
  29. * @param container
  30. */
  31. SlickUI.Element.Slider.prototype.setContainer = function (container) {
  32. this.container = container;
  33. };
  34.  
  35. /**
  36. * Adds the Slider and makes it interactable
  37. */
  38. SlickUI.Element.Slider.prototype.init = function() {
  39. var theme = game.cache.getJSON('slick-ui-theme');
  40. this.onDragStart = new Phaser.Signal();
  41. this.onDrag = new Phaser.Signal();
  42. this.onDragStop = new Phaser.Signal();
  43.  
  44. var x = this.container.x + this._x;
  45. var y = this.container.y + this._y;
  46. var width = Math.min(this.container.width - this._y, this._size);
  47. var height = Math.min(this.container.height - this._y, this._size);
  48. var initialPosition = {
  49. x: Math.min(1, Math.max(0, this._value)) * width + x,
  50. y: Math.min(1, Math.max(0, this._value)) * height + y
  51. };
  52. var renderedSprites = this.renderSprites(width, height);
  53.  
  54. var sprite_base = renderedSprites[0];
  55. var handle_off = renderedSprites[1];
  56. var handle_on = renderedSprites[2];
  57. sprite_base.x = x;
  58. sprite_base.y = y;
  59.  
  60. sprite_handle = this.makeSprite(x, y, initialPosition, handle_off.texture);
  61. sprite_handle.anchor.setTo(0.5);
  62.  
  63. sprite_handle.inputEnabled = true;
  64. sprite_handle.input.useHandCursor = true;
  65. var dragging = false;
  66.  
  67. sprite_handle.events.onInputDown.add(function () {
  68. sprite_handle.loadTexture(handle_on.texture);
  69. dragging = true;
  70. this.onDragStart.dispatch(this.getCoord(sprite_handle, x, y, width, height));
  71. }, this);
  72. sprite_handle.events.onInputUp.add(function () {
  73. sprite_handle.loadTexture(handle_off.texture);
  74. dragging = false;
  75. this.onDragStop.dispatch(this.getCoord(sprite_handle, x ,y, width, height));
  76. }, this);
  77.  
  78. game.input.addMoveCallback(function (pointer, pointer_x,pointer_y) {
  79. if(!dragging) {
  80. return;
  81. }
  82. this.modifySpriteHandle(sprite_handle, x, y, width, height,
  83. pointer_x - this.container.displayGroup.x,
  84. pointer_y - this.container.displayGroup.y);
  85. this.onDrag.dispatch(this.getCoord(sprite_handle, x ,y, width, height));
  86. }, this);
  87.  
  88. this.container.displayGroup.add(sprite_base);
  89. this.container.displayGroup.add(sprite_handle);
  90. };
  91.  
  92. /**
  93. * render the sprites
  94. * @param width
  95. * @param height
  96. * @returns {*}
  97. */
  98. SlickUI.Element.Slider.prototype.renderSprites = function (width, height) {
  99. var rendererString, renderedSprites;
  100. if(this._orientation === "horizontal"){
  101. rendererString = 'slider';
  102. renderedSprites = this.container.root.getRenderer(rendererString).render(width);
  103. }
  104. else{
  105. rendererString = 'Slider';
  106. renderedSprites = this.container.root.getRenderer(rendererString).render(height);
  107. }
  108. return renderedSprites;
  109. };
  110.  
  111. /**
  112. * @param sprite_handle
  113. * @param x
  114. * @param y
  115. * @param width
  116. * @param height
  117. * @returns {number}
  118. */
  119. SlickUI.Element.Slider.prototype.getCoord = function (sprite_handle, x, y, width, height) {
  120. if(this._orientation === "horizontal"){
  121. return (sprite_handle.x - x) / width;
  122. }
  123. else{//vertical orientation
  124. return (sprite_handle.y - y) / height;
  125. }
  126. };
  127.  
  128. /**
  129. * @param x
  130. * @param y
  131. * @param initialPosition
  132. * @param texture
  133. * @returns {*}
  134. */
  135. SlickUI.Element.Slider.prototype.makeSprite = function (x, y, initialPosition, texture) {
  136. var sprite_handle;
  137. if(this._orientation === "horizontal"){
  138. sprite_handle = game.make.sprite(initialPosition.x, y, texture);
  139. }
  140. else{//vertical orientation
  141. sprite_handle = game.make.sprite(x, initialPosition.x, texture);
  142. }
  143. return sprite_handle;
  144. };
  145.  
  146. /**
  147. *
  148. * @param sprite_handle
  149. * @param x
  150. * @param y
  151. * @param width
  152. * @param height
  153. * @param pointer_x
  154. * @param pointer_y
  155. */
  156. SlickUI.Element.Slider.prototype.modifySpriteHandle = function (sprite_handle, x, y, width, height, pointer_x, pointer_y) {
  157. if(this._orientation === "horizontal"){
  158. sprite_handle.x = Math.min(x + width, Math.max(x, pointer_x));
  159. }
  160. else{//vertical orientation
  161. sprite_handle.y = Math.min(y + height, Math.max(y, pointer_y));
  162. }
  163. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement