GameCreateFriendViewModel.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using System.Collections.Generic;
  2. using System.Collections.ObjectModel;
  3. using System.Media;
  4. using System.Threading.Tasks;
  5. using Avalonia;
  6. using Avalonia.Controls;
  7. using Avalonia.Controls.Shapes;
  8. using Avalonia.Layout;
  9. using Avalonia.Media;
  10. using HelloItQuantum.Function;
  11. using HelloItQuantum.Models;
  12. using HelloItQuantum.Views;
  13. namespace HelloItQuantum.ViewModels
  14. {
  15. public class GameCreateFriendViewModel : MainWindowViewModel
  16. {
  17. Dictionary<int, Color> keyValueColor = new Dictionary<int, Color>
  18. {
  19. { 0, Color.Parse("#0036A0") },
  20. { 1, Color.Parse("#F26527") },
  21. { 2, Color.Parse("#B21E22") },
  22. { 3, Color.Parse("#006838") }
  23. };
  24. public void GoBack() => PageSwitch.View = new PlaySectionView();
  25. #region Object and Property for Xaml
  26. ObservableCollection<FriendElement> listElements = new ObservableCollection<FriendElement>();
  27. public ObservableCollection<FriendElement> ListElements { get => listElements; set => SetProperty(ref listElements, value); }
  28. Panel pChildrens = new Panel();
  29. public Panel PChildrens { get => pChildrens; set => SetProperty(ref pChildrens, value); }
  30. bool isVisibleHello = false;
  31. public bool IsVisibleHello { get => isVisibleHello; set => SetProperty(ref isVisibleHello, value); }
  32. bool isVisibleClue = true;
  33. public bool IsVisibleClue { get => isVisibleClue; set => SetProperty(ref isVisibleClue, value); }
  34. string btnContent = "ÑÎÇÄÀÒÜ";
  35. public string BtnContent { get => btnContent; set => SetProperty(ref btnContent, value); }
  36. SolidColorBrush btnColor = new SolidColorBrush(Color.Parse("#7CBE41"));
  37. public SolidColorBrush BtnColor { get => btnColor; set => SetProperty(ref btnColor, value); }
  38. #endregion
  39. public void ClickCreateElement()
  40. {
  41. IsVisibleClue = false;
  42. FriendElement friendElement = new FriendElement();
  43. ObservableCollection<Ellipse> lColors = new ObservableCollection<Ellipse>();
  44. ObservableCollection<ComboBoxItem> lElements = new ObservableCollection<ComboBoxItem>();
  45. ObservableCollection<ComboBoxItem> lNavigateOne = new ObservableCollection<ComboBoxItem>();
  46. ObservableCollection<ComboBoxItem> lNavigateTwo = new ObservableCollection<ComboBoxItem>();
  47. for (int i = 0; i < 7; i++)
  48. {
  49. lElements.Add(Converts.GenericToItem(CreateElFriend.CreateElement(i, Color.Parse("#293558"))));
  50. }
  51. friendElement.CbElement = lElements;
  52. foreach (var item in keyValueColor)
  53. {
  54. lColors.Add(CreateElFriend.CreateEllipse(50, item.Value));
  55. }
  56. friendElement.CbColor = lColors;
  57. lNavigateOne.Add(Converts.GenericToItem(CreateElFriend.CreateSvgImage("/Assets/ImgCreateFriend/no_shift.svg", 30, null)));
  58. lNavigateOne.Add(Converts.GenericToItem(CreateElFriend.CreateSvgImage("/Assets/ImgCreateFriend/left.svg", 30, null)));
  59. lNavigateOne.Add(Converts.GenericToItem(CreateElFriend.CreateSvgImage("/Assets/ImgCreateFriend/right.svg", 30, null)));
  60. friendElement.CbNavigateOne = lNavigateOne;
  61. lNavigateTwo.Add(Converts.GenericToItem(CreateElFriend.CreateSvgImage("/Assets/ImgCreateFriend/no_shift.svg", 30, null)));
  62. lNavigateTwo.Add(Converts.GenericToItem(CreateElFriend.CreateSvgImage("/Assets/ImgCreateFriend/top.svg", 30, null)));
  63. lNavigateTwo.Add(Converts.GenericToItem(CreateElFriend.CreateSvgImage("/Assets/ImgCreateFriend/bottom.svg", 30, null)));
  64. friendElement.CbNavigateTwo = lNavigateTwo;
  65. friendElement.Id = ListElements.Count;
  66. ListElements.Add(friendElement);
  67. UpdateDrawing(ListElements.Count - 1);
  68. }
  69. public void ClickCreateFriend()
  70. {
  71. if (btnContent == "ÑÎÇÄÀÒÜ" && PChildrens.Children.Count > 1)
  72. {
  73. WorkWithFile.UpdateValueGameProgress(3, 100, CurrentUser);
  74. IsVisibleHello = true;
  75. BtnContent = "ÇÀÍÎÂÎ";
  76. BtnColor = new SolidColorBrush(Color.Parse("#F26527"));
  77. PlayTwoAudio();
  78. }
  79. else if (btnContent == "ÇÀÍÎÂÎ")
  80. {
  81. IsVisibleClue = true;
  82. IsVisibleHello = false;
  83. BtnContent = "ÑÎÇÄÀÒÜ";
  84. BtnColor = new SolidColorBrush(Color.Parse("#7CBE41"));
  85. PChildrens.Children.Clear();
  86. ListElements.Clear();
  87. }
  88. }
  89. public void PlayTask()
  90. {
  91. PlayVoice($"CreateFrendAudio/voice1.wav");
  92. }
  93. public async Task PlayTwoAudio()
  94. {
  95. await PlayVoice($"CreateFrendAudio/voice2.wav");
  96. await PlayVoice($"CreateFrendAudio/voice3.wav");
  97. }
  98. public async Task PlayVoice(string path)
  99. {
  100. using (SoundPlayer snd = new SoundPlayer(path))
  101. {
  102. snd.Play();
  103. await Task.Delay(2000);
  104. }
  105. }
  106. public void Delete(int id)
  107. {
  108. pChildrens.Children.RemoveAt(id);
  109. ListElements.RemoveAt(id);
  110. for (int i = 0; i < ListElements.Count; i++)
  111. {
  112. ListElements[i].Id = i;
  113. }
  114. }
  115. public void UpdateDrawing(int id)
  116. {
  117. Color color = keyValueColor[ListElements[id].SelectedColorIndex];
  118. int indexEl = ListElements[id].SelectedElementIndex;
  119. Control element = CreateElFriend.CreateElement(indexEl, color);
  120. if (id < pChildrens.Children.Count)
  121. pChildrens.Children[id] = element; //Èçìåíÿåì ýëåìåíò
  122. else
  123. pChildrens.Children.Add(element); //Äîáàâëÿåì íîâûé
  124. UpdateNavigate(id);
  125. }
  126. public void UpdateNavigate(int id)
  127. {
  128. int element = ListElements[id].SelectedElementIndex;
  129. pChildrens.Children[id].Margin = new Thickness(0, 0, 0, 0);
  130. if (element >= 3 && element <= 6)
  131. {
  132. int margin = (element == 3 || element == 4) ? 27 : 24;
  133. switch (ListElements[id].SelectedNavigateOne)
  134. {
  135. case 1:
  136. pChildrens.Children[id].HorizontalAlignment = HorizontalAlignment.Left;
  137. pChildrens.Children[id].Margin = new Thickness(margin, 0, 0, 0);
  138. break;
  139. case 2:
  140. pChildrens.Children[id].HorizontalAlignment = HorizontalAlignment.Right;
  141. pChildrens.Children[id].Margin = new Thickness(0, 0, margin, 0);
  142. break;
  143. default: pChildrens.Children[id].HorizontalAlignment = HorizontalAlignment.Center; break;
  144. }
  145. if (element == 5 || element == 6)
  146. {
  147. switch (ListElements[id].SelectedNavigateTwo)
  148. {
  149. case 1:
  150. pChildrens.Children[id].VerticalAlignment = VerticalAlignment.Top;
  151. pChildrens.Children[id].Margin = new Thickness(pChildrens.Children[id].Margin.Left, 40, pChildrens.Children[id].Margin.Right, 0);
  152. break;
  153. case 2:
  154. pChildrens.Children[id].VerticalAlignment = VerticalAlignment.Bottom;
  155. pChildrens.Children[id].Margin = new Thickness(pChildrens.Children[id].Margin.Left, 0, pChildrens.Children[id].Margin.Right, 40);
  156. break;
  157. default: pChildrens.Children[id].VerticalAlignment = VerticalAlignment.Center; break;
  158. }
  159. }
  160. else
  161. {
  162. pChildrens.Children[id].VerticalAlignment = VerticalAlignment.Bottom;
  163. }
  164. }
  165. else
  166. {
  167. pChildrens.Children[id].HorizontalAlignment = HorizontalAlignment.Center;
  168. pChildrens.Children[id].VerticalAlignment = VerticalAlignment.Center;
  169. }
  170. }
  171. /// <summary>
  172. /// Âèäèìîñòü äîïîëíèòåëüíîãî ïàðàìåòðà äëÿ ñäâèãà ôèãóðû
  173. /// </summary>
  174. /// <param name="id"></param>
  175. public void UpdateVisible(int id)
  176. {
  177. if (ListElements[id].SelectedElementIndex == 5 || ListElements[id].SelectedElementIndex == 6)
  178. {
  179. ListElements[id].IsVisibleNavigateOne = true;
  180. ListElements[id].IsVisibleNavigateTwo = true;
  181. }
  182. else if (ListElements[id].SelectedElementIndex == 3 || ListElements[id].SelectedElementIndex == 4)
  183. {
  184. ListElements[id].IsVisibleNavigateOne = true;
  185. ListElements[id].IsVisibleNavigateTwo = false;
  186. }
  187. else
  188. {
  189. ListElements[id].IsVisibleNavigateOne = false;
  190. ListElements[id].IsVisibleNavigateTwo = false;
  191. }
  192. }
  193. }
  194. }