[WIP] LIVE controls editor

Note: Exiting the editor is done thru the "Save" menu
This commit is contained in:
artdeell 2021-02-24 22:31:34 +03:00
parent 66890e0f2d
commit c7eabb5ec0
4 changed files with 134 additions and 73 deletions

View File

@ -10,6 +10,7 @@ import android.view.View.*;
import android.view.inputmethod.*; import android.view.inputmethod.*;
import android.widget.*; import android.widget.*;
import androidx.annotation.NonNull;
import androidx.drawerlayout.widget.*; import androidx.drawerlayout.widget.*;
import com.google.android.material.navigation.*; import com.google.android.material.navigation.*;
import java.io.*; import java.io.*;
@ -89,7 +90,8 @@ public class BaseMainActivity extends LoggableActivity {
private GestureDetector gestureDetector; private GestureDetector gestureDetector;
private TextView debugText; private TextView debugText;
private NavigationView.OnNavigationItemSelectedListener gameActionListener;
public NavigationView.OnNavigationItemSelectedListener ingameControlsEditorListener;
// private String mQueueText = new String(); // private String mQueueText = new String();
protected JMinecraftVersionList.Version mVersionInfo; protected JMinecraftVersionList.Version mVersionInfo;
@ -151,8 +153,7 @@ public class BaseMainActivity extends LoggableActivity {
drawerLayout = findViewById(R.id.main_drawer_options); drawerLayout = findViewById(R.id.main_drawer_options);
navDrawer = findViewById(R.id.main_navigation_view); navDrawer = findViewById(R.id.main_navigation_view);
navDrawer.setNavigationItemSelectedListener( gameActionListener = new NavigationView.OnNavigationItemSelectedListener() {
new NavigationView.OnNavigationItemSelectedListener() {
@Override @Override
public boolean onNavigationItemSelected(MenuItem menuItem) { public boolean onNavigationItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) { switch (menuItem.getItemId()) {
@ -174,7 +175,9 @@ public class BaseMainActivity extends LoggableActivity {
drawerLayout.closeDrawers(); drawerLayout.closeDrawers();
return true; return true;
} }
}); };
navDrawer.setNavigationItemSelectedListener(
gameActionListener);
// this.overlayView = (ViewGroup) findViewById(R.id.main_control_overlay); // this.overlayView = (ViewGroup) findViewById(R.id.main_control_overlay);
@ -985,16 +988,25 @@ public class BaseMainActivity extends LoggableActivity {
}); });
dialog.show(); dialog.show();
} }
boolean isInEditor;
private void openCustomControls() { private void openCustomControls() {
if (this instanceof MainActivity) { if(ingameControlsEditorListener != null) {
((MainActivity) this).mControlLayout.loadLayout((CustomControls) null); ((MainActivity)this).mControlLayout.setModifiable(true);
navDrawer.getMenu().clear();
navDrawer.inflateMenu(R.menu.menu_customctrl);
navDrawer.setNavigationItemSelectedListener(ingameControlsEditorListener);
isInEditor = true;
} }
Intent intent = new Intent(this, CustomControlsActivity.class);
intent.putExtra("fromMainActivity", true);
startActivityForResult(intent, 1);
} }
public void leaveCustomControls() {
if(this instanceof MainActivity) {
((MainActivity) this).mControlLayout.setModifiable(false);
}
navDrawer.getMenu().clear();
navDrawer.inflateMenu(R.menu.menu_runopt);
navDrawer.setNavigationItemSelectedListener(gameActionListener);
isInEditor = false;
}
private void openLogOutput() { private void openLogOutput() {
contentLog.setVisibility(View.VISIBLE); contentLog.setVisibility(View.VISIBLE);
mIsResuming = false; mIsResuming = false;
@ -1102,7 +1114,6 @@ public class BaseMainActivity extends LoggableActivity {
public void onBackPressed() { public void onBackPressed() {
// Prevent back // Prevent back
// Catch back as Esc keycode at another place // Catch back as Esc keycode at another place
sendKeyPress(LWJGLGLFWKeycode.GLFW_KEY_ESCAPE); sendKeyPress(LWJGLGLFWKeycode.GLFW_KEY_ESCAPE);
} }

View File

@ -30,7 +30,7 @@ public class CustomControlsActivity extends BaseActivity
public boolean isModified = false; public boolean isModified = false;
public boolean isFromMainActivity = false; public boolean isFromMainActivity = false;
private String selectedName = "new_control"; private static String selectedName = "new_control";
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -57,16 +57,16 @@ public class CustomControlsActivity extends BaseActivity
public boolean onNavigationItemSelected(MenuItem menuItem) { public boolean onNavigationItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) { switch (menuItem.getItemId()) {
case R.id.menu_ctrl_load: case R.id.menu_ctrl_load:
actionLoad(); load(ctrlLayout);
break; break;
case R.id.menu_ctrl_add: case R.id.menu_ctrl_add:
ctrlLayout.addControlButton(new ControlData("New", LWJGLGLFWKeycode.GLFW_KEY_UNKNOWN, 100, 100)); ctrlLayout.addControlButton(new ControlData("New", LWJGLGLFWKeycode.GLFW_KEY_UNKNOWN, 100, 100));
break; break;
case R.id.menu_ctrl_selectdefault: case R.id.menu_ctrl_selectdefault:
dialogSelectDefaultCtrl(); dialogSelectDefaultCtrl(ctrlLayout);
break; break;
case R.id.menu_ctrl_save: case R.id.menu_ctrl_save:
save(false); save(false,ctrlLayout);
break; break;
} }
//Toast.makeText(MainActivity.this, menuItem.getTitle() + ":" + menuItem.getItemId(), Toast.LENGTH_SHORT).show(); //Toast.makeText(MainActivity.this, menuItem.getTitle() + ":" + menuItem.getItemId(), Toast.LENGTH_SHORT).show();
@ -79,7 +79,7 @@ public class CustomControlsActivity extends BaseActivity
ctrlLayout.setActivity(this); ctrlLayout.setActivity(this);
ctrlLayout.setModifiable(true); ctrlLayout.setModifiable(true);
loadControl(LauncherPreferences.PREF_DEFAULTCTRL_PATH); loadControl(LauncherPreferences.PREF_DEFAULTCTRL_PATH,ctrlLayout);
} }
@Override @Override
@ -90,22 +90,22 @@ public class CustomControlsActivity extends BaseActivity
return; return;
} }
save(true); save(true,ctrlLayout);
} }
private void setDefaultControlJson(String path) { private static void setDefaultControlJson(String path,ControlLayout ctrlLayout) {
try { try {
// Load before save to make sure control is not error // Load before save to make sure control is not error
ctrlLayout.loadLayout(Tools.GLOBAL_GSON.fromJson(Tools.read(path), CustomControls.class)); ctrlLayout.loadLayout(Tools.GLOBAL_GSON.fromJson(Tools.read(path), CustomControls.class));
LauncherPreferences.DEFAULT_PREF.edit().putString("defaultCtrl", path).commit(); LauncherPreferences.DEFAULT_PREF.edit().putString("defaultCtrl", path).commit();
LauncherPreferences.PREF_DEFAULTCTRL_PATH = path; LauncherPreferences.PREF_DEFAULTCTRL_PATH = path;
} catch (Throwable th) { } catch (Throwable th) {
Tools.showError(this, th); Tools.showError(ctrlLayout.getContext(), th);
} }
} }
private void dialogSelectDefaultCtrl() { public static void dialogSelectDefaultCtrl(final ControlLayout layout) {
AlertDialog.Builder builder = new AlertDialog.Builder(this); AlertDialog.Builder builder = new AlertDialog.Builder(layout.getContext());
builder.setTitle(R.string.customctrl_selectdefault); builder.setTitle(R.string.customctrl_selectdefault);
builder.setPositiveButton(android.R.string.cancel, null); builder.setPositiveButton(android.R.string.cancel, null);
@ -116,7 +116,7 @@ public class CustomControlsActivity extends BaseActivity
@Override @Override
public void onFileSelected(File file, String path) { public void onFileSelected(File file, String path) {
setDefaultControlJson(path); setDefaultControlJson(path,layout);
dialog.dismiss(); dialog.dismiss();
} }
}); });
@ -124,19 +124,20 @@ public class CustomControlsActivity extends BaseActivity
dialog.show(); dialog.show();
} }
private String doSaveCtrl(String name) throws Exception { private static String doSaveCtrl(String name, final ControlLayout layout) throws Exception {
String jsonPath = Tools.CTRLMAP_PATH + "/" + name + ".json"; String jsonPath = Tools.CTRLMAP_PATH + "/" + name + ".json";
ctrlLayout.saveLayout(jsonPath); layout.saveLayout(jsonPath);
return jsonPath; return jsonPath;
} }
private void save(final boolean exit) { public static void save(final boolean exit, final ControlLayout layout) {
final EditText edit = new EditText(this); final Context ctx = layout.getContext();
final EditText edit = new EditText(ctx);
edit.setSingleLine(); edit.setSingleLine();
edit.setText(selectedName); edit.setText(selectedName);
AlertDialog.Builder builder = new AlertDialog.Builder(this); AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
builder.setTitle(R.string.global_save); builder.setTitle(R.string.global_save);
builder.setView(edit); builder.setView(edit);
builder.setPositiveButton(android.R.string.ok, null); builder.setPositiveButton(android.R.string.ok, null);
@ -145,8 +146,14 @@ public class CustomControlsActivity extends BaseActivity
builder.setNeutralButton(R.string.mcn_exit_call, new AlertDialog.OnClickListener(){ builder.setNeutralButton(R.string.mcn_exit_call, new AlertDialog.OnClickListener(){
@Override @Override
public void onClick(DialogInterface p1, int p2) { public void onClick(DialogInterface p1, int p2) {
setResult(Activity.RESULT_OK, new Intent()); layout.setModifiable(false);
CustomControlsActivity.super.onBackPressed(); if(ctx instanceof MainActivity) {
((MainActivity) ctx).leaveCustomControls();
}else{
((Activity)ctx).onBackPressed();
}
// setResult(Activity.RESULT_OK, new Intent());
// CustomControlsActivity.super.onBackPressed();
} }
}); });
} }
@ -162,19 +169,19 @@ public class CustomControlsActivity extends BaseActivity
@Override @Override
public void onClick(View view) { public void onClick(View view) {
if (edit.getText().toString().isEmpty()) { if (edit.getText().toString().isEmpty()) {
edit.setError(getResources().getString(R.string.global_error_field_empty)); edit.setError(ctx.getResources().getString(R.string.global_error_field_empty));
} else { } else {
try { try {
String jsonPath = doSaveCtrl(edit.getText().toString()); String jsonPath = doSaveCtrl(edit.getText().toString(),layout);
Toast.makeText(CustomControlsActivity.this, getString(R.string.global_save) + ": " + jsonPath, Toast.LENGTH_SHORT).show(); Toast.makeText(ctx, ctx.getString(R.string.global_save) + ": " + jsonPath, Toast.LENGTH_SHORT).show();
dialog.dismiss(); dialog.dismiss();
if (exit) { if (exit) {
CustomControlsActivity.super.onBackPressed(); //CustomControlsActivity.super.onBackPressed();
} }
} catch (Throwable th) { } catch (Throwable th) {
Tools.showError(CustomControlsActivity.this, th, exit); Tools.showError(ctx, th, exit);
} }
} }
} }
@ -185,8 +192,8 @@ public class CustomControlsActivity extends BaseActivity
} }
private void actionLoad() { public static void load(final ControlLayout layout) {
AlertDialog.Builder builder = new AlertDialog.Builder(this); AlertDialog.Builder builder = new AlertDialog.Builder(layout.getContext());
builder.setTitle(R.string.global_load); builder.setTitle(R.string.global_load);
builder.setPositiveButton(android.R.string.cancel, null); builder.setPositiveButton(android.R.string.cancel, null);
@ -197,7 +204,7 @@ public class CustomControlsActivity extends BaseActivity
@Override @Override
public void onFileSelected(File file, String path) { public void onFileSelected(File file, String path) {
loadControl(path); loadControl(path,layout);
dialog.dismiss(); dialog.dismiss();
} }
}); });
@ -205,15 +212,14 @@ public class CustomControlsActivity extends BaseActivity
dialog.show(); dialog.show();
} }
private void loadControl(String path) { private static void loadControl(String path,ControlLayout layout) {
try { try {
ctrlLayout.loadLayout(path); layout.loadLayout(path);
selectedName = new File(path).getName(); selectedName = new File(path).getName();
// Remove `.json` // Remove `.json`
selectedName = selectedName.substring(0, selectedName.length() - 5); selectedName = selectedName.substring(0, selectedName.length() - 5);
} catch (Exception e) { } catch (Exception e) {
Tools.showError(CustomControlsActivity.this, e); Tools.showError(layout.getContext(), e);
} }
} }
} }

View File

@ -7,6 +7,8 @@ import android.view.*;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.google.android.material.navigation.NavigationView;
import net.kdt.pojavlaunch.customcontrols.*; import net.kdt.pojavlaunch.customcontrols.*;
import net.kdt.pojavlaunch.prefs.*; import net.kdt.pojavlaunch.prefs.*;
import net.kdt.pojavlaunch.utils.MCOptionUtils; import net.kdt.pojavlaunch.utils.MCOptionUtils;
@ -27,7 +29,27 @@ public class MainActivity extends BaseMainActivity {
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
initLayout(R.layout.main_with_customctrl); initLayout(R.layout.main_with_customctrl);
super.ingameControlsEditorListener = new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.menu_ctrl_load:
CustomControlsActivity.load(mControlLayout);
break;
case R.id.menu_ctrl_add:
mControlLayout.addControlButton(new ControlData("New", LWJGLGLFWKeycode.GLFW_KEY_UNKNOWN, 100, 100));
break;
case R.id.menu_ctrl_selectdefault:
CustomControlsActivity.dialogSelectDefaultCtrl(mControlLayout);
break;
case R.id.menu_ctrl_save:
CustomControlsActivity.save(true,mControlLayout);
break;
}
//Toast.makeText(MainActivity.this, menuItem.getTitle() + ":" + menuItem.getItemId(), Toast.LENGTH_SHORT).show();
return true;
}
};
mClickListener = new View.OnClickListener(){ mClickListener = new View.OnClickListener(){
@Override @Override
@ -172,4 +194,9 @@ public class MainActivity extends BaseMainActivity {
} }
} }
} }
@Override
public void onBackPressed() {
//if(isInEditor) CustomControlsActivity.save(true,mControlLayout);
}
} }

View File

@ -5,6 +5,9 @@ import android.view.*;
import android.widget.*; import android.widget.*;
import com.google.gson.*; import com.google.gson.*;
import java.io.*; import java.io.*;
import java.util.ArrayList;
import java.util.List;
import net.kdt.pojavlaunch.*; import net.kdt.pojavlaunch.*;
import net.kdt.pojavlaunch.prefs.*; import net.kdt.pojavlaunch.prefs.*;
import org.lwjgl.glfw.*; import org.lwjgl.glfw.*;
@ -41,15 +44,15 @@ public class ControlLayout extends FrameLayout
if (mModifiable) { if (mModifiable) {
hideAllHandleViews(); hideAllHandleViews();
} }
if (getChildAt(0) instanceof MinecraftGLView) { //if (getChildAt(0) instanceof MinecraftGLView) {
View viewGL = getChildAt(0); // View viewGL = getChildAt(0);
View viewTouchpad = getChildAt(1); // View viewTouchpad = getChildAt(1);
removeAllViews(); // removeAllViews();
addView(viewGL); // addView(viewGL);
addView(viewTouchpad); // addView(viewTouchpad);
} else { //} else {
removeAllViews(); removeAllButtons();
} //}
if (mLayout != null) { if (mLayout != null) {
mLayout.mControlDataList = null; mLayout.mControlDataList = null;
mLayout = null; mLayout = null;
@ -94,7 +97,21 @@ public class ControlLayout extends FrameLayout
setModified(true); setModified(true);
} }
private void removeAllButtons() {
List<View> viewList = new ArrayList<>();
for(int i = 0; i < getChildCount(); i++) {
viewList.add(getChildAt(i));
}
for(View v : viewList) {
if(v instanceof ControlButton) {
removeView(v);
}
}
viewList = null;
System.gc();
//i wanna be sure that all the removed Views will be removed after a reload
//because if frames will slowly go down after many control changes it will be warm and bad
}
public void removeControlButton(ControlButton controlButton) { public void removeControlButton(ControlButton controlButton) {
mLayout.mControlDataList.remove(controlButton.getProperties()); mLayout.mControlDataList.remove(controlButton.getProperties());
controlButton.setVisibility(View.GONE); controlButton.setVisibility(View.GONE);