예제 #1
0
func (v *signalGraphView) Sync() {
	g := v.sgType
	var selected bh.NodeIdIf
	var wasSelected bool
	if v.nodes != nil {
		selected, wasSelected = v.getSelectedNode()
	}
	v.nodes = make([]graph.NodeIf, len(g.Nodes()))

	var numberOfConnections = 0
	for _, n := range g.Nodes() {
		for _, p := range n.OutPorts() {
			numberOfConnections += len(p.Connections())
		}
	}
	v.connections = make([]graph.ConnectIf, numberOfConnections)

	getPositioner := func(nId bh.NodeIdIf) gr.ModePositioner {
		n, ok := g.NodeByPath(nId.String())
		if !ok {
			log.Panicf("getPositioner: could not find node %v\n", nId)
		}
		log.Printf("getPositioner(%v): node=%s\n", nId, n.Name())
		proxy := gr.PathModePositionerProxyNew(n)
		proxy.SetActivePath(nId.Parent().String())
		return proxy
	}
	for i, n := range g.Nodes() {
		nId := freesp.NodeIdFromString(n.Name(), v.graphId)
		if n.Expanded() {
			n.SetActiveMode(gr.PositionModeExpanded)
			v.nodes[i] = graph.ExpandedNodeNew(getPositioner, n, nId)
		} else {
			n.SetActiveMode(gr.PositionModeNormal)
			v.nodes[i] = graph.NodeNew(getPositioner, n, nId)
		}
	}
	var index = 0
	for _, n := range g.Nodes() {
		from := v.findNode(n.Name())
		for _, p := range n.OutPorts() {
			fromId := from.OutPortIndex(p.Name())
			for _, c := range p.Connections() {
				to := v.findNode(c.Node().Name())
				toId := to.InPortIndex(c.Name())
				v.connections[index] = graph.ConnectionNew(from, to, fromId, toId)
				index++
			}
		}
	}
	v.area.SetSizeRequest(v.calcSceneWidth(), v.calcSceneHeight())
	v.drawAll()
	if wasSelected {
		log.Printf("signalGraphView.Sync: select %v after sync\n", selected)
		v.selectNode(selected)
	}
}
예제 #2
0
func ExpandedNodeNew(getPositioner GetPositioner, userObj bh.NodeIf, nId bh.NodeIdIf) (ret *ExpandedNode) {
	positioner := getPositioner(nId)
	pos := positioner.Position()
	path := nId.String()
	config := DrawConfig{ColorInit(ColorOption(NormalExpandedNode)),
		ColorInit(ColorOption(HighlightExpandedNode)),
		ColorInit(ColorOption(SelectExpandedNode)),
		ColorInit(ColorOption(BoxFrame)),
		ColorInit(ColorOption(Text)),
		image.Point{global.padX, global.padY}}
	cconfig := ContainerConfig{expandedPortWidth, expandedPortHeight, 120, 80}
	// Add children
	var g bh.SignalGraphTypeIf
	nt := userObj.ItsType()
	for _, impl := range nt.Implementation() {
		if impl.ImplementationType() == bh.NodeTypeGraph {
			g = impl.Graph()
			break
		}
	}
	var children []ContainerChild
	if g != nil {
		empty := image.Point{}
		first := image.Point{16, 32}
		shift := image.Point{16, 16}
		for i, n := range g.ProcessingNodes() {
			var ch ContainerChild
			var mode gr.PositionMode
			if n.Expanded() {
				mode = gr.PositionModeExpanded
			} else {
				mode = gr.PositionModeNormal
			}
			proxy := gr.PathModePositionerProxyNew(n)
			proxy.SetActivePath(path)
			proxy.SetActiveMode(mode)
			log.Printf("ExpandedNodeNew TODO: position of child nodes. path=%s, mode=%v\n", path, mode)
			chpos := proxy.Position()
			if chpos == empty {
				chpos = pos.Add(first.Add(shift.Mul(i)))
				proxy.SetPosition(chpos)
			}
			id := freesp.NodeIdNew(nId, n.Name())
			if n.Expanded() {
				ch = ExpandedNodeNew(getPositioner, n, id)
			} else {
				ch = NodeNew(getPositioner, n, id)
			}
			children = append(children, ch)
		}
	}
	ret = &ExpandedNode{ContainerInit(children, config, userObj, cconfig),
		userObj, positioner, nil, nil}
	ret.ContainerInit()
	empty := image.Point{}
	config = DrawConfig{ColorInit(ColorOption(InputPort)),
		ColorInit(ColorOption(HighlightInPort)),
		ColorInit(ColorOption(SelectInPort)),
		ColorInit(ColorOption(BoxFrame)),
		Color{},
		image.Point{}}
	for i, p := range userObj.InPorts() {
		pos := p.ModePosition(gr.PositionModeExpanded)
		if pos == empty {
			pos = ret.CalcInPortPos(i)
		}
		positioner := gr.ModePositionerProxyNew(p, gr.PositionModeExpanded)
		ret.AddPort(config, p, positioner)
	}
	config = DrawConfig{ColorInit(ColorOption(OutputPort)),
		ColorInit(ColorOption(HighlightOutPort)),
		ColorInit(ColorOption(SelectOutPort)),
		ColorInit(ColorOption(BoxFrame)),
		Color{},
		image.Point{}}
	for i, p := range userObj.OutPorts() {
		pos := p.ModePosition(gr.PositionModeExpanded)
		if pos == empty {
			pos = ret.CalcOutPortPos(i)
		}
		positioner := gr.ModePositionerProxyNew(p, gr.PositionModeExpanded)
		ret.AddPort(config, p, positioner)
	}
	for _, n := range g.ProcessingNodes() {
		from, ok := ret.ChildByName(n.Name())
		if !ok {
			log.Printf("ExpandedNodeNew error: node %s not found\n", n.Name())
			continue
		}
		for _, p := range n.OutPorts() {
			fromId := from.OutPortIndex(p.Name())
			for _, c := range p.Connections() {
				to, ok := ret.ChildByName(c.Node().Name())
				if ok {
					toId := to.InPortIndex(c.Name())
					ret.connections = append(ret.connections, ConnectionNew(from, to, fromId, toId))
				} else {
					portname, ok := c.Node().PortLink()
					if !ok {
						log.Printf("ExpandedNodeNew error: output node %s not linked\n", c.Node().Name())
						continue
					}
					ownPort, ok := ret.OutPortByName(portname)
					if !ok {
						log.Printf("ExpandedNodeNew error: linked port %s of output node %s not found\n", portname, c.Node().Name())
						continue
					}
					nodePort, ok := from.OutPortByName(p.Name())
					if !ok {
						log.Printf("ExpandedNodeNew error: port %s of output node %s not found\n", p.Name(), from.Name())
						continue
					}
					ret.portconn = append(ret.portconn, PortConnectorNew(nodePort, ownPort))
				}
			}
		}
	}
	for _, n := range g.InputNodes() {
		for _, p := range n.OutPorts() {
			fromlink, ok := p.Node().PortLink()
			if !ok {
				log.Printf("ExpandedNodeNew error: input node %s not linked\n", p.Node().Name())
				continue
			}
			fromPort, ok := ret.InPortByName(fromlink)
			if !ok {
				log.Printf("ExpandedNodeNew error: linked port %s of input node %s not found\n", fromlink, n.Name())
				continue
			}
			for _, c := range p.Connections() {
				to, ok := ret.ChildByName(c.Node().Name())
				if ok {
					// TODO: connect with node
					toPort, ok := to.InPortByName(c.Name())
					if !ok {
						log.Printf("ExpandedNodeNew error: port %s of node %s not found\n", c.Name(), to.Name())
						continue
					}
					ret.portconn = append(ret.portconn, PortConnectorNew(fromPort, toPort))
				} else {
					tolink, ok := c.Node().PortLink()
					if !ok {
						log.Printf("ExpandedNodeNew error: output node %s not linked\n", c.Node().Name())
						continue
					}
					toPort, ok := ret.OutPortByName(tolink)
					if !ok {
						log.Printf("ExpandedNodeNew error: linked port %s of output node %s not found\n", tolink, c.Node().Name())
						continue
					}
					ret.portconn = append(ret.portconn, PortConnectorNew(fromPort, toPort))
				}
			}
		}
	}
	ret.RegisterOnDraw(func(ctxt interface{}) {
		expandedNodeOnDraw(ret, ctxt)
	})
	return
}