hyperandroid / CAAT

Canvas Advanced Animation Toolkit
hyperandroid.github.com/CAAT
MIT License
727 stars 117 forks source link

How to make ActorContainer 'transparent' to mouse events #29

Closed garymcleanhall closed 12 years ago

garymcleanhall commented 12 years ago

I have fixed a problem I had with your otherwise rather amazing CAAT library.

The problem was that I had two ActorContainers, each with their own ShapeActor children. These were both added to a scene and enableDrag was called on each ShapeActor. Only the last-added ActorContainer's children were responding to mouse events. The reason was that the findActorAtPosition() method would return 'this' if none of the children contained the mouse location. For what I was wanting to achieve, I needed only the ShapeActor children to be returned from this method, and never an ActorContainer.

Long story short, you can make an ActorContainer transparent to mouse events by replacing the final return statement of ActorContainer.findActorAtPosition() with this:

return this.constructor == CAAT.ActorContainer ? null : this;

In other words, "given that the mouse is not contained in one of my children, if I am purely an ActorContainer, admit that I haven't found an actor - otherwise, return this concrete subclass of ActorContainer."

robboerman commented 12 years ago

Hi Gary,

Personaly I think the default behavior has it's use as well. I would not want an ActorContainer to stop being selectable/draggable. Wouldn't it be easier to override the behavior in your containers? You could create a custom findActorAtPosition method on your container that just calls the same method on its parent and checks if the return value is the container itself, in that case returning null

Met vriendelijke groet,

Rob Boerman

Op Jan 21, 2012 om 5:54 heeft garymcleanhall reply@reply.github.com het volgende geschreven:

I have fixed a problem I had with your otherwise rather amazing CAAT library.

The problem was that I had two ActorContainers, each with their own ShapeActor children. These were both added to a scene and enableDrag was called on each ShapeActor. Only the last-added ActorContainer's children were responding to mouse events. The reason was that the findActorAtPosition() method would return 'this' if none of the children contained the mouse location. For what I was wanting to achieve, I needed only the ShapeActor children to be returned from this method, and never an ActorContainer.

Long story short, you can make an ActorContainer transparent to mouse events by replacing the final return statement of ActorContainer.findActorAtPosition() with this:

return this.constructor == CAAT.ActorContainer ? null : this;

In other words, "given that the mouse is not contained in one of my children, if I am purely an ActorContainer, admit that I haven't found an actor - otherwise, return this concrete subclass of ActorContainer."


Reply to this email directly or view it on GitHub: https://github.com/hyperandroid/CAAT/issues/29

garymcleanhall commented 12 years ago

Yeah, it'd certainly be more maintainable, but I'm just doing some mockups with CAAT at the moment to see if it meets my requirements. When I firm this up, I'll create a flag on ActorContainer or a new subclass to house this behavior.

Thanks

hyperandroid commented 12 years ago

Hi Gary,

the behavior for input routing is the same as that of DOM events, where on overlapping elements only the top most z-indexed one will receive notifications. CAAT defines a tree in its Scene graph, and thus, events get sent to top-most elements. You solutions assumes you don't want a container to be a valid event's endpoint. Rob's suggestion will give a valid solution for your requirements, but i think it'll too specific for CAAT. Thanks for your suggestion.

best, -ibon