private void SaveCustomWarpRates()
        {
            if (!SettingsNode.HasNode("BetterTimeWarp"))
            {
                SettingsNode.AddNode("BetterTimeWarp");
            }

            ConfigNode node = SettingsNode.GetNode("BetterTimeWarp");

            node.SetValue("ScaleCameraSpeed", ScaleCameraSpeed.ToString(), true);
            node.SetValue("UseLosslessPhysics", UseLosslessPhysics.ToString(), true);
            node.SetValue("LosslessUpperThreshold", LosslessUpperThreshold.ToString(), true);

            node.RemoveNodes("CustomWarpRate");
            foreach (var rates in customWarps)
            {
                if (rates != StandardWarp && rates != StandardPhysWarp)
                {
                    ConfigNode rateNode = new ConfigNode("CustomWarpRate");
                    rateNode.AddValue("name", rates.Name);
                    for (int i = 1; i < 8; i++)
                    {
                        if (i < 4 || !rates.Physics)
                        {
                            rateNode.AddValue("warpRate" + i.ToString(), rates.Rates[i].ToString(TimeWarpRates.rateFmt(rates.Rates[i])));
                        }
                    }
#if false
                    rateNode.AddValue("warpRate1", rates.Rates[1]);
                    rateNode.AddValue("warpRate2", rates.Rates[2]);
                    rateNode.AddValue("warpRate3", rates.Rates[3]);
                    if (!rates.Physics)
                    {
                        rateNode.AddValue("warpRate4", rates.Rates[4]);
                        rateNode.AddValue("warpRate5", rates.Rates[5]);
                        rateNode.AddValue("warpRate6", rates.Rates[6]);
                        rateNode.AddValue("warpRate7", rates.Rates[7]);
                    }
#endif
                    rateNode.AddValue("physics", rates.Physics);
                    node.AddNode(rateNode);
                }
            }

            if (CurrentWarp == null)
            {
                CurrentWarp = StandardWarp;
                Log.Info("CurrentWarp set to StandardWarp  3");
            }
            if (CurrentPhysWarp == null)
            {
                CurrentPhysWarp = StandardPhysWarp;
            }

            node.SetValue("CurrentTimeWarp", CurrentWarp.Name, true);
            node.SetValue("CurrentPhysWarp", CurrentPhysWarp.Name, true);
        }
        public void SetWarpRates(TimeWarpRates rates, bool message = true)
        {
            if (TimeWarp.fetch != null)
            {
                if (TimeWarp.fetch.warpRates.Length == rates.Rates.Length && !rates.Physics)
                {
                    TimeWarp.fetch.warpRates = rates.Rates;
                    CurrentWarp = rates;

                    for (var i = 0; i < warpRates.Length; i++)
                    {
                        var r = warpRates[i];
                        if (r == rates)
                        {
                            currWarpIndex = i;
                        }
                    }

                    Log.Info("Set time warp rates to " + rates.ToString());
                    if (message)
                    {
                        ScreenMessages.PostScreenMessage(new ScreenMessage("New time warp rates: " + rates.Name, 3f, ScreenMessageStyle.UPPER_CENTER));
                    }
                    return;
                }
                else if (TimeWarp.fetch.physicsWarpRates.Length == rates.Rates.Length && rates.Physics)
                {
                    TimeWarp.fetch.physicsWarpRates = rates.Rates;
                    CurrentPhysWarp = rates;

                    for (var i = 0; i < physRates.Length; i++)
                    {
                        var r = physRates[i];
                        if (r == rates)
                        {
                            currPhysIndex = i;
                        }
                    }

                    print("[BetterTimeWarp]: Set time warp rates to " + rates.ToString());
                    if (message)
                    {
                        ScreenMessages.PostScreenMessage(new ScreenMessage("New physic warp rates: " + rates.Name, 3f, ScreenMessageStyle.UPPER_CENTER));
                    }
                    return;
                }
                return;
            }
            Debug.LogWarning("[BetterTimeWarp]: Failed to set warp rates");

#if false
            //reset it to standard in case of  failiure
            if (rates.Physics)
            {
                for (var i = 0; i < physRates.Length; i++)
                {
                    var r = physRates[i];
                    if (r == StandardPhysWarp)
                    {
                        currPhysIndex   = i;
                        CurrentPhysWarp = StandardPhysWarp;
                    }
                }
            }
            else
            {
                for (var i = 0; i < warpRates.Length; i++)
                {
                    var r = warpRates[i];
                    if (r == StandardWarp)
                    {
                        currWarpIndex = i;
                        CurrentWarp   = StandardWarp;
                        Log.Info("CurrentWarp set to StandardWarp  1");
                    }
                }
            }
#endif
        }
        public void TimeWarpWindow(int id)
        {
            GUILayout.BeginHorizontal();
            GUILayout.BeginVertical();

            scrollPos = GUILayout.BeginScrollView(scrollPos);

            editToggle = GUILayout.Toggle(editToggle, "Create", skin.button);
            selected   = GUILayout.SelectionGrid(selected, names, 1, smallButtonStyle);

            currentRates = customWarps.Find(r => r.Name == names[selected] ||
                                            (names[selected].Contains("<") && names[selected].Split('<', '>')[2] == r.Name)
                                            );

            if (currentRates == null)
            {
                currentRates = StandardWarp;
            }

            GUILayout.EndScrollView();

            GUILayout.EndVertical();

            GUILayout.BeginVertical();
            scrollPos2 = GUILayout.BeginScrollView(scrollPos2);

            if (editToggle)
            {
                bool canExport = true;

                GUILayout.BeginVertical(GUILayout.ExpandHeight(true));

                warpName = GUILayout.TextField(warpName);
                for (int i = 1; i < 8; i++)
                {
                    if (i < 4 || !physics)
                    {
                        w[i] = GUILayout.TextField(w[i]);
                    }
                }
#if false
                w1 = GUILayout.TextField(w1);
                w2 = GUILayout.TextField(w2);
                w3 = GUILayout.TextField(w3);
                if (!physics)
                {
                    w4 = GUILayout.TextField(w4);
                    w5 = GUILayout.TextField(w5);
                    w6 = GUILayout.TextField(w6);
                    w7 = GUILayout.TextField(w7);
                }
#endif
                physics = GUILayout.Toggle(physics, "Physics Warp?");

                GUILayout.EndVertical();

                if (GUILayout.Button("Save"))
                {
                    float[] rates;
                    if (physics)
                    {
                        rates = new float[4];
                    }
                    else
                    {
                        rates = new float[8];
                    }

                    rates[0] = 1f;

                    float pw;
                    for (int i = 1; i < 8; i++)
                    {
                        if (i < 4 || !physics)
                        {
                            if (float.TryParse(w[i], out pw))
                            {
                                rates[i] = pw;
                            }
                            else
                            {
                                canExport = false;
                            }
                        }
                    }
#if false
                    float pw1;
                    if (float.TryParse(w1, out pw1))
                    {
                        rates[1] = pw1;
                    }
                    else
                    {
                        canExport = false;
                    }
                    float pw2;
                    if (float.TryParse(w2, out pw2))
                    {
                        rates[2] = pw2;
                    }
                    else
                    {
                        canExport = false;
                    }
                    float pw3;
                    if (float.TryParse(w3, out pw3))
                    {
                        rates[3] = pw3;
                    }
                    else
                    {
                        canExport = false;
                    }
                    if (!physics)
                    {
                        float pw4;
                        if (float.TryParse(w4, out pw4))
                        {
                            rates[4] = pw4;
                        }
                        else
                        {
                            canExport = false;
                        }
                        float pw5;
                        if (float.TryParse(w5, out pw5))
                        {
                            rates[5] = pw5;
                        }
                        else
                        {
                            canExport = false;
                        }
                        float pw6;
                        if (float.TryParse(w6, out pw6))
                        {
                            rates[6] = pw6;
                        }
                        else
                        {
                            canExport = false;
                        }
                        float pw7;
                        if (float.TryParse(w7, out pw7))
                        {
                            rates[7] = pw7;
                        }
                        else
                        {
                            canExport = false;
                        }
                    }
#endif
                    if (canExport)
                    {
                        TimeWarpRates timeWarpRates = new TimeWarpRates(warpName, rates, physics);
                        customWarps.Add(timeWarpRates);
                        SaveCustomWarpRates();
                        editToggle = false;
                        //SetWarpRates (timeWarpRates);
                        warpName = "Name";
                        physics  = false;
                        InitW();
#if false
                        w[1] = "10";
                        w[2] = "100";
                        w[3] = "1000";
                        w[4] = "10000";
                        w[5] = "100000";
                        w[6] = "1000000";
                        w[7] = "10000000";
#endif
                    }
                    else
                    {
                        //PopupDialog.SpawnPopupDialog (new MultiOptionDialog("Cannot save because there are non-numbers in the editing fields", "Error", null), false, null);
                        var dialog = new MultiOptionDialog("btw1", "Cannot save because there are non-numbers in the editing fields", "Error", HighLogic.UISkin, new DialogGUIBase[] {
                            new DialogGUIButton("OK", () => { })
                        });
                        PopupDialog.SpawnPopupDialog(new Vector2(0.5f, 0.5f), new Vector2(0.5f, 0.5f), dialog, false, HighLogic.UISkin, true);
                    }
                    //save the settings, so if they have been regenerated, it exsists and wont cause errors
                    BetterTimeWarp.SettingsNode.Save(BetterTimeWarpInitializer.BTW_CFG_FILE);
                }
                if (GUILayout.Button("Cancel", smallButtonStyle))
                {
                    editToggle = false;
                    warpName   = "Name";
                    physics    = false;
                    InitW();
#if false
                    w[1] = "10";
                    w[2] = "100";
                    w[3] = "1000";
                    w[4] = "10000";
                    w[5] = "100000";
                    w[6] = "1000000";
                    w[7] = "10000000";
#endif
                }
            }
            else
            {
                GUILayout.BeginVertical(GUILayout.ExpandHeight(true));
                for (int i = 1; i <= 8; i++)
                {
                    if (i <= 4 || !currentRates.Physics)
                    {
                        var f = currentRates.Rates[i - 1];
                        GUILayout.Label("<b><color=lime>" + i.ToString() + ":</color></b> <color=white>" + f.ToString(TimeWarpRates.rateFmt(f)) + "x</color>");
                    }
                }
#if false
                GUILayout.Label("<b><color=lime>1:</color></b> <color=white>" + currentRates.Rates[0].ToString(TimeWarpRates.rateFmt(currentRates.Rates[0]) + "x</color>");
                                GUILayout.Label("<b><color=lime>2:</color></b> <color=white>" + currentRates.Rates[1].ToString("N0") + "x</color>");
                                GUILayout.Label("<b><color=lime>3:</color></b> <color=white>" + currentRates.Rates[2].ToString("N0") + "x</color>");
                                GUILayout.Label("<b><color=lime>4:</color></b> <color=white>" + currentRates.Rates[3].ToString("N0") + "x</color>");
                                if (!currentRates.Physics)
                {
                    GUILayout.Label("<b><color=lime>5:</color></b> <color=white>" + currentRates.Rates[4].ToString("N0") + "x</color>");
                    GUILayout.Label("<b><color=lime>6:</color></b> <color=white>" + currentRates.Rates[5].ToString("N0") + "x</color>");
                    GUILayout.Label("<b><color=lime>7:</color></b> <color=white>" + currentRates.Rates[6].ToString("N0") + "x</color>");
                    GUILayout.Label("<b><color=lime>8:</color></b> <color=white>" + currentRates.Rates[7].ToString("N0") + "x</color>");
                }
#endif
                GUILayout.EndVertical();

                GUILayout.Space(15f);
                if (GUILayout.Button("Select"))
                {
                    SetWarpRates(currentRates);
                }
                if (currentRates != StandardWarp && currentRates != StandardPhysWarp && GUILayout.Button("Edit", smallButtonStyle))
                {
                    if (currentRates != StandardWarp && currentRates != StandardPhysWarp)
                    {
                        if (!currentRates.Physics)
                        {
                            SetWarpRates(StandardWarp, false);
                        }
                        else
                        {
                            SetWarpRates(StandardPhysWarp, false);
                        }
                        customWarps.Remove(currentRates);
                        editToggle = true;
                        warpName   = currentRates.Name;
                        physics    = currentRates.Physics;

                        for (int i = 1; i < 8; i++)
                        {
                            if (i < 4 || !physics)
                            {
                                w[i] = currentRates.Rates[i].ToString(TimeWarpRates.rateFmt(currentRates.Rates[i]));
                            }
                        }
#if false
                        w1 = currentRates.Rates[1].ToString(TimeWarpRates.rateFmt(currentRates.Rates[1]));
                        w2 = currentRates.Rates[2].ToString(TimeWarpRates.rateFmt(currentRates.Rates[2]));
                        w3 = currentRates.Rates[3].ToString(TimeWarpRates.rateFmt(currentRates.Rates[3]));
                        if (!physics)
                        {
                            w4 = currentRates.Rates[4].ToString(TimeWarpRates.rateFmt(currentRates.Rates[4]));
                            w5 = currentRates.Rates[5].ToString(TimeWarpRates.rateFmt(currentRates.Rates[5]));
                            w6 = currentRates.Rates[6].ToString(TimeWarpRates.rateFmt(currentRates.Rates[6]));
                            w7 = currentRates.Rates[7].ToString(TimeWarpRates.rateFmt(currentRates.Rates[7]));
                        }
#endif
                        selected = 0;
                    }
                    else
                    {
                        //PopupDialog.SpawnPopupDialog (new MultiOptionDialog("Cannot edit standard warp rates", "Better Time Warp", null), true, null);

                        var dialog = new MultiOptionDialog("btw2", "Cannot edit standard warp rates", "Better Time Warp", HighLogic.UISkin, new DialogGUIBase[] {
                            new DialogGUIButton("OK", () => { })
                        });
                        PopupDialog.SpawnPopupDialog(new Vector2(0.5f, 0.5f), new Vector2(0.5f, 0.5f), dialog, false, HighLogic.UISkin, true);
                    }
                }
                if (currentRates != StandardWarp && currentRates != StandardPhysWarp && GUILayout.Button("Delete", smallButtonStyle))
                {
                    if (currentRates != StandardWarp && currentRates != StandardPhysWarp)
                    {
                        customWarps.Remove(currentRates);
                        selected = 0;
                        SaveCustomWarpRates();
                        //	PopupDialog.SpawnPopupDialog (new MultiOptionDialog("Deleted " + currentRates.Name + " time warp rates", "Better Time Warp", null), true, null);



                        var dialog = new MultiOptionDialog("btw3", "Deleted " + currentRates.Name + " time warp rates", "Better Time Warp", HighLogic.UISkin, new DialogGUIBase[] {
                            new DialogGUIButton("OK", () => {
                                // winState = winContent.close;
                            })
                        });
                        PopupDialog.SpawnPopupDialog(new Vector2(0.5f, 0.5f), new Vector2(0.5f, 0.5f), dialog, false, HighLogic.UISkin, true);
                        // winState = winContent.dialog;



                        SetWarpRates(StandardWarp, false);
                    }
                    else
                    {
                        //PopupDialog.SpawnPopupDialog (new MultiOptionDialog("Cannot delete standard warp rates", "Better Time Warp", null), true, null);

                        var dialog = new MultiOptionDialog("btw4", "Cannot delete standard warp rates", "Better Time Warp", HighLogic.UISkin, new DialogGUIBase[] {
                            new DialogGUIButton("OK", () => { })
                        });
                        PopupDialog.SpawnPopupDialog(new Vector2(0.5f, 0.5f), new Vector2(0.5f, 0.5f), dialog, false, HighLogic.UISkin, true);
                    }
                }
            }
            GUILayout.EndScrollView();
            GUILayout.EndVertical();

            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            if (GUILayout.Button("Simple", GUILayout.ExpandWidth(true)))
            {
                advWindowOpen = false;
            }
            showPhysicsSettings = GUILayout.Toggle(showPhysicsSettings, "<color=lime>Physics Settings</color>", skin.button, GUILayout.ExpandWidth(true));
            GUILayout.EndHorizontal();
            if (!lockWindow())
            {
                GUI.DragWindow();
            }
        }
        private void LoadCustomWarpRates()
        {
            if (BetterTimeWarp.SettingsNode == null)
            {
                BetterTimeWarp.SettingsNode = new ConfigNode();
            }
            if (!SettingsNode.HasNode("BetterTimeWarp"))
            {
                SettingsNode.AddNode("BetterTimeWarp");
            }
            var node = SettingsNode.GetNode("BetterTimeWarp");

            if (!SettingsNode.HasNode("BetterTimeWarp"))
            {
                SettingsNode.AddNode("BetterTimeWarp");
            }

            if (node.HasValue("ScaleCameraSpeed"))
            {
                ScaleCameraSpeed = bool.Parse(node.GetValue("ScaleCameraSpeed"));
            }
            if (node.HasValue("UseLosslessPhysics"))
            {
                UseLosslessPhysics = bool.Parse(node.GetValue("UseLosslessPhysics"));
            }
            if (node.HasValue("LosslessUpperThreshold"))
            {
                LosslessUpperThreshold = float.Parse(node.GetValue("LosslessUpperThreshold"));
            }

            customWarps.Clear();
            customWarps.Add(StandardWarp);
            customWarps.Add(StandardPhysWarp);

            foreach (ConfigNode cNode in node.GetNodes("CustomWarpRate"))
            {
                string  name    = cNode.GetValue("name");
                bool    physics = bool.Parse(cNode.GetValue("physics"));
                float[] rates;
                if (physics)
                {
                    rates = new float[4];
                }
                else
                {
                    rates = new float[8];
                }
                rates[0] = 1f;
                for (int i = 1; i < 8; i++)
                {
                    if (i < 4 || !physics)
                    {
                        rates[i] = float.Parse(cNode.GetValue("warpRate" + i.ToString()));
                    }
                }
#if false
                rates[1] = float.Parse(cNode.GetValue("warpRate1"));
                rates[2] = float.Parse(cNode.GetValue("warpRate2"));
                rates[3] = float.Parse(cNode.GetValue("warpRate3"));
                if (!physics)
                {
                    rates[4] = float.Parse(cNode.GetValue("warpRate4"));
                    rates[5] = float.Parse(cNode.GetValue("warpRate5"));
                    rates[6] = float.Parse(cNode.GetValue("warpRate6"));
                    rates[7] = float.Parse(cNode.GetValue("warpRate7"));
                }
#endif
                customWarps.Add(new TimeWarpRates(name, rates, physics));
            }

            //populate the seperate arrays
            warpRates = customWarps.Where(r => !r.Physics).ToArray();
            physRates = customWarps.Where(r => r.Physics).ToArray();

            //load selected rates
            string currentTimeWarp = StandardWarp.Name;
            string currentPhysWarp = StandardPhysWarp.Name;

            if (node.HasValue("CurrentTimeWarp"))
            {
                currentTimeWarp = node.GetValue("CurrentTimeWarp");
            }
            if (node.HasValue("CurrentPhysWarp"))
            {
                currentPhysWarp = node.GetValue("CurrentPhysWarp");
            }

            if (warpRates.Where(w => w.Name == currentTimeWarp).Count() > 0)
            {
                CurrentWarp = warpRates.Where(w => w.Name == currentTimeWarp).First();
            }
            if (physRates.Where(w => w.Name == currentPhysWarp).Count() > 0)
            {
                CurrentPhysWarp = physRates.Where(w => w.Name == currentPhysWarp).First();
            }

            if (CurrentWarp == null)
            {
                CurrentWarp = StandardWarp;
                Log.Info("CurrentWarp set to StandardWarp  2");
            }
            if (CurrentPhysWarp == null)
            {
                CurrentPhysWarp = StandardPhysWarp;
            }
        }
示例#5
0
        public void TimeWarpWindow(int id)
        {
            GUILayout.BeginHorizontal();
            GUILayout.BeginVertical();

            scrollPos = GUILayout.BeginScrollView(scrollPos);

            editToggle   = GUILayout.Toggle(editToggle, "Create", skin.button);
            selected     = GUILayout.SelectionGrid(selected, names, 1, smallButtonStyle);
            currentRates = customWarps.Find(r => r.Name == names [selected] || names[selected].Split('<', '>').Contains(r.Name));
            if (currentRates == null)
            {
                currentRates = StandardWarp;
            }

            GUILayout.EndScrollView();

            GUILayout.EndVertical();

            GUILayout.BeginVertical();
            scrollPos2 = GUILayout.BeginScrollView(scrollPos2);

            if (editToggle)
            {
                GUILayout.BeginVertical(GUILayout.ExpandHeight(true));

                bool canExport = true;
                warpName = GUILayout.TextField(warpName);
                w1       = GUILayout.TextField(w1);
                w2       = GUILayout.TextField(w2);
                w3       = GUILayout.TextField(w3);
                if (!physics)
                {
                    w4 = GUILayout.TextField(w4);
                    w5 = GUILayout.TextField(w5);
                    w6 = GUILayout.TextField(w6);
                    w7 = GUILayout.TextField(w7);
                }
                physics = GUILayout.Toggle(physics, "Physics Warp?");

                GUILayout.EndVertical();

                if (GUILayout.Button("Save"))
                {
                    float[] rates;
                    if (physics)
                    {
                        rates = new float[4];
                    }
                    else
                    {
                        rates = new float[8];
                    }

                    rates [0] = 1f;
                    float pw1;
                    if (float.TryParse(w1, out pw1))
                    {
                        rates [1] = pw1;
                    }
                    else
                    {
                        canExport = false;
                    }
                    float pw2;
                    if (float.TryParse(w2, out pw2))
                    {
                        rates [2] = pw2;
                    }
                    else
                    {
                        canExport = false;
                    }
                    float pw3;
                    if (float.TryParse(w3, out pw3))
                    {
                        rates [3] = pw3;
                    }
                    else
                    {
                        canExport = false;
                    }
                    if (!physics)
                    {
                        float pw4;
                        if (float.TryParse(w4, out pw4))
                        {
                            rates [4] = pw4;
                        }
                        else
                        {
                            canExport = false;
                        }
                        float pw5;
                        if (float.TryParse(w5, out pw5))
                        {
                            rates [5] = pw5;
                        }
                        else
                        {
                            canExport = false;
                        }
                        float pw6;
                        if (float.TryParse(w6, out pw6))
                        {
                            rates [6] = pw6;
                        }
                        else
                        {
                            canExport = false;
                        }
                        float pw7;
                        if (float.TryParse(w7, out pw7))
                        {
                            rates [7] = pw7;
                        }
                        else
                        {
                            canExport = false;
                        }
                    }
                    if (canExport)
                    {
                        TimeWarpRates timeWarpRates = new TimeWarpRates(warpName, rates, physics);
                        customWarps.Add(timeWarpRates);
                        SaveCustomWarpRates();
                        editToggle = false;
                        //SetWarpRates (timeWarpRates);
                        warpName = "Name";
                        physics  = false;
                        w1       = "10";
                        w2       = "100";
                        w3       = "1000";
                        w4       = "10000";
                        w5       = "100000";
                        w6       = "1000000";
                        w7       = "10000000";
                    }
                    else
                    {
                        PopupDialog.SpawnPopupDialog("Error", "Cannot save because there are non-numbers in the editing fields", "Ok", false, skin);
                    }
                }
                if (GUILayout.Button("Cancel", smallButtonStyle))
                {
                    editToggle = false;
                    warpName   = "Name";
                    physics    = false;
                    w1         = "10";
                    w2         = "100";
                    w3         = "1000";
                    w4         = "10000";
                    w5         = "100000";
                    w6         = "1000000";
                    w7         = "10000000";
                }
            }
            else
            {
                GUILayout.BeginVertical(GUILayout.ExpandHeight(true));
                GUILayout.Label("<b><color=lime>1:</color></b> <color=white>" + currentRates.Rates [0].ToString() + "x</color>");
                GUILayout.Label("<b><color=lime>2:</color></b> <color=white>" + currentRates.Rates [1].ToString() + "x</color>");
                GUILayout.Label("<b><color=lime>3:</color></b> <color=white>" + currentRates.Rates [2].ToString() + "x</color>");
                GUILayout.Label("<b><color=lime>4:</color></b> <color=white>" + currentRates.Rates [3].ToString() + "x</color>");
                if (!currentRates.Physics)
                {
                    GUILayout.Label("<b><color=lime>5:</color></b> <color=white>" + currentRates.Rates [4].ToString() + "x</color>");
                    GUILayout.Label("<b><color=lime>6:</color></b> <color=white>" + currentRates.Rates [5].ToString() + "x</color>");
                    GUILayout.Label("<b><color=lime>7:</color></b> <color=white>" + currentRates.Rates [6].ToString() + "x</color>");
                    GUILayout.Label("<b><color=lime>8:</color></b> <color=white>" + currentRates.Rates [7].ToString() + "x</color>");
                }
                GUILayout.EndVertical();

                GUILayout.Space(15f);
                if (GUILayout.Button("Select"))
                {
                    SetWarpRates(currentRates);
                }
                if (currentRates != StandardWarp && currentRates != StandardPhysWarp && GUILayout.Button("Edit", smallButtonStyle))
                {
                    if (currentRates != StandardWarp && currentRates != StandardPhysWarp)
                    {
                        if (!currentRates.Physics)
                        {
                            SetWarpRates(StandardWarp, false);
                        }
                        else
                        {
                            SetWarpRates(StandardPhysWarp, false);
                        }
                        customWarps.Remove(currentRates);
                        editToggle = true;
                        warpName   = currentRates.Name;
                        physics    = currentRates.Physics;
                        w1         = currentRates.Rates[1].ToString();
                        w2         = currentRates.Rates[2].ToString();
                        w3         = currentRates.Rates[3].ToString();
                        if (!physics)
                        {
                            w4 = currentRates.Rates[4].ToString();
                            w5 = currentRates.Rates[5].ToString();
                            w6 = currentRates.Rates[6].ToString();
                            w7 = currentRates.Rates[7].ToString();
                        }
                        selected = 0;
                    }
                    else
                    {
                        PopupDialog.SpawnPopupDialog("Better Time Warp", "Cannot edit standard warp rates", "Ok", true, skin);
                    }
                }
                if (currentRates != StandardWarp && currentRates != StandardPhysWarp && GUILayout.Button("Delete", smallButtonStyle))
                {
                    if (currentRates != StandardWarp && currentRates != StandardPhysWarp)
                    {
                        customWarps.Remove(currentRates);
                        selected = 0;
                        SaveCustomWarpRates();
                        PopupDialog.SpawnPopupDialog("Better Time Warp", "Deleted " + currentRates.Name + " time warp rates", "Ok", true, skin);
                        SetWarpRates(StandardWarp, false);
                    }
                    else
                    {
                        PopupDialog.SpawnPopupDialog("Better Time Warp", "Cannot delete standard warp rates", "Ok", true, skin);
                    }
                }
            }
            GUILayout.EndScrollView();
            GUILayout.EndVertical();

            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            if (GUILayout.Button("Simple", GUILayout.ExpandWidth(true)))
            {
                advWindowOpen = false;
            }
            showPhysicsSettings = GUILayout.Toggle(showPhysicsSettings, "<color=lime>Physics Settings</color>", skin.button, GUILayout.ExpandWidth(true));
            GUILayout.EndHorizontal();
        }
        private void SaveCustomWarpRates()
        {
            if (!SettingsNode.HasNode ("BetterTimeWarp"))
                SettingsNode.AddNode ("BetterTimeWarp");

            ConfigNode node = SettingsNode.GetNode ("BetterTimeWarp");

            node.SetValue ("ScaleCameraSpeed", ScaleCameraSpeed.ToString (), true);
            node.SetValue ("UseLosslessPhysics", UseLosslessPhysics.ToString (), true);
            node.SetValue ("LosslessUpperThreshold", LosslessUpperThreshold.ToString (), true);

            node.RemoveNodes ("CustomWarpRate");
            foreach (var rates in customWarps)
            {
                if (rates != StandardWarp && rates != StandardPhysWarp)
                {
                    ConfigNode rateNode = new ConfigNode ("CustomWarpRate");
                    rateNode.AddValue ("name", rates.Name);
                    rateNode.AddValue ("warpRate1", rates.Rates [1]);
                    rateNode.AddValue ("warpRate2", rates.Rates [2]);
                    rateNode.AddValue ("warpRate3", rates.Rates [3]);
                    if (!rates.Physics)
                    {
                        rateNode.AddValue ("warpRate4", rates.Rates [4]);
                        rateNode.AddValue ("warpRate5", rates.Rates [5]);
                        rateNode.AddValue ("warpRate6", rates.Rates [6]);
                        rateNode.AddValue ("warpRate7", rates.Rates [7]);
                    }
                    rateNode.AddValue ("physics", rates.Physics);
                    node.AddNode (rateNode);
                }
            }

            if (CurrentWarp == null)
                CurrentWarp = StandardWarp;
            if (CurrentPhysWarp == null)
                CurrentPhysWarp = StandardPhysWarp;

            node.SetValue ("CurrentTimeWarp", CurrentWarp.Name, true);
            node.SetValue ("CurrentPhysWarp", CurrentPhysWarp.Name, true);
        }
        private void LoadCustomWarpRates()
        {
            if (!SettingsNode.HasNode ("BetterTimeWarp"))
                SettingsNode.AddNode ("BetterTimeWarp");
            var node = SettingsNode.GetNode ("BetterTimeWarp");

            if (!SettingsNode.HasNode ("BetterTimeWarp"))
                SettingsNode.AddNode ("BetterTimeWarp");

            if (node.HasValue ("ScaleCameraSpeed"))
                ScaleCameraSpeed = bool.Parse (node.GetValue ("ScaleCameraSpeed"));
            if (node.HasValue ("UseLosslessPhysics"))
                UseLosslessPhysics = bool.Parse (node.GetValue ("UseLosslessPhysics"));
            if (node.HasValue ("LosslessUpperThreshold"))
                LosslessUpperThreshold = float.Parse (node.GetValue ("LosslessUpperThreshold"));

            customWarps.Clear ();
            customWarps.Add (StandardWarp);
            customWarps.Add (StandardPhysWarp);

            foreach (ConfigNode cNode in node.GetNodes("CustomWarpRate"))
            {
                string name = cNode.GetValue ("name");
                bool physics = bool.Parse (cNode.GetValue("physics"));
                float[] rates;
                if (physics)
                    rates = new float[4];
                else
                    rates = new float[8];
                rates [0] = 1f;
                rates [1] = float.Parse(cNode.GetValue ("warpRate1"));
                rates [2] = float.Parse(cNode.GetValue ("warpRate2"));
                rates [3] = float.Parse(cNode.GetValue ("warpRate3"));
                if (!physics)
                {
                    rates [4] = float.Parse (cNode.GetValue ("warpRate4"));
                    rates [5] = float.Parse (cNode.GetValue ("warpRate5"));
                    rates [6] = float.Parse (cNode.GetValue ("warpRate6"));
                    rates [7] = float.Parse (cNode.GetValue ("warpRate7"));
                }
                customWarps.Add (new TimeWarpRates(name, rates, physics));
            }

            //populate the seperate arrays
            warpRates = customWarps.Where (r => !r.Physics).ToArray();
            physRates = customWarps.Where (r => r.Physics).ToArray();

            //load selected rates
            string currentTimeWarp = StandardWarp.Name;
            string currentPhysWarp = StandardPhysWarp.Name;

            if (node.HasValue ("CurrentTimeWarp"))
                currentTimeWarp = node.GetValue ("CurrentTimeWarp");
            if (node.HasValue ("CurrentPhysWarp"))
                currentPhysWarp = node.GetValue ("CurrentPhysWarp");

            if (warpRates.Where (w => w.Name == currentTimeWarp).Count () > 0)
                CurrentWarp = warpRates.Where (w => w.Name == currentTimeWarp).First();
            if (physRates.Where (w => w.Name == currentPhysWarp).Count () > 0)
                CurrentPhysWarp = physRates.Where (w => w.Name == currentPhysWarp).First();

            if (CurrentWarp == null)
                CurrentWarp = StandardWarp;
            if (CurrentPhysWarp == null)
                CurrentPhysWarp = StandardPhysWarp;
        }
        public void TimeWarpWindow(int id)
        {
            GUILayout.BeginHorizontal ();
            GUILayout.BeginVertical ();

            scrollPos = GUILayout.BeginScrollView (scrollPos);

            editToggle = GUILayout.Toggle (editToggle, "Create", skin.button);
            selected = GUILayout.SelectionGrid (selected, names, 1, smallButtonStyle);
            currentRates = customWarps.Find (r => r.Name == names [selected] || names[selected].Split('<', '>').Contains(r.Name));
            if (currentRates == null)
                currentRates = StandardWarp;

            GUILayout.EndScrollView ();

            GUILayout.EndVertical ();

            GUILayout.BeginVertical ();
            scrollPos2 = GUILayout.BeginScrollView (scrollPos2);

            if (editToggle)
            {
                GUILayout.BeginVertical (GUILayout.ExpandHeight(true));

                bool canExport = true;
                warpName = GUILayout.TextField (warpName);
                w1 = GUILayout.TextField (w1);
                w2 = GUILayout.TextField (w2);
                w3 = GUILayout.TextField (w3);
                if (!physics)
                {
                    w4 = GUILayout.TextField (w4);
                    w5 = GUILayout.TextField (w5);
                    w6 = GUILayout.TextField (w6);
                    w7 = GUILayout.TextField (w7);
                }
                physics = GUILayout.Toggle (physics, "Physics Warp?");

                GUILayout.EndVertical ();

                if (GUILayout.Button ("Save"))
                {
                    float[] rates;
                    if (physics)
                        rates = new float[4];
                    else
                        rates = new float[8];

                    rates [0] = 1f;
                    float pw1;
                    if(float.TryParse(w1, out pw1))
                        rates [1] = pw1;
                    else
                        canExport = false;
                    float pw2;
                    if(float.TryParse(w2, out pw2))
                        rates [2] = pw2;
                    else
                        canExport = false;
                    float pw3;
                    if(float.TryParse(w3, out pw3))
                        rates [3] = pw3;
                    else
                        canExport = false;
                    if (!physics)
                    {
                        float pw4;
                        if (float.TryParse (w4, out pw4))
                            rates [4] = pw4;
                        else
                            canExport = false;
                        float pw5;
                        if (float.TryParse (w5, out pw5))
                            rates [5] = pw5;
                        else
                            canExport = false;
                        float pw6;
                        if (float.TryParse (w6, out pw6))
                            rates [6] = pw6;
                        else
                            canExport = false;
                        float pw7;
                        if (float.TryParse (w7, out pw7))
                            rates [7] = pw7;
                        else
                            canExport = false;
                    }
                    if (canExport)
                    {
                        TimeWarpRates timeWarpRates = new TimeWarpRates (warpName, rates, physics);
                        customWarps.Add (timeWarpRates);
                        SaveCustomWarpRates ();
                        editToggle = false;
                        //SetWarpRates (timeWarpRates);
                        warpName = "Name";
                        physics = false;
                        w1 = "10";
                        w2 = "100";
                        w3 = "1000";
                        w4 = "10000";
                        w5 = "100000";
                        w6 = "1000000";
                        w7 = "10000000";
                    }
                    else
                    {
                        PopupDialog.SpawnPopupDialog ("Error", "Cannot save because there are non-numbers in the editing fields", "Ok", false, skin);
                    }
                }
                if (GUILayout.Button ("Cancel", smallButtonStyle))
                {
                    editToggle = false;
                    warpName = "Name";
                    physics = false;
                    w1 = "10";
                    w2 = "100";
                    w3 = "1000";
                    w4 = "10000";
                    w5 = "100000";
                    w6 = "1000000";
                    w7 = "10000000";
                }
            }
            else
            {
                GUILayout.BeginVertical (GUILayout.ExpandHeight(true));
                GUILayout.Label ("<b><color=lime>1:</color></b> <color=white>" + currentRates.Rates [0].ToString () + "x</color>");
                GUILayout.Label ("<b><color=lime>2:</color></b> <color=white>" + currentRates.Rates [1].ToString () + "x</color>");
                GUILayout.Label ("<b><color=lime>3:</color></b> <color=white>" + currentRates.Rates [2].ToString () + "x</color>");
                GUILayout.Label ("<b><color=lime>4:</color></b> <color=white>" + currentRates.Rates [3].ToString () + "x</color>");
                if (!currentRates.Physics)
                {
                    GUILayout.Label ("<b><color=lime>5:</color></b> <color=white>" + currentRates.Rates [4].ToString () + "x</color>");
                    GUILayout.Label ("<b><color=lime>6:</color></b> <color=white>" + currentRates.Rates [5].ToString () + "x</color>");
                    GUILayout.Label ("<b><color=lime>7:</color></b> <color=white>" + currentRates.Rates [6].ToString () + "x</color>");
                    GUILayout.Label ("<b><color=lime>8:</color></b> <color=white>" + currentRates.Rates [7].ToString () + "x</color>");
                }
                GUILayout.EndVertical ();

                GUILayout.Space (15f);
                if (GUILayout.Button ("Select"))
                {
                    SetWarpRates (currentRates);
                }
                if (currentRates != StandardWarp && currentRates != StandardPhysWarp && GUILayout.Button ("Edit", smallButtonStyle))
                {
                    if (currentRates != StandardWarp && currentRates != StandardPhysWarp)
                    {
                        if(!currentRates.Physics)
                            SetWarpRates (StandardWarp, false);
                        else
                            SetWarpRates (StandardPhysWarp, false);
                        customWarps.Remove (currentRates);
                        editToggle = true;
                        warpName = currentRates.Name;
                        physics = currentRates.Physics;
                        w1 = currentRates.Rates[1].ToString();
                        w2 = currentRates.Rates[2].ToString();
                        w3 = currentRates.Rates[3].ToString();
                        if (!physics)
                        {
                            w4 = currentRates.Rates[4].ToString();
                            w5 = currentRates.Rates[5].ToString();
                            w6 = currentRates.Rates[6].ToString();
                            w7 = currentRates.Rates[7].ToString();
                        }
                        selected = 0;
                    }
                    else
                    {
                        PopupDialog.SpawnPopupDialog ("Better Time Warp", "Cannot edit standard warp rates", "Ok", true, skin);
                    }
                }
                if (currentRates != StandardWarp && currentRates != StandardPhysWarp && GUILayout.Button ("Delete", smallButtonStyle))
                {
                    if (currentRates != StandardWarp && currentRates != StandardPhysWarp)
                    {
                        customWarps.Remove (currentRates);
                        selected = 0;
                        SaveCustomWarpRates ();
                        PopupDialog.SpawnPopupDialog ("Better Time Warp", "Deleted " + currentRates.Name + " time warp rates", "Ok", true, skin);
                        SetWarpRates (StandardWarp, false);
                    }
                    else
                    {
                        PopupDialog.SpawnPopupDialog ("Better Time Warp", "Cannot delete standard warp rates", "Ok", true, skin);
                    }
                }
            }
            GUILayout.EndScrollView ();
            GUILayout.EndVertical ();

            GUILayout.EndHorizontal ();

            GUILayout.BeginHorizontal();
            if (GUILayout.Button ("Simple", GUILayout.ExpandWidth(true)))
            {
                advWindowOpen = false;
            }
            showPhysicsSettings = GUILayout.Toggle (showPhysicsSettings, "<color=lime>Physics Settings</color>", skin.button, GUILayout.ExpandWidth(true));
            GUILayout.EndHorizontal();
        }
        public void SetWarpRates(TimeWarpRates rates, bool message = true)
        {
            if (TimeWarp.fetch != null)
            {
                if (TimeWarp.fetch.warpRates.Length == rates.Rates.Length && !rates.Physics)
                {
                    TimeWarp.fetch.warpRates = rates.Rates;
                    CurrentWarp = rates;

                    for (var i = 0; i < warpRates.Length; i++)
                    {
                        var r = warpRates [i];
                        if (r == rates)
                        {
                            currWarpIndex = i;
                        }
                    }

                    print ("[BetterTimeWarp]: Set time warp rates to " + rates.ToString());
                    if (message)
                        ScreenMessages.PostScreenMessage (new ScreenMessage ("New time warp rates: " + rates.Name, 3f, ScreenMessageStyle.UPPER_CENTER), false);
                    return;
                }
                else if (TimeWarp.fetch.physicsWarpRates.Length == rates.Rates.Length && rates.Physics)
                {
                    TimeWarp.fetch.physicsWarpRates = rates.Rates;
                    CurrentPhysWarp = rates;

                    for (var i = 0; i < physRates.Length; i++)
                    {
                        var r = physRates [i];
                        if (r == rates)
                        {
                            currPhysIndex = i;
                        }
                    }

                    print ("[BetterTimeWarp]: Set time warp rates to " + rates.ToString());
                    if (message)
                        ScreenMessages.PostScreenMessage (new ScreenMessage ("New physic warp rates: " + rates.Name, 3f, ScreenMessageStyle.UPPER_CENTER), false);
                    return;
                }
                return;
            }
            Debug.LogWarning ("[BetterTimeWarp]: Failed to set warp rates");

            //reset it to standard in case of  failiure
            if (rates.Physics)
            {
                for (var i = 0; i < physRates.Length; i++)
                {
                    var r = physRates [i];
                    if (r == StandardPhysWarp)
                    {
                        currPhysIndex = i;
                        CurrentPhysWarp = StandardPhysWarp;
                    }
                }
            }
            else
            {
                for (var i = 0; i < warpRates.Length; i++)
                {
                    var r = warpRates [i];
                    if (r == StandardWarp)
                    {
                        currWarpIndex = i;
                        CurrentWarp = StandardWarp;
                    }
                }
            }
        }