Files
beyond/Assets/Scripts/UI/Editor/TradeMenuControllerEditor.cs
2024-11-20 15:21:28 +01:00

231 lines
10 KiB
C#

using UnityEditor;
using UnityEngine;
namespace Beyond
{
[CustomEditor(typeof(TradeMenuController))]
[System.Serializable]
public class TradeMenuControllerEditor : vEditorBase
{
// SerializedProperty itemListData;
private SerializedProperty shopItemRefs;
private TradeMenuController tradeMenuController;
private Rect buttonRect;
private Vector2 scroll;
private GUISkin oldSkin;
protected override void OnEnable()
{
base.OnEnable();
skin = Resources.Load("vSkin") as GUISkin;
tradeMenuController = (TradeMenuController)target;
shopItemRefs = serializedObject.FindProperty("shopItemRefs");
}
public override void OnInspectorGUI()
{
// GUILayout.Label("This is a Label in a Custom Editor");
oldSkin = GUI.skin;
if (skin)
{
GUI.skin = skin;
}
DrawPropertiesExcluding(serializedObject, "shopItemRefs");
if (tradeMenuController.itemListData)
{
if (GUILayout.Button("Open Item List"))
{
ShowItemListWindow();
}
GUILayout.BeginVertical("box");
GUILayout.Box("Shop Items " + tradeMenuController.shopItemRefs.Count);
if (GUILayout.Button("Add Item", EditorStyles.miniButton))
{
PopupWindow.Show(buttonRect, new bItemSelector
(tradeMenuController.itemListData.items, ref tradeMenuController.itemsFilter,
(bItem item) => //OnSelectItem
{
Debug.Log(item);
shopItemRefs.arraySize++;
shopItemRefs.GetArrayElementAtIndex(shopItemRefs.arraySize - 1)
.FindPropertyRelative("id").intValue = item.id;
shopItemRefs.GetArrayElementAtIndex(shopItemRefs.arraySize - 1)
.FindPropertyRelative("amount").intValue = 1;
EditorUtility.SetDirty(tradeMenuController);
serializedObject.ApplyModifiedProperties();
}
));
}
if (Event.current.type == EventType.Repaint)
{
buttonRect = GUILayoutUtility.GetLastRect();
}
GUIStyle boxStyle = new GUIStyle(GUI.skin.box);
scroll = GUILayout.BeginScrollView(scroll, GUILayout.MinHeight(200), GUILayout.ExpandHeight(false),
GUILayout.ExpandWidth(false));
try
{
for (int i = 0; i < tradeMenuController.shopItemRefs.Count; i++)
{
var item = tradeMenuController.itemListData.items.Find(t =>
t.id.Equals(tradeMenuController.shopItemRefs[i].id));
if (item)
{
GUILayout.BeginVertical("box");
GUILayout.BeginHorizontal();
GUILayout.BeginHorizontal();
var rect = GUILayoutUtility.GetRect(50, 50);
if (item.icon != null)
{
DrawTextureGUI(rect, item.icon, new Vector2(50, 50));
}
var name = " ID " + item.id.ToString("00") + "\n - " + item.name + "\n - " +
item.type.ToString();
var content = new GUIContent(name, null, "Click to Open");
GUILayout.Label(content, EditorStyles.miniLabel);
GUILayout.BeginVertical("box");
GUILayout.BeginHorizontal();
GUILayout.Label("Amount", EditorStyles.miniLabel);
tradeMenuController.shopItemRefs[i].amount =
EditorGUILayout.IntField(tradeMenuController.shopItemRefs[i].amount, GUILayout.Width(30));
if (tradeMenuController.shopItemRefs[i].amount < 1)
{
tradeMenuController.shopItemRefs[i].amount = 1;
}
GUILayout.EndHorizontal();
GUILayout.EndVertical();
GUILayout.EndHorizontal();
if (GUILayout.Button("x", GUILayout.Width(25), GUILayout.Height(25)))
{
shopItemRefs.DeleteArrayElementAtIndex(i);
EditorUtility.SetDirty(target);
serializedObject.ApplyModifiedProperties();
break;
}
GUILayout.EndHorizontal();
Color backgroundColor = GUI.backgroundColor;
GUI.backgroundColor = Color.clear;
var _rec = GUILayoutUtility.GetLastRect();
_rec.width -= 100;
EditorGUIUtility.AddCursorRect(_rec, MouseCursor.Link);
if (GUI.Button(_rec, ""))
{
ShowItemListWindow(item);
}
GUILayout.Space(7);
GUI.backgroundColor = backgroundColor;
if (item.attributes != null && item.attributes.Count > 0)
{
if (tradeMenuController.shopItemRefs[i].changeAttributes)
{
if (GUILayout.Button("Reset", EditorStyles.miniButton))
{
tradeMenuController.shopItemRefs[i].attributes = null;
}
if (tradeMenuController.shopItemRefs[i].attributes == null)
{
tradeMenuController.shopItemRefs[i].attributes = item.attributes.CopyAsNew();
}
else if (tradeMenuController.shopItemRefs[i].attributes.Count !=
item.attributes.Count)
{
tradeMenuController.shopItemRefs[i].attributes = item.attributes.CopyAsNew();
}
else
{
for (int a = 0; a < tradeMenuController.shopItemRefs[i].attributes.Count; a++)
{
GUILayout.BeginHorizontal();
GUILayout.Label(tradeMenuController.shopItemRefs[i].attributes[a].name
.ToString());
tradeMenuController.shopItemRefs[i].attributes[a].value =
EditorGUILayout.IntField(
tradeMenuController.shopItemRefs[i].attributes[a].value,
GUILayout.MaxWidth(60));
GUILayout.EndHorizontal();
}
}
}
}
GUILayout.EndVertical();
}
else
{
shopItemRefs.DeleteArrayElementAtIndex(i);
EditorUtility.SetDirty(tradeMenuController);
serializedObject.ApplyModifiedProperties();
break;
}
}
}
catch
{
}
GUILayout.EndScrollView();
GUI.skin.box = boxStyle;
GUILayout.EndVertical();
}
GUI.skin = oldSkin;
if (GUI.changed)
{
EditorUtility.SetDirty(tradeMenuController);
serializedObject.ApplyModifiedProperties();
}
}
private void DrawTextureGUI(Rect position, Sprite sprite, Vector2 size)
{
Rect spriteRect = new Rect(sprite.rect.x / sprite.texture.width, sprite.rect.y / sprite.texture.height,
sprite.rect.width / sprite.texture.width, sprite.rect.height / sprite.texture.height);
Vector2 actualSize = size;
actualSize.y *= (sprite.rect.height / sprite.rect.width);
GUI.DrawTextureWithTexCoords(new Rect(position.x, position.y + (size.y - actualSize.y) / 2, actualSize.x, actualSize.y), sprite.texture, spriteRect);
}
protected virtual void ShowItemListWindow(bItem item = null)
{
if (item == null)
{
bItemListWindow.CreateWindow(tradeMenuController.itemListData);
}
else if (tradeMenuController.itemListData.inEdition)
{
if (bItemListWindow.Instance == null)
{
bItemListWindow.CreateWindow(tradeMenuController.itemListData, tradeMenuController.itemListData.items.IndexOf(item));
}
}
else
{
bItemListWindow.CreateWindow(tradeMenuController.itemListData, tradeMenuController.itemListData.items.IndexOf(item));
}
if (item)
{
bItemListWindow.SetCurrentSelectedItem(tradeMenuController.itemListData.items.IndexOf(item));
}
}
}
}