PhotographView.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. using FairyGUI;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UI.DressUp;
  6. using UnityEngine;
  7. using UnityEngine.EventSystems;
  8. namespace GFGGame
  9. {
  10. public class PhotographView : BaseView
  11. {
  12. private UI_PhotographUI _ui;
  13. private GameObject _scenePrefab;
  14. private GameObject _sceneObject;
  15. private DressUpObjDataCache equipDataCache;
  16. private const string MOVE = "MOVE";
  17. private const string SCALE = "SCALE";
  18. private const string ROTATION = "ROTATION";
  19. private const float MaxScale = 2;
  20. private const float MinScale = 0.1f;
  21. private GameObject bodyParent;
  22. private GameObject bgParent;
  23. private GameObject npcParent;
  24. private GameObject borderParent;
  25. private GameObject sceneParent;
  26. private List<int> _listData = null;//当前选择的资源数据
  27. private List<int> _equipRoleData = new List<int>();//当前穿戴的角色数据
  28. private Dictionary<int, List<int>> _equipSceneData = new Dictionary<int, List<int>>();//当前穿戴的场景数据
  29. private Dictionary<GameObject, Vector2> _equipPos = new Dictionary<GameObject, Vector2>();
  30. private Dictionary<GameObject, Vector2> _mousePos = new Dictionary<GameObject, Vector2>();
  31. private Dictionary<GameObject, float> _equipDistance = new Dictionary<GameObject, float>();
  32. // private GameObject hitGameObj;//当前选中的物体
  33. private GameObject hitParentGameObj;//当前选中的父物体(需要进行移动缩放旋转的物体)
  34. private GameObject memoryHitParentGameObj;//当前选中的父物体(需要进行移动缩放旋转的物体)
  35. private Vector3 distance = Vector3.zero;//点击位置和点击物体原点的距离,用于
  36. private Vector2 lastPos = Vector2.zero;//上一次移动后从物体中心到鼠标位置的方向
  37. private float _startDistance;//从物体中心到缩放按钮的距离
  38. private SwipeGesture swipeGesture;
  39. private PinchGesture pinchGesture;
  40. private RotationGesture rotationGesture;
  41. private bool isTouchUI = false;
  42. protected override void OnInit()
  43. {
  44. base.OnInit();
  45. packageName = UI_PhotographUI.PACKAGE_NAME;
  46. _ui = UI_PhotographUI.Create();
  47. viewCom = _ui.target;
  48. isfullScreen = true;
  49. _ui.m_btnBg.onClick.Add(OnClickBtnBg);
  50. _ui.m_btnChoose.onClick.Add(OnClickBtnChoose);
  51. _ui.m_btnBack.onClick.Add(OnClickBtnBack);
  52. _ui.m_comSelectBox.m_btnDelete.onClick.Add(OnClickBtnDelete);
  53. _ui.m_comSelectBox.m_btnFlip.onClick.Add(OnClickBtnFlip);
  54. _ui.m_btnPhotograph.onClick.Add(OnClickBtnPhotograph);
  55. _ui.m_ComSelectRes.m_list.itemRenderer = RenderListItem;
  56. _ui.m_ComSelectRes.m_list.onClickItem.Add(OnListItemClick);
  57. _ui.m_ComSelectRes.m_comBtnTab.m_c1.onChanged.Add(OnContorllerChanged);
  58. _scenePrefab = GFGAsset.Load<GameObject>(ResPathUtil.GetPrefabPath("ScenePhotograph"));
  59. _ui.target.onTouchBegin.Add(OnClickUIBegin);
  60. _ui.target.onTouchMove.Add(OnClickUIMove);
  61. _ui.target.onTouchEnd.Add(OnClickUIEnd);
  62. _ui.m_comSelectBox.m_btnSize.onTouchBegin.Add(OnClickBtnSizeBegin);
  63. _ui.m_comSelectBox.m_btnSize.onTouchMove.Add(OnClickBtnSizeMove);
  64. _ui.m_comSelectBox.m_btnSize.onTouchEnd.Add(OnClickBtnSizeEnd);
  65. }
  66. protected override void OnShown()
  67. {
  68. base.OnShown();
  69. Input.multiTouchEnabled = true;
  70. equipDataCache = EquipDataCache.cacher;
  71. if (_sceneObject == null)
  72. {
  73. _sceneObject = GameObject.Instantiate(_scenePrefab);
  74. EquipDataCache.cacher.setSceneObj(_sceneObject);
  75. bgParent = _sceneObject.transform.Find("Bg").gameObject;
  76. bodyParent = _sceneObject.transform.Find("Scene/Role").gameObject;
  77. sceneParent = _sceneObject.transform.Find("Scene").gameObject;
  78. npcParent = _sceneObject.transform.Find("Scene/Npc").gameObject;
  79. borderParent = _sceneObject.transform.Find("Border").gameObject;
  80. }
  81. pinchGesture = new PinchGesture(_ui.target);
  82. pinchGesture.onAction.Add(OnPinch);
  83. rotationGesture = new RotationGesture(_ui.target);
  84. rotationGesture.onAction.Add(OnRotate);
  85. _ui.m_ComSelectRes.m_comBtnTab.m_c1.selectedIndex = 0;
  86. OnClickBtnChoose();
  87. RefreshList(EnumPhotographType.BG);
  88. UpdateEquipData();
  89. UpdateBgOrNpcOrBorder(EnumPhotographType.BG, equipDataCache.bgId);
  90. UpdateBody();
  91. UpdateScene();
  92. // Timers.inst.Add(0.001f, 0, OnTimerListener);
  93. }
  94. /************************************************************UI界面*********************************************************/
  95. private void OnContorllerChanged(EventContext context)
  96. {
  97. int index = _ui.m_ComSelectRes.m_comBtnTab.m_c1.selectedIndex;
  98. RefreshList((EnumPhotographType)index);
  99. }
  100. private void RefreshList(EnumPhotographType index)
  101. {
  102. _ui.m_ComSelectRes.m_list.numItems = 0;
  103. switch (index)
  104. {
  105. case EnumPhotographType.BG:
  106. _listData = PhotographDataManager.Instance.listBgData;
  107. break;
  108. case EnumPhotographType.NPC:
  109. _listData = PhotographDataManager.Instance.listNpcData;
  110. break;
  111. case EnumPhotographType.SCENE:
  112. _listData = PhotographDataManager.Instance.listSceneData;
  113. break;
  114. case EnumPhotographType.BORDER:
  115. _listData = PhotographDataManager.Instance.listBorderData;
  116. break;
  117. case EnumPhotographType.EFFECT:
  118. _listData = PhotographDataManager.Instance.listEffectData;
  119. break;
  120. }
  121. _ui.m_ComSelectRes.m_list.numItems = _listData.Count;
  122. }
  123. private void RenderListItem(int index, GObject obj)
  124. {
  125. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(_listData[index]);
  126. string resPath = ResPathUtil.GetIconPath(itemCfg);
  127. UI_ListItem item = UI_ListItem.Proxy(obj);
  128. item.target.data = _listData[index];
  129. item.m_loaIcon.url = resPath;
  130. item.m_txtName.text = itemCfg.name;
  131. }
  132. private void OnListItemClick(EventContext context)
  133. {
  134. int itemID = (int)((context.data as GObject).data);
  135. EnumPhotographType type = (EnumPhotographType)_ui.m_ComSelectRes.m_comBtnTab.m_c1.selectedIndex;
  136. switch (type)
  137. {
  138. case EnumPhotographType.BG:
  139. case EnumPhotographType.BORDER:
  140. case EnumPhotographType.NPC:
  141. UpdateBgOrNpcOrBorder(type, itemID);
  142. break;
  143. case EnumPhotographType.SCENE:
  144. if (!_equipSceneData.ContainsKey(itemID))
  145. {
  146. _equipSceneData.Add(itemID, new List<int>());
  147. }
  148. _equipSceneData[itemID].Add(itemID);
  149. SceneController.AddScene(_sceneObject, itemID, _equipSceneData[itemID].Count - 1);
  150. break;
  151. case EnumPhotographType.EFFECT:
  152. break;
  153. }
  154. }
  155. /************************** **********************************场景*********************************************************/
  156. private void OnClickUIBegin(EventContext context)
  157. {
  158. context.CaptureTouch();
  159. if (_ui.m_ComSelectRes.target.visible == true) return;
  160. if (context.inputEvent.touchId != 0) return;
  161. if (isTouchUI) return;
  162. RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
  163. if (hit.collider != null)
  164. {
  165. hitParentGameObj = hit.collider.name == "BgRes" ? hit.collider.gameObject : hit.collider.transform.parent.gameObject;
  166. distance = Input.mousePosition - Camera.main.WorldToScreenPoint(hitParentGameObj.transform.position);
  167. _ui.m_comSelectBox.target.visible = hitParentGameObj.transform.parent.gameObject == bgParent ? false : true;
  168. if (_ui.m_comSelectBox.target.data == null || _ui.m_comSelectBox.target.data as GameObject != hitParentGameObj)
  169. {
  170. lastPos = Vector2.zero;
  171. // _ui.m_comSelectBox.target.SetScale(1, 1);
  172. _ui.m_comSelectBox.target.rotation = -hitParentGameObj.transform.eulerAngles.z;
  173. _ui.m_comSelectBox.target.size = SceneController.GetGameObjectSize(hitParentGameObj);
  174. _ui.m_comSelectBox.target.SetScale(hitParentGameObj.transform.localScale.x, hitParentGameObj.transform.localScale.y);
  175. }
  176. _ui.m_comSelectBox.m_btnDelete.visible = true;
  177. if (hitParentGameObj == bodyParent)
  178. {
  179. _ui.m_comSelectBox.m_btnDelete.visible = false;
  180. }
  181. _ui.m_comSelectBox.target.data = hitParentGameObj;
  182. ControllerSelectBoxPos(hitParentGameObj);
  183. }
  184. }
  185. private void OnClickUIMove(EventContext context)
  186. {
  187. if (hitParentGameObj == null) return;//未选中任何物体
  188. if (Stage.inst.touchCount > 1) return;//只监听1根手指
  189. if (isTouchUI) return;
  190. Debug.Log("拖动");
  191. ControllerObjectPos(hitParentGameObj);
  192. ControllerSelectBoxPos(hitParentGameObj);
  193. }
  194. private void OnClickUIEnd(EventContext context)
  195. {
  196. if (context.inputEvent.touchId == 0) hitParentGameObj = null;
  197. }
  198. //选中物体的位置
  199. private void ControllerObjectPos(GameObject hitParentGameObj)
  200. {
  201. hitParentGameObj.transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition - distance);
  202. if (hitParentGameObj.name == "BgRes")
  203. {
  204. Vector2 size = hitParentGameObj.GetComponent<SpriteRenderer>().size;
  205. Vector2 uiSize = _ui.target.size;
  206. float deviationWidth = (size.x - uiSize.x / 100) / 2;
  207. float deviationHeigh = (size.y - uiSize.y / 100) / 2;
  208. Vector2 pos = hitParentGameObj.transform.position;
  209. if (pos.x <= -deviationWidth)
  210. {
  211. hitParentGameObj.transform.position = new Vector2(-deviationWidth, hitParentGameObj.transform.position.y);
  212. }
  213. if (pos.x >= deviationWidth)
  214. {
  215. hitParentGameObj.transform.position = new Vector2(deviationWidth, hitParentGameObj.transform.position.y);
  216. }
  217. if (pos.y <= -deviationHeigh)
  218. {
  219. hitParentGameObj.transform.position = new Vector2(hitParentGameObj.transform.position.x, -deviationHeigh);
  220. }
  221. if (pos.y >= deviationHeigh)
  222. {
  223. hitParentGameObj.transform.position = new Vector2(hitParentGameObj.transform.position.x, deviationHeigh);
  224. }
  225. }
  226. }
  227. //选中框的位置
  228. private void ControllerSelectBoxPos(GameObject hitParentGameObj)
  229. {
  230. SceneController.SetGameObjectCenter(hitParentGameObj);
  231. //位置:角色、道具、npc全用对应父物体的位置
  232. Vector2 objScreenPos = Camera.main.WorldToScreenPoint(hitParentGameObj.transform.position);
  233. Vector2 localPos = this.viewCom.GlobalToLocal(new Vector2(objScreenPos.x, (Screen.height - objScreenPos.y)));
  234. _ui.m_comSelectBox.target.position = localPos;//(localPos.x, localPos.y);
  235. }
  236. private void OnClickBtnSizeBegin(EventContext context)
  237. {
  238. isTouchUI = true;
  239. GameObject gameObject = _ui.m_comSelectBox.target.data as GameObject;
  240. InputEvent inputEvent = (InputEvent)context.data;
  241. Vector2 screenPos = this.viewCom.GlobalToLocal(_ui.m_comSelectBox.m_btnSize.LocalToGlobal(Vector2.zero));
  242. Vector2 pt = new Vector2(screenPos.x, screenPos.y);
  243. // Vector2 pt1 = this.viewCom.GlobalToLocal(new Vector2(inputEvent.x, inputEvent.y));
  244. Vector2 pt2 = this.viewCom.GlobalToLocal(new Vector2(_ui.m_comSelectBox.target.x, _ui.m_comSelectBox.target.y));
  245. // Debug.Log("pt1" + pt1 + " pt2:" + pt2 + " pt:" + pt);
  246. if (!_equipDistance.ContainsKey(gameObject))
  247. {
  248. float distance = Vector2.Distance(pt, pt2);
  249. _equipDistance.Add(gameObject, distance);
  250. }
  251. _startDistance = _equipDistance[gameObject];
  252. // _startDistance = Vector2.Distance(pt1, pt2);
  253. // Vector2 pt1 = _ui.m_comSelectBox.target.size;// this.viewCom.GlobalToLocal(screenPos); ;
  254. // _startDistance = Vector2.Distance(pt, pt2);
  255. Debug.Log("_startDistance:" + _startDistance);
  256. }
  257. private void OnClickBtnSizeMove(EventContext context)
  258. {
  259. GameObject gameObject = _ui.m_comSelectBox.target.data as GameObject;
  260. if (gameObject == null) return;
  261. InputEvent inputEvent = (InputEvent)context.data;
  262. Vector2 pt1 = this.viewCom.GlobalToLocal(new Vector2(inputEvent.x, inputEvent.y));
  263. Vector2 pt2 = this.viewCom.GlobalToLocal(new Vector2(_ui.m_comSelectBox.target.x, _ui.m_comSelectBox.target.y));
  264. Vector2 curPos = pt1 - pt2;
  265. Debug.Log("pt1" + pt1 + " pt2:" + pt2 + " curPos:" + curPos + " lastPos:" + lastPos);
  266. float angle = Vector3.Angle(lastPos, curPos); //求出两向量之间的夹角
  267. Vector3 normal = Vector3.Cross(lastPos, curPos);//叉乘求出法线向量
  268. angle *= Mathf.Sign(Vector3.Dot(normal, Vector3.forward)); //Mathf.Sign()求符号,Vector3.Dot()求方向,求法线向量与物体上方向向量点乘,结果为1或-1,修正旋转方向
  269. lastPos = curPos;
  270. ControllerRotate(angle, gameObject);
  271. Debug.Log("angle:" + angle);
  272. float dist = Vector2.Distance(pt1, pt2);
  273. Debug.Log("dist:" + dist);
  274. float ss = dist / _startDistance;
  275. // float local = _equipDistance[gameObject] * gameObject.transform.localScale.x * ss / _equipDistance[gameObject];
  276. float newValue = Mathf.Clamp(ss, 0.1f, 2);
  277. ControllerScale(newValue, gameObject);
  278. }
  279. private void OnClickBtnSizeEnd(EventContext context)
  280. {
  281. isTouchUI = false;
  282. lastPos = Vector2.zero;
  283. }
  284. //双指缩放
  285. private void OnPinch(EventContext context)
  286. {
  287. if (hitParentGameObj == null) return;
  288. GTween.Kill(hitParentGameObj);
  289. PinchGesture gesture = (PinchGesture)context.sender;
  290. float newValue = Mathf.Clamp(hitParentGameObj.transform.localScale.x + gesture.delta, 0.3f, 2);
  291. ControllerScale(newValue, hitParentGameObj);
  292. }
  293. private void ControllerScale(float value, GameObject gameObject)
  294. {
  295. if (value > MaxScale || value < MinScale) return;
  296. gameObject.transform.localScale = new Vector3(value, value, value);
  297. Vector2 size = SceneController.GetGameObjectSize(gameObject);
  298. _ui.m_comSelectBox.target.SetSize(size.x * value, size.y * value);
  299. }
  300. private void OnRotate(EventContext context)
  301. {
  302. if (hitParentGameObj == null) return;
  303. GTween.Kill(hitParentGameObj.transform);
  304. RotationGesture gesture = (RotationGesture)context.sender;
  305. ControllerRotate(gesture.delta, hitParentGameObj);
  306. }
  307. private void ControllerRotate(float value, GameObject gameObject)
  308. {
  309. gameObject.transform.Rotate(Vector3.forward, -value, Space.World);
  310. _ui.m_comSelectBox.target.rotation += value;
  311. }
  312. //背景
  313. private void UpdateBgOrNpcOrBorder(EnumPhotographType type, int itemId)
  314. {
  315. SceneController.UpdatePhotographBgOrNpcOrBorder(_sceneObject, type, itemId);
  316. }
  317. //主角
  318. private void UpdateBody()
  319. {
  320. // int[] equipDatas = equipDataCache.equipDatas;
  321. SceneController.UpdateRole(_equipRoleData.ToArray(), _sceneObject, false, null, false, bodyParent);
  322. if (equipDataCache.IsSuitPic && equipDataCache.suitId > 0)
  323. {
  324. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(equipDataCache.suitId);
  325. DressUpUtil.UpdateBody(suitCfg.picRes, _sceneObject, false, null, false, bodyParent);
  326. }
  327. GameObject gameObject = _sceneObject.transform.Find("Scene/Role/Body").gameObject;
  328. SceneController.PhotographAddCollider(gameObject);
  329. }
  330. //场景道具
  331. private void UpdateScene()
  332. {
  333. ICollection keys = _equipSceneData.Keys;
  334. foreach (int key in keys)
  335. {
  336. for (int i = 0; i < _equipSceneData[key].Count; i++)
  337. {
  338. SceneController.AddScene(_sceneObject, key, i);
  339. }
  340. }
  341. }
  342. //滤镜效果
  343. private void UpdateEffect()
  344. {
  345. }
  346. private void UpdateEquipData()
  347. {
  348. for (int i = 0; i < equipDataCache.equipDatas.Length; i++)
  349. {
  350. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(equipDataCache.equipDatas[i]);
  351. if (itemCfg.subType == ConstDressUpItemType.QIAN_JING || itemCfg.subType == ConstDressUpItemType.BEI_SHI || itemCfg.subType == ConstDressUpItemType.HUAN_JING)
  352. {
  353. if (!_equipSceneData.ContainsKey(itemCfg.id))
  354. {
  355. _equipSceneData.Add(itemCfg.id, new List<int>());
  356. }
  357. _equipSceneData[itemCfg.id].Add(itemCfg.id);
  358. }
  359. else
  360. {
  361. _equipRoleData.Add(equipDataCache.equipDatas[i]);
  362. }
  363. }
  364. }
  365. private void OnClickBtnBg()
  366. {
  367. _ui.m_ComSelectRes.target.visible = false;
  368. }
  369. private void OnClickBtnChoose()
  370. {
  371. _ui.m_ComSelectRes.target.visible = true;
  372. _ui.m_comSelectBox.target.visible = false;
  373. hitParentGameObj = null;
  374. }
  375. private void OnClickBtnFlip()
  376. {
  377. GameObject gameObject = _ui.m_comSelectBox.target.data as GameObject;
  378. gameObject.transform.GetChild(0).Rotate(Vector3.up, 180, Space.World);
  379. // gameObject.transform.rotation.x = 180;
  380. }
  381. private void OnClickBtnDelete()
  382. {
  383. GameObject gameObject = _ui.m_comSelectBox.target.data as GameObject;
  384. if (gameObject.transform.gameObject == bodyParent)
  385. {
  386. return;
  387. }
  388. else if (gameObject.transform.gameObject == npcParent)
  389. {
  390. SpriteRenderer spriteRenderer = gameObject.transform.GetChild(0).GetComponent<SpriteRenderer>();
  391. if (spriteRenderer != null)
  392. {
  393. GameObject.Destroy(spriteRenderer);
  394. }
  395. }
  396. else
  397. {
  398. GameObject.DestroyImmediate(gameObject);
  399. }
  400. _ui.m_comSelectBox.target.visible = false;
  401. }
  402. private void OnClickBtnPhotograph()
  403. {
  404. _ui.target.visible = false;
  405. Timers.inst.StartCoroutine(ScreenShotTex());// ();
  406. }
  407. private IEnumerator ScreenShotTex()
  408. {
  409. _ui.target.visible = false;
  410. yield return new WaitForEndOfFrame();
  411. Rect rect = new Rect(0, 0, UnityEngine.Screen.width, UnityEngine.Screen.height);
  412. Texture2D tex = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.ARGB32, false);//新建一个Texture2D对象
  413. tex.ReadPixels(rect, 0, 0);//读取像素,屏幕左下角为0点
  414. tex.Apply();//保存像素信息
  415. ViewManager.Show<PhotographSaveView>(tex);
  416. _ui.target.visible = true;
  417. }
  418. private void OnClickBtnBack()
  419. {
  420. this.Hide();
  421. // ViewManager.Show(ViewName.DRESS_UP_VIEW);
  422. EventAgent.DispatchEvent(ConstMessage.CLOSE_PHOTOGRAPHVIEW);
  423. }
  424. protected override void OnHide()
  425. {
  426. base.OnHide();
  427. if (_sceneObject != null)
  428. {
  429. GameObject.Destroy(_sceneObject);
  430. _sceneObject = null;
  431. }
  432. equipDataCache = null;
  433. _equipRoleData.Clear();
  434. _equipSceneData.Clear();
  435. hitParentGameObj = null;
  436. pinchGesture.onAction.Remove(OnPinch);
  437. rotationGesture.onAction.Remove(OnRotate);
  438. pinchGesture = null;
  439. rotationGesture = null;
  440. }
  441. public override void Dispose()
  442. {
  443. if (_scenePrefab != null)
  444. {
  445. GameObject.Destroy(_scenePrefab);
  446. _scenePrefab = null;
  447. }
  448. base.Dispose();
  449. }
  450. }
  451. }