no message

This commit is contained in:
gem
2025-02-21 14:40:32 +08:00
parent d7699c93cd
commit 40409fd882
13 changed files with 190 additions and 145 deletions

View File

@@ -6,7 +6,6 @@
//
import Foundation
import cocos_view_pod
private var cocos_warmed_up = false
@@ -37,7 +36,7 @@ public func InitCocosIntegrationWithOptions(
/***********************************PLUGIN_ENTRY END**************************************/
// Load cocos framework for fisrt run
func CocosFrameworkLoad() -> CocosFramework? {
func CocosFrameworkLoad() -> dcocos_bridge? {
return dcocos_bridge.instance()
@@ -58,7 +57,7 @@ func GetCocosPlayerUtils() -> CocosPlayerUtils {
/*********************************** GLOBAL FUNCS & VARS END****************************************/
var controller: CocosAppController?
//var controller: CocosAppController?
var sharedApplication: UIApplication?
@objc protocol CocosEventListener: AnyObject {
@@ -67,31 +66,74 @@ var sharedApplication: UIApplication?
}
@objc public class CocosPlayerUtils: UIResponder, UIApplicationDelegate, CocosFrameworkListener {
var ufw: CocosFramework!
@objc public class CocosPlayerUtils: UIResponder, UIApplicationDelegate {
var ufw: dcocos_bridge!
private var _isCocosPaused = false
private var _isCocosReady = false
private var _isCocosLoaded = false
func initCocos() {
if (self.cocosIsInitiallized()) {
self.ufw?.showCocosWindow()
override init() {
super.init()
//
NotificationCenter.default.addObserver(self, selector: #selector(handleViewWillTransition), name: NSNotification.Name("ViewWillTransition"), object: nil)
}
deinit {
//
NotificationCenter.default.removeObserver(self)
}
@objc func handleViewWillTransition(notification: Notification) {
guard let size = notification.userInfo?["size"] as? CGSize,
let coordinator = notification.userInfo?["coordinator"] as? UIViewControllerTransitionCoordinator else {
return
}
//
print("CocosPlayerUtils will transition to size: \(size)")
//
let selector = NSSelectorFromString("viewWillTransitionToSize:withTransitionCoordinator:")
if self.ufw.responds(to: selector) {
self.ufw.perform(selector, with: size, with: coordinator)
}
}
func initCocos() {
if (self.cocosIsInitiallized()) {
//todo
//self.ufw?.showCocosWindow()
return
}
//
//UIDevice.current.beginGeneratingDeviceOrientationNotifications()
self.ufw = CocosFrameworkLoad()
// PodBundle
let bundle = Bundle(for: dcocos_bridge.self)
// `YourPodResources.bundle`
if let bundleURL = bundle.url(forResource: "cocos_main_bundle", withExtension: "bundle") {
let bundlePath = bundleURL.path
print("Bundle absolute path: \(bundlePath)")
self.ufw.configureCocos2dSearchPaths(bundlePath)
} else {
print("Could not find the bundle.")
}
//self.ufw?.setDataBundleId("com.cocos3d.framework")
self.ufw.initPlatform()
let application = UIApplication.shared
self.ufw.application(application, didFinishLaunchingWithOptions: appLaunchOpts )
registerCocosListener()
// self.ufw?.runEmbedded(withArgc: gArgc, argv: gArgv, appLaunchOpts: appLaunchOpts)
if self.ufw?.appController() != nil {
controller = self.ufw?.appController()
controller?.cocosMessageHandler = self.cocosMessageHandlers
controller?.cocosSceneLoadedHandler = self.cocosSceneLoadedHandlers
self.ufw?.appController()?.window?.windowLevel = UIWindow.Level(UIWindow.Level.normal.rawValue - 1)
}
// if self.ufw?.appController() != nil {
// controller = self.ufw?.appController()
// controller?.cocosMessageHandler = self.cocosMessageHandlers
// controller?.cocosSceneLoadedHandler = self.cocosSceneLoadedHandlers
// self.ufw?.appController()?.window?.windowLevel = UIWindow.Level(UIWindow.Level.normal.rawValue - 1)
// }
_isCocosLoaded = true
}
@@ -107,13 +149,18 @@ var sharedApplication: UIApplication?
// Create new cocos player
func createPlayer(completed: @escaping (_ view: UIView?) -> Void) {
if self.cocosIsInitiallized() && self._isCocosReady {
completed(controller?.rootView)
//todo
//completed(controller?.rootView)
completed(nil)
return
}
NotificationCenter.default.addObserver(forName: NSNotification.Name("CocosReady"), object: nil, queue: OperationQueue.main, using: { note in
self._isCocosReady = true
completed(controller?.rootView)
//todo
//completed(controller?.rootView)
completed(nil)
})
DispatchQueue.main.async {
@@ -134,7 +181,9 @@ var sharedApplication: UIApplication?
self.listenAppState()
completed(controller?.rootView)
//todo
//completed(controller?.rootView)
completed(nil)
}
}
@@ -163,29 +212,30 @@ var sharedApplication: UIApplication?
if !self._isCocosReady {
return
}
let cocosAppController = self.ufw?.appController() as? CocosAppController
//todo
//let cocosAppController = self.ufw?.appController() as? CocosAppController
// let cocosAppController = nil
let application = UIApplication.shared
if notification?.name == UIApplication.willResignActiveNotification {
cocosAppController?.applicationWillResignActive(application)
self.ufw?.applicationWillResignActive(application)
} else if notification?.name == UIApplication.didEnterBackgroundNotification {
cocosAppController?.applicationDidEnterBackground(application)
// self.ufw?.applicationDidEnterBackground(application)
} else if notification?.name == UIApplication.willEnterForegroundNotification {
cocosAppController?.applicationWillEnterForeground(application)
// self.ufw?.applicationWillEnterForeground(application)
} else if notification?.name == UIApplication.didBecomeActiveNotification {
cocosAppController?.applicationDidBecomeActive(application)
self.ufw?.applicationDidBecomeActive(application)
} else if notification?.name == UIApplication.willTerminateNotification {
cocosAppController?.applicationWillTerminate(application)
self.ufw?.applicationWillTerminate(application)
} else if notification?.name == UIApplication.didReceiveMemoryWarningNotification {
cocosAppController?.applicationDidReceiveMemoryWarning(application)
}
// self.ufw?.applicationDidReceiveMemoryWarning(application)
}
}
// Listener for app lifecycle eventa
func listenAppState() {
for name in [
UIApplication.didBecomeActiveNotification,
UIApplication.didEnterBackgroundNotification,
UIApplication.willTerminateNotification,
@@ -202,19 +252,22 @@ var sharedApplication: UIApplication?
}
// Pause cocos player
func pause() {
self.ufw?.pause(true)
//todo
// self.ufw?.pause(true)
self._isCocosPaused = true
}
// Resume cocos player
func resume() {
self.ufw?.pause(false)
//todo
//self.ufw?.pause(false)
self._isCocosPaused = false
}
// Unoad cocos player
func unload() {
self.ufw?.unloadApplication()
//todo
//self.ufw?.unloadApplication()
}
func isCocosLoaded() -> Bool {
@@ -227,14 +280,16 @@ var sharedApplication: UIApplication?
// Quit cocos player application
func quit() {
self.ufw?.quitApplication(0)
//todo
//self.ufw?.quitApplication(0)
self._isCocosLoaded = false
}
// Post message to cocos
func postMessageToCocos(gameObject: String?, cocosMethodName: String?, cocosMessage: String?) {
if self.cocosIsInitiallized() {
self.ufw?.sendMessageToGO(withName: gameObject, functionName: cocosMethodName, message: cocosMessage)
//todo
//self.ufw?.sendMessageToGO(withName: gameObject, functionName: cocosMethodName, message: cocosMessage)
}
}

View File

@@ -13,7 +13,8 @@ class FLTCocosView: UIView {
override func layoutSubviews() {
super.layoutSubviews()
if (!self.bounds.isEmpty) {
GetCocosPlayerUtils().ufw?.appController()?.rootView.frame = self.bounds
//todo
GetCocosPlayerUtils().ufw?.getCocosView()?.frame = self.bounds
}
}
}

View File

@@ -23,9 +23,11 @@ public class FLTCocosWidgetController: NSObject, FLTCocosOptionsSink, FlutterPla
arguments args: Any?,
registrar: NSObjectProtocol & FlutterPluginRegistrar
) {
//CGRect(x: 0, y: 0, width: 700, height: 700)
self._rootView = FLTCocosView(frame: frame)
super.init()
globalControllers.append(self)
self.viewId = viewId
@@ -38,6 +40,8 @@ public class FLTCocosWidgetController: NSObject, FLTCocosOptionsSink, FlutterPla
}
func methodHandler(_ call: FlutterMethodCall, result: FlutterResult) {
NSLog("call : \(call.method)")
if call.method == "cocos#dispose" {
self.dispose()
result(nil)
@@ -87,33 +91,35 @@ public class FLTCocosWidgetController: NSObject, FLTCocosOptionsSink, FlutterPla
private func startCocosIfNeeded() {
GetCocosPlayerUtils().createPlayer(completed: { [self] (view: UIView?) in
})
}
func attachView() {
startCocosIfNeeded()
//GetCocosPlayerUtils().ufw?.setCocosView(_rootView)
// self.channel?.invokeMethod("events#onViewReattached", arguments: "")
let cocosView = GetCocosPlayerUtils().ufw?.getCocosView()
if let superview = cocosView?.superview {
cocosView?.removeFromSuperview()
superview.layoutIfNeeded()
}
let cocosView = GetCocosPlayerUtils().ufw?.appController()?.rootView
if let superview = cocosView?.superview {
cocosView?.removeFromSuperview()
superview.layoutIfNeeded()
}
if let cocosView = cocosView {
_rootView.addSubview(cocosView)
_rootView.layoutIfNeeded()
self.channel?.invokeMethod("events#onViewReattached", arguments: "")
}
if let cocosView = cocosView {
_rootView.addSubview(cocosView)
_rootView.layoutIfNeeded()
self.channel?.invokeMethod("events#onViewReattached", arguments: "")
}
GetCocosPlayerUtils().resume()
}
func reattachView() {
let cocosView = GetCocosPlayerUtils().ufw?.appController()?.rootView
//todo
let cocosView = GetCocosPlayerUtils().ufw?.getCocosView()
let superview = cocosView?.superview
if superview != _rootView {
attachView()
}
}
GetCocosPlayerUtils().resume()
}
@@ -123,7 +129,7 @@ public class FLTCocosWidgetController: NSObject, FLTCocosOptionsSink, FlutterPla
return
}
let cocosView = GetCocosPlayerUtils().ufw?.appController()?.rootView
let cocosView = GetCocosPlayerUtils().ufw?.getCocosView()
if _rootView == cocosView?.superview {
if globalControllers.isEmpty {
cocosView?.removeFromSuperview()

View File

@@ -1,11 +1,10 @@
import Flutter
import UIKit
public class FlutterCocosViewPlugin: NSObject, FlutterPlugin {
public class SwiftFlutterCocosViewPlugin: NSObject, FlutterPlugin {
public static func register(with registrar: FlutterPluginRegistrar) {
let channel = FlutterMethodChannel(name: "flutter_cocos_view", binaryMessenger: registrar.messenger())
let instance = FlutterCocosViewPlugin()
registrar.addMethodCallDelegate(instance, channel: channel)
print("FlutterCocosViewPlugin registered~~~~~~~~~~~~~~") //
let fuwFactory = FLTCocosWidgetFactory(registrar: registrar)
registrar.register(fuwFactory, withId: "plugin.gem.com/cocos_view", gestureRecognizersBlockingPolicy: FlutterPlatformViewGestureRecognizersBlockingPolicyWaitUntilTouchesEnded)

View File

@@ -1,12 +1,12 @@
#import FlutterCocosWidgetPlugin.h
#import "FlutterCocosWidgetPlugin.h"
#import <Foundation/Foundation.h>
#if __has_include(<flutter_cocos_widget/flutter_cocos_widget-Swift.h>)
#import <flutter_cocos_widget/flutter_cocos_widget-Swift.h>
#if __has_include(<flutter_cocos_view/flutter_cocos_-Swift.h>)
#import <flutter_cocos_view/flutter_cocos_view-Swift.h>
#else
// Support project import fallback if the generated compatibility header
// is not copied when this plugin is created as a library.
// https://forums.swift.org/t/swift-static-libraries-dont-copy-generated-objective-c-header/19816
#import "flutter_cocos_widget-Swift.h"
#import <flutter_cocos_view/flutter_cocos_view-Swift.h>
#endif
@implementation FlutterCocosWidgetPlugin {
@@ -16,7 +16,7 @@
}
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
[SwiftFlutterCocosWidgetPlugin registerWithRegistrar:registrar];
[SwiftFlutterCocosViewPlugin registerWithRegistrar:registrar];
}
@end