/*
* 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 PeterHan.PLib.Core;
using PeterHan.PLib.UI.Layouts;
using System;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace PeterHan.PLib.UI {
///
/// A factory for scrollable panes.
///
public sealed class PScrollPane : IUIComponent {
///
/// The track size of scrollbars is based on the sprite.
///
private const float DEFAULT_TRACK_SIZE = 16.0f;
///
/// Whether the horizontal scrollbar is always visible.
///
public bool AlwaysShowHorizontal { get; set; }
///
/// Whether the vertical scrollbar is always visible.
///
public bool AlwaysShowVertical { get; set; }
///
/// The background color of this scroll pane.
///
public Color BackColor { get; set; }
///
/// The child of this scroll pane.
///
public IUIComponent Child { get; set; }
///
/// The flexible size bounds of this component.
///
public Vector2 FlexSize { get; set; }
public string Name { get; }
///
/// Whether horizontal scrolling is allowed.
///
public bool ScrollHorizontal { get; set; }
///
/// Whether vertical scrolling is allowed.
///
public bool ScrollVertical { get; set; }
///
/// The size of the scrollbar track.
///
public float TrackSize { get; set; }
public event PUIDelegates.OnRealize OnRealize;
public PScrollPane() : this(null) { }
public PScrollPane(string name) {
AlwaysShowHorizontal = AlwaysShowVertical = true;
BackColor = PUITuning.Colors.Transparent;
Child = null;
FlexSize = Vector2.zero;
Name = name ?? "Scroll";
ScrollHorizontal = false;
ScrollVertical = false;
TrackSize = DEFAULT_TRACK_SIZE;
}
///
/// Adds a handler when this scroll pane is realized.
///
/// The handler to invoke on realization.
/// This scroll pane for call chaining.
public PScrollPane AddOnRealize(PUIDelegates.OnRealize onRealize) {
OnRealize += onRealize;
return this;
}
public GameObject Build() {
if (Child == null)
throw new InvalidOperationException("No child component");
var pane = BuildScrollPane(null, Child.Build());
OnRealize?.Invoke(pane);
return pane;
}
///
/// Builds the actual scroll pane object.
///
/// The parent of this scroll pane.
/// The child element of this scroll pane.
/// The realized scroll pane.
internal GameObject BuildScrollPane(GameObject parent, GameObject child) {
var pane = PUIElements.CreateUI(parent, Name);
if (BackColor.a > 0.0f)
pane.AddComponent().color = BackColor;
pane.SetActive(false);
// Scroll pane itself
var scroll = pane.AddComponent();
scroll.horizontal = ScrollHorizontal;
scroll.vertical = ScrollVertical;
// Viewport
var viewport = PUIElements.CreateUI(pane, "Viewport");
viewport.rectTransform().pivot = Vector2.up;
viewport.AddComponent().enabled = true;
viewport.AddComponent();
scroll.viewport = viewport.rectTransform();
// Give the Child a separate Canvas to reduce layout rebuilds
child.AddOrGet