Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ru.startandroid.develop.mapeditor;
- import java.io.BufferedReader;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.util.ArrayList;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.text.TextUtils;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- public class MainActivity extends Activity implements OnClickListener{
- EditText sorcFld;
- EditText nameFld;
- EditText lFld;
- EditText bFld;
- EditText rFld;
- EditText tFld;
- Button editBtn;
- String sourceName;
- static String mapName;
- static int lbrt[];
- static int w;
- static int h;
- static ArrayList<ArrayList<Cells>> cell;
- Cells c;
- String types[] = {"none", "vall", "wood", "ore", "farm", "saw", "mine", "barr", "fort", "cstl"};
- String colors[] = {"red", "grn", "yel", "ppl", "pnk"};
- class Cells {
- int x;
- int y;
- String type = "none";
- String color = "";
- String pl1 = "";
- String pl1Col = "";
- String pl2 = "";
- String pl2Col = "";
- String data = "";
- Cells() {
- }
- public void setCoords(int x, int y) {
- data = x + " " + y;
- this.x = x;
- this.y = y;
- }
- public void setData(String data) {
- this.data = data;
- String d[] = data.split(" ");
- x = Integer.parseInt(d[0]);
- y = Integer.parseInt(d[1]);
- type = d[2];
- color = d[3];
- if (d.length > 4) {
- pl1 = d[4];
- pl1Col = d[5];
- }
- if (d.length > 6) {
- pl2 = d[6];
- pl2Col = d[7];
- }
- }
- }
- public void prepairMap() {
- String d = "";
- try {
- sourceName = sorcFld.getText().toString();
- FileReader FR = new FileReader(sourceName+".txt");
- BufferedReader source = new BufferedReader (FR);
- do {
- d = source.readLine();
- String coords = "";
- l1: for (int x = 0; x < w; x++) {
- for (int y = 0; y < h; y++) {
- coords = (x + lbrt[0]) + " " + (y + lbrt[1] );
- if (d.startsWith(coords)) {
- c.setData(d);
- break l1;
- }
- }
- }
- } while(d != null);
- source.close();
- Toast.makeText(this, "карта " + sourceName + " готова к редактированию", Toast.LENGTH_SHORT).show();
- }
- catch (FileNotFoundException e) {
- e.printStackTrace();
- Toast.makeText(this, "Указан неверный источник", Toast.LENGTH_SHORT).show();
- }
- catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return;
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- sorcFld = (EditText) findViewById(R.id.sorsFld);
- nameFld = (EditText) findViewById(R.id.nameFld);
- lFld = (EditText) findViewById(R.id.lFld);
- bFld = (EditText) findViewById(R.id.bFld);
- rFld = (EditText) findViewById(R.id.rFld);
- tFld = (EditText) findViewById(R.id.tFld);
- editBtn = (Button) findViewById(R.id.editBtn);
- editBtn.setOnClickListener(this);
- }
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.editBtn:
- sourceName = "";
- mapName = nameFld.getText().toString();
- lbrt[0] = Integer.parseInt(lFld.getText().toString());
- lbrt[1] = Integer.parseInt(bFld.getText().toString());
- lbrt[2] = Integer.parseInt(rFld.getText().toString());
- lbrt[3] = Integer.parseInt(tFld.getText().toString());
- w = lbrt[2] - lbrt[0];
- h = lbrt[3] - lbrt[1];
- if (TextUtils.isEmpty(sorcFld.getText().toString())){
- for (int x = 0; x < w; x++) {
- for (int y = 0; y < h; y++) {
- c.setCoords((x + lbrt[0]) , (y + lbrt[1]));
- }
- }
- Toast.makeText(this, "создана пустая карта", Toast.LENGTH_SHORT).show();
- }else{
- prepairMap();
- }
- Intent intent = new Intent(this, CellEditorActivity.class);
- startActivity(intent);
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment