111 lines
3.2 KiB
C#
111 lines
3.2 KiB
C#
using UnityEngine;
|
|||
|
|
using System.Collections;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
|
||
|
|
|
||
|
|
public static class RectTransformTools {
|
||
|
|
public static Vector2 Size(this RectTransform tf) {
|
||
|
|
Vector3[] c = new Vector3[4];
|
||
|
|
tf.GetLocalCorners(c);
|
||
|
|
return new Vector2(c[2].x - c[0].x, c[1].y - c[0].y);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public class ItemViewReference : MonoBehaviour {
|
||
|
|
public ItemViewController itemView;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
public delegate void ItemsViewDelegate(ItemsView container);
|
||
|
|
|
||
|
|
public class ItemsView : MonoBehaviour {
|
||
|
|
public RectTransform container;
|
||
|
|
private IEnumerable<PickableItem> _items;
|
||
|
|
public GameObject itemViewPrototype;
|
||
|
|
private List<ItemViewController> _views = new List<ItemViewController>();
|
||
|
|
public List<ItemViewController> Views {
|
||
|
|
get { return _views; }
|
||
|
|
}
|
||
|
|
public float itemsMargin = 5.0f;
|
||
|
|
public event ItemsViewDelegate SelectionChanged;
|
||
|
|
private ItemViewController _selection;
|
||
|
|
public ItemViewController Selection {
|
||
|
|
get { return _selection; }
|
||
|
|
}
|
||
|
|
|
||
|
|
protected virtual void OnSelectionChanged() {
|
||
|
|
ItemsViewDelegate handler = SelectionChanged;
|
||
|
|
if (handler != null) {
|
||
|
|
handler(this);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public IEnumerable<PickableItem> Items {
|
||
|
|
get { return _items; }
|
||
|
|
set {
|
||
|
|
_items = value;
|
||
|
|
RebuildUI();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private void Start() {
|
||
|
|
|
||
|
|
var t = itemViewPrototype.GetComponent<RectTransform>();
|
||
|
|
|
||
|
|
var tfSize = t.anchoredPosition;
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
void OnClick(UnityEngine.UI.Button b) {
|
||
|
|
var view = b.gameObject.GetComponent<ItemViewReference>().itemView;
|
||
|
|
_selection = view;
|
||
|
|
OnSelectionChanged();
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
public void RebuildUI() {
|
||
|
|
//remove old items view
|
||
|
|
foreach (var view in _views) {
|
||
|
|
Destroy(view.gameObject);
|
||
|
|
}
|
||
|
|
_views.Clear();
|
||
|
|
|
||
|
|
//create new views
|
||
|
|
if (itemViewPrototype != null) {
|
||
|
|
foreach (var item in _items) {
|
||
|
|
var view = Instantiate<GameObject>(itemViewPrototype).GetComponent<ItemViewController>();
|
||
|
|
view.item = item;
|
||
|
|
_views.Add(view);
|
||
|
|
view.GetComponent<RectTransform>().parent = container;
|
||
|
|
var reference = view.itemButton.gameObject.AddComponent<ItemViewReference>();
|
||
|
|
reference.itemView = view;
|
||
|
|
view.itemButton.onClick.AddListener(OnClick);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
else {
|
||
|
|
Debug.LogWarning("Can't create items view! Prototype not assigned!");
|
||
|
|
}
|
||
|
|
|
||
|
|
ArrangeItems();
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
public void ArrangeItems() {
|
||
|
|
var areaSize = container.Size();
|
||
|
|
var itemSize = itemViewPrototype.GetComponent<RectTransform>().Size();
|
||
|
|
|
||
|
|
int hCount = (int)((areaSize.x - itemsMargin) / (itemSize.x + itemsMargin));
|
||
|
|
|
||
|
|
for (int i = 0; i < _views.Count; ++i) {
|
||
|
|
var view = _views[i];
|
||
|
|
var tf = view.GetComponent<RectTransform>();
|
||
|
|
int row = i/hCount;
|
||
|
|
int column = i%hCount;
|
||
|
|
tf.anchoredPosition = new Vector2(column * (itemSize.x + itemsMargin) + itemsMargin + itemSize.x * 0.5f, -(row * (itemSize.y + itemsMargin) + itemsMargin + itemSize.y * 0.5f));
|
||
|
|
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|