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

1. support long press like flutter #23

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
bf89458
Migrated to null safety and made another branch
IsmailAlamKhan Mar 6, 2021
0b79ae9
fixed the graphx_widget for the null_check operator error
IsmailAlamKhan Mar 13, 2021
b951329
another small fix for the graphx_widget converter
shariful2011 Mar 13, 2021
b512710
removed the trace when calling ContextUtils.getRenderObjectBounds
IsmailAlamKhan Mar 14, 2021
14f393c
Fix for the GText conflict
IsmailAlamKhan Mar 15, 2021
0f7662d
Another fix for GText confilct
IsmailAlamKhan Mar 15, 2021
6550a9e
same as previous commit
IsmailAlamKhan Mar 15, 2021
e416dbc
same as before
IsmailAlamKhan Mar 15, 2021
9edd3f9
Merge branch 'master' into null-safety
IsmailAlamKhan Mar 16, 2021
778fe9b
added the gtween addition
IsmailAlamKhan Mar 16, 2021
45292fc
fixed the duplicate prop on text
IsmailAlamKhan Mar 16, 2021
d86e186
Merge pull request #15 from IsmailAlamKhan/null-safety
roipeker Mar 18, 2021
c75233b
migrated the examples to null-safety
IsmailAlamKhan Mar 22, 2021
bc12fda
critical fixes for null-safety.
roipeker Mar 25, 2021
5eb611f
Merge pull request #20 from IsmailAlamKhan/null-safety
roipeker Mar 25, 2021
2bb46ec
Update graphics.dart
roipeker Mar 26, 2021
348a86b
1. support long press like flutter
May 27, 2021
21a3ced
fix bug: update event.type to long press
May 31, 2021
bb5dd6f
1. keep event signal stateless.
Jun 1, 2021
2ce2f3e
Merge remote-tracking branch 'origin/null-safety' into add-long-press…
Jul 18, 2021
edf5349
1. fix null-safety
Jul 18, 2021
cad4077
1. disable drag event to parent
Sep 1, 2021
1940d14
1. update keyboard
Sep 3, 2021
616a02c
1. add custom mouse cursor
Sep 13, 2021
f61877a
1. add custom mouse cursor
Sep 13, 2021
05d3ca4
1. support flutter 3.0
May 15, 2022
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
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 =
imiskolee marked this conversation as resolved.
Show resolved Hide resolved
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) {
imiskolee marked this conversation as resolved.
Show resolved Hide resolved
var signal = EventSignal<T>();
signal.configure[EventSignalConfKey.LongPressDuration] = duration;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another observation, even if the Duration type makes total sense in Dart/Flutter, GraphX is currently using double to express durations in the tween system and the "ticker" elapsed time since last frame ... ( 0.1 = 100 milliseconds). Just a thought, as that concept is taken from JS/AS3.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Duration is better on user side, maybe we can translate Duration to double ticks?

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