@ -14,36 +14,32 @@ using UnityEngine.U2D;
/// and display the details of the item. The original implementation was hardcoded for the grid view. We use patterns to
/// make this one a little more universally useful
/// </summary>
public class ViewItemContainer : MonoBehaviour , IItemContainer , IShopModelObserver < Item >
public abstract class ViewItemContainer : MonoBehaviour , IItemContainer , IShopModelObserver < Item >
{
public Item Item = > item ; //Public getter for the item, required by IItemContainer interface.
// IItemContainer interface requires to know whether an item is selected visually or not. This implementation determines that by checking the info panel
public bool IsSelected {
get { return infoPanel . gameObject . activeSelf ; }
set { infoPanel . SetActive ( value ) ; highLight . SetActive ( value ) ; }
public virtual bool IsSelected {
get { return highLight . gameObject . activeSelf ; }
set { highLight . SetActive ( value ) ; }
}
//Link to the highlight image (set in prefab)
[SerializeField]
private GameObject highLight ;
protected GameObject highLight ;
//Link to the infomation panel (set in prefab), prototype pattern
[SerializeField]
private GameObject infoPanel ;
[SerializeField]
private Image icon ;
protected Image icon ;
//Link to the atlas of all the item icons, use to retrieve sprites for items. For more information of the API check:
// https://docs.unity3d.com/2019.3/Documentation/Manual/class-SpriteAtlas.html
[SerializeField]
private SpriteAtlas iconAtlas ;
protected SpriteAtlas iconAtlas ;
//link to the original item (set in Initialize)
private Item item ;
protected Item item ;
private IDisposable _ unsubscriber ; // If this item manages its own lifetime, this will not be null
private IDisposable unsubscriber ; // If this item manages its own lifetime, this will not be null
//------------------------------------------------------------------------------------------------------------------------
// Initialize()
//------------------------------------------------------------------------------------------------------------------------
@ -51,28 +47,21 @@ public class ViewItemContainer : MonoBehaviour, IItemContainer, IShopModelObserv
//Stores the item
this . item = item ;
_ unsubscriber = unsubscriber ;
//Sets the highlight image and infoPanel's visibility
//if (isSelected) {
// highLight.SetActive(true);
// infoPanel.SetActive(true);
//}
this . unsubscriber = unsubscriber ;
// Clones the first Sprite in the icon atlas that matches the iconName and uses it as the sprite of the icon image.
Sprite sprite = iconAtlas . GetSprite ( item . iconName ) ;
// Sets name in all locations the prototype has a name component attached to
var nameComps = GetComponentsInChildren < ItemNameDisplay > ( true ) ; // Include inactives!
foreach ( var name in nameComps )
{
name . SetName ( item . iconName ) ; // Use iconname for now, debugging
}
if ( sprite ! = null ) {
icon . sprite = sprite ;
}
IsSelected = false ;
OnInitialize ( item ) ;
}
protected abstract void OnInitialize ( Item item ) ;
// When the observable fires, check if we're the view corresponding to the selected item. Select if we are!
public void OnSelected ( Item item )
{
@ -94,6 +83,6 @@ public class ViewItemContainer : MonoBehaviour, IItemContainer, IShopModelObserv
// The reason we do this in OnDestroy() is so we don't have a memory leak when this gets removed externally somehow
private void OnDestroy ( )
{
_ unsubscriber ? . Dispose ( ) ; // Careful! If we don't make this thing aware that it manages itself, and we forget to manage it, we have a memory leak
unsubscriber ? . Dispose ( ) ; // Careful! If we don't make this thing aware that it manages itself, and we forget to manage it, we have a memory leak
}
}