mirror of
https://github.com/AngelAuraMC/Amethyst-Android.git
synced 2025-09-15 23:59:21 -04:00
Different exit handling for Custom Controls
This commit is contained in:
parent
c963bc2b6c
commit
a25865a1ed
@ -1,6 +1,5 @@
|
|||||||
package net.kdt.pojavlaunch;
|
package net.kdt.pojavlaunch;
|
||||||
|
|
||||||
import android.app.Activity;
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
@ -9,7 +8,6 @@ import android.os.Bundle;
|
|||||||
import android.provider.DocumentsContract;
|
import android.provider.DocumentsContract;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.ArrayAdapter;
|
import android.widget.ArrayAdapter;
|
||||||
import android.widget.Button;
|
|
||||||
import android.widget.EditText;
|
import android.widget.EditText;
|
||||||
import android.widget.ListView;
|
import android.widget.ListView;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
@ -24,18 +22,17 @@ import com.kdt.pickafile.FileSelectedListener;
|
|||||||
import net.kdt.pojavlaunch.customcontrols.ControlData;
|
import net.kdt.pojavlaunch.customcontrols.ControlData;
|
||||||
import net.kdt.pojavlaunch.customcontrols.ControlDrawerData;
|
import net.kdt.pojavlaunch.customcontrols.ControlDrawerData;
|
||||||
import net.kdt.pojavlaunch.customcontrols.ControlLayout;
|
import net.kdt.pojavlaunch.customcontrols.ControlLayout;
|
||||||
|
import net.kdt.pojavlaunch.customcontrols.Exitable;
|
||||||
import net.kdt.pojavlaunch.prefs.LauncherPreferences;
|
import net.kdt.pojavlaunch.prefs.LauncherPreferences;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
|
||||||
public class CustomControlsActivity extends BaseActivity {
|
public class CustomControlsActivity extends BaseActivity implements Exitable {
|
||||||
private DrawerLayout mDrawerLayout;
|
private DrawerLayout mDrawerLayout;
|
||||||
private ListView mDrawerNavigationView;
|
private ListView mDrawerNavigationView;
|
||||||
private ControlLayout mControlLayout;
|
private ControlLayout mControlLayout;
|
||||||
|
|
||||||
public boolean isModified = false;
|
|
||||||
private static String sSelectedName = "new_control";
|
private static String sSelectedName = "new_control";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -59,7 +56,7 @@ public class CustomControlsActivity extends BaseActivity {
|
|||||||
case 1: mControlLayout.addDrawer(new ControlDrawerData()); break;
|
case 1: mControlLayout.addDrawer(new ControlDrawerData()); break;
|
||||||
//case 2: mControlLayout.addJoystickButton(new ControlData()); break;
|
//case 2: mControlLayout.addJoystickButton(new ControlData()); break;
|
||||||
case 2: load(mControlLayout); break;
|
case 2: load(mControlLayout); break;
|
||||||
case 3: save(false, mControlLayout); break;
|
case 3: save( mControlLayout, this); break;
|
||||||
case 4: dialogSelectDefaultCtrl(mControlLayout); break;
|
case 4: dialogSelectDefaultCtrl(mControlLayout); break;
|
||||||
case 5: // Saving the currently shown control
|
case 5: // Saving the currently shown control
|
||||||
try {
|
try {
|
||||||
@ -81,7 +78,6 @@ public class CustomControlsActivity extends BaseActivity {
|
|||||||
}
|
}
|
||||||
mDrawerLayout.closeDrawers();
|
mDrawerLayout.closeDrawers();
|
||||||
});
|
});
|
||||||
mControlLayout.setActivity(this);
|
|
||||||
mControlLayout.setModifiable(true);
|
mControlLayout.setModifiable(true);
|
||||||
|
|
||||||
loadControl(LauncherPreferences.PREF_DEFAULTCTRL_PATH, mControlLayout);
|
loadControl(LauncherPreferences.PREF_DEFAULTCTRL_PATH, mControlLayout);
|
||||||
@ -89,13 +85,7 @@ public class CustomControlsActivity extends BaseActivity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onBackPressed() {
|
public void onBackPressed() {
|
||||||
if (!isModified) {
|
mControlLayout.askToExit(this);
|
||||||
setResult(Activity.RESULT_OK, new Intent());
|
|
||||||
super.onBackPressed();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
save(true, mControlLayout);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void dialogSelectDefaultCtrl(final ControlLayout layout) {
|
public static void dialogSelectDefaultCtrl(final ControlLayout layout) {
|
||||||
@ -119,7 +109,7 @@ public class CustomControlsActivity extends BaseActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static void save(final boolean exit, final ControlLayout layout) {
|
public static void save(final ControlLayout layout, final Exitable exitListener) {
|
||||||
final Context ctx = layout.getContext();
|
final Context ctx = layout.getContext();
|
||||||
final EditText edit = new EditText(ctx);
|
final EditText edit = new EditText(ctx);
|
||||||
edit.setSingleLine();
|
edit.setSingleLine();
|
||||||
@ -130,49 +120,58 @@ public class CustomControlsActivity extends BaseActivity {
|
|||||||
builder.setView(edit);
|
builder.setView(edit);
|
||||||
builder.setPositiveButton(android.R.string.ok, null);
|
builder.setPositiveButton(android.R.string.ok, null);
|
||||||
builder.setNegativeButton(android.R.string.cancel, null);
|
builder.setNegativeButton(android.R.string.cancel, null);
|
||||||
if (exit) {
|
if(exitListener != null) builder.setNeutralButton(R.string.global_save_and_exit, null);
|
||||||
builder.setNeutralButton(R.string.mcn_exit_call, (p1, p2) -> {
|
|
||||||
layout.setModifiable(false);
|
|
||||||
if(ctx instanceof MainActivity) {
|
|
||||||
((MainActivity) ctx).leaveCustomControls();
|
|
||||||
}else{
|
|
||||||
((CustomControlsActivity) ctx).isModified = false;
|
|
||||||
((Activity)ctx).onBackPressed();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
final AlertDialog dialog = builder.create();
|
final AlertDialog dialog = builder.create();
|
||||||
dialog.setOnShowListener(dialogInterface -> {
|
dialog.setOnShowListener(dialogInterface -> {
|
||||||
|
dialog.getButton(AlertDialog.BUTTON_POSITIVE)
|
||||||
Button button = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
|
.setOnClickListener(new OnClickExitListener(dialog, edit, layout, null));
|
||||||
button.setOnClickListener(view -> {
|
if(exitListener != null) dialog.getButton(AlertDialog.BUTTON_NEUTRAL)
|
||||||
if (edit.getText().toString().isEmpty()) {
|
.setOnClickListener(new OnClickExitListener(dialog, edit, layout, exitListener));
|
||||||
edit.setError(ctx.getResources().getString(R.string.global_error_field_empty));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
String jsonPath = doSaveCtrl(edit.getText().toString(),layout);
|
|
||||||
Toast.makeText(ctx, ctx.getString(R.string.global_save) + ": " + jsonPath, Toast.LENGTH_SHORT).show();
|
|
||||||
|
|
||||||
dialog.dismiss();
|
|
||||||
if (!exit) return;
|
|
||||||
|
|
||||||
if(ctx instanceof MainActivity) {
|
|
||||||
((MainActivity) ctx).leaveCustomControls();
|
|
||||||
}else{
|
|
||||||
((Activity)ctx).onBackPressed();
|
|
||||||
}
|
|
||||||
} catch (Throwable th) {
|
|
||||||
Tools.showError(ctx, th, exit);
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
dialog.show();
|
dialog.show();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static class OnClickExitListener implements View.OnClickListener {
|
||||||
|
private final AlertDialog mDialog;
|
||||||
|
private final EditText mEditText;
|
||||||
|
private final ControlLayout mLayout;
|
||||||
|
private final Exitable mListener;
|
||||||
|
|
||||||
|
public OnClickExitListener(AlertDialog mDialog, EditText mEditText, ControlLayout mLayout, Exitable mListener) {
|
||||||
|
this.mDialog = mDialog;
|
||||||
|
this.mEditText = mEditText;
|
||||||
|
this.mLayout = mLayout;
|
||||||
|
this.mListener = mListener;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onClick(View v) {
|
||||||
|
Context context = v.getContext();
|
||||||
|
if (mEditText.getText().toString().isEmpty()) {
|
||||||
|
mEditText.setError(context.getString(R.string.global_error_field_empty));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
String jsonPath = doSaveCtrl(mEditText.getText().toString(),mLayout);
|
||||||
|
Toast.makeText(context, context.getString(R.string.global_save) + ": " + jsonPath, Toast.LENGTH_SHORT).show();
|
||||||
|
mDialog.dismiss();
|
||||||
|
if(mListener != null) mListener.exitEditor();
|
||||||
|
} catch (Throwable th) {
|
||||||
|
Tools.showError(context, th, mListener != null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void showExitDialog(Context context, Exitable exitListener) {
|
||||||
|
AlertDialog.Builder builder = new AlertDialog.Builder(context);
|
||||||
|
builder.setTitle(R.string.customctrl_editor_exit_title);
|
||||||
|
builder.setMessage(R.string.customctrl_editor_exit_msg);
|
||||||
|
builder.setPositiveButton(R.string.global_yes, (d,w)->exitListener.exitEditor());
|
||||||
|
builder.setNegativeButton(R.string.global_no, (d,w)->{});
|
||||||
|
builder.show();
|
||||||
|
}
|
||||||
|
|
||||||
public static void load(final ControlLayout layout) {
|
public static void load(final ControlLayout layout) {
|
||||||
AlertDialog.Builder builder = new AlertDialog.Builder(layout.getContext());
|
AlertDialog.Builder builder = new AlertDialog.Builder(layout.getContext());
|
||||||
builder.setTitle(R.string.global_load);
|
builder.setTitle(R.string.global_load);
|
||||||
@ -222,4 +221,9 @@ public class CustomControlsActivity extends BaseActivity {
|
|||||||
Tools.showError(layout.getContext(), e);
|
Tools.showError(layout.getContext(), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void exitEditor() {
|
||||||
|
super.onBackPressed();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -46,6 +46,7 @@ import net.kdt.pojavlaunch.customcontrols.ControlData;
|
|||||||
import net.kdt.pojavlaunch.customcontrols.ControlDrawerData;
|
import net.kdt.pojavlaunch.customcontrols.ControlDrawerData;
|
||||||
import net.kdt.pojavlaunch.customcontrols.ControlLayout;
|
import net.kdt.pojavlaunch.customcontrols.ControlLayout;
|
||||||
import net.kdt.pojavlaunch.customcontrols.CustomControls;
|
import net.kdt.pojavlaunch.customcontrols.CustomControls;
|
||||||
|
import net.kdt.pojavlaunch.customcontrols.Exitable;
|
||||||
import net.kdt.pojavlaunch.customcontrols.keyboard.LwjglCharSender;
|
import net.kdt.pojavlaunch.customcontrols.keyboard.LwjglCharSender;
|
||||||
import net.kdt.pojavlaunch.customcontrols.keyboard.TouchCharInput;
|
import net.kdt.pojavlaunch.customcontrols.keyboard.TouchCharInput;
|
||||||
import net.kdt.pojavlaunch.prefs.LauncherPreferences;
|
import net.kdt.pojavlaunch.prefs.LauncherPreferences;
|
||||||
@ -61,7 +62,7 @@ import org.lwjgl.glfw.CallbackBridge;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
public class MainActivity extends BaseActivity implements ControlButtonMenuListener{
|
public class MainActivity extends BaseActivity implements ControlButtonMenuListener, Exitable {
|
||||||
public static volatile ClipboardManager GLOBAL_CLIPBOARD;
|
public static volatile ClipboardManager GLOBAL_CLIPBOARD;
|
||||||
public static final String INTENT_MINECRAFT_VERSION = "intent_version";
|
public static final String INTENT_MINECRAFT_VERSION = "intent_version";
|
||||||
|
|
||||||
@ -111,8 +112,9 @@ public class MainActivity extends BaseActivity implements ControlButtonMenuListe
|
|||||||
case 1: mControlLayout.addDrawer(new ControlDrawerData()); break;
|
case 1: mControlLayout.addDrawer(new ControlDrawerData()); break;
|
||||||
//case 2: mControlLayout.addJoystickButton(new ControlData()); break;
|
//case 2: mControlLayout.addJoystickButton(new ControlData()); break;
|
||||||
case 2 : CustomControlsActivity.load(mControlLayout); break;
|
case 2 : CustomControlsActivity.load(mControlLayout); break;
|
||||||
case 3: CustomControlsActivity.save(true,mControlLayout); break;
|
case 3: CustomControlsActivity.save(mControlLayout, this); break;
|
||||||
case 4: CustomControlsActivity.dialogSelectDefaultCtrl(mControlLayout); break;
|
case 4: CustomControlsActivity.dialogSelectDefaultCtrl(mControlLayout); break;
|
||||||
|
case 5: CustomControlsActivity.showExitDialog(this, this);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -365,24 +367,6 @@ public class MainActivity extends BaseActivity implements ControlButtonMenuListe
|
|||||||
isInEditor = true;
|
isInEditor = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void leaveCustomControls() {
|
|
||||||
try {
|
|
||||||
MainActivity.mControlLayout.loadLayout((CustomControls)null);
|
|
||||||
MainActivity.mControlLayout.setModifiable(false);
|
|
||||||
System.gc();
|
|
||||||
MainActivity.mControlLayout.loadLayout(
|
|
||||||
minecraftProfile.controlFile == null
|
|
||||||
? LauncherPreferences.PREF_DEFAULTCTRL_PATH
|
|
||||||
: Tools.CTRLMAP_PATH + "/" + minecraftProfile.controlFile);
|
|
||||||
mDrawerPullButton.setVisibility(mControlLayout.hasMenuButton() ? View.GONE : View.VISIBLE);
|
|
||||||
} catch (IOException e) {
|
|
||||||
Tools.showError(this,e);
|
|
||||||
}
|
|
||||||
//((MainActivity) this).mControlLayout.loadLayout((CustomControls)null);
|
|
||||||
navDrawer.setAdapter(gameActionArrayAdapter);
|
|
||||||
navDrawer.setOnItemClickListener(gameActionClickListener);
|
|
||||||
isInEditor = false;
|
|
||||||
}
|
|
||||||
private void openLogOutput() {
|
private void openLogOutput() {
|
||||||
loggerView.setVisibility(View.VISIBLE);
|
loggerView.setVisibility(View.VISIBLE);
|
||||||
}
|
}
|
||||||
@ -410,7 +394,13 @@ public class MainActivity extends BaseActivity implements ControlButtonMenuListe
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean dispatchKeyEvent(KeyEvent event) {
|
public boolean dispatchKeyEvent(KeyEvent event) {
|
||||||
if(isInEditor) return super.dispatchKeyEvent(event);
|
if(isInEditor) {
|
||||||
|
if(event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
|
||||||
|
if(event.getAction() == KeyEvent.ACTION_DOWN) mControlLayout.askToExit(this);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return super.dispatchKeyEvent(event);
|
||||||
|
}
|
||||||
boolean handleEvent;
|
boolean handleEvent;
|
||||||
if(!(handleEvent = minecraftGLView.processKeyEvent(event))) {
|
if(!(handleEvent = minecraftGLView.processKeyEvent(event))) {
|
||||||
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && !touchCharInput.isEnabled()) {
|
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && !touchCharInput.isEnabled()) {
|
||||||
@ -559,4 +549,24 @@ public class MainActivity extends BaseActivity implements ControlButtonMenuListe
|
|||||||
drawerLayout.openDrawer(navDrawer);
|
drawerLayout.openDrawer(navDrawer);
|
||||||
navDrawer.requestLayout();
|
navDrawer.requestLayout();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void exitEditor() {
|
||||||
|
try {
|
||||||
|
MainActivity.mControlLayout.loadLayout((CustomControls)null);
|
||||||
|
MainActivity.mControlLayout.setModifiable(false);
|
||||||
|
System.gc();
|
||||||
|
MainActivity.mControlLayout.loadLayout(
|
||||||
|
minecraftProfile.controlFile == null
|
||||||
|
? LauncherPreferences.PREF_DEFAULTCTRL_PATH
|
||||||
|
: Tools.CTRLMAP_PATH + "/" + minecraftProfile.controlFile);
|
||||||
|
mDrawerPullButton.setVisibility(mControlLayout.hasMenuButton() ? View.GONE : View.VISIBLE);
|
||||||
|
} catch (IOException e) {
|
||||||
|
Tools.showError(this,e);
|
||||||
|
}
|
||||||
|
//((MainActivity) this).mControlLayout.loadLayout((CustomControls)null);
|
||||||
|
navDrawer.setAdapter(gameActionArrayAdapter);
|
||||||
|
navDrawer.setOnItemClickListener(gameActionClickListener);
|
||||||
|
isInEditor = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -34,7 +34,7 @@ public class ControlLayout extends FrameLayout {
|
|||||||
/* Cache to buttons for performance purposes */
|
/* Cache to buttons for performance purposes */
|
||||||
private List<ControlInterface> mButtons;
|
private List<ControlInterface> mButtons;
|
||||||
private boolean mModifiable = false;
|
private boolean mModifiable = false;
|
||||||
private CustomControlsActivity mActivity;
|
private boolean mIsModified;
|
||||||
private boolean mControlVisible = false;
|
private boolean mControlVisible = false;
|
||||||
|
|
||||||
private EditControlPopup mControlPopup = null;
|
private EditControlPopup mControlPopup = null;
|
||||||
@ -187,10 +187,6 @@ public class ControlLayout extends FrameLayout {
|
|||||||
setModified(false);
|
setModified(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setActivity(CustomControlsActivity activity) {
|
|
||||||
mActivity = activity;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void toggleControlVisible(){
|
public void toggleControlVisible(){
|
||||||
mControlVisible = !mControlVisible;
|
mControlVisible = !mControlVisible;
|
||||||
setControlVisible(mControlVisible);
|
setControlVisible(mControlVisible);
|
||||||
@ -225,8 +221,7 @@ public class ControlLayout extends FrameLayout {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setModified(boolean isModified) {
|
public void setModified(boolean isModified) {
|
||||||
if (mActivity != null) mActivity.isModified = isModified;
|
mIsModified = isModified;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<ControlInterface> getButtonChildren(){
|
public List<ControlInterface> getButtonChildren(){
|
||||||
@ -412,4 +407,12 @@ public class ControlLayout extends FrameLayout {
|
|||||||
}
|
}
|
||||||
return mGameSurface;
|
return mGameSurface;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void askToExit(Exitable exitable) {
|
||||||
|
if(mIsModified) {
|
||||||
|
CustomControlsActivity.save(this, exitable);
|
||||||
|
}else{
|
||||||
|
CustomControlsActivity.showExitDialog(getContext(), exitable);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,5 @@
|
|||||||
|
package net.kdt.pojavlaunch.customcontrols;
|
||||||
|
|
||||||
|
public interface Exitable {
|
||||||
|
void exitEditor();
|
||||||
|
}
|
@ -13,6 +13,7 @@
|
|||||||
<item name="2">@string/global_load</item>
|
<item name="2">@string/global_load</item>
|
||||||
<item name="3">@string/global_save</item>
|
<item name="3">@string/global_save</item>
|
||||||
<item name="4">@string/customctrl_selectdefault</item>
|
<item name="4">@string/customctrl_selectdefault</item>
|
||||||
|
<item name="5">@string/customctrl_editor_exit</item>
|
||||||
</string-array>
|
</string-array>
|
||||||
|
|
||||||
<string-array name="menu_customcontrol_customactivity">
|
<string-array name="menu_customcontrol_customactivity">
|
||||||
|
@ -357,4 +357,10 @@
|
|||||||
<string name="preference_shader_dump_title">Enable shader dumping</string>
|
<string name="preference_shader_dump_title">Enable shader dumping</string>
|
||||||
<string name="preference_shader_dump_description">Log converted shaders into the log file</string>
|
<string name="preference_shader_dump_description">Log converted shaders into the log file</string>
|
||||||
<string name="percent_format">%d%%</string>
|
<string name="percent_format">%d%%</string>
|
||||||
|
<string name="customctrl_editor_exit_title">Leaving?</string>
|
||||||
|
<string name="customctrl_editor_exit_msg">Are you sure you want to exit?</string>
|
||||||
|
<string name="customctrl_editor_exit">Leave editor</string>
|
||||||
|
<string name="global_save_and_exit">Save and exit</string>
|
||||||
|
<string name="global_yes">Yes</string>
|
||||||
|
<string name="global_no">No</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user