DisplayTerrainPanel.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using CommonAI.RTS; using CommonLang.Vector;
  10. using CommonLang;
  11. namespace GameEditorPlugin.Win32
  12. {
  13. public partial class DisplayTerrainPanel : UserControl
  14. {
  15. private DisplayTerrain display;
  16. public DisplayTerrainPanel()
  17. {
  18. InitializeComponent();
  19. this.pictureBox1.MouseWheel += new MouseEventHandler(pictureBox1_MouseWheel);
  20. this.pictureBox1.MouseDown += new MouseEventHandler(pictureBox1_MouseDown);
  21. this.pictureBox1.MouseMove += new MouseEventHandler(pictureBox1_MouseMove);
  22. this.pictureBox1.MouseUp += new MouseEventHandler(pictureBox1_MouseUp);
  23. this.pictureBox1.Paint += new PaintEventHandler(pictureBox1_Paint);
  24. }
  25. public event MouseEventHandler PictureBoxMouseDown { add { this.pictureBox1.MouseDown += value; } remove { this.pictureBox1.MouseDown -= value; } }
  26. public event MouseEventHandler PictureBoxMouseUp { add { this.pictureBox1.MouseUp += value; } remove { this.pictureBox1.MouseUp -= value; } }
  27. public event MouseEventHandler PictureBoxMouseMove { add { this.pictureBox1.MouseMove += value; } remove { this.pictureBox1.MouseMove -= value; } }
  28. public DisplayTerrain Display
  29. {
  30. get { return display; }
  31. set
  32. {
  33. this.display = value;
  34. if (display != null)
  35. {
  36. if (display.Terrain != null)
  37. {
  38. float scale = Math.Max(
  39. ((float)pictureBox1.Width) / display.Terrain.TotalWidth,
  40. ((float)pictureBox1.Height) / display.Terrain.TotalHeight);
  41. this.display.setCameraScale(scale, scale);
  42. this.display.setCamera(display.Terrain.TotalWidth / 2, display.Terrain.TotalHeight / 2);
  43. }
  44. }
  45. }
  46. }
  47. public int FPS
  48. {
  49. get { return 1000 / timer1.Interval; }
  50. set { timer1.Interval = 1000 / value; }
  51. }
  52. public PictureBox GetPictureBox()
  53. {
  54. return pictureBox1;
  55. }
  56. private void DisplayTerrainPanel_Enter(object sender, EventArgs e)
  57. {
  58. pictureBox1.Focus();
  59. }
  60. private void timer1_Tick(object sender, EventArgs e)
  61. {
  62. if (Display != null)
  63. {
  64. Display.update(timer1.Interval);
  65. pictureBox1.Refresh();
  66. }
  67. }
  68. private Vector2 pic_lastMouesDown;
  69. private Vector2 pic_lastCameraPos;
  70. private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
  71. {
  72. pictureBox1.Focus();
  73. if (Display != null)
  74. {
  75. pic_lastMouesDown = new Vector2(e.Location.X, e.Location.Y);
  76. pic_lastCameraPos = new Vector2(Display.CameraX, Display.CameraY);
  77. }
  78. }
  79. private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
  80. {
  81. if (Display != null)
  82. {
  83. if (e.Button == System.Windows.Forms.MouseButtons.Right)
  84. {
  85. if (pic_lastMouesDown != null)
  86. {
  87. float x = pic_lastCameraPos.X + Display.screenToWorldSizeX(pic_lastMouesDown.X - e.X);
  88. float y = pic_lastCameraPos.Y + Display.screenToWorldSizeY(pic_lastMouesDown.Y - e.Y);
  89. Display.setCamera(x, y);
  90. }
  91. }
  92. }
  93. }
  94. private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
  95. {
  96. pic_lastMouesDown = null;
  97. pic_lastCameraPos = null;
  98. }
  99. private void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
  100. {
  101. if (Display != null)
  102. {
  103. int d = CMath.getDirect(e.Delta);
  104. if (d > 0)
  105. {
  106. float newD = Display.getCameraScale() * 1.1f;
  107. Display.setCameraScale(newD, newD);
  108. }
  109. else if (d < 0)
  110. {
  111. float newD = Display.getCameraScale() / 1.1f;
  112. Display.setCameraScale(newD, newD);
  113. }
  114. }
  115. }
  116. private void pictureBox1_Paint(object sender, PaintEventArgs e)
  117. {
  118. if (Display != null)
  119. {
  120. try
  121. {
  122. this.Display.setWindow(new RectangleF(0, 0, pictureBox1.Width, pictureBox1.Height));
  123. this.Display.render(e.Graphics);
  124. }
  125. catch(Exception err)
  126. {
  127. MessageBox.Show(err.Message);
  128. }
  129. }
  130. }
  131. }
  132. }