Skip to content

Commit

Permalink
1. support long press like flutter
Browse files Browse the repository at this point in the history
  • Loading branch information
Misko Lee authored and Misko Lee committed May 31, 2021
1 parent 2cf0815 commit 348a86b
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 29 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,16 @@ There are a bunch of signals to listen on each object... taken from AS3, and JS.
- onMouseOver
- onMouseOut
- onMouseScroll
- onLongPress(Duration duration,double distance)

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

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`.

Why need `distance` ?

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.

---

### Demos.
Expand Down
1 change: 1 addition & 0 deletions lib/src/core/scene_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class ScenePainter with EventDispatcherMixin {
/// Runs on the first "render".
bool _isReady = false;


bool get isReady => _isReady;

/// Automatically manage the `tick()` and `render()` requests.
Expand Down
35 changes: 33 additions & 2 deletions lib/src/display/display_object.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'dart:async';
import 'dart:ui' as ui;
import 'package:flutter/foundation.dart';
import 'package:flutter/painting.dart' as painting;
import 'package:graphx/src/events/events.dart';
import '../../graphx.dart';

abstract class GDisplayObject
Expand All @@ -13,7 +15,7 @@ abstract class GDisplayObject
static GDisplayObject $currentDrag;
static GRect $currentDragBounds;
GPoint _dragCenterOffset;

MouseInputData _lastMouseInput;
/// Lets the user drag the specified sprite.
/// The sprite remains draggable until explicitly stopped through a call to
/// the Sprite.stopDrag() method, or until another sprite is made draggable.
Expand Down Expand Up @@ -75,6 +77,7 @@ abstract class GDisplayObject

bool $debugBounds = false;
bool mouseUseShape = false;
Timer _longPressTimer;

List<GBaseFilter> $filters;

Expand Down Expand Up @@ -138,6 +141,15 @@ abstract class GDisplayObject
$onMouseWheel?.dispatch(mouseInput);
break;
case MouseInputType.down:
if(_longPressTimer != null) {
_longPressTimer.cancel();
_longPressTimer = null;
}
if($onLongPress != null) {
_longPressTimer = Timer($onLongPress.configure[EventSignalConfKey.LongPressDuration], () {
$onLongPress?.dispatch(mouseInput);
});
}
$mouseDownObj = object;
$onMouseDown?.dispatch(mouseInput);
break;
Expand All @@ -146,14 +158,29 @@ abstract class GDisplayObject
// $onRightMouseDown?.dispatch(mouseInput);
// break;
case MouseInputType.move:
if(_lastMouseInput != null && $onLongPress != null) {
// ignore: lines_longer_than_80_chars
if((_lastMouseInput.localX - input.localX).abs() >
// ignore: lines_longer_than_80_chars
$onLongPress.configure[EventSignalConfKey.LongPressShakingDistance] ||
(_lastMouseInput.localY - input.localY).abs() >
// ignore: lines_longer_than_80_chars
$onLongPress.configure[EventSignalConfKey.LongPressShakingDistance]
) {
_longPressTimer?.cancel();
_longPressTimer = null;
}
}
$onMouseMove?.dispatch(mouseInput);
break;
case MouseInputType.up:
_longPressTimer?.cancel();
_longPressTimer = null;

if ($mouseDownObj == object &&
($onMouseClick != null || $onMouseDoubleClick != null)) {
var mouseClickInput = input.clone(this, object, MouseInputType.up);
$onMouseClick?.dispatch(mouseClickInput);

if ($lastClickTime > 0 &&
input.time - $lastClickTime < MouseInputData.doubleClickTime) {
$onMouseDoubleClick?.dispatch(mouseClickInput);
Expand All @@ -166,13 +193,16 @@ abstract class GDisplayObject
$onMouseUp?.dispatch(mouseInput);
break;
case MouseInputType.over:
_longPressTimer?.cancel();
$mouseOverObj = object;
if (useCursor && GMouse.isShowing()) {
GMouse.setClickCursor();
}
$onMouseOver?.dispatch(mouseInput);
break;
case MouseInputType.out:
_longPressTimer?.cancel();
_longPressTimer = null;
$mouseOverObj = null;
if (useCursor && GMouse.isShowing()) {
GMouse.cursor = null;
Expand All @@ -184,6 +214,7 @@ abstract class GDisplayObject
}
}
$parent?.$dispatchMouseCallback(type, object, input);
_lastMouseInput = input.clone(this, object, type);
}

/// todo: add caching to local bounds (Rect).
Expand Down
2 changes: 2 additions & 0 deletions lib/src/display/stage.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:ui' as ui;

import '../../graphx.dart';
Expand Down Expand Up @@ -40,6 +41,7 @@ class Stage extends GDisplayObjectContainer
ui.Paint _backgroundPaint;
DisplayBoundsDebugger _boundsDebugger;


/// Shortcut to access the owner [SceneController].
SceneController get controller => scene.core;

Expand Down
9 changes: 9 additions & 0 deletions lib/src/events/mixins.dart
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ mixin MouseSignalsMixin<T extends MouseInputData> {
EventSignal<T> $onMouseOut;
EventSignal<T> $onMouseOver;
EventSignal<T> $onMouseWheel;
EventSignal<T> $onLongPress;

EventSignal<T> get onMouseClick => $onMouseClick ??= EventSignal();
EventSignal<T> get onMouseDoubleClick =>
Expand All @@ -137,6 +138,12 @@ mixin MouseSignalsMixin<T extends MouseInputData> {
EventSignal<T> get onMouseOut => $onMouseOut ??= EventSignal();
EventSignal<T> get onMouseScroll => $onMouseWheel ??= EventSignal();

EventSignal<T> onLongPress([Duration duration =
const Duration(milliseconds: 600),double distance = 1]) {
$onLongPress = EventSignal<T>.longPress(duration,distance);
return $onLongPress;
}

void $disposePointerSignals() {
$onRightMouseDown?.removeAll();
$onRightMouseDown = null;
Expand All @@ -156,6 +163,8 @@ mixin MouseSignalsMixin<T extends MouseInputData> {
$onMouseOut = null;
$onMouseWheel?.removeAll();
$onMouseWheel = null;
$onLongPress?.removeAll();
$onLongPress = null;
}
}

Expand Down
13 changes: 13 additions & 0 deletions lib/src/events/signal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,20 @@ class Signal {
}
}

enum EventSignalConfKey {
LongPressDuration,
LongPressShakingDistance,
}

class EventSignal<T> {
Map<EventSignalConfKey,Object> configure = {};
EventSignal();
factory EventSignal.longPress(Duration duration,double distance) {
var signal = EventSignal<T>();
signal.configure[EventSignalConfKey.LongPressDuration] = duration;
signal.configure[EventSignalConfKey.LongPressShakingDistance] = distance;
return signal;
}
void call(EventSignalCallback<T> callback) {
add(callback);
}
Expand Down
1 change: 0 additions & 1 deletion lib/src/render/graphics.dart
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,6 @@ class Graphics with RenderUtilMixin implements GxRenderable {
}
_constrainAlpha();
if (!_isVisible) return;

// trace("en", _drawingQueue.length);
for (var graph in _drawingQueue) {
if (graph.hasPicture) {
Expand Down
52 changes: 26 additions & 26 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,63 +5,63 @@ packages:
dependency: transitive
description:
name: async
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.5.0"
boolean_selector:
dependency: transitive
description:
name: boolean_selector
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.1.0"
characters:
dependency: transitive
description:
name: characters
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.1.0"
charcode:
dependency: transitive
description:
name: charcode
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.2.0"
clock:
dependency: transitive
description:
name: clock
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.1.0"
collection:
dependency: transitive
description:
name: collection
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.15.0"
convert:
dependency: transitive
description:
name: convert
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.1.1"
effective_dart:
dependency: "direct dev"
description:
name: effective_dart
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.3.1"
fake_async:
dependency: transitive
description:
name: fake_async
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.2.0"
flutter:
Expand All @@ -78,49 +78,49 @@ packages:
dependency: "direct main"
description:
name: http
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.13.1"
version: "0.13.3"
http_parser:
dependency: transitive
description:
name: http_parser
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "4.0.0"
matcher:
dependency: transitive
description:
name: matcher
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.12.10"
meta:
dependency: transitive
description:
name: meta
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.3.0"
path:
dependency: transitive
description:
name: path
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.8.0"
pedantic:
dependency: "direct dev"
description:
name: pedantic
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.11.0"
petitparser:
dependency: transitive
description:
name: petitparser
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "3.1.0"
sky_engine:
Expand All @@ -132,63 +132,63 @@ packages:
dependency: transitive
description:
name: source_span
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.8.0"
stack_trace:
dependency: transitive
description:
name: stack_trace
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.10.0"
stream_channel:
dependency: transitive
description:
name: stream_channel
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.1.0"
string_scanner:
dependency: transitive
description:
name: string_scanner
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.1.0"
term_glyph:
dependency: transitive
description:
name: term_glyph
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.2.0"
test_api:
dependency: transitive
description:
name: test_api
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.2.19"
typed_data:
dependency: transitive
description:
name: typed_data
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.3.0"
vector_math:
dependency: transitive
description:
name: vector_math
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.1.0"
xml:
dependency: "direct main"
description:
name: xml
url: "https://pub.dartlang.org"
url: "https://pub.flutter-io.cn"
source: hosted
version: "4.5.1"
sdks:
Expand Down

0 comments on commit 348a86b

Please sign in to comment.