summaryrefslogtreecommitdiff
path: root/plugins/hello_signals/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/hello_signals/src/main')
-rw-r--r--plugins/hello_signals/src/main/AndroidManifest.xml20
-rw-r--r--plugins/hello_signals/src/main/java/fhuyakou/godot/plugin/android/hellosignals/HelloSignalsPlugin.kt59
2 files changed, 79 insertions, 0 deletions
diff --git a/plugins/hello_signals/src/main/AndroidManifest.xml b/plugins/hello_signals/src/main/AndroidManifest.xml
new file mode 100644
index 0000000..8f791bd
--- /dev/null
+++ b/plugins/hello_signals/src/main/AndroidManifest.xml
@@ -0,0 +1,20 @@
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+ package="fhuyakou.godot.plugin.android.hellosignals">
+
+ <application>
+ <!--
+ Plugin metadata:
+
+ - In the `android:name` attribute, the `org.godotengine.plugin.v1` prefix
+ is required so Godot can recognize the project as a valid Godot
+ Android plugin. The plugin name following the prefix should match the value
+ of the plugin name returned by the plugin initializer.
+
+ - The `android:value` attribute should be the classpath to the plugin
+ initializer.
+ -->
+ <meta-data
+ android:name="org.godotengine.plugin.v1.HelloSignals"
+ android:value="fhuyakou.godot.plugin.android.hellosignals.HelloSignalsPlugin" />
+ </application>
+</manifest>
diff --git a/plugins/hello_signals/src/main/java/fhuyakou/godot/plugin/android/hellosignals/HelloSignalsPlugin.kt b/plugins/hello_signals/src/main/java/fhuyakou/godot/plugin/android/hellosignals/HelloSignalsPlugin.kt
new file mode 100644
index 0000000..d6df7ef
--- /dev/null
+++ b/plugins/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 org.godotengine.godot.plugin.UsedByGodot
+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)
+ }
+
+ 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
+ }
+ }
+
+ @UsedByGodot
+ private fun onButtonPressed() {
+ Log.i(TAG, "OnButtonPressed from Kotlin")
+ if (!startTikTok()) {
+ stopTikTok()
+ }
+ }
+}