PhotographView.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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;//点击在m_comSelectBox按钮上
  42. private int maxLayer = int.MinValue;//最上层的层级数
  43. protected override void OnInit()
  44. {
  45. base.OnInit();
  46. packageName = UI_PhotographUI.PACKAGE_NAME;
  47. _ui = UI_PhotographUI.Create();
  48. viewCom = _ui.target;
  49. isfullScreen = true;
  50. _ui.m_btnBg.onClick.Add(OnClickBtnBg);
  51. _ui.m_btnChoose.onClick.Add(OnClickBtnChoose);
  52. _ui.m_btnBack.onClick.Add(OnClickBtnBack);
  53. _ui.m_btnPhotograph.onClick.Add(OnClickBtnPhotograph);
  54. _ui.m_ComSelectRes.m_list.itemRenderer = RenderListItem;
  55. _ui.m_ComSelectRes.m_list.onClickItem.Add(OnListItemClick);
  56. _ui.m_ComSelectRes.m_comBtnTab.m_c1.onChanged.Add(OnContorllerChanged);
  57. _scenePrefab = GFGAsset.Load<GameObject>(ResPathUtil.GetPrefabPath("ScenePhotograph"));
  58. _ui.target.onTouchBegin.Add(OnTouchUIBegin);
  59. _ui.target.onTouchMove.Add(OnTouchUIMove);
  60. _ui.target.onTouchEnd.Add(OnTouchUIEnd);
  61. _ui.m_comSelectBox.m_btnSize.onTouchBegin.Add(OnTouchBtnSizeBegin);
  62. _ui.m_comSelectBox.m_btnSize.onTouchMove.Add(OnTouchBtnSizeMove);
  63. _ui.m_comSelectBox.m_btnSize.onTouchEnd.Add(OnTouchBtnSizeEnd);
  64. _ui.m_comSelectBox.m_btnFlip.onTouchBegin.Add(OnTouchBtnFlipBegin);
  65. _ui.m_comSelectBox.m_btnFlip.onTouchEnd.Add(OnTouchBtnFlipEnd);
  66. _ui.m_comSelectBox.m_btnDelete.onTouchBegin.Add(OnTouchBtnDeleteBegin);
  67. _ui.m_comSelectBox.m_btnDelete.onTouchEnd.Add(OnTouchBtnDeleteEnd);
  68. }
  69. protected override void OnShown()
  70. {
  71. base.OnShown();
  72. Input.multiTouchEnabled = true;
  73. equipDataCache = EquipDataCache.cacher;
  74. if (_sceneObject == null)
  75. {
  76. _sceneObject = GameObject.Instantiate(_scenePrefab);
  77. EquipDataCache.cacher.setSceneObj(_sceneObject);
  78. bgParent = _sceneObject.transform.Find("Bg").gameObject;
  79. bodyParent = _sceneObject.transform.Find("Scene/Role").gameObject;
  80. sceneParent = _sceneObject.transform.Find("Scene").gameObject;
  81. npcParent = _sceneObject.transform.Find("Scene/Npc").gameObject;
  82. borderParent = _sceneObject.transform.Find("Border").gameObject;
  83. }
  84. pinchGesture = new PinchGesture(_ui.target);
  85. pinchGesture.onAction.Add(OnPinch);
  86. rotationGesture = new RotationGesture(_ui.target);
  87. rotationGesture.onAction.Add(OnRotate);
  88. _ui.m_ComSelectRes.m_comBtnTab.m_c1.selectedIndex = 0;
  89. OnClickBtnChoose();
  90. RefreshList(EnumPhotographType.BG);
  91. DisposeEquipData();
  92. SceneController.UpdatePhotographBgOrNpcOrBorder(_sceneObject, EnumPhotographType.BG, equipDataCache.bgId, maxLayer, out maxLayer);
  93. UpdateBody();
  94. UpdateScene();
  95. }
  96. /************************************************************UI界面*********************************************************/
  97. private void OnContorllerChanged(EventContext context)
  98. {
  99. int index = _ui.m_ComSelectRes.m_comBtnTab.m_c1.selectedIndex;
  100. RefreshList((EnumPhotographType)index);
  101. }
  102. private void RefreshList(EnumPhotographType index)
  103. {
  104. _ui.m_ComSelectRes.m_list.numItems = 0;
  105. switch (index)
  106. {
  107. case EnumPhotographType.BG:
  108. _listData = PhotographDataManager.Instance.listBgData;
  109. break;
  110. case EnumPhotographType.NPC:
  111. _listData = PhotographDataManager.Instance.listNpcData;
  112. break;
  113. case EnumPhotographType.SCENE:
  114. _listData = PhotographDataManager.Instance.listSceneData;
  115. break;
  116. case EnumPhotographType.BORDER:
  117. _listData = PhotographDataManager.Instance.listBorderData;
  118. break;
  119. case EnumPhotographType.EFFECT:
  120. _listData = PhotographDataManager.Instance.listEffectData;
  121. break;
  122. }
  123. _ui.m_ComSelectRes.m_list.numItems = _listData.Count;
  124. }
  125. private void RenderListItem(int index, GObject obj)
  126. {
  127. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(_listData[index]);
  128. string resPath = ResPathUtil.GetIconPath(itemCfg);
  129. UI_ListItem item = UI_ListItem.Proxy(obj);
  130. item.target.data = _listData[index];
  131. item.m_loaIcon.url = resPath;
  132. item.m_txtName.text = itemCfg.name;
  133. }
  134. private void OnListItemClick(EventContext context)
  135. {
  136. int itemID = (int)((context.data as GObject).data);
  137. EnumPhotographType type = (EnumPhotographType)_ui.m_ComSelectRes.m_comBtnTab.m_c1.selectedIndex;
  138. switch (type)
  139. {
  140. case EnumPhotographType.BG:
  141. case EnumPhotographType.BORDER:
  142. case EnumPhotographType.NPC:
  143. SceneController.UpdatePhotographBgOrNpcOrBorder(_sceneObject, type, itemID, maxLayer, out maxLayer);
  144. break;
  145. case EnumPhotographType.SCENE:
  146. PhotographDataManager.Instance.AddEquipItem(_equipSceneData, itemID, out _equipSceneData);
  147. SceneController.AddScene(_sceneObject, itemID, _equipSceneData[itemID].Count - 1, maxLayer + 1);
  148. maxLayer++;
  149. break;
  150. case EnumPhotographType.EFFECT:
  151. break;
  152. }
  153. }
  154. /************************** **********************************场景*********************************************************/
  155. private void OnTouchUIBegin(EventContext context)
  156. {
  157. context.CaptureTouch();
  158. if (_ui.m_ComSelectRes.target.visible == true) return;
  159. if (Stage.inst.touchCount > 1 || context.inputEvent.touchId != 0) return;//只监听第一根1手指
  160. if (isTouchUI) return;
  161. RaycastHit2D[] hit2Ds = Physics2D.RaycastAll(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
  162. if (hit2Ds.Length > 0)
  163. {
  164. maxLayer++;
  165. hitParentGameObj = SceneController.GetFirstHitObj(hit2Ds);
  166. _ui.m_comSelectBox.m_btnDelete.visible = true;
  167. if (hitParentGameObj.name == "Body")//主角不可删除
  168. {
  169. _ui.m_comSelectBox.m_btnDelete.visible = false;
  170. }
  171. _ui.m_comSelectBox.target.visible = false;
  172. if (hitParentGameObj.name != "BgRes")//背景不可改动层级
  173. {
  174. hitParentGameObj.GetComponent<SpriteRenderer>().sortingOrder = maxLayer;
  175. hitParentGameObj = hitParentGameObj.transform.parent.gameObject;
  176. _ui.m_comSelectBox.target.visible = true;
  177. }
  178. distance = Input.mousePosition - Camera.main.WorldToScreenPoint(hitParentGameObj.transform.position);
  179. if (_ui.m_comSelectBox.target.data == null || _ui.m_comSelectBox.target.data as GameObject != hitParentGameObj)
  180. {
  181. lastPos = Vector2.zero;
  182. _ui.m_comSelectBox.target.rotation = -hitParentGameObj.transform.eulerAngles.z;
  183. _ui.m_comSelectBox.target.size = SceneController.GetGameObjectSize(hitParentGameObj);
  184. }
  185. _ui.m_comSelectBox.target.data = hitParentGameObj;
  186. ControllerSelectBoxPos();
  187. }
  188. }
  189. private void OnTouchUIMove(EventContext context)
  190. {
  191. if (hitParentGameObj == null) return;//未选中任何物体
  192. if (Stage.inst.touchCount > 1 || context.inputEvent.touchId != 0) return; //只监听1根手指
  193. if (isTouchUI) return;
  194. Debug.Log("拖动");
  195. ControllerObjectPos();
  196. ControllerSelectBoxPos();
  197. }
  198. private void OnTouchUIEnd(EventContext context)
  199. {
  200. if (Stage.inst.touchCount > 1 || context.inputEvent.touchId != 0) return; //只监听1根手指
  201. hitParentGameObj = null;
  202. }
  203. //选中物体的位置
  204. private void ControllerObjectPos()
  205. {
  206. hitParentGameObj.transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition - distance);
  207. if (hitParentGameObj.name == "BgRes")
  208. {
  209. Vector2 size = hitParentGameObj.GetComponent<SpriteRenderer>().size;
  210. Vector2 uiSize = _ui.target.size;
  211. float deviationWidth = (size.x - uiSize.x / 100) / 2;
  212. float deviationHeigh = (size.y - uiSize.y / 100) / 2;
  213. Vector2 pos = hitParentGameObj.transform.position;
  214. if (pos.x <= -deviationWidth)
  215. {
  216. hitParentGameObj.transform.position = new Vector2(-deviationWidth, hitParentGameObj.transform.position.y);
  217. }
  218. if (pos.x >= deviationWidth)
  219. {
  220. hitParentGameObj.transform.position = new Vector2(deviationWidth, hitParentGameObj.transform.position.y);
  221. }
  222. if (pos.y <= -deviationHeigh)
  223. {
  224. hitParentGameObj.transform.position = new Vector2(hitParentGameObj.transform.position.x, -deviationHeigh);
  225. }
  226. if (pos.y >= deviationHeigh)
  227. {
  228. hitParentGameObj.transform.position = new Vector2(hitParentGameObj.transform.position.x, deviationHeigh);
  229. }
  230. }
  231. }
  232. //选中框的位置
  233. private void ControllerSelectBoxPos()
  234. {
  235. SceneController.SetGameObjectCenter(hitParentGameObj);
  236. //位置:角色、道具、npc全用对应父物体的位置
  237. Vector2 objScreenPos = Camera.main.WorldToScreenPoint(hitParentGameObj.transform.position);
  238. Vector2 localPos = this.viewCom.GlobalToLocal(new Vector2(objScreenPos.x, (Screen.height - objScreenPos.y)));
  239. _ui.m_comSelectBox.target.position = localPos;//(localPos.x, localPos.y);
  240. }
  241. private void OnTouchBtnSizeBegin(EventContext context)
  242. {
  243. isTouchUI = true;
  244. GameObject gameObject = _ui.m_comSelectBox.target.data as GameObject;
  245. InputEvent inputEvent = (InputEvent)context.data;
  246. Vector2 pt1 = this.viewCom.GlobalToLocal(new Vector2(inputEvent.x, inputEvent.y));
  247. Vector2 pt2 = new Vector2(_ui.m_comSelectBox.target.x, _ui.m_comSelectBox.target.y);
  248. Debug.Log("pt1" + pt1 + " pt2:" + pt2);
  249. if (!_equipDistance.ContainsKey(gameObject))
  250. {
  251. float distance = Vector2.Distance(pt1, pt2);
  252. _equipDistance.Add(gameObject, distance);
  253. }
  254. _startDistance = _equipDistance[gameObject];
  255. Debug.Log("_startDistance:" + _startDistance);
  256. }
  257. private void OnTouchBtnSizeMove(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 = 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 newValue = Mathf.Clamp(ss, 0.1f, 2);
  276. ControllerScale(newValue, gameObject);
  277. }
  278. private void OnTouchBtnSizeEnd(EventContext context)
  279. {
  280. isTouchUI = false;
  281. lastPos = Vector2.zero;
  282. }
  283. //双指缩放
  284. private void OnPinch(EventContext context)
  285. {
  286. if (hitParentGameObj == null) return;
  287. if (hitParentGameObj == bgParent) 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. Debug.Log("双指缩放:" + value);
  298. distance = distance * value;
  299. Vector2 size = SceneController.GetGameObjectSize(gameObject);
  300. _ui.m_comSelectBox.target.SetSize(size.x, size.y);
  301. }
  302. private void OnRotate(EventContext context)
  303. {
  304. if (hitParentGameObj == null) return;
  305. if (hitParentGameObj == bgParent) return;//背景不可以缩放、旋转
  306. GTween.Kill(hitParentGameObj.transform);
  307. RotationGesture gesture = (RotationGesture)context.sender;
  308. ControllerRotate(gesture.delta, hitParentGameObj);
  309. }
  310. private void ControllerRotate(float value, GameObject gameObject)
  311. {
  312. Debug.Log("双指旋转:" + value);
  313. gameObject.transform.Rotate(Vector3.forward, -value, Space.World);
  314. _ui.m_comSelectBox.target.rotation += value;
  315. }
  316. //主角
  317. private void UpdateBody()
  318. {
  319. // SceneController.UpdateRole(_equipRoleData.ToArray(), _sceneObject, false, null, false, bodyParent);
  320. // if (equipDataCache.IsSuitPic && equipDataCache.suitId > 0)
  321. // {
  322. // SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(equipDataCache.suitId);
  323. // DressUpUtil.UpdateBody(suitCfg.picRes, _sceneObject, false, null, false, bodyParent);
  324. // }
  325. // GameObject gameObject = _sceneObject.transform.Find("Scene/Role/Body").gameObject;
  326. // SceneController.PhotographAddCollider(gameObject);
  327. SceneController.UpdatePhotographBody(_equipRoleData.ToArray(), _sceneObject, false, null, false, bodyParent);
  328. }
  329. //场景道具
  330. private void UpdateScene()
  331. {
  332. ICollection keys = _equipSceneData.Keys;
  333. foreach (int key in keys)
  334. {
  335. for (int i = 0; i < _equipSceneData[key].Count; i++)
  336. {
  337. SceneController.AddScene(_sceneObject, key, i);
  338. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(key);
  339. PhotographDataManager.Instance.GetMaxLayer(maxLayer, false, itemCfg, out maxLayer);
  340. }
  341. }
  342. }
  343. //滤镜效果
  344. private void UpdateEffect()
  345. {
  346. }
  347. private void DisposeEquipData()
  348. {
  349. for (int i = 0; i < equipDataCache.equipDatas.Length; i++)
  350. {
  351. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(equipDataCache.equipDatas[i]);
  352. if (itemCfg.subType == ConstDressUpItemType.QIAN_JING || itemCfg.subType == ConstDressUpItemType.BEI_SHI || itemCfg.subType == ConstDressUpItemType.HUAN_JING)
  353. {
  354. PhotographDataManager.Instance.AddEquipItem(_equipSceneData, itemCfg.id, out _equipSceneData);
  355. }
  356. else
  357. {
  358. _equipRoleData.Add(equipDataCache.equipDatas[i]);
  359. }
  360. }
  361. }
  362. private void OnClickBtnBg()
  363. {
  364. _ui.m_ComSelectRes.target.visible = false;
  365. }
  366. private void OnClickBtnChoose()
  367. {
  368. _ui.m_ComSelectRes.target.visible = true;
  369. _ui.m_comSelectBox.target.visible = false;
  370. hitParentGameObj = null;
  371. }
  372. private void OnTouchBtnFlipBegin()
  373. {
  374. isTouchUI = true;
  375. Transform transform = (_ui.m_comSelectBox.target.data as GameObject).transform;
  376. for (int i = 0; i < transform.childCount; i++)
  377. {
  378. transform.GetChild(i).Rotate(Vector3.up, 180, Space.World);
  379. }
  380. }
  381. private void OnTouchBtnFlipEnd()
  382. {
  383. isTouchUI = false;
  384. }
  385. private void OnTouchBtnDeleteBegin()
  386. {
  387. isTouchUI = true;
  388. GameObject gameObject = _ui.m_comSelectBox.target.data as GameObject;
  389. if (gameObject.transform.gameObject == bodyParent)
  390. {
  391. return;
  392. }
  393. else if (gameObject.transform.gameObject == npcParent)
  394. {
  395. SpriteRenderer spriteRenderer = gameObject.transform.GetChild(0).GetComponent<SpriteRenderer>();
  396. if (spriteRenderer != null)
  397. {
  398. GameObject.Destroy(spriteRenderer);
  399. }
  400. }
  401. else
  402. {
  403. GameObject.DestroyImmediate(gameObject);
  404. }
  405. _ui.m_comSelectBox.target.visible = false;
  406. }
  407. private void OnTouchBtnDeleteEnd()
  408. {
  409. isTouchUI = false;
  410. }
  411. private void OnClickBtnPhotograph()
  412. {
  413. _ui.target.visible = false;
  414. Timers.inst.StartCoroutine(ScreenShotTex());// ();
  415. }
  416. private IEnumerator ScreenShotTex()
  417. {
  418. _ui.target.visible = false;
  419. yield return new WaitForEndOfFrame();
  420. Rect rect = new Rect(0, 0, UnityEngine.Screen.width, UnityEngine.Screen.height);
  421. Texture2D tex = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.ARGB32, false);//新建一个Texture2D对象
  422. tex.ReadPixels(rect, 0, 0);//读取像素,屏幕左下角为0点
  423. tex.Apply();//保存像素信息
  424. ViewManager.Show<PhotographSaveView>(tex);
  425. _ui.target.visible = true;
  426. }
  427. private void OnClickBtnBack()
  428. {
  429. this.Hide();
  430. // ViewManager.Show(ViewName.DRESS_UP_VIEW);
  431. EventAgent.DispatchEvent(ConstMessage.CLOSE_PHOTOGRAPHVIEW);
  432. }
  433. protected override void OnHide()
  434. {
  435. base.OnHide();
  436. if (_sceneObject != null)
  437. {
  438. GameObject.Destroy(_sceneObject);
  439. _sceneObject = null;
  440. }
  441. equipDataCache = null;
  442. _equipRoleData.Clear();
  443. _equipSceneData.Clear();
  444. hitParentGameObj = null;
  445. pinchGesture.onAction.Remove(OnPinch);
  446. rotationGesture.onAction.Remove(OnRotate);
  447. pinchGesture = null;
  448. rotationGesture = null;
  449. }
  450. public override void Dispose()
  451. {
  452. if (_scenePrefab != null)
  453. {
  454. GameObject.Destroy(_scenePrefab);
  455. _scenePrefab = null;
  456. }
  457. base.Dispose();
  458. }
  459. }
  460. }