PhotographView.cs 19 KB

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