Skip to content

Commit

Permalink
Merge pull request #8 from teogor/feature/unity-controller
Browse files Browse the repository at this point in the history
Code Refactoring and Documentation Updates
  • Loading branch information
teogor authored Sep 29, 2023
2 parents 9c558e5 + 01eb81e commit 62cbb00
Show file tree
Hide file tree
Showing 15 changed files with 290 additions and 106 deletions.
31 changes: 16 additions & 15 deletions app/src/main/kotlin/dev/teogor/drifter/demo/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ import androidx.lifecycle.Lifecycle
import dev.teogor.drifter.compose.OnLifecycleEvent
import dev.teogor.drifter.compose.UvpComposable
import dev.teogor.drifter.demo.ui.theme.UnityViewTheme
import dev.teogor.drifter.demo.unity.AndroidHandler
import dev.teogor.drifter.demo.unity.Storage
import dev.teogor.drifter.demo.unity.UnityController
import dev.teogor.drifter.demo.unity.UnityStorage
import dev.teogor.drifter.wallpaper.LiveWallpaperUtility

class MainActivity : ComponentActivity() {
Expand All @@ -84,8 +84,8 @@ class MainActivity : ComponentActivity() {
) {
val context = LocalContext.current

val androidHandler = AndroidHandler()
val storage = Storage()
val controller = UnityController()
val storage = UnityStorage()

UvpComposable(
modifier = Modifier
Expand All @@ -96,15 +96,16 @@ class MainActivity : ComponentActivity() {
shape = RoundedCornerShape(20.dp),
),
onCreated = {
androidHandler.apply {
controller.apply {
setEditorMode(true)
setEditorColour(storage.selectedColour, false)
setEditorColor(storage.waterColor, false)
setEditorCycleOption(storage.cycleOption)
}
},
)

UnityColorPicker(
androidHandler = androidHandler,
controller = controller,
storage = storage,
)

Expand All @@ -127,8 +128,8 @@ class MainActivity : ComponentActivity() {

@Composable
fun UnityColorPicker(
androidHandler: AndroidHandler,
storage: Storage,
controller: UnityController,
storage: UnityStorage,
) {
val colors = listOf(
Color(0xFFB19CD9), // Lavender
Expand All @@ -154,7 +155,7 @@ fun UnityColorPicker(
)

var selectedColor by remember {
val color = storage.selectedColour
val color = storage.waterColor
mutableStateOf(
if (color == Color.Unspecified || !colors.contains(color)) {
colors[0]
Expand All @@ -165,18 +166,18 @@ fun UnityColorPicker(
}

LaunchedEffect(selectedColor) {
androidHandler.setEditorColour(selectedColor)
controller.setEditorColor(selectedColor)
}

OnLifecycleEvent { _, event ->
when (event) {
Lifecycle.Event.ON_RESUME -> {
androidHandler.setEditorMode(true)
androidHandler.setEditorColour(selectedColor)
controller.setEditorMode(true)
controller.setEditorColor(selectedColor)
}

Lifecycle.Event.ON_PAUSE -> {
androidHandler.setEditorMode(false)
controller.setEditorMode(false)
}

else -> {}
Expand All @@ -203,7 +204,7 @@ fun UnityColorPicker(

Button(
onClick = {
storage.selectedColour = selectedColor
storage.waterColor = selectedColor
},
modifier = Modifier
.padding(top = 4.dp),
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2023 teogor (Teodor Grigor)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package dev.teogor.drifter.demo.unity

import androidx.compose.ui.graphics.Color
import dev.teogor.drifter.demo.unity.models.CycleOption
import dev.teogor.drifter.demo.unity.models.UnityActionParams
import dev.teogor.drifter.demo.unity.models.UnityActions
import dev.teogor.drifter.demo.unity.models.toJsonObject
import dev.teogor.drifter.integration.core.UnityControllerBase

class UnityController : UnityControllerBase(
receiver = "BridgeController",
) {

fun setEditorMode(isEditorMode: Boolean) {
val params = UnityActionParams(isEditorMode = isEditorMode)
invokeAction(
UnityActions.SetEditorMode,
params.toJsonObject(),
)
}

fun setEditorColor(waterColor: Color, animated: Boolean = true) {
val params = UnityActionParams(waterColor = waterColor, animated = animated)
invokeAction(
UnityActions.SetWaterColor,
params.toJsonObject(),
)
}

fun setEditorCycleOption(cycleOption: CycleOption) {
val params = UnityActionParams(cycleOption = cycleOption)
invokeAction(
UnityActions.SetCycleOption,
params.toJsonObject(),
)
}

}
57 changes: 46 additions & 11 deletions app/src/main/kotlin/dev/teogor/drifter/demo/unity/UnityStorage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,52 @@

package dev.teogor.drifter.demo.unity

import dev.teogor.drifter.integration.core.Validator
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import dev.teogor.drifter.demo.unity.models.CycleOption
import dev.teogor.drifter.demo.unity.models.StorageKeys
import dev.teogor.drifter.integration.common.TypeConverter
import dev.teogor.drifter.integration.core.UnityStorageBase

// todo same as Storage
open class UnityStorage {
protected open fun writeElement(key: String, content: String?) {
Validator.instance.writeElement(key, content)
}
class UnityStorage : UnityStorageBase() {

open fun getStorageElement(
key: String,
): String? {
return Validator.instance.getStorageElement(key)
}
private val colorConverter = TypeConverter(
toString = { color -> color.toArgb().toString() },
fromString = { string -> Color(string.toInt()) },
)

var waterColor: Color = Color(0xFF6FA3EF)
get() {
field = getStorageElement(
key = StorageKeys.WaterColor,
defaultValue = field,
converter = colorConverter,
)
return field
}
set(value) {
println("Write Color ::")
field = writeElement(
key = StorageKeys.WaterColor,
content = value,
converter = colorConverter,
)
}

var cycleOption: CycleOption = CycleOption.Day
get() {
val cycleOptionContent = getStorageElement(
key = StorageKeys.CycleOption,
defaultValue = field.ordinal,
)
field = CycleOption.entries[cycleOptionContent]
return field
}
set(value) {
field = value
writeElement(
key = StorageKeys.CycleOption,
content = value.ordinal,
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dev.teogor.drifter.demo.unity.models

enum class CycleOption {
Day,
Night,
Auto;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package dev.teogor.drifter.demo.unity.models

object StorageKeys {
const val WaterColor = "storage_water_color"
const val CycleOption = "storage_cycle_option"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package dev.teogor.drifter.demo.unity.models

import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import org.json.JSONObject

data class UnityActionParams(
val isEditorMode: Boolean? = null,
val waterColor: Color? = null,
val animated: Boolean? = null,
val cycleOption: CycleOption? = null
)

fun UnityActionParams.toJsonObject(): JSONObject {
val json = JSONObject()
isEditorMode?.let { json.put("isEditorMode", it) }
waterColor?.let { json.put("waterColor", it.toArgb()) }
animated?.let { json.put("animated", it) }
cycleOption?.let { json.put("cycleOption", it) }
return json
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dev.teogor.drifter.demo.unity.models

object UnityActions {
const val SetEditorMode = "SetEditorMode"
const val SetWaterColor = "SetWaterColor"
const val SetCycleOption = "SetCycleOption"
}
21 changes: 21 additions & 0 deletions drifter-integration/api/drifter-integration.api
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ public final class dev/teogor/drifter/integration/activities/UnityPlayerInstanti
public fun <init> ()V
}

public final class dev/teogor/drifter/integration/common/TypeConverter {
public fun <init> (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;)V
public final fun convertFromString (Ljava/lang/String;)Ljava/lang/Object;
public final fun convertToString (Ljava/lang/Object;)Ljava/lang/String;
}

public final class dev/teogor/drifter/integration/core/AndroidUtils {
public static final field Companion Ldev/teogor/drifter/integration/core/AndroidUtils$Companion;
public fun <init> ()V
Expand Down Expand Up @@ -84,23 +90,38 @@ public class dev/teogor/drifter/integration/core/Storage {
public abstract interface annotation class dev/teogor/drifter/integration/core/UnityCallback : java/lang/annotation/Annotation {
}

public class dev/teogor/drifter/integration/core/UnityControllerBase {
public fun <init> (Ljava/lang/String;)V
public final fun invokeAction (Ljava/lang/String;Lorg/json/JSONObject;)V
}

public final class dev/teogor/drifter/integration/core/UnityDispatcher {
public fun <init> (Ljava/lang/String;Ljava/lang/String;Ldev/teogor/drifter/integration/core/Message;)V
public final fun getFunctionName ()Ljava/lang/String;
public final fun getFunctionParameter ()Ldev/teogor/drifter/integration/core/Message;
public final fun getGameObject ()Ljava/lang/String;
}

public class dev/teogor/drifter/integration/core/UnityStorageBase {
public fun <init> ()V
protected fun getStorageElement (Ljava/lang/String;Ljava/lang/Object;Ldev/teogor/drifter/integration/common/TypeConverter;)Ljava/lang/Object;
public static synthetic fun getStorageElement$default (Ldev/teogor/drifter/integration/core/UnityStorageBase;Ljava/lang/String;Ljava/lang/Object;Ldev/teogor/drifter/integration/common/TypeConverter;ILjava/lang/Object;)Ljava/lang/Object;
protected fun writeElement (Ljava/lang/String;Ljava/lang/Object;Ldev/teogor/drifter/integration/common/TypeConverter;)Ljava/lang/Object;
public static synthetic fun writeElement$default (Ldev/teogor/drifter/integration/core/UnityStorageBase;Ljava/lang/String;Ljava/lang/Object;Ldev/teogor/drifter/integration/common/TypeConverter;ILjava/lang/Object;)Ljava/lang/Object;
}

public final class dev/teogor/drifter/integration/core/Validator {
public static final field Companion Ldev/teogor/drifter/integration/core/Validator$Companion;
public fun <init> ()V
public final fun dataChangedRead ()V
public final fun getContext ()Landroid/content/Context;
public final fun getStorageElement (Ljava/lang/String;)Ljava/lang/String;
public final fun getStorageElement (Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/Object;
public final fun isDataChanged ()Z
public final fun markAsRead (Ljava/lang/String;)V
public final fun setDataChanged ()V
public final fun toast (Ljava/lang/String;)V
public final fun writeElement (Ljava/lang/String;I)V
public final fun writeElement (Ljava/lang/String;Ljava/lang/String;)V
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,12 @@
* limitations under the License.
*/

package dev.teogor.drifter.demo.unity
package dev.teogor.drifter.integration.common

import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb

class Storage : UnityStorage() {

var selectedColour: Color = Color.Unspecified
get() {
val colourContent = getStorageElement(
key = StorageElements.COLOUR,
)
if (colourContent != null) {
field = Color(colourContent.toUInt().toInt())
}
return field
}
set(value) {
field = value
writeElement(
key = StorageElements.COLOUR,
content = value.toArgb().toUInt().toString(),
)
}
class TypeConverter<T>(
private val toString: (T) -> String,
private val fromString: (String) -> T,
) {
fun convertToString(value: T): String = toString(value)
fun convertFromString(string: String): T = fromString(string)
}
Loading

0 comments on commit 62cbb00

Please sign in to comment.