PalettesBrowser.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. using PixiEditor.Models.DataHolders;
  2. using PixiEditor.Models.DataHolders.Palettes;
  3. using PixiEditor.Models.DataProviders;
  4. using PixiEditor.Models.Enums;
  5. using PixiEditor.Models.Events;
  6. using System;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Input;
  12. namespace PixiEditor.Views.Dialogs
  13. {
  14. public delegate void ListFetched(PaletteList list);
  15. /// <summary>
  16. /// Interaction logic for LospecPalettesBrowser.xaml
  17. /// </summary>
  18. public partial class PalettesBrowser : Window
  19. {
  20. public event ListFetched OnListFetched;
  21. public const int ItemsPerLoad = 10;
  22. public PaletteList PaletteList
  23. {
  24. get { return (PaletteList)GetValue(PaletteListProperty); }
  25. set { SetValue(PaletteListProperty, value); }
  26. }
  27. // Using a DependencyProperty as the backing store for PaletteList. This enables animation, styling, binding, etc...
  28. public static readonly DependencyProperty PaletteListProperty =
  29. DependencyProperty.Register("PaletteList", typeof(PaletteList), typeof(PalettesBrowser));
  30. public ICommand ImportPaletteCommand
  31. {
  32. get { return (ICommand)GetValue(ImportPaletteCommandProperty); }
  33. set { SetValue(ImportPaletteCommandProperty, value); }
  34. }
  35. // Using a DependencyProperty as the backing store for ImportPaletteCommand. This enables animation, styling, binding, etc...
  36. public static readonly DependencyProperty ImportPaletteCommandProperty =
  37. DependencyProperty.Register("ImportPaletteCommand", typeof(ICommand), typeof(PalettesBrowser));
  38. public bool IsFetching
  39. {
  40. get { return (bool)GetValue(IsFetchingProperty); }
  41. set { SetValue(IsFetchingProperty, value); }
  42. }
  43. // Using a DependencyProperty as the backing store for IsFetching. This enables animation, styling, binding, etc...
  44. public static readonly DependencyProperty IsFetchingProperty =
  45. DependencyProperty.Register("IsFetching", typeof(bool), typeof(PalettesBrowser), new PropertyMetadata(false));
  46. public int ColorsNumber
  47. {
  48. get { return (int)GetValue(ColorsNumberProperty); }
  49. set { SetValue(ColorsNumberProperty, value); }
  50. }
  51. // Using a DependencyProperty as the backing store for ColorsNumber. This enables animation, styling, binding, etc...
  52. public static readonly DependencyProperty ColorsNumberProperty =
  53. DependencyProperty.Register("ColorsNumber", typeof(int), typeof(PalettesBrowser),
  54. new PropertyMetadata(8, ColorsNumberChanged));
  55. public WpfObservableRangeCollection<PaletteListDataSource> PaletteListDataSources
  56. {
  57. get { return (WpfObservableRangeCollection<PaletteListDataSource>)GetValue(PaletteListDataSourcesProperty); }
  58. set { SetValue(PaletteListDataSourcesProperty, value); }
  59. }
  60. // Using a DependencyProperty as the backing store for PaletteListDataSources. This enables animation, styling, binding, etc...
  61. public static readonly DependencyProperty PaletteListDataSourcesProperty =
  62. DependencyProperty.Register("PaletteListDataSources", typeof(WpfObservableRangeCollection<PaletteListDataSource>), typeof(PalettesBrowser), new PropertyMetadata(new WpfObservableRangeCollection<PaletteListDataSource>()));
  63. public bool SortAscending
  64. {
  65. get { return (bool)GetValue(SortAscendingProperty); }
  66. set { SetValue(SortAscendingProperty, value); }
  67. }
  68. // Using a DependencyProperty as the backing store for SortDescending. This enables animation, styling, binding, etc...
  69. public static readonly DependencyProperty SortAscendingProperty =
  70. DependencyProperty.Register("SortAscending", typeof(bool), typeof(PalettesBrowser), new PropertyMetadata(true, OnSortAscendingChanged));
  71. public static readonly DependencyProperty SortedResultsProperty = DependencyProperty.Register(
  72. "SortedResults", typeof(WpfObservableRangeCollection<Palette>), typeof(PalettesBrowser), new PropertyMetadata(default(WpfObservableRangeCollection<Palette>)));
  73. public WpfObservableRangeCollection<Palette> SortedResults
  74. {
  75. get { return (WpfObservableRangeCollection<Palette>)GetValue(SortedResultsProperty); }
  76. set { SetValue(SortedResultsProperty, value); }
  77. }
  78. public static readonly DependencyProperty NameFilterProperty = DependencyProperty.Register(
  79. "NameFilter", typeof(string), typeof(PalettesBrowser), new PropertyMetadata(default(string)));
  80. public string NameFilter
  81. {
  82. get { return (string)GetValue(NameFilterProperty); }
  83. set { SetValue(NameFilterProperty, value); }
  84. }
  85. public string SortingType { get; set; } = "Default";
  86. public ColorsNumberMode ColorsNumberMode { get; set; } = ColorsNumberMode.Any;
  87. public string[] Tags { get; set; } = Array.Empty<string>();
  88. private FilteringSettings _filteringSettings;
  89. public FilteringSettings Filtering => _filteringSettings ??= new FilteringSettings(ColorsNumberMode, ColorsNumber, Tags, NameFilter);
  90. private char[] _separators = new char[] { ' ', ',' };
  91. private SortingType _sortingType => (SortingType)Enum.Parse(typeof(SortingType), SortingType.Replace(" ", ""));
  92. public PalettesBrowser()
  93. {
  94. InitializeComponent();
  95. }
  96. private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
  97. {
  98. e.CanExecute = true;
  99. }
  100. private void CommandBinding_Executed_Close(object sender, ExecutedRoutedEventArgs e)
  101. {
  102. SystemCommands.CloseWindow(this);
  103. }
  104. private static async void ColorsNumberChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  105. {
  106. PalettesBrowser browser = (PalettesBrowser)d;
  107. browser.Filtering.ColorsCount = (int)e.NewValue;
  108. await browser.UpdatePaletteList();
  109. }
  110. private static void OnSortAscendingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  111. {
  112. PalettesBrowser browser = (PalettesBrowser)d;
  113. browser.Sort();
  114. }
  115. public async Task UpdatePaletteList()
  116. {
  117. IsFetching = true;
  118. PaletteList?.Palettes?.Clear();
  119. for (int i = 0; i < PaletteListDataSources.Count; i++)
  120. {
  121. PaletteList src = await FetchPaletteList(i, Filtering);
  122. if (!src.FetchedCorrectly) continue;
  123. if (PaletteList == null)
  124. {
  125. PaletteList = src;
  126. }
  127. else
  128. {
  129. PaletteList.Palettes?.AddRange(src.Palettes);
  130. }
  131. }
  132. Sort();
  133. OnListFetched?.Invoke(PaletteList);
  134. IsFetching = false;
  135. }
  136. private async Task<PaletteList> FetchPaletteList(int index, FilteringSettings filtering)
  137. {
  138. int startIndex = PaletteList != null ? PaletteList.Palettes.Count : 0;
  139. var src = await PaletteListDataSources[index].FetchPaletteList(startIndex, ItemsPerLoad, filtering);
  140. return src;
  141. }
  142. private async void ScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
  143. {
  144. if (PaletteList?.Palettes == null) return;
  145. var scrollViewer = (ScrollViewer)sender;
  146. if (scrollViewer.VerticalOffset == scrollViewer.ScrollableHeight)
  147. {
  148. IsFetching = true;
  149. var newPalettes = await FetchPaletteList(0, Filtering);
  150. if (newPalettes is not { FetchedCorrectly: true } || newPalettes.Palettes == null)
  151. {
  152. IsFetching = false;
  153. return;
  154. }
  155. PaletteList.Palettes.AddRange(newPalettes.Palettes);
  156. Sort();
  157. OnListFetched?.Invoke(PaletteList);
  158. IsFetching = false;
  159. }
  160. }
  161. private void SortingComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  162. {
  163. if (e.AddedItems is { Count: > 0 } && e.AddedItems[0] is ComboBoxItem { Content: string value })
  164. {
  165. SortingType = value;
  166. Sort();
  167. scrollViewer.ScrollToHome();
  168. }
  169. }
  170. private async void TagsInput_OnSubmit(object sender, InputBoxEventArgs e)
  171. {
  172. Tags = e.Input.Split(_separators, options: StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
  173. Filtering.Tags = Tags;
  174. await UpdatePaletteList();
  175. scrollViewer.ScrollToHome();
  176. }
  177. private async void NameInput_OnSubmit(object sender, InputBoxEventArgs e)
  178. {
  179. NameFilter = e.Input;
  180. Filtering.Name = NameFilter;
  181. await UpdatePaletteList();
  182. scrollViewer.ScrollToHome();
  183. }
  184. private async void ColorsComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  185. {
  186. if (e.AddedItems is { Count: > 0 } && e.AddedItems[0] is ComboBoxItem item && item.Content is string value)
  187. {
  188. ColorsNumberMode = Enum.Parse<ColorsNumberMode>(value);
  189. Filtering.ColorsNumberMode = ColorsNumberMode;
  190. await UpdatePaletteList();
  191. scrollViewer.ScrollToHome();
  192. }
  193. }
  194. private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
  195. {
  196. ((Grid)sender).Focus();
  197. }
  198. private void Sort()
  199. {
  200. Sort(!SortAscending);
  201. }
  202. private void Sort(bool descending)
  203. {
  204. if(PaletteList?.Palettes == null) return;
  205. IOrderedEnumerable<Palette> sorted = null;
  206. if (!descending)
  207. {
  208. switch (_sortingType)
  209. {
  210. case Models.DataHolders.Palettes.SortingType.Default:
  211. sorted = PaletteList.Palettes.OrderBy(x => PaletteList.Palettes.IndexOf(x));
  212. break;
  213. case Models.DataHolders.Palettes.SortingType.Alphabetical:
  214. sorted = PaletteList.Palettes.OrderBy(x => x.Title);
  215. break;
  216. case Models.DataHolders.Palettes.SortingType.ColorCount:
  217. sorted = PaletteList.Palettes.OrderBy(x => x.Colors.Count);
  218. break;
  219. }
  220. }
  221. else
  222. {
  223. switch (_sortingType)
  224. {
  225. case Models.DataHolders.Palettes.SortingType.Default:
  226. sorted = PaletteList.Palettes.OrderByDescending(x => PaletteList.Palettes.IndexOf(x));
  227. break;
  228. case Models.DataHolders.Palettes.SortingType.Alphabetical:
  229. sorted = PaletteList.Palettes.OrderByDescending(x => x.Title);
  230. break;
  231. case Models.DataHolders.Palettes.SortingType.ColorCount:
  232. sorted = PaletteList.Palettes.OrderByDescending(x => x.Colors.Count);
  233. break;
  234. }
  235. }
  236. if(sorted != null)
  237. {
  238. SortedResults = new WpfObservableRangeCollection<Palette>(sorted);
  239. }
  240. }
  241. }
  242. }