CyclicScrollerCommunication.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. using MediaViewerLib.Utilities;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Media;
  10. using System.Windows.Media.Imaging;
  11. namespace HistoryDLL
  12. {
  13. /// <summary>
  14. /// CyclicScroller 事件傳遞與通訊
  15. /// </summary>
  16. public partial class CyclicScroller : UserControl
  17. {
  18. //外掛 Delegate event for communicate with LiveContainer (通知用 ~ 通知確認動作清除 LiveContainer 點擊動作)
  19. public delegate void CyclicScrollerBubbleToLiveContainer(string ProcessCode);
  20. public event CyclicScrollerBubbleToLiveContainer CyclicScrollerBubbleToLiveContainerEvent;
  21. public List<TouchInfo> touchinfoList = new List<TouchInfo>();
  22. //通知各 LiveContainer 函數~manipulation 事件或者為點擊展開事件
  23. void SendInfoToLiveContainer(TouchorManipulation Triggerevent)
  24. {
  25. if (Triggerevent == TouchorManipulation.TouchEvent)
  26. {
  27. if (touchinfoList.Count != 0)
  28. {
  29. for (int index = 0; index < touchinfoList.Count; index++)
  30. {
  31. //for (int count = 0; count < ExtraUnitGridList.Count; count++)
  32. {
  33. int count = touchinfoList[index].TouchIndex - 1;
  34. if (touchinfoList[index].TouchIndex == ExtraUnitGridList[count].Get_Set_MonitorIndex)
  35. {
  36. if (CheckExtendIsLegal(count))
  37. {
  38. //判斷是否可以展開?
  39. if (ExtraUnitGridList[count].Get_Set_BlockControlMode == ExtraUnitGrid.ControlMode.CanExtend ||
  40. ExtraUnitGridList[count].IsUsing == 0)
  41. {
  42. //將點擊到的最上層物件加到MainWindow
  43. AddExtraUnitGridListInToMainWindow(count);
  44. AddExtraUnitGridListInToMainWindow(count - 1);
  45. AddExtraUnitGridListInToMainWindow(count + 1);
  46. //加載DetailViewer
  47. ExtraUnitGridList[count].BorderUp.VerticalAlignment = System.Windows.VerticalAlignment.Bottom;
  48. ExtraUnitGridList[count].BorderUp.Child = AddDetailViewer(touchinfoList[index].TouchIndex, touchinfoList[index].IndexofEvent, touchinfoList[index].IndexofPic);
  49. ExtraUnitGridList[count].ExtendFunction();
  50. //寫入文字
  51. ExtraUnitGridList[count].textblock.Text = ((LiveContainer)uxMyStackPanel.Children[touchinfoList[index].TouchIndex]).uxYearStr.Text;
  52. ExtraUnitGridList[count].curDataIndex = -1;
  53. if (count + 1 < ExtraUnitGridList.Count)
  54. {
  55. ThumbnailContainer thumbnailControl = new ThumbnailContainer();
  56. List<string> folderPaths = new List<string>();
  57. folderPaths = GetThumbnailControl(touchinfoList[index].TouchIndex + 1);
  58. thumbnailControl.ImgPath = MediaUtilties.GetImageFromFolders(folderPaths.ToArray());
  59. ExtraUnitGridList[count + 1].BorderUp.VerticalAlignment = System.Windows.VerticalAlignment.Bottom;
  60. ExtraUnitGridList[count + 1].BorderUp.Child = thumbnailControl;
  61. ExtraUnitGridList[count + 1].textblock.Text = ((LiveContainer)uxMyStackPanel.Children[touchinfoList[index].TouchIndex + 1]).uxYearStr.Text;
  62. ExtraUnitGridList[count + 1].curDataIndex = touchinfoList[index].TouchIndex + 1;
  63. ExtraUnitGridList[count + 1].ShrinkFunction("Right");
  64. }
  65. if (count - 1 >= 0)
  66. {
  67. ThumbnailContainer thumbnailControl = new ThumbnailContainer();
  68. List<string> folderPaths = new List<string>();
  69. folderPaths = GetThumbnailControl(touchinfoList[index].TouchIndex - 1);
  70. thumbnailControl.ImgPath = MediaUtilties.GetImageFromFolders(folderPaths.ToArray());
  71. ExtraUnitGridList[count - 1].BorderUp.VerticalAlignment = System.Windows.VerticalAlignment.Bottom;
  72. ExtraUnitGridList[count - 1].BorderUp.Child = thumbnailControl;
  73. ExtraUnitGridList[count - 1].textblock.Text = ((LiveContainer)uxMyStackPanel.Children[touchinfoList[index].TouchIndex - 1]).uxYearStr.Text;
  74. ExtraUnitGridList[count - 1].curDataIndex = touchinfoList[index].TouchIndex - 1;
  75. ExtraUnitGridList[count - 1].ShrinkFunction("Left");
  76. }
  77. CheckDisplayRange();
  78. }
  79. }
  80. }
  81. }
  82. }
  83. }
  84. }
  85. else if (Triggerevent == TouchorManipulation.ManipulationEvent)
  86. {
  87. this.Dispatcher.BeginInvoke(CyclicScrollerBubbleToLiveContainerEvent, "Manipulation");
  88. }
  89. touchinfoList.Clear();
  90. }
  91. private bool CheckExtendIsLegal(int index)
  92. {
  93. bool result = true;
  94. index += 1;
  95. //判斷右邊
  96. if (index + 2 < ExtraUnitGridList.Count)
  97. {
  98. if (ExtraUnitGridList[index + 2].ShrinkStoryboardRun)
  99. result = false;
  100. }
  101. //判斷左邊
  102. if (index - 2 >= 0)
  103. {
  104. if (ExtraUnitGridList[index - 2].ShrinkStoryboardRun)
  105. result = false;
  106. }
  107. return result;
  108. }
  109. /// <summary>
  110. /// 通知各 LiveContainer 函數 自動輪播或者停止自動輪播事件通知
  111. /// </summary>
  112. /// <param name="ControlMode">當前模式</param>
  113. private void SendControlModeToLiveContainer(HistoryControlMode ControlMode)
  114. {
  115. if (ControlMode == HistoryControlMode.AutoRun)
  116. {
  117. this.Dispatcher.BeginInvoke(CyclicScrollerBubbleToLiveContainerEvent, "AutoRun");
  118. }
  119. else if (ControlMode == HistoryControlMode.Manual)
  120. {
  121. this.Dispatcher.BeginInvoke(CyclicScrollerBubbleToLiveContainerEvent, "Manual");
  122. }
  123. }
  124. private UIElement AddDetailViewer(int LiveContainerIndex, int indexofevent, int indexofpic)
  125. {
  126. //ImageBrush imagebrush = new ImageBrush(new BitmapImage(new Uri(@"pack://application:,,,/" + GlobalFunction.projectname + @";component/Data/Thumb/shadow_big.png", UriKind.RelativeOrAbsolute)));
  127. ImageBrush imagebrush = null;
  128. Grid gd = new Grid();
  129. int xy = 15;
  130. gd.Width = 810 + xy;
  131. gd.Height = 535 + 14;
  132. if (GlobalFunction.isSolutionUsing4K)
  133. {
  134. gd.Width *= 2;
  135. gd.Height *= 2;
  136. }
  137. //imagebrush.Stretch = Stretch.Uniform;
  138. gd.Background = Brushes.Transparent;
  139. gd.VerticalAlignment = System.Windows.VerticalAlignment.Center;
  140. gd.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
  141. gd.Margin = GlobalFunction.isSolutionUsing4K ? new Thickness(0, 50, 0, 0) : new Thickness(0);
  142. DetailViewer viewer = new DetailViewer(((LiveContainer)uxMyStackPanel.Children[LiveContainerIndex]).xmlData, LiveContainerIndex, indexofevent, indexofpic);
  143. viewer.IndexOfControl = LiveContainerIndex;
  144. viewer.Margin = new Thickness(0, 0, GlobalFunction.isSolutionUsing4K ? 8 : 4, GlobalFunction.isSolutionUsing4K ? 9 : 4);
  145. viewer.VerticalAlignment = System.Windows.VerticalAlignment.Center;
  146. viewer.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
  147. viewer.ChangeLanguageBtnTriggerEvent += new DetailViewer.ChangeLanguageBtnTriggerDelegate(viewer_ChangeLanguageBtnTriggerEvent);
  148. viewer.CloseBtnTriggerEvent += Viewer_CloseBtnTriggerEvent;
  149. //viewer.OnCloseEvent += new EventHandler<EventArgs>(viewer_OnCloseEvent);
  150. gd.Children.Add(viewer);
  151. return gd;
  152. }
  153. private void viewer_ChangeLanguageBtnTriggerEvent(int index, int useLangIndex)
  154. {
  155. LiveContainer lc = uxMyStackPanel.Children[index] as LiveContainer;
  156. if (useLangIndex == 0)
  157. {
  158. ExtraUnitGridList[index - 1].textblock.Text = lc.xmlData.yearlan1;
  159. }
  160. else if (useLangIndex == 1)
  161. {
  162. ExtraUnitGridList[index - 1].textblock.Text = lc.xmlData.yearlan2;
  163. }
  164. else if (useLangIndex == 2)
  165. {
  166. ExtraUnitGridList[index - 1].textblock.Text = lc.xmlData.yearlan3;
  167. }
  168. else if (useLangIndex == 3)
  169. {
  170. ExtraUnitGridList[index - 1].textblock.Text = lc.xmlData.yearlan4;
  171. }
  172. }
  173. private void Viewer_CloseBtnTriggerEvent(int indexOfCtrl)
  174. {
  175. SendInfoToMainWindow(indexOfCtrl);
  176. }
  177. private List<string> GetThumbnailControl(int LiveContainerIndex)
  178. {
  179. List<string> resultList = new List<string>();
  180. XmlData data = ((LiveContainer)uxMyStackPanel.Children[LiveContainerIndex]).xmlData;
  181. for (int i = 0; i < data.monthclassList.Count; i++)
  182. {
  183. resultList.Add(GlobalFunction.EventFileImgPath + data.monthclassList[i].imagepath);
  184. }
  185. return resultList;
  186. }
  187. //通知主視窗~可以將點擊開的物件縮回
  188. private void SendInfoToMainWindow(int index)
  189. {
  190. for (int count = 0; count < ExtraUnitGridList.Count; count++)
  191. {
  192. if (index == ExtraUnitGridList[count].Get_Set_MonitorIndex)
  193. {
  194. //判斷是否可以縮小
  195. if (ExtraUnitGridList[count].Get_Set_BlockControlMode == ExtraUnitGrid.ControlMode.Extend)
  196. {
  197. //回到原始大小
  198. ExtraUnitGridList[count].ReturnToOriginalSize();
  199. if (count + 1 < ExtraUnitGridList.Count)
  200. {
  201. ExtraUnitGridList[count + 1].BackSize("Right");
  202. }
  203. if (count - 1 >= 0)
  204. {
  205. ExtraUnitGridList[count - 1].BackSize("Left");
  206. }
  207. }
  208. }
  209. }
  210. }
  211. private void AddExtraUnitGridListInToMainWindow(int index)
  212. {
  213. if (index < 0 || index >= ExtraUnitGridList.Count)
  214. {
  215. return;
  216. }
  217. if (ExtraUnitGridList[index].IsUsing == 0)
  218. {
  219. ExtraUnitGridList[index].IsUsing = 1;
  220. if (!uxMainWindow.Children.Contains(ExtraUnitGridList[index]))
  221. {
  222. uxMainWindow.Children.Add(ExtraUnitGridList[index]);
  223. }
  224. }
  225. else
  226. {
  227. ExtraUnitGridList[index].IsUsing = 2;
  228. }
  229. }
  230. //接收到~來自各LiveContainer 的通知
  231. void ReceiveFromLiveContainerInfo(string ProcessCode, int index, int indexofevent, int indexofpic)
  232. {
  233. if (ProcessCode == "TouchDown")
  234. {
  235. TouchInfo tif = new TouchInfo(index, indexofevent, indexofpic);
  236. touchinfoList.Add(tif);
  237. //檢查 TouchInfo 是否有重複的索引
  238. //檢查先被點擊的兩旁是否有被點擊~如果有被點擊則點擊無效~將該點擊濾掉
  239. touchinfoList = FilterTouchInfo(touchinfoList);
  240. //排列處理順序由小至大
  241. if (touchinfoList.Count > 1)
  242. {
  243. touchinfoList = ArrangeTouchInfo(touchinfoList);
  244. }
  245. }
  246. else if (ProcessCode == "Load Complete")
  247. {
  248. if (RunHomePageAnimation)
  249. {
  250. hp.AddLoadCount();
  251. //OnDataLoadCompeleted?.Invoke(this,null);
  252. }
  253. }
  254. }
  255. //接收來自 ExtraUnitGrid 的通知
  256. void ExtraGrid_ExtraUnitGridBubbleToCyclicScrollerEvent(string ProcessCode, int index)
  257. {
  258. if (ProcessCode == "Change")
  259. {
  260. //通知主視窗~可以將點擊開的物件縮回
  261. ExtraUnitGridList[index].ShrinkStoryboardRun = true;
  262. SendInfoToMainWindow(index);
  263. }
  264. else if (ProcessCode == "Remove")
  265. {
  266. //移除該物件~先判斷該物件的兩邊的IsUsing已經被False
  267. if (ExtraUnitGridList[index].IsUsing == 0)
  268. {
  269. uxMainWindow.Children.Remove(ExtraUnitGridList[index]);
  270. }
  271. if (index + 1 > 0 && index + 1 < ExtraUnitGridList.Count)
  272. {
  273. ExtraUnitGridList[index + 1].ShrinkStoryboardRun = false;
  274. }
  275. CheckDisplayRange();
  276. }
  277. }
  278. /// <summary>
  279. /// 1.檢查 TouchInfo 是否有重複的索引
  280. /// 2.檢查先被點擊的兩旁是否有被點擊~如果有被點擊則點擊無效~將該點擊濾掉
  281. /// </summary>
  282. /// <param name="tif">來源</param>
  283. private List<TouchInfo> FilterTouchInfo(List<TouchInfo> tifList)
  284. {
  285. List<TouchInfo> resultList = new List<TouchInfo>();
  286. int totalcount = tifList.Count;
  287. for (int count = 0; count < totalcount; count++)
  288. {
  289. int currentindex = tifList[count].TouchIndex;
  290. if (!IsExistCount(currentindex, resultList))
  291. {
  292. //不存在則新增
  293. resultList.Add(tifList[count]);
  294. }
  295. }
  296. return resultList;
  297. }
  298. /// <summary>
  299. /// 排列處理順序由小至大
  300. /// </summary>
  301. /// <param name="touchinfoList">來源</param>
  302. /// <returns></returns>
  303. private List<TouchInfo> ArrangeTouchInfo(List<TouchInfo> tifList)
  304. {
  305. List<TouchInfo> resultList = new List<TouchInfo>();
  306. List<int> Arrange = new List<int>();
  307. for (int count = 0; count < tifList.Count; count++)
  308. {
  309. Arrange.Add(tifList[count].TouchIndex);
  310. }
  311. Arrange.Sort();
  312. for (int i = 0; i < Arrange.Count; i++)
  313. {
  314. for (int j = 0; j < tifList.Count; j++)
  315. {
  316. if (Arrange[i] == tifList[j].TouchIndex)
  317. {
  318. resultList.Add(tifList[j]);
  319. }
  320. }
  321. }
  322. return resultList;
  323. }
  324. private bool IsExistCount(int currentindex, List<TouchInfo> resultList)
  325. {
  326. bool resultbool = false;
  327. int resultListCount = resultList.Count;
  328. for (int count = 0; count < resultListCount; count++)
  329. {
  330. if (currentindex == resultList[count].TouchIndex)
  331. {
  332. return true;
  333. }
  334. //兩旁也要濾掉
  335. if ((currentindex - 1) == resultList[count].TouchIndex)
  336. {
  337. return true;
  338. }
  339. if ((currentindex + 1) == resultList[count].TouchIndex)
  340. {
  341. return true;
  342. }
  343. }
  344. return resultbool;
  345. }
  346. }
  347. }