- Major flattening of the code to avoid it being unreadeable.

- New edit button interface with new properties.
This commit is contained in:
SerpentSpirale 2021-04-21 09:22:32 +02:00
parent 9748df5d49
commit fdddabd1c7

View File

@ -19,7 +19,10 @@
*/
package net.kdt.pojavlaunch.customcontrols.handleview;
import android.app.Dialog;
import android.content.*;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.*;
import android.view.ViewGroup.*;
import android.widget.*;
@ -30,10 +33,49 @@ import android.view.View.OnClickListener;
import net.kdt.pojavlaunch.customcontrols.*;
import androidx.appcompat.app.*;
import com.rarepebble.colorpicker.ColorPickerView;
public class ActionPopupWindow extends PinnedPopupWindow implements OnClickListener {
private TextView mEditTextView;
private TextView mDeleteTextView;
private AlertDialog.Builder alertBuilder;
private Dialog dialog;
private EditText editName;
private Spinner[] spinnersKeycode;
private CheckBox checkToggle;
private CheckBox checkPassThrough;
private CheckBox checkDynamicPosition;
private CheckBox checkHoldAlt;
private CheckBox checkHoldCtrl;
private CheckBox checkHoldShift;
private EditText editWidth;
private EditText editHeight;
private EditText editDynamicX;
private EditText editDynamicY;
private SeekBar seekBarOpacity;
private SeekBar seekBarCornerRadius;
private SeekBar seekBarStrokeWidth;
private ImageButton buttonBackgroundColor;
private ImageButton buttonStrokeColor;
private TextView textOpacity;
private TextView textCornerRadius;
private TextView textStrokeWidth;
private ControlData properties;
private ArrayAdapter<String> adapter;
private String[] specialArr;
public ActionPopupWindow(HandleView handleView) {
super(handleView);
}
@ -77,178 +119,30 @@ public class ActionPopupWindow extends PinnedPopupWindow implements OnClickListe
@Override
public void onClick(final View view) {
AlertDialog.Builder alert = new AlertDialog.Builder(view.getContext());
alert.setCancelable(false);
alertBuilder = new AlertDialog.Builder(view.getContext());
alertBuilder.setCancelable(false);
if (view == mEditTextView) {
alert.setTitle(view.getResources().getString(R.string.customctrl_edit, mHandleView.mView.getText()));
alert.setView(R.layout.control_setting);
alert.setPositiveButton(android.R.string.ok, null);
alert.setNegativeButton(android.R.string.cancel, null);
final AlertDialog dialog = alert.create();
final ControlData properties = mHandleView.mView.getProperties();
properties = mHandleView.mView.getProperties();
initializeEditDialog(view.getContext());
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialogInterface) {
final EditText editName = dialog.findViewById(R.id.controlsetting_edit_name);
editName.setText(properties.name);
final Spinner spinnerKeycode = dialog.findViewById(R.id.controlsetting_spinner_lwjglkeycode);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(view.getContext(), android.R.layout.simple_spinner_item);
String[] oldSpecialArr = ControlData.buildSpecialButtonArray();
final String[] specialArr = new String[oldSpecialArr.length];
for (int i = 0; i < specialArr.length; i++) {
specialArr[i] = "SPECIAL_" + oldSpecialArr[specialArr.length - i - 1];
}
adapter.addAll(specialArr);
adapter.addAll(AndroidLWJGLKeycode.generateKeyName());
adapter.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
spinnerKeycode.setAdapter(adapter);
if (properties.keycode < 0) {
spinnerKeycode.setSelection(properties.keycode + specialArr.length);
} else {
spinnerKeycode.setSelection(AndroidLWJGLKeycode.getIndexByLWJGLKey(properties.keycode) + specialArr.length);
}
final CheckBox checkToggle = dialog.findViewById(R.id.controlsetting_checkbox_toggle);
checkToggle.setChecked(properties.isToggle);
final CheckBox checkPassthru = dialog.findViewById(R.id.controlsetting_checkbox_passthru);
checkPassthru.setChecked(properties.passThruEnabled);
final CheckBox checkRound = dialog.findViewById(R.id.controlsetting_checkbox_round);
checkRound.setChecked(properties.isRound);
final EditText editWidth = dialog.findViewById(R.id.controlsetting_edit_width);
final EditText editHeight = dialog.findViewById(R.id.controlsetting_edit_height);
editWidth.setText(Float.toString(properties.width));
editHeight.setText(Float.toString(properties.height));
final EditText editDynamicX = dialog.findViewById(R.id.controlsetting_edit_dynamicpos_x);
final EditText editDynamicY = dialog.findViewById(R.id.controlsetting_edit_dynamicpos_y);
editDynamicX.setEnabled(properties.isDynamicBtn);
editDynamicY.setEnabled(properties.isDynamicBtn);
final SeekBar seekTransparency = dialog.findViewById(R.id.controlsetting_seek_transparency);
seekTransparency.setMax(100);
seekTransparency.setProgress(properties.hidden ? 100 : properties.transparency);
final CheckBox checkDynamicPos = dialog.findViewById(R.id.controlsetting_checkbox_dynamicpos);
checkDynamicPos.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton btn, boolean checked) {
editDynamicX.setEnabled(checked);
editDynamicY.setEnabled(checked);
}
});
checkDynamicPos.setChecked(properties.isDynamicBtn);
editDynamicX.setHint(Float.toString(properties.x));
editDynamicX.setText(properties.dynamicX);
editDynamicY.setHint(Float.toString(properties.y));
editDynamicY.setText(properties.dynamicY);
final CheckBox checkHoldAlt = dialog.findViewById(R.id.controlsetting_checkbox_keycombine_alt);
checkHoldAlt.setChecked(properties.holdAlt);
final CheckBox checkHoldControl = dialog.findViewById(R.id.controlsetting_checkbox_keycombine_control);
checkHoldControl.setChecked(properties.holdCtrl);
final CheckBox checkHoldShift = dialog.findViewById(R.id.controlsetting_checkbox_keycombine_shift);
checkHoldShift.setChecked(properties.holdShift);
Button button = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view2) {
if (editName.getText().toString().isEmpty()) {
editName.setError(view.getResources().getString(R.string.global_error_field_empty));
} else {
/*
String errorAt = null;
try {
errorAt = "DynamicX";
properties.insertDynamicPos(editDynamicX.getText().toString());
errorAt = "DynamicY";
properties.insertDynamicPos(editDynamicY.getText().toString());
} catch (Throwable th) {
Error e = new Error(errorAt, th);
e.setStackTrace(null);
Tools.showError(view.getContext(), e);
return;
}
errorAt = null;
*/
if (properties.isDynamicBtn) {
int errorAt = 0;
try {
properties.insertDynamicPos(editDynamicX.getText().toString());
errorAt = 1;
properties.insertDynamicPos(editDynamicY.getText().toString());
} catch (Throwable th) {
(errorAt == 0 ? editDynamicX : editDynamicY)
.setError(th.getMessage());
return;
}
}
if (spinnerKeycode.getSelectedItemPosition() < specialArr.length) {
properties.keycode = spinnerKeycode.getSelectedItemPosition() - specialArr.length;
} else {
properties.keycode = AndroidLWJGLKeycode.getKeyByIndex(spinnerKeycode.getSelectedItemPosition() - specialArr.length);
}
properties.name = editName.getText().toString();
properties.transparency = seekTransparency.getProgress();
properties.hidden = false;
properties.isToggle = checkToggle.isChecked();
properties.passThruEnabled = checkPassthru.isChecked();
properties.isRound = checkRound.isChecked();
properties.isDynamicBtn = checkDynamicPos.isChecked();
properties.width = Float.parseFloat(editWidth.getText().toString());
properties.height = Float.parseFloat(editHeight.getText().toString());
properties.dynamicX = editDynamicX.getText().toString();
properties.dynamicY = editDynamicY.getText().toString();
properties.holdAlt = checkHoldAlt.isChecked();
properties.holdCtrl = checkHoldControl.isChecked();
properties.holdShift = checkHoldShift.isChecked();
if (properties.dynamicX.isEmpty()) {
properties.dynamicX = Float.toString(properties.x);
} if (properties.dynamicY.isEmpty()) {
properties.dynamicY = Float.toString(properties.y);
}
mHandleView.mView.updateProperties();
dialog.dismiss();
}
}
});
}
});
dialog.show();
} else if (view == mDeleteTextView) {
alert.setMessage(view.getContext().getString(R.string.customctrl_remove, mHandleView.mView.getText()) + "?");
alert.setPositiveButton(R.string.global_remove, new DialogInterface.OnClickListener(){
alertBuilder.setMessage(view.getContext().getString(R.string.customctrl_remove, mHandleView.mView.getText()) + "?");
@Override
public void onClick(DialogInterface p1, int p2)
{
alertBuilder.setPositiveButton(R.string.global_remove, (p1, p2) -> {
ControlLayout layout = ((ControlLayout) mHandleView.mView.getParent());
layout.removeControlButton(mHandleView.mView);
}
});
alert.setNegativeButton(android.R.string.cancel, null);
alert.show();
alertBuilder.setNegativeButton(android.R.string.cancel, null);
alertBuilder.show();
}
hide();
@ -257,32 +151,229 @@ public class ActionPopupWindow extends PinnedPopupWindow implements OnClickListe
@Override
protected int getTextOffset() {
return 0;
// return (mTextView.getSelectionStart() + mTextView.getSelectionEnd()) / 2;
}
@Override
protected int getVerticalLocalPosition(int line) {
// return mTextView.getLayout().getLineTop(line) - mContentView.getMeasuredHeight();
return 0;
}
@Override
protected int clipVertically(int positionY) {
/*
if (positionY < 0) {
final int offset = getTextOffset();
final Layout layout = mTextView.getLayout();
final int line = layout.getLineForOffset(offset);
positionY += layout.getLineBottom(line) - layout.getLineTop(line);
positionY += mContentView.getMeasuredHeight();
// Assumes insertion and selection handles share the same height
final Drawable handle = mHandleView.getContext().getDrawable(
mTextView.mTextSelectHandleRes);
positionY += handle.getIntrinsicHeight();
}
*/
return positionY;
}
private void initializeEditDialog(Context ctx){
//TODO: Support the color picker, stroke width/color and corner radius
//Create the editing dialog
LayoutInflater layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = layoutInflater.inflate(R.layout.control_setting_v2,null);
alertBuilder.setTitle(ctx.getResources().getString(R.string.customctrl_edit, mHandleView.mView.getText()));
alertBuilder.setView(v);
//Linking a lot of stuff
editName = v.findViewById(R.id.controlsetting_edit_name);
spinnersKeycode = new Spinner[]{
v.findViewById(R.id.controlsetting_spinner_lwjglkeycode),
v.findViewById(R.id.controlsetting_spinner_lwjglkeycode2),
v.findViewById(R.id.controlsetting_spinner_lwjglkeycode3),
v.findViewById(R.id.controlsetting_spinner_lwjglkeycode4)
};
checkToggle = v.findViewById(R.id.controlsetting_checkbox_toggle);
checkPassThrough = v.findViewById(R.id.controlsetting_checkbox_passthru);
editWidth = v.findViewById(R.id.controlsetting_edit_width);
editHeight = v.findViewById(R.id.controlsetting_edit_height);
editDynamicX = v.findViewById(R.id.controlsetting_edit_dynamicpos_x);
editDynamicY = v.findViewById(R.id.controlsetting_edit_dynamicpos_y);
seekBarOpacity = v.findViewById(R.id.controlsetting_seek_opacity);
seekBarCornerRadius = v.findViewById(R.id.controlsetting_seek_corner_radius);
seekBarStrokeWidth = v.findViewById(R.id.controlsetting_seek_stroke_width);
buttonBackgroundColor = v.findViewById(R.id.controlsetting_background_color);
buttonStrokeColor = v.findViewById(R.id.controlsetting_stroke_color);
textOpacity = v.findViewById(R.id.controlsetting_text_opacity);
textCornerRadius = v.findViewById(R.id.controlsetting_text_corner_radius);
textStrokeWidth = v.findViewById(R.id.controlsetting_text_stroke_width);
checkDynamicPosition = v.findViewById(R.id.controlsetting_checkbox_dynamicpos);
checkDynamicPosition.setOnCheckedChangeListener((btn, checked) -> {
editDynamicX.setEnabled(checked);
editDynamicY.setEnabled(checked);
});
checkHoldAlt = v.findViewById(R.id.controlsetting_checkbox_keycombine_alt);
checkHoldCtrl = v.findViewById(R.id.controlsetting_checkbox_keycombine_control);
checkHoldShift = v.findViewById(R.id.controlsetting_checkbox_keycombine_shift);
//Initialize adapter for keycodes
adapter= new ArrayAdapter<String>(ctx, android.R.layout.simple_spinner_item);
String[] oldSpecialArr = ControlData.buildSpecialButtonArray();
specialArr = new String[oldSpecialArr.length];
for (int i = 0; i < specialArr.length; i++) {
specialArr[i] = "SPECIAL_" + oldSpecialArr[specialArr.length - i - 1];
}
adapter.addAll(specialArr);
adapter.addAll(AndroidLWJGLKeycode.generateKeyName());
adapter.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
for (Spinner spinner : spinnersKeycode) {
spinner.setAdapter(adapter);
}
//Set color imageButton behavior
buttonBackgroundColor.setOnClickListener(view -> showBackgroundColorPicker(view.getContext()));
buttonStrokeColor.setOnClickListener(view -> showStrokeColorPicker(view.getContext()));
//Set dialog buttons behavior
alertBuilder.setPositiveButton(android.R.string.ok, (dialogInterface1, i) -> {
if(!hasErrors(dialog.getContext())){
saveProperties();
}
});
alertBuilder.setNegativeButton(android.R.string.cancel, null);
//Create the finalized dialog
dialog = alertBuilder.create();
dialog.setOnShowListener(dialogInterface -> {
setEditDialogValues();
});
}
private void setEditDialogValues(){
editName.setText(properties.name);
checkToggle.setChecked(properties.isToggle);
checkPassThrough.setChecked(properties.passThruEnabled);
editWidth.setText(Float.toString(properties.width));
editHeight.setText(Float.toString(properties.height));
editDynamicX.setEnabled(properties.isDynamicBtn);
editDynamicY.setEnabled(properties.isDynamicBtn);
editDynamicX.setHint(Float.toString(properties.x));
editDynamicX.setText(properties.dynamicX);
editDynamicY.setHint(Float.toString(properties.y));
editDynamicY.setText(properties.dynamicY);
seekBarOpacity.setProgress(properties.hidden ? 0 : (int)properties.opacity*100);
seekBarStrokeWidth.setProgress(properties.strokeWidth);
seekBarCornerRadius.setProgress((int)properties.cornerRadius);
buttonBackgroundColor.setBackgroundColor(properties.bgColor);
buttonStrokeColor.setBackgroundColor(properties.strokeColor);
checkHoldAlt.setChecked(properties.holdAlt);
checkHoldCtrl.setChecked(properties.holdCtrl);
checkHoldShift.setChecked(properties.holdShift);
checkDynamicPosition.setChecked(properties.isDynamicBtn);
for(int i=0; i< properties.keycodes.length; i++){
if (properties.keycodes[i] < 0) {
spinnersKeycode[i].setSelection(properties.keycodes[i] + specialArr.length);
} else {
spinnersKeycode[i].setSelection(AndroidLWJGLKeycode.getIndexByLWJGLKey(properties.keycodes[i]) + specialArr.length);
}
}
}
private void saveProperties(){
//TODO save new properties
//This method assumes there are no error.
properties.name = editName.getText().toString();
//Keycodes
for(int i=0; i<spinnersKeycode.length; ++i){
if (spinnersKeycode[i].getSelectedItemPosition() < specialArr.length) {
properties.keycodes[i] = spinnersKeycode[i].getSelectedItemPosition() - specialArr.length;
} else {
properties.keycodes[i] = AndroidLWJGLKeycode.getKeyByIndex(spinnersKeycode[i].getSelectedItemPosition() - specialArr.length);
}
}
properties.opacity = seekBarOpacity.getProgress()/100f;
properties.strokeWidth = seekBarStrokeWidth.getProgress();
properties.cornerRadius = seekBarCornerRadius.getProgress();
properties.bgColor = ((ColorDrawable) buttonBackgroundColor.getBackground()).getColor();
properties.strokeColor = ((ColorDrawable) buttonStrokeColor.getBackground()).getColor();
properties.hidden = false;
properties.isToggle = checkToggle.isChecked();
properties.passThruEnabled = checkPassThrough.isChecked();
properties.width = Float.parseFloat(editWidth.getText().toString());
properties.height = Float.parseFloat(editHeight.getText().toString());
properties.isDynamicBtn = checkDynamicPosition.isChecked();
properties.dynamicX = editDynamicX.getText().toString().isEmpty() ? properties.dynamicX = Float.toString(properties.x) : editDynamicX.getText().toString();
properties.dynamicY = editDynamicY.getText().toString().isEmpty() ? properties.dynamicY = Float.toString(properties.y) : editDynamicY.getText().toString();
properties.holdAlt = checkHoldAlt.isChecked();
properties.holdCtrl = checkHoldCtrl.isChecked();
properties.holdShift = checkHoldShift.isChecked();
mHandleView.mView.updateProperties();
}
private boolean hasErrors(Context ctx){
if (editName.getText().toString().isEmpty()) {
editName.setError(ctx.getResources().getString(R.string.global_error_field_empty));
return true;
}
if (properties.isDynamicBtn) {
int errorAt = 0;
try {
properties.insertDynamicPos(editDynamicX.getText().toString());
errorAt = 1;
properties.insertDynamicPos(editDynamicY.getText().toString());
} catch (Throwable th) {
(errorAt == 0 ? editDynamicX : editDynamicY).setError(th.getMessage());
return true;
}
}
return false;
}
private void showBackgroundColorPicker(Context ctx){
ColorPickerView picker = new ColorPickerView(ctx);
picker.setColor(((ColorDrawable)buttonBackgroundColor.getBackground()).getColor());
AlertDialog.Builder dialog = new AlertDialog.Builder(ctx);
dialog.setTitle("Edit background color");
dialog.setView(picker);
dialog.setNegativeButton(android.R.string.cancel, null);
dialog.setPositiveButton(android.R.string.ok, (dialogInterface, i) -> buttonBackgroundColor.setBackgroundColor(picker.getColor()));
dialog.show();
}
private void showStrokeColorPicker(Context ctx){
ColorPickerView picker = new ColorPickerView(ctx);
picker.setColor(((ColorDrawable)buttonStrokeColor.getBackground()).getColor());
picker.showAlpha(false);
AlertDialog.Builder dialog = new AlertDialog.Builder(ctx);
dialog.setTitle("Edit stroke color");
dialog.setView(picker);
dialog.setNegativeButton(android.R.string.cancel, null);
dialog.setPositiveButton(android.R.string.ok, (dialogInterface, i) -> buttonStrokeColor.setBackgroundColor(picker.getColor()));
dialog.show();
}
}