Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions js/pmjs-plugins/yed/retained-tiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
Math.ceil(map._height / map._tileHeight) + 1];
}

function visit(map, x, y, value, points) {
// `cells` receives each visible tile layer's window; readCells lists its cells for it.
function visit(map, x, y, value, points, cells) {
var data = map.tiledData, size = windowSize(map);
value(data); value(data.layers); value(data.tilesets); value(map._layers);
value(map._priorityTiles); value(map._priorityTiles.length);
Expand Down Expand Up @@ -68,11 +69,7 @@
value(meta.properties && meta.properties.priority); value(meta.properties && meta.properties.zIndex);
value(meta.properties && meta.properties.hideOnLevel); value(meta.data);
if (meta.type !== 'tilelayer' || !meta.visible) return;
for (var row = 0; row < size[1]; row++) {
for (var column = 0; column < size[0]; column++) {
value(cell(map, meta, x + column, y + row));
}
}
cells(map, meta, x, y, size);
});
data.layers.forEach(function(layer) {
if (layer.type !== 'objectgroup') return;
Expand All @@ -92,6 +89,10 @@
visit(map, state.x, state.y, function(value) { state.values.push(value); },
function(points) {
for (var index = 0; index < points.length; index++) state.values.push(points[index]);
},
function(target, layer, x, y, size) {
var values = readCells(target, layer, x, y, size);
for (var index = 0; index < values.length; index++) state.values.push(values[index]);
});
}

Expand All @@ -102,6 +103,24 @@
return x < 0 || y < 0 || x >= width || y >= height ? 0 : layer.data[y * width + x] || 0;
}

// The window's cells of one tile layer, row by row, with cell()'s values. Without wrapping this
// reads layer.data directly: the check runs on every frame for every visible cell.
var cellScratch = [];
function readCells(map, layer, x, y, size) {
var width = map._mapWidth, height = map._mapHeight, data = layer.data, count = 0;
var wraps = map.horizontalWrap || map.verticalWrap;
cellScratch.length = size[0] * size[1];
for (var row = 0; row < size[1]; row++) {
var cy = y + row, inside = cy >= 0 && cy < height, base = cy * width;
for (var column = 0; column < size[0]; column++) {
var cx = x + column;
cellScratch[count++] = wraps ? cell(map, layer, cx, cy) :
inside && cx >= 0 && cx < width ? data[base + cx] || 0 : 0;
}
}
return cellScratch;
}

function source(map, material) {
var id = map._getAnimTileId(material.texture, material.local);
return [id % material.set.columns * material.set.tilewidth,
Expand Down Expand Up @@ -274,6 +293,12 @@
for (var index = 0; index < points.length; index++) {
if (state.values[cursor++] !== points[index]) same = false;
}
},
function(target, layer, cx, cy, size) {
var values = readCells(target, layer, cx, cy, size);
for (var index = 0; index < values.length; index++) {
if (state.values[cursor++] !== values[index]) same = false;
}
});
if (cursor !== state.values.length) same = false;
map._layers.forEach(function(layer, index) {
Expand Down
22 changes: 22 additions & 0 deletions test/yed-retained-tiles.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,25 @@ test('a null native layer conservatively declines before mutation', () => {
assert.equal(api.prepare(map, 0, 0), false);
assert.equal(rect.calls, 0);
});

test('windows over map edges compare the cells they show, with and without wrapping', () => {
const { api, map, rect } = fixture();
// A 2x2 window at (-1, 0) shows column 0 of the map; column 1 is outside it.
assert.equal(api.prepare(map, -1, 0), true);
const painted = rect.calls;
map.tiledData.layers[0].data[1] = 1;
api.prepare(map, -1, 0);
assert.equal(rect.calls, painted);
map.tiledData.layers[0].data[0] = 2;
api.prepare(map, -1, 0);
assert.ok(rect.calls > painted);
// With horizontal wrapping, the same window shows column 1 at x = -1.
map.horizontalWrap = true;
api.prepare(map, -1, 0);
const wrapped = rect.calls;
api.prepare(map, -1, 0);
assert.equal(rect.calls, wrapped);
map.tiledData.layers[0].data[3] = 1;
api.prepare(map, -1, 0);
assert.ok(rect.calls > wrapped);
});