-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSlider.js
71 lines (61 loc) · 2.04 KB
/
Slider.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
Adventurous.Slider = function (x,y,value)
{
this.dragging = false;
this.value = value;
this.prevX = -1;
this.sliderGroup = game.add.group();
this.background = game.add.sprite(x,y,"slider_bg");
this.slider = game.add.sprite(this.background.x+value*this.background.width,y,"slider");
this.slider.x -= Math.floor(this.slider.width/2);
this.slider.x = Math.min(this.background.x + this.background.width - this.slider.width,Math.max(this.slider.x,this.background.x));
this.sliderGroup.add(this.background);
this.sliderGroup.add(this.slider);
game.input.onDown.add(this.mouseDown, this);
game.input.onUp.add(this.mouseUp, this);
};
Adventurous.Slider.prototype =
{
update: function()
{
if(this.dragging)
{
this.slider.x += (game.input.activePointer.x-this.prevX);
this.slider.x = Math.min(this.background.x + this.background.width - this.slider.width,Math.max(this.slider.x,this.background.x));
this.prevX = game.input.activePointer.x;
}
},
mouseDown: function()
{
if(this.sliderGroup.visible)
{
if(Adventurous.Util.isMouseOverObject(this.slider))
{
this.dragging = true;
this.prevX = game.input.activePointer.x;
}
else
{
this.dragging = false;
}
}
},
mouseUp: function()
{
this.dragging = false;
this.value = (this.slider.x - this.background.x)/(this.background.width-this.slider.width);
},
hide: function()
{
this.sliderGroup.visible = false;
},
show: function()
{
this.sliderGroup.visible = true;
},
setValue: function(value)
{
this.slider.x = this.background.x+value*this.background.width;
this.slider.x -= Math.floor(this.slider.width/2);
this.slider.x = Math.min(this.background.x + this.background.width - this.slider.width,Math.max(this.slider.x,this.background.x));
}
};