Skip to content

Commit

Permalink
Rotation #7
Browse files Browse the repository at this point in the history
  • Loading branch information
SaadArdati committed Aug 14, 2024
1 parent 100a41b commit 8b3fb5a
Showing 1 changed file with 32 additions and 13 deletions.
45 changes: 32 additions & 13 deletions packages/flutter_box_transform/lib/src/transformable_box.dart
Original file line number Diff line number Diff line change
Expand Up @@ -371,11 +371,14 @@ class TransformableBox extends StatefulWidget {

enum _PrimaryGestureOperation {
resize,
drag;
drag,
rotate;

bool get isDragging => this == _PrimaryGestureOperation.drag;

bool get isResizing => this == _PrimaryGestureOperation.resize;

bool get isRotating => this == _PrimaryGestureOperation.rotate;
}

class _TransformableBoxState extends State<TransformableBox> {
Expand All @@ -389,7 +392,9 @@ class _TransformableBoxState extends State<TransformableBox> {

bool get isResizing => primaryGestureOperation?.isResizing == true;

bool get isGestureActive => isDragging || isResizing;
bool get isRotating => primaryGestureOperation?.isRotating == true;

bool get isGestureActive => isDragging || isResizing || isRotating;

bool mismatchedHandle(HandlePosition handle) =>
lastHandle != null && lastHandle != handle;
Expand Down Expand Up @@ -595,28 +600,35 @@ class _TransformableBoxState extends State<TransformableBox> {
};

void onHandleRotateStart(DragStartDetails event, HandlePosition handle) {
if (isGestureActive) return;

primaryGestureOperation = _PrimaryGestureOperation.rotate;
lastHandle = handle;

final offset =
widget.handleAlignment.offset(widget.rotationHandleGestureSize);
initialPos = rectQuadrantOffset(handle.quadrant) +
event.localPosition -
Offset(offset, offset);
localPos = initialPos;
setState(() {});

// Two fingers were used to start the drag. This produces issues with
// the box drag event. Therefore, we ignore it.
if (event.kind == PointerDeviceKind.trackpad) {
isLegalGesture = false;
return;
} else {
isLegalGesture = true;
}
// if (event.kind == PointerDeviceKind.trackpad) {
// isLegalGesture = false;
// return;
// } else {
// isLegalGesture = true;
// }

controller.onRotateStart(initialPos);
widget.onRotationStart?.call(handle, event);
}

void onHandleRotateUpdate(DragUpdateDetails event, HandlePosition handle) {
if (!isLegalGesture) return;
if (!isGestureActive) return;

final offset =
widget.handleAlignment.offset(widget.rotationHandleGestureSize);
localPos = rectQuadrantOffset(handle.quadrant) +
Expand Down Expand Up @@ -652,7 +664,10 @@ class _TransformableBoxState extends State<TransformableBox> {
}

void onHandleRotateEnd(DragEndDetails event, HandlePosition handle) {
if (!isLegalGesture) return;
if (!isGestureActive) return;

primaryGestureOperation = null;
lastHandle = null;

controller.onRotateEnd();
widget.onRotationEnd?.call(handle, event);
Expand All @@ -666,7 +681,10 @@ class _TransformableBoxState extends State<TransformableBox> {
}

void onHandleRotateCancel(HandlePosition handle) {
if (!isLegalGesture) return;
if (!isGestureActive) return;

primaryGestureOperation = null;
lastHandle = null;

controller.onRotateEnd();
widget.onRotationCancel?.call(handle);
Expand Down Expand Up @@ -825,7 +843,8 @@ class _TransformableBoxState extends State<TransformableBox> {
resizeHandleGestureSize: widget.resizeHandleGestureSize,
rotationHandleGestureSize:
widget.rotationHandleGestureSize,
supportedDevices: widget.supportedResizeDevices,enabled: widget.enabledHandles.contains(handle),
supportedDevices: widget.supportedResizeDevices,
enabled: widget.enabledHandles.contains(handle),
visible: widget.visibleHandles.contains(handle),
rotatable: widget.rotatable,
// Resize
Expand Down Expand Up @@ -855,7 +874,7 @@ class _TransformableBoxState extends State<TransformableBox> {
rotationHandleGestureSize:
widget.rotationHandleGestureSize,
rotatable: widget.rotatable,
supportedDevices: widget.supportedResizeDevices,
supportedDevices: widget.supportedResizeDevices,
enabled: widget.enabledHandles.contains(handle),
visible: widget.visibleHandles.contains(handle),
onPanStart: (event) => onHandlePanStart(event, handle),
Expand Down

0 comments on commit 8b3fb5a

Please sign in to comment.