-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuiController.js
More file actions
58 lines (45 loc) · 1.57 KB
/
uiController.js
File metadata and controls
58 lines (45 loc) · 1.57 KB
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
var UIController = function(max_population, stage_width, stage_height, dot_radius, resetHandler, pauseHandler, keyboard) {
var ui = {};
ko.numericObservable = function(initialValue) {
var _actual = ko.observable(initialValue);
var result = ko.dependentObservable({
read: function() {
return _actual();
},
write: function(newValue) {
var parsedValue = parseFloat(newValue);
_actual(isNaN(parsedValue) ? newValue : parsedValue);
}
});
return result;
};
ui.changeHandler = function(newValue) {
resetHandler();
}
ui.highlightID = ko.numericObservable(-1);
ui.population = ko.observable(0);
ui.pause = pauseHandler;
ui.stageWidth = function () { return stage_width; };
ui.stageHeight = function () { return stage_height; };
ui.maxPopulation = function () { return max_population; };
ui.dotRadius = function () { return dot_radius; };
$(document).keydown(function(e) {
switch(e.which) {
case 37: // left
keyboard.left();
break;
case 38: // up
keyboard.up();
break;
case 39: // right
keyboard.right();
break;
case 40: // down
keyboard.down();
break;
default: return; // exit this handler for other keys
}
e.preventDefault(); // prevent the default action (scroll / move caret)
});
return ui;
};