MirControl.cs 31 KB

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