Skip to content

Commit

Permalink
Utils to control the layout of docks
Browse files Browse the repository at this point in the history
By default the windows are added with an average layout. This can be
changed to specified proportions provided you have insight on how the
layout tree is structured.
  • Loading branch information
Reavenk committed Jan 16, 2021
1 parent d4cc55c commit 813a197
Showing 1 changed file with 102 additions and 1 deletion.
103 changes: 102 additions & 1 deletion Assets/UIDock/Root.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1118,8 +1118,11 @@ private void UpdateWindowsManagement(bool moveSashes, bool regenSashes)

this.dirtySashPosition = false;
}
}


public void ForceLayout()
{
this.UpdateWindowsManagement(true, true);
}

/// <summary>
Expand Down Expand Up @@ -1888,5 +1891,103 @@ protected override void OnRectTransformDimensionsChange()
this.SetDirtySashPos();
}

protected Dock GetDockAtAddress(bool lenient, params int [] ri)
{
if(ri == null || ri.Length == 0)
return this.root;

Dock dit = this.root;
for(int i = 0; i < ri.Length; ++i)
{
int idx = ri[i];

if(
dit.IsContainerType() == false ||
dit.children == null ||
idx < 0 ||
idx >= dit.children.Count)
{
return lenient ? dit : null;
}

dit = dit.children[idx];
}
return dit;
}

protected bool ResizeDock(Dock d, params float [] rf)
{
if( d == null ||
(d.dockType != Dock.Type.Horizontal && d.dockType != Dock.Type.Vertical))
{
return false;
}

float avg = 0.0f;
foreach(float f in rf)
avg += f;

if(rf.Length > 0)
avg /= rf.Length;

if(avg == 0.0f)
avg = 1.0f;

// These values are treated as proportions, they'll be converted to actual pixel sizes on the layout.
float totalProp = d.children.Count * avg;

if (d.dockType == Dock.Type.Horizontal)
{
float totalW = 0.0f;
for (int i = 0; i < d.children.Count; ++i)
totalW += d.children[i].cachedPlace.width;

float fx = d.cachedPlace.x;
for (int i = 0; i < d.children.Count; ++i)
{
float nw = (i < rf.Length) ? rf[i] : avg;
float prop = nw / totalProp * totalW;
d.children[i].cachedPlace.x = fx;
d.children[i].cachedPlace.width = nw;

fx += nw;
fx += this.props.sashWidth;
UpdateDockedLayoutHeirarchy(new LayoutEntry(d.children[i], d.children[i].cachedPlace));
}
}
else
{
float totalH = 0.0f;
for (int i = 0; i < d.children.Count; ++i)
totalH += d.children[i].cachedPlace.height;

float fy = d.cachedPlace.y;
for (int i = 0; i < d.children.Count; ++i)
{
float nh = (i < rf.Length) ? rf[i] : avg;
float prop = nh / totalProp * totalH;
d.children[i].cachedPlace.y = fy;
d.children[i].cachedPlace.height = nh;

fy += nh;
fy += this.props.sashWidth;
UpdateDockedLayoutHeirarchy(new LayoutEntry(d.children[i], d.children[i].cachedPlace));
}
}

this.dirtySashPosition = true;
this.SetDirty();
return true;
}

public bool ResizeAddressWithProportions(int [] addr, float [] proportions)
{
Dock d = GetDockAtAddress(false, addr);
if(d == null)
return false;

return ResizeDock(d, proportions);
}

}
}

0 comments on commit 813a197

Please sign in to comment.