-
Notifications
You must be signed in to change notification settings - Fork 119
/
ConversationActivity.java
426 lines (375 loc) · 14.5 KB
/
ConversationActivity.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
package com.gpl.rpg.AndorsTrail.activity;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.Spannable;
import android.text.style.ForegroundColorSpan;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.*;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView.BufferType;
import com.gpl.rpg.AndorsTrail.AndorsTrailApplication;
import com.gpl.rpg.AndorsTrail.Dialogs;
import com.gpl.rpg.AndorsTrail.R;
import com.gpl.rpg.AndorsTrail.context.WorldContext;
import com.gpl.rpg.AndorsTrail.controller.ConversationController;
import com.gpl.rpg.AndorsTrail.model.actor.Actor;
import com.gpl.rpg.AndorsTrail.model.actor.Monster;
import com.gpl.rpg.AndorsTrail.model.actor.Player;
import com.gpl.rpg.AndorsTrail.model.conversation.Reply;
import com.gpl.rpg.AndorsTrail.model.item.Loot;
import com.gpl.rpg.AndorsTrail.model.quest.Quest;
import com.gpl.rpg.AndorsTrail.model.quest.QuestLogEntry;
import com.gpl.rpg.AndorsTrail.model.quest.QuestProgress;
import com.gpl.rpg.AndorsTrail.resource.tiles.TileManager;
import java.util.ArrayList;
public final class ConversationActivity extends Activity implements OnKeyListener
, ConversationController.ConversationStatemachine.ConversationStateListener {
public static final int ACTIVITYRESULT_ATTACK = Activity.RESULT_FIRST_USER + 1;
public static final int ACTIVITYRESULT_REMOVE = Activity.RESULT_FIRST_USER + 2;
private static final int playerNameColor = Color.argb(255, 0xbb, 0x22, 0x22);
private static final int NPCNameColor = Color.argb(255, 0xbb, 0xbb, 0x22);
private static final int playerPhraseColor = 0;
private static final int NPCPhraseColor = 0;
private static final int rewardColor = Color.argb(255, 0x99, 0x99, 0x55);
private WorldContext world;
private Player player;
private ArrayList<ConversationStatement> conversationHistory = new ArrayList<ConversationStatement>();
private ConversationController.ConversationStatemachine conversationState;
private StatementContainerAdapter listAdapter;
private Button nextButton;
private ListView statementList;
private RadioGroup replyGroup;
private OnCheckedChangeListener radioButtonListener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndorsTrailApplication app = AndorsTrailApplication.getApplicationFromActivity(this);
if (!app.isInitialized()) { finish(); return; }
this.world = app.getWorld();
this.player = world.model.player;
this.conversationState = new ConversationController.ConversationStatemachine(world, app.getControllerContext(), getResources(), this);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.conversation);
replyGroup = new RadioGroup(this);
replyGroup.setLayoutParams(new ListView.LayoutParams(ListView.LayoutParams.MATCH_PARENT, ListView.LayoutParams.WRAP_CONTENT));
statementList = (ListView) findViewById(R.id.conversation_statements);
statementList.addFooterView(replyGroup);
listAdapter = new StatementContainerAdapter(this, conversationHistory, world.tileManager);
statementList.setAdapter(listAdapter);
nextButton = (Button) findViewById(R.id.conversation_next);
Button leaveButton = (Button) findViewById(R.id.conversation_leave);
leaveButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ConversationActivity.this.finish();
}
});
radioButtonListener = new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
nextButton.setEnabled(true);
}
};
nextButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
nextButtonClicked();
}
});
nextButton.setEnabled(false);
statementList.setOnKeyListener(this);
statementList.setSelected(false);
statementList.setFocusable(false);
statementList.setFocusableInTouchMode(false);
if (savedInstanceState != null) {
conversationState.setCurrentNPC(Dialogs.getMonsterFromBundle(savedInstanceState, world));
ArrayList<ConversationStatement> savedConversationHistory = savedInstanceState.getParcelableArrayList("conversationHistory");
if (savedConversationHistory != null) conversationHistory.addAll(savedConversationHistory);
final String phraseID = savedInstanceState.getString("phraseID");
conversationState.proceedToRestoredState(phraseID);
} else {
conversationState.setCurrentNPC(Dialogs.getMonsterFromIntent(getIntent(), world));
final String phraseID = getIntent().getData().getLastPathSegment();
conversationState.proceedToPhrase(phraseID);
}
}
@Override
protected void onResume() {
super.onResume();
nextButton.requestFocus();
}
private int getSelectedReplyIndex() {
for (int i = 0; i < replyGroup.getChildCount(); ++i) {
final View v = replyGroup.getChildAt(i);
if (v == null) continue;
final RadioButton rb = (RadioButton) v;
if (rb.isChecked()) return i;
}
return -1;
}
private void setSelectedReplyIndex(int i) {
int replyCount = replyGroup.getChildCount();
if (replyCount <= 0) return;
if (i < 0) i = 0;
else if (i >= replyCount) i = replyCount - 1;
View v = replyGroup.getChildAt(i);
if (v == null) return;
RadioButton rb = (RadioButton) v;
rb.setChecked(true);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (handleKeypress(keyCode, event)) return true;
else return super.onKeyDown(keyCode, event);
}
@Override
public boolean onKey(View arg0, int keyCode, KeyEvent event) {
if (event.getAction() != KeyEvent.ACTION_DOWN) return false;
return handleKeypress(keyCode, event);
}
public boolean handleKeypress(int keyCode, KeyEvent event) {
int selectedReplyIndex = getSelectedReplyIndex();
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_UP:
--selectedReplyIndex;
setSelectedReplyIndex(selectedReplyIndex);
return true;
case KeyEvent.KEYCODE_DPAD_DOWN:
++selectedReplyIndex;
setSelectedReplyIndex(selectedReplyIndex);
return true;
case KeyEvent.KEYCODE_SPACE:
case KeyEvent.KEYCODE_ENTER:
case KeyEvent.KEYCODE_DPAD_CENTER:
if (nextButton.isEnabled()) nextButton.performClick();
return true;
case KeyEvent.KEYCODE_1: setSelectedReplyIndex(0); return true;
case KeyEvent.KEYCODE_2: setSelectedReplyIndex(1); return true;
case KeyEvent.KEYCODE_3: setSelectedReplyIndex(2); return true;
case KeyEvent.KEYCODE_4: setSelectedReplyIndex(3); return true;
case KeyEvent.KEYCODE_5: setSelectedReplyIndex(4); return true;
case KeyEvent.KEYCODE_6: setSelectedReplyIndex(5); return true;
case KeyEvent.KEYCODE_7: setSelectedReplyIndex(6); return true;
case KeyEvent.KEYCODE_8: setSelectedReplyIndex(7); return true;
case KeyEvent.KEYCODE_9: setSelectedReplyIndex(8); return true;
default: return false;
}
}
private RadioButton getSelectedReplyButton() {
for (int i = 0; i < replyGroup.getChildCount(); ++i) {
final View v = replyGroup.getChildAt(i);
if (v == null) continue;
final RadioButton rb = (RadioButton) v;
if (rb.isChecked()) {
return rb;
}
}
return null; // No reply was found. This is probably an error.
}
private void nextButtonClicked() {
RadioButton rb = getSelectedReplyButton();
replyGroup.removeAllViews();
nextButton.setEnabled(false);
if (conversationState.hasOnlyOneNextReply()) {
conversationState.playerSelectedNextStep();
} else {
if (rb == null) return;
Reply r = (Reply) rb.getTag();
addConversationStatement(player, rb.getText().toString(), playerPhraseColor);
conversationState.playerSelectedReply(r);
}
}
private void addConversationStatement(Actor actor, String text, int textColor) {
ConversationStatement s = new ConversationStatement();
if (actor != null) {
s.iconID = actor.iconID;
s.actorName = actor.getName();
} else {
s.iconID = ConversationStatement.NO_ICON;
}
s.text = text;
s.nameColor = actor == player ? playerNameColor : NPCNameColor;
s.textColor = textColor;
s.isPlayerActor = actor != null && actor == player;
conversationHistory.add(s);
statementList.clearFocus();
listAdapter.notifyDataSetChanged();
statementList.requestLayout();
}
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putString("phraseID", conversationState.getCurrentPhraseID());
outState.putParcelableArrayList("conversationHistory", conversationHistory);
Dialogs.addMonsterIdentifiers(outState, conversationState.getCurrentNPC());
}
private static final class ConversationStatement implements Parcelable {
public static final int NO_ICON = -1;
public String actorName;
public String text;
public int iconID;
public int nameColor;
public int textColor;
public boolean isPlayerActor;
public boolean hasActor() {
return iconID != NO_ICON;
}
@Override
public int describeContents() { return 0; }
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(actorName);
dest.writeString(text);
dest.writeInt(iconID);
dest.writeInt(nameColor);
dest.writeInt(textColor);
dest.writeByte((byte) (isPlayerActor ? 1 : 0));
}
@SuppressWarnings("unused")
public static final Parcelable.Creator<ConversationStatement> CREATOR = new Parcelable.Creator<ConversationStatement>() {
public ConversationStatement createFromParcel(Parcel in) {
ConversationStatement result = new ConversationStatement();
result.actorName = in.readString();
result.text = in.readString();
result.iconID = in.readInt();
result.nameColor = in.readInt();
result.textColor = in.readInt();
result.isPlayerActor = in.readByte() == 1;
return result;
}
public ConversationStatement[] newArray(int size) {
return new ConversationStatement[size];
}
};
}
private static final class StatementContainerAdapter extends ArrayAdapter<ConversationStatement> {
private final TileManager tileManager;
public StatementContainerAdapter(Context context, ArrayList<ConversationStatement> items, TileManager tileManager) {
super(context, 0, items);
this.tileManager = tileManager;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ConversationStatement statement = getItem(position);
View result = convertView;
if (result == null) {
result = View.inflate(getContext(), R.layout.conversation_statement, null);
}
final TextView tv = (TextView) result.findViewById(R.id.conversation_text);
if (statement.hasActor()) {
final Resources res = getContext().getResources();
if (statement.isPlayerActor) tileManager.setImageViewTileForPlayer(res, tv, statement.iconID);
else tileManager.setImageViewTileForMonster(res, tv, statement.iconID);
tv.setText(statement.actorName + ": " + statement.text, BufferType.SPANNABLE);
Spannable sp = (Spannable) tv.getText();
sp.setSpan(new ForegroundColorSpan(statement.nameColor), 0, statement.actorName.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
if (statement.textColor != 0) {
sp.setSpan(new ForegroundColorSpan(statement.textColor), statement.actorName.length()+1, sp.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
} else {
tv.setCompoundDrawables(null, null, null, null);
if (statement.textColor == 0) {
tv.setText(statement.text);
} else {
tv.setText(statement.text, BufferType.SPANNABLE);
Spannable sp = (Spannable) tv.getText();
sp.setSpan(new ForegroundColorSpan(statement.textColor), 0, statement.text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
return result;
}
@Override
public boolean areAllItemsEnabled() {
return false;
}
@Override
public boolean isEnabled(int position) {
return false;
}
}
@Override
public void onTextPhraseReached(String message, Actor actor) {
addConversationStatement(actor, message, NPCPhraseColor);
}
@Override
public void onPlayerReceivedRewards(ConversationController.PhraseRewards phraseRewards) {
Loot loot = phraseRewards.loot;
for (QuestProgress reward : phraseRewards.questProgress) {
Quest q = world.quests.getQuest(reward.questID);
if (!q.showInLog) continue;
QuestLogEntry logEntry = q.getQuestLogEntry(reward.progress);
if (logEntry.finishesQuest) {
addRewardMessage(getString(R.string.conversation_reward_quest_finished, q.name));
} else {
addRewardMessage(getString(R.string.conversation_reward_quest_updated, q.name));
}
}
if (loot.exp > 0) {
addRewardMessage(getString(R.string.conversation_rewardexp, loot.exp));
}
if (loot.gold > 0) {
addRewardMessage(getString(R.string.conversation_rewardgold, loot.gold));
} else if (loot.gold < 0) {
addRewardMessage(getString(R.string.conversation_lostgold, -loot.gold));
}
if (!loot.items.isEmpty()) {
final int len = loot.items.countItems();
if (len == 1) {
addRewardMessage(getString(R.string.conversation_rewarditem));
} else {
addRewardMessage(getString(R.string.conversation_rewarditems, len));
}
}
}
private void addRewardMessage(String text) {
addConversationStatement(null, text, rewardColor);
}
@Override
public void onConversationEnded() {
ConversationActivity.this.finish();
}
@Override
public void onConversationEndedWithShop(Monster npc) {
Intent intent = new Intent(this, ShopActivity.class);
Dialogs.addMonsterIdentifiers(intent, npc);
startActivity(intent);
ConversationActivity.this.finish();
}
@Override
public void onConversationEndedWithCombat(Monster npc) {
ConversationActivity.this.setResult(ACTIVITYRESULT_ATTACK);
ConversationActivity.this.finish();
}
@Override
public void onConversationEndedWithRemoval(Monster npc) {
ConversationActivity.this.setResult(ACTIVITYRESULT_REMOVE);
ConversationActivity.this.finish();
}
@Override
public void onConversationCanProceedWithNext() {
nextButton.setEnabled(true);
}
@Override
public void onConversationHasReply(Reply r, String message) {
RadioGroup.LayoutParams layoutParams = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT, RadioGroup.LayoutParams.WRAP_CONTENT);
RadioButton rb = new RadioButton(this);
rb.setLayoutParams(layoutParams);
rb.setText(message);
rb.setOnCheckedChangeListener(radioButtonListener);
rb.setTag(r);
rb.setShadowLayer(1, 1, 1, Color.BLACK);
rb.setFocusable(false);
rb.setFocusableInTouchMode(false);
replyGroup.addView(rb, layoutParams);
}
}