Advertisement
Guest User

Untitled

a guest
Mar 19th, 2015
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 18.56 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using RPG.Items;
  6. using System;
  7.  
  8. namespace RPG.Editor.Items
  9. {
  10.     public class ItemDatabaseEditor : UnityEditor.EditorWindow
  11.     {
  12.         private const string DATABASE_PATH = @"Assets/Database/ItemDatabase.asset";
  13.         private RPG.Items.ItemDatabase m_itemDatabase;
  14.         private Vector2 _scrollPos;
  15.         private List<bool> m_categoryExpanded;
  16.  
  17.         private Item m_selectedItem;
  18.         private int m_selectedItem_SelectedCategory;
  19.         private int m_selectedItem_SelectedClass;
  20.  
  21.         [MenuItem("RPG/Databases/Item Database  %#i")]
  22.         public static void Open() {
  23.             ItemDatabaseEditor window = EditorWindow.GetWindow<ItemDatabaseEditor>();
  24.             window.minSize = new Vector2(800, 400);
  25.             window.Show();
  26.         }
  27.  
  28.         void OnEnable()
  29.         {
  30.             m_itemDatabase = (RPG.Items.ItemDatabase)AssetDatabase.LoadAssetAtPath(DATABASE_PATH, typeof(RPG.Items.ItemDatabase));
  31.  
  32.             if (m_itemDatabase == null)
  33.             {
  34.                 m_itemDatabase = ScriptableObject.CreateInstance<RPG.Items.ItemDatabase>();
  35.                 AssetDatabase.CreateAsset(m_itemDatabase, DATABASE_PATH);
  36.                 AssetDatabase.SaveAssets();
  37.                 AssetDatabase.Refresh();
  38.             }
  39.  
  40.             Init();
  41.         }
  42.  
  43.         void Init()
  44.         {
  45.             if (!m_itemDatabase.Categories.Contains("Unassigned"))
  46.             {
  47.                 m_itemDatabase.Categories.Add("Unassigned");
  48.             }
  49.  
  50.             m_categoryExpanded = new List<bool>();
  51.  
  52.             for (int i = 0; i < m_itemDatabase.CategoryCount(); i++)
  53.             {
  54.                 m_categoryExpanded.Add(false);
  55.             }
  56.         }
  57.  
  58.         void OnGUI()
  59.         {
  60.             EditorGUILayout.BeginHorizontal(GUILayout.ExpandWidth(true));
  61.             {
  62.                 DrawList();
  63.                 DrawDetails();
  64.             }
  65.             EditorGUILayout.EndHorizontal();
  66.         }
  67.  
  68.         void DrawList()
  69.         {
  70.             EditorGUILayout.BeginVertical(GUILayout.Width(250));
  71.             {
  72.                 EditorGUILayout.Space();
  73.                 _scrollPos = EditorGUILayout.BeginScrollView(_scrollPos, "box", GUILayout.ExpandHeight(true));
  74.                 {
  75.                     string[] l_categories = m_itemDatabase.GetCategories();
  76.  
  77.                     for (int i = 0; i < l_categories.Length; i++)
  78.                     {
  79.                         m_categoryExpanded[i] = EditorGUILayout.Foldout(m_categoryExpanded[i], l_categories[i]);
  80.  
  81.                         if (m_categoryExpanded[i])
  82.                         {
  83.                             Item[] l_items = m_itemDatabase.GetItemsInCategory(l_categories[i]);
  84.  
  85.                             for (int j = 0; j < l_items.Length; j++)
  86.                             {
  87.                                 if (m_selectedItem != null && m_selectedItem.m_id == l_items[j].m_id)
  88.                                 {
  89.                                     DrawSelectedItem(l_items[j]);
  90.                                 }
  91.                                 else
  92.                                 {
  93.                                     DrawUnselectedItem(l_items[j]);
  94.                                 }
  95.                             }
  96.                         }
  97.                     }
  98.  
  99.                 }
  100.                 EditorGUILayout.EndScrollView();
  101.  
  102.                 EditorGUILayout.BeginHorizontal();
  103.                 {
  104.  
  105.                     if (GUILayout.Button("Edit...", GUILayout.Width(125)))
  106.                     {
  107.                         GenericMenu l_addItemMenu = new GenericMenu();
  108.  
  109.                         l_addItemMenu.AddItem(new GUIContent("Add Item/Generic Item", "Add a new Generic Item."), false, MenuOption_AddNewItem, "Item");
  110.                         l_addItemMenu.AddItem(new GUIContent("Add Item/Consumable Item", "Add a new Consumable Item."), false, MenuOption_AddNewItem, "Consumable");
  111.                         l_addItemMenu.AddItem(new GUIContent("Add Item/Quest Item", "Add a new Quest Item."), false, MenuOption_AddNewItem, "Quest");
  112.                         l_addItemMenu.AddItem(new GUIContent("Add Item/Armor Item", "Add a new Armor Item."), false, MenuOption_AddNewItem, "Armor");
  113.                         l_addItemMenu.AddItem(new GUIContent("Add Item/Weapon nItem", "Add a new Weapon Item."), false, MenuOption_AddNewItem, "Weapon");
  114.  
  115.                         l_addItemMenu.AddSeparator("");
  116.  
  117.                         l_addItemMenu.AddItem(new GUIContent("Add Category"), false, MenuOption_AddCategory);
  118.  
  119.                         if (this.m_itemDatabase.CategoryCount() >= 2)
  120.                         {
  121.                             l_addItemMenu.AddItem(new GUIContent("Remove Category"), false, MenuOption_RemoveCategory);
  122.                         }
  123.                         else
  124.                         {
  125.                             l_addItemMenu.AddDisabledItem(new GUIContent("Remove Category"));
  126.                         }
  127.  
  128.  
  129.  
  130.                         l_addItemMenu.AddSeparator("");
  131.  
  132.                         if (m_selectedItem != null)
  133.                         {
  134.                             l_addItemMenu.AddItem(new GUIContent("Remove Selected Item", "Removes the Selected Item."), false, MenuOption_RemoveSelectedItem);
  135.                         }
  136.                         else
  137.                         {
  138.                             l_addItemMenu.AddDisabledItem(new GUIContent("Remove Selected Item", "Removes the Selected Item."));
  139.                         }
  140.  
  141.                         l_addItemMenu.ShowAsContext();
  142.                     }
  143.  
  144.                     if (GUILayout.Button("Xml...", GUILayout.Width(125)))
  145.                     {
  146.                         GenericMenu l_addItemMenu = new GenericMenu();
  147.  
  148.                         l_addItemMenu.AddItem(new GUIContent("Import Xml", "Imports an xml file to the ItemDatabase."), false, MenuOption_ImportXML);
  149.                         l_addItemMenu.AddItem(new GUIContent("Export Xml", "Exports the entire ItemDatabase to an Xml File."), false, MenuOption_ExportXML);
  150.  
  151.                         l_addItemMenu.ShowAsContext();
  152.                     }
  153.                 }
  154.                 EditorGUILayout.EndHorizontal();
  155.  
  156.                 EditorGUILayout.Space();
  157.             }
  158.             EditorGUILayout.EndVertical();
  159.         }
  160.  
  161.         void DrawDetails()
  162.         {
  163.             if (m_selectedItem != null)
  164.             {
  165.  
  166.                 EditorGUILayout.BeginVertical("box", GUILayout.Width(530.0f), GUILayout.Height(390.0f));
  167.                 {
  168.                     EditorGUILayout.Space();
  169.  
  170.                     EditorGUILayout.BeginHorizontal();
  171.                     {
  172.                         if (GUILayout.Button(RPG.Utils.TextureUtils.GetTextureFromSprite(m_selectedItem.m_sprite), GUILayout.Width(64.0f), GUILayout.Height(64.0f)))
  173.                         {
  174.                             int controlID = EditorGUIUtility.GetControlID(FocusType.Passive);
  175.                             EditorGUIUtility.ShowObjectPicker<Sprite>(m_selectedItem.m_sprite, false, "", controlID);
  176.                         }
  177.  
  178.                         if (Event.current.commandName == "ObjectSelectorUpdated")
  179.                         {
  180.                             Sprite sprite = EditorGUIUtility.GetObjectPickerObject() as Sprite;
  181.                             m_selectedItem.m_sprite = sprite;
  182.                             this.Repaint();
  183.                         }
  184.  
  185.                        
  186.                         GUILayout.Space(15.0f);
  187.                         EditorGUILayout.BeginVertical();
  188.                         {
  189.                             GUILayout.Space(16.0f);
  190.                             EditorGUILayout.LabelField(m_selectedItem.m_name, StyleManager.Instance.largeLabel, GUILayout.Width(300.0f));
  191.                         }
  192.                         EditorGUILayout.EndVertical();
  193.                     }
  194.                     EditorGUILayout.EndHorizontal();
  195.                     EditorGUILayout.Space();
  196.  
  197.                    
  198.                     EditorGUILayout.BeginHorizontal();
  199.                     {
  200.                         EditorGUILayout.LabelField("Category:", GUILayout.Width(100.0f));
  201.  
  202.                         m_selectedItem.m_selectedCategory = this.m_itemDatabase.FindCategoryIndex(this.m_selectedItem.m_category);
  203.                         if (m_selectedItem.m_selectedCategory > -1)
  204.                         {
  205.                             m_selectedItem.m_selectedCategory = EditorGUILayout.Popup(this.m_selectedItem.m_selectedCategory, this.m_itemDatabase.GetCategories(), GUILayout.Width(200.0f));
  206.                             this.m_selectedItem.m_category = this.m_itemDatabase.GetCategories()[this.m_selectedItem.m_selectedCategory];
  207.                         }
  208.                     }
  209.                     EditorGUILayout.EndHorizontal();
  210.  
  211.                     EditorGUILayout.BeginHorizontal();
  212.                     {
  213.                         EditorGUILayout.LabelField("Class:", GUILayout.Width(100.0f));
  214.                         m_selectedItem.m_selectedClass = this.m_itemDatabase.FindClassIndex(this.m_selectedItem.m_class);
  215.                         if (m_selectedItem.m_selectedClass > -1)
  216.                         {
  217.                             m_selectedItem.m_selectedClass = EditorGUILayout.Popup(this.m_selectedItem.m_selectedClass, this.m_itemDatabase.m_classes, GUILayout.Width(200.0f));
  218.                             this.m_selectedItem.m_class = this.m_itemDatabase.m_classes[this.m_selectedItem.m_selectedClass];
  219.                         }
  220.                     }
  221.                     EditorGUILayout.EndHorizontal();
  222.  
  223.                     EditorGUILayout.BeginHorizontal();
  224.                     {
  225.                         EditorGUILayout.LabelField("Id:", GUILayout.Width(100.0f));
  226.                         m_selectedItem.m_id = EditorGUILayout.TextField(m_selectedItem.m_id, GUILayout.Width(200.0f));
  227.                     }
  228.                     EditorGUILayout.EndHorizontal();
  229.  
  230.                     EditorGUILayout.BeginHorizontal();
  231.                     {
  232.                         EditorGUILayout.LabelField("Name:", GUILayout.Width(100.0f));
  233.                         m_selectedItem.m_name = EditorGUILayout.TextField(m_selectedItem.m_name, GUILayout.Width(200.0f));
  234.                     }
  235.                     EditorGUILayout.EndHorizontal();
  236.  
  237.                     EditorGUILayout.Space();
  238.  
  239.                     EditorGUILayout.BeginHorizontal();
  240.                     {
  241.  
  242.                         EditorGUILayout.LabelField("Description:", GUILayout.Width(100.0f));
  243.                         GUIStyle style = StyleManager.Instance.textAreaStyle;
  244.                         style.fixedWidth = 300.0f;
  245.                         style.fixedHeight = style.lineHeight * 6;
  246.                         m_selectedItem.m_description = EditorGUILayout.TextArea(m_selectedItem.m_description, style);
  247.                     }
  248.                     EditorGUILayout.EndHorizontal();
  249.  
  250.                     EditorGUILayout.Space();
  251.                 }
  252.                 EditorGUILayout.EndVertical();
  253.             }
  254.         }
  255.  
  256.         void DrawSelectedItem(Item l_item)
  257.         {
  258.             GUIStyle l_style = new GUIStyle("box");
  259.             l_style.normal.background = RPG.Utils.TextureUtils.CreateEmptyTexture(240, 36, Color.cyan);
  260.  
  261.             EditorGUILayout.BeginHorizontal(l_style, GUILayout.Width(240.0f), GUILayout.Height(36.0f));
  262.             {
  263.                 Rect l_rect = EditorGUILayout.GetControlRect();
  264.  
  265.                 l_rect.x = 8.0f;
  266.                 l_rect.width = 32.0f;
  267.                 l_rect.height = 32.0f;
  268.  
  269.                 GUI.Label(l_rect, RPG.Utils.TextureUtils.GetTextureFromSprite(l_item.m_sprite));
  270.  
  271.                 l_rect.x += 40.0f;
  272.                 l_rect.y += 8.0f;
  273.                 l_rect.width = 190.0f;
  274.                 l_rect.height = 32.0f;
  275.  
  276.                 GUI.Label(l_rect, l_item.m_name);
  277.             }
  278.             EditorGUILayout.EndHorizontal();
  279.         }
  280.  
  281.         void DrawUnselectedItem(Item l_item)
  282.         {
  283.             GUIStyle l_style = new GUIStyle("box");
  284.             l_style.normal.background = RPG.Utils.TextureUtils.CreateEmptyTexture(240, 36, Color.white);
  285.  
  286.             EditorGUILayout.BeginHorizontal(l_style, GUILayout.Width(240.0f), GUILayout.Height(36.0f));
  287.             {
  288.                 Rect l_rect = EditorGUILayout.GetControlRect();
  289.  
  290.                 l_rect.x -= 4.0f;
  291.                 l_rect.y -= 4.0f;
  292.                 l_rect.height = 38.0f;
  293.                 l_rect.width = 240.0f;
  294.  
  295.                 GUIStyle style = new GUIStyle();
  296.  
  297.                 if (GUI.Button(l_rect, "", style))
  298.                 {
  299.                     if (Event.current.button == 0)
  300.                     {
  301.                         m_selectedItem = null;
  302.                         m_selectedItem = l_item;
  303.                     }
  304.                 }
  305.  
  306.                 l_rect.x = 8.0f;
  307.                 l_rect.y += 4.0f;
  308.                 l_rect.width = 32.0f;
  309.                 l_rect.height = 32.0f;
  310.  
  311.                 GUI.Label(l_rect, RPG.Utils.TextureUtils.GetTextureFromSprite(l_item.m_sprite));
  312.  
  313.                 l_rect.x += 40.0f;
  314.                 l_rect.y += 8.0f;
  315.                 l_rect.width = 190.0f;
  316.                 l_rect.height = 32.0f;
  317.  
  318.                 GUI.Label(l_rect, l_item.m_name);
  319.             }
  320.             EditorGUILayout.EndHorizontal();
  321.         }
  322.  
  323.         void MenuOption_AddNewItem(object obj)
  324.         {
  325.             if (obj.ToString() == "Item")
  326.             {
  327.                 Item l_item = new Item();
  328.                 l_item.m_class = "Item";
  329.                 l_item.m_id = "newItem";
  330.                 l_item.m_name = "{New Item}";
  331.                 l_item.m_category = "Unassigned";
  332.  
  333.                 bool success = this.m_itemDatabase.AddItem(l_item);
  334.  
  335.                 if (!success)
  336.                 {
  337.                     EditorUtility.DisplayDialog("Item Exists!", "An item {m_id: " + l_item.m_id + "} already exists.", "Ok");
  338.                 }
  339.  
  340.             }
  341.             else if (obj.ToString() == "Consumable")
  342.             {
  343.                 ConsumableItem l_item = new ConsumableItem();
  344.                 l_item.m_class = "Consumable";
  345.                 l_item.m_id = "newConsumableItem";
  346.                 l_item.m_name = "{New Consumable Item}";
  347.                 l_item.m_category = "Unassigned";
  348.  
  349.                 bool success = this.m_itemDatabase.AddItem(l_item);
  350.  
  351.                 if (!success)
  352.                 {
  353.                     EditorUtility.DisplayDialog("Item Exists!", "An item {m_id: " + l_item.m_id + "} already exists.", "Ok");
  354.                 }
  355.             }
  356.             else if (obj.ToString() == "Quest")
  357.             {
  358.                 QuestItem l_item = new QuestItem();
  359.                 l_item.m_class = "Quest";
  360.                 l_item.m_id = "newQuestItem";
  361.                 l_item.m_name = "{New Quest Item}";
  362.                 l_item.m_category = "Unassigned";
  363.  
  364.                 bool success = this.m_itemDatabase.AddItem(l_item);
  365.  
  366.                 if (!success)
  367.                 {
  368.                     EditorUtility.DisplayDialog("Item Exists!", "An item {m_id: " + l_item.m_id + "} already exists.", "Ok");
  369.                 }
  370.             }
  371.             else if (obj.ToString() == "Armor")
  372.             {
  373.                 ArmorItem l_item = new ArmorItem();
  374.                 l_item.m_class = "Armor";
  375.                 l_item.m_id = "newArmorItem";
  376.                 l_item.m_name = "{New Armor Item}";
  377.                 l_item.m_category = "Unassigned";
  378.  
  379.                 bool success = this.m_itemDatabase.AddItem(l_item);
  380.  
  381.                 if (!success)
  382.                 {
  383.                     EditorUtility.DisplayDialog("Item Exists!", "An item {m_id: " + l_item.m_id + "} already exists.", "Ok");
  384.                 }
  385.             }
  386.             else if (obj.ToString() == "Weapon")
  387.             {
  388.                 WeaponItem l_item = new WeaponItem();
  389.                 l_item.m_class = "Weapon";
  390.                 l_item.m_id = "newWeaponItem";
  391.                 l_item.m_name = "{New Weapon Item}";
  392.                 l_item.m_category = "Unassigned";
  393.  
  394.                 bool success = this.m_itemDatabase.AddItem(l_item);
  395.  
  396.                 if (!success)
  397.                 {
  398.                     EditorUtility.DisplayDialog("Item Exists!", "An item {m_id: " + l_item.m_id + "} already exists.", "Ok");
  399.                 }
  400.             }
  401.         }
  402.  
  403.         void MenuOption_RemoveSelectedItem()
  404.         {
  405.             if (this.m_selectedItem != null)
  406.             {
  407.                 this.m_itemDatabase.RemoveItem(this.m_selectedItem.m_id);
  408.                 this.m_selectedItem = null;
  409.                 this.Repaint();
  410.             }
  411.         }
  412.  
  413.         void MenuOption_ImportXML()
  414.         {
  415.             string path = EditorUtility.OpenFilePanel(
  416.                     "Import Xml File",
  417.                     "",
  418.                     "xml");
  419.  
  420.             path = path.Replace(Application.dataPath, "Assets");
  421.  
  422.             if (path.Length != 0)
  423.             {
  424.                 TextAsset text = AssetDatabase.LoadAssetAtPath(path,typeof(TextAsset)) as TextAsset;
  425.                 m_itemDatabase.LoadFromXml(text);
  426.                 this.Repaint();
  427.             }
  428.         }
  429.  
  430.         void MenuOption_ExportXML()
  431.         {
  432.             throw new NotImplementedException();
  433.         }
  434.  
  435.         void MenuOption_AddCategory()
  436.         {
  437.             NewCategoryWindow.Show(CreateCategory, this.position);
  438.         }
  439.  
  440.         void CreateCategory(string category)
  441.         {
  442.             if (!this.m_itemDatabase.Categories.Contains(category))
  443.             {
  444.                 this.m_itemDatabase.Categories.Add(category);
  445.                 m_categoryExpanded.Add(false);
  446.                 this.Repaint();
  447.             }
  448.         }
  449.  
  450.         void MenuOption_RemoveCategory()
  451.         {
  452.             RemoveCategoryWindow.Show(this.m_itemDatabase.GetCategories(), RemoveCategory, this.position);
  453.         }
  454.  
  455.         void RemoveCategory(string category)
  456.         {
  457.             if (this.m_itemDatabase.Categories.Contains(category))
  458.             {
  459.                 Item[] l_items = this.m_itemDatabase.GetItemsInCategory(category);
  460.  
  461.                 for (int i = 0; i < l_items.Length; i++)
  462.                 {
  463.                     l_items[i].m_category = "Unassigned";
  464.                 }
  465.  
  466.                 this.m_itemDatabase.Categories.Remove(category);
  467.  
  468.                 m_categoryExpanded.Clear();
  469.  
  470.                 for (int i = 0; i < m_itemDatabase.CategoryCount(); i++)
  471.                 {
  472.                     m_categoryExpanded.Add(false);
  473.                 }
  474.                 this.Repaint();
  475.             }
  476.         }
  477.     }
  478. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement