From 9fc36fe79489090624608175f7d877585fa2484d Mon Sep 17 00:00:00 2001 From: Viktor Lidholt Date: Tue, 25 Aug 2015 09:26:49 -0700 Subject: [PATCH] Adds virtual joystick to games library --- examples/game/lib/sprites.dart | 1 + examples/game/lib/virtual_joystick.dart | 62 +++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 examples/game/lib/virtual_joystick.dart diff --git a/examples/game/lib/sprites.dart b/examples/game/lib/sprites.dart index 7afdf904c6..9521be3e1d 100644 --- a/examples/game/lib/sprites.dart +++ b/examples/game/lib/sprites.dart @@ -39,3 +39,4 @@ part 'sprite_box.dart'; part 'sprite_widget.dart'; part 'texture.dart'; part 'util.dart'; +part 'virtual_joystick.dart'; diff --git a/examples/game/lib/virtual_joystick.dart b/examples/game/lib/virtual_joystick.dart new file mode 100644 index 0000000000..b4dabcf000 --- /dev/null +++ b/examples/game/lib/virtual_joystick.dart @@ -0,0 +1,62 @@ +part of sprites; + +class VirtualJoystick extends NodeWithSize { + VirtualJoystick() : super(new Size(160.0, 160.0)) { + userInteractionEnabled = true; + handleMultiplePointers = false; + position = new Point(160.0, -20.0); + pivot = new Point(0.5, 1.0); + _center = new Point(size.width / 2.0, size.height / 2.0); + _handlePos = _center; + + _paintHandle = new Paint() + ..color=new Color(0xffffffff); + _paintControl = new Paint() + ..color=new Color(0xffffffff) + ..strokeWidth = 1.0 + ..setStyle(PaintingStyle.stroke); + } + + Point _value = Point.origin; + Point get value => _value; + + bool _isDown = false; + bool get isDown => _isDown; + + Point _pointerDownAt; + Point _center; + Point _handlePos; + + Paint _paintHandle; + Paint _paintControl; + + bool handleEvent(SpriteBoxEvent event) { + if (event.type == "pointerdown") { + _pointerDownAt = event.boxPosition; + actions.stopAll(); + _isDown = true; + } + else if (event.type == "pointerup" || event.type == "pointercancel") { + _pointerDownAt = null; + _value = Point.origin; + ActionTween moveToCenter = new ActionTween((a) => _handlePos = a, _handlePos, _center, 0.4, elasticOut); + actions.run(moveToCenter); + _isDown = false; + } else if (event.type == "pointermove") { + Offset movedDist = event.boxPosition - _pointerDownAt; + + _value = new Point( + (movedDist.dx / 80.0).clamp(-1.0, 1.0), + (movedDist.dy / 80.0).clamp(-1.0, 1.0)); + + _handlePos = _center + new Offset(_value.x * 40.0, _value.y * 40.0); + } + return true; + } + + void paint(PaintingCanvas canvas) { + applyTransformForPivot(canvas); + canvas.drawCircle(_handlePos, 25.0, _paintHandle); + canvas.drawCircle(_center, 40.0, _paintControl); + } +}