diff --git a/example/lib/main.dart b/example/lib/main.dart index fc76aed..b09fc87 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -1,63 +1,37 @@ import 'package:flutter/material.dart'; -import 'dart:async'; +import 'package:flutter_cocos_view_example/screens/no_interaction_screen.dart'; +import 'package:flutter_cocos_view_example/screens/orientation_screen.dart'; -import 'package:flutter/services.dart'; -import 'package:flutter_cocos_view/flutter_cocos_view.dart'; +import 'menu_screen.dart'; +import 'screens/api_screen.dart'; +import 'screens/loader_screen.dart'; +import 'screens/simple_screen.dart'; void main() { runApp(const MyApp()); } -class MyApp extends StatefulWidget { - const MyApp({super.key}); - - @override - State createState() => _MyAppState(); -} - -class _MyAppState extends State { - String _platformVersion = 'Unknown'; - final _flutterCocosViewPlugin = FlutterCocosView(); - - @override - void initState() { - super.initState(); - initPlatformState(); - } - - // Platform messages are asynchronous, so we initialize in an async method. - Future initPlatformState() async { - String platformVersion; - // Platform messages may fail, so we use a try/catch PlatformException. - // We also handle the message potentially returning null. - try { - platformVersion = - await _flutterCocosViewPlugin.getPlatformVersion() ?? 'Unknown platform version'; - } on PlatformException { - platformVersion = 'Failed to get platform version.'; - } - - // If the widget was removed from the tree while the asynchronous platform - // message was in flight, we want to discard the reply rather than calling - // setState to update our non-existent appearance. - if (!mounted) return; - - setState(() { - _platformVersion = platformVersion; - }); - } +class MyApp extends StatelessWidget { + const MyApp({Key? key}) : super(key: key); + // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( - home: Scaffold( - appBar: AppBar( - title: const Text('Plugin example app'), - ), - body: Center( - child: Text('Running on: $_platformVersion\n'), - ), + title: 'Flutter Cocos Demo', + theme: ThemeData( + primarySwatch: Colors.blue, + visualDensity: VisualDensity.adaptivePlatformDensity, ), + initialRoute: '/', + routes: { + '/': (context) => const MenuScreen(), + '/simple': (context) => const SimpleScreen(), + '/loader': (context) => const LoaderScreen(), + '/orientation': (context) => const OrientationScreen(), + '/api': (context) => const ApiScreen(), + '/none': (context) => const NoInteractionScreen(), + }, ); } } diff --git a/example/lib/menu_screen.dart b/example/lib/menu_screen.dart new file mode 100644 index 0000000..debf9d3 --- /dev/null +++ b/example/lib/menu_screen.dart @@ -0,0 +1,76 @@ +import 'package:flutter/material.dart'; + +class MenuScreen extends StatefulWidget { + const MenuScreen({Key? key}) : super(key: key); + + @override + State createState() => _MenuScreenState(); +} + +class _MenuScreenState extends State { + List<_MenuListItem> menus = [ + _MenuListItem( + description: 'Simple demonstration of cocos flutter library', + route: '/simple', + title: 'Simple Cocos Demo', + ), + _MenuListItem( + description: 'No interaction of cocos flutter library', + route: '/none', + title: 'No Interaction Cocos Demo', + ), + _MenuListItem( + description: 'Cocos load and unload cocos demo', + route: '/loader', + title: 'Safe mode Demo', + ), + _MenuListItem( + description: + 'This example shows various native API exposed by the library', + route: '/api', + title: 'Native exposed API demo', + ), + _MenuListItem( + title: 'Test Orientation', + route: '/orientation', + description: 'test orientation change', + ), + ]; + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: const Text('Menu List'), + ), + body: Center( + child: ListView.builder( + itemCount: menus.length, + itemBuilder: (BuildContext context, int i) { + return ListTile( + title: Text(menus[i].title), + subtitle: Text(menus[i].description), + onTap: () { + Navigator.of(context).pushNamed( + menus[i].route, + ); + }, + ); + }, + ), + ), + ); + } +} + +class _MenuListItem { + final String title; + final String description; + final String route; + + _MenuListItem({ + required this.title, + required this.description, + required this.route, + }); +} diff --git a/example/lib/screens/api_screen.dart b/example/lib/screens/api_screen.dart new file mode 100644 index 0000000..e9f82be --- /dev/null +++ b/example/lib/screens/api_screen.dart @@ -0,0 +1,168 @@ +// ignore_for_file: avoid_print + +import 'package:flutter/material.dart'; +import "package:flutter_cocos_view/flutter_cocos_view.dart"; +import 'package:pointer_interceptor/pointer_interceptor.dart'; + +class ApiScreen extends StatefulWidget { + const ApiScreen({Key? key}) : super(key: key); + + @override + State createState() => _ApiScreenState(); +} + +class _ApiScreenState extends State { + CocosWidgetController? _cocosWidgetController; + double _sliderValue = 0.0; + + @override + void initState() { + super.initState(); + } + + @override + void dispose() { + _cocosWidgetController?.dispose(); + super.dispose(); + } + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: const Text('API Screen'), + ), + body: Card( + margin: const EdgeInsets.all(8), + clipBehavior: Clip.antiAlias, + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(20.0), + ), + child: Stack( + children: [ + CocosWidget( + onCocosCreated: onCocosCreated, + onCocosMessage: onCocosMessage, + onCocosSceneLoaded: onCocosSceneLoaded, + fullscreen: false, + useAndroidViewSurface: false, + ), + Positioned( + bottom: 20, + left: 20, + right: 20, + child: PointerInterceptor( + child: Card( + elevation: 10, + child: Column( + mainAxisSize: MainAxisSize.min, + children: [ + const Padding( + padding: EdgeInsets.only(top: 20), + child: Text("Rotation speed:"), + ), + Slider( + onChanged: (value) { + setState(() { + _sliderValue = value; + }); + setRotationSpeed(value.toString()); + }, + value: _sliderValue, + min: 0, + max: 20, + ), + FittedBox( + child: Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + MaterialButton( + onPressed: () { + _cocosWidgetController?.quit(); + }, + child: const Text("Quit"), + ), + MaterialButton( + onPressed: () { + _cocosWidgetController?.create(); + }, + child: const Text("Create"), + ), + MaterialButton( + onPressed: () { + _cocosWidgetController?.pause(); + }, + child: const Text("Pause"), + ), + MaterialButton( + onPressed: () { + _cocosWidgetController?.resume(); + }, + child: const Text("Resume"), + ), + ], + ), + ), + FittedBox( + child: Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + MaterialButton( + onPressed: () async { + await _cocosWidgetController + ?.openInNativeProcess(); + }, + child: const Text("Open Native"), + ), + MaterialButton( + onPressed: () { + _cocosWidgetController?.unload(); + }, + child: const Text("Unload"), + ), + MaterialButton( + onPressed: () { + _cocosWidgetController?.quit(); + }, + child: const Text("Silent Quit"), + ), + ], + ), + ), + ], + ), + ), + ), + ), + ], + ), + ), + ); + } + + void setRotationSpeed(String speed) { + _cocosWidgetController?.postMessage( + 'Cube', + 'SetRotationSpeed', + speed, + ); + } + + void onCocosMessage(message) { + print('Received message from cocos: ${message.toString()}'); + } + + void onCocosSceneLoaded(SceneLoaded? scene) { + if (scene != null) { + print('Received scene loaded from cocos: ${scene.name}'); + print('Received scene loaded from cocos buildIndex: ${scene.buildIndex}'); + } else { + print('Received scene loaded from cocos: null'); + } + } + + // Callback that connects the created controller to the cocos controller + void onCocosCreated(controller) { + _cocosWidgetController = controller; + } +} diff --git a/example/lib/screens/loader_screen.dart b/example/lib/screens/loader_screen.dart new file mode 100644 index 0000000..09c201d --- /dev/null +++ b/example/lib/screens/loader_screen.dart @@ -0,0 +1,93 @@ +// ignore_for_file: avoid_print + +import 'package:flutter/material.dart'; +import 'package:flutter_cocos_view/flutter_cocos_view.dart'; +import 'package:pointer_interceptor/pointer_interceptor.dart'; + +class LoaderScreen extends StatefulWidget { + const LoaderScreen({Key? key}) : super(key: key); + + @override + State createState() => _LoaderScreenState(); +} + +class _LoaderScreenState extends State { + CocosWidgetController? _cocosWidgetController; + double _sliderValue = 0.0; + + @override + void initState() { + super.initState(); + } + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: const Text('Safe Mode Screen'), + ), + body: Card( + margin: const EdgeInsets.all(8), + clipBehavior: Clip.antiAlias, + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(20.0), + ), + child: Stack( + children: [ + CocosWidget( + onCocosCreated: onCocosCreated, + onCocosMessage: onCocosMessage, + useAndroidViewSurface: true, + ), + Positioned( + bottom: 20, + left: 20, + right: 20, + child: PointerInterceptor( + child: Card( + elevation: 10, + child: Column( + children: [ + const Padding( + padding: EdgeInsets.only(top: 20), + child: Text("Rotation speed:"), + ), + Slider( + onChanged: (value) { + setState(() { + _sliderValue = value; + }); + setRotationSpeed(value.toString()); + }, + value: _sliderValue, + min: 0, + max: 20, + ), + ], + ), + ), + ), + ), + ], + ), + ), + ); + } + + void setRotationSpeed(String speed) { + _cocosWidgetController?.postMessage( + 'Cube', + 'SetRotationSpeed', + speed, + ); + } + + void onCocosMessage(message) { + print('Received message from cocos: ${message.toString()}'); + } + + // Callback that connects the created controller to the cocos controller + void onCocosCreated(controller) { + _cocosWidgetController = controller; + } +} diff --git a/example/lib/screens/no_interaction_screen.dart b/example/lib/screens/no_interaction_screen.dart new file mode 100644 index 0000000..0b2f4d3 --- /dev/null +++ b/example/lib/screens/no_interaction_screen.dart @@ -0,0 +1,96 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_cocos_view/flutter_cocos_view.dart'; +import 'package:pointer_interceptor/pointer_interceptor.dart'; + +class NoInteractionScreen extends StatefulWidget { + const NoInteractionScreen({Key? key}) : super(key: key); + + @override + State createState() => _NoInteractionScreenState(); +} + +class _NoInteractionScreenState extends State { + static final GlobalKey _scaffoldKey = + GlobalKey(); + + CocosWidgetController? _cocosWidgetController; + + @override + void initState() { + super.initState(); + } + + @override + void dispose() { + _cocosWidgetController?.dispose(); + super.dispose(); + } + + @override + Widget build(BuildContext context) { + return Scaffold( + key: _scaffoldKey, + appBar: AppBar( + title: const Text('No Interaction Screen'), + ), + body: Card( + margin: const EdgeInsets.all(8), + clipBehavior: Clip.antiAlias, + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(20.0), + ), + child: Stack( + children: [ + CocosWidget( + onCocosCreated: _onCocosCreated, + onCocosMessage: onCocosMessage, + onCocosSceneLoaded: onCocosSceneLoaded, + useAndroidViewSurface: true, + borderRadius: const BorderRadius.all(Radius.circular(70)), + ), + Positioned( + bottom: 20, + left: 20, + right: 20, + child: PointerInterceptor( + child: ElevatedButton( + onPressed: () { + Navigator.of(context).pushNamed('/simple'); + }, + child: const Text('Switch Flutter Screen'), + ), + ), + ), + ], + ), + ), + ); + } + + void setRotationSpeed(String speed) { + _cocosWidgetController?.postMessage( + 'Cube', + 'SetRotationSpeed', + speed, + ); + } + + void onCocosMessage(message) { + print('Received message from cocos: ${message.toString()}'); + } + + void onCocosSceneLoaded(SceneLoaded? scene) { + if (scene != null) { + print('Received scene loaded from cocos: ${scene.name}'); + print('Received scene loaded from cocos buildIndex: ${scene.buildIndex}'); + } else { + print('Received scene loaded from cocos: null'); + } + } + + // Callback that connects the created controller to the cocos controller + void _onCocosCreated(controller) { + controller.resume(); + _cocosWidgetController = controller; + } +} diff --git a/example/lib/screens/orientation_screen.dart b/example/lib/screens/orientation_screen.dart new file mode 100644 index 0000000..24cdf3a --- /dev/null +++ b/example/lib/screens/orientation_screen.dart @@ -0,0 +1,108 @@ +import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; +import 'package:flutter_cocos_view/flutter_cocos_view.dart'; +import 'package:pointer_interceptor/pointer_interceptor.dart'; + +class OrientationScreen extends StatefulWidget { + const OrientationScreen({Key? key}) : super(key: key); + + @override + State createState() => _OrientationScreenState(); +} + +class _OrientationScreenState extends State { + CocosWidgetController? _cocosWidgetController; + double _sliderValue = 0.0; + + @override + void initState() { + super.initState(); + } + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: const Text('Orientation Screen'), + ), + body: Card( + margin: const EdgeInsets.all(8), + clipBehavior: Clip.antiAlias, + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(20.0), + ), + child: Stack( + children: [ + CocosWidget( + onCocosCreated: onCocosCreated, + onCocosMessage: onCocosMessage, + useAndroidViewSurface: true, + ), + Positioned( + bottom: 20, + left: 20, + right: 20, + child: PointerInterceptor( + child: Card( + elevation: 10, + child: Column( + children: [ + ElevatedButton( + onPressed: () { + if (MediaQuery.of(context).orientation == + Orientation.portrait) { + SystemChrome.setPreferredOrientations([ + DeviceOrientation.landscapeLeft, + DeviceOrientation.landscapeRight + ]); + } else if (MediaQuery.of(context).orientation == + Orientation.landscape) { + SystemChrome.setPreferredOrientations( + [DeviceOrientation.portraitUp]); + } + }, + child: const Text("Change Orientation"), + ), + const Padding( + padding: EdgeInsets.only(top: 20), + child: Text("Rotation speed:"), + ), + Slider( + onChanged: (value) { + setState(() { + _sliderValue = value; + }); + setRotationSpeed(value.toString()); + }, + value: _sliderValue, + min: 0, + max: 20, + ), + ], + ), + ), + ), + ), + ], + ), + ), + ); + } + + void setRotationSpeed(String speed) { + _cocosWidgetController?.postMessage( + 'Cube', + 'SetRotationSpeed', + speed, + ); + } + + void onCocosMessage(message) { + print('Received message from cocos: ${message.toString()}'); + } + + // Callback that connects the created controller to the cocos controller + void onCocosCreated(controller) { + _cocosWidgetController = controller; + } +} diff --git a/example/lib/screens/simple_screen.dart b/example/lib/screens/simple_screen.dart new file mode 100644 index 0000000..719a21a --- /dev/null +++ b/example/lib/screens/simple_screen.dart @@ -0,0 +1,112 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_cocos_view/flutter_cocos_view.dart'; +import 'package:pointer_interceptor/pointer_interceptor.dart'; + +class SimpleScreen extends StatefulWidget { + const SimpleScreen({Key? key}) : super(key: key); + + @override + State createState() => _SimpleScreenState(); +} + +class _SimpleScreenState extends State { + static final GlobalKey _scaffoldKey = + GlobalKey(); + + CocosWidgetController? _cocosWidgetController; + double _sliderValue = 0.0; + + @override + void initState() { + super.initState(); + } + + @override + void dispose() { + _cocosWidgetController?.dispose(); + super.dispose(); + } + + @override + Widget build(BuildContext context) { + return Scaffold( + key: _scaffoldKey, + appBar: AppBar( + title: const Text('Simple Screen'), + ), + body: Card( + margin: const EdgeInsets.all(0), + clipBehavior: Clip.antiAlias, + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(20.0), + ), + child: Stack( + children: [ + CocosWidget( + onCocosCreated: _onCocosCreated, + onCocosMessage: onCocosMessage, + onCocosSceneLoaded: onCocosSceneLoaded, + useAndroidViewSurface: false, + borderRadius: const BorderRadius.all(Radius.circular(70)), + ), + Positioned( + bottom: 0, + left: 0, + right: 0, + child: PointerInterceptor( + child: Card( + elevation: 10, + child: Column( + children: [ + const Padding( + padding: EdgeInsets.only(top: 20), + child: Text("Rotation speed:"), + ), + Slider( + onChanged: (value) { + setState(() { + _sliderValue = value; + }); + setRotationSpeed(value.toString()); + }, + value: _sliderValue, + min: 0.0, + max: 1.0, + ), + ], + ), + ), + ), + ), + ], + )), + ); + } + + void setRotationSpeed(String speed) { + _cocosWidgetController?.postMessage( + 'Cube', + 'SetRotationSpeed', + speed, + ); + } + + void onCocosMessage(message) { + print('Received message from cocos: ${message.toString()}'); + } + + void onCocosSceneLoaded(SceneLoaded? scene) { + if (scene != null) { + print('Received scene loaded from cocos: ${scene.name}'); + print('Received scene loaded from cocos buildIndex: ${scene.buildIndex}'); + } else { + print('Received scene loaded from cocos: null'); + } + } + + // Callback that connects the created controller to the cocos controller + void _onCocosCreated(controller) { + controller.resume(); + _cocosWidgetController = controller; + } +} diff --git a/example/pubspec.lock b/example/pubspec.lock index 351c2ee..fe34f17 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -185,6 +185,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.8" + pointer_interceptor: + dependency: "direct main" + description: + name: pointer_interceptor + sha256: adf7a637f97c077041d36801b43be08559fd4322d2127b3f20bb7be1b9eebc22 + url: "https://pub.dev" + source: hosted + version: "0.9.3+7" process: dependency: transitive description: diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 2b2e1c6..09ff21e 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -28,7 +28,7 @@ dependencies: # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^1.0.8 - + pointer_interceptor: ^0.9.3+2 dev_dependencies: integration_test: sdk: flutter diff --git a/lib/flutter_cocos_view.dart b/lib/flutter_cocos_view.dart index dd0df7b..41709de 100644 --- a/lib/flutter_cocos_view.dart +++ b/lib/flutter_cocos_view.dart @@ -5,7 +5,7 @@ // platforms in the `pubspec.yaml` at // https://flutter.dev/to/pubspec-plugin-platforms. -library flutter_Cocos_widget; +library flutter_cocos_view; export 'src/facade_controller.dart'; export 'src/facade_widget.dart'