]> git.zerfleddert.de Git - FreeShisen/blob - src/de/cwde/freeshisen/ShisenSho.java
a1ee0eb9b4fe73f70550730b9bc6dc2abc24d986
[FreeShisen] / src / de / cwde / freeshisen / ShisenSho.java
1 package de.cwde.freeshisen;
2
3 import android.app.AlertDialog;
4 import android.app.Application;
5 import android.content.DialogInterface;
6 import android.content.SharedPreferences;
7 import android.preference.PreferenceManager;
8 import android.util.Log;
9
10 public class ShisenSho extends Application {
11 private static ShisenSho instance = null;
12 private ShisenShoView view = null;
13 public ShisenShoActivity activity = null;
14
15 public Board board;
16 public int[] boardSize=new int[2];
17 public int difficulty=1; // 1=Easy, 2=Hard
18 public int size=3; // 1=Small, 2=Medium, 3=Big
19 public String tilesetid = "classic";
20 public boolean gravity=true;
21 public boolean timeCounter=true;
22
23 public void newPlay() {
24 board = new Board();
25 board.buildRandomBoard(boardSize[0],boardSize[1],difficulty,gravity);
26 }
27
28 public void setSize(int s) {
29 size = s;
30
31 switch (s) {
32 case 1:
33 boardSize[0] = 6 + 2;
34 boardSize[1] = 8 + 2;
35 break;
36 case 2:
37 boardSize[0] = 6 + 2;
38 boardSize[1] = 12 + 2;
39 break;
40 case 3:
41 default:
42 boardSize[0] = 6 + 2;
43 boardSize[1] = 16 + 2;
44 break;
45 }
46 }
47
48 public void sleep(int deciSeconds) {
49 try {
50 Thread.sleep(deciSeconds*100);
51 } catch (InterruptedException e) { }
52 }
53
54 public ShisenSho() {
55 instance = this;
56 setSize(size);
57 }
58
59 public static synchronized ShisenSho app() {
60 return instance;
61 }
62
63 public ShisenShoView getView() {
64 if (view == null) view = new ShisenShoView(this);
65 return view;
66 }
67
68 /** Called when the activity is first created. */
69 @Override
70 public void onCreate() {
71 super.onCreate();
72 PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
73 Log.d("ShisenSho", "starting up...");
74 loadOptions();
75 }
76
77 private void loadOptions() {
78 SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
79
80 // FIXME: handle NumberFormatException here?
81 setSize(Integer.parseInt(sp.getString("pref_size", "1")));
82 difficulty = Integer.parseInt(sp.getString("pref_diff", "1"));
83 gravity = sp.getBoolean("pref_grav", true);
84 timeCounter = sp.getBoolean("pref_time", true);
85 tilesetid = sp.getString("pref_tile", "");
86 }
87
88 public void checkForChangedOptions() {
89 SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
90
91 // FIXME: handle NumberFormatException here?
92 int size = Integer.parseInt(sp.getString("pref_size", "1"));
93 int difficulty = Integer.parseInt(sp.getString("pref_diff", "1"));
94 boolean gravity = sp.getBoolean("pref_grav", true);
95 boolean timeCounter = sp.getBoolean("pref_time", true);
96 String tilesetid = sp.getString("pref_tile", "");
97
98 boolean needsReset = false;
99
100 if (size != this.size) {
101 needsReset = true;
102 }
103
104 if (difficulty != this.difficulty) {
105 needsReset = true;
106 }
107
108 if (gravity != this.gravity) {
109 needsReset = true;
110 }
111
112 if (timeCounter != this.timeCounter) {
113 needsReset = true;
114 }
115
116 if ((tilesetid != this.tilesetid) && (view != null)) {
117 // tileset can be changed without a reset
118 this.tilesetid = tilesetid;
119 view.loadTileset();
120 }
121
122 if (needsReset && (view != null) && (activity != null)) {
123 new AlertDialog.Builder(this)
124 .setTitle("Preferences changed!") // FIXME: hardcoded string
125 .setCancelable(true)
126 .setIcon(R.drawable.icon)
127 .setPositiveButton(android.R.string.yes,
128 new DialogInterface.OnClickListener() {
129 public void onClick(DialogInterface dialog, int id) {
130 // User clicked OK button - reset game
131 ((ShisenSho) ((AlertDialog) dialog).getContext()).view.reset();
132 }
133 })
134 .setNegativeButton(android.R.string.no, null)
135 .setMessage("Changes in Preferences will only have an effect if" +
136 " a new game is started. Abort current game and start" +
137 " a new one?").create() // FIXME: hardcoded string
138 .show();
139 } else {
140 Log.d("ShisenSho", "Preferences changed, but no view or activity online - huh?");
141 }
142
143 }
144 }
Impressum, Datenschutz