mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-12 19:01:09 +08:00
improvements code
This commit is contained in:
@@ -15,22 +15,21 @@ class JoystickAction {
|
|||||||
final Sprite spritePressed;
|
final Sprite spritePressed;
|
||||||
final Sprite spriteBackgroundDirection;
|
final Sprite spriteBackgroundDirection;
|
||||||
final double size;
|
final double size;
|
||||||
double sizeBackgroundDirection;
|
final double sizeFactorBackgroundDirection;
|
||||||
final EdgeInsets margin;
|
final EdgeInsets margin;
|
||||||
final JoystickActionAlign align;
|
final JoystickActionAlign align;
|
||||||
final bool enableDirection;
|
final bool enableDirection;
|
||||||
final Color color;
|
final Color color;
|
||||||
|
|
||||||
Rect _rect;
|
Rect _rectAction;
|
||||||
Rect _rectBackgroundDirection;
|
Rect _rectBackgroundDirection;
|
||||||
bool _dragging = false;
|
bool _dragging = false;
|
||||||
Sprite _sprite;
|
Sprite _spriteAction;
|
||||||
double _tileSize;
|
|
||||||
Offset _dragPosition;
|
Offset _dragPosition;
|
||||||
Paint _paintBackground;
|
Paint _paintBackground;
|
||||||
Paint _paintAction;
|
Paint _paintAction;
|
||||||
JoystickController _joystickController;
|
JoystickController _joystickController;
|
||||||
|
double _sizeBackgroundDirection;
|
||||||
DragEvent _currentDragEvent;
|
DragEvent _currentDragEvent;
|
||||||
|
|
||||||
JoystickAction({
|
JoystickAction({
|
||||||
@@ -40,14 +39,13 @@ class JoystickAction {
|
|||||||
this.spriteBackgroundDirection,
|
this.spriteBackgroundDirection,
|
||||||
this.enableDirection = false,
|
this.enableDirection = false,
|
||||||
this.size = 50,
|
this.size = 50,
|
||||||
this.sizeBackgroundDirection,
|
this.sizeFactorBackgroundDirection = 1.5,
|
||||||
this.margin = EdgeInsets.zero,
|
this.margin = EdgeInsets.zero,
|
||||||
this.color = Colors.blueGrey,
|
this.color = Colors.blueGrey,
|
||||||
this.align = JoystickActionAlign.BOTTOM_RIGHT,
|
this.align = JoystickActionAlign.BOTTOM_RIGHT,
|
||||||
}) {
|
}) {
|
||||||
_sprite = sprite;
|
_spriteAction = sprite;
|
||||||
sizeBackgroundDirection = sizeBackgroundDirection ?? size * 1.5;
|
_sizeBackgroundDirection = sizeFactorBackgroundDirection * size;
|
||||||
_tileSize = sizeBackgroundDirection;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void initialize(Size _screenSize, JoystickController joystickController) {
|
void initialize(Size _screenSize, JoystickController joystickController) {
|
||||||
@@ -72,13 +70,13 @@ class JoystickAction {
|
|||||||
dy = _screenSize.height - (margin.bottom + radius);
|
dy = _screenSize.height - (margin.bottom + radius);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
_rect = Rect.fromCircle(
|
_rectAction = Rect.fromCircle(
|
||||||
center: Offset(dx, dy),
|
center: Offset(dx, dy),
|
||||||
radius: radius,
|
radius: radius,
|
||||||
);
|
);
|
||||||
_rectBackgroundDirection = Rect.fromCircle(
|
_rectBackgroundDirection = Rect.fromCircle(
|
||||||
center: Offset(dx, dy),
|
center: Offset(dx, dy),
|
||||||
radius: sizeBackgroundDirection / 2,
|
radius: _sizeBackgroundDirection / 2,
|
||||||
);
|
);
|
||||||
|
|
||||||
_paintBackground = Paint()
|
_paintBackground = Paint()
|
||||||
@@ -89,13 +87,13 @@ class JoystickAction {
|
|||||||
..color = color.withOpacity(0.8)
|
..color = color.withOpacity(0.8)
|
||||||
..style = PaintingStyle.fill;
|
..style = PaintingStyle.fill;
|
||||||
|
|
||||||
_dragPosition = _rect.center;
|
_dragPosition = _rectAction.center;
|
||||||
}
|
}
|
||||||
|
|
||||||
void render(Canvas c) {
|
void render(Canvas c) {
|
||||||
if (_rectBackgroundDirection != null && _dragging && enableDirection) {
|
if (_rectBackgroundDirection != null && _dragging && enableDirection) {
|
||||||
if (spriteBackgroundDirection == null) {
|
if (spriteBackgroundDirection == null) {
|
||||||
double radiusBackground = _rectBackgroundDirection.width / 2;
|
final double radiusBackground = _rectBackgroundDirection.width / 2;
|
||||||
c.drawCircle(
|
c.drawCircle(
|
||||||
Offset(
|
Offset(
|
||||||
_rectBackgroundDirection.left + radiusBackground,
|
_rectBackgroundDirection.left + radiusBackground,
|
||||||
@@ -109,14 +107,16 @@ class JoystickAction {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_sprite != null) {
|
if (_spriteAction != null) {
|
||||||
if (_rect != null) _sprite.renderRect(c, _rect);
|
if (_rectAction != null) {
|
||||||
|
_spriteAction.renderRect(c, _rectAction);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
double radiusAction = _rect.width / 2;
|
final double radiusAction = _rectAction.width / 2;
|
||||||
c.drawCircle(
|
c.drawCircle(
|
||||||
Offset(
|
Offset(
|
||||||
_rect.left + radiusAction,
|
_rectAction.left + radiusAction,
|
||||||
_rect.top + radiusAction,
|
_rectAction.top + radiusAction,
|
||||||
),
|
),
|
||||||
radiusAction,
|
radiusAction,
|
||||||
_paintAction,
|
_paintAction,
|
||||||
@@ -126,13 +126,13 @@ class JoystickAction {
|
|||||||
|
|
||||||
void update(double dt) {
|
void update(double dt) {
|
||||||
if (_dragging) {
|
if (_dragging) {
|
||||||
double _radAngle = atan2(
|
final double _radAngle = atan2(
|
||||||
_dragPosition.dy - _rectBackgroundDirection.center.dy,
|
_dragPosition.dy - _rectBackgroundDirection.center.dy,
|
||||||
_dragPosition.dx - _rectBackgroundDirection.center.dx,
|
_dragPosition.dx - _rectBackgroundDirection.center.dx,
|
||||||
);
|
);
|
||||||
|
|
||||||
// Distance between the center of joystick background & drag position
|
// Distance between the center of joystick background & drag position
|
||||||
Point centerPoint = Point(
|
final Point centerPoint = Point(
|
||||||
_rectBackgroundDirection.center.dx,
|
_rectBackgroundDirection.center.dx,
|
||||||
_rectBackgroundDirection.center.dy,
|
_rectBackgroundDirection.center.dy,
|
||||||
);
|
);
|
||||||
@@ -142,7 +142,9 @@ class JoystickAction {
|
|||||||
// The maximum distance for the knob position the edge of
|
// The maximum distance for the knob position the edge of
|
||||||
// the background + half of its own size. The knob can wander in the
|
// the background + half of its own size. The knob can wander in the
|
||||||
// background image, but not outside.
|
// background image, but not outside.
|
||||||
dist = dist < (_tileSize / 3) ? dist : (_tileSize / 3);
|
dist = dist < (_sizeBackgroundDirection / 3)
|
||||||
|
? dist
|
||||||
|
: (_sizeBackgroundDirection / 3);
|
||||||
|
|
||||||
// Calculation the knob position
|
// Calculation the knob position
|
||||||
final double nextX = dist * cos(_radAngle);
|
final double nextX = dist * cos(_radAngle);
|
||||||
@@ -153,10 +155,10 @@ class JoystickAction {
|
|||||||
_rectBackgroundDirection.center.dx + nextPoint.dx,
|
_rectBackgroundDirection.center.dx + nextPoint.dx,
|
||||||
_rectBackgroundDirection.center.dy + nextPoint.dy,
|
_rectBackgroundDirection.center.dy + nextPoint.dy,
|
||||||
) -
|
) -
|
||||||
_rect.center;
|
_rectAction.center;
|
||||||
_rect = _rect.shift(diff);
|
_rectAction = _rectAction.shift(diff);
|
||||||
|
|
||||||
final double _intensity = dist / (_tileSize / 3);
|
final double _intensity = dist / (_sizeBackgroundDirection / 3);
|
||||||
|
|
||||||
_joystickController.joystickAction(
|
_joystickController.joystickAction(
|
||||||
JoystickActionEvent(
|
JoystickActionEvent(
|
||||||
@@ -167,15 +169,15 @@ class JoystickAction {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
if (_rect != null) {
|
if (_rectAction != null) {
|
||||||
final Offset diff = _dragPosition - _rect.center;
|
final Offset diff = _dragPosition - _rectAction.center;
|
||||||
_rect = _rect.shift(diff);
|
_rectAction = _rectAction.shift(diff);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void onReceiveDrag(DragEvent event) {
|
void onReceiveDrag(DragEvent event) {
|
||||||
if (!_dragging && _rect != null && _rect.contains(event.initialPosition)) {
|
if (!_dragging && (_rectAction?.contains(event.initialPosition) ?? false)) {
|
||||||
if (enableDirection) {
|
if (enableDirection) {
|
||||||
_dragPosition = event.initialPosition;
|
_dragPosition = event.initialPosition;
|
||||||
_dragging = true;
|
_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() {
|
void pressed() {
|
||||||
if (spritePressed != null) {
|
if (spritePressed != null) {
|
||||||
_sprite = spritePressed;
|
_spriteAction = spritePressed;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void unPressed() {
|
void unPressed() {
|
||||||
_sprite = sprite;
|
_spriteAction = sprite;
|
||||||
}
|
}
|
||||||
|
|
||||||
void onPanUpdate(DragUpdateDetails details) {
|
void onPanUpdate(DragUpdateDetails details) {
|
||||||
@@ -270,7 +233,7 @@ class JoystickAction {
|
|||||||
_joystickController.joystickAction(
|
_joystickController.joystickAction(
|
||||||
JoystickActionEvent(
|
JoystickActionEvent(
|
||||||
id: actionId,
|
id: actionId,
|
||||||
event: ActionEvent.UP,
|
event: ActionEvent.CANCEL,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
unPressed();
|
unPressed();
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ import 'package:flame/sprite.dart';
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
class JoystickDirectional {
|
class JoystickDirectional {
|
||||||
|
static const double _backgroundAspectRatio = 2.2;
|
||||||
|
|
||||||
final double size;
|
final double size;
|
||||||
final Sprite spriteBackgroundDirectional;
|
final Sprite spriteBackgroundDirectional;
|
||||||
final Sprite spriteKnobDirectional;
|
final Sprite spriteKnobDirectional;
|
||||||
@@ -17,17 +19,14 @@ class JoystickDirectional {
|
|||||||
Paint _paintBackground;
|
Paint _paintBackground;
|
||||||
Paint _paintKnob;
|
Paint _paintKnob;
|
||||||
|
|
||||||
double _backgroundAspectRatio = 2.2;
|
|
||||||
Rect _backgroundRect;
|
|
||||||
Sprite _backgroundSprite;
|
Sprite _backgroundSprite;
|
||||||
|
|
||||||
Rect _knobRect;
|
|
||||||
Sprite _knobSprite;
|
Sprite _knobSprite;
|
||||||
|
|
||||||
|
Rect _backgroundRect;
|
||||||
|
Rect _knobRect;
|
||||||
|
|
||||||
bool _dragging = false;
|
bool _dragging = false;
|
||||||
|
|
||||||
Offset _dragPosition;
|
Offset _dragPosition;
|
||||||
|
|
||||||
double _tileSize;
|
double _tileSize;
|
||||||
|
|
||||||
JoystickController _joystickController;
|
JoystickController _joystickController;
|
||||||
@@ -65,7 +64,7 @@ class JoystickDirectional {
|
|||||||
void initialize(Size _screenSize, JoystickController joystickController) {
|
void initialize(Size _screenSize, JoystickController joystickController) {
|
||||||
this._screenSize = _screenSize;
|
this._screenSize = _screenSize;
|
||||||
_joystickController = joystickController;
|
_joystickController = joystickController;
|
||||||
Offset osBackground =
|
final Offset osBackground =
|
||||||
Offset(margin.left, _screenSize.height - margin.bottom);
|
Offset(margin.left, _screenSize.height - margin.bottom);
|
||||||
_backgroundRect = Rect.fromCircle(center: osBackground, radius: size / 2);
|
_backgroundRect = Rect.fromCircle(center: osBackground, radius: size / 2);
|
||||||
|
|
||||||
@@ -81,7 +80,7 @@ class JoystickDirectional {
|
|||||||
if (_backgroundSprite != null) {
|
if (_backgroundSprite != null) {
|
||||||
_backgroundSprite.renderRect(canvas, _backgroundRect);
|
_backgroundSprite.renderRect(canvas, _backgroundRect);
|
||||||
} else {
|
} else {
|
||||||
double radiusBackground = _backgroundRect.width / 2;
|
final double radiusBackground = _backgroundRect.width / 2;
|
||||||
canvas.drawCircle(
|
canvas.drawCircle(
|
||||||
Offset(_backgroundRect.left + radiusBackground,
|
Offset(_backgroundRect.left + radiusBackground,
|
||||||
_backgroundRect.top + radiusBackground),
|
_backgroundRect.top + radiusBackground),
|
||||||
@@ -95,7 +94,7 @@ class JoystickDirectional {
|
|||||||
if (_knobSprite != null) {
|
if (_knobSprite != null) {
|
||||||
_knobSprite.renderRect(canvas, _knobRect);
|
_knobSprite.renderRect(canvas, _knobRect);
|
||||||
} else {
|
} else {
|
||||||
double radiusKnob = _knobRect.width / 2;
|
final double radiusKnob = _knobRect.width / 2;
|
||||||
canvas.drawCircle(
|
canvas.drawCircle(
|
||||||
Offset(_knobRect.left + radiusKnob, _knobRect.top + radiusKnob),
|
Offset(_knobRect.left + radiusKnob, _knobRect.top + radiusKnob),
|
||||||
radiusKnob,
|
radiusKnob,
|
||||||
@@ -107,14 +106,17 @@ class JoystickDirectional {
|
|||||||
|
|
||||||
void update(double t) {
|
void update(double t) {
|
||||||
if (_dragging) {
|
if (_dragging) {
|
||||||
double _radAngle = atan2(_dragPosition.dy - _backgroundRect.center.dy,
|
final double _radAngle = atan2(
|
||||||
|
_dragPosition.dy - _backgroundRect.center.dy,
|
||||||
_dragPosition.dx - _backgroundRect.center.dx);
|
_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
|
// Distance between the center of joystick background & drag position
|
||||||
Point p = Point(_backgroundRect.center.dx, _backgroundRect.center.dy);
|
final Point centerPoint =
|
||||||
double dist = p.distanceTo(Point(_dragPosition.dx, _dragPosition.dy));
|
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 maximum distance for the knob position the edge of
|
||||||
// the background + half of its own size. The knob can wander in the
|
// the background + half of its own size. The knob can wander in the
|
||||||
@@ -124,16 +126,16 @@ class JoystickDirectional {
|
|||||||
: (_tileSize * _backgroundAspectRatio / 3);
|
: (_tileSize * _backgroundAspectRatio / 3);
|
||||||
|
|
||||||
// Calculation the knob position
|
// Calculation the knob position
|
||||||
double nextX = dist * cos(_radAngle);
|
final double nextX = dist * cos(_radAngle);
|
||||||
double nextY = dist * sin(_radAngle);
|
final double nextY = dist * sin(_radAngle);
|
||||||
Offset nextPoint = Offset(nextX, nextY);
|
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) -
|
_backgroundRect.center.dy + nextPoint.dy) -
|
||||||
_knobRect.center;
|
_knobRect.center;
|
||||||
_knobRect = _knobRect.shift(diff);
|
_knobRect = _knobRect.shift(diff);
|
||||||
|
|
||||||
double _intensity = dist / (_tileSize * _backgroundAspectRatio / 3);
|
final double _intensity = dist / (_tileSize * _backgroundAspectRatio / 3);
|
||||||
|
|
||||||
if (_intensity == 0) {
|
if (_intensity == 0) {
|
||||||
_joystickController.joystickChangeDirectional(JoystickDirectionalEvent(
|
_joystickController.joystickChangeDirectional(JoystickDirectionalEvent(
|
||||||
@@ -210,7 +212,7 @@ class JoystickDirectional {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (_knobRect != null) {
|
if (_knobRect != null) {
|
||||||
Offset diff = _dragPosition - _knobRect.center;
|
final Offset diff = _dragPosition - _knobRect.center;
|
||||||
_knobRect = _knobRect.shift(diff);
|
_knobRect = _knobRect.shift(diff);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -219,12 +221,13 @@ class JoystickDirectional {
|
|||||||
void onReceiveDrag(DragEvent event) {
|
void onReceiveDrag(DragEvent event) {
|
||||||
_updateDirectionalRect(event.initialPosition);
|
_updateDirectionalRect(event.initialPosition);
|
||||||
|
|
||||||
Rect directional = Rect.fromLTWH(
|
final Rect directional = Rect.fromLTWH(
|
||||||
_backgroundRect.left - 50,
|
_backgroundRect.left - 50,
|
||||||
_backgroundRect.top - 50,
|
_backgroundRect.top - 50,
|
||||||
_backgroundRect.width + 100,
|
_backgroundRect.width + 100,
|
||||||
_backgroundRect.height + 100,
|
_backgroundRect.height + 100,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!_dragging && directional.contains(event.initialPosition)) {
|
if (!_dragging && directional.contains(event.initialPosition)) {
|
||||||
_dragging = true;
|
_dragging = true;
|
||||||
_dragPosition = event.initialPosition;
|
_dragPosition = event.initialPosition;
|
||||||
@@ -240,12 +243,16 @@ class JoystickDirectional {
|
|||||||
if (_screenSize != null &&
|
if (_screenSize != null &&
|
||||||
(position.dx > _screenSize.width / 2 ||
|
(position.dx > _screenSize.width / 2 ||
|
||||||
position.dy < _screenSize.height / 2 ||
|
position.dy < _screenSize.height / 2 ||
|
||||||
isFixed)) return;
|
isFixed)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
_backgroundRect = Rect.fromCircle(center: position, radius: size / 2);
|
_backgroundRect = Rect.fromCircle(center: position, radius: size / 2);
|
||||||
|
|
||||||
Offset osKnob =
|
final Offset osKnob = Offset(
|
||||||
Offset(_backgroundRect.center.dx, _backgroundRect.center.dy);
|
_backgroundRect.center.dx,
|
||||||
|
_backgroundRect.center.dy,
|
||||||
|
);
|
||||||
_knobRect = Rect.fromCircle(center: osKnob, radius: size / 4);
|
_knobRect = Rect.fromCircle(center: osKnob, radius: size / 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ enum JoystickMoveDirectional {
|
|||||||
IDLE
|
IDLE
|
||||||
}
|
}
|
||||||
|
|
||||||
enum ActionEvent { DOWN, UP, MOVE }
|
enum ActionEvent { DOWN, UP, MOVE, CANCEL }
|
||||||
|
|
||||||
class JoystickDirectionalEvent {
|
class JoystickDirectionalEvent {
|
||||||
final JoystickMoveDirectional directional;
|
final JoystickMoveDirectional directional;
|
||||||
|
|||||||
Reference in New Issue
Block a user