/*
* Copyright 2022 Peter Han
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using UnityEngine;
using UnityEngine.UI;
namespace PeterHan.PLib.UI {
///
/// The abstract parent of PLib UI objects that are meant to contain other UI objects.
///
public abstract class PContainer : IUIComponent {
///
/// The background color of this panel.
///
public Color BackColor { get; set; }
///
/// The background image of this panel. Tinted by the background color, acts as all
/// white if left null.
///
/// Note that the default background color is transparent, so unless it is set to
/// some other color this image will be invisible!
///
public Sprite BackImage { get; set; }
///
/// The flexible size bounds of this component.
///
public Vector2 FlexSize { get; set; }
///
/// The mode to use when displaying the background image.
///
public Image.Type ImageMode { get; set; }
///
/// The margin left around the contained components in pixels. If null, no margin will
/// be used.
///
public RectOffset Margin { get; set; }
public string Name { get; protected set; }
public event PUIDelegates.OnRealize OnRealize;
protected PContainer(string name) {
BackColor = PUITuning.Colors.Transparent;
BackImage = null;
FlexSize = Vector2.zero;
ImageMode = Image.Type.Simple;
Margin = null;
Name = name ?? "Container";
}
public abstract GameObject Build();
///
/// Invokes the OnRealize event.
///
/// The realized text component.
protected void InvokeRealize(GameObject obj) {
OnRealize?.Invoke(obj);
}
///
/// Configures the background color and/or image for this panel.
///
/// The realized panel object.
protected void SetImage(GameObject panel) {
if (BackColor.a > 0.0f || BackImage != null) {
var img = panel.AddComponent();
img.color = BackColor;
if (BackImage != null) {
img.sprite = BackImage;
img.type = ImageMode;
}
}
}
public override string ToString() {
return string.Format("PContainer[Name={0}]", Name);
}
}
}