PhotographView.cs 15 KB

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