You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
233 lines
7.4 KiB
233 lines
7.4 KiB
using System.Collections; |
|
using System.Collections.Generic; |
|
using UnityEngine; |
|
using AX.MessageSystem; |
|
using System; |
|
using AX.InputSystem; |
|
|
|
[Serializable] |
|
public class DragData |
|
{ |
|
public string name; |
|
//public bool dragStart; |
|
public Vector3 postion; |
|
} |
|
public class ObjDrag : MonoBehaviour |
|
{ |
|
|
|
protected Vector3 prevPos = Vector3.zero;//进入拖动过程前(开始拖动)时鼠标在屏幕坐标系中的位置 |
|
protected Vector3 mousePostion = Vector3.zero; |
|
protected bool dragStart; |
|
protected GameObject hitObj; |
|
protected Vector3 endPos; |
|
protected bool dragLerp = false; |
|
protected float timmer; |
|
protected float alltime; |
|
// Use this for initialization |
|
public virtual void Start() |
|
{ |
|
MessageDispatcher.AddListener("ReplayEvent", ReplayEventDrag); |
|
} |
|
|
|
public virtual void ReplayEventDrag(IMessage obj) |
|
{ |
|
var eventData = (EventData)obj.Data; |
|
if (eventData.eventType == RecordEventType.ObjDragExit) |
|
{ |
|
DragData data = JsonUtility.FromJson<DragData>(eventData.json); |
|
if (gameObject.name == data.name) |
|
{ |
|
endPos = data.postion; |
|
alltime = Vector3.Distance(transform.position, endPos) / 100 / ReplayManager.playSpeed; |
|
//Debug.Log(alltime); |
|
dragLerp = true; |
|
} |
|
} |
|
//DragData data; |
|
//if (eventData.eventType == RecordEventType.ObjDragEnter) |
|
//{ |
|
// data = JsonUtility.FromJson<DragData>(eventData.json); |
|
// if (gameObject.name == data.name) |
|
// { |
|
// mousePostion = data.postion; |
|
// dragStart = data.dragStart; |
|
// hitObj = gameObject; |
|
// var screenSpace = Camera.main.WorldToScreenPoint(transform.position); |
|
// prevPos = new Vector3(mousePostion.x, mousePostion.y, screenSpace.z); |
|
// } |
|
//} |
|
//else if (eventData.eventType == RecordEventType.ObjDraging) |
|
//{ |
|
// data = JsonUtility.FromJson<DragData>(eventData.json); |
|
// if (gameObject.name == data.name) |
|
// { |
|
// mousePostion = data.postion; |
|
|
|
// dragStart = data.dragStart; |
|
// } |
|
//} |
|
//else if (eventData.eventType == RecordEventType.ObjDragExit) |
|
//{ |
|
// data = JsonUtility.FromJson<DragData>(eventData.json); |
|
// if (gameObject.name == data.name) |
|
// { |
|
// prevPos = data.postion; |
|
|
|
// dragStart = data.dragStart; |
|
// } |
|
//} |
|
} |
|
public void AddRecordEventDrag(RecordEventType type, DragData data) |
|
{ |
|
if (ReplaySetting.PlayStatus == PlayStatus.isEditor && RecordManager.Instance.recordStatus == RecordStatus.normal) |
|
{ |
|
var eventData = new EventData(); |
|
eventData.time = RecordManager.Instance.RecordTimer; |
|
eventData.cloneObjType = GetComponent<BaseGameObjInfo>().gameObjType; |
|
eventData.eventType = type; |
|
eventData.json = JsonUtility.ToJson(data); |
|
|
|
RecordManager.Instance.jsonData.eventDataList.Add(eventData); |
|
} |
|
} |
|
// Update is called once per frame |
|
public virtual void Update() |
|
{ |
|
if (dragStart) |
|
ObjTranslate(); |
|
} |
|
public virtual void LateUpdate() |
|
{ |
|
if (dragLerp) |
|
{ |
|
timmer += Time.deltaTime; |
|
if (transform.position != endPos) |
|
{ |
|
transform.position = Vector3.Lerp(transform.position, endPos, timmer / alltime); |
|
} |
|
if (timmer >= alltime) |
|
{ |
|
transform.position = endPos; |
|
dragLerp = false; |
|
timmer = 0; |
|
} |
|
} |
|
|
|
} |
|
public virtual void OnEnable() |
|
{ |
|
MessageDispatcher.AddListener("OBJ_DRAG_ENTER_COMMAND", ObjDragEnterExecute); |
|
MessageDispatcher.AddListener("OBJ_DRAGING_COMMAND", ObjDragingExecute); |
|
MessageDispatcher.AddListener("OBJ_DRAG_EXIT_COMMAND", ObjDragExitExecute); |
|
} |
|
|
|
public virtual void OnDisable() |
|
{ |
|
MessageDispatcher.RemoveListener("OBJ_DRAG_ENTER_COMMAND", ObjDragEnterExecute); |
|
MessageDispatcher.RemoveListener("OBJ_DRAGING_COMMAND", ObjDragingExecute); |
|
MessageDispatcher.RemoveListener("OBJ_DRAG_EXIT_COMMAND", ObjDragExitExecute); |
|
} |
|
|
|
public virtual void OnDestroy() |
|
{ |
|
MessageDispatcher.RemoveListener("OBJ_DRAG_ENTER_COMMAND", ObjDragEnterExecute); |
|
MessageDispatcher.RemoveListener("OBJ_DRAGING_COMMAND", ObjDragingExecute); |
|
MessageDispatcher.RemoveListener("OBJ_DRAG_EXIT_COMMAND", ObjDragExitExecute); |
|
MessageDispatcher.RemoveListener("ReplayEvent", ReplayEventDrag); |
|
} |
|
|
|
public virtual void ObjDragEnterExecute(IMessage obj) |
|
{ |
|
var data = (ObjDragCmdArgs)obj.Data; |
|
|
|
mousePostion = data.mousePosition; |
|
dragStart = data.dragStart; |
|
|
|
var screenSpace = Camera.main.WorldToScreenPoint(transform.position); |
|
prevPos = new Vector3(mousePostion.x, mousePostion.y, screenSpace.z); |
|
|
|
var gameObjID = (long)obj.Sender; |
|
hitObj = EntitiesManager.Instance.GetEntityByID(gameObjID); |
|
|
|
//DragData dragdata = new DragData() |
|
//{ |
|
// name = hitObj.name, |
|
// dragStart = data.dragStart, |
|
// postion = data.mousePosition |
|
//}; |
|
//AddRecordEventDrag(RecordEventType.ObjDragEnter, dragdata); |
|
|
|
} |
|
|
|
public virtual void ObjDragingExecute(IMessage obj) |
|
{ |
|
var data = (ObjDragCmdArgs)obj.Data; |
|
|
|
mousePostion = data.mousePosition; |
|
|
|
dragStart = data.dragStart; |
|
|
|
var gameObjID = (long)obj.Sender; |
|
hitObj = EntitiesManager.Instance.GetEntityByID(gameObjID); |
|
//DragData dragdata = new DragData() |
|
//{ |
|
// name = hitObj.name, |
|
// dragStart = data.dragStart, |
|
// postion = data.mousePosition |
|
//}; |
|
//AddRecordEventDrag(RecordEventType.ObjDraging, dragdata); |
|
} |
|
|
|
public virtual void ObjDragExitExecute(IMessage obj) |
|
{ |
|
var data = (ObjDragCmdArgs)obj.Data; |
|
|
|
prevPos = data.mousePosition; |
|
|
|
dragStart = data.dragStart; |
|
|
|
var gameObjID = (long)obj.Sender; |
|
hitObj = EntitiesManager.Instance.GetEntityByID(gameObjID); |
|
//DragData dragdata = new DragData() |
|
//{ |
|
// name = hitObj.name, |
|
// dragStart = data.dragStart, |
|
// postion = data.mousePosition |
|
//}; |
|
//AddRecordEventDrag(RecordEventType.ObjDragExit, dragdata); |
|
DragData dragdata = new DragData() |
|
{ |
|
name = gameObject.name, |
|
postion = transform.position |
|
}; |
|
AddRecordEventDrag(RecordEventType.ObjDragExit, dragdata); |
|
} |
|
|
|
/// <summary> |
|
/// 拖拽克隆对象平面移动 |
|
/// </summary> |
|
public virtual void ObjTranslate() |
|
{ |
|
if (gameObject == hitObj) |
|
{ |
|
if (mousePostion.x > Screen.width || mousePostion.y > Screen.height) |
|
{ |
|
return; |
|
} |
|
var screenSpace = Camera.main.WorldToScreenPoint(transform.position); |
|
|
|
var currPos = new Vector3(mousePostion.x, mousePostion.y, screenSpace.z); |
|
|
|
var prev = Camera.main.ScreenToWorldPoint(prevPos); |
|
var curr = Camera.main.ScreenToWorldPoint(currPos); |
|
|
|
var offset = curr - prev; |
|
|
|
offset.y = 0; |
|
|
|
transform.position += offset; |
|
|
|
prevPos = currPos; |
|
} |
|
} |
|
}
|
|
|