Skip to content

Commit 29026f6

Browse files
Misko LeeMisko Lee
Misko Lee
authored and
Misko Lee
committed
1. support long press like flutter
1 parent 2cf0815 commit 29026f6

File tree

8 files changed

+89
-29
lines changed

8 files changed

+89
-29
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,16 @@ There are a bunch of signals to listen on each object... taken from AS3, and JS.
213213
- onMouseOver
214214
- onMouseOut
215215
- onMouseScroll
216+
- onLongPress(Duration duration,double distance)
216217

217218
They all emit a `MouseInputData` with all the needed info inside, like stage coordinates, or translated local coordinates, which "mouse" button is pressed, etc.
218219

220+
Specially on event `onLongPress`, the handler register function differcent others, it’s includes two params for configure the event, one of `Duration duration`, it’s mean keep press how long for trigg the event, another is `distance`.
221+
222+
Why need `distance` ?
223+
224+
Beacuse of human can not keep static on screen, so, we are always trigged event `MouseMove`, but event `LongPress` will be cancelled automaticlly other event happed. It’s also mean when I pressed screen and other event happed before `duration`, So, it’s NOT LONG PRESS.
225+
219226
---
220227

221228
### Demos.

lib/src/core/scene_painter.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class ScenePainter with EventDispatcherMixin {
4141
/// Runs on the first "render".
4242
bool _isReady = false;
4343

44+
4445
bool get isReady => _isReady;
4546

4647
/// Automatically manage the `tick()` and `render()` requests.

lib/src/display/display_object.dart

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import 'dart:async';
12
import 'dart:ui' as ui;
23
import 'package:flutter/foundation.dart';
34
import 'package:flutter/painting.dart' as painting;
5+
import 'package:graphx/src/events/events.dart';
46
import '../../graphx.dart';
57

68
abstract class GDisplayObject
@@ -13,7 +15,7 @@ abstract class GDisplayObject
1315
static GDisplayObject $currentDrag;
1416
static GRect $currentDragBounds;
1517
GPoint _dragCenterOffset;
16-
18+
MouseInputData _lastMouseInput;
1719
/// Lets the user drag the specified sprite.
1820
/// The sprite remains draggable until explicitly stopped through a call to
1921
/// the Sprite.stopDrag() method, or until another sprite is made draggable.
@@ -75,6 +77,7 @@ abstract class GDisplayObject
7577

7678
bool $debugBounds = false;
7779
bool mouseUseShape = false;
80+
Timer _longPressTimer;
7881

7982
List<GBaseFilter> $filters;
8083

@@ -138,6 +141,15 @@ abstract class GDisplayObject
138141
$onMouseWheel?.dispatch(mouseInput);
139142
break;
140143
case MouseInputType.down:
144+
if(_longPressTimer != null) {
145+
_longPressTimer.cancel();
146+
}
147+
if($onLongPress != null) {
148+
_longPressTimer = Timer(Duration(milliseconds:
149+
$onLongPress.configure[EventSignalConfKey.LongPressDuration]), () {
150+
$onLongPress.dispatch(mouseInput);
151+
});
152+
}
141153
$mouseDownObj = object;
142154
$onMouseDown?.dispatch(mouseInput);
143155
break;
@@ -146,14 +158,27 @@ abstract class GDisplayObject
146158
// $onRightMouseDown?.dispatch(mouseInput);
147159
// break;
148160
case MouseInputType.move:
161+
if(_lastMouseInput != null) {
162+
// ignore: lines_longer_than_80_chars
163+
if((_lastMouseInput.localX - input.localX).abs() >
164+
// ignore: lines_longer_than_80_chars
165+
$onLongPress.configure[EventSignalConfKey.LongPressShakingDistance] ||
166+
(_lastMouseInput.localY - input.localY).abs() >
167+
// ignore: lines_longer_than_80_chars
168+
$onLongPress.configure[EventSignalConfKey.LongPressShakingDistance]
169+
) {
170+
_longPressTimer?.cancel();
171+
}
172+
}
149173
$onMouseMove?.dispatch(mouseInput);
150174
break;
151175
case MouseInputType.up:
176+
_longPressTimer?.cancel();
177+
152178
if ($mouseDownObj == object &&
153179
($onMouseClick != null || $onMouseDoubleClick != null)) {
154180
var mouseClickInput = input.clone(this, object, MouseInputType.up);
155181
$onMouseClick?.dispatch(mouseClickInput);
156-
157182
if ($lastClickTime > 0 &&
158183
input.time - $lastClickTime < MouseInputData.doubleClickTime) {
159184
$onMouseDoubleClick?.dispatch(mouseClickInput);
@@ -166,13 +191,15 @@ abstract class GDisplayObject
166191
$onMouseUp?.dispatch(mouseInput);
167192
break;
168193
case MouseInputType.over:
194+
_longPressTimer?.cancel();
169195
$mouseOverObj = object;
170196
if (useCursor && GMouse.isShowing()) {
171197
GMouse.setClickCursor();
172198
}
173199
$onMouseOver?.dispatch(mouseInput);
174200
break;
175201
case MouseInputType.out:
202+
_longPressTimer?.cancel();
176203
$mouseOverObj = null;
177204
if (useCursor && GMouse.isShowing()) {
178205
GMouse.cursor = null;
@@ -184,6 +211,7 @@ abstract class GDisplayObject
184211
}
185212
}
186213
$parent?.$dispatchMouseCallback(type, object, input);
214+
_lastMouseInput = input.clone(this, object, type);
187215
}
188216

189217
/// todo: add caching to local bounds (Rect).

lib/src/display/stage.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'dart:async';
12
import 'dart:ui' as ui;
23

34
import '../../graphx.dart';
@@ -40,6 +41,7 @@ class Stage extends GDisplayObjectContainer
4041
ui.Paint _backgroundPaint;
4142
DisplayBoundsDebugger _boundsDebugger;
4243

44+
4345
/// Shortcut to access the owner [SceneController].
4446
SceneController get controller => scene.core;
4547

lib/src/events/mixins.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ mixin MouseSignalsMixin<T extends MouseInputData> {
125125
EventSignal<T> $onMouseOut;
126126
EventSignal<T> $onMouseOver;
127127
EventSignal<T> $onMouseWheel;
128+
EventSignal<T> $onLongPress;
128129

129130
EventSignal<T> get onMouseClick => $onMouseClick ??= EventSignal();
130131
EventSignal<T> get onMouseDoubleClick =>
@@ -136,6 +137,13 @@ mixin MouseSignalsMixin<T extends MouseInputData> {
136137
EventSignal<T> get onMouseOver => $onMouseOver ??= EventSignal();
137138
EventSignal<T> get onMouseOut => $onMouseOut ??= EventSignal();
138139
EventSignal<T> get onMouseScroll => $onMouseWheel ??= EventSignal();
140+
//EventSignal<T> get onLongPress => $onLongPress ??= EventSignal();
141+
142+
EventSignal<T> onLongPress([Duration duration =
143+
const Duration(milliseconds: 600),double distance = 1]) {
144+
$onLongPress = EventSignal.longPress(duration,distance);
145+
return $onLongPress;
146+
}
139147

140148
void $disposePointerSignals() {
141149
$onRightMouseDown?.removeAll();
@@ -156,6 +164,8 @@ mixin MouseSignalsMixin<T extends MouseInputData> {
156164
$onMouseOut = null;
157165
$onMouseWheel?.removeAll();
158166
$onMouseWheel = null;
167+
$onLongPress?.removeAll();
168+
$onLongPress = null;
159169
}
160170
}
161171

lib/src/events/signal.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,20 @@ class Signal {
6464
}
6565
}
6666

67+
enum EventSignalConfKey {
68+
LongPressDuration,
69+
LongPressShakingDistance,
70+
}
71+
6772
class EventSignal<T> {
73+
Map<EventSignalConfKey,Object> configure = {};
74+
EventSignal();
75+
factory EventSignal.longPress(Duration duration,double distance) {
76+
var signal = EventSignal();
77+
signal.configure[EventSignalConfKey.LongPressDuration] = duration;
78+
signal.configure[EventSignalConfKey.LongPressShakingDistance] = distance;
79+
return signal;
80+
}
6881
void call(EventSignalCallback<T> callback) {
6982
add(callback);
7083
}

lib/src/render/graphics.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,6 @@ class Graphics with RenderUtilMixin implements GxRenderable {
700700
}
701701
_constrainAlpha();
702702
if (!_isVisible) return;
703-
704703
// trace("en", _drawingQueue.length);
705704
for (var graph in _drawingQueue) {
706705
if (graph.hasPicture) {

pubspec.lock

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,63 +5,63 @@ packages:
55
dependency: transitive
66
description:
77
name: async
8-
url: "https://pub.dartlang.org"
8+
url: "https://pub.flutter-io.cn"
99
source: hosted
1010
version: "2.5.0"
1111
boolean_selector:
1212
dependency: transitive
1313
description:
1414
name: boolean_selector
15-
url: "https://pub.dartlang.org"
15+
url: "https://pub.flutter-io.cn"
1616
source: hosted
1717
version: "2.1.0"
1818
characters:
1919
dependency: transitive
2020
description:
2121
name: characters
22-
url: "https://pub.dartlang.org"
22+
url: "https://pub.flutter-io.cn"
2323
source: hosted
2424
version: "1.1.0"
2525
charcode:
2626
dependency: transitive
2727
description:
2828
name: charcode
29-
url: "https://pub.dartlang.org"
29+
url: "https://pub.flutter-io.cn"
3030
source: hosted
3131
version: "1.2.0"
3232
clock:
3333
dependency: transitive
3434
description:
3535
name: clock
36-
url: "https://pub.dartlang.org"
36+
url: "https://pub.flutter-io.cn"
3737
source: hosted
3838
version: "1.1.0"
3939
collection:
4040
dependency: transitive
4141
description:
4242
name: collection
43-
url: "https://pub.dartlang.org"
43+
url: "https://pub.flutter-io.cn"
4444
source: hosted
4545
version: "1.15.0"
4646
convert:
4747
dependency: transitive
4848
description:
4949
name: convert
50-
url: "https://pub.dartlang.org"
50+
url: "https://pub.flutter-io.cn"
5151
source: hosted
5252
version: "2.1.1"
5353
effective_dart:
5454
dependency: "direct dev"
5555
description:
5656
name: effective_dart
57-
url: "https://pub.dartlang.org"
57+
url: "https://pub.flutter-io.cn"
5858
source: hosted
5959
version: "1.3.1"
6060
fake_async:
6161
dependency: transitive
6262
description:
6363
name: fake_async
64-
url: "https://pub.dartlang.org"
64+
url: "https://pub.flutter-io.cn"
6565
source: hosted
6666
version: "1.2.0"
6767
flutter:
@@ -78,49 +78,49 @@ packages:
7878
dependency: "direct main"
7979
description:
8080
name: http
81-
url: "https://pub.dartlang.org"
81+
url: "https://pub.flutter-io.cn"
8282
source: hosted
83-
version: "0.13.1"
83+
version: "0.13.3"
8484
http_parser:
8585
dependency: transitive
8686
description:
8787
name: http_parser
88-
url: "https://pub.dartlang.org"
88+
url: "https://pub.flutter-io.cn"
8989
source: hosted
9090
version: "4.0.0"
9191
matcher:
9292
dependency: transitive
9393
description:
9494
name: matcher
95-
url: "https://pub.dartlang.org"
95+
url: "https://pub.flutter-io.cn"
9696
source: hosted
9797
version: "0.12.10"
9898
meta:
9999
dependency: transitive
100100
description:
101101
name: meta
102-
url: "https://pub.dartlang.org"
102+
url: "https://pub.flutter-io.cn"
103103
source: hosted
104104
version: "1.3.0"
105105
path:
106106
dependency: transitive
107107
description:
108108
name: path
109-
url: "https://pub.dartlang.org"
109+
url: "https://pub.flutter-io.cn"
110110
source: hosted
111111
version: "1.8.0"
112112
pedantic:
113113
dependency: "direct dev"
114114
description:
115115
name: pedantic
116-
url: "https://pub.dartlang.org"
116+
url: "https://pub.flutter-io.cn"
117117
source: hosted
118118
version: "1.11.0"
119119
petitparser:
120120
dependency: transitive
121121
description:
122122
name: petitparser
123-
url: "https://pub.dartlang.org"
123+
url: "https://pub.flutter-io.cn"
124124
source: hosted
125125
version: "3.1.0"
126126
sky_engine:
@@ -132,63 +132,63 @@ packages:
132132
dependency: transitive
133133
description:
134134
name: source_span
135-
url: "https://pub.dartlang.org"
135+
url: "https://pub.flutter-io.cn"
136136
source: hosted
137137
version: "1.8.0"
138138
stack_trace:
139139
dependency: transitive
140140
description:
141141
name: stack_trace
142-
url: "https://pub.dartlang.org"
142+
url: "https://pub.flutter-io.cn"
143143
source: hosted
144144
version: "1.10.0"
145145
stream_channel:
146146
dependency: transitive
147147
description:
148148
name: stream_channel
149-
url: "https://pub.dartlang.org"
149+
url: "https://pub.flutter-io.cn"
150150
source: hosted
151151
version: "2.1.0"
152152
string_scanner:
153153
dependency: transitive
154154
description:
155155
name: string_scanner
156-
url: "https://pub.dartlang.org"
156+
url: "https://pub.flutter-io.cn"
157157
source: hosted
158158
version: "1.1.0"
159159
term_glyph:
160160
dependency: transitive
161161
description:
162162
name: term_glyph
163-
url: "https://pub.dartlang.org"
163+
url: "https://pub.flutter-io.cn"
164164
source: hosted
165165
version: "1.2.0"
166166
test_api:
167167
dependency: transitive
168168
description:
169169
name: test_api
170-
url: "https://pub.dartlang.org"
170+
url: "https://pub.flutter-io.cn"
171171
source: hosted
172172
version: "0.2.19"
173173
typed_data:
174174
dependency: transitive
175175
description:
176176
name: typed_data
177-
url: "https://pub.dartlang.org"
177+
url: "https://pub.flutter-io.cn"
178178
source: hosted
179179
version: "1.3.0"
180180
vector_math:
181181
dependency: transitive
182182
description:
183183
name: vector_math
184-
url: "https://pub.dartlang.org"
184+
url: "https://pub.flutter-io.cn"
185185
source: hosted
186186
version: "2.1.0"
187187
xml:
188188
dependency: "direct main"
189189
description:
190190
name: xml
191-
url: "https://pub.dartlang.org"
191+
url: "https://pub.flutter-io.cn"
192192
source: hosted
193193
version: "4.5.1"
194194
sdks:

0 commit comments

Comments
 (0)