Browse Source

[texture] Add OrientationChanged event and method

embedder_texture
Vitaliy Zarubin 1 year ago
parent
commit
ec4b86573c
  1. 28
      packages/embedder_texture/aurora/embedder_texture_plugin.cpp
  2. 2
      packages/embedder_texture/aurora/include/embedder_texture/embedder_texture_plugin.h
  3. 127
      packages/embedder_texture/lib/embedder_texture.dart
  4. 33
      packages/embedder_texture/lib/embedder_texture_method_channel.dart
  5. 5
      packages/embedder_texture/lib/embedder_texture_platform_interface.dart

28
packages/embedder_texture/aurora/embedder_texture_plugin.cpp

@ -4,6 +4,19 @@
*/ */
#include <embedder_texture/embedder_texture_plugin.h> #include <embedder_texture/embedder_texture_plugin.h>
#include <flutter/method-channel.h> #include <flutter/method-channel.h>
#include <flutter/platform-data.h>
#include <flutter/platform-events.h>
#include <flutter/platform-methods.h>
EmbedderTexturePlugin::EmbedderTexturePlugin()
{
PlatformEvents::SubscribeOrientationChanged([this](DisplayRotation orientation) {
if (this->m_isEnableOrientationChanged) {
EventChannel("embedder_texture_orientation", MethodCodecType::Standard)
.SendEvent(static_cast<int>(orientation));
}
});
}
void EmbedderTexturePlugin::RegisterWithRegistrar(PluginRegistrar &registrar) void EmbedderTexturePlugin::RegisterWithRegistrar(PluginRegistrar &registrar)
{ {
@ -14,6 +27,19 @@ void EmbedderTexturePlugin::RegisterWithRegistrar(PluginRegistrar &registrar)
registrar.RegisterMethodChannel("embedder_texture", registrar.RegisterMethodChannel("embedder_texture",
MethodCodecType::Standard, MethodCodecType::Standard,
[this](const MethodCall &call) { this->onMethodCall(call); }); [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) void EmbedderTexturePlugin::onMethodCall(const MethodCall &call)
@ -41,6 +67,8 @@ void EmbedderTexturePlugin::onCreate(const MethodCall &call)
auto result = m_camera->Register(cameraId, width, height); auto result = m_camera->Register(cameraId, width, height);
result["orientation"] = static_cast<int>(PlatformMethods::GetOrientation());
call.SendSuccessResponse(result); call.SendSuccessResponse(result);
} }

2
packages/embedder_texture/aurora/include/embedder_texture/embedder_texture_plugin.h

@ -18,6 +18,7 @@
class PLUGIN_EXPORT EmbedderTexturePlugin final : public PluginInterface class PLUGIN_EXPORT EmbedderTexturePlugin final : public PluginInterface
{ {
public: public:
EmbedderTexturePlugin();
void RegisterWithRegistrar(PluginRegistrar &registrar) override; void RegisterWithRegistrar(PluginRegistrar &registrar) override;
private: private:
@ -28,6 +29,7 @@ private:
private: private:
Camera *m_camera; Camera *m_camera;
bool m_isEnableOrientationChanged = false;
}; };
#endif /* EMBEDDER_TEXTURE_PLUGIN_H */ #endif /* EMBEDDER_TEXTURE_PLUGIN_H */

127
packages/embedder_texture/lib/embedder_texture.dart

@ -1,8 +1,15 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'embedder_texture_platform_interface.dart'; import 'embedder_texture_platform_interface.dart';
enum OrientationEvent {
undefined,
portrait,
landscape,
portraitFlipped,
landscapeFlipped,
}
class EmbedderTexture extends StatefulWidget { class EmbedderTexture extends StatefulWidget {
const EmbedderTexture({ const EmbedderTexture({
super.key, super.key,
@ -19,8 +26,9 @@ class EmbedderTexture extends StatefulWidget {
class _EmbedderTextureState extends State<EmbedderTexture> { class _EmbedderTextureState extends State<EmbedderTexture> {
int _textureID = 0; int _textureID = 0;
int _camera_width = 0; int _cameraWidth = 0;
int _camera_height = 0; int _cameraHeight = 0;
OrientationEvent _orientation = OrientationEvent.undefined;
@override @override
initState() { initState() {
@ -28,13 +36,39 @@ class _EmbedderTextureState extends State<EmbedderTexture> {
EmbedderTexturePlatform.instance EmbedderTexturePlatform.instance
.create(widget.width, widget.height) .create(widget.width, widget.height)
.then((data) => setState(() { .then((data) => setState(() {
if (mounted) { if (mounted) {
_textureID = data['textureId']!; _textureID = data['textureId']!;
_camera_width = data['width']!; _cameraWidth = data['width']!;
_camera_height = data['height']!; _cameraHeight = data['height']!;
debugPrint(_textureID.toString());
} 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 @override
@ -43,34 +77,83 @@ class _EmbedderTextureState extends State<EmbedderTexture> {
EmbedderTexturePlatform.instance.remove(_textureID); 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 @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
if (_textureID != 0) { if (_textureID != 0) {
double w = 0; double w = 0;
double h = 0; double h = 0;
if (MediaQuery.of(context).orientation == Orientation.portrait) { if (_orientation == OrientationEvent.portrait ||
w = _camera_width * widget.height / (widget.width - 40); _orientation == OrientationEvent.portraitFlipped) {
w = _cameraWidth * widget.height / (widget.width - 40);
h = widget.width - 40; h = widget.width - 40;
} else { } else {
w = _camera_height.toDouble(); w = _cameraHeight.toDouble();
h = widget.height - 40 - 56; h = widget.height - 40 - 56;
} }
AlwaysStoppedAnimation<double>? 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( return Container(
color: Colors.black, color: Colors.black,
width: widget.width, width: widget.width,
height: widget.height, height: widget.height,
child: Center( child: Center(
child: RotationTransition( child: RotationTransition(
turns: AlwaysStoppedAnimation(MediaQuery.of(context).orientation == Orientation.portrait ? 90 / 360 : 0), turns: turns,
child: SizedBox( child: SizedBox(
width: w, width: w,
height: h, height: h,
child: Texture(textureId: _textureID) child: Texture(textureId: _textureID),
), ),
) ),
), ),
); );
} }

33
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/foundation.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
@ -11,7 +12,8 @@ class MethodChannelEmbedderTexture extends EmbedderTexturePlatform {
@override @override
Future<Map<dynamic, dynamic>> create(double width, double height) async { Future<Map<dynamic, dynamic>> create(double width, double height) async {
final data = await methodChannel.invokeMethod<Map<dynamic, dynamic>?>('create', { final data =
await methodChannel.invokeMethod<Map<dynamic, dynamic>?>('create', {
'width': width.round(), 'width': width.round(),
'height': height.round(), 'height': height.round(),
}); });
@ -21,7 +23,32 @@ class MethodChannelEmbedderTexture extends EmbedderTexturePlatform {
@override @override
Future<bool> remove(int textureId) async { Future<bool> remove(int textureId) async {
return await methodChannel.invokeMethod<bool>('remove', { return await methodChannel.invokeMethod<bool>('remove', {
'textureId': textureId, 'textureId': textureId,
}) ?? false; }) ??
false;
}
@override
Stream<OrientationEvent> 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;
}
}
} }
} }

5
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 'package:plugin_platform_interface/plugin_platform_interface.dart';
import 'embedder_texture_method_channel.dart'; import 'embedder_texture_method_channel.dart';
@ -23,6 +24,10 @@ abstract class EmbedderTexturePlatform extends PlatformInterface {
_instance = instance; _instance = instance;
} }
Stream<OrientationEvent> onChangeOrientation() {
throw UnimplementedError('onChangeOrientation() has not been implemented.');
}
Future<Map<dynamic, dynamic>> create(double width, double height) { Future<Map<dynamic, dynamic>> create(double width, double height) {
throw UnimplementedError('create() has not been implemented.'); throw UnimplementedError('create() has not been implemented.');
} }

Loading…
Cancel
Save