172 lines
5.5 KiB
C#
172 lines
5.5 KiB
C#
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
|
|
public class SingleLevelProbabilityPanel : MonoBehaviour
|
|
{
|
|
public TextMeshProUGUI levelNameText;
|
|
public TMP_InputField inputField;
|
|
public Button lockButton;
|
|
public Image lockImg;
|
|
public Image unlockImg;
|
|
|
|
public Slider probabilitySlider;
|
|
|
|
[SerializeField]
|
|
private float probabilityValue = 0f;
|
|
|
|
private bool isLocked = false;
|
|
|
|
/// <summary>
|
|
/// Initialize the level probability panel, setting the level name and probability value.
|
|
/// </summary>
|
|
/// <param name="levelName">The level name.</param>
|
|
/// <param name="probability">The probability value.</param>
|
|
public void InitializeLevelProbabilityPanel(string levelName, float probability)
|
|
{
|
|
SetLevelName(levelName);
|
|
SetProbability(probability);
|
|
InitializeButton();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initialize the level probability panel, setting the level number and probability value.
|
|
/// </summary>
|
|
/// <param name="levelName">The level number.</param>
|
|
/// <param name="probability">The probability value.</param>
|
|
public void InitializeLevelProbabilityPanel(int levelName, float probability)
|
|
{
|
|
SetLevelName(levelName);
|
|
SetProbability(probability);
|
|
InitializeButton();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Set the level name.
|
|
/// </summary>
|
|
/// <param name="levelName">The level name.</param>
|
|
public void SetLevelName(string levelName)
|
|
{
|
|
levelNameText.text = levelName;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Set the level name.
|
|
/// </summary>
|
|
/// <param name="levelName">The level number.</param>
|
|
public void SetLevelName(int levelName)
|
|
{
|
|
levelNameText.text = "Level" + levelName.ToString();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Set the probability value.
|
|
/// </summary>
|
|
/// <param name="prob">The probability value.</param>
|
|
public void SetProbability(float prob)
|
|
{
|
|
probabilityValue = prob;
|
|
UpdateProbabilityText();
|
|
UpdateProbabilitySlider();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates the probability text display and can optionally synchronize the probability slider update.
|
|
/// </summary>
|
|
/// <param name="value">The probability value (optional).</param>
|
|
/// <param name="syncToSlider">Whether to synchronize the probability slider update (default is false).</param>
|
|
/// <remarks>
|
|
/// If a probability value (value) is provided, it updates the probability text using that value. Otherwise, it parses the percentage value from the input field and updates the text.
|
|
/// If syncToSlider is true, it also synchronizes the probability slider update.
|
|
/// </remarks>
|
|
public void UpdateProbabilityText(float? value = null)
|
|
{
|
|
if (value != null)
|
|
{
|
|
probabilityValue = (float)value;
|
|
}
|
|
inputField.text = (probabilityValue * 100f).ToString("F1") + "%";
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates the value of the probability slider and can optionally synchronize the probability text update.
|
|
/// </summary>
|
|
/// <param name="value">The probability value (optional).</param>
|
|
/// <param name="syncToText">Whether to synchronize the probability text update (default is false).</param>
|
|
/// <remarks>
|
|
/// If a probability value (value) is provided, it updates the probability slider using that value. Otherwise, it parses the percentage value from the input field and updates the slider.
|
|
/// If syncToText is true, it also synchronizes the probability text update.
|
|
/// </remarks>
|
|
public void UpdateProbabilitySlider(float? value = null)
|
|
{
|
|
if (value != null)
|
|
{
|
|
probabilityValue = (float)value;
|
|
}
|
|
probabilitySlider.value = probabilityValue;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the current probability value.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// This property allows external code to read the current probability value.
|
|
/// </remarks>
|
|
public float ProbabilityValue
|
|
{
|
|
get { return probabilityValue; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets a value indicating whether the object is locked.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Returns true if the object is locked; otherwise, returns false.
|
|
/// </remarks>
|
|
public bool IsLocked
|
|
{
|
|
get { return isLocked; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets a value indicating whether the object is unlocked.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Returns true if the object is unlocked; otherwise, returns false.
|
|
/// </remarks>
|
|
public bool UnLocked
|
|
{
|
|
get { return !isLocked; }
|
|
}
|
|
|
|
private void InitializeButton()
|
|
{
|
|
EventTrigger eventTrigger = lockButton.GetComponent<EventTrigger>();
|
|
if (eventTrigger == null)
|
|
{
|
|
eventTrigger = lockButton.gameObject.AddComponent<EventTrigger>();
|
|
}
|
|
EventTrigger.Entry entry = new EventTrigger.Entry();
|
|
entry.eventID = EventTriggerType.PointerClick;
|
|
entry.callback.AddListener((data) => { OnLockButtonClicked(); });
|
|
eventTrigger.triggers.Add(entry);
|
|
// set lockImg as Unlock
|
|
isLocked = false;
|
|
UpdateLockImg();
|
|
}
|
|
|
|
private void OnLockButtonClicked()
|
|
{
|
|
isLocked = !isLocked;
|
|
UpdateLockImg();
|
|
probabilitySlider.interactable = !isLocked;
|
|
inputField.interactable = !isLocked;
|
|
}
|
|
|
|
private void UpdateLockImg()
|
|
{
|
|
lockImg.gameObject.SetActive(isLocked);
|
|
unlockImg.gameObject.SetActive(!isLocked);
|
|
}
|
|
} |