From 16959f57986cffefb558804c275c4b06a1f62ee0 Mon Sep 17 00:00:00 2001 From: cha0s Date: Fri, 8 Apr 2022 09:40:10 -0500 Subject: [PATCH] refactor: efficiency --- .../persea/room-sides/tiles-side/index.jsx | 80 ++++++++----------- 1 file changed, 32 insertions(+), 48 deletions(-) diff --git a/packages/topdown/src/persea/room-sides/tiles-side/index.jsx b/packages/topdown/src/persea/room-sides/tiles-side/index.jsx index 4f6aa8e..359b353 100644 --- a/packages/topdown/src/persea/room-sides/tiles-side/index.jsx +++ b/packages/topdown/src/persea/room-sides/tiles-side/index.jsx @@ -127,19 +127,6 @@ function TilesPage({ const where = Rectangle.compose(origin, size); room.tiles[currentLayer].stampAt(where, stamp); room.tiles[currentLayer].emit('update', where); - const {data} = room.tiles[currentLayer].toJSON(); - if (!data) { - patch({ - op: 'remove', - path: `/tiles/${currentLayer}/data`, - }); - } - else { - patch({ - path: `/tiles/${currentLayer}/data`, - value: data, - }); - } }; const fill = (origin) => { const position = Vector.div([selection[0], selection[1]], tileSize); @@ -196,19 +183,6 @@ function TilesPage({ } const where = Rectangle.compose(min, Vector.add([1, 1], Vector.sub(max, min))); room.tiles[currentLayer].emit('update', where); - const {data} = room.tiles[currentLayer].toJSON(); - if (!data) { - patch({ - op: 'remove', - path: `/tiles/${currentLayer}/data`, - }); - } - else { - patch({ - path: `/tiles/${currentLayer}/data`, - value: data, - }); - } }; const onValue = ( { @@ -217,37 +191,47 @@ function TilesPage({ }, ) => { const origin = Vector.floor(Vector.div(position, tileSize)); + const edit = () => { + const {data: originalData} = room.tiles[currentLayer].toJSON(); + switch (tilesetMode) { + case 0: { + stamp(origin); + break; + } + case 1: { + fill(origin); + break; + } + default: + } + const {data} = room.tiles[currentLayer].toJSON(); + if (data === originalData) { + return; + } + if (!data) { + patch({ + op: 'remove', + path: `/tiles/${currentLayer}/data`, + }); + } + else { + patch({ + path: `/tiles/${currentLayer}/data`, + value: data, + }); + } + }; switch (type) { case 'touchstart': case 'mousedown': { - switch (tilesetMode) { - case 0: { - stamp(origin); - break; - } - case 1: { - fill(origin); - break; - } - default: - } + edit(); setIsHoldingPaint(true); break; } case 'touchmove': case 'mousemove': { if (isHoldingPaint) { - switch (tilesetMode) { - case 0: { - stamp(origin); - break; - } - case 1: { - fill(origin); - break; - } - default: - } + edit(); } break; }