-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayout.js
90 lines (86 loc) · 2.63 KB
/
layout.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
"use strict";
var _ = require('underscore');
var Backbone = require("backbone");
// A layout of buttons for the launchpad
var Layout = function(outputs){
_.extend(this, Backbone.Events);
this.buttons = {};
this.outputs = outputs || {};
this.init = function(launchpad, buttons, reset){
this.buttons = buttons || this.buttons;
this.launchpad = launchpad;
if (reset){
this.launchpad.clear();
}
_.each(this.buttons, function(button, name){
this.buttons[name].button = this.launchpad.getButton(
button.position.x,
button.position.y
);
this.buttons[name].button.light(button.color);
_.each(button.callbacks, function(callback, event){
this.layout.buttons[name].button.on(event, callback, this);
}, {layout: this, name: name, button: this.buttons[name].button});
}, this);
};
this.blinkers = {};
this.blink = function(button, colors){
this.blinkers[button.toString()] = colors;
if (1 == _.keys(this.blinkers).length){
this._blink();
}
};
this.stopBlink = function(button, color){
this.blinkers = _.omit(this.blinkers, button.toString());
// console.log('stopping blink', color);
button.light(color);
};
this.stopAllBlink = function(){
this.blinkers = [];
};
this._blink = function(){
var duration = 250;
_.each(this.blinkers, function(colors, button){
var matches = button.match(/\d+/g);
var button = this.launchpad.getButton(matches[0], matches[1]);
_.each(colors, function(color, idx){
_.delay(_.bind(function(){
if (_.has(this.blinkers, button.toString())){
button.light(color);
}
}, this), idx * duration, this);
}, this);
}, this);
if (!_.isEmpty(this.blinkers)){
_.delay(_.bind(this._blink, this), 1000);
}
};
this.drawGrid = function(bank, pattern){
// console.log('drawGrid');
// Wipe the area
this.clearArea(0, 8, 0, 8);
_.each(pattern.notes, function(ticknotes) {
_.each(ticknotes, function(note){
note.button.light(bank);
});
});
};
this.clearArea = function(x1, x2, y1, y2) {
// console.log('clearArea');
for(var y = y1; y < y2; y++) {
for(var x = x1; x < x2; x++) {
this.launchpad.getButton(x, y).dark();
}
}
};
this.applyToArea = function(fn, x1, x2, y1, y2) {
// console.log('applyToArea', x1, y1, x2, y2);
// Call fn for all buttons in the area (x1, y1), (x2, y2)
for(var y = y1; y < y2; y++) {
for(var x = x1; x < x2; x++) {
fn({layout: this, button: this.launchpad.getButton(x, y)});
}
}
};
}
exports.Layout = Layout;