public void OnAddForceButtonClick() { // Check if more than one force has been selected. if (int.Parse(numberOfTotalForces.text) > 1) { // Create an array which will only store defined values. float[][] forceDataStoredDefined = new float[99][]; foreach (float[] set in forceDataStored) { // Check if values were actually saved or not. if (set != null) { // Find a not yet defined value in the array. int indexToSaveOn = Array.IndexOf(forceDataStoredDefined, null); // In case the array is full - logically speaking, it should never be full for all values to be added. if (indexToSaveOn != -1) { forceDataStoredDefined[indexToSaveOn] = set; } } } // Check if more than one value was defined. if (Array.IndexOf(forceDataStoredDefined, null) > 1) { // Create an array of forces. Force[] arrayOfForce = new Force[99]; foreach (float[] forceValue in forceDataStoredDefined) { // Checks if the value is null and all forces have beem created. if (forceValue == null) { break; } float x = forceValue[0]; float y = forceValue[1]; float z = forceValue[2]; // Find a not yet defined value in the array. int indexToSaveOn = Array.IndexOf(arrayOfForce, null); // In case the array is full - logically speaking, it should never be full for all values to be added. if (indexToSaveOn != -1) { // Create the force and store. arrayOfForce[indexToSaveOn] = new Force(x, y, z); } } // Check if more than one value was defined. if (Array.IndexOf(arrayOfForce, null) > 1) { // Calculated the Resultant Force. Force resultForce = ForceEngine.AddArrayOfForce(arrayOfForce); // Turn on the result panel. resultForcePanel.SetActive(true); // Convert the values to string (because text only displays strings) and set them to display. inputIResult.text = resultForce.x.ToString(); inputJResult.text = resultForce.y.ToString(); inputKResult.text = resultForce.z.ToString(); inputMagnitudeResult.text = resultForce.magnitude.ToString(); inputDirectionResult.text = resultForce.direction2D.ToString(); // Check if vector needs to be displayed or not. if (resultForce.magnitude > 0) { // Turn on the graph vector. graphVector.SetActive(true); } else { // Turn off the graph vector because it isn't valuable here. graphVector.SetActive(false); } // Set the graph vector's directions. graphVector.transform.rotation = Quaternion.Euler(0, 0, resultForce.direction2D); } } } }
public void OnValueChangedOfAnyForceDataInputField() { // Create an array with all the force data input fields. TMP_InputField[] forceDataInputArray = new TMP_InputField[] { inputI, inputJ, inputK, inputMagnitude, inputDirection }; // Create two seperate arrays for optional inputs. TMP_InputField[] forceDataInputArrayOptionOne = new TMP_InputField[] { inputI, inputJ, inputK }; TMP_InputField[] forceDataInputArrayOptionTwo = new TMP_InputField[] { inputMagnitude, inputDirection }; foreach (TMP_InputField input in forceDataInputArrayOptionOne) { // Make sure the field is empty and that the value was changed. if (input.text != "" && ForceDataInputFieldValueWasChanged(input, Array.IndexOf(forceDataInputArray, input))) { // Resolve the components. float i = float.Parse(forceDataInputArrayOptionOne[0].text); float j = float.Parse(forceDataInputArrayOptionOne[1].text); float k = float.Parse(forceDataInputArrayOptionOne[2].text); // Create a component array. float[] component = new float[] { i, j, k }; // Get the calculated force. Force calculatedForce = ForceEngine.GetForceGivenComponent(component); // Convert the values to string (because text only displays strings) and set them to display. inputMagnitude.text = calculatedForce.magnitude.ToString(); inputDirection.text = calculatedForce.direction2D.ToString(); // Check if vector needs to be displayed or not. if (calculatedForce.magnitude > 0) { // Turn on the graph vector. graphVector.SetActive(true); } else { // Turn off the graph vector because it isn't valuable here. graphVector.SetActive(false); } // Set the graph vector's directions. graphVector.transform.rotation = Quaternion.Euler(0, 0, calculatedForce.direction2D); // Save all the values. SaveValueOfForceData(forceDataInputArray); return; } } foreach (TMP_InputField input in forceDataInputArrayOptionTwo) { // Make sure the field is empty and that the value was changed. if (input.text != "" && ForceDataInputFieldValueWasChanged(input, Array.IndexOf(forceDataInputArray, input))) { // Resolve the magnitude and direction. float magnitude = float.Parse(forceDataInputArrayOptionTwo[0].text); float direction = float.Parse(forceDataInputArrayOptionTwo[1].text); // If force is given with negative value on the horizontal just via magnitude. if (Math.Sign(magnitude) == -1 && direction == 0) { direction = 180; } // Get the calculated force. Force calculatedForce = ForceEngine.GetForceGivenMagnitudeAndDirection(magnitude, direction); // Convert the values to string (because text only displays strings) and set them to display. inputI.text = calculatedForce.x.ToString(); inputJ.text = calculatedForce.y.ToString(); inputK.text = calculatedForce.z.ToString(); // Check if vector needs to be displayed or not. if (magnitude > 0) { // Turn on the graph vector. graphVector.SetActive(true); } else { // Turn off the graph vector because it isn't valuable here. graphVector.SetActive(false); } // Set the graph vector's directions. graphVector.transform.rotation = Quaternion.Euler(0, 0, direction); // Save all the values. SaveValueOfForceData(forceDataInputArray); return; } } }