/lenasys/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/lenasys/trunk
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/* 

Widget Code!

*/

// Recursive Pos of div in document - should work in most browsers
function findPos(obj) {
	var curLeft = curTop = 0;
	if(obj.offsetParent) {
		curLeft = obj.offsetLeft
		curTop = obj.offsetTop
		while(obj = obj.offsetParent) {
			curLeft += obj.offsetLeft
			curTop += obj.offsetTop
		}
	}
	return {
		x:curLeft,
		y:curTop
	}
}

/*

ui read widget functions

*/

function readSlider(sliderId) {
	var value = parseFloat(document.getElementById(sliderId + "box").value);
	if(isNaN(value)) {
		value = 0.0;
	}
	return value;
}

function readBinary(binaryId) {
	lf = document.getElementById(binaryId + "marker").style.left;
	if(lf == "2px") {
		return 0;
	} else {
		return 1;
	}
}

/*

ui change widget functions

*/

// The idea is to have a few functions like this one and minimum javascript intervention		
function updateBinary(event, binaryId) {
	if(mb) {
		coords = findPos(event.currentTarget);
		coords.x = event.clientX - coords.x;
		coords.y = event.clientY - coords.y;
		if(coords.x > 24) {
			document.getElementById(binaryId + "marker").style.left = 20 + "px";										
			document.getElementById(binaryId).style.backgroundColor = "#ddd";										
		}else if(coords.x < 16) {
			document.getElementById(binaryId + "marker").style.left = 2 + "px";																		
			document.getElementById(binaryId).style.backgroundColor = "#777";										
		}
	}
}

// The idea is to have a few functions like this one and minimum javascript intervention		
function updateSlider(event, kind, min, max, length, sliderId, boxId) {
	if(mb) {
		coords = findPos(event.currentTarget);
		coords.x = event.clientX - coords.x;
		coords.y = event.clientY - coords.y;
		cx = coords.x / length;
		sd = max - min;
		value = min + (sd * cx);	
		if(value > max) value = max;
		if(kind == 0) {
			value = Math.round(value);
		}
		if(kind == 1) {
			value = Math.round(value * 10.0) / 10.0;
		}
		if(kind == 2) {
			value = Math.round(value * 100.0) / 100.0;
		}
		if(kind == 3) {
			value = Math.round(value * 1000.0) / 1000.0;
		}
		document.getElementById(boxId).value = value;				
		document.getElementById(sliderId).style.left = Math.ceil((length) * ((value - min) / sd)) + "px";
	}
}
		
function sliderPosition(value, min, max, length) {
		if(value < min) {
			value = min;
		}
		if(value > max) {
			value = max;
		}
		pos = Math.round(((value - min) / (max - min)) * length);
		return pos;
}

function changeSlider(value, sliderId, kind, min, max, length) {
		value = parseFloat(value);		
		if(isNaN(value)) {
			value = 0.0;
		}
		if(value < min) {
			value = min;
		}
		if(value > max) {
			value = max;
		}
		if(kind == 0) {
			value = Math.round(value);
		}
		if(kind == 1) {
			value = Math.round(value * 10.0) / 10.0;
		}
		if(kind == 2) {
			value = Math.round(value * 100.0) / 100.0;
		}
		if(kind == 3) {
			value = Math.round(value * 1000.0) / 1000.0;
		}
		pos = sliderPosition(value, min, max, length);
		document.getElementById(sliderId + "box").value = value;
		document.getElementById(sliderId + "marker").style.left = pos + "px";										
}

/*

ui make widget functions

*/

function makeLabel(name) {
	s = "";
	s += "<div class='label'>" + name + "</div>";
	return s;		
}

function makePanel(width, height) {
	s = "";		
	s += "<div class='panel' style='height: " + height + "px; width: " + width + "px;'>";
	return s;
}

function makePanelEnd() {
	s = "</div>";
	return s;		
}

function makeSlider(sliderId, kind, minValue, maxValue, midValue, sliderWidth, defaultValue) {
	s = "";
	defaultPosition = sliderPosition(defaultValue, minValue, maxValue, sliderWidth);
	if(defaultValue < minValue) {
		defaultValue = minValue;
	}
	if(defaultValue > maxValue) {
		defaultValue = maxValue;
	}					
	s += "<input id='" + sliderId + "box' value='" + defaultValue + "' type='text' class='sliderBox' onBlur='changeSlider(this.value,\"" + sliderId + "\"," + kind + "," + minValue + "," + maxValue + "," + sliderWidth + ");'>";
	s += "<div id='" + sliderId + "' class='sliderLine' style='width:" + sliderWidth + "px;'";
	s += " onmousemove='updateSlider(event," + kind + "," + minValue + "," + maxValue + "," + sliderWidth + ",\"" + sliderId + "marker\",\"" + sliderId + "box\");'";
	s += " onmouseup='updateSlider(event," + kind + "," + minValue + "," + maxValue + "," + sliderWidth + ",\"" + sliderId + "marker\",\"" + sliderId + "box\");'>";
	s += " <div id='" + sliderId + "marker' class='sliderMarker' style='left:" + defaultPosition + "px;'></div>";
	s += "</div>";
	s += "<div class='slidermidValue' style='width:" + sliderWidth + "px;'>" + midValue;
	s += "<div class='sliderminValue'>" + minValue + "</div>";
	s += "<div class='slidermaxValue'>" + maxValue + "</div></div>";
	return s;
}

function makeBinary(binaryId, defaultValue) {
	s = "";
	if(defaultValue == 1) {
		defaultPosition = 20;
		defaultColor = "#ddd";
	} else {
		defaultPosition = 2;
		defaultColor = "#777";
	}
	s += "<div id='" + binaryId + "' class='binaryLine' style='background-color:" + defaultColor + ";' onmousemove='updateBinary(event,\"" + binaryId + "\");' onmouseup='updateBinary(event,\"" + binaryId + "\");'>";
	s += "<div id='" + binaryId + "marker' class='binaryMarker' style='left:" + defaultPosition + "px;'></div>";
	s += "</div>";
	return s;
}

var mb = 0;

function mbPress(event) {
	mb = 1;
}

function mbRelease(event) {
	mb = 0;
}