-
Notifications
You must be signed in to change notification settings - Fork 5
/
ProgressBar.js
120 lines (104 loc) · 4.45 KB
/
ProgressBar.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**@
* #ProgressBar
* @category Custom
*
* Used to display the progress of a specific task (e.g. progress of asset loading, HP bar).
* Internally it creates two 2D blocks representing the empty and full part of the progress spectrum.
* These blocks adjust their size with respect to the current progress.
* Changes to the progress bar's 2D properties are reflected on to the blocks.
* The blocks cut the bar along the highest progress bar's dimension
* (blocks span horizontally if progressbar.width >= progressbar.height).
*
*/
Crafty.c("ProgressBar", {
init: function(entity) {
this.requires("2D");
this._pbFilledFraction = 0;
},
/**@
* #.updateBarProgress
* @comp ProgressBar
* @sign public this .updateBarProgress(Number currentValue)
* @param currentValue - The current progress. The value must be a 0 <= number <= maxValue
* representing the current progress.
* @return this - The current entity for chaining.
*
* Update method to update the current progress of the progressbar.
*/
updateBarProgress: function(val) {
this._pbFilledFraction = val / this._pbMaxValue;
if (this._pbFlipDirection)
this._pbFilledFraction = 1 - this._pbFilledFraction;
this._updateBarDimension();
return this;
},
_updateBarDimension: function() {
this._pbBlockWidth = this._w * this._pbFilledFraction;
this._pbBlockHeight = this._h * this._pbFilledFraction;
if (this._pbBlockWidth >= this._pbBlockHeight) {
this._pbLowerBlock.attr({ x: this._x, y: this._y,
w: this._pbBlockWidth, h: this._h });
this._pbHigherBlock.attr({ x: this._x + this._pbBlockWidth, y: this._y,
w: this._w - this._pbBlockWidth, h: this._h });
} else {
this._pbLowerBlock.attr({ x: this._x, y: this._y,
w: this._w, h: this._pbBlockHeight });
this._pbHigherBlock.attr({ x: this._x, y: this._y + this._pbBlockHeight,
w: this._w, h: this._h - this._pbBlockHeight });
}
return this;
},
_updateBarOrder: function() {
this._pbLowerBlock.z = this._z;
this._pbHigherBlock.z = this._z;
},
/**@
* #.progressBar
* @comp ProgressBar
* @sign public this .progressBar(Number maxValue, Boolean flipDirection,
* String emptyColor, String filledColor)
* @param maxValue - The maximum value the incoming value can have.
* @param flipDirection - Whether to flip the fill direction. False to fill blocks from left/top
* to right/bottom. True to inverse.
* @param emptyColor - The color for 2D blocks that are empty.
* @param filledColor - The color for 2D blocks that are filled.
* @return this - The current entity for chaining.
*
* Constructor method to setup the progress bar.
*
* @example
* ~~~
* var progressBar = Crafty.e("2D, DOM, ProgressBar")
* .attr({ x: 150, y : 140, w: 100, h: 25, z: 100 })
* .progressBar(100, false, "blue", "green");
* ...
* progressBar.updateBarProgress(someValue);
* ~~~
*/
progressBar : function(maxValue, flipDirection, emptyColor, filledColor) {
this._pbMaxValue = maxValue;
this._pbFlipDirection = flipDirection;
var renderMethod = this.has("Canvas") ? "Canvas" : "DOM";
this._pbLowerBlock = Crafty.e("2D, " + renderMethod + ", Color")
.color(flipDirection ? emptyColor : filledColor);
this._pbHigherBlock = Crafty.e("2D, " + renderMethod + ", Color")
.color(flipDirection ? filledColor : emptyColor);
this.attach(this._pbLowerBlock);
this.attach(this._pbHigherBlock);
this._updateBarDimension();
this._updateBarOrder();
this.bind("Resize", this._updateBarDimension);
this.bind("reorder", this._updateBarOrder);
this.bind("RemoveComponent", function(component) {
if (component === "ProgressBar") {
this.unbind("Resize", this._updateBarDimension);
this.unbind("reorder", this._updateBarOrder);
this.detach(this._pbLowerBlock);
this.detach(this._pbHigherBlock)
this._pbLowerBlock.destroy();
this._pbHigherBlock.destroy();
}
});
return this;
}
});