Skip to content
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
b95ce23
add component based scaling gesture
stilnat Nov 13, 2025
4b8bc31
rename multi scale to scale
stilnat Nov 15, 2025
487e579
Delete my_tests_2 directory
stilnat Nov 15, 2025
01e81cd
Delete my_tests directory
stilnat Nov 15, 2025
8701f00
fix typo and format example
stilnat Nov 16, 2025
8b7105a
fix spelling
stilnat Nov 16, 2025
9723f97
remove TODO
stilnat Nov 16, 2025
28e24bf
format scale dispatcher
stilnat Nov 16, 2025
9c2acf6
remove automatically create multi drag dispatcher in Scale dispatcher
stilnat Nov 16, 2025
975bbdb
remove french
stilnat Nov 16, 2025
bea44fc
format scale dispatcher
stilnat Nov 16, 2025
d6f78cb
test first batch
stilnat Nov 19, 2025
987f977
add new test
stilnat Nov 21, 2025
f7b7153
format and change zoom api
stilnat Nov 21, 2025
cbc2333
fix name not recognised
stilnat Nov 21, 2025
6162fb3
format fix
stilnat Nov 21, 2025
528e665
Merge branch 'main' into scale-gesture
stilnat Nov 21, 2025
f945eea
fix melos analyse
stilnat Nov 21, 2025
8ddce28
format stuff
stilnat Nov 21, 2025
1ce9618
Merge remote-tracking branch 'upstream/main' into scale-gesture
stilnat Nov 21, 2025
c18fd0c
Merge branch 'main' into scale-gesture
spydon Nov 21, 2025
bf69b48
fix vector 2 creations
stilnat Nov 22, 2025
5369f79
add doc
stilnat Nov 22, 2025
c6e9b98
Merge branch 'main' into scale-gesture
stilnat Nov 22, 2025
0d10332
markdown fixes
stilnat Nov 22, 2025
4a8f8ad
Merge branch 'scale-gesture' of https://github.com/stilnat/flame into…
stilnat Nov 22, 2025
4441db2
fix markdown
stilnat Nov 22, 2025
47eb614
rename is scaled
stilnat Nov 22, 2025
64fe308
analyse fix
stilnat Nov 22, 2025
42f0d18
fix lint
stilnat Nov 22, 2025
9ed050a
Update doc/flame/inputs/scale_events.md
stilnat Nov 22, 2025
0eba91e
Update doc/flame/inputs/scale_events.md
stilnat Nov 22, 2025
ff19238
add dt dependency
stilnat Nov 22, 2025
967bfa3
Merge branch 'scale-gesture' of https://github.com/stilnat/flame into…
stilnat Nov 22, 2025
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
186 changes: 186 additions & 0 deletions doc/flame/inputs/scale_events.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
# Scale Events

**Scale events** occur when the user moves two fingers in a pinch in, or in a pinch out move.
Only one single scale gesture can occur at the same time.


For those components that you want to respond to scale events, add the `ScaleCallbacks` mixin.

- This mixin adds three overridable methods to your component: `onScaleStart`, `onScaleUpdate`,
`onScaleEnd`. By default, these methods do nothing -- they need to be
overridden in order to perform any function.
- In addition, the component must implement the `containsLocalPoint()` method (already implemented
in `PositionComponent`, so most of the time you don't need to do anything here) -- this method
allows Flame to know whether the event occurred within the component or not.

```dart
class MyComponent extends PositionComponent with ScaleCallbacks {
MyComponent() : super(size: Vector2(180, 120));

@override
void onScaleStart(ScaleStartEvent event) {
// Do something in response to a scale event
}
}
```


## Scale anatomy


### onScaleStart

This is the first event that occurs in a scale sequence. Usually, the event will be delivered to the
topmost component at the focal point (the point at the center of the line formed by the two fingers)
with the `ScaleCallbacks` mixin. However, by setting the flag
`event.continuePropagation` to true, you can allow the event to propagate to the components below.

The `ScaleStartEvent` object associated with this event will contain
the coordinate of the first focal point
recognized by the scale gesture recognizer. This point is available in multiple coordinate system:
`devicePosition` is given in the coordinate system of the entire device, `canvasPosition` is in the
coordinate system of the game widget, and `localPosition` provides the position in the component's
local coordinate system.

Any component that receives `onScaleStart` will later be receiving `onScaleUpdate` and `onScaleEnd`
events as well.


### onScaleUpdate

This event is fired continuously as user drags their finger across the screen. It will not fire if
the user is holding their finger still.

The default implementation delivers this event to all the components that received the previous
`onScaleStart`. If the point of touch is still within the component, then
`event.localPosition` will give the position of that point in the local coordinate system. However,
if the user moves their finger away from the component, the property `event.localPosition` will
return a point whose coordinates are NaNs. Likewise, the `event.renderingTrace` in this case will be
empty. However, the `canvasPosition` and `devicePosition` properties of the event will be valid.

In addition, the `ScaleUpdateEvent` will contain `focalPointDelta` --
the amount the focal point has moved since the
previous `onScaleUpdate`, or since the `onScaleStart` if this is the first scale-update after a scale-
start.

The `event.timestamp` property measures the time elapsed since the beginning of the scale. It can be
used, for example, to compute the speed of the movement.

The `event.rotation` property measures the angle of rotation in radians, between the line formed
from the two fingers at the start, and the line formed when this event is called.

The `event.scale` property measures the ratio of length between the line formed
from the two fingers at the start, and the line formed when this event is called.


### onScaleEnd

This event is fired when the user lifts their finger and thus stops the scale gesture. There is no
position associated with this event.


## Mixins


### ScaleCallbacks

The `ScaleCallbacks` mixin can be added to any `Component` in order for that component to start
receiving scale events.

This mixin adds methods `onScaleStart`, `onScaleUpdate`, `onScaleEnd` to the
component, which by default don't do anything, but can be overridden to implement any real
functionality.

Another crucial detail is that a component will only receive scale events that originate *within*
that component, as judged by the `containsLocalPoint()` function. The commonly-used
`PositionComponent` class provides such an implementation based on its `size` property. Thus, if
your component derives from a `PositionComponent`, then make sure that you set its size correctly.
If, however, your component derives from the bare `Component`, then the `containsLocalPoint()`
method must be implemented manually.

If your component is a part of a larger hierarchy, then it will only receive scale events if its
ancestors have all implemented the `containsLocalPoint` correctly.

```dart
class ScaleOnlyRectangle extends RectangleComponent with ScaleCallbacks {
ScaleOnlyRectangle({
required Vector2 position,
required Vector2 size,
Color color = Colors.blue,
Anchor anchor = Anchor.center,
}) : super(
position: position,
size: size,
anchor: anchor,
paint: Paint()..color = color,
);

@override
Future<void> onLoad() async {
final text = TextComponent(
text: 'scale',
textRenderer: TextPaint(
style: const TextStyle(fontSize: 25, color: Colors.white),
),
position: size / 2,
anchor: Anchor.center,
);
add(text);
}

bool isScaling = false;
double initialAngle = 0;
Vector2 initialScale = Vector2.all(1);
double lastScale = 1.0;

/// ScaleCallbacks overrides
@override
void onScaleStart(ScaleStartEvent event) {
super.onScaleStart(event);
isScaling = true;
initialAngle = angle;
initialScale = scale;
lastScale = 1.0;
debugPrint('Scale started at ${event.devicePosition}');
}

@override
void onScaleUpdate(ScaleUpdateEvent event) {
super.onScaleUpdate(event);
// scale rectangle size by pinch
angle = initialAngle + event.rotation;
// delta scale since last frame
if (lastScale == 0) {
return;
}
final scaleDelta = event.scale / lastScale;
lastScale = event.scale; // update for next frame

// apply delta gently
scale *= sqrt(scaleDelta);

// clamp
scale.clamp(Vector2.all(0.8), Vector2.all(3));
}

@override
void onScaleEnd(ScaleEndEvent event) {
super.onScaleEnd(event);
isScaling = false;
debugPrint('Scale ended with velocity ${event.velocity}');
}
}

```


## Scale and drag gestures interactions


A multi drag gesture can sometimes look exactly like a scale gesture.
This is the case for instance, if you try to move two components toward each other at the same time.
If you added both a component using ScaleCallbacks and
one using DragCallbacks (or one using both), this issue will arise.
The Scale gesture will win over the drag gesture
and prevent your user to perform the multi drag gesture as they wanted. This is a limitation
with the current implementation that devs need to be aware of.
Loading