Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add mouse-focus scaling signal #173

Merged
merged 3 commits into from
Dec 8, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 37 additions & 17 deletions lib/src/common/defaults.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import 'package:graphic/src/interaction/gesture.dart';

/// Gets signal update for different dimensions.
SignalUpdater<List<double>> _getRangeUpdate(
double Function(ScaleUpdateDetails) getDeltaDim,
double Function(ScaleUpdateDetails) getScaleDim,
double Function(Size) getSizeDim) =>
bool isHorizontal, bool focusMouseScale) =>
(
List<double> init,
List<double> pre,
Expand All @@ -25,12 +23,19 @@ SignalUpdater<List<double>> _getRangeUpdate(
if (detail.pointerCount == 1) {
// Panning.

final deltaRatio = getDeltaDim(gesture.preScaleDetail!);
final delta = deltaRatio / getSizeDim(gesture.chartSize);
final deltaRatio = isHorizontal
? gesture.preScaleDetail!.focalPointDelta.dx
: -gesture.preScaleDetail!.focalPointDelta.dy;
final delta = deltaRatio /
(isHorizontal
? gesture.chartSize.width
: gesture.chartSize.height);
return [pre.first + delta, pre.last + delta];
} else {
// Scaling.

double Function(ScaleUpdateDetails) getScaleDim =
(p0) => isHorizontal ? p0.horizontalScale : p0.verticalScale;
final preScale = getScaleDim(gesture.preScaleDetail!);
final scale = getScaleDim(detail);
final deltaRatio = (scale - preScale) / preScale / 2;
Expand All @@ -39,7 +44,7 @@ SignalUpdater<List<double>> _getRangeUpdate(
return [pre.first - delta, pre.last + delta];
}
} else if (gesture.type == GestureType.scroll) {
const step = 0.1;
const step = -0.1;
final scrollDelta = gesture.details as Offset;
final deltaRatio = scrollDelta.dy == 0
? 0.0
Expand All @@ -48,7 +53,21 @@ SignalUpdater<List<double>> _getRangeUpdate(
: (-step / 2);
final preRange = pre.last - pre.first;
final delta = deltaRatio * preRange;
return [pre.first - delta, pre.last + delta];
if (!focusMouseScale) {
return [pre.first - delta, pre.last + delta];
} else {
double mousePos;
if (isHorizontal) {
mousePos = (gesture.localPosition.dx - 39.5) / (gesture.chartSize.width - 51);
} else {
mousePos = 1 - (gesture.localPosition.dy - 5) / (gesture.chartSize.height - 25);
}
mousePos = (mousePos - pre.first) / (pre.last - pre.first);
return [
pre.first - delta * 2 * mousePos,
pre.last + delta * 2 * (1 - mousePos)
];
}
} else if (gesture.type == GestureType.doubleTap) {
return init;
}
Expand Down Expand Up @@ -158,16 +177,17 @@ abstract class Defaults {

/// A signal update for scaling and panning horizontal coordinate range.
static SignalUpdater<List<double>> get horizontalRangeSignal =>
_getRangeUpdate(
(detail) => detail.focalPointDelta.dx,
(detail) => detail.horizontalScale,
(size) => size.width,
);
_getRangeUpdate(true, false);

/// A signal update for scaling and panning vertical coordinate range.
static SignalUpdater<List<double>> get verticalRangeSignal => _getRangeUpdate(
(detail) => -detail.focalPointDelta.dy,
(detail) => detail.verticalScale,
(size) => size.height,
);
static SignalUpdater<List<double>> get verticalRangeSignal =>
_getRangeUpdate(false, false);

/// A signal update for scaling and panning horizontal coordinate range by cursor focus.
static SignalUpdater<List<double>> get horizontalRangeFocusSignal =>
_getRangeUpdate(true, true);

/// A signal update for scaling and panning vertical coordinate range by cursor focus.
static SignalUpdater<List<double>> get verticalRangeFocusSignal =>
_getRangeUpdate(false, true);
}