diff --git a/addons/SharePlugin/Share.gd b/addons/SharePlugin/Share.gd new file mode 100644 index 0000000..3ceb7db --- /dev/null +++ b/addons/SharePlugin/Share.gd @@ -0,0 +1,80 @@ +# +# © 2024-present https://github.com/cengiz-pz +# + +@tool +class_name Share +extends Node + +const PLUGIN_SINGLETON_NAME: String = "SharePlugin" +const MIME_TYPE_TEXT: String = "text/plain" +const MIME_TYPE_IMAGE: String = "image/*" + +@onready var _temp_image_path: String = OS.get_user_data_dir() + "/tmp_share_img_path.png" + +var _plugin_singleton: Object + + +func _ready() -> void: + if OS.get_name() == "Android": + _update_plugin() + + +func _notification(a_what: int) -> void: + if a_what == NOTIFICATION_APPLICATION_RESUMED: + _update_plugin() + + +func _update_plugin() -> void: + if _plugin_singleton == null: + if Engine.has_singleton(PLUGIN_SINGLETON_NAME): + _plugin_singleton = Engine.get_singleton(PLUGIN_SINGLETON_NAME) + else: + printerr("%s singleton not found!" % PLUGIN_SINGLETON_NAME) + + +func share_text(a_title: String, a_subject: String, a_content: String) -> void: + if _plugin_singleton != null: + _plugin_singleton.share( + SharedData.new() + .set_title(a_title) + .set_subject(a_subject) + .set_content(a_content) + .set_mime_type(MIME_TYPE_TEXT) + .get_raw_data() + ) + else: + printerr("%s plugin not initialized" % PLUGIN_SINGLETON_NAME) + + +func share_image(a_path: String, a_title: String, a_subject: String, a_content: String) -> void: + share_file(a_path, MIME_TYPE_IMAGE, a_title, a_subject, a_content) + + +func share_texture(a_texture: Texture2D, a_title: String, a_subject: String, a_content: String) -> void: + var __image: Image = a_texture.get_image() + __image.save_png(_temp_image_path) + share_file(_temp_image_path, MIME_TYPE_IMAGE, a_title, a_subject, a_content) + + +func share_viewport(a_viewport: Viewport, a_title: String, a_subject: String, a_content: String, a_flip_y: bool = false) -> void: + var __image: Image = a_viewport.get_texture().get_image() + if a_flip_y: + __image.flip_y() + __image.save_png(_temp_image_path) + share_file(_temp_image_path, MIME_TYPE_IMAGE, a_title, a_subject, a_content) + + +func share_file(a_path: String, a_mime_type: String, a_title: String, a_subject: String, a_content: String) -> void: + if _plugin_singleton != null: + _plugin_singleton.share( + SharedData.new() + .set_title(a_title) + .set_subject(a_subject) + .set_content(a_content) + .set_mime_type(a_mime_type) + .set_file_path(a_path) + .get_raw_data() + ) + else: + printerr("%s plugin not initialized" % PLUGIN_SINGLETON_NAME) diff --git a/addons/SharePlugin/ShareExportPlugin.gd b/addons/SharePlugin/ShareExportPlugin.gd new file mode 100644 index 0000000..2b414bb --- /dev/null +++ b/addons/SharePlugin/ShareExportPlugin.gd @@ -0,0 +1,65 @@ +# +# © 2024-present https://github.com/cengiz-pz +# + +@tool +extends EditorPlugin + +const PLUGIN_NODE_TYPE_NAME = "Share" +const PLUGIN_PARENT_NODE_TYPE = "Node" +const PLUGIN_NAME: String = "SharePlugin" +const PLUGIN_VERSION: String = "3.0" +const PLUGIN_PACKAGE: String = "org.godotengine.plugin.android.share" +const PLUGIN_DEPENDENCIES: Array = [ "androidx.appcompat:appcompat:1.7.0" ] + +const PROVIDER_TAG = """ + + + +""" + +var export_plugin: AndroidExportPlugin + + +func _enter_tree() -> void: + add_custom_type(PLUGIN_NODE_TYPE_NAME, PLUGIN_PARENT_NODE_TYPE, preload("Share.gd"), preload("icon.png")) + export_plugin = AndroidExportPlugin.new() + add_export_plugin(export_plugin) + + +func _exit_tree() -> void: + remove_custom_type(PLUGIN_NODE_TYPE_NAME) + remove_export_plugin(export_plugin) + export_plugin = null + + +class AndroidExportPlugin extends EditorExportPlugin: + var _plugin_name = PLUGIN_NAME + + + func _supports_platform(platform: EditorExportPlatform) -> bool: + if platform is EditorExportPlatformAndroid: + return true + return false + + + func _get_android_libraries(platform: EditorExportPlatform, debug: bool) -> PackedStringArray: + if debug: + return PackedStringArray(["%s/bin/debug/%s-%s-debug.aar" % [_plugin_name, _plugin_name, PLUGIN_VERSION]]) + else: + return PackedStringArray(["%s/bin/release/%s-%s-release.aar" % [_plugin_name, _plugin_name, PLUGIN_VERSION]]) + + + func _get_name() -> String: + return _plugin_name + + + func _get_android_dependencies(platform: EditorExportPlatform, debug: bool) -> PackedStringArray: + return PackedStringArray(PLUGIN_DEPENDENCIES) + + + func _get_android_manifest_application_element_contents(platform: EditorExportPlatform, debug: bool) -> String: + return PROVIDER_TAG % [PLUGIN_PACKAGE, get_option("package/unique_name")] diff --git a/addons/SharePlugin/bin/debug/SharePlugin-3.0-debug.aar b/addons/SharePlugin/bin/debug/SharePlugin-3.0-debug.aar new file mode 100644 index 0000000..810c4bb Binary files /dev/null and b/addons/SharePlugin/bin/debug/SharePlugin-3.0-debug.aar differ diff --git a/addons/SharePlugin/bin/release/SharePlugin-3.0-release.aar b/addons/SharePlugin/bin/release/SharePlugin-3.0-release.aar new file mode 100644 index 0000000..1f68b69 Binary files /dev/null and b/addons/SharePlugin/bin/release/SharePlugin-3.0-release.aar differ diff --git a/addons/SharePlugin/icon.png b/addons/SharePlugin/icon.png new file mode 100644 index 0000000..962cffa --- /dev/null +++ b/addons/SharePlugin/icon.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e1a6f8c11c4b3db19790929eedcb84e9726ed397940b51329edcac880c0bc1fd +size 10697 diff --git a/addons/SharePlugin/icon.png.import b/addons/SharePlugin/icon.png.import new file mode 100644 index 0000000..b5634d8 --- /dev/null +++ b/addons/SharePlugin/icon.png.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://cm4eejjdmpyec" +path="res://.godot/imported/icon.png-cbd45f0ce843eb69e82e7474c8559974.ctex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://addons/SharePlugin/icon.png" +dest_files=["res://.godot/imported/icon.png-cbd45f0ce843eb69e82e7474c8559974.ctex"] + +[params] + +compress/mode=0 +compress/high_quality=false +compress/lossy_quality=0.7 +compress/hdr_compression=1 +compress/normal_map=0 +compress/channel_pack=0 +mipmaps/generate=false +mipmaps/limit=-1 +roughness/mode=0 +roughness/src_normal="" +process/fix_alpha_border=true +process/premult_alpha=false +process/normal_map_invert_y=false +process/hdr_as_srgb=false +process/hdr_clamp_exposure=false +process/size_limit=0 +detect_3d/compress_to=1 diff --git a/addons/SharePlugin/model/SharedData.gd b/addons/SharePlugin/model/SharedData.gd new file mode 100644 index 0000000..6ca58f7 --- /dev/null +++ b/addons/SharePlugin/model/SharedData.gd @@ -0,0 +1,47 @@ +# +# © 2024-present https://github.com/cengiz-pz +# + +class_name SharedData +extends RefCounted + +const DATA_KEY_TITLE = "title" +const DATA_KEY_SUBJECT = "subject" +const DATA_KEY_CONTENT = "content" +const DATA_KEY_FILE_PATH = "file_path" +const DATA_KEY_MIME_TYPE = "mime_type" + +var _data: Dictionary + + +func _init() -> void: + _data = {} + + +func set_title(a_title: String) -> SharedData: + _data[DATA_KEY_TITLE] = a_title + return self + + +func set_subject(a_subject: String) -> SharedData: + _data[DATA_KEY_SUBJECT] = a_subject + return self + + +func set_content(a_content: String) -> SharedData: + _data[DATA_KEY_CONTENT] = a_content + return self + + +func set_file_path(a_file_path: String) -> SharedData: + _data[DATA_KEY_FILE_PATH] = a_file_path + return self + + +func set_mime_type(a_mime_type: String) -> SharedData: + _data[DATA_KEY_MIME_TYPE] = a_mime_type + return self + + +func get_raw_data() -> Dictionary: + return _data diff --git a/addons/SharePlugin/plugin.cfg b/addons/SharePlugin/plugin.cfg new file mode 100644 index 0000000..ccfe71f --- /dev/null +++ b/addons/SharePlugin/plugin.cfg @@ -0,0 +1,10 @@ +; +; © 2024-present https://github.com/cengiz-pz +; +[plugin] + +name="Share" +description="Allow sharing of text or images with other apps" +author="Cengiz" +version="3.0" +script="ShareExportPlugin.gd"