Files
digital_person/app/src/main/java/com/digitalperson/ui/Live2DUiManager.kt
gcw_4spBpAfv 1701ecfb7f push2talk
2026-03-02 17:18:18 +08:00

146 lines
4.4 KiB
Kotlin

package com.digitalperson.ui
import android.app.Activity
import android.opengl.GLSurfaceView
import android.text.method.ScrollingMovementMethod
import android.view.MotionEvent
import android.widget.Button
import android.widget.LinearLayout
import android.widget.ScrollView
import android.widget.TextView
import android.widget.Toast
import com.digitalperson.live2d.Live2DAvatarManager
class Live2DUiManager(private val activity: Activity) {
private var textView: TextView? = null
private var scrollView: ScrollView? = null
private var startButton: Button? = null
private var stopButton: Button? = null
private var recordButton: Button? = null
private var traditionalButtons: LinearLayout? = null
private var avatarManager: Live2DAvatarManager? = null
private var lastUiText: String = ""
fun initViews(
textViewId: Int,
scrollViewId: Int,
startButtonId: Int = -1,
stopButtonId: Int = -1,
recordButtonId: Int = -1,
traditionalButtonsId: Int = -1,
silentPlayerViewId: Int,
speakingPlayerViewId: Int,
live2dViewId: Int
) {
textView = activity.findViewById(textViewId)
scrollView = activity.findViewById(scrollViewId)
if (startButtonId != -1) startButton = activity.findViewById(startButtonId)
if (stopButtonId != -1) stopButton = activity.findViewById(stopButtonId)
if (recordButtonId != -1) recordButton = activity.findViewById(recordButtonId)
if (traditionalButtonsId != -1) traditionalButtons = activity.findViewById(traditionalButtonsId)
textView?.movementMethod = ScrollingMovementMethod()
val glView = activity.findViewById<GLSurfaceView>(live2dViewId)
avatarManager = Live2DAvatarManager(glView)
avatarManager?.setSpeaking(false)
}
fun setStartButtonListener(listener: () -> Unit) {
startButton?.setOnClickListener { listener() }
}
fun setStopButtonListener(listener: () -> Unit) {
stopButton?.setOnClickListener { listener() }
}
fun setRecordButtonTouchListener(listener: (Boolean) -> Unit) {
recordButton?.setOnTouchListener {
_, event ->
when (event.action) {
MotionEvent.ACTION_DOWN -> {
recordButton?.isPressed = true
listener(true)
true
}
MotionEvent.ACTION_UP,
MotionEvent.ACTION_CANCEL -> {
recordButton?.isPressed = false
listener(false)
true
}
else -> false
}
}
}
fun setUseHoldToSpeak(useHoldToSpeak: Boolean) {
if (useHoldToSpeak) {
traditionalButtons?.visibility = LinearLayout.GONE
recordButton?.visibility = Button.VISIBLE
} else {
traditionalButtons?.visibility = LinearLayout.VISIBLE
recordButton?.visibility = Button.GONE
}
}
fun appendToUi(s: String) {
lastUiText += s
textView?.text = lastUiText
scrollView?.post { scrollView?.fullScroll(ScrollView.FOCUS_DOWN) }
}
fun clearText() {
lastUiText = ""
textView?.text = ""
}
fun setText(text: String) {
lastUiText = text
textView?.text = text
}
fun setButtonsEnabled(startEnabled: Boolean = false, stopEnabled: Boolean = false, recordEnabled: Boolean = true) {
startButton?.isEnabled = startEnabled
stopButton?.isEnabled = stopEnabled
recordButton?.isEnabled = recordEnabled
}
fun setSpeaking(speaking: Boolean) {
activity.runOnUiThread {
avatarManager?.setSpeaking(speaking)
}
}
fun setMood(mood: String) {
activity.runOnUiThread {
avatarManager?.setMood(mood)
}
}
fun startSpecificMotion(motionName: String) {
activity.runOnUiThread {
avatarManager?.startSpecificMotion(motionName)
}
}
fun showToast(message: String, duration: Int = Toast.LENGTH_SHORT) {
activity.runOnUiThread {
Toast.makeText(activity, message, duration).show()
}
}
fun onResume() {
avatarManager?.onResume()
}
fun onPause() {
avatarManager?.onPause()
}
fun release() {
avatarManager?.release()
avatarManager = null
}
}