-
Notifications
You must be signed in to change notification settings - Fork 24
/
tile-rectangle.js
118 lines (100 loc) · 2.65 KB
/
tile-rectangle.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
/// <reference types="@mapeditor/tiled-api" />
/*
* tile-rectangle.js
*
* Example tool that draws a custom tile rectangle using the current brush.
*
* Rather than simply repeating the brush, the corners are not repeated and the
* sides are repeated only in one direction. The remaining center part of the
* brush is repeated in all directions.
*/
tiled.registerTool("PaintTileRectangle", {
name: "Paint Tile Rectangle",
icon: "tile-rectangle.svg",
usesSelectedTiles: true,
activated() {
this.preview = new TileMap();
},
tilePositionChanged(curX, curY) {
if (!tiled.mapEditor.currentBrush) {
return;
}
/** @type TileLayer */
const brush = tiled.mapEditor.currentBrush.layerAt(0);
if (!brush || brush.width < 1 || brush.height < 1) {
return;
}
// Don't stretch when the brush is too small, unless it's small in both
// directions
const stretchX = (brush.width > 1 || brush.height == 1);
const stretchY = (brush.height > 1 || brush.width == 1);
const preview = new TileMap();
const tileLayer = new TileLayer();
preview.addLayer(tileLayer);
const edit = tileLayer.edit();
let startX = curX;
let startY = curY;
let endX = curX;
let endY = curY;
if (this.startX) {
if (!stretchX) {
curX = this.startX;
}
if (!stretchY) {
curY = this.startY;
}
startX = Math.min(this.startX, curX);
startY = Math.min(this.startY, curY);
endX = Math.max(this.startX, curX);
endY = Math.max(this.startY, curY);
} else {
if (stretchX) {
startX = curX - 1;
endX = curX + 1;
}
if (stretchY) {
startY = curY - 1;
endY = curY + 1;
}
}
for (let y = startY; y <= endY; ++y) {
for (let x = startX; x <= endX; ++x) {
let brushX;
let brushY;
if (y == startY) {
brushY = 0;
} else if (y == endY) {
brushY = brush.height - 1;
} else {
brushY = Math.min((y - startY - 1) % (brush.height - 2) + 1, brush.height - 1);
}
if (x == startX) {
brushX = 0;
} else if (x == endX) {
brushX = brush.width - 1;
} else {
brushX = Math.min((x - startX - 1) % (brush.width - 2) + 1, brush.width - 1);
}
const tile = brush.tileAt(brushX, brushY);
edit.setTile(x, y, tile);
}
}
edit.apply();
this.preview = preview;
},
updateStatusInfo() {},
mousePressed(/* button, x, y, modifiers */) {
const tileLayer = this.map.currentLayer;
if (!tileLayer.isTileLayer) {
return;
}
this.startX = this.tilePosition.x;
this.startY = this.tilePosition.y;
},
mouseReleased(/* button, x, y, modifiers */) {
this.startX = undefined;
this.startY = undefined;
this.map.merge(this.preview, false);
this.preview = new TileMap();
},
});