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

Support Tiled 1.9 class property #238

Merged
merged 5 commits into from Dec 12, 2022

Conversation

barthap
Copy link
Contributor

@barthap barthap commented Dec 7, 2022

Why

Closes #234

In Tiled 1.9 the "type" property was renamed to "class". My PR addresses this.

How

In addition to "type", the "class" property is parsed. If the former is not present, the latter takes its place. This way, the solution is compatible with both 1.9+ and older maps.

After the below conversation, this is what I did:

  • Deprecated obj_type for Objects and tile_type for Tiles in favor of user_type. Added deprecation notices
  • Added user_type to Maps, Tilesets and Layers.
  • Both class and type XML params now resolve to user_type everywhere.

Support for type/class is now implemented for the following entities:

  • Tiles in tileset (Tile)
  • Objects
  • Maps
  • Tilesets
  • Layers

These are all places that I'm aware of.

Test plan

Tested locally on both old and new maps.
I didn't add tests as I haven't found any existing ones (nor test fixtures) which have the "type" or "class" property set. Eventually, I can create/modify one if requested.

@aleokdev
Copy link
Contributor

aleokdev commented Dec 7, 2022

Thank you! Could you also update the changelog (On the "Unreleased" section)?

@barthap barthap marked this pull request as ready for review December 7, 2022 19:19
@barthap
Copy link
Contributor Author

barthap commented Dec 7, 2022

Ouch, actually skipping CI blocked the required workflow from running 🙄 Force-push might help Fixed

@aleokdev
Copy link
Contributor

aleokdev commented Dec 7, 2022

@bjorn Could you check this out? I'm still not 100% familiar with Tiled 1.9 changes

@barthap
Copy link
Contributor Author

barthap commented Dec 8, 2022

Actually, I found out, that layers (all kinds of them?) and maps can have a class property too.

So my PR only fixes this for existing entities which previously had type. I can add it to the others, but how the property should be named? I thought about map_type etc.. to keep the convention, but for layers there is already a (private) layer_type so I'm not sure how to name it. I don't know your breaking change policy, but maybe let's change this to property named {obj,map,tileset,layer}_class: String? 🤔

@aleokdev
Copy link
Contributor

aleokdev commented Dec 8, 2022

Bjorn plans to rename class back to type on 1.10, so I don't think the rename is worth it:

[...] that rename didn't go very well, everybody hates their project breaking due to some arbitrary format change. So I plan to rename "class" back to "type", [...]

@barthap
Copy link
Contributor Author

barthap commented Dec 8, 2022

Bjorn plans to rename class back to type on 1.10, so I don't think the rename is worth it

Ok, in this case it would be good to just add support for the type/class property to all entities, without renaming it. I pushed a proposal commit.

@bjorn
Copy link
Member

bjorn commented Dec 8, 2022

If we go for user_type then I think we can consistently use this name rather than map_type and tileset_type and such. I realize we already have obj_type, which made sense when it only applied to objects, but maybe it could be renamed to user_type as well now.

And indeed, I plan to rename class back to type for Tiled 1.10, in which case it would be type everywhere else too. While we're at it, it might be good to already support this.

@barthap
Copy link
Contributor Author

barthap commented Dec 10, 2022

Yes, in this case, using the user_type everywhere makes the most sense. I've updated my PR to:

  • Deprecated obj_type for Objects and tile_type for Tiles in favor of user_type. Added deprecation notices
  • Added user_type to Maps, Tilesets and Layers.
  • Both class and type XML params now resolve to user_type everywhere.

I have found one inconsistency. For tiles, the tile_type was of type Option<String>, and for Objects it was just String and optional unwrapped to empty string.
Which way should be preferred? I'd go with the optional type instead of empty strings everywhere, but for now I left it as-is for objects.

src/objects.rs Outdated Show resolved Hide resolved
src/tile.rs Outdated Show resolved Hide resolved
@aleokdev
Copy link
Contributor

I have found one inconsistency. For tiles, the tile_type was of type Option<String>, and for Objects it was just String and optional unwrapped to empty string.

This inconsistency also exists in the TMX docs, so I guess we should leave it as is. @bjorn Thoughts?

@aleokdev
Copy link
Contributor

Eh, it's good enough 🙂

@aleokdev aleokdev merged commit f8d853e into mapeditor:current Dec 12, 2022
@aleokdev
Copy link
Contributor

Thank you! 🙏

CHANGELOG.md Show resolved Hide resolved
@bjorn
Copy link
Member

bjorn commented Dec 15, 2022

This inconsistency also exists in the TMX docs, so I guess we should leave it as is. @bjorn Thoughts?

Hmm, I guess the docs have to be fixed. While this property is optional in the file, storage wise I've always defaulted to an empty string rather than having an explicit "type has not been specified" state. Is it helpful to have such a state in Rust?

@aleokdev
Copy link
Contributor

Hmm, I guess the docs have to be fixed. While this property is optional in the file, storage wise I've always defaulted to an empty string rather than having an explicit "type has not been specified" state. Is it helpful to have such a state in Rust?

Is it helpful to distinguish between having an empty text attribute vs not having it at all? I don't know 🙂 Depends on the format and editor, not the language really

@barthap barthap deleted the barthap/class-field-support branch December 16, 2022 10:05
@barthap
Copy link
Contributor Author

barthap commented Dec 16, 2022

Regarding Option vs empty string, I have mixed feelings about that:

  • From my perspective, the option seems to be more "correct" from the language point of view, which means the value hasn't been set. An empty string suggests that a value exists.
  • On the other hand, in this particular case it's more convenient to compare against empty string than option:
    // option
    if tile.user_type.as_deref() == Some("light_source") { /* ... */ }
    
    // empty string
    if tile.user_type == "light_source" { /* ... */ }

@bjorn
Copy link
Member

bjorn commented Dec 17, 2022

Right, then I think we should not use Option. Since it is both less convenient, and actually in the editor a "type has not been set" state does not exist either, or equals the value being an empty string.

@aleokdev aleokdev mentioned this pull request Feb 13, 2023
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.

since tiled 1.9 object type is renamed into class
3 participants