GizmoObject.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. using System;
  2. using UnityEngine;
  3. using wf;
  4. public class GizmoObject : MonoBehaviour
  5. {
  6. public GizmoRender gizmoRot { get; private set; }
  7. public WorldTransformAxis gizmoAxis { get; private set; }
  8. public Vector3 position
  9. {
  10. get
  11. {
  12. return base.transform.position;
  13. }
  14. set
  15. {
  16. Vector3 vector = base.transform.position - value;
  17. base.transform.position = value;
  18. if (vector.sqrMagnitude > 0f && this.onChangePosition != null)
  19. {
  20. this.onChangePosition(value);
  21. }
  22. this.m_CachedSnapPosition = value;
  23. }
  24. }
  25. public Vector3 eulerAngles
  26. {
  27. get
  28. {
  29. return this.rotation.eulerAngles;
  30. }
  31. set
  32. {
  33. this.rotation = Quaternion.Euler(value);
  34. }
  35. }
  36. public Vector3 scale
  37. {
  38. get
  39. {
  40. return this.gizmoRot.transform.localScale;
  41. }
  42. set
  43. {
  44. Vector3 vector = this.gizmoRot.transform.localScale - value;
  45. this.gizmoRot.transform.localScale = value;
  46. if (vector.sqrMagnitude > 0f && this.onChangeScale != null)
  47. {
  48. this.onChangeScale(value);
  49. }
  50. this.m_CachedSnapScale = value;
  51. }
  52. }
  53. public Vector3 nonCallbackScale
  54. {
  55. set
  56. {
  57. this.gizmoRot.transform.localScale = value;
  58. this.UpdateSnapping();
  59. }
  60. }
  61. public Quaternion rotation
  62. {
  63. get
  64. {
  65. return this.gizmoRot.transform.rotation;
  66. }
  67. set
  68. {
  69. float f = Quaternion.Angle(this.gizmoRot.transform.rotation, value);
  70. this.gizmoRot.transform.rotation = value;
  71. if (Mathf.Abs(f) > 0f && this.onChangeRotation != null)
  72. {
  73. this.onChangeRotation(value);
  74. }
  75. this.m_CachedSnapRotation = value;
  76. }
  77. }
  78. public bool activeAxis
  79. {
  80. get
  81. {
  82. return this.gizmoAxis.gameObject.activeInHierarchy;
  83. }
  84. set
  85. {
  86. this.gizmoAxis.gameObject.SetActive(value);
  87. }
  88. }
  89. public bool activeRot
  90. {
  91. get
  92. {
  93. return this.gizmoRot.gameObject.activeInHierarchy;
  94. }
  95. set
  96. {
  97. this.gizmoRot.gameObject.SetActive(value);
  98. }
  99. }
  100. public GizmoObject.ControlType controlType
  101. {
  102. get
  103. {
  104. return this.m_ControlType;
  105. }
  106. set
  107. {
  108. this.m_ControlType = value;
  109. }
  110. }
  111. public float snapValuePosition
  112. {
  113. get
  114. {
  115. return this.m_SnapValuePosition;
  116. }
  117. set
  118. {
  119. this.m_SnapValuePosition = value;
  120. }
  121. }
  122. public float snapValueAngles
  123. {
  124. get
  125. {
  126. return this.m_SnapValueAngles;
  127. }
  128. set
  129. {
  130. this.m_SnapValueAngles = value;
  131. }
  132. }
  133. public float snapValueScale
  134. {
  135. get
  136. {
  137. return this.m_SnapValueScale;
  138. }
  139. set
  140. {
  141. this.m_SnapValueScale = value;
  142. }
  143. }
  144. public Vector3 cachedSnapScale
  145. {
  146. get
  147. {
  148. return this.m_CachedSnapScale;
  149. }
  150. }
  151. private void Awake()
  152. {
  153. GameObject gameObject = new GameObject("Gizmo Rot");
  154. GizmoRender gizmoRot = gameObject.AddComponent<GizmoRender>();
  155. this.gizmoRot = gizmoRot;
  156. Transform transform = gameObject.transform;
  157. transform.SetParent(base.gameObject.transform);
  158. transform.localPosition = Vector3.zero;
  159. transform.localRotation = Quaternion.identity;
  160. transform.localScale = Vector3.one;
  161. GameObject gameObject2 = Utility.CreatePrefab(null, "System/Prefab/XYZAxis", true);
  162. WorldTransformAxis component = gameObject2.GetComponent<WorldTransformAxis>();
  163. this.gizmoAxis = component;
  164. Transform transform2 = gameObject2.transform;
  165. transform2.SetParent(base.gameObject.transform);
  166. transform2.localPosition = Vector3.zero;
  167. transform2.localRotation = Quaternion.AngleAxis(90f, Vector3.left);
  168. transform2.localScale = Vector3.one;
  169. this.gizmoAxis.TargetObject = base.gameObject;
  170. this.m_CachedSnapPosition = this.position;
  171. this.m_CachedSnapRotation = this.rotation;
  172. this.m_CachedSnapScale = this.scale;
  173. }
  174. private void OnDestroy()
  175. {
  176. if (this.gizmoRot != null && this.gizmoRot.lineMaterial != null)
  177. {
  178. UnityEngine.Object.Destroy(this.gizmoRot.lineMaterial);
  179. }
  180. this.onChangePosition = null;
  181. this.onChangeRotation = null;
  182. this.onChangeScale = null;
  183. }
  184. private void LateUpdate()
  185. {
  186. if (NInput.GetMouseButtonDown(0))
  187. {
  188. this.OnClickEnter();
  189. }
  190. else if (NInput.GetMouseButton(0))
  191. {
  192. this.OnClickExecute();
  193. }
  194. else if (NInput.GetMouseButtonUp(0))
  195. {
  196. this.OnClickExit();
  197. }
  198. if (!GizmoRender.UIVisible && GizmoRender.UIVisible != base.gameObject.activeInHierarchy)
  199. {
  200. base.gameObject.SetActive(false);
  201. }
  202. }
  203. private void OnClickEnter()
  204. {
  205. }
  206. private void OnClickExecute()
  207. {
  208. this.UpdateSnapping();
  209. this.UpdateTargetTransform();
  210. }
  211. private void OnClickExit()
  212. {
  213. this.UpdateSnapping();
  214. this.UpdateTargetTransform();
  215. if (this.controlType == GizmoObject.ControlType.Snap)
  216. {
  217. this.position = this.m_CachedSnapPosition;
  218. this.rotation = this.m_CachedSnapRotation;
  219. this.scale = this.m_CachedSnapScale;
  220. }
  221. }
  222. private void UpdateTargetTransform()
  223. {
  224. if (this.target == null)
  225. {
  226. return;
  227. }
  228. Transform transform = this.target.transform;
  229. if (this.controlType == GizmoObject.ControlType.Default)
  230. {
  231. transform.position = this.position;
  232. transform.rotation = this.rotation;
  233. transform.localScale = this.scale;
  234. }
  235. else if (this.controlType == GizmoObject.ControlType.Snap)
  236. {
  237. transform.position = this.m_CachedSnapPosition;
  238. transform.rotation = this.m_CachedSnapRotation;
  239. transform.localScale = this.m_CachedSnapScale;
  240. }
  241. }
  242. private void UpdateSnapping()
  243. {
  244. float sqrMagnitude = (this.m_CachedSnapPosition - this.position).sqrMagnitude;
  245. float num = Quaternion.Angle(this.m_CachedSnapRotation, this.rotation);
  246. float sqrMagnitude2 = (this.m_CachedSnapScale - this.scale).sqrMagnitude;
  247. if (this.controlType == GizmoObject.ControlType.Default)
  248. {
  249. this.m_CachedSnapPosition = this.position;
  250. this.m_CachedSnapRotation = this.rotation;
  251. this.m_CachedSnapScale = this.scale;
  252. if (sqrMagnitude > 0f && this.onChangePosition != null)
  253. {
  254. this.m_CachedSnapPosition = this.position;
  255. this.onChangePosition(this.position);
  256. }
  257. if (num > 0f && this.onChangeRotation != null)
  258. {
  259. this.m_CachedSnapRotation = this.rotation;
  260. this.onChangeRotation(this.rotation);
  261. }
  262. if (sqrMagnitude2 > 0f && this.onChangeScale != null)
  263. {
  264. this.m_CachedSnapScale = this.scale;
  265. this.onChangeScale(this.scale);
  266. }
  267. }
  268. else if (this.controlType == GizmoObject.ControlType.Snap)
  269. {
  270. if (sqrMagnitude >= this.snapValuePosition * this.snapValuePosition)
  271. {
  272. Vector3 position = this.position;
  273. position.x = Mathf.Round(position.x / this.snapValuePosition) * this.snapValuePosition;
  274. position.y = Mathf.Round(position.y / this.snapValuePosition) * this.snapValuePosition;
  275. position.z = Mathf.Round(position.z / this.snapValuePosition) * this.snapValuePosition;
  276. if (!float.IsNaN(position.x) && !float.IsNaN(position.y) && !float.IsNaN(position.z))
  277. {
  278. this.position = position;
  279. this.m_CachedSnapPosition = position;
  280. }
  281. }
  282. if (num >= this.snapValueAngles)
  283. {
  284. Quaternion rotation = this.rotation;
  285. Quaternion cachedSnapRotation = this.m_CachedSnapRotation;
  286. float snapValueAngles = this.snapValueAngles;
  287. float num2 = Quaternion.Angle(cachedSnapRotation, rotation);
  288. if (Mathf.Abs(num2) > snapValueAngles)
  289. {
  290. Quaternion quaternion = Quaternion.RotateTowards(cachedSnapRotation, rotation, Mathf.Floor(num2 / snapValueAngles) * snapValueAngles);
  291. this.rotation = quaternion;
  292. this.m_CachedSnapRotation = quaternion;
  293. }
  294. }
  295. if (sqrMagnitude2 >= this.snapValueScale * this.snapValueScale)
  296. {
  297. Vector3 vector = Mathf.Round(this.scale.x / this.snapValueScale) * this.snapValueScale * Vector3.one;
  298. if (!float.IsNaN(vector.x) && !float.IsNaN(vector.y) && !float.IsNaN(vector.z))
  299. {
  300. this.scale = vector;
  301. this.m_CachedSnapScale = vector;
  302. }
  303. }
  304. }
  305. }
  306. public GameObject target;
  307. [SerializeField]
  308. private GizmoObject.ControlType m_ControlType;
  309. [SerializeField]
  310. private float m_SnapValuePosition = 1f;
  311. [SerializeField]
  312. private float m_SnapValueAngles = 30f;
  313. [SerializeField]
  314. private float m_SnapValueScale = 1f;
  315. private Vector3 m_CachedSnapPosition;
  316. private Quaternion m_CachedSnapRotation;
  317. private Vector3 m_CachedSnapScale;
  318. public Action<Vector3> onChangePosition;
  319. public Action<Quaternion> onChangeRotation;
  320. public Action<Vector3> onChangeScale;
  321. public enum ControlType
  322. {
  323. Default,
  324. Snap
  325. }
  326. }