-
Notifications
You must be signed in to change notification settings - Fork 6k
Fixes issues with invalidating matrix for SpriteBox & adds HUD to demo game #202
Changes from 3 commits
cf1896d
61c0394
6216bc2
ed4329e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,7 @@ class SpriteBox extends RenderBox { | |
|
||
// Add new references | ||
_addSpriteBoxReference(_rootNode); | ||
markNeedsLayout(); | ||
} | ||
|
||
// Tracking of frame rate and updates | ||
|
@@ -55,7 +56,7 @@ class SpriteBox extends RenderBox { | |
_transformMode = value; | ||
|
||
// Invalidate stuff | ||
if (attached) performLayout(); | ||
markNeedsLayout(); | ||
} | ||
|
||
/// The transform mode used by the [SpriteBox]. | ||
|
@@ -68,7 +69,10 @@ class SpriteBox extends RenderBox { | |
|
||
Rect _visibleArea; | ||
|
||
Rect get visibleArea => _visibleArea; | ||
Rect get visibleArea { | ||
if (_visibleArea == null) _calcTransformMatrix(); | ||
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. split if statement onto two lines |
||
return _visibleArea; | ||
} | ||
|
||
// Setup | ||
|
||
|
@@ -150,6 +154,8 @@ class SpriteBox extends RenderBox { | |
} | ||
|
||
void handleEvent(Event event, _SpriteBoxHitTestEntry entry) { | ||
if (!attached) return; | ||
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. Please put if statement bodies, especially control flow statements like 'return', clearly on their own line so that they are obvious when scanning the code. |
||
|
||
if (event is PointerEvent) { | ||
|
||
if (event.type == 'pointerdown') { | ||
|
@@ -212,10 +218,13 @@ class SpriteBox extends RenderBox { | |
/// var matrix = mySpriteBox.transformMatrix; | ||
Matrix4 get transformMatrix { | ||
// Get cached matrix if available | ||
if (_transformMatrix != null) { | ||
return _transformMatrix; | ||
if (_transformMatrix == null) { | ||
_calcTransformMatrix(); | ||
} | ||
return _transformMatrix; | ||
} | ||
|
||
void _calcTransformMatrix() { | ||
_transformMatrix = new Matrix4.identity(); | ||
|
||
// Calculate matrix | ||
|
@@ -273,13 +282,17 @@ class SpriteBox extends RenderBox { | |
break; | ||
} | ||
|
||
_visibleArea = new Rect.fromLTRB(-offsetX / scaleX, | ||
-offsetY / scaleY, | ||
systemWidth + offsetX / scaleX, | ||
systemHeight + offsetY / scaleY); | ||
|
||
_transformMatrix.translate(offsetX, offsetY); | ||
_transformMatrix.scale(scaleX, scaleY); | ||
|
||
return _transformMatrix; | ||
} | ||
|
||
void _invalidateTransformMatrix() { | ||
_visibleArea = null; | ||
_transformMatrix = null; | ||
_rootNode._invalidateToBoxTransformMatrix(); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,6 +51,7 @@ main() async { | |
class GameDemoApp extends App { | ||
|
||
NavigationState _navigationState; | ||
GameDemoWorld _game; | ||
|
||
void initState() { | ||
_navigationState = new NavigationState([ | ||
|
@@ -84,16 +85,18 @@ class GameDemoApp extends App { | |
} | ||
|
||
Widget _buildGameScene(navigator, route) { | ||
return new SpriteWidget( | ||
new GameDemoWorld(_app, navigator, _loader, _spriteSheet) | ||
); | ||
if (_game == null) _game = new GameDemoWorld(_app, navigator, _loader, _spriteSheet); | ||
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. please split if statements over two lines |
||
return new SpriteWidget(_game); | ||
} | ||
|
||
Widget _buildMainScene(navigator, route) { | ||
return new Center( | ||
child: new RaisedButton( | ||
child: new Text("Play"), | ||
onPressed: () => navigator.pushNamed('/game') | ||
onPressed: () { | ||
_game = new GameDemoWorld(_app, navigator, _loader, _spriteSheet, _spriteSheetUI); | ||
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. isn't this redundant with _buildGameScene? 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. BTW, generally, build functions should be stateless and idempotent, and state setting should happen in event callbacks within setState() blocks. Might be worth refactoring this a bit. |
||
navigator.pushNamed('/game'); | ||
} | ||
) | ||
); | ||
} | ||
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. resetGame seems to be dead code (nothing calls it). |
||
|
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.
constructors should come first, then fields