This commit is contained in:
2026-04-25 23:37:10 +02:00
commit 19d6bd934a
476 changed files with 9198 additions and 0 deletions
@@ -0,0 +1,272 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using RPGCoreCommon.DynamicValues;
using RPGCoreCommon.Helpers;
using RPGCoreCommon.Helpers.CustomTypes;
using RPGCoreCommon.Helpers.Exceptions;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;
using Object = UnityEngine.Object;
namespace RPGCoreCommon.DynamicValues.Editor
{
[Serializable]
[CustomPropertyDrawer(typeof(DynamicValue))]
public class DynamicValueDrawer : PropertyDrawer
{
[SerializeField] private VisualTreeAsset _visualTreeAsset;
private SerializedProperty _property;
private SerializedProperty _propertySourceDict;
private SerializedProperty _propertyTypeDict;
private SerializedProperty _propertyFormula;
private DynamicValueAttribute _attribute;
// Elements
private VisualElement _background;
private Foldout _foldoutLabel;
private TextField _formulaEdit;
private PropertyField _objectDictField;
private PropertyField _typeDictField;
private ScrollView _markerList;
private Label _formulaPreview;
public override VisualElement CreatePropertyGUI(SerializedProperty property)
{
_property = property;
_propertySourceDict = property.FindPropertyRelative(nameof(DynamicValue._serializedSources));
_propertyTypeDict = property.FindPropertyRelative(nameof(DynamicValue._serializedTypes));
_propertyFormula = property.FindPropertyRelative(nameof(DynamicValue._serializedFormula));
_attribute = fieldInfo.GetCustomAttribute<DynamicValueAttribute>() ?? new DynamicValueAttribute();
var rootVisualElement = _visualTreeAsset.CloneTree();
// FIND ELEMENTS
_background = rootVisualElement.Q("dv-background");
_foldoutLabel = rootVisualElement.Q<Foldout>("dv-foldout-label");
_foldoutLabel.text = property.displayName;
_foldoutLabel.Q<Toggle>().RegisterValueChangedCallback(ev => TogglePreview(!ev.newValue));
_formulaEdit = rootVisualElement.Q<TextField>("dv-formula-edit");
_formulaEdit.BindProperty(_propertyFormula);
_objectDictField = rootVisualElement.Q<PropertyField>("dv-sources-objects");
_objectDictField.label = "";
_objectDictField.BindProperty(_propertySourceDict);
_objectDictField.TrackPropertyValue(_propertySourceDict, _ => OnSourceChanged());
_typeDictField = rootVisualElement.Q<PropertyField>("dv-sources-types");
_typeDictField.label = "";
_typeDictField.BindProperty(_propertyTypeDict);
_typeDictField.TrackPropertyValue(_propertyTypeDict, _ => OnSourceChanged());
_markerList = rootVisualElement.Q("dv-markers").Q<ScrollView>();
_formulaPreview = rootVisualElement.Q<Label>("dv-formula-preview");
// BACKGROUND HIGHLIGHT
rootVisualElement.RegisterCallback<MouseEnterEvent>(_ => _background.visible = true);
rootVisualElement.RegisterCallback<MouseLeaveEvent>(_ => _background.visible = false);
OnSourceChanged();
// DEFAULT STATE
TogglePreview(!string.IsNullOrEmpty(_propertyFormula.stringValue));
return rootVisualElement;
}
private void TogglePreview(bool show)
{
_formulaPreview.style.display = show ? DisplayStyle.Flex : DisplayStyle.None;
_background.style.display = show ? DisplayStyle.None : DisplayStyle.Flex;
_foldoutLabel.SetValueWithoutNotify(!show);
_formulaPreview.text = _propertyFormula.stringValue;
if (show) ValidateMarkers();
}
private void ValidateMarkers()
{
var sourceList = GetSourceTypes();
var invalidMarkers = new List<string>();
var value = (DynamicValue)_property.boxedValue;
// This is fix - when using SerializedReference via boxedValue it will always return null.
// To prevent this we have to get manually SerializedReference values via SerializedProperty.
// Also, we don't want to update serialized data!
value._serializedTypes.Keys.ToList().ForEach((i, key) => value._serializedTypes[key] =
_propertyTypeDict.FindPropertyRelative("_values").GetArrayElementAtIndex(i).boxedValue as SerializableType);
foreach (var marker in value.GetMarkers())
{
var color = "#23C552";
try
{
var sourceName = sourceList.Keys.FirstOrDefault(sourceName =>
sourceName.Equals(marker.sourceMarker, StringComparison.OrdinalIgnoreCase));
// Check if used marker uses source that is provided via serialization (via object or type)
if (string.IsNullOrEmpty(sourceName))
throw new DynamicValueInvalidMarkerException("SOURCE TYPE DO NOT EXIST");
// This will parse marker and throw exception if something wrong will occur
marker.GetMembers();
}
catch (Exception e)
{
if (e is not DynamicValueInvalidMarkerException and not MemberNotFoundException) throw;
Debug.LogException(e);
color = "#F84F31";
invalidMarkers.Add(marker.wholeMarker);
}
_formulaPreview.text = _formulaPreview.text.Replace(marker.wholeMarker, $"<color={color}>{marker.wholeMarker}</color>");
}
if (invalidMarkers.Any())
{
var errorBox = new HelpBox($"Found invalid markers: <b>{string.Join("</b>, <b>", invalidMarkers)}</b>.", HelpBoxMessageType.Error);
errorBox.FadeOut(30000, 250, true);
errorBox.RemoveOnClick();
_foldoutLabel.InsertBefore(errorBox);
}
}
private void OnSourceChanged()
{
// CREATE SOURCE LIST
switch (_attribute.type)
{
case DynamicValueType.BySerializedSources:
_objectDictField.style.display = DisplayStyle.Flex;
_typeDictField.style.display = DisplayStyle.None;
break;
case DynamicValueType.ByDynamicTypes:
case DynamicValueType.ByStaticTypes:
_typeDictField.SetEnabled(false);
_objectDictField.style.display = DisplayStyle.None;
_typeDictField.style.display = DisplayStyle.Flex;
break;
default:
throw new ArgumentOutOfRangeException();
}
var allSourceTypes = GetSourceTypes();
// REMOVE MARKER LIST - remove changed and deleted ones
_markerList.Children().ToList()
.Where(markerElement => allSourceTypes.GetValueOrDefault(markerElement.name) != (Type)markerElement.userData)
.ForEach(_markerList.Remove);
// CREATE MARKER LIST - add missing ones
allSourceTypes
.Where(pair => _markerList.Children().All(marker => marker.name != pair.Key))
.Select(pair => CreateMarkerListElement(pair.Key, pair.Value))
.ForEach(_markerList.Add);
if (!_foldoutLabel.value) ValidateMarkers();
}
private VisualElement CreateMarkerListElement(string name, Type type, string path = "")
{
var foldout = new Foldout();
foldout.text = $"<b>{name}</b> ({type.Name})";
foldout.value = false;
// Path not available - first level marker - this string represent source
if (string.IsNullOrEmpty(path))
{
foldout.name = name;
foldout.userData = type;
path = name;
}
var copyButton = new Button();
copyButton.text = "Copy";
copyButton.style.width = 50f;
copyButton.style.unityTextAlign = TextAnchor.MiddleCenter;
copyButton.style.paddingRight = 0;
copyButton.style.paddingLeft = 0;
copyButton.style.marginTop = 0;
copyButton.style.marginBottom = 0;
copyButton.clicked += () => EditorGUIUtility.systemCopyBuffer = $"{{{path}}}";
copyButton.clicked += () => copyButton.text = "Copied!";
copyButton.clicked += () => copyButton.schedule.Execute(() => copyButton.text = "Copy").ExecuteLater(3000);
var foldoutText = foldout.Q(className: "unity-foldout__text");
foldoutText.style.flexShrink = 1;
var foldoutInput = foldout.Q(className: "unity-foldout__input");
foldoutInput.style.maxWidth = Length.Percent(100f);
foldoutInput.Add(copyButton);
var membersToDisplay = MarkerParser.GetMembersForType(type, _attribute);
// BACKGROUND HIGHLIGHT
foldoutInput.RegisterCallback<MouseEnterEvent>(_ => foldoutInput.style.backgroundColor = new Color(255,255,255,0.1f));
foldoutInput.RegisterCallback<MouseLeaveEvent>(_ => foldoutInput.style.backgroundColor = StyleKeyword.Null);
// NO CHILDREN - hide extend/collapse arrow
if (!membersToDisplay.Any()) foldout.Q("unity-checkmark").style.visibility = Visibility.Hidden;
// ONLY ONCE - when foldout extended then fill its content
foldout.RegisterCallbackOnce<ChangeEvent<bool>>(_ =>
{
foreach (var member in membersToDisplay)
{
var parameters = MarkerParser.GetSubTypesInProvider(member, out var isProvider, out var isDynamic);
if (isProvider)
{
// This is provider - it can have multiple parameters in addition to this marker
parameters.ForEach(subType =>
{
var subName = $"{member.Name}[" + (isDynamic ? "---" : subType.Name)+ "]";
var element = CreateMarkerListElement(subName, subType, $"{path}.{subName}");
foldout.Add(element);
});
}
else
{
// This is just simple marker
foldout.Add(CreateMarkerListElement($"{member.Name}", member.GetMemberValueType(), $"{path}.{member.Name}"));
}
}
});
// LITTLE FIX - dragger sometimes don't change its height
foldout.RegisterValueChangedCallback(_ => _markerList.Q("unity-dragger").style.height = Length.Auto());
return foldout;
}
private Dictionary<string, Type> GetSourceTypes()
{
switch (_attribute.type)
{
case DynamicValueType.BySerializedSources:
var objKeyProps = _propertySourceDict.FindPropertyRelative("_keys");
var objValueProps = _propertySourceDict.FindPropertyRelative("_values");
return Enumerable.Range(0, objKeyProps.arraySize)
.ToDictionary(
i => objKeyProps.GetArrayElementAtIndex(i).boxedValue as string,
i => objValueProps.GetArrayElementAtIndex(i).boxedValue.GetType()
)
.Where(pair => pair.Value != null)
.ToDictionary(pair => pair.Key, pair => pair.Value);
case DynamicValueType.ByDynamicTypes or DynamicValueType.ByStaticTypes:
var typeKeyProps = _propertyTypeDict.FindPropertyRelative("_keys");
var typeValueProps = _propertyTypeDict.FindPropertyRelative("_values");
return Enumerable.Range(0, typeKeyProps.arraySize)
.ToDictionary(
i => typeKeyProps.GetArrayElementAtIndex(i).boxedValue as string,
i => (typeValueProps.GetArrayElementAtIndex(i).boxedValue as SerializableType)?.type
)
.Where(pair => pair.Value != null)
.ToDictionary(pair => pair.Key, pair => pair.Value);
default:
throw new ArgumentOutOfRangeException();
}
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 7310d287a63540638415ae227def94b9
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences:
- _visualTreeAsset: {fileID: 9197481963319205126, guid: e692746f57132b74aadeafd745f0f16e, type: 3}
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,84 @@
#dv-background {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
background-color: rgba(255, 255, 255, 0.04);
margin-left: -12px;
margin-right: -12px;
visibility: hidden;
}
#dv-foldout-label {
}
#dv-foldout-label Foldout > Toggle {
margin-left: 0;
margin-top: 1px;
margin-bottom: 1px;
max-width: 100%;
}
#dv-formula-preview {
padding-top: 1px;
padding-right: 4px;
padding-bottom: 1px;
padding-left: 4px;
border-top-width: 1px;
border-right-width: 1px;
border-bottom-width: 1px;
border-left-width: 1px;
border-top-left-radius: 3px;
border-top-right-radius: 3px;
border-bottom-right-radius: 3px;
border-bottom-left-radius: 3px;
border-left-color: var(--unity-colors-helpbox-border);
border-right-color: var(--unity-colors-helpbox-border);
border-top-color: var(--unity-colors-helpbox-border);
border-bottom-color: var(--unity-colors-helpbox-border);
background-color: var(--unity-colors-helpbox-background);
margin-left: 15px;
min-height: 18px;
}
#dv-formula-edit {
margin-left: 0;
}
#dv-formula-edit > TextInput {
min-height: 45px;
}
#dv-sources {
margin-top: 5px;
padding-right: 3px;
padding-left: 3px;
margin-bottom: 5px;
}
#dv-markers {
margin-top: 5px;
padding-left: 3px;
padding-right: 3px;
margin-bottom: 5px;
}
.border {
border-top-left-radius: 2px;
border-bottom-left-radius: 2px;
border-top-right-radius: 2px;
border-bottom-right-radius: 2px;
border-right-width: 1px;
border-top-width: 1px;
border-bottom-width: 1px;
border-left-width: 1px;
border-left-color: rgb(0, 0, 0);
border-right-color: rgb(0, 0, 0);
border-top-color: rgb(0, 0, 0);
border-bottom-color: rgb(0, 0, 0);
}
.unity-base-field {
margin-right: 0;
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 683dc949e5b79274f8fbc2bc650fd84b
ScriptedImporter:
internalIDToNameTable: []
externalObjects: {}
serializedVersion: 2
userData:
assetBundleName:
assetBundleVariant:
script: {fileID: 12385, guid: 0000000000000000e000000000000000, type: 0}
disableValidation: 0
@@ -0,0 +1,17 @@
<ui:UXML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" noNamespaceSchemaLocation="../../../../../UIElementsSchema/UIElements.xsd" editor-extension-mode="True">
<Style src="project://database/Assets/TheVVaS/RPGCoreCommon/DynamicValues/Editor/DynamicValueDrawer.uss?fileID=7433441132597879392&amp;guid=683dc949e5b79274f8fbc2bc650fd84b&amp;type=3#DynamicValueDrawer" />
<ui:VisualElement name="dv-root">
<ui:VisualElement name="dv-background" />
<ui:Foldout text="{property_name}" name="dv-foldout-label">
<ui:TextField placeholder-text="Formula here" multiline="true" name="dv-formula-edit" />
<ui:Foldout text="Sources:" name="dv-sources" class="border">
<uie:PropertyField name="dv-sources-objects" />
<uie:PropertyField name="dv-sources-types" />
</ui:Foldout>
<ui:Foldout text="Markers:" name="dv-markers" class="border">
<ui:ScrollView name="dv-markers-list" horizontal-scroller-visibility="Hidden" vertical-scroller-visibility="AlwaysVisible" />
</ui:Foldout>
</ui:Foldout>
<ui:Label text="{preview_formula}" name="dv-formula-preview" />
</ui:VisualElement>
</ui:UXML>
@@ -0,0 +1,10 @@
fileFormatVersion: 2
guid: e692746f57132b74aadeafd745f0f16e
ScriptedImporter:
internalIDToNameTable: []
externalObjects: {}
serializedVersion: 2
userData:
assetBundleName:
assetBundleVariant:
script: {fileID: 13804, guid: 0000000000000000e000000000000000, type: 0}
@@ -0,0 +1,20 @@
{
"name": "RPGCoreCommon.DynamicValues.Editor",
"rootNamespace": "RPGCoreCommon.DynamicValues.Editor",
"references": [
"RPGCoreCommon.DynamicValues",
"RPGCoreCommon.Helpers.Editor",
"RPGCoreCommon.Helpers"
],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 63d26f6c5e724ba085544f845889ee92
timeCreated: 1759334877