浏览代码

Add doubleClickSample and comment to drag_target.cs

/demo
Yi-21 3 年前
当前提交
3efa092b
共有 2 个文件被更改,包括 192 次插入84 次删除
  1. 187
      com.unity.uiwidgets/Runtime/widgets/drag_target.cs
  2. 89
      Samples/UIWidgetsSamples_2019_4/Assets/WidgetsSample/DoubleClickSample.cs

187
com.unity.uiwidgets/Runtime/widgets/drag_target.cs


using System;
using System.Collections.Generic;
using Unity.UIWidgets.foundation;
using Unity.UIWidgets.gestures;

using UnityEngine;
namespace Unity.UIWidgets.widgets {
public delegate bool DragTargetWillAccept<T>(T data);

child,
pointer
}
static class _DragUtils {
public static List<T> _mapAvatarsToData<T>(List<_DragAvatar<T>> avatars) {
List<T> ret = new List<T>(avatars.Count);

}
}
/**
* @description: Define a draggable item(short press to drag).
*/
readonly Axis? affinity;
public readonly Axis? axis;
public readonly Widget child;
public readonly Widget childWhenDragging;
public readonly T data;
public readonly DragAnchor dragAnchor;
public readonly Widget feedback;
public readonly Offset feedbackOffset;
public readonly int? maxSimultaneousDrags;
public readonly VoidCallback onDragCompleted;
public readonly DragEndCallback onDragEnd;
public readonly DraggableCanceledCallback onDraggableCanceled;
public readonly VoidCallback onDragStarted;
public Draggable(
Key key = null,
Widget child = null,

DraggableCanceledCallback onDraggableCanceled = null,
DragEndCallback onDragEnd = null,
VoidCallback onDragCompleted = null
) : base(key) {
) : base(key) {
D.assert(child != null);
D.assert(feedback != null);
D.assert(maxSimultaneousDrags == null || maxSimultaneousDrags >= 0);

this.onDragCompleted = onDragCompleted;
}
public readonly T data;
public readonly Axis? axis;
public readonly Widget child;
public readonly Widget childWhenDragging;
public readonly Widget feedback;
public readonly Offset feedbackOffset;
public readonly DragAnchor dragAnchor;
readonly Axis? affinity;
public readonly int? maxSimultaneousDrags;
public readonly VoidCallback onDragStarted;
public readonly DraggableCanceledCallback onDraggableCanceled;
public readonly VoidCallback onDragCompleted;
public readonly DragEndCallback onDragEnd;
public virtual GestureRecognizer createRecognizer(GestureMultiDragStartCallback onStart) {
switch (affinity) {

}
}
/**
* @description: Define a draggable item(long press to drag).
*/
public class LongPressDraggable<T> : Draggable<T> {
public LongPressDraggable(
Key key = null,

}
}
/**
* @description: Define the state of a draggable item
*/
int _activeCount;
GestureRecognizer _recognizer;
public override void initState() {
base.initState();
_recognizer = widget.createRecognizer(_startDrag);

base.dispose();
}
GestureRecognizer _recognizer;
int _activeCount;
_recognizer.dispose();
_recognizer = null;
}

}
}
/**
* @description: Define details of a draggable item.
*/
public class DraggableDetails {
public readonly Offset offset;
public class DraggableDetails {
public readonly Velocity velocity;
public readonly bool wasAccepted;
public DraggableDetails(
bool wasAccepted = false,
Velocity velocity = null,

this.velocity = velocity;
this.offset = offset;
}
}
public readonly bool wasAccepted;
/**
* @description: Define drag target. Could interact with a draggable item in the event of "enter", "leave" or "drop".
* When start dragging, _startDrag in _DraggableState is called and return a dragAvatar of that dragging item.
* updateDrag in _DragAvatar is called whenever the item is dragged and not called when the item stay unmoved.
* In updateDrag, didEnter is called when new draggable item enter a drag target(detect by hittest)
* didLeave is called in updateDrag if draggable is dragged through the drag target, and is called in finishDrag(didDrop is called if finish dragging by dropping the item).
*/
public class DragTarget<T> : StatefulWidget {
public readonly DragTargetBuilder<T> builder;
public readonly Velocity velocity;
public readonly DragTargetAccept<T> onAccept;
public readonly Offset offset;
}
public readonly DragTargetLeave<T> onLeave;
public readonly DragTargetWillAccept<T> onWillAccept;
public class DragTarget<T> : StatefulWidget {
public DragTarget(
Key key = null,
DragTargetBuilder<T> builder = null,

this.onLeave = onLeave;
}
public readonly DragTargetBuilder<T> builder;
public readonly DragTargetWillAccept<T> onWillAccept;
public readonly DragTargetAccept<T> onAccept;
public readonly DragTargetLeave<T> onLeave;
/**
* @description: Define drag target state. State change when draggable item enter, leave or drop on the drag target.
*/
public class _DragTargetState<T> : State<DragTarget<T>> {
readonly List<_DragAvatar<T>> _candidateAvatars = new List<_DragAvatar<T>>();
readonly List<_DragAvatar<T>> _rejectedAvatars = new List<_DragAvatar<T>>();

return true;
}
else {
setState(() => {
_rejectedAvatars.Add(avatar);
});
setState(() => { _rejectedAvatars.Add(avatar); });
return false;
}
}

widget.onLeave(avatar.data);
}
}
//Triggered if avatar is dropped at drag target
public void didDrop(_DragAvatar<T> avatar) {
D.assert(_candidateAvatars.Contains(avatar));

public delegate void _OnDragEnd(Velocity velocity, Offset offset, bool wasAccepted);
/**
* @description: Define drag avatar. Is created in _DraggableState when start dragging
*/
public class _DragAvatar<T> : Drag {
readonly List<_DragTargetState<T>> _enteredTargets = new List<_DragTargetState<T>>();
readonly Axis? axis;
public readonly T data;
public class _DragAvatar<T> : Drag {
readonly Offset dragStartPoint;
readonly Widget feedback;
readonly Offset feedbackOffset;
readonly _OnDragEnd onDragEnd;
readonly OverlayState overlayState;
_DragTargetState<T> _activeTarget;
OverlayEntry _entry;
Offset _lastOffset;
Offset _position;
public _DragAvatar(
OverlayState overlayState,
T data = default,

Offset feedbackOffset = null,
_OnDragEnd onDragEnd = null
) {
if (feedbackOffset == null) {
feedbackOffset = Offset.zero;
}

updateDrag(initialPosition);
}
public readonly T data;
readonly Axis? axis;
readonly Offset dragStartPoint;
readonly Widget feedback;
readonly Offset feedbackOffset;
readonly _OnDragEnd onDragEnd;
readonly OverlayState overlayState;
_DragTargetState<T> _activeTarget;
readonly List<_DragTargetState<T>> _enteredTargets = new List<_DragTargetState<T>>();
Offset _position;
Offset _lastOffset;
OverlayEntry _entry;
public void update(DragUpdateDetails details) {
_position += _restrictAxis(details.delta);
updateDrag(_position);

if (listsMatch) {
return;
}
//Loop through every target and check if current avatar enter the target
foreach (var target in targets) {
_enteredTargets.Add(target);
if (target.didEnter(this)) {

_enteredTargets.Clear();
}
//Finish dragging the draggable
/**
* @description: Finish dragging the draggable.
*/
void finishDrag(_DragEndKind endKind, Velocity velocity = null) {
bool wasAccepted = false;
//If finish drag by dropping the avatar, and avatar has entered any drag target, trigger didDrop

}
public Widget _build(BuildContext context) {
RenderBox box = overlayState.context.findRenderObject() as RenderBox;
RenderBox box = overlayState.context.findRenderObject() as RenderBox;
Offset overlayTopLeft = box.localToGlobal(Offset.zero);
return new Positioned(
left: _lastOffset.dx - overlayTopLeft.dx,

89
Samples/UIWidgetsSamples_2019_4/Assets/WidgetsSample/DoubleClickSample.cs


using System.Collections.Generic;
using uiwidgets;
using Unity.UIWidgets.engine;
using Unity.UIWidgets.foundation;
using Unity.UIWidgets.rendering;
using Unity.UIWidgets.ui;
using Unity.UIWidgets.widgets;
using UnityEngine;
using Color = Unity.UIWidgets.ui.Color;
using ui_ = Unity.UIWidgets.widgets.ui_;
namespace UIWidgetsSample {
public class DoubleClickSample : UIWidgetsPanel {
protected override void main() {
ui_.runApp(new MyApp());
}
class MyApp : StatelessWidget
{
public override Widget build(BuildContext context)
{
return new WidgetsApp(
color: Color.white,
home: new DoubleClickApp(),
pageRouteBuilder: (settings, builder) =>
new PageRouteBuilder(
settings: settings,
pageBuilder: (Buildcontext, animation, secondaryAnimation) => builder(context)
)
);
}
}
class DoubleClickApp : StatefulWidget
{
public DoubleClickApp(Key key = null) : base(key)
{
}
public override State createState()
{
return new DoubleClickState();
}
}
class DoubleClickState : State<DoubleClickApp> {
int _doubleClickCount = 0;
void _toggleDoubleClick()
{
Debug.Log("ON DOUBLECLICK ...");
setState(() =>
{
_doubleClickCount += 1;
});
print("double tap");
}
public override Widget build(BuildContext context) {
var entries = new List<OverlayEntry>();
var entry = new OverlayEntry(
inner_context => new Center(
child: new GestureDetector(
onTap:() => {},
onDoubleTap: _toggleDoubleClick,
child: new Container(
color: Colors.green,
width: 40.0f,
height: 40.0f,
constraints: BoxConstraints.tight(new Size(40, 40)),
child: new Center(child: new Text(_doubleClickCount.ToString()))
)
)
)
);
entries.Add(entry);
return new Container(
color: Colors.white,
child: new Overlay(
initialEntries: entries
)
);
}
}
}
}
正在加载...
取消
保存