diff --git a/modules/share/SCsub b/modules/share/SCsub new file mode 100644 index 000000000000..965a3562b12e --- /dev/null +++ b/modules/share/SCsub @@ -0,0 +1,6 @@ +Import("env") + +sources = ["register_types.cpp", "godot_share.mm"] + +if env["platform"] in ["ios"]: + env.add_source_files(env.modules_sources, sources) diff --git a/modules/share/config.py b/modules/share/config.py new file mode 100644 index 000000000000..88000ddeebc3 --- /dev/null +++ b/modules/share/config.py @@ -0,0 +1,8 @@ +def can_build(env, platform): + return env["platform"] in ["ios"] + + +def configure(env): + if env["platform"] in ["ios"]: + env.Append(LINKFLAGS=["-ObjC"]) + env.Append(CPPPATH=["#core"]) diff --git a/modules/share/godot_share.h b/modules/share/godot_share.h new file mode 100644 index 000000000000..4ccb01637402 --- /dev/null +++ b/modules/share/godot_share.h @@ -0,0 +1,52 @@ +/**************************************************************************/ +/* godot_share.h */ +/**************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/**************************************************************************/ +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/**************************************************************************/ + +#ifndef GODOT_SHARE_H +#define GODOT_SHARE_H + +#include "core/object/ref_counted.h" + +class GodotShare : public RefCounted { + GDCLASS(GodotShare, RefCounted); + +protected: + static void _bind_methods(); + + static GodotShare *singleton; + +public: + void share(const String &p_text, const String &p_subject, const String &p_title, const String &p_path); + void rate(); + + GodotShare(); + ~GodotShare(); +}; + +#endif // GODOT_SHARE_H diff --git a/modules/share/godot_share.mm b/modules/share/godot_share.mm new file mode 100644 index 000000000000..afaad07dd4e4 --- /dev/null +++ b/modules/share/godot_share.mm @@ -0,0 +1,84 @@ +/**************************************************************************/ +/* godot_share.mm */ +/**************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/**************************************************************************/ +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/**************************************************************************/ + +#include "godot_share.h" + +#import "app_delegate.h" + +#import + +GodotShare *GodotShare::singleton = nullptr; + +GodotShare::GodotShare() { + ERR_FAIL_COND(singleton != nullptr); + singleton = this; +} + +GodotShare::~GodotShare() { + singleton = nullptr; +} + +void GodotShare::share(const String &p_text, const String &p_subject, const String &p_title, const String &p_path) { + UIViewController *root_controller = [[UIApplication sharedApplication] delegate].window.rootViewController; + + NSString *ns_text = [NSString stringWithCString:p_text.utf8().get_data() encoding:NSUTF8StringEncoding]; + NSMutableArray *share_items = [@[ ns_text ] mutableCopy]; + if (p_path.is_empty() == false) { + NSString *image_path = [NSString stringWithCString:p_path.utf8().get_data() encoding:NSUTF8StringEncoding]; + UIImage *ui_image = [UIImage imageWithContentsOfFile:image_path]; + [share_items addObject:ui_image]; + } + + UIActivityViewController *avc = [[UIActivityViewController alloc] initWithActivityItems:share_items applicationActivities:nil]; + if (p_subject.is_empty() == false) { + NSString *ns_subject = [NSString stringWithCString:p_subject.utf8().get_data() encoding:NSUTF8StringEncoding]; + [avc setValue:ns_subject forKey:@"subject"]; + } + //if iPad + if (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPhone) { + // Change Rect to position Popover + avc.modalPresentationStyle = UIModalPresentationPopover; + avc.popoverPresentationController.sourceView = root_controller.view; + avc.popoverPresentationController.sourceRect = CGRectMake(CGRectGetMidX(root_controller.view.bounds), CGRectGetMidY(root_controller.view.bounds), 0, 0); + avc.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirection(0); + } + [root_controller presentViewController:avc animated:YES completion:nil]; +} + +void GodotShare::rate() { + if (@available(iOS 10.3, *)) { + [SKStoreReviewController requestReview]; + } +} + +void GodotShare::_bind_methods() { + ClassDB::bind_method(D_METHOD("share", "text", "subject", "title", "path"), &GodotShare::share); + ClassDB::bind_method(D_METHOD("rate"), &GodotShare::rate); +} diff --git a/modules/share/register_types.cpp b/modules/share/register_types.cpp new file mode 100644 index 000000000000..95ca9bebca36 --- /dev/null +++ b/modules/share/register_types.cpp @@ -0,0 +1,45 @@ +/**************************************************************************/ +/* register_types.cpp */ +/**************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/**************************************************************************/ +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/**************************************************************************/ + +#include "register_types.h" + +#include "core/config/engine.h" + +#include "godot_share.h" + +void initialize_share_module(ModuleInitializationLevel p_level) { + if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) { + return; + } + Engine::get_singleton()->add_singleton(Engine::Singleton("GodotShare", memnew(GodotShare))); +} + +void uninitialize_share_module(ModuleInitializationLevel p_level) { +} diff --git a/modules/share/register_types.h b/modules/share/register_types.h new file mode 100644 index 000000000000..14c2f10383e0 --- /dev/null +++ b/modules/share/register_types.h @@ -0,0 +1,39 @@ +/**************************************************************************/ +/* register_types.h */ +/**************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/**************************************************************************/ +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/**************************************************************************/ + +#ifndef SHARE_REGISTER_TYPES_H +#define SHARE_REGISTER_TYPES_H + +#include "modules/register_module_types.h" + +void initialize_share_module(ModuleInitializationLevel p_level); +void uninitialize_share_module(ModuleInitializationLevel p_level); + +#endif // SHARE_REGISTER_TYPES_H