Make popups and text fields nicer to interact with on Android (#7211)

* Make popups and text fields nicer to interact with on Android

* Refactor: Rename createTextField to UncivTextField.create

* Fix multiplayer save delete text
This commit is contained in:
Timo T
2022-06-22 08:32:20 +02:00
committed by GitHub
parent a13a39293f
commit 68cc4303ec
27 changed files with 434 additions and 73 deletions

View File

@ -1,19 +1,27 @@
package com.unciv.app
import android.content.Intent
import android.graphics.Rect
import android.net.Uri
import android.os.Build
import android.os.Bundle
import androidx.annotation.RequiresApi
import android.view.View
import android.view.ViewTreeObserver
import androidx.core.app.NotificationManagerCompat
import androidx.work.WorkManager
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.backends.android.AndroidApplication
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration
import com.badlogic.gdx.backends.android.AndroidGraphics
import com.badlogic.gdx.math.Rectangle
import com.unciv.UncivGame
import com.unciv.UncivGameParameters
import com.unciv.logic.GameSaver
import com.unciv.logic.event.EventBus
import com.unciv.ui.UncivStage
import com.unciv.ui.utils.BaseScreen
import com.unciv.ui.utils.Fonts
import com.unciv.utils.Log
import com.unciv.utils.concurrency.Concurrency
import java.io.File
open class AndroidLauncher : AndroidApplication() {
@ -53,6 +61,41 @@ open class AndroidLauncher : AndroidApplication() {
initialize(game, config)
setDeepLinkedGame(intent)
addScreenObscuredListener((Gdx.graphics as AndroidGraphics).view)
}
private fun addScreenObscuredListener(contentView: View) {
contentView.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
/** [onGlobalLayout] gets triggered not only when the [windowVisibleDisplayFrame][View.getWindowVisibleDisplayFrame] changes, but also on other things.
* So we need to check if that was actually the thing that changed. */
private var lastVisibleDisplayFrame: Rect? = null
override fun onGlobalLayout() {
if (!UncivGame.isCurrentInitialized() || UncivGame.Current.screen == null) {
return
}
val r = Rect()
contentView.getWindowVisibleDisplayFrame(r)
if (r.equals(lastVisibleDisplayFrame)) return
lastVisibleDisplayFrame = r
val stage = (UncivGame.Current.screen as BaseScreen).stage
val horizontalRatio = stage.width / contentView.width
val verticalRatio = stage.height / contentView.height
val visibleStage = Rectangle(
r.left * horizontalRatio,
(contentView.height - r.bottom) * verticalRatio, // Android coordinate system has the origin in the top left, while GDX uses bottom left
r.width() * horizontalRatio,
r.height() * verticalRatio
)
Concurrency.runOnGLThread {
EventBus.send(UncivStage.VisibleAreaChanged(visibleStage))
}
}
})
}
/**