move texture ID to stream on FilamentController
This commit is contained in:
@@ -13,7 +13,7 @@ typedef FilamentLight = int;
|
|||||||
|
|
||||||
abstract class FilamentController {
|
abstract class FilamentController {
|
||||||
Size get size;
|
Size get size;
|
||||||
late int textureId;
|
late Stream<int> textureId;
|
||||||
Future get initialized;
|
Future get initialized;
|
||||||
Stream get onInitializationRequested;
|
Stream get onInitializationRequested;
|
||||||
Future initialize();
|
Future initialize();
|
||||||
@@ -102,7 +102,10 @@ class PolyvoxFilamentController extends FilamentController {
|
|||||||
double _pixelRatio = 1.0;
|
double _pixelRatio = 1.0;
|
||||||
Size size = Size(0, 0);
|
Size size = Size(0, 0);
|
||||||
|
|
||||||
final _onInitRequestedController = StreamController();
|
final _textureIdController = StreamController<int>();
|
||||||
|
Stream<int> get textureId => _textureIdController.stream;
|
||||||
|
|
||||||
|
final _onInitRequestedController = StreamController.broadcast();
|
||||||
Stream get onInitializationRequested => _onInitRequestedController.stream;
|
Stream get onInitializationRequested => _onInitRequestedController.stream;
|
||||||
|
|
||||||
final _initialized = Completer();
|
final _initialized = Completer();
|
||||||
@@ -139,16 +142,21 @@ class PolyvoxFilamentController extends FilamentController {
|
|||||||
|
|
||||||
Future createTextureViewer(int width, int height) async {
|
Future createTextureViewer(int width, int height) async {
|
||||||
size = Size(width * _pixelRatio, height * _pixelRatio);
|
size = Size(width * _pixelRatio, height * _pixelRatio);
|
||||||
textureId =
|
print("Creating texture of size $size");
|
||||||
|
var textureId =
|
||||||
await _channel.invokeMethod("initialize", [size.width, size.height]);
|
await _channel.invokeMethod("initialize", [size.width, size.height]);
|
||||||
|
_textureIdController.add(textureId);
|
||||||
_initialized.complete(true);
|
_initialized.complete(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future resize(int width, int height,
|
Future resize(int width, int height,
|
||||||
{double contentScaleFactor = 1.0}) async {
|
{double contentScaleFactor = 1.0}) async {
|
||||||
size = Size(width * _pixelRatio, height * _pixelRatio);
|
size = Size(width * _pixelRatio, height * _pixelRatio);
|
||||||
textureId = await _channel.invokeMethod("resize",
|
|
||||||
|
var textureId = await _channel.invokeMethod("resize",
|
||||||
[width * _pixelRatio, height * _pixelRatio, contentScaleFactor]);
|
[width * _pixelRatio, height * _pixelRatio, contentScaleFactor]);
|
||||||
|
print("Resized to $size with texutre Id $textureId");
|
||||||
|
_textureIdController.add(textureId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
|||||||
@@ -55,15 +55,11 @@ class FilamentWidget extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _FilamentWidgetState extends State<FilamentWidget> {
|
class _FilamentWidgetState extends State<FilamentWidget> {
|
||||||
bool _ready = false;
|
|
||||||
StreamSubscription? _listener;
|
StreamSubscription? _listener;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
_listener = widget.controller.onInitializationRequested.listen((_) {
|
_listener = widget.controller.onInitializationRequested.listen((_) {
|
||||||
if (_ready) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
|
WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
|
||||||
var size = ((context.findRenderObject()) as RenderBox).size;
|
var size = ((context.findRenderObject()) as RenderBox).size;
|
||||||
print(
|
print(
|
||||||
@@ -71,13 +67,9 @@ class _FilamentWidgetState extends State<FilamentWidget> {
|
|||||||
await widget.controller
|
await widget.controller
|
||||||
.createTextureViewer(size.width.toInt(), size.height.toInt());
|
.createTextureViewer(size.width.toInt(), size.height.toInt());
|
||||||
print("Filament texture/viewer created.");
|
print("Filament texture/viewer created.");
|
||||||
setState(() {
|
|
||||||
_ready = true;
|
|
||||||
});
|
|
||||||
_listener!.cancel();
|
_listener!.cancel();
|
||||||
_listener = null;
|
_listener = null;
|
||||||
});
|
});
|
||||||
// we need to make sure a new frame is requested, otherwise the callback may not run
|
|
||||||
setState(() {});
|
setState(() {});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -91,23 +83,33 @@ class _FilamentWidgetState extends State<FilamentWidget> {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
if (!_ready) {
|
return StreamBuilder(
|
||||||
return Container();
|
stream: widget.controller.textureId,
|
||||||
}
|
builder: (ctx, AsyncSnapshot<int> textureId) {
|
||||||
var texture = Texture(
|
if (textureId.data == null) {
|
||||||
textureId: widget.controller.textureId,
|
return Container();
|
||||||
filterQuality: FilterQuality.high,
|
}
|
||||||
);
|
|
||||||
return ResizeObserver(
|
var texture = Texture(
|
||||||
onResized: (Size oldSize, Size newSize) async {
|
key: ObjectKey("texture_${textureId.data}"),
|
||||||
await widget.controller
|
textureId: textureId.data!,
|
||||||
.resize(newSize.width.toInt(), newSize.height.toInt());
|
filterQuality: FilterQuality.high,
|
||||||
},
|
);
|
||||||
child: Platform.isLinux
|
return LayoutBuilder(
|
||||||
? Transform(
|
builder: ((context, constraints) => SizedBox(
|
||||||
alignment: Alignment.center,
|
height: constraints.maxHeight,
|
||||||
transform: Matrix4.rotationX(pi),
|
width: constraints.maxWidth,
|
||||||
child: texture)
|
child: ResizeObserver(
|
||||||
: texture);
|
onResized: (Size oldSize, Size newSize) async {
|
||||||
|
await widget.controller.resize(
|
||||||
|
newSize.width.toInt(), newSize.height.toInt());
|
||||||
|
},
|
||||||
|
child: Platform.isLinux
|
||||||
|
? Transform(
|
||||||
|
alignment: Alignment.center,
|
||||||
|
transform: Matrix4.rotationX(pi),
|
||||||
|
child: texture)
|
||||||
|
: texture))));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user