#region Includes
using UnityEngine;
#endregion
#if UNITY_EDITOR
using UnityEditor;
#endif
///
/// This class represents a container for a page in a paginated view.
/// It handles assigning content to the container and manages the active state of the contained page.
///
public class PageContainer : MonoBehaviour
{
#region Variables
[Header("Children")]
//
/// The PageView component representing the content of this page container.
///
[Tooltip("The PageView component representing the content of this page container")]
[SerializeField] private PageView _page;
#endregion
///
/// Assigns content (RectTransform) to this container.
/// If no content is provided, it creates a new GameObject with a RectTransform and a PageView component.
/// The assigned content is then parented to this container and its properties are set to ensure proper positioning and scaling.
///
/// The RectTransform representing the content to be assigned.
public void AssignContent(RectTransform content)
{
if (content == null)
{
// Create a new GameObject with required components if content is not provided.
var contentObject = new GameObject("Content", typeof(RectTransform), typeof(PageView));
content = contentObject.GetComponent();
}
content.SetParent(transform);
content.anchorMin = Vector2.zero;
content.anchorMax = Vector2.one;
content.offsetMin = Vector2.zero;
content.offsetMax = Vector2.zero;
content.anchoredPosition = Vector2.zero;
content.localScale = Vector3.one;
_page = content.GetComponent();
#if UNITY_EDITOR
EditorUtility.SetDirty(this);
#endif
}
///
/// Calls the ChangingToActiveState method on the contained PageView component,
/// to signal a transition to an active state.
///
public void ChangingToActiveState()
{
_page.ChangingToActiveState();
}
///
/// Calls the ChangingToInactiveState method on the contained PageView component,
/// to signal a transition to an inactive state.
///
public void ChangingToInactiveState()
{
_page.ChangingToInactiveState();
}
///
/// Calls the ChangeActiveState method on the contained PageView component with the provided active state.
///
/// True to set the page to active, False to set it to inactive.
public void ChangeActiveState(bool active)
{
_page.ChangeActiveState(active);
}
}