提交FairyGUI

This commit is contained in:
PC-20230316NUNE\Administrator
2024-10-15 20:37:54 +08:00
parent ef1d3dfb2f
commit 9cd469b811
409 changed files with 66228 additions and 4076 deletions

View File

@@ -0,0 +1,155 @@
using System;
using System.Collections.Generic;
using UnityEngine;
namespace FairyGUI
{
/// <summary>
/// 长按手势。当按下一定时间后(duration)派发onAction如果once为false则间隔duration时间持续派发onAction直到手指释放。
/// </summary>
public class LongPressGesture : EventDispatcher
{
/// <summary>
///
/// </summary>
public GObject host { get; private set; }
/// <summary>
/// 当手指按下时派发该事件。
/// </summary>
public EventListener onBegin { get; private set; }
/// <summary>
/// 手指离开屏幕时派发该事件。
/// </summary>
public EventListener onEnd { get; private set; }
/// <summary>
/// 当手指按下后一段时间后派发该事件。并且在手指离开前按一定周期派发该事件。
/// </summary>
public EventListener onAction { get; private set; }
/// <summary>
/// 第一次派发事件的触发时间。单位秒
/// </summary>
public float trigger;
/// <summary>
/// 派发onAction事件的时间间隔。单位秒。
/// </summary>
public float interval;
/// <summary>
/// 如果为真则onAction再一次按下释放过程只派发一次。如果为假则每隔duration时间派发一次。
/// </summary>
public bool once;
/// <summary>
/// 手指按住后,移动超出此半径范围则手势停止。
/// </summary>
public int holdRangeRadius;
Vector2 _startPoint;
bool _started;
int _touchId;
public static float TRIGGER = 1.5f;
public static float INTERVAL = 1f;
public LongPressGesture(GObject host)
{
this.host = host;
trigger = TRIGGER;
interval = INTERVAL;
holdRangeRadius = 50;
Enable(true);
onBegin = new EventListener(this, "onLongPressBegin");
onEnd = new EventListener(this, "onLongPressEnd");
onAction = new EventListener(this, "onLongPressAction");
}
public void Dispose()
{
Enable(false);
host = null;
}
public void Enable(bool value)
{
if (value)
{
if (host == GRoot.inst)
{
Stage.inst.onTouchBegin.Add(__touchBegin);
Stage.inst.onTouchEnd.Add(__touchEnd);
}
else
{
host.onTouchBegin.Add(__touchBegin);
host.onTouchEnd.Add(__touchEnd);
}
}
else
{
if (host == GRoot.inst)
{
Stage.inst.onTouchBegin.Remove(__touchBegin);
Stage.inst.onTouchEnd.Remove(__touchEnd);
}
else
{
host.onTouchBegin.Remove(__touchBegin);
host.onTouchEnd.Remove(__touchEnd);
}
Timers.inst.Remove(__timer);
}
}
public void Cancel()
{
Timers.inst.Remove(__timer);
_started = false;
}
void __touchBegin(EventContext context)
{
InputEvent evt = context.inputEvent;
_startPoint = host.GlobalToLocal(new Vector2(evt.x, evt.y));
_started = false;
_touchId = evt.touchId;
Timers.inst.Add(trigger, 1, __timer);
context.CaptureTouch();
}
void __timer(object param)
{
Vector2 pt = host.GlobalToLocal(Stage.inst.GetTouchPosition(_touchId));
if (Mathf.Pow(pt.x - _startPoint.x, 2) + Mathf.Pow(pt.y - _startPoint.y, 2) > Mathf.Pow(holdRangeRadius, 2))
{
Timers.inst.Remove(__timer);
return;
}
if (!_started)
{
_started = true;
onBegin.Call();
if (!once)
Timers.inst.Add(interval, 0, __timer);
}
onAction.Call();
}
void __touchEnd(EventContext context)
{
Timers.inst.Remove(__timer);
if (_started)
{
_started = false;
onEnd.Call();
}
}
}
}

View File

@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 036b702098f47c24ea9cda6c2948507d
timeCreated: 1464244762
licenseType: Store
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,156 @@
using System;
using System.Collections.Generic;
using UnityEngine;
namespace FairyGUI
{
/// <summary>
/// 两个指头捏或者放的手势。
/// </summary>
public class PinchGesture : EventDispatcher
{
/// <summary>
///
/// </summary>
public GObject host { get; private set; }
/// <summary>
/// 当两个手指开始呈捏手势时派发该事件。
/// </summary>
public EventListener onBegin { get; private set; }
/// <summary>
/// 当其中一个手指离开屏幕时派发该事件。
/// </summary>
public EventListener onEnd { get; private set; }
/// <summary>
/// 当手势动作时派发该事件。
/// </summary>
public EventListener onAction { get; private set; }
/// <summary>
/// 总共缩放的量。
/// </summary>
public float scale;
/// <summary>
/// 从上次通知后的改变量。
/// </summary>
public float delta;
float _startDistance;
float _lastScale;
int[] _touches;
bool _started;
bool _touchBegan;
public PinchGesture(GObject host)
{
this.host = host;
Enable(true);
_touches = new int[2];
onBegin = new EventListener(this, "onPinchBegin");
onEnd = new EventListener(this, "onPinchEnd");
onAction = new EventListener(this, "onPinchAction");
}
public void Dispose()
{
Enable(false);
host = null;
}
public void Enable(bool value)
{
if (value)
{
if (host == GRoot.inst)
{
Stage.inst.onTouchBegin.Add(__touchBegin);
Stage.inst.onTouchMove.Add(__touchMove);
Stage.inst.onTouchEnd.Add(__touchEnd);
}
else
{
host.onTouchBegin.Add(__touchBegin);
host.onTouchMove.Add(__touchMove);
host.onTouchEnd.Add(__touchEnd);
}
}
else
{
_started = false;
_touchBegan = false;
if (host == GRoot.inst)
{
Stage.inst.onTouchBegin.Remove(__touchBegin);
Stage.inst.onTouchMove.Remove(__touchMove);
Stage.inst.onTouchEnd.Remove(__touchEnd);
}
else
{
host.onTouchBegin.Remove(__touchBegin);
host.onTouchMove.Remove(__touchMove);
host.onTouchEnd.Remove(__touchEnd);
}
}
}
void __touchBegin(EventContext context)
{
if (Stage.inst.touchCount == 2)
{
if (!_started && !_touchBegan)
{
_touchBegan = true;
Stage.inst.GetAllTouch(_touches);
Vector2 pt1 = host.GlobalToLocal(Stage.inst.GetTouchPosition(_touches[0]));
Vector2 pt2 = host.GlobalToLocal(Stage.inst.GetTouchPosition(_touches[1]));
_startDistance = Vector2.Distance(pt1, pt2);
context.CaptureTouch();
}
}
}
void __touchMove(EventContext context)
{
if (!_touchBegan || Stage.inst.touchCount != 2)
return;
InputEvent evt = context.inputEvent;
Vector2 pt1 = host.GlobalToLocal(Stage.inst.GetTouchPosition(_touches[0]));
Vector2 pt2 = host.GlobalToLocal(Stage.inst.GetTouchPosition(_touches[1]));
float dist = Vector2.Distance(pt1, pt2);
if (!_started && Mathf.Abs(dist - _startDistance) > UIConfig.touchDragSensitivity)
{
_started = true;
scale = 1;
_lastScale = 1;
onBegin.Call(evt);
}
if (_started)
{
float ss = dist / _startDistance;
delta = ss - _lastScale;
_lastScale = ss;
this.scale += delta;
onAction.Call(evt);
}
}
void __touchEnd(EventContext context)
{
_touchBegan = false;
if (_started)
{
_started = false;
onEnd.Call(context.inputEvent);
}
}
}
}

View File

@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: dc85a819551042448a16308b82054c89
timeCreated: 1464244766
licenseType: Store
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,169 @@
using System;
using System.Collections.Generic;
using UnityEngine;
namespace FairyGUI
{
/// <summary>
/// 手指反向操作的手势。
/// </summary>
public class RotationGesture : EventDispatcher
{
/// <summary>
///
/// </summary>
public GObject host { get; private set; }
/// <summary>
/// 当两个手指开始呈反向操作时派发该事件。
/// </summary>
public EventListener onBegin { get; private set; }
/// <summary>
/// 当其中一个手指离开屏幕时派发该事件。
/// </summary>
public EventListener onEnd { get; private set; }
/// <summary>
/// 当手势动作时派发该事件。
/// </summary>
public EventListener onAction { get; private set; }
/// <summary>
/// 总共旋转的角度。
/// </summary>
public float rotation;
/// <summary>
/// 从上次通知后的改变量。
/// </summary>
public float delta;
/// <summary>
/// 是否把变化量强制为整数。默认true。
/// </summary>
public bool snapping;
Vector2 _startVector;
float _lastRotation;
int[] _touches;
bool _started;
bool _touchBegan;
public RotationGesture(GObject host)
{
this.host = host;
Enable(true);
_touches = new int[2];
snapping = true;
onBegin = new EventListener(this, "onRotationBegin");
onEnd = new EventListener(this, "onRotationEnd");
onAction = new EventListener(this, "onRotationAction");
}
public void Dispose()
{
Enable(false);
host = null;
}
public void Enable(bool value)
{
if (value)
{
if (host == GRoot.inst)
{
Stage.inst.onTouchBegin.Add(__touchBegin);
Stage.inst.onTouchMove.Add(__touchMove);
Stage.inst.onTouchEnd.Add(__touchEnd);
}
else
{
host.onTouchBegin.Add(__touchBegin);
host.onTouchMove.Add(__touchMove);
host.onTouchEnd.Add(__touchEnd);
}
}
else
{
_started = false;
_touchBegan = false;
if (host == GRoot.inst)
{
Stage.inst.onTouchBegin.Remove(__touchBegin);
Stage.inst.onTouchMove.Remove(__touchMove);
Stage.inst.onTouchEnd.Remove(__touchEnd);
}
else
{
host.onTouchBegin.Remove(__touchBegin);
host.onTouchMove.Remove(__touchMove);
host.onTouchEnd.Remove(__touchEnd);
}
}
}
void __touchBegin(EventContext context)
{
if (Stage.inst.touchCount == 2)
{
if (!_started && !_touchBegan)
{
_touchBegan = true;
Stage.inst.GetAllTouch(_touches);
Vector2 pt1 = host.GlobalToLocal(Stage.inst.GetTouchPosition(_touches[0]));
Vector2 pt2 = host.GlobalToLocal(Stage.inst.GetTouchPosition(_touches[1]));
_startVector = pt1 - pt2;
context.CaptureTouch();
}
}
}
void __touchMove(EventContext context)
{
if (!_touchBegan || Stage.inst.touchCount != 2)
return;
InputEvent evt = context.inputEvent;
Vector2 pt1 = host.GlobalToLocal(Stage.inst.GetTouchPosition(_touches[0]));
Vector2 pt2 = host.GlobalToLocal(Stage.inst.GetTouchPosition(_touches[1]));
Vector2 vec = pt1 - pt2;
float rot = Mathf.Rad2Deg * ((Mathf.Atan2(vec.y, vec.x) - Mathf.Atan2(_startVector.y, _startVector.x)));
if (snapping)
{
rot = Mathf.Round(rot);
if (rot == 0)
return;
}
if (!_started && rot > 5)
{
_started = true;
rotation = 0;
_lastRotation = 0;
onBegin.Call(evt);
}
if (_started)
{
delta = rot - _lastRotation;
_lastRotation = rot;
this.rotation += delta;
onAction.Call(evt);
}
}
void __touchEnd(EventContext context)
{
_touchBegan = false;
if (_started)
{
_started = false;
onEnd.Call(context.inputEvent);
}
}
}
}

View File

@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 155eda39968f1194d9b4b7bbd63596ee
timeCreated: 1464244762
licenseType: Store
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,228 @@
using System;
using System.Collections.Generic;
using UnityEngine;
namespace FairyGUI
{
/// <summary>
/// 滑动手势。你可以通过onBegin+onMove+onEnd关心整个滑动过程也可以只使用onAction关注最后的滑动结果。滑动结果包括方向和加速度可以从position和velocity获得。
/// 注意onAction仅当滑动超过一定距离(actionDistance)时才触发。
/// </summary>
public class SwipeGesture : EventDispatcher
{
/// <summary>
///
/// </summary>
public GObject host { get; private set; }
/// <summary>
/// 当手指开始扫动时派发该事件。
/// </summary>
public EventListener onBegin { get; private set; }
/// <summary>
/// 手指离开屏幕时派发该事件。
/// </summary>
public EventListener onEnd { get; private set; }
/// <summary>
/// 手指在滑动时派发该事件。
/// </summary>
public EventListener onMove { get; private set; }
/// <summary>
/// 当手指从按下到离开经过的距离大于actionDistance时派发该事件。
/// </summary>
public EventListener onAction { get; private set; }
/// <summary>
/// 手指离开时的加速度
/// </summary>
public Vector2 velocity;
/// <summary>
/// 你可以在onBegin事件中设置这个值那个后续将根据手指移动的距离修改这个值。如果不设置那position初始为(0,0),反映手指扫过的距离。
/// </summary>
public Vector2 position;
/// <summary>
/// 移动的变化值
/// </summary>
public Vector2 delta;
/// <summary>
/// The min distance to fire onAction event
/// 派发onAction事件的最小距离。如果手指扫过的距离少于此值onAction不会触发但onEnd仍然会派发
/// </summary>
public int actionDistance;
/// <summary>
/// 是否把变化量强制为整数。默认true。
/// </summary>
public bool snapping;
Vector2 _startPoint;
Vector2 _lastPoint;
float _time;
bool _started;
bool _touchBegan;
public static int ACTION_DISTANCE = 200;
public SwipeGesture(GObject host)
{
this.host = host;
actionDistance = ACTION_DISTANCE;
snapping = true;
Enable(true);
onBegin = new EventListener(this, "onSwipeBegin");
onEnd = new EventListener(this, "onSwipeEnd");
onMove = new EventListener(this, "onSwipeMove");
onAction = new EventListener(this, "onnSwipeAction");
}
public void Dispose()
{
Enable(false);
host = null;
}
public void Enable(bool value)
{
if (value)
{
if (host == GRoot.inst)
{
Stage.inst.onTouchBegin.Add(__touchBegin);
Stage.inst.onTouchMove.Add(__touchMove);
Stage.inst.onTouchEnd.Add(__touchEnd);
}
else
{
host.onTouchBegin.Add(__touchBegin);
host.onTouchMove.Add(__touchMove);
host.onTouchEnd.Add(__touchEnd);
}
}
else
{
_started = false;
_touchBegan = false;
if (host == GRoot.inst)
{
Stage.inst.onTouchBegin.Remove(__touchBegin);
Stage.inst.onTouchMove.Remove(__touchMove);
Stage.inst.onTouchEnd.Remove(__touchEnd);
}
else
{
host.onTouchBegin.Remove(__touchBegin);
host.onTouchMove.Remove(__touchMove);
host.onTouchEnd.Remove(__touchEnd);
}
}
}
void __touchBegin(EventContext context)
{
if (Stage.inst.touchCount > 1)
{
_touchBegan = false;
if (_started)
{
_started = false;
onEnd.Call(context.inputEvent);
}
return;
}
InputEvent evt = context.inputEvent;
_startPoint = _lastPoint = host.GlobalToLocal(new Vector2(evt.x, evt.y));
_lastPoint = _startPoint;
_time = Time.unscaledTime;
_started = false;
velocity = Vector2.zero;
position = Vector2.zero;
_touchBegan = true;
context.CaptureTouch();
}
void __touchMove(EventContext context)
{
if (!_touchBegan || Stage.inst.touchCount > 1)
return;
InputEvent evt = context.inputEvent;
Vector2 pt = host.GlobalToLocal(new Vector2(evt.x, evt.y));
delta = pt - _lastPoint;
if (snapping)
{
delta.x = Mathf.Round(delta.x);
delta.y = Mathf.Round(delta.y);
if (delta.x == 0 && delta.y == 0)
return;
}
float deltaTime = Time.unscaledDeltaTime;
float elapsed = (Time.unscaledTime - _time) * 60 - 1;
if (elapsed > 1) //速度衰减
velocity = velocity * Mathf.Pow(0.833f, elapsed);
velocity = Vector3.Lerp(velocity, delta / deltaTime, deltaTime * 10);
_time = Time.unscaledTime;
position += delta;
_lastPoint = pt;
if (!_started)
{ //灵敏度检查,为了和点击区分
int sensitivity;
if (Stage.touchScreen)
sensitivity = UIConfig.touchDragSensitivity;
else
sensitivity = 5;
if (Mathf.Abs(delta.x) < sensitivity && Mathf.Abs(delta.y) < sensitivity)
return;
_started = true;
onBegin.Call(evt);
}
onMove.Call(evt);
}
void __touchEnd(EventContext context)
{
_touchBegan = false;
if (!_started)
return;
_started = false;
InputEvent evt = context.inputEvent;
Vector2 pt = host.GlobalToLocal(new Vector2(evt.x, evt.y));
delta = pt - _lastPoint;
if (snapping)
{
delta.x = Mathf.Round(delta.x);
delta.y = Mathf.Round(delta.y);
}
position += delta;
//更新速度
float elapsed = (Time.unscaledTime - _time) * 60 - 1;
if (elapsed > 1)
velocity = velocity * Mathf.Pow(0.833f, elapsed);
if (snapping)
{
velocity.x = Mathf.Round(velocity.x);
velocity.y = Mathf.Round(velocity.y);
}
onEnd.Call(evt);
pt -= _startPoint;
if (Mathf.Abs(pt.x) > actionDistance || Mathf.Abs(pt.y) > actionDistance)
onAction.Call(evt);
}
}
}

View File

@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 7bed3df24eb979345a4d3ed9cd34ca0e
timeCreated: 1464244762
licenseType: Store
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: