LiveContainer.xaml.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. namespace HistoryDLL
  16. {
  17. /// <summary>
  18. /// LiveContainer.xaml 的互動邏輯
  19. /// </summary>
  20. public partial class LiveContainer : UserControl
  21. {
  22. //uxUpSide 被點擊紀錄為第幾個事件
  23. private int TriggerEvent = 0;
  24. private int TriggerPicture = 0;
  25. public enum ShowMethod
  26. {
  27. ShowForTwo = 0x01,
  28. ShowForSix = 0x10,
  29. };
  30. //創造 LiveContainer 物件
  31. private delegate void CreateLiveContainerDelegate();
  32. //事件個數
  33. public XmlData xmlData = new XmlData();
  34. private int eventCount = 2;
  35. private ShowMethod showMethod = ShowMethod.ShowForTwo;
  36. //PlaneRotate 暫存區
  37. private List<PlaneRotateMap> PlaneRotateMapList = new List<PlaneRotateMap>();
  38. private Grid MainUpSideGrid = new Grid();
  39. //當前 LiveConainer 在 CyclisScroller中的索引位置
  40. private int ContainerIndex = -1;
  41. //Gird 經過切割後每個小區塊的寬高設定
  42. //兩塊
  43. private int BitmapBWidth = 290;
  44. private int BitmapBHeight = 225;
  45. //六塊
  46. private int BitmapSWidth = 210;
  47. private int BitmapSHeight = 158;
  48. // 區塊切割倍數
  49. private int cutMultiple = 1;
  50. //亂數產生
  51. private Random rnd = new Random(Guid.NewGuid().GetHashCode());
  52. //顯示該區塊是否可點擊的顏色設定
  53. private SolidColorBrush CanControlColor = new SolidColorBrush(ConfigSettingClass.TimelineCollect.BackgroundColor);
  54. private SolidColorBrush CantControlColor = new SolidColorBrush(ConfigSettingClass.TimelineExtentCollect.BackgroundColor);
  55. private SolidColorBrush TextBlockControlColor = new SolidColorBrush(ConfigSettingClass.TimelineCollect.TextColor);
  56. //原始物件寬度
  57. private int OriginalObjectWidth = 0;
  58. private bool canControl = true;
  59. public int Set_Get_ContainerIndex
  60. {
  61. set { ContainerIndex = value; }
  62. get { return ContainerIndex; }
  63. }
  64. public LiveContainer(XmlData Data, int objectWidth, int objectHeight)
  65. {
  66. InitializeComponent();
  67. if (GlobalFunction.isSolutionUsing4K)
  68. {
  69. BitmapBWidth *= 2;
  70. BitmapBHeight *= 2;
  71. BitmapSWidth *= 2;
  72. BitmapSHeight *= 2;
  73. uxDownSide.Height *= 2;
  74. cutMultiple *= 2;
  75. uxYearStr.FontSize *= 1.5;
  76. }
  77. xmlData = Data;
  78. uxUpSide.Width = uxLiveContainerMainGrid.Width = OriginalObjectWidth = objectWidth;
  79. uxUpSide.Height = uxLiveContainerMainGrid.Height = objectHeight;
  80. //該年份有幾個事件~?
  81. eventCount = xmlData.monthclassList.Count;
  82. //上方 Border 處理
  83. //判斷該年份事件各數是兩個以下(包括兩個)或者以上
  84. if (eventCount > 2)
  85. {
  86. showMethod = ShowMethod.ShowForSix;
  87. //切割成六塊
  88. CutTheGridToSixBlock();
  89. //縮小每個區塊可容納圖片大小
  90. BitmapBWidth = BitmapSWidth;
  91. BitmapBHeight = BitmapSHeight;
  92. }
  93. else
  94. {
  95. showMethod = ShowMethod.ShowForTwo;
  96. //切割成兩塊
  97. CutTheGridToTwoBlock();
  98. }
  99. uxUpSide.Child = MainUpSideGrid;
  100. //下方 Border 處理
  101. WriteYearBarStr();
  102. uxYearStr.Foreground = TextBlockControlColor;
  103. uxDownSide.PreviewTouchDown += new EventHandler<TouchEventArgs>(uxDownSide_PreviewTouchDown);
  104. }
  105. internal void WriteYearBarStr()
  106. {
  107. string langStr = xmlData.yearlan1;
  108. if (ConfigSettingClass.MainBackCollect.DefaultLangIndex == 1)
  109. {
  110. langStr = xmlData.yearlan2;
  111. }
  112. else if (ConfigSettingClass.MainBackCollect.DefaultLangIndex == 2)
  113. {
  114. langStr = xmlData.yearlan3;
  115. }
  116. else if (ConfigSettingClass.MainBackCollect.DefaultLangIndex == 3)
  117. {
  118. langStr = xmlData.yearlan4;
  119. }
  120. if (ConfigSettingClass.MainBackCollect.DefaultLangIndex < ConfigSettingClass.MainBackCollect.LangFlowDir.Length)
  121. {
  122. uxYearStr.FlowDirection = ConfigSettingClass.MainBackCollect.LangFlowDir[ConfigSettingClass.MainBackCollect.DefaultLangIndex];
  123. }
  124. uxYearStr.Text = langStr;
  125. }
  126. internal void ChangeCurPlaneRotateLan()
  127. {
  128. for (int i = 0; i < MainUpSideGrid.Children.Count; i++)
  129. {
  130. PlaneRotateMap prm = MainUpSideGrid.Children[i] as PlaneRotateMap;
  131. prm.ChangeLanByDynamic();
  132. }
  133. }
  134. internal void ChangePlaneRotateLan()
  135. {
  136. for (int i = 0; i < PlaneRotateMapList.Count; i++)
  137. {
  138. PlaneRotateMap prm = PlaneRotateMapList[i] as PlaneRotateMap;
  139. prm.ChangeLanByDynamic();
  140. }
  141. }
  142. internal void CreateLiveContainer()
  143. {
  144. //將這個年份的所有事件創出來並建立出 PlaneRotate
  145. System.Windows.Application.Current.Dispatcher.Invoke(new CreateLiveContainerDelegate(CreateLiveContainerBkWork));
  146. }
  147. private void CreateLiveContainerBkWork()
  148. {
  149. CreateEventRotate();
  150. //開始將創建出來的PlaneRotate放入
  151. ShowThePlaneRotate();
  152. }
  153. //被通知當前區塊應該要處理的動作
  154. public void ListenFromCyclicTouchStatus(CyclicScroller cs)
  155. {
  156. cs.CyclicScrollerBubbleToLiveContainerEvent += new CyclicScroller.CyclicScrollerBubbleToLiveContainer(ReceiveFromCyclicScroller);
  157. }
  158. #region 當上或者下區塊被點擊~這兩個其中一個Key會被啟動並通知CyclicScroller
  159. //下方年份
  160. void uxDownSide_PreviewTouchDown(object sender, TouchEventArgs e)
  161. {
  162. TriggerEvent = 0;
  163. TriggerPicture = 0;
  164. SendInfoToCyclicScroller("TouchDown");
  165. }
  166. //上方 PlaneRotate 有物件被點擊
  167. void RecevieFromPlaneRotateMapForCheckTouch(int indexofevent, int indexofpicture)
  168. {
  169. TriggerEvent = indexofevent;
  170. TriggerPicture = indexofpicture;
  171. SendInfoToCyclicScroller("TouchDown");
  172. }
  173. #endregion
  174. /// <summary>
  175. /// 將創建出來的PlaneRotate放入
  176. /// (放入的方式由做下層開始由左至右)
  177. /// </summary>
  178. private void ShowThePlaneRotate()
  179. {
  180. //兩個事件處理
  181. if (showMethod == ShowMethod.ShowForTwo)
  182. {
  183. int j = 3;
  184. int rndIndex = 0;
  185. MainUpSideGrid.Children.Clear();
  186. for (int i = 0; i < eventCount; i++, j -= 2)
  187. {
  188. PlaneRotateMap newprm = new PlaneRotateMap();
  189. //亂數取得要放入的 PlaneRotateMap (不重複)
  190. rndIndex = rnd.Next(0, PlaneRotateMapList.Count);
  191. //抽出該PlaneRotateMap 並做設定~完後指到 newprm
  192. newprm = SelectprmToShow(rndIndex, i);
  193. //加到Gird中
  194. MainUpSideGrid.Children.Add(newprm);
  195. //放入指定的位置
  196. Grid.SetRow(newprm, j);
  197. //移除該prm在亂數選擇的Buffer當中 (該 Buffer 只存在沒有顯示的事件)
  198. PlaneRotateMapList.RemoveAt(rndIndex);
  199. }
  200. }
  201. else if (showMethod == ShowMethod.ShowForSix)
  202. {
  203. int j = 5, k = 1;
  204. int rndIndex = 0;
  205. MainUpSideGrid.Children.Clear();
  206. for (int i = 0; i < eventCount; i++)
  207. {
  208. PlaneRotateMap newprm = new PlaneRotateMap();
  209. //亂數取得要放入的 PlaneRotateMap (不重複)
  210. rndIndex = rnd.Next(0, PlaneRotateMapList.Count);
  211. //抽出該PlaneRotateMap 並做設定~完後指到 newprm
  212. newprm = SelectprmToShow(rndIndex, i);
  213. //加到Gird中
  214. MainUpSideGrid.Children.Add(newprm);
  215. //放入指定的位置
  216. Grid.SetRow(PlaneRotateMapList[rndIndex], j);
  217. Grid.SetColumn(PlaneRotateMapList[rndIndex], k);
  218. //移除該prm在亂數選擇的Buffer當中 (該 Buffer 只存在沒有顯示的事件)
  219. PlaneRotateMapList.RemoveAt(rndIndex);
  220. //如果是基數次則
  221. if (k == 3)
  222. {
  223. j -= 2;
  224. k = 1;
  225. }
  226. else
  227. {
  228. k += 2;
  229. }
  230. if (j <= 0)
  231. break;
  232. }
  233. }
  234. }
  235. /// <summary>
  236. /// 抽出該PlaneRotateMap 並做設定~完後指到 newprm
  237. /// </summary>
  238. /// <param name="rndIndex">選擇的PlaneRotateMap的索引值</param>
  239. /// <param name="blockIndex">在該區塊中顯示的位置</param>
  240. /// <returns></returns>
  241. private PlaneRotateMap SelectprmToShow(int rndIndex, int blockIndex)
  242. {
  243. //顯示
  244. PlaneRotateMapList[rndIndex].startShow();
  245. //將該區塊位置索引指給 PlaneRotateMap的BlockIndex
  246. PlaneRotateMapList[rndIndex].BlockIndex = blockIndex;
  247. return PlaneRotateMapList[rndIndex];
  248. }
  249. /// <summary>
  250. ///依照事件數量創造出相應的PlaneRotate
  251. /// </summary>
  252. private void CreateEventRotate()
  253. {
  254. for (int eventcount = 0; eventcount < eventCount; eventcount++)
  255. {
  256. PlaneRotateMap prm = new PlaneRotateMap(xmlData.monthclassList[eventcount], BitmapBWidth, BitmapBHeight);
  257. prm.indexOfEvent = eventcount;
  258. //通知 LiveContainer 通道
  259. prm.ListenFromLiveContainerCode(this);
  260. //接收來自 PlaneRotateMap 通知的通道(確認是否為點擊?)
  261. prm.PlaneRotateMapBubbleToLiveContainerEvent += new PlaneRotateMap.PlaneRotateMapBubbleToLiveContainer(RecevieFromPlaneRotateMapForCheckTouch);
  262. //接收來自 PlaneRotateMap 通知的通道(確認可顯示的資料是否已經到底)
  263. prm.PlaneRotateFileIsBlankEvent += new PlaneRotateMap.PlaneRotateFileIsBlank(ReceiveFromPlaneRotateFileBlankInfo);
  264. prm.CreatePlaneRotateMap();
  265. //之後在換事件的時候可以透過SetRow or SetCol去選擇Grid要顯示哪一個或者哪一個需要改變
  266. PlaneRotateMapList.Add(prm);
  267. }
  268. }
  269. //控制該年份的時間軸顏色
  270. public void ChangeControlColor(bool CanControl)
  271. {
  272. if (CanControl)
  273. {
  274. uxDownSide.Background = CanControlColor;
  275. }
  276. else
  277. {
  278. uxDownSide.Background = CantControlColor;
  279. }
  280. canControl = CanControl;
  281. }
  282. //將 MainUpSideGrid 切成兩塊
  283. private void CutTheGridToTwoBlock()
  284. {
  285. //切成上下兩塊
  286. RowDefinition NoneUseUp = new RowDefinition();
  287. RowDefinition rdBk1 = new RowDefinition();
  288. RowDefinition rdMid = new RowDefinition();
  289. RowDefinition rdBk2 = new RowDefinition();
  290. NoneUseUp.Height = new GridLength(48 * cutMultiple);
  291. rdBk1.Height = new GridLength(225 * cutMultiple);
  292. rdMid.Height = new GridLength(10 * cutMultiple);
  293. rdBk2.Height = new GridLength(225 * cutMultiple);
  294. MainUpSideGrid.RowDefinitions.Add(NoneUseUp);
  295. MainUpSideGrid.RowDefinitions.Add(rdBk1);
  296. MainUpSideGrid.RowDefinitions.Add(rdMid);
  297. MainUpSideGrid.RowDefinitions.Add(rdBk2);
  298. }
  299. //將 MainUpSideGrid 切成六塊
  300. private void CutTheGridToSixBlock()
  301. {
  302. //切成上中下三塊
  303. RowDefinition rdUp = new RowDefinition();
  304. RowDefinition rdBk1 = new RowDefinition();
  305. RowDefinition rdRow1 = new RowDefinition();
  306. RowDefinition rdBk2 = new RowDefinition();
  307. RowDefinition rdRow2 = new RowDefinition();
  308. RowDefinition rdBk3 = new RowDefinition();
  309. rdUp.Height = new GridLength(15 * cutMultiple);
  310. rdBk1.Height = new GridLength(158 * cutMultiple);
  311. rdRow1.Height = new GridLength(10 * cutMultiple);
  312. rdBk2.Height = new GridLength(158 * cutMultiple);
  313. rdRow2.Height = new GridLength(10 * cutMultiple);
  314. rdBk3.Height = new GridLength(158 * cutMultiple);
  315. MainUpSideGrid.RowDefinitions.Add(rdUp);
  316. MainUpSideGrid.RowDefinitions.Add(rdBk1);
  317. MainUpSideGrid.RowDefinitions.Add(rdRow1);
  318. MainUpSideGrid.RowDefinitions.Add(rdBk2);
  319. MainUpSideGrid.RowDefinitions.Add(rdRow2);
  320. MainUpSideGrid.RowDefinitions.Add(rdBk3);
  321. //切成左右兩個區塊
  322. ColumnDefinition colLeft = new ColumnDefinition();
  323. ColumnDefinition colLeftLeft = new ColumnDefinition();
  324. ColumnDefinition colRight = new ColumnDefinition();
  325. ColumnDefinition colRightRight = new ColumnDefinition();
  326. ColumnDefinition colMid = new ColumnDefinition();
  327. colLeftLeft.Width = new GridLength(10 * cutMultiple);
  328. colLeft.Width = new GridLength(210 * cutMultiple);
  329. colMid.Width = new GridLength(10 * cutMultiple);
  330. colRight.Width = new GridLength(210 * cutMultiple);
  331. colRightRight.Width = new GridLength(10 * cutMultiple);
  332. MainUpSideGrid.ColumnDefinitions.Add(colLeftLeft);
  333. MainUpSideGrid.ColumnDefinitions.Add(colLeft);
  334. MainUpSideGrid.ColumnDefinitions.Add(colMid);
  335. MainUpSideGrid.ColumnDefinitions.Add(colRight);
  336. MainUpSideGrid.ColumnDefinitions.Add(colRightRight);
  337. }
  338. }
  339. }