DataAdapters.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using CommonFroms.Utils;
  5. using System.Reflection;
  6. using System.Drawing.Design;
  7. using CommonLang.Property;
  8. using CommonAI.Zone.Attributes;
  9. using CommonAI.Zone;
  10. using System.ComponentModel;
  11. using System.Drawing;
  12. using System.Windows.Forms;
  13. using System.Globalization;
  14. using CommonAI.Zone.ZoneEditor;
  15. using System.IO;
  16. using CommonAIEditor.Scene;
  17. using CommonAI.ZoneEditor;
  18. using CommonAI.Zone.ZoneEditor.EventTrigger;
  19. using CommonAI.Zone.EventTrigger;
  20. using CommonLang;
  21. using System.Collections;
  22. using CommonFroms.G2D.DataGrid;
  23. using CommonFroms.G2D;
  24. using CommonAIEditor.Unit;
  25. namespace CommonAIEditor
  26. {
  27. public static class GameEditorPropertyAdapter
  28. {
  29. public static void Init()
  30. {
  31. G2DPropertyDescriptor.RegistPropertyAdapter(new DataAdapters());
  32. }
  33. }
  34. public class SceneDataAdapters : IG2DPropertyAdapter
  35. {
  36. public int SceneID { get { return scene.Data.ID; } }
  37. public readonly SceneEditor scene;
  38. public readonly SceneEventEditor evtEditor;
  39. public SceneDataAdapters(SceneEditor scene, SceneEventEditor evtEditor = null)
  40. {
  41. this.scene = scene;
  42. this.evtEditor = evtEditor;
  43. }
  44. public UITypeEditor GetEditor(MemberInfo field, Type fieldType, object ownerData)
  45. {
  46. if (field is FieldInfo)
  47. {
  48. if (PropertyUtil.GetAttribute<SceneScriptIDAttribute>(field) != null)
  49. {
  50. return new ScriptEditor(SceneID, field as FieldInfo, ownerData);
  51. }
  52. else if (PropertyUtil.GetAttribute<SceneObjectIDAttribute>(field) != null)
  53. {
  54. return new SceneObjectIDEditor(scene, field as FieldInfo, ownerData);
  55. }
  56. else if (PropertyUtil.GetAttribute<SceneEventIDAttribute>(field) != null)
  57. {
  58. return new SceneEventIDEditor(scene, field as FieldInfo, ownerData);
  59. }
  60. else if (PropertyUtil.GetAttribute<SceneVarIDAttribute>(field) != null)
  61. {
  62. return new SceneVarIDEditor(scene, field as FieldInfo, ownerData);
  63. }
  64. else if (PropertyUtil.GetAttribute<LocalVarIDAttribute>(field) != null)
  65. {
  66. if (evtEditor != null)
  67. {
  68. return new LocalVarIDEditor(evtEditor, field as FieldInfo, ownerData);
  69. }
  70. }
  71. }
  72. return null;
  73. }
  74. public TypeConverter GetConverter(MemberInfo field, Type fieldType, object ownerData)
  75. {
  76. return null;
  77. }
  78. public class ScriptEditor : UITypeEditor
  79. {
  80. private readonly int SceneID;
  81. private FieldInfo fieldInfo;
  82. private object ownerData;
  83. public ScriptEditor(int sceneID, FieldInfo field, object ownerData)
  84. {
  85. this.SceneID = sceneID;
  86. this.fieldInfo = field;
  87. this.ownerData = ownerData;
  88. }
  89. public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
  90. {
  91. return UITypeEditorEditStyle.Modal;
  92. }
  93. public override object EditValue(
  94. ITypeDescriptorContext context,
  95. IServiceProvider provider,
  96. object value)
  97. {
  98. // IGameEditorPlugin plugin = EditorPlugin.CurrentPlugin;
  99. // FileInfo sfile = plugin.LaunchSceneScriptEditor(SceneID, ownerData);
  100. // if (sfile != null && sfile.Exists)
  101. // {
  102. // fieldInfo.SetValue(ownerData, sfile.Name);
  103. // return sfile.Name;
  104. // }
  105. return base.EditValue(context, provider, value);
  106. }
  107. }
  108. public class SceneObjectIDEditor : UITypeEditor
  109. {
  110. private SceneEditor scene;
  111. private FieldInfo fieldInfo;
  112. private object ownerData;
  113. private string objName;
  114. public SceneObjectIDEditor(
  115. SceneEditor scene,
  116. FieldInfo field,
  117. object ownerData)
  118. {
  119. this.scene = scene;
  120. this.fieldInfo = field;
  121. this.ownerData = ownerData;
  122. this.objName = fieldInfo.GetValue(ownerData) as string;
  123. }
  124. public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
  125. {
  126. return UITypeEditorEditStyle.Modal;
  127. }
  128. public override object EditValue(
  129. ITypeDescriptorContext context,
  130. IServiceProvider provider,
  131. object value)
  132. {
  133. SceneObjectIDAttribute soid = PropertyUtil.GetAttribute<SceneObjectIDAttribute>(fieldInfo);
  134. SceneObjectData sobj = scene.ShowSelectSceneObject(soid.ObjectType, objName);
  135. if (sobj != null)
  136. {
  137. return sobj.Name;
  138. }
  139. return base.EditValue(context, provider, value);
  140. }
  141. }
  142. public class SceneEventIDEditor : UITypeEditor
  143. {
  144. private SceneEditor scene;
  145. private FieldInfo fieldInfo;
  146. private object ownerData;
  147. private string objName;
  148. public SceneEventIDEditor(
  149. SceneEditor scene,
  150. FieldInfo field,
  151. object ownerData)
  152. {
  153. this.scene = scene;
  154. this.fieldInfo = field;
  155. this.ownerData = ownerData;
  156. this.objName = fieldInfo.GetValue(ownerData) as string;
  157. }
  158. public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
  159. {
  160. return UITypeEditorEditStyle.Modal;
  161. }
  162. public override object EditValue(
  163. ITypeDescriptorContext context,
  164. IServiceProvider provider,
  165. object value)
  166. {
  167. SceneEventIDAttribute soid = PropertyUtil.GetAttribute<SceneEventIDAttribute>(fieldInfo);
  168. CommonAI.Zone.ZoneEditor.ZoneEvent sobj = scene.ShowSelectSceneEvent(objName);
  169. if (sobj != null)
  170. {
  171. return sobj.Name;
  172. }
  173. return base.EditValue(context, provider, value);
  174. }
  175. }
  176. public class SceneVarIDEditor : UITypeEditor
  177. {
  178. private SceneEditor scene;
  179. private FieldInfo fieldInfo;
  180. private object ownerData;
  181. private string objName;
  182. public SceneVarIDEditor(
  183. SceneEditor scene,
  184. FieldInfo field,
  185. object ownerData)
  186. {
  187. this.scene = scene;
  188. this.fieldInfo = field;
  189. this.ownerData = ownerData;
  190. this.objName = fieldInfo.GetValue(ownerData) as string;
  191. }
  192. public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
  193. {
  194. return UITypeEditorEditStyle.Modal;
  195. }
  196. public override object EditValue(
  197. ITypeDescriptorContext context,
  198. IServiceProvider provider,
  199. object value)
  200. {
  201. SceneVarIDAttribute svid = PropertyUtil.GetAttribute<SceneVarIDAttribute>(fieldInfo);
  202. ZoneVar vobj = scene.ShowSelectSceneVar(objName, svid.VarType);
  203. if (vobj != null)
  204. {
  205. return vobj.Key;
  206. }
  207. return base.EditValue(context, provider, value);
  208. }
  209. }
  210. public class LocalVarIDEditor : UITypeEditor
  211. {
  212. private SceneEventEditor scene;
  213. private FieldInfo fieldInfo;
  214. private object ownerData;
  215. private string objName;
  216. public LocalVarIDEditor(
  217. SceneEventEditor scene,
  218. FieldInfo field,
  219. object ownerData)
  220. {
  221. this.scene = scene;
  222. this.fieldInfo = field;
  223. this.ownerData = ownerData;
  224. this.objName = fieldInfo.GetValue(ownerData) as string;
  225. }
  226. public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
  227. {
  228. return UITypeEditorEditStyle.Modal;
  229. }
  230. public override object EditValue(
  231. ITypeDescriptorContext context,
  232. IServiceProvider provider,
  233. object value)
  234. {
  235. LocalVarIDAttribute svid = PropertyUtil.GetAttribute<LocalVarIDAttribute>(fieldInfo);
  236. LocalVar vobj = scene.ShowSelectLocalVar(objName, svid.VarType);
  237. if (vobj != null)
  238. {
  239. return vobj.Key;
  240. }
  241. return base.EditValue(context, provider, value);
  242. }
  243. }
  244. }
  245. public class UnitEventAdapters : IG2DPropertyAdapter
  246. {
  247. public readonly UnitEventEditor evtEditor;
  248. public UnitEventAdapters(UnitEventEditor evtEditor)
  249. {
  250. this.evtEditor = evtEditor;
  251. }
  252. public UITypeEditor GetEditor(MemberInfo field, Type fieldType, object ownerData)
  253. {
  254. if (field is FieldInfo && PropertyUtil.GetAttribute<UnitEventIDAttribute>(field) != null)
  255. {
  256. return new UnitEventIDEditor(evtEditor, field as FieldInfo, ownerData);
  257. }
  258. return null;
  259. }
  260. public TypeConverter GetConverter(MemberInfo field, Type fieldType, object ownerData)
  261. {
  262. return null;
  263. }
  264. public class UnitEventIDEditor : UITypeEditor
  265. {
  266. private UnitEventEditor editor;
  267. private FieldInfo fieldInfo;
  268. private object ownerData;
  269. private string objName;
  270. public UnitEventIDEditor(
  271. UnitEventEditor editor,
  272. FieldInfo field,
  273. object ownerData)
  274. {
  275. this.editor = editor;
  276. this.fieldInfo = field;
  277. this.ownerData = ownerData;
  278. this.objName = fieldInfo.GetValue(ownerData) as string;
  279. }
  280. public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
  281. {
  282. return UITypeEditorEditStyle.Modal;
  283. }
  284. public override object EditValue(
  285. ITypeDescriptorContext context,
  286. IServiceProvider provider,
  287. object value)
  288. {
  289. var soid = PropertyUtil.GetAttribute<UnitEventIDAttribute>(fieldInfo);
  290. var sobj = editor.ShowSelectEvent(objName);
  291. if (sobj != null)
  292. {
  293. return sobj.Name;
  294. }
  295. return base.EditValue(context, provider, value);
  296. }
  297. }
  298. }
  299. public class DataAdapters : IG2DPropertyAdapter
  300. {
  301. public UITypeEditor GetEditor(MemberInfo field, Type fieldType, object ownerData)
  302. {
  303. if (field is FieldInfo)
  304. {
  305. if (PropertyUtil.GetAttribute<ObjectMemberNameAttribute>(field) != null)
  306. {
  307. return new ObjectMemberNameEditor(field as FieldInfo, ownerData);
  308. }
  309. if (PropertyUtil.GetAttribute<TemplateIDAttribute>(field) != null)
  310. {
  311. return new TemplateIDEditor(field as FieldInfo, ownerData, PropertyUtil.GetAttribute<TemplateIDAttribute>(field));
  312. }
  313. if (PropertyUtil.GetAttribute<TemplatesIDAttribute>(field) != null)
  314. {
  315. return new TemplatesIDEditor(field as FieldInfo, ownerData, PropertyUtil.GetAttribute<TemplatesIDAttribute>(field));
  316. }
  317. if (PropertyUtil.GetAttribute<ResourceIDAttribute>(field) != null)
  318. {
  319. return new ResourceIDEditor(field as FieldInfo, ownerData, PropertyUtil.GetAttribute<ResourceIDAttribute>(field));
  320. }
  321. if (PropertyUtil.GetAttribute<ColorValueAttribute>(field) != null)
  322. {
  323. return new G2DColorEditor(field, ownerData);
  324. }
  325. }
  326. return null;
  327. }
  328. public TypeConverter GetConverter(MemberInfo field, Type fieldType, object ownerData)
  329. {
  330. if (field is FieldInfo)
  331. {
  332. TemplateIDAttribute tid = PropertyUtil.GetAttribute<TemplateIDAttribute>(field);
  333. if (tid != null)
  334. {
  335. return new TemplateIDConvert(tid);
  336. }
  337. TemplatesIDAttribute tsid = PropertyUtil.GetAttribute<TemplatesIDAttribute>(field);
  338. if (tsid != null)
  339. {
  340. return new TemplatesIDConvert(tsid);
  341. }
  342. DescAttribute desc = PropertyUtil.GetAttribute<DescAttribute>(field);
  343. ColorValueAttribute color = PropertyUtil.GetAttribute<ColorValueAttribute>(field);
  344. if (color != null && fieldType.Equals(typeof(int)))
  345. {
  346. return new G2DIntToHexTypeConverter();
  347. }
  348. }
  349. return null;
  350. }
  351. /// <summary>
  352. /// AbstractValue 获取对象字段
  353. /// </summary>
  354. public class ObjectMemberNameEditor : UITypeEditor
  355. {
  356. private FieldInfo fieldInfo;
  357. private object ownerData;
  358. private ObjectMemberNameAttribute member;
  359. private Type ownerType;
  360. public ObjectMemberNameEditor(FieldInfo field, object ownerData)
  361. {
  362. this.fieldInfo = field;
  363. this.ownerData = ownerData;
  364. this.member = PropertyUtil.GetAttribute<ObjectMemberNameAttribute>(field);
  365. if (ownerData is IFieldMemberValue)
  366. {
  367. ownerType = (ownerData as IFieldMemberValue).OwnerType;
  368. }
  369. else
  370. {
  371. ownerType = member.ObjectType;
  372. }
  373. }
  374. public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
  375. {
  376. return UITypeEditorEditStyle.Modal;
  377. }
  378. public override object EditValue(
  379. ITypeDescriptorContext context,
  380. IServiceProvider provider,
  381. object value)
  382. {
  383. if (ownerType != null)
  384. {
  385. FieldsMap fm = GameFields.Manager.GetFields(ownerType, member.FieldType);
  386. if (fm != null)
  387. {
  388. G2DListSelectEditor<MemberDescAttribute> dialog = new G2DListSelectEditor<MemberDescAttribute>(
  389. fm.ListFields, fm.GetFieldDesc(value + ""));
  390. if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  391. {
  392. value = dialog.SelectedObject.DataField.Name;
  393. return value;
  394. }
  395. }
  396. }
  397. return base.EditValue(context, provider, value);
  398. }
  399. }
  400. public class ResourceIDEditor : UITypeEditor
  401. {
  402. private Brush brush = new SolidBrush(Color.Green);
  403. private FieldInfo fieldInfo;
  404. private object ownerData;
  405. private ResourceIDAttribute resourceID;
  406. public ResourceIDEditor(FieldInfo field, object ownerData, ResourceIDAttribute tid)
  407. {
  408. this.fieldInfo = field;
  409. this.ownerData = ownerData;
  410. this.resourceID = tid;
  411. }
  412. public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
  413. {
  414. return UITypeEditorEditStyle.Modal;
  415. }
  416. public override object EditValue(
  417. ITypeDescriptorContext context,
  418. IServiceProvider provider,
  419. object value)
  420. {
  421. string id = (string)fieldInfo.GetValue(ownerData);
  422. {
  423. string fullPath = Editor.EditorRootDir + "\\" + id;
  424. fullPath = fullPath.Replace('/', '\\');
  425. fullPath = fullPath.Replace("\\\\", "\\");
  426. OpenFileDialog fd = new OpenFileDialog();
  427. DirectoryInfo resdir = new DirectoryInfo(Editor.EditorRootDir + "/../xmds_Data/StreamingAssets/AssetBundles/PC");
  428. fd.InitialDirectory = resdir.FullName + "\\res";
  429. fd.FileName = fullPath;
  430. fd.Multiselect = false;
  431. if (fd.ShowDialog() == DialogResult.OK)
  432. {
  433. FileInfo fdinfo = new FileInfo(fd.FileName);
  434. if (fdinfo.FullName.StartsWith(resdir.FullName))
  435. {
  436. id = fdinfo.FullName.Substring(resdir.FullName.Length);
  437. id = id.Replace('\\', '/');
  438. return id;
  439. }
  440. else
  441. {
  442. MessageBox.Show("无法引用编辑器目录之外的资源");
  443. }
  444. }
  445. }
  446. return base.EditValue(context, provider, value);
  447. }
  448. }
  449. public class TemplateIDEditor : UITypeEditor
  450. {
  451. private FieldInfo fieldInfo;
  452. private object ownerData;
  453. private TemplateIDAttribute templateID;
  454. public TemplateIDEditor(FieldInfo field, object ownerData, TemplateIDAttribute tid)
  455. {
  456. this.fieldInfo = field;
  457. this.ownerData = ownerData;
  458. this.templateID = tid;
  459. }
  460. public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
  461. {
  462. return UITypeEditorEditStyle.Modal;
  463. }
  464. public override object EditValue(
  465. ITypeDescriptorContext context,
  466. IServiceProvider provider,
  467. object value)
  468. {
  469. int id = (int)fieldInfo.GetValue(ownerData);
  470. object obj = Editor.Instance.GetTemplateByType(id, templateID.TemplateType);
  471. if (Editor.Instance.ShowSelectTemplateIDDialog(obj, templateID.TemplateType, out id))
  472. {
  473. return id;
  474. }
  475. return base.EditValue(context, provider, value);
  476. }
  477. }
  478. public class TemplateIDConvert : TypeConverter
  479. {
  480. private TemplateIDAttribute templateID;
  481. public TemplateIDConvert(TemplateIDAttribute templateID)
  482. {
  483. this.templateID = templateID;
  484. }
  485. public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  486. {
  487. if (sourceType == typeof(string))
  488. {
  489. return true;
  490. }
  491. return false;
  492. }
  493. public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
  494. {
  495. if (destinationType == typeof(string))
  496. {
  497. return true;
  498. }
  499. return false;
  500. }
  501. public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  502. {
  503. if (destinationType == typeof(string) && value.GetType() == typeof(int))
  504. {
  505. int id = (int)value;
  506. object obj = Editor.Instance.GetTemplateByType(id, templateID.TemplateType);
  507. return obj + "";
  508. }
  509. else
  510. {
  511. return base.ConvertTo(context, culture, value, destinationType);
  512. }
  513. }
  514. public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
  515. {
  516. if (value.GetType() == typeof(string))
  517. {
  518. int id = 0;
  519. try
  520. {
  521. string text = value.ToString();
  522. int i1 = text.LastIndexOf("(") + 1;
  523. int i2 = text.LastIndexOf(")");
  524. text = text.Substring(i1, i2 - i1);
  525. id = int.Parse(text);
  526. }
  527. catch (Exception err) { }
  528. return id;
  529. }
  530. else
  531. {
  532. return base.ConvertFrom(context, culture, value);
  533. }
  534. }
  535. }
  536. public class TemplatesIDEditor : UITypeEditor
  537. {
  538. private FieldInfo fieldInfo;
  539. private object ownerData;
  540. private TemplatesIDAttribute templatesID;
  541. public TemplatesIDEditor(FieldInfo field, object ownerData, TemplatesIDAttribute tid)
  542. {
  543. this.fieldInfo = field;
  544. this.ownerData = ownerData;
  545. this.templatesID = tid;
  546. }
  547. public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
  548. {
  549. return UITypeEditorEditStyle.Modal;
  550. }
  551. public override object EditValue(
  552. ITypeDescriptorContext context,
  553. IServiceProvider provider,
  554. object value)
  555. {
  556. if (fieldInfo.FieldType == typeof(List<int>) ||
  557. fieldInfo.FieldType == typeof(int[]))
  558. {
  559. int[] list = new int[] { };
  560. if (value != null)
  561. {
  562. if (fieldInfo.FieldType == typeof(List<int>))
  563. {
  564. list = (value as List<int>).ToArray();
  565. }
  566. else if (fieldInfo.FieldType == typeof(int[]))
  567. {
  568. list = (value as int[]);
  569. }
  570. }
  571. object listvalue = GetListByType(templatesID.TemplateType, list);
  572. G2DCollectionEditor listedit = new G2DCollectionEditor(
  573. listvalue.GetType(), listvalue,
  574. new Type[] { GetListElementType(templatesID.TemplateType) });
  575. if (listedit.ShowDialog() == DialogResult.OK)
  576. {
  577. List<int> ret = GetListValue(
  578. templatesID.TemplateType,
  579. listedit.GetEditCompleteData());
  580. if (fieldInfo.FieldType == typeof(List<int>))
  581. {
  582. return ret;
  583. }
  584. else if (fieldInfo.FieldType == typeof(int[]))
  585. {
  586. return ret.ToArray();
  587. }
  588. }
  589. }
  590. return base.EditValue(context, provider, value);
  591. }
  592. //-------------------------------------------------------------------------------------------
  593. #region _static_
  594. private static HashMap<Type, Type> mElementTypeMap = new HashMap<Type, Type>();
  595. private static HashMap<Type, Type> mListTypeMap = new HashMap<Type, Type>();
  596. static TemplatesIDEditor()
  597. {
  598. mElementTypeMap[typeof(UnitInfo)] = typeof(UnitListItem);
  599. mElementTypeMap[typeof(SkillTemplate)] = typeof(SkillListItem);
  600. mElementTypeMap[typeof(SpellTemplate)] = typeof(SpellListItem);
  601. mElementTypeMap[typeof(BuffTemplate)] = typeof(BuffListItem);
  602. mElementTypeMap[typeof(ItemTemplate)] = typeof(ItemListItem);
  603. mElementTypeMap[typeof(UnitTriggerTemplate)] = typeof(UnitTriggerListItem);
  604. mElementTypeMap[typeof(UnitEventTemplate)] = typeof(UnitEventListItem);
  605. mListTypeMap[typeof(UnitInfo)] = typeof(List<UnitListItem>);
  606. mListTypeMap[typeof(SkillTemplate)] = typeof(List<SkillListItem>);
  607. mListTypeMap[typeof(SpellTemplate)] = typeof(List<SpellListItem>);
  608. mListTypeMap[typeof(BuffTemplate)] = typeof(List<BuffListItem>);
  609. mListTypeMap[typeof(ItemTemplate)] = typeof(List<ItemListItem>);
  610. mListTypeMap[typeof(UnitTriggerTemplate)] = typeof(List<UnitTriggerListItem>);
  611. mListTypeMap[typeof(UnitEventTemplate)] = typeof(List<UnitEventListItem>);
  612. }
  613. public static Type GetListElementType(Type template_type)
  614. {
  615. return mElementTypeMap[template_type];
  616. }
  617. static public IList GetListByType(Type type, int[] list)
  618. {
  619. Type list_type = mListTypeMap[type];
  620. Type element_type = mElementTypeMap[type];
  621. IList ret = (IList)Activator.CreateInstance(list_type);
  622. foreach (int tid in list)
  623. {
  624. IListItem item = (IListItem)Activator.CreateInstance(element_type);
  625. item.ID = tid;
  626. ret.Add(item);
  627. }
  628. return ret;
  629. }
  630. static public List<int> GetListValue(Type type, object completeData)
  631. {
  632. List<int> ret = new List<int>();
  633. IList list = completeData as IList;
  634. foreach (IListItem tid in list)
  635. {
  636. ret.Add(tid.ID);
  637. }
  638. return ret;
  639. }
  640. public interface IListItem
  641. {
  642. int ID { get; set; }
  643. }
  644. public class UnitListItem : IListItem
  645. {
  646. [TemplateID(typeof(UnitInfo))]
  647. public int TemplateID;
  648. public int ID { get { return TemplateID; } set { this.TemplateID = value; } }
  649. public override string ToString()
  650. {
  651. object obj = Editor.Instance.GetTemplateByType(TemplateID, typeof(UnitInfo));
  652. return obj + "";
  653. }
  654. }
  655. public class SkillListItem : IListItem
  656. {
  657. [TemplateID(typeof(SkillTemplate))]
  658. public int TemplateID;
  659. public int ID { get { return TemplateID; } set { this.TemplateID = value; } }
  660. public override string ToString()
  661. {
  662. object obj = Editor.Instance.GetTemplateByType(TemplateID, typeof(SkillTemplate));
  663. return obj + "";
  664. }
  665. }
  666. public class SpellListItem : IListItem
  667. {
  668. [TemplateID(typeof(SpellTemplate))]
  669. public int TemplateID;
  670. public int ID { get { return TemplateID; } set { this.TemplateID = value; } }
  671. public override string ToString()
  672. {
  673. object obj = Editor.Instance.GetTemplateByType(TemplateID, typeof(SpellTemplate));
  674. return obj + "";
  675. }
  676. }
  677. public class BuffListItem : IListItem
  678. {
  679. [TemplateID(typeof(BuffTemplate))]
  680. public int TemplateID;
  681. public int ID { get { return TemplateID; } set { this.TemplateID = value; } }
  682. public override string ToString()
  683. {
  684. object obj = Editor.Instance.GetTemplateByType(TemplateID, typeof(BuffTemplate));
  685. return obj + "";
  686. }
  687. }
  688. public class ItemListItem : IListItem
  689. {
  690. [TemplateID(typeof(ItemTemplate))]
  691. public int TemplateID;
  692. public int ID { get { return TemplateID; } set { this.TemplateID = value; } }
  693. public override string ToString()
  694. {
  695. object obj = Editor.Instance.GetTemplateByType(TemplateID, typeof(ItemTemplate));
  696. return obj + "";
  697. }
  698. }
  699. public class UnitTriggerListItem : IListItem
  700. {
  701. [TemplateID(typeof(UnitTriggerTemplate))]
  702. public int TemplateID;
  703. public int ID { get { return TemplateID; } set { this.TemplateID = value; } }
  704. public override string ToString()
  705. {
  706. object obj = Editor.Instance.GetTemplateByType(TemplateID, typeof(UnitTriggerTemplate));
  707. return obj + "";
  708. }
  709. }
  710. public class UnitEventListItem : IListItem
  711. {
  712. [TemplateID(typeof(UnitEventTemplate))]
  713. public int TemplateID;
  714. public int ID { get { return TemplateID; } set { this.TemplateID = value; } }
  715. public override string ToString()
  716. {
  717. object obj = Editor.Instance.GetTemplateByType(TemplateID, typeof(UnitEventTemplate));
  718. return obj + "";
  719. }
  720. }
  721. #endregion
  722. }
  723. public class TemplatesIDConvert : TypeConverter
  724. {
  725. private TemplatesIDAttribute templatesID;
  726. public TemplatesIDConvert(TemplatesIDAttribute templatesID)
  727. {
  728. this.templatesID = templatesID;
  729. }
  730. public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  731. {
  732. return false;
  733. }
  734. public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
  735. {
  736. if (destinationType == typeof(string))
  737. {
  738. return true;
  739. }
  740. return false;
  741. }
  742. public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  743. {
  744. if (destinationType == typeof(string) && value.GetType() == typeof(List<int>))
  745. {
  746. int[] list = (value as List<int>).ToArray();
  747. StringBuilder sb = new StringBuilder();
  748. sb.Append(list.Length + "个单位");
  749. return sb.ToString();
  750. }
  751. else if (destinationType == typeof(string) && value.GetType() == typeof(int[]))
  752. {
  753. int[] list = (value as int[]);
  754. StringBuilder sb = new StringBuilder();
  755. sb.Append(list.Length + "个单位");
  756. return sb.ToString();
  757. }
  758. else
  759. {
  760. return base.ConvertTo(context, culture, value, destinationType);
  761. }
  762. }
  763. }
  764. }
  765. }