Skip to content

Node's role in MacawView

Yuri Strot edited this page Oct 23, 2018 · 3 revisions

Usually graphics libraries use one of the following approaches to build scene graph:

  1. Each node has exact position in the tree and only one parent.
  2. 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.bounds method. 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.rootView which allows you to get NodeView for the root node and go down to the node you need.
  • Event.view which gives you access to the runtime information on any event. For example, once user click to a node, onTap callback function will receive exact NodeView which force this event, so you can use runtime information to handle the event.
Clone this wiki locally