SceneEditor.cs 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using CommonAI.Zone.ZoneEditor;
  9. using CommonFroms.Utils;
  10. using System.IO;
  11. using CommonAI.Zone;
  12. using CommonAI.RTS;
  13. using CommonLang.Vector;
  14. using CommonAI.Zone.ZoneEditor.Plugin.EditorToScene;
  15. using CommonAI.Zone.ZoneEditor.Plugin.SceneToEditor;
  16. using CommonLang;
  17. using CommonLang.Xml;
  18. using System.Diagnostics;
  19. using CommonAIEditor.Plugins.Win32;
  20. using CommonFroms;
  21. using CommonAI.ZoneEditor;
  22. using GameEditorPlugin.Win32.Runtime;
  23. using GameEditorPlugin.Win32;
  24. using CommonFroms.G2D;
  25. using CommonFroms.G2D.DataGrid;
  26. using GameEditorPlugin.Tools;
  27. using System.Xml;
  28. using CommonFroms.Drawing;
  29. namespace CommonAIEditor.Scene
  30. {
  31. public partial class SceneEditor : Form
  32. {
  33. public delegate void SaveHandler(SceneEditor editor, G2DTreeNode<SceneData> node);
  34. public event SaveHandler OnSaved;
  35. public SceneData Data { get { return mData; } }
  36. private G2DTreeNode<SceneData> mNode;
  37. private SceneData mData;
  38. private ZoneInfo mZoneData;
  39. private ISceneEditorPlugin mPlugin;
  40. private G2DTreeNodeGroup treeRoot;
  41. private EditorMiniMap displayWorld;
  42. private RightMouseFilter rightFilter;
  43. private SceneEventEditor eventEditor;
  44. private SceneVarEditor varEditor;
  45. public SceneEditor(DataManagerPanel<SceneData> owner, G2DTreeNode<SceneData> node, ISceneEditorPlugin plugin)
  46. {
  47. SceneData data = node.Data;
  48. this.tree_setting_file = new FileInfo(owner.TreeRoot.SettingDir + "\\" + data.ID + ".tree");
  49. InitializeComponent();
  50. this.Text = "场景: " + data.ToString();
  51. this.DoubleBuffered = true;
  52. this.splitContainer1.Enabled = false;
  53. this.toolStrip1.Enabled = false;
  54. // tree and objects //
  55. {
  56. this.propertyGrid1.PropertyTabs.AddTabType(
  57. typeof(System.Windows.Forms.Design.EventsTab),
  58. PropertyTabScope.Global);
  59. this.treeView1.ImageList = Editor.Instance.Icons;
  60. this.treeRoot = new G2DTreeNodeGroup("所有单位");
  61. this.treeRoot.ImageKey = "icons_tool_bar2.png";
  62. this.treeRoot.SelectedImageKey = "icons_tool_bar2.png";
  63. this.treeRoot.ContextMenuStrip = groupMenuStrip;
  64. this.treeView1.Nodes.Add(treeRoot);
  65. }
  66. this.mNode = node;
  67. this.mData = data;
  68. this.mZoneData = data.ZoneData;
  69. this.displayWorld = new EditorMiniMap(treeRoot, mZoneData);
  70. // init plugin //
  71. {
  72. this.mPlugin = plugin;
  73. this.mPlugin.OnGetPluginMessage += new PluginMessageHandler(OnPluginMessageHandler);
  74. this.mPlugin.AsControl().Dock = DockStyle.Fill;
  75. this.mPlugin.CallAddObject += call_AddObject;
  76. this.mPlugin.CallResetObject += call_ResetObject;
  77. this.panel1.Controls.Add(mPlugin.AsControl());
  78. }
  79. // map define //
  80. {
  81. var brush = Editor.Instance.DefaultTerrainBrush;
  82. if (brush != null)
  83. {
  84. toolStripButton_Color.BackColor = Color.FromArgb(brush.Value);
  85. toolStripButton_Color.Text = brush.Name.Length > 0 ? brush.Name.Substring(0, 1) : "";
  86. }
  87. btn_BrushRect.Tag = MsgSetTerrainBrush.BrushType.Rectangle;
  88. btn_BrushRound.Tag = MsgSetTerrainBrush.BrushType.Round;
  89. btn_Brush.Tag = btn_BrushRound;
  90. }
  91. this.rightFilter = new RightMouseFilter(mPlugin.AsControl());
  92. if (!mPlugin.EnableRight)
  93. {
  94. Application.AddMessageFilter(rightFilter);
  95. }
  96. }
  97. class RightMouseFilter : IMessageFilter
  98. {
  99. const int WM_RBUTTONDBLCLK = 0x206;
  100. const int WM_RBUTTONDOWN = 0x204;
  101. const int WM_RBUTTONUP = 0x205;
  102. private Control component;
  103. public RightMouseFilter(Control component)
  104. {
  105. this.component = component;
  106. }
  107. public bool PreFilterMessage(ref System.Windows.Forms.Message m)
  108. {
  109. switch (m.Msg)
  110. {
  111. case WM_RBUTTONDBLCLK:
  112. case WM_RBUTTONDOWN:
  113. case WM_RBUTTONUP:
  114. {
  115. Point pt = component.PointToClient(Control.MousePosition);
  116. if (component.TopLevelControl.Focused)
  117. {
  118. if (component.Bounds.Contains(pt))
  119. return true; //返回true则消息被裁取,系统不再处理
  120. }
  121. }
  122. break;
  123. default:
  124. return false;    //返回false则消息未被裁取,系统会处理
  125. }
  126. return false;
  127. }
  128. }
  129. private void SceneEditor_FormClosing(object sender, FormClosingEventArgs e)
  130. {
  131. if (eventEditor != null)
  132. {
  133. eventEditor.Save();
  134. eventEditor.Close();
  135. eventEditor = null;
  136. }
  137. if (varEditor != null)
  138. {
  139. varEditor.Save();
  140. varEditor.Close();
  141. varEditor = null;
  142. }
  143. System.Windows.Forms.DialogResult result = MessageBox.Show(this,
  144. "关闭前保存场景?",
  145. "确认关闭?",
  146. MessageBoxButtons.YesNoCancel);
  147. if (result == System.Windows.Forms.DialogResult.Yes)
  148. {
  149. SaveToScene();
  150. Application.RemoveMessageFilter(rightFilter);
  151. }
  152. else if (result == System.Windows.Forms.DialogResult.No)
  153. {
  154. Application.RemoveMessageFilter(rightFilter);
  155. }
  156. else
  157. {
  158. e.Cancel = true;
  159. }
  160. }
  161. private void LaunchVarEdit()
  162. {
  163. if (varEditor == null)
  164. {
  165. varEditor = new SceneVarEditor(this);
  166. varEditor.FormClosed += new FormClosedEventHandler(
  167. (object sender, FormClosedEventArgs e) =>
  168. {
  169. varEditor.Save();
  170. varEditor = null;
  171. }
  172. );
  173. varEditor.Show();
  174. }
  175. else
  176. {
  177. FormUtils.SwithToThisForm(varEditor, true);
  178. }
  179. }
  180. private void LaunchEventEdit()
  181. {
  182. if (eventEditor == null)
  183. {
  184. eventEditor = new SceneEventEditor(this);
  185. eventEditor.OnDataLoaded += (editor) =>
  186. {
  187. LoadEventsTreeInfo(editor as SceneEventEditor);
  188. };
  189. eventEditor.OnDataSaved += (editor) =>
  190. {
  191. SaveEventsTreeInfo(editor as SceneEventEditor);
  192. };
  193. eventEditor.Text = "事件列表";
  194. eventEditor.FormClosed += new FormClosedEventHandler(
  195. (object sender, FormClosedEventArgs e) =>
  196. {
  197. eventEditor.Save();
  198. eventEditor = null;
  199. }
  200. );
  201. eventEditor.Show();
  202. }
  203. else
  204. {
  205. FormUtils.SwithToThisForm(eventEditor, true);
  206. }
  207. }
  208. //----------------------------------------------------------------------------
  209. #region SaveLoad
  210. private readonly FileInfo tree_setting_file;
  211. private void LoadScene()
  212. {
  213. this.numericCellW.Value = mData.Terrain.GridCellW;
  214. this.numericCellH.Value = mData.Terrain.GridCellH;
  215. this.numericXCount.Value = mData.Terrain.XCount;
  216. this.numericYCount.Value = mData.Terrain.YCount;
  217. post_MsgSetScene(mData);
  218. foreach (PointData tn in mData.Points)
  219. {
  220. LoadSceneObject(tn);
  221. }
  222. foreach (RegionData tn in mData.Regions)
  223. {
  224. LoadSceneObject(tn);
  225. }
  226. foreach (UnitData tn in mData.Units)
  227. {
  228. LoadSceneObject(tn);
  229. }
  230. foreach (DecorationData tn in mData.Decorations)
  231. {
  232. LoadSceneObject(tn);
  233. }
  234. foreach (ItemData tn in mData.Items)
  235. {
  236. LoadSceneObject(tn);
  237. }
  238. foreach (AreaData tn in mData.Areas)
  239. {
  240. LoadSceneObject(tn);
  241. }
  242. post_MsgShowTerrain();
  243. post_MsgSetEditorMode(MsgSetEditorMode.MODE_OBJECT);
  244. post_MsgSetTerrainBrush();
  245. post_MsgLocateCamera(
  246. displayWorld.Width / 2,
  247. displayWorld.Height / 2);
  248. LoadTreeInfo(tree_setting_file);
  249. treeRoot.Expand();
  250. }
  251. private void SaveToScene()
  252. {
  253. if (!bLoadScene) return;
  254. mData.SetTerrain(mZoneData);
  255. mData.Points = new List<PointData>();
  256. mData.Regions = new List<RegionData>();
  257. mData.Units = new List<UnitData>();
  258. mData.Decorations = new List<DecorationData>();
  259. mData.Items = new List<ItemData>();
  260. mData.Areas = new List<AreaData>();
  261. foreach (TreeNode tn in treeRoot.GetAllNodes())
  262. {
  263. if (tn is ObjectTreeNode)
  264. {
  265. ObjectTreeNode otn = tn as ObjectTreeNode;
  266. otn.Data.SavePath = treeRoot.GetSavePath(otn);
  267. if (otn.Data is PointData)
  268. {
  269. mData.Points.Add(otn.Data as PointData);
  270. }
  271. else if (otn.Data is RegionData)
  272. {
  273. mData.Regions.Add(otn.Data as RegionData);
  274. }
  275. else if (otn.Data is DecorationData)
  276. {
  277. mData.Decorations.Add(otn.Data as DecorationData);
  278. }
  279. else if (otn.Data is UnitData)
  280. {
  281. mData.Units.Add(otn.Data as UnitData);
  282. }
  283. else if (otn.Data is ItemData)
  284. {
  285. mData.Items.Add(otn.Data as ItemData);
  286. }
  287. else if (otn.Data is AreaData)
  288. {
  289. mData.Areas.Add(otn.Data as AreaData);
  290. }
  291. }
  292. }
  293. if (eventEditor != null)
  294. {
  295. eventEditor.Save();
  296. }
  297. if (varEditor != null)
  298. {
  299. varEditor.Save();
  300. }
  301. if (OnSaved != null)
  302. {
  303. OnSaved.Invoke(this, mNode);
  304. }
  305. SaveTreeInfo(tree_setting_file);
  306. }
  307. private XmlDocument tree_xml_status;
  308. private XmlElement events_xml_status;
  309. private XmlElement objects_xml_status;
  310. private void LoadTreeInfo(FileInfo file)
  311. {
  312. try
  313. {
  314. var xmltext = File.ReadAllText(file.FullName, CUtils.UTF8);
  315. tree_xml_status = XmlUtil.FromString(xmltext);
  316. }
  317. catch (Exception)
  318. {
  319. tree_xml_status = new XmlDocument();
  320. tree_xml_status.AppendChild(tree_xml_status.CreateElement("root"));
  321. }
  322. var eroot = tree_xml_status.DocumentElement;
  323. objects_xml_status = XmlUtil.FindChild(eroot, "objects") as XmlElement;
  324. if (objects_xml_status != null)
  325. {
  326. G2DTreeNodeGroup.SetTreeInfo(objects_xml_status as XmlElement, treeRoot);
  327. }
  328. else
  329. {
  330. objects_xml_status = tree_xml_status.CreateElement("objects");
  331. eroot.AppendChild(objects_xml_status);
  332. }
  333. events_xml_status = XmlUtil.FindChild(eroot, "events") as XmlElement;
  334. if (events_xml_status == null)
  335. {
  336. events_xml_status = tree_xml_status.CreateElement("events");
  337. eroot.AppendChild(events_xml_status);
  338. }
  339. }
  340. private void SaveTreeInfo(FileInfo file)
  341. {
  342. try
  343. {
  344. objects_xml_status.RemoveAll();
  345. G2DTreeNodeGroup.GetTreeInfo(objects_xml_status, treeRoot);
  346. var xmltext = XmlUtil.ToString(tree_xml_status);
  347. File.WriteAllText(file.FullName, xmltext, CUtils.UTF8);
  348. }
  349. catch (Exception) { }
  350. }
  351. private void LoadEventsTreeInfo(SceneEventEditor editor)
  352. {
  353. editor.LoadTreeInfo(events_xml_status);
  354. }
  355. private void SaveEventsTreeInfo(SceneEventEditor editor)
  356. {
  357. events_xml_status.RemoveAll();
  358. editor.SaveTreeInfo(events_xml_status);
  359. }
  360. #endregion
  361. //----------------------------------------------------------------------------
  362. private void ChangeSceneTerrain()
  363. {
  364. try
  365. {
  366. ZoneInfo old_info = mData.ZoneData;
  367. {
  368. int cellW = (int)(numericCellW.Value);
  369. int cellH = (int)(numericCellH.Value);
  370. int xCount = (int)(numericXCount.Value);
  371. int yCount = (int)(numericYCount.Value);
  372. ZoneInfo new_info = new ZoneInfo(xCount, yCount, cellW, cellH);
  373. for (int x = 0; x < xCount && x < old_info.XCount; x++)
  374. {
  375. for (int y = 0; y < yCount && y < old_info.YCount; y++)
  376. {
  377. new_info.TerrainMatrix[x, y] = old_info.TerrainMatrix[x, y];
  378. }
  379. }
  380. mZoneData = new_info;
  381. mData.SetTerrain(new_info);
  382. this.displayWorld.ResetTerrain(mZoneData);
  383. }
  384. post_MsgSetScene(mData);
  385. }
  386. catch (Exception err)
  387. {
  388. MessageBox.Show(err.Message);
  389. }
  390. }
  391. //----------------------------------------------------------------------------
  392. #region Delegates
  393. //----------------------------------------------------------------------------
  394. private void tool_RunGameEmu_Click(object sender, EventArgs e)
  395. {
  396. SaveToScene();
  397. IGameEditorPlugin plugin = EditorPlugin.CurrentPlugin;
  398. plugin.RunLocalPlay(EditorPlugin.DefaultPlugin, new DirectoryInfo(Editor.DataDir), mData.ID, false);
  399. }
  400. private void tool_RunGamePlugin_Click(object sender, EventArgs e)
  401. {
  402. SaveToScene();
  403. try
  404. {
  405. IGameEditorPlugin plugin = EditorPlugin.CurrentPlugin;
  406. plugin.RunTest(EditorPlugin.DefaultPlugin, Editor.DataDir, mData.ID, Editor.GetTestActorTemplateID(mData, 0));
  407. }
  408. catch (Exception err)
  409. {
  410. MessageBox.Show(err.Message);
  411. }
  412. }
  413. private void btn_TestAstar_Click(object sender, EventArgs e)
  414. {
  415. SaveToScene();
  416. new FormAstar(Editor.DataDir_scenes, Editor.Instance.TerrainDefinition, mData).Show();
  417. }
  418. private void btn_ScriptEditor_Click(object sender, EventArgs e)
  419. {
  420. }
  421. private void btn_Events_Click(object sender, EventArgs e)
  422. {
  423. LaunchEventEdit();
  424. }
  425. private void btn_EnvVars_Click(object sender, EventArgs e)
  426. {
  427. LaunchVarEdit();
  428. }
  429. //----------------------------------------------------------------------------
  430. private SceneObjectData call_AddObject(Type type, Action<SceneObjectData> callback)
  431. {
  432. return AddSceneObject(type, callback);
  433. }
  434. private SceneObjectData call_ResetObject(string name, Action<SceneObjectData> callback)
  435. {
  436. var data = this.GetSceneObjectData(name);
  437. if (data != null)
  438. {
  439. callback.Invoke(data);
  440. RefreshObject(data);
  441. return data;
  442. }
  443. return null;
  444. }
  445. //----------------------------------------------------------------------------
  446. private void addUnitToolStripMenuItem_Click(object sender, EventArgs e)
  447. {
  448. AddSceneObject(typeof(UnitData));
  449. }
  450. private void addItem_MenuItem_Click(object sender, EventArgs e)
  451. {
  452. AddSceneObject(typeof(ItemData));
  453. }
  454. private void addRegionToolStripMenuItem_Click(object sender, EventArgs e)
  455. {
  456. AddSceneObject(typeof(RegionData));
  457. }
  458. private void addPointToolStripMenuItem_Click(object sender, EventArgs e)
  459. {
  460. AddSceneObject(typeof(PointData));
  461. }
  462. private void addDecoration_MenuItem_Click(object sender, EventArgs e)
  463. {
  464. AddSceneObject(typeof(DecorationData));
  465. }
  466. private void addArea_MenuItem_Click(object sender, EventArgs e)
  467. {
  468. AddSceneObject(typeof(AreaData));
  469. }
  470. private void addGroup_MenuItem_Click(object sender, EventArgs e)
  471. {
  472. AddSceneGroup();
  473. }
  474. private void copyGroup_MenuItem_Click(object sender, EventArgs e)
  475. {
  476. CopySelected();
  477. }
  478. private void pasteGroup_MenuItem_Click(object sender, EventArgs e)
  479. {
  480. Paste();
  481. }
  482. private void cloneGroup_MenuItem_Click(object sender, EventArgs e)
  483. {
  484. DuplicateSelected();
  485. }
  486. private void renameGroup_MenuItem_Click(object sender, EventArgs e)
  487. {
  488. RenameSceneGroup();
  489. }
  490. //----------------------------------------------------------------------------
  491. private void btn_Brush_Click(object sender, EventArgs e)
  492. {
  493. btn_Brush.Tag = sender;
  494. btn_Brush.Image = (sender as ToolStripMenuItem).Image;
  495. post_MsgSetTerrainBrush();
  496. }
  497. //----------------------------------------------------------------------------------
  498. private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
  499. {
  500. if (propertyGrid1.SelectedObject is G2DPropertyDescriptor)
  501. {
  502. G2DPropertyDescriptor prop = (G2DPropertyDescriptor)propertyGrid1.SelectedObject;
  503. if (prop.EditData is UnitData)
  504. {
  505. post_MsgPutUnit(prop.EditData as UnitData);
  506. }
  507. else if (prop.EditData is ItemData)
  508. {
  509. post_MsgPutItem(prop.EditData as ItemData);
  510. }
  511. else if (prop.EditData is RegionData)
  512. {
  513. post_MsgPutRegion(prop.EditData as RegionData);
  514. }
  515. else if (prop.EditData is PointData)
  516. {
  517. post_MsgPutPoint(prop.EditData as PointData);
  518. }
  519. else if (prop.EditData is DecorationData)
  520. {
  521. post_MsgPutDecoration(prop.EditData as DecorationData);
  522. }
  523. else if (prop.EditData is AreaData)
  524. {
  525. post_MsgPutArea(prop.EditData as AreaData);
  526. }
  527. ObjectTreeNode node = GetSelectedAsSceneObject();
  528. if (node != null)
  529. {
  530. node.RefreshData();
  531. }
  532. }
  533. }
  534. private void propertyGrid1_Click(object sender, EventArgs e)
  535. {
  536. }
  537. private void propertyGrid1_DragEnter(object sender, DragEventArgs e)
  538. {
  539. if (e.Data.GetDataPresent(typeof(ObjectTreeNode)))
  540. {
  541. e.Effect = DragDropEffects.Move;
  542. }
  543. else
  544. {
  545. e.Effect = DragDropEffects.None;
  546. }
  547. }
  548. private void propertyGrid1_DragOver(object sender, DragEventArgs e)
  549. {
  550. if (e.Data.GetDataPresent(typeof(ObjectTreeNode)))
  551. {
  552. e.Effect = DragDropEffects.Move;
  553. }
  554. else
  555. {
  556. e.Effect = DragDropEffects.None;
  557. }
  558. }
  559. private void propertyGrid1_DragDrop(object sender, DragEventArgs e)
  560. {
  561. ObjectTreeNode dragObjNode = (ObjectTreeNode)e.Data.GetData(typeof(ObjectTreeNode));
  562. if (propertyGrid1.SelectedObject != null && dragObjNode.Data is PointData)
  563. {
  564. PointData srcP = dragObjNode.Data as PointData;
  565. G2DPropertyDescriptor desc = propertyGrid1.SelectedObject as G2DPropertyDescriptor;
  566. if (desc.EditData is PointData)
  567. {
  568. PointData dstP = desc.EditData as PointData;
  569. if (!dstP.NextNames.Contains(srcP.Name))
  570. {
  571. dstP.NextNames.Add(srcP.Name);
  572. post_MsgPutPoint(dstP);
  573. propertyGrid1.Refresh();
  574. }
  575. }
  576. else if (desc.EditData is UnitData)
  577. {
  578. UnitData dstP = desc.EditData as UnitData;
  579. dstP.StartPointName = srcP.Name;
  580. propertyGrid1.Refresh();
  581. }
  582. }
  583. }
  584. //----------------------------------------------------------------------------------
  585. private void treeView1_ItemDrag(object sender, ItemDragEventArgs e)
  586. {
  587. treeView1.DoDragDrop(e.Item, DragDropEffects.Move);
  588. }
  589. private void treeView1_DragDrop(object sender, DragEventArgs e)
  590. {
  591. Point pos = treeView1.PointToClient(new Point(e.X, e.Y));
  592. TreeNode dropNode = this.treeView1.GetNodeAt(pos);
  593. G2DTreeNodeBase child_node = (G2DTreeNodeBase)e.Data.GetData(typeof(ObjectTreeNode));
  594. G2DTreeNodeBase group_node = (G2DTreeNodeBase)e.Data.GetData(typeof(G2DTreeNodeGroup));
  595. if (dropNode is G2DTreeNodeGroup)
  596. {
  597. if (child_node == null && group_node == null)
  598. {
  599. MessageBox.Show("error");
  600. }
  601. else if (child_node != null)
  602. {
  603. child_node.RemoveFromParent();
  604. dropNode.Nodes.Add(child_node);
  605. dropNode.Expand();
  606. //treeView1.SelectedNode = child_node;
  607. }
  608. else if (group_node != dropNode)
  609. {
  610. if (!G2DTreeNodeBase.ContainsChild(group_node, dropNode, true))
  611. {
  612. group_node.RemoveFromParent();
  613. dropNode.Nodes.Add(group_node);
  614. dropNode.Expand();
  615. //treeView1.SelectedNode = group_node;
  616. }
  617. }
  618. }
  619. else if (dropNode is ObjectTreeNode && child_node is ObjectTreeNode)
  620. {
  621. ObjectTreeNode dragObjNode = child_node as ObjectTreeNode;
  622. ObjectTreeNode dropObjNode = dropNode as ObjectTreeNode;
  623. if (dragObjNode.Data is PointData &&
  624. dropObjNode.Data is PointData)
  625. {
  626. PointData srcP = dragObjNode.Data as PointData;
  627. PointData dstP = dropObjNode.Data as PointData;
  628. if (!dstP.NextNames.Contains(srcP.Name))
  629. {
  630. dstP.NextNames.Add(srcP.Name);
  631. post_MsgPutPoint(dstP);
  632. propertyGrid1.Refresh();
  633. MessageBox.Show(dstP.Name + " -> " + srcP.Name + " 已连接");
  634. }
  635. }
  636. if (dragObjNode.Data is PointData &&
  637. dropObjNode.Data is UnitData)
  638. {
  639. PointData srcP = dragObjNode.Data as PointData;
  640. UnitData dstP = dropObjNode.Data as UnitData;
  641. dstP.StartPointName = srcP.Name;
  642. propertyGrid1.Refresh();
  643. MessageBox.Show(dstP.Name + " -> " + srcP.Name + " 已设置路径");
  644. }
  645. }
  646. }
  647. private void treeView1_DragEnter(object sender, DragEventArgs e)
  648. {
  649. if (e.Data.GetDataPresent(typeof(ObjectTreeNode)))
  650. {
  651. e.Effect = DragDropEffects.Move;
  652. }
  653. else if (e.Data.GetDataPresent(typeof(G2DTreeNodeGroup)))
  654. {
  655. e.Effect = DragDropEffects.Move;
  656. }
  657. else
  658. {
  659. e.Effect = DragDropEffects.None;
  660. }
  661. }
  662. private void treeView1_DragOver(object sender, DragEventArgs e)
  663. {
  664. Point pos = treeView1.PointToClient(new Point(e.X, e.Y));
  665. TreeNode dropNode = this.treeView1.GetNodeAt(pos);
  666. if (dropNode is G2DTreeNodeBase)
  667. {
  668. e.Effect = DragDropEffects.Move;
  669. }
  670. else
  671. {
  672. e.Effect = DragDropEffects.None;
  673. }
  674. }
  675. private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
  676. {
  677. treeView1.SelectedNode = e.Node;
  678. }
  679. private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
  680. {
  681. if (e.Node is ObjectTreeNode)
  682. {
  683. ObjectTreeNode tn = e.Node as ObjectTreeNode;
  684. post_MsgLocateCamera(tn.Data.X, tn.Data.Y);
  685. }
  686. }
  687. private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
  688. {
  689. if (e.Node is ObjectTreeNode)
  690. {
  691. SelectSceneObject(e.Node as ObjectTreeNode);
  692. }
  693. }
  694. private void btn_TreeExpandAll_Click(object sender, EventArgs e)
  695. {
  696. treeRoot.ExpandAll();
  697. }
  698. private void btn_TreeCollapseAll_Click(object sender, EventArgs e)
  699. {
  700. foreach (TreeNode tn in treeRoot.Nodes)
  701. {
  702. tn.Collapse();
  703. }
  704. }
  705. //----------------------------------------------------------------------------
  706. private void saveToolStripMenuItem_Click(object sender, EventArgs e)
  707. {
  708. if (bLoadScene)
  709. {
  710. SaveToScene();
  711. }
  712. }
  713. private void buttonSetTerrain_Click(object sender, EventArgs e)
  714. {
  715. ChangeSceneTerrain();
  716. }
  717. private void toolStripButtonSave_Click(object sender, EventArgs e)
  718. {
  719. if (bLoadScene)
  720. {
  721. SaveToScene();
  722. }
  723. }
  724. //----------------------------------------------------------------------------
  725. private void btn_selectUnit_Click(object sender, EventArgs e)
  726. {
  727. btn_selectUnit.Checked = true;
  728. btn_setTerrain.Checked = !btn_selectUnit.Checked;
  729. post_MsgSetEditorMode(MsgSetEditorMode.MODE_OBJECT);
  730. }
  731. private void btn_setTerrain_Click(object sender, EventArgs e)
  732. {
  733. btn_setTerrain.Checked = true;
  734. btn_selectUnit.Checked = !btn_setTerrain.Checked;
  735. post_MsgSetEditorMode(MsgSetEditorMode.MODE_TERRAIN);
  736. }
  737. private void objBtn_Rename_Click(object sender, EventArgs e)
  738. {
  739. RenameSceneObject();
  740. }
  741. private void objBtn_Remove_Click(object sender, EventArgs e)
  742. {
  743. RemoveSelectedSceneObject();
  744. }
  745. private void objBtn_Duplicate_Click(object sender, EventArgs e)
  746. {
  747. DuplicateSelected();
  748. }
  749. private void objBtn_Copy_Click(object sender, EventArgs e)
  750. {
  751. CopySelected();
  752. }
  753. private void objBtn_Paste_Click(object sender, EventArgs e)
  754. {
  755. Paste();
  756. }
  757. private void objBtn_CopyNameText_Click(object sender, EventArgs e)
  758. {
  759. ObjectTreeNode node = GetSelectedAsSceneObject();
  760. if (node != null)
  761. {
  762. Clipboard.SetText(node.DataID);
  763. }
  764. }
  765. private void objBtn_Event_Click(object sender, EventArgs e)
  766. {
  767. ObjectTreeNode node = GetSelectedAsSceneObject();
  768. if (node != null)
  769. {
  770. }
  771. }
  772. private void objBtn_OnOff_Click(object sender, EventArgs e)
  773. {
  774. ObjectTreeNode node = GetSelectedAsSceneObject();
  775. if (node != null)
  776. {
  777. node.Data.Enable = !node.Data.Enable;
  778. node.RefreshData();
  779. }
  780. }
  781. //----------------------------------------------------------------------------
  782. private void btn_CopyToClipboard_Click(object sender, EventArgs e)
  783. {
  784. TreeNode node = treeView1.SelectedNode;
  785. if (node != null)
  786. {
  787. Clipboard.SetText(node.Text);
  788. }
  789. }
  790. private void btn_Copy_Click(object sender, EventArgs e)
  791. {
  792. TreeNode node = treeView1.SelectedNode;
  793. if (node != null)
  794. {
  795. Clipboard.SetText(node.Text);
  796. }
  797. CopySelected();
  798. }
  799. private void btn_Paste_Click(object sender, EventArgs e)
  800. {
  801. Paste();
  802. }
  803. private void btn_Delete_Click(object sender, EventArgs e)
  804. {
  805. RemoveSelectedSceneObject();
  806. }
  807. //----------------------------------------------------------------------------
  808. private void panel1_MouseDown(object sender, MouseEventArgs e)
  809. {
  810. }
  811. private void panel1_MouseMove(object sender, MouseEventArgs e)
  812. {
  813. }
  814. private void panel1_MouseUp(object sender, MouseEventArgs e)
  815. {
  816. }
  817. private void toolStripButtonColor_Click(object sender, EventArgs e)
  818. {
  819. var cd = Editor.Instance.ShowTerrainDefinitionDialog(this, toolStripButton_Color.BackColor.ToArgb());
  820. var brush = cd.SelectedBrush;
  821. if (brush != null)
  822. {
  823. toolStripButton_Color.BackColor = Color.FromArgb(brush.Value);
  824. toolStripButton_Color.Text = brush.Name.Length > 0 ? brush.Name.Substring(0, 1) : "";
  825. toolStripButton_Color.ToolTipText = brush.Desc;
  826. post_MsgSetTerrainBrush();
  827. }
  828. }
  829. private void toolStripButtonShowTerrain_Click(object sender, EventArgs e)
  830. {
  831. post_MsgShowTerrain();
  832. }
  833. private void combo_BrushSize_TextChanged(object sender, EventArgs e)
  834. {
  835. post_MsgSetTerrainBrush();
  836. }
  837. //---------------------------------------------------------------------------
  838. private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
  839. {
  840. int wx = (int)displayWorld.screenToWorldX(e.X);
  841. int wy = (int)displayWorld.screenToWorldY(e.Y);
  842. if (e.Button == MouseButtons.Left)
  843. {
  844. post_MsgLocateCamera(wx, wy);
  845. pictureBox1.Refresh();
  846. }
  847. }
  848. private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
  849. {
  850. int wx = (int)displayWorld.screenToWorldX(e.X);
  851. int wy = (int)displayWorld.screenToWorldY(e.Y);
  852. if (e.Button == MouseButtons.Left)
  853. {
  854. post_MsgLocateCamera(wx, wy);
  855. pictureBox1.Refresh();
  856. }
  857. }
  858. private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
  859. {
  860. }
  861. private Pen camera_pen = new Pen(Color.White);
  862. private void pictureBox1_Paint(object sender, PaintEventArgs e)
  863. {
  864. float scaleX = pictureBox1.Width / (float)mZoneData.TotalWidth;
  865. float scaleY = pictureBox1.Height / (float)mZoneData.TotalHeight;
  866. float scaleD = Math.Min(scaleX, scaleY);
  867. displayWorld.setWindow(
  868. new RectangleF(0, 0,
  869. pictureBox1.Width,
  870. pictureBox1.Height));
  871. displayWorld.setCameraScale(scaleD, scaleD);
  872. displayWorld.setCamera(
  873. mZoneData.TotalWidth / 2f,
  874. mZoneData.TotalHeight / 2f);
  875. displayWorld.render(e.Graphics);
  876. if (bLastCamera != null)
  877. {
  878. e.Graphics.DrawRectangle(camera_pen,
  879. displayWorld.worldToScreenX(bLastCamera.X - bLastCamera.W / 2),
  880. displayWorld.worldToScreenY(bLastCamera.Y - bLastCamera.H / 2),
  881. displayWorld.worldToScreenSizeX(bLastCamera.W),
  882. displayWorld.worldToScreenSizeY(bLastCamera.H)
  883. );
  884. }
  885. }
  886. private void pictureBox1_ClientSizeChanged(object sender, EventArgs e)
  887. {
  888. this.pictureBox1.Refresh();
  889. }
  890. private void SceneEditor_KeyDown(object sender, KeyEventArgs e)
  891. {
  892. }
  893. #endregion
  894. //---------------------------------------------------------------------------------
  895. #region Post To Scene
  896. //---------------------------------------------------------------------------------
  897. private void post_MsgSetScene(SceneData zonedata)
  898. {
  899. MsgSetScene ss = new MsgSetScene();
  900. ss.ProjectName = TemplateManager.Factory.GetType().Name;
  901. ss.Data = zonedata.ZoneData;
  902. ss.FileName = zonedata.FileName;
  903. ss.ResourceDir = Editor.EditorRootDir;
  904. ss.ResourceProperty = zonedata.ResourceProperty;
  905. mPlugin.SendMessage(ss);
  906. }
  907. private void post_MsgPutUnit(UnitData data)
  908. {
  909. MsgPutUnit ss = new MsgPutUnit();
  910. ss.Data = data;
  911. ss.UnitData = Editor.Instance.GetTemplateByType<UnitInfo>(data.UnitTemplateID);
  912. mPlugin.SendMessage(ss);
  913. }
  914. private void post_MsgPutItem(ItemData data)
  915. {
  916. MsgPutItem ss = new MsgPutItem();
  917. ss.Data = data;
  918. ss.Item = Editor.Instance.GetTemplateByType<ItemTemplate>(data.ItemTemplateID);
  919. mPlugin.SendMessage(ss);
  920. }
  921. private void post_MsgPutPoint(PointData data)
  922. {
  923. MsgPutPoint ss = new MsgPutPoint();
  924. ss.Data = data;
  925. mPlugin.SendMessage(ss);
  926. }
  927. private void post_MsgPutRegion(RegionData data)
  928. {
  929. MsgPutRegion ss = new MsgPutRegion();
  930. ss.Data = data;
  931. mPlugin.SendMessage(ss);
  932. }
  933. private void post_MsgPutDecoration(DecorationData data)
  934. {
  935. MsgPutDecoration ss = new MsgPutDecoration();
  936. ss.Data = data;
  937. mPlugin.SendMessage(ss);
  938. }
  939. private void post_MsgPutArea(AreaData data)
  940. {
  941. MsgPutArea ss = new MsgPutArea();
  942. ss.Data = data;
  943. mPlugin.SendMessage(ss);
  944. }
  945. private void post_MsgRemoveObject(string name)
  946. {
  947. MsgRemoveObject ss = new MsgRemoveObject();
  948. ss.Name = name;
  949. mPlugin.SendMessage(ss);
  950. }
  951. private void post_MsgRenameObject(string src, string dst)
  952. {
  953. MsgRenameObject ss = new MsgRenameObject();
  954. ss.SrcName = src;
  955. ss.DstName = dst;
  956. mPlugin.SendMessage(ss);
  957. }
  958. private void post_MsgSelectObject(string name)
  959. {
  960. MsgSelectObject ss = new MsgSelectObject();
  961. ss.Name = name;
  962. mPlugin.SendMessage(ss);
  963. }
  964. private void post_MsgShowTerrain()
  965. {
  966. MsgShowTerrain ss = new MsgShowTerrain();
  967. ss.Show = toolStripButton_ShowTerrain.Checked;
  968. mPlugin.SendMessage(ss);
  969. }
  970. private void post_MsgLocateCamera(float x, float y)
  971. {
  972. MsgLocateCamera ss = new MsgLocateCamera();
  973. ss.X = x;
  974. ss.Y = y;
  975. mPlugin.SendMessage(ss);
  976. }
  977. private void post_MsgSetEditorMode(int mode)
  978. {
  979. MsgSetEditorMode ss = new MsgSetEditorMode();
  980. ss.Mode = mode;
  981. mPlugin.SendMessage(ss);
  982. }
  983. private void post_MsgSetTerrainBrush()
  984. {
  985. try
  986. {
  987. int color = toolStripButton_Color.BackColor.ToArgb();
  988. int size = int.Parse(combo_BrushSize.Text);
  989. MsgSetTerrainBrush ss = new MsgSetTerrainBrush();
  990. ss.ARGB = color;
  991. ss.Size = size;
  992. ss.Size = Math.Max(1, ss.Size);
  993. ss.Brush = (MsgSetTerrainBrush.BrushType)((btn_Brush.Tag as ToolStripMenuItem).Tag);
  994. mPlugin.SendMessage(ss);
  995. }
  996. catch (Exception err)
  997. {
  998. MessageBox.Show(err.Message);
  999. }
  1000. }
  1001. #endregion
  1002. // ---------------------------------------------------------------------------------------------
  1003. #region Recived From Scene
  1004. private bool bLoadScene = false;
  1005. private RspCameraChanged bLastCamera = null;
  1006. private void rsp(RspZoneFlagBathChanged dt)
  1007. {
  1008. foreach (RspZoneFlagChanged dd in dt.Flags)
  1009. {
  1010. try
  1011. {
  1012. mZoneData.TerrainMatrix[dd.SceneX, dd.SceneY] = dd.Flag;
  1013. }
  1014. catch (Exception ) { }
  1015. }
  1016. pictureBox1.Refresh();
  1017. }
  1018. private void rsp(RspTerrainBrushChanged data)
  1019. {
  1020. combo_BrushSize.Text = data.Size.ToString();
  1021. }
  1022. private void rsp(RspZoneFlagChanged dt)
  1023. {
  1024. try
  1025. {
  1026. mZoneData.TerrainMatrix[dt.SceneX, dt.SceneY] = dt.Flag;
  1027. pictureBox1.Refresh();
  1028. }
  1029. catch (Exception err) { }
  1030. }
  1031. private void rsp(RspOnObjectSelected dt)
  1032. {
  1033. ObjectTreeNode tn = G2DTreeNodeGroup.FindNodeByText<ObjectTreeNode>(treeRoot, dt.Name, true);
  1034. if (tn != null)
  1035. {
  1036. if (dt.Selected)
  1037. {
  1038. treeView1.SelectedNode = tn;
  1039. propertyGrid1.SelectedObject = new G2DPropertyDescriptor(tn.Data, new SceneDataAdapters(this));
  1040. }
  1041. else
  1042. {
  1043. if (tn.IsSelected && treeView1.SelectedNode == tn)
  1044. {
  1045. treeView1.SelectedNode = tn.Parent;
  1046. propertyGrid1.SelectedObject = null;
  1047. }
  1048. }
  1049. }
  1050. }
  1051. private void rsp(RspObjectPositionChanged dt)
  1052. {
  1053. ObjectTreeNode tn = G2DTreeNodeGroup.FindNodeByText<ObjectTreeNode>(treeRoot, dt.Name, true);
  1054. if (tn != null)
  1055. {
  1056. tn.Data.X = dt.x;
  1057. tn.Data.Y = dt.y;
  1058. propertyGrid1.Refresh();
  1059. }
  1060. }
  1061. private void rsp(RspObjectDirectionChanged dt)
  1062. {
  1063. ObjectTreeNode tn = G2DTreeNodeGroup.FindNodeByText<ObjectTreeNode>(treeRoot, dt.Name, true);
  1064. if (tn != null)
  1065. {
  1066. if (tn.Data is UnitData)
  1067. {
  1068. UnitData ud = tn.Data as UnitData;
  1069. ud.Direction = dt.dir;
  1070. }
  1071. else if (tn.Data is ItemData)
  1072. {
  1073. ItemData ud = tn.Data as ItemData;
  1074. ud.Direction = dt.dir;
  1075. }
  1076. propertyGrid1.Refresh();
  1077. }
  1078. }
  1079. private void rsp(RspObjectSizeChanged dt)
  1080. {
  1081. ObjectTreeNode tn = G2DTreeNodeGroup.FindNodeByText<ObjectTreeNode>(treeRoot, dt.Name, true);
  1082. if (tn != null && tn.Data is RegionData)
  1083. {
  1084. RegionData rg = tn.Data as RegionData;
  1085. rg.W = dt.x;
  1086. rg.H = dt.y;
  1087. propertyGrid1.Refresh();
  1088. }
  1089. }
  1090. private void rsp(RspPointLinkChanged dt)
  1091. {
  1092. ObjectTreeNode src_tn = G2DTreeNodeGroup.FindNodeByText<ObjectTreeNode>(treeRoot, dt.SrcPointName, true);
  1093. ObjectTreeNode dst_tn = G2DTreeNodeGroup.FindNodeByText<ObjectTreeNode>(treeRoot, dt.DstPointName, true);
  1094. if (src_tn != null &&
  1095. dst_tn != null &&
  1096. src_tn.Data is PointData &&
  1097. dst_tn.Data is PointData)
  1098. {
  1099. PointData sp = src_tn.Data as PointData;
  1100. PointData dp = dst_tn.Data as PointData;
  1101. sp.NextNames.Add(dp.Name);
  1102. }
  1103. }
  1104. private void rsp(RspEditorState dt)
  1105. {
  1106. if (!bLoadScene)
  1107. {
  1108. if (dt.State == RspEditorState.STATE_SUCCEED)
  1109. {
  1110. LoadScene();
  1111. this.bLoadScene = true;
  1112. this.splitContainer1.Enabled = true;
  1113. this.toolStrip1.Enabled = true;
  1114. }
  1115. }
  1116. }
  1117. private void rsp(RspCameraChanged dt)
  1118. {
  1119. bLastCamera = dt;
  1120. pictureBox1.Refresh();
  1121. }
  1122. private void OnPluginMessageHandler(object data)
  1123. {
  1124. try
  1125. {
  1126. if (data is RspEditorState)
  1127. {
  1128. rsp(data as RspEditorState);
  1129. }
  1130. else if (data is RspTerrainBrushChanged)
  1131. {
  1132. rsp(data as RspTerrainBrushChanged);
  1133. }
  1134. else if (data is RspZoneFlagChanged)
  1135. {
  1136. rsp(data as RspZoneFlagChanged);
  1137. }
  1138. else if (data is RspZoneFlagBathChanged)
  1139. {
  1140. rsp(data as RspZoneFlagBathChanged);
  1141. }
  1142. else if (data is RspOnObjectSelected)
  1143. {
  1144. rsp(data as RspOnObjectSelected);
  1145. }
  1146. else if (data is RspObjectPositionChanged)
  1147. {
  1148. rsp(data as RspObjectPositionChanged);
  1149. }
  1150. else if (data is RspObjectDirectionChanged)
  1151. {
  1152. rsp(data as RspObjectDirectionChanged);
  1153. }
  1154. else if (data is RspObjectSizeChanged)
  1155. {
  1156. rsp(data as RspObjectSizeChanged);
  1157. }
  1158. else if (data is RspPointLinkChanged)
  1159. {
  1160. rsp(data as RspPointLinkChanged);
  1161. }
  1162. else if (data is RspCameraChanged)
  1163. {
  1164. rsp(data as RspCameraChanged);
  1165. }
  1166. }
  1167. catch (Exception err)
  1168. {
  1169. MessageBox.Show("OnPluginMessageHandler\n" + err.Message);
  1170. }
  1171. }
  1172. #endregion
  1173. //----------------------------------------------------------------------------------
  1174. #region Tree_Node_And_Units
  1175. public class ObjectTreeNode : G2DTreeNode<SceneObjectData>
  1176. {
  1177. public ObjectTreeNode(SceneObjectData data) : base(data) { }
  1178. public ObjectTreeNode(Stream input) : base(input) { }
  1179. public void RefreshData()
  1180. {
  1181. if (!Data.Enable)
  1182. {
  1183. this.ForeColor = Color.LightGray;
  1184. }
  1185. else
  1186. {
  1187. this.ForeColor = Color.Black;
  1188. }
  1189. }
  1190. }
  1191. private G2DTreeNodeGroup GetSelectedSceneGroup()
  1192. {
  1193. if (treeView1.SelectedNode is G2DTreeNodeGroup)
  1194. {
  1195. return treeView1.SelectedNode as G2DTreeNodeGroup;
  1196. }
  1197. if (treeView1.SelectedNode is ObjectTreeNode)
  1198. {
  1199. return (treeView1.SelectedNode as ObjectTreeNode).Parent as G2DTreeNodeGroup;
  1200. }
  1201. return null;
  1202. }
  1203. private ObjectTreeNode GetSelectedAsSceneObject()
  1204. {
  1205. if (treeView1.SelectedNode is ObjectTreeNode)
  1206. {
  1207. return treeView1.SelectedNode as ObjectTreeNode;
  1208. }
  1209. return null;
  1210. }
  1211. private void SelectSceneObject(ObjectTreeNode node)
  1212. {
  1213. node.RefreshData();
  1214. if (propertyGrid1.SelectedObject != null)
  1215. {
  1216. G2DPropertyDescriptor desc = propertyGrid1.SelectedObject as G2DPropertyDescriptor;
  1217. if (desc.EditData.Equals(node.Data))
  1218. {
  1219. return;
  1220. }
  1221. }
  1222. propertyGrid1.SelectedObject = new G2DPropertyDescriptor(node.Data, new SceneDataAdapters(this));
  1223. toolStripTextBox1.Text = node.DataID;
  1224. post_MsgSelectObject(node.DataID);
  1225. }
  1226. public SceneObjectData ShowSelectSceneObject(Type type, string srcName)
  1227. {
  1228. TreeNode droot = G2DListSelectEditor<SceneObjectData>.TreeNodeDuplicate(treeRoot);
  1229. foreach (TreeNode tn in G2DTreeNodeBase.GetAllNodes(droot))
  1230. {
  1231. if (!type.IsInstanceOfType(tn.Tag) && tn.Nodes.Count == 0)
  1232. {
  1233. tn.Remove();
  1234. }
  1235. }
  1236. SceneObjectData src = GetSceneObjectData(srcName);
  1237. G2DListSelectEditor<SceneObjectData> dialog = new G2DListSelectEditor<SceneObjectData>(droot, treeView1.ImageList, src);
  1238. if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  1239. {
  1240. return dialog.SelectedObject;
  1241. }
  1242. return null;
  1243. }
  1244. public CommonAI.Zone.ZoneEditor.ZoneEvent ShowSelectSceneEvent(string srcName)
  1245. {
  1246. CommonAI.Zone.ZoneEditor.ZoneEvent src = GetSceneEventData(srcName);
  1247. G2DListSelectEditor<CommonAI.Zone.ZoneEditor.ZoneEvent> dialog = new G2DListSelectEditor<CommonAI.Zone.ZoneEditor.ZoneEvent>(
  1248. new List<CommonAI.Zone.ZoneEditor.ZoneEvent>(mData.Events), src);
  1249. if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  1250. {
  1251. return dialog.SelectedObject;
  1252. }
  1253. return null;
  1254. }
  1255. public ZoneVar ShowSelectSceneVar(string srcKey, Type valueType)
  1256. {
  1257. ZoneVar src = GetSceneVarData(srcKey);
  1258. G2DListSelectEditor<ZoneVar> dialog = new G2DListSelectEditor<ZoneVar>(
  1259. GetSceneVarsAsType(valueType), src);
  1260. if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  1261. {
  1262. return dialog.SelectedObject;
  1263. }
  1264. return null;
  1265. }
  1266. private static List<SceneObjectData> copying_objects = new List<SceneObjectData>();
  1267. private void DuplicateSelected()
  1268. {
  1269. var selected = GetSelectedAsSceneObject();
  1270. if (selected != null)
  1271. {
  1272. G2DTreeNodeGroup parent = selected.Parent as G2DTreeNodeGroup;
  1273. SceneObjectData copy = XmlUtil.CloneObject(selected.Data);
  1274. ObjectTreeNode copy_node = new ObjectTreeNode(copy);
  1275. copy_node.ImageKey = selected.ImageKey;
  1276. copy_node.SelectedImageKey = selected.SelectedImageKey;
  1277. copy_node.ContextMenuStrip = selected.ContextMenuStrip;
  1278. copy.Name = selected.Data.Name + "_Copy";
  1279. copy.SavePath = "";
  1280. TryAddSceneNode(copy_node, parent, parent.Nodes.IndexOf(selected) + 1);
  1281. return;
  1282. }
  1283. var group = GetSelectedSceneGroup();
  1284. if (group != null)
  1285. {
  1286. G2DTreeNodeGroup parent = group.Parent as G2DTreeNodeGroup;
  1287. foreach (var sub in group.GetAllNodes())
  1288. {
  1289. if (sub is ObjectTreeNode)
  1290. {
  1291. var otn = sub as ObjectTreeNode;
  1292. SceneObjectData copy = XmlUtil.CloneObject(otn.Data);
  1293. ObjectTreeNode copy_node = new ObjectTreeNode(copy);
  1294. copy_node.ImageKey = sub.ImageKey;
  1295. copy_node.SelectedImageKey = sub.SelectedImageKey;
  1296. copy_node.ContextMenuStrip = sub.ContextMenuStrip;
  1297. copy.Name = otn.Data.Name + "_Copy";
  1298. copy.SavePath = group.GetSavePath(otn, true);
  1299. TryAddSceneNode(copy_node, parent, parent.Nodes.IndexOf(group) + 1);
  1300. }
  1301. }
  1302. return;
  1303. }
  1304. }
  1305. private void CopySelected()
  1306. {
  1307. copying_objects.Clear();
  1308. var node = GetSelectedAsSceneObject();
  1309. if (node != null)
  1310. {
  1311. var parent = node.Parent as G2DTreeNodeGroup;
  1312. var copying = XmlUtil.CloneObject(node.Data);
  1313. copying.SavePath = parent.GetSavePath(node);
  1314. copying_objects.Add(copying);
  1315. return;
  1316. }
  1317. var group = GetSelectedSceneGroup();
  1318. if (group != null)
  1319. {
  1320. foreach (var sub in group.GetAllNodes())
  1321. {
  1322. if (sub is ObjectTreeNode)
  1323. {
  1324. var otn = sub as ObjectTreeNode;
  1325. var copying = XmlUtil.CloneObject(otn.Data);
  1326. copying.SavePath = group.GetSavePath(otn, true);
  1327. copying_objects.Add(copying);
  1328. }
  1329. }
  1330. return;
  1331. }
  1332. }
  1333. private string BestNewName(string name)
  1334. {
  1335. if (ContainsSceneObject(name))
  1336. {
  1337. int i = name.Length - 1;
  1338. for (; i >= 0; --i)
  1339. {
  1340. char ch = name[i];
  1341. if (ch >= '0' && ch <= '9')
  1342. {
  1343. continue;
  1344. }
  1345. else
  1346. {
  1347. break;
  1348. }
  1349. }
  1350. name = name.Substring(0, i + 1);
  1351. }
  1352. return name + treeRoot.GetAllNodes().Count;
  1353. }
  1354. private void Paste()
  1355. {
  1356. G2DTreeNodeGroup parent = treeRoot;
  1357. int index = parent.Nodes.Count;
  1358. do
  1359. {
  1360. var selected = GetSelectedAsSceneObject();
  1361. if (selected != null)
  1362. {
  1363. parent = selected.Parent as G2DTreeNodeGroup;
  1364. index = parent.Nodes.IndexOf(selected) + 1;
  1365. break;
  1366. }
  1367. var group = GetSelectedSceneGroup();
  1368. if (group != null)
  1369. {
  1370. parent = group;
  1371. index = parent.Nodes.Count;
  1372. break;
  1373. }
  1374. break;
  1375. }
  1376. while (true);
  1377. foreach (var copying_object in copying_objects)
  1378. {
  1379. SceneObjectData copy = XmlUtil.CloneObject(copying_object);
  1380. ObjectTreeNode copy_node = CreateSceneObjectNode(copy);
  1381. TryAddSceneNode(copy_node, parent, index);
  1382. }
  1383. }
  1384. private bool TryAddSceneNode(ObjectTreeNode copy_node, G2DTreeNodeGroup parent, int index)
  1385. {
  1386. string src_name = copy_node.Name;
  1387. SceneObjectData copy = copy_node.Data;
  1388. while (!string.IsNullOrEmpty(copy.Name))
  1389. {
  1390. if (ContainsSceneObject(copy.Name))
  1391. {
  1392. copy.Name = BestNewName(copy.Name);
  1393. copy.Name = G2DTextDialog.Show(copy.Name, "复制: " + src_name);
  1394. }
  1395. else
  1396. {
  1397. copy_node.SetDataID(copy.Name);
  1398. var group = parent.GetOrCreateGroup(copy.SavePath);
  1399. if (group != parent)
  1400. {
  1401. group.Nodes.Add(copy_node);
  1402. parent.Expand();
  1403. group.Expand();
  1404. }
  1405. else
  1406. {
  1407. parent.Nodes.Insert(index, copy_node);
  1408. parent.Expand();
  1409. }
  1410. copy_node.RefreshData();
  1411. RefreshObject(copy);
  1412. treeView1.SelectedNode = copy_node;
  1413. SelectSceneObject(copy_node);
  1414. return true;
  1415. }
  1416. }
  1417. return false;
  1418. }
  1419. private void RemoveSelectedSceneObject()
  1420. {
  1421. ObjectTreeNode node = GetSelectedAsSceneObject();
  1422. if (node != null)
  1423. {
  1424. if (MessageBox.Show(
  1425. "确定要删除: " + node.DataID,
  1426. "确认",
  1427. MessageBoxButtons.OKCancel) == DialogResult.OK)
  1428. {
  1429. node.Parent.Nodes.Remove(node);
  1430. post_MsgRemoveObject(node.DataID);
  1431. }
  1432. }
  1433. }
  1434. private void RenameSceneObject()
  1435. {
  1436. ObjectTreeNode node = GetSelectedAsSceneObject();
  1437. if (node != null)
  1438. {
  1439. string name = node.DataID;
  1440. while (!string.IsNullOrEmpty(name))
  1441. {
  1442. name = G2DTextDialog.Show(name, "重命名: " + name);
  1443. if (name != null)
  1444. {
  1445. if (ContainsSceneObject(name))
  1446. {
  1447. MessageBox.Show("\"" + name + "\" 已存在!");
  1448. }
  1449. else
  1450. {
  1451. string src_name = node.DataID;
  1452. node.SetDataID(name);
  1453. post_MsgRenameObject(src_name, name);
  1454. return;
  1455. }
  1456. }
  1457. }
  1458. }
  1459. }
  1460. private bool ContainsSceneObject(string name)
  1461. {
  1462. TreeNode tn = G2DTreeNodeBase.FindNodeByText<ObjectTreeNode>(treeRoot, name, true);
  1463. return tn != null;
  1464. }
  1465. private SceneObjectData GetSceneObjectData(string name)
  1466. {
  1467. if (name != null)
  1468. {
  1469. TreeNode tn = G2DTreeNodeBase.FindNodeByText<ObjectTreeNode>(treeRoot, name, true);
  1470. if (tn is ObjectTreeNode)
  1471. {
  1472. return (tn as ObjectTreeNode).Data;
  1473. }
  1474. }
  1475. return null;
  1476. }
  1477. private CommonAI.Zone.ZoneEditor.ZoneEvent GetSceneEventData(string name)
  1478. {
  1479. if (name != null)
  1480. {
  1481. foreach (CommonAI.Zone.ZoneEditor.ZoneEvent evt in mData.Events)
  1482. {
  1483. if (evt.Name.Equals(name))
  1484. {
  1485. return evt;
  1486. }
  1487. }
  1488. }
  1489. return null;
  1490. }
  1491. private ZoneVar GetSceneVarData(string name)
  1492. {
  1493. if (name != null)
  1494. {
  1495. foreach (ZoneVar var in mData.EnvironmentVars)
  1496. {
  1497. if (var.Key.Equals(name))
  1498. {
  1499. return var;
  1500. }
  1501. }
  1502. }
  1503. return null;
  1504. }
  1505. private List<ZoneVar> GetSceneVarsAsType(Type valueType)
  1506. {
  1507. if (valueType == null)
  1508. {
  1509. return new List<ZoneVar>(mData.EnvironmentVars);
  1510. }
  1511. else
  1512. {
  1513. List<ZoneVar> ret = new List<ZoneVar>();
  1514. foreach (ZoneVar var in mData.EnvironmentVars)
  1515. {
  1516. if (valueType.IsInstanceOfType(var.Value))
  1517. {
  1518. ret.Add(var);
  1519. }
  1520. }
  1521. return ret;
  1522. }
  1523. }
  1524. private void AddSceneGroup()
  1525. {
  1526. G2DTreeNodeGroup parent = GetSelectedSceneGroup();
  1527. if (parent != null)
  1528. {
  1529. string name = G2DTextDialog.Show("分组", "添加过滤器");
  1530. if (name != null)
  1531. {
  1532. G2DTreeNodeGroup group = parent.AddG2DGroup(name);
  1533. parent.Expand();
  1534. treeView1.SelectedNode = group;
  1535. }
  1536. }
  1537. }
  1538. private void RenameSceneGroup()
  1539. {
  1540. G2DTreeNodeGroup parent = GetSelectedSceneGroup();
  1541. if (parent != null)
  1542. {
  1543. string name = G2DTextDialog.Show(parent.Text, "添加过滤器");
  1544. if (name != null)
  1545. {
  1546. parent.Text = name;
  1547. }
  1548. }
  1549. }
  1550. private ObjectTreeNode CreateSceneObjectNode(SceneObjectData data)
  1551. {
  1552. string imageKey = "icon_res_1.png";
  1553. if (data is PointData)
  1554. {
  1555. imageKey = "icon_trigger.png";
  1556. }
  1557. else if (data is RegionData)
  1558. {
  1559. imageKey = "icon_layer.png";
  1560. }
  1561. else if (data is UnitData)
  1562. {
  1563. imageKey = "icon_res_2.png";
  1564. }
  1565. else if (data is ItemData)
  1566. {
  1567. imageKey = "icon_res_4.png";
  1568. }
  1569. else if (data is DecorationData)
  1570. {
  1571. imageKey = "icon_res_1.png";
  1572. }
  1573. else if (data is AreaData)
  1574. {
  1575. imageKey = "icon_res_6.png";
  1576. }
  1577. ObjectTreeNode node = new ObjectTreeNode(data);
  1578. node.ImageKey = imageKey;
  1579. node.SelectedImageKey = imageKey;
  1580. node.ContextMenuStrip = objectMenuStrip;
  1581. node.RefreshData();
  1582. return node;
  1583. }
  1584. private SceneObjectData AddSceneObject(Type dataType, Action<SceneObjectData> callback = null)
  1585. {
  1586. G2DTreeNodeGroup parent = GetSelectedSceneGroup();
  1587. if (parent == null)
  1588. {
  1589. parent = treeRoot;
  1590. }
  1591. if (parent != null)
  1592. {
  1593. string name = dataType.Name + treeRoot.GetAllNodes().Count;
  1594. while (!string.IsNullOrEmpty(name))
  1595. {
  1596. name = G2DTextDialog.Show(name, "添加: " + name);
  1597. if (name != null)
  1598. {
  1599. if (ContainsSceneObject(name))
  1600. {
  1601. MessageBox.Show("\"" + name + "\" 已存在!");
  1602. }
  1603. else
  1604. {
  1605. SceneObjectData data = (SceneObjectData)Activator.CreateInstance(dataType);
  1606. data.Name = name;
  1607. if (bLastCamera != null)
  1608. {
  1609. data.X = bLastCamera.X;
  1610. data.Y = bLastCamera.Y;
  1611. }
  1612. ObjectTreeNode node = CreateSceneObjectNode(data);
  1613. parent.Nodes.Add(node);
  1614. parent.Expand();
  1615. if (callback != null)
  1616. {
  1617. callback.Invoke(data);
  1618. }
  1619. RefreshObject(data);
  1620. treeView1.SelectedNode = node;
  1621. SelectSceneObject(node);
  1622. return data;
  1623. }
  1624. }
  1625. }
  1626. }
  1627. return null;
  1628. }
  1629. private bool LoadSceneObject(SceneObjectData data)
  1630. {
  1631. if (ContainsSceneObject(data.Name))
  1632. {
  1633. MessageBox.Show("\"" + data.Name + "\" 已存在!");
  1634. return false;
  1635. }
  1636. else
  1637. {
  1638. ObjectTreeNode node = CreateSceneObjectNode(data);
  1639. G2DTreeNodeGroup parent = treeRoot.GetOrCreateGroup(data.SavePath);
  1640. parent.Nodes.Add(node);
  1641. RefreshObject(data);
  1642. return true;
  1643. }
  1644. }
  1645. private void RefreshObject(SceneObjectData data)
  1646. {
  1647. if (data is UnitData)
  1648. {
  1649. post_MsgPutUnit(data as UnitData);
  1650. }
  1651. else if (data is ItemData)
  1652. {
  1653. post_MsgPutItem(data as ItemData);
  1654. }
  1655. else if (data is PointData)
  1656. {
  1657. post_MsgPutPoint(data as PointData);
  1658. }
  1659. else if (data is RegionData)
  1660. {
  1661. post_MsgPutRegion(data as RegionData);
  1662. }
  1663. else if (data is DecorationData)
  1664. {
  1665. post_MsgPutDecoration(data as DecorationData);
  1666. }
  1667. else if (data is AreaData)
  1668. {
  1669. post_MsgPutArea(data as AreaData);
  1670. }
  1671. }
  1672. #endregion
  1673. //----------------------------------------------------------------------------------
  1674. class EditorMiniMap : DisplayTerrain
  1675. {
  1676. private G2DTreeNodeGroup treeRoot;
  1677. public EditorMiniMap(G2DTreeNodeGroup treeRoot, ZoneInfo terrain)
  1678. {
  1679. this.treeRoot = treeRoot;
  1680. base.InitTerrain(terrain);
  1681. }
  1682. public override void Dispose()
  1683. {
  1684. }
  1685. public void ResetTerrain(ZoneInfo terrain)
  1686. {
  1687. base.InitTerrain(terrain);
  1688. }
  1689. protected override void renderObjects(Graphics g, RectangleF worldBounds)
  1690. {
  1691. foreach (TreeNode tn in treeRoot.GetAllNodes())
  1692. {
  1693. if (tn is ObjectTreeNode)
  1694. {
  1695. SceneObjectData sd = (tn as ObjectTreeNode).Data;
  1696. System.Drawing.Drawing2D.GraphicsState state = g.Save();
  1697. g.TranslateTransform(sd.X, sd.Y);
  1698. renderNode(g, sd);
  1699. g.Restore(state);
  1700. }
  1701. }
  1702. }
  1703. protected override void renderScreen(Graphics g, RectangleF worldBounds)
  1704. {
  1705. }
  1706. private Pen pen = new Pen(Color.White);
  1707. private SolidBrush brush = new SolidBrush(Color.White);
  1708. private void renderNode(Graphics g, SceneObjectData data)
  1709. {
  1710. pen.Color = brush.Color = Color.FromArgb(data.Color);
  1711. if (data is PointData)
  1712. {
  1713. g.FillEllipse(brush, -1, -1, 2, 2);
  1714. }
  1715. else if (data is RegionData)
  1716. {
  1717. RegionData rd = data as RegionData;
  1718. if (rd.RegionType == RegionData.Shape.RECTANGLE)
  1719. {
  1720. g.FillRectangle(brush, -rd.W / 2, -rd.H / 2, rd.W, rd.H);
  1721. }
  1722. else if (rd.RegionType == RegionData.Shape.STRIP)
  1723. {
  1724. float line_r = rd.W / 2;
  1725. Line2 line = new Line2(0, 0, 0, 0);
  1726. MathVector.movePolar(line.p, rd.StripDirection, -rd.H / 2);
  1727. MathVector.movePolar(line.q, rd.StripDirection, +rd.H / 2);
  1728. DrawingUtils.FillLineRect(g, brush, line.p.X, line.p.Y, line.q.X, line.q.Y, line_r);
  1729. }
  1730. else
  1731. {
  1732. g.FillEllipse(brush, -rd.W / 2, -rd.H / 2, rd.W, rd.H);
  1733. }
  1734. }
  1735. else if (data is UnitData)
  1736. {
  1737. UnitData rd = data as UnitData;
  1738. UnitInfo ut = Editor.Instance.GetTemplateByType<UnitInfo>(rd.UnitTemplateID);
  1739. if (ut != null)
  1740. {
  1741. g.FillEllipse(brush, -ut.BodySize, -ut.BodySize, ut.BodySize * 2, ut.BodySize * 2);
  1742. }
  1743. else
  1744. {
  1745. g.FillEllipse(brush, -1, -1, 2, 2);
  1746. }
  1747. }
  1748. else if (data is ItemData)
  1749. {
  1750. ItemData rd = data as ItemData;
  1751. ItemTemplate ut = Editor.Instance.GetTemplateByType<ItemTemplate>(rd.ItemTemplateID);
  1752. if (ut != null)
  1753. {
  1754. g.FillEllipse(brush, -ut.BodySize, -ut.BodySize, ut.BodySize * 2, ut.BodySize * 2);
  1755. }
  1756. else
  1757. {
  1758. g.FillEllipse(brush, -1, -1, 2, 2);
  1759. }
  1760. }
  1761. else if (data is DecorationData)
  1762. {
  1763. DecorationData rd = data as DecorationData;
  1764. if (rd.RegionType == DecorationData.Shape.RECTANGLE)
  1765. {
  1766. g.FillRectangle(brush, -rd.W / 2, -rd.H / 2, rd.W, rd.H);
  1767. }
  1768. else
  1769. {
  1770. g.FillEllipse(brush, -rd.W / 2, -rd.H / 2, rd.W, rd.H);
  1771. }
  1772. }
  1773. else if (data is AreaData)
  1774. {
  1775. AreaData rd = data as AreaData;
  1776. g.FillEllipse(brush, -1, -1, 2, 2);
  1777. }
  1778. }
  1779. }
  1780. }
  1781. }