using System.Collections.Generic; using System.Linq; using TMPro; using UnityEngine; using UnityEngine.EventSystems; public class TargetLevelProbabilityPanel : MonoBehaviour { public GameObject singleLevelProbabilityPanel; public GameObject targetTitleText; private GameObject titleText; private List singleLevelPanelsObjs = new List(); private List singleLevelPanels = new List(); private int panelNum = 0; public void IntializePanels(int levelNum, string titleName) { // initialize target level probability panel size float defaultWidth = singleLevelProbabilityPanel.GetComponent().sizeDelta.x; float defaultLevelHeight = singleLevelProbabilityPanel.GetComponent().sizeDelta.y; float titleHeight = targetTitleText.GetComponent().sizeDelta.y; float averageProbability = 1f / levelNum; float lastLevelProbability = 1f - averageProbability * (levelNum - 1); Debug.Log("averageProbability: " + averageProbability); transform.GetComponent().sizeDelta = new Vector2(defaultWidth, (defaultLevelHeight * levelNum) + titleHeight); // create title text titleText = Instantiate(targetTitleText, transform); titleText.GetComponent().text = titleName; // create and initialize single level probability panels for (int i = 0; i < levelNum; i++) { int tempIndex = i; singleLevelPanelsObjs.Add(Instantiate(singleLevelProbabilityPanel, transform)); singleLevelPanels.Add(singleLevelPanelsObjs[i].GetComponent()); singleLevelPanels[i].InitializeLevelProbabilityPanel(i, i == levelNum - 1 ? lastLevelProbability : averageProbability); //add onValueChanged event to slider and input field singleLevelPanels[i].probabilitySlider.onValueChanged.AddListener((value) => OnSliderValueChange(value, tempIndex)); } panelNum = levelNum; } /// /// Adds an event trigger entry to an event trigger. /// /// The event trigger object. /// The event trigger type. /// The event handler method to execute. private void AddEventTrigger(GameObject gameObject, EventTriggerType triggerType, System.Action action) { EventTrigger eventTrigger = gameObject.GetComponent(); if (eventTrigger == null) { eventTrigger = gameObject.AddComponent(); } EventTrigger.Entry entry = new EventTrigger.Entry(); entry.eventID = triggerType; entry.callback.AddListener(new UnityEngine.Events.UnityAction(action)); eventTrigger.triggers.Add(entry); } /// /// On Probability Slider Value Change.Adjust other sliders' value to make sure the total value is 1. /// /// change to this value /// changed panel index private void OnSliderValueChange(float value, int exceptedIndex) { float newTotalValue = 0; int unlockedPanelNum = 0; int remainCorrectionNum = panelNum; float extraValue = 0; float[] correctionValues = new float[panelNum]; // calculate total probability value for (int i = 0; i < panelNum; i++) { // disable slider listener singleLevelPanels[i].probabilitySlider.onValueChanged.RemoveAllListeners(); newTotalValue += (i == exceptedIndex ? value : singleLevelPanels[i].ProbabilityValue); if (singleLevelPanels[i].UnLocked) { unlockedPanelNum++; } } // only have one panel if (panelNum == 1) { singleLevelPanels[exceptedIndex].SetProbability(1); enableSliderListener(); return; } //only one panel is unlocked if (unlockedPanelNum == 1) { // limit this panel value under 1 - (newTotalValue - value) singleLevelPanels[exceptedIndex].SetProbability(newTotalValue - 1 > 0 ? 1 - newTotalValue + value : value); enableSliderListener(); return; } // Calculate the average correction value extraValue = newTotalValue - 1; // initialize correction value to each panel correctionValues = Enumerable.Repeat(extraValue / (unlockedPanelNum - 1), panelNum).ToArray(); // make sure all probability value is not less than 0 and equal to 1 int iterationCount = 0; while (remainCorrectionNum > 0) { iterationCount++; (correctionValues, remainCorrectionNum) = reCalculateCorrectionValues(correctionValues, exceptedIndex, value, extraValue); // protect the infinite loop if (iterationCount >= 100) { Debug.LogError("Infinite loop detected!"); break; } } // applicate the value to all unlocked panels excepted the changed one applyCorrectionValue(correctionValues); enableSliderListener(); } /// /// enable all sliders' onValueChanged action listener /// private void enableSliderListener() { for (int i = 0; i < panelNum; i++) { int tempIndex = i; singleLevelPanels[i].probabilitySlider.onValueChanged.AddListener((value) => OnSliderValueChange(value, tempIndex)); } } /// /// calculate the correction value to each panel,while the total value is not equal to 1 /// private (float[], int) reCalculateCorrectionValues(float[] correctionValues, int exceptedIndex, float value, float extraValue) { int remainCorrectionNum = 0; List reCorrectionIndex = new List(); int lastReCorrectionIndex = 0; float underZeroExtraValueAfterCorrected = 0; for (int i = 0; i < panelNum; ++i) { // if the panel is the changed one if (i == exceptedIndex) { correctionValues[i] = singleLevelPanels[i].ProbabilityValue - value; continue; } // if the panel is locked if (singleLevelPanels[i].IsLocked) { correctionValues[i] = 0; continue; } //if the probability value is 0 or the probability value is equal to the correction value if (singleLevelPanels[i].ProbabilityValue < 0 || singleLevelPanels[i].ProbabilityValue > 1) { correctionValues[i] = 0; } else if (singleLevelPanels[i].ProbabilityValue == correctionValues[i]) { underZeroExtraValueAfterCorrected += correctionValues[i]; } else if (singleLevelPanels[i].ProbabilityValue - correctionValues[i] < 0 || singleLevelPanels[i].ProbabilityValue - correctionValues[i] > 1) { underZeroExtraValueAfterCorrected += correctionValues[i]; correctionValues[i] = singleLevelPanels[i].ProbabilityValue; remainCorrectionNum++; } else { lastReCorrectionIndex = i; reCorrectionIndex.Add(i); } } foreach (int index in reCorrectionIndex) { float newAverageCorrectionValue = (extraValue - underZeroExtraValueAfterCorrected) / reCorrectionIndex.Count; if (index == lastReCorrectionIndex) { correctionValues[index] = extraValue - (newAverageCorrectionValue * (reCorrectionIndex.Count - 1)); if (singleLevelPanels[index].ProbabilityValue - correctionValues[index] < 0) { correctionValues[index] = singleLevelPanels[index].ProbabilityValue; remainCorrectionNum++; } } else { correctionValues[index] = newAverageCorrectionValue; if (singleLevelPanels[index].ProbabilityValue - correctionValues[index] < 0) { correctionValues[index] = singleLevelPanels[index].ProbabilityValue; remainCorrectionNum++; } } } return (correctionValues, remainCorrectionNum); } /// /// applicate correction value to each panel /// private void applyCorrectionValue(float[] correctionValues) { for (int i = 0; i < panelNum; i++) { if (singleLevelPanels[i].ProbabilityValue - correctionValues[i] < 0) { Debug.LogWarning("Probability value is less than 0"); Debug.Log(i); Debug.Log(singleLevelPanels[i].ProbabilityValue); Debug.Log(correctionValues[i]); } singleLevelPanels[i].SetProbability(singleLevelPanels[i].ProbabilityValue - correctionValues[i]); } } }