Skip to content

Commit

Permalink
Attributes persistence and many fixes
Browse files Browse the repository at this point in the history
Attribute as ScriptableObject for data persistence
Creating asset files for attributes
Fixed: attribute editor
Fixed: saving attributes in Specialized Class and Characters
Fixed: saving values in attributes
Fixed: changing order of attributes changes references
Fixed: character and specialized class visual issues
  • Loading branch information
WyrnCael committed May 17, 2018
1 parent d3120de commit b19eb43
Show file tree
Hide file tree
Showing 10 changed files with 169 additions and 69 deletions.
10 changes: 10 additions & 0 deletions Assets/TRPGMaker/Database/Attributes.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 8 additions & 31 deletions Assets/TRPGMaker/Database/Database.asset
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ MonoBehaviour:
m_EditorClassIdentifier:
attributes: []
tags: []
slotTypes: []
slotTypes:
- slotName: Hand
items:
- {fileID: 11400000, guid: 242d1289cf2bba54c88d032eafa8eec4, type: 2}
- {fileID: 11400000, guid: 4c69bdc0758205346836ff1ddb49b90d, type: 2}
Expand All @@ -25,33 +26,9 @@ MonoBehaviour:
_TRPGOptions:
_gameType: 0
_turnType: 0
_healthAttribute:
name:
id:
description:
isCore: 0
_damageAttribute:
name:
id:
description:
isCore: 0
_moveRange:
name:
id:
description:
isCore: 0
_moveHeight:
name:
id:
description:
isCore: 0
_attackRange:
name:
id:
description:
isCore: 0
_attackHeight:
name:
id:
description:
isCore: 0
_healthAttribute: {fileID: 0}
_damageAttribute: {fileID: 0}
_moveRange: {fileID: 0}
_moveHeight: {fileID: 0}
_attackRange: {fileID: 0}
_attackHeight: {fileID: 0}
34 changes: 34 additions & 0 deletions Assets/TRPGMaker/Editor/AttributeEditor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;

[CustomEditor(typeof(Attribute))]
public class AttributeEditor : Editor {

public bool foldout;

public override void OnInspectorGUI()
{
Rect rectL = EditorGUILayout.GetControlRect();
Attribute attribute = (Attribute) target;
var textDimensions = GUI.skin.label.CalcSize(new GUIContent(attribute.name));

foldout = EditorGUI.Foldout(new Rect(rectL.x, rectL.y, textDimensions.x + 5, rectL.height), foldout, attribute.name);
if (foldout)
{
rectL.height = EditorGUIUtility.singleLineHeight;
rectL.x += 15;
rectL.y += EditorGUIUtility.singleLineHeight;
EditorGUI.PropertyField(rectL, serializedObject.FindProperty("name"));
rectL.y += EditorGUIUtility.singleLineHeight;
EditorGUI.PropertyField(rectL, serializedObject.FindProperty("id"));
rectL.y += EditorGUIUtility.singleLineHeight;
EditorGUI.PropertyField(rectL, serializedObject.FindProperty("description"));
rectL.y += EditorGUIUtility.singleLineHeight;
EditorGUI.PropertyField(rectL, serializedObject.FindProperty("isCore"));
}
}

}
13 changes: 13 additions & 0 deletions Assets/TRPGMaker/Editor/AttributeEditor.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions Assets/TRPGMaker/Editor/CharacterEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ private void OnEnable()
rectL.x += 15;
rectL.y += EditorGUIUtility.singleLineHeight;
GUI.SetNextControlName("Value");
EditorGUI.PropertyField(rectL, element.FindPropertyRelative("value"));
EditorGUI.PropertyField(new Rect(rectL.x, rectL.y, rectL.width - 15, rectL.height), element.FindPropertyRelative("value"));
if (GUI.GetNameOfFocusedControl() != "Value" && character.attributes[index].value > character.attributes[index].maxValue)
{
if (EditorUtility.DisplayDialog("Value error!",
Expand All @@ -185,7 +185,7 @@ private void OnEnable()
}
rectL.y += EditorGUIUtility.singleLineHeight;
GUI.SetNextControlName("MinValue");
EditorGUI.PropertyField(rectL, element.FindPropertyRelative("minValue"));
EditorGUI.PropertyField(new Rect(rectL.x, rectL.y, rectL.width - 15, rectL.height), element.FindPropertyRelative("minValue"));
if (GUI.GetNameOfFocusedControl() != "MinValue" && character.attributes[index].minValue > character.attributes[index].maxValue)
{
if (EditorUtility.DisplayDialog("Value error!",
Expand All @@ -195,7 +195,7 @@ private void OnEnable()
}
}
rectL.y += EditorGUIUtility.singleLineHeight;
EditorGUI.PropertyField(rectL, element.FindPropertyRelative("maxValue"));
EditorGUI.PropertyField(new Rect(rectL.x, rectL.y, rectL.width - 15, rectL.height), element.FindPropertyRelative("maxValue"));
listAttributes.elementHeight = EditorGUIUtility.singleLineHeight * 4.0f + 4.0f;
}
else
Expand Down Expand Up @@ -350,8 +350,7 @@ public override void OnInspectorGUI()
private void clickHandlerAttributes(object target)
{
var data = (Attribute) target;
AttributeValue attributeValue = new AttributeValue();
attributeValue.attribute = data;
AttributeValue attributeValue = (AttributeValue)data.Clone();
attributes.Add(attributeValue);
foldout.Add(false);
serializedObject.ApplyModifiedProperties();
Expand Down
22 changes: 10 additions & 12 deletions Assets/TRPGMaker/Editor/SpecializedClassEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,17 @@ private void OnEnable()
rectL.y += 2;

foldout[index] = EditorGUI.Foldout(new Rect(rectL.x, rectL.y, textDimensions.x + 5, rectL.height), foldout[index], specializedClass.attributes[index].attribute.name);
if (!specializedClass.attributes[index].attribute.isCore && GUI.Button(new Rect(rectL.width - 14, rectL.y, 16, 16), new GUIContent("", removeTexture), removeStyle))
{
specializedClass.attributes.RemoveAt(index);
}
if (foldout[index])
{
rectL.height = EditorGUIUtility.singleLineHeight;
rectL.x += 15;
rectL.y += EditorGUIUtility.singleLineHeight;
GUI.SetNextControlName("Value");
EditorGUI.PropertyField(rectL, element.FindPropertyRelative("value"));
EditorGUI.PropertyField(new Rect(rectL.x, rectL.y, rectL.width - 15, rectL.height), element.FindPropertyRelative("value"));
if (GUI.GetNameOfFocusedControl() != "Value" && specializedClass.attributes[index].value > specializedClass.attributes[index].maxValue)
{
if (EditorUtility.DisplayDialog("Value error!",
Expand All @@ -139,7 +143,7 @@ private void OnEnable()
}
rectL.y += EditorGUIUtility.singleLineHeight;
GUI.SetNextControlName("MinValue");
EditorGUI.PropertyField(rectL, element.FindPropertyRelative("minValue"));
EditorGUI.PropertyField(new Rect(rectL.x, rectL.y, rectL.width - 15, rectL.height), element.FindPropertyRelative("minValue"));
if (GUI.GetNameOfFocusedControl() != "MinValue" && specializedClass.attributes[index].minValue > specializedClass.attributes[index].maxValue)
{
if (EditorUtility.DisplayDialog("Value error!",
Expand All @@ -149,19 +153,14 @@ private void OnEnable()
}
}
rectL.y += EditorGUIUtility.singleLineHeight;
EditorGUI.PropertyField(rectL, element.FindPropertyRelative("maxValue"));
EditorGUI.PropertyField(new Rect(rectL.x, rectL.y, rectL.width - 15, rectL.height), element.FindPropertyRelative("maxValue"));
listAttributes.elementHeight = EditorGUIUtility.singleLineHeight * 4.0f + 4.0f;
}
else
{

listAttributes.elementHeight = EditorGUIUtility.singleLineHeight + 4.0f;
}

if (!specializedClass.attributes[index].attribute.isCore && GUI.Button(new Rect(rectL.width - 14, rectL.y, 16, 16), new GUIContent("", removeTexture), removeStyle))
{
specializedClass.attributes.RemoveAt(index);
}
};

listAttributes.elementHeightCallback += (idx) => {
Expand Down Expand Up @@ -338,10 +337,9 @@ public override void OnInspectorGUI()

private void clickHandlerAttributes(object target)
{
var data = (Attribute) target;
AttributeValue attributeValue = new AttributeValue();
attributeValue.attribute = data;
attributes.Add(attributeValue);
Attribute data = (Attribute) target;
AttributeValue attributeValue = (AttributeValue) data.Clone();
specializedClass.attributes.Add(attributeValue);
foldout.Add(false);
serializedObject.ApplyModifiedProperties();
}
Expand Down
89 changes: 72 additions & 17 deletions Assets/TRPGMaker/Editor/Windows/Database/AttributesWindow.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;
Expand All @@ -10,13 +11,16 @@ public class AttributesWindow : LayoutWindow
private Vector2 scrollPosition;
private Texture2D removeTexture;
private GUIStyle removeStyle;
List<bool> foldout;

public override void Init()
{
createReorderableList();

//Database.Instance.attributes = new List<Attribute>();
// Remove button
removeTexture = (Texture2D)Resources.Load("Buttons/remove", typeof(Texture2D));

foldout = Enumerable.Repeat(false, Database.Instance.attributes.Count).ToList();
}

public override void Draw(Rect rect)
Expand Down Expand Up @@ -49,36 +53,58 @@ public override void Draw(Rect rect)

private void createReorderableList()
{
editor = Editor.CreateEditor(Database.Instance);
editor = Editor.CreateEditor(Database.Instance);

// Get Attributes
listAttributes = new ReorderableList(editor.serializedObject,
editor.serializedObject.FindProperty("attributes"),
true, true, true, true);
true, true, true, true);

// Draw attributes
listAttributes.drawElementCallback =
(Rect rectL, int index, bool isActive, bool isFocused) => {
var element = listAttributes.serializedProperty.GetArrayElementAtIndex(index);
rectL.y += 2;

if (element.propertyType == SerializedPropertyType.Generic)
Attribute attribute = element.objectReferenceValue as Attribute;
if (attribute != null)
{
EditorGUI.LabelField(new Rect(rectL.x + 15, rectL.y, rectL.width, rectL.height), element.displayName);
if (GUI.Button(new Rect(rectL.width, rectL.y, 16, 16), new GUIContent("", removeTexture), removeStyle))
var textDimensions = GUI.skin.label.CalcSize(new GUIContent(attribute.name));
rectL.y += 2;

foldout[index] = EditorGUI.Foldout(new Rect(rectL.x, rectL.y, textDimensions.x + 5, rectL.height), foldout[index], attribute.name);
if (foldout[index])
{
Database.Instance.attributes.RemoveAt(index);
SerializedObject attr = new SerializedObject(attribute);
rectL.height = EditorGUIUtility.singleLineHeight;
rectL.x += 15;
rectL.y += EditorGUIUtility.singleLineHeight;
GUI.SetNextControlName("Name");
EditorGUI.PropertyField(rectL, attr.FindProperty("name"));
rectL.y += EditorGUIUtility.singleLineHeight;
EditorGUI.PropertyField(rectL, attr.FindProperty("id"));
rectL.y += EditorGUIUtility.singleLineHeight;
EditorGUI.PropertyField(rectL, attr.FindProperty("description"));
rectL.y += EditorGUIUtility.singleLineHeight;
EditorGUI.PropertyField(rectL, attr.FindProperty("isCore"));
if (GUI.changed)
{
attr.ApplyModifiedProperties();
if (GUI.GetNameOfFocusedControl() != "Name")
AssetDatabase.RenameAsset(AssetDatabase.GetAssetPath(attribute), attribute.name + ".asset");
}
listAttributes.elementHeight = EditorGUIUtility.singleLineHeight * 5.0f + 4.0f;
}
}
rectL.height = EditorGUI.GetPropertyHeight(element, GUIContent.none, true);
rectL.y += 1;
EditorGUI.PropertyField(rectL, element, GUIContent.none, true);
listAttributes.elementHeight = rectL.height + 4.0f;

else
{

listAttributes.elementHeight = EditorGUIUtility.singleLineHeight + 4.0f;
}
}
};

listAttributes.elementHeightCallback += (idx) => { return Mathf.Max(EditorGUIUtility.singleLineHeight, EditorGUI.GetPropertyHeight(listAttributes.serializedProperty.GetArrayElementAtIndex(idx), GUIContent.none, true)) + 4.0f; };
listAttributes.elementHeightCallback += (idx) => {
if (foldout[idx]) return EditorGUIUtility.singleLineHeight * 5.0f + 4.0f;
else return EditorGUIUtility.singleLineHeight + 4.0f;
};

// listAttributes header
listAttributes.drawHeaderCallback = (Rect rectH) => {
Expand All @@ -87,7 +113,36 @@ private void createReorderableList()

// Add attributes
listAttributes.onAddDropdownCallback = (Rect buttonRect, ReorderableList l) => {
Database.Instance.attributes.Add(new Attribute("New Attribute " + Database.Instance.attributes.Count.ToString("D3"), "X" + Database.Instance.attributes.Count.ToString("D2"), "", false));
Attribute attrb = CreateInstance<Attribute>();
attrb.name = "New Attribute " + Database.Instance.attributes.Count.ToString("D3");
attrb.id = "X" + Database.Instance.attributes.Count.ToString("D2");
Database.Instance.attributes.Add(attrb);
foldout = Enumerable.Repeat(false, Database.Instance.attributes.Count).ToList();

var specializedClassesPath = "Assets/TRPGMaker/Database/Attributes";
var _exists = AssetDatabase.LoadAssetAtPath(specializedClassesPath + "/" + attrb.name + ".asset", typeof(SpecializedClass));
if (_exists == null)
{
//Create the folder if doesn't exist
if (!System.IO.Directory.Exists(specializedClassesPath))
{
System.IO.Directory.CreateDirectory(specializedClassesPath);
}
AssetDatabase.CreateAsset(attrb, specializedClassesPath + "/" + attrb.name + ".asset");
}

};

// Detect attribute changed
listAttributes.onChangedCallback = (ReorderableList l) => {
foldout = Enumerable.Repeat(false, Database.Instance.attributes.Count).ToList();
};

// On remove attribute
listAttributes.onRemoveCallback = (ReorderableList l) => {
var attribute = l.serializedProperty.GetArrayElementAtIndex(l.index).objectReferenceValue as Attribute;
Database.Instance.attributes.Remove(attribute);
AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(attribute));
};
}

Expand Down
2 changes: 1 addition & 1 deletion Assets/TRPGMaker/Scripts/Database/Attribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using UnityEngine;

[Serializable]
public class Attribute : ICloneable
public class Attribute : ScriptableObject
{
[SerializeField]
public string name;
Expand Down
16 changes: 14 additions & 2 deletions Assets/TRPGMaker/Scripts/Database/AttributeValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,21 @@
using UnityEngine;

[Serializable]
public class AttributeValue {
public class AttributeValue{

public Attribute attribute;
[SerializeField]
private Attribute _attribute;
[SerializeField]
public Attribute attribute
{
get
{
return _attribute;
}
set {
_attribute = value;
}
}
[SerializeField]
public int maxValue = 0;
[SerializeField]
Expand Down
4 changes: 3 additions & 1 deletion Assets/TRPGMaker/Scripts/Database/SpecializedClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public class SpecializedClass : ScriptableObject
public new string name;
public List<Tag> tags;
public List<Slot> slots;
public List<AttributeValue> attributes = null;
[SerializeField]
public List<AttributeValue> attributes;
public FormulaScript formula;
public List<Skills> skills;

Expand All @@ -21,6 +22,7 @@ public void Init()
tags = new List<Tag>();
slots = new List<Slot>();
formula = ScriptableObject.CreateInstance<FormulaScript>();
attributes = new List<AttributeValue>();
}

public void refreshAttributes()
Expand Down

0 comments on commit b19eb43

Please sign in to comment.