[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.widget.*;
import androidx.annotation.NonNull;
import androidx.drawerlayout.widget.*;
import com.google.android.material.navigation.*;
import java.io.*;
@ -89,7 +90,8 @@ public class BaseMainActivity extends LoggableActivity {
private GestureDetector gestureDetector;
private TextView debugText;
private NavigationView.OnNavigationItemSelectedListener gameActionListener;
public NavigationView.OnNavigationItemSelectedListener ingameControlsEditorListener;
// private String mQueueText = new String();
protected JMinecraftVersionList.Version mVersionInfo;
@ -151,30 +153,31 @@ public class BaseMainActivity extends LoggableActivity {
drawerLayout = findViewById(R.id.main_drawer_options);
navDrawer = findViewById(R.id.main_navigation_view);
navDrawer.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.nav_forceclose: dialogForceClose(BaseMainActivity.this);
break;
case R.id.nav_viewlog: openLogOutput();
break;
case R.id.nav_debug: toggleDebug();
break;
case R.id.nav_customkey: dialogSendCustomKey();
break;
case R.id.nav_mousespd: adjustMouseSpeedLive();
break;
case R.id.nav_customctrl: openCustomControls();
break;
}
//Toast.makeText(MainActivity.this, menuItem.getTitle() + ":" + menuItem.getItemId(), Toast.LENGTH_SHORT).show();
drawerLayout.closeDrawers();
return true;
gameActionListener = new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.nav_forceclose: dialogForceClose(BaseMainActivity.this);
break;
case R.id.nav_viewlog: openLogOutput();
break;
case R.id.nav_debug: toggleDebug();
break;
case R.id.nav_customkey: dialogSendCustomKey();
break;
case R.id.nav_mousespd: adjustMouseSpeedLive();
break;
case R.id.nav_customctrl: openCustomControls();
break;
}
});
//Toast.makeText(MainActivity.this, menuItem.getTitle() + ":" + menuItem.getItemId(), Toast.LENGTH_SHORT).show();
drawerLayout.closeDrawers();
return true;
}
};
navDrawer.setNavigationItemSelectedListener(
gameActionListener);
// this.overlayView = (ViewGroup) findViewById(R.id.main_control_overlay);
@ -985,16 +988,25 @@ public class BaseMainActivity extends LoggableActivity {
});
dialog.show();
}
boolean isInEditor;
private void openCustomControls() {
if (this instanceof MainActivity) {
((MainActivity) this).mControlLayout.loadLayout((CustomControls) null);
if(ingameControlsEditorListener != 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() {
contentLog.setVisibility(View.VISIBLE);
mIsResuming = false;
@ -1102,7 +1114,6 @@ public class BaseMainActivity extends LoggableActivity {
public void onBackPressed() {
// Prevent back
// Catch back as Esc keycode at another place
sendKeyPress(LWJGLGLFWKeycode.GLFW_KEY_ESCAPE);
}

View File

@ -30,7 +30,7 @@ public class CustomControlsActivity extends BaseActivity
public boolean isModified = false;
public boolean isFromMainActivity = false;
private String selectedName = "new_control";
private static String selectedName = "new_control";
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -57,16 +57,16 @@ public class CustomControlsActivity extends BaseActivity
public boolean onNavigationItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.menu_ctrl_load:
actionLoad();
load(ctrlLayout);
break;
case R.id.menu_ctrl_add:
ctrlLayout.addControlButton(new ControlData("New", LWJGLGLFWKeycode.GLFW_KEY_UNKNOWN, 100, 100));
break;
case R.id.menu_ctrl_selectdefault:
dialogSelectDefaultCtrl();
dialogSelectDefaultCtrl(ctrlLayout);
break;
case R.id.menu_ctrl_save:
save(false);
save(false,ctrlLayout);
break;
}
//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.setModifiable(true);
loadControl(LauncherPreferences.PREF_DEFAULTCTRL_PATH);
loadControl(LauncherPreferences.PREF_DEFAULTCTRL_PATH,ctrlLayout);
}
@Override
@ -90,22 +90,22 @@ public class CustomControlsActivity extends BaseActivity
return;
}
save(true);
save(true,ctrlLayout);
}
private void setDefaultControlJson(String path) {
private static void setDefaultControlJson(String path,ControlLayout ctrlLayout) {
try {
// Load before save to make sure control is not error
ctrlLayout.loadLayout(Tools.GLOBAL_GSON.fromJson(Tools.read(path), CustomControls.class));
LauncherPreferences.DEFAULT_PREF.edit().putString("defaultCtrl", path).commit();
LauncherPreferences.PREF_DEFAULTCTRL_PATH = path;
} catch (Throwable th) {
Tools.showError(this, th);
Tools.showError(ctrlLayout.getContext(), th);
}
}
private void dialogSelectDefaultCtrl() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
public static void dialogSelectDefaultCtrl(final ControlLayout layout) {
AlertDialog.Builder builder = new AlertDialog.Builder(layout.getContext());
builder.setTitle(R.string.customctrl_selectdefault);
builder.setPositiveButton(android.R.string.cancel, null);
@ -116,7 +116,7 @@ public class CustomControlsActivity extends BaseActivity
@Override
public void onFileSelected(File file, String path) {
setDefaultControlJson(path);
setDefaultControlJson(path,layout);
dialog.dismiss();
}
});
@ -124,19 +124,20 @@ public class CustomControlsActivity extends BaseActivity
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";
ctrlLayout.saveLayout(jsonPath);
layout.saveLayout(jsonPath);
return jsonPath;
}
private void save(final boolean exit) {
final EditText edit = new EditText(this);
public static void save(final boolean exit, final ControlLayout layout) {
final Context ctx = layout.getContext();
final EditText edit = new EditText(ctx);
edit.setSingleLine();
edit.setText(selectedName);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
builder.setTitle(R.string.global_save);
builder.setView(edit);
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(){
@Override
public void onClick(DialogInterface p1, int p2) {
setResult(Activity.RESULT_OK, new Intent());
CustomControlsActivity.super.onBackPressed();
layout.setModifiable(false);
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
public void onClick(View view) {
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 {
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();
if (exit) {
CustomControlsActivity.super.onBackPressed();
//CustomControlsActivity.super.onBackPressed();
}
} 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() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
public static void load(final ControlLayout layout) {
AlertDialog.Builder builder = new AlertDialog.Builder(layout.getContext());
builder.setTitle(R.string.global_load);
builder.setPositiveButton(android.R.string.cancel, null);
@ -197,7 +204,7 @@ public class CustomControlsActivity extends BaseActivity
@Override
public void onFileSelected(File file, String path) {
loadControl(path);
loadControl(path,layout);
dialog.dismiss();
}
});
@ -205,15 +212,14 @@ public class CustomControlsActivity extends BaseActivity
dialog.show();
}
private void loadControl(String path) {
private static void loadControl(String path,ControlLayout layout) {
try {
ctrlLayout.loadLayout(path);
layout.loadLayout(path);
selectedName = new File(path).getName();
// Remove `.json`
selectedName = selectedName.substring(0, selectedName.length() - 5);
} 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 com.google.android.material.navigation.NavigationView;
import net.kdt.pojavlaunch.customcontrols.*;
import net.kdt.pojavlaunch.prefs.*;
import net.kdt.pojavlaunch.utils.MCOptionUtils;
@ -27,7 +29,27 @@ public class MainActivity extends BaseMainActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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(){
@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 com.google.gson.*;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import net.kdt.pojavlaunch.*;
import net.kdt.pojavlaunch.prefs.*;
import org.lwjgl.glfw.*;
@ -41,15 +44,15 @@ public class ControlLayout extends FrameLayout
if (mModifiable) {
hideAllHandleViews();
}
if (getChildAt(0) instanceof MinecraftGLView) {
View viewGL = getChildAt(0);
View viewTouchpad = getChildAt(1);
removeAllViews();
addView(viewGL);
addView(viewTouchpad);
} else {
removeAllViews();
}
//if (getChildAt(0) instanceof MinecraftGLView) {
// View viewGL = getChildAt(0);
// View viewTouchpad = getChildAt(1);
// removeAllViews();
// addView(viewGL);
// addView(viewTouchpad);
//} else {
removeAllButtons();
//}
if (mLayout != null) {
mLayout.mControlDataList = null;
mLayout = null;
@ -94,7 +97,21 @@ public class ControlLayout extends FrameLayout
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) {
mLayout.mControlDataList.remove(controlButton.getProperties());
controlButton.setVisibility(View.GONE);