Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEditor;
- using System.Collections.Generic;
- using System.Linq;
- using RPG.Items;
- using System;
- namespace RPG.Editor.Items
- {
- public class ItemDatabaseEditor : UnityEditor.EditorWindow
- {
- private const string DATABASE_PATH = @"Assets/Database/ItemDatabase.asset";
- private RPG.Items.ItemDatabase m_itemDatabase;
- private Vector2 _scrollPos;
- private List<bool> m_categoryExpanded;
- private Item m_selectedItem;
- private int m_selectedItem_SelectedCategory;
- private int m_selectedItem_SelectedClass;
- [MenuItem("RPG/Databases/Item Database %#i")]
- public static void Open() {
- ItemDatabaseEditor window = EditorWindow.GetWindow<ItemDatabaseEditor>();
- window.minSize = new Vector2(800, 400);
- window.Show();
- }
- void OnEnable()
- {
- m_itemDatabase = (RPG.Items.ItemDatabase)AssetDatabase.LoadAssetAtPath(DATABASE_PATH, typeof(RPG.Items.ItemDatabase));
- if (m_itemDatabase == null)
- {
- m_itemDatabase = ScriptableObject.CreateInstance<RPG.Items.ItemDatabase>();
- AssetDatabase.CreateAsset(m_itemDatabase, DATABASE_PATH);
- AssetDatabase.SaveAssets();
- AssetDatabase.Refresh();
- }
- Init();
- }
- void Init()
- {
- if (!m_itemDatabase.Categories.Contains("Unassigned"))
- {
- m_itemDatabase.Categories.Add("Unassigned");
- }
- m_categoryExpanded = new List<bool>();
- for (int i = 0; i < m_itemDatabase.CategoryCount(); i++)
- {
- m_categoryExpanded.Add(false);
- }
- }
- void OnGUI()
- {
- EditorGUILayout.BeginHorizontal(GUILayout.ExpandWidth(true));
- {
- DrawList();
- DrawDetails();
- }
- EditorGUILayout.EndHorizontal();
- }
- void DrawList()
- {
- EditorGUILayout.BeginVertical(GUILayout.Width(250));
- {
- EditorGUILayout.Space();
- _scrollPos = EditorGUILayout.BeginScrollView(_scrollPos, "box", GUILayout.ExpandHeight(true));
- {
- string[] l_categories = m_itemDatabase.GetCategories();
- for (int i = 0; i < l_categories.Length; i++)
- {
- m_categoryExpanded[i] = EditorGUILayout.Foldout(m_categoryExpanded[i], l_categories[i]);
- if (m_categoryExpanded[i])
- {
- Item[] l_items = m_itemDatabase.GetItemsInCategory(l_categories[i]);
- for (int j = 0; j < l_items.Length; j++)
- {
- if (m_selectedItem != null && m_selectedItem.m_id == l_items[j].m_id)
- {
- DrawSelectedItem(l_items[j]);
- }
- else
- {
- DrawUnselectedItem(l_items[j]);
- }
- }
- }
- }
- }
- EditorGUILayout.EndScrollView();
- EditorGUILayout.BeginHorizontal();
- {
- if (GUILayout.Button("Edit...", GUILayout.Width(125)))
- {
- GenericMenu l_addItemMenu = new GenericMenu();
- l_addItemMenu.AddItem(new GUIContent("Add Item/Generic Item", "Add a new Generic Item."), false, MenuOption_AddNewItem, "Item");
- l_addItemMenu.AddItem(new GUIContent("Add Item/Consumable Item", "Add a new Consumable Item."), false, MenuOption_AddNewItem, "Consumable");
- l_addItemMenu.AddItem(new GUIContent("Add Item/Quest Item", "Add a new Quest Item."), false, MenuOption_AddNewItem, "Quest");
- l_addItemMenu.AddItem(new GUIContent("Add Item/Armor Item", "Add a new Armor Item."), false, MenuOption_AddNewItem, "Armor");
- l_addItemMenu.AddItem(new GUIContent("Add Item/Weapon nItem", "Add a new Weapon Item."), false, MenuOption_AddNewItem, "Weapon");
- l_addItemMenu.AddSeparator("");
- l_addItemMenu.AddItem(new GUIContent("Add Category"), false, MenuOption_AddCategory);
- if (this.m_itemDatabase.CategoryCount() >= 2)
- {
- l_addItemMenu.AddItem(new GUIContent("Remove Category"), false, MenuOption_RemoveCategory);
- }
- else
- {
- l_addItemMenu.AddDisabledItem(new GUIContent("Remove Category"));
- }
- l_addItemMenu.AddSeparator("");
- if (m_selectedItem != null)
- {
- l_addItemMenu.AddItem(new GUIContent("Remove Selected Item", "Removes the Selected Item."), false, MenuOption_RemoveSelectedItem);
- }
- else
- {
- l_addItemMenu.AddDisabledItem(new GUIContent("Remove Selected Item", "Removes the Selected Item."));
- }
- l_addItemMenu.ShowAsContext();
- }
- if (GUILayout.Button("Xml...", GUILayout.Width(125)))
- {
- GenericMenu l_addItemMenu = new GenericMenu();
- l_addItemMenu.AddItem(new GUIContent("Import Xml", "Imports an xml file to the ItemDatabase."), false, MenuOption_ImportXML);
- l_addItemMenu.AddItem(new GUIContent("Export Xml", "Exports the entire ItemDatabase to an Xml File."), false, MenuOption_ExportXML);
- l_addItemMenu.ShowAsContext();
- }
- }
- EditorGUILayout.EndHorizontal();
- EditorGUILayout.Space();
- }
- EditorGUILayout.EndVertical();
- }
- void DrawDetails()
- {
- if (m_selectedItem != null)
- {
- EditorGUILayout.BeginVertical("box", GUILayout.Width(530.0f), GUILayout.Height(390.0f));
- {
- EditorGUILayout.Space();
- EditorGUILayout.BeginHorizontal();
- {
- if (GUILayout.Button(RPG.Utils.TextureUtils.GetTextureFromSprite(m_selectedItem.m_sprite), GUILayout.Width(64.0f), GUILayout.Height(64.0f)))
- {
- int controlID = EditorGUIUtility.GetControlID(FocusType.Passive);
- EditorGUIUtility.ShowObjectPicker<Sprite>(m_selectedItem.m_sprite, false, "", controlID);
- }
- if (Event.current.commandName == "ObjectSelectorUpdated")
- {
- Sprite sprite = EditorGUIUtility.GetObjectPickerObject() as Sprite;
- m_selectedItem.m_sprite = sprite;
- this.Repaint();
- }
- GUILayout.Space(15.0f);
- EditorGUILayout.BeginVertical();
- {
- GUILayout.Space(16.0f);
- EditorGUILayout.LabelField(m_selectedItem.m_name, StyleManager.Instance.largeLabel, GUILayout.Width(300.0f));
- }
- EditorGUILayout.EndVertical();
- }
- EditorGUILayout.EndHorizontal();
- EditorGUILayout.Space();
- EditorGUILayout.BeginHorizontal();
- {
- EditorGUILayout.LabelField("Category:", GUILayout.Width(100.0f));
- m_selectedItem.m_selectedCategory = this.m_itemDatabase.FindCategoryIndex(this.m_selectedItem.m_category);
- if (m_selectedItem.m_selectedCategory > -1)
- {
- m_selectedItem.m_selectedCategory = EditorGUILayout.Popup(this.m_selectedItem.m_selectedCategory, this.m_itemDatabase.GetCategories(), GUILayout.Width(200.0f));
- this.m_selectedItem.m_category = this.m_itemDatabase.GetCategories()[this.m_selectedItem.m_selectedCategory];
- }
- }
- EditorGUILayout.EndHorizontal();
- EditorGUILayout.BeginHorizontal();
- {
- EditorGUILayout.LabelField("Class:", GUILayout.Width(100.0f));
- m_selectedItem.m_selectedClass = this.m_itemDatabase.FindClassIndex(this.m_selectedItem.m_class);
- if (m_selectedItem.m_selectedClass > -1)
- {
- m_selectedItem.m_selectedClass = EditorGUILayout.Popup(this.m_selectedItem.m_selectedClass, this.m_itemDatabase.m_classes, GUILayout.Width(200.0f));
- this.m_selectedItem.m_class = this.m_itemDatabase.m_classes[this.m_selectedItem.m_selectedClass];
- }
- }
- EditorGUILayout.EndHorizontal();
- EditorGUILayout.BeginHorizontal();
- {
- EditorGUILayout.LabelField("Id:", GUILayout.Width(100.0f));
- m_selectedItem.m_id = EditorGUILayout.TextField(m_selectedItem.m_id, GUILayout.Width(200.0f));
- }
- EditorGUILayout.EndHorizontal();
- EditorGUILayout.BeginHorizontal();
- {
- EditorGUILayout.LabelField("Name:", GUILayout.Width(100.0f));
- m_selectedItem.m_name = EditorGUILayout.TextField(m_selectedItem.m_name, GUILayout.Width(200.0f));
- }
- EditorGUILayout.EndHorizontal();
- EditorGUILayout.Space();
- EditorGUILayout.BeginHorizontal();
- {
- EditorGUILayout.LabelField("Description:", GUILayout.Width(100.0f));
- GUIStyle style = StyleManager.Instance.textAreaStyle;
- style.fixedWidth = 300.0f;
- style.fixedHeight = style.lineHeight * 6;
- m_selectedItem.m_description = EditorGUILayout.TextArea(m_selectedItem.m_description, style);
- }
- EditorGUILayout.EndHorizontal();
- EditorGUILayout.Space();
- }
- EditorGUILayout.EndVertical();
- }
- }
- void DrawSelectedItem(Item l_item)
- {
- GUIStyle l_style = new GUIStyle("box");
- l_style.normal.background = RPG.Utils.TextureUtils.CreateEmptyTexture(240, 36, Color.cyan);
- EditorGUILayout.BeginHorizontal(l_style, GUILayout.Width(240.0f), GUILayout.Height(36.0f));
- {
- Rect l_rect = EditorGUILayout.GetControlRect();
- l_rect.x = 8.0f;
- l_rect.width = 32.0f;
- l_rect.height = 32.0f;
- GUI.Label(l_rect, RPG.Utils.TextureUtils.GetTextureFromSprite(l_item.m_sprite));
- l_rect.x += 40.0f;
- l_rect.y += 8.0f;
- l_rect.width = 190.0f;
- l_rect.height = 32.0f;
- GUI.Label(l_rect, l_item.m_name);
- }
- EditorGUILayout.EndHorizontal();
- }
- void DrawUnselectedItem(Item l_item)
- {
- GUIStyle l_style = new GUIStyle("box");
- l_style.normal.background = RPG.Utils.TextureUtils.CreateEmptyTexture(240, 36, Color.white);
- EditorGUILayout.BeginHorizontal(l_style, GUILayout.Width(240.0f), GUILayout.Height(36.0f));
- {
- Rect l_rect = EditorGUILayout.GetControlRect();
- l_rect.x -= 4.0f;
- l_rect.y -= 4.0f;
- l_rect.height = 38.0f;
- l_rect.width = 240.0f;
- GUIStyle style = new GUIStyle();
- if (GUI.Button(l_rect, "", style))
- {
- if (Event.current.button == 0)
- {
- m_selectedItem = null;
- m_selectedItem = l_item;
- }
- }
- l_rect.x = 8.0f;
- l_rect.y += 4.0f;
- l_rect.width = 32.0f;
- l_rect.height = 32.0f;
- GUI.Label(l_rect, RPG.Utils.TextureUtils.GetTextureFromSprite(l_item.m_sprite));
- l_rect.x += 40.0f;
- l_rect.y += 8.0f;
- l_rect.width = 190.0f;
- l_rect.height = 32.0f;
- GUI.Label(l_rect, l_item.m_name);
- }
- EditorGUILayout.EndHorizontal();
- }
- void MenuOption_AddNewItem(object obj)
- {
- if (obj.ToString() == "Item")
- {
- Item l_item = new Item();
- l_item.m_class = "Item";
- l_item.m_id = "newItem";
- l_item.m_name = "{New Item}";
- l_item.m_category = "Unassigned";
- bool success = this.m_itemDatabase.AddItem(l_item);
- if (!success)
- {
- EditorUtility.DisplayDialog("Item Exists!", "An item {m_id: " + l_item.m_id + "} already exists.", "Ok");
- }
- }
- else if (obj.ToString() == "Consumable")
- {
- ConsumableItem l_item = new ConsumableItem();
- l_item.m_class = "Consumable";
- l_item.m_id = "newConsumableItem";
- l_item.m_name = "{New Consumable Item}";
- l_item.m_category = "Unassigned";
- bool success = this.m_itemDatabase.AddItem(l_item);
- if (!success)
- {
- EditorUtility.DisplayDialog("Item Exists!", "An item {m_id: " + l_item.m_id + "} already exists.", "Ok");
- }
- }
- else if (obj.ToString() == "Quest")
- {
- QuestItem l_item = new QuestItem();
- l_item.m_class = "Quest";
- l_item.m_id = "newQuestItem";
- l_item.m_name = "{New Quest Item}";
- l_item.m_category = "Unassigned";
- bool success = this.m_itemDatabase.AddItem(l_item);
- if (!success)
- {
- EditorUtility.DisplayDialog("Item Exists!", "An item {m_id: " + l_item.m_id + "} already exists.", "Ok");
- }
- }
- else if (obj.ToString() == "Armor")
- {
- ArmorItem l_item = new ArmorItem();
- l_item.m_class = "Armor";
- l_item.m_id = "newArmorItem";
- l_item.m_name = "{New Armor Item}";
- l_item.m_category = "Unassigned";
- bool success = this.m_itemDatabase.AddItem(l_item);
- if (!success)
- {
- EditorUtility.DisplayDialog("Item Exists!", "An item {m_id: " + l_item.m_id + "} already exists.", "Ok");
- }
- }
- else if (obj.ToString() == "Weapon")
- {
- WeaponItem l_item = new WeaponItem();
- l_item.m_class = "Weapon";
- l_item.m_id = "newWeaponItem";
- l_item.m_name = "{New Weapon Item}";
- l_item.m_category = "Unassigned";
- bool success = this.m_itemDatabase.AddItem(l_item);
- if (!success)
- {
- EditorUtility.DisplayDialog("Item Exists!", "An item {m_id: " + l_item.m_id + "} already exists.", "Ok");
- }
- }
- }
- void MenuOption_RemoveSelectedItem()
- {
- if (this.m_selectedItem != null)
- {
- this.m_itemDatabase.RemoveItem(this.m_selectedItem.m_id);
- this.m_selectedItem = null;
- this.Repaint();
- }
- }
- void MenuOption_ImportXML()
- {
- string path = EditorUtility.OpenFilePanel(
- "Import Xml File",
- "",
- "xml");
- path = path.Replace(Application.dataPath, "Assets");
- if (path.Length != 0)
- {
- TextAsset text = AssetDatabase.LoadAssetAtPath(path,typeof(TextAsset)) as TextAsset;
- m_itemDatabase.LoadFromXml(text);
- this.Repaint();
- }
- }
- void MenuOption_ExportXML()
- {
- throw new NotImplementedException();
- }
- void MenuOption_AddCategory()
- {
- NewCategoryWindow.Show(CreateCategory, this.position);
- }
- void CreateCategory(string category)
- {
- if (!this.m_itemDatabase.Categories.Contains(category))
- {
- this.m_itemDatabase.Categories.Add(category);
- m_categoryExpanded.Add(false);
- this.Repaint();
- }
- }
- void MenuOption_RemoveCategory()
- {
- RemoveCategoryWindow.Show(this.m_itemDatabase.GetCategories(), RemoveCategory, this.position);
- }
- void RemoveCategory(string category)
- {
- if (this.m_itemDatabase.Categories.Contains(category))
- {
- Item[] l_items = this.m_itemDatabase.GetItemsInCategory(category);
- for (int i = 0; i < l_items.Length; i++)
- {
- l_items[i].m_category = "Unassigned";
- }
- this.m_itemDatabase.Categories.Remove(category);
- m_categoryExpanded.Clear();
- for (int i = 0; i < m_itemDatabase.CategoryCount(); i++)
- {
- m_categoryExpanded.Add(false);
- }
- this.Repaint();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement