Skip to content

Instantly share code, notes, and snippets.

@J4stMart
Last active February 1, 2024 00:15
Show Gist options
  • Save J4stMart/1a1b5f00eef8e5945afdd3e3a9db4a6a to your computer and use it in GitHub Desktop.
Save J4stMart/1a1b5f00eef8e5945afdd3e3a9db4a6a to your computer and use it in GitHub Desktop.
Code snippets of the UI initialization made for the BIM Analyzer Project
/// <summary>
/// Monobehaviour attached to the hierachy view
/// </summary>
public class HierachyView : MonoBehaviour
{
//NOTE: Member variables, Constructor and other Functions have been omitted from this code snippet
/// <summary>
/// Loads the hierachy view from the root ifc element
/// </summary>
/// <param name="rootElement"></param>
public void LoadHierachyView(IfcElement rootElement)
{
var gameObject = Instantiate(hierachyPrefab, hierachyContent.transform);
root = gameObject.GetComponent<HierachyItem>();
root.SetupItem(this, rootElement, SiblingOrder.FIRST_CHILD, true, true);
root.SetIfcType(rootElement.ifcType);
root.SetName(rootElement.name);
root.AddChildren(0, 0);
//Select the root item after initializing
selectedItem = root;
SelectManager.Instance.Select(rootElement, root);
}
}
/// <summary>
/// Monobehaviour for a hierachy item in the hierachy view
/// </summary>
public class HierachyItem : MonoBehaviour, IPointerClickHandler
{
//NOTE: Member variables, Constructor and other Functions have been omitted from this code snippet
/// <summary>
/// Add the childern to this hierachy item
/// </summary>
/// <param name="depth"></param> Amount of steps we're in the hierachy tree
/// <param name="noGuide"></param> Counter keeping track of the amount of guides that don't need a sprite (because we're in the last child of the depth shown by the counter)
public void AddChildren(int depth, int noGuide)
{
//Sets up all the guide sprites
for (int i = 0; i < depth; i++)
{
var gameObject = Instantiate(hierachyView.guidePrefab, nameObject);
var image = gameObject.GetComponentInChildren<Image>();
gameObject.transform.SetSiblingIndex(i);
//Last guide is special, as we need it needs a L guide, rest of the guides are Vertical lines
if (i == depth - 1)
{
lastGuide = image;
lastGuide.sprite = hierachyView.GuideSprite(siblingOrder);
}
else
{
//If we don't need the guide to show, make it invisible
if (i < noGuide)
image.color = Color.clear;
image.sprite = hierachyView.Guide;
}
}
//While initializing we only expand the 4 first elements
bool expand = ++depth < 3;
//If this is the last child in the sibling order, we can increase the noguide by 1
if (siblingOrder == SiblingOrder.LAST_CHILD)
noGuide++;
//Load childern hierachy items
for (int i = 0; i < element.childern.Count; i++)
{
SiblingOrder siblingOrder = i == element.childern.Count - 1 ? SiblingOrder.LAST_CHILD : SiblingOrder.MIDDLE_CHILD;
IfcElement child = element.childern[i];
var gameObject = Instantiate(hierachyView.hierachyPrefab, hierachyView.hierachyContent.transform);
HierachyItem item = gameObject.GetComponent<HierachyItem>();
item.SetupItem(hierachyView, child, siblingOrder, child.childern.Count != 0, expand);
item.SetIfcType(child.ifcType);
item.SetName(child.name);
//Load elements recusively
item.AddChildren(depth, noGuide);
gameObject.SetActive(isExpanded);
childern.Add(item);
activeChildern.Add(item);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment