Exemple #1
0
        internal static ResourceMonitorDef FromConfigNode(ConfigNode configNode)
        {
            ResourceMonitorDef rmd = new ResourceMonitorDef();

            rmd.resname    = configNode.GetValue(RESNAME);
            rmd.percentage = float.Parse(configNode.GetValue(PERCENTAGE));
            rmd.minAmt     = double.Parse(configNode.GetValue(MINAMT));
            rmd.alarm      = configNode.GetValue(ALARM);
            rmd.Enabled    = bool.Parse(configNode.GetValue(ENABLED));
            rmd.SetResource(rmd.resname);
            rmd.InitSoundplayer();
            return(rmd);
        }
        void LoadDefaults()
        {
            Log.Info("LoadDefaults, file: " + dataFile + " exists: " + File.Exists(dataFile).ToString());
            if (!File.Exists(dataFile))
            {
                return;
            }
            initialDefaultRMD = new List <ResourceMonitorDef>();
            ConfigNode defaults = ConfigNode.Load(dataFile);
            ConfigNode r        = defaults.GetNode(DEF_NODENAME);

            if (r.HasNode(DEF_DEFAULT_NODENAME))
            {
                ConfigNode resAlertnode = r.GetNode(DEF_DEFAULT_NODENAME);
                var        resNodes     = resAlertnode.GetNodes(RESNODE);
                foreach (var resNode in resNodes)
                {
                    ResourceMonitorDef defaultResource = new ResourceMonitorDef();

                    resNode.TryGetValue(VAL_RESNAME, ref defaultResource.resname);
                    if (!defaultResource.SetResource(defaultResource.resname))
                    {
                        Log.Error("defaultResource.resname (" + defaultResource.resname + ") not set correctly");
                    }
                    resNode.TryGetValue(VAL_MONITOR, ref defaultResource.monitorByPercentage);
                    resNode.TryGetValue(VAL_PERCENT, ref defaultResource.percentage);
                    resNode.TryGetValue(VAL_AMT, ref defaultResource.minAmt);
                    resNode.TryGetValue(VAL_ALARM, ref defaultResource.alarm);
                    resNode.TryGetValue(VAL_ENABLED, ref defaultResource.Enabled);

                    initialDefaultRMD.Add(defaultResource);

                    Log.Info("Default resource loaded: " + defaultResource.resname);
                }
            }
            Scenario_Module.ResetDefaultRMD();
            if (r.HasNode(DEF_COMMON_NODENAME))
            {
                ConfigNode common        = r.GetNode(DEF_COMMON_NODENAME);
                string[]   resourceNodes = common.GetValues("resource");
                if (resourceNodes.Length > 0)
                {
                    commonResources = resourceNodes;
                    foreach (var rrr in commonResources)
                    {
                        Log.Info("CommonResource loaded: " + rrr);
                    }
                }
            }
        }
Exemple #3
0
 public ResourceMonitorDef(ResourceMonitorDef r)
 {
     Init();
     this.resname             = r.resname;
     this.monitorByPercentage = r.monitorByPercentage;
     this.minAmt  = r.minAmt;
     this.alarm   = r.alarm;
     this.Enabled = r.Enabled;
     this.prd     = r.prd;
     if (!InitSoundplayer())
     {
         Main.Log.Error("Soundplayer can't be initialized");
     }
 }
        public override void VSMLoad(ConfigNode node)
        {
            if (NotA_Vessel)
            {
                return;
            }

            if (!(vessel.missionTime == 0 && Vessel.isActiveVessel && HighLogic.CurrentGame.Parameters.CustomParams <RM_1>().applyCommonToAll))
            {
                rmdList.Clear();
            }

            if (!initted)
            {
                // rmdList.Add(new ResourceMonitorDef("ElectricCharge", "Alarm1", 5f, 1));

                initted = true;
            }

            try
            {
                Main.Log.Info("VSMLoad, configNode: " + node.ToString());
                ConfigNode alertMonitorsNode = null;
                alertMonitorsNode = node;
                double time = DateTime.Now.Ticks;

                var nodes = alertMonitorsNode.GetNodes(Main.RESNODE);
                if (nodes != null)
                {
                    Main.Log.Info("VSMLoad, nodes.Count: " + nodes.Count());
                    foreach (var configNode in nodes)
                    {
                        ResourceMonitorDef rmd = ResourceMonitorDef.FromConfigNode(configNode);
                        rmdList.Add(rmd);
                    }
                }

                time = (DateTime.Now.Ticks - time) / TimeSpan.TicksPerSecond;
                Main.Log.Info("retrieved ScenarioModule in " + time.ToString("0.000s"));
            }
            catch (Exception e)
            {
                Main.Log.Error("[KRnD] OnLoad(): " + e.ToString());
            }
        }
        public override void OnLoad(ConfigNode node)
        {
            try
            {
                defaultRMD.Clear();
                ConfigNode resourceMonitorsNode = null;
                if (node.TryGetNode(Main.MODNAME, ref resourceMonitorsNode))
                {
                    double time = DateTime.Now.Ticks;

                    var nodes = resourceMonitorsNode.GetNodes(Main.RESNODE);
                    if (nodes != null)
                    {
                        foreach (var configNode in nodes)
                        {
                            ResourceMonitorDef rmd = ResourceMonitorDef.FromConfigNode(configNode);
                            defaultRMD.Add(rmd);
                        }
                    }
                    if (node.TryGetNode(Main.GUIName, ref resourceMonitorsNode))
                    {
                        if (resourceMonitorsNode.HasValue(X))
                        {
                            ResourceAlertWindow.windowPosition.x = (float)Double.Parse(resourceMonitorsNode.GetValue(X));
                        }
                        if (resourceMonitorsNode.HasValue(Y))
                        {
                            ResourceAlertWindow.windowPosition.y = (float)Double.Parse(resourceMonitorsNode.GetValue(Y));
                        }

                        if (resourceMonitorsNode.HasValue(soundX))
                        {
                            ResourceAlertWindow.soundWindowPosition.x = (float)Double.Parse(resourceMonitorsNode.GetValue(soundX));
                        }
                        if (resourceMonitorsNode.HasValue(soundY))
                        {
                            ResourceAlertWindow.soundWindowPosition.y = (float)Double.Parse(resourceMonitorsNode.GetValue(soundY));
                        }


                        if (resourceMonitorsNode.HasValue(resourceX))
                        {
                            ResourceAlertWindow.resourceWindowPosition.x = (float)Double.Parse(resourceMonitorsNode.GetValue(resourceX));
                        }
                        if (resourceMonitorsNode.HasValue(resourceY))
                        {
                            ResourceAlertWindow.resourceWindowPosition.y = (float)Double.Parse(resourceMonitorsNode.GetValue(resourceY));
                        }



                        ResourceAlertWindow.windowPosition.height = Main.HEIGHT;
                        ResourceAlertWindow.windowPosition.width  = Main.WIDTH;

                        ResourceAlertWindow.soundWindowPosition.height = Main.HEIGHT;
                        ResourceAlertWindow.soundWindowPosition.width  = Main.SEL_WIN_WIDTH;

                        ResourceAlertWindow.resourceWindowPosition.height = Main.HEIGHT;
                        ResourceAlertWindow.resourceWindowPosition.width  = Main.SEL_WIN_WIDTH;
                    }

                    time = (DateTime.Now.Ticks - time) / TimeSpan.TicksPerSecond;
                    Main.Log.Info("retrieved ScenarioModule in " + time.ToString("0.000s"));
                }
                else
                {
                    ResetDefaultRMD();
                }
            }
            catch (Exception e)
            {
                Debug.LogError("[KRnD] OnLoad(): " + e.ToString());
            }
        }
Exemple #6
0
        void ShowResourceGUIWindow(int windowId)
        {
            //Main.WIDTH = 583;
            Main.WIDTH = 613;

            previewWidth = 70;

            if (HighLogic.CurrentGame.Parameters.CustomParams <RM_2>().altSkin)
            {
                Main.WIDTH -= 10;
            }
            if (HighLogic.CurrentGame.Parameters.CustomParams <RM_2>().compact)
            {
                Main.WIDTH -= 10;
            }

            int resourceWidth = 120;

            if (HighLogic.CurrentGame.Parameters.CustomParams <RM_2>().useIconsOnly)
            {
                resourceWidth = 38;
                Main.WIDTH    = Main.WIDTH - 120 + 38; // (120 - 38) ;
            }
            if (HighLogic.CurrentGame.Parameters.CustomParams <RM_2>().useIconsAndText)
            {
                Main.WIDTH    += 40;
                resourceWidth += 40;
            }
            // Following to avoid flickering when moving the window with certain options specified
            if (Main.WIDTH + 40 != oldWidth)
            {
                oldWidth = Main.WIDTH + 40;
                ResourceAlertWindow.windowPosition.width = oldWidth;
            }
            toDel = null;
            Main.textFieldStyle.normal.textColor = Color.green;
            if (HighLogic.LoadedSceneIsFlight)
            {
                if (HaystackWrapper.HaystackAvailable && HighLogic.CurrentGame.Parameters.CustomParams <RM_1>().useHaystackIfAvailable)
                {
                    GUILayout.BeginHorizontal(); // GUILayout.Width(Main.WIDTH));
                                                 // GUILayout.FlexibleSpace();
                    if (GUILayout.Button("Haystack", btnStyle, GUILayout.Width(120)))
                    {
                        HaystackWrapper.ButtonClick();
                    }
                    GUILayout.FlexibleSpace();
                    if (HaystackWrapper.IsVisible)
                    {
                        GUILayout.FlexibleSpace();
                        if (GUILayout.Button("Get Vessel From Haystack", btnStyle, GUILayout.Width(240)))
                        {
                            Main.Log.Info("GetVesselFromHaystack");
                            if (HaystackWrapper.SelectedVessel != null)
                            {
                                Main.Log.Info("Vessel name: " + HaystackWrapper.SelectedVessel.vesselName);
                                JumpAndBackup.JumpToVessel(HaystackWrapper.SelectedVessel);
                            }
                        }
                    }
                    //GUILayout.FlexibleSpace();
                    GUILayout.EndHorizontal();
                }
            }
            else
            {
                GUILayout.BeginHorizontal(); // GUILayout.Width(Main.WIDTH));
                GUILayout.FlexibleSpace();
                GUILayout.Label("Common Resource Monitor Definition");
                GUILayout.FlexibleSpace();
                GUILayout.EndHorizontal();
            }
            GUILayout.BeginHorizontal();
            var b = GUILayout.Toggle(Main.common, "Only allow common resources for new monitors", toggleStyle);

            if (b != Main.common)
            {
                Main.common = b;
                GetResourceList();
            }
            GUILayout.EndHorizontal();
            GUILayout.BeginHorizontal(); // GUILayout.Width(Main.WIDTH));
            GUILayout.FlexibleSpace();
            if (GUILayout.Button("Add Resource Monitor", btnStyle))
            {
                workingRMD.Add(new ResourceMonitorDef("ElectricCharge", "Alarm1", 5f, 1));

                resourceIndex = workingRMD.Count - 1; //sets index to last one
            }
            if (workingRMD.Count == 0 && HighLogic.LoadedSceneIsFlight)
            {
                GUILayout.FlexibleSpace();
                if (GUILayout.Button("Apply Common Resource Monitor Settings", btnStyle))
                {
                    AddCommonResourceMonitors();
                    resourceIndex = workingRMD.Count - 1; //sets index to last one
                }
            }
            GUILayout.FlexibleSpace();
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            GUILayout.Space(25);
            if (HighLogic.CurrentGame.Parameters.CustomParams <RM_2>().useIconsOnly)
            {
                GUILayout.Label("Res", GUILayout.Width(resourceWidth));
            }
            else
            {
                GUILayout.Label("Resource", GUILayout.Width(resourceWidth));
            }
            GUILayout.Space(25);
            GUILayout.Label("Alarm Sound", GUILayout.Width(120));


            //GUILayout.Space(30);
            //GUILayout.Space(15);
            //GUILayout.Space(30);

            GUILayout.Space(75);
            GUILayout.Label("Amt or % ");
            //GUILayout.FlexibleSpace();
            GUILayout.EndHorizontal();



            GUILayout.BeginHorizontal();
            scrollPos = GUILayout.BeginScrollView(scrollPos, GUIStyle.none, GUI.skin.verticalScrollbar, GUILayout.Width(Main.WIDTH + 20));

            for (int i = 0; i < workingRMD.Count; i++)
            {
                GUILayout.BeginHorizontal(GUILayout.Width(Main.WIDTH + 20));
                resourceAlert = workingRMD[i];
                if (resourceAlert.Enabled)
                {
                    Main.buttonStyle.normal.textColor    = Color.green;
                    Main.toggleStyle.normal.textColor    = Color.green;
                    Main.textFieldStyle.normal.textColor = Color.green;
                }
                else
                {
                    Main.buttonStyle.normal.textColor    = Color.red;
                    Main.toggleStyle.normal.textColor    = Color.red;
                    Main.textFieldStyle.normal.textColor = Color.red;
                }
                int curResource = -1;


                // ****************************************************************************
                //                                                                            *
                GUIContent btn = null;

                if (!HighLogic.CurrentGame.Parameters.CustomParams <RM_2>().useTextOnly&& Main.icons.ContainsKey(resourceAlert.resname))
                {
                    if (HighLogic.CurrentGame.Parameters.CustomParams <RM_2>().useIconsOnly)
                    {
                        btn = new GUIContent(Main.icons[resourceAlert.resname]);
                    }
                    if (HighLogic.CurrentGame.Parameters.CustomParams <RM_2>().useIconsAndText)
                    {
                        btn = new GUIContent(PartResourceLibrary.Instance.resourceDefinitions[resourceAlert.resname].displayName,
                                             Main.icons[resourceAlert.resname]);
                    }
                }
                else
                {
                    btn = new GUIContent(PartResourceLibrary.Instance.resourceDefinitions[resourceAlert.resname].displayName);
                }


                if (GUILayout.Button(btn, btnStyle, GUILayout.Width(resourceWidth)))
                {
                    resourceSelectionWindow = !resourceSelectionWindow;
                    if (resourceSelectionWindow)
                    {
                        lastSelectedResource = resourceAlert.resname;
                    }
                }

                GUILayout.Space(10);
                //                                                                            *
                // ****************************************************************************

                // ****************************************************************************
                //                                                                            *
                if (GUILayout.Button(resourceAlert.alarm, btnStyle, GUILayout.Width(120)))
                {
                    soundSelectionWindow = !soundSelectionWindow;
                    if (soundSelectionWindow)
                    {
                        lastSelectedSoundFile = resourceAlert.alarm;
                    }
                }

                GUILayout.Space(10);
                //                                                                            *
                // ****************************************************************************
                if (GUILayout.Button(resourceAlert.monitorHighValue ? ">" : "<", GUILayout.Width(20)))
                {
                    resourceAlert.monitorHighValue = !resourceAlert.monitorHighValue;
                }
                GUILayout.Space(10);
                var newMonitorByPercentage = GUILayout.Toggle(resourceAlert.monitorByPercentage, "", toggleStyle, GUILayout.Width(30));
                //GUILayout.Label("%", labelStyle, GUILayout.Width(20));
                if (newMonitorByPercentage)
                {
                    if (HighLogic.LoadedSceneIsFlight && !resourceAlert.monitorByPercentage)
                    {
                        //if (resourceAlert.percentage == 0 && resourceAlert.minAmt > 0)
                        {
                            var resource = PartResourceLibrary.Instance.GetDefinition(resourceAlert.resname);
                            FlightGlobals.ActiveVessel.rootPart.GetConnectedResourceTotals(resource.id, out double amount, out double maxAmount, true);
                            resourceAlert.percentage = (float)(100f * resourceAlert.minAmt / maxAmount);
                        }
                        resourceAlert.minAmt = 0;
                    }
                    float f = Math.Max(1f, Math.Min(resourceAlert.percentage, 99));

                    f = GUILayout.HorizontalSlider(f, 1f, 99f, horSliderStyle, thumbStyle, GUILayout.Width(100));
                    resourceAlert.percentage = (int)f;
                    string p = f.ToString("F0") + "%";
                    GUILayout.Label(p, GUILayout.Width(30));
                }
                else
                {
                    var labelStyleAmt = new GUIStyle(labelStyle);
                    labelStyleAmt.alignment = TextAnchor.MiddleRight;
                    GUILayout.Label("Amt:", labelStyleAmt, GUILayout.Width(40));
                    if (HighLogic.LoadedSceneIsFlight && resourceAlert.monitorByPercentage)
                    {
                        if (!newMonitorByPercentage && resourceAlert.percentage > 0 && resourceAlert.minAmt == 0)
                        {
                            var resource = PartResourceLibrary.Instance.GetDefinition(resourceAlert.resname);
                            FlightGlobals.ActiveVessel.rootPart.GetConnectedResourceTotals(resource.id, out double amount, out double maxAmount, true);
                            resourceAlert.minAmt = resourceAlert.percentage / 100f * maxAmount;
                        }
                        resourceAlert.percentage = 0;
                    }
                    string f = resourceAlert.minAmt.ToString("F1");
                    f = GUILayout.TextField(f, textFieldStyle, GUILayout.Width(90));
                    if (double.TryParse(f, out double d))
                    {
                        resourceAlert.minAmt = Math.Max(0.1, d);
                    }
                }
                resourceAlert.monitorByPercentage = newMonitorByPercentage;
                resourceAlert.Enabled             = GUILayout.Toggle(resourceAlert.Enabled, "", toggleStyle, GUILayout.Width(30));
                GUILayout.Space(5);
                //if (HighLogic.CurrentGame.Parameters.CustomParams<AlertMonitor>().altSkin)
                //    GUILayout.Space(10);
                if (GUILayout.Button("X", redBtnStyle, GUILayout.Width(23)))
                {
                    toDel = resourceAlert;
                }
                GUILayout.Space(5);
                if (previewSound != curResource || !soundplayer.SoundPlaying())
                {
                    if (GUILayout.Button("Preview", btnStyle, GUILayout.Width(previewWidth)))
                    {
                        previewSound = curResource;
                        Main.Log.Info("Preview: " + Main.SOUND_DIR + resourceAlert.alarm);
                        soundplayer.LoadNewSound(Main.SOUND_DIR + resourceAlert.alarm, HighLogic.CurrentGame.Parameters.CustomParams <RM_1>().resourceAlertRepetition);
                        soundplayer.SetVolume(HighLogic.CurrentGame.Parameters.CustomParams <RM_2>().masterVolume);

                        soundplayer.PlaySound(true); //Plays sound
                    }
                }
                else
                {
                    if (GUILayout.Button("Stop", GUILayout.Width(previewWidth)))
                    {
                        soundplayer.StopSound();
                    }
                }
                GUILayout.EndHorizontal();
            }
            GUILayout.EndScrollView();
            GUILayout.FlexibleSpace();
            GUILayout.EndHorizontal();

            if (toDel != null)
            {
                workingRMD.Remove(toDel);
            }
            GUILayout.BeginHorizontal(); // GUILayout.Width(Main.WIDTH));

            if (HighLogic.LoadedSceneIsFlight)
            {
                if (GUILayout.Button("Close"))
                {
                    SaveSettings();
                    GuiOff();
                    soundSelectionWindow = false;
                }
            }
            else
            {
                if (GUILayout.Button("Save"))
                {
                    SaveSettings();
                    GuiOff();
                    soundSelectionWindow = false;
                }
                if (GUILayout.Button("Cancel"))
                {
                    GuiOff();
                    soundSelectionWindow = false;
                }
            }
            GUILayout.EndHorizontal();
            GUI.DragWindow();
        }