Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to rearrange tiles in a tileset #2983

Merged
merged 18 commits into from
May 20, 2021
Merged

Add ability to rearrange tiles in a tileset #2983

merged 18 commits into from
May 20, 2021

Conversation

jmi2k
Copy link
Contributor

@jmi2k jmi2k commented Feb 7, 2021

WIP: The patch works: I think I've fixed all the bugs I could have introduced and it's ready for code review. However, there are still some details I want to fix before merging. Should fix #1856, or at least the latest requests exposed in that issue.

This PR allows tiles in a tileset to be moved around at will. Previously, the tileset order was fixed and couldn't be changed. That was acceptable for prebuilt tilesets but horrendous for collections of images (after you've added some tile, it was appended to the list couldn't be placed elsewhere, which made working with them a nightmare).

Copy link
Member

@bjorn bjorn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works quite well already! I've left some inline comments on the implementation.

Apart from the comments, there was a little problem with loading old maps, which only include tile elements when the tile was customized in some way. Those tiles will now appear at the start. We need to make sure the order of the tiles is not affected in this case.

I think a possible solution to this would be, to remember the last already found tile index in Tileset::loadFromImage, and to insert tiles after this index in mSortedTileIds instead of appending them (also incrementing this insertion index for each newly created tile).

src/libtiled/mapwriter.cpp Outdated Show resolved Hide resolved
src/libtiled/mapwriter.cpp Outdated Show resolved Hide resolved
src/tiled/tilesetmodel.cpp Show resolved Hide resolved
void TilesetDocument::relocateTile(const Tile *tile, int location)
{
mTileset->relocateTile(tile->id(), location);
/* TODO: is that the right signal? */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is currently the right signal, though it isn't optimal or entirely correct. Ideally we'd send a signal before and after the change, such that the model can call beginResetModel and endResetModel in the appropriate moments. At the same time I'm trying to move away from all the separate signals, instead sending an specialized ChangeEvent in a unified Document::changed signal. I can help with this part of the change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll leave it as-is. If that's the best signal for this scenario (given how signal handling is currently implemented) I think it's better to make it consistent with the rest of the code. Signal handling improvements can come later in a separate change.

src/tiled/tilesetmodel.cpp Outdated Show resolved Hide resolved
src/tiled/tileseteditor.cpp Show resolved Hide resolved
jmi2k and others added 4 commits April 9, 2021 04:18
Also addressed two clang warnings in the TilesetModel
* Don't block mouse press events on non-tiles, which makes for strange
  behavior where the view thinks you're dragging something you previously
  pressed.

* Instead, return nullptr instead of an empty QMimeData instance, to avoid
  going into a drag operation when dragging from a place with no tile.
@bjorn
Copy link
Member

bjorn commented May 19, 2021

@jmi2k I've addressed a few of my own comments today and plan to do another review later this week and address the rest of my feedback, hoping to get this merged.

I think it'd also make sense to look into supporting multi-selection, because moving a lot of tiles can get tedious. Please feel free to still help polish up this feature. I'll be off for the next 16 hours or so. :-)

@jmi2k
Copy link
Contributor Author

jmi2k commented May 19, 2021

Thanks, I'm very sorry I couldn't put some time aside to push this forward. I dismissed multi-selection because it could behave in weird ways. Does it preserve the shape? what does it do when reaching a corner? How does it behave when replacing tiles? I was hoping to get a basic version merged before discussing future improvements

bjorn added 7 commits May 19, 2021 20:59
Now the model needs a TilesetDocument, but that is not a problem because
all tilesets have one (I think that didn't used to be the case when this
model was written).

Now I could also simplify some update connections by moving them to the
model.
Otherwise the preservation of the custom tile ordering depends on there
being some other reason to write out the tiles, for example the tileset
being an image collection.
When tiles based on a tileset image are not re-ordered then only the
ones with relevant meta-information are included in the file. Since
these tiles are now added to the mTiles list earlier, they would appear
at the top in the tileset view.

This problem is fixed by inserting each tile at its expected location,
instead of appending it.
This reverts part of aac7b41. In
AdjustTileMetaData we always need to make sure to copy the meta-data
either to larger indices or smaller indices, which is why we can't just
iterate the list of tiles since it can have a custom order.
@bjorn bjorn changed the title [WIP] Relocate tiles Add ability to rearrange tiles in a tileset May 20, 2021
@bjorn bjorn merged commit 2c10b37 into mapeditor:master May 20, 2021
@bjorn
Copy link
Member

bjorn commented May 20, 2021

Thanks for your help with this change @jmi2k! No worries about not finishing it, I fully understand other things can come up, as also happens for me since I've let #1856 be unresolved for way too long as well.

I've added support for multi-selection in a follow-up change (a794403). It does not preserve shapes, it just moves all selected tiles as a sequence to the target location, in what appears to be the order in which they were selected. Edit: though it does behave rather strangely if you drag tiles from both directions to the middle...

Let's hope we're still on time to help @hepari with some tileset reorganization. The feature is now available in the artifacts resulting from the Build Packages workflow, and should soon (within a few weeks) be part of a Tiled 1.7 release.

@hepari
Copy link

hepari commented May 24, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rearranging Image Collection by Image Name
3 participants