-
-
Notifications
You must be signed in to change notification settings - Fork 891
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added .anchor property to CameraComponent.Viewfinder (#1458)
- Loading branch information
Showing
9 changed files
with
217 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
examples/lib/stories/camera_and_viewport/camera_component_properties_example.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import 'dart:ui'; | ||
|
||
import 'package:flame/components.dart'; | ||
import 'package:flame/experimental.dart'; | ||
import 'package:flame/game.dart' hide Viewport; | ||
|
||
class CameraComponentPropertiesExample extends FlameGame { | ||
static const description = ''' | ||
This example uses FixedSizeViewport which is dynamically sized and | ||
positioned based on the size of the game widget. | ||
The underlying world is represented as a simple coordinate plane, with | ||
green dot being the origin. The viewfinder uses custom anchor in order to | ||
declare its "center" half-way between the bottom left corner and the true | ||
center. | ||
'''; | ||
|
||
CameraComponent? _camera; | ||
|
||
@override | ||
Color backgroundColor() => const Color(0xff333333); | ||
|
||
@override | ||
Future<void> onLoad() async { | ||
final world = World(); | ||
world.add(Background()); | ||
_camera = CameraComponent( | ||
world: world, | ||
viewport: FixedSizeViewport(200, 200)..add(ViewportFrame()), | ||
) | ||
..viewfinder.zoom = 5 | ||
..viewfinder.anchor = const Anchor(0.25, 0.75); | ||
await add(world); | ||
await add(_camera!); | ||
onGameResize(canvasSize); | ||
} | ||
|
||
@override | ||
void onGameResize(Vector2 size) { | ||
super.onGameResize(size); | ||
_camera?.viewport.size = size * 0.7; | ||
_camera?.viewport.position = size * 0.6; | ||
} | ||
} | ||
|
||
class ViewportFrame extends Component { | ||
final paint = Paint() | ||
..style = PaintingStyle.stroke | ||
..strokeWidth = 3 | ||
..color = const Color(0xff87c4e2); | ||
|
||
@override | ||
void render(Canvas canvas) { | ||
final size = (parent! as Viewport).size; | ||
canvas.drawRRect( | ||
RRect.fromRectAndRadius( | ||
Rect.fromLTWH(-size.x / 2, -size.y / 2, size.x, size.y), | ||
const Radius.circular(5), | ||
), | ||
paint, | ||
); | ||
} | ||
} | ||
|
||
class Background extends Component { | ||
final bgPaint = Paint()..color = const Color(0xffff0000); | ||
final originPaint = Paint()..color = const Color(0xff2f8750); | ||
final axisPaint = Paint() | ||
..strokeWidth = 1 | ||
..style = PaintingStyle.stroke | ||
..color = const Color(0xff878787); | ||
final gridPaint = Paint() | ||
..style = PaintingStyle.stroke | ||
..strokeWidth = 0 | ||
..color = const Color(0xff555555); | ||
|
||
@override | ||
void render(Canvas canvas) { | ||
canvas.drawColor(const Color(0xff000000), BlendMode.src); | ||
for (var i = -100.0; i <= 100.0; i += 10) { | ||
canvas.drawLine(Offset(i, -100), Offset(i, 100), gridPaint); | ||
canvas.drawLine(Offset(-100, i), Offset(100, i), gridPaint); | ||
} | ||
canvas.drawLine(Offset.zero, const Offset(0, 10), axisPaint); | ||
canvas.drawLine(Offset.zero, const Offset(10, 0), axisPaint); | ||
canvas.drawCircle(Offset.zero, 1.0, originPaint); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.