This repository has been archived by the owner on Dec 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
[3.0] Define custom XML element
Yuya Matsuo edited this page Sep 23, 2016
·
4 revisions
You can define custom XML element such as <my-entity>
. (This is called "primitive" in Meganekko.)
- Create class which implements
XmlPrimitiveFactory.XmlPrimitiveHandler
. - Call
XmlPrimitiveFactory.getInstance().install()
with a instance of this class
Example:
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import org.meganekkovr.Entity;
import org.meganekkovr.xml.XmlPrimitiveFactory;
import org.w3c.dom.Node;
public class MyEntityHandler implements XmlPrimitiveFactory.XmlPrimitiveHandler {
@Nullable
@Override
public Entity createEntity(@NonNull Node node, @NonNull Context context) {
// createEntity() will be called with all element in XML.
// Check XML element's tag name and handle if interested in it.
if ("my-entity".equals(node.getNodeName())) {
// Get properties from node
Node propertyNode = node.getAttributes().getNamedItem("propX");
Node otherPropertyNode = node.getAttributes().getNamedItem("propY");
String propX = propertyNode.getNodeValue();
String propY = otherPropertyNode.getNodeValue();
// Create entity, set some properties, and return it
Entity entity = new Entity();
// entity.setPotision(), entity.setRotation(), entity.add(component), etc...
return entity;
}
// Returning null means to ignore this element.
return null;
}
}
And install it:
public class App extends MeganekkoApp {
@Override
public void init() {
super.init();
// Add your primitive handler before setSceneFromXml()
XmlPrimitiveFactory.getInstance().install(new MyEntityHandler());
setSceneFromXml(R.xml.scene);
}
}
R.xml.scene
is something like:
<scene>
<my-entity
propX="this is property"
propY="other property"
position="0.0 0.0 -5.0"/>
</scene>