private void InitializeWavelengthSelector()
        {
            Debug.Log("[KSPI] - Setup Transmit Beams Configurations for " + part.partInfo.title);

            chooseField           = Fields["selectedBeamConfiguration"];
            chooseField.guiActive = CheatOptions.NonStrictAttachmentOrientation || (canSwitchWavelengthInFlight && BeamConfigurations.Count > 1);

            var chooseOptionEditor = chooseField.uiControlEditor as UI_ChooseOption;
            var chooseOptionFlight = chooseField.uiControlFlight as UI_ChooseOption;

            var names = BeamConfigurations.Select(m => m.beamWaveName).ToArray();

            chooseOptionEditor.options = names;
            chooseOptionFlight.options = names;

            if (wavelength != 0)
            {
                activeConfiguration = BeamConfigurations.SingleOrDefault(m => m.wavelength == wavelength);
                if (activeConfiguration != null)
                {
                    selectedBeamConfiguration = BeamConfigurations.IndexOf(activeConfiguration);
                }
            }

            UpdateFromGUI(chooseField, selectedBeamConfiguration);

            // connect on change event
            chooseOptionEditor.onFieldChanged = UpdateFromGUI;
            chooseOptionFlight.onFieldChanged = UpdateFromGUI;
        }
        public override void OnLoad(ConfigNode node)
        {
            beamConfigurationNodes = node.GetNodes("BeamConfiguration");

            if (beamConfigurationNodes.Any())
            {
                Debug.Log("[KSPI]: OnLoad Found " + beamConfigurationNodes.Count() + " BeamConfigurations");
            }

            var inlineConfigurations = new  List <BeamConfiguration>();

            foreach (var beamConfigurationNode in beamConfigurationNodes)
            {
                var beamConfiguration = new BeamConfiguration
                {
                    beamWaveName = beamConfigurationNode.GetValue("beamWaveName"),
                    wavelength   = ReadDouble(beamConfigurationNode, "wavelength", 1),
                    atmosphericAbsorptionPercentage = ReadDouble(beamConfigurationNode, "atmosphericAbsorptionPercentage", 100),
                    waterAbsorptionPercentage       = ReadDouble(beamConfigurationNode, "waterAbsorptionPercentage", 100),

                    techRequirement0      = beamConfigurationNode.HasValue("techRequirement0")? beamConfigurationNode.GetValue("techRequirement0"): null,
                    techRequirement1      = beamConfigurationNode.HasValue("techRequirement1")? beamConfigurationNode.GetValue("techRequirement1"): null,
                    techRequirement2      = beamConfigurationNode.HasValue("techRequirement2")? beamConfigurationNode.GetValue("techRequirement2"): null,
                    techRequirement3      = beamConfigurationNode.HasValue("techRequirement3")? beamConfigurationNode.GetValue("techRequirement3"): null,
                    efficiencyPercentage0 = beamConfigurationNode.HasValue("efficiencyPercentage0")? double.Parse(beamConfigurationNode.GetValue("efficiencyPercentage0")): 0,
                    efficiencyPercentage1 = beamConfigurationNode.HasValue("efficiencyPercentage1")? double.Parse(beamConfigurationNode.GetValue("efficiencyPercentage1")): 0,
                    efficiencyPercentage2 = beamConfigurationNode.HasValue("efficiencyPercentage2")? double.Parse(beamConfigurationNode.GetValue("efficiencyPercentage2")): 0,
                    efficiencyPercentage3 = beamConfigurationNode.HasValue("efficiencyPercentage3")? double.Parse(beamConfigurationNode.GetValue("efficiencyPercentage3")): 0
                };

                inlineConfigurations.Add(beamConfiguration);
            }

            _inlineConfigurations = inlineConfigurations.OrderByDescending(m => m.wavelength).ToList();
        }
        private void LoadInitialConfiguration()
        {
            isLoaded = true;

            var currentWavelength = wavelength != 0 ? wavelength : 1;

            //Debug.Log("[KSP Interstellar] UpdateFromGUI initialize initial beam configuration with wavelength target " + currentWavelength);

            // find wavelength closes to target wavelength
            activeConfiguration       = BeamConfigurations.FirstOrDefault();
            selectedBeamConfiguration = 0;
            var lowestWavelengthDifference = Math.Abs(currentWavelength - activeConfiguration.wavelength);

            if (BeamConfigurations.Count > 1)
            {
                foreach (var currentConfig in BeamConfigurations)
                {
                    var configWaveLengthDifference = Math.Abs(currentWavelength - currentConfig.wavelength);
                    if (configWaveLengthDifference < lowestWavelengthDifference)
                    {
                        activeConfiguration        = currentConfig;
                        lowestWavelengthDifference = configWaveLengthDifference;
                        selectedBeamConfiguration  = BeamConfigurations.IndexOf(currentConfig);
                    }
                }
            }
        }
Example #4
0
        private void LoadInitialConfiguration()
        {
            isLoaded = true;

            var currentWavelength = wavelength != 0 ? wavelength : 1;

            activeConfiguration = BeamConfigurations.FirstOrDefault();

            selectedBeamConfiguration = 0;

            if (BeamConfigurations.Count <= 1 || activeConfiguration == null)
            {
                return;
            }

            var lowestWavelengthDifference = Math.Abs(currentWavelength - activeConfiguration.wavelength);

            foreach (var currentConfig in BeamConfigurations)
            {
                var configWaveLengthDifference = Math.Abs(currentWavelength - currentConfig.wavelength);

                if (!(configWaveLengthDifference < lowestWavelengthDifference))
                {
                    continue;
                }

                activeConfiguration        = currentConfig;
                lowestWavelengthDifference = configWaveLengthDifference;
                selectedBeamConfiguration  = BeamConfigurations.IndexOf(currentConfig);
            }
        }
Example #5
0
        private void InitializeWavelengthSelector()
        {
            Debug.Log("[KSPI]: Setup Transmit Beams Configurations for " + part.partInfo.title);

            chooseField           = Fields["selectedBeamConfiguration"];
            chooseField.guiActive = CheatOptions.NonStrictAttachmentOrientation || (canSwitchWavelengthInFlight && BeamConfigurations.Count > 1);

            var chooseOptionEditor = chooseField.uiControlEditor as UI_ChooseOption;
            var chooseOptionFlight = chooseField.uiControlFlight as UI_ChooseOption;

            if (chooseOptionEditor == null)
            {
                return;
            }

            if (chooseOptionFlight == null)
            {
                return;
            }

            var names = BeamConfigurations.Select(m => m.beamWaveName).ToArray();

            chooseOptionEditor.options = names;
            chooseOptionFlight.options = names;

            if (!string.IsNullOrEmpty(beamWaveName))
            {
                activeConfiguration = BeamConfigurations.FirstOrDefault(m => String.Equals(m.beamWaveName, beamWaveName, StringComparison.CurrentCultureIgnoreCase));
                if (activeConfiguration != null)
                {
                    selectedBeamConfiguration = BeamConfigurations.IndexOf(activeConfiguration);
                    wavelength = activeConfiguration.wavelength;
                    return;
                }
            }

            if (wavelength != 0)
            {
                // find first wavelength with equal or shorter wavelength
                activeConfiguration = BeamConfigurations.FirstOrDefault(m => m.wavelength <= wavelength);

                if (activeConfiguration == null)
                {
                    activeConfiguration = selectedBeamConfiguration < BeamConfigurations.Count ? BeamConfigurations[selectedBeamConfiguration] : BeamConfigurations.FirstOrDefault();
                }

                if (activeConfiguration != null)
                {
                    selectedBeamConfiguration = BeamConfigurations.IndexOf(activeConfiguration);
                }
            }

            UpdateFromGui(chooseField, selectedBeamConfiguration);

            // connect on change event
            chooseOptionEditor.onFieldChanged = UpdateFromGui;
            chooseOptionFlight.onFieldChanged = UpdateFromGui;
        }
        /// <summary>
        /// Called whenever chooseOption is changed by user
        /// </summary>
        /// <param name="field"></param>
        /// <param name="oldFieldValueObj"></param>
        private void UpdateFromGUI(BaseField field, object oldFieldValueObj)
        {
            Debug.Log("[KSPI]: BeamGenerator UpdateFromGUI called");

            if (!BeamConfigurations.Any())
            {
                Debug.LogWarning("[KSPI]: BeamGenerator UpdateFromGUI no BeamConfigurations found");
                return;
            }

            if (isLoaded == false)
            {
                LoadInitialConfiguration();
            }
            else
            {
                if (selectedBeamConfiguration < BeamConfigurations.Count)
                {
                    activeConfiguration = BeamConfigurations[selectedBeamConfiguration];
                }
                else
                {
                    Debug.LogWarning("[KSPI]: selectedBeamConfiguration < " + BeamConfigurations.Count + ", selecting last");
                    selectedBeamConfiguration = BeamConfigurations.Count - 1;
                    activeConfiguration       = BeamConfigurations.Last();
                }
            }

            if (activeConfiguration == null)
            {
                Debug.Log("[KSPI]: UpdateFromGUI no activeConfiguration found");
                return;
            }

            beamWaveName   = activeConfiguration.beamWaveName;
            wavelength     = activeConfiguration.wavelength;
            wavelengthText = WavelenthToText(wavelength);
            atmosphericAbsorptionPercentage = activeConfiguration.atmosphericAbsorptionPercentage;
            waterAbsorptionPercentage       = activeConfiguration.waterAbsorptionPercentage;

            UpdateEfficiencyPercentage();

            // synchronize with reciever;
            if (transmitter != null && transmitter.part_receiver != null)
            {
                Debug.Log("[KSPI]: Called SetActiveBandwidthConfigurationByWaveLength with wavelength " + wavelength);
                transmitter.part_receiver.SetActiveBandwidthConfigurationByWaveLength(wavelength);
            }
            //else
            //{
            //    Debug.Log("[KSPI]: No transmitter found ");
            //}
        }
        private void UpdateFromGUI(BaseField field, object oldFieldValueObj)
        {
            //Debug.Log("[KSP Interstellar] UpdateFromGUI is called with " + selectedBeamConfiguration);

            if (!BeamConfigurations.Any())
            {
                //Debug.Log("[KSP Interstellar] UpdateFromGUI no BeamConfigurations found");
                return;
            }

            if (isLoaded == false)
            {
                LoadInitialConfiguration();
            }
            else
            {
                if (selectedBeamConfiguration < BeamConfigurations.Count)
                {
                    //Debug.Log("[KSP Interstellar] UpdateFromGUI " + selectedBeamConfiguration + " < orderedBeamGenerators.Count");
                    activeConfiguration = BeamConfigurations[selectedBeamConfiguration];
                }
                else
                {
                    //Debug.Log("[KSP Interstellar] UpdateFromGUI " + selectedBeamConfiguration + " >= orderedBeamGenerators.Count");
                    selectedBeamConfiguration = BeamConfigurations.Count - 1;
                    activeConfiguration       = BeamConfigurations.Last();
                }
            }

            if (activeConfiguration == null)
            {
                Debug.Log("[KSP Interstellar] UpdateFromGUI no activeConfiguration found");
                return;
            }

            beamWaveName = activeConfiguration.beamWaveName;
            //Debug.Log("[KSP Interstellar] UpdateFromGUI set beamWaveName to " + beamWaveName);

            wavelength = activeConfiguration.wavelength;
            //Debug.Log("[KSP Interstellar] UpdateFromGUI set wavelenth to " + wavelength);

            atmosphericAbsorptionPercentage = activeConfiguration.atmosphericAbsorptionPercentage;
            //Debug.Log("[KSP Interstellar] UpdateFromGUI set atmosphericAbsorptionPercentage to " + atmosphericAbsorptionPercentage);

            waterAbsorptionPercentage = activeConfiguration.waterAbsorptionPercentage;
            //Debug.Log("[KSP Interstellar] UpdateFromGUI set waterAbsorptionPercentage to " + waterAbsorptionPercentage);

            UpdateEfficiencyPercentage();
        }
        private void LoadInitialConfiguration()
        {
            isLoaded = true;

            if (!string.IsNullOrEmpty(beamWaveName))
            {
                activeConfiguration = BeamConfigurations.FirstOrDefault(m => String.Equals(m.beamWaveName, beamWaveName, StringComparison.CurrentCultureIgnoreCase));
                if (activeConfiguration != null)
                {
                    selectedBeamConfiguration = BeamConfigurations.IndexOf(activeConfiguration);
                    wavelength = activeConfiguration.wavelength;
                    return;
                }
            }

            var currentWavelength = wavelength != 0 ? wavelength : 1;

            activeConfiguration = BeamConfigurations.FirstOrDefault();

            selectedBeamConfiguration = 0;

            if (BeamConfigurations.Count <= 1 || activeConfiguration == null)
            {
                return;
            }

            var lowestWavelengthDifference = Math.Abs(currentWavelength - activeConfiguration.wavelength);

            foreach (var currentConfig in BeamConfigurations)
            {
                var configWaveLengthDifference = Math.Abs(currentWavelength - currentConfig.wavelength);

                if (!(configWaveLengthDifference < lowestWavelengthDifference))
                {
                    continue;
                }

                activeConfiguration        = currentConfig;
                lowestWavelengthDifference = configWaveLengthDifference;
                selectedBeamConfiguration  = BeamConfigurations.IndexOf(currentConfig);
            }
        }
        private void UpdateFromGUI(BaseField field, object oldFieldValueObj)
        {
            if (!BeamConfigurations.Any())
            {
                return;
            }

            if (isLoaded == false)
            {
                LoadInitialConfiguration();
            }
            else
            {
                if (selectedBeamConfiguration < BeamConfigurations.Count)
                {
                    activeConfiguration = BeamConfigurations[selectedBeamConfiguration];
                }
                else
                {
                    Debug.LogWarning("[KSPI] - selectedBeamConfiguration < BeamConfigurations.Count, selecting last");
                    selectedBeamConfiguration = BeamConfigurations.Count - 1;
                    activeConfiguration       = BeamConfigurations.Last();
                }
            }

            if (activeConfiguration == null)
            {
                Debug.Log("[KSPI] - UpdateFromGUI no activeConfiguration found");
                return;
            }

            beamWaveName   = activeConfiguration.beamWaveName;
            wavelength     = activeConfiguration.wavelength;
            wavelengthText = WavelenthToText(wavelength);
            atmosphericAbsorptionPercentage = activeConfiguration.atmosphericAbsorptionPercentage;
            waterAbsorptionPercentage       = activeConfiguration.waterAbsorptionPercentage;

            UpdateEfficiencyPercentage();
        }