From fc81b87e7fdcd820f771dd4393188c2626774dda Mon Sep 17 00:00:00 2001 From: Mathias-Boulay Date: Sun, 1 Dec 2024 23:29:16 +0100 Subject: [PATCH] Fix(controls): non square joystick, set size via text --- app_pojavlauncher/build.gradle | 2 +- .../customcontrols/CustomControls.java | 6 +-- .../customcontrols/LayoutConverter.java | 53 ++++++++++++++----- .../handleview/EditControlSideDialog.java | 9 ++++ 4 files changed, 54 insertions(+), 16 deletions(-) diff --git a/app_pojavlauncher/build.gradle b/app_pojavlauncher/build.gradle index 3654bba93c..19bcb89309 100644 --- a/app_pojavlauncher/build.gradle +++ b/app_pojavlauncher/build.gradle @@ -204,7 +204,7 @@ dependencies { implementation 'com.github.PojavLauncherTeam:portrait-ssp:6c02fd739b' implementation 'com.github.Mathias-Boulay:ExtendedView:1.0.0' implementation 'com.github.Mathias-Boulay:android_gamepad_remapper:2.0.3' - implementation 'com.github.Mathias-Boulay:virtual-joystick-android:2e7aa25e50' + implementation 'com.github.Mathias-Boulay:virtual-joystick-android:1.14' // implementation 'com.intuit.sdp:sdp-android:1.0.5' // implementation 'com.intuit.ssp:ssp-android:1.0.5' diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/CustomControls.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/CustomControls.java index 3983b38491..a73f23eede 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/CustomControls.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/CustomControls.java @@ -57,13 +57,13 @@ public CustomControls(Context ctx) { this.mControlDataList.add(new ControlData(ctx, R.string.control_jump, new int[]{LwjglGlfwKeycode.GLFW_KEY_SPACE}, "${right} - ${margin} * 2 - ${width}", "${bottom} - ${margin} * 2 - ${height}", true)); //The default controls are conform to the V3 - version = 7; + version = 8; } public void save(String path) throws IOException { - //Current version is the V3.1 so the version as to be marked as 7 ! - version = 7; + //Current version is the V3.2 so the version as to be marked as 8 ! + version = 8; Tools.write(path, Tools.GLOBAL_GSON.toJson(this)); } diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/LayoutConverter.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/LayoutConverter.java index 25352897bc..81c949fd31 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/LayoutConverter.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/LayoutConverter.java @@ -24,27 +24,56 @@ public static CustomControls loadAndConvertIfNecessary(String jsonPath) throws I CustomControls layout = LayoutConverter.convertV1Layout(layoutJobj); layout.save(jsonPath); return layout; - } else if (layoutJobj.getInt("version") == 2) { - CustomControls layout = LayoutConverter.convertV2Layout(layoutJobj); - layout.save(jsonPath); - return layout; - }else if (layoutJobj.getInt("version") >= 3 && layoutJobj.getInt("version") <= 5) { - return LayoutConverter.convertV3_4Layout(layoutJobj); - } else if (layoutJobj.getInt("version") == 6 || layoutJobj.getInt("version") == 7) { - return Tools.GLOBAL_GSON.fromJson(jsonLayoutData, CustomControls.class); } else { - return null; + int version = layoutJobj.getInt("version"); + if (version == 2) { + CustomControls layout = LayoutConverter.convertV2Layout(layoutJobj); + layout.save(jsonPath); + return layout; + } + if (version == 3 || version == 4 || version == 5) { + return LayoutConverter.convertV3_4Layout(layoutJobj); + } + if (version == 6 || version == 7) { + return convertV6_7Layout(layoutJobj); + } + else if (version == 8) { + return Tools.GLOBAL_GSON.fromJson(jsonLayoutData, CustomControls.class); + } } + return null; } catch (JSONException e) { throw new JsonSyntaxException("Failed to load", e); } } + /** + * Normalize the layout to v8 from v6/7. An issue from the joystick height and position has to be fixed. + * @param oldLayoutJson The old layout + * @return The new layout with the fixed joystick height + */ + public static CustomControls convertV6_7Layout(JSONObject oldLayoutJson) { + CustomControls layout = Tools.GLOBAL_GSON.fromJson(oldLayoutJson.toString(), CustomControls.class); + for (ControlJoystickData data : layout.mJoystickDataList) { + if (data.getHeight() > data.getWidth()) { + // Make the size square, adjust the dynamic position related to height + float ratio = data.getHeight() / data.getWidth(); + + data.dynamicX = data.dynamicX.replace("${height}", "(" + ratio + " * ${height})"); + data.dynamicY = data.dynamicY.replace("${height}", "(" + ratio + " * ${height})") + " + (" + (ratio-1) + " * ${height})"; + + data.setHeight(data.getWidth()); + } + } + layout.version = 8; + return layout; + } + /** * Normalize the layout to v6 from v3/4: The stroke width is no longer dependant on the button size */ - public static CustomControls convertV3_4Layout(JSONObject oldLayoutJson) { + private static CustomControls convertV3_4Layout(JSONObject oldLayoutJson) { CustomControls layout = Tools.GLOBAL_GSON.fromJson(oldLayoutJson.toString(), CustomControls.class); convertStrokeWidth(layout); layout.version = 6; @@ -52,7 +81,7 @@ public static CustomControls convertV3_4Layout(JSONObject oldLayoutJson) { } - public static CustomControls convertV2Layout(JSONObject oldLayoutJson) throws JSONException { + private static CustomControls convertV2Layout(JSONObject oldLayoutJson) throws JSONException { CustomControls layout = Tools.GLOBAL_GSON.fromJson(oldLayoutJson.toString(), CustomControls.class); JSONArray layoutMainArray = oldLayoutJson.getJSONArray("mControlDataList"); layout.mControlDataList = new ArrayList<>(layoutMainArray.length()); @@ -95,7 +124,7 @@ public static CustomControls convertV2Layout(JSONObject oldLayoutJson) throws JS return layout; } - public static CustomControls convertV1Layout(JSONObject oldLayoutJson) throws JSONException { + private static CustomControls convertV1Layout(JSONObject oldLayoutJson) throws JSONException { CustomControls empty = new CustomControls(); JSONArray layoutMainArray = oldLayoutJson.getJSONArray("mControlDataList"); for (int i = 0; i < layoutMainArray.length(); i++) { diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/handleview/EditControlSideDialog.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/handleview/EditControlSideDialog.java index 14bf4dde4f..47b9b68d9f 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/handleview/EditControlSideDialog.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/handleview/EditControlSideDialog.java @@ -332,6 +332,7 @@ private void bindLayout() { /** * A long function linking all the displayed data on the popup and, * the currently edited mCurrentlyEditedButton + * @noinspection SuspiciousNameCombination */ private void setupRealTimeListeners() { mNameEditText.addTextChangedListener((SimpleTextWatcher) s -> { @@ -349,6 +350,10 @@ private void setupRealTimeListeners() { float width = safeParseFloat(s.toString()); if (width >= 0) { mCurrentlyEditedButton.getProperties().setWidth(width); + if (mCurrentlyEditedButton.getProperties() instanceof ControlJoystickData) { + // Joysticks are square + mCurrentlyEditedButton.getProperties().setHeight(width); + } mCurrentlyEditedButton.updateProperties(); } }); @@ -359,6 +364,10 @@ private void setupRealTimeListeners() { float height = safeParseFloat(s.toString()); if (height >= 0) { mCurrentlyEditedButton.getProperties().setHeight(height); + if (mCurrentlyEditedButton.getProperties() instanceof ControlJoystickData) { + // Joysticks are square + mCurrentlyEditedButton.getProperties().setWidth(height); + } mCurrentlyEditedButton.updateProperties(); } });