-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStoryDialog.java
252 lines (220 loc) · 8.51 KB
/
StoryDialog.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
package imo.card.gametest;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
//TODO: Explain everything
public class StoryDialog extends Dialog {
LinearLayout mainStoryLayout;
TextView storyTxt;
TextView choicesHintTxt;
LinearLayout choicesParentLayout;
LinearLayout choice1Layout;
int choice1LayoutId;
ImageView choice1Img;
TextView choice1Txt;
LinearLayout choice2Layout;
int choice2LayoutId;
ImageView choice2Img;
TextView choice2Txt;
TextView hintTxt;
ArrayList<Map<String, String>> routeList = new ArrayList<>();
ArrayList<Map<String, String>> initialSceneList = new ArrayList<>();
String[] sceneSequencesArray;
int sequenceIndex = 1;
int textIndex = 1;
boolean stillDisplaySequence = true;
boolean hasChosen = false;
Map<String, String> choice1Map = new HashMap<>();
Map<String, String> choice2Map = new HashMap<>();
public Map<String, String> chosenMap = new HashMap<>();
public StoryDialog(final Context context) {
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setCancelable(false);
setContentView(R.layout.story);
//Set the dialog window to match the parent's width and height then set it to transparent
getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
mainStoryLayout = findViewById(R.id.main_story_layout);
storyTxt = findViewById(R.id.story_txt);
choicesHintTxt = findViewById(R.id.choices_hint_txt);
choicesParentLayout = findViewById(R.id.choices_parent_layout);
choice1LayoutId = R.id.choice1_layout;
choice1Layout = findViewById(choice1LayoutId);
choice1Img = findViewById(R.id.choice1_img);
choice1Txt = findViewById(R.id.choice1_txt);
choice2LayoutId = R.id.choice2_layout;
choice2Layout = findViewById(choice2LayoutId);
choice2Img = findViewById(R.id.choice2_img);
choice2Txt = findViewById(R.id.choice2_txt);
hintTxt = findViewById(R.id.hint_txt);
onDialogCreate(context);
mainStoryLayout.setOnClickListener(new View.OnClickListener(){
@Override public void onClick(View v){
mainOnClick(context);
}
});
View.OnClickListener choicesClickListener = new View.OnClickListener(){
@Override public void onClick(View view){
choicesOnClick(context, view);
}
};
choice1Layout.setOnClickListener(choicesClickListener);
choice2Layout.setOnClickListener(choicesClickListener);
choicesParentLayout.setOnClickListener(new View.OnClickListener(){
@Override public void onClick(View v){
//this is to prevent mainStoryLayout from clicking inside this layout
}
});
}
public void onDialogCreate(Context context){
choicesParentLayout.setVisibility(View.GONE);
int strokeColor = Color.WHITE;
int strokeWidth = 2;
float strokeAlpha = 0.3f;
int cornerRadius = 10;
int bgColor = Color.TRANSPARENT;
Tools.setCustomBgWithStroke(choice1Layout, bgColor, cornerRadius, strokeWidth, strokeColor, strokeAlpha);
Tools.setCustomBgWithStroke(choice2Layout, bgColor, cornerRadius, strokeWidth, strokeColor, strokeAlpha);
routeList = Data.routeData;
initialSceneList = Data.initialSceneData;
Map<String, String> startingSceneMap =
Tools.findMapFromArraylist(initialSceneList, "scene_id", "initial_scene");
String getSceneSequences = startingSceneMap.get("scene_sequences").trim();
if (getSceneSequences.charAt(0) == '»') {
getSceneSequences = getSceneSequences.replaceFirst("»", "");
}
sceneSequencesArray = getSceneSequences.split("»");
String firstSequence = sceneSequencesArray[0];
storyTxt.setText(firstSequence.substring(5).trim());
}
public void mainOnClick(Context context){
int sceneSequencesLength = sceneSequencesArray.length - 1;
if(sequenceIndex <= sceneSequencesLength){
if(stillDisplaySequence){
if(hasChosen){
choicesParentLayout.setVisibility(View.GONE);
choicesHintTxt.setVisibility(View.GONE);
hintTxt.setVisibility(View.VISIBLE);
storyTxt.setText("");
hasChosen = false;
}
String stringSequence = sceneSequencesArray[sequenceIndex].trim();
if(stringSequence.startsWith("text:")){
String textviewString = storyTxt.getText().toString().trim();
stringSequence = stringSequence.substring(5);
//this is to only display 4 string at a time
if (textIndex % 4 == 0) {
// index is a multiple of 3.
storyTxt.setText("");
storyTxt.setText(stringSequence);
} else {
// index is not a multiple of 3.
storyTxt.setText(textviewString +"\n\n" + stringSequence);
}
textIndex++;
}else if (stringSequence.startsWith("pick:")){
choicesParentLayout.setVisibility(View.VISIBLE);
choicesHintTxt.setVisibility(View.VISIBLE);
hintTxt.setVisibility(View.INVISIBLE);
stillDisplaySequence = false;
hasChosen = false;
makeChoicesSelection(stringSequence, choice1Txt, choice2Txt);
textIndex = 1;
}
sequenceIndex++;
}
}else{
dismiss();
}
}
public void makeChoicesSelection(String stringSequence, TextView choice1Txt, TextView choice2Txt){
//usually stringSequence has "pick:" at the start.
//remove it by doing substring(5)
stringSequence = stringSequence.substring(5).trim();
//typically it has "," in between two text. split it by that into 2 parts
String[] choiceArray = stringSequence.split(",");
Map<String, String> outputMap = new HashMap<>();
Random random = new Random();
int forChoice = 0;
for(String choiceString : choiceArray){
forChoice++;
//for picking route by its level
String routeLevelKey = null;
String routeLevelValue = null;
if (choiceString.equals("route_level_1")) {
routeLevelKey = "level";
routeLevelValue = "1";
} else if (choiceString.equals("route_level_2")) {
routeLevelKey = "level";
routeLevelValue = "2";
}
if (routeLevelKey != null||routeLevelValue != null) {//this will run only if its populated
List<Map<String, String>> levelMaps = new ArrayList<>();
//this will search a map with desired key and value from routeData
//list maps that has the same key and value
for (Map<String, String> map : routeList) {
if (map.get(routeLevelKey).equals(routeLevelValue)) {
levelMaps.add(map);
}
}
if (!levelMaps.isEmpty()) {
//only execute if there is any map added on levelMaps
//randomly pick a map from the levelMaps
outputMap = levelMaps.get(random.nextInt(levelMaps.size()));
}
if (forChoice == 1){
choice1Map = outputMap;
choiceArray[0] = choice1Map.get("name");
}else if (forChoice == 2){
choice2Map = outputMap;
choiceArray[1] = choice2Map.get("name");
}
}
choice1Txt.setText(choiceArray[0]);
choice2Txt.setText(choiceArray[1]);
}
}
public void choicesOnClick(Context context, View view){
stillDisplaySequence = true;
hasChosen = true;
hintTxt.setVisibility(View.VISIBLE);
int strokeColor = Color.WHITE;
int strokeWidth = 2;
float strokeAlpha = 0.3f;
int cornerRadius = 10;
int bgColor = Color.TRANSPARENT;
Tools.setCustomBgWithStroke(choice1Layout, bgColor, cornerRadius, strokeWidth, strokeColor, strokeAlpha);
Tools.setCustomBgWithStroke(choice2Layout, bgColor, cornerRadius, strokeWidth, strokeColor, strokeAlpha);
strokeWidth = 4;
bgColor = Color.parseColor("#1affffff");
if (view.getId() == choice1LayoutId) {
Tools.setCustomBgWithStroke(choice1Layout, bgColor, cornerRadius, strokeWidth, strokeColor, strokeAlpha);
chosenMap = choice1Map;
} else if (view.getId() == choice2LayoutId) {
Tools.setCustomBgWithStroke(choice2Layout, bgColor, cornerRadius, strokeWidth, strokeColor, strokeAlpha);
chosenMap = choice2Map;
}
if (!chosenMap.isEmpty()) {
String hintString = "";
if(chosenMap.containsKey("name")){
hintString = hintString + chosenMap.get("name");
if(chosenMap.containsKey("info")){
hintString = hintString + ": " + chosenMap.get("info");
}
}
choicesHintTxt.setText(hintString);
}
}
}