Add control offsets

This commit is contained in:
SerpentSpirale 2021-11-06 16:13:07 +01:00
parent 0d08bcbd51
commit ed9810aecf
6 changed files with 273 additions and 11 deletions

View File

@ -23,6 +23,10 @@ import org.lwjgl.glfw.*;
import static net.kdt.pojavlaunch.BaseMainActivity.sendMouseButton;
import static net.kdt.pojavlaunch.LWJGLGLFWKeycode.GLFW_KEY_UNKNOWN;
import static net.kdt.pojavlaunch.prefs.LauncherPreferences.PREF_BUTTONSIZE;
import static net.kdt.pojavlaunch.prefs.LauncherPreferences.PREF_CONTROL_BOTTOM_OFFSET;
import static net.kdt.pojavlaunch.prefs.LauncherPreferences.PREF_CONTROL_LEFT_OFFSET;
import static net.kdt.pojavlaunch.prefs.LauncherPreferences.PREF_CONTROL_RIGHT_OFFSET;
import static net.kdt.pojavlaunch.prefs.LauncherPreferences.PREF_CONTROL_TOP_OFFSET;
@SuppressLint("ViewConstructor")
public class ControlButton extends androidx.appcompat.widget.AppCompatButton implements OnLongClickListener
@ -152,16 +156,57 @@ public class ControlButton extends androidx.appcompat.widget.AppCompatButton imp
@Override
public void setX(float x) {
// We have to account for control offset preference
if(x + (mProperties.getWidth()/2f) > CallbackBridge.physicalWidth/2f){
x -= PREF_CONTROL_RIGHT_OFFSET;
}else{
x += PREF_CONTROL_LEFT_OFFSET;
}
super.setX(x);
setModified(true);
}
@Override
public void setY(float y) {
// We have to account for control offset preference
if(y - PREF_CONTROL_TOP_OFFSET + (mProperties.getHeight()/2f) > CallbackBridge.physicalHeight/2f){
y -= PREF_CONTROL_BOTTOM_OFFSET;
}else{
y += PREF_CONTROL_TOP_OFFSET;
}
super.setY(y);
setModified(true);
}
@Override
public float getX() {
float x = super.getX();
// We have to account for control offset preference
if(x + (mProperties.getWidth()/2f) > (CallbackBridge.physicalWidth)/2f){
x += PREF_CONTROL_RIGHT_OFFSET;
}else{
x -= PREF_CONTROL_LEFT_OFFSET;
}
return x;
}
@Override
public float getY(){
// We have to account for control offset preference
float y = super.getY();
if(y + (mProperties.getHeight()/2f) > CallbackBridge.physicalHeight/2f){
y += PREF_CONTROL_BOTTOM_OFFSET;
}else{
y -= PREF_CONTROL_TOP_OFFSET;
}
return y;
}
/**
* Apply the dynamic equation on the x axis.
* @param dynamicX The equation to compute the position from

View File

@ -0,0 +1,122 @@
package net.kdt.pojavlaunch.prefs;
import static net.kdt.pojavlaunch.prefs.LauncherPreferences.DEFAULT_PREF;
import static net.kdt.pojavlaunch.prefs.LauncherPreferences.PREF_CONTROL_BOTTOM_OFFSET;
import static net.kdt.pojavlaunch.prefs.LauncherPreferences.PREF_CONTROL_LEFT_OFFSET;
import static net.kdt.pojavlaunch.prefs.LauncherPreferences.PREF_CONTROL_RIGHT_OFFSET;
import static net.kdt.pojavlaunch.prefs.LauncherPreferences.PREF_CONTROL_TOP_OFFSET;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.AttributeSet;
import android.widget.SeekBar;
import android.widget.TextView;
import androidx.preference.Preference;
import net.kdt.pojavlaunch.R;
/** Custom preference class displaying a dialog */
public class ControlOffsetPreference extends Preference {
private AlertDialog preferenceDialog;
public ControlOffsetPreference(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public ControlOffsetPreference(Context context) {
super(context);
init();
}
private void init(){
// Setup visual values
if(getTitle() == null){
setTitle("Control side offsets");
}
if(getIcon() == null){
setIcon(android.R.drawable.radiobutton_off_background);
}
// Prepare Alert dialog
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getContext());
dialogBuilder.setView(R.layout.control_offset_preference_dialog);
dialogBuilder.setTitle("Control offset");
dialogBuilder.setPositiveButton(android.R.string.ok, null);
dialogBuilder.setNegativeButton(android.R.string.cancel, null);
preferenceDialog = dialogBuilder.create();
}
@Override
protected void onClick() {
preferenceDialog.show();
SeekBar topOffsetSeekbar = preferenceDialog.findViewById(R.id.control_offset_top_seekbar);
SeekBar rightOffsetSeekbar = preferenceDialog.findViewById(R.id.control_offset_right_seekbar);
SeekBar bottomOffsetSeekbar = preferenceDialog.findViewById(R.id.control_offset_bottom_seekbar);
SeekBar leftOffsetSeekbar = preferenceDialog.findViewById(R.id.control_offset_left_seekbar);
TextView topOffsetTextView = preferenceDialog.findViewById(R.id.control_offset_top_textview);
TextView rightOffsetTextView = preferenceDialog.findViewById(R.id.control_offset_right_textview);
TextView bottomOffsetTextView = preferenceDialog.findViewById(R.id.control_offset_bottom_textview);
TextView leftOffsetTextView = preferenceDialog.findViewById(R.id.control_offset_left_textview);
SeekBar.OnSeekBarChangeListener seekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
if(seekBar == topOffsetSeekbar){
topOffsetTextView.setText("Top offset: " + i);
return;
}
if(seekBar == rightOffsetSeekbar){
rightOffsetTextView.setText("Right offset: " + i);
return;
}
if(seekBar == bottomOffsetSeekbar){
bottomOffsetTextView.setText("Bottom offset: " + i);
return;
}
if(seekBar == leftOffsetSeekbar){
leftOffsetTextView.setText("Left offset: " + i);
return;
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
};
topOffsetSeekbar.setOnSeekBarChangeListener(seekBarChangeListener);
rightOffsetSeekbar.setOnSeekBarChangeListener(seekBarChangeListener);
bottomOffsetSeekbar.setOnSeekBarChangeListener(seekBarChangeListener);
leftOffsetSeekbar.setOnSeekBarChangeListener(seekBarChangeListener);
topOffsetSeekbar.setProgress(PREF_CONTROL_TOP_OFFSET);
rightOffsetSeekbar.setProgress(PREF_CONTROL_RIGHT_OFFSET);
bottomOffsetSeekbar.setProgress(PREF_CONTROL_BOTTOM_OFFSET);
leftOffsetSeekbar.setProgress(PREF_CONTROL_LEFT_OFFSET);
// Custom writing to preferences
preferenceDialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(view -> {
DEFAULT_PREF.edit().putInt("controlTopOffset", topOffsetSeekbar.getProgress()).apply();
DEFAULT_PREF.edit().putInt("controlRightOffset", rightOffsetSeekbar.getProgress()).apply();
DEFAULT_PREF.edit().putInt("controlBottomOffset", bottomOffsetSeekbar.getProgress()).apply();
DEFAULT_PREF.edit().putInt("controlLeftOffset", leftOffsetSeekbar.getProgress()).apply();
preferenceDialog.dismiss();
});
}
}

View File

@ -29,6 +29,12 @@ public class LauncherPreferences
public static float PREF_MOUSESPEED = 1f;
public static int PREF_RAM_ALLOCATION;
public static String PREF_DEFAULT_RUNTIME;
public static int PREF_CONTROL_TOP_OFFSET = 0;
public static int PREF_CONTROL_RIGHT_OFFSET = 0;
public static int PREF_CONTROL_BOTTOM_OFFSET = 0;
public static int PREF_CONTROL_LEFT_OFFSET = 0;
public static void loadPreferences(Context ctx) {
//Required for the data folder.
Tools.initContextConstants(ctx);
@ -51,6 +57,11 @@ public class LauncherPreferences
PREF_DISABLE_GESTURES = DEFAULT_PREF.getBoolean("disableGestures",false);
PREF_RAM_ALLOCATION = DEFAULT_PREF.getInt("allocation", findBestRAMAllocation(ctx));
PREF_CUSTOM_JAVA_ARGS = DEFAULT_PREF.getString("javaArgs", "");
PREF_CONTROL_TOP_OFFSET = DEFAULT_PREF.getInt("controlTopOffset", 0);
PREF_CONTROL_RIGHT_OFFSET = DEFAULT_PREF.getInt("controlRightOffset", 0);
PREF_CONTROL_BOTTOM_OFFSET = DEFAULT_PREF.getInt("controlBottomOffset", 0);
PREF_CONTROL_LEFT_OFFSET = DEFAULT_PREF.getInt("controlTopOffset", 0);
/*
if (PREF_CUSTOM_JAVA_ARGS.isEmpty()) {
String DEFAULT_JAVA_ARGS = "";

View File

@ -1,5 +1,6 @@
package net.kdt.pojavlaunch.prefs.screens;
import android.content.SharedPreferences;
import android.os.Bundle;
import net.kdt.pojavlaunch.R;
@ -39,5 +40,19 @@ public class LauncherPreferenceControlFragment extends LauncherPreferenceFragmen
seek6.setValue((int)(mouseSpeed *100f));
seek6.setSuffix(" %");
computeVisibility();
}
@Override
public void onSharedPreferenceChanged(SharedPreferences p, String s) {
super.onSharedPreferenceChanged(p, s);
computeVisibility();
}
private void computeVisibility(){
CustomSeekBarPreference seek2 = findPreference("timeLongPressTrigger");
seek2.setVisible(!LauncherPreferences.PREF_DISABLE_GESTURES);
}
}

View File

@ -0,0 +1,64 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginHorizontal="15dp">
<TextView
android:id="@+id/control_offset_top_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Top offset:"
/>
<SeekBar
android:id="@+id/control_offset_top_seekbar"
android:layout_width="match_parent"
android:layout_height="40dp" />
<TextView
android:id="@+id/control_offset_right_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Right offset:"
/>
<SeekBar
android:id="@+id/control_offset_right_seekbar"
android:layout_width="match_parent"
android:layout_height="40dp" />
<TextView
android:id="@+id/control_offset_bottom_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Bottom offset:"
/>
<SeekBar
android:id="@+id/control_offset_bottom_seekbar"
android:layout_width="match_parent"
android:layout_height="40dp" />
<TextView
android:id="@+id/control_offset_left_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Left offset:"
/>
<SeekBar
android:id="@+id/control_offset_left_seekbar"
android:layout_width="match_parent"
android:layout_height="40dp" />
</LinearLayout>
</ScrollView>

View File

@ -20,10 +20,26 @@
android:title="@string/mcl_setting_title_longpresstrigger"
app2:showSeekBarValue="true"
app2:selectable="false"
app2:seekBarIncrement="10"
app2:icon="@drawable/tap_len" />
</PreferenceCategory>
<PreferenceCategory
android:title="Buttons">
<net.kdt.pojavlaunch.prefs.CustomSeekBarPreference
android:key="buttonscale"
android:title="@string/mcl_setting_title_buttonscale"
android:summary="@string/mcl_setting_subtitle_buttonscale"
app2:showSeekBarValue="true"
app2:selectable="false"
app2:icon="@drawable/btn_scale"/>
<net.kdt.pojavlaunch.prefs.ControlOffsetPreference/>
</PreferenceCategory>
<PreferenceCategory
android:title="Virtual mouse">
@ -43,18 +59,7 @@
app2:showSeekBarValue="true" />
</PreferenceCategory>
<PreferenceCategory
android:title="Buttons">
<net.kdt.pojavlaunch.prefs.CustomSeekBarPreference
android:key="buttonscale"
android:title="@string/mcl_setting_title_buttonscale"
android:summary="@string/mcl_setting_subtitle_buttonscale"
app2:showSeekBarValue="true"
app2:selectable="false"
app2:icon="@drawable/btn_scale"/>
</PreferenceCategory>
</PreferenceScreen>