]> git.lizzy.rs Git - dragonblocks.git/blob - engine/schematic.js
Rename PixelManipulator to Schematic
[dragonblocks.git] / engine / schematic.js
1 /*
2  * schematic.js
3  *
4  * Copyright 2020 Elias Fleckenstein <eliasfleckenstein@web.de>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  * MA 02110-1301, USA.
20  *
21  *
22  */
23
24 dragonblocks.Schematic = class
25 {
26         constructor(arr)
27         {
28                 this.data = [];
29                 this.functions = [];
30
31                 let anchor;
32
33                 for (let y = 0; y < arr.length; y++) {
34                         for (let x = 0; x < arr[y].length; x++) {
35                                 let node = arr[y][x];
36
37                                 if (node[0] == "ยง") {
38                                         anchor = {x: x, y: y};
39                                         node = node.slice(1, node.length);
40                                 }
41
42                                 if (node == "")
43                                         continue;
44
45                                 this.data.push({
46                                         x: x,
47                                         y: y,
48                                         node: node,
49                                 });
50                         }
51                 }
52
53                 if (! anchor)
54                         anchor = {x: 0, y: 0};
55
56                 for (let pixel of this.data) {
57                         pixel.x = pixel.x - anchor.x;
58                         pixel.y = pixel.y - anchor.y;
59                 }
60         }
61
62         apply(x, y)
63         {
64                 for (let pixel of this.data) {
65                         let mx, my;
66                         mx = pixel.x + x;
67                         my = pixel.y + y;
68
69                         let node = dragonblocks.getNode(mx, my);
70                         if (! node)
71                                 continue;
72
73                         let nodeDef = node.toNode();
74
75                         let doApply = true;
76
77                         for (let func of this.functions) {
78                                 if (func(nodeDef, mx, my, pixel.node) == false) {
79                                         doApply = false;
80                                         break;
81                                 }
82                         }
83
84                         if (doApply)
85                                 dragonblocks.setNode(mx, my, pixel.node);
86                 }
87
88                 return this;
89         }
90
91         replace(toReplace, replaceWith)
92         {
93                 for (let pixel of this.data)
94                         if (pixel.node == toReplace)
95                                 pixel.node = replaceWith;
96
97                 return this;
98         }
99
100         addFunction(func)
101         {
102                 this.functions.push(func);
103                 return this;
104         }
105 };