Skip to content

Commit

Permalink
Merge pull request #93 from arimger/develop
Browse files Browse the repository at this point in the history
Develop - 0.12.7
  • Loading branch information
arimger authored Dec 10, 2023
2 parents c86a67f + 193cfac commit 3d4e8ab
Show file tree
Hide file tree
Showing 16 changed files with 322 additions and 38 deletions.
9 changes: 9 additions & 0 deletions Assets/Editor Toolbox/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## 0.12.7 [10.12.2023]

### Changed:
- Possibility to interact with ProgressBarDrawer (added IsInteractable property to the ProgressBarAttribute)
- MinMaxAttribute now supports Vector2Int

### Added:
- 'Revert Prefab Name' option for prefabs in the GameObject/Prefabs context menu

## 0.12.6 [19.10.2023]

### Changed:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,49 @@ public class MinMaxSliderAttributeDrawer : PropertyDrawerBase
{
protected override float GetPropertyHeightSafe(SerializedProperty property, GUIContent label)
{
return base.GetPropertyHeightSafe(property, label);
return EditorGUIUtility.singleLineHeight;
}

protected override void OnGUISafe(Rect position, SerializedProperty property, GUIContent label)
{
var minValue = Attribute.MinValue;
var maxValue = Attribute.MaxValue;
var xValue = property.vector2Value.x;
var yValue = property.vector2Value.y;

var xValue = 0.0f;
var yValue = 0.0f;
switch (property.propertyType)
{
case SerializedPropertyType.Vector2:
xValue = property.vector2Value.x;
yValue = property.vector2Value.y;
break;
case SerializedPropertyType.Vector2Int:
xValue = property.vector2IntValue.x;
yValue = property.vector2IntValue.y;
break;
}

label = EditorGUI.BeginProperty(position, label, property);
EditorGUI.BeginChangeCheck();
position = EditorGUI.PrefixLabel(position, label);
using (new ZeroIndentScope())
{
ToolboxEditorGui.DrawMinMaxSlider(position, label, ref xValue, ref yValue, minValue, maxValue);
ToolboxEditorGui.DrawMinMaxSlider(position, ref xValue, ref yValue, minValue, maxValue);
}

if (EditorGUI.EndChangeCheck())
{
property.vector2Value = new Vector2(xValue, yValue);
switch (property.propertyType)
{
case SerializedPropertyType.Vector2:
property.vector2Value = new Vector2(xValue, yValue);
break;
case SerializedPropertyType.Vector2Int:
var intXValue = Mathf.RoundToInt(xValue);
var intYValue = Mathf.RoundToInt(yValue);
property.vector2IntValue = new Vector2Int(intXValue, intYValue);
break;
}
}

EditorGUI.EndProperty();
Expand All @@ -38,7 +61,8 @@ protected override void OnGUISafe(Rect position, SerializedProperty property, GU

public override bool IsPropertyValid(SerializedProperty property)
{
return property.propertyType == SerializedPropertyType.Vector2;
return property.propertyType == SerializedPropertyType.Vector2 ||
property.propertyType == SerializedPropertyType.Vector2Int;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,61 @@ namespace Toolbox.Editor.Drawers
[CustomPropertyDrawer(typeof(ProgressBarAttribute))]
public class ProgressBarAttributeDrawer : PropertyDrawerBase
{
private static readonly int drawerHash = nameof(ProgressBarAttributeDrawer).GetHashCode();

private void HandleGuiEvents(SerializedProperty property, Rect progressBarRect)
{
var mousePosition = Event.current.mousePosition;
var id = GUIUtility.GetControlID(drawerHash, FocusType.Passive, progressBarRect);
switch (Event.current.GetTypeForControl(id))
{
case EventType.MouseDown:
if (progressBarRect.Contains(mousePosition))
{
GUIUtility.hotControl = id;
SetProgressValue(property, progressBarRect, mousePosition.x);
Event.current.Use();
}
break;
case EventType.MouseDrag:
if (GUIUtility.hotControl == id)
{
SetProgressValue(property, progressBarRect, mousePosition.x);
Event.current.Use();
}
break;
case EventType.MouseUp:
if (GUIUtility.hotControl == id)
{
GUIUtility.hotControl = 0;
Event.current.Use();
}
break;
}
}

private void SetProgressValue(SerializedProperty property, Rect progressBarRect, float xPosition)
{
var minValue = Attribute.MinValue;
var maxValue = Attribute.MaxValue;

var range = progressBarRect.xMax - progressBarRect.xMin;
xPosition = Mathf.Clamp(xPosition - progressBarRect.xMin, 0, range);

var fill = Mathf.Clamp01(xPosition / range);
var newValue = (maxValue - minValue) * fill + minValue;

switch (property.propertyType)
{
case SerializedPropertyType.Integer:
property.intValue = Mathf.RoundToInt(newValue);
break;
case SerializedPropertyType.Float:
property.floatValue = newValue;
break;
}
}

protected override float GetPropertyHeightSafe(SerializedProperty property, GUIContent label)
{
return Style.barHeight;
Expand Down Expand Up @@ -51,19 +106,23 @@ protected override void OnGUISafe(Rect position, SerializedProperty property, GU
position.y -= Style.textOffset;
//finally draw the progress bar label
EditorGUI.DropShadowLabel(position, labelText);
}

if (!attribute.IsInteractable)
{
return;
}

HandleGuiEvents(property, position);
}

public override bool IsPropertyValid(SerializedProperty property)
{
return property.propertyType == SerializedPropertyType.Float ||
property.propertyType == SerializedPropertyType.Integer;
}


private ProgressBarAttribute Attribute => attribute as ProgressBarAttribute;


private static class Style
{
internal static readonly float rowHeight = EditorGUIUtility.singleLineHeight;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,49 @@
using UnityEditor;
using UnityEditor;
using UnityEngine;

namespace Toolbox.Editor.Drawers
{
using Toolbox.Editor.Internal;

public class DynamicMinMaxSliderAttributeDrawer : DynamicMinMaxBaseDrawer<DynamicMinMaxSliderAttribute>
{
protected override void OnGuiSafe(SerializedProperty property, GUIContent label, float minValue, float maxValue)
{
var xValue = property.vector2Value.x;
var yValue = property.vector2Value.y;
var xValue = 0.0f;
var yValue = 0.0f;
switch (property.propertyType)
{
case SerializedPropertyType.Vector2:
xValue = property.vector2Value.x;
yValue = property.vector2Value.y;
break;
case SerializedPropertyType.Vector2Int:
xValue = property.vector2IntValue.x;
yValue = property.vector2IntValue.y;
break;
}

ToolboxEditorGui.BeginProperty(property, ref label, out var position);
position = EditorGUI.PrefixLabel(position, label);
EditorGUI.BeginChangeCheck();
ToolboxEditorGui.DrawMinMaxSlider(position, label, ref xValue, ref yValue, minValue, maxValue);
using (new ZeroIndentScope())
{
ToolboxEditorGui.DrawMinMaxSlider(position, ref xValue, ref yValue, minValue, maxValue);
}

if (EditorGUI.EndChangeCheck())
{
property.vector2Value = new Vector2(xValue, yValue);
switch (property.propertyType)
{
case SerializedPropertyType.Vector2:
property.vector2Value = new Vector2(xValue, yValue);
break;
case SerializedPropertyType.Vector2Int:
var intXValue = Mathf.RoundToInt(xValue);
var intYValue = Mathf.RoundToInt(yValue);
property.vector2IntValue = new Vector2Int(intXValue, intYValue);
break;
}
}

ToolboxEditorGui.CloseProperty();
Expand All @@ -23,7 +52,8 @@ protected override void OnGuiSafe(SerializedProperty property, GUIContent label,

public override bool IsPropertyValid(SerializedProperty property)
{
return property.propertyType == SerializedPropertyType.Vector2;
return property.propertyType == SerializedPropertyType.Vector2 ||
property.propertyType == SerializedPropertyType.Vector2Int;
}
}
}
22 changes: 13 additions & 9 deletions Assets/Editor Toolbox/Editor/ToolboxEditorGui.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,21 +199,14 @@ public static void DrawTexture(Rect rect, Texture texture, ScaleMode scaleMode,
GUI.DrawTexture(rect, texture, scaleMode, alphaBlend);
}

public static void DrawMinMaxSlider(Rect rect, string label, ref float xValue, ref float yValue, float minValue, float maxValue)
public static void DrawMinMaxSlider(Rect rect, ref float xValue, ref float yValue, float minValue, float maxValue)
{
DrawMinMaxSlider(rect, new GUIContent(label), ref xValue, ref yValue, minValue, maxValue);
}

public static void DrawMinMaxSlider(Rect rect, GUIContent label, ref float xValue, ref float yValue, float minValue, float maxValue)
{
rect = EditorGUI.PrefixLabel(rect, label);

var fieldWidth = EditorGUIUtility.fieldWidth;
var minFieldRect = new Rect(rect.xMin, rect.y, fieldWidth, rect.height);
var maxFieldRect = new Rect(rect.xMax - fieldWidth, rect.y, fieldWidth, rect.height);

//set slider rect between min and max fields + additional padding
var spacing = 8.0f;
const float spacing = 8.0f;
var sliderRect = Rect.MinMaxRect(minFieldRect.xMax + spacing,
rect.yMin,
maxFieldRect.xMin - spacing,
Expand All @@ -229,6 +222,17 @@ public static void DrawMinMaxSlider(Rect rect, GUIContent label, ref float xValu
yValue = Mathf.Clamp(yValue, Mathf.Max(minValue, xValue), maxValue);
}

public static void DrawMinMaxSlider(Rect rect, string label, ref float xValue, ref float yValue, float minValue, float maxValue)
{
DrawMinMaxSlider(rect, new GUIContent(label), ref xValue, ref yValue, minValue, maxValue);
}

public static void DrawMinMaxSlider(Rect rect, GUIContent label, ref float xValue, ref float yValue, float minValue, float maxValue)
{
rect = EditorGUI.PrefixLabel(rect, label);
DrawMinMaxSlider(rect, ref xValue, ref yValue, minValue, maxValue);
}

public static void BoldLabel(Rect rect, string label)
{
BoldLabel(rect, new GUIContent(label));
Expand Down
6 changes: 3 additions & 3 deletions Assets/Editor Toolbox/Editor/ToolboxEditorSceneView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@ private static List<GameObject> GetObjectsUnderCursor()
private static void UpdateEventCallback()
{
#if UNITY_2019_1_OR_NEWER
UnityEditor.SceneView.duringSceneGui -= SceneViewDuringSceneGUI;
UnityEditor.SceneView.duringSceneGui -= SceneViewDuringSceneGui;

if (UseToolboxSceneView)
{
UnityEditor.SceneView.duringSceneGui += SceneViewDuringSceneGUI;
UnityEditor.SceneView.duringSceneGui += SceneViewDuringSceneGui;
}
#endif
}

private static void SceneViewDuringSceneGUI(UnityEditor.SceneView sceneView)
private static void SceneViewDuringSceneGui(UnityEditor.SceneView sceneView)
{
if (Event.current.type != EventType.KeyDown ||
Event.current.keyCode != SelectorKey)
Expand Down
41 changes: 41 additions & 0 deletions Assets/Editor Toolbox/Editor/Utilities/PrefabUtility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using UnityEditor;

namespace Toolbox.Editor.Utilities
{
public static class PrefabUtility
{
[MenuItem("GameObject/Prefab/Revert Prefab Name", true, -100)]
public static bool ValidateRevertPrefabName()
{
var gameObjects = Selection.gameObjects;
for (int i = 0; i < gameObjects.Length; i++)
{
var gameObject = gameObjects[i];
if (UnityEditor.PrefabUtility.IsAnyPrefabInstanceRoot(gameObject))
{
return true;
}
}

return false;
}

[MenuItem("GameObject/Prefab/Revert Prefab Name", false, -100)]
public static void RevertPrefabName()
{
var gameObjects = Selection.gameObjects;
Undo.RecordObjects(gameObjects, "Revert Prefab Name");
for (int i = 0; i < gameObjects.Length; i++)
{
var gameObject = gameObjects[i];
var prefabObject = UnityEditor.PrefabUtility.GetCorrespondingObjectFromSource(gameObject);
if (prefabObject == null)
{
continue;
}

gameObject.name = prefabObject.name;
}
}
}
}
11 changes: 11 additions & 0 deletions Assets/Editor Toolbox/Editor/Utilities/PrefabUtility.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3d4e8ab

Please sign in to comment.