Skip to content

Commit

Permalink
Merge pull request #26 from techno-dwarf-works/feature/refactoring
Browse files Browse the repository at this point in the history
Version 0.0.23
  • Loading branch information
uurha authored Jul 21, 2024
2 parents f407e74 + 2614c76 commit 794ff27
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Better.Commons.EditorAddons.Enums;
using Better.Commons.EditorAddons.Utility;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;

namespace Better.Commons.EditorAddons.Extensions
Expand Down Expand Up @@ -37,5 +38,12 @@ public static Image AddIcon(this VisualElement self, IconType iconType)
self.Insert(0, image);
return image;
}

public static Image AddIcon(this VisualElement self, Texture texture)
{
var image = VisualElementUtility.CreateLabelIcon(texture);
self.Insert(0, image);
return image;
}
}
}
122 changes: 122 additions & 0 deletions Assets/BetterCommons/Editor/Utility/SelectionUtility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Better.Commons.Runtime.Extensions;
using Better.Commons.Runtime.Utility;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace Better.Commons.EditorAddons.Utility
{
public static class SelectionUtility
{
//TODO: Test with GameObject
public static void OpenReference(UnityEngine.Object reference)
{
if (reference.IsNullOrDestroyed())
{
DebugUtility.LogException<ArgumentNullException>(nameof(reference));
return;
}

if (reference is Component component)
{
var transform = component.transform;
if (PrefabUtility.IsPartOfPrefabAsset(reference))
{
var assetPath = AssetDatabase.GetAssetPath(reference);
var stage = PrefabStageUtility.OpenPrefab(assetPath);
var prefabRootTransform = stage.prefabContentsRoot.transform;
var indexes = GetParentIndices(transform);

transform = GetChildBySiblingIndices(prefabRootTransform, indexes);
}
else
{
var countLoaded = SceneManager.sceneCount;
var loadedScenes = new Scene[countLoaded];

for (var i = 0; i < countLoaded; i++)
{
loadedScenes[i] = SceneManager.GetSceneAt(i);
}

var gameObject = component.gameObject;
if (loadedScenes.Contains(gameObject.scene))
{
EditorSceneManager.SetActiveScene(gameObject.scene);
}
else
{
EditorSceneManager.OpenScene(gameObject.scene.path);
}
}

Selection.activeTransform = transform;

//TODO: check if working
if (SceneView.lastActiveSceneView != null)
{
SceneView.lastActiveSceneView.MoveToView(transform);
}

EditorGUIUtility.PingObject(transform);
}
else
{
Selection.SetActiveObjectWithContext(reference, reference);
EditorGUIUtility.PingObject(reference);
}
}

private static List<int> GetParentIndices(Transform instance)
{
if (instance.IsNullOrDestroyed())
{
DebugUtility.LogException<ArgumentNullException>(nameof(instance));
return new List<int>();
}

var siblingIndices = new List<int>();

var parent = instance.parent;
if (parent == null)
{
return siblingIndices;
}

// Add the sibling index of the initial transform
siblingIndices.Add(instance.GetSiblingIndex());
// Traverse up the hierarchy to add sibling indices of parent transforms
while (parent != null && parent != instance.root)
{
siblingIndices.Add(parent.GetSiblingIndex());
parent = parent.parent;
}

return siblingIndices;
}

private static Transform GetChildBySiblingIndices(Transform transform, IReadOnlyList<int> siblingIndices)
{
var child = transform;
for (var i = siblingIndices.Count - 1; i >= 0; i--)
{
var siblingIndex = siblingIndices[i];
if (siblingIndex >= 0 && siblingIndex < child.childCount)
{
child = child.GetChild(siblingIndex);
}
else
{
// Sibling index out of range, return null
return null;
}
}

return child;
}
}
}
3 changes: 3 additions & 0 deletions Assets/BetterCommons/Editor/Utility/SelectionUtility.cs.meta

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

12 changes: 10 additions & 2 deletions Assets/BetterCommons/Editor/Utility/VisualElementUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Better.Commons.Runtime.Utility;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;

namespace Better.Commons.EditorAddons.Utility
Expand Down Expand Up @@ -118,19 +119,26 @@ public static VisualElement CreateHorizontalGroup()
public static VisualElement CreateVerticalGroup()
{
var element = new VisualElement();
element.style.flexDirection = new StyleEnum<FlexDirection>(FlexDirection.Column);
element.style.flexDirection = FlexDirection.Column;
return element;
}

public static Image CreateLabelIcon(IconType iconType)
{
var icon = iconType.GetIcon();
return CreateLabelIcon(icon);
}

public static Image CreateLabelIcon(Texture icon)
{
var image = new Image
{
image = icon
};

image.style.Height(StyleDefinition.SingleLineHeight).Width(StyleDefinition.SingleLineHeight).AlignSelf(new StyleEnum<Align>(Align.Center));
image.style.Height(StyleDefinition.SingleLineHeight)
.Width(StyleDefinition.SingleLineHeight)
.AlignSelf(new StyleEnum<Align>(Align.Center));

return image;
}
Expand Down
5 changes: 0 additions & 5 deletions Assets/BetterCommons/Runtime/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ public static string FormatBoldItalic(this string text)
return $"<b><i>{text}</i></b>";
}

public static string BeautifyFormat(string text)
{
return $"\"<b><i>{text}</i></b>\"";
}

/// <summary>
/// Makes first char upper
/// </summary>
Expand Down
18 changes: 18 additions & 0 deletions Assets/BetterCommons/Runtime/Extensions/StyleExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,15 @@ public static IStyle MarginTop(this IStyle self, StyleLength marginTop)
self.marginTop = marginTop;
return self;
}

public static IStyle Margin(this IStyle self, StyleLength marginTop)
{
self.marginTop = marginTop;
self.marginRight = marginTop;
self.marginBottom = marginTop;
self.marginLeft = marginTop;
return self;
}

public static IStyle MaxHeight(this IStyle self, StyleLength maxHeight)
{
Expand Down Expand Up @@ -296,6 +305,15 @@ public static IStyle PaddingTop(this IStyle self, StyleLength paddingTop)
self.paddingTop = paddingTop;
return self;
}

public static IStyle Padding(this IStyle self, StyleLength marginTop)
{
self.paddingTop = marginTop;
self.paddingRight = marginTop;
self.paddingBottom = marginTop;
self.paddingLeft = marginTop;
return self;
}

public static IStyle Position(this IStyle self, StyleEnum<Position> position)
{
Expand Down
2 changes: 1 addition & 1 deletion Assets/BetterCommons/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.tdw.better.commons",
"displayName": "Better Commons",
"version": "0.0.22",
"version": "0.0.23",
"unity": "2021.3",
"description": " ",
"dependencies": {
Expand Down

0 comments on commit 794ff27

Please sign in to comment.