From 2f47d11271f6193de3b7d808de5a294bd0ab9233 Mon Sep 17 00:00:00 2001 From: fgenesis Date: Sat, 13 Jul 2024 06:44:41 +0200 Subject: [PATCH] fix editor bug (tile confusion) when moving tiles to front or back while also dragging them --- Aquaria/SceneEditor.cpp | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/Aquaria/SceneEditor.cpp b/Aquaria/SceneEditor.cpp index 38f0e17..988aa36 100644 --- a/Aquaria/SceneEditor.cpp +++ b/Aquaria/SceneEditor.cpp @@ -133,24 +133,31 @@ static void quadToTile(TileData& t, const Quad *q) class MultiTileHelper : public Quad { TileStorage& _ts; - std::vector _indices; std::vector _quads; Vector lastScale; - MultiTileHelper(TileStorage& ts, const size_t *indices, size_t n) + // This NEEDS to be a reference. The tile bulk-manipulation functions that take an array of indices + // as parameters and also update those parameters shall modify only a single source of truth, + // and that's SceneEditor::selectedTiles. + // Otherwise it could happen that indices change in that vector while a previously made copy + // carries around outdated indices, possibly leading to modification of unrelated tiles. + const std::vector & _indices; + + MultiTileHelper(TileStorage& ts, const std::vector& indices) : Quad() - , _ts(ts), _indices(indices, indices + n) + , _ts(ts), _indices(indices) { - _quads.reserve(n); + _quads.reserve(indices.size()); this->cull = false; } public: - static MultiTileHelper *New(unsigned bgLayer, const size_t *indices, size_t n) + static MultiTileHelper *New(unsigned bgLayer, const std::vector& indices) { + const size_t n = indices.size(); assert(n); TileStorage& ts = dsq->tilemgr.tilestore[bgLayer]; - MultiTileHelper *th = new MultiTileHelper(ts, indices, n); + MultiTileHelper *th = new MultiTileHelper(ts, indices); if(n == 1) { @@ -241,7 +248,7 @@ public: void finish() { - size_t n = _indices.size(); + const size_t n = _indices.size(); if(n == 1) { TileData& t = _ts.tiles[_indices[0]]; @@ -250,6 +257,7 @@ public: } else { + assert(n == _quads.size()); for(size_t i = 0; i < n; ++i) { TileData& t = _ts.tiles[_indices[i]]; @@ -2894,7 +2902,7 @@ MultiTileHelper * SceneEditor::createMultiTileHelperFromSelection() assert(!multi); if(selectedTiles.empty()) return NULL; - return (multi = MultiTileHelper::New(bgLayer, &selectedTiles[0], selectedTiles.size())); + return (multi = MultiTileHelper::New(bgLayer, selectedTiles)); } void SceneEditor::destroyMultiTileHelper()