Exemple #1
0
func ensureChild(xn *xmlx.Node, name string) (sn *xmlx.Node) {
	if sn = subNode(xn, name); sn == nil {
		sn = xmlx.NewNode(xn.Type)
		sn.Name.Local = name
		xn.AddChild(sn)
	}
	return
}
Exemple #2
0
func convertSurface(xn *xmlx.Node) {
	var (
		imgNode, imgCreateNode, imgCreateFormatNode, rn, sn, tn *xmlx.Node
		ensureCreateNode                                        = func() *xmlx.Node {
			if imgCreateNode == nil {
				if rn = subNode(imgNode, "init_from"); rn != nil {
					imgNode.RemoveChild(rn)
				}
				imgCreateNode = ensureChild(imgNode, "create_"+strings.ToLower(attVal(xn, "type")))
				if rn != nil {
					imgCreateNode.AddChild(rn)
				}
			}
			return imgCreateNode
		}
		ensureCreateFormatNode = func(exact, hint bool) *xmlx.Node {
			imgCreateFormatNode = ensureChild(ensureCreateNode(), "format")
			if hint {
				ensureChild(imgCreateFormatNode, "hint")
			}
			if exact {
				ensureChild(imgCreateFormatNode, "exact")
			}
			return imgCreateFormatNode
		}
	)
	surfaceNodes = append(surfaceNodes, xn)
	myID, imgID, initNode := attVal(xn.Parent, "sid"), "", subNode(xn, "init_as_target")
	if len(myID) == 0 {
		myID = attVal(xn.Parent, "ref")
	}
	if myID = ustr.StripPrefix(myID, "#"); len(myID) > 0 {
		if initNode != nil {
			imgID = fmt.Sprintf("img_target_%v", time.Now().UnixNano())
			imgNode, rn = xmlx.NewNode(xn.Type), xmlx.NewNode(xn.Type)
			imgNode.Name.Local, rn.Name.Local = "image", "renderable"
			setAttr(imgNode, "id", imgID, false)
			setAttr(rn, "share", "true", false)
			imgNode.AddChild(rn)
			moveNode(imgNode, nil, "library_images")
		} else if initNode = subNode(xn, "init_from"); initNode != nil {
			imgID = initNode.Value
		} else {
			for _, sn = range xn.Children {
				if strings.HasPrefix(sn.Name.Local, "init_") {
					initNode = sn
					break
				}
			}
			if initNode != nil {
				for _, sn = range initNode.Children {
					if imgID = attVal(sn, "ref"); len(imgID) > 0 {
						break
					}
				}
			}
		}
		if imgID = ustr.StripPrefix(imgID, "#"); len(imgID) > 0 {
			surfaceImages[myID] = imgID
			if imgNode == nil {
				for _, sn = range subNode(srcDoc.Root.Children[0], "library_images").Children {
					if attVal(sn, "id") == imgID {
						imgNode = sn
						break
					}
				}
			}
		}
		if imgNode != nil {
			if tn = subNode(xn, "format"); tn != nil {
				subNode(ensureCreateFormatNode(true, false), "exact").Value = tn.Value
			}
			if tn = subNode(xn, "format_hint"); tn != nil {
				rn = subNode(ensureCreateFormatNode(false, true), "hint")
				if sn = subNode(tn, "channels"); sn != nil {
					setAttr(rn, "channels", sn.Value, false)
				}
				if sn = subNode(tn, "range"); sn != nil {
					setAttr(rn, "range", sn.Value, false)
				}
				if sn = subNode(tn, "precision"); sn != nil {
					setAttr(rn, "precision", sn.Value, false)
				}
				if sn = subNode(tn, "option"); sn != nil {
					setAttr(rn, "space", sn.Value, false)
				}
			}
			if tn = subNode(xn, "size"); tn != nil {
				vals := ustr.Split(tn.Value, " ")
				rn = ensureChild(ensureCreateNode(), "size_exact")
				if len(vals) > 0 {
					setAttr(rn, "width", vals[0], false)
				}
				if len(vals) > 1 {
					setAttr(rn, "height", vals[1], false)
				}
				if len(vals) > 2 {
					setAttr(rn, "depth", vals[2], false)
				}
			}
			if tn = subNode(xn, "viewport_ratio"); tn != nil {
				vals := ustr.Split(tn.Value, " ")
				rn = ensureChild(ensureCreateNode(), "size_ratio")
				setAttr(rn, "width", vals[0], false)
				setAttr(rn, "height", vals[1], false)
			}
			if tn = subNode(xn, "mip_levels"); tn != nil {
				setAttr(ensureChild(ensureCreateNode(), "mips"), "levels", tn.Value, false)
			}
			if tn = subNode(xn, "mipmap_generate"); tn != nil {
				setAttr(ensureChild(ensureCreateNode(), "mips"), "auto_generate", tn.Value, false)
			}
		}
	}
}
Exemple #3
0
func convertImage(xn *xmlx.Node) {
	hexFormat := attVal(xn, "format")
	imgHeight := attValU64(xn, "height")
	imgWidth := attValU64(xn, "width")
	imgDepth := attValU64(xn, "depth")
	if imgDepth == 0 {
		imgDepth = 1
	}
	delAtts(xn, "height", "width", "depth", "format")
	if len(hexFormat) == 0 {
		hexFormat = HexFormat
	}
	hexData, refUrl, initNode, hexNode := "", "", subNode(xn, "init_from"), subNode(xn, "data")
	if initNode != nil {
		refUrl = initNode.Value
		delNodeForce(initNode, true)
		initNode = nil
	}
	if hexNode != nil {
		hexData = hexNode.Value
		delNodeForce(hexNode, true)
		hexNode = nil
	}
	if (len(refUrl) > 0) || (len(hexData) > 0) {
		initNode = xmlx.NewNode(xn.Type)
		initNode.Name.Local = "init_from"
		if len(refUrl) > 0 {
			ensureChild(initNode, "ref").Value = refUrl
		}
		if len(hexData) > 0 {
			hex := ensureChild(initNode, "hex")
			setAttr(hex, "format", hexFormat, false)
			hex.Value = hexData
			hexData = ""
		}
	}
	if (imgWidth != 0) && (imgHeight != 0) {
		cn := xmlx.NewNode(xn.Type)
		switch imgDepth {
		case 1:
			cn.Name.Local = "create_2d"
		case 2:
			cn.Name.Local = "create_3d"
		default:
			cn.Name.Local = "create_cube"
		}
		sn := ensureChild(cn, "size_exact")
		setAttr(sn, "width", fmt.Sprintf("%v", imgWidth), false)
		setAttr(sn, "height", fmt.Sprintf("%v", imgHeight), false)
		if initNode != nil {
			cn.AddChild(initNode)
		}
		xn.AddChild(cn)
	} else if initNode != nil {
		xn.AddChild(initNode)
	}
	if oldParent := xn.Parent; oldParent.Name.Local != "library_images" {
		logFmt("!!MOVE image!!; this may be BUGGY, please report your use-case at GitHub Issues for this package!!\n")
		id := attVal(xn, "id")
		if len(id) == 0 {
			id = fmt.Sprintf("img_moved_%v", time.Now().UnixNano())
		}
		if _, pos := moveNode(xn, nil, "library_images"); pos >= 0 {
			xn = xmlx.NewNode(xn.Type)
			xn.Name.Local = "instance_image"
			setAttr(xn, "url", "#"+id, false)
			xn.Parent = oldParent
			oldParent.Children[pos] = xn
		}
	}
}