Skip to content

Commit

Permalink
share text and rate app
Browse files Browse the repository at this point in the history
  • Loading branch information
theromis committed Apr 29, 2024
1 parent beb798d commit 69d4e5e
Show file tree
Hide file tree
Showing 6 changed files with 234 additions and 0 deletions.
6 changes: 6 additions & 0 deletions modules/share/SCsub
Original file line number Diff line number Diff line change
@@ -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)
8 changes: 8 additions & 0 deletions modules/share/config.py
Original file line number Diff line number Diff line change
@@ -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"])
52 changes: 52 additions & 0 deletions modules/share/godot_share.h
Original file line number Diff line number Diff line change
@@ -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
84 changes: 84 additions & 0 deletions modules/share/godot_share.mm
Original file line number Diff line number Diff line change
@@ -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 <StoreKit/StoreKit.h>

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);
}
45 changes: 45 additions & 0 deletions modules/share/register_types.cpp
Original file line number Diff line number Diff line change
@@ -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) {
}
39 changes: 39 additions & 0 deletions modules/share/register_types.h
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 69d4e5e

Please sign in to comment.