Win32ScenePanel.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. 
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Drawing;
  6. using System.Data;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Windows.Forms;
  10. using CommonLang.Geometry.SceneGraph2D;
  11. using CommonLang.Geometry;
  12. using CommonLang;
  13. namespace CommonFroms.SceneGraph2D
  14. {
  15. public partial class Win32ScenePanel : UserControl
  16. {
  17. static Win32ScenePanel()
  18. {
  19. Win32SceneGraphFactory.Init();
  20. }
  21. private DisplayRoot root;
  22. private int fps = 30;
  23. private SystemTimeRecoder update_time = new SystemTimeRecoder();
  24. public DisplayRoot RootNode { get { return root; } }
  25. public PictureBox RootCanvas { get { return canvas; } }
  26. public int Tick { get; private set; }
  27. public int FPS
  28. {
  29. get { return fps; }
  30. set
  31. {
  32. if (fps != value)
  33. {
  34. timer.Interval = 1000 / fps;
  35. }
  36. }
  37. }
  38. public int CurrentIntervalMS { get { return update_time.CurrentIntervalMS; } }
  39. public bool AutoUpdate
  40. {
  41. get { return timer.Enabled; }
  42. set
  43. {
  44. if (timer.Enabled != value)
  45. {
  46. timer.Enabled = value;
  47. update_time.Reset();
  48. if (value)
  49. {
  50. timer.Start();
  51. }
  52. else
  53. {
  54. timer.Stop();
  55. }
  56. }
  57. }
  58. }
  59. public Win32ScenePanel()
  60. {
  61. InitializeComponent();
  62. this.canvas.MouseDown += this.camera_MouseDown;
  63. this.canvas.MouseUp += this.camera_MouseUp;
  64. this.canvas.MouseMove += this.camera_MouseMove;
  65. this.canvas.MouseWheel += camera_MouseWheel;
  66. this.canvas.Paint += canvas_Paint;
  67. this.timer.Interval = 1000 / fps;
  68. this.timer.Tick += Timer1_Tick;
  69. this.MouseRightMoveCamera = true;
  70. }
  71. //-----------------------------------------------------------------------------------------------------
  72. public Vector2 CanvasMouseToRoot(System.Drawing.Point mpos)
  73. {
  74. var rpos = root.ParentToLocal(new Vector2(mpos.X, mpos.Y));
  75. return rpos;
  76. }
  77. public Vector2 ScreenMouseToRoot()
  78. {
  79. var rpos = canvas.PointToClient(MousePosition);
  80. return CanvasMouseToRoot(rpos);
  81. }
  82. protected virtual DisplayRoot CreateRoot()
  83. {
  84. return new DisplayRoot();
  85. }
  86. protected override void OnLoad(EventArgs e)
  87. {
  88. base.OnLoad(e);
  89. this.root = CreateRoot();
  90. this.AutoUpdate = true;
  91. }
  92. protected virtual void Timer1_Tick(object sender, EventArgs e)
  93. {
  94. canvas.Refresh();
  95. Tick++;
  96. }
  97. protected virtual void canvas_Paint(object sender, PaintEventArgs e)
  98. {
  99. int intervalMS = update_time.Update();
  100. OnCanvasUpdate?.Invoke(intervalMS);
  101. var g = e.Graphics;
  102. var st = g.Save();
  103. try
  104. {
  105. if (root != null)
  106. {
  107. root.Visit(new Win32Graphics(g));
  108. }
  109. }
  110. catch (Exception err)
  111. {
  112. Console.WriteLine(err.Message);
  113. }
  114. finally
  115. {
  116. g.Restore(st);
  117. }
  118. OnCanvasDraw?.Invoke(g);
  119. }
  120. protected virtual void canvas_MouseDown(object sender, MouseEventArgs e)
  121. {
  122. }
  123. protected virtual void canvas_MouseMove(object sender, MouseEventArgs e)
  124. {
  125. }
  126. protected virtual void canvas_MouseUp(object sender, MouseEventArgs e)
  127. {
  128. }
  129. public delegate void CanvasDraw(Graphics g);
  130. public event CanvasDraw OnCanvasDraw;
  131. public delegate void CanvasUpdate(int intervalMS);
  132. public event CanvasUpdate OnCanvasUpdate;
  133. //-----------------------------------------------------------------------------------------------------
  134. #region Camera
  135. private bool is_last_mouse_down = false;
  136. private Vector2 last_mouse_pos;
  137. private Vector2 last_camera_pos;
  138. public bool MouseRightMoveCamera { get; set; }
  139. public Vector2 CameraPos
  140. {
  141. get { return -root.Translation; }
  142. set { this.root.Translation = -value; }
  143. }
  144. public float CameraZoom
  145. {
  146. get { return root.Scale.X; }
  147. set { this.root.Scale = new Vector2(value, value); }
  148. }
  149. private void camera_MouseDown(object sender, MouseEventArgs e)
  150. {
  151. if (MouseRightMoveCamera)
  152. {
  153. canvas.Focus();
  154. if (e.Button == MouseButtons.Right)
  155. {
  156. is_last_mouse_down = true;
  157. last_mouse_pos = new Vector2(e.Location.X, e.Location.Y);
  158. last_camera_pos = this.CameraPos;
  159. }
  160. }
  161. RefreshCanvas();
  162. }
  163. private void camera_MouseMove(object sender, MouseEventArgs e)
  164. {
  165. if (MouseRightMoveCamera)
  166. {
  167. if (is_last_mouse_down && e.Button == MouseButtons.Right)
  168. {
  169. var offset = new Vector2(
  170. last_mouse_pos.X - e.Location.X,
  171. last_mouse_pos.Y - e.Location.Y);
  172. this.CameraPos = last_camera_pos + offset;
  173. }
  174. }
  175. RefreshCanvas();
  176. }
  177. private void camera_MouseUp(object sender, MouseEventArgs e)
  178. {
  179. is_last_mouse_down = false;
  180. RefreshCanvas();
  181. }
  182. private void camera_MouseWheel(object sender, MouseEventArgs e)
  183. {
  184. if (MouseRightMoveCamera)
  185. {
  186. this.CameraZoom += CMath.getDirect(e.Delta) * 0.1f;
  187. }
  188. RefreshCanvas();
  189. }
  190. public void RefreshCanvas()
  191. {
  192. if (timer.Enabled == false)
  193. {
  194. canvas.Refresh();
  195. }
  196. }
  197. #endregion
  198. //-----------------------------------------------------------------------------------------------------
  199. }
  200. }