using ORKFramework;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using ORKEnums;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class HotkeyButtonControl : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerClickHandler
{
[Tooltip("Text field showing the key bound to this hotkey/shortcut")]
[SerializeField]
private TextMeshProUGUI buttonInfoText;
[Tooltip("Text field to show the quantity of the shortcut (if any)")]
[SerializeField]
private TextMeshProUGUI quantityText;
[Tooltip("The ID for the shortcut key (1-9..0)")]
[SerializeField]
private int id;
[Tooltip("Image of the hotkey (Ability, item etc)")]
[SerializeField]
private Image hotkeyImage;
[Tooltip("Quanity value background image")]
[SerializeField]
private Image quanityBackgroundImage;
[Tooltip("Image user for ability timer function")]
[SerializeField]
private Image cooldownImage;
[Tooltip("Text field showing the time left (in seconds) until this ability can be used again")]
[SerializeField]
private TextMeshProUGUI cooldownText;
[Tooltip("Show the cooldown text field?")]
[SerializeField]
private bool showCooldownText;
protected Combatant playerCombatant = null;
private IShortcut currentShortcut = null;
private bool canUseState = true;
private int lastQuantityValue = 0;
private float currentMaxReuseTime = 0f;
public void Start()
{
StartCoroutine(GetPlayer());
// Initially clear the icons
hotkeyImage.enabled = false;
currentShortcut = null;
buttonInfoText.text = id.ToString(); // TODO: this needs to be changed to the real SHORTCUT key
quantityText.alpha = 0;
cooldownText.text = "";
cooldownText.alpha = 0;
cooldownImage.fillAmount = 0;
}
private IEnumerator GetPlayer()
{
do
{
playerCombatant = ORK.Game.ActiveGroup.Leader;
yield return null;
}
while (this.playerCombatant == null);
// ** Initialization code that requires a combatant here **
buttonInfoText.text = id.ToString(); // TODO: this needs to be changed to the real SHORTCUT key
}
// Update is called once per frame
public void Update()
{
if (playerCombatant != null)
{
IShortcut shortcut = playerCombatant.Shortcuts.Current[id];
if (shortcut != null)
{
if (currentShortcut == null || currentShortcut.ID != shortcut.ID || ((currentShortcut is ItemShortcut) && currentShortcut.Quantity != lastQuantityValue))
{
// we have a new shortcut lets deal with it
currentShortcut = shortcut;
cooldownText.text = "";
hotkeyImage.enabled = true;
if (currentShortcut is AbilityShortcut)
{
AbilityShortcut abs = (currentShortcut as AbilityShortcut);
Texture tex = abs.GetIcon();
hotkeyImage.sprite = Sprite.Create(tex as Texture2D, new Rect(0, 0, tex.width, tex.height), new Vector2(.5f, .5f));
quantityText.alpha = 0;
quanityBackgroundImage.color = new Color(quanityBackgroundImage.color.r, quanityBackgroundImage.color.g, quanityBackgroundImage.color.b, 0);
}
else if (currentShortcut is ItemShortcut)
{
Texture tex = (currentShortcut as ItemShortcut).GetIcon();
hotkeyImage.sprite = Sprite.Create(tex as Texture2D, new Rect(0, 0, tex.width, tex.height), new Vector2(.5f, .5f));
//; and display the QUANTIY count
lastQuantityValue = currentShortcut.Quantity;
quantityText.text = lastQuantityValue.ToString();
quantityText.alpha = 1;
quanityBackgroundImage.color = new Color(quanityBackgroundImage.color.r, quanityBackgroundImage.color.g, quanityBackgroundImage.color.b, 1);
}
}
bool canWeCurrentlyUseShortcut = currentShortcut.CanUse(playerCombatant, true, true);
if (canWeCurrentlyUseShortcut && !canUseState)
{
// enable button
canUseState = canWeCurrentlyUseShortcut;
hotkeyImage.color = new Color(1f, 1f, 1f, 1f);
}
else if (!canWeCurrentlyUseShortcut && canUseState)
{
canUseState = canWeCurrentlyUseShortcut;
hotkeyImage.color = new Color(0.4f, 0.4f, 0.4f, 0.4f);
}
// check to see if we need to update on screen cooldown timer
if (currentShortcut is AbilityShortcut)
{
AbilityShortcut abs = (currentShortcut as AbilityShortcut);
float abilityCooldownTime = abs.GetReuseTime(playerCombatant);
if (abilityCooldownTime > 0)
{
if (currentMaxReuseTime == 0)
{
currentMaxReuseTime = abilityCooldownTime;
cooldownImage.fillAmount = 1;
}
float percentVal = 1.0f / currentMaxReuseTime * abilityCooldownTime;
cooldownImage.fillAmount = percentVal;
if (showCooldownText)
{
cooldownText.alpha = 1;
string newText = abs.GetReuseTimeText(playerCombatant, 1); // Change the Decimal place here for WHOLE seconds (or whatever desired)
if (cooldownText.text != newText)
cooldownText.text = newText;
}
}
else
{
cooldownText.alpha = 0;
currentMaxReuseTime = 0f;
cooldownImage.fillAmount = 0;
}
}
}
else
{
// no shortcut, clear the icons
hotkeyImage.enabled = false;
currentShortcut = null;
quantityText.alpha = 0;
quanityBackgroundImage.color = new Color(quanityBackgroundImage.color.r, quanityBackgroundImage.color.g, quanityBackgroundImage.color.b, 0);
}
}
}
public void DoShortcutClick()
{
if (currentShortcut != null)
{
if (currentShortcut is AbilityShortcut)
{
if ((currentShortcut as AbilityShortcut).CanUse(playerCombatant, true, true))
{
(currentShortcut as AbilityShortcut).AutoUse(playerCombatant);
}
}
else if (currentShortcut is ItemShortcut)
{
if ((currentShortcut as ItemShortcut).CanUse(playerCombatant, true, true))
{
(currentShortcut as ItemShortcut).AutoUse(playerCombatant);
}
}
}
}
public void OnPointerEnter(PointerEventData eventData)
{
ORK.GUI.ForceCursorOver = true;
}
public void OnPointerExit(PointerEventData eventData)
{
ORK.GUI.ForceCursorOver = false;
}
public void OnPointerClick(PointerEventData eventData)
{
DoShortcutClick();
}
}
It looks like you're new here. If you want to get involved, click one of these buttons!
If you're enjoying my products, updates and support, please consider supporting me on patreon.com!