EditorDisplayPanel.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using CommonAI.Zone.ZoneEditor.Plugin.EditorToScene;
  9. using CommonAI.Zone.ZoneEditor.Plugin.SceneToEditor;
  10. using CommonAI.Zone;
  11. using CommonAI.Zone.ZoneEditor;
  12. using CommonAI.RTS; using CommonLang.Vector;
  13. using System.Collections;
  14. using CommonAI.Zone.Instance;
  15. using CommonLang;
  16. using GameEditorPlugin.Win32;
  17. using CommonFroms.Drawing;
  18. namespace GameEditorPlugin.Win32.Editor
  19. {
  20. public abstract partial class EditorDisplayPanel : UserControl
  21. {
  22. public static bool ShowUnit = false;
  23. public static bool ShowRegion = false;
  24. public static bool ShowItem = false;
  25. public static bool ShowPoint = false;
  26. public static bool ShowDecoration = false;
  27. public static bool ShowArea = false;
  28. public static bool ShowAll
  29. {
  30. get { return !(ShowUnit || ShowItem || ShowDecoration || ShowRegion || ShowPoint || ShowArea); }
  31. }
  32. //---------------------------------------------------------------------------
  33. private EditorWorld world;
  34. public bool ShowGrid { get; private set; }
  35. private ToolStripButton[] btns_ShowGroup;
  36. public EditorDisplayPanel()
  37. {
  38. InitializeComponent();
  39. this.btns_ShowGroup = new ToolStripButton[] { btn_ShowUnit, btn_ShowItem, btn_ShowDecoration, btn_ShowPoint, btn_ShowRegion, btn_ShowArea};
  40. this.pictureBox1.KeyDown += PictureBox1_KeyDown;
  41. this.pictureBox1.KeyUp += PictureBox1_KeyUp;
  42. this.pictureBox1.MouseWheel += new MouseEventHandler(pictureBox1_MouseWheel);
  43. }
  44. public void SetSceneData(MsgSetScene data)
  45. {
  46. try
  47. {
  48. if (world != null)
  49. {
  50. this.world.ResetTerrain(data.Data);
  51. }
  52. else
  53. {
  54. this.world = new EditorWorld(this, data.Data);
  55. float scale = Math.Max(
  56. ((float)pictureBox1.Width) / world.Width,
  57. ((float)pictureBox1.Height) / world.Height);
  58. world.setCameraScale(scale, scale);
  59. }
  60. }
  61. catch (Exception err)
  62. {
  63. MessageBox.Show(err.Message);
  64. }
  65. }
  66. private void Win32EditorPanel_Load(object sender, EventArgs e)
  67. {
  68. this.SendToEditor(new RspEditorState());
  69. btn_ShowUnit.Checked = ShowUnit;
  70. btn_ShowItem.Checked = ShowItem;
  71. btn_ShowDecoration.Checked = ShowDecoration;
  72. btn_ShowRegion.Checked = ShowRegion;
  73. btn_ShowPoint.Checked = ShowPoint;
  74. refresh_Show();
  75. }
  76. //---------------------------------------------------------------------------
  77. #region Abstract
  78. abstract public void SendToEditor(object data);
  79. abstract protected T CallAddObjectData<T>(Action<T> callback) where T : SceneObjectData;
  80. abstract protected T CallResetObjectData<T>(string name, Action<T> callback) where T : SceneObjectData;
  81. #endregion
  82. //---------------------------------------------------------------------------
  83. #region ShowObject
  84. private void btn_Show_Click(object sender, EventArgs e)
  85. {
  86. foreach (ToolStripButton btn in btns_ShowGroup)
  87. {
  88. if (sender != btn)
  89. {
  90. btn.Checked = false;
  91. }
  92. }
  93. }
  94. private void btn_Show_CheckedChanged(object sender, EventArgs e)
  95. {
  96. pictureBox1.Refresh();
  97. }
  98. private void refresh_Show()
  99. {
  100. ShowUnit = btn_ShowUnit.Checked;
  101. ShowItem = btn_ShowItem.Checked;
  102. ShowDecoration = btn_ShowDecoration.Checked;
  103. ShowRegion = btn_ShowRegion.Checked;
  104. ShowPoint = btn_ShowPoint.Checked;
  105. ShowArea = btn_ShowArea.Checked;
  106. if (world.SelectedObject != null && !ShowAll && !world.SelectedObject.Pickable)
  107. {
  108. world.Deselect(world.SelectedObject);
  109. }
  110. }
  111. #endregion
  112. //---------------------------------------------------------------------------
  113. #region Message
  114. internal MsgSetTerrainBrush LastBrush = new MsgSetTerrainBrush();
  115. internal MsgSetEditorMode LastMode = new MsgSetEditorMode();
  116. virtual public void OnMsgReceived(object data)
  117. {
  118. if (data is MsgSetScene)
  119. {
  120. SetSceneData(data as MsgSetScene);
  121. }
  122. else if (data is MsgPutUnit)
  123. {
  124. world.AddUnit(data as MsgPutUnit);
  125. }
  126. else if (data is MsgPutItem)
  127. {
  128. world.AddItem(data as MsgPutItem);
  129. }
  130. else if (data is MsgPutPoint)
  131. {
  132. world.AddPoint(data as MsgPutPoint);
  133. }
  134. else if (data is MsgPutRegion)
  135. {
  136. world.AddRegion(data as MsgPutRegion);
  137. }
  138. else if (data is MsgPutDecoration)
  139. {
  140. world.AddDecoration(data as MsgPutDecoration);
  141. }
  142. else if (data is MsgPutArea)
  143. {
  144. world.AddArea(data as MsgPutArea);
  145. }
  146. else if (data is MsgRemoveObject)
  147. {
  148. world.RemoveObject(data as MsgRemoveObject);
  149. }
  150. else if (data is MsgRenameObject)
  151. {
  152. world.RenameObject(data as MsgRenameObject);
  153. }
  154. else if (data is MsgSelectObject)
  155. {
  156. world.SelectObject(data as MsgSelectObject);
  157. }
  158. else if (data is MsgShowTerrain)
  159. {
  160. ShowGrid = (data as MsgShowTerrain).Show;
  161. }
  162. else if (data is MsgLocateCamera)
  163. {
  164. MsgLocateCamera msg = data as MsgLocateCamera;
  165. world.setCamera(msg.X, msg.Y);
  166. rsp_CameraChanged();
  167. }
  168. else if (data is MsgSetTerrainBrush)
  169. {
  170. this.LastBrush = data as MsgSetTerrainBrush;
  171. this.LastBrush.Size = Math.Max(1, LastBrush.Size);
  172. }
  173. else if (data is MsgSetEditorMode)
  174. {
  175. this.LastMode = data as MsgSetEditorMode;
  176. }
  177. pictureBox1.Refresh();
  178. }
  179. void rsp_CameraChanged()
  180. {
  181. RspCameraChanged rsp = new RspCameraChanged();
  182. rsp.X = world.CameraX;
  183. rsp.Y = world.CameraY;
  184. rsp.W = world.screenToWorldSizeX(pictureBox1.Width);
  185. rsp.H = world.screenToWorldSizeY(pictureBox1.Height);
  186. SendToEditor(rsp);
  187. }
  188. void rsp_ObjectPositionChanged(EditorObject u)
  189. {
  190. RspObjectPositionChanged rsp = new RspObjectPositionChanged();
  191. rsp.Name = u.Name;
  192. rsp.x = u.X;
  193. rsp.y = u.Y;
  194. SendToEditor(rsp);
  195. }
  196. void rsp_RspObjectDirectionChanged(EditorObject u)
  197. {
  198. RspObjectDirectionChanged rsp = new RspObjectDirectionChanged();
  199. rsp.Name = u.Name;
  200. rsp.dir = u.Direction;
  201. SendToEditor(rsp);
  202. }
  203. void rsp_FillTerrain()
  204. {
  205. List<RspZoneFlagChanged> changed = world.PopFillTerrainStack();
  206. if (changed.Count > 0)
  207. {
  208. RspZoneFlagBathChanged bath = new RspZoneFlagBathChanged();
  209. bath.Flags = changed;
  210. SendToEditor(bath);
  211. }
  212. }
  213. void rsp_RspTerrainBrushChanged()
  214. {
  215. RspTerrainBrushChanged changed = new RspTerrainBrushChanged();
  216. changed.Size = LastBrush.Size;
  217. SendToEditor(changed);
  218. }
  219. #endregion
  220. //---------------------------------------------------------------------------
  221. #region Render
  222. private void pictureBox1_Paint(object sender, PaintEventArgs e)
  223. {
  224. refresh_Show();
  225. world.setWindow(new RectangleF(0, 0, pictureBox1.Width, pictureBox1.Height));
  226. world.render(e.Graphics);
  227. if (LastMode.Mode == MsgSetEditorMode.MODE_TERRAIN)
  228. {
  229. draw_mode_terrain(e);
  230. }
  231. else if (LastMode.Mode == MsgSetEditorMode.MODE_OBJECT)
  232. {
  233. draw_mode_object(e);
  234. }
  235. }
  236. private void draw_mode_terrain(PaintEventArgs e)
  237. {
  238. Point pp = pictureBox1.PointToClient(Control.MousePosition);
  239. Pen pen = new Pen(Color.White);
  240. float sizeW = world.worldToScreenSizeX(LastBrush.Size * world.CellW);
  241. float sizeH = world.worldToScreenSizeY(LastBrush.Size * world.CellH);
  242. if (LastBrush.Brush == MsgSetTerrainBrush.BrushType.Round)
  243. {
  244. e.Graphics.DrawEllipse(pen, pp.X - sizeW / 2, pp.Y - sizeH / 2, sizeW, sizeH);
  245. }
  246. else if (LastBrush.Brush == MsgSetTerrainBrush.BrushType.Rectangle)
  247. {
  248. e.Graphics.DrawRectangle(pen, pp.X - sizeW / 2, pp.Y - sizeH / 2, sizeW, sizeH);
  249. }
  250. if (pic_lastMouesPos != null)
  251. {
  252. float wx = world.screenToWorldX(pic_lastMouesPos.X);
  253. float wy = world.screenToWorldY(pic_lastMouesPos.Y);
  254. float ww = world.CellW;
  255. float wh = world.CellH;
  256. Brush brush = new SolidBrush(Color.FromArgb(0x20, Color.White));
  257. world.drawInWorldSpace(e.Graphics, (g, bounds) =>
  258. {
  259. if (LastBrush.Brush == MsgSetTerrainBrush.BrushType.Round)
  260. {
  261. world.ForEachTerrainRound(wx, wy, LastBrush.Size, (t, ix, iy) =>
  262. {
  263. g.FillRectangle(brush, ix * ww, iy * wh, ww, wh);
  264. });
  265. }
  266. else if (LastBrush.Brush == MsgSetTerrainBrush.BrushType.Rectangle)
  267. {
  268. world.ForEachTerrainRectangle(wx, wy, LastBrush.Size, LastBrush.Size, (t, ix, iy) =>
  269. {
  270. g.FillRectangle(brush, ix * ww, iy * wh, ww, wh);
  271. });
  272. }
  273. });
  274. }
  275. float sy = pictureBox1.Height - 1;
  276. DrawStringLeftBottom(e.Graphics, "Page Up 扩大笔刷", true, ref sy);
  277. DrawStringLeftBottom(e.Graphics, "Page Down 缩小笔刷", true, ref sy);
  278. DrawStringLeftBottom(e.Graphics, "按住 Shift 抹去地块", CommonFroms.Keyboard.IsShiftDown, ref sy);
  279. }
  280. private void draw_mode_object(PaintEventArgs e)
  281. {
  282. if (pic_lastMouesPos != null)
  283. {
  284. float wx = world.screenToWorldX(pic_lastMouesPos.X);
  285. float wy = world.screenToWorldY(pic_lastMouesPos.Y);
  286. e.Graphics.DrawString(string.Format("Mouse=({0} , {1})",
  287. wx.ToString("#0.0"),
  288. wy.ToString("#0.0")),
  289. this.Font, tip_brush_h, 0, 1);
  290. }
  291. float sy = pictureBox1.Height - 1;
  292. if (world.SelectedObject != null)
  293. {
  294. DrawStringLeftBottom(e.Graphics, "按住 Shift 调整坐标", CommonFroms.Keyboard.IsShiftDown, ref sy);
  295. if (world.SelectedObject.IsDirectionality)
  296. DrawStringLeftBottom(e.Graphics, "按住 Ctrl 调整角度", CommonFroms.Keyboard.IsCtrlDown, ref sy);
  297. }
  298. if (ShowPoint)
  299. {
  300. DrawStringLeftBottom(e.Graphics, "按住 Ctrl 点击添加路点", CommonFroms.Keyboard.IsCtrlDown & !CommonFroms.Keyboard.IsAltDown, ref sy);
  301. DrawStringLeftBottom(e.Graphics, "按住 Ctrl+Alt 点击添加并连接路点", CommonFroms.Keyboard.IsCtrlDown & CommonFroms.Keyboard.IsAltDown, ref sy);
  302. DrawStringLeftBottom(e.Graphics, "按住 Alt 点击链接/解开路点", !CommonFroms.Keyboard.IsCtrlDown & CommonFroms.Keyboard.IsAltDown, ref sy);
  303. }
  304. }
  305. private Brush tip_brush_h = new SolidBrush(Color.White);
  306. private Brush tip_brush_t = new SolidBrush(Color.FromArgb(0xFF, 0xA0, 0xA0, 0xA0));
  307. private void DrawStringLeftBottom(Graphics g, string text, bool enable, ref float sy)
  308. {
  309. SizeF size = g.MeasureString(text, this.Font);
  310. g.DrawString(text, this.Font, enable ? tip_brush_h : tip_brush_t, 0, sy - size.Height);
  311. sy -= size.Height + 1;
  312. }
  313. #endregion
  314. //---------------------------------------------------------------------------
  315. #region Mouse
  316. private void PictureBox1_KeyDown(object sender, KeyEventArgs e)
  317. {
  318. pictureBox1.Focus();
  319. pictureBox1.Refresh();
  320. if (LastMode.Mode == MsgSetEditorMode.MODE_TERRAIN)
  321. {
  322. key_down_mode_terrain(e);
  323. }
  324. }
  325. private void PictureBox1_KeyUp(object sender, KeyEventArgs e)
  326. {
  327. pictureBox1.Focus();
  328. pictureBox1.Refresh();
  329. }
  330. private void key_down_mode_terrain(KeyEventArgs e)
  331. {
  332. if (e.KeyCode == Keys.PageUp)
  333. {
  334. LastBrush.Size++;
  335. LastBrush.Size = Math.Max(1, LastBrush.Size);
  336. rsp_RspTerrainBrushChanged();
  337. }
  338. else if (e.KeyCode == Keys.PageDown)
  339. {
  340. LastBrush.Size--;
  341. LastBrush.Size = Math.Max(1, LastBrush.Size);
  342. rsp_RspTerrainBrushChanged();
  343. }
  344. }
  345. private Vector2 pic_lastMouesDown;
  346. private Vector2 pic_lastMouesPos;
  347. private Vector2 pic_lastCameraPos;
  348. private EditorObject pic_lastPickObj;
  349. private Vector2 pic_lastPickObjOffset;
  350. private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
  351. {
  352. pictureBox1.Focus();
  353. pic_lastMouesPos = new Vector2(e.X, e.Y);
  354. if (e.Button == System.Windows.Forms.MouseButtons.Right)
  355. {
  356. pic_lastMouesDown = new Vector2(e.X, e.Y);
  357. pic_lastCameraPos = new Vector2(world.CameraX, world.CameraY);
  358. }
  359. else if (e.Button == System.Windows.Forms.MouseButtons.Left)
  360. {
  361. if (LastMode.Mode == MsgSetEditorMode.MODE_OBJECT)
  362. {
  363. float wx = world.screenToWorldX(e.X);
  364. float wy = world.screenToWorldY(e.Y);
  365. if (ShowPoint && CommonFroms.Keyboard.IsCtrlDown & CommonFroms.Keyboard.IsAltDown)
  366. {
  367. do_add_and_link_point(wx, wy);
  368. }
  369. else if (ShowPoint && CommonFroms.Keyboard.IsCtrlDown)
  370. {
  371. do_add_point(wx, wy);
  372. }
  373. else if (ShowPoint && CommonFroms.Keyboard.IsAltDown)
  374. {
  375. do_link_point(wx, wy);
  376. }
  377. else
  378. {
  379. pic_lastPickObj = world.PickObject(wx, wy);
  380. if (pic_lastPickObj != null)
  381. {
  382. pic_lastPickObjOffset = new Vector2(
  383. wx - pic_lastPickObj.X,
  384. wy - pic_lastPickObj.Y);
  385. }
  386. }
  387. }
  388. else if (LastMode.Mode == MsgSetEditorMode.MODE_TERRAIN)
  389. {
  390. mouse_fill_terrain(e);
  391. }
  392. }
  393. pictureBox1.Refresh();
  394. }
  395. private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
  396. {
  397. pic_lastMouesPos = new Vector2(e.X, e.Y);
  398. if (e.Button == System.Windows.Forms.MouseButtons.Right)
  399. {
  400. if (pic_lastMouesDown != null)
  401. {
  402. float x = pic_lastCameraPos.X + world.screenToWorldSizeX(pic_lastMouesDown.X - e.X);
  403. float y = pic_lastCameraPos.Y + world.screenToWorldSizeY(pic_lastMouesDown.Y - e.Y);
  404. world.setCamera(x, y);
  405. rsp_CameraChanged();
  406. }
  407. }
  408. else if (e.Button == System.Windows.Forms.MouseButtons.Left)
  409. {
  410. if (LastMode.Mode == MsgSetEditorMode.MODE_OBJECT)
  411. {
  412. if (e.Button == System.Windows.Forms.MouseButtons.Left)
  413. {
  414. if (pic_lastPickObj != null)
  415. {
  416. float wx = world.screenToWorldX(e.X);
  417. float wy = world.screenToWorldY(e.Y);
  418. pic_lastPickObj.SetPos(
  419. wx - pic_lastPickObjOffset.X,
  420. wy - pic_lastPickObjOffset.Y);
  421. rsp_ObjectPositionChanged(pic_lastPickObj);
  422. }
  423. }
  424. }
  425. else if (LastMode.Mode == MsgSetEditorMode.MODE_TERRAIN)
  426. {
  427. mouse_fill_terrain(e);
  428. }
  429. }
  430. else
  431. {
  432. if (LastMode.Mode == MsgSetEditorMode.MODE_OBJECT)
  433. {
  434. if (CommonFroms.Keyboard.IsCtrlDown)
  435. {
  436. var selected = world.SelectedObject;
  437. if (selected != null)
  438. {
  439. float wx = world.screenToWorldX(e.X);
  440. float wy = world.screenToWorldY(e.Y);
  441. selected.Direction = (MathVector.getDegree(wx - selected.X, wy - selected.Y));
  442. rsp_RspObjectDirectionChanged(selected);
  443. }
  444. }
  445. else if (CommonFroms.Keyboard.IsShiftDown)
  446. {
  447. var selected = world.SelectedObject;
  448. if (selected != null)
  449. {
  450. float wx = world.screenToWorldX(e.X);
  451. float wy = world.screenToWorldY(e.Y);
  452. selected.SetPos(wx, wy);
  453. rsp_ObjectPositionChanged(selected);
  454. }
  455. }
  456. }
  457. }
  458. pictureBox1.Refresh();
  459. }
  460. private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
  461. {
  462. pic_lastMouesDown = null;
  463. pic_lastCameraPos = null;
  464. pic_lastPickObj = null;
  465. pic_lastPickObjOffset = null;
  466. rsp_FillTerrain();
  467. pictureBox1.Refresh();
  468. }
  469. private void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
  470. {
  471. try
  472. {
  473. int d = CMath.getDirect(e.Delta);
  474. if (d > 0)
  475. {
  476. float newD = world.getCameraScale() * 1.1f;
  477. world.setCameraScale(newD, newD);
  478. rsp_CameraChanged();
  479. }
  480. else if (d < 0)
  481. {
  482. float newD = world.getCameraScale() / 1.1f;
  483. world.setCameraScale(newD, newD);
  484. rsp_CameraChanged();
  485. }
  486. }
  487. catch (Exception err)
  488. {
  489. Console.WriteLine(err.Message);
  490. }
  491. pictureBox1.Refresh();
  492. }
  493. private void do_add_point(float wx, float wy)
  494. {
  495. var add = CallAddObjectData<PointData>((t) =>
  496. {
  497. t.X = wx;
  498. t.Y = wy;
  499. });
  500. if (add != null)
  501. {
  502. world.PickObject(add.Name);
  503. }
  504. }
  505. private void do_link_point(float wx, float wy)
  506. {
  507. if (world.SelectedObject is EditorPoint)
  508. {
  509. var next = world.TryPickObject(wx, wy);
  510. if (next != null)
  511. {
  512. var selected = CallResetObjectData<PointData>(world.SelectedObject.Name, (prev) =>
  513. {
  514. if (next.Name != prev.Name)
  515. {
  516. if (prev.NextNames.Contains(next.Name))
  517. {
  518. prev.NextNames.Remove(next.Name);
  519. }
  520. else
  521. {
  522. prev.NextNames.Add(next.Name);
  523. }
  524. }
  525. });
  526. world.PickObject(next.Name);
  527. }
  528. }
  529. }
  530. private void do_add_and_link_point(float wx, float wy)
  531. {
  532. var prev = world.SelectedObject;
  533. var add = CallAddObjectData<PointData>((t) =>
  534. {
  535. t.X = wx;
  536. t.Y = wy;
  537. });
  538. if (add != null)
  539. {
  540. if (prev is EditorPoint)
  541. {
  542. CallResetObjectData<PointData>(prev.Name, (p) =>
  543. {
  544. p.NextNames.Add(add.Name);
  545. });
  546. }
  547. world.PickObject(add.Name);
  548. }
  549. }
  550. private void mouse_fill_terrain(MouseEventArgs e)
  551. {
  552. int value = LastBrush.ARGB;
  553. if (CommonFroms.Keyboard.IsShiftDown)
  554. {
  555. value = 0;
  556. }
  557. float wx = world.screenToWorldX(e.X);
  558. float wy = world.screenToWorldY(e.Y);
  559. if (LastBrush.Brush == MsgSetTerrainBrush.BrushType.Round)
  560. {
  561. world.FillTerrainRound(wx, wy, LastBrush.Size, value);
  562. }
  563. else if (LastBrush.Brush == MsgSetTerrainBrush.BrushType.Rectangle)
  564. {
  565. world.FillTerrainRectangle(wx, wy, LastBrush.Size, LastBrush.Size, value);
  566. }
  567. rsp_FillTerrain();
  568. }
  569. #endregion
  570. //---------------------------------------------------------------------------
  571. }
  572. }