using System; using System.Collections.Generic; using System.Text; using CommonFroms.Utils; using System.Reflection; using System.Drawing.Design; using CommonLang.Property; using CommonAI.Zone.Attributes; using CommonAI.Zone; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; using System.Globalization; using CommonAI.Zone.ZoneEditor; using System.IO; using CommonAIEditor.Scene; using CommonAI.ZoneEditor; using CommonAI.Zone.ZoneEditor.EventTrigger; using CommonAI.Zone.EventTrigger; using CommonLang; using System.Collections; using CommonFroms.G2D.DataGrid; using CommonFroms.G2D; using CommonAIEditor.Unit; namespace CommonAIEditor { public static class GameEditorPropertyAdapter { public static void Init() { G2DPropertyDescriptor.RegistPropertyAdapter(new DataAdapters()); } } public class SceneDataAdapters : IG2DPropertyAdapter { public int SceneID { get { return scene.Data.ID; } } public readonly SceneEditor scene; public readonly SceneEventEditor evtEditor; public SceneDataAdapters(SceneEditor scene, SceneEventEditor evtEditor = null) { this.scene = scene; this.evtEditor = evtEditor; } public UITypeEditor GetEditor(MemberInfo field, Type fieldType, object ownerData) { if (field is FieldInfo) { if (PropertyUtil.GetAttribute(field) != null) { return new ScriptEditor(SceneID, field as FieldInfo, ownerData); } else if (PropertyUtil.GetAttribute(field) != null) { return new SceneObjectIDEditor(scene, field as FieldInfo, ownerData); } else if (PropertyUtil.GetAttribute(field) != null) { return new SceneEventIDEditor(scene, field as FieldInfo, ownerData); } else if (PropertyUtil.GetAttribute(field) != null) { return new SceneVarIDEditor(scene, field as FieldInfo, ownerData); } else if (PropertyUtil.GetAttribute(field) != null) { if (evtEditor != null) { return new LocalVarIDEditor(evtEditor, field as FieldInfo, ownerData); } } } return null; } public TypeConverter GetConverter(MemberInfo field, Type fieldType, object ownerData) { return null; } public class ScriptEditor : UITypeEditor { private readonly int SceneID; private FieldInfo fieldInfo; private object ownerData; public ScriptEditor(int sceneID, FieldInfo field, object ownerData) { this.SceneID = sceneID; this.fieldInfo = field; this.ownerData = ownerData; } public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) { return UITypeEditorEditStyle.Modal; } public override object EditValue( ITypeDescriptorContext context, IServiceProvider provider, object value) { // IGameEditorPlugin plugin = EditorPlugin.CurrentPlugin; // FileInfo sfile = plugin.LaunchSceneScriptEditor(SceneID, ownerData); // if (sfile != null && sfile.Exists) // { // fieldInfo.SetValue(ownerData, sfile.Name); // return sfile.Name; // } return base.EditValue(context, provider, value); } } public class SceneObjectIDEditor : UITypeEditor { private SceneEditor scene; private FieldInfo fieldInfo; private object ownerData; private string objName; public SceneObjectIDEditor( SceneEditor scene, FieldInfo field, object ownerData) { this.scene = scene; this.fieldInfo = field; this.ownerData = ownerData; this.objName = fieldInfo.GetValue(ownerData) as string; } public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) { return UITypeEditorEditStyle.Modal; } public override object EditValue( ITypeDescriptorContext context, IServiceProvider provider, object value) { SceneObjectIDAttribute soid = PropertyUtil.GetAttribute(fieldInfo); SceneObjectData sobj = scene.ShowSelectSceneObject(soid.ObjectType, objName); if (sobj != null) { return sobj.Name; } return base.EditValue(context, provider, value); } } public class SceneEventIDEditor : UITypeEditor { private SceneEditor scene; private FieldInfo fieldInfo; private object ownerData; private string objName; public SceneEventIDEditor( SceneEditor scene, FieldInfo field, object ownerData) { this.scene = scene; this.fieldInfo = field; this.ownerData = ownerData; this.objName = fieldInfo.GetValue(ownerData) as string; } public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) { return UITypeEditorEditStyle.Modal; } public override object EditValue( ITypeDescriptorContext context, IServiceProvider provider, object value) { SceneEventIDAttribute soid = PropertyUtil.GetAttribute(fieldInfo); CommonAI.Zone.ZoneEditor.ZoneEvent sobj = scene.ShowSelectSceneEvent(objName); if (sobj != null) { return sobj.Name; } return base.EditValue(context, provider, value); } } public class SceneVarIDEditor : UITypeEditor { private SceneEditor scene; private FieldInfo fieldInfo; private object ownerData; private string objName; public SceneVarIDEditor( SceneEditor scene, FieldInfo field, object ownerData) { this.scene = scene; this.fieldInfo = field; this.ownerData = ownerData; this.objName = fieldInfo.GetValue(ownerData) as string; } public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) { return UITypeEditorEditStyle.Modal; } public override object EditValue( ITypeDescriptorContext context, IServiceProvider provider, object value) { SceneVarIDAttribute svid = PropertyUtil.GetAttribute(fieldInfo); ZoneVar vobj = scene.ShowSelectSceneVar(objName, svid.VarType); if (vobj != null) { return vobj.Key; } return base.EditValue(context, provider, value); } } public class LocalVarIDEditor : UITypeEditor { private SceneEventEditor scene; private FieldInfo fieldInfo; private object ownerData; private string objName; public LocalVarIDEditor( SceneEventEditor scene, FieldInfo field, object ownerData) { this.scene = scene; this.fieldInfo = field; this.ownerData = ownerData; this.objName = fieldInfo.GetValue(ownerData) as string; } public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) { return UITypeEditorEditStyle.Modal; } public override object EditValue( ITypeDescriptorContext context, IServiceProvider provider, object value) { LocalVarIDAttribute svid = PropertyUtil.GetAttribute(fieldInfo); LocalVar vobj = scene.ShowSelectLocalVar(objName, svid.VarType); if (vobj != null) { return vobj.Key; } return base.EditValue(context, provider, value); } } } public class UnitEventAdapters : IG2DPropertyAdapter { public readonly UnitEventEditor evtEditor; public UnitEventAdapters(UnitEventEditor evtEditor) { this.evtEditor = evtEditor; } public UITypeEditor GetEditor(MemberInfo field, Type fieldType, object ownerData) { if (field is FieldInfo && PropertyUtil.GetAttribute(field) != null) { return new UnitEventIDEditor(evtEditor, field as FieldInfo, ownerData); } return null; } public TypeConverter GetConverter(MemberInfo field, Type fieldType, object ownerData) { return null; } public class UnitEventIDEditor : UITypeEditor { private UnitEventEditor editor; private FieldInfo fieldInfo; private object ownerData; private string objName; public UnitEventIDEditor( UnitEventEditor editor, FieldInfo field, object ownerData) { this.editor = editor; this.fieldInfo = field; this.ownerData = ownerData; this.objName = fieldInfo.GetValue(ownerData) as string; } public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) { return UITypeEditorEditStyle.Modal; } public override object EditValue( ITypeDescriptorContext context, IServiceProvider provider, object value) { var soid = PropertyUtil.GetAttribute(fieldInfo); var sobj = editor.ShowSelectEvent(objName); if (sobj != null) { return sobj.Name; } return base.EditValue(context, provider, value); } } } public class DataAdapters : IG2DPropertyAdapter { public UITypeEditor GetEditor(MemberInfo field, Type fieldType, object ownerData) { if (field is FieldInfo) { if (PropertyUtil.GetAttribute(field) != null) { return new ObjectMemberNameEditor(field as FieldInfo, ownerData); } if (PropertyUtil.GetAttribute(field) != null) { return new TemplateIDEditor(field as FieldInfo, ownerData, PropertyUtil.GetAttribute(field)); } if (PropertyUtil.GetAttribute(field) != null) { return new TemplatesIDEditor(field as FieldInfo, ownerData, PropertyUtil.GetAttribute(field)); } if (PropertyUtil.GetAttribute(field) != null) { return new ResourceIDEditor(field as FieldInfo, ownerData, PropertyUtil.GetAttribute(field)); } if (PropertyUtil.GetAttribute(field) != null) { return new G2DColorEditor(field, ownerData); } } return null; } public TypeConverter GetConverter(MemberInfo field, Type fieldType, object ownerData) { if (field is FieldInfo) { TemplateIDAttribute tid = PropertyUtil.GetAttribute(field); if (tid != null) { return new TemplateIDConvert(tid); } TemplatesIDAttribute tsid = PropertyUtil.GetAttribute(field); if (tsid != null) { return new TemplatesIDConvert(tsid); } DescAttribute desc = PropertyUtil.GetAttribute(field); ColorValueAttribute color = PropertyUtil.GetAttribute(field); if (color != null && fieldType.Equals(typeof(int))) { return new G2DIntToHexTypeConverter(); } } return null; } /// /// AbstractValue 获取对象字段 /// public class ObjectMemberNameEditor : UITypeEditor { private FieldInfo fieldInfo; private object ownerData; private ObjectMemberNameAttribute member; private Type ownerType; public ObjectMemberNameEditor(FieldInfo field, object ownerData) { this.fieldInfo = field; this.ownerData = ownerData; this.member = PropertyUtil.GetAttribute(field); if (ownerData is IFieldMemberValue) { ownerType = (ownerData as IFieldMemberValue).OwnerType; } else { ownerType = member.ObjectType; } } public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) { return UITypeEditorEditStyle.Modal; } public override object EditValue( ITypeDescriptorContext context, IServiceProvider provider, object value) { if (ownerType != null) { FieldsMap fm = GameFields.Manager.GetFields(ownerType, member.FieldType); if (fm != null) { G2DListSelectEditor dialog = new G2DListSelectEditor( fm.ListFields, fm.GetFieldDesc(value + "")); if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { value = dialog.SelectedObject.DataField.Name; return value; } } } return base.EditValue(context, provider, value); } } public class ResourceIDEditor : UITypeEditor { private Brush brush = new SolidBrush(Color.Green); private FieldInfo fieldInfo; private object ownerData; private ResourceIDAttribute resourceID; public ResourceIDEditor(FieldInfo field, object ownerData, ResourceIDAttribute tid) { this.fieldInfo = field; this.ownerData = ownerData; this.resourceID = tid; } public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) { return UITypeEditorEditStyle.Modal; } public override object EditValue( ITypeDescriptorContext context, IServiceProvider provider, object value) { string id = (string)fieldInfo.GetValue(ownerData); { string fullPath = Editor.EditorRootDir + "\\" + id; fullPath = fullPath.Replace('/', '\\'); fullPath = fullPath.Replace("\\\\", "\\"); OpenFileDialog fd = new OpenFileDialog(); DirectoryInfo resdir = new DirectoryInfo(Editor.EditorRootDir + "/../xmds_Data/StreamingAssets/AssetBundles/PC"); fd.InitialDirectory = resdir.FullName + "\\res"; fd.FileName = fullPath; fd.Multiselect = false; if (fd.ShowDialog() == DialogResult.OK) { FileInfo fdinfo = new FileInfo(fd.FileName); if (fdinfo.FullName.StartsWith(resdir.FullName)) { id = fdinfo.FullName.Substring(resdir.FullName.Length); id = id.Replace('\\', '/'); return id; } else { MessageBox.Show("无法引用编辑器目录之外的资源"); } } } return base.EditValue(context, provider, value); } } public class TemplateIDEditor : UITypeEditor { private FieldInfo fieldInfo; private object ownerData; private TemplateIDAttribute templateID; public TemplateIDEditor(FieldInfo field, object ownerData, TemplateIDAttribute tid) { this.fieldInfo = field; this.ownerData = ownerData; this.templateID = tid; } public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) { return UITypeEditorEditStyle.Modal; } public override object EditValue( ITypeDescriptorContext context, IServiceProvider provider, object value) { int id = (int)fieldInfo.GetValue(ownerData); object obj = Editor.Instance.GetTemplateByType(id, templateID.TemplateType); if (Editor.Instance.ShowSelectTemplateIDDialog(obj, templateID.TemplateType, out id)) { return id; } return base.EditValue(context, provider, value); } } public class TemplateIDConvert : TypeConverter { private TemplateIDAttribute templateID; public TemplateIDConvert(TemplateIDAttribute templateID) { this.templateID = templateID; } public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) { if (sourceType == typeof(string)) { return true; } return false; } public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { if (destinationType == typeof(string)) { return true; } return false; } public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (destinationType == typeof(string) && value.GetType() == typeof(int)) { int id = (int)value; object obj = Editor.Instance.GetTemplateByType(id, templateID.TemplateType); return obj + ""; } else { return base.ConvertTo(context, culture, value, destinationType); } } public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value.GetType() == typeof(string)) { int id = 0; try { string text = value.ToString(); int i1 = text.LastIndexOf("(") + 1; int i2 = text.LastIndexOf(")"); text = text.Substring(i1, i2 - i1); id = int.Parse(text); } catch (Exception err) { } return id; } else { return base.ConvertFrom(context, culture, value); } } } public class TemplatesIDEditor : UITypeEditor { private FieldInfo fieldInfo; private object ownerData; private TemplatesIDAttribute templatesID; public TemplatesIDEditor(FieldInfo field, object ownerData, TemplatesIDAttribute tid) { this.fieldInfo = field; this.ownerData = ownerData; this.templatesID = tid; } public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) { return UITypeEditorEditStyle.Modal; } public override object EditValue( ITypeDescriptorContext context, IServiceProvider provider, object value) { if (fieldInfo.FieldType == typeof(List) || fieldInfo.FieldType == typeof(int[])) { int[] list = new int[] { }; if (value != null) { if (fieldInfo.FieldType == typeof(List)) { list = (value as List).ToArray(); } else if (fieldInfo.FieldType == typeof(int[])) { list = (value as int[]); } } object listvalue = GetListByType(templatesID.TemplateType, list); G2DCollectionEditor listedit = new G2DCollectionEditor( listvalue.GetType(), listvalue, new Type[] { GetListElementType(templatesID.TemplateType) }); if (listedit.ShowDialog() == DialogResult.OK) { List ret = GetListValue( templatesID.TemplateType, listedit.GetEditCompleteData()); if (fieldInfo.FieldType == typeof(List)) { return ret; } else if (fieldInfo.FieldType == typeof(int[])) { return ret.ToArray(); } } } return base.EditValue(context, provider, value); } //------------------------------------------------------------------------------------------- #region _static_ private static HashMap mElementTypeMap = new HashMap(); private static HashMap mListTypeMap = new HashMap(); static TemplatesIDEditor() { mElementTypeMap[typeof(UnitInfo)] = typeof(UnitListItem); mElementTypeMap[typeof(SkillTemplate)] = typeof(SkillListItem); mElementTypeMap[typeof(SpellTemplate)] = typeof(SpellListItem); mElementTypeMap[typeof(BuffTemplate)] = typeof(BuffListItem); mElementTypeMap[typeof(ItemTemplate)] = typeof(ItemListItem); mElementTypeMap[typeof(UnitTriggerTemplate)] = typeof(UnitTriggerListItem); mElementTypeMap[typeof(UnitEventTemplate)] = typeof(UnitEventListItem); mListTypeMap[typeof(UnitInfo)] = typeof(List); mListTypeMap[typeof(SkillTemplate)] = typeof(List); mListTypeMap[typeof(SpellTemplate)] = typeof(List); mListTypeMap[typeof(BuffTemplate)] = typeof(List); mListTypeMap[typeof(ItemTemplate)] = typeof(List); mListTypeMap[typeof(UnitTriggerTemplate)] = typeof(List); mListTypeMap[typeof(UnitEventTemplate)] = typeof(List); } public static Type GetListElementType(Type template_type) { return mElementTypeMap[template_type]; } static public IList GetListByType(Type type, int[] list) { Type list_type = mListTypeMap[type]; Type element_type = mElementTypeMap[type]; IList ret = (IList)Activator.CreateInstance(list_type); foreach (int tid in list) { IListItem item = (IListItem)Activator.CreateInstance(element_type); item.ID = tid; ret.Add(item); } return ret; } static public List GetListValue(Type type, object completeData) { List ret = new List(); IList list = completeData as IList; foreach (IListItem tid in list) { ret.Add(tid.ID); } return ret; } public interface IListItem { int ID { get; set; } } public class UnitListItem : IListItem { [TemplateID(typeof(UnitInfo))] public int TemplateID; public int ID { get { return TemplateID; } set { this.TemplateID = value; } } public override string ToString() { object obj = Editor.Instance.GetTemplateByType(TemplateID, typeof(UnitInfo)); return obj + ""; } } public class SkillListItem : IListItem { [TemplateID(typeof(SkillTemplate))] public int TemplateID; public int ID { get { return TemplateID; } set { this.TemplateID = value; } } public override string ToString() { object obj = Editor.Instance.GetTemplateByType(TemplateID, typeof(SkillTemplate)); return obj + ""; } } public class SpellListItem : IListItem { [TemplateID(typeof(SpellTemplate))] public int TemplateID; public int ID { get { return TemplateID; } set { this.TemplateID = value; } } public override string ToString() { object obj = Editor.Instance.GetTemplateByType(TemplateID, typeof(SpellTemplate)); return obj + ""; } } public class BuffListItem : IListItem { [TemplateID(typeof(BuffTemplate))] public int TemplateID; public int ID { get { return TemplateID; } set { this.TemplateID = value; } } public override string ToString() { object obj = Editor.Instance.GetTemplateByType(TemplateID, typeof(BuffTemplate)); return obj + ""; } } public class ItemListItem : IListItem { [TemplateID(typeof(ItemTemplate))] public int TemplateID; public int ID { get { return TemplateID; } set { this.TemplateID = value; } } public override string ToString() { object obj = Editor.Instance.GetTemplateByType(TemplateID, typeof(ItemTemplate)); return obj + ""; } } public class UnitTriggerListItem : IListItem { [TemplateID(typeof(UnitTriggerTemplate))] public int TemplateID; public int ID { get { return TemplateID; } set { this.TemplateID = value; } } public override string ToString() { object obj = Editor.Instance.GetTemplateByType(TemplateID, typeof(UnitTriggerTemplate)); return obj + ""; } } public class UnitEventListItem : IListItem { [TemplateID(typeof(UnitEventTemplate))] public int TemplateID; public int ID { get { return TemplateID; } set { this.TemplateID = value; } } public override string ToString() { object obj = Editor.Instance.GetTemplateByType(TemplateID, typeof(UnitEventTemplate)); return obj + ""; } } #endregion } public class TemplatesIDConvert : TypeConverter { private TemplatesIDAttribute templatesID; public TemplatesIDConvert(TemplatesIDAttribute templatesID) { this.templatesID = templatesID; } public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) { return false; } public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { if (destinationType == typeof(string)) { return true; } return false; } public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (destinationType == typeof(string) && value.GetType() == typeof(List)) { int[] list = (value as List).ToArray(); StringBuilder sb = new StringBuilder(); sb.Append(list.Length + "个单位"); return sb.ToString(); } else if (destinationType == typeof(string) && value.GetType() == typeof(int[])) { int[] list = (value as int[]); StringBuilder sb = new StringBuilder(); sb.Append(list.Length + "个单位"); return sb.ToString(); } else { return base.ConvertTo(context, culture, value, destinationType); } } } } }