-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmlca_Layer.js
150 lines (130 loc) · 4.22 KB
/
mlca_Layer.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
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
/* Layer: contain the layer characteristics
-dimensions: the size of the LayerDataStructure
-isVisible: tells if it's currently begin drawn
-id: layer identifier
-type: contain the inner representation of the states (boolean, integer, etc);
-topology: layer's edge rule ('noloop','xloop','yloop','xyloop')
-defaultState: the target state of the cells when the layer is cleared
-interfaceData: contain the graphic representation for each state
-buffer: stores the data of the current and the next configuration of states of the layer
-lock: boolean to tell if the user can edit this layer or not.
specs = {
DataStructure: contain the way states are stored (int matrix, bit map, etc)
dimensions,
type,
topology,
defaultState,
layerID,
interfaceData
lock
}
+clear(state): set all cells in the current and next step buffer to the state in parameter,
if none is given, search for the default state, if there isn't any, returns false.
+read(coords): return the state of the current cell given by the coords parameter.
+write(coords, value, dontSwap): writes the value passed to the cell given by the coords parameter,
if dontSwap is true, writes to the current cell, else writes to the next step cell. returns if the
cell writing was successful.
+swap(): swap the current buffer with the next step one.
-initDataStructure(DataStructure): assign the data structure given by parameter to the buffers,
the layerDataStructure parameters are taken from the layer object.
+readFromString(string,func,offset): reads a string and assign the states given by the chars, according to the given func,
to the cells starting at a certain offset, from left to right. '\n' indicates a line break, which means the next cell to
be assigned is on a line below the current.
*/
mlca.Layer = function(specs) {
'use strict';
this.buffer = [];
this.dimensions = specs.dimensions;
this.isVisible = specs.visibility;
this.id = specs.layerID;
this.type = specs.type;
this.topology = specs.topology;
this.defaultState = specs.defaultState;
this.interfaceData = specs.interfaceData;
this.lock = specs.lock;
this.initDataStructure(specs.DataStructure);
this.buffer.current=this.buffer[0];
this.buffer.next=this.buffer[1];
console.log(this.id + "'s data structure initialized");
return this;
};
mlca.Layer.prototype = {
type : '',
id : '',
interfaceData: {},
clear: function(state){
if (state===undefined){
if (this.defaultState!==undefined){
state = this.defaultState;
}
else {return false};
}
console.log(state);
this.buffer.next.clear(state);
this.buffer.current.clear(state);
},
read: function(coords){
'use strict';
return this.buffer.current.getCell(coords);
},
write: function(coords, value, dontSwap){
'use strict';
var matrix;
if (dontSwap === true){
matrix = this.buffer.current;
}
else {
matrix = this.buffer.next;
}
return matrix.setCell(coords, value);
},
swap: function(){
'use strict';
var a = this.buffer.next;
this.buffer.next = this.buffer.current;
this.buffer.current = a;
},
initDataStructure: function (DataStructure){
'use strict';
console.log("Initializing data structure type "
+ this.type + " to layer " + this.id);
var i;
for (i = 0; i<=1; i+=1){
this.buffer[i] = new DataStructure(
{
dimensions: this.dimensions,
type: this.type,
topology: this.topology
}
);
}//end for
},
readFromString: function (string,func,offset){
'use strict';
console.log('Reading from string:');
if (offset === undefined){
offset = {x:0,y:0};
}
console.log(string);
var i;
var it = {x:0, y:0};
for (i = 0;i<string.length;i+=1){
console.log(i + 'th character: '+ string.charAt(i));
if (string.charAt(i)==='\n'){
it.x=0;
it.y+=1;
console.log('Line break.');
continue;
}
if (it.x>=this.dimensions.x){
it.x=0;
it.y+=1;
}
if (it.y >= this.dimensions.y) {return;}
console.log('Writing ' + string.charAt(i) + ' at ' + it.x + ',' + it.y);
this.write({x:it.x+offset.x,y:it.y+offset.y},func(string.charAt(i)));
it.x+=1;
}
this.swap();
}
};