Line data Source code
1 : import 'package:flutter/widgets.dart' show EdgeInsets; 2 : import 'package:meta/meta.dart'; 3 : 4 : import '../../../components.dart'; 5 : import '../../../extensions.dart'; 6 : import '../../../game.dart'; 7 : 8 : class HudMarginComponent<T extends BaseGame> extends PositionComponent 9 : with HasGameRef<T> { 10 : @override 11 : bool isHud = true; 12 : 13 : /// Instead of setting a position of the [HudMarginComponent] a margin 14 : /// from the edges of the viewport can be used instead. 15 : EdgeInsets? margin; 16 : 17 1 : HudMarginComponent({ 18 : this.margin, 19 : Vector2? position, 20 : Vector2? size, 21 : Anchor anchor = Anchor.topLeft, 22 : }) : assert( 23 0 : margin != null || position != null, 24 : 'Either margin or position must be defined', 25 : ), 26 1 : super( 27 : size: size, 28 : position: position, 29 : anchor: anchor, 30 : ); 31 : 32 : @override 33 : @mustCallSuper 34 1 : Future<void> onLoad() async { 35 1 : super.onLoad(); 36 1 : if (margin != null) { 37 1 : final margin = this.margin!; 38 2 : final x = margin.left != 0 39 5 : ? margin.left + scaledSize.x / 2 40 0 : : gameRef.viewport.effectiveSize.x - margin.right - scaledSize.x / 2; 41 2 : final y = margin.top != 0 42 0 : ? margin.top + scaledSize.y / 2 43 10 : : gameRef.viewport.effectiveSize.y - margin.bottom - scaledSize.y / 2; 44 2 : position.setValues(x, y); 45 1 : position = 46 4 : Anchor.center.toOtherAnchorPosition(center, anchor, scaledSize); 47 : } else { 48 0 : final topLeft = gameRef.viewport.effectiveSize - 49 0 : anchor.toOtherAnchorPosition( 50 0 : position, 51 : Anchor.topLeft, 52 0 : scaledSize, 53 : ); 54 0 : final bottomRight = gameRef.viewport.effectiveSize - 55 0 : anchor.toOtherAnchorPosition( 56 0 : position, 57 : Anchor.bottomRight, 58 0 : scaledSize, 59 : ); 60 0 : margin = EdgeInsets.fromLTRB( 61 0 : topLeft.x, 62 0 : topLeft.y, 63 0 : bottomRight.x, 64 0 : bottomRight.y, 65 : ); 66 : } 67 : } 68 : }