Skip to content

Commit

Permalink
UI added
Browse files Browse the repository at this point in the history
  • Loading branch information
Duron27 committed Oct 24, 2024
1 parent c47dd82 commit e572565
Show file tree
Hide file tree
Showing 12 changed files with 975 additions and 428 deletions.
153 changes: 79 additions & 74 deletions app/src/main/java/org/openmw/EngineActivity.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package org.openmw


import android.annotation.SuppressLint
import android.content.Context
import android.os.Bundle
import android.os.Process
import android.system.ErrnoException
Expand All @@ -15,17 +13,13 @@ import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Star
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
Expand All @@ -38,17 +32,21 @@ import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
import org.libsdl.app.SDLActivity
import org.openmw.ui.controls.ButtonState
import org.openmw.ui.controls.CustomCursorView
import org.openmw.ui.overlay.GameControllerButtons
import org.openmw.ui.overlay.HiddenMenu
import org.openmw.ui.controls.DynamicButtonManager
import org.openmw.ui.controls.ResizableDraggableButton
import org.openmw.ui.controls.ResizableDraggableThumbstick
import org.openmw.ui.controls.UIStateManager
import org.openmw.ui.controls.loadButtonState
import org.openmw.ui.controls.saveButtonState
import org.openmw.ui.overlay.OverlayUI
import org.openmw.ui.overlay.Thumbstick
import org.openmw.ui.overlay.sendKeyEvent
import org.openmw.utils.LogCat

class EngineActivity : SDLActivity() {
private var customCursorView: CustomCursorView? = null
private lateinit var customCursorView: CustomCursorView
private lateinit var sdlView: View
private val createdButtons = mutableStateListOf<ButtonState>()
private var editMode = mutableStateOf(false)

init {
setEnvironmentVariables()
Expand All @@ -73,10 +71,20 @@ class EngineActivity : SDLActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.engine_activity)
sdlView = getContentView()
customCursorView = findViewById<CustomCursorView>(R.id.customCursorView).apply {
sdlView = this@EngineActivity.sdlView // Set SDL view reference
customCursorView = findViewById(R.id.customCursorView)

customCursorView.apply {
sdlView = this@EngineActivity.sdlView
}

// Ensure the correct initial state of the cursor
setupInitialCursorState()

// Load saved buttons
val allButtons = loadButtonState(this)
val thumbstick = allButtons.find { it.id == 99 }
createdButtons.addAll(allButtons.filter { it.id != 99 })

// Add SDL view programmatically
val sdlContainer = findViewById<FrameLayout>(R.id.sdl_container)
sdlContainer.addView(sdlView) // Add SDL view to the sdl_container
Expand All @@ -93,28 +101,50 @@ class EngineActivity : SDLActivity() {
//logCat.enableLogcat()
getPathToJni(filesDir.parent!!, Constants.USER_FILE_STORAGE)

// Setup Compose overlay for thumbstick
val composeView = findViewById<ComposeView>(R.id.compose_overlay)
composeView.setContent {
Thumbstick(
onWClick = { onNativeKeyDown(KeyEvent.KEYCODE_W) },
onAClick = { onNativeKeyDown(KeyEvent.KEYCODE_A) },
onSClick = { onNativeKeyDown(KeyEvent.KEYCODE_S) },
onDClick = { onNativeKeyDown(KeyEvent.KEYCODE_D) },
onRelease = {
onNativeKeyUp(KeyEvent.KEYCODE_W)
onNativeKeyUp(KeyEvent.KEYCODE_A)
onNativeKeyUp(KeyEvent.KEYCODE_S)
onNativeKeyUp(KeyEvent.KEYCODE_D)
}
)
}

// Setup Compose overlay for buttons
val composeViewMenu = findViewById<ComposeView>(R.id.compose_overlayMenu)
composeViewMenu.setContent {
OverlayUI()
HiddenMenu()
OverlayUI(engineActivityContext = this)

createdButtons.forEach { button ->
ResizableDraggableButton(
context = this@EngineActivity,
id = button.id,
keyCode = button.keyCode,
editMode = editMode.value,
onDelete = { deleteButton(button.id) }
)
}

thumbstick?.let {
ResizableDraggableThumbstick(
context = this,
id = 99,
keyCode = it.keyCode,
editMode = editMode.value,
onWClick = { onNativeKeyDown(KeyEvent.KEYCODE_W) },
onAClick = { onNativeKeyDown(KeyEvent.KEYCODE_A) },
onSClick = { onNativeKeyDown(KeyEvent.KEYCODE_S) },
onDClick = { onNativeKeyDown(KeyEvent.KEYCODE_D) },
onRelease = {
onNativeKeyUp(KeyEvent.KEYCODE_W)
onNativeKeyUp(KeyEvent.KEYCODE_A)
onNativeKeyUp(KeyEvent.KEYCODE_S)
onNativeKeyUp(KeyEvent.KEYCODE_D)
}
)
}

DynamicButtonManager(
context = this@EngineActivity,
onNewButtonAdded = { newButtonState ->
createdButtons.add(newButtonState)
},
editMode = editMode,
createdButtons = createdButtons // Pass created buttons to DynamicButtonManager
)

//HiddenMenu() // RadialMenu.kt
}

// Setup Compose overlay for buttons
Expand Down Expand Up @@ -143,52 +173,27 @@ class EngineActivity : SDLActivity() {
fontWeight = FontWeight.Bold
)
}
Button(
onClick = {
onNativeKeyDown(KeyEvent.KEYCODE_F)
sendKeyEvent(KeyEvent.KEYCODE_F)
onNativeKeyUp(KeyEvent.KEYCODE_F)
},
colors = ButtonDefaults.buttonColors(
containerColor = Color.Transparent
),
modifier = Modifier.size(50.dp).border(3.dp, Color.Black, shape = CircleShape)
) {
Text(
text = "W",
color = Color.White,
fontSize = 15.sp,
fontWeight = FontWeight.Bold
)
}
// Button to perform mouse click
Button(
onClick = {
customCursorView!!.performMouseClick()
},
colors = ButtonDefaults.buttonColors(
containerColor = Color.Transparent
),
modifier = Modifier.size(50.dp).border(3.dp, Color.Black, shape = CircleShape)
) {
Text(
text = "C",
color = Color.White,
fontSize = 15.sp,
fontWeight = FontWeight.Bold
)
}
// Game Controller Buttons at the Bottom
GameControllerButtons()
}
}
}

private var isCustomCursorEnabled = false
private fun setupInitialCursorState() {
if (UIStateManager.isCustomCursorEnabled) {
customCursorView.visibility = View.VISIBLE
} else {
customCursorView.visibility = View.GONE
}
}

private fun deleteButton(buttonId: Int) {
createdButtons.removeIf { it.id == buttonId }
saveButtonState(this, createdButtons + loadButtonState(this).filter { it.id == 99 }) // Ensure thumbstick is not removed
}

fun toggleCustomCursor() {
runOnUiThread {
isCustomCursorEnabled = !isCustomCursorEnabled
customCursorView?.visibility = if (isCustomCursorEnabled) View.VISIBLE else View.GONE
UIStateManager.isCustomCursorEnabled = !UIStateManager.isCustomCursorEnabled
customCursorView.visibility = if (UIStateManager.isCustomCursorEnabled) View.VISIBLE else View.GONE
}
}

Expand Down
11 changes: 9 additions & 2 deletions app/src/main/java/org/openmw/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ class MainActivity : ComponentActivity() {
controller.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
}

// a little extra protection from people deleting the thumbstick by mistake
val file = File("${Constants.USER_CONFIG}/UI.cfg")
if (!file.exists()) {
file.createNewFile()
file.appendText("ButtonID_99(200.0;200.56776;281.6349;false;29)\n")
}

setContent {
OpenMWTheme {
Surface(modifier = Modifier.fillMaxSize()) {
Expand All @@ -99,8 +106,8 @@ class MainActivity : ComponentActivity() {
val file = File(Constants.SETTINGS_FILE)
val lines = file.readLines().map { line ->
when {
line.startsWith("resolution y =") -> "resolution y = $height" // $height
line.startsWith("resolution x =") -> "resolution x = $width" // $width
line.startsWith("resolution y =") -> "resolution y = $width" // mix these up to convert to landscape
line.startsWith("resolution x =") -> "resolution x = $height"
else -> line
}
}
Expand Down
2 changes: 0 additions & 2 deletions app/src/main/java/org/openmw/fragments/ModsFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.openmw.Constants
import org.openmw.getAbsolutePathFromUri
import org.openmw.storeGameFilesUri
import org.openmw.utils.ModValue
import org.openmw.utils.writeModValuesToFile

Expand All @@ -31,7 +30,6 @@ class ModsFragment {
try {
context.contentResolver.takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
CoroutineScope(Dispatchers.IO).launch {
storeGameFilesUri(context, uri)
val ignoreList = listOf("Morrowind.bsa", "Tribunal.bsa", "Bloodmoon.bsa")
val extensions = arrayOf("bsa", "esm", "esp", "omwaddon", "omwgame", "omwscripts")
val selectedDirectory = DocumentFile.fromTreeUri(context, uri)
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/java/org/openmw/fragments/SettingsFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ class SettingsFragment : ComponentActivity() {
val extensions = arrayOf("esm", "bsa")
val modDirectory = dataFilesFolder ?: selectedDirectory
val files = findFilesWithExtensions(modDirectory, extensions)
val modPath = getAbsolutePathFromUri(context, uri)

if (iniFile != null && dataFilesFolder != null && dataFilesFolder.isDirectory) {
// Update savedPath after setting it with the document tree
Expand Down Expand Up @@ -93,7 +92,7 @@ class SettingsFragment : ComponentActivity() {
// Define the regex pattern to match any user-data value

val regexData = Regex("""^data\s*=\s*".*?"""")
val replacementStringData = """data="${modPath}Data Files""""
val replacementStringData = """data="${savedPath}Data Files""""
val file = File(fileName)

// Read and replace lines in the file
Expand Down
Loading

0 comments on commit e572565

Please sign in to comment.