diff --git a/CLEditor/CLEngine.Editor.csproj b/CLEditor/CLEngine.Editor.csproj index 39785dd9ea2c4ffc348faac6adefde412b30ef27..850384bc3f5cec92948fd7d21a4fd4f61e0a7d0f 100644 --- a/CLEditor/CLEngine.Editor.csproj +++ b/CLEditor/CLEngine.Editor.csproj @@ -253,7 +253,7 @@ VisualScriptingNode.xaml - + diff --git a/CLEditor/core/CommandBindings.cs b/CLEditor/core/CommandBindings.cs deleted file mode 100644 index 76f87008789c6428de64dc71f2d856885546d3c9..0000000000000000000000000000000000000000 --- a/CLEditor/core/CommandBindings.cs +++ /dev/null @@ -1,10 +0,0 @@ -using System.Windows.Input; - -namespace CLEngine.Editor.core -{ - public static class CommandBindings - { - public static readonly RoutedUICommand FullDebug = new RoutedUICommand("完整调试", "FullDebug", typeof(MainWindow)); - public static readonly RoutedUICommand ToCollisionBlock = new RoutedUICommand("碰撞阻挡", "ToCollisionBlock", typeof(MainWindow)); - } -} \ No newline at end of file diff --git a/CLEditor/core/converts/EditorImageConvert.cs b/CLEditor/core/converts/EditorImageConvert.cs new file mode 100644 index 0000000000000000000000000000000000000000..655bfe8888c181cb2e7b152ef7f95014d9b96ade --- /dev/null +++ b/CLEditor/core/converts/EditorImageConvert.cs @@ -0,0 +1,32 @@ +using System; +using System.Globalization; +using System.IO; +using System.Windows.Data; +using System.Windows.Media.Imaging; +using CLEngine.Core; + +namespace CLEngine.Editor.core.converts +{ + /// + /// 编辑器内路径转图片 + /// + public class EditorImageConvert : IValueConverter + { + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + { + if (value is string path) + { + // 先转绝对路径 + var relativePath = Path.Combine(SceneManager.GameProject.ProjectPath, path); + return new BitmapImage(new Uri(relativePath, UriKind.RelativeOrAbsolute)); + } + + return null; + } + + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + { + throw new NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/CLEditor/viewmodel/DataBaseViewModel.cs b/CLEditor/viewmodel/DataBaseViewModel.cs index e5b6b8773e85c519b13908c5614be8735c753659..f5591e12aa1bacd13a4e0bfb51c59fd5ac15605a 100644 --- a/CLEditor/viewmodel/DataBaseViewModel.cs +++ b/CLEditor/viewmodel/DataBaseViewModel.cs @@ -1,8 +1,14 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Collections.ObjectModel; +using System.Linq; +using System.Windows.Forms; +using CLEngine.Core; using CLEngine.Core.framework; +using CLEngine.Editor.windows; using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.Command; +using MessageBox = System.Windows.Forms.MessageBox; namespace CLEngine.Editor.viewmodel { @@ -25,6 +31,19 @@ namespace CLEngine.Editor.viewmodel public List GenderTypes { get { return FrameworkSettings.GenderTypes; } } + /// + /// 物品分类 + /// + public List ItemTypes + { + get { return FrameworkSettings.ItemTypes.Keys.ToList(); } + } + + /// + /// 物品子类 + /// + public ObservableCollection ItemChildTypes { get; set; } + /// /// 职业列表 /// @@ -32,10 +51,113 @@ namespace CLEngine.Editor.viewmodel get { return FrameworkSettings.OccupationTypes; } } + /// + /// 背包图标浏览 + /// + public RelayCommand BagBrowserCommand { get; set; } + /// + /// 掉落图标浏览 + /// + public RelayCommand DropBrowserCommand { get; set; } + /// + /// 保存数据按钮 + /// + public RelayCommand SaveCommand { get; set; } + public DataBaseViewModel() { ItemObjects = new ObservableCollection(); AddItemCommand = new RelayCommand(AddItemAction); + ItemChildTypes = new ObservableCollection(); + // 我们需要数据通知子类发生变化 + ItemChildTypes.CollectionChanged += (sender, args) => { RaisePropertyChanged(() => ItemChildTypes); }; + + BagBrowserCommand = new RelayCommand(BagBrowserAction); + DropBrowserCommand = new RelayCommand(DropBrowserAction); + SaveCommand = new RelayCommand(SaveAction); + } + + private void SaveAction() + { + FrameworkManager.SaveData("item"); + } + + private void DropBrowserAction() + { + Logger.Error("还未支持"); + //var fileSelectDialog = new OpenFileDialog(); + //var itemObject = (ItemObject)DataBaseWindow.ItemListInstance.SelectedItem; + } + + private void BagBrowserAction() + { + var fileSelectDialog = new OpenFileDialog(); + var itemObject = (ItemObject)DataBaseWindow.ItemListInstance.SelectedItem; + if (itemObject != null) + { + itemObject.IconPath = ProcessDialog(fileSelectDialog, "Icon\\"); + RaisePropertyChanged(() => itemObject.IconPath); + } + } + + private string ProcessDialog(OpenFileDialog ofd, string specificFolder = "") + { + var result = ofd.ShowDialog(); + if (result == DialogResult.OK || result == DialogResult.Yes) + { + string destFolder = (SceneManager.GameProject.ProjectPath + "\\Content\\" + specificFolder).Trim(); + string filename = System.IO.Path.GetFileName(ofd.FileName); + + bool fileOnDirectory = ofd.FileName.StartsWith(SceneManager.GameProject.ProjectPath); + + if (!System.IO.Directory.Exists(destFolder) && !fileOnDirectory) + System.IO.Directory.CreateDirectory(destFolder); + + if (!System.IO.File.Exists(destFolder + filename) || fileOnDirectory) + return this.SetNewPath(ofd.FileName, destFolder, specificFolder, filename); + + var overwriteResult = MessageBox.Show("带有名称的文件 " + filename + " 已经存在。 你想覆盖它吗?", "警告", MessageBoxButtons.YesNoCancel); + if (overwriteResult == DialogResult.Yes) + return this.SetNewPath(ofd.FileName, destFolder, specificFolder, filename, true); + } + + return null; + } + + private string SetNewPath(string srcPath, string destFolder, string specificFolder, string filename, bool overwrite = false) + { + try + { + bool fileOnDirectory = srcPath.StartsWith(SceneManager.GameProject.ProjectPath); + + if (!fileOnDirectory) + System.IO.File.Copy(srcPath, destFolder + filename, overwrite); + + string relativePath = (@"\Content\" + specificFolder + filename).Trim(); + if (fileOnDirectory) + relativePath = srcPath.Replace(SceneManager.GameProject.ProjectPath, string.Empty); + + if (relativePath.StartsWith("\\")) + relativePath = relativePath.Substring(1, relativePath.Length - 1); + + return relativePath; + } + catch (Exception ex) + { + MessageBox.Show(ex.Message); + return null; + } + } + + /// + /// 清除子类数据并重新赋值 + /// + /// + public void ClearItemChildTypes(object value) + { + ItemChildTypes.Clear(); + foreach (var itemType in FrameworkSettings.ItemTypes[value.ToString()]) + ItemChildTypes.Add(itemType); } private void AddItemAction() diff --git a/CLEditor/windows/DataBaseWindow.xaml b/CLEditor/windows/DataBaseWindow.xaml index 8e91539dfcafa5e52a05c8ba57d29c8a709ddce3..fd3e467c2602a98ece3690391d8c8b9ddf2935d5 100644 --- a/CLEditor/windows/DataBaseWindow.xaml +++ b/CLEditor/windows/DataBaseWindow.xaml @@ -8,9 +8,15 @@ xmlns:controls="clr-namespace:CLEngine.Editor.controls" xmlns:framework="clr-namespace:CLEngine.Core.framework;assembly=CLEngine.Core" xmlns:xcad="http://schemas.xceed.com/wpf/xaml/avalondock" + xmlns:converts="clr-namespace:CLEngine.Editor.core.converts" x:Class="CLEngine.Editor.windows.DataBaseWindow" mc:Ignorable="d" DataContext="{Binding DataBase, Source={StaticResource Locator}}" Title="游戏数据库" Height="800" Width="1366" Background="{DynamicResource PanelBackground}"> + + + + + @@ -22,13 +28,13 @@ - + - + - + @@ -39,9 +45,9 @@ - - - + + + @@ -91,7 +97,7 @@ - + diff --git a/CLEditor/windows/DataBaseWindow.xaml.cs b/CLEditor/windows/DataBaseWindow.xaml.cs index 7206cae7058d8b97cb11297fbcf95018a28b1ef6..e4670c2b88f0698242b52b9c3d10f3cd3e460444 100644 --- a/CLEditor/windows/DataBaseWindow.xaml.cs +++ b/CLEditor/windows/DataBaseWindow.xaml.cs @@ -1,17 +1,6 @@ -using CLEngine.Core; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows; +using System.Windows; using System.Windows.Controls; -using System.Windows.Data; -using System.Windows.Documents; -using System.Windows.Input; -using System.Windows.Media; -using System.Windows.Media.Imaging; -using System.Windows.Shapes; +using CLEngine.Editor.viewmodel; namespace CLEngine.Editor.windows { @@ -20,10 +9,24 @@ namespace CLEngine.Editor.windows /// public partial class DataBaseWindow : Window { - public DataBaseWindow() + public static ListBox ItemListInstance; + + public DataBaseWindow() { InitializeComponent(); - } + ItemListInstance = ItemList; + ClassificationComboBox.SelectionChanged += ClassificationComboBoxOnSelectionChanged; + } + + /// + /// 分类下拉框值发生改变 + /// + /// + /// + private void ClassificationComboBoxOnSelectionChanged(object sender, SelectionChangedEventArgs e) + { + (DataContext as DataBaseViewModel)?.ClearItemChildTypes(ClassificationComboBox.SelectedValue); + } private void CloseBtn_Click(object sender, RoutedEventArgs e) { diff --git a/Engine/CLEngine.Core/CLEngine.Core.csproj b/Engine/CLEngine.Core/CLEngine.Core.csproj index c4bb1bf437aa339cdc142aba2a32344a419ee007..da368f324bc28582c129ac3a051f0e5d569d8f67 100644 --- a/Engine/CLEngine.Core/CLEngine.Core.csproj +++ b/Engine/CLEngine.Core/CLEngine.Core.csproj @@ -293,10 +293,13 @@ + + + diff --git a/Engine/CLEngine.Core/framework/FrameworkManager.cs b/Engine/CLEngine.Core/framework/FrameworkManager.cs index d220041e83c12a48dac0e15fecae4866be394f24..8f141203f65ab8abe476aa60e9d560f8959836ec 100644 --- a/Engine/CLEngine.Core/framework/FrameworkManager.cs +++ b/Engine/CLEngine.Core/framework/FrameworkManager.cs @@ -1,4 +1,9 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.IO; +using System.Linq; +using System.Windows.Forms; namespace CLEngine.Core.framework { @@ -7,7 +12,7 @@ namespace CLEngine.Core.framework /// public static class FrameworkManager { - private static Dictionary _dataFrameworks; + private static readonly Dictionary _dataFrameworks; static FrameworkManager() { @@ -19,7 +24,8 @@ namespace CLEngine.Core.framework /// /// 注册事件框架 /// - /// + /// 事件名 + /// 事件数据 public static void RegisterDataFramework(string name, IDataFramework framework) { _dataFrameworks.Add(name, framework); @@ -49,10 +55,13 @@ namespace CLEngine.Core.framework /// /// 加载所有数据 /// + [SuppressMessage("ReSharper", "ForCanBeConvertedToForeach")] public static void LoadAllData() { - foreach (var dataFramework in _dataFrameworks) - dataFramework.Value.LoadData(); + var dataFrameworkList = _dataFrameworks.ToList(); + // 不要使用foreach 列表发生更改时会报错 + for (int i = 0; i < dataFrameworkList.Count; i++) + dataFrameworkList[i].Value.LoadData(); } /// @@ -64,6 +73,22 @@ namespace CLEngine.Core.framework dataFramework.Value.SaveData(); } + /// + /// 保存指定数据 + /// + /// + public static void SaveData(string name) + { + try + { + _dataFrameworks[name].SaveData(); + } + catch (Exception e) + { + MessageBox.Show(e.StackTrace); + } + } + /// /// 改变指定数据 /// @@ -73,5 +98,49 @@ namespace CLEngine.Core.framework { _dataFrameworks[name] = data; } + + /// + /// 加载数据 + /// + /// 文件名 + public static object LoadFrameworkData(string name) + { + if (string.IsNullOrEmpty(SceneManager.GameProject.ProjectPath)) + throw new Exception("未加载工程前无法加载游戏数据"); + + var dataPath = Path.Combine(SceneManager.GameProject.ProjectPath, "Content", "Data"); + try + { + var path = Path.Combine(dataPath, name + ".data"); + + return File.Exists(path) ? CHelper.DeserializeObject(path) : null; + } + catch (Exception e) + { + throw new Exception(e.Message, e); + } + } + + /// + /// 保存数据 + /// + /// 文件名 + /// 数据 + public static void SaveFrameworkData(string name, object type) + { + if (string.IsNullOrEmpty(SceneManager.GameProject.ProjectPath)) + throw new Exception("未加载工程前无法保存游戏数据"); + + var dataPath = Path.Combine(SceneManager.GameProject.ProjectPath, "Content", "Data"); + try + { + Directory.CreateDirectory(dataPath); + CHelper.SerializeObject(Path.Combine(dataPath, name + ".data"), type); + } + catch (Exception e) + { + throw new Exception(e.Message, e); + } + } } } \ No newline at end of file diff --git a/Engine/CLEngine.Core/framework/FrameworkSettings.cs b/Engine/CLEngine.Core/framework/FrameworkSettings.cs index 6bd686a8dcab72d39e11e43407df04f5db4dfa2c..3a6db8c47faa4247238a43c14d950598e9411636 100644 --- a/Engine/CLEngine.Core/framework/FrameworkSettings.cs +++ b/Engine/CLEngine.Core/framework/FrameworkSettings.cs @@ -34,8 +34,8 @@ namespace CLEngine.Core.framework /// 物品列表 /// public static Dictionary> ItemTypes = new Dictionary>{ - {"物品",new List{"消耗品","任务用品","材料","礼包" } }, - {"装备",new List{"武器","鞋子","帽子","衣服","裤子","首饰","戒指" } }, + {"物品",new List{"消耗品","任务用品","材料","礼包" } }, + {"装备",new List{"武器","鞋子","帽子","衣服","裤子","首饰","戒指" } }, }; /// @@ -43,17 +43,17 @@ namespace CLEngine.Core.framework /// public static List GenderTypes = new List() { - "无", + "无", "男", "女" }; - /// - /// 职业列表 - /// - public static List OccupationTypes = new List() - { + /// + /// 职业列表 + /// + public static List OccupationTypes = new List() + { - }; - } + }; + } } \ No newline at end of file diff --git a/Engine/CLEngine.Core/framework/ItemManager.cs b/Engine/CLEngine.Core/framework/ItemManager.cs index a562e0ac0bda2c21e735c82a78a7e8638af30995..a9c48991a872e68af55f0517cc5a77d33bfed6a0 100644 --- a/Engine/CLEngine.Core/framework/ItemManager.cs +++ b/Engine/CLEngine.Core/framework/ItemManager.cs @@ -31,8 +31,8 @@ namespace CLEngine.Core.framework static ItemManager() { _worldItem = new Dictionary(); - _playerItem = new List(); - WorldId = 1; + _playerItem = new List(); + WorldId = 1; } /// @@ -43,7 +43,7 @@ namespace CLEngine.Core.framework var item = new ItemObject(name) { Id = WorldId }; _worldItem.Add(name, item); - WorldId++; + WorldId++; return item; } @@ -85,20 +85,9 @@ namespace CLEngine.Core.framework /// public void SaveData() { - if (string.IsNullOrEmpty(SceneManager.GameProject.ProjectPath)) - throw new Exception("未加载工程前无法保存物品信息"); - - var itemPath = Path.Combine(SceneManager.GameProject.ProjectPath, "Content", "Items"); - Directory.CreateDirectory(itemPath); - try - { - CHelper.SerializeObject(Path.Combine(itemPath, "worldItem.db"), this); - Logger.Info("物品保存成功"); - } - catch (Exception e) - { - throw new Exception(e.Message, e); - } + FrameworkManager.SaveFrameworkData("worldItem", _worldItem); + FrameworkManager.SaveFrameworkData("playerItem", _playerItem); + FrameworkManager.SaveFrameworkData("worldId", WorldId); } /// @@ -106,21 +95,13 @@ namespace CLEngine.Core.framework /// public void LoadData() { - if (string.IsNullOrEmpty(SceneManager.GameProject.ProjectPath)) - throw new Exception("未加载工程前无法加载物品信息"); - - var itemPath = Path.Combine(SceneManager.GameProject.ProjectPath, "Content", "Items"); - try - { - var itemManager = (ItemManager) CHelper.DeserializeObject(Path.Combine(itemPath, "worldItem.db")); - FrameworkManager.ChangeData("item", itemManager); - Logger.Info("物品加载成功"); - } - catch (Exception e) - { - throw new Exception(e.Message, e); - } + _worldItem = (Dictionary)FrameworkManager.LoadFrameworkData("worldItem") ?? new Dictionary(); + _playerItem = (List)FrameworkManager.LoadFrameworkData("playerItem") ?? new List(); + var worldId = FrameworkManager.LoadFrameworkData("worldId"); + if (worldId != null) + WorldId = (int) worldId; } + /// /// 世界物品中通过名称找物品 /// @@ -250,7 +231,7 @@ namespace CLEngine.Core.framework } } } - if (!is_finish||(!have&&item.OnlyGetOne)) + if (!is_finish || (!have && item.OnlyGetOne)) { onget.Number = number; _playerItem.Add(onget); diff --git a/Engine/CLEngine.Core/framework/ItemObject.cs b/Engine/CLEngine.Core/framework/ItemObject.cs index 41c1a7c8e4388fd1a24fa71a4595984bb96d4df1..fff307842c30c6405f203d29b7dfdf5cb04e7a04 100644 --- a/Engine/CLEngine.Core/framework/ItemObject.cs +++ b/Engine/CLEngine.Core/framework/ItemObject.cs @@ -76,7 +76,6 @@ namespace CLEngine.Core.framework [DataMember] private int _addAtkSpeed; [DataMember] private int _addAtkRange; [DataMember] private Dictionary _customProp; - [DataMember] private Image _guiIcon; [DataMember] private bool _isGuiShow; [DataMember] private int _lv; [DataMember] private string _child; @@ -101,6 +100,7 @@ namespace CLEngine.Core.framework [DataMember] private float _magicUseSpeed; [DataMember] private string _classification; [DataMember] private bool _onlyStoreOne; + [NonSerialized] private Image _guiIcon; [NonSerialized] private Texture2D _texture; private int _positionInBag; @@ -260,14 +260,17 @@ namespace CLEngine.Core.framework if (string.IsNullOrEmpty(_iconPath)) { IconTex = null; + return; } - else if (File.Exists(_iconPath)) + + var path = Path.Combine(SceneManager.GameProject.ProjectPath, _iconPath); + if (File.Exists(path)) { IconTex = TextureLoader.FromContent(_iconPath); } else { - throw new Exception("文件" + _iconPath + "不存在"); + throw new Exception("文件" + path + "不存在"); } } } diff --git a/Engine/CLEngine.Core/framework/SkillManager.cs b/Engine/CLEngine.Core/framework/SkillManager.cs index b66d867ef251e18f7de201b08bea080f91ec5aa7..c913b220dfa9306091b5aa77f5d05fa719177087 100644 --- a/Engine/CLEngine.Core/framework/SkillManager.cs +++ b/Engine/CLEngine.Core/framework/SkillManager.cs @@ -1,56 +1,56 @@ using System; using System.Collections.Generic; -using System.IO; using System.Runtime.Serialization; namespace CLEngine.Core.framework { - /// - /// 技能管理器 - /// + /// + /// 技能管理器 + /// #if WIN [Serializable] #endif [DataContract] - public class SkillManager : IDataFramework - { - /// - /// 全局技能 - /// - [DataMember] private static Dictionary _worldSkill; + public class SkillManager : IDataFramework + { + /// + /// 全局技能 + /// + [DataMember] private static Dictionary _worldSkill; - static SkillManager() - { - _worldSkill = new Dictionary(); - } + static SkillManager() + { + _worldSkill = new Dictionary(); + } - /// - /// 添加技能 - /// - /// 技能名 - /// 技能 - public static SkillObject AddSkill(string name) - { - var skill = new SkillObject(); - _worldSkill.Add(name, skill); + /// + /// 添加技能 + /// + /// 技能名 + /// 技能 + public static SkillObject AddSkill(string name) + { + var skill = new SkillObject(); + _worldSkill.Add(name, skill); - return skill; - } + return skill; + } /// /// 删除技能 /// /// 技能名 - public static void RemoveSkill(string name) - { - _worldSkill.Remove(name); - } + public static void RemoveSkill(string name) + { + _worldSkill.Remove(name); + } /// /// 获取技能列表 /// /// 技能列表 - public static List GetSkillList(){ + public static List GetSkillList() + { var skillList = new List(); foreach (var skill in _worldSkill) skillList.Add(skill.Value); @@ -63,20 +63,7 @@ namespace CLEngine.Core.framework /// public void LoadData() { - if (string.IsNullOrEmpty(SceneManager.GameProject.ProjectPath)) - throw new Exception("未加载工程前无法加载物品信息"); - - var skillPath = Path.Combine(SceneManager.GameProject.ProjectPath, "Content", "Skills"); - try - { - var skillManager = (ItemManager)CHelper.DeserializeObject(Path.Combine(skillPath, "worldSkill.db")); - FrameworkManager.ChangeData("skill", skillManager); - Logger.Info("技能加载成功"); - } - catch (Exception e) - { - throw new Exception(e.Message, e); - } + _worldSkill = (Dictionary)FrameworkManager.LoadFrameworkData("skills") ?? new Dictionary(); } /// @@ -84,20 +71,7 @@ namespace CLEngine.Core.framework /// public void SaveData() { - if (string.IsNullOrEmpty(SceneManager.GameProject.ProjectPath)) - throw new Exception("未加载工程前无法保存物品信息"); - - var itemPath = Path.Combine(SceneManager.GameProject.ProjectPath, "Content", "Skills"); - Directory.CreateDirectory(itemPath); - try - { - CHelper.SerializeObject(Path.Combine(itemPath, "worldSkill.db"), this); - Logger.Info("技能保存成功"); - } - catch (Exception e) - { - throw new Exception(e.Message, e); - } + FrameworkManager.SaveFrameworkData("skills", _worldSkill); } - } + } } \ No newline at end of file