namespace UIWidgets
{
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using UIWidgets.Extensions;
using UIWidgets.Styles;
using UnityEngine;
using UnityEngine.UI;
///
/// Class for the buttons instances.
///
/// Type of the owner.
/// Base type of the owner.
/// Type of the button instance.
/// Type of the button configuration.
public abstract class DialogButtonsPoolCustom
where TOwner : class, IHideable, TOwnerBase
where TOwnerBase : class, IHideable
where TButtonInstance : DialogButtonCustom
where TButtonConfig : ButtonConfiguration
{
///
/// Owner.
///
protected TOwner Owner;
///
/// Buttons templates.
///
protected ReadOnlyCollection Templates;
///
/// Active buttons.
///
protected List Active;
///
/// Cached buttons.
///
protected List> Cache;
///
/// Count.
///
public int Count
{
get
{
return Templates.Count;
}
}
///
/// Buttons container.
///
protected RectTransform Container;
///
/// Initializes a new instance of the class.
///
/// Dialog.
/// Templates.
/// Container.
/// List for the active buttons.
/// List for the cached buttons.
public DialogButtonsPoolCustom(TOwner owner, ReadOnlyCollection templates, RectTransform container, List active, List> cache)
{
Owner = owner;
Active = active;
Cache = cache;
Container = container;
SetTemplates(templates);
}
///
/// Set focus to the specified button.
///
/// Button label.
/// true if button found with specified label; otherwise false.
public bool Focus(string focusButton)
{
for (int i = 0; i < Active.Count; i++)
{
if (Active[i].Info.Label == focusButton)
{
Active[i].Button.Select();
return true;
}
}
return false;
}
///
/// Set templates.
///
/// Templates.
public void SetTemplates(ReadOnlyCollection templates)
{
Templates = templates;
for (int i = 0; i < Templates.Count; i++)
{
Templates[i].gameObject.SetActive(false);
}
EnsureListSize(Cache, Templates.Count);
}
///
/// Ensure list size.
///
/// Type of the list data.
/// List.
/// Required size.
protected static void EnsureListSize(List> list, int size)
{
for (int i = list.Count; i < size; i++)
{
list.Add(new List());
}
for (int i = size; i > list.Count; i--)
{
list.RemoveAt(i - 1);
}
}
///
/// Get the button.
///
/// Index of the button.
/// Button info.
/// Button.
public TButtonInstance Get(int buttonIndex, TButtonConfig info)
{
TButtonInstance instance;
if (Cache[info.TemplateIndex].Count > 0)
{
instance = Cache[info.TemplateIndex].Pop();
instance.Change(buttonIndex, info);
}
else
{
instance = CreateButtonInstance(buttonIndex, info);
}
Active.Add(instance);
instance.SetActive(true);
return instance;
}
///
/// Create the button.
///
/// Index of the button.
/// Button info.
/// Button.
protected abstract TButtonInstance CreateButtonInstance(int buttonIndex, TButtonConfig info);
///
/// Replace buttons templates.
///
/// Templates.
public void Replace(ReadOnlyCollection templates)
{
ClearCache();
SetTemplates(templates);
for (int button_index = 0; button_index < Active.Count; button_index++)
{
var button = Active[button_index];
button.Replace(Templates[button.TemplateIndex]);
}
}
///
/// Disable.
///
public void Disable()
{
for (int button_index = 0; button_index < Active.Count; button_index++)
{
var button = Active[button_index];
button.SetActive(false);
Cache[button.TemplateIndex].Add(button);
}
Active.Clear();
}
///
/// Clear cache.
///
protected void ClearCache()
{
for (int template_index = 0; template_index < Cache.Count; template_index++)
{
for (int i = 0; i < Cache[template_index].Count; i++)
{
Cache[template_index][i].Destroy();
}
Cache[template_index].Clear();
}
}
///
/// Execute action for each button.
///
/// Action.
public void ForEach(Action action)
{
for (int template_index = 0; template_index < Templates.Count; template_index++)
{
action(Templates[template_index]);
for (int button_index = 0; button_index < Cache[template_index].Count; button_index++)
{
action(Cache[template_index][button_index].Button);
}
}
for (int button_index = 0; button_index < Active.Count; button_index++)
{
action(Active[button_index].Button);
}
}
///
/// Set style.
///
/// Button style.
public void SetStyle(StyleButton styleButton)
{
for (int template_index = 0; template_index < Templates.Count; template_index++)
{
styleButton.ApplyTo(Templates[template_index].gameObject);
for (int button_index = 0; button_index < Cache[template_index].Count; button_index++)
{
styleButton.ApplyTo(Cache[template_index][button_index].Button.gameObject);
}
}
for (int button_index = 0; button_index < Active.Count; button_index++)
{
styleButton.ApplyTo(Active[button_index].Button.gameObject);
}
}
///
/// Update buttons name.
///
public void UpdateButtonsName()
{
for (int i = 0; i < Active.Count; i++)
{
Active[i].UpdateButtonName();
}
}
}
}