-
Notifications
You must be signed in to change notification settings - Fork 563
Node's role in MacawView
Usually graphics libraries use one of the following approaches to build scene graph:
- Each node has exact position in the tree and only one parent.
- Node can be referenced in a tree several times.
Second approach looks like a better choice which provides additional features, however in the first case we can gives more useful API by implicitly providing access to the runtime information through Node, for example:
- Return node's parent
- Return absolute position/bounds of a node
- etc.
In Macaw we want to combine benefits of both approaches. So here is the proposal.
First, we need to think about Node as about abstract representation of something to draw. Node doesn't relate to any view, it has no size or bounds, no absolute position, etc. It can be used many times in different views. This approach also means that we can build scene graph in any thread and use SVGParser everywhere.
NOTE: We already have
Node.boundsmethod. However you need to think about this method like "what if this node will be drawn on some abstract canvas, where will it be placed?"
Once we associate node to some MacawView you will be able to access NodeView. This protocol represents appearance of the node in the exact position of the exact MacawView. This protocol will looks as follow:
protocol NodeView {
let node: Node
let parent: NodeView?
var bounds: Rect? { get }
var absBounds: Rect? { get }
func contentAt(index: Int) -> NodeView?
// And some more methods...
}Since node can have multiple views, you can't access NodeView from Node. So you have following ways to access this information:
-
MacawView.rootViewwhich allows you to getNodeViewfor the root node and go down to the node you need. -
Event.viewwhich gives you access to the runtime information on any event. For example, once user click to a node,onTapcallback function will receive exactNodeViewwhich force this event, so you can use runtime information to handle the event.