addObjectDrawListener - will this work for an object that is not actually drawn? (e.g. to use to create a custom image of the same size)

addObjectDrawListener - will this work for an object that is not actually drawn?  (e.g. to use to create a custom image of the same size)

(i.e. use an object rectangle, which can be rotated, resized, as a means to create an image programmatically later which fits exactly - i.e. a way to get around the fact that you can’t resize an inserted tile image in a object layer in Tiled)

any other ideas welcomed  :)

Hi Dyson - tried this (i.e. addObjectDrawListener(name, listener)) but could not seem to get it working.  I couldn’t find an example in the samples that I could see.   Then I noted it’s description is “…which monitors the Tiled Object draw functions for the creation of an object with the matching name…”.     So does this mean it would work for the “Insert Tile” on an object layer but not the “Insert Rectangle” on an object layer?  

With the goal of trying to support placing images (albeit via putting them into tiles first) on the level in Tiled and being able to place, rotate, resize them…noting that Tiled doesn’t support resizing images…then if I want to use the Tiled object layer’s “Add Rectangle” object as the basis for placing them (not ideal as they are not rendered in Tiled) and then programmatically create the image myself, then what would be the best approach in MTE for this?  

I’m thinking ideally you should use MTE’s ability to create/remove the images as required, i.e. the whole point of MTE.  So this would mean one would require a generic listener perhaps in MTE to pick up all of the object creation & deletions.  Rather than passing an object name it would perhaps be best to keep the listener more open/generic,  So perhaps: 

local function objectLayerListenerFunction(event)      local phase = event.phase   -- either "create", or "dispose"     local target = event.target    -- to give you a handle to the object     print(target.type)                   -- prints Object Type from Tiled     print(target.myCustomPropertyFieldNameFromTiled)   -- Can get all the tiled name properties     -- then could     if (event.phase == "create") then         -- create your image     else          -- dispose the image     end end mte.addObjectLayerListener(objectLayerListenerFunction)  

Would adding such an “addObjectLayerListener” be possible?    Is there another way of doing this already I’m missing?

Hello Greg,

It monitors for any Tiled Object being drawn as a result of calling drawObjects() whose name matches the name argument. Tile images placed on an object layer always draw in this situation because they are implicitly visual objects. The listener will fire for polygons, boxes, polylines, etc, if any of those has properties which imply that it is meant to be drawn. So, giving an polygon a lineWidth and lineColor property will cause it to trigger the listener when drawObjects() executes. Otherwise the object is just abstract data and is never drawn.

You could give your rectangle objects a common name such as “bound” along with a lineColor of [1, 1, 1, 0], which is fully transparent. Then the object will trigger the listener as you desire. In this case, because of the common name, you’d want to use a property to define which rectangle is for which image and all that jazz. 

By the way, you can change the size of tiles added to an object layer when the map displays by creating a levelWidth and levelHeight property for that object in Tiled. At the moment the tile behaves as if it is anchored at the top left corner. When you run the map it will display at the levelWidth and levelHeight you entered.

I’m still working on rotation support.

thanks Dyson - rotation support will be good

re main discussion:  Can I just clarify with a few questions:

a) Does MTE support objects (tiled image or rectangles/circles) being created/removed as they come into the view area like tiles in the Tiled layers?  (i.e. does it perform culling?)

b) If yes, my follow up question is whether the listener fires as objects are create/removed, or just upfront on initial map creation when I call “drawObjects”

c) effectively trying to find out if I use the “rectangle” object as a means to place images myself, whether I should be doing this upfront directly after “drawObjects” and iterate through all objects, OR should I use the listener?    I guess using the listener might be best in any case, as if the answer to question (a) currently is “no”, you might update this in the future?  

Oh also, 

d) would it be possible to get wildcard or regex support for the name matching?   Then one could do a “mte.addObjectDrawListener(”*", onTestRectObject)" for example to pickup all objects.   Is there a specific line in the code I could change for the moment here?  

A) MTE does not cull Tiled Objects, only map tiles.

B ) The listener is fired during your call to drawObjects().

C) Object culling is something I’d like to do, but again, it comes down to time! I have little to spare, and I’ll have much less soon.

D) You’ll want to make a change in the drawObject function in mte.lua at line 5112. Down at line 6561 near the bottom of the function you’ll see this:

if object.name == key then

Changing it like so will fire the event for all objects when “*” is used:

if object.name == key or key == "\*" then

Hi Dyson - noting objects are not culled I’ve been doing the below, i.e. just iterating over the object myself and create my own objects then adding to the “spriteGroup”.  Is this ok?  Am I losing any functionality by not using the object that MTE might create?  (seems to work ok for me)

    -- Setup     local allObjects = mte.getObject( {layer=objLayer} )     local spriteLayerNumber = mte.getSpriteLayer(1)     local spriteGroup = mte.getLayerObj( {layer = spriteLayerNumber} )          -- Review all Objects     for i,disObj in ipairs(allObjects) do         -- Handle Case 1 (Wind)         if Utils.stringsMatchCaseInsensitive(disObj.type, "wind") then             -- CREATE NEW CORONA OBJECT AND ADD TO GROUP:  "spriteGroup"         end     end   

   

You won’t lose any functionality greg886, nothing difficult to add yourself anyway. The drawObjects function does the same thing internally. On the other hand, sprites are not culled either. Sprite culling would introduce a whole host of new hoops users would have to jump through. The added complexity would get in the way, not to mention break all of Corona’s native functions for moving sprites.

Hi Dyson - tried this (i.e. addObjectDrawListener(name, listener)) but could not seem to get it working.  I couldn’t find an example in the samples that I could see.   Then I noted it’s description is “…which monitors the Tiled Object draw functions for the creation of an object with the matching name…”.     So does this mean it would work for the “Insert Tile” on an object layer but not the “Insert Rectangle” on an object layer?  

With the goal of trying to support placing images (albeit via putting them into tiles first) on the level in Tiled and being able to place, rotate, resize them…noting that Tiled doesn’t support resizing images…then if I want to use the Tiled object layer’s “Add Rectangle” object as the basis for placing them (not ideal as they are not rendered in Tiled) and then programmatically create the image myself, then what would be the best approach in MTE for this?  

I’m thinking ideally you should use MTE’s ability to create/remove the images as required, i.e. the whole point of MTE.  So this would mean one would require a generic listener perhaps in MTE to pick up all of the object creation & deletions.  Rather than passing an object name it would perhaps be best to keep the listener more open/generic,  So perhaps: 

local function objectLayerListenerFunction(event)      local phase = event.phase   -- either "create", or "dispose"     local target = event.target    -- to give you a handle to the object     print(target.type)                   -- prints Object Type from Tiled     print(target.myCustomPropertyFieldNameFromTiled)   -- Can get all the tiled name properties     -- then could     if (event.phase == "create") then         -- create your image     else          -- dispose the image     end end mte.addObjectLayerListener(objectLayerListenerFunction)  

Would adding such an “addObjectLayerListener” be possible?    Is there another way of doing this already I’m missing?

Hello Greg,

It monitors for any Tiled Object being drawn as a result of calling drawObjects() whose name matches the name argument. Tile images placed on an object layer always draw in this situation because they are implicitly visual objects. The listener will fire for polygons, boxes, polylines, etc, if any of those has properties which imply that it is meant to be drawn. So, giving an polygon a lineWidth and lineColor property will cause it to trigger the listener when drawObjects() executes. Otherwise the object is just abstract data and is never drawn.

You could give your rectangle objects a common name such as “bound” along with a lineColor of [1, 1, 1, 0], which is fully transparent. Then the object will trigger the listener as you desire. In this case, because of the common name, you’d want to use a property to define which rectangle is for which image and all that jazz. 

By the way, you can change the size of tiles added to an object layer when the map displays by creating a levelWidth and levelHeight property for that object in Tiled. At the moment the tile behaves as if it is anchored at the top left corner. When you run the map it will display at the levelWidth and levelHeight you entered.

I’m still working on rotation support.

thanks Dyson - rotation support will be good

re main discussion:  Can I just clarify with a few questions:

a) Does MTE support objects (tiled image or rectangles/circles) being created/removed as they come into the view area like tiles in the Tiled layers?  (i.e. does it perform culling?)

b) If yes, my follow up question is whether the listener fires as objects are create/removed, or just upfront on initial map creation when I call “drawObjects”

c) effectively trying to find out if I use the “rectangle” object as a means to place images myself, whether I should be doing this upfront directly after “drawObjects” and iterate through all objects, OR should I use the listener?    I guess using the listener might be best in any case, as if the answer to question (a) currently is “no”, you might update this in the future?  

Oh also, 

d) would it be possible to get wildcard or regex support for the name matching?   Then one could do a “mte.addObjectDrawListener(”*", onTestRectObject)" for example to pickup all objects.   Is there a specific line in the code I could change for the moment here?  

A) MTE does not cull Tiled Objects, only map tiles.

B ) The listener is fired during your call to drawObjects().

C) Object culling is something I’d like to do, but again, it comes down to time! I have little to spare, and I’ll have much less soon.

D) You’ll want to make a change in the drawObject function in mte.lua at line 5112. Down at line 6561 near the bottom of the function you’ll see this:

if object.name == key then

Changing it like so will fire the event for all objects when “*” is used:

if object.name == key or key == "\*" then

Hi Dyson - noting objects are not culled I’ve been doing the below, i.e. just iterating over the object myself and create my own objects then adding to the “spriteGroup”.  Is this ok?  Am I losing any functionality by not using the object that MTE might create?  (seems to work ok for me)

    -- Setup     local allObjects = mte.getObject( {layer=objLayer} )     local spriteLayerNumber = mte.getSpriteLayer(1)     local spriteGroup = mte.getLayerObj( {layer = spriteLayerNumber} )          -- Review all Objects     for i,disObj in ipairs(allObjects) do         -- Handle Case 1 (Wind)         if Utils.stringsMatchCaseInsensitive(disObj.type, "wind") then             -- CREATE NEW CORONA OBJECT AND ADD TO GROUP:  "spriteGroup"         end     end   

   

You won’t lose any functionality greg886, nothing difficult to add yourself anyway. The drawObjects function does the same thing internally. On the other hand, sprites are not culled either. Sprite culling would introduce a whole host of new hoops users would have to jump through. The added complexity would get in the way, not to mention break all of Corona’s native functions for moving sprites.