MirControl.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5. using Client.MirGraphics;
  6. using Client.MirSounds;
  7. using Microsoft.DirectX;
  8. using Microsoft.DirectX.Direct3D;
  9. namespace Client.MirControls
  10. {
  11. public class MirControl : IDisposable
  12. {
  13. public static MirControl ActiveControl, MouseControl;
  14. public virtual Point DisplayLocation { get { return Parent == null ? Location : Parent.DisplayLocation.Add(Location); } }
  15. public Rectangle DisplayRectangle { get { return new Rectangle(DisplayLocation, Size); } }
  16. public bool GrayScale { get; set; }
  17. public bool Blending { get; set; }
  18. public float BlendingRate { get; set; }
  19. public BlendMode BlendMode { get; set; }
  20. public bool drawimgaes=true;
  21. public bool Zoom = false;
  22. public float zoomrate;
  23. #region Back Colour
  24. private Color _backColour;
  25. public Color BackColour
  26. {
  27. get { return _backColour; }
  28. set
  29. {
  30. if (_backColour == value)
  31. return;
  32. _backColour = value;
  33. OnBackColourChanged();
  34. }
  35. }
  36. public event EventHandler BackColourChanged;
  37. protected virtual void OnBackColourChanged()
  38. {
  39. TextureValid = false;
  40. Redraw();
  41. if (BackColourChanged != null)
  42. BackColourChanged.Invoke(this, EventArgs.Empty);
  43. }
  44. #endregion
  45. #region Border
  46. protected Rectangle BorderRectangle;
  47. private bool _border;
  48. protected Vector2[] _borderInfo;
  49. protected virtual Vector2[] BorderInfo
  50. {
  51. get
  52. {
  53. if (Size == Size.Empty)
  54. return null;
  55. if (BorderRectangle != DisplayRectangle)
  56. {
  57. _borderInfo = new[]
  58. {
  59. new Vector2(DisplayRectangle.Left - 1, DisplayRectangle.Top - 1),
  60. new Vector2(DisplayRectangle.Right, DisplayRectangle.Top - 1),
  61. new Vector2(DisplayRectangle.Left - 1, DisplayRectangle.Top - 1),
  62. new Vector2(DisplayRectangle.Left - 1, DisplayRectangle.Bottom),
  63. new Vector2(DisplayRectangle.Left - 1, DisplayRectangle.Bottom),
  64. new Vector2(DisplayRectangle.Right, DisplayRectangle.Bottom),
  65. new Vector2(DisplayRectangle.Right, DisplayRectangle.Top - 1),
  66. new Vector2(DisplayRectangle.Right, DisplayRectangle.Bottom)
  67. };
  68. BorderRectangle = DisplayRectangle;
  69. }
  70. return _borderInfo;
  71. }
  72. }
  73. public virtual bool Border
  74. {
  75. get { return _border; }
  76. set
  77. {
  78. if (_border == value)
  79. return;
  80. _border = value;
  81. OnBorderChanged();
  82. }
  83. }
  84. public event EventHandler BorderChanged;
  85. private void OnBorderChanged()
  86. {
  87. Redraw();
  88. if (BorderChanged != null)
  89. BorderChanged.Invoke(this, EventArgs.Empty);
  90. }
  91. #endregion
  92. #region Border Colour
  93. private Color _borderColour;
  94. public Color BorderColour
  95. {
  96. get { return _borderColour; }
  97. set
  98. {
  99. if (_borderColour == value)
  100. return;
  101. _borderColour = value;
  102. OnBorderColourChanged();
  103. }
  104. }
  105. public event EventHandler BorderColourChanged;
  106. private void OnBorderColourChanged()
  107. {
  108. Redraw();
  109. if (BorderColourChanged != null)
  110. BorderColourChanged.Invoke(this, EventArgs.Empty);
  111. }
  112. #endregion
  113. #region Control Texture
  114. public long CleanTime;
  115. protected Texture ControlTexture;
  116. protected internal bool TextureValid;
  117. private bool _drawControlTexture;
  118. protected Size TextureSize;
  119. public bool DrawControlTexture
  120. {
  121. get { return _drawControlTexture; }
  122. set
  123. {
  124. if (_drawControlTexture == value)
  125. return;
  126. _drawControlTexture = value;
  127. Redraw();
  128. }
  129. }
  130. protected virtual void CreateTexture()
  131. {
  132. if (ControlTexture != null && !ControlTexture.Disposed && Size != TextureSize)
  133. ControlTexture.Dispose();
  134. if (ControlTexture == null || ControlTexture.Disposed)
  135. {
  136. DXManager.ControlList.Add(this);
  137. // ControlTexture = new Texture(DXManager.Device, Size.Width, Size.Height, 0, Usage.RenderTarget, Format.A8R8G8B8, Pool.Default);
  138. try
  139. {
  140. ControlTexture = new Texture(DXManager.Device, Size.Width, Size.Height, 0, Usage.RenderTarget, Format.X8R8G8B8, Pool.Default);
  141. }
  142. catch (Exception)
  143. {
  144. throw;
  145. }
  146. ControlTexture.Disposing += ControlTexture_Disposing;
  147. TextureSize = Size;
  148. }
  149. Surface oldSurface = DXManager.CurrentSurface;
  150. Surface surface = ControlTexture.GetSurfaceLevel(0);
  151. DXManager.SetSurface(surface);
  152. DXManager.Device.Clear(ClearFlags.Target, BackColour, 0, 0);
  153. DXManager.SetSurface(oldSurface);
  154. TextureValid = true;
  155. surface.Dispose();
  156. }
  157. protected void ControlTexture_Disposing(object sender, EventArgs e)
  158. {
  159. ControlTexture = null;
  160. TextureValid = false;
  161. TextureSize = Size.Empty;
  162. DXManager.ControlList.Remove(this);
  163. }
  164. internal void DisposeTexture()
  165. {
  166. if (ControlTexture == null || ControlTexture.Disposed) return;
  167. ControlTexture.Dispose();
  168. }
  169. #endregion
  170. #region Controls
  171. public List<MirControl> Controls { get; private set; }
  172. public event EventHandler ControlAdded , ControlRemoved;
  173. private void AddControl(MirControl control)
  174. {
  175. Controls.Add(control);
  176. OnControlAdded();
  177. }
  178. public void InsertControl(int index, MirControl control)
  179. {
  180. if (control.Parent != this)
  181. {
  182. control.Parent = null;
  183. control._parent = this;
  184. }
  185. if (index >= Controls.Count)
  186. Controls.Add(control);
  187. else
  188. {
  189. Controls.Insert(index, control);
  190. OnControlAdded();
  191. }
  192. }
  193. private void RemoveControl(MirControl control)
  194. {
  195. Controls.Remove(control);
  196. OnControlRemoved();
  197. }
  198. private void OnControlAdded()
  199. {
  200. Redraw();
  201. if (ControlAdded != null)
  202. ControlAdded.Invoke(this, EventArgs.Empty);
  203. }
  204. private void OnControlRemoved()
  205. {
  206. Redraw();
  207. if (ControlRemoved != null)
  208. ControlRemoved.Invoke(this, EventArgs.Empty);
  209. }
  210. #endregion
  211. #region Enabled
  212. private bool _enabled;
  213. public bool Enabled
  214. {
  215. internal get { return Parent == null ? _enabled : Parent.Enabled && _enabled; }
  216. set
  217. {
  218. if (_enabled == value)
  219. return;
  220. _enabled = value;
  221. OnEnabledChanged();
  222. }
  223. }
  224. public event EventHandler EnabledChanged;
  225. protected virtual void OnEnabledChanged()
  226. {
  227. Redraw();
  228. if (EnabledChanged != null)
  229. EnabledChanged.Invoke(this, EventArgs.Empty);
  230. if (!Enabled && ActiveControl == this)
  231. ActiveControl.Deactivate();
  232. if (Controls != null)
  233. foreach (MirControl control in Controls)
  234. control.OnEnabledChanged();
  235. }
  236. #endregion
  237. #region Events
  238. protected bool HasShown;
  239. public event EventHandler Click , DoubleClick, BeforeDraw , AfterDraw , MouseEnter , MouseLeave , Shown , BeforeShown, Disposing;
  240. public event MouseEventHandler MouseWheel,MouseMove, MouseDown, MouseUp;
  241. public event KeyEventHandler KeyDown , KeyUp;
  242. public event KeyPressEventHandler KeyPress;
  243. #endregion
  244. #region Fore Colour
  245. private Color _foreColour;
  246. public Color ForeColour
  247. {
  248. get { return _foreColour; }
  249. set
  250. {
  251. if (_foreColour == value)
  252. return;
  253. _foreColour = value;
  254. OnForeColourChanged();
  255. }
  256. }
  257. public event EventHandler ForeColourChanged;
  258. protected virtual void OnForeColourChanged()
  259. {
  260. TextureValid = false;
  261. if (ForeColourChanged != null)
  262. ForeColourChanged.Invoke(this, EventArgs.Empty);
  263. }
  264. #endregion
  265. #region Location
  266. private Point _location;
  267. public Point Location
  268. {
  269. get { return _location; }
  270. set
  271. {
  272. if (_location == value)
  273. return;
  274. _location = value;
  275. OnLocationChanged();
  276. }
  277. }
  278. public event EventHandler LocationChanged;
  279. protected virtual void OnLocationChanged()
  280. {
  281. Redraw();
  282. if (Controls != null)
  283. for (int i = 0; i < Controls.Count; i++)
  284. Controls[i].OnLocationChanged();
  285. if (LocationChanged != null)
  286. LocationChanged.Invoke(this, EventArgs.Empty);
  287. }
  288. #endregion
  289. #region Hint
  290. private string _hint;
  291. public string Hint
  292. {
  293. get { return _hint; }
  294. set
  295. {
  296. if (_hint == value)
  297. return;
  298. _hint = value;
  299. OnHintChanged(EventArgs.Empty);
  300. }
  301. }
  302. public event EventHandler HintChanged;
  303. private void OnHintChanged(EventArgs e)
  304. {
  305. Redraw();
  306. if (HintChanged != null)
  307. HintChanged.Invoke(this, e);
  308. }
  309. #endregion
  310. #region Modal
  311. private bool _modal;
  312. public bool Modal
  313. {
  314. get { return _modal; }
  315. set
  316. {
  317. if (_modal == value)
  318. return;
  319. _modal = value;
  320. OnModalChanged();
  321. }
  322. }
  323. public event EventHandler ModalChanged;
  324. private void OnModalChanged()
  325. {
  326. Redraw();
  327. if (ModalChanged != null)
  328. ModalChanged.Invoke(this, EventArgs.Empty);
  329. }
  330. #endregion
  331. #region Movable
  332. protected internal bool Moving;
  333. private bool _movable;
  334. private Point _movePoint;
  335. public bool Movable
  336. {
  337. get { return _movable; }
  338. set
  339. {
  340. if (_movable == value)
  341. return;
  342. _movable = value;
  343. OnMovableChanged();
  344. }
  345. }
  346. public event EventHandler MovableChanged;
  347. public event MouseEventHandler OnMoving;
  348. private void OnMovableChanged()
  349. {
  350. Redraw();
  351. if (MovableChanged != null)
  352. MovableChanged.Invoke(this, EventArgs.Empty);
  353. }
  354. #endregion
  355. #region Not Control
  356. private bool _notControl;
  357. public bool NotControl
  358. {
  359. private get { return _notControl; }
  360. set
  361. {
  362. if (_notControl == value)
  363. return;
  364. _notControl = value;
  365. OnNotControlChanged();
  366. }
  367. }
  368. public event EventHandler NotControlChanged;
  369. private void OnNotControlChanged()
  370. {
  371. Redraw();
  372. if (NotControlChanged != null)
  373. NotControlChanged.Invoke(this, EventArgs.Empty);
  374. }
  375. #endregion
  376. #region Opacity
  377. private float _opacity;
  378. public float Opacity
  379. {
  380. get { return _opacity; }
  381. set
  382. {
  383. if (value > 1F)
  384. value = 1F;
  385. if (value < 0F)
  386. value = 0;
  387. if (_opacity == value)
  388. return;
  389. _opacity = value;
  390. OnOpacityChanged();
  391. }
  392. }
  393. public event EventHandler OpacityChanged;
  394. private void OnOpacityChanged()
  395. {
  396. Redraw();
  397. if (OpacityChanged != null)
  398. OpacityChanged.Invoke(this, EventArgs.Empty);
  399. }
  400. #endregion
  401. #region Parent
  402. private MirControl _parent;
  403. public MirControl Parent
  404. {
  405. get { return _parent; }
  406. set
  407. {
  408. if (_parent == value) return;
  409. if (_parent != null)
  410. _parent.RemoveControl(this);
  411. _parent = value;
  412. if (_parent != null)
  413. _parent.AddControl(this);
  414. OnParentChanged();
  415. }
  416. }
  417. public event EventHandler ParentChanged;
  418. protected virtual void OnParentChanged()
  419. {
  420. OnLocationChanged();
  421. if (ParentChanged != null)
  422. ParentChanged.Invoke(this, EventArgs.Empty);
  423. }
  424. #endregion
  425. #region Size
  426. // ReSharper disable InconsistentNaming
  427. protected Size _size;
  428. // ReSharper restore InconsistentNaming
  429. public virtual Size Size
  430. {
  431. get { return _size; }
  432. set
  433. {
  434. if (_size == value)
  435. return;
  436. _size = value;
  437. OnSizeChanged();
  438. }
  439. }
  440. public virtual Size TrueSize
  441. {
  442. get { return _size; }
  443. }
  444. public event EventHandler SizeChanged;
  445. protected virtual void OnSizeChanged()
  446. {
  447. TextureValid = false;
  448. Redraw();
  449. if (SizeChanged != null)
  450. SizeChanged.Invoke(this, EventArgs.Empty);
  451. }
  452. #endregion
  453. #region Sound
  454. private int _sound;
  455. public int Sound
  456. {
  457. get { return _sound; }
  458. set
  459. {
  460. if (_sound == value)
  461. return;
  462. _sound = value;
  463. OnSoundChanged();
  464. }
  465. }
  466. public event EventHandler SoundChanged;
  467. private void OnSoundChanged()
  468. {
  469. if (SoundChanged != null)
  470. SoundChanged.Invoke(this, EventArgs.Empty);
  471. }
  472. #endregion
  473. #region Sort
  474. private bool _sort;
  475. public bool Sort
  476. {
  477. get { return _sort; }
  478. set
  479. {
  480. if (_sort == value)
  481. return;
  482. _sort = value;
  483. OnSortChanged();
  484. }
  485. }
  486. public event EventHandler SortChanged;
  487. private void OnSortChanged()
  488. {
  489. Redraw();
  490. if (SortChanged != null)
  491. SortChanged.Invoke(this, EventArgs.Empty);
  492. }
  493. public void TrySort()
  494. {
  495. if (Parent == null)
  496. return;
  497. Parent.TrySort();
  498. if (Parent.Controls[Parent.Controls.Count - 1] == this)
  499. return;
  500. if (!Sort) return;
  501. Parent.Controls.Remove(this);
  502. Parent.Controls.Add(this);
  503. Redraw();
  504. }
  505. #endregion
  506. #region Visible
  507. private bool _visible;
  508. public virtual bool Visible
  509. {
  510. get { return Parent == null ? _visible : Parent.Visible && _visible; }
  511. set
  512. {
  513. if (_visible == value)
  514. return;
  515. _visible = value;
  516. OnVisibleChanged();
  517. }
  518. }
  519. public event EventHandler VisibleChanged;
  520. protected virtual void OnVisibleChanged()
  521. {
  522. Redraw();
  523. if (VisibleChanged != null)
  524. VisibleChanged.Invoke(this, EventArgs.Empty);
  525. Moving = false;
  526. _movePoint = Point.Empty;
  527. if (Sort && Parent != null)
  528. {
  529. Parent.Controls.Remove(this);
  530. Parent.Controls.Add(this);
  531. }
  532. if (MouseControl == this && !Visible)
  533. {
  534. Dehighlight();
  535. Deactivate();
  536. }
  537. else if (IsMouseOver(CMain.MPoint))
  538. Highlight();
  539. if (Controls != null)
  540. foreach (MirControl control in Controls)
  541. control.OnVisibleChanged();
  542. }
  543. protected void OnBeforeShown()
  544. {
  545. if (HasShown)
  546. return;
  547. if (Visible && IsMouseOver(CMain.MPoint))
  548. Highlight();
  549. if (BeforeShown != null)
  550. BeforeShown.Invoke(this, EventArgs.Empty);
  551. }
  552. protected void OnShown()
  553. {
  554. if (HasShown)
  555. return;
  556. if (Shown != null)
  557. Shown.Invoke(this, EventArgs.Empty);
  558. HasShown = true;
  559. }
  560. #endregion
  561. #region MultiLine
  562. public virtual void MultiLine()
  563. {
  564. }
  565. #endregion
  566. #region Positions
  567. protected Point Center
  568. {
  569. get { return new Point((Settings.ScreenWidth - Size.Width) / 2, (Settings.ScreenHeight - Size.Height) / 2); }
  570. }
  571. protected Point Left
  572. {
  573. get { return new Point(0, (Settings.ScreenHeight - Size.Height) / 2); }
  574. }
  575. protected Point Top
  576. {
  577. get { return new Point((Settings.ScreenWidth - Size.Width) / 2, 0); }
  578. }
  579. protected Point Right
  580. {
  581. get { return new Point(Settings.ScreenWidth - Size.Width, (Settings.ScreenHeight - Size.Height) / 2); }
  582. }
  583. protected Point Bottom
  584. {
  585. get { return new Point((Settings.ScreenWidth - Size.Width) / 2, Settings.ScreenHeight - Size.Height); }
  586. }
  587. protected Point TopLeft
  588. {
  589. get { return new Point(0, 0); }
  590. }
  591. protected Point TopRight
  592. {
  593. get { return new Point(Settings.ScreenWidth - Size.Width, 0); }
  594. }
  595. protected Point BottomRight
  596. {
  597. get { return new Point(Settings.ScreenWidth - Size.Width, Settings.ScreenHeight - Size.Height); }
  598. }
  599. protected Point BottomLeft
  600. {
  601. get { return new Point(0, Settings.ScreenHeight - Size.Height); }
  602. }
  603. #endregion
  604. public void BringToFront()
  605. {
  606. if (Parent == null) return;
  607. int index = _parent.Controls.IndexOf(this);
  608. if (index == _parent.Controls.Count - 1) return;
  609. _parent.Controls.RemoveAt(index);
  610. _parent.Controls.Add(this);
  611. Redraw();
  612. }
  613. public MirControl()
  614. {
  615. Controls = new List<MirControl>();
  616. _opacity = 1F;
  617. _enabled = true;
  618. _foreColour = Color.White;
  619. _visible = true;
  620. _sound = SoundList.None;
  621. }
  622. public virtual void Draw()
  623. {
  624. // if (IsDisposed || !Visible ||!drawimgaes/*|| Size.Width == 0 || Size.Height == 0*/ || Size.Width > Settings.ScreenWidth || Size.Height > Settings.ScreenHeight)
  625. // return;
  626. if (IsDisposed || !Visible || !drawimgaes/*|| Size.Width == 0 || Size.Height == 0*/ || (!Zoom && Size.Width > Settings.ScreenWidth) || (!Zoom&& Size.Height > Settings.ScreenHeight))
  627. return;
  628. OnBeforeShown();
  629. BeforeDrawControl();
  630. DrawControl();
  631. DrawChildControls();
  632. DrawBorder();
  633. AfterDrawControl();
  634. CleanTime = CMain.Time + Settings.CleanDelay;
  635. OnShown();
  636. }
  637. protected virtual void BeforeDrawControl()
  638. {
  639. if (BeforeDraw != null)
  640. BeforeDraw.Invoke(this, EventArgs.Empty);
  641. }
  642. protected internal virtual void DrawControl()
  643. {
  644. if (!DrawControlTexture)
  645. return;
  646. if (!TextureValid)
  647. CreateTexture();
  648. if (ControlTexture == null || ControlTexture.Disposed)
  649. return;
  650. float oldOpacity = DXManager.Opacity;
  651. DXManager.SetOpacity(Opacity);
  652. DXManager.Device.RenderState.AlphaBlendEnable = true;
  653. DXManager.Sprite.Transform = Microsoft.DirectX.Matrix.Translation(DisplayLocation.X, DisplayLocation.Y, 0f);
  654. DXManager.Sprite.Draw(ControlTexture, Rectangle.Empty, Vector3.Empty, new Vector3(0, 0, 0), Color.White);
  655. DXManager.Sprite.Transform = Microsoft.DirectX.Matrix.Identity;
  656. // DXManager.Sprite.Draw2D(ControlTexture, Point.Empty, 0F, DisplayLocation, Color.White);
  657. DXManager.SetOpacity(oldOpacity);
  658. CleanTime = CMain.Time + Settings.CleanDelay;
  659. }
  660. protected void DrawChildControls()
  661. {
  662. if (Controls != null)
  663. for (int i = 0; i < Controls.Count; i++)
  664. {
  665. if (Controls[i] == null) continue;
  666. if (Controls[i] != null)
  667. Controls[i].Draw();
  668. }
  669. }
  670. protected virtual void DrawBorder()
  671. {
  672. if (!Border || BorderInfo == null)
  673. return;
  674. DXManager.Sprite.Flush();
  675. DXManager.Line.Draw(BorderInfo, _borderColour);
  676. }
  677. protected void AfterDrawControl()
  678. {
  679. if (AfterDraw != null)
  680. AfterDraw.Invoke(this, EventArgs.Empty);
  681. }
  682. protected virtual void Deactivate()
  683. {
  684. if (ActiveControl != this)
  685. return;
  686. ActiveControl = null;
  687. Moving = false;
  688. _movePoint = Point.Empty;
  689. }
  690. protected virtual void Dehighlight()
  691. {
  692. if (MouseControl != this)
  693. return;
  694. MouseControl.OnMouseLeave();
  695. MouseControl = null;
  696. }
  697. protected virtual void Activate()
  698. {
  699. if (ActiveControl == this)
  700. return;
  701. if (ActiveControl != null)
  702. ActiveControl.Deactivate();
  703. ActiveControl = this;
  704. }
  705. protected virtual void Highlight()
  706. {
  707. if (MouseControl == this)
  708. return;
  709. if (NotControl)
  710. {
  711. }
  712. if (MouseControl != null)
  713. MouseControl.Dehighlight();
  714. if (ActiveControl != null && ActiveControl != this) return;
  715. OnMouseEnter();
  716. MouseControl = this;
  717. }
  718. public virtual bool IsMouseOver(Point p)
  719. {
  720. return Visible && (DisplayRectangle.Contains(p) || Moving || Modal) && !NotControl;
  721. }
  722. protected virtual void OnMouseEnter()
  723. {
  724. if (!_enabled)
  725. return;
  726. Redraw();
  727. if (MouseEnter != null)
  728. MouseEnter.Invoke(this, EventArgs.Empty);
  729. }
  730. protected virtual void OnMouseLeave()
  731. {
  732. if (!_enabled)
  733. return;
  734. Redraw();
  735. if (MouseLeave != null)
  736. MouseLeave.Invoke(this, EventArgs.Empty);
  737. }
  738. public virtual void OnMouseClick(MouseEventArgs e)
  739. {
  740. if (!Enabled)
  741. return;
  742. if (Sound != SoundList.None)
  743. SoundManager.PlaySound(Sound);
  744. if (Click != null)
  745. InvokeMouseClick(e);
  746. }
  747. public virtual void OnMouseDoubleClick(MouseEventArgs e)
  748. {
  749. if (!Enabled)
  750. return;
  751. if (DoubleClick != null)
  752. {
  753. if (Sound != SoundList.None)
  754. SoundManager.PlaySound(Sound);
  755. InvokeMouseDoubleClick(e);
  756. }
  757. else
  758. OnMouseClick(e);
  759. }
  760. public void InvokeMouseClick(EventArgs e)
  761. {
  762. if (Click != null)
  763. Click.Invoke(this, e);
  764. }
  765. public void InvokeMouseDoubleClick(EventArgs e)
  766. {
  767. DoubleClick.Invoke(this, e);
  768. }
  769. public virtual void OnMouseMove(MouseEventArgs e)
  770. {
  771. if (!_enabled)
  772. return;
  773. if (Moving)
  774. {
  775. Point tempPoint = CMain.MPoint.Subtract(_movePoint);
  776. if (Parent == null)
  777. {
  778. if (tempPoint.Y + TrueSize.Height > Settings.ScreenHeight)
  779. tempPoint.Y = Settings.ScreenHeight - TrueSize.Height - 1;
  780. if (tempPoint.X + TrueSize.Width > Settings.ScreenWidth)
  781. tempPoint.X = Settings.ScreenWidth - TrueSize.Width - 1;
  782. }
  783. else
  784. {
  785. if (tempPoint.Y + TrueSize.Height > Parent.TrueSize.Height)
  786. tempPoint.Y = Parent.TrueSize.Height - TrueSize.Height;
  787. if (tempPoint.X + TrueSize.Width > Parent.TrueSize.Width)
  788. tempPoint.X = Parent.TrueSize.Width - TrueSize.Width;
  789. }
  790. if (tempPoint.X < 0)
  791. tempPoint.X = 0;
  792. if (tempPoint.Y < 0)
  793. tempPoint.Y = 0;
  794. Location = tempPoint;
  795. if (OnMoving != null)
  796. OnMoving.Invoke(this, e);
  797. return;
  798. }
  799. if (Controls != null)
  800. for (int i = Controls.Count - 1; i >= 0; i--)
  801. if (Controls[i].IsMouseOver(CMain.MPoint))
  802. {
  803. Controls[i].OnMouseMove(e);
  804. return;
  805. }
  806. Highlight();
  807. if (MouseMove != null)
  808. MouseMove.Invoke(this, e);
  809. }
  810. public virtual void OnMouseDown(MouseEventArgs e)
  811. {
  812. if (!_enabled)
  813. return;
  814. Activate();
  815. TrySort();
  816. if (_movable)
  817. {
  818. Moving = true;
  819. _movePoint = CMain.MPoint.Subtract(Location);
  820. }
  821. if (MouseDown != null)
  822. MouseDown.Invoke(this, e);
  823. }
  824. public virtual void OnMouseUp(MouseEventArgs e)
  825. {
  826. if (!_enabled)
  827. return;
  828. if (Moving)
  829. {
  830. Moving = false;
  831. _movePoint = Point.Empty;
  832. }
  833. if (ActiveControl != null) ActiveControl.Deactivate();
  834. if (MouseUp != null)
  835. MouseUp.Invoke(this, e);
  836. }
  837. public virtual void OnMouseWheel(MouseEventArgs e)
  838. {
  839. if (!Enabled)
  840. return;
  841. if (MouseWheel != null)
  842. MouseWheel(this, e);
  843. }
  844. public virtual void OnKeyPress(KeyPressEventArgs e)
  845. {
  846. if (!_enabled)
  847. return;
  848. if (Controls != null)
  849. for (int i = Controls.Count - 1; i >= 0; i--)
  850. if (e.Handled)
  851. return;
  852. else
  853. Controls[i].OnKeyPress(e);
  854. if (KeyPress == null)
  855. return;
  856. KeyPress.Invoke(this, e);
  857. }
  858. public virtual void OnKeyDown(KeyEventArgs e)
  859. {
  860. if (!_enabled)
  861. return;
  862. if (Controls != null)
  863. for (int i = Controls.Count - 1; i >= 0; i--)
  864. if (e.Handled)
  865. return;
  866. else
  867. Controls[i].OnKeyDown(e);
  868. if (KeyDown == null)
  869. return;
  870. KeyDown.Invoke(this, e);
  871. }
  872. public virtual void OnKeyUp(KeyEventArgs e)
  873. {
  874. if (!_enabled)
  875. return;
  876. if (Controls != null)
  877. for (int i = Controls.Count - 1; i >= 0; i--)
  878. if (e.Handled)
  879. return;
  880. else
  881. Controls[i].OnKeyUp(e);
  882. if (KeyUp == null)
  883. return;
  884. KeyUp.Invoke(this, e);
  885. }
  886. public virtual void Redraw()
  887. {
  888. if (Parent != null) Parent.Redraw();
  889. }
  890. #region Disposable
  891. public bool IsDisposed { get; private set; }
  892. public void Dispose()
  893. {
  894. if (IsDisposed)
  895. return;
  896. Dispose(true);
  897. }
  898. protected virtual void Dispose(bool disposing)
  899. {
  900. if (disposing)
  901. {
  902. if (Disposing != null)
  903. Disposing.Invoke(this, EventArgs.Empty);
  904. Disposing = null;
  905. BackColourChanged = null;
  906. _backColour = Color.Empty;
  907. BorderChanged = null;
  908. _border = false;
  909. BorderRectangle = Rectangle.Empty;
  910. _borderInfo = null;
  911. BorderColourChanged = null;
  912. _borderColour = Color.Empty;
  913. DrawControlTexture = false;
  914. if (ControlTexture != null && !ControlTexture.Disposed)
  915. ControlTexture.Dispose();
  916. ControlTexture = null;
  917. TextureValid = false;
  918. ControlAdded = null;
  919. ControlRemoved = null;
  920. if (Controls != null)
  921. {
  922. for (int i = Controls.Count - 1; i >= 0; i--)
  923. {
  924. if (Controls[i] != null && !Controls[i].IsDisposed)
  925. Controls[i].Dispose();
  926. }
  927. Controls = null;
  928. }
  929. _enabled = false;
  930. EnabledChanged = null;
  931. HasShown = false;
  932. BeforeDraw = null;
  933. AfterDraw = null;
  934. Shown = null;
  935. BeforeShown = null;
  936. Click = null;
  937. DoubleClick = null;
  938. MouseEnter = null;
  939. MouseLeave = null;
  940. MouseMove = null;
  941. MouseDown = null;
  942. MouseUp = null;
  943. MouseWheel = null;
  944. KeyPress = null;
  945. KeyUp = null;
  946. KeyDown = null;
  947. ForeColourChanged = null;
  948. _foreColour = Color.Empty;
  949. LocationChanged = null;
  950. _location = Point.Empty;
  951. ModalChanged = null;
  952. _modal = false;
  953. MovableChanged = null;
  954. _movePoint = Point.Empty;
  955. Moving = false;
  956. OnMoving = null;
  957. _movable = false;
  958. NotControlChanged = null;
  959. _notControl = false;
  960. OpacityChanged = null;
  961. _opacity = 0F;
  962. if (Parent != null && Parent.Controls != null)
  963. Parent.Controls.Remove(this);
  964. ParentChanged = null;
  965. _parent = null;
  966. SizeChanged = null;
  967. _size = Size.Empty;
  968. SoundChanged = null;
  969. _sound = 0;
  970. VisibleChanged = null;
  971. _visible = false;
  972. if (ActiveControl == this) ActiveControl = null;
  973. if (MouseControl == this) MouseControl = null;
  974. }
  975. IsDisposed = true;
  976. }
  977. #endregion
  978. }
  979. }