PhotographView.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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 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.m_comSelectBox.target);
  69. pinchGesture.onAction.Add(OnPinch);
  70. rotationGesture = new RotationGesture(_ui.m_comSelectBox.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)
  147. {
  148. firstTouchLastPos = Stage.inst.touchPosition;
  149. }
  150. else if (context.inputEvent.touchId == 1)
  151. {
  152. secondTouchLastPos = Stage.inst.touchPosition;
  153. }
  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. Debug.Log("ClickName:" + hitParentGameObj.name);
  161. _ui.m_comSelectBox.target.visible = hitParentGameObj.transform.parent.gameObject == bgParent ? false : true;
  162. _ui.m_comSelectBox.target.size = SceneController.GetGameObjectSize(hitParentGameObj);
  163. ControllerSelectBoxPos(hitParentGameObj);
  164. }
  165. }
  166. private void OnClickUIMove(EventContext context)
  167. {
  168. if (hitParentGameObj == null) return;//未选中任何物体
  169. if (Stage.inst.touchCount > MaxTouchCount) return;//只监听两根手指
  170. string state = GetTransformState();
  171. if (state == MOVE)
  172. {
  173. ControllerObjectPos(hitParentGameObj);
  174. ControllerSelectBoxPos(hitParentGameObj);
  175. }
  176. else if (state == ROTATION)
  177. {
  178. // Vector2 lastTransform = secondTouchLastPos - firstTouchLastPos;
  179. // Vector2 curTransform = Stage.inst.GetTouchPosition(1) - Stage.inst.GetTouchPosition(0);
  180. // float angle = SceneController.GetRotationAngle(lastTransform, curTransform);
  181. // _ui.m_comSelectBox.target.rotation += angle;
  182. // hitParentGameObj.transform.rotation = Quaternion.Euler(new Vector3(hitParentGameObj.transform.rotation.x, hitParentGameObj.transform.rotation.y, hitParentGameObj.transform.rotation.z + angle));
  183. }
  184. else
  185. {
  186. // Vector2 lastTransform = secondTouchLastPos - firstTouchLastPos;
  187. // Vector2 curTransform = Stage.inst.GetTouchPosition(1) - Stage.inst.GetTouchPosition(0);
  188. // float scale = curTransform.magnitude / lastTransform.magnitude;
  189. // _ui.m_comSelectBox.target.SetScale(scale, scale);
  190. // hitParentGameObj.transform.localScale = new Vector3(scale, scale, scale);
  191. }
  192. }
  193. private void OnPinch(EventContext context)
  194. {
  195. GTween.Kill(hitParentGameObj);
  196. PinchGesture gesture = (PinchGesture)context.sender;
  197. float newValue = Mathf.Clamp(hitParentGameObj.transform.localScale.x + gesture.delta, 0.3f, 2);
  198. hitParentGameObj.transform.localScale = new Vector3(newValue, newValue, newValue);
  199. }
  200. private void OnRotate(EventContext context)
  201. {
  202. GTween.Kill(hitParentGameObj.transform);
  203. RotationGesture gesture = (RotationGesture)context.sender;
  204. hitParentGameObj.transform.Rotate(Vector3.forward, -gesture.delta, Space.World);
  205. }
  206. private void OnClickUIEnd(EventContext context)
  207. {
  208. hitParentGameObj = null;
  209. firstTouchLastPos = Vector3.zero;
  210. }
  211. //选中物体的位置
  212. private void ControllerObjectPos(GameObject hitParentGameObj)
  213. {
  214. hitParentGameObj.transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition - distance);
  215. if (hitParentGameObj.name == "BgRes")
  216. {
  217. Vector2 size = hitParentGameObj.GetComponent<SpriteRenderer>().size;
  218. Vector2 uiSize = _ui.target.size;
  219. float deviationWidth = (size.x - uiSize.x / 100) / 2;
  220. float deviationHeigh = (size.y - uiSize.y / 100) / 2;
  221. Vector2 pos = hitParentGameObj.transform.position;
  222. if (pos.x <= -deviationWidth)
  223. {
  224. hitParentGameObj.transform.position = new Vector2(-deviationWidth, hitParentGameObj.transform.position.y);
  225. }
  226. if (pos.x >= deviationWidth)
  227. {
  228. hitParentGameObj.transform.position = new Vector2(deviationWidth, hitParentGameObj.transform.position.y);
  229. }
  230. if (pos.y <= -deviationHeigh)
  231. {
  232. hitParentGameObj.transform.position = new Vector2(hitParentGameObj.transform.position.x, -deviationHeigh);
  233. }
  234. if (pos.y >= deviationHeigh)
  235. {
  236. hitParentGameObj.transform.position = new Vector2(hitParentGameObj.transform.position.x, deviationHeigh);
  237. }
  238. }
  239. }
  240. //选中框的大小位置旋转
  241. private void ControllerSelectBoxPos(GameObject hitParentGameObj)
  242. {
  243. SceneController.SetGameObjectCenter(hitParentGameObj);
  244. //位置:角色、道具、npc全用对应父物体的位置
  245. Vector2 objScreenPos = Camera.main.WorldToScreenPoint(hitParentGameObj.transform.position);
  246. Vector2 localPos = new Vector2(objScreenPos.x, Screen.height - objScreenPos.y);
  247. _ui.m_comSelectBox.target.SetXY(localPos.x, localPos.y);
  248. }
  249. private string GetTransformState()
  250. {
  251. if (Stage.inst.touchCount == 1) return MOVE;//一根手指只能移动
  252. Vector2 lastDistance = secondTouchLastPos - firstTouchLastPos;
  253. Vector2 curDistance = Stage.inst.GetTouchPosition(1) - Stage.inst.GetTouchPosition(0);
  254. if (lastDistance == curDistance)
  255. {
  256. return MOVE;//向量大小和方向都不变是位移
  257. }
  258. else
  259. {
  260. if (lastDistance.magnitude != curDistance.magnitude)
  261. {
  262. //两指距离有变化是缩放
  263. return SCALE;
  264. }
  265. else
  266. {
  267. //两指距离无变化是缩放
  268. return ROTATION;
  269. }
  270. }
  271. }
  272. //背景
  273. private void UpdateBgOrNpcOrBorder(EnumPhotographType type, int itemId)
  274. {
  275. SceneController.UpdatePhotographBgOrNpcOrBorder(_sceneObject, type, itemId);
  276. }
  277. //主角
  278. private void UpdateBody()
  279. {
  280. // int[] equipDatas = equipDataCache.equipDatas;
  281. SceneController.UpdateRole(_equipRoleData.ToArray(), _sceneObject, false, null, false, bodyParent);
  282. if (equipDataCache.IsSuitPic && equipDataCache.suitId > 0)
  283. {
  284. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(equipDataCache.suitId);
  285. DressUpUtil.UpdateBody(suitCfg.picRes, _sceneObject, false, null, false, bodyParent);
  286. }
  287. GameObject gameObject = _sceneObject.transform.Find("Scene/Role/Body").gameObject;
  288. SceneController.PhotographAddCollider(gameObject);
  289. }
  290. //场景道具
  291. private void UpdateScene()
  292. {
  293. ICollection keys = _equipSceneData.Keys;
  294. foreach (int key in keys)
  295. {
  296. for (int i = 0; i < _equipSceneData[key].Count; i++)
  297. {
  298. SceneController.AddScene(_sceneObject, key, i);
  299. }
  300. }
  301. }
  302. //滤镜效果
  303. private void UpdateEffect()
  304. {
  305. }
  306. private void UpdateEquipData()
  307. {
  308. for (int i = 0; i < equipDataCache.equipDatas.Length; i++)
  309. {
  310. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(equipDataCache.equipDatas[i]);
  311. if (itemCfg.subType == ConstDressUpItemType.QIAN_JING || itemCfg.subType == ConstDressUpItemType.BEI_SHI || itemCfg.subType == ConstDressUpItemType.HUAN_JING)
  312. {
  313. if (!_equipSceneData.ContainsKey(itemCfg.id))
  314. {
  315. _equipSceneData.Add(itemCfg.id, new List<int>());
  316. }
  317. _equipSceneData[itemCfg.id].Add(itemCfg.id);
  318. }
  319. else
  320. {
  321. _equipRoleData.Add(equipDataCache.equipDatas[i]);
  322. }
  323. }
  324. }
  325. private void OnClickBtnBg()
  326. {
  327. _ui.m_ComSelectRes.target.visible = false;
  328. }
  329. private void OnClickBtnChoose()
  330. {
  331. _ui.m_ComSelectRes.target.visible = true;
  332. _ui.m_comSelectBox.target.visible = false;
  333. hitParentGameObj = null;
  334. }
  335. private void OnClickBtnBack()
  336. {
  337. this.Hide();
  338. // ViewManager.Show(ViewName.DRESS_UP_VIEW);
  339. EventAgent.DispatchEvent(ConstMessage.CLOSE_PHOTOGRAPHVIEW);
  340. }
  341. protected override void OnHide()
  342. {
  343. base.OnHide();
  344. if (_sceneObject != null)
  345. {
  346. GameObject.Destroy(_sceneObject);
  347. _sceneObject = null;
  348. }
  349. equipDataCache = null;
  350. _equipRoleData.Clear();
  351. _equipSceneData.Clear();
  352. hitParentGameObj = null;
  353. pinchGesture.onAction.Remove(OnPinch);
  354. rotationGesture.onAction.Remove(OnRotate);
  355. pinchGesture = null;
  356. rotationGesture = null;
  357. }
  358. public override void Dispose()
  359. {
  360. if (_scenePrefab != null)
  361. {
  362. GameObject.Destroy(_scenePrefab);
  363. _scenePrefab = null;
  364. }
  365. base.Dispose();
  366. }
  367. }
  368. }