Восстановление фона при загрузке.

This commit is contained in:
Victor 2015-05-03 14:59:41 +03:00
parent 8e198f3616
commit 722ef1a719
2 changed files with 53 additions and 3 deletions

View File

@ -18,14 +18,18 @@ public final class SaveInfo implements Parcelable {
private long time;
private int position;
private Map<String, Double> variables;
private String backgroundType, backgroundName;
public SaveInfo() {}
public SaveInfo(String path, long time, int position, Map<String, Double> variables) {
public SaveInfo(String path, long time, int position, Map<String, Double> variables,
String bgType, String bgName) {
this.path = path;
this.time = time;
this.position = position;
this.variables = variables;
this.backgroundType = bgType;
this.backgroundName = bgName;
}
private SaveInfo(Parcel in) {
@ -37,6 +41,10 @@ public final class SaveInfo implements Parcelable {
for (int i = 0; i < varSize; i++) {
variables.put(in.readString(), in.readDouble());
}
try {
backgroundType = in.readString();
backgroundName = in.readString();
} catch (Exception ioe) {}
}
private SaveInfo(DataInputStream in) throws IOException {
@ -48,6 +56,10 @@ public final class SaveInfo implements Parcelable {
for (int i = 0; i < varSize; i++) {
variables.put(in.readUTF(), in.readDouble());
}
try {
backgroundType = in.readUTF();
backgroundName = in.readUTF();
} catch (Exception ioe) {}
}
public String getPath() {
@ -82,6 +94,22 @@ public final class SaveInfo implements Parcelable {
this.variables = variables;
}
public String getBackgroundType() {
return backgroundType;
}
public void setBackgroundType(String backgroundType) {
this.backgroundType = backgroundType;
}
public String getBackgroundName() {
return backgroundName;
}
public void setBackgroundName(String backgroundName) {
this.backgroundName = backgroundName;
}
public static SaveInfo readFromStream(DataInputStream dis) throws IOException {
return new SaveInfo(dis);
}
@ -95,6 +123,8 @@ public final class SaveInfo implements Parcelable {
dos.writeUTF(entry.getKey());
dos.writeDouble(entry.getValue());
}
dos.writeUTF(backgroundType);
dos.writeUTF(backgroundName);
}
@Override
@ -112,6 +142,8 @@ public final class SaveInfo implements Parcelable {
dest.writeString(entry.getKey());
dest.writeDouble(entry.getValue());
}
dest.writeString(backgroundType);
dest.writeString(backgroundName);
}
public static final Parcelable.Creator<SaveInfo> CREATOR = new Parcelable.Creator<SaveInfo>() {

View File

@ -47,6 +47,7 @@ public final class ViewActivity extends Activity implements TouchGesture.OnTouch
private String scriptPath;
private String backgroundName, backgroundType;
private ImageView background;
private FrameLayout container;
private TextView textview;
@ -97,7 +98,6 @@ public final class ViewActivity extends Activity implements TouchGesture.OnTouch
InputStream stream = null;
if (save != null) {
stream = fromSave(save);
Toast.makeText(this, R.string.loaded, Toast.LENGTH_SHORT).show();
} else if (intent.hasExtra(EXTRA_NAME)) {
stream = fromAssets( PathResolver.script(intent.getStringExtra(EXTRA_NAME)) );
} else if (intent.getData() != null) {
@ -108,7 +108,6 @@ public final class ViewActivity extends Activity implements TouchGesture.OnTouch
}
Parser.parse(Lexer.tokenize( IOUtil.readContents(stream) ), save);
Parser.getInstance().next();
stream = null;
} catch (Exception ex) {
final String message = getString(R.string.error_open_file, scriptPath);
@ -116,6 +115,15 @@ public final class ViewActivity extends Activity implements TouchGesture.OnTouch
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
finish();
}
if (save != null) {
backgroundType = save.getBackgroundType();
backgroundName = save.getBackgroundName();
Toast.makeText(this, R.string.loaded, Toast.LENGTH_SHORT).show();
}
if (backgroundName != null) {
background(backgroundType, backgroundName, "");
}
Parser.getInstance().next();
}
private InputStream fromAssets(String path) throws IOException {
@ -196,6 +204,9 @@ public final class ViewActivity extends Activity implements TouchGesture.OnTouch
}
public void background(String type, String name, String effect) {
if (TextUtils.isEmpty(name)) return;
backgroundType = type;
backgroundName = name;
text("");
spritesClear();
if (name.equalsIgnoreCase("black")) background.setImageResource(android.R.color.black);
@ -460,6 +471,11 @@ public final class ViewActivity extends Activity implements TouchGesture.OnTouch
if (save.getPath().equals(scriptPath)) {
// Восстанавливаем сохранение текущего сценария.
Variables.setVariables(save.getVariables());
if (!TextUtils.isEmpty(save.getBackgroundName())) {
backgroundType = save.getBackgroundType();
backgroundName = save.getBackgroundName();
ViewActivity.getInstance().background(backgroundType, backgroundName, "");
}
Parser.getInstance().setPosition(save.getPosition());
Toast.makeText(ViewActivity.this, R.string.loaded, Toast.LENGTH_SHORT).show();
} else {
@ -480,6 +496,8 @@ public final class ViewActivity extends Activity implements TouchGesture.OnTouch
info.setPosition(Parser.getInstance().getLastPosition());
info.setTime(timestamp);
info.setVariables(Variables.getVariables());
info.setBackgroundType(backgroundType);
info.setBackgroundName(backgroundName);
try {
final String filename = Long.toString(timestamp);
IOUtil.writeSaveInfo(getApplicationContext(), filename, info);