-
Notifications
You must be signed in to change notification settings - Fork 3
/
num_status_obj.js
115 lines (93 loc) · 2.4 KB
/
num_status_obj.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
// Generic wrapper for a status that includes text and a num
// Can be drawn as a number or a bar
// Provides draw, incr, set_num, and get_num
// spec:
// pos : center pos of text
// text : Will display "<text> <num>"
// num : num to start at
// bar : bool (optional)
// max : num (required if bar)
var num_status_obj = function(p, spec) {
// obj to return
var obj = {};
// --- private variables ---
var pos = spec.pos;
var number = spec.num;
var txt = spec.text;
var bar = spec.bar || false;
var max = spec.max;
var height = 20;
var max_width = 100; //for the bar, might need to be passed in
var get_obj_text = function() {
if (bar) {
return txt;
}
else {
var num_txt = ""+number;
if (spec.format) {
num_txt = spec.format(number);
}
return txt + " " + num_txt;
}
}
var rect = rectangle(p, { // Really just a text obj
pos : pos,
width : 0,
height : 0,
text : get_obj_text(),
text_size : 14,
text_color: spec.text_color || 255,
});
// --- private methods
var update = function(n) {
number = n;
rect.update_text(get_obj_text());
};
var draw_full_rect = function(color) {
p.fill(color[0], color[1], color[2]);
//p.strokeWeight(1);
//p.stroke(0);
var topy = pos.y - (height / 2);
var leftx = pos.x + (txt.length * 4);
p.rect(leftx, topy, max_width, height);
};
// --- public methods ---
obj.draw = function() {
rect.draw();
// Draw the bar separately, if appropriate
if (bar) {
p.noStroke();
// Draw empty rectangle first
draw_full_rect([255, 255, 255]);
// Then draw mutation status bar
p.fill(150);
var topy = pos.y - (height / 2);
var leftx = pos.x + (txt.length * 4);
var rect_width = (number / max) * max_width;
rect_width = rect_width > max_width ? max_width : rect_width;
p.rectMode(p.CORNER);
p.rect(leftx, topy, rect_width, height);
p.fill(255)
}
};
// SHOULD ONLY BE CALLED IF bar IS TRUE
// Draws a full bar of the specified color
// color : 3 element rgb array
obj.draw_color = function(color) {
draw_full_rect(color);
};
obj.incr = function(n) {
update(number + n);
};
// Set and get
obj.set_max = function(m) {
max = m;
};
obj.set_num = function(n) {
update(n);
};
obj.get_num = function() {
return number;
};
return obj;
};