Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add interface for better controlling single-child containers #869

Merged
merged 9 commits into from
Jun 30, 2017
54 changes: 24 additions & 30 deletions osu.Framework.Testing/TestBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,13 @@ private void load(Storage storage)
{
RelativeSizeAxes = Axes.Both,
ScrollbarOverlapsContent = false,
Children = new[]
Child = leftFlowContainer = new FillFlowContainer<TestCaseButton>
{
leftFlowContainer = new FillFlowContainer<TestCaseButton>
{
Padding = new MarginPadding(3),
Direction = FillDirection.Vertical,
Spacing = new Vector2(0, 5),
AutoSizeAxes = Axes.Y,
RelativeSizeAxes = Axes.X,
}
Padding = new MarginPadding(3),
Direction = FillDirection.Vertical,
Spacing = new Vector2(0, 5),
AutoSizeAxes = Axes.Y,
RelativeSizeAxes = Axes.X,
}
}
}
Expand All @@ -84,31 +81,28 @@ private void load(Storage storage)
{
RelativeSizeAxes = Axes.Both,
Padding = new MarginPadding { Left = 200 },
Children = new Drawable[]
Child = compilingNotice = new Container
{
compilingNotice = new Container
Alpha = 0,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Masking = true,
Depth = float.MinValue,
CornerRadius = 5,
AutoSizeAxes = Axes.Both,
Children = new Drawable[]
{
Alpha = 0,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Masking = true,
Depth = float.MinValue,
CornerRadius = 5,
AutoSizeAxes = Axes.Both,
Children = new Drawable[]
new Box
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = Color4.Black,
},
new SpriteText
{
TextSize = 30,
Text = @"Compiling new version..."
}
RelativeSizeAxes = Axes.Both,
Colour = Color4.Black,
},
}
new SpriteText
{
TextSize = 30,
Text = @"Compiling new version..."
}
},
}
}
};
Expand Down
9 changes: 3 additions & 6 deletions osu.Framework.Testing/TestCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,10 @@ public virtual void Reset()
Bottom = padding,
},
RelativeSizeAxes = Axes.Both,
Children = new []
Child = content = new Container
{
content = new Container
{
Masking = true,
RelativeSizeAxes = Axes.Both
}
Masking = true,
RelativeSizeAxes = Axes.Both
}
},
};
Expand Down
9 changes: 3 additions & 6 deletions osu.Framework/Graphics/Animations/TextureAnimation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@ public class TextureAnimation : Animation<Texture>

public TextureAnimation()
{
Children = new[]
Child = textureHolder = new Sprite
{
textureHolder = new Sprite
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
}
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
};
}

Expand Down
40 changes: 29 additions & 11 deletions osu.Framework/Graphics/Containers/Container.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,15 @@ public IEnumerable<T> ChildrenEnumerable
}
}

public T Child

This comment was marked as off-topic.

{
set
{
Clear();
Add(value);
}
}

private readonly LifetimeList<Drawable> internalChildren;
private readonly IReadOnlyList<T> internalChildrenAsT;

Expand All @@ -180,7 +189,7 @@ public IEnumerable<Drawable> InternalChildrenEnumerable
{
set
{
Clear();
ClearInternal();

This comment was marked as off-topic.

AddInternal(value);
}
}
Expand Down Expand Up @@ -233,18 +242,22 @@ public void Add(IEnumerable<T> range)
}

/// <summary>
/// Removes a given child from this container.
/// Removes a given child from this container's <see cref="InternalChildren"/>.
/// </summary>
public void Remove(T drawable)
{
if (drawable == null)
throw new ArgumentNullException(nameof(drawable));

if (Content != this)
{
Content.Remove(drawable);
return;
}
else RemoveInternal(drawable);

This comment was marked as off-topic.

}

/// <summary>
/// Removes a given child from this container.
/// </summary>
public void RemoveInternal(T drawable)
{
if (drawable == null)
throw new ArgumentNullException(nameof(drawable));

if (!internalChildren.Remove(drawable))
throw new InvalidOperationException($@"Cannot remove a drawable ({drawable}) which is not a child of this ({this}), but {drawable.Parent}.");
Expand Down Expand Up @@ -312,11 +325,16 @@ public void Remove(IEnumerable<T> range)
public virtual void Clear(bool disposeChildren = true)
{
if (Content != null && Content != this)
{
Content.Clear(disposeChildren);
return;
}
else ClearInternal(disposeChildren);

This comment was marked as off-topic.

}

/// <summary>
/// Clear all of <see cref="InternalChildren"/>.

This comment was marked as off-topic.

/// </summary>
/// <param name="disposeChildren">Whether removed children should also get disposed.</param>
public void ClearInternal(bool disposeChildren = true)
{
foreach (Drawable t in internalChildren)
{
if (disposeChildren)
Expand Down
5 changes: 1 addition & 4 deletions osu.Framework/Graphics/Containers/ScrollContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -396,10 +396,7 @@ public Scrollbar(Direction scrollDir)

Masking = true;

Children = new Drawable[]
{
box = new Box { RelativeSizeAxes = Axes.Both }
};
Child = box = new Box { RelativeSizeAxes = Axes.Both };

ResizeTo(1);
}
Expand Down
11 changes: 4 additions & 7 deletions osu.Framework/Graphics/Cursor/CursorContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,11 @@ public Cursor()
Radius = 50
};

Children = new[]
Child = new Box
{
new Box
{
Size = new Vector2(8, 8),
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
}
Size = new Vector2(8, 8),
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
};
}
}
Expand Down
5 changes: 1 addition & 4 deletions osu.Framework/Graphics/Effects/BlurEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ public BufferedContainer ApplyTo(Drawable drawable)
Vertical = Blur.KernelSize(Sigma.Y)
},
Alpha = Strength,
Children = new[]
{
drawable
}
Child = drawable
};
}
}
Expand Down
2 changes: 1 addition & 1 deletion osu.Framework/Graphics/Effects/EdgeEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public Container ApplyTo(Drawable drawable)
Origin = drawable.Origin,
RelativeSizeAxes = drawable.RelativeSizeAxes,
AutoSizeAxes = Axes.Both & ~drawable.RelativeSizeAxes,
Children = new[] { drawable }
Child = drawable
};
}
}
Expand Down
Loading