PageRenderTime 57ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/SuperMemory/Order/OrderPage.xaml.cs

https://github.com/antonkulaga/SuperMemory
C# | 226 lines | 147 code | 35 blank | 44 comment | 2 complexity | b9b07342464a0c7abf71988e229e312e MD5 | raw file
  1. using System;
  2. using System.Linq;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Navigation;
  6. using Smart.Classes.Collections;
  7. using Smart.Classes.Extensions;
  8. using Smart.Classes.Subjects;
  9. using Smart.UI.Classes.Animations;
  10. using Smart.UI.Classes.Events;
  11. using Smart.UI.Classes.Extensions;
  12. using Smart.UI.Classes.Extensions.StructExtensions;
  13. using Smart.UI.Classes.Layout;
  14. using Smart.UI.Panels;
  15. using Smart.UI.Widgets;
  16. using Smart.UI.Widgets.Events;
  17. using Smart.UI.Classes.Events;
  18. namespace SuperMemory.Order
  19. {
  20. public partial class OrderPage : Page
  21. {
  22. public OrderViewModel ViewModel;
  23. public OrderPage()
  24. {
  25. InitializeComponent();
  26. Init();
  27. }
  28. // Executes when the user navigates to this page.
  29. protected override void OnNavigatedTo(NavigationEventArgs e)
  30. {
  31. }
  32. protected void Init()
  33. {
  34. if (this.Settings == null) this.Settings = this.DataContext as OrderSettings;
  35. this.field.DataContext = this.Settings;
  36. this.tools.DataContext = this.Settings;
  37. this.ViewModel = new OrderViewModel(this.Settings);
  38. this.InitEvents();
  39. // var r = this.field.TESTRAWS;
  40. // var b = this.field.BINDED;
  41. this.ViewModel.Start();
  42. this.field.RunAterNextLayoutUpdate(i => this.InitFieldResize());
  43. }
  44. #region LINES CHANGES
  45. //TODO : rewrite entirely
  46. /// <summary>
  47. /// NAPILNIK
  48. /// TODO : rewrite enitrely
  49. /// </summary>
  50. private void InitFieldResize()
  51. {
  52. this.field.SetRelativeToGrid(true).SetBottom().SetRight();
  53. this.field.ColumnDefinitions.ItemsAdded.DoOnNext += this.AddColumnHandler;
  54. this.field.RowDefinitions.ItemsAdded.DoOnNext += this.AddRowHandler;
  55. this.field.ColumnDefinitions.ItemsRemoved.DoOnNext += this.RemoveColumnHandler;
  56. this.field.RowDefinitions.ItemsRemoved.DoOnNext += this.RemoveRowHandler;
  57. //TODO :fix ADDBOTTOM AND ADDRIGHT BUGS
  58. }
  59. protected void AddRowHandler(LineDefinition ldf)
  60. {
  61. field.SetBottom(field.GetBottom() - ldf.AbsoluteValue);
  62. }
  63. protected void RemoveRowHandler(LineDefinition ldf)
  64. {
  65. field.SetBottom(field.GetBottom() + ldf.AbsoluteValue);
  66. }
  67. protected void AddColumnHandler(LineDefinition ldf)
  68. {
  69. field.SetRight(field.GetRight() - ldf.AbsoluteValue);
  70. }
  71. protected void RemoveColumnHandler(LineDefinition ldf)
  72. {
  73. field.SetRight(field.GetRight() + ldf.AbsoluteValue);
  74. }
  75. #endregion
  76. /// <summary>
  77. /// Adds events to the controls and handlers to them
  78. /// </summary>
  79. protected void InitEvents()
  80. {
  81. field.AllowDock = AlloDockHandler;
  82. this.AsEventsHolder().RouteEventTo<String>(OrderEvents.GO_2_STATE, ViewModel);
  83. ViewModel.AddEventHandler<MassiveRelativeResizeEvent>(OrderEvents.APPEAR, AppearHandler);
  84. ViewModel.AddEventHandler<string>(OrderEvents.DISAPPEAR, DisappearHandler);
  85. ViewModel.AddEventCallback<AnimationEvent<FrameworkElement>,double>(OrderEvents.SHOW_POPUP,ShowPopupHandler);
  86. ViewModel.AddEventCallback<AnimationEvent<FrameworkElement>,double>(OrderEvents.HIDE_POPUP,HidePopupHandler);
  87. ViewModel.AddEventCallback<MoveToCellsEvent, double>(OrderEvents.MOVE_TO_INVENTORY, MoveToInventory);
  88. }
  89. private Animation ShowPopupHandler(AnimationEvent<FrameworkElement> args)
  90. {
  91. var target = args.Target.SetRow(1).SetColumn(2);//.SetColumnSpan(1)
  92. this.LayoutRoot.AddChild(target);
  93. target.Opacity = 0.1;
  94. return target.AnimateProperty(UIElement.OpacityProperty, 0.8, args.Duration).Go();
  95. /*
  96. this.AppearOne(
  97. new RelativeResizeEvent(target, new Point(1, 1), new TimeSpan(0, 0, 0, 2), Easings.CubicEaseInOut),
  98. this.LayoutRoot);
  99. * */
  100. }
  101. /// <summary>
  102. /// Hides popup
  103. /// </summary>
  104. /// <param name="args"></param>
  105. private Animation HidePopupHandler(AnimationEvent<FrameworkElement> args)
  106. {
  107. var target = args.Target;
  108. var ani = target.AnimateProperty(UIElement.OpacityProperty, 0.1, args.Duration);
  109. ani.DoOnCompleted+=()=>
  110. this.LayoutRoot.RemoveChild(args.Target);
  111. return ani.Go();
  112. }
  113. /// <summary>
  114. /// Let one element to appear with animation
  115. /// </summary>
  116. /// <param name="args"></param>
  117. /// <param name="grid"></param>
  118. protected void AppearOne(RelativeResizeEvent args, WidgetGrid grid)
  119. {
  120. var target = args.Target;
  121. grid.AddChild(target);
  122. field.AfterArrange.At().DoOnNext +=
  123. i =>
  124. {
  125. grid.PlaceInCells(target, target.GetSlot().SetSize(10, 10));
  126. target.ResizeInCellsRelatively(args.RelativeSize, args.Duration, args.Easing).Go();
  127. };
  128. }
  129. /// <summary>
  130. /// Cleanups tools and fields
  131. /// </summary>
  132. /// <param name="param"></param>
  133. private void DisappearHandler(string param)
  134. {
  135. this.field.RunAterNextLayoutUpdate(u =>
  136. {
  137. this.field.RemoveChildren(i => i is Image);
  138. this.tools.RemoveChildren(i => i is Image);
  139. });
  140. }
  141. /// <summary>
  142. /// Checks if dockin is allowed
  143. /// </summary>
  144. /// <param name="fly"></param>
  145. /// <returns></returns>
  146. protected Boolean AlloDockHandler(ObjectFly fly)
  147. {
  148. CellsRegion cells = field.MeasureCellsRegion(fly.Target);
  149. return this.ViewModel.MoveElementTo(new Num2D(cells.Col, cells.Row), fly.Target);
  150. }
  151. /// <summary>
  152. /// Handler that makes resizing effect for elements when they first appear on the field
  153. /// </summary>
  154. /// <param name="args"></param>
  155. private void AppearHandler(MassiveRelativeResizeEvent args)
  156. {
  157. args.Handled = true;
  158. args.Targets.ForEach(i => field.AddChild(i.Item1));
  159. field.AfterArrange.At().DoOnNext +=
  160. i =>
  161. {
  162. args.Targets.ForEach(e => field.PlaceInCells(e.Item1, e.Item1.GetSlot().SetSize(10, 10)));
  163. args.Targets.ResizeInCellsRelatively(args.Duration, args.Easing).Go();
  164. };
  165. }
  166. /// <summary>
  167. /// Handler that makes resizing effect for elements when they first appear on the field
  168. /// </summary>
  169. /// <param name="args"></param>
  170. private Animation MoveToInventory(MoveToCellsEvent args)
  171. {
  172. var objects = args.Targets;
  173. //var ani = this.tools.MoveInCellsMassive(args.Targets, args.Duration, args.Easing);
  174. objects.ForEach(o =>
  175. {
  176. var bounds = o.Item1.GetRelativeRect(tools);
  177. this.field.Children.MoveElementTo(o.Item2.SetPosition(o.Item1), this.tools.Children);
  178. tools.PlaceInCells(o.Item1, bounds, o.Item2);
  179. });
  180. var targets = objects.Select(s => new Tuple<FrameworkElement, Rect>(s.Item1, tools.GetCellsRect(s.Item2)));
  181. var ani = tools.MoveInCellsMassive(targets, new TimeSpan(0, 0, 0, 2), Easings.CubicEaseInOut);
  182. tools.DragEnabled = field.DragEnabled = true;
  183. args.Handled = true;
  184. return ani.Go();
  185. }
  186. private void playBtn_Click(object sender, RoutedEventArgs e)
  187. {
  188. ViewModel.OnButtonClick();
  189. }
  190. }
  191. }