PhotographView.cs 40 KB

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