Skip to content

Commit

Permalink
🚧 Improving the content editor editing
Browse files Browse the repository at this point in the history
Allowed EditorSession editing of properties.
  • Loading branch information
Fydar committed Mar 7, 2020
1 parent ce75367 commit 848dd3c
Show file tree
Hide file tree
Showing 21 changed files with 264 additions and 55 deletions.
80 changes: 71 additions & 9 deletions src/RPGCore.Behaviour/Manifest/FieldInformation.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Reflection;

Expand All @@ -13,17 +14,78 @@ public sealed class FieldInformation
public FieldFormat Format;
public FieldInformation ValueFormat;

public abstract class ModelMember
{
public abstract Type ValueType { get; }
public abstract object[] GetCustomAttributes(bool inherit);
public abstract object GetValue(object instance);
}

public class ModelField : ModelMember
{
public FieldInfo Field { get; }

public override Type ValueType => Field.FieldType;

public ModelField(FieldInfo field)
{
Field = field;
}

public override object[] GetCustomAttributes(bool inherit)
{
return Field.GetCustomAttributes(inherit);
}

public override object GetValue(object instance)
{
return Field.GetValue(instance);
}
}

public class ModelProperty : ModelMember
{
public PropertyInfo Property { get; }

public override Type ValueType => Property.PropertyType;

public ModelProperty(PropertyInfo property)
{
Property = property;
}

public override object[] GetCustomAttributes(bool inherit)
{
return Property.GetCustomAttributes(inherit);
}

public override object GetValue(object instance)
{
return Property.GetValue(instance);
}
}

public static FieldInformation ConstructFieldInformation(FieldInfo field, object defaultInstance)
{
object[] attributes = field.GetCustomAttributes(false);
return ConstructFieldInformation(new ModelField(field), defaultInstance);
}

public static FieldInformation ConstructFieldInformation(PropertyInfo property, object defaultInstance)
{
return ConstructFieldInformation(new ModelProperty(property), defaultInstance);
}

public static FieldInformation ConstructFieldInformation(ModelMember member, object defaultInstance)
{
object[] attributes = member.GetCustomAttributes(false);
string[] attributeIds = new string[attributes.Length];
for (int i = 0; i < attributes.Length; i++)
{
attributeIds[i] = attributes.GetType().Name;
}

FieldInformation fieldInformation;
if (typeof(InputSocket).IsAssignableFrom(field.FieldType))
if (typeof(InputSocket).IsAssignableFrom(member.ValueType))
{
fieldInformation = new FieldInformation()
{
Expand All @@ -39,10 +101,10 @@ public static FieldInformation ConstructFieldInformation(FieldInfo field, object

if (defaultInstance != null)
{
defaultValue = field.GetValue(defaultInstance);
defaultValue = member.GetValue(defaultInstance);
}

string typeName = field.FieldType.Name;
string typeName = member.ValueType.Name;

try
{
Expand All @@ -65,20 +127,20 @@ public static FieldInformation ConstructFieldInformation(FieldInfo field, object
};
}

if (typeof(IDictionary).IsAssignableFrom(field.FieldType))
if (typeof(IDictionary).IsAssignableFrom(member.ValueType))
{
fieldInformation.Format = FieldFormat.Dictionary;
fieldInformation.Type = field.FieldType.GetGenericArguments()[1].Name;
fieldInformation.Type = member.ValueType.GetGenericArguments()[1].Name;

fieldInformation.ValueFormat = new FieldInformation()
{
Type = field.FieldType.GetGenericArguments()[1].Name,
Type = member.ValueType.GetGenericArguments()[1].Name,
Format = FieldFormat.Object
};
}
else if (field.FieldType.IsArray)
else if (member.ValueType.IsArray)
{
var elementType = field.FieldType.GetElementType();
var elementType = member.ValueType.GetElementType();

fieldInformation.Format = FieldFormat.List;
fieldInformation.Type = elementType.Name;
Expand Down
24 changes: 24 additions & 0 deletions src/RPGCore.Behaviour/Manifest/TypeInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,30 @@ protected static void ConstructTypeInformation(Type type, TypeInformation typeIn

fieldInfos.Add(field.Name, FieldInformation.ConstructFieldInformation(field, defaultInstance));
}
foreach (var field in type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
{
if (field.PropertyType == typeof(OutputSocket))
{
continue;
}

if (field.GetCustomAttribute<JsonIgnoreAttribute>() != null)
{
continue;
}

var getter = field.GetGetMethod();
var setter = field.GetSetMethod();

if (getter == null
|| getter.IsPrivate
|| setter == null)
{
continue;
}

fieldInfos.Add(field.Name, FieldInformation.ConstructFieldInformation(field, defaultInstance));
}
typeInformation.Fields = fieldInfos;
}
}
Expand Down
1 change: 0 additions & 1 deletion src/RPGCore.Demo.BoardGame/GamePlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace RPGCore.Demo.BoardGame
{

public class GamePlayer
{
public LocalId OwnerId;
Expand Down
1 change: 0 additions & 1 deletion src/RPGCore.Demo.BoardGame/GameView.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using RPGCore.Behaviour;
using RPGCore.Demo.BoardGame.Models;
using RPGCore.Packages;
using RPGCore.Traits;
using System.Collections.Generic;
Expand Down
20 changes: 15 additions & 5 deletions src/RPGCore.Demo.BoardGame/Models/BuildingTemplate.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
using RPGCore.Behaviour;
using Newtonsoft.Json;
using RPGCore.Behaviour;
using System.Collections.Generic;

namespace RPGCore.Demo.BoardGame.Models
{
public class BuildingTemplate
{
public string DisplayName;
public string DisplayName { get; set; }
public string BodyText { get; set; }

public string[,] Recipe;
public string[,] Recipe { get; set; }

public SerializedGraph GlobalEffectGraph;
public SerializedGraph LocalEffectGraph;
public SerializedGraph GlobalEffectGraph { get; set; }
public SerializedGraph LocalEffectGraph { get; set; }

[JsonIgnore]
public int Width => Recipe?.GetLength(0) ?? 0;

[JsonIgnore]
public int Height => Recipe?.GetLength(1) ?? 0;

[JsonIgnore]
public bool IsHorizontallySymmetric
{
get
Expand Down Expand Up @@ -42,6 +48,7 @@ public bool IsHorizontallySymmetric
}
}

[JsonIgnore]
public bool IsVerticallySymmetric
{
get
Expand Down Expand Up @@ -69,6 +76,7 @@ public bool IsVerticallySymmetric
}
}

[JsonIgnore]
public bool IsRotating
{
get
Expand All @@ -85,6 +93,7 @@ public bool IsRotating
}
}

[JsonIgnore]
private bool IsTheSameWhenRotated
{
get
Expand Down Expand Up @@ -116,6 +125,7 @@ private bool IsTheSameWhenRotated
}
}

[JsonIgnore]
private bool IsRotatedSameAsMirror
{
get
Expand Down
3 changes: 2 additions & 1 deletion src/RPGCore.Demo.BoardGame/Models/ResourceTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
public class ResourceTemplate
{
public string DisplayName;
public string DisplayName { get; set; }
public VoxelColour Colour { get; set; }
}
}
9 changes: 9 additions & 0 deletions src/RPGCore.Demo.BoardGame/Models/VoxelColour.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace RPGCore.Demo.BoardGame.Models
{
public struct VoxelColour
{
public float Red { get; set; }
public float Green { get; set; }
public float Blue { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/RPGCore.Demo.BoardGame/SpecialCardSlot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
public class SpecialCardSlot
{
public string BuildingIdentifier;
public string BuildingIdentifier { get; set; }

}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified src/RPGCoreUnity/Assets/RPGCore/Plugins/RPGCore.Behaviour.pdb
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace RPGCore.Unity.Editors
{
public static class RPGCoreEditor
{
const int FoldoutIndent = -10;

public static void DrawEditor(EditorSession editor)
{
if (editor == null)
Expand All @@ -30,9 +32,11 @@ public static void DrawField(EditorField field)
{
var fieldFeature = field.GetOrCreateFeature<FieldFeature>();

EditorGUI.indentLevel--;
fieldFeature.Expanded = EditorGUILayout.Foldout(fieldFeature.Expanded, field.Name, true);
EditorGUI.indentLevel++;
var fieldRect = GUILayoutUtility.GetRect(0, EditorGUIUtility.singleLineHeight, GUILayout.ExpandWidth(true));
GUILayout.Space(EditorGUIUtility.standardVerticalSpacing);
fieldRect.xMin += FoldoutIndent;

fieldFeature.Expanded = EditorGUI.Foldout(fieldRect, fieldFeature.Expanded, field.Name, true);

if (fieldFeature.Expanded)
{
Expand Down Expand Up @@ -64,6 +68,26 @@ public static void DrawField(EditorField field)
field.ApplyModifiedProperties();
}
}
else if (field.Field.Type == "Single")
{
EditorGUI.BeginChangeCheck();
float newValue = EditorGUILayout.FloatField(field.Name, field.GetValue<float>());
if (EditorGUI.EndChangeCheck())
{
field.SetValue(newValue);
field.ApplyModifiedProperties();
}
}
else if(field.Field.Type == "Double")
{
EditorGUI.BeginChangeCheck();
double newValue = EditorGUILayout.DoubleField(field.Name, field.GetValue<double>());
if (EditorGUI.EndChangeCheck())
{
field.SetValue(newValue);
field.ApplyModifiedProperties();
}
}
else if (field.Field.Type == "String")
{
EditorGUI.BeginChangeCheck();
Expand Down Expand Up @@ -110,12 +134,13 @@ public static void DrawField(EditorField field)
}

var fieldRect = GUILayoutUtility.GetRect(0, EditorGUIUtility.singleLineHeight, GUILayout.ExpandWidth(true));
PrefixLable(fieldRect, out var lable, out var content);
content.width = 140;
GUILayout.Space(EditorGUIUtility.standardVerticalSpacing);
fieldRect.xMin += 4;
var buttonRect = EditorGUI.PrefixLabel(fieldRect, new GUIContent(field.Name));

EditorGUI.LabelField(lable, field.Name);
buttonRect.width = 160;

if (GUI.Button(content, $"Edit Graph"))
if (GUI.Button(buttonRect, $"Edit Graph"))
{
BehaviourEditor.Open(field.Session, field);
}
Expand All @@ -138,9 +163,11 @@ public static void DrawField(EditorField field)
{
var fieldFeature = field.GetOrCreateFeature<FieldFeature>();

EditorGUI.indentLevel--;
fieldFeature.Expanded = EditorGUILayout.Foldout(fieldFeature.Expanded, field.Name, true);
EditorGUI.indentLevel++;
var fieldRect = GUILayoutUtility.GetRect(0, EditorGUIUtility.singleLineHeight, GUILayout.ExpandWidth(true));
fieldRect.xMin += FoldoutIndent;
GUILayout.Space(EditorGUIUtility.standardVerticalSpacing);

fieldFeature.Expanded = EditorGUI.Foldout(fieldRect, fieldFeature.Expanded, field.Name, true);

if (fieldFeature.Expanded)
{
Expand All @@ -158,18 +185,5 @@ public static void DrawField(EditorField field)
EditorGUILayout.LabelField(field.Name, "Unknown Type");
}
}

public static void PrefixLable (Rect position, out Rect lable, out Rect content)
{
content = EditorGUI.PrefixLabel (position, new GUIContent (" "));

float indent = EditorGUI.indentLevel * 15.0f;
position.xMin += indent;

lable = new Rect (position)
{
xMax = content.xMin
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public EditorSessionFrame(IResource resource)
typeof(PackageNodeEditor),
typeof(PackageNodePosition),
typeof(ExtraData),
typeof(VoxelColour),

typeof(ResourceTemplate),
typeof(BuildingTemplate),
Expand Down Expand Up @@ -109,6 +110,7 @@ public override void OnGUI()
{
if (Resource is ProjectResource projectResource)
{
EditorGUILayout.BeginHorizontal(EditorStyles.toolbar);
if (GUILayout.Button("Save", EditorStyles.toolbarButton, GUILayout.Width(100)))
{
using (var file = projectResource.WriteStream())
Expand All @@ -122,6 +124,12 @@ public override void OnGUI()
);
}
}

if (GUILayout.Button("View Manifest", EditorStyles.toolbarButton, GUILayout.Width(120)))
{
GenericWindow.Open(new GUIContent("Manifest"), new JsonTextWindowFrame(EditorSession.Manifest.ToString()));
}
EditorGUILayout.EndHorizontal();
}

RPGCoreEditor.DrawEditor(EditorSession);
Expand Down
Loading

0 comments on commit 848dd3c

Please sign in to comment.