Telis

mapEditor

Sep 25th, 2014
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 4.14 KB | None | 0 0
  1. package ru.startandroid.develop.mapeditor;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileReader;
  6. import java.util.ArrayList;
  7.  
  8. import android.app.Activity;
  9. import android.content.Intent;
  10. import android.os.Bundle;
  11. import android.text.TextUtils;
  12. import android.view.View;
  13. import android.view.View.OnClickListener;
  14. import android.widget.Button;
  15. import android.widget.EditText;
  16. import android.widget.Toast;
  17.  
  18. public class MainActivity extends Activity implements OnClickListener{
  19.  
  20.     EditText sorcFld;
  21.     EditText nameFld;
  22.     EditText lFld;
  23.     EditText bFld;
  24.     EditText rFld;
  25.     EditText tFld;
  26.     Button editBtn;
  27.    
  28.     String sourceName;
  29.     static String mapName;
  30.  
  31.     static int lbrt[];
  32.     static int w;
  33.     static int h;
  34.    
  35.     static ArrayList<ArrayList<Cells>> cell;
  36.     Cells c;
  37.    
  38.     String types[] = {"none", "vall", "wood", "ore", "farm", "saw", "mine", "barr", "fort", "cstl"};
  39.     String colors[] = {"red", "grn", "yel", "ppl", "pnk"};
  40.  
  41.  class Cells {
  42.     int x;
  43.     int y;
  44.     String type = "none";
  45.     String color = "";
  46.     String pl1 = "";
  47.     String pl1Col = "";
  48.     String pl2 = "";
  49.     String pl2Col = "";
  50.     String data = "";
  51.  
  52.     Cells() {
  53.     }
  54.  
  55.     public void setCoords(int x, int y) {
  56.        data = x + " " + y;
  57.        this.x = x;
  58.        this.y = y;
  59.     }
  60.  
  61.     public void setData(String data) {
  62.        this.data = data;
  63.        String d[] = data.split(" ");
  64.        x = Integer.parseInt(d[0]);
  65.        y = Integer.parseInt(d[1]);
  66.        type = d[2];
  67.        color = d[3];
  68.  
  69.        if (d.length > 4) {
  70.           pl1 = d[4];
  71.           pl1Col = d[5];
  72.        }
  73.        if (d.length > 6) {
  74.           pl2 = d[6];
  75.           pl2Col = d[7];
  76.        }
  77.     }
  78.  }
  79.  
  80.  public void prepairMap() {
  81.     String d = "";
  82.  
  83.     try {
  84.         sourceName = sorcFld.getText().toString();
  85.         FileReader FR = new FileReader(sourceName+".txt");
  86.         BufferedReader source = new BufferedReader (FR);
  87.         do {
  88.             d = source.readLine();
  89.             String coords = "";
  90.         l1: for (int x = 0; x < w; x++) {
  91.                 for (int y = 0; y < h; y++) {
  92.                     coords = (x + lbrt[0]) + " " + (y + lbrt[1] );
  93.                     if (d.startsWith(coords)) {
  94.                         c.setData(d);
  95.                         break l1;
  96.                     }
  97.                 }
  98.             }
  99.         } while(d != null);
  100.         source.close();
  101.         Toast.makeText(this, "карта " + sourceName + " готова к редактированию", Toast.LENGTH_SHORT).show();
  102.     }
  103.     catch (FileNotFoundException e) {
  104.         e.printStackTrace();
  105.         Toast.makeText(this, "Указан неверный источник", Toast.LENGTH_SHORT).show();
  106.     }
  107.     catch (Exception e) {
  108.         // TODO Auto-generated catch block
  109.         e.printStackTrace();
  110.     }
  111.     return;
  112.  }
  113.     @Override
  114.     protected void onCreate(Bundle savedInstanceState) {
  115.         super.onCreate(savedInstanceState);
  116.         setContentView(R.layout.main);
  117.        
  118.         sorcFld = (EditText) findViewById(R.id.sorsFld);
  119.         nameFld = (EditText) findViewById(R.id.nameFld);
  120.         lFld = (EditText) findViewById(R.id.lFld);
  121.         bFld = (EditText) findViewById(R.id.bFld);
  122.         rFld = (EditText) findViewById(R.id.rFld);
  123.         tFld = (EditText) findViewById(R.id.tFld);
  124.        
  125.         editBtn = (Button) findViewById(R.id.editBtn);
  126.         editBtn.setOnClickListener(this);
  127.                  
  128.     }
  129.  
  130.     @Override
  131.     public void onClick(View v) {
  132.         switch (v.getId()) {
  133.         case R.id.editBtn:
  134.            
  135.             sourceName = "";
  136.             mapName = nameFld.getText().toString();
  137.  
  138.             lbrt[0] = Integer.parseInt(lFld.getText().toString());
  139.             lbrt[1] = Integer.parseInt(bFld.getText().toString());
  140.             lbrt[2] = Integer.parseInt(rFld.getText().toString());
  141.             lbrt[3] = Integer.parseInt(tFld.getText().toString());
  142.            
  143.             w = lbrt[2] - lbrt[0];
  144.             h = lbrt[3] - lbrt[1];
  145.            
  146.             if (TextUtils.isEmpty(sorcFld.getText().toString())){
  147.                 for (int x = 0; x < w; x++) {
  148.                     for (int y = 0; y < h; y++) {
  149.                         c.setCoords((x + lbrt[0]) , (y + lbrt[1]));
  150.                     }
  151.                 }
  152.                 Toast.makeText(this, "создана пустая карта", Toast.LENGTH_SHORT).show();
  153.             }else{
  154.                 prepairMap();
  155.             }
  156.             Intent intent = new Intent(this, CellEditorActivity.class);
  157.             startActivity(intent);
  158.             break;
  159.         }
  160.     }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment