Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions launcher/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ dependencies {
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.8.7")
implementation("org.apache.commons:commons-compress:1.27.1")
implementation("org.tukaani:xz:1.9")
implementation("com.google.code.gson:gson:2.10.1")
}

val stageBootstrapJar by tasks.registering(Copy::class) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ package com.skarm.launcher.touch

import android.content.Context
import android.content.SharedPreferences
import androidx.annotation.Keep
import com.google.gson.Gson
import com.skarm.launcher.GameActivity
import org.json.JSONArray
import org.json.JSONObject

enum class ControlType {
BUTTON,
JOYSTICK_LEFT,
JOYSTICK_RIGHT
}

@Keep
data class ControlNode(
val id: String,
val type: ControlType,
Expand All @@ -27,6 +28,7 @@ data class ControlNode(
val label: String = ""
)

@Keep
data class TouchLayoutData(
var globalOpacity: Float = 0.5f,
var controlsEnabled: Boolean = true,
Expand All @@ -37,6 +39,8 @@ object TouchControlManager {
private const val PREFS_NAME = "touch_controls_prefs"
private const val KEY_LAYOUT_DATA = "layout_data"

private val gson = Gson()

// Axis codes for triggers, mapped to GameActivity constants
const val AXIS_LTRIGGER = 4
const val AXIS_RTRIGGER = 5
Expand Down Expand Up @@ -67,63 +71,19 @@ object TouchControlManager {
}

return try {
val json = JSONObject(jsonStr)
val layout = TouchLayoutData(
globalOpacity = json.optDouble("globalOpacity", 0.5).toFloat(),
controlsEnabled = json.optBoolean("controlsEnabled", true)
)

val nodesArray = json.getJSONArray("nodes")
for (i in 0 until nodesArray.length()) {
val nodeObj = nodesArray.getJSONObject(i)
val type = ControlType.valueOf(nodeObj.getString("type"))
layout.nodes.add(ControlNode(
id = nodeObj.getString("id"),
type = type,
xPercent = nodeObj.getDouble("xPercent").toFloat(),
yPercent = nodeObj.getDouble("yPercent").toFloat(),
scale = nodeObj.optDouble("scale", 1.0).toFloat(),
visible = nodeObj.optBoolean("visible", true),
buttonCode = nodeObj.optInt("buttonCode", -1),
isAxisTrigger = nodeObj.optBoolean("isAxisTrigger", false),
isToggle = nodeObj.optBoolean("isToggle", false),
label = nodeObj.optString("label", "")
))
}
layout
gson.fromJson(jsonStr, TouchLayoutData::class.java) ?: createDefaultLayout()
} catch (e: Exception) {
e.printStackTrace()
createDefaultLayout()
}
}

fun saveLayout(context: Context, layout: TouchLayoutData) {
val json = JSONObject().apply {
put("globalOpacity", layout.globalOpacity.toDouble())
put("controlsEnabled", layout.controlsEnabled)

val nodesArray = JSONArray()
for (node in layout.nodes) {
val nodeObj = JSONObject().apply {
put("id", node.id)
put("type", node.type.name)
put("xPercent", node.xPercent.toDouble())
put("yPercent", node.yPercent.toDouble())
put("scale", node.scale.toDouble())
put("visible", node.visible)
put("buttonCode", node.buttonCode)
put("isAxisTrigger", node.isAxisTrigger)
put("isToggle", node.isToggle)
put("label", node.label)
}
nodesArray.put(nodeObj)
}
put("nodes", nodesArray)
}
val jsonStr = gson.toJson(layout)

context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
.edit()
.putString(KEY_LAYOUT_DATA, json.toString())
.putString(KEY_LAYOUT_DATA, jsonStr)
.apply()
}

Expand Down