improvements code

This commit is contained in:
rafaelbarbosatec
2020-06-22 15:45:31 -03:00
parent 68d7798c59
commit 2ee76e7cbf
3 changed files with 64 additions and 94 deletions

View File

@ -15,22 +15,21 @@ class JoystickAction {
final Sprite spritePressed;
final Sprite spriteBackgroundDirection;
final double size;
double sizeBackgroundDirection;
final double sizeFactorBackgroundDirection;
final EdgeInsets margin;
final JoystickActionAlign align;
final bool enableDirection;
final Color color;
Rect _rect;
Rect _rectAction;
Rect _rectBackgroundDirection;
bool _dragging = false;
Sprite _sprite;
double _tileSize;
Sprite _spriteAction;
Offset _dragPosition;
Paint _paintBackground;
Paint _paintAction;
JoystickController _joystickController;
double _sizeBackgroundDirection;
DragEvent _currentDragEvent;
JoystickAction({
@ -40,14 +39,13 @@ class JoystickAction {
this.spriteBackgroundDirection,
this.enableDirection = false,
this.size = 50,
this.sizeBackgroundDirection,
this.sizeFactorBackgroundDirection = 1.5,
this.margin = EdgeInsets.zero,
this.color = Colors.blueGrey,
this.align = JoystickActionAlign.BOTTOM_RIGHT,
}) {
_sprite = sprite;
sizeBackgroundDirection = sizeBackgroundDirection ?? size * 1.5;
_tileSize = sizeBackgroundDirection;
_spriteAction = sprite;
_sizeBackgroundDirection = sizeFactorBackgroundDirection * size;
}
void initialize(Size _screenSize, JoystickController joystickController) {
@ -72,13 +70,13 @@ class JoystickAction {
dy = _screenSize.height - (margin.bottom + radius);
break;
}
_rect = Rect.fromCircle(
_rectAction = Rect.fromCircle(
center: Offset(dx, dy),
radius: radius,
);
_rectBackgroundDirection = Rect.fromCircle(
center: Offset(dx, dy),
radius: sizeBackgroundDirection / 2,
radius: _sizeBackgroundDirection / 2,
);
_paintBackground = Paint()
@ -89,13 +87,13 @@ class JoystickAction {
..color = color.withOpacity(0.8)
..style = PaintingStyle.fill;
_dragPosition = _rect.center;
_dragPosition = _rectAction.center;
}
void render(Canvas c) {
if (_rectBackgroundDirection != null && _dragging && enableDirection) {
if (spriteBackgroundDirection == null) {
double radiusBackground = _rectBackgroundDirection.width / 2;
final double radiusBackground = _rectBackgroundDirection.width / 2;
c.drawCircle(
Offset(
_rectBackgroundDirection.left + radiusBackground,
@ -109,14 +107,16 @@ class JoystickAction {
}
}
if (_sprite != null) {
if (_rect != null) _sprite.renderRect(c, _rect);
if (_spriteAction != null) {
if (_rectAction != null) {
_spriteAction.renderRect(c, _rectAction);
}
} else {
double radiusAction = _rect.width / 2;
final double radiusAction = _rectAction.width / 2;
c.drawCircle(
Offset(
_rect.left + radiusAction,
_rect.top + radiusAction,
_rectAction.left + radiusAction,
_rectAction.top + radiusAction,
),
radiusAction,
_paintAction,
@ -126,13 +126,13 @@ class JoystickAction {
void update(double dt) {
if (_dragging) {
double _radAngle = atan2(
final double _radAngle = atan2(
_dragPosition.dy - _rectBackgroundDirection.center.dy,
_dragPosition.dx - _rectBackgroundDirection.center.dx,
);
// Distance between the center of joystick background & drag position
Point centerPoint = Point(
final Point centerPoint = Point(
_rectBackgroundDirection.center.dx,
_rectBackgroundDirection.center.dy,
);
@ -142,7 +142,9 @@ class JoystickAction {
// The maximum distance for the knob position the edge of
// the background + half of its own size. The knob can wander in the
// background image, but not outside.
dist = dist < (_tileSize / 3) ? dist : (_tileSize / 3);
dist = dist < (_sizeBackgroundDirection / 3)
? dist
: (_sizeBackgroundDirection / 3);
// Calculation the knob position
final double nextX = dist * cos(_radAngle);
@ -153,10 +155,10 @@ class JoystickAction {
_rectBackgroundDirection.center.dx + nextPoint.dx,
_rectBackgroundDirection.center.dy + nextPoint.dy,
) -
_rect.center;
_rect = _rect.shift(diff);
_rectAction.center;
_rectAction = _rectAction.shift(diff);
final double _intensity = dist / (_tileSize / 3);
final double _intensity = dist / (_sizeBackgroundDirection / 3);
_joystickController.joystickAction(
JoystickActionEvent(
@ -167,15 +169,15 @@ class JoystickAction {
),
);
} else {
if (_rect != null) {
final Offset diff = _dragPosition - _rect.center;
_rect = _rect.shift(diff);
if (_rectAction != null) {
final Offset diff = _dragPosition - _rectAction.center;
_rectAction = _rectAction.shift(diff);
}
}
}
void onReceiveDrag(DragEvent event) {
if (!_dragging && _rect != null && _rect.contains(event.initialPosition)) {
if (!_dragging && (_rectAction?.contains(event.initialPosition) ?? false)) {
if (enableDirection) {
_dragPosition = event.initialPosition;
_dragging = true;
@ -195,53 +197,14 @@ class JoystickAction {
}
}
// void actionDown(int pointer, Offset localPosition) {
// if (!_dragging && _rect != null && _rect.contains(localPosition)) {
// _pointerDragging = pointer;
// if (enableDirection) {
// _dragPosition = localPosition;
// _dragging = true;
// }
// _joystickController.joystickAction(
// JoystickActionEvent(
// id: actionId,
// event: ActionEvent.DOWN,
// ),
// );
// pressed();
// }
// }
// void actionMove(int pointer, Offset localPosition) {
// if (pointer == _pointerDragging) {
// if (_dragging) {
// _dragPosition = localPosition;
// }
// }
// }
// void actionUp(int pointer) {
// if (pointer == _pointerDragging) {
// _dragging = false;
// _dragPosition = _rectBackgroundDirection.center;
// _joystickController.joystickAction(
// JoystickActionEvent(
// id: actionId,
// event: ActionEvent.UP,
// ),
// );
// unPressed();
// }
// }
void pressed() {
if (spritePressed != null) {
_sprite = spritePressed;
_spriteAction = spritePressed;
}
}
void unPressed() {
_sprite = sprite;
_spriteAction = sprite;
}
void onPanUpdate(DragUpdateDetails details) {
@ -270,7 +233,7 @@ class JoystickAction {
_joystickController.joystickAction(
JoystickActionEvent(
id: actionId,
event: ActionEvent.UP,
event: ActionEvent.CANCEL,
),
);
unPressed();

View File

@ -7,6 +7,8 @@ import 'package:flame/sprite.dart';
import 'package:flutter/material.dart';
class JoystickDirectional {
static const double _backgroundAspectRatio = 2.2;
final double size;
final Sprite spriteBackgroundDirectional;
final Sprite spriteKnobDirectional;
@ -17,17 +19,14 @@ class JoystickDirectional {
Paint _paintBackground;
Paint _paintKnob;
double _backgroundAspectRatio = 2.2;
Rect _backgroundRect;
Sprite _backgroundSprite;
Rect _knobRect;
Sprite _knobSprite;
Rect _backgroundRect;
Rect _knobRect;
bool _dragging = false;
Offset _dragPosition;
double _tileSize;
JoystickController _joystickController;
@ -65,7 +64,7 @@ class JoystickDirectional {
void initialize(Size _screenSize, JoystickController joystickController) {
this._screenSize = _screenSize;
_joystickController = joystickController;
Offset osBackground =
final Offset osBackground =
Offset(margin.left, _screenSize.height - margin.bottom);
_backgroundRect = Rect.fromCircle(center: osBackground, radius: size / 2);
@ -81,7 +80,7 @@ class JoystickDirectional {
if (_backgroundSprite != null) {
_backgroundSprite.renderRect(canvas, _backgroundRect);
} else {
double radiusBackground = _backgroundRect.width / 2;
final double radiusBackground = _backgroundRect.width / 2;
canvas.drawCircle(
Offset(_backgroundRect.left + radiusBackground,
_backgroundRect.top + radiusBackground),
@ -95,7 +94,7 @@ class JoystickDirectional {
if (_knobSprite != null) {
_knobSprite.renderRect(canvas, _knobRect);
} else {
double radiusKnob = _knobRect.width / 2;
final double radiusKnob = _knobRect.width / 2;
canvas.drawCircle(
Offset(_knobRect.left + radiusKnob, _knobRect.top + radiusKnob),
radiusKnob,
@ -107,14 +106,17 @@ class JoystickDirectional {
void update(double t) {
if (_dragging) {
double _radAngle = atan2(_dragPosition.dy - _backgroundRect.center.dy,
final double _radAngle = atan2(
_dragPosition.dy - _backgroundRect.center.dy,
_dragPosition.dx - _backgroundRect.center.dx);
double degrees = _radAngle * 180 / pi;
final double degrees = _radAngle * 180 / pi;
// Distance between the center of joystick background & drag position
Point p = Point(_backgroundRect.center.dx, _backgroundRect.center.dy);
double dist = p.distanceTo(Point(_dragPosition.dx, _dragPosition.dy));
final Point centerPoint =
Point(_backgroundRect.center.dx, _backgroundRect.center.dy);
double dist =
centerPoint.distanceTo(Point(_dragPosition.dx, _dragPosition.dy));
// The maximum distance for the knob position the edge of
// the background + half of its own size. The knob can wander in the
@ -124,16 +126,16 @@ class JoystickDirectional {
: (_tileSize * _backgroundAspectRatio / 3);
// Calculation the knob position
double nextX = dist * cos(_radAngle);
double nextY = dist * sin(_radAngle);
Offset nextPoint = Offset(nextX, nextY);
final double nextX = dist * cos(_radAngle);
final double nextY = dist * sin(_radAngle);
final Offset nextPoint = Offset(nextX, nextY);
Offset diff = Offset(_backgroundRect.center.dx + nextPoint.dx,
final Offset diff = Offset(_backgroundRect.center.dx + nextPoint.dx,
_backgroundRect.center.dy + nextPoint.dy) -
_knobRect.center;
_knobRect = _knobRect.shift(diff);
double _intensity = dist / (_tileSize * _backgroundAspectRatio / 3);
final double _intensity = dist / (_tileSize * _backgroundAspectRatio / 3);
if (_intensity == 0) {
_joystickController.joystickChangeDirectional(JoystickDirectionalEvent(
@ -210,7 +212,7 @@ class JoystickDirectional {
}
} else {
if (_knobRect != null) {
Offset diff = _dragPosition - _knobRect.center;
final Offset diff = _dragPosition - _knobRect.center;
_knobRect = _knobRect.shift(diff);
}
}
@ -219,12 +221,13 @@ class JoystickDirectional {
void onReceiveDrag(DragEvent event) {
_updateDirectionalRect(event.initialPosition);
Rect directional = Rect.fromLTWH(
final Rect directional = Rect.fromLTWH(
_backgroundRect.left - 50,
_backgroundRect.top - 50,
_backgroundRect.width + 100,
_backgroundRect.height + 100,
);
if (!_dragging && directional.contains(event.initialPosition)) {
_dragging = true;
_dragPosition = event.initialPosition;
@ -240,12 +243,16 @@ class JoystickDirectional {
if (_screenSize != null &&
(position.dx > _screenSize.width / 2 ||
position.dy < _screenSize.height / 2 ||
isFixed)) return;
isFixed)) {
return;
}
_backgroundRect = Rect.fromCircle(center: position, radius: size / 2);
Offset osKnob =
Offset(_backgroundRect.center.dx, _backgroundRect.center.dy);
final Offset osKnob = Offset(
_backgroundRect.center.dx,
_backgroundRect.center.dy,
);
_knobRect = Rect.fromCircle(center: osKnob, radius: size / 4);
}

View File

@ -10,7 +10,7 @@ enum JoystickMoveDirectional {
IDLE
}
enum ActionEvent { DOWN, UP, MOVE }
enum ActionEvent { DOWN, UP, MOVE, CANCEL }
class JoystickDirectionalEvent {
final JoystickMoveDirectional directional;