expose method for retrieving all child entities

This commit is contained in:
Nick Fisher
2024-03-26 00:24:21 +08:00
parent 849ad6c530
commit b357144a79
13 changed files with 101 additions and 21 deletions

View File

@@ -65,6 +65,9 @@ class Gizmo {
}
void detach() async {
await controller.setParent(x, 0);
await controller.setParent(y, 0);
await controller.setParent(z, 0);
await controller.hide(x, null);
await controller.hide(y, null);
await controller.hide(z, null);

View File

@@ -576,6 +576,12 @@ abstract class FilamentController {
double orbitSpeedY = 0.01,
double zoomSpeed = 0.01});
///
/// Returns all child entities under [parent].
///
Future<List<FilamentEntity>> getChildEntities(
FilamentEntity parent, bool renderableOnly);
///
/// Finds the child entity named [childName] associated with the given parent.
/// Usually, [parent] will be the return value from [loadGlb]/[loadGltf] and [childName] will be the name of a node/mesh.
@@ -584,9 +590,9 @@ abstract class FilamentController {
FilamentEntity parent, String childName);
///
/// List all child entities under the given entity.
/// List the name of all child entities under the given entity.
///
Future<List<String>> getChildEntities(FilamentEntity entity,
Future<List<String>> getChildEntityNames(FilamentEntity entity,
{bool renderableOnly = true});
///

View File

@@ -358,7 +358,7 @@ class FilamentControllerFFI extends FilamentController {
get_gizmo(_sceneManager!, out);
var gizmo = Gizmo(out[0], out[1], out[2], this);
allocator.free(out);
_scene = SceneImpl(gizmo);
_scene = SceneImpl(gizmo, this);
hasViewer.value = true;
_creating = false;
@@ -1504,8 +1504,19 @@ class FilamentControllerFFI extends FilamentController {
return childEntity;
}
Future<List<FilamentEntity>> getChildEntities(
FilamentEntity parent, bool renderableOnly) async {
var count = get_entity_count(_sceneManager!, parent, renderableOnly);
var out = allocator<Int32>(count);
get_entities(_sceneManager!, parent, renderableOnly, out);
var outList =
List.generate(count, (index) => out[index]).cast<FilamentEntity>();
allocator.free(out);
return outList;
}
@override
Future<List<String>> getChildEntities(FilamentEntity entity,
Future<List<String>> getChildEntityNames(FilamentEntity entity,
{bool renderableOnly = false}) async {
var count = get_entity_count(_sceneManager!, entity, renderableOnly);
var names = <String>[];

View File

@@ -885,6 +885,17 @@ external int get_entity_count(
bool renderableOnly,
);
@ffi.Native<
ffi.Void Function(
ffi.Pointer<ffi.Void>, EntityId, ffi.Bool, ffi.Pointer<EntityId>)>(
symbol: 'get_entities', assetId: 'flutter_filament_plugin')
external void get_entities(
ffi.Pointer<ffi.Void> sceneManager,
int target,
bool renderableOnly,
ffi.Pointer<EntityId> out,
);
@ffi.Native<
ffi.Pointer<ffi.Char> Function(
ffi.Pointer<ffi.Void>, EntityId, ffi.Int, ffi.Bool)>(

View File

@@ -9,7 +9,9 @@ class SceneImpl extends Scene {
final Gizmo _gizmo;
Gizmo get gizmo => _gizmo;
SceneImpl(this._gizmo);
FilamentController controller;
SceneImpl(this._gizmo, this.controller);
@override
FilamentEntity? selected;
@@ -35,8 +37,9 @@ class SceneImpl extends Scene {
_onUpdatedController.add(true);
}
void unregisterLight(FilamentEntity entity) {
if (selected == entity) {
void unregisterLight(FilamentEntity entity) async {
var children = await controller.getChildEntities(entity, true);
if (selected == entity || children.contains(selected)) {
selected = null;
_gizmo.detach();
}
@@ -45,8 +48,9 @@ class SceneImpl extends Scene {
_onUpdatedController.add(true);
}
void unregisterEntity(FilamentEntity entity) {
if (selected == entity) {
void unregisterEntity(FilamentEntity entity) async {
var children = await controller.getChildEntities(entity, true);
if (selected == entity || children.contains(selected)) {
selected = null;
_gizmo.detach();
}