Skip to content

Commit

Permalink
latest push
Browse files Browse the repository at this point in the history
  • Loading branch information
Duron27 committed Oct 27, 2024
1 parent e572565 commit de067f9
Show file tree
Hide file tree
Showing 16 changed files with 613 additions and 492 deletions.
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ dependencies {
implementation(libs.androidx.runner)
implementation(libs.androidx.espresso.core)
implementation(libs.androidx.profileinstaller)
implementation(libs.core.ktx)
implementation(libs.reorderable)
implementation(libs.relinker)
implementation(libs.androidx.window)
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@
<activity
android:name="org.openmw.MainActivity"
android:exported="true"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustResize|stateAlwaysVisible"
android:theme="@style/Theme.AppCompat.DayNight.NoActionBar">
android:theme="@style/Theme.AppCompat.DayNight.NoActionBar"
tools:ignore="DiscouragedApi,LockedOrientationActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
Expand Down
109 changes: 32 additions & 77 deletions app/src/main/java/org/openmw/EngineActivity.kt
Original file line number Diff line number Diff line change
@@ -1,46 +1,35 @@
package org.openmw

import android.annotation.SuppressLint
import android.os.Build
import android.os.Bundle
import android.os.Process
import android.system.ErrnoException
import android.system.Os
import android.util.Log
import android.view.KeyEvent
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
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.fillMaxHeight
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Text
import androidx.annotation.RequiresApi
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
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.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.utils.enableLogcat

class EngineActivity : SDLActivity() {
private lateinit var customCursorView: CustomCursorView
Expand All @@ -65,6 +54,8 @@ class EngineActivity : SDLActivity() {
return OPENMW_MAIN_LIB
}

@OptIn(ExperimentalComposeUiApi::class)
@RequiresApi(Build.VERSION_CODES.O)
@SuppressLint("ClickableViewAccessibility")
override fun onCreate(savedInstanceState: Bundle?) {
enableEdgeToEdge()
Expand All @@ -73,21 +64,24 @@ class EngineActivity : SDLActivity() {
sdlView = getContentView()
customCursorView = findViewById(R.id.customCursorView)

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

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

// Load saved buttons
// Load UI saved buttons, 99 is the Thumbstick. Without these 3 lines the button loader will read 99
// from the UI.cfg file and create a duplicate as a button
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)

// Remove sdlView from its parent if necessary
(sdlView.parent as? ViewGroup)?.removeView(sdlView)
sdlContainer.addView(sdlView) // Add SDL view to the sdl_container
(customCursorView.parent as? ViewGroup)?.removeView(customCursorView)
sdlContainer.addView(customCursorView)

WindowCompat.setDecorFitsSystemWindows(window, false)
// Hide the system bars
Expand All @@ -96,26 +90,33 @@ class EngineActivity : SDLActivity() {
controller.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
}

// Create an instance of LogCat and call enableLogcat
//val logCat = LogCat(this)
//logCat.enableLogcat()
getPathToJni(filesDir.parent!!, Constants.USER_FILE_STORAGE)

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

// Setup Compose overlay for buttons
val composeViewUI = findViewById<ComposeView>(R.id.compose_overlayUI)
(composeViewUI.parent as? ViewGroup)?.removeView(composeViewUI)
sdlContainer.addView(composeViewUI)
composeViewUI.setContent {
OverlayUI(
engineActivityContext = this,
editMode = editMode,
createdButtons = createdButtons,
customCursorView = customCursorView
)
createdButtons.forEach { button ->
ResizableDraggableButton(
context = this@EngineActivity,
context = this,
id = button.id,
keyCode = button.keyCode,
editMode = editMode.value,
onDelete = { deleteButton(button.id) }
onDelete = { deleteButton(button.id) },
customCursorView = customCursorView
)
}

thumbstick?.let {
ResizableDraggableThumbstick(
context = this,
Expand All @@ -134,47 +135,8 @@ class EngineActivity : SDLActivity() {
}
)
}

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
val composeViewButtons = findViewById<ComposeView>(R.id.compose_overlayButtons)
composeViewButtons.setContent {
Column(
verticalArrangement = Arrangement.SpaceBetween,
horizontalAlignment = Alignment.End,
modifier = Modifier.fillMaxHeight()
) {
// Toggle Custom Cursor Visibility Button
Button(
onClick = { toggleCustomCursor() },
colors = ButtonDefaults.buttonColors(
containerColor = Color.Transparent
),
modifier = Modifier
.size(50.dp)
.align(Alignment.End)
.border(3.dp, Color.Black, shape = CircleShape) // Add border
) {
Text(
text = "M",
color = Color.White,
fontSize = 15.sp,
fontWeight = FontWeight.Bold
)
}
}
}
}

private fun setupInitialCursorState() {
Expand All @@ -190,13 +152,6 @@ class EngineActivity : SDLActivity() {
saveButtonState(this, createdButtons + loadButtonState(this).filter { it.id == 99 }) // Ensure thumbstick is not removed
}

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

private fun setEnvironmentVariables() {
try {
Os.setenv("OSG_TEXT_SHADER_TECHNIQUE", "ALL", true)
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/org/openmw/HomeScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import kotlinx.coroutines.flow.map
import org.openmw.fragments.SettingsFragment
import org.openmw.navigation.MyFloatingActionButton
import org.openmw.navigation.MyTopBar
import org.openmw.utils.BouncingBackground
import org.openmw.utils.ModValue
import org.openmw.utils.ModValuesList

Expand Down
113 changes: 2 additions & 111 deletions app/src/main/java/org/openmw/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,38 +1,16 @@
package org.openmw

import android.content.Context
import android.os.Build
import android.os.Bundle
import android.view.WindowInsets
import android.view.WindowManager
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.size
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.scale
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.dp
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
Expand All @@ -43,15 +21,13 @@ import androidx.datastore.preferences.preferencesDataStore
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.window.layout.WindowMetrics
import androidx.window.layout.WindowMetricsCalculator
import kotlinx.coroutines.delay
import org.openmw.ui.theme.OpenMWTheme
import org.openmw.utils.CaptureCrash
import org.openmw.utils.ModValue
import org.openmw.utils.PermissionHelper
import org.openmw.utils.getAvailableStorageSpace
import org.openmw.utils.getScreenWidthAndHeight
import org.openmw.utils.readModValues
import org.openmw.utils.updateResolutionInConfig
import java.io.File

val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "game_files_prefs")
Expand Down Expand Up @@ -82,58 +58,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()) {
App(applicationContext, modValues)
}
}
}

// Get storage space
val availableSpace = getAvailableStorageSpace(this)
println("Available storage space: $availableSpace bytes")
}

private fun updateResolutionInConfig(width: Int, height: Int) {
val file = File(Constants.SETTINGS_FILE)
val lines = file.readLines().map { line ->
when {
line.startsWith("resolution y =") -> "resolution y = $width" // mix these up to convert to landscape
line.startsWith("resolution x =") -> "resolution x = $height"
else -> line
}
}
file.writeText(lines.joinToString("\n"))
}

private fun getScreenWidthAndHeight(context: Context): Pair<Int, Int> {
val windowMetrics: WindowMetrics = WindowMetricsCalculator.getOrCreate().computeCurrentWindowMetrics(context)
val bounds = windowMetrics.bounds
var width = bounds.width()
var height = bounds.height()

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
val windowManager = context.getSystemService(WINDOW_SERVICE) as WindowManager
val windowInsets: WindowInsets = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
windowManager.currentWindowMetrics.windowInsets
} else {
TODO("VERSION.SDK_INT < R")
}
val displayCutout = windowInsets.displayCutout
if (displayCutout != null) {
width += displayCutout.safeInsetLeft + displayCutout.safeInsetRight
height += displayCutout.safeInsetTop + displayCutout.safeInsetBottom
}
}
return Pair(width, height)
}
}

Expand Down Expand Up @@ -168,43 +99,3 @@ sealed class Screen(val route: String) {
object Setting: Screen(Route.SETTINGS)
object Home: Screen(Route.HOME)
}

@Composable
fun BouncingBackground() {
val image: Painter = painterResource(id = R.drawable.backgroundbouncebw)
val configuration = LocalConfiguration.current
val screenWidth = configuration.screenWidthDp * configuration.densityDpi / 160
val screenHeight = configuration.screenHeightDp * configuration.densityDpi / 160

val imageWidth = 2000 // Replace with your image width
val imageHeight = 2337 // Replace with your image height

var offset: Offset by remember { mutableStateOf(Offset.Zero) }
val xDirection by remember { mutableFloatStateOf(1f) }
val yDirection by remember { mutableFloatStateOf(1f) }

// Adjust this value to increase the distance
val stepSize = 1f

LaunchedEffect(Unit) {
while (true) {
offset = Offset(
x = (offset.x + xDirection * stepSize) % screenWidth,
y = (offset.y + yDirection * stepSize) % screenHeight
)

delay(16L) // Update every frame (approx 60fps)
}
}

Box(modifier = Modifier.fillMaxSize()) {
Image(
painter = image,
contentDescription = null,
modifier = Modifier
.offset { IntOffset(offset.x.toInt(), offset.y.toInt()) }
.size(imageWidth.dp, imageHeight.dp) // Convert Int to Dp
.scale(6f) // Scale the image up by a factor of 5
.background(color = Color.LightGray))
}
}
Loading

0 comments on commit de067f9

Please sign in to comment.