9d5d1dd98a959583192a824cf4728da00eebc271
[FreeShisen] / src / de / cwde / freeshisen / ShisenSho.java
1 package de.cwde.freeshisen;
2
3 import android.app.Application;
4 import android.content.SharedPreferences;
5 import android.preference.PreferenceManager;
6 import android.util.Log;
7
8 public class ShisenSho extends Application {
9 private static ShisenSho instance = null;
10 private ShisenShoView view = null;
11 public ShisenShoActivity activity = null;
12
13 public Board board;
14 public int[] boardSize=new int[2];
15 public int difficulty=1; // 1=Easy, 2=Hard
16 public int size=3; // 1=Small, 2=Medium, 3=Big
17 public int tilesetid = R.drawable.classic;
18 public boolean gravity=true;
19 public boolean timeCounter=true;
20
21 public static void log(String msg) {
22 Log.w("ShisenSho", msg);
23 }
24
25 public void newPlay() {
26 board = new Board();
27 board.buildRandomBoard(boardSize[0],boardSize[1],difficulty,gravity);
28 }
29
30 public void setSize(int s) {
31 switch (s) {
32 case 1:
33 size=1;
34 boardSize[0]=6+2;
35 boardSize[1]=8+2;
36 break;
37 case 2:
38 size=2;
39 boardSize[0]=6+2;
40 boardSize[1]=12+2;
41 break;
42 case 3:
43 default:
44 size=3;
45 boardSize[0]=6+2;
46 boardSize[1]=16+2;
47 break;
48 }
49 }
50
51 public void sleep(int deciSeconds) {
52 try {
53 Thread.sleep(deciSeconds*100);
54 } catch (InterruptedException e) { }
55 }
56
57 public ShisenSho() {
58 instance = this;
59 setSize(size);
60 }
61
62 public static synchronized ShisenSho app() {
63 return instance;
64 }
65
66 public ShisenShoView getView() {
67 if (view == null) view = new ShisenShoView(this);
68 return view;
69 }
70
71 /** Called when the activity is first created. */
72 @Override
73 public void onCreate() {
74 super.onCreate();
75 PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
76 setOptions();
77 }
78
79 public void setOptions() {
80 SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
81
82 // FIXME: handle NumberFormatException here?
83 int size = Integer.parseInt(sharedPref.getString("pref_size", "1"));
84 int difficulty = Integer.parseInt(sharedPref.getString("pref_diff", "1"));
85 boolean gravity = sharedPref.getBoolean("pref_grav", true);
86 boolean timeCounter = sharedPref.getBoolean("pref_time", true);
87 int tilesetid = tilesetStringToRes(sharedPref.getString("pref_tile", ""));
88
89 boolean needsReset = false;
90
91 if (size != this.size) {
92 setSize(size);
93 needsReset = true;
94 }
95
96 if (difficulty != this.difficulty) {
97 this.difficulty = difficulty;
98 needsReset = true;
99 }
100
101 if (gravity != this.gravity) {
102 this.gravity = gravity;
103 needsReset = true;
104 }
105
106 if ((timeCounter != this.timeCounter) && (view != null)) {
107 this.timeCounter = timeCounter;
108 view.onTimeCounterActivate();
109 }
110
111 if ((tilesetid != this.tilesetid) && (view != null)) {
112 this.tilesetid = tilesetid;
113 view.loadTileset();
114 }
115
116 if (needsReset && (view != null)) {
117 view.reset();
118 }
119
120 }
121
122 private int tilesetStringToRes(String s)
123 {
124 if (s.equals("classic")) {
125 return R.drawable.classic;
126 } else if (s.equals("jade")) {
127 return R.drawable.jade;
128 } else if (s.equals("traditional")) {
129 return R.drawable.traditional;
130 } else if (s.equals("pixel")) {
131 return R.drawable.pixel;
132 } else {
133 return R.drawable.classic;
134 }
135 }
136 }
Impressum, Datenschutz