Skip to content
This repository was archived by the owner on May 9, 2025. It is now read-only.

Commit 0e28a76

Browse files
Clamped variables can now be assigned to their counter-variable type fields
1 parent b5bcd46 commit 0e28a76

30 files changed

+385
-142
lines changed

Assets/SO Architecture/Editor/Code Generation/SO_CodeGenerationWindow.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ private void ClampedVariableHelpBox()
124124
{
125125
if(anim.visible)
126126
EditorGUILayout.HelpBox(
127-
"Clamped values are abstract and require implementation.\nAn error will be raised in your console upon creation",
127+
"Clamped variables comes with a generic clamp function, but it might need adjustments based on your type",
128128
MessageType.Warning);
129129
}
130130
}

Assets/SO Architecture/Editor/Code Generation/Templates/ClampedVariableTemplate

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,37 @@ using UnityEngine;
44
fileName = "$TYPE_NAME$ClampedVariable.asset",
55
menuName = SOArchitecture_Utility.VARIABLE_CLAMPED_SUBMENU + "$MENU_NAME$",
66
order = $ORDER$)]
7-
public class $TYPE_NAME$ClampedVariable : ClampedVariable<$TYPE$, $TYPE_NAME$Variable, $TYPE_NAME$Reference>
7+
public class $TYPE_NAME$ClampedVariable : $TYPE_NAME$Variable, IClampedVariable<$TYPE$, $TYPE_NAME$Variable, $TYPE_NAME$Reference>
88
{
9+
public $TYPE_NAME$Reference MinValue { get { return _minClampedValue; } }
10+
public $TYPE_NAME$Reference MaxValue { get { return _maxClampedValue; } }
11+
12+
[SerializeField]
13+
private $TYPE_NAME$Reference _minClampedValue;
14+
[SerializeField]
15+
private $TYPE_NAME$Reference _maxClampedValue;
16+
17+
public virtual $TYPE$ ClampValue($TYPE$ value)
18+
{
19+
if (value.CompareTo(MinValue.Value) < 0)
20+
{
21+
return MinValue.Value;
22+
}
23+
else if (value.CompareTo(MaxValue.Value) > 0)
24+
{
25+
return MaxValue.Value;
26+
}
27+
else
28+
{
29+
return value;
30+
}
31+
}
32+
public override $TYPE$ SetValue(BaseVariable<$TYPE$> value)
33+
{
34+
return ClampValue(value.Value);
35+
}
36+
public override $TYPE$ SetValue($TYPE$ value)
37+
{
38+
return ClampValue(value);
39+
}
940
}

Assets/SO Architecture/Editor/Code Generation/Templates/VariableTemplate

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
fileName = "$TYPE_NAME$Variable.asset",
55
menuName = SOArchitecture_Utility.VARIABLE_SUBMENU + "$MENU_NAME$",
66
order = $ORDER$)]
7-
public sealed class $TYPE_NAME$Variable : BaseVariable<$TYPE$>
7+
public class $TYPE_NAME$Variable : BaseVariable<$TYPE$>
88
{
99
}

Assets/SO Architecture/Editor/Inspectors/BaseVariableEditor.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@
99
public class BaseVariableEditor : Editor
1010
{
1111
private BaseVariable Target { get { return (BaseVariable)target; } }
12+
protected bool IsClamped { get { return Target is IClampedVariable; } }
1213

1314
private SerializedProperty _valueProperty;
1415
private SerializedProperty _developerDescription;
1516
private SerializedProperty _readOnly;
1617
private SerializedProperty _raiseWarning;
18+
private SerializedProperty _minValueProperty;
19+
private SerializedProperty _maxValueProperty;
1720
private AnimBool _raiseWarningAnimation;
1821

1922
private const string READONLY_TOOLTIP = "Should this value be changable during runtime? Will still be editable in the inspector regardless";
@@ -25,6 +28,12 @@ protected virtual void OnEnable()
2528
_readOnly = serializedObject.FindProperty("_readOnly");
2629
_raiseWarning = serializedObject.FindProperty("_raiseWarning");
2730

31+
if (IsClamped)
32+
{
33+
_minValueProperty = serializedObject.FindProperty("_minClampedValue");
34+
_maxValueProperty = serializedObject.FindProperty("_maxClampedValue");
35+
}
36+
2837
_raiseWarningAnimation = new AnimBool(_readOnly.boolValue);
2938
_raiseWarningAnimation.valueChanged.AddListener(Repaint);
3039
}
@@ -33,6 +42,7 @@ public override void OnInspectorGUI()
3342
serializedObject.Update();
3443

3544
DrawValue();
45+
DrawClampedFields();
3646
DrawReadonlyField();
3747
DrawDeveloperDescription();
3848
}
@@ -63,8 +73,22 @@ protected void DrawValue()
6373
EditorGUILayout.LabelField(new GUIContent(labelContent, labelContent));
6474
}
6575
}
76+
protected void DrawClampedFields()
77+
{
78+
if (!IsClamped)
79+
return;
80+
81+
using (new EditorGUI.IndentLevelScope())
82+
{
83+
EditorGUILayout.PropertyField(_minValueProperty);
84+
EditorGUILayout.PropertyField(_maxValueProperty);
85+
}
86+
}
6687
protected void DrawReadonlyField()
6788
{
89+
if (IsClamped)
90+
return;
91+
6892
EditorGUILayout.PropertyField(_readOnly, new GUIContent("Read Only", READONLY_TOOLTIP));
6993

7094
_raiseWarningAnimation.target = _readOnly.boolValue;

Assets/SO Architecture/Editor/Inspectors/ClampedVariableEditor.cs

Lines changed: 0 additions & 38 deletions
This file was deleted.

Assets/SO Architecture/Resources/SOArchitecture_Settings.asset

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ MonoBehaviour:
1212
m_Name: SOArchitecture_Settings
1313
m_EditorClassIdentifier:
1414
_drawEventGizmos: 1
15-
_codeGenerationTargetDirectory: CODE_GENERATION
15+
_codeGenerationTargetDirectory: SO Architecture
1616
_codeGenerationAllowOverwrite: 0
1717
_defualtCreateAssetMenuOrder: 120

Assets/SO Architecture/Variables/ByteVariable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
fileName = "ByteVariable.asset",
55
menuName = SOArchitecture_Utility.ADVANCED_VARIABLE_SUBMENU + "byte",
66
order = SOArchitecture_Utility.ASSET_MENU_ORDER_COLLECTIONS + 6)]
7-
public sealed class ByteVariable : BaseVariable<byte>
7+
public class ByteVariable : BaseVariable<byte>
88
{
99
}

Assets/SO Architecture/Variables/Clamped/ByteClampedVariable.cs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,38 @@
33
[CreateAssetMenu(
44
fileName = "ByteClampedVariable.asset",
55
menuName = SOArchitecture_Utility.VARIABLE_CLAMPED_SUBMENU + "byte",
6-
order = SOArchitecture_Utility.ASSET_MENU_ORDER_CLAMPED_VARIABLES + 2)]
7-
public class ByteClampedVariable : ClampedVariable<byte, ByteVariable, ByteReference>
6+
order = 120)]
7+
public class ByteClampedVariable : ByteVariable, IClampedVariable<byte, ByteVariable, ByteReference>
88
{
9-
protected override byte ClampValue(byte value)
9+
public ByteReference MinValue { get { return _minClampedValue; } }
10+
public ByteReference MaxValue { get { return _maxClampedValue; } }
11+
12+
[SerializeField]
13+
private ByteReference _minClampedValue;
14+
[SerializeField]
15+
private ByteReference _maxClampedValue;
16+
17+
public virtual byte ClampValue(byte value)
18+
{
19+
if (value.CompareTo(MinValue.Value) < 0)
20+
{
21+
return MinValue.Value;
22+
}
23+
else if (value.CompareTo(MaxValue.Value) > 0)
24+
{
25+
return MaxValue.Value;
26+
}
27+
else
28+
{
29+
return value;
30+
}
31+
}
32+
public override byte SetValue(BaseVariable<byte> value)
33+
{
34+
return ClampValue(value.Value);
35+
}
36+
public override byte SetValue(byte value)
1037
{
11-
return (byte)Mathf.Clamp(value, MinValue.Value, MaxValue.Value);
38+
return ClampValue(value);
1239
}
1340
}

Assets/SO Architecture/Variables/Clamped/ClampedVariable.cs

Lines changed: 0 additions & 27 deletions
This file was deleted.

Assets/SO Architecture/Variables/Clamped/ClampedVariable.cs.meta

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)