summaryrefslogtreecommitdiff
path: root/hello_signals/src/main/java
diff options
context:
space:
mode:
authorfhuya <fhuya@google.com>2020-03-27 01:35:23 -0700
committerfhuya <fhuya@google.com>2020-03-27 01:35:23 -0700
commit8a50b716f5d335d0c060eb096032fc2d214dc635 (patch)
tree720c5f933732a949e0c2368a4cb0ee62354d9c28 /hello_signals/src/main/java
parent6837796a9d7acd3a5ea7ef241a23fd393c3a6609 (diff)
downloadgodot-android-samples-8a50b716f5d335d0c060eb096032fc2d214dc635.tar.gz
godot-android-samples-8a50b716f5d335d0c060eb096032fc2d214dc635.tar.bz2
godot-android-samples-8a50b716f5d335d0c060eb096032fc2d214dc635.zip
Add `HelloSignals` sample project to show how to register and emit signals with a Godot Android plugin
Diffstat (limited to 'hello_signals/src/main/java')
-rw-r--r--hello_signals/src/main/java/fhuyakou/godot/plugin/android/hellosignals/HelloSignalsPlugin.kt59
1 files changed, 59 insertions, 0 deletions
diff --git a/hello_signals/src/main/java/fhuyakou/godot/plugin/android/hellosignals/HelloSignalsPlugin.kt b/hello_signals/src/main/java/fhuyakou/godot/plugin/android/hellosignals/HelloSignalsPlugin.kt
new file mode 100644
index 0000000..9f6eb87
--- /dev/null
+++ b/hello_signals/src/main/java/fhuyakou/godot/plugin/android/hellosignals/HelloSignalsPlugin.kt
@@ -0,0 +1,59 @@
+package fhuyakou.godot.plugin.android.hellosignals
+
+import android.util.Log
+import org.godotengine.godot.Godot
+import org.godotengine.godot.plugin.GodotPlugin
+import org.godotengine.godot.plugin.SignalInfo
+import java.util.concurrent.Executors
+import java.util.concurrent.ScheduledFuture
+import java.util.concurrent.TimeUnit
+
+/**
+ * Exposes a [onButtonPressed] method to the game logic. Invoking the method starts a timer which
+ * fires a `TikTok` signal every second. Invoking the method a second time stops the timer.
+ */
+class HelloSignalsPlugin(godot: Godot) : GodotPlugin(godot) {
+
+ companion object {
+ val TAG = HelloSignalsPlugin::class.java.simpleName
+ }
+
+ private val tikTokSignalInfo = SignalInfo("TikTok")
+ private var tikTokTask : ScheduledFuture<*>? = null
+
+ override fun getPluginName() = "HelloSignals"
+
+ override fun getPluginSignals(): Set<SignalInfo> {
+ Log.i(TAG, "Registering $tikTokSignalInfo")
+ return setOf(tikTokSignalInfo)
+ }
+
+ override fun getPluginMethods() = listOf("onButtonPressed")
+
+ private fun startTikTok(): Boolean {
+ if (tikTokTask == null || tikTokTask!!.isDone) {
+ Log.i(TAG, "Starting tiktok...")
+ tikTokTask = Executors.newSingleThreadScheduledExecutor()
+ .scheduleAtFixedRate({ emitSignal(tikTokSignalInfo.name) }, 0, 1, TimeUnit.SECONDS)
+ return true
+ }
+ return false
+ }
+
+ private fun stopTikTok() {
+ if (tikTokTask != null) {
+ if (!tikTokTask!!.isDone) {
+ Log.i(TAG, "Stopping tiktok...")
+ tikTokTask!!.cancel(true)
+ }
+ tikTokTask = null
+ }
+ }
+
+ private fun onButtonPressed() {
+ Log.i(TAG, "OnButtonPressed from Kotlin")
+ if (!startTikTok()) {
+ stopTikTok()
+ }
+ }
+} \ No newline at end of file