A game framework written with osu! in mind.
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2// See the LICENCE file in the repository root for full licence text.
3
4using osu.Framework.Allocation;
5using osu.Framework.Bindables;
6using osu.Framework.Graphics.Primitives;
7using osu.Framework.Platform;
8
9namespace osu.Framework.Graphics.Containers
10{
11 /// <summary>
12 /// A <see cref="Container"/> that is automatically cached and provides a <see cref="BindableSafeArea"/> representing
13 /// the desired safe area margins. Should be used in conjunction with child <see cref="SafeAreaContainer"/>s.
14 /// The root of the scenegraph contains an instance of this container, with <see cref="BindableSafeArea"/> automatically bound
15 /// to the host <see cref="IWindow"/>'s <see cref="IWindow.SafeAreaPadding"/>.
16 /// </summary>
17 [Cached(typeof(ISafeArea))]
18 public class SafeAreaDefiningContainer : Container<Drawable>, ISafeArea
19 {
20 private readonly bool usesCustomBinding;
21
22 private readonly BindableSafeArea safeArea = new BindableSafeArea();
23
24 /// <summary>
25 /// Initialises a <see cref="SafeAreaDefiningContainer"/> by optionally providing a custom <see cref="BindableSafeArea"/>.
26 /// If no such binding is provided, the container will default to <see cref="OsuTKWindow.SafeAreaPadding"/>.
27 /// </summary>
28 /// <param name="safeArea">The custom <see cref="BindableSafeArea"/> to bind to, if required.</param>
29 public SafeAreaDefiningContainer(BindableSafeArea safeArea = null)
30 {
31 if (safeArea != null)
32 {
33 usesCustomBinding = true;
34 this.safeArea.BindTo(safeArea);
35 }
36 }
37
38 [BackgroundDependencyLoader]
39 private void load(GameHost host)
40 {
41 if (!usesCustomBinding && host.Window != null)
42 safeArea.BindTo(host.Window.SafeAreaPadding);
43 }
44
45 #region ISafeArea Implementation
46
47 RectangleF ISafeArea.AvailableNonSafeSpace => DrawRectangle;
48
49 Quad ISafeArea.ExpandRectangleToSpaceOfOtherDrawable(IDrawable other) => ToSpaceOfOtherDrawable(DrawRectangle, other);
50
51 BindableSafeArea ISafeArea.SafeAreaPadding => safeArea;
52
53 #endregion
54 }
55}