-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#173 port reference editor, sim connections, proxy cache for lists
- Loading branch information
Showing
12 changed files
with
369 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
using CCL.Types.Proxies.Ports; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using UnityEditor; | ||
using UnityEditor.Experimental.SceneManagement; | ||
using UnityEditor.SceneManagement; | ||
using UnityEngine; | ||
|
||
namespace CCL.Creator.Editor | ||
{ | ||
[CustomPropertyDrawer(typeof(PortReferenceIdAttribute))] | ||
public class PortReferenceIdEditor : PropertyDrawer | ||
{ | ||
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) | ||
{ | ||
string? currentValue = property.stringValue; | ||
if (string.IsNullOrEmpty(currentValue)) | ||
{ | ||
currentValue = null; | ||
} | ||
|
||
var component = property.serializedObject.targetObject; | ||
|
||
EditorGUI.BeginProperty(position, label, property); | ||
position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label); | ||
|
||
IEnumerable<GameObject> sources; | ||
|
||
// otherwise we want to list ports on all parts of the car | ||
string? prefabAssetPath = GetCurrentPrefabPath(component); | ||
if (prefabAssetPath != null) | ||
{ | ||
sources = GetSiblingPrefabs(prefabAssetPath); | ||
} | ||
else | ||
{ | ||
var scene = EditorSceneManager.GetActiveScene(); | ||
sources = scene.GetRootGameObjects(); | ||
} | ||
|
||
var options = GetPortOptions(sources); | ||
int selected = options.FindIndex(p => p.ID == currentValue); | ||
|
||
if ((selected < 0) && !string.IsNullOrEmpty(currentValue)) | ||
{ | ||
options.Add(new PortReferenceOption(currentValue)); | ||
selected = options.Count - 1; | ||
} | ||
|
||
string[] optionNames = options.Select(p => p.Description).ToArray(); | ||
|
||
int newIndex = EditorGUI.Popup(position, Math.Max(selected, 0), optionNames); | ||
|
||
if (newIndex != selected) | ||
{ | ||
property.stringValue = options[newIndex].ID; | ||
} | ||
|
||
EditorGUI.EndProperty(); | ||
|
||
property.serializedObject.ApplyModifiedProperties(); | ||
} | ||
|
||
private static string? GetCurrentPrefabPath(UnityEngine.Object contextObj) | ||
{ | ||
if (PrefabUtility.IsPartOfAnyPrefab(contextObj)) | ||
{ | ||
return PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(contextObj); | ||
} | ||
|
||
if (PrefabStageUtility.GetCurrentPrefabStage() is PrefabStage stage) | ||
{ | ||
return stage.prefabAssetPath; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private static IEnumerable<GameObject> GetSiblingPrefabs(string prefabPath) | ||
{ | ||
string curFolder = Path.GetDirectoryName(prefabPath); | ||
|
||
return AssetDatabase.FindAssets("t:prefab", new string[] { curFolder }) | ||
.Select(AssetDatabase.GUIDToAssetPath) | ||
.Where(p => p != null) | ||
.Select(AssetDatabase.LoadAssetAtPath<GameObject>); | ||
} | ||
|
||
private static List<PortReferenceOption> GetPortOptions(IEnumerable<GameObject> sources) | ||
{ | ||
var ids = new List<PortReferenceOption> | ||
{ | ||
new PortReferenceOption(null, "Not Set") | ||
}; | ||
|
||
foreach (var source in sources) | ||
{ | ||
ids.AddRange(GetPortsInObject(source)); | ||
} | ||
return ids; | ||
} | ||
|
||
private static IEnumerable<PortReferenceOption> GetPortsInObject(GameObject root) | ||
{ | ||
foreach (var component in root.GetComponentsInChildren<SimComponentDefinitionProxy>()) | ||
{ | ||
foreach (var portDef in component.ExposedPortReferences) | ||
{ | ||
yield return new PortReferenceOption(root.name, component.ID, portDef.ID); | ||
} | ||
} | ||
} | ||
|
||
private readonly struct PortReferenceOption | ||
{ | ||
public readonly string PrefabName; | ||
public readonly string? ID; | ||
|
||
public PortReferenceOption(string prefabName, string compId, string portId) | ||
{ | ||
PrefabName = prefabName; | ||
ID = $"{compId}.{portId}"; | ||
} | ||
|
||
public PortReferenceOption(string? fullId, string prefabName = "Unknown") | ||
{ | ||
PrefabName = prefabName; | ||
ID = fullId; | ||
} | ||
|
||
public string Description => $"{ID} ({PrefabName})"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
CCL.Importer/Proxies/Controllers/SimConnectionsReplacer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using AutoMapper; | ||
using CCL.Types.Proxies.Ports; | ||
using LocoSim.Definitions; | ||
using System.ComponentModel.Composition; | ||
using UnityEngine; | ||
|
||
namespace CCL.Importer.Proxies.Controllers | ||
{ | ||
[Export(typeof(IProxyReplacer))] | ||
public class SimConnectionsReplacer : Profile, IProxyReplacer | ||
{ | ||
public SimConnectionsReplacer() | ||
{ | ||
CreateMap<SimConnectionsDefinitionProxy, SimConnectionDefinition>() | ||
.ForMember("executionOrder", o => o.ConvertUsing(new CacheConverter(o.DestinationMember), "executionOrder")); | ||
|
||
CreateMap<PortConnectionProxy, Connection>(); | ||
CreateMap<PortReferenceConnectionProxy, PortReferenceConnection>(); | ||
} | ||
|
||
public void CacheAndReplaceProxies(GameObject prefab) | ||
{ | ||
prefab.StoreComponentsInChildrenInCache<SimConnectionsDefinitionProxy, SimConnectionDefinition>(_ => true); | ||
|
||
} | ||
|
||
public void MapProxies(GameObject prefab) | ||
{ | ||
|
||
} | ||
|
||
public void ReplaceProxiesUncached(GameObject prefab) | ||
{ | ||
prefab.ConvertFromCache(typeof(SimConnectionsDefinitionProxy), typeof(SimConnectionDefinition), _ => true); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System; | ||
|
||
namespace CCL.Types.Proxies.Ports | ||
{ | ||
[Serializable] | ||
public class PortReferenceDefinition | ||
{ | ||
public DVPortValueType valueType; | ||
|
||
public string ID; | ||
|
||
public bool writeAllowed; | ||
|
||
public PortReferenceDefinition(DVPortValueType valueType, string iD, bool writeAllowed = false) | ||
{ | ||
this.valueType = valueType; | ||
this.writeAllowed = writeAllowed; | ||
ID = iD; | ||
} | ||
} | ||
} |
Oops, something went wrong.