Android Open Source - donatello-y-raphael Levels Activity






From Project

Back to project page donatello-y-raphael.

License

The source code is released under:

MIT License

If you think the Android project donatello-y-raphael listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.example.ATracePath;
/*from   w  w  w  . j av a 2s. co  m*/
import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.app.ActionBar.LayoutParams;
import android.view.Gravity;
import android.widget.*;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.util.Log;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by solberg on 21/09/14.
 */

public class LevelsActivity extends Activity {

    private Global global = Global.getInstance();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.levels);

        final LinearLayout layout = (LinearLayout) findViewById(R.id.levels);

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

        for(int i = 0; i < global.mPacks.size(); i++) {

            // Create LinearLayout
            LinearLayout ll = new LinearLayout(this);
            ll.setOrientation(LinearLayout.HORIZONTAL);


            final Button btn = new Button(this);
            btn.setId(i);
            btn.setText( global.mPacks.get(i).getName() );
            btn.setHeight(200);
            btn.setTextSize((float) 25);
            btn.setLayoutParams(params);

            final int index = i;
            btn.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    if (global.mPacks.get(index).getChallenge() == null) {
                        try {
                            List<Challenge> challenges = new ArrayList<Challenge>();
                            readLevels(getAssets().open(global.mPacks.get(index).getFile()), challenges);
                            global.mPacks.get(index).setChallenge(challenges);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    global.packID = index;
                    startActivity(new Intent(getApplicationContext(), MapsActivity.class));
                    Log.i("LevelsActivity goto:", " MapsActivity");
                }
            });

            ll.addView(btn);
            layout.addView(ll);
        }
    }


    private void readLevels( InputStream is, List<Challenge> challenge) {

        try {
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse( is );
            NodeList nList = doc.getElementsByTagName("challenge");

            for (int c = 0; c < nList.getLength(); ++c ) {

                Node nNode = nList.item(c);
                if ( nNode.getNodeType() == Node.ELEMENT_NODE ) {
                    Element eNode = (Element) nNode;

                    int id = Integer.parseInt(eNode.getAttributes().getNamedItem("id").getNodeValue());
                    String name = eNode.getAttributes().getNamedItem("name").getNodeValue();

                    List<Puzzle> puzzleList = new ArrayList<Puzzle>();

                    NodeList pList = eNode.getElementsByTagName("puzzle");
                    for (int p = 0; p < pList.getLength(); ++p ) {
                        Node puzzle = pList.item(p);
                        if ( puzzle.getNodeType() == Node.ELEMENT_NODE ) {
                            Element pNode = (Element) puzzle;

                            int size = Integer.parseInt(pNode.getElementsByTagName("size").item(0).getFirstChild().getNodeValue());
                            String[] cord = pNode.getElementsByTagName("flows").item(0).getFirstChild().getNodeValue().split("\\s*,\\s*");

                            List<Coordinate> coordinate = new ArrayList<Coordinate>();

                            for(int i = 0; i < cord.length; i++) {
                                String[] xy = cord[i].replace("(", "").replace(")", "").split(" ");

                                coordinate.add( new Coordinate(Integer.parseInt(xy[0]), Integer.parseInt(xy[1]), 0));
                                coordinate.add( new Coordinate(Integer.parseInt(xy[2]), Integer.parseInt(xy[3]), 0));
                            }
                            puzzleList.add(new Puzzle(size, coordinate));
                        }
                    }
                    challenge.add( new Challenge( id, name, puzzleList ) );
                }
            }
        }
        catch ( Exception e ) {
            e.printStackTrace();
        }
    }
}




Java Source Code List

com.example.ATracePath.Board.java
com.example.ATracePath.Cellpath.java
com.example.ATracePath.Challenge.java
com.example.ATracePath.Coordinate.java
com.example.ATracePath.DbHelper.java
com.example.ATracePath.Global.java
com.example.ATracePath.LevelsActivity.java
com.example.ATracePath.MainActivity.java
com.example.ATracePath.MapsActivity.java
com.example.ATracePath.Pack.java
com.example.ATracePath.PlayActivity.java
com.example.ATracePath.ProgressAdapter.java
com.example.ATracePath.Puzzle.java
com.example.ATracePath.SettingsActivity.java