/*
* 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 System;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace PeterHan.PLib.UI {
///
/// An improved variant of DropDown/Dropdown.
///
internal sealed class PComboBoxComponent : KMonoBehaviour {
///
/// The container where the combo box items will be placed.
///
internal RectTransform ContentContainer { get; set; }
///
/// The color for the checkbox if it is selected.
///
internal Color CheckColor { get; set; }
///
/// The prefab used to display each row.
///
internal GameObject EntryPrefab { get; set; }
///
/// The maximum number of rows to be shown.
///
internal int MaxRowsShown { get; set; }
///
/// Called when an item is selected.
///
internal Action OnSelectionChanged { get; set; }
///
/// The object which contains the pull down section of the combo box.
///
internal GameObject Pulldown { get; set; }
///
/// The selected label.
///
internal TMP_Text SelectedLabel { get; set; }
///
/// The items which are currently shown in this combo box.
///
private readonly IList currentItems;
///
/// The currently active mouse event handler, or null if not yet configured.
///
private MouseEventHandler handler;
///
/// Whether the combo box is expanded.
///
private bool open;
internal PComboBoxComponent() {
CheckColor = Color.white;
currentItems = new List(32);
handler = null;
MaxRowsShown = 8;
open = false;
}
///
/// Closes the pulldown. The selected choice is not saved.
///
public void Close() {
Pulldown?.SetActive(false);
open = false;
}
///
/// Triggered when the combo box is clicked.
///
public void OnClick() {
if (open)
Close();
else
Open();
}
protected override void OnPrefabInit() {
base.OnPrefabInit();
handler = gameObject.AddOrGet();
}
///
/// Opens the pulldown.
///
public void Open() {
var pdn = Pulldown;
if (pdn != null) {
float rowHeight = 0.0f;
int itemCount = currentItems.Count, rows = Math.Min(MaxRowsShown, itemCount);
var canvas = pdn.AddOrGet