-
Notifications
You must be signed in to change notification settings - Fork 119
/
BulkSelectionInterface.java
308 lines (265 loc) · 11.2 KB
/
BulkSelectionInterface.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package com.gpl.rpg.AndorsTrail.activity;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;
import com.gpl.rpg.AndorsTrail.AndorsTrailApplication;
import com.gpl.rpg.AndorsTrail.R;
import com.gpl.rpg.AndorsTrail.context.WorldContext;
import com.gpl.rpg.AndorsTrail.controller.ItemController;
import com.gpl.rpg.AndorsTrail.model.item.ItemType;
/**
* @author ejwessel
* Creates the BulkSelectionInterface dialog that allows for buy/drop/selling
*/
public final class BulkSelectionInterface extends Activity implements TextWatcher {
// class variables
public static enum BulkInterfaceType {
buy, sell, drop
}
private static final int BUTTON_REPEAT_FIRST_TIME = 300; // Delay after the touch before the counting starts
private static final int BUTTON_REPEAT_FURTHER_TIMES = 50; // Delay between two count events
private static final int BUTTON_REPEAT_DOUBLE_AFTER = 10; // after how many count events the countValue doubles?
private WorldContext world;
private BulkInterfaceType interfaceType;
private ItemType itemType;
private int totalAvailableAmount;
private int pricePerUnit;
private TextView bulkselection_summary_totalgold;
private EditText bulkselection_amount_taken; // the amount we're going to take from the totalAmount
private SeekBar bulkselection_slider;
private Button okButton;
private final Handler timedEventHandler = new Handler(); // variables to count up or down on long presses on the buttons
private int countValue;
private int countTime;
private final Runnable countEvent = new Runnable() {
@Override
public void run() {
incrementValueAndRepeat(BUTTON_REPEAT_FURTHER_TIMES);
}
};
/*
* constructor
* @param context - the activity it is displayed upon
* @param p_interfaceType - the type of interface: BULK_INTERFACE_BUY, BULK_INTERFACE_SELL or BULK_INTERFACE_DROP
* @param p_itemName - the name of the item currently used - example: meat, rock, fish
* @param p_totalAmount - the total amount available of that item
* @param p_price - the price of the item - not necessary when dropping items
* @param p_money - the total amount of money available - only necessary when buying
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndorsTrailApplication app = AndorsTrailApplication.getApplicationFromActivity(this);
if (!app.isInitialized()) { finish(); return; }
this.world = app.getWorld();
app.setWindowParameters(this);
final Resources res = getResources();
final Intent intent = getIntent();
Bundle params = intent.getExtras();
String itemTypeID = params.getString("itemTypeID");
itemType = world.itemTypes.getItemType(itemTypeID);
totalAvailableAmount = params.getInt("totalAvailableAmount");
interfaceType = BulkInterfaceType.valueOf(params.getString("interfaceType"));
int intialSelection = 1;
setContentView(R.layout.bulkselection);
// initialize UI variables
TextView bulkselection_action_type = (TextView)findViewById(R.id.bulkselection_action_type);
bulkselection_amount_taken = (EditText)findViewById(R.id.bulkselection_amount_taken);
TextView bulkselection_amount_available = (TextView) findViewById(R.id.bulkselection_amount_available);
bulkselection_slider = (SeekBar)findViewById(R.id.bulkselection_slider);
bulkselection_summary_totalgold = (TextView)findViewById(R.id.bulkselection_summary_totalgold);
okButton = (Button)findViewById(R.id.bulkselection_finalize_button);
Button cancelButton = (Button)findViewById(R.id.bulkselection_cancel_button);
final Button decrementButton = (Button)findViewById(R.id.bulkselection_decrement_button);
final Button incrementButton = (Button)findViewById(R.id.bulkselection_increment_button);
final Button selectAllButton = (Button)findViewById(R.id.bulkselection_select_all_button);
// change image and name of the item
final TextView itemName = (TextView)findViewById(R.id.bulkselection_itemname);
itemName.setText(itemType.getName(world.model.player));
world.tileManager.setImageViewTileForSingleItemType(res, itemName, itemType);
int actionTextResourceID = 0;
if (interfaceType == BulkInterfaceType.buy) {
pricePerUnit = ItemController.getBuyingPrice(world.model.player, itemType);
actionTextResourceID = R.string.shop_buy;
} else if (interfaceType == BulkInterfaceType.sell) {
pricePerUnit = ItemController.getSellingPrice(world.model.player, itemType);
actionTextResourceID = R.string.shop_sell;
} else if (interfaceType == BulkInterfaceType.drop) {
pricePerUnit = 0;
actionTextResourceID = R.string.inventory_drop;
bulkselection_summary_totalgold.setVisibility(View.GONE);
}
String actionText = res.getString(actionTextResourceID);
// initialize the visual components visuals
okButton.setText(actionText);
bulkselection_action_type.setText(actionText + ' ');
bulkselection_amount_available.setText(Integer.toString(totalAvailableAmount));
bulkselection_slider.setMax(totalAvailableAmount - 1);
// hide Slider and Buttons when there is only 1 item available
if(totalAvailableAmount == 1){
decrementButton.setVisibility(View.GONE);
incrementButton.setVisibility(View.GONE);
selectAllButton.setVisibility(View.GONE);
bulkselection_slider.setVisibility(View.GONE);
}
updateControls(intialSelection);
OnTouchListener incrementDecrementListener = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
countTime = 0;
if (v == decrementButton) countValue = -1;
if (v == incrementButton) countValue = +1;
incrementValueAndRepeat(BUTTON_REPEAT_FIRST_TIME);
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_OUTSIDE:
timedEventHandler.removeCallbacks(countEvent);
break;
}
return false;
}
};
// setup decrement button
decrementButton.setOnTouchListener(incrementDecrementListener);
// setup increment button
incrementButton.setOnTouchListener(incrementDecrementListener);
// setup EditText listeners
bulkselection_amount_taken.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_ENTER){
updateControls(getTextboxAmount());
}
return false;
}
});
bulkselection_amount_taken.addTextChangedListener(this);
// setup slider event listeners
bulkselection_slider.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
updateControls(bulkselection_slider.getProgress() + 1);
return false;
}
});
// setup OK button
okButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (requiresConfirmation()) {
final String displayType = ItemInfoActivity.getDisplayTypeString(res, itemType).toLowerCase();
final String message = res.getString(R.string.bulkselection_sell_confirmation, itemType.getName(world.model.player), displayType);
new AlertDialog.Builder(v.getContext())
.setIcon(android.R.drawable.ic_dialog_info)
.setTitle(R.string.bulkselection_sell_confirmation_title)
.setMessage(message)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
itemsResult(intent);
}
})
.setNegativeButton(android.R.string.no, null)
.show();
} else {
itemsResult(intent);
}
}
private boolean requiresConfirmation() {
if (interfaceType != BulkInterfaceType.sell) return false;
if (itemType.isOrdinaryItem()) return false;
return true;
}
});
// setup cancel button
cancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setResult(RESULT_CANCELED);
finish();
}
});
selectAllButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
updateControls(totalAvailableAmount);
}
});
}
private void itemsResult(Intent intent){
Intent result = new Intent();
result.putExtras(intent);
result.putExtra("selectedAmount", getTextboxAmount());
setResult(RESULT_OK, result);
BulkSelectionInterface.this.finish();
}
private void incrementValueAndRepeat(int repeatAfterInterval) {
if(++countTime % BUTTON_REPEAT_DOUBLE_AFTER == 0) countValue *= 2;
int newAmount = getTextboxAmount() + countValue;
updateControls(newAmount);
if (newAmount <= 1 || newAmount >= totalAvailableAmount) return; // Do not repeat if we have reached the end of the scale.
timedEventHandler.postDelayed(countEvent, repeatAfterInterval);
}
private boolean canSelectFinalizeButton() {
int amount = getTextboxAmount();
if (amount <= 0) return false;
if (amount > totalAvailableAmount) return false;
if (interfaceType == BulkInterfaceType.buy) {
if (amount * pricePerUnit > world.model.player.getGold()) return false;
}
return true;
}
// adjusts the amount to the possible interval / synchronizes changes between controls
private void updateControls(int newAmount) {
int oldSliderAmount = bulkselection_slider.getProgress() + 1;
int oldEditboxAmount = getTextboxAmount();
// adjust amount
if (newAmount < 1) newAmount = 1;
if (newAmount > totalAvailableAmount) newAmount = totalAvailableAmount;
// update controls
if (newAmount != oldEditboxAmount) bulkselection_amount_taken.setText(Integer.toString(newAmount)); // change the amount taken/text
if (newAmount != oldSliderAmount) bulkselection_slider.setProgress(newAmount - 1); // change the amount taken/text
// display buying/selling information if not dropping
if (interfaceType == BulkInterfaceType.buy) {
bulkselection_summary_totalgold.setText(getResources().getString(R.string.bulkselection_totalcost_buy, newAmount * pricePerUnit));
} else if (interfaceType == BulkInterfaceType.sell) {
bulkselection_summary_totalgold.setText(getResources().getString(R.string.bulkselection_totalcost_sell, newAmount * pricePerUnit));
}
okButton.setEnabled(canSelectFinalizeButton());
}
private int getTextboxAmount() {
final String s = bulkselection_amount_taken.getText().toString();
if (s.equals("")) return 0;
try {
return Integer.parseInt(s);
} catch (NumberFormatException e) { }
return 0;
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { }
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) { }
@Override
public void afterTextChanged(Editable s) {
if (bulkselection_amount_taken.getText().toString().equals("")) return;
int newAmount = getTextboxAmount();
updateControls(newAmount);
}
}