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

Missing Color type of custom property . #12

Open
finscn opened this issue Oct 5, 2021 · 2 comments
Open

Missing Color type of custom property . #12

finscn opened this issue Oct 5, 2021 · 2 comments

Comments

@finscn
Copy link

finscn commented Oct 5, 2021

image

when I set a color-type, I can't check it's type.

the typeof value is object. but the object hasn't any property.

@finscn
Copy link
Author

finscn commented Oct 6, 2021

I write a method for detect property type , but it's not good. I hope TIledMap could supply some ways.

    function parseProperties(obj) {
        // const properties = obj.properties()
        const properties = obj.resolvedProperties()
        const p = {}

        for (const key in properties) {
            const value = properties[key]

            const isObject = typeof value === 'object';
            const isString = typeof value === 'string';
            // const isNumber = typeof value === 'number'
            // const isBoolean = typeof value === 'boolean'

            const str = String(value)

            if (isObject && str.indexOf('Tiled::ObjectRef') === 0) {
                // object, but unset
                continue
            }
            if (isObject && str.indexOf('Tiled::EditableMapObject') === 0) {
                // object
                p[key] = {
                    id: value.id,
                    location: ObjectLocation[value.id]
                }
            } else if (isObject && str.indexOf('#') === 0) {
                // color
                p[key] = {
                    color: str
                }
            } else if (isObject && value && !value.url) {
                // file , but unset
                continue
            } else if (isObject && value && value.url) {
                // file
                p[key] = {
                    file: value.url
                }
            } else if (isString && !value) {
                // string , but unset or ""
                continue
            } else {
                // string number boolean
                p[key] = value
            }

            // console.log(key, typeof value, String(value))
        }

        return p
    }

@bjorn
Copy link
Member

bjorn commented Oct 6, 2021

Right, there is no reliable way to get the type of a property in JavaScript. I think I'll need to add a function like tiled.propertyType(value) that returns its type as string.

Though one question is, in case of custom classes or enums, whether this function should return the name of the custom class or enum, or a generic string like "class" or "enum".

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

No branches or pull requests

2 participants