init
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4914a4d27525472aac1393e98ed6b8e2
|
||||
timeCreated: 1760521419
|
||||
@@ -0,0 +1,71 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using RPGCoreCommon.Helpers;
|
||||
using UnityEditor;
|
||||
using UnityEditor.UIElements;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace RPGCoreCommon.DropTable.Editor
|
||||
{
|
||||
[Serializable]
|
||||
[CanEditMultipleObjects]
|
||||
[CustomEditor(typeof(DropTableSO))]
|
||||
public class DropTableDrawer : UnityEditor.Editor
|
||||
{
|
||||
[SerializeField] private VisualTreeAsset _groupVisualTreeAsset;
|
||||
[SerializeField] private VisualTreeAsset _rowVisualTreeAsset;
|
||||
public static VisualTreeAsset groupVisualTreeAsset { get; private set; }
|
||||
public static VisualTreeAsset rowVisualTreeAsset { get; private set; }
|
||||
|
||||
public override VisualElement CreateInspectorGUI()
|
||||
{
|
||||
groupVisualTreeAsset = _groupVisualTreeAsset;
|
||||
rowVisualTreeAsset = _rowVisualTreeAsset;
|
||||
|
||||
if (serializedObject.isEditingMultipleObjects)
|
||||
return new HelpBox($"Multiple {nameof(DropTableSO)} editing is disabled.", HelpBoxMessageType.Warning);
|
||||
|
||||
var property = serializedObject.FindProperty(nameof(DropTableSO.group));
|
||||
|
||||
var wrapper = new VisualElement();
|
||||
wrapper.schedule.Execute(() =>
|
||||
{
|
||||
var mainInspector = wrapper.GetFirstAncestorOfType<InspectorElement>();
|
||||
mainInspector.style.paddingLeft = 4f;
|
||||
mainInspector.style.paddingRight = 4f;
|
||||
});
|
||||
|
||||
var resultBox = new HelpBox();
|
||||
resultBox.style.display = DisplayStyle.None;
|
||||
resultBox.style.unityTextAlign = TextAnchor.MiddleCenter;
|
||||
|
||||
var resultButton = new Button();
|
||||
resultButton.style.marginTop = 20f;
|
||||
resultButton.clicked += () => PrintResult(resultBox, property.boxedValue as DropTableGroup);
|
||||
resultButton.text = "Try your luck...";
|
||||
|
||||
wrapper.Add(new DropTableGroupElement(property));
|
||||
wrapper.Add(resultButton);
|
||||
wrapper.Add(resultBox);
|
||||
return wrapper;
|
||||
}
|
||||
|
||||
private void PrintResult(HelpBox resultBox, DropTableGroup value)
|
||||
{
|
||||
resultBox.Clear();
|
||||
resultBox.style.display = DisplayStyle.Flex;
|
||||
var result = new Dictionary<Object, int>();
|
||||
value.Get(ref result);
|
||||
result.DictForEach((item, count) =>
|
||||
{
|
||||
var row = new VisualElement();
|
||||
row.style.flexDirection = FlexDirection.Row;
|
||||
row.Add(new ObjectField{value = item, enabledSelf = false});
|
||||
row.Add(new Label($" -> {count}"));
|
||||
resultBox.Add(row);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 287610d3321e42ccb5dbad00b9b585e5
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences:
|
||||
- _groupVisualTreeAsset: {fileID: 9197481963319205126, guid: 6d2d365fc9f0e0e4a93cee141a824492, type: 3}
|
||||
- _rowVisualTreeAsset: {fileID: 9197481963319205126, guid: 0b23d9b3ec1017740933e19785b8964b, type: 3}
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,85 @@
|
||||
#dt-config {
|
||||
background-color: rgba(0, 0, 0, 0.39);
|
||||
border-top-width: 0;
|
||||
border-right-width: 0;
|
||||
border-bottom-width: 0;
|
||||
border-left-width: 0;
|
||||
}
|
||||
|
||||
#dt-config > * {
|
||||
flex-grow: 0;
|
||||
flex-shrink: 1;
|
||||
-unity-text-align: middle-center;
|
||||
margin-left: 1px;
|
||||
margin-right: 1px;
|
||||
}
|
||||
|
||||
#dt-config > ToolbarSpacer {
|
||||
flex-grow: 1;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
#dt-config > #dt-add-row {
|
||||
border-top-width: 1px;
|
||||
border-right-width: 1px;
|
||||
border-bottom-width: 1px;
|
||||
border-left-width: 1px;
|
||||
border-top-left-radius: 4px;
|
||||
border-top-right-radius: 4px;
|
||||
border-bottom-right-radius: 4px;
|
||||
border-bottom-left-radius: 4px;
|
||||
border-left-color: rgb(26, 26, 26);
|
||||
border-right-color: rgb(26, 26, 26);
|
||||
border-top-color: rgb(26, 26, 26);
|
||||
border-bottom-color: rgb(26, 26, 26);
|
||||
-unity-text-align: middle-center;
|
||||
font-size: 14px;
|
||||
-unity-font-style: bold;
|
||||
}
|
||||
|
||||
#dt-config > #dt-chance-type {
|
||||
}
|
||||
|
||||
#dt-config > #dt-chance-type > VisualElement {
|
||||
margin-top: 0;
|
||||
margin-right: 0;
|
||||
margin-bottom: 0;
|
||||
margin-left: 0;
|
||||
padding-top: 0;
|
||||
padding-right: 0;
|
||||
padding-bottom: 0;
|
||||
padding-left: 0;
|
||||
}
|
||||
|
||||
#dt-config > #dt-chance-type > VisualElement > TextElement {
|
||||
margin-top: 0;
|
||||
margin-right: 0;
|
||||
margin-bottom: 0;
|
||||
margin-left: 0;
|
||||
padding-right: 8px;
|
||||
padding-left: 8px;
|
||||
}
|
||||
|
||||
#dt-list > ScrollView {
|
||||
padding-bottom: 0;
|
||||
}
|
||||
|
||||
#unity-list-view__reorderable-item__container {
|
||||
padding-left: 2px;
|
||||
padding-right: 2px;
|
||||
padding-bottom: 1px;
|
||||
padding-top: 1px;
|
||||
}
|
||||
|
||||
#unity-list-view__reorderable-handle {
|
||||
justify-content: center;
|
||||
margin-top: 0;
|
||||
margin-right: 0;
|
||||
margin-bottom: 0;
|
||||
margin-left: 0;
|
||||
padding-top: 0;
|
||||
padding-right: 0;
|
||||
padding-bottom: 0;
|
||||
padding-left: 0;
|
||||
display: none;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f1b1bb265dde2264f9cb1caf449494f3
|
||||
ScriptedImporter:
|
||||
internalIDToNameTable: []
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
script: {fileID: 12385, guid: 0000000000000000e000000000000000, type: 0}
|
||||
disableValidation: 0
|
||||
@@ -0,0 +1,11 @@
|
||||
<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/DropTable/Editor/DropTableGroup.uss?fileID=7433441132597879392&guid=f1b1bb265dde2264f9cb1caf449494f3&type=3#DropTableGroup" />
|
||||
<ui:VisualElement name="dt-group">
|
||||
<uie:Toolbar name="dt-config">
|
||||
<ui:EnumField value="Center" type="RPGCoreCommon.DropTable.ChanceTypeEnum, RPGCoreCommon.DropTable" name="dt-chance-type" />
|
||||
<uie:ToolbarSpacer />
|
||||
<ui:Button text="+" name="dt-add-row" />
|
||||
</uie:Toolbar>
|
||||
<ui:ListView name="dt-list" reorder-mode="Animated" reorderable="true" selection-type="None" virtualization-method="DynamicHeight" show-border="true" show-bound-collection-size="false" />
|
||||
</ui:VisualElement>
|
||||
</ui:UXML>
|
||||
@@ -0,0 +1,10 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6d2d365fc9f0e0e4a93cee141a824492
|
||||
ScriptedImporter:
|
||||
internalIDToNameTable: []
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
script: {fileID: 13804, guid: 0000000000000000e000000000000000, type: 0}
|
||||
@@ -0,0 +1,80 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using UnityEditor;
|
||||
using UnityEditor.UIElements;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace RPGCoreCommon.DropTable.Editor
|
||||
{
|
||||
public class DropTableGroupElement : VisualElement
|
||||
{
|
||||
// Property data
|
||||
private SerializedProperty _property;
|
||||
private SerializedProperty _propertyRows;
|
||||
private List<DropTableGroupRowElement> _rowElements = new();
|
||||
|
||||
// Elements
|
||||
private readonly EnumField _chanceTypeField;
|
||||
private readonly VisualElement _addRowButton;
|
||||
private readonly ListView _list;
|
||||
|
||||
public DropTableGroupElement(SerializedProperty property) : this()
|
||||
{
|
||||
BindProperty(property);
|
||||
}
|
||||
|
||||
public DropTableGroupElement()
|
||||
{
|
||||
DropTableDrawer.groupVisualTreeAsset.CloneTree(this);
|
||||
|
||||
SetEnabled(false);
|
||||
|
||||
// CHANCE TYPE
|
||||
_chanceTypeField = this.Q<EnumField>("dt-chance-type");
|
||||
_chanceTypeField.RegisterValueChangedCallback(_ => Refresh());
|
||||
|
||||
// ADD (replacement for default list button)
|
||||
_addRowButton = this.Q("dt-add-row");
|
||||
_addRowButton.RegisterCallback<ClickEvent>(_ => _list.onAdd(_list));
|
||||
|
||||
// LIST
|
||||
_list = this.Q<ListView>("dt-list");
|
||||
_list.makeItem = () => new DropTableGroupRowElement();
|
||||
_list.bindItem = (element, i) =>
|
||||
{
|
||||
var rowElement = element as DropTableGroupRowElement;
|
||||
_rowElements.Add(rowElement);
|
||||
rowElement.BindProperty(_propertyRows.GetArrayElementAtIndex(i));
|
||||
};
|
||||
_list.unbindItem = (element, _) => _rowElements.Remove(element as DropTableGroupRowElement);
|
||||
_list.onAdd = _ =>
|
||||
{
|
||||
_propertyRows.arraySize++;
|
||||
_propertyRows.GetArrayElementAtIndex(_propertyRows.arraySize - 1).boxedValue = new DropTableGroupRow();
|
||||
_propertyRows.serializedObject.ApplyModifiedProperties();
|
||||
_list.RefreshItems();
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Created by <see cref="DropTableDrawer"/>. First in hierarchy <see cref="DropTableGroupElement"/>.
|
||||
/// </summary>
|
||||
internal void BindProperty(SerializedProperty property)
|
||||
{
|
||||
_property = property;
|
||||
_propertyRows = _property.FindPropertyRelative(nameof(DropTableGroup.rows));
|
||||
|
||||
_chanceTypeField.BindProperty(_property.FindPropertyRelative(nameof(DropTableGroup.chanceType)));
|
||||
_list.BindProperty(_propertyRows);
|
||||
|
||||
SetEnabled(true);
|
||||
}
|
||||
|
||||
internal void Refresh()
|
||||
{
|
||||
var weightSum = (_property.boxedValue as DropTableGroup).rows.Sum(row => row.chance);
|
||||
_rowElements.ForEach(row => row.Refresh((ChanceTypeEnum)_chanceTypeField.value, weightSum));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b971c281bc214ba8b7502d1f1a22cbd1
|
||||
timeCreated: 1760895922
|
||||
@@ -0,0 +1,130 @@
|
||||
#dt-row {
|
||||
flex-direction: row;
|
||||
border-top-left-radius: 4px;
|
||||
border-top-right-radius: 4px;
|
||||
border-bottom-right-radius: 4px;
|
||||
border-bottom-left-radius: 4px;
|
||||
}
|
||||
|
||||
#dt-percent-background {
|
||||
background-color: rgba(255, 0, 0, 0.29);
|
||||
align-self: stretch;
|
||||
justify-content: center;
|
||||
cursor: resize-vertical;
|
||||
border-top-left-radius: 4px;
|
||||
border-top-right-radius: 4px;
|
||||
border-bottom-right-radius: 4px;
|
||||
border-bottom-left-radius: 4px;
|
||||
}
|
||||
|
||||
#dt-percent {
|
||||
color: rgb(255, 250, 250);
|
||||
-unity-font-style: bold;
|
||||
-unity-text-align: middle-center;
|
||||
margin-top: 0;
|
||||
margin-right: -5px;
|
||||
margin-bottom: 0;
|
||||
margin-left: -5px;
|
||||
padding-top: 0;
|
||||
padding-right: 0;
|
||||
padding-bottom: 0;
|
||||
padding-left: 0;
|
||||
width: 60px;
|
||||
}
|
||||
|
||||
#dt-right {
|
||||
flex-grow: 1;
|
||||
}
|
||||
|
||||
#dt-right.single-line {
|
||||
flex-direction: row-reverse;
|
||||
}
|
||||
|
||||
#dt-header {
|
||||
flex-direction: row;
|
||||
justify-content: flex-end;
|
||||
}
|
||||
|
||||
#dt-header > * {
|
||||
margin-top: 1px;
|
||||
margin-right: 1px;
|
||||
margin-bottom: 1px;
|
||||
margin-left: 1px;
|
||||
}
|
||||
|
||||
#dt-header > #dt-chance {
|
||||
}
|
||||
|
||||
#dt-header > #dt-chance > Label {
|
||||
min-width: auto;
|
||||
}
|
||||
|
||||
#dt-header > #dt-chance > #unity-text-input {
|
||||
flex-grow: 0;
|
||||
min-width: 50px;
|
||||
}
|
||||
|
||||
#dt-header > #dt-count {
|
||||
}
|
||||
|
||||
#dt-header > #dt-count > Label {
|
||||
min-width: auto;
|
||||
}
|
||||
|
||||
#dt-header > #dt-count > #unity-text-input {
|
||||
flex-grow: 0;
|
||||
min-width: 50px;
|
||||
}
|
||||
|
||||
#dt-body {
|
||||
min-height: 20px;
|
||||
flex-grow: 1;
|
||||
padding-left: 3px;
|
||||
}
|
||||
|
||||
#dt-body > ObjectField {
|
||||
margin-top: 0;
|
||||
margin-right: 0;
|
||||
margin-bottom: 0;
|
||||
margin-left: 0;
|
||||
}
|
||||
|
||||
#dt-body > ObjectField > Label {
|
||||
min-width: auto;
|
||||
}
|
||||
|
||||
#dt-drop-here {
|
||||
position: absolute;
|
||||
left: 0;
|
||||
top: 0;
|
||||
right: 0;
|
||||
bottom: 0;
|
||||
background-color: var(--unity-colors-window-background);
|
||||
border-top-width: 1px;
|
||||
border-right-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);
|
||||
border-top-left-radius: 4px;
|
||||
border-top-right-radius: 4px;
|
||||
border-bottom-right-radius: 4px;
|
||||
border-bottom-left-radius: 4px;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
background-image: url("project://database/Packages/com.unity.timeline/Editor/StyleSheets/Images/DarkSkin/TimelineDisabledBackground.png?fileID=2800000&guid=412f73153f3a3c549b67b2a847741145&type=3#TimelineDisabledBackground");
|
||||
display: none;
|
||||
}
|
||||
|
||||
#dt-drop-here:hover {
|
||||
-unity-background-image-tint-color: rgb(255, 235, 4);
|
||||
border-left-color: rgb(126, 117, 2);
|
||||
border-right-color: rgb(126, 117, 2);
|
||||
border-top-color: rgb(126, 117, 2);
|
||||
border-bottom-color: rgb(126, 117, 2);
|
||||
}
|
||||
|
||||
#dt-drop-here > #dt-new-inline {
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a4b26fa3a17d7914bb564222d2d3c1b4
|
||||
ScriptedImporter:
|
||||
internalIDToNameTable: []
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
script: {fileID: 12385, guid: 0000000000000000e000000000000000, type: 0}
|
||||
disableValidation: 0
|
||||
@@ -0,0 +1,22 @@
|
||||
<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/DropTable/Editor/DropTableGroupRow.uss?fileID=7433441132597879392&guid=a4b26fa3a17d7914bb564222d2d3c1b4&type=3#DropTableGroupRow" />
|
||||
<ui:VisualElement name="dt-row">
|
||||
<ui:VisualElement name="dt-percent-background">
|
||||
<ui:Label text="12.34%" name="dt-percent" picking-mode="Ignore" />
|
||||
</ui:VisualElement>
|
||||
<ui:VisualElement name="dt-right">
|
||||
<ui:VisualElement name="dt-header">
|
||||
<ui:FloatField value="42.2" name="dt-chance" label="{chance}:" />
|
||||
<ui:IntegerField value="42" name="dt-count" label="Count:" />
|
||||
</ui:VisualElement>
|
||||
<ui:VisualElement name="dt-body">
|
||||
<uie:ObjectField name="dt-item" allow-scene-objects="false" label="Item:" />
|
||||
<uie:ObjectField name="dt-group-reference" allow-scene-objects="false" />
|
||||
</ui:VisualElement>
|
||||
</ui:VisualElement>
|
||||
<ui:VisualElement name="dt-drop-here" class="drop-area" style="display: none;">
|
||||
<ui:Label text="Drop <b>Asset</b> or <b>DropTableSO</b> here<br>" />
|
||||
<ui:Label text="Or <u>Click Here</u> to create inline" name="dt-new-inline" />
|
||||
</ui:VisualElement>
|
||||
</ui:VisualElement>
|
||||
</ui:UXML>
|
||||
@@ -0,0 +1,10 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0b23d9b3ec1017740933e19785b8964b
|
||||
ScriptedImporter:
|
||||
internalIDToNameTable: []
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
script: {fileID: 13804, guid: 0000000000000000e000000000000000, type: 0}
|
||||
@@ -0,0 +1,263 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using UnityEditor;
|
||||
using UnityEditor.UIElements;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
using Cursor = UnityEngine.Cursor;
|
||||
|
||||
namespace RPGCoreCommon.DropTable.Editor
|
||||
{
|
||||
internal class DropTableGroupRowElement : VisualElement
|
||||
{
|
||||
// Property data
|
||||
private SerializedProperty _property;
|
||||
private SerializedProperty _propertyInlineGroup;
|
||||
private DropTableGroupRow value => (DropTableGroupRow)_property.boxedValue;
|
||||
|
||||
// Elements
|
||||
private readonly VisualElement _percentBackground;
|
||||
private readonly Label _percentLabel;
|
||||
private readonly VisualElement _rightElement;
|
||||
private readonly FloatField _chanceField;
|
||||
private readonly IntegerField _countField;
|
||||
private readonly ObjectField _itemField;
|
||||
private readonly ObjectField _groupReferenceField;
|
||||
private readonly DropTableGroupElement _groupInline;
|
||||
private readonly Label _newInlineLabel;
|
||||
private readonly VisualElement _dropHere;
|
||||
|
||||
internal DropTableGroupRowElement()
|
||||
{
|
||||
DropTableDrawer.rowVisualTreeAsset.CloneTree(this);
|
||||
|
||||
this.AddManipulator(new ContextualMenuManipulator(null));
|
||||
RegisterCallback<ContextualMenuPopulateEvent>(ev =>
|
||||
{
|
||||
ev.StopImmediatePropagation();
|
||||
|
||||
ev.menu.InsertAction(0, "Reset row", _ =>
|
||||
{
|
||||
SetEditingMode(EditTypeEnum.None);
|
||||
});
|
||||
ev.menu.InsertAction(1, "Remove row", _ =>
|
||||
{
|
||||
var arrayPath = string.Join(".", _property.propertyPath.Split(".").SkipLast(2));
|
||||
var arrayProperty = _property.serializedObject.FindProperty(arrayPath);
|
||||
var i = int.Parse(_property.propertyPath[_property.propertyPath.LastIndexOf('[')..].Trim('[', ']'));
|
||||
arrayProperty.DeleteArrayElementAtIndex(i);
|
||||
_property.serializedObject.ApplyModifiedProperties();
|
||||
});
|
||||
ev.menu.InsertSeparator(null, 2);
|
||||
});
|
||||
|
||||
// PERCENTAGE
|
||||
_percentBackground = this.Q("dt-percent-background");
|
||||
_percentLabel = this.Q<Label>("dt-percent");
|
||||
|
||||
// RIGHT SIDE
|
||||
_rightElement = this.Q("dt-right");
|
||||
|
||||
// CHANCE
|
||||
_chanceField = this.Q<FloatField>("dt-chance");
|
||||
_chanceField.RegisterValueChangedCallback(_ =>
|
||||
{
|
||||
_chanceField.SetValueWithoutNotify(Mathf.Clamp(_chanceField.value, 0, 100));
|
||||
RefreshGroup();
|
||||
});
|
||||
|
||||
// COUNT
|
||||
_countField = this.Q<IntegerField>("dt-count");
|
||||
_countField.RegisterValueChangedCallback(_ => _countField.SetValueWithoutNotify(Mathf.Max(0, _countField.value)));
|
||||
|
||||
// CREATE INLINE
|
||||
_newInlineLabel = this.Q<Label>("dt-new-inline");
|
||||
_newInlineLabel.RegisterCallback<ClickEvent>(_ => SetEditingMode(EditTypeEnum.InlineGroup));
|
||||
|
||||
// DROP AREA
|
||||
_dropHere = this.Q("dt-drop-here");
|
||||
RegisterCallback<DragLeaveEvent>(_ => ShowDropHere());
|
||||
RegisterCallback<DragExitedEvent>(_ => ShowDropHere());
|
||||
RegisterCallback<DragUpdatedEvent>(ev =>
|
||||
{
|
||||
if ((ev.target as VisualElement).GetFirstAncestorOfType<DropTableGroupRowElement>() != this)
|
||||
{
|
||||
ShowDropHere(false);
|
||||
return;
|
||||
}
|
||||
|
||||
if (value.subGroupInline is not null)
|
||||
{
|
||||
ShowDropHere(false);
|
||||
return;
|
||||
}
|
||||
|
||||
ShowDropHere(true);
|
||||
});
|
||||
RegisterCallback<DragPerformEvent>(ev =>
|
||||
{
|
||||
ev.StopImmediatePropagation();
|
||||
|
||||
if (DragAndDrop.objectReferences.Length > 1)
|
||||
Debug.LogWarning("Trying to drag more than one <b>Object</b> to row, using only first one.");
|
||||
|
||||
SetEditingMode(EditTypeEnum.None);
|
||||
|
||||
var obj = DragAndDrop.objectReferences[0];
|
||||
if (obj is DropTableSO dropTableSO)
|
||||
{
|
||||
_groupReferenceField.value = dropTableSO;
|
||||
SetEditingMode(EditTypeEnum.ReferenceGroup);
|
||||
}
|
||||
else
|
||||
{
|
||||
_itemField.value = obj;
|
||||
SetEditingMode(EditTypeEnum.Item);
|
||||
}
|
||||
|
||||
ShowDropHere();
|
||||
});
|
||||
|
||||
// ITEM
|
||||
_itemField = this.Q<ObjectField>("dt-item");
|
||||
|
||||
// SUBGROUP REF
|
||||
_groupReferenceField = this.Q<ObjectField>("dt-group-reference");
|
||||
_groupReferenceField.objectType = typeof(DropTableSO);
|
||||
|
||||
// SUBGROUP INLINE
|
||||
_groupInline = new DropTableGroupElement { name = "dt-group-inline" };
|
||||
this.Q("dt-body").Add(_groupInline);
|
||||
|
||||
SetEnabled(false);
|
||||
}
|
||||
|
||||
private void ShowDropHere()
|
||||
{
|
||||
ShowDropHere(!value.item && !value.subGroupRef && value.subGroupInline is null);
|
||||
}
|
||||
|
||||
private void ShowDropHere(bool show)
|
||||
{
|
||||
if (show)
|
||||
{
|
||||
_dropHere.style.display = DisplayStyle.Flex;
|
||||
_dropHere.pickingMode = PickingMode.Position;
|
||||
DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
|
||||
|
||||
ShowCreateNewInline(!value.item && !value.subGroupRef && value.subGroupInline is null);
|
||||
}
|
||||
else
|
||||
{
|
||||
_dropHere.style.display = DisplayStyle.None;
|
||||
_dropHere.pickingMode = PickingMode.Ignore;
|
||||
ShowCreateNewInline(false);
|
||||
}
|
||||
}
|
||||
|
||||
private void ShowCreateNewInline(bool show)
|
||||
{
|
||||
_newInlineLabel.style.display = show ? DisplayStyle.Flex : DisplayStyle.None;
|
||||
}
|
||||
|
||||
internal void BindProperty(SerializedProperty property)
|
||||
{
|
||||
SetEnabled(true);
|
||||
|
||||
_property = property;
|
||||
_propertyInlineGroup = property.FindPropertyRelative(nameof(DropTableGroupRow.subGroupInline));
|
||||
var value = _property.boxedValue as DropTableGroupRow;
|
||||
|
||||
_chanceField.BindProperty(_property.FindPropertyRelative(nameof(DropTableGroupRow.chance)));
|
||||
_countField.BindProperty(_property.FindPropertyRelative(nameof(DropTableGroupRow.count)));
|
||||
_itemField.BindProperty(_property.FindPropertyRelative(nameof(DropTableGroupRow.item)));
|
||||
_groupReferenceField.BindProperty(_property.FindPropertyRelative(nameof(DropTableGroupRow.subGroupRef)));
|
||||
|
||||
// Default editing mode by currently set content
|
||||
if (value.item) SetEditingMode(EditTypeEnum.Item);
|
||||
else if (value.subGroupRef) SetEditingMode(EditTypeEnum.ReferenceGroup);
|
||||
else if (value.subGroupInline != null) SetEditingMode(EditTypeEnum.InlineGroup);
|
||||
else SetEditingMode(EditTypeEnum.None);
|
||||
}
|
||||
|
||||
private void SetEditingMode(EditTypeEnum @enum)
|
||||
{
|
||||
// Hide all
|
||||
_itemField.style.display = DisplayStyle.None;
|
||||
_groupReferenceField.style.display = DisplayStyle.None;
|
||||
_groupInline.style.display = DisplayStyle.None;
|
||||
|
||||
switch (@enum)
|
||||
{
|
||||
// 1. Starting point when row is newly created with no values
|
||||
// 2. When row is resetting - clean values
|
||||
case EditTypeEnum.None:
|
||||
_itemField.value = null;
|
||||
_groupReferenceField.value = null;
|
||||
_propertyInlineGroup.managedReferenceValue = null;
|
||||
_rightElement.EnableInClassList("single-line", false);
|
||||
ShowDropHere(true);
|
||||
break;
|
||||
// Editing selected - item
|
||||
case EditTypeEnum.Item:
|
||||
_itemField.style.display = DisplayStyle.Flex;
|
||||
_rightElement.EnableInClassList("single-line", true);
|
||||
ShowDropHere(false);
|
||||
break;
|
||||
// Editing selected - recursion by reference (ScriptableObject)
|
||||
case EditTypeEnum.ReferenceGroup:
|
||||
_groupReferenceField.style.display = DisplayStyle.Flex;
|
||||
_rightElement.EnableInClassList("single-line", true);
|
||||
ShowDropHere(false);
|
||||
break;
|
||||
// Editing selected - recursion by inline definition
|
||||
case EditTypeEnum.InlineGroup:
|
||||
_groupInline.style.display = DisplayStyle.Flex;
|
||||
_propertyInlineGroup.managedReferenceValue ??= new DropTableGroup();
|
||||
_property.serializedObject.ApplyModifiedProperties();
|
||||
_groupInline.BindProperty(_propertyInlineGroup);
|
||||
_rightElement.EnableInClassList("single-line", false);
|
||||
ShowDropHere(false);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
}
|
||||
|
||||
internal void RefreshGroup()
|
||||
{
|
||||
GetFirstAncestorOfType<DropTableGroupElement>().Refresh();
|
||||
}
|
||||
|
||||
internal void Refresh(ChanceTypeEnum chanceType, float weightSum)
|
||||
{
|
||||
switch (chanceType)
|
||||
{
|
||||
case ChanceTypeEnum.All:
|
||||
_chanceField.style.display = DisplayStyle.None;
|
||||
_percentBackground.style.display = DisplayStyle.None;
|
||||
break;
|
||||
case ChanceTypeEnum.ByWeight:
|
||||
_chanceField.style.display = DisplayStyle.Flex;
|
||||
_chanceField.label = "Weight:";
|
||||
_percentBackground.style.display = DisplayStyle.Flex;
|
||||
SetPercent(_chanceField.value/weightSum*100);
|
||||
break;
|
||||
case ChanceTypeEnum.ByPercent:
|
||||
_chanceField.style.display = DisplayStyle.Flex;
|
||||
_chanceField.label = "Percent:";
|
||||
_percentBackground.style.display = DisplayStyle.Flex;
|
||||
SetPercent(_chanceField.value);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException(nameof(chanceType), chanceType, null);
|
||||
}
|
||||
}
|
||||
|
||||
private void SetPercent(float percent)
|
||||
{
|
||||
_percentBackground.style.backgroundColor = Color.Lerp(Color.red, Color.green, percent / 100);
|
||||
_percentLabel.text = $"{percent:F}%";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8972582db2c34fdbb953598e5ab5f008
|
||||
timeCreated: 1760896354
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace RPGCoreCommon.DropTable.Editor
|
||||
{
|
||||
internal enum EditTypeEnum
|
||||
{
|
||||
None,
|
||||
Item,
|
||||
ReferenceGroup,
|
||||
InlineGroup
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 15cc33125741460a8632dc8ddecd8f4a
|
||||
timeCreated: 1760810491
|
||||
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"name": "RPGCoreCommon.DropTable.Editor",
|
||||
"rootNamespace": "RPGCoreCommon.DropTable.Editor",
|
||||
"references": [
|
||||
"RPGCoreCommon.DropTable",
|
||||
"RPGCoreCommon.Helpers"
|
||||
],
|
||||
"includePlatforms": [
|
||||
"Editor"
|
||||
],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": false,
|
||||
"precompiledReferences": [],
|
||||
"autoReferenced": true,
|
||||
"defineConstraints": [],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cbe301a2824a4590a84eaeeb1d960654
|
||||
timeCreated: 1760521419
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b134d2bbe2c24303b5dc46a0bce52ec1
|
||||
timeCreated: 1760521419
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace RPGCoreCommon.DropTable
|
||||
{
|
||||
public enum ChanceTypeEnum
|
||||
{
|
||||
All,
|
||||
ByWeight,
|
||||
ByPercent
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7c0aa7d4711440a697970eb700168030
|
||||
timeCreated: 1760523882
|
||||
@@ -0,0 +1,9 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace RPGCoreCommon.DropTable
|
||||
{
|
||||
public class DropTableComponent : MonoBehaviour
|
||||
{
|
||||
[SerializeReference] private DropTableSO dropTableGroup;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: dc0580faa40e4f62bc65a675c74c4c96
|
||||
timeCreated: 1760523736
|
||||
@@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using RPGCoreCommon.Helpers;
|
||||
using UnityEngine;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace RPGCoreCommon.DropTable
|
||||
{
|
||||
[Serializable]
|
||||
internal class DropTableGroup
|
||||
{
|
||||
// Rules for randomizing
|
||||
[SerializeField] internal ChanceTypeEnum chanceType = ChanceTypeEnum.ByPercent;
|
||||
|
||||
// SubGroups and/or items of this group
|
||||
[SerializeField] internal List<DropTableGroupRow> rows = new();
|
||||
|
||||
internal void Get(ref Dictionary<Object, int> result)
|
||||
{
|
||||
if (!rows.Any())
|
||||
{
|
||||
Debug.LogWarning($"Empty <b>{nameof(DropTableGroup)}</b>!");
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var row in GetRows())
|
||||
{
|
||||
if (row.item)
|
||||
{
|
||||
result.TryAdd(row.item, 0);
|
||||
result[row.item] += row.count;
|
||||
}
|
||||
else if (row.subGroupRef)
|
||||
{
|
||||
for (var i = 0; i < row.count; i++)
|
||||
row.subGroupRef.group.Get(ref result);
|
||||
}
|
||||
else if (row.subGroupInline != null)
|
||||
{
|
||||
for (var i = 0; i < row.count; i++)
|
||||
row.subGroupInline.Get(ref result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal List<DropTableGroupRow> GetRows()
|
||||
{
|
||||
return chanceType switch
|
||||
{
|
||||
ChanceTypeEnum.All => rows,
|
||||
ChanceTypeEnum.ByWeight => RandomHelper.RandomElementsByWeight(rows, row => row.chance, 1),
|
||||
ChanceTypeEnum.ByPercent => rows.Where(row => RandomHelper.Chance(row.chance/100)).ToList(),
|
||||
_ => throw new ArgumentOutOfRangeException()
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e24e6877320d4170b09ca966a136951a
|
||||
timeCreated: 1760709210
|
||||
@@ -0,0 +1,14 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace RPGCoreCommon.DropTable
|
||||
{
|
||||
[System.Serializable]
|
||||
public class DropTableGroupRow
|
||||
{
|
||||
[SerializeField] internal float chance;
|
||||
[SerializeField] internal int count = 1;
|
||||
[SerializeReference] internal DropTableGroup subGroupInline;
|
||||
[SerializeReference] internal DropTableSO subGroupRef;
|
||||
[SerializeReference] internal Object item;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3597a5bce3db4068885d19da0a9e4a2a
|
||||
timeCreated: 1760694296
|
||||
@@ -0,0 +1,19 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace RPGCoreCommon.DropTable
|
||||
{
|
||||
[System.Serializable]
|
||||
[CreateAssetMenu(menuName = "RPG Core/Drop Table")]
|
||||
public class DropTableSO : ScriptableObject
|
||||
{
|
||||
[SerializeField] internal DropTableGroup group;
|
||||
|
||||
public Dictionary<Object, int> Get()
|
||||
{
|
||||
var result = new Dictionary<Object, int>();
|
||||
group.Get(ref result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c1984d6153504b90a4dd21a18d5f034a
|
||||
timeCreated: 1760522473
|
||||
@@ -0,0 +1,8 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
[assembly: InternalsVisibleTo("RPGCoreCommon.DropTable.Editor")]
|
||||
|
||||
namespace RPGCoreCommon.DropTable
|
||||
{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2efd7efdd7b349f1a73c5a2a4997e9d3
|
||||
timeCreated: 1760693427
|
||||
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"name": "RPGCoreCommon.DropTable",
|
||||
"rootNamespace": "RPGCoreCommon.DropTable",
|
||||
"references": [
|
||||
"RPGCoreCommon.Helpers"
|
||||
],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": false,
|
||||
"precompiledReferences": [],
|
||||
"autoReferenced": true,
|
||||
"defineConstraints": [],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8788c3c029994136b7e8bbbff4b283f5
|
||||
timeCreated: 1760521419
|
||||
Reference in New Issue
Block a user