setBounds method
Sets or clears the world bounds for the camera's viewfinder.
The bound is a Shape
, given in the world coordinates. The viewfinder's
position will be restricted to always remain inside this region.
When considerViewport
is true none of the viewport can go outside
of the bounds, when it is false only the viewfinder anchor is considered.
Note that this option only works with Rectangle
, RoundedRectangle
and
Circle
shapes.
Implementation
void setBounds(Shape? bounds, {bool considerViewport = false}) {
final boundedBehavior = viewfinder.firstChild<BoundedPositionBehavior>();
final viewPortAwareBoundsBehavior =
viewfinder.firstChild<ViewportAwareBoundsBehavior>();
if (bounds == null) {
// When bounds is null, all bounds-related components need to be dropped.
boundedBehavior?.removeFromParent();
viewPortAwareBoundsBehavior?.removeFromParent();
return;
}
Future<void>? boundedBehaviorFuture;
if (boundedBehavior == null) {
final BoundedPositionBehavior ref;
viewfinder.add(
ref = BoundedPositionBehavior(
bounds: bounds,
priority: 1000,
),
);
boundedBehaviorFuture = ref.mounted;
} else {
boundedBehavior.bounds = bounds;
}
if (!considerViewport) {
// Edge case: remove pre-existing viewport aware components.
viewPortAwareBoundsBehavior?.removeFromParent();
return;
}
// Param `considerViewPort` was true and we have a bounds.
// Add a ViewportAwareBoundsBehavior component with
// our desired bounds shape or update the boundsShape if the
// component already exists.
if (viewPortAwareBoundsBehavior == null) {
switch (boundedBehaviorFuture) {
case null:
// This represents the case when BoundedPositionBehavior was mounted
// earlier in another cycle. This allows us to immediately add the
// ViewportAwareBoundsBehavior component which will subsequently adapt
// the camera to the virtual resolution this frame.
_addViewPortAwareBoundsBehavior(bounds);
case _:
// This represents the case when BoundedPositionBehavior was added
// in this exact cycle but did not mount into the tree.
// We must wait for that component to mount first in order for
// ViewportAwareBoundsBehavior to correctly affect the camera.
boundedBehaviorFuture
.whenComplete(() => _addViewPortAwareBoundsBehavior(bounds));
}
} else {
viewPortAwareBoundsBehavior.boundsShape = bounds;
}
}