Skip to content

Added drag-to-create maps in the world view - #4590

Open
kanishka0411 wants to merge 22 commits into
mapeditor:masterfrom
kanishka0411:feature/create-delete-maps
Open

kanishka0411 wants to merge 22 commits into
mapeditor:masterfrom
kanishka0411:feature/create-delete-maps

Conversation

@kanishka0411

@kanishka0411 kanishka0411 commented Aug 13, 2026 •

Copy link
Copy Markdown
Contributor

With the World Tool active, a new map can now be created by dragging out arectangle in empty world space. The rectangle snaps to the world grid, with ctrl bypassing the snapping and Escape or right-click cancelling the drag.
The new map takes its orientation, tile size, render order and tilesets from the nearest map in the world, and is saved immediately as untitled-N.tmx alongside the .world file, since worlds refer to their maps by file name.

@kanishka0411
kanishka0411 marked this pull request as draft August 13, 2026 18:19
@kanishka0411

Copy link
Copy Markdown
Contributor Author

While working on this i ran into a few things i wasn't sure about-

  • Right now undoing a create also moves the new file to the trash, so you don't end up with leftover untitled files lying around. but that means the undo command is doing file operations and nothing else in the codebase doesthat. Is that okay or would you rather the file gets written outside the undo stack?
  • Should the new map open in a tab right away or is it fine that it just shows up in the world view?
  • The new file gets saved next to the .world file, should it go next to the nearest map instead? like in sticker-knight all the maps live in map/

@Ablu

Ablu commented Aug 18, 2026

Copy link
Copy Markdown
Contributor
  • Right now undoing a create also moves the new file to the trash, so you don't end up with leftover untitled files lying around. but that means the undo command is doing file operations and nothing else in the codebase doesthat. Is that okay or would you rather the file gets written outside the undo stack?

I think the most user-friendly solution would be to allow an unsaved map to be added to an (unsaved) world view. That would allow creating the new map just like the "New Map" option (which creates a new map without saving it yet). It would also save the user from ending up with many untitled-{}.tmx maps as there does not seem to be a way within tiled to rename maps? But adding unsaved maps to the world view probably complicates the saving procedure (maybe the whole project always would have to be saved at once?).

I think this needs @bjorn to decide :)

  • Should the new map open in a tab right away or is it fine that it just shows up in the world view?

I think it would make sense to open it right away. But the open is just a click away anyway. So not sure how important it it.

  • The new file gets saved next to the .world file, should it go next to the nearest map instead? like in sticker-knight all the maps live in map/

Yeah, this one seems tricky too... I think next to the .world file is probably the only reasonable choice one can make. map/ seems highly opinionated and is unlikely to match what other projects want. Again, this problem would go away if we would allow unsaved maps to be added. In that case the user would get prompted for the location on the first save of the map...

@bjorn

bjorn commented Aug 18, 2026

Copy link
Copy Markdown
Member

Due to the way WorldMapEntry refers to maps by their file name, I'd suggest to keep the automatic saving in this PR (and deleting, but I'd say only when the map has no content).

But consider making the necessary changes to allow maps to stay unsaved and be saved only on-demand (either when user triggers save on map, or tries to save the world). I think that could be done by keeping a MapDocumentPtr in the WorldMapEntry to keep the map loaded along with the world. You can make it as separate commit, and if that change seems small enough, you can make it part of this PR.

@kanishka0411
kanishka0411 marked this pull request as ready for review August 20, 2026 18:09
Comment thread src/tiled/worlddocument.h
struct UnsavedMap {
MapDocumentPtr mapDocument;
QRect rect;
};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Mainly curious for now, but why did you decide to add a separate collection for this, rather than adding a MapDocumentPtr member to WorldMapEntry?

@kanishka0411 kanishka0411 Aug 23, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Because WorldMapEntry is in libtiled and MapDocument is editor side, so the pointer can't go in there. I agree the separate list isn't ideal though,that's also why move/resize of unsaved maps are still open. A middle way could be making them normal entries with an empty fileName and a non-owningMap* just for identity, while WorldDocument keeps the MapDocumentPtr alive.would that work?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hmm, right, I had missed that. I don't think non-owning Map* is a good idea, it sounds too fragile to make sure those pointers stay valid.

I think we can stick with the separate collection in WorldDocument for these maps, but patch up WorldManager::worldForMap to take the MapDocument* instead of just its file name, so that it can look for maps by pointer as well.

@bjorn

bjorn commented Aug 23, 2026

Copy link
Copy Markdown
Member

When I click on an unsaved map in the world, it opens in its own tab, no longer showing the world, because worldManager.worldForMap works based on a file name only, it currently can't return the world.

@kanishka0411

Copy link
Copy Markdown
Contributor Author

When I click on an unsaved map in the world, it opens in its own tab, no longer showing the world, because worldManager.worldForMap works based on a file name only, it currently can't return the world.

yeah I think this also comes from the same root cause, the scene looks up the current map's world by file name. so if we give the entry a Map*identity like i mentioned above, the scene can fall back to looking up unsaved maps by pointer and this would get fixed

This keeps the world and its grid visible when an unsaved map is open,
and makes Remove from World work for unsaved maps too. Also fixed the
selection rectangle staying on a map that was removed from the scene.
@kanishka0411
kanishka0411 requested a review from bjorn August 24, 2026 18:27
@bjorn

bjorn commented Aug 26, 2026

Copy link
Copy Markdown
Member

I haven't looked very deeply at the code yet, but in testing I noticed a few problems with the unsaved maps:

  • Pressing them immediately selects them, whereas for saved maps this happens on release. It seems abrupt and I think it should better be consistent.

  • Creating more new maps with an unsaved map selected does not create add new maps in the right position in the world.

  • The first time you switch to a new map in a world, the view displays it fitting in the view. This is the normal behavior when a map becomes visible the first time, but in the case of switching to a new map within a world, we should suppress or override this somehow. I kind of wonder now why this doesn't happen when switching to saved maps which were not opened before yet.

But it's nice to be able to add a map like this, especially in combination with the world grid! I do worry a little that it becomes too easy to trigger by accident, since adding a new map to a world can be a pretty rare thing to do.

Comment thread src/tiled/abstractworldtool.cpp Resolved
Unsaved maps now open on release like saved maps, keeping the current
view position and zoom. And fixed the position of maps created while
an unsaved map is open.
Resizing from the left or top edge now keeps the content in place like
it does for saved maps, by updating the rect kept by the world document
as part of the resize.
@kanishka0411
kanishka0411 requested a review from bjorn September 5, 2026 20:33
bjorn added 4 commits November 1, 2026 19:27
Maps created in the world view had no file to reference in the world
file and were silently dropped when the world was saved, unloaded or
Tiled quit. Saving the world now offers to save each unsaved map
first, which promotes it to a regular world entry. Cancelling the
save of a map aborts the world save.

A world that still contains unsaved maps is now considered modified,
so it gets the modified marker and prompts on unload or quit, even
when its undo stack is clean (for example after a reload). The
"Unload All Worlds" action also no longer discards modifications
without prompting.
The "Delete ... from Disk" action closed an open tab without asking,
silently discarding unsaved changes. The confirmation dialog now
mentions that unsaved changes will be lost.

The open tab is now looked up by document pointer instead of by
canonical path, which also removes the need for the lookup to happen
while the file still exists.
I think it's probably better to be a little more deterministic regarding
the parameters for the new map, so always base them on the current map
properties rather than the nearest one (which might not even have been
loaded yet).

Also rely on Map::parameters() rather than implementing a manual copy,
and fix the missing compressionLevel and chunkSize properties.
@bjorn

bjorn commented Nov 1, 2026

Copy link
Copy Markdown
Member

I'm sorry it took me so long to get back to this. I had another look at the behavior and there were still a number of issues:

  • "Unload All Worlds" didn't care to notify users about any worlds having unsaved changes. This was an existing problem.
  • When saving a world that contained unsaved maps, the unsaved maps were silently dropped from the saved file, but the world is marked as saved anyway. This meant that afterwards one can unload the world, and the new unsaved maps will be gone from the world.

I have pushed a fix for the above issues.

Also, when using the new "Delete from Disk" action on a map file with unsaved changes, the unsaved changes are lost forever because the trashed file is the old version. I have adjusted the confirmation to make this clear.

The World::contextMaps(const QString &fileName) override was nowhere used, so I've removed it.

I've changed AbstractWorldTool::createMapAt to use Map::parameters() to copy over the parameters from the template map, and fixed the missing copy of compressionLevel and chunkSize (I wonder if we should add an accessor and setter for entire EditorSettings, but I've left that for now).

I've decided to remove the "nearest map" search, because I think it's better to just use the currently active map as a template for the new one. That should be more predictable for the user, and doesn't require this strange logic of potentially loading a map on-demand just to use it as a template for the new map.

Should the new map open in a tab right away or is it fine that it just shows up in the world view?

Yes, I feel like it should probably switch to the new map right away.

@bjorn

bjorn commented Nov 1, 2026 •

Copy link
Copy Markdown
Member

There are a number of odd behaviors which we should probably find good answers to:

  • Undo after delete re-adds an entry for the trashed file.

  • I feel like it's too easy to create new maps in a world by accident, so the drag to create a new map mode should probably be explicitly activated by an action on the tool bar, with potentially a quick shortcut being to drag while holding Ctrl for example.

  • It seems odd that I can't delete the active map, but of course it's not clear what other map we should switch to then and even bigger issue is when it is the last one. Maybe to be resolved when the "current world" is no longer derived from the current map.

  • Directly after deleting a map I get the message "Error Reloading Map" due to "File not found".

  • The delete action adds an undoable RemoveMapCommand to the world, but since the deletion can't be undone, this makes little sense. Does this help people who perform this undo after restoring the file from their trash, or should we in this case remove the map without undo command and call QUndoStack::resetClean instead?

  • Deleting a map only removes it from one world, but sadly we can have multiple worlds loaded that refer to a map. Maybe we should remove the map from all its worlds. This is of course something that also causes issues in other cases.

  • World entries can get duplicated when undoing the add after saving the file, and then redoing it, because addUnsavedMap appends an entry for the map unconditionally regardless of whether it might in the meantime have gained a file name.

  • Create new map and delete map actions still need to be documented.

Minor things.

  • We should probably rename "Delete ... from Disk" to "Move ... to Trash".
  • Maybe trashed documents shouldn't be added to recent files, maybe they should even be automatically removed from there (which might anyway be easier to implement).
  • mClickedUnsavedMap should potentially use QPointer, like mTargetMap, to avoid crash when things happen between press/release.
  • AddUnsavedMapCommand and RemoveUnsavedMapCommand should probably be merged.

This branch has not been deployed

No deployments
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.

3 participants