NotePadComponent.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. using BeinLab.Util;
  2. using Newtonsoft.Json;
  3. using PublicTools.Unity;
  4. using SC.XR.Unity;
  5. using ShadowStudio.Tool;
  6. using ShadowStudio.UI;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using UnityEngine;
  10. using UnityEngine.UI;
  11. using XRTool.Util;
  12. using XRTool.WorldUI;
  13. namespace ShadowStudio.Model
  14. {
  15. /// <summary>
  16. /// 记事本
  17. /// 不即时同步,当点击发送的时候再同步
  18. /// 主动创建流程:点击标签,创建便签,此时创建本地便签。点击发送,将此便签的数据发送至服务器,等待便签的同步数据
  19. /// </summary>
  20. public class NotePadComponent : UIComponent
  21. {
  22. private NoteData noteData = new NoteData();
  23. private SCInputField cInputField;
  24. private Button sendButton;
  25. private Transform colorToggleTran;
  26. //private ToggleGroup colorToggleGroup;
  27. private GameObject editState;
  28. private GameObject showState;
  29. private Image notePad;
  30. private Text notePadText;
  31. private bool isHadHide = false;
  32. private Color NotePadColor;
  33. private Image back;
  34. private static Vector2 defSize = new Vector2(200, 130);
  35. public static string notePadId = "ArtId_NodePad";
  36. public virtual void Start()
  37. {
  38. if (SendButton)
  39. {
  40. SendButton.onClick.AddListener(OnClickSendButton);
  41. }
  42. if (ColorToggleTran)
  43. {
  44. Toggle[] toggles = ColorToggleTran.GetComponentsInChildren<Toggle>(true);
  45. bool isInit = false;
  46. for (int i = 0; i < toggles.Length; i++)
  47. {
  48. Toggle toggle = toggles[i];
  49. if (!isInit && toggle)
  50. {
  51. GetNotePadColor(toggle);
  52. isInit = true;
  53. }
  54. toggles[i].onValueChanged.AddListener((bool isOn) => { OnToggle(isOn, toggle); });
  55. }
  56. }
  57. }
  58. /// <summary>
  59. /// 创建本地的资源id
  60. /// </summary>
  61. public static ArtContainer CreateLocalNotePad()
  62. {
  63. ArtInfo info = ArtInfoMgr.Instance.GetArtInfo(notePadId);
  64. ArtHandler handler = ArtInfoMgr.Instance.CreateArtHandler(info);
  65. var container = handler.CreateArt();
  66. WorldDlgContainer dlg = container as WorldDlgContainer;
  67. if (dlg)
  68. {
  69. dlg.Dlg.DlgTrans.sizeDelta = defSize;
  70. dlg.Dlg.SetScale(defSize, 1);
  71. }
  72. container.Position = GameSession.Instance.GetHeadForwadPos(0.35f);
  73. return container;
  74. }
  75. public void Init()
  76. {
  77. EditState.SetActive(true);
  78. ShowState.SetActive(false);
  79. }
  80. /// <summary>
  81. /// 点击了发送的按钮
  82. /// </summary>
  83. private void OnClickSendButton()
  84. {
  85. ///添加缓存
  86. if (CInputField.text != string.Empty)
  87. {
  88. ArtInfoMgr.Instance.AddCacheContainer(notePadId, Container);
  89. SendNotePad();
  90. OnEndEdit();
  91. }
  92. }
  93. public void SendNotePad()
  94. {
  95. Transform worldDlgContainer = this.transform.GetComponentInParent<WorldDlgContainer>().transform;
  96. worldDlgContainer.SetParent(ConsoleDlg.Instance.WorldCanvas, true);
  97. worldDlgContainer.localPosition = new Vector3(0f, 0f, -0.01f);
  98. worldDlgContainer.localEulerAngles = new Vector3(0f, 0f, 0f);
  99. worldDlgContainer.localScale = new Vector3(1f, 1f, 1f);
  100. if (ConsoleDlg.Instance.PiZhuToggle)
  101. {
  102. ConsoleDlg.Instance.PiZhuToggle.isOn = false;
  103. }
  104. }
  105. public SCInputField CInputField
  106. {
  107. get
  108. {
  109. if (!cInputField)
  110. {
  111. cInputField = UnityUtil.GetBreadthChild<SCInputField>(transform, "SCInputField");
  112. }
  113. return cInputField;
  114. }
  115. }
  116. public Transform ColorToggleTran
  117. {
  118. get
  119. {
  120. if (!colorToggleTran)
  121. {
  122. colorToggleTran = UnityUtil.GetBreadthChild<Transform>(transform, "ColorToggleGroup");
  123. }
  124. return colorToggleTran;
  125. }
  126. }
  127. public GameObject EditState
  128. {
  129. get
  130. {
  131. if (!editState)
  132. {
  133. editState = UnityUtil.GetBreadthChild<Transform>(transform, "EditState").gameObject;
  134. }
  135. return editState;
  136. }
  137. }
  138. public GameObject ShowState
  139. {
  140. get
  141. {
  142. if (!showState)
  143. {
  144. showState = UnityUtil.GetBreadthChild<Transform>(transform, "ShowState").gameObject;
  145. }
  146. return showState;
  147. }
  148. }
  149. public Image NotePad
  150. {
  151. get
  152. {
  153. if (!notePad)
  154. {
  155. notePad = UnityUtil.GetBreadthChild<Image>(transform, "NotePad");
  156. }
  157. return notePad;
  158. }
  159. }
  160. public Text NotePadText
  161. {
  162. get
  163. {
  164. if (!notePadText)
  165. {
  166. notePadText = UnityUtil.GetBreadthChild<Text>(transform, "NotePadText");
  167. }
  168. return notePadText;
  169. }
  170. }
  171. /// <summary>
  172. /// 发送按钮
  173. /// </summary>
  174. public Button SendButton
  175. {
  176. get
  177. {
  178. if (!sendButton)
  179. {
  180. sendButton = UnityUtil.GetBreadthChild<Button>(transform, "SendButton");
  181. }
  182. return sendButton;
  183. }
  184. }
  185. public override void SetContainer(ArtContainerHandler container, Object art, ArtInfo info)
  186. {
  187. base.SetContainer(container, art, info);
  188. RectTransform tar = GetComponent<RectTransform>();
  189. tar.offsetMax = Vector2.zero;
  190. tar.offsetMin = Vector2.zero;
  191. if (CInputField)
  192. {
  193. CInputField.onEndEdit.AddListener(OnEndInput);
  194. }
  195. }
  196. private void OnEndInput(string arg0)
  197. {
  198. //OnEndEdit();
  199. }
  200. /// <summary>
  201. /// 完成编辑,删除此标签
  202. /// </summary>
  203. public virtual void OnEndEdit()
  204. {
  205. ArtInfo info = ArtInfoMgr.Instance.GetArtInfo(notePadId);
  206. if (string.IsNullOrEmpty(noteData.noteName))
  207. {
  208. noteData.noteName = UnityUtil.CurTimeString;
  209. }
  210. EditState.SetActive(false);
  211. ShowState.SetActive(true);
  212. NotePad.color = NotePadColor;
  213. NotePadText.text = CInputField.text;
  214. //noteData.fontSize = FontSize;
  215. noteData.notePadColor = new XColor(NotePadColor);
  216. noteData.noteMsg = CInputField.text;
  217. Container.data.goods_extended = JsonConvert.SerializeObject(noteData);
  218. Container.data.art_id = notePadId;
  219. //Transform tmp = transform.parent;
  220. //transform.SetParent(GameNode.Instance.LocalWorldCanvas);
  221. //Vector3 scale = transform.localScale;
  222. //transform.localScale = scale * UnityUtil.maxTransfer;
  223. Container.data.goods_info = UnityUtil.TransferToString(Container.transform, 2);
  224. Container.data.rid = info.AID;
  225. Container.data.goods_name = info.ArtName;
  226. WSHandler.Room.CreateGood(Container.data);
  227. //transform.SetParent(tmp);
  228. //transform.localScale = scale;
  229. }
  230. /// <summary>
  231. /// 同步数据
  232. /// </summary>
  233. /// <param name="goods"></param>
  234. /// <param name="smooth"></param>
  235. public override void TransferSyn(GoodsInfo goods, bool smooth = true)
  236. {
  237. base.TransferSyn(goods, smooth);
  238. if (!isHadHide && !string.IsNullOrEmpty(goods.goods_extended))
  239. {
  240. noteData = JsonConvert.DeserializeObject<NoteData>(goods.goods_extended);
  241. //FontSize = noteData.fontSize;
  242. EditState.SetActive(false);
  243. ShowState.SetActive(true);
  244. NotePad.color = noteData.notePadColor.Trans();
  245. NotePadText.text = noteData.noteMsg;
  246. SetEditActive(false);
  247. if (NotePad)
  248. {
  249. NotePad.SetNativeSize();
  250. Vector2 size = NotePad.rectTransform.sizeDelta + new Vector2(70, 50);
  251. DlgContainer.Dlg.DlgTrans.sizeDelta = size;
  252. DlgContainer.Dlg.SetScale(size, 1);
  253. if (DlgContainer.Transfer && DlgContainer.Transfer.IsInit)
  254. {
  255. DlgContainer.Transfer.RemoveBound();
  256. TimerMgr.Instance.CreateTimer(() => { DlgContainer.Transfer.IsDragEnable = false; }, 0);
  257. }
  258. }
  259. }
  260. }
  261. /// <summary>
  262. /// 禁用编辑功能
  263. /// </summary>
  264. public virtual void SetEditActive(bool isActive = false)
  265. {
  266. if (SendButton)
  267. {
  268. SendButton.gameObject.SetActive(isActive);
  269. }
  270. if (CInputField)
  271. {
  272. CInputField.image.enabled = isActive;
  273. }
  274. isHadHide = true;
  275. }
  276. /// <summary>
  277. /// 设置文字大小
  278. /// </summary>
  279. public int FontSize
  280. {
  281. get
  282. {
  283. return CInputField.textComponent.fontSize;
  284. }
  285. set
  286. {
  287. CInputField.textComponent.fontSize = value;
  288. }
  289. }
  290. public void GetNotePadColor(Toggle toggle)
  291. {
  292. if (toggle)
  293. {
  294. Image img = toggle.transform.Find("Background").GetComponent<Image>();
  295. NotePadColor = img.color;
  296. Back.color = NotePadColor;
  297. }
  298. }
  299. private void OnToggle(bool isOn, Toggle toggle)
  300. {
  301. if (isOn)
  302. {
  303. switch (toggle.name)
  304. {
  305. case "ToggleGray":
  306. GetNotePadColor(toggle);
  307. break;
  308. case "ToggleBlue":
  309. GetNotePadColor(toggle);
  310. break;
  311. case "ToggleYellow":
  312. GetNotePadColor(toggle);
  313. break;
  314. case "ToggleRed":
  315. GetNotePadColor(toggle);
  316. break;
  317. case "ToggleGreen":
  318. GetNotePadColor(toggle);
  319. break;
  320. }
  321. }
  322. }
  323. /// <summary>
  324. /// 设置文字颜色
  325. /// </summary>
  326. public Color FontColor
  327. {
  328. get
  329. {
  330. return CInputField.textComponent.color;
  331. }
  332. set
  333. {
  334. CInputField.textComponent.color = value;
  335. }
  336. }
  337. public Image Back
  338. {
  339. get
  340. {
  341. if (!back)
  342. {
  343. back = UnityUtil.GetBreadthChild<Image>(transform, "Back");
  344. }
  345. return back;
  346. }
  347. }
  348. }
  349. }