Skip to content

Commit

Permalink
修复游戏内修改分辨率缩放,鼠标指针位置的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
MovTery committed Oct 9, 2024
1 parent a515599 commit 4b7201d
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,23 @@
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.util.Log;

import net.kdt.pojavlaunch.MinecraftGLSurface;
import net.kdt.pojavlaunch.prefs.LauncherPreferences;
import net.kdt.pojavlaunch.R;
import net.kdt.pojavlaunch.Tools;

public class ResolutionAdjuster {

private float mScaleFactor;
private float mScaleFactor = LauncherPreferences.PREF_SCALE_FACTOR / 100f;
private final Context context;
private MinecraftGLSurface glSurface;
private final OnResolutionChangeListener listener;

// 构造函数,传入Context
public ResolutionAdjuster(Context context) {
public ResolutionAdjuster(Context context, OnResolutionChangeListener listener) {
this.context = context;
}
public ResolutionAdjuster(Context context, MinecraftGLSurface glSurface) {
this.context = context;
this.glSurface = glSurface;
this.listener = listener;
}

// 显示滑动条弹窗
public void showSeekBarDialog() {
if (glSurface == null) {
glSurface = new MinecraftGLSurface(context);
}
mScaleFactor = glSurface.mScaleFactor;
int percentage = Math.round(mScaleFactor * 100);
// 动态创建一个LinearLayout作为容器
// 什么?为什么不用.xml来构建?
Expand Down Expand Up @@ -66,13 +55,10 @@ public void showSeekBarDialog() {
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// 更新缩放因子
mScaleFactor = (progress + 25) / 100f;
glSurface.mScaleFactor = mScaleFactor;
listener.onChange(mScaleFactor);
int scaleFactor = Math.round(mScaleFactor * 100);
// 实时更新显示的缩放因子
scaleTextView.setText(scaleFactor + "%");

// 新分辨率
if (glSurface != null) glSurface.refreshSize();
}

@Override
Expand All @@ -82,7 +68,7 @@ public void onStartTrackingTouch(SeekBar seekBar) {

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// Nothing to do here
LauncherPreferences.PREF_SCALE_FACTOR = (int) (mScaleFactor * 100);
}
});

Expand All @@ -96,4 +82,7 @@ public void onStopTrackingTouch(SeekBar seekBar) {
builder.show();
}

public interface OnResolutionChangeListener {
void onChange(float value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import net.kdt.pojavlaunch.customcontrols.keyboard.LwjglCharSender;
import net.kdt.pojavlaunch.customcontrols.keyboard.TouchCharInput;
import net.kdt.pojavlaunch.customcontrols.mouse.GyroControl;
import net.kdt.pojavlaunch.customcontrols.mouse.HotbarView;
import net.kdt.pojavlaunch.customcontrols.mouse.Touchpad;
import net.kdt.pojavlaunch.lifecycle.ContextExecutor;
import net.kdt.pojavlaunch.prefs.LauncherPreferences;
Expand Down Expand Up @@ -87,6 +88,7 @@ public class MainActivity extends BaseActivity implements ControlButtonMenuListe
private DrawerLayout drawerLayout;
private ListView navDrawer;
private View mDrawerPullButton;
private HotbarView mHotbarView;
private GyroControl mGyroControl = null;
public static ControlLayout mControlLayout;

Expand Down Expand Up @@ -222,7 +224,10 @@ protected void initLayout(int resId) {
openCustomControls();
break;
case 6:{
ResolutionAdjuster adjuster = new ResolutionAdjuster(this, minecraftGLView);
ResolutionAdjuster adjuster = new ResolutionAdjuster(this, value -> {
minecraftGLView.refreshSize(value);
mHotbarView.refreshScaleFactor(value);
});
adjuster.showSeekBarDialog();
} break;
}
Expand Down Expand Up @@ -294,6 +299,7 @@ private void bindValues() {
mControlLayout = findViewById(R.id.main_control_layout);
touchCharInput = findViewById(R.id.mainTouchCharInput);
mDrawerPullButton = findViewById(R.id.drawer_button);
mHotbarView = findViewById(R.id.hotbar_view);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public class MinecraftGLSurface extends View implements GrabListener {
.remapDpad(true));

/* Resolution scaler option, allow downsizing a window */
public float mScaleFactor = LauncherPreferences.PREF_SCALE_FACTOR / 100f;
private float mScaleFactor = LauncherPreferences.PREF_SCALE_FACTOR / 100f;
/* Sensitivity, adjusted according to screen size */
private final double mSensitivityFactor = (1.4 * (1080f / Tools.getDisplayMetrics((Activity) getContext()).heightPixels));

Expand All @@ -92,6 +92,15 @@ public MinecraftGLSurface(Context context, AttributeSet attributeSet) {
setFocusable(true);
}

public void refreshSize(float value) {
this.mScaleFactor = value;
mInGUIProcessor.refreshScaleFactor(mScaleFactor);
if (mPointerCapture != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mPointerCapture.refreshScaleFactor(mScaleFactor);
}
if (mGamepad != null) mGamepad.refreshScaleFactor(mScaleFactor);
refreshSize();
}

@RequiresApi(api = Build.VERSION_CODES.O)
private void setUpPointerCapture(AbstractTouchpad touchpad) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
public class Gamepad implements GrabListener, GamepadHandler {

/* Resolution scaler option, allow downsizing a window */
private final float mScaleFactor = LauncherPreferences.DEFAULT_PREF.getInt("resolutionRatio",100)/100f;
private float mScaleFactor = LauncherPreferences.DEFAULT_PREF.getInt("resolutionRatio",100)/100f;

/* Sensitivity, adjusted according to screen size */
private final double mSensitivityFactor = (1.4 * (1080f/ currentDisplayMetrics.heightPixels));
Expand Down Expand Up @@ -134,6 +134,9 @@ public void doFrame(long frameTimeNanos) {
mMapProvider.attachGrabListener(this);
}

public void refreshScaleFactor(float scaleFactor) {
this.mScaleFactor = scaleFactor;
}

public void reloadGamepadMaps() {
if(mGameMap != null) mGameMap.resetPressedState();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,6 @@ default void applyMotionVector(float[] vector) {
* Sets the state of the touchpad to "disabled".
*/
void disable();

void refreshScaleFactor(float scaleFactor);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ public class AndroidPointerCapture implements ViewTreeObserver.OnWindowFocusChan
private static final float TOUCHPAD_SCROLL_THRESHOLD = 1;
private final AbstractTouchpad mTouchpad;
private final View mHostView;
private final float mScaleFactor;
private final float mMousePrescale = Tools.dpToPx(1);
private final PointerTracker mPointerTracker = new PointerTracker();
private final Scroller mScroller = new Scroller(TOUCHPAD_SCROLL_THRESHOLD);
private final float[] mVector = mPointerTracker.getMotionVector();

private float mScaleFactor;
private int mInputDeviceIdentifier;
private boolean mDeviceSupportsRelativeAxis;

Expand All @@ -36,6 +36,11 @@ public AndroidPointerCapture(AbstractTouchpad touchpad, View hostView, float sca
hostView.getViewTreeObserver().addOnWindowFocusChangeListener(this);
}

public void refreshScaleFactor(float scaleFactor) {
this.mScaleFactor = scaleFactor;
this.mTouchpad.refreshScaleFactor(scaleFactor);
}

private void enableTouchpadIfNecessary() {
if (!mTouchpad.getDisplayState()) mTouchpad.enable(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class HotbarView extends View implements MCOptionUtils.MCOptionListener,
LwjglGlfwKeycode.GLFW_KEY_4, LwjglGlfwKeycode.GLFW_KEY_5, LwjglGlfwKeycode.GLFW_KEY_6,
LwjglGlfwKeycode.GLFW_KEY_7, LwjglGlfwKeycode.GLFW_KEY_8, LwjglGlfwKeycode.GLFW_KEY_9};
private final DropGesture mDropGesture = new DropGesture(new Handler(Looper.getMainLooper()));
private final float mScaleFactor = LauncherPreferences.PREF_SCALE_FACTOR / 100f;
private float mScaleFactor = LauncherPreferences.PREF_SCALE_FACTOR / 100f;
private int mWidth;
private int mLastIndex;
private int mGuiScale;
Expand All @@ -52,6 +52,10 @@ public HotbarView(Context context, @Nullable AttributeSet attrs, int defStyleAtt
initialize();
}

public void refreshScaleFactor(float scaleFactor) {
this.mScaleFactor = scaleFactor;
}

private void initialize() {
MCOptionUtils.addMCOptionListener(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@ public class InGUIEventProcessor implements TouchEventProcessor {
private AbstractTouchpad mTouchpad;
private boolean mIsMouseDown = false;
private float mStartX, mStartY;
private final float mScaleFactor;
private final Scroller mScroller = new Scroller(FINGER_SCROLL_THRESHOLD);
private float mScaleFactor;

public InGUIEventProcessor(float scaleFactor) {
mSingleTapDetector = new TapDetector(1, TapDetector.DETECTION_METHOD_BOTH);
mScaleFactor = scaleFactor;
}

public void refreshScaleFactor(float scaleFactor) {
this.mScaleFactor = scaleFactor;
}

@Override
public boolean processTouchEvent(MotionEvent motionEvent) {
boolean singleTap = mSingleTapDetector.onTouchEvent(motionEvent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class Touchpad extends View implements GrabListener, AbstractTouchpad {
private Drawable mMousePointerDrawable;
private float mMouseX, mMouseY;
/* Resolution scaler option, allow downsizing a window */
private final float mScaleFactor = DEFAULT_PREF.getInt("resolutionRatio", 100) / 100f;
private float mScaleFactor = DEFAULT_PREF.getInt("resolutionRatio", 100) / 100f;

public Touchpad(@NonNull Context context) {
this(context, null);
Expand All @@ -49,6 +49,11 @@ public Touchpad(@NonNull Context context, @Nullable AttributeSet attrs) {
init();
}

@Override
public void refreshScaleFactor(float scaleFactor) {
this.mScaleFactor = scaleFactor;
}

/**
* Enable the touchpad
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
android:layout_gravity="center_horizontal"/>

<net.kdt.pojavlaunch.customcontrols.mouse.HotbarView
android:id="@+id/hotbar_view"
android:layout_width="0px"
android:layout_height="0px"/>

Expand Down

0 comments on commit 4b7201d

Please sign in to comment.