-
-
Notifications
You must be signed in to change notification settings - Fork 899
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat!: Added anchor for the Viewport #1611
Merged
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b069093
tests for MaxViewport
st-pasha 0cfbdf9
tests for FixedSizeViewport
st-pasha dc04004
update comments
st-pasha 7cc0e07
Added golden test
st-pasha c1be3fc
Added anchor for Viewport
st-pasha 6acfb9c
fix example
st-pasha a88b347
update comments
st-pasha afad792
format
st-pasha 75f989b
add golden file
st-pasha 1d9e452
Merge branch 'main' into ps/camera-tests
st-pasha 91a9ed6
Merge branch 'main' into ps/camera-tests
spydon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,7 +88,10 @@ class CameraComponent extends Component { | |
@override | ||
void renderTree(Canvas canvas) { | ||
canvas.save(); | ||
canvas.translate(viewport.position.x, viewport.position.y); | ||
canvas.translate( | ||
viewport.position.x - viewport.anchor.x * viewport.size.x, | ||
viewport.position.y - viewport.anchor.y * viewport.size.y, | ||
); | ||
// Render the world through the viewport | ||
if (world.isMounted && currentCameras.length < maxCamerasDepth) { | ||
canvas.save(); | ||
|
@@ -110,7 +113,10 @@ class CameraComponent extends Component { | |
|
||
@override | ||
Iterable<ComponentPointPair> componentsAtPoint(Vector2 point) sync* { | ||
final viewportPoint = point - viewport.position; | ||
final viewportPoint = Vector2( | ||
point.x - viewport.position.x + viewport.anchor.x * viewport.size.x, | ||
point.y - viewport.position.y + viewport.anchor.y * viewport.size.y, | ||
); | ||
Comment on lines
+116
to
+119
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this the same as: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could be, but I don't really understand what is |
||
if (world.isMounted && currentCameras.length < maxCamerasDepth) { | ||
if (viewport.containsPoint(viewportPoint)) { | ||
try { | ||
|
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
87 changes: 87 additions & 0 deletions
87
packages/flame/test/experimental/fixed_size_viewport_test.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,87 @@ | ||
import 'dart:ui'; | ||
|
||
import 'package:flame/components.dart'; | ||
import 'package:flame/experimental.dart'; | ||
import 'package:flame/game.dart'; | ||
import 'package:flame_test/flame_test.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
|
||
void main() { | ||
group('FixedSizeViewport', () { | ||
testWithFlameGame('camera with FixedSizeViewport', (game) async { | ||
final camera = CameraComponent( | ||
world: World(), | ||
viewport: FixedSizeViewport(300, 100), | ||
); | ||
game.addAll([camera.world, camera]); | ||
await game.ready(); | ||
|
||
expect(camera.viewport, isA<FixedSizeViewport>()); | ||
expect(camera.viewport.size, Vector2(300, 100)); | ||
expect(camera.viewport.position, Vector2(0, 0)); | ||
|
||
game.onGameResize(Vector2(200, 200)); | ||
expect(camera.viewport.size, Vector2(300, 100)); | ||
expect(camera.viewport.position, Vector2(0, 0)); | ||
}); | ||
|
||
testWithFlameGame('hit-testing', (game) async { | ||
final camera = CameraComponent( | ||
world: World(), | ||
viewport: FixedSizeViewport(400, 100), | ||
); | ||
game.addAll([camera.world, camera]); | ||
await game.ready(); | ||
|
||
final viewport = camera.viewport; | ||
expect(viewport, isA<FixedSizeViewport>()); | ||
expect(viewport.containsLocalPoint(Vector2(0, 0)), true); | ||
expect(viewport.containsLocalPoint(Vector2(-1, -1)), false); | ||
expect(viewport.containsLocalPoint(Vector2(400, 100)), true); | ||
expect(viewport.containsLocalPoint(Vector2(150, 50)), true); | ||
expect(viewport.containsLocalPoint(Vector2(-150, 50)), false); | ||
expect(viewport.containsLocalPoint(Vector2(150, -50)), false); | ||
expect(viewport.containsLocalPoint(Vector2(100, 20)), true); | ||
expect(viewport.containsLocalPoint(Vector2(1000, -1000)), false); | ||
expect(viewport.containsLocalPoint(Vector2(300, 100)), true); | ||
}); | ||
|
||
FlameTester(() => FlameGame()).testGameWidget( | ||
'Clipping behavior', | ||
setUp: (game, tester) async { | ||
final world = World(); | ||
final camera = CameraComponent( | ||
world: world, | ||
viewport: FixedSizeViewport(500, 200), | ||
) | ||
..viewport.position = Vector2(400, 300) | ||
..viewport.anchor = Anchor.center | ||
..viewfinder.position = Vector2.zero(); | ||
world.add( | ||
CircleComponent( | ||
position: Vector2.zero(), | ||
radius: 200, | ||
anchor: Anchor.center, | ||
paint: Paint()..color = const Color(0x4400ffff), | ||
), | ||
); | ||
world.addAll([ | ||
for (var i = -6; i <= 6; i++) | ||
CircleComponent( | ||
position: Vector2(i * 60.0 - i * i.abs() * 3, 0), | ||
radius: 25 - i.abs() * 3, | ||
anchor: Anchor.center, | ||
) | ||
]); | ||
game.addAll([world, camera]); | ||
await game.ready(); | ||
}, | ||
verify: (game, tester) async { | ||
await expectLater( | ||
find.byGame<FlameGame>(), | ||
matchesGoldenFile('../_goldens/fixed_size_viewport_test_1.png'), | ||
); | ||
}, | ||
); | ||
}); | ||
} |
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,58 @@ | ||
import 'package:flame/experimental.dart'; | ||
import 'package:flame_test/flame_test.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:vector_math/vector_math_64.dart'; | ||
|
||
void main() { | ||
group('MaxViewport', () { | ||
testWithFlameGame('camera with MaxViewport', (game) async { | ||
expect(game.size, Vector2(800, 600)); | ||
final world = World()..addToParent(game); | ||
final camera = CameraComponent(world: world)..addToParent(game); | ||
await game.ready(); | ||
|
||
expect(camera.viewport, isA<MaxViewport>()); | ||
expect(camera.viewport.size, Vector2(800, 600)); | ||
expect(camera.viewport.position, Vector2(0, 0)); | ||
|
||
game.onGameResize(Vector2(500, 200)); | ||
expect(camera.viewport.size, Vector2(500, 200)); | ||
expect(camera.viewport.position, Vector2(0, 0)); | ||
}); | ||
|
||
testWithFlameGame('hit-testing', (game) async { | ||
final world = World()..addToParent(game); | ||
final camera = CameraComponent(world: world)..addToParent(game); | ||
await game.ready(); | ||
|
||
final viewport = camera.viewport; | ||
expect(viewport, isA<MaxViewport>()); | ||
expect(viewport.containsLocalPoint(Vector2(0, 0)), true); | ||
expect(viewport.containsLocalPoint(Vector2(100, 200)), true); | ||
expect(viewport.containsLocalPoint(Vector2(-1, -1)), true); | ||
expect(viewport.containsLocalPoint(Vector2(1000, -1000)), true); | ||
}); | ||
|
||
testWithFlameGame('check that onViewportResize is called', (game) async { | ||
final world = World(); | ||
final camera = CameraComponent(world: world, viewport: _MyMaxViewport()); | ||
game.addAll([world, camera]); | ||
await game.ready(); | ||
|
||
final viewport = camera.viewport; | ||
expect(viewport, isA<_MyMaxViewport>()); | ||
expect((viewport as _MyMaxViewport).onViewportResizeCalled, 1); | ||
game.onGameResize(Vector2(200, 200)); | ||
expect(viewport.onViewportResizeCalled, 2); | ||
}); | ||
}); | ||
} | ||
|
||
class _MyMaxViewport extends MaxViewport { | ||
int onViewportResizeCalled = 0; | ||
|
||
@override | ||
void onViewportResize() { | ||
onViewportResizeCalled++; | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this the same as
canvas.translateVector(anchor.translate(viewport.position, viewport.size)
?we could even have a function to combine an anchor and a viewport that calls
anchor.translate