-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add JSON support for reusable object templates #1667
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
Conversation
Yeah, I initially thought the same of the Sometimes it's nice to give people options but it also becomes quite a burden sometimes and it causes complexity that often also affects people not using those options. :-/ |
{ | ||
QVariantMap templateGroupVariant; | ||
|
||
templateGroupVariant[QLatin1String("firsttid")] = firstTid; | ||
if (firstTid > 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you have essentially two entirely different implementations based on whether a firstTid
is passed or not, I think it would be better to have two separate overloads for this. In fact the version that doesn't take a firstTid
can be inlined in MapToVariantConverter::toVariant(const TemplateGroup &templateGroup, QDir &directory)
.
This is different for the function for tilesets, since those can be embedded.
{ | ||
QVariantMap templateGroupVariant; | ||
|
||
templateGroupVariant[QLatin1String("firsttid")] = firstTid; | ||
if (firstTid > 0) { | ||
templateGroupVariant[QLatin1String("type")] = QLatin1String("templategroup"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually the type
should be set when no firstTid
is passed, and doesn't need to be set when it is.
Okay, the groups file contains just a list of file paths for template groups to be loaded on startup, so it should be simple to use JSON, although I'm not entirely convinced that it might be useful to read them as the path of the group will be also included in the map :). Either way, I'm not sure how to integrate both formats in the UI. Currently the groups XML file is updated automatically with a new path whenever a new group is added or created with no action required from the user. I think it could be done with an export option offered by a button in the dock that offers saving in any of the two formats. What do you think? |
Ah, right, this file is really not very useful and can just be maintained by Tiled in whatever format it wants, so XML is fine. |
Okay 😄 I will support the the template group file for sure. |
Sorry for not being clear in the first description. |
ff4ba6c
to
4d5579b
Compare
I think this PR is now ready. Now all actions are supported on maps and template groups. |
XML format was hard coded as it was the only format, now the enhancements PR should be updated to allow opening JSON template groups as well, this could be done when both PRs are merged and the template format singleton can be removed altogether. |
} | ||
|
||
if (templateGroup) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check is superfluous since the above code always makes sure there is a template group.
src/plugins/json/jsonplugin.cpp
Outdated
|
||
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { | ||
mError = tr("Could not open file for reading."); | ||
return new Tiled::TemplateGroup(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not return nullptr
? (and on the line below)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah that's wrong, I used tileset JSON reader as reference, where the returned shared pointer is actually a nullptr.
I applied the last two review comments,The hardest things while doing this PR was connecting everything together, like finding out that format instances should be added to the plugin manager. at first I thought it would pick them up automatically :). |
Can you rebase this change onto the latest
Ah yeah, this is where some documentation of the overall design could help... |
Also removed the template format singleton.
8d91e1f
to
a6fb85c
Compare
I updated the dialogs to support JSON. The new template group is duplicated from the templates dock in the new template dialog as they both offer creating new groups. Is there a way to reuse the newTemplateGroup dialog directly from the templates dock without duplication? |
Maybe you could create a public static helper function in the |
This PR adds JSON support for the reusable object templates, Tiled should be able to:
The groups file is used internally by Tiled, so I'm not sure if it's necessary to save it in JSON.