PhotographView.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  1. using ET;
  2. using FairyGUI;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Drawing;
  7. using System.Linq;
  8. using UI.DressUp;
  9. using UnityEditor;
  10. using UnityEngine;
  11. using UnityEngine.EventSystems;
  12. using System.Threading.Tasks;
  13. namespace GFGGame
  14. {
  15. public class PhotographView : BaseView
  16. {
  17. private UI_PhotographUI _ui;
  18. private GameObject _sceneObject;
  19. private const int MAX_COUNT = 20;
  20. private const float MaxScale = 2;
  21. private const float MinScale = 0.1f;
  22. // private const string BgResPath = "Bg/BgRes";
  23. // private const string BorderResPath = "Border/BorderRes";
  24. // private const string NpcResPath = "Scene/Npc/NpcRes";
  25. private const string RolePath = "Scene/Role";
  26. private const string RoleName = "Role";
  27. private const string BodyResName = "Body";
  28. private const string BodyResName1 = "Body_a";
  29. // private const string NpcPath = "Scene/Npc";
  30. private const string NpcName = "Npc";
  31. private const string BgResName = "BgRes";
  32. private const string Border = "Border";
  33. private List<int> _listData = null;//当前选择的资源数据
  34. private List<GameObject> _itemGameObjs;// = new List<GameObject>();
  35. public Dictionary<int, List<int>> _equipSceneData = new Dictionary<int, List<int>>();//当前穿戴的场景数据
  36. private Dictionary<GameObject, float> _equipDistance = new Dictionary<GameObject, float>();
  37. private GameObject hitGameObj;//当前选中的物体(单指拖动,双指缩放旋转)
  38. private GameObject memoryHitGameObj;//当前选中的物体(单指缩放旋转)
  39. private Vector3 distance = Vector3.zero;//点击位置和点击物体原点的距离,用于
  40. private Vector2 lastPos = Vector2.zero;//上一次移动后从物体中心到鼠标位置的方向
  41. private float lastDistance = 0;//上一次移动后从物体中心到鼠标位置的方向
  42. // private float _startDistance;//从物体中心到缩放按钮的距离
  43. private SwipeGesture swipeGesture;
  44. private PinchGesture pinchGesture;
  45. private RotationGesture rotationGesture;
  46. // 以下两个属于单选,单独保存
  47. private int lastChooseBorder; // 上一次选择的边框
  48. private int lastChooseBg; // 上一次选择的背景
  49. public override void Dispose()
  50. {
  51. if (_sceneObject != null)
  52. {
  53. PrefabManager.Instance.Restore(_sceneObject);
  54. _sceneObject = null;
  55. }
  56. if (_ui != null)
  57. {
  58. _ui.Dispose();
  59. _ui = null;
  60. }
  61. base.Dispose();
  62. }
  63. protected override void OnInit()
  64. {
  65. base.OnInit();
  66. packageName = UI_PhotographUI.PACKAGE_NAME;
  67. _ui = UI_PhotographUI.Create();
  68. viewCom = _ui.target;
  69. isfullScreen = true;
  70. //isReturnView = true;
  71. _ui.m_btnBg.onClick.Add(OnClickBtnBg);
  72. _ui.m_btnChoose.onClick.Add(OnClickBtnChoose);
  73. _ui.m_btnBack.onClick.Add(OnClickBtnBack);
  74. _ui.m_btnPhotograph.onClick.Add(OnClickBtnPhotograph);
  75. _ui.m_btnGalleryJoin.onClick.Add(OnClickBtnGalleryJoin);
  76. _ui.m_ComSelectRes.m_list.itemRenderer = RenderListItem;
  77. _ui.m_ComSelectRes.m_list.onClickItem.Add(OnListItemClick);
  78. _ui.m_ComSelectRes.m_comBtnTab.m_c1.onChanged.Add(OnContorllerChanged);
  79. _ui.target.onTouchBegin.Add(OnTouchUIBegin);
  80. _ui.target.onTouchMove.Add(OnTouchUIMove);
  81. _ui.target.onTouchEnd.Add(OnTouchUIEnd);
  82. _ui.m_comSelectBox.m_btnSize.onTouchBegin.Add(OnTouchBtnSizeBegin);
  83. _ui.m_comSelectBox.m_btnSize.onTouchMove.Add(OnTouchBtnSizeMove);
  84. _ui.m_comSelectBox.m_btnSize.onTouchEnd.Add(OnTouchBtnSizeEnd);
  85. _ui.m_comSelectBox.m_btnFlip.onTouchBegin.Add(OnTouchBtnFlipBegin);
  86. // _ui.m_comSelectBox.m_btnFlip.onTouchEnd.Add(OnTouchBtnFlipEnd);
  87. _ui.m_comSelectBox.m_btnDelete.onTouchBegin.Add(OnTouchBtnDeleteBegin);
  88. _ui.m_btnUp.onClick.Add(OnClickBtnUp);
  89. _ui.m_btnDown.onClick.Add(OnClickBtnDown);
  90. _ui.m_btnDelete.onTouchBegin.Add(OnClickBtnDelete);
  91. }
  92. protected override async void OnShown()
  93. {
  94. base.OnShown();
  95. Input.multiTouchEnabled = true;
  96. if (this.viewData != null)
  97. {
  98. _ui.m_c1.selectedIndex = (int)this.viewData;
  99. }
  100. PhotographDataManager.Instance.ClassifyEquipData();
  101. _equipSceneData = PhotographDataManager.Instance._equipSceneData;
  102. PhotographDataManager.Instance.itemGameObjs.Clear();
  103. _itemGameObjs = PhotographDataManager.Instance.itemGameObjs;
  104. if (_sceneObject == null)
  105. {
  106. _sceneObject = PrefabManager.Instance.InstantiateSync(ResPathUtil.GetPrefabPath("ScenePhotograph"));
  107. }
  108. PhotographSceneManager.Instance.sceneObject = _sceneObject;
  109. pinchGesture = new PinchGesture(_ui.target);
  110. pinchGesture.onAction.Add(OnPinch);
  111. rotationGesture = new RotationGesture(_ui.target);
  112. rotationGesture.onAction.Add(OnRotate);
  113. _ui.m_ComSelectRes.m_comBtnTab.m_c1.selectedIndex = 0;
  114. MatchingCompetitionDataManager.Instance.itemGameObjs.Clear();
  115. RefreshList(EnumPhotographType.BG);
  116. SetUIView();
  117. PhotographSceneManager.Instance.AddBgItem(ItemCfgArray.Instance.GetCfg(MyDressUpHelper.dressUpObj.bgId));
  118. SetListScrollByID(MyDressUpHelper.dressUpObj.bgId);
  119. PhotographSceneManager.Instance.AddBodyItem();
  120. UpdateScene();
  121. PhotographUtil.Instance.SetLayer(null, "refresh");
  122. _ui.m_txtCount.text = string.Format("{0}/{1}", PoemGalleryDataManager.Instance.MyWorksCountOfTheme, GlobalCfgArray.globalCfg.maxGalleryWorksCount);
  123. Timers.inst.AddUpdate(CheckGuide);
  124. //Timers.inst.Add(0.5f, 1, UpdateSceneTransform);
  125. if(this.viewData != null && (int)this.viewData == 2)
  126. {
  127. if (MyDressUpHelper.dressUpObj.bgId != 0)
  128. {
  129. MatchingCompetitionDataManager.Instance.DressUpBgID = MyDressUpHelper.dressUpObj.bgId;
  130. MatchingCompetitionDataManager.Instance.MathingDressDate.bgId = MatchingCompetitionDataManager.Instance.DressUpBgID;
  131. }
  132. await UpdateSceneTransform();
  133. }
  134. }
  135. /************************************************************UI界面*********************************************************/
  136. private void OnContorllerChanged(EventContext context)
  137. {
  138. int index = _ui.m_ComSelectRes.m_comBtnTab.m_c1.selectedIndex;
  139. RefreshList((EnumPhotographType)index);
  140. }
  141. private void RefreshList(EnumPhotographType index)
  142. {
  143. _ui.m_ComSelectRes.m_list.numItems = 0;
  144. _listData = PhotographDataManager.Instance.GetListData(index);
  145. _ui.m_ComSelectRes.m_list.numItems = _listData.Count;
  146. bool setTop = false;
  147. switch (index)
  148. {
  149. case EnumPhotographType.BORDER:
  150. _ui.m_ComSelectRes.m_list.selectedIndex = lastChooseBorder;
  151. break;
  152. case EnumPhotographType.BG:
  153. _ui.m_ComSelectRes.m_list.selectedIndex = lastChooseBg;
  154. break;
  155. default:
  156. setTop = true;
  157. break;
  158. }
  159. if (!setTop)
  160. {
  161. if(_ui.m_ComSelectRes.m_list.selectedIndex >= 0)
  162. {
  163. _ui.m_ComSelectRes.m_list.ScrollToView(_ui.m_ComSelectRes.m_list.selectedIndex);
  164. }
  165. }
  166. else
  167. {
  168. _ui.m_ComSelectRes.m_list.scrollPane.ScrollTop();
  169. }
  170. //if (index == EnumPhotographType.BORDER || index == EnumPhotographType.EFFECT)
  171. //{
  172. // _ui.m_ComSelectRes.m_list.selectedIndex = 0;
  173. //}
  174. }
  175. private void SetListScrollByID(int id)
  176. {
  177. for(int i = 0; i < _listData.Count; i++)
  178. {
  179. if(id == _listData[i])
  180. {
  181. _ui.m_ComSelectRes.m_list.selectedIndex = i;
  182. _ui.m_ComSelectRes.m_list.ScrollToView(_ui.m_ComSelectRes.m_list.selectedIndex);
  183. lastChooseBg = i;
  184. }
  185. }
  186. }
  187. private void RenderListItem(int index, GObject obj)
  188. {
  189. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(_listData[index]);
  190. string resPath = ResPathUtil.GetIconPath(itemCfg);
  191. UI_ListItem item = UI_ListItem.Proxy(obj);
  192. item.target.data = _listData[index];
  193. item.m_loaIcon.url = resPath;
  194. item.m_txtName.text = itemCfg.name;
  195. UI_ListItem.ProxyEnd();
  196. }
  197. private void OnListItemClick(EventContext context)
  198. {
  199. EnumPhotographType type = (EnumPhotographType)_ui.m_ComSelectRes.m_comBtnTab.m_c1.selectedIndex;
  200. if (type == EnumPhotographType.SCENE && _itemGameObjs.Count >= MAX_COUNT)
  201. {
  202. PromptController.Instance.ShowFloatTextPrompt("最多穿戴" + MAX_COUNT + "件物品");
  203. return;
  204. }
  205. switch (type)
  206. {
  207. case EnumPhotographType.BORDER:
  208. lastChooseBorder = _ui.m_ComSelectRes.m_list.selectedIndex;
  209. break;
  210. case EnumPhotographType.BG:
  211. lastChooseBg = _ui.m_ComSelectRes.m_list.selectedIndex;
  212. break;
  213. }
  214. int itemID = (int)((context.data as GObject).data);
  215. ChangeItem(itemID);
  216. }
  217. private void ChangeItem(int itemID)
  218. {
  219. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemID);
  220. EnumPhotographType type = (EnumPhotographType)_ui.m_ComSelectRes.m_comBtnTab.m_c1.selectedIndex;
  221. switch (type)
  222. {
  223. case EnumPhotographType.BG:
  224. MatchingCompetitionDataManager.Instance.DressUpBgID = itemCfg.id;
  225. MatchingCompetitionDataManager.Instance.MathingDressDate.bgId = itemCfg.id;
  226. PhotographSceneManager.Instance.AddBgItem(itemCfg);
  227. break;
  228. case EnumPhotographType.BORDER:
  229. if (itemCfg.id != ConstItemID.BORDERID)
  230. {
  231. MatchingCompetitionDataManager.Instance.BorderID = itemCfg.id;
  232. }
  233. else
  234. {
  235. MatchingCompetitionDataManager.Instance.BorderID = 0;
  236. }
  237. PhotographSceneManager.Instance.AddBorderItem(itemCfg);
  238. break;
  239. case EnumPhotographType.NPC:
  240. MatchingCompetitionDataManager.Instance.NpcID = itemCfg.id;
  241. PhotographSceneManager.Instance.AddNpcItem(itemCfg);
  242. break;
  243. case EnumPhotographType.SCENE:
  244. if (this.viewData == null || (int)this.viewData != 2)
  245. {
  246. AddSceneItem(itemCfg, true);
  247. }
  248. else
  249. {
  250. MatchingCompetitionDataManager.Instance.AddSceneItem(itemCfg, true);
  251. }
  252. break;
  253. case EnumPhotographType.EFFECT:
  254. break;
  255. }
  256. }
  257. //添加初始场景道具
  258. private void UpdateScene()
  259. {
  260. if (this.viewData == null || (int)this.viewData != 2)
  261. {
  262. ICollection keys = _equipSceneData.Keys;
  263. foreach (int key in keys)
  264. {
  265. for (int i = 0; i < _equipSceneData[key].Count; i++)
  266. {
  267. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(key);
  268. AddSceneItem(itemCfg, false);
  269. }
  270. }
  271. }
  272. else
  273. {
  274. ICollection keys = _equipSceneData.Keys;
  275. foreach (int key in keys)
  276. {
  277. for (int i = 0; i < _equipSceneData[key].Count; i++)
  278. {
  279. if(!MatchingCompetitionDataManager.Instance.DressPropTransInfoDic.ContainsKey(key.ToString()))
  280. {
  281. MatchingCompetitionDataManager.Instance.DressPropTransInfoDic.Add(key.ToString(),new TransformData());
  282. }
  283. }
  284. }
  285. //对搭配赛特殊处理
  286. foreach(var item in MatchingCompetitionDataManager.Instance.DressPropTransInfoDic)
  287. {
  288. if(item.Key == MatchingCompetitionDataManager.Instance.roleID)
  289. {
  290. continue;
  291. }
  292. else
  293. {
  294. List<int> idList = MatchingCompetitionDataManager.Instance.GetIDListByString(item.Key);
  295. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(idList[0]);
  296. if (itemCfg.itemType == ConstItemType.PHOTOGRAPH && itemCfg.subType == ConstItemSubType.PHOTOGRAPH_BOREDR)
  297. {
  298. PhotographSceneManager.Instance.AddBorderItem(itemCfg);
  299. }
  300. else if(itemCfg.itemType == ConstItemType.PHOTOGRAPH && itemCfg.subType == ConstItemSubType.PHOTOGRAPH_NPC)
  301. {
  302. PhotographSceneManager.Instance.AddNpcItem(itemCfg);
  303. }
  304. else
  305. {
  306. MatchingCompetitionDataManager.Instance.AddSceneItemOne(itemCfg,idList[1],idList[2]);
  307. }
  308. }
  309. }
  310. }
  311. }
  312. public async Task UpdateSceneTransform()
  313. {
  314. //int time = 5 * (MatchingCompetitionDataManager.Instance.TransformDataList.Count+1);
  315. if (MatchingCompetitionDataManager.Instance.DressUpBgID == 0)
  316. {
  317. PhotographSceneManager.Instance.AddBgItem(ItemCfgArray.Instance.GetCfg(180001));
  318. }
  319. else
  320. {
  321. PhotographSceneManager.Instance.AddBgItem(ItemCfgArray.Instance.GetCfg(MatchingCompetitionDataManager.Instance.DressUpBgID));
  322. }
  323. await Task.Delay(100);
  324. if (MatchingCompetitionDataManager.Instance.BorderID != 0)
  325. {
  326. GameObject borderGameObj = PhotographSceneManager.Instance.sceneObject.transform.Find("Border").gameObject;
  327. MatchingCompetitionDataManager.Instance.itemGameObjs.Add(borderGameObj);
  328. }
  329. if (MatchingCompetitionDataManager.Instance.NpcID != 0)
  330. {
  331. GameObject NpcGameObj = PhotographSceneManager.Instance.sceneObject.transform.Find("Scene/Npc").gameObject;
  332. MatchingCompetitionDataManager.Instance.itemGameObjs.Add(NpcGameObj);
  333. }
  334. MatchingCompetitionDataManager.Instance.InsertGameObjectList();
  335. for (int i = 0; i < MatchingCompetitionDataManager.Instance.itemGameObjs.Count; i++)
  336. {
  337. GameObject itemGObj = MatchingCompetitionDataManager.Instance.itemGameObjs[i];
  338. TransformData itemdata = new TransformData();
  339. if (MatchingCompetitionDataManager.Instance.DressPropTransInfoDic.ContainsKey(itemGObj.name))
  340. {
  341. itemdata = MatchingCompetitionDataManager.Instance.DressPropTransInfoDic[itemGObj.name];
  342. }
  343. MatchingCompetitionDataManager.Instance.itemGameObjs[i].transform.position = itemdata.position;
  344. MatchingCompetitionDataManager.Instance.itemGameObjs[i].transform.Rotate(itemGObj.transform.eulerAngles.x, itemGObj.transform.eulerAngles.y, itemdata.rotationZ);
  345. MatchingCompetitionDataManager.Instance.itemGameObjs[i].transform.localScale = itemdata.scale;
  346. PhotographUtil.Instance.ChangeLayer(MatchingCompetitionDataManager.Instance.itemGameObjs[i], i * PhotographDataManager.layerCount, "up");
  347. }
  348. }
  349. private async void AddSceneItem(ItemCfg itemCfg, bool setLayer)
  350. {
  351. Vector3 pos = Vector3.zero;
  352. if (!string.IsNullOrEmpty(itemCfg.resLayer3))
  353. {
  354. GameObject parentGameObj3 = new GameObject(string.Format("{0}_{1}", itemCfg.id, 3));
  355. await PhotographSceneManager.Instance.AddSceneItem(parentGameObj3, itemCfg, 3, setLayer);
  356. if (setLayer)
  357. {
  358. if (parentGameObj3.transform.childCount > 0)
  359. {
  360. parentGameObj3.transform.localPosition = -parentGameObj3.transform.GetChild(0).localPosition;
  361. pos = parentGameObj3.transform.localPosition;
  362. }
  363. }
  364. }
  365. if (!string.IsNullOrEmpty(itemCfg.resLayer2))
  366. {
  367. GameObject parentGameObj2 = new GameObject(string.Format("{0}_{1}", itemCfg.id, 2));
  368. await PhotographSceneManager.Instance.AddSceneItem(parentGameObj2, itemCfg, 2, setLayer);
  369. if (setLayer)
  370. {
  371. if (parentGameObj2.transform.childCount > 0)
  372. {
  373. parentGameObj2.transform.localPosition = pos == Vector3.zero ? -parentGameObj2.transform.GetChild(0).localPosition : pos;
  374. pos = parentGameObj2.transform.localPosition;
  375. }
  376. }
  377. }
  378. if (!string.IsNullOrEmpty(itemCfg.resLayer1))
  379. {
  380. GameObject parentGameObj1 = new GameObject(string.Format("{0}_{1}", itemCfg.id, 1));
  381. await PhotographSceneManager.Instance.AddSceneItem(parentGameObj1, itemCfg, 1, setLayer);
  382. if (setLayer)
  383. {
  384. if (parentGameObj1.transform.childCount > 0)
  385. {
  386. parentGameObj1.transform.localPosition = pos == Vector3.zero ? -parentGameObj1.transform.GetChild(0).localPosition : pos;
  387. pos = parentGameObj1.transform.localPosition;
  388. }
  389. }
  390. }
  391. }
  392. /************************************************************场景*********************************************************/
  393. private void OnTouchUIBegin(EventContext context)
  394. {
  395. context.CaptureTouch();
  396. if (_ui.m_ComSelectRes.target.visible == true) return;//添加道具不监听场景点击
  397. if (Stage.inst.touchCount > 1 && hitGameObj != null || Stage.inst.touchCount == 1 && context.inputEvent.touchId != 0) return;//两根手指&&两指不是同时按下||一根手指但属于中途换指
  398. if (PhotographUtil.Instance.IsTouchUI(this.viewCom)) return;
  399. RaycastHit2D[] hit2Ds = Physics2D.RaycastAll(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
  400. if (hit2Ds.Length > 0)
  401. {
  402. lastPos = Vector2.zero;
  403. hitGameObj = PhotographUtil.Instance.GetFirstHitObj(hit2Ds);
  404. _ui.m_comSelectBox.m_btnDelete.visible = true;
  405. if (hitGameObj.transform.parent.name == RoleName)//主角不可删除
  406. {
  407. _ui.m_comSelectBox.m_btnDelete.visible = false;
  408. }
  409. _ui.m_comSelectBox.target.visible = false;
  410. if (hitGameObj.name != BgResName)//背景不可改动层级
  411. {
  412. hitGameObj = hitGameObj.transform.parent.gameObject;
  413. _ui.m_comSelectBox.target.visible = true;
  414. if (hitGameObj.name != Border)
  415. {
  416. PhotographUtil.Instance.SetLayer(hitGameObj, "top");
  417. }
  418. }
  419. memoryHitGameObj = hitGameObj;
  420. distance = Input.mousePosition - Camera.main.WorldToScreenPoint(hitGameObj.transform.position);
  421. Vector3 localEulerAngles = hitGameObj.transform.localEulerAngles;
  422. float rotation = 0;
  423. if (localEulerAngles.y == 0)
  424. {
  425. rotation = -hitGameObj.transform.localEulerAngles.z;
  426. }
  427. else
  428. {
  429. if (hitGameObj.transform.localEulerAngles.z > 180)
  430. {
  431. rotation = hitGameObj.transform.localEulerAngles.z - 360;
  432. }
  433. else
  434. {
  435. rotation = hitGameObj.transform.localEulerAngles.z;
  436. }
  437. }
  438. _ui.m_comSelectBox.target.rotation = rotation;
  439. _ui.m_comSelectBox.target.size = PhotographUtil.Instance.GetGameObjectBoundsSize(hitGameObj);
  440. ControllerSelectBoxPos();
  441. }
  442. }
  443. private void OnTouchUIMove(EventContext context)
  444. {
  445. if (hitGameObj == null) return;//未选中任何物体
  446. if (Stage.inst.touchCount > 1) return; //只监听1根手指
  447. if (PhotographUtil.Instance.IsTouchUI(this.viewCom)) return;
  448. ControllerObjectPos();
  449. ControllerSelectBoxPos();
  450. }
  451. private void OnTouchUIEnd(EventContext context)
  452. {
  453. hitGameObj = null;
  454. }
  455. //选中物体的位置
  456. private void ControllerObjectPos()
  457. {
  458. hitGameObj.transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition - distance);
  459. if (hitGameObj.name == BgResName)
  460. {
  461. PhotographUtil.Instance.SetBgPos(hitGameObj, _ui.target.size);
  462. }
  463. }
  464. //选中框的位置
  465. private void ControllerSelectBoxPos()
  466. {
  467. Vector2 objScreenPos = Camera.main.WorldToScreenPoint(hitGameObj.transform.position);
  468. Vector2 localPos = this.viewCom.GlobalToLocal(new Vector2(objScreenPos.x, (Screen.height - objScreenPos.y)));
  469. _ui.m_comSelectBox.target.position = localPos;
  470. }
  471. private void OnTouchBtnSizeBegin(EventContext context)
  472. {
  473. InputEvent inputEvent = (InputEvent)context.data;
  474. Vector2 pt0 = this.viewCom.GlobalToLocal(new Vector2(inputEvent.x, inputEvent.y));
  475. Vector2 screenPos = this.viewCom.GlobalToLocal(_ui.m_comSelectBox.m_btnSize.LocalToGlobal(Vector2.zero));
  476. Vector2 pt1 = new Vector2(screenPos.x, screenPos.y);
  477. Vector2 pt2 = new Vector2(_ui.m_comSelectBox.target.x, _ui.m_comSelectBox.target.y);
  478. lastDistance = Vector2.Distance(pt0, pt2);
  479. if (!_equipDistance.ContainsKey(memoryHitGameObj))
  480. {
  481. float distance = Vector2.Distance(pt1, pt2) / memoryHitGameObj.transform.localScale.x;
  482. Debug.Log("distance:" + distance + " pt1:" + pt1 + " pt2:" + pt2);
  483. _equipDistance.Add(memoryHitGameObj, distance);
  484. }
  485. }
  486. private void OnTouchBtnSizeMove(EventContext context)
  487. {
  488. if (memoryHitGameObj == null) return;
  489. InputEvent inputEvent = (InputEvent)context.data;
  490. Vector2 pt1 = this.viewCom.GlobalToLocal(inputEvent.position);
  491. Vector2 pt2 = _ui.m_comSelectBox.target.xy;
  492. Vector2 curPos = pt1 - pt2;
  493. float angle = Vector3.Angle(lastPos, curPos); //求出两向量之间的夹角
  494. Vector3 normal = Vector3.Cross(lastPos, curPos);//叉乘求出法线向量
  495. angle *= Mathf.Sign(Vector3.Dot(normal, Vector3.forward)); //Mathf.Sign()求符号,Vector3.Dot()求方向,求法线向量与物体上方向向量点乘,结果为1或-1,修正旋转方向
  496. lastPos = curPos;
  497. ControllerRotate(angle, memoryHitGameObj);
  498. float dist = Vector2.Distance(pt1, pt2);
  499. float ss = dist / lastDistance;
  500. float curValue = Math.Abs(memoryHitGameObj.transform.localScale.x);
  501. float newValue = Mathf.Clamp(ss * curValue, 0.1f, 2);
  502. ControllerScale(newValue, memoryHitGameObj, Math.Sign(memoryHitGameObj.transform.localScale.x));
  503. if (newValue <= 0.1f)
  504. {
  505. lastDistance = Vector2.Distance(_ui.m_comSelectBox.m_btnDelete.xy, _ui.m_comSelectBox.m_btnSize.xy) / 2;
  506. }
  507. else
  508. {
  509. lastDistance = dist;
  510. }
  511. }
  512. private void OnTouchBtnSizeEnd(EventContext context)
  513. {
  514. lastPos = Vector2.zero;
  515. }
  516. //双指缩放
  517. private void OnPinch(EventContext context)
  518. {
  519. if (hitGameObj == null) return;
  520. if (hitGameObj.name == BgResName) return;//背景不可以缩放、旋转
  521. GTween.Kill(hitGameObj);
  522. PinchGesture gesture = (PinchGesture)context.sender;
  523. float newValue = Mathf.Clamp(hitGameObj.transform.localScale.x + gesture.delta, 0.3f, 2);
  524. Debug.Log("双指缩放:" + newValue);
  525. ControllerScale(newValue, hitGameObj, Math.Sign(hitGameObj.transform.localScale.x));
  526. }
  527. //双指旋转
  528. private void OnRotate(EventContext context)
  529. {
  530. Debug.Log("双指旋转hitGameObj:" + hitGameObj);
  531. if (hitGameObj == null) return;
  532. Debug.Log("双指旋转name:" + hitGameObj.name);
  533. if (hitGameObj.name == BgResName) return;//背景不可以缩放、旋转
  534. // isTwoTouchPoint = true;
  535. GTween.Kill(hitGameObj.transform);
  536. RotationGesture gesture = (RotationGesture)context.sender;
  537. Debug.Log("双指旋转:" + gesture.delta);
  538. ControllerRotate(gesture.delta, hitGameObj);
  539. }
  540. private void ControllerScale(float value, GameObject gameObject, int sign)
  541. {
  542. if (value > MaxScale || value < MinScale) return;
  543. gameObject.transform.localScale = new Vector3(value * sign, value, value);
  544. Vector2 size = PhotographUtil.Instance.GetGameObjectBoundsSize(gameObject);
  545. _ui.m_comSelectBox.target.size = size;
  546. }
  547. private void ControllerRotate(float value, GameObject gameObject)
  548. {
  549. gameObject.transform.Rotate(Vector3.forward, -value, Space.World);
  550. _ui.m_comSelectBox.target.rotation += value;
  551. }
  552. private void OnClickBtnBg()
  553. {
  554. _ui.m_ComSelectRes.target.visible = false;
  555. }
  556. private void OnTouchBtnFlipBegin()//翻转
  557. {
  558. Transform transform = memoryHitGameObj.transform;
  559. var value = Math.Abs(transform.localScale.x);
  560. // transform.localScale = new Vector3(-value, Math.Abs(value), 1);
  561. ControllerScale(value, memoryHitGameObj, -Math.Sign(transform.localScale.x));
  562. }
  563. private void OnTouchBtnDeleteBegin()//删除
  564. {
  565. if (memoryHitGameObj.transform.name == RolePath)
  566. {
  567. return;
  568. }
  569. else if (memoryHitGameObj.transform.name == Border || memoryHitGameObj.transform.name == NpcName)
  570. {
  571. memoryHitGameObj.transform.localPosition = Vector3.zero;
  572. memoryHitGameObj.transform.localRotation = Quaternion.Euler(Vector3.zero);
  573. memoryHitGameObj.transform.localScale = Vector3.one;
  574. GameObject gameObject = memoryHitGameObj.transform.GetChild(0).gameObject;
  575. PhotographSceneManager.Instance.DeleteSpriteRendererFromGameObject(gameObject);
  576. PhotographSceneManager.Instance.DeleteBoxCollider2DFromGameObject(gameObject);
  577. }
  578. else
  579. {
  580. GameObject.DestroyImmediate(memoryHitGameObj);
  581. }
  582. _itemGameObjs.Remove(memoryHitGameObj);
  583. MatchingCompetitionDataManager.Instance.DressPropIndex--;
  584. _ui.m_comSelectBox.target.visible = false;
  585. }
  586. private void SetUIView()
  587. {
  588. _ui.m_ComSelectRes.target.visible = true;
  589. _ui.m_comSelectBox.target.visible = false;
  590. hitGameObj = null;
  591. memoryHitGameObj = null;
  592. lastChooseBorder = 0;
  593. lastChooseBg = 0;
  594. _ui.m_ComSelectRes.m_comBtnTab.target.scrollPane.SetPercX(0, false);
  595. _ui.m_ComSelectRes.m_comBtnTab.m_c1.selectedIndex = 0;
  596. if (_ui.m_ComSelectRes.m_list.numItems > 0)
  597. {
  598. _ui.m_ComSelectRes.m_list.ScrollToView(0);
  599. _ui.m_ComSelectRes.m_list.selectedIndex = 0;
  600. }
  601. }
  602. private void OnClickBtnChoose()
  603. {
  604. _ui.m_ComSelectRes.target.visible = true;
  605. _ui.m_comSelectBox.target.visible = false;
  606. hitGameObj = null;
  607. memoryHitGameObj = null;
  608. }
  609. private void OnClickBtnUp()
  610. {
  611. PhotographUtil.Instance.SetLayer(memoryHitGameObj, "up");
  612. }
  613. private void OnClickBtnDown()
  614. {
  615. PhotographUtil.Instance.SetLayer(memoryHitGameObj, "down");
  616. }
  617. private void OnClickBtnDelete()
  618. {
  619. if(memoryHitGameObj == null || !_ui.m_comSelectBox.target.visible || !_ui.m_comSelectBox.m_btnDelete.visible)
  620. {
  621. return;
  622. }
  623. OnTouchBtnDeleteBegin();
  624. }
  625. private void OnClickBtnPhotograph()
  626. {
  627. // GRoot.inst.visible = false;
  628. Timers.inst.StartCoroutine(ScreenShotTex());// ();
  629. }
  630. private void OnClickBtnGalleryJoin()
  631. {
  632. // GRoot.inst.visible = false;
  633. Timers.inst.StartCoroutine(ScreenShotTex());// ();
  634. }
  635. private IEnumerator ScreenShotTex()
  636. {
  637. GRoot.inst.visible = false;
  638. yield return new WaitForEndOfFrame();
  639. Rect rect = new Rect(0, 0, UnityEngine.Screen.width, UnityEngine.Screen.height);
  640. Texture2D tex = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.ARGB32, false);//新建一个Texture2D对象
  641. tex.ReadPixels(rect, 0, 0);//读取像素,屏幕左下角为0点
  642. tex.Apply();//保存像素信息
  643. GRoot.inst.visible = true;
  644. if (_ui.m_c1.selectedIndex == 0)
  645. {
  646. ViewManager.Show<PhotographSaveView>(tex);
  647. LogServerHelper.SendPlayParticipationLog((int)PlayParticipationEnum.PAI_ZHAO, 2);
  648. }
  649. else if (_ui.m_c1.selectedIndex == 1)
  650. {
  651. AlertUI.Show("是否确认上传此作品?")
  652. .SetLeftButton(true, "否").SetRightButton(true, "是", async (object data) =>
  653. {
  654. ViewManager.Show<ModalStatusView>("上传中...");
  655. object[] rsp = await PoemGallerySProxy.ReqUpLoadGalleryWorks();
  656. if (rsp == null) return;
  657. byte[] bytes = tex.EncodeToJPG();
  658. bool pushResult = await PictureStorageHelper.PushToHWCloud(rsp[0].ToString(), bytes);
  659. if (!pushResult) return;
  660. bool result = await PoemGallerySProxy.ReqNoticeGalleryWorksUploaded((long)(rsp[1]), tex);
  661. if (result)
  662. {
  663. _ui.m_txtCount.text = string.Format("{0}/{1}", PoemGalleryDataManager.Instance.MyWorksCountOfTheme, GlobalCfgArray.globalCfg.maxGalleryWorksCount);
  664. ViewManager.Hide<ModalStatusView>();
  665. OnClickBtnBack();
  666. ViewManager.GoBackFrom(typeof(DressUpView).FullName);
  667. }
  668. });
  669. }
  670. else if (_ui.m_c1.selectedIndex == 2)
  671. {
  672. MatchingCompetitionDataManager.Instance.itemGameObjs = _itemGameObjs.ToList();
  673. if(MatchingCompetitionDataManager.Instance.BorderID != 0)
  674. {
  675. GameObject borderGameObj = PhotographSceneManager.Instance.sceneObject.transform.Find("Border").gameObject;
  676. MatchingCompetitionDataManager.Instance.itemGameObjs.Add(borderGameObj);
  677. }
  678. if(MatchingCompetitionDataManager.Instance.NpcID != 0)
  679. {
  680. GameObject NpcGameObj = PhotographSceneManager.Instance.sceneObject.transform.Find("Scene/Npc").gameObject;
  681. MatchingCompetitionDataManager.Instance.itemGameObjs.Add(NpcGameObj);
  682. }
  683. MatchingCompetitionDataManager.Instance.MathingDressDate = MyDressUpHelper.dressUpObj.DressUpDataClone();
  684. MatchingCompetitionDataManager.Instance.SetTransformData();
  685. AlertUI.Show("作品已保存,是否确认上传此作品?")
  686. .SetLeftButton(true, "否").SetRightButton(true, "是", async (object data) =>
  687. {
  688. ViewManager.Show<ModalStatusView>("上传中...");
  689. //await MatchingCompetitionSproxy.ReqMatchingCompetitionInfo();
  690. object[] rsp = await MatchingCompetitionSproxy.UpLoadMatchingCompetitionDressData();
  691. if (rsp == null) return;
  692. byte[] bytes = tex.EncodeToJPG();
  693. MatchingCompetitionDataManager.Instance.MyNtextture = new NTexture(tex);
  694. if (rsp[0] == null)
  695. {
  696. ViewManager.Hide<ModalStatusView>();
  697. PromptController.Instance.ShowFloatTextPrompt("网络异常,上传失败!");
  698. return;
  699. }
  700. bool pushResult = await PictureStorageHelper.PushToHWCloud(rsp[0].ToString(), bytes);
  701. if (!pushResult)
  702. {
  703. ViewManager.Hide<ModalStatusView>();
  704. PromptController.Instance.ShowFloatTextPrompt("网络异常,上传失败!");
  705. return;
  706. }
  707. bool result = await MatchingCompetitionSproxy.UploadSuccess();
  708. if (result)
  709. {
  710. ViewManager.Hide<ModalStatusView>();
  711. PromptController.Instance.ShowFloatTextPrompt("上传成功!");
  712. OnClickBtnBack();
  713. ViewManager.GoBackFrom(typeof(DressUpView).FullName);
  714. }
  715. ViewManager.Hide<ModalStatusView>();
  716. });
  717. }
  718. }
  719. private void OnClickBtnBack()
  720. {
  721. this.Hide();
  722. //ViewManager.Show<DressUpView>();
  723. EventAgent.DispatchEvent(ConstMessage.CLOSE_PHOTOGRAPHVIEW);
  724. }
  725. protected override void OnHide()
  726. {
  727. base.OnHide();
  728. if (_sceneObject != null)
  729. {
  730. PrefabManager.Instance.Restore(_sceneObject);
  731. _sceneObject = null;
  732. }
  733. MatchingCompetitionDataManager.Instance.DressPropIndex = 0;
  734. _equipSceneData.Clear();
  735. hitGameObj = null;
  736. memoryHitGameObj = null;
  737. pinchGesture.onAction.Remove(OnPinch);
  738. rotationGesture.onAction.Remove(OnRotate);
  739. pinchGesture = null;
  740. rotationGesture = null;
  741. _ui.m_c1.selectedIndex = 0;
  742. Timers.inst.Remove(CheckGuide);
  743. //Timers.inst.Remove(UpdateSceneTransform);
  744. }
  745. private void CheckGuide(object param)
  746. {
  747. if (GuideDataManager.IsGuideFinish(ConstGuideId.FREEDOM_DRESS) <= 0 ||
  748. GuideDataManager.IsGuideFinish(ConstGuideId.ENTER_CHAPTER) <= 0)
  749. {
  750. UpdateToCheckGuide(null);
  751. }
  752. else
  753. {
  754. Timers.inst.Remove(CheckGuide);
  755. }
  756. }
  757. protected override void UpdateToCheckGuide(object param)
  758. {
  759. if (!ViewManager.CheckIsTopView(this.viewCom)) return;
  760. GuideController.TryGuide(null, ConstGuideId.FREEDOM_DRESS, 7, "在这里可以选择已获得的背景、道具等,也可以放大缩小物品,增加画面丰富度。", -1, true, _ui.target.height - _ui.m_ComSelectRes.target.height - 250);
  761. GuideController.TryGuide(_ui.m_btnPhotograph, ConstGuideId.FREEDOM_DRESS, 8, "来记录下这一刻吧。");
  762. // GuideController.TryCompleteGuide(ConstGuideId.FREEDOM_DRESS, 8);
  763. GuideController.TryGuide(_ui.m_btnBack, ConstGuideId.ENTER_CHAPTER, 1, "");
  764. }
  765. protected override void TryCompleteGuide()
  766. {
  767. base.TryCompleteGuide();
  768. // GuideController.TryCompleteGuide(ConstGuideId.FREEDOM_DRESS, 8);
  769. }
  770. }
  771. }