PhotographView.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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 Vector3 distance = Vector3.zero;//点击位置和点击物体原点的距离,用于
  32. private SwipeGesture swipeGesture;
  33. private PinchGesture pinchGesture;
  34. private RotationGesture rotationGesture;
  35. protected override void OnInit()
  36. {
  37. base.OnInit();
  38. packageName = UI_PhotographUI.PACKAGE_NAME;
  39. _ui = UI_PhotographUI.Create();
  40. viewCom = _ui.target;
  41. isfullScreen = true;
  42. _ui.m_btnBg.onClick.Add(OnClickBtnBg);
  43. _ui.m_btnChoose.onClick.Add(OnClickBtnChoose);
  44. _ui.m_btnBack.onClick.Add(OnClickBtnBack);
  45. _ui.m_ComSelectRes.m_list.itemRenderer = RenderListItem;
  46. _ui.m_ComSelectRes.m_list.onClickItem.Add(OnListItemClick);
  47. _ui.m_ComSelectRes.m_c1.onChanged.Add(OnContorllerChanged);
  48. _scenePrefab = GFGAsset.Load<GameObject>(ResPathUtil.GetPrefabPath("ScenePhotograph"));
  49. _ui.target.onTouchBegin.Add(OnClickUIBegin);
  50. _ui.target.onTouchMove.Add(OnClickUIMove);
  51. _ui.target.onTouchEnd.Add(OnClickUIEnd);
  52. }
  53. protected override void OnShown()
  54. {
  55. base.OnShown();
  56. Input.multiTouchEnabled = true;
  57. equipDataCache = EquipDataCache.cacher;
  58. if (_sceneObject == null)
  59. {
  60. _sceneObject = GameObject.Instantiate(_scenePrefab);
  61. EquipDataCache.cacher.setSceneObj(_sceneObject);
  62. bgParent = _sceneObject.transform.Find("Bg").gameObject;
  63. bodyParent = _sceneObject.transform.Find("Scene/Role").gameObject;
  64. sceneParent = _sceneObject.transform.Find("Scene").gameObject;
  65. npcParent = _sceneObject.transform.Find("Scene/Npc").gameObject;
  66. borderParent = _sceneObject.transform.Find("Border").gameObject;
  67. }
  68. pinchGesture = new PinchGesture(_ui.target);
  69. pinchGesture.onAction.Add(OnPinch);
  70. rotationGesture = new RotationGesture(_ui.target);
  71. rotationGesture.onAction.Add(OnRotate);
  72. _ui.m_ComSelectRes.m_c1.selectedIndex = 0;
  73. OnClickBtnChoose();
  74. RefreshList(EnumPhotographType.BG);
  75. UpdateEquipData();
  76. UpdateBgOrNpcOrBorder(EnumPhotographType.BG, equipDataCache.bgId);
  77. UpdateBody();
  78. UpdateScene();
  79. // Timers.inst.Add(0.001f, 0, OnTimerListener);
  80. }
  81. /************************************************************UI界面*********************************************************/
  82. private void OnContorllerChanged(EventContext context)
  83. {
  84. int index = _ui.m_ComSelectRes.m_c1.selectedIndex;
  85. RefreshList((EnumPhotographType)index);
  86. }
  87. private void RefreshList(EnumPhotographType index)
  88. {
  89. _ui.m_ComSelectRes.m_list.numItems = 0;
  90. switch (index)
  91. {
  92. case EnumPhotographType.BG:
  93. _listData = PhotographDataManager.Instance.listBgData;
  94. break;
  95. case EnumPhotographType.NPC:
  96. _listData = PhotographDataManager.Instance.listNpcData;
  97. break;
  98. case EnumPhotographType.SCENE:
  99. _listData = PhotographDataManager.Instance.listSceneData;
  100. break;
  101. case EnumPhotographType.BORDER:
  102. _listData = PhotographDataManager.Instance.listBorderData;
  103. break;
  104. case EnumPhotographType.EFFECT:
  105. _listData = PhotographDataManager.Instance.listEffectData;
  106. break;
  107. }
  108. _ui.m_ComSelectRes.m_list.numItems = _listData.Count;
  109. }
  110. private void RenderListItem(int index, GObject obj)
  111. {
  112. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(_listData[index]);
  113. string resPath = ResPathUtil.GetPhotographFPath(itemCfg.res, ItemUtil.GetItemResExt(itemCfg.itemType, itemCfg.subType));
  114. UI_ListItem item = UI_ListItem.Proxy(obj);
  115. item.target.data = _listData[index];
  116. item.m_loaIcon.url = resPath;
  117. }
  118. private void OnListItemClick(EventContext context)
  119. {
  120. int itemID = (int)((context.data as GObject).data);
  121. EnumPhotographType type = (EnumPhotographType)_ui.m_ComSelectRes.m_c1.selectedIndex;
  122. switch (type)
  123. {
  124. case EnumPhotographType.BG:
  125. case EnumPhotographType.BORDER:
  126. case EnumPhotographType.NPC:
  127. UpdateBgOrNpcOrBorder(type, itemID);
  128. break;
  129. case EnumPhotographType.SCENE:
  130. if (!_equipSceneData.ContainsKey(itemID))
  131. {
  132. _equipSceneData.Add(itemID, new List<int>());
  133. }
  134. _equipSceneData[itemID].Add(itemID);
  135. SceneController.AddScene(_sceneObject, itemID, _equipSceneData[itemID].Count - 1);
  136. break;
  137. case EnumPhotographType.EFFECT:
  138. break;
  139. }
  140. }
  141. /************************************************************场景*********************************************************/
  142. private void OnClickUIBegin(EventContext context)
  143. {
  144. context.CaptureTouch();
  145. if (_ui.m_ComSelectRes.target.visible == true) return;
  146. if (context.inputEvent.touchId != 0) return;
  147. RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
  148. if (hit.collider != null)
  149. {
  150. hitParentGameObj = hit.collider.name == "BgRes" ? hit.collider.gameObject : hit.collider.transform.parent.gameObject;
  151. distance = Input.mousePosition - Camera.main.WorldToScreenPoint(hitParentGameObj.transform.position);
  152. Debug.Log("ClickName:" + hitParentGameObj.name);
  153. _ui.m_comSelectBox.target.visible = hitParentGameObj.transform.parent.gameObject == bgParent ? false : true;
  154. _ui.m_comSelectBox.target.size = SceneController.GetGameObjectSize(hitParentGameObj);
  155. ControllerSelectBoxPos(hitParentGameObj);
  156. }
  157. }
  158. private void OnClickUIMove(EventContext context)
  159. {
  160. if (hitParentGameObj == null) return;//未选中任何物体
  161. if (Stage.inst.touchCount > 1) return;//只监听1根手指
  162. ControllerObjectPos(hitParentGameObj);
  163. ControllerSelectBoxPos(hitParentGameObj);
  164. }
  165. private void OnClickUIEnd(EventContext context)
  166. {
  167. if (context.inputEvent.touchId == 0) hitParentGameObj = null;
  168. }
  169. //选中物体的位置
  170. private void ControllerObjectPos(GameObject hitParentGameObj)
  171. {
  172. hitParentGameObj.transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition - distance);
  173. if (hitParentGameObj.name == "BgRes")
  174. {
  175. Vector2 size = hitParentGameObj.GetComponent<SpriteRenderer>().size;
  176. Vector2 uiSize = _ui.target.size;
  177. float deviationWidth = (size.x - uiSize.x / 100) / 2;
  178. float deviationHeigh = (size.y - uiSize.y / 100) / 2;
  179. Vector2 pos = hitParentGameObj.transform.position;
  180. if (pos.x <= -deviationWidth)
  181. {
  182. hitParentGameObj.transform.position = new Vector2(-deviationWidth, hitParentGameObj.transform.position.y);
  183. }
  184. if (pos.x >= deviationWidth)
  185. {
  186. hitParentGameObj.transform.position = new Vector2(deviationWidth, hitParentGameObj.transform.position.y);
  187. }
  188. if (pos.y <= -deviationHeigh)
  189. {
  190. hitParentGameObj.transform.position = new Vector2(hitParentGameObj.transform.position.x, -deviationHeigh);
  191. }
  192. if (pos.y >= deviationHeigh)
  193. {
  194. hitParentGameObj.transform.position = new Vector2(hitParentGameObj.transform.position.x, deviationHeigh);
  195. }
  196. }
  197. }
  198. //选中框的位置
  199. private void ControllerSelectBoxPos(GameObject hitParentGameObj)
  200. {
  201. SceneController.SetGameObjectCenter(hitParentGameObj);
  202. Debug.Log("物体世界坐标:" + hitParentGameObj.transform.position);
  203. //位置:角色、道具、npc全用对应父物体的位置
  204. Vector2 objScreenPos = Camera.main.WorldToScreenPoint(hitParentGameObj.transform.position);
  205. this.viewCom.GlobalToLocal(objScreenPos);
  206. Debug.Log("物体屏幕坐标:" + hitParentGameObj.transform.position);
  207. Debug.Log("屏幕高:" + Screen.height);
  208. // Vector2 localPos = new Vector2(objScreenPos.x, (Screen.height - objScreenPos.y));
  209. Vector2 localPos = this.viewCom.GlobalToLocal(new Vector2(objScreenPos.x, (Screen.height - objScreenPos.y)));
  210. Debug.Log("localPos:" + localPos);
  211. Debug.Log("选框坐标:" + _ui.m_comSelectBox.target.position);
  212. _ui.m_comSelectBox.target.position = localPos;//(localPos.x, localPos.y);
  213. Debug.Log("选框坐标:" + _ui.m_comSelectBox.target.position);
  214. }
  215. private void OnPinch(EventContext context)
  216. {
  217. if (hitParentGameObj == null) return;
  218. GTween.Kill(hitParentGameObj);
  219. PinchGesture gesture = (PinchGesture)context.sender;
  220. float newValue = Mathf.Clamp(hitParentGameObj.transform.localScale.x + gesture.delta, 0.3f, 2);
  221. if (newValue > MaxScale || newValue < MinScale) return;
  222. hitParentGameObj.transform.localScale = new Vector3(newValue, newValue, newValue);
  223. _ui.m_comSelectBox.target.SetScale(newValue, newValue);
  224. }
  225. private void OnRotate(EventContext context)
  226. {
  227. if (hitParentGameObj == null) return;
  228. GTween.Kill(hitParentGameObj.transform);
  229. RotationGesture gesture = (RotationGesture)context.sender;
  230. hitParentGameObj.transform.Rotate(Vector3.forward, -gesture.delta, Space.World);
  231. _ui.m_comSelectBox.target.rotation += -gesture.delta;
  232. }
  233. //背景
  234. private void UpdateBgOrNpcOrBorder(EnumPhotographType type, int itemId)
  235. {
  236. SceneController.UpdatePhotographBgOrNpcOrBorder(_sceneObject, type, itemId);
  237. }
  238. //主角
  239. private void UpdateBody()
  240. {
  241. // int[] equipDatas = equipDataCache.equipDatas;
  242. SceneController.UpdateRole(_equipRoleData.ToArray(), _sceneObject, false, null, false, bodyParent);
  243. if (equipDataCache.IsSuitPic && equipDataCache.suitId > 0)
  244. {
  245. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(equipDataCache.suitId);
  246. DressUpUtil.UpdateBody(suitCfg.picRes, _sceneObject, false, null, false, bodyParent);
  247. }
  248. GameObject gameObject = _sceneObject.transform.Find("Scene/Role/Body").gameObject;
  249. SceneController.PhotographAddCollider(gameObject);
  250. }
  251. //场景道具
  252. private void UpdateScene()
  253. {
  254. ICollection keys = _equipSceneData.Keys;
  255. foreach (int key in keys)
  256. {
  257. for (int i = 0; i < _equipSceneData[key].Count; i++)
  258. {
  259. SceneController.AddScene(_sceneObject, key, i);
  260. }
  261. }
  262. }
  263. //滤镜效果
  264. private void UpdateEffect()
  265. {
  266. }
  267. private void UpdateEquipData()
  268. {
  269. for (int i = 0; i < equipDataCache.equipDatas.Length; i++)
  270. {
  271. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(equipDataCache.equipDatas[i]);
  272. if (itemCfg.subType == ConstDressUpItemType.QIAN_JING || itemCfg.subType == ConstDressUpItemType.BEI_SHI || itemCfg.subType == ConstDressUpItemType.HUAN_JING)
  273. {
  274. if (!_equipSceneData.ContainsKey(itemCfg.id))
  275. {
  276. _equipSceneData.Add(itemCfg.id, new List<int>());
  277. }
  278. _equipSceneData[itemCfg.id].Add(itemCfg.id);
  279. }
  280. else
  281. {
  282. _equipRoleData.Add(equipDataCache.equipDatas[i]);
  283. }
  284. }
  285. }
  286. private void OnClickBtnBg()
  287. {
  288. _ui.m_ComSelectRes.target.visible = false;
  289. }
  290. private void OnClickBtnChoose()
  291. {
  292. _ui.m_ComSelectRes.target.visible = true;
  293. _ui.m_comSelectBox.target.visible = false;
  294. hitParentGameObj = null;
  295. }
  296. private void OnClickBtnBack()
  297. {
  298. this.Hide();
  299. // ViewManager.Show(ViewName.DRESS_UP_VIEW);
  300. EventAgent.DispatchEvent(ConstMessage.CLOSE_PHOTOGRAPHVIEW);
  301. }
  302. protected override void OnHide()
  303. {
  304. base.OnHide();
  305. if (_sceneObject != null)
  306. {
  307. GameObject.Destroy(_sceneObject);
  308. _sceneObject = null;
  309. }
  310. equipDataCache = null;
  311. _equipRoleData.Clear();
  312. _equipSceneData.Clear();
  313. hitParentGameObj = null;
  314. pinchGesture.onAction.Remove(OnPinch);
  315. rotationGesture.onAction.Remove(OnRotate);
  316. pinchGesture = null;
  317. rotationGesture = null;
  318. }
  319. public override void Dispose()
  320. {
  321. if (_scenePrefab != null)
  322. {
  323. GameObject.Destroy(_scenePrefab);
  324. _scenePrefab = null;
  325. }
  326. base.Dispose();
  327. }
  328. }
  329. }