PhotographView.cs 21 KB

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