AtkinsSJ

Read SVG, produce list of vertices from a path. Libgdx

Apr 5th, 2013
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.99 KB | None | 0 0
  1.     public boolean fromFile(FileHandle file) {
  2.         XmlReader reader = new XmlReader();
  3.         XmlReader.Element xml;
  4.         try {
  5.             xml = reader.parse(file);
  6.         } catch (IOException e) {
  7.             // TODO Auto-generated catch block
  8.             e.printStackTrace();
  9.             return false;
  10.         }
  11.        
  12.         Array<Element> paths = xml.getChildByName("g").getChildrenByName("path");
  13.        
  14.         for (Element path: paths) {
  15.             switch ( path.get("id") ) {
  16.             case "outside":
  17.                 ArrayList<Vector2> vertexList = new ArrayList<Vector2>();
  18.                
  19.                 // get the path string
  20.                 String[] pathParts = path.get("d").split(" ");
  21.                 for (String s: pathParts) {
  22.                     if (Character.isDigit(s.charAt(0)) || s.charAt(0) == '-') {
  23.                         String[] coords = s.split(",");
  24.                         vertexList.add( new Vector2( Float.parseFloat(coords[0]), Float.parseFloat(coords[1]) ) );
  25.                     }
  26.                 }
  27.                
  28.                 this.vertices = new Vector2[vertexList.size()];
  29.                 this.vertices = vertexList.toArray(this.vertices);
  30.                
  31.                 break;
  32.             default:
  33.                 break;
  34.             }
  35.         }
  36.        
  37.         return true;
  38.     }
  39.  
  40.     public void assignPhysicsBody(World world) {
  41.        
  42.         BodyDef bodyDef = new BodyDef();
  43.         bodyDef.type = BodyType.StaticBody;
  44.        
  45.         this.body = world.createBody(bodyDef);
  46.        
  47.         ChainShape outside = new ChainShape();
  48.         outside.createLoop(this.vertices);
  49.         this.body.createFixture(outside, 0);
  50.        
  51.         outside.dispose();
  52.     }
  53.    
  54.     @Override
  55.     public void draw(SpriteBatch batch, float parentAlpha) {
  56.         batch.end();
  57.        
  58.         this.shapeRenderer.setProjectionMatrix(world.getCamera().combined);
  59.         this.shapeRenderer.identity();
  60.        
  61.         this.shapeRenderer.begin(ShapeType.Line);
  62.         this.shapeRenderer.setColor(0, 0, 1, 1);
  63.        
  64.         int length = this.vertices.length;
  65.        
  66.         for (int i = 1; i < length; i++) {
  67.             this.shapeRenderer.line(this.vertices[i-1].x, this.vertices[i-1].y,
  68.                     this.vertices[i].x, this.vertices[i].y);
  69.         }
  70.         this.shapeRenderer.line(this.vertices[length-1].x, this.vertices[length-1].y,
  71.                 this.vertices[0].x, this.vertices[0].y);
  72.        
  73.         this.shapeRenderer.end();
  74.        
  75.         batch.begin();
  76.     }
Advertisement
Add Comment
Please, Sign In to add comment