diff --git a/packages/embedder_texture/aurora/embedder_texture_plugin.cpp b/packages/embedder_texture/aurora/embedder_texture_plugin.cpp index a5f8966..b076d4b 100644 --- a/packages/embedder_texture/aurora/embedder_texture_plugin.cpp +++ b/packages/embedder_texture/aurora/embedder_texture_plugin.cpp @@ -4,6 +4,19 @@ */ #include #include +#include +#include +#include + +EmbedderTexturePlugin::EmbedderTexturePlugin() +{ + PlatformEvents::SubscribeOrientationChanged([this](DisplayRotation orientation) { + if (this->m_isEnableOrientationChanged) { + EventChannel("embedder_texture_orientation", MethodCodecType::Standard) + .SendEvent(static_cast(orientation)); + } + }); +} void EmbedderTexturePlugin::RegisterWithRegistrar(PluginRegistrar ®istrar) { @@ -14,6 +27,19 @@ void EmbedderTexturePlugin::RegisterWithRegistrar(PluginRegistrar ®istrar) registrar.RegisterMethodChannel("embedder_texture", MethodCodecType::Standard, [this](const MethodCall &call) { this->onMethodCall(call); }); + + registrar.RegisterEventChannel( + "embedder_texture_orientation", + MethodCodecType::Standard, + [this](const Encodable &) { + this->m_isEnableOrientationChanged = true; + return EventResponse(); + }, + [this](const Encodable &) { + this->m_isEnableOrientationChanged = false; + return EventResponse(); + }); + } void EmbedderTexturePlugin::onMethodCall(const MethodCall &call) @@ -41,6 +67,8 @@ void EmbedderTexturePlugin::onCreate(const MethodCall &call) auto result = m_camera->Register(cameraId, width, height); + result["orientation"] = static_cast(PlatformMethods::GetOrientation()); + call.SendSuccessResponse(result); } diff --git a/packages/embedder_texture/aurora/include/embedder_texture/embedder_texture_plugin.h b/packages/embedder_texture/aurora/include/embedder_texture/embedder_texture_plugin.h index 2261b06..c1ca0a5 100644 --- a/packages/embedder_texture/aurora/include/embedder_texture/embedder_texture_plugin.h +++ b/packages/embedder_texture/aurora/include/embedder_texture/embedder_texture_plugin.h @@ -18,6 +18,7 @@ class PLUGIN_EXPORT EmbedderTexturePlugin final : public PluginInterface { public: + EmbedderTexturePlugin(); void RegisterWithRegistrar(PluginRegistrar ®istrar) override; private: @@ -28,6 +29,7 @@ private: private: Camera *m_camera; + bool m_isEnableOrientationChanged = false; }; #endif /* EMBEDDER_TEXTURE_PLUGIN_H */ diff --git a/packages/embedder_texture/lib/embedder_texture.dart b/packages/embedder_texture/lib/embedder_texture.dart index eeefffe..a5daa62 100644 --- a/packages/embedder_texture/lib/embedder_texture.dart +++ b/packages/embedder_texture/lib/embedder_texture.dart @@ -1,8 +1,15 @@ - import 'package:flutter/material.dart'; import 'embedder_texture_platform_interface.dart'; +enum OrientationEvent { + undefined, + portrait, + landscape, + portraitFlipped, + landscapeFlipped, +} + class EmbedderTexture extends StatefulWidget { const EmbedderTexture({ super.key, @@ -19,8 +26,9 @@ class EmbedderTexture extends StatefulWidget { class _EmbedderTextureState extends State { int _textureID = 0; - int _camera_width = 0; - int _camera_height = 0; + int _cameraWidth = 0; + int _cameraHeight = 0; + OrientationEvent _orientation = OrientationEvent.undefined; @override initState() { @@ -28,13 +36,39 @@ class _EmbedderTextureState extends State { EmbedderTexturePlatform.instance .create(widget.width, widget.height) .then((data) => setState(() { - if (mounted) { - _textureID = data['textureId']!; - _camera_width = data['width']!; - _camera_height = data['height']!; - debugPrint(_textureID.toString()); - } - })); + if (mounted) { + _textureID = data['textureId']!; + _cameraWidth = data['width']!; + _cameraHeight = data['height']!; + + switch (data['orientation']!) { + case 0: + _orientation = OrientationEvent.portrait; + break; + case 90: + _orientation = OrientationEvent.landscape; + break; + case 180: + _orientation = OrientationEvent.portraitFlipped; + break; + case 270: + _orientation = OrientationEvent.landscapeFlipped; + break; + default: + _orientation = OrientationEvent.undefined; + } + + debugPrint(data.toString()); + } + })); + + EmbedderTexturePlatform.instance.onChangeOrientation().listen((event) { + setState(() { + if (mounted) { + _orientation = event; + } + }); + }); } @override @@ -43,34 +77,83 @@ class _EmbedderTextureState extends State { EmbedderTexturePlatform.instance.remove(_textureID); } + // @override + // Widget build(BuildContext context) { + // if (_textureID != 0) { + // double w = 0; + // double h = 0; + + // if (MediaQuery.of(context).orientation == Orientation.portrait) { + // w = _cameraWidth * widget.height / (widget.width - 40); + // h = widget.width - 40; + // } else { + // w = _cameraHeight.toDouble(); + // h = widget.height - 40 - 56; + // } + + // return Container( + // color: Colors.black, + // width: widget.width, + // height: widget.height, + // child: RotationTransition( + // turns: AlwaysStoppedAnimation( + // _orientation == OrientationEvent.portrait ? 90 / 360 : 0, + // ), + // child: SizedBox( + // width: w, + // height: h, + // child: Texture(textureId: _textureID), + // ), + // ), + // ); + // } + // return const SizedBox.shrink(); + // } + @override Widget build(BuildContext context) { if (_textureID != 0) { - double w = 0; double h = 0; - if (MediaQuery.of(context).orientation == Orientation.portrait) { - w = _camera_width * widget.height / (widget.width - 40); + if (_orientation == OrientationEvent.portrait || + _orientation == OrientationEvent.portraitFlipped) { + w = _cameraWidth * widget.height / (widget.width - 40); h = widget.width - 40; } else { - w = _camera_height.toDouble(); + w = _cameraHeight.toDouble(); h = widget.height - 40 - 56; } + AlwaysStoppedAnimation? turns; + + switch (_orientation) { + case OrientationEvent.portrait: + turns = const AlwaysStoppedAnimation(90 / 360); + break; + case OrientationEvent.portraitFlipped: + turns = const AlwaysStoppedAnimation(270 / 360); + break; + case OrientationEvent.landscapeFlipped: + turns = const AlwaysStoppedAnimation(180 / 360); + break; + default: + turns = const AlwaysStoppedAnimation(0); + } + return Container( color: Colors.black, width: widget.width, height: widget.height, child: Center( - child: RotationTransition( - turns: AlwaysStoppedAnimation(MediaQuery.of(context).orientation == Orientation.portrait ? 90 / 360 : 0), - child: SizedBox( - width: w, - height: h, - child: Texture(textureId: _textureID) - ), - ) + child: RotationTransition( + turns: turns, + child: SizedBox( + width: w, + height: h, + child: Texture(textureId: _textureID), + ), + ), ), ); } diff --git a/packages/embedder_texture/lib/embedder_texture_method_channel.dart b/packages/embedder_texture/lib/embedder_texture_method_channel.dart index d5a455d..f7886e2 100644 --- a/packages/embedder_texture/lib/embedder_texture_method_channel.dart +++ b/packages/embedder_texture/lib/embedder_texture_method_channel.dart @@ -1,3 +1,4 @@ +import 'package:embedder_texture/embedder_texture.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; @@ -11,7 +12,8 @@ class MethodChannelEmbedderTexture extends EmbedderTexturePlatform { @override Future> create(double width, double height) async { - final data = await methodChannel.invokeMethod?>('create', { + final data = + await methodChannel.invokeMethod?>('create', { 'width': width.round(), 'height': height.round(), }); @@ -21,7 +23,32 @@ class MethodChannelEmbedderTexture extends EmbedderTexturePlatform { @override Future remove(int textureId) async { return await methodChannel.invokeMethod('remove', { - 'textureId': textureId, - }) ?? false; + 'textureId': textureId, + }) ?? + false; + } + + @override + Stream onChangeOrientation() async* { + await for (final orientation + in const EventChannel('embedder_texture_orientation') + .receiveBroadcastStream()) { + switch (orientation) { + case 0: + yield OrientationEvent.portrait; + break; + case 90: + yield OrientationEvent.landscape; + break; + case 180: + yield OrientationEvent.portraitFlipped; + break; + case 270: + yield OrientationEvent.landscapeFlipped; + break; + default: + yield OrientationEvent.undefined; + } + } } } diff --git a/packages/embedder_texture/lib/embedder_texture_platform_interface.dart b/packages/embedder_texture/lib/embedder_texture_platform_interface.dart index f06ad88..ed630af 100644 --- a/packages/embedder_texture/lib/embedder_texture_platform_interface.dart +++ b/packages/embedder_texture/lib/embedder_texture_platform_interface.dart @@ -1,3 +1,4 @@ +import 'package:embedder_texture/embedder_texture.dart'; import 'package:plugin_platform_interface/plugin_platform_interface.dart'; import 'embedder_texture_method_channel.dart'; @@ -23,6 +24,10 @@ abstract class EmbedderTexturePlatform extends PlatformInterface { _instance = instance; } + Stream onChangeOrientation() { + throw UnimplementedError('onChangeOrientation() has not been implemented.'); + } + Future> create(double width, double height) { throw UnimplementedError('create() has not been implemented.'); }