Drawing1PointBase.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. using MuchInfo.Chart.Data;
  2. using MuchInfo.Chart.Data.EnumTypes;
  3. using MuchInfo.Chart.Infrastructure.Controls;
  4. using MuchInfo.Chart.Infrastructure.Helpers;
  5. using MuchInfo.Chart.Infrastructure.Utilities;
  6. using MuchInfo.Chart.WPF.Controls.Editors;
  7. using MuchInfo.Chart.WPF.Helpers;
  8. using MuchInfo.Chart.WPF.Primitives.Interfaces;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Windows;
  12. using System.Windows.Controls;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Xml.Linq;
  16. namespace MuchInfo.Chart.WPF.Primitives.Drawing
  17. {
  18. public abstract class Drawing1PointBase : IDrawingTool
  19. {
  20. #region Fields
  21. protected object mAnchorByPercent;
  22. protected Color mColor;
  23. protected Rect mLastRect;
  24. protected ScaleLayer mLastScale;
  25. protected double mLastX1;
  26. protected double mLastY1;
  27. protected bool mSaveByPercent;
  28. protected DateTime P1Date;
  29. /// <summary>
  30. /// TheYVal
  31. /// </summary>
  32. protected float P1Val;
  33. protected double P1XPercent;
  34. protected double P1YPercent;
  35. protected DrawingSelectionArea SelectionArea;
  36. public Chart Chart { get; set; }
  37. private IDrawingToolSpec _drawingToolSpec;
  38. /// <summary>
  39. /// PlotOnAllSymbols
  40. /// </summary>
  41. private bool _plotOnAllSymbols;
  42. private double _lastX1;
  43. private double _lastY1;
  44. #endregion Fields
  45. #region Constructors
  46. protected Drawing1PointBase()
  47. {
  48. this.P1XPercent = 0.0;
  49. this.P1YPercent = 0.0;
  50. this.mSaveByPercent = false;
  51. this.mColor = Colors.Red;
  52. this._plotOnAllSymbols = false;
  53. }
  54. #endregion Constructors
  55. #region Properties
  56. #region Public Properties
  57. public Color Color
  58. {
  59. get
  60. {
  61. return this.mColor;
  62. }
  63. set
  64. {
  65. bool flag = !this.mColor.Equals(value);
  66. if (flag)
  67. {
  68. this.mColor = value;
  69. this.ColorChanged();
  70. //this.PropertyChanged();
  71. }
  72. }
  73. }
  74. public IDrawingToolSpec OwnerSpec
  75. {
  76. get
  77. {
  78. return this._drawingToolSpec;
  79. }
  80. set
  81. {
  82. this._drawingToolSpec = value;
  83. }
  84. }
  85. public bool PlotOnAllSymbols
  86. {
  87. get
  88. {
  89. return this._plotOnAllSymbols;
  90. }
  91. set
  92. {
  93. bool flag = value != this._plotOnAllSymbols;
  94. if (flag)
  95. {
  96. this._plotOnAllSymbols = value;
  97. this.PropertyChanged();
  98. }
  99. }
  100. }
  101. public abstract string RootName
  102. {
  103. get;
  104. }
  105. public bool SaveByPercent
  106. {
  107. get
  108. {
  109. return this.mSaveByPercent;
  110. }
  111. set
  112. {
  113. bool flag = value != this.mSaveByPercent;
  114. if (flag)
  115. {
  116. this.mSaveByPercent = value;
  117. //this.PropertyChanged();
  118. }
  119. }
  120. }
  121. public float YValue
  122. {
  123. get
  124. {
  125. return this.P1Val;
  126. }
  127. set
  128. {
  129. bool flag = this.P1Val != value;
  130. if (flag)
  131. {
  132. this.P1Val = value;
  133. IDateScaler dateScaler = this.Chart.DateScaler;
  134. this.mLastX1 = (double)dateScaler.XforDate(this.P1Date);
  135. this.mLastY1 = (double)this.mLastScale.GetRightValueScaler().ScaledY(this.P1Val, this.mLastRect);
  136. this.P1YPercent = (this.mLastY1 - this.mLastRect.Left) / this.mLastRect.Height;
  137. this.MoveDrawingToNewLocations(this.mLastRect);
  138. this.PropertyChanged();
  139. }
  140. }
  141. }
  142. #endregion Public Properties
  143. #region Internal Properties
  144. internal float TheYVal
  145. {
  146. get
  147. {
  148. return this.P1Val;
  149. }
  150. set
  151. {
  152. this.P1Val = value;
  153. }
  154. }
  155. #endregion Internal Properties
  156. #endregion Properties
  157. #region Indexers
  158. public Point this[int aIndex]
  159. {
  160. get
  161. {
  162. bool flag = aIndex == 0;
  163. Point result = new Point();
  164. if (flag)
  165. {
  166. Point point = new Point(this.mLastX1, this.mLastY1);
  167. result = point;
  168. }
  169. return result;
  170. }
  171. set
  172. {
  173. //加上左边纵坐标位移
  174. var offset = this.Chart.GetChartPanelOffset();
  175. DateTime p1Date = this.Chart.DateScaler.OriginalDateFromX((float)value.X + (float)offset);
  176. float p1Val = this.mLastScale.GetRightValueScaler().ValueFromPercent((float)(1.0 - value.Y / this.mLastRect.Height));
  177. bool flag = aIndex == 0;
  178. if (flag)
  179. {
  180. this.P1Date = p1Date;
  181. this.P1Val = p1Val;
  182. this.mLastX1 = value.X;
  183. this.mLastY1 = value.Y;
  184. this.P1XPercent = (value.X + offset - this.mLastRect.Top) / this.mLastRect.Width;
  185. this.P1YPercent = (value.Y - this.mLastRect.Left) / this.mLastRect.Height;
  186. this.MoveDrawingToNewLocations(this.mLastRect);
  187. }
  188. }
  189. }
  190. #endregion Indexers
  191. #region Methods
  192. #region Public Methods
  193. public abstract string Abbreviation();
  194. public virtual List<ControlPair> AdditionalEditors()
  195. {
  196. List<ControlPair> result = null;
  197. return result;
  198. }
  199. public DateTime aP1Date()
  200. {
  201. return this.P1Date;
  202. }
  203. public float aP1Val()
  204. {
  205. return this.P1Val;
  206. }
  207. public float aP1XPercent()
  208. {
  209. return (float)this.P1XPercent;
  210. }
  211. public float aP1YPercent()
  212. {
  213. return (float)this.P1YPercent;
  214. }
  215. public abstract bool CanPlotOnAllSymbols();
  216. public abstract bool CanSaveByPercent();
  217. public abstract IDrawingTool Clone();
  218. public void Delete()
  219. {
  220. bool flag = this._drawingToolSpec != null;
  221. if (flag)
  222. {
  223. this._drawingToolSpec.DeleteTool();
  224. }
  225. }
  226. public abstract string Description();
  227. public void FromInfo(DrawingToolInfo info)
  228. {
  229. bool flag = info.P1Date.HasValue;
  230. if (flag)
  231. {
  232. this.P1Date = info.P1Date.Value;
  233. }
  234. flag = info.P1Val.HasValue;
  235. if (flag)
  236. {
  237. this.P1Val = info.P1Val.Value;
  238. }
  239. flag = info.Color1.HasValue;
  240. if (flag)
  241. {
  242. this.mColor = ColorHelper.ColorFromInt(info.Color1.Value);
  243. }
  244. flag = this.mSaveByPercent;
  245. //if (flag)
  246. //{
  247. // IL_8B:
  248. flag = (info.P2Val.HasValue && info.P3Val.HasValue);
  249. if (flag)
  250. {
  251. this.mSaveByPercent = true;
  252. this.P1XPercent = (double)info.P2Val.Value;
  253. this.P1YPercent = (double)info.P3Val.Value;
  254. }
  255. else
  256. {
  257. this.mSaveByPercent = false;
  258. }
  259. this._plotOnAllSymbols = info.DrawOnAllSymbols;
  260. this.AddFromInfo(info);
  261. return;
  262. //}
  263. //goto IL_8B;
  264. }
  265. public void FromXml(XElement node)
  266. {
  267. var flag = node.Attribute("D1") != null;
  268. if (flag)
  269. {
  270. this.P1Date = new DateTime(long.Parse(node.Attribute("D1").Value));
  271. }
  272. flag = (node.Attribute("V1") != null);
  273. if (flag)
  274. {
  275. this.P1Val = XMLHelp.ParseSingle(node.Attribute("V1").Value);
  276. }
  277. flag = (node.Attribute("Clr") != null);
  278. if (flag)
  279. {
  280. this.mColor = ColorHelper.ColorFromString(node.Attribute("Clr").Value);
  281. }
  282. flag = (node.Attribute("ALS") != null);
  283. if (flag)
  284. {
  285. this._plotOnAllSymbols = bool.Parse(node.Attribute("ALS").Value);
  286. }
  287. this.AddFromXml(node);
  288. }
  289. public void GetDrawingInfo(DrawingToolInfo info)
  290. {
  291. info.P1Date = this.P1Date;
  292. info.P1Val = this.P1Val;
  293. info.Color1 = ColorHelper.ColorToInt(this.mColor);
  294. info.DrawOnAllSymbols = this._plotOnAllSymbols;
  295. bool flag = this.mSaveByPercent;
  296. if (flag)
  297. {
  298. info.P2Val = (float)this.P1XPercent;
  299. info.P3Val = (float)this.P1YPercent;
  300. }
  301. else
  302. {
  303. info.P2Val = default(float?);
  304. info.P3Val = default(float?);
  305. }
  306. this.AddToInfo(info);
  307. }
  308. public List<FrameworkElement> GetPlots(Chart owner, ScaleLayer aScale, Rect aRect, bool DrawHandles)
  309. {
  310. this.Chart = owner;
  311. this.mLastScale = aScale;
  312. this.mLastRect = aRect;
  313. bool flag = !ColorHelper.ColorIsContrasting(this.mColor, ColorHelper.GetBrushColor(owner.ChartBackground));
  314. if (flag)
  315. {
  316. this.mColor = ColorHelper.InverseColor(this.mColor);
  317. }
  318. var list = new List<FrameworkElement>();
  319. flag = this.IsTooSmallToShow(owner, aScale);
  320. List<FrameworkElement> result;
  321. if (flag)
  322. {
  323. result = list;
  324. }
  325. else
  326. {
  327. IDateScaler dateScaler = owner.DateScaler;
  328. flag = !this.mSaveByPercent;
  329. if (flag)
  330. {
  331. this.mLastX1 = (double)dateScaler.XforDate(this.P1Date);
  332. this.mLastY1 = (double)aScale.GetRightValueScaler().ScaledY(this.P1Val, aRect);
  333. this.P1XPercent = (this.mLastX1 - this.mLastRect.Top) / this.mLastRect.Width;
  334. this.P1YPercent = (this.mLastY1 - this.mLastRect.Left) / this.mLastRect.Height;
  335. }
  336. else
  337. {
  338. this.mLastX1 = aRect.Left + aRect.Width * this.P1XPercent;
  339. this.mLastY1 = aRect.Top + aRect.Height * this.P1YPercent;
  340. DateTime p1Date = this.Chart.DateScaler.DateFromX((float)this.mLastX1);
  341. float p1Val = this.mLastScale.GetRightValueScaler().ValueFromPercent((float)(1.0 - this.mLastY1 / this.mLastRect.Height));
  342. this.P1Date = p1Date;
  343. this.P1Val = p1Val;
  344. }
  345. bool arg_1E8_0;
  346. if (this.mLastX1 <= 30000.0 && this.mLastX1 >= -30000.0)
  347. {
  348. if (this.mLastY1 <= 30000.0)
  349. {
  350. if (this.mLastY1 >= -30000.0)
  351. {
  352. arg_1E8_0 = false;
  353. goto IL_1E8;
  354. }
  355. }
  356. }
  357. arg_1E8_0 = true;
  358. IL_1E8:
  359. flag = arg_1E8_0;
  360. if (flag)
  361. {
  362. result = list;
  363. }
  364. else
  365. {
  366. flag = (this.SelectionArea == null);
  367. if (flag)
  368. {
  369. this.SelectionArea = new DrawingSelectionArea(this, owner);
  370. }
  371. list.AddRange(this.GetDrawingPlots(owner, aScale, aRect));
  372. flag = (DrawHandles && this.ShowDragHandle());
  373. if (flag)
  374. {
  375. list.AddRange(this.SelectionArea.GetControls());
  376. }
  377. result = list;
  378. }
  379. }
  380. return result;
  381. }
  382. public virtual List<ControlPair> GetPropertyEditors()
  383. {
  384. var list = new List<ControlPair>
  385. {
  386. new ControlPair
  387. {
  388. ControlLeft =
  389. new TextBlock {Text = LanguageManager.FindResource(LanguageConst.DrawingTool_Edit_Color)},
  390. ControlRight = new ColorOpacityEditor(this, "Color", "", "")
  391. }
  392. };
  393. List<ControlPair> list2 = this.AdditionalEditors();
  394. bool flag = list2 != null;
  395. if (flag)
  396. {
  397. list.AddRange(list2);
  398. }
  399. //flag = this.CanPlotOnAllSymbols();
  400. //if (flag)
  401. //{
  402. // list.Add(new ControlPair
  403. // {
  404. // ControlRight = new BooleanEditor(this, "PlotOnAllSymbols", "Draw on all Symbols")
  405. // });
  406. //}
  407. flag = this.CanSaveByPercent();
  408. if (flag)
  409. {
  410. list.Add(new ControlPair()
  411. {
  412. ControlLeft = new BooleanEditor(this, "SaveByPercent", LanguageManager.FindResource(LanguageConst.DrawingTool_Edit_SaveByPercent))
  413. });
  414. }
  415. return list;
  416. }
  417. public abstract ImageSource Icon();
  418. public void InitDone(MouseEventArgs e)
  419. {
  420. }
  421. public void InitPoint(DateTime aDate, float aVal, ScaleLayer aScale, Rect aRect, Point p)
  422. {
  423. this.mLastRect = aRect;
  424. this.mLastScale = aScale;
  425. this.P1Date = aDate;
  426. this.P1Val = aVal;
  427. this.P1XPercent = (p.X - this.mLastRect.Top) / this.mLastRect.Width;
  428. this.P1YPercent = (p.Y - this.mLastRect.Left) / this.mLastRect.Height;
  429. }
  430. public bool IsTooSmallToShow(Chart owner, ScaleLayer aScale, DrawingToolInfo Info)
  431. {
  432. return false;
  433. }
  434. public bool IsTooSmallToShow(Chart owner, ScaleLayer aScale)
  435. {
  436. return false;
  437. }
  438. public abstract string MenuDescription();
  439. public void MovePlot(Point initialPosition, Point currentPosition)
  440. {
  441. double num = currentPosition.X - initialPosition.X;
  442. double num2 = currentPosition.Y - initialPosition.Y;
  443. var value = new Point(this._lastX1 + num, this._lastY1 + num2);
  444. value.X = Math.Max(0, value.X);
  445. value.Y = Math.Max(0, value.Y);
  446. this[0] = value;
  447. }
  448. public void MovePlot(Point initialPosition, Point currentPosition, Point maxPoint)
  449. {
  450. double num = currentPosition.X - initialPosition.X;
  451. double num2 = currentPosition.Y - initialPosition.Y;
  452. var value = new Point(this._lastX1 + num, this._lastY1 + num2);
  453. value.X = Math.Max(0, value.X);
  454. value.X = Math.Min(maxPoint.X, value.X);
  455. value.Y = Math.Max(0, value.Y);
  456. value.Y = Math.Min(maxPoint.Y, value.Y);
  457. this[0] = value;
  458. }
  459. public void MoveSecondPoint(DateTime aDate, float aVal, Point p)
  460. {
  461. this.P1Date = aDate;
  462. this.P1Val = aVal;
  463. this.P1XPercent = (p.X - this.mLastRect.Top) / this.mLastRect.Width;
  464. this.P1YPercent = (p.Y - this.mLastRect.Left) / this.mLastRect.Height;
  465. }
  466. public void PrepareToMove()
  467. {
  468. this._lastX1 = this.mLastX1;
  469. this._lastY1 = this.mLastY1;
  470. }
  471. public void PropertyChanged()
  472. {
  473. bool flag = this._drawingToolSpec != null;
  474. if (flag)
  475. {
  476. this._drawingToolSpec.ToolChanged(this);
  477. }
  478. }
  479. public void ShowEditor(MouseEventArgs mouseargs)
  480. {
  481. bool flag = this.Chart == null;
  482. if (!flag)
  483. {
  484. var drawingToolEditor = new DrawingToolEditor(this);
  485. drawingToolEditor.Show(mouseargs);
  486. }
  487. }
  488. public abstract DrawingToolType ToolType();
  489. public XElement ToXml(List<object> args)
  490. {
  491. var xElement = new XElement(this.RootName);
  492. var flag = args == null || !args.Contains("SAVEDEF");
  493. if (flag)
  494. {
  495. xElement.Add(new XAttribute("D1", this.P1Date.Ticks.ToString()));
  496. xElement.Add(new XAttribute("V1", XMLHelp.NumToString(this.P1Val)));
  497. }
  498. xElement.Add(new XAttribute("Clr", this.mColor.ToString()));
  499. xElement.Add(new XAttribute("ALS", this._plotOnAllSymbols.ToString()));
  500. this.AddToXml(xElement, args);
  501. return xElement;
  502. }
  503. #endregion Public Methods
  504. #region Protected Methods
  505. protected virtual void AddFromInfo(DrawingToolInfo info)
  506. {
  507. }
  508. protected virtual void AddFromXml(XElement xml)
  509. {
  510. }
  511. protected virtual void AddToInfo(DrawingToolInfo info)
  512. {
  513. }
  514. protected virtual void AddToXml(XElement xml, List<object> args)
  515. {
  516. }
  517. protected abstract void ColorChanged();
  518. protected abstract List<FrameworkElement> GetDrawingPlots(Chart owner, ScaleLayer aScale, Rect aRect);
  519. protected abstract void MoveDrawingToNewLocations(Rect aRect);
  520. protected virtual bool ShowDragHandle()
  521. {
  522. return true;
  523. }
  524. public string GetXmlForDefault()
  525. {
  526. var list = new List<object> { "SAVEDEF" };
  527. return this.ToXml(list).ToString();
  528. }
  529. #endregion Protected Methods
  530. /// <summary>
  531. /// Gets the size of the chart font.
  532. /// </summary>
  533. /// <returns>System.Double.</returns>
  534. public double GetChartFontSize()
  535. {
  536. return (Chart == null) ? 12f : this.Chart.ChartFontSize;
  537. }
  538. #endregion Methods
  539. }
  540. }