private void renderTreeHelper(XML xml, int[] widths, int depth, int[] ltor) { float x = (g.width / widths[depth]) * (0.5f + ltor[depth]++); float y = (g.height / widths.length) * (0.5f + depth); xml.setFloat("x", x); xml.setFloat("y", y); if (xml.getParent() != null) g.line( xml.getParent().getFloat("x"), xml.getParent().getFloat("y") + RECT_HEIGHT / 2, x, y - RECT_HEIGHT / 2); if (xml.hasAttribute("altColor")) { String hexColor = xml.getString("altColor"); g.fill( getColorComponent(hexColor, 0), getColorComponent(hexColor, 1), getColorComponent(hexColor, 2)); } g.rect(x, y, RECT_WIDTH, RECT_HEIGHT, 4); g.fill(0); if (xml.hasAttribute("altName")) g.text(xml.getString("altName"), x, y, RECT_WIDTH, RECT_HEIGHT); else g.text(xml.getName(), x, y, RECT_WIDTH, RECT_HEIGHT); g.fill(255); XML[] children = xml.getChildren(); for (int i = 0; i < children.length; i++) renderTreeHelper(children[i], widths, depth + 1, ltor); }
private int getWidthsHelper(XML xml, int[] widths, int depth, int maxWidth) { widths[depth]++; if (widths[depth] > maxWidth) maxWidth = widths[depth]; XML[] children = xml.getChildren(); for (int i = 0; i < children.length; i++) { maxWidth = getWidthsHelper(children[i], widths, depth + 1, maxWidth); } return maxWidth; }
private int getDepth(XML xml) { int maxDepth = 0; XML[] children = xml.getChildren(); for (int i = 0; i < children.length; i++) { int depth = getDepth(children[i]); if (depth > maxDepth) maxDepth = depth; } return maxDepth + 1; }
public Level(String filename, PixelPie pie) { XML file = pie.app.loadXML(filename); // Get level properties, if any. if (file.getChild("properties") != null) { properties = new StringDict(); for (XML child : file.getChild("properties").getChildren("property")) { properties.set(child.getString("name"), child.getString("value")); } } // Get tile dimensions. tileWidth = file.getInt("tilewidth"); tileHeight = file.getInt("tileheight"); // Get Row and Column count. levelColumns = file.getInt("width"); levelRows = file.getInt("height"); // Get level dimensions. levelWidth = levelColumns * tileWidth; levelHeight = levelRows * tileHeight; // Get tileSets. tileSets = file.getChildren("tileset"); // Get objects. objects = file.getChildren("objectgroup"); // Get scripts. scripts = new HashMap<String, Script>(); for (XML objectType : objects) { String objectName = objectType.getString("name"); // Check type of object. if (objectName.substring(0, 1).equals("(")) { // Break up name into parts. String[] nameParts = PApplet.split(objectName, " "); // If script. if (nameParts[0].equals("(script)")) { // Grab script name. String name = nameParts[1]; // Initialize coordinates array (if any). IntDict scriptCoords = new IntDict(); // Check for objects in script layer. Get coordinates from these objects. if (objectType.getChild("object") != null) { for (XML coordObj : objectType.getChildren("object")) { // Check if object has a name, if not, skip it. if (coordObj.getString("name") == null) { pie.log.printlg("Script coordinate container does not have a name. Ignoring."); continue; } // Figure out object type. int coordType = 0; if (coordObj.hasChildren()) { coordType = (coordObj.getChild("polyline") != null) ? 2 : 1; } // Process coordinates depending on object type. switch (coordType) { // If rectangle, take top-left corner. case 0: scriptCoords.set( coordObj.getString("name"), PixelPie.coordToArray(coordObj.getInt("x"), coordObj.getInt("y"), this)); break; // If ellipse, take center. case 1: scriptCoords.set( coordObj.getString("name"), PixelPie.coordToArray( coordObj.getInt("x") + coordObj.getInt("width") / 2, coordObj.getInt("y") + coordObj.getInt("height") / 2, this)); break; // If poly line, record each point. case 2: String[] coordsArray = PApplet.split(coordObj.getChild("polyline").getString("points"), " "); int startX = Integer.parseInt(objectType.getChild("object").getString("x")); int startY = Integer.parseInt(objectType.getChild("object").getString("y")); for (int i = 0; i < coordsArray.length; i++) { scriptCoords.set( coordObj.getString("name") + "_" + PApplet.nf(i, 2), PixelPie.coordToArray( startX + Integer.parseInt(PApplet.split(coordsArray[i], ",")[0]), startY + Integer.parseInt(PApplet.split(coordsArray[i], ",")[1]), this)); } break; } } } else { scriptCoords = null; } // Get the total amount of frames. int endFrame = 0; for (XML obj : objectType.getChild("properties").getChildren("property")) { endFrame = PApplet.max(endFrame, PixelPie.getScriptFrame(obj.getString("name"))); } // Create the scriptAction container "children". HashMap<String, ScriptAction[]> children = new HashMap<String, ScriptAction[]>(); // Create the script object. Script Script = new Script(endFrame, scriptCoords, children, pie); scripts.put(name, Script); // Create scriptAction temporary container. This one uses a HashMap so we can dynamically // add scriptActions to it. HashMap<String, ArrayList<ScriptAction>> scriptTemp = new HashMap<String, ArrayList<ScriptAction>>(); // Add scriptActions to the temporary container. for (XML obj : objectType.getChild("properties").getChildren("property")) { String frame = PApplet.str(PixelPie.getScriptFrame(obj.getString("name"))); // Create scriptAction object from parameters. String[] actionArray = PApplet.split(obj.getString("value"), "("); String params = actionArray[1].substring(0, actionArray[1].length() - 1); ScriptAction action = null; // try {action = (scriptAction) Class.forName(app.getClass().getName() + "$script_" + // actionArray[0]).getDeclaredConstructors()[0].newInstance(new Object[]{app, params, // Script});} try { action = (ScriptAction) Class.forName(pie.app.getClass().getName() + "$script_" + actionArray[0]) .getDeclaredConstructors()[0] .newInstance(new Object[] {pie.app}); action.setup(params, Script); } catch (Exception e) { pie.log.printlg(e); } // Check whether there is an entry in the scriptTemp array. if (scriptTemp.get(frame) != null) { scriptTemp.get(frame).add(action); } else { // Initiate Arraylist. scriptTemp.put(frame, new ArrayList<ScriptAction>()); scriptTemp.get(frame).add(action); } } // Turn over contents of temporary container to "children" array. for (@SuppressWarnings("rawtypes") Map.Entry entry : scriptTemp.entrySet()) { ScriptAction[] tempActions = new ScriptAction[scriptTemp.get(entry.getKey()).size()]; for (int i = 0; i < scriptTemp.get(entry.getKey()).size(); i++) { tempActions[i] = scriptTemp.get(entry.getKey()).get(i); } children.put(entry.getKey().toString(), tempActions); } } } } // Load level layers. XML[] layers = file.getChildren("layer"); // Count number of background and level layers. for (int i = 0; i < layers.length; i++) { if (layers[i].getString("name").substring(0, 4).equals("(bg)")) { backgroundLayers++; } else { levelLayers++; } } // Initiate arrays. background = new int[backgroundLayers][levelColumns * levelRows]; levelMap = new int[levelLayers][levelColumns * levelRows]; bgScroll = new float[backgroundLayers]; // Process each layer. int currentLayer = 0; int bgCurrentLayer = 0; for (int i = 0; i < layers.length; i++) { // If it's a background layer... if (layers[i].getString("name").substring(0, 4).equals("(bg)")) { // Get properties. HashMap<String, String> prop = new HashMap<String, String>(); for (XML property : layers[i].getChild("properties").getChildren("property")) { prop.put(property.getString("name"), property.getString("value")); } // Process the scroll rate. bgScroll[bgCurrentLayer] = PApplet.constrain(Float.parseFloat(PixelPie.getProp(prop, "scroll")), 0, 1); // Process each background tile. XML[] tiles = layers[i].getChild("data").getChildren("tile"); for (int k = 0; k < tiles.length; k++) { background[bgCurrentLayer][k] = tiles[k].getInt("gid"); } // Increase background layer counter. bgCurrentLayer++; } // Else, load layer as normal. else { // Load each tile. XML[] tiles = layers[i].getChild("data").getChildren("tile"); for (int k = 0; k < tiles.length; k++) { levelMap[currentLayer][k] = tiles[k].getInt("gid"); } currentLayer++; } } }
public Mesh(XML child) { addVertices(child.getChildren("vertices/vertex")); addFaces(child.getChildren("faces/face")); }