示例#1
0
 public Vertex(XML element) {
   x = element.getFloat("x");
   y = element.getFloat("y");
   u = element.getFloat("u");
   v = element.getFloat("v");
   selected = element.getInt("selected");
 }
示例#2
0
  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);
  }
示例#3
0
  /** @nowebref */
  protected XML(XML parent, Node node) {
    this.node = node;
    this.parent = parent;

    for (String attr : parent.listAttributes()) {
      if (attr.startsWith("xmlns")) {
        // Copy namespace attributes to the kids, otherwise this XML
        // can no longer be printed (or manipulated in most ways).
        // Only do this when it's an Element, otherwise it's trying to set
        // attributes on text notes (interstitial content).
        if (node instanceof Element) {
          setString(attr, parent.getString(attr));
        }
      }
    }
  }
示例#4
0
 /**
  * Get a child by its name or path.
  *
  * @param name element name or path/to/element
  * @return the first matching element or null if no match
  */
 public XML getChild(String name) {
   if (name.length() > 0 && name.charAt(0) == '/') {
     throw new IllegalArgumentException("getChild() should not begin with a slash");
   }
   if (name.indexOf('/') != -1) {
     return getChildRecursive(PApplet.split(name, '/'), 0);
   }
   int childCount = getChildCount();
   for (int i = 0; i < childCount; i++) {
     XML kid = getChild(i);
     String kidName = kid.getName();
     if (kidName != null && kidName.equals(name)) {
       return kid;
     }
   }
   return null;
 }
示例#5
0
 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;
 }
示例#6
0
 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;
 }
示例#7
0
 /**
  * Get any children that match this name or path. Similar to getChild(), but will grab multiple
  * matches rather than only the first.
  *
  * @param name element name or path/to/element
  * @return array of child elements that match
  * @author processing.org
  */
 public XML[] getChildren(String name) {
   if (name.length() > 0 && name.charAt(0) == '/') {
     throw new IllegalArgumentException("getChildren() should not begin with a slash");
   }
   if (name.indexOf('/') != -1) {
     return getChildrenRecursive(PApplet.split(name, '/'), 0);
   }
   // if it's a number, do an index instead
   // (returns a single element array, since this will be a single match
   if (Character.isDigit(name.charAt(0))) {
     return new XML[] {getChild(Integer.parseInt(name))};
   }
   int childCount = getChildCount();
   XML[] matches = new XML[childCount];
   int matchCount = 0;
   for (int i = 0; i < childCount; i++) {
     XML kid = getChild(i);
     String kidName = kid.getName();
     if (kidName != null && kidName.equals(name)) {
       matches[matchCount++] = kid;
     }
   }
   return (XML[]) PApplet.subset(matches, 0, matchCount);
 }
示例#8
0
 /**
  * Internal helper function for getChild(String).
  *
  * @param items result of splitting the query on slashes
  * @param offset where in the items[] array we're currently looking
  * @return matching element or null if no match
  * @author processing.org
  */
 protected XML getChildRecursive(String[] items, int offset) {
   // if it's a number, do an index instead
   if (Character.isDigit(items[offset].charAt(0))) {
     XML kid = getChild(Integer.parseInt(items[offset]));
     if (offset == items.length - 1) {
       return kid;
     } else {
       return kid.getChildRecursive(items, offset + 1);
     }
   }
   int childCount = getChildCount();
   for (int i = 0; i < childCount; i++) {
     XML kid = getChild(i);
     String kidName = kid.getName();
     if (kidName != null && kidName.equals(items[offset])) {
       if (offset == items.length - 1) {
         return kid;
       } else {
         return kid.getChildRecursive(items, offset + 1);
       }
     }
   }
   return null;
 }
示例#9
0
  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++;
      }
    }
  }
示例#10
0
 public Mesh(XML child) {
   addVertices(child.getChildren("vertices/vertex"));
   addFaces(child.getChildren("faces/face"));
 }
示例#11
0
 public Face(XML element, Vertex[] vertices) {
   this.vertices[0] = vertices[element.getInt("v0")];
   this.vertices[1] = vertices[element.getInt("v1")];
   this.vertices[2] = vertices[element.getInt("v2")];
 }
示例#12
0
  /**
   * Loads the syntax as specified in the XML
   *
   * @param xml
   */
  public void loadSyntax(XML xml) {
    // Get the list of defined styles
    XML[] styleList = xml.getChild("styles").getChildren();
    for (XML style : styleList) {
      // Make sure that this is a "style" element
      if (!style.getName().equals("style")) continue;

      // Parse the style
      TextPaint paint = new TextPaint(getPaint());
      String name = style.getContent();
      String hex = style.getString("color", "#FF000000").substring(1);
      boolean bold = style.getString("bold", "false").equals("true") ? true : false;

      // Build the TextPaint
      paint.setStyle(TextPaint.Style.FILL);
      paint.setColor(PApplet.unhex(hex));
      paint.setFakeBoldText(
          bold); // TODO what does "fake" mean? Is this something we should be concerned about?

      // Add the style
      styles.put(name, paint);
    }

    // Get the list of defined keywords
    XML[] keywords = xml.getChild("keywords").getChildren();
    ArrayList<Keyword> tempSyntax = new ArrayList<>();

    syntax = new Keyword[keywords.length];
    for (int i = 0; i < keywords.length; i++) {
      XML keyword = keywords[i];

      // Make sure that this is a "keyword" element
      if (!keyword.getName().equals("keyword")) continue;

      // Parse the keyword
      String style = keyword.getString("style", "");
      String name = keyword.getContent();
      boolean function = keyword.getString("function", "false").equals("true");

      String reference = keyword.getString("reference", "processing");
      boolean noUnderscore = keyword.getString("reference_hint", "").equals("no_underscore");

      String parentClass = keyword.getString("class", "");
      boolean staticInParentClass = Boolean.valueOf(keyword.getString("static", "false"));

      // If there is a parent class, then open that class... we don't know how to be fancier yet
      // Automatically add an underscore if the keyword is a function and doesn't have the
      // "no_underscore" hint
      String referenceTarget =
          keyword.getString(
              "reference_target",
              parentClass.length() > 0
                  ? parentClass
                  : (name + (function && !noUnderscore ? "_" : "")));

      // If this isn't a valid style, bail out
      if (!styles.containsKey(style)) continue;

      // Add the keyword
      tempSyntax.add(
          new Keyword(
              name,
              styles.get(style),
              function,
              reference,
              referenceTarget,
              parentClass,
              staticInParentClass));
    }

    syntax = new Keyword[tempSyntax.size()];

    // We need to do this because more than the half of the reported XML elements are null
    for (int i = 0; i < tempSyntax.size(); i++) {
      syntax[i] = tempSyntax.get(i);
    }

    syntaxLoaded.set(true);
  }
示例#13
0
 public XML addChild(XML child) {
   Document document = node.getOwnerDocument();
   Node newChild = document.importNode((Node) child.getNative(), true);
   return appendChild(newChild);
 }
示例#14
0
 /**
  * @webref xml:method
  * @brief Converts String content to an XML object
  * @param data the content to be parsed as XML
  * @return an XML object, or null
  * @throws SAXException
  * @throws ParserConfigurationException
  * @throws IOException
  * @nowebref
  */
 public static XML parse(String data)
     throws IOException, ParserConfigurationException, SAXException {
   return XML.parse(data, null);
 }