Skip to content

Conversation

thabetx
Copy link
Contributor

@thabetx thabetx commented Jul 30, 2017

This PR adds JSON support for the reusable object templates, Tiled should be able to:

  • Write template instances.
  • Read template instances.
  • Write template groups.
  • Read template groups.

The groups file is used internally by Tiled, so I'm not sure if it's necessary to save it in JSON.

@thabetx thabetx changed the title Json templates Add JSON support for reusable object templates Jul 30, 2017
@bjorn
Copy link
Member

bjorn commented Jul 30, 2017

The groups file is used internally by Tiled, so I'm not sure if it's necessary to save it in JSON.

Yeah, I initially thought the same of the objecttypes.xml file, but then people who used the JSON format for maps/tilesets had a reason to parse that file as well and asked for it in JSON format. I guess we'll run into a similar situation for the templates, since game engines will need to load them to understand what the objects are.

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) {
Copy link
Member

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");
Copy link
Member

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.

@thabetx
Copy link
Contributor Author

thabetx commented Jul 30, 2017

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?

@bjorn
Copy link
Member

bjorn commented Jul 30, 2017

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 :).

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.

@thabetx
Copy link
Contributor Author

thabetx commented Jul 30, 2017

Okay 😄 I will support the the template group file for sure.

@thabetx
Copy link
Contributor Author

thabetx commented Jul 30, 2017

Sorry for not being clear in the first description.

@thabetx thabetx force-pushed the JSON-templates branch 2 times, most recently from ff4ba6c to 4d5579b Compare July 31, 2017 07:03
@thabetx
Copy link
Contributor Author

thabetx commented Jul 31, 2017

I think this PR is now ready. Now all actions are supported on maps and template groups.

@thabetx
Copy link
Contributor Author

thabetx commented Jul 31, 2017

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)
Copy link
Member

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.


if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
mError = tr("Could not open file for reading.");
return new Tiled::TemplateGroup();
Copy link
Member

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)

Copy link
Contributor Author

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.

@thabetx
Copy link
Contributor Author

thabetx commented Jul 31, 2017

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 :).

@bjorn
Copy link
Member

bjorn commented Jul 31, 2017

Can you rebase this change onto the latest wip/templates now that #1666 is merged?

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 :).

Ah yeah, this is where some documentation of the overall design could help...

@thabetx thabetx force-pushed the JSON-templates branch 2 times, most recently from 8d91e1f to a6fb85c Compare August 1, 2017 07:46
@thabetx
Copy link
Contributor Author

thabetx commented Aug 1, 2017

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?

@bjorn
Copy link
Member

bjorn commented Aug 14, 2017

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 NewTemplateDialog that takes the needed parameters, so that you can call this from the TemplatesDock as well. For the rest the PR looks good so I'll merge it now.

@bjorn bjorn merged commit a6fb85c into mapeditor:wip/templates Aug 14, 2017
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.

2 participants