2023-09-14 11:13:53 +00:00
using TMPro ;
using UnityEngine ;
2023-10-05 09:29:43 +00:00
using UnityEngine.EventSystems ;
2023-09-14 11:13:53 +00:00
using UnityEngine.UI ;
public class SingleLevelProbabilityPanel : MonoBehaviour
{
public TextMeshProUGUI levelNameText ;
2023-10-05 09:29:43 +00:00
public TMP_InputField inputField ;
public Button lockButton ;
public Image lockImg ;
public Image unlockImg ;
2023-09-14 11:13:53 +00:00
public Slider probabilitySlider ;
2023-10-05 09:29:43 +00:00
[SerializeField]
private float probabilityValue = 0f ;
private bool isLocked = false ;
2023-09-14 11:13:53 +00:00
/// <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 ) ;
2023-10-05 09:29:43 +00:00
InitializeButton ( ) ;
2023-09-14 11:13:53 +00:00
}
/// <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 )
{
2023-10-05 09:29:43 +00:00
Debug . Log ( levelName + " " + probability ) ;
2023-09-14 11:13:53 +00:00
SetLevelName ( levelName ) ;
SetProbability ( probability ) ;
2023-10-05 09:29:43 +00:00
InitializeButton ( ) ;
2023-09-14 11:13:53 +00:00
}
/// <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 )
{
2023-10-05 09:29:43 +00:00
probabilityValue = prob ;
2023-09-14 11:13:53 +00:00
UpdateProbabilityText ( ) ;
2023-10-05 09:29:43 +00:00
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 ;
2023-09-14 11:13:53 +00:00
}
/// <summary>
2023-10-05 09:29:43 +00:00
/// Gets the current probability value.
2023-09-14 11:13:53 +00:00
/// </summary>
2023-10-05 09:29:43 +00:00
/// <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 ( )
2023-09-14 11:13:53 +00:00
{
2023-10-05 09:29:43 +00:00
lockImg . gameObject . SetActive ( isLocked ) ;
unlockImg . gameObject . SetActive ( ! isLocked ) ;
2023-09-14 11:13:53 +00:00
}
}