/**
* Copyright 2019 Heroic Labs and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using UnityEngine;
using UnityEngine.UI;
namespace DemoGame.Scripts.Gameplay.Cards
{
///
/// Visual representation of a card shown in .
///
public class CardSlotUI : MonoBehaviour
{
#region Fields
///
/// Image representing the card.
///
[SerializeField] private Image _image = null;
///
/// Image representing the background of a card.
///
[SerializeField] private Image _background = null;
///
/// The sprite shown in when is null.
///
[SerializeField] private Sprite _emptySprite = null;
///
/// Button used to select this card to show its stats and options
/// in the .
///
[SerializeField] private Button _selectButton = null;
///
/// Textfield containing the level of this card.
///
[SerializeField] private Text _levelText = null;
///
/// Textfield displaying the card cost.
///
[SerializeField] private Text _cost = null;
///
/// Color of when this slot is selected.
///
[SerializeField] private Color _selectedColor = Color.black;
#endregion
#region Properties
///
/// Reference to the card this object is displaying info of.
/// This can be set using method.
///
public Card Card { get; protected set; }
#endregion
#region Methods
///
/// Should be invoked whenever an instance of
/// is created. Initializes the 's onClick handler.
///
public void Init(Action onClicked)
{
_selectButton.onClick.AddListener(() => onClicked(this));
}
///
/// Sets the reference to displayed by this object.
///
public virtual void SetCard(Card card = null)
{
if (card != null)
{
_image.sprite = card.GetCardInfo().Sprite;
_cost.text = card.GetCardInfo().Cost.ToString();
_levelText.text = "lvl " + card.level.ToString();
_selectButton.interactable = true;
}
else
{
_image.sprite = _emptySprite;
_cost.text = string.Empty;
_levelText.text = string.Empty;
_selectButton.interactable = false;
}
Card = card;
}
///
/// Changes the color of image.
///
public virtual void Select()
{
_background.color = _selectedColor;
}
///
/// Changes the color of image.
///
public virtual void Unselect()
{
_background.color = Color.white;
}
#endregion
}
}