diff options
author | fhuya <fhuya@google.com> | 2020-03-27 01:35:23 -0700 |
---|---|---|
committer | fhuya <fhuya@google.com> | 2020-03-27 01:35:23 -0700 |
commit | 8a50b716f5d335d0c060eb096032fc2d214dc635 (patch) | |
tree | 720c5f933732a949e0c2368a4cb0ee62354d9c28 | |
parent | 6837796a9d7acd3a5ea7ef241a23fd393c3a6609 (diff) | |
download | godot-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
-rw-r--r-- | build.gradle | 6 | ||||
-rw-r--r-- | hello_signals/.gitignore | 1 | ||||
-rw-r--r-- | hello_signals/build.gradle | 37 | ||||
-rw-r--r-- | hello_signals/demo/.gitignore | 2 | ||||
-rw-r--r-- | hello_signals/demo/Main.gd | 29 | ||||
-rw-r--r-- | hello_signals/demo/Main.tscn | 34 | ||||
-rw-r--r-- | hello_signals/demo/default_env.tres | 7 | ||||
-rw-r--r-- | hello_signals/demo/export_presets.cfg | 200 | ||||
-rw-r--r-- | hello_signals/demo/icon.png | bin | 0 -> 3305 bytes | |||
-rw-r--r-- | hello_signals/demo/project.godot | 27 | ||||
-rw-r--r-- | hello_signals/src/main/AndroidManifest.xml | 20 | ||||
-rw-r--r-- | hello_signals/src/main/java/fhuyakou/godot/plugin/android/hellosignals/HelloSignalsPlugin.kt | 59 | ||||
-rw-r--r-- | libs/godot-lib.release.aar | bin | 9149797 -> 11795976 bytes | |||
-rw-r--r-- | settings.gradle | 1 |
14 files changed, 422 insertions, 1 deletions
diff --git a/build.gradle b/build.gradle index 9494940..601d66a 100644 --- a/build.gradle +++ b/build.gradle @@ -2,6 +2,9 @@ buildscript { + ext { + kotlin_version = '1.3.70' + } repositories { google() jcenter() @@ -9,7 +12,8 @@ buildscript { } dependencies { classpath 'com.android.tools.build:gradle:3.6.1' - + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" + // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/hello_signals/.gitignore b/hello_signals/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/hello_signals/.gitignore @@ -0,0 +1 @@ +/build diff --git a/hello_signals/build.gradle b/hello_signals/build.gradle new file mode 100644 index 0000000..55bee0e --- /dev/null +++ b/hello_signals/build.gradle @@ -0,0 +1,37 @@ +apply plugin: 'com.android.library' +apply plugin: 'kotlin-android' +apply plugin: 'kotlin-android-extensions' + +android { + compileSdkVersion 29 + buildToolsVersion "29.0.1" + + defaultConfig { + minSdkVersion 18 + targetSdkVersion 29 + versionCode 1 + versionName "1.0" + } + + // Used to customize the name of generated AAR file. + libraryVariants.all { variant -> + variant.outputs.all { output -> + output.outputFileName = "HelloSignals.${variant.name}.aar" + } + } + +} + +dependencies { + /* + Used to provide dependency on the Godot Android library. A version of that + library will be made available for each stable release of Godot. + + `compileOnly` is used instead of `implementation` to ensure that the + godot library is not bundled with the generated plugin AAR file. This is + necessary since the Godot editor will also provide a version of the godot + library when building the final binary. + */ + compileOnly fileTree(dir: '../libs', include: ['godot-lib*.aar']) + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" +} diff --git a/hello_signals/demo/.gitignore b/hello_signals/demo/.gitignore new file mode 100644 index 0000000..b405634 --- /dev/null +++ b/hello_signals/demo/.gitignore @@ -0,0 +1,2 @@ +*.import +/android/ diff --git a/hello_signals/demo/Main.gd b/hello_signals/demo/Main.gd new file mode 100644 index 0000000..22e5255 --- /dev/null +++ b/hello_signals/demo/Main.gd @@ -0,0 +1,29 @@ +extends Node2D + +var timerCount = 0 +var timerRunning = false +var helloSignals + +func _ready(): + if Engine.has_singleton("HelloSignals"): + helloSignals = Engine.get_singleton("HelloSignals") + helloSignals.connect("TikTok", self, "_on_tiktok") + + $Button.connect("pressed", self, "_on_Button_pressed") + else: + print("Couldn't find HelloSignals singleton") + + +func _on_tiktok(): + print("TikTok signal received") + timerCount = timerCount + 1 + $Label.text = str(timerCount) + +func _on_Button_pressed(): + print("on button pressed from GDScript") + helloSignals.onButtonPressed() + timerRunning = !timerRunning + if (timerRunning): + $Button.text = "Stop Timer" + else: + $Button.text = "Start Timer" diff --git a/hello_signals/demo/Main.tscn b/hello_signals/demo/Main.tscn new file mode 100644 index 0000000..41e5fb4 --- /dev/null +++ b/hello_signals/demo/Main.tscn @@ -0,0 +1,34 @@ +[gd_scene load_steps=3 format=2] + +[ext_resource path="res://Main.gd" type="Script" id=1] + +[sub_resource type="StyleBoxEmpty" id=1] + +[node name="Main" type="Node2D"] +script = ExtResource( 1 ) + +[node name="Button" type="Button" parent="."] +margin_left = 90.0 +margin_top = 178.0 +margin_right = 281.0 +margin_bottom = 261.0 +rect_scale = Vector2( 2, 2 ) +text = "Start Timer" +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="Label" type="Label" parent="."] +margin_left = 515.0 +margin_top = 217.0 +margin_right = 591.0 +margin_bottom = 265.0 +rect_scale = Vector2( 2, 2 ) +custom_styles/normal = SubResource( 1 ) +text = "00" +align = 1 +valign = 1 +uppercase = true +__meta__ = { +"_edit_use_anchors_": false +} diff --git a/hello_signals/demo/default_env.tres b/hello_signals/demo/default_env.tres new file mode 100644 index 0000000..20207a4 --- /dev/null +++ b/hello_signals/demo/default_env.tres @@ -0,0 +1,7 @@ +[gd_resource type="Environment" load_steps=2 format=2] + +[sub_resource type="ProceduralSky" id=1] + +[resource] +background_mode = 2 +background_sky = SubResource( 1 ) diff --git a/hello_signals/demo/export_presets.cfg b/hello_signals/demo/export_presets.cfg new file mode 100644 index 0000000..76c50f1 --- /dev/null +++ b/hello_signals/demo/export_presets.cfg @@ -0,0 +1,200 @@ +[preset.0] + +name="Android" +platform="Android" +runnable=true +custom_features="" +export_filter="all_resources" +include_filter="" +exclude_filter="" +export_path="" +patch_list=PoolStringArray( ) +script_export_mode=1 +script_encryption_key="" + +[preset.0.options] + +graphics/32_bits_framebuffer=true +xr_features/xr_mode=0 +xr_features/degrees_of_freedom=0 +xr_features/hand_tracking=0 +one_click_deploy/clear_previous_install=false +custom_template/debug="" +custom_template/release="" +custom_template/use_custom_build=true +custom_template/plugins="HelloSignals" +command_line/extra_args="" +version/code=1 +version/name="1.0" +package/unique_name="fhuyakou.godot.plugin.android.hellosignals.demo" +package/name="" +package/signed=true +screen/immersive_mode=true +screen/orientation=0 +screen/support_small=true +screen/support_normal=true +screen/support_large=true +screen/support_xlarge=true +screen/opengl_debug=false +launcher_icons/main_192x192="" +launcher_icons/adaptive_foreground_432x432="" +launcher_icons/adaptive_background_432x432="" +keystore/debug="" +keystore/debug_user="" +keystore/debug_password="" +keystore/release="" +keystore/release_user="" +keystore/release_password="" +apk_expansion/enable=false +apk_expansion/SALT="" +apk_expansion/public_key="" +architectures/armeabi-v7a=true +architectures/arm64-v8a=true +architectures/x86=false +architectures/x86_64=false +permissions/custom_permissions=PoolStringArray( ) +permissions/access_checkin_properties=false +permissions/access_coarse_location=false +permissions/access_fine_location=false +permissions/access_location_extra_commands=false +permissions/access_mock_location=false +permissions/access_network_state=false +permissions/access_surface_flinger=false +permissions/access_wifi_state=false +permissions/account_manager=false +permissions/add_voicemail=false +permissions/authenticate_accounts=false +permissions/battery_stats=false +permissions/bind_accessibility_service=false +permissions/bind_appwidget=false +permissions/bind_device_admin=false +permissions/bind_input_method=false +permissions/bind_nfc_service=false +permissions/bind_notification_listener_service=false +permissions/bind_print_service=false +permissions/bind_remoteviews=false +permissions/bind_text_service=false +permissions/bind_vpn_service=false +permissions/bind_wallpaper=false +permissions/bluetooth=false +permissions/bluetooth_admin=false +permissions/bluetooth_privileged=false +permissions/brick=false +permissions/broadcast_package_removed=false +permissions/broadcast_sms=false +permissions/broadcast_sticky=false +permissions/broadcast_wap_push=false +permissions/call_phone=false +permissions/call_privileged=false +permissions/camera=false +permissions/capture_audio_output=false +permissions/capture_secure_video_output=false +permissions/capture_video_output=false +permissions/change_component_enabled_state=false +permissions/change_configuration=false +permissions/change_network_state=false +permissions/change_wifi_multicast_state=false +permissions/change_wifi_state=false +permissions/clear_app_cache=false +permissions/clear_app_user_data=false +permissions/control_location_updates=false +permissions/delete_cache_files=false +permissions/delete_packages=false +permissions/device_power=false +permissions/diagnostic=false +permissions/disable_keyguard=false +permissions/dump=false +permissions/expand_status_bar=false +permissions/factory_test=false +permissions/flashlight=false +permissions/force_back=false +permissions/get_accounts=false +permissions/get_package_size=false +permissions/get_tasks=false +permissions/get_top_activity_info=false +permissions/global_search=false +permissions/hardware_test=false +permissions/inject_events=false +permissions/install_location_provider=false +permissions/install_packages=false +permissions/install_shortcut=false +permissions/internal_system_window=false +permissions/internet=false +permissions/kill_background_processes=false +permissions/location_hardware=false +permissions/manage_accounts=false +permissions/manage_app_tokens=false +permissions/manage_documents=false +permissions/master_clear=false +permissions/media_content_control=false +permissions/modify_audio_settings=false +permissions/modify_phone_state=false +permissions/mount_format_filesystems=false +permissions/mount_unmount_filesystems=false +permissions/nfc=false +permissions/persistent_activity=false +permissions/process_outgoing_calls=false +permissions/read_calendar=false +permissions/read_call_log=false +permissions/read_contacts=false +permissions/read_external_storage=false +permissions/read_frame_buffer=false +permissions/read_history_bookmarks=false +permissions/read_input_state=false +permissions/read_logs=false +permissions/read_phone_state=false +permissions/read_profile=false +permissions/read_sms=false +permissions/read_social_stream=false +permissions/read_sync_settings=false +permissions/read_sync_stats=false +permissions/read_user_dictionary=false +permissions/reboot=false +permissions/receive_boot_completed=false +permissions/receive_mms=false +permissions/receive_sms=false +permissions/receive_wap_push=false +permissions/record_audio=false +permissions/reorder_tasks=false +permissions/restart_packages=false +permissions/send_respond_via_message=false +permissions/send_sms=false +permissions/set_activity_watcher=false +permissions/set_alarm=false +permissions/set_always_finish=false +permissions/set_animation_scale=false +permissions/set_debug_app=false +permissions/set_orientation=false +permissions/set_pointer_speed=false +permissions/set_preferred_applications=false +permissions/set_process_limit=false +permissions/set_time=false +permissions/set_time_zone=false +permissions/set_wallpaper=false +permissions/set_wallpaper_hints=false +permissions/signal_persistent_processes=false +permissions/status_bar=false +permissions/subscribed_feeds_read=false +permissions/subscribed_feeds_write=false +permissions/system_alert_window=false +permissions/transmit_ir=false +permissions/uninstall_shortcut=false +permissions/update_device_stats=false +permissions/use_credentials=false +permissions/use_sip=false +permissions/vibrate=false +permissions/wake_lock=false +permissions/write_apn_settings=false +permissions/write_calendar=false +permissions/write_call_log=false +permissions/write_contacts=false +permissions/write_external_storage=false +permissions/write_gservices=false +permissions/write_history_bookmarks=false +permissions/write_profile=false +permissions/write_secure_settings=false +permissions/write_settings=false +permissions/write_sms=false +permissions/write_social_stream=false +permissions/write_sync_settings=false +permissions/write_user_dictionary=false diff --git a/hello_signals/demo/icon.png b/hello_signals/demo/icon.png Binary files differnew file mode 100644 index 0000000..c98fbb6 --- /dev/null +++ b/hello_signals/demo/icon.png diff --git a/hello_signals/demo/project.godot b/hello_signals/demo/project.godot new file mode 100644 index 0000000..b0283f4 --- /dev/null +++ b/hello_signals/demo/project.godot @@ -0,0 +1,27 @@ +; Engine configuration file. +; It's best edited using the editor UI and not directly, +; since the parameters that go here are not all obvious. +; +; Format: +; [section] ; section goes between [] +; param=value ; assign values to parameters + +config_version=4 + +_global_script_classes=[ ] +_global_script_class_icons={ + +} + +[application] + +config/name="Hello Signals Plugin Demo" +run/main_scene="res://Main.tscn" +config/icon="res://icon.png" + +[rendering] + +quality/driver/driver_name="GLES2" +vram_compression/import_etc=true +vram_compression/import_etc2=false +environment/default_environment="res://default_env.tres" diff --git a/hello_signals/src/main/AndroidManifest.xml b/hello_signals/src/main/AndroidManifest.xml new file mode 100644 index 0000000..8f791bd --- /dev/null +++ b/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/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 diff --git a/libs/godot-lib.release.aar b/libs/godot-lib.release.aar Binary files differindex 4762e4f..63102ec 100644 --- a/libs/godot-lib.release.aar +++ b/libs/godot-lib.release.aar diff --git a/settings.gradle b/settings.gradle index 212b118..153a4a9 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,3 +1,4 @@ rootProject.name='Godot Android Plugins' include ':hello_world' +include ':hello_signals' |