Skip to content

Commit 5c81fb6

Browse files
authored
add mouse-focus scaling signal (#173)
* add mouse-focus scaling signal * fix any component size scale * suitable all size charts
1 parent 17c95ae commit 5c81fb6

1 file changed

Lines changed: 37 additions & 17 deletions

File tree

lib/src/common/defaults.dart

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ import 'package:graphic/src/interaction/gesture.dart';
88

99
/// Gets signal update for different dimensions.
1010
SignalUpdater<List<double>> _getRangeUpdate(
11-
double Function(ScaleUpdateDetails) getDeltaDim,
12-
double Function(ScaleUpdateDetails) getScaleDim,
13-
double Function(Size) getSizeDim) =>
11+
bool isHorizontal, bool focusMouseScale) =>
1412
(
1513
List<double> init,
1614
List<double> pre,
@@ -25,12 +23,19 @@ SignalUpdater<List<double>> _getRangeUpdate(
2523
if (detail.pointerCount == 1) {
2624
// Panning.
2725

28-
final deltaRatio = getDeltaDim(gesture.preScaleDetail!);
29-
final delta = deltaRatio / getSizeDim(gesture.chartSize);
26+
final deltaRatio = isHorizontal
27+
? gesture.preScaleDetail!.focalPointDelta.dx
28+
: -gesture.preScaleDetail!.focalPointDelta.dy;
29+
final delta = deltaRatio /
30+
(isHorizontal
31+
? gesture.chartSize.width
32+
: gesture.chartSize.height);
3033
return [pre.first + delta, pre.last + delta];
3134
} else {
3235
// Scaling.
3336

37+
double Function(ScaleUpdateDetails) getScaleDim =
38+
(p0) => isHorizontal ? p0.horizontalScale : p0.verticalScale;
3439
final preScale = getScaleDim(gesture.preScaleDetail!);
3540
final scale = getScaleDim(detail);
3641
final deltaRatio = (scale - preScale) / preScale / 2;
@@ -39,7 +44,7 @@ SignalUpdater<List<double>> _getRangeUpdate(
3944
return [pre.first - delta, pre.last + delta];
4045
}
4146
} else if (gesture.type == GestureType.scroll) {
42-
const step = 0.1;
47+
const step = -0.1;
4348
final scrollDelta = gesture.details as Offset;
4449
final deltaRatio = scrollDelta.dy == 0
4550
? 0.0
@@ -48,7 +53,21 @@ SignalUpdater<List<double>> _getRangeUpdate(
4853
: (-step / 2);
4954
final preRange = pre.last - pre.first;
5055
final delta = deltaRatio * preRange;
51-
return [pre.first - delta, pre.last + delta];
56+
if (!focusMouseScale) {
57+
return [pre.first - delta, pre.last + delta];
58+
} else {
59+
double mousePos;
60+
if (isHorizontal) {
61+
mousePos = (gesture.localPosition.dx - 39.5) / (gesture.chartSize.width - 51);
62+
} else {
63+
mousePos = 1 - (gesture.localPosition.dy - 5) / (gesture.chartSize.height - 25);
64+
}
65+
mousePos = (mousePos - pre.first) / (pre.last - pre.first);
66+
return [
67+
pre.first - delta * 2 * mousePos,
68+
pre.last + delta * 2 * (1 - mousePos)
69+
];
70+
}
5271
} else if (gesture.type == GestureType.doubleTap) {
5372
return init;
5473
}
@@ -158,16 +177,17 @@ abstract class Defaults {
158177

159178
/// A signal update for scaling and panning horizontal coordinate range.
160179
static SignalUpdater<List<double>> get horizontalRangeSignal =>
161-
_getRangeUpdate(
162-
(detail) => detail.focalPointDelta.dx,
163-
(detail) => detail.horizontalScale,
164-
(size) => size.width,
165-
);
180+
_getRangeUpdate(true, false);
166181

167182
/// A signal update for scaling and panning vertical coordinate range.
168-
static SignalUpdater<List<double>> get verticalRangeSignal => _getRangeUpdate(
169-
(detail) => -detail.focalPointDelta.dy,
170-
(detail) => detail.verticalScale,
171-
(size) => size.height,
172-
);
183+
static SignalUpdater<List<double>> get verticalRangeSignal =>
184+
_getRangeUpdate(false, false);
185+
186+
/// A signal update for scaling and panning horizontal coordinate range by cursor focus.
187+
static SignalUpdater<List<double>> get horizontalRangeFocusSignal =>
188+
_getRangeUpdate(true, true);
189+
190+
/// A signal update for scaling and panning vertical coordinate range by cursor focus.
191+
static SignalUpdater<List<double>> get verticalRangeFocusSignal =>
192+
_getRangeUpdate(false, true);
173193
}

0 commit comments

Comments
 (0)