Ejemplo n.º 1
0
        internal void Build()
        {
            string modInfo = null;

            foreach (GadgetMod mod in GadgetMods.ListAllMods().Where(x => x.LoadedGadgets.Count > 0))
            {
                if (mod.Name == "GadgetCore")
                {
                    modInfo = CoreMod.GadgetCoreMod.GetDesc();
                }
                else if (mod.HasModFile("ModInfo.txt"))
                {
                    using (GadgetModFile infoFile = mod.GetModFile("ModInfo.txt")) modInfo = infoFile.ReadAllText();
                }
                if (string.IsNullOrEmpty(modInfo) || modInfo == "Insert the description for your mod here!")
                {
                    modInfo = "This Gadget mod does not have a ModInfo file.";
                }
                Dictionary <string, string> info = new Dictionary <string, string>
                {
                    ["Name"]         = mod.Name,
                    ["Version"]      = mod.Version.ToString(),
                    ["File Name"]    = Path.GetFileName(mod.ModPath),
                    ["Dependencies"] = mod.ModDependencies.Count > 0 ? mod.ModDependencies.Concat() : "None",
                    ["Gadgets"]      = mod.LoadedGadgets.Select(x => x.Attribute.Name).Concat()
                };
                if (mod.UnloadedGadgets.Count > 0)
                {
                    info.Add("Unloaded/Errored Gadgets", mod.UnloadedGadgets.Select(x => x.Attribute.Name).Concat());
                }
                using (GadgetModFile infoFile = mod.GetModFile("ModInfo.txt"))
                    modEntries.Add(new ModMenuEntry(mod.Name, ModMenuEntryType.GADGET, modInfo, info, mod.LoadedGadgets.ToArray()));
            }
            foreach (GadgetMod mod in GadgetLoader.EmptyMods)
            {
                if (mod.Name == "GadgetCore")
                {
                    modInfo = CoreMod.GadgetCoreMod.GetDesc();
                }
                else if (mod.HasModFile("ModInfo.txt"))
                {
                    using (GadgetModFile infoFile = mod.GetModFile("ModInfo.txt")) modInfo = infoFile.ReadAllText();
                }
                if (string.IsNullOrEmpty(modInfo) || modInfo == "Insert the description for your mod here!")
                {
                    modInfo = "This Gadget mod does not have a ModInfo file.";
                }
                Dictionary <string, string> info = new Dictionary <string, string>
                {
                    ["Name"]         = mod.Name,
                    ["Version"]      = mod.Version.ToString(),
                    ["File Name"]    = Path.GetFileName(mod.ModPath),
                    ["Dependencies"] = mod.ModDependencies.Count > 0 ? mod.ModDependencies.Concat() : "None",
                };
                if (mod.UnloadedGadgets.Count > 0)
                {
                    info.Add("Unloaded/Errored Gadgets", mod.UnloadedGadgets.Select(x => x.Attribute.Name).Concat());
                }
                modEntries.Add(new ModMenuEntry(mod.Name, ModMenuEntryType.EMPTY_GADGET, modInfo, info));
            }
            foreach (GadgetMod mod in GadgetLoader.IncompatibleMods)
            {
                if (mod.Name == "GadgetCore")
                {
                    modInfo = CoreMod.GadgetCoreMod.GetDesc();
                }
                else if (mod.HasModFile("ModInfo.txt"))
                {
                    using (GadgetModFile infoFile = mod.GetModFile("ModInfo.txt")) modInfo = infoFile.ReadAllText();
                }
                if (string.IsNullOrEmpty(modInfo) || modInfo == "Insert the description for your mod here!")
                {
                    modInfo = "This Gadget mod does not have a ModInfo file.";
                }
                Dictionary <string, string> info = new Dictionary <string, string>
                {
                    ["Name"]         = mod.Name,
                    ["Version"]      = mod.Version.ToString(),
                    ["File Name"]    = Path.GetFileName(mod.ModPath),
                    ["Dependencies"] = mod.ModDependencies.Count > 0 ? mod.ModDependencies.Concat() : "None"
                };
                modEntries.Add(new ModMenuEntry(mod.Name, ModMenuEntryType.INCOMPATIBLE_GADGET, modInfo, info));
            }
            foreach (GadgetMod mod in GadgetMods.ListAllMods().Where(x => x.LoadedGadgets.Count == 0))
            {
                if (mod.Name == "GadgetCore")
                {
                    modInfo = CoreMod.GadgetCoreMod.GetDesc();
                }
                else if (mod.HasModFile("ModInfo.txt"))
                {
                    using (GadgetModFile infoFile = mod.GetModFile("ModInfo.txt")) modInfo = infoFile.ReadAllText();
                }
                if (string.IsNullOrEmpty(modInfo) || modInfo == "Insert the description for your mod here!")
                {
                    modInfo = "This Gadget mod does not have a ModInfo file.";
                }
                Dictionary <string, string> info = new Dictionary <string, string>
                {
                    ["Name"]         = mod.Name,
                    ["Version"]      = mod.Version.ToString(),
                    ["File Name"]    = Path.GetFileName(mod.ModPath),
                    ["Dependencies"] = mod.ModDependencies.Count > 0 ? mod.ModDependencies.Concat() : "None",
                };
                if (mod.UnloadedGadgets.Count > 0)
                {
                    info.Add("Unloaded/Errored Gadgets", mod.UnloadedGadgets.Select(x => x.Attribute.Name).Concat());
                }
                modEntries.Add(new ModMenuEntry(mod.Name, ModMenuEntryType.ERRORED_GADGET, modInfo, info));
            }
            foreach (Tuple <string, string> mod in GadgetLoader.ErroredMods)
            {
                if (mod.Item1 == "GadgetCore")
                {
                    modInfo = CoreMod.GadgetCoreMod.GetDesc();
                }
                modInfo = "As there was an error loading this mod, its description could not be loaded.\nThe error that prevented this mod from loading should be in the log.\nYou should report this to the mod author.";
                Dictionary <string, string> info = new Dictionary <string, string>
                {
                    ["Name"]      = mod.Item1,
                    ["File Name"] = Path.GetFileName(mod.Item2)
                };
                modEntries.Add(new ModMenuEntry(mod.Item1, ModMenuEntryType.ERRORED_GADGET, modInfo, info));
            }
            if (GadgetCoreAPI.GetUMFAPI() != null)
            {
                foreach (string mod in GadgetCoreAPI.GetUMFAPI().GetModNames())
                {
                    try
                    {
                        modInfo = File.ReadAllText(GadgetCoreAPI.GetUMFAPI().GetModInfosPath() + "/" + mod + "_v" + GadgetCoreAPI.GetUMFAPI().GetModVersion(mod) + "_ModInfo.txt");
                    }
                    catch (Exception) { }
                    if (string.IsNullOrEmpty(modInfo) || modInfo == "A UMF Mod(umodframework.com) for Roguelands")
                    {
                        modInfo = "This UMF mod does not have a ModInfo file.";
                    }
                    Dictionary <string, string> info = new Dictionary <string, string>()
                    {
                        ["Name"]        = mod,
                        ["Version"]     = GadgetCoreAPI.GetUMFAPI().GetModVersion(mod).ToString(),
                        ["Description"] = GadgetCoreAPI.GetUMFAPI().GetModDescription(mod),
                    };
                    if (GadgetCoreAPI.GetUMFAPI().GetModNamesEnabled().Contains(mod))
                    {
                        modEntries.Add(new ModMenuEntry(mod, ModMenuEntryType.UMF, modInfo, info));
                    }
                    else
                    {
                        modEntries.Add(new ModMenuEntry(mod, ModMenuEntryType.DISABLED_UMF, modInfo, info));
                    }
                    umfModEntries.Add(modEntries[modEntries.Count - 1]);
                }
                foreach (string mod in GadgetCoreAPI.GetUMFAPI().GetModNamesMissingDependencies())
                {
                    try
                    {
                        modInfo = File.ReadAllText(GadgetCoreAPI.GetUMFAPI().GetModInfosPath() + "/" + mod + "_v" + GadgetCoreAPI.GetUMFAPI().GetModVersion(mod) + "_ModInfo.txt");
                    }
                    catch (Exception) { }
                    if (string.IsNullOrEmpty(modInfo) || modInfo == "A UMF Mod(umodframework.com) for Roguelands")
                    {
                        modInfo = "This UMF mod does not have a ModInfo file.";
                    }
                    Dictionary <string, string> info = new Dictionary <string, string>()
                    {
                        ["Name"]        = mod,
                        ["Version"]     = GadgetCoreAPI.GetUMFAPI().GetModVersion(mod).ToString(),
                        ["Description"] = GadgetCoreAPI.GetUMFAPI().GetModDescription(mod),
                    };
                    modEntries.Add(new ModMenuEntry(mod, ModMenuEntryType.INCOMPATIBLE_UMF, modInfo, info));
                }
            }

            int modCount = modEntries.Count + modEntries.Where(x => x.Type == ModMenuEntryType.GADGET).SelectMany(x => x.Gadgets).Count();

            ScrollRect modListScrollView = new GameObject("Scroll View", typeof(RectTransform), typeof(ScrollRect), typeof(CanvasRenderer), typeof(Image)).GetComponent <ScrollRect>();

            modListScrollView.GetComponent <RectTransform>().SetParent(transform);
            modListScrollView.GetComponent <RectTransform>().anchorMin = new Vector2(0f, 0f);
            modListScrollView.GetComponent <RectTransform>().anchorMax = new Vector2(0.3f, 1f);
            modListScrollView.GetComponent <RectTransform>().offsetMin = new Vector2(10, 10);
            modListScrollView.GetComponent <RectTransform>().offsetMax = new Vector2(0, -10);
            modListScrollView.GetComponent <Image>().sprite            = SceneInjector.BoxSprite;
            modListScrollView.GetComponent <Image>().type       = Image.Type.Sliced;
            modListScrollView.GetComponent <Image>().fillCenter = true;
            Mask modListScrollViewMask = new GameObject("Mask", typeof(RectTransform), typeof(CanvasRenderer), typeof(Image), typeof(Mask)).GetComponent <Mask>();

            modListScrollViewMask.GetComponent <RectTransform>().SetParent(modListScrollView.transform);
            modListScrollViewMask.GetComponent <RectTransform>().anchorMin = new Vector2(0f, 0f);
            modListScrollViewMask.GetComponent <RectTransform>().anchorMax = new Vector2(1f, 1f);
            modListScrollViewMask.GetComponent <RectTransform>().offsetMin = Vector2.zero;
            modListScrollViewMask.GetComponent <RectTransform>().offsetMax = Vector2.zero;
            modListScrollViewMask.GetComponent <Image>().sprite            = SceneInjector.BoxMask;
            modListScrollViewMask.GetComponent <Image>().type       = Image.Type.Sliced;
            modListScrollViewMask.GetComponent <Image>().fillCenter = true;
            modListScrollViewMask.showMaskGraphic = false;
            RectTransform modListViewport = new GameObject("Viewport", typeof(RectTransform)).GetComponent <RectTransform>();

            modListViewport.SetParent(modListScrollViewMask.transform);
            modListViewport.anchorMin = new Vector2(0f, 0f);
            modListViewport.anchorMax = new Vector2(1f, 1f);
            modListViewport.offsetMin = new Vector2(10, 10);
            modListViewport.offsetMax = new Vector2(-10, -10);
            RectTransform modList = new GameObject("ModList", typeof(RectTransform), typeof(ToggleGroup)).GetComponent <RectTransform>();

            modList.SetParent(modListViewport);
            modList.anchorMin = new Vector2(0f, modCount <= 6 ? 0f : (1f - (modCount / 6f)));
            modList.anchorMax = new Vector2(1f, 1f);
            modList.offsetMin = Vector2.zero;
            modList.offsetMax = Vector2.zero;
            Scrollbar modListScrollBar = new GameObject("Scrollbar", typeof(RectTransform), typeof(CanvasRenderer), typeof(Image), typeof(Scrollbar)).GetComponent <Scrollbar>();

            modListScrollBar.GetComponent <RectTransform>().SetParent(modListScrollView.transform);
            modListScrollBar.GetComponent <RectTransform>().anchorMin = new Vector2(1f, 0f);
            modListScrollBar.GetComponent <RectTransform>().anchorMax = new Vector2(1.25f, 1f);
            modListScrollBar.GetComponent <RectTransform>().offsetMin = Vector2.zero;
            modListScrollBar.GetComponent <RectTransform>().offsetMax = Vector2.zero;
            modListScrollBar.GetComponent <Image>().sprite            = SceneInjector.BoxSprite;
            modListScrollBar.GetComponent <Image>().type       = Image.Type.Sliced;
            modListScrollBar.GetComponent <Image>().fillCenter = true;
            RectTransform modListScrollBarHandle = new GameObject("Handle", typeof(RectTransform), typeof(CanvasRenderer), typeof(Image)).GetComponent <RectTransform>();

            modListScrollBarHandle.SetParent(modListScrollBar.transform);
            modListScrollBarHandle.anchorMin = new Vector2(0.05f, 0.05f);
            modListScrollBarHandle.anchorMax = new Vector2(0.95f, 0.95f);
            modListScrollBarHandle.offsetMin = Vector2.zero;
            modListScrollBarHandle.offsetMax = Vector2.zero;
            modListScrollBarHandle.GetComponent <Image>().sprite     = SceneInjector.BoxSprite;
            modListScrollBarHandle.GetComponent <Image>().type       = Image.Type.Sliced;
            modListScrollBarHandle.GetComponent <Image>().fillCenter = true;
            modListScrollBar.targetGraphic = modListScrollBarHandle.GetComponent <Image>();
            modListScrollBar.handleRect    = modListScrollBarHandle;
            modListScrollBar.direction     = Scrollbar.Direction.BottomToTop;
            if (modCount <= 5)
            {
                modListScrollBar.interactable = false;
            }
            modListScrollView.content           = modList;
            modListScrollView.horizontal        = false;
            modListScrollView.scrollSensitivity = 5;
            modListScrollView.movementType      = ScrollRect.MovementType.Clamped;
            modListScrollView.viewport          = modListViewport;
            modListScrollView.verticalScrollbar = modListScrollBar;
            float  entryHeight    = modCount <= 6 ? (1f / 6f) : (1f / modCount);
            Toggle selectedToggle = null;
            int    bonusOffset    = 0;

            for (int i = 0; i < modEntries.Count; i++)
            {
                ModMenuEntry modEntry = modEntries[i];
                if (modEntry.Gadgets.Length > 0)
                {
                    int           gadgetCount  = modEntry.Gadgets.Count();
                    float         gadgetHeight = 1f / (gadgetCount + 1);
                    RectTransform modEntryRect = new GameObject("Gadget-Containing Mod Entry: " + modEntry.Name, typeof(RectTransform), typeof(Toggle), typeof(CanvasRenderer), typeof(Image), typeof(Mask)).GetComponent <RectTransform>();
                    modEntryRect.SetParent(modList);
                    modEntryRect.anchorMin = new Vector2(0f, 1 - ((i + bonusOffset + 1 + gadgetCount) * entryHeight));
                    modEntryRect.anchorMax = new Vector2(1f, 1 - ((i + bonusOffset) * entryHeight));
                    modEntryRect.offsetMin = Vector2.zero;
                    modEntryRect.offsetMax = Vector2.zero;
                    Toggle modToggle   = modEntryRect.GetComponent <Toggle>();
                    Image  modSelected = new GameObject("Selected", typeof(RectTransform), typeof(CanvasRenderer), typeof(Image)).GetComponent <Image>();
                    modSelected.rectTransform.SetParent(modEntryRect);
                    modSelected.rectTransform.anchorMin = new Vector2(0f, 0f);
                    modSelected.rectTransform.anchorMax = new Vector2(1f, 1f);
                    modSelected.rectTransform.offsetMin = Vector2.zero;
                    modSelected.rectTransform.offsetMax = Vector2.zero;
                    modSelected.sprite = SceneInjector.BoxSprite;
                    modSelected.type   = Image.Type.Sliced;
                    Text modLabel = new GameObject("Label", typeof(RectTransform), typeof(CanvasRenderer), typeof(Text)).GetComponent <Text>();
                    modLabel.rectTransform.SetParent(modEntryRect);
                    modLabel.rectTransform.anchorMin = new Vector2(0f, 1f - gadgetHeight);
                    modLabel.rectTransform.anchorMax = new Vector2(1f, 1f);
                    modLabel.rectTransform.offsetMin = new Vector2(10, 10);
                    modLabel.rectTransform.offsetMax = new Vector2(-10, -10);
                    modLabel.font                           = SceneInjector.ModConfigMenuText.GetComponent <TextMesh>().font;
                    modLabel.fontSize                       = 12;
                    modLabel.horizontalOverflow             = HorizontalWrapMode.Overflow;
                    modLabel.verticalOverflow               = VerticalWrapMode.Overflow;
                    modLabel.alignment                      = TextAnchor.MiddleLeft;
                    modToggle.GetComponent <Image>().sprite = SceneInjector.BoxSprite;
                    modToggle.GetComponent <Image>().type   = Image.Type.Sliced;
                    modToggle.transition                    = Selectable.Transition.None;
                    modToggle.isOn                          = i == modIndex && gadgetIndex == -1;
                    modToggle.toggleTransition              = Toggle.ToggleTransition.None;
                    modToggle.graphic                       = modSelected;
                    modToggle.group                         = modList.GetComponent <ToggleGroup>();
                    int toggleIndex = i;
                    if (i == modIndex)
                    {
                        selectedToggle = modToggle;
                    }
                    modToggle.onValueChanged.AddListener((toggled) => { if (toggled)
                                                                        {
                                                                            UpdateInfo(modToggle, toggleIndex); GadgetCoreAPI.CloseDialog();
                                                                        }
                                                         });

                    switch (modEntry.Type)
                    {
                    case ModMenuEntryType.GADGET:
                        modSelected.color = new Color(1f, 1f, 0.5f, 1f);
                        modToggle.GetComponent <Image>().color = new Color(1f, 1f, 0.5f, 0.25f);
                        modLabel.color = new Color(1f, 1f, 1f, 1f);
                        modLabel.text  = modEntry.Name + "\nGadget Mod";
                        break;

                    case ModMenuEntryType.EMPTY_GADGET:
                        modSelected.color = new Color(0.5f, 0.5f, 0.125f, 1f);
                        modToggle.GetComponent <Image>().color = new Color(0.5f, 0.5f, 0.125f, 0.25f);
                        modLabel.color = new Color(1f, 1f, 1f, 0.5f);
                        modLabel.text  = modEntry.Name + "\nEmpty Gadget Mod";
                        break;

                    case ModMenuEntryType.INCOMPATIBLE_GADGET:
                        modSelected.color = new Color(1f, 0.5f, 0f, 1f);
                        modToggle.GetComponent <Image>().color = new Color(1f, 0.5f, 0f, 0.25f);
                        modLabel.color = new Color(1f, 1f, 1f, 0.5f);
                        modLabel.text  = modEntry.Name + "\nIncompatible Gadget Mod";
                        break;

                    case ModMenuEntryType.ERRORED_GADGET:
                        modSelected.color = new Color(1f, 0f, 0f, 1f);
                        modToggle.GetComponent <Image>().color = new Color(1f, 0f, 0f, 0.25f);
                        modLabel.color = new Color(1f, 1f, 1f, 0.5f);
                        modLabel.text  = modEntry.Name + "\nErrored Gadget Mod";
                        break;

                    case ModMenuEntryType.UMF:
                        modSelected.color = new Color(139 / 255f, 69 / 255f, 19 / 255f, 1f);
                        modToggle.GetComponent <Image>().color = new Color(139 / 255f, 69 / 255f, 19 / 255f, 0.25f);
                        modLabel.color = new Color(139 / 255f, 69 / 255f, 19 / 255f, 1f);
                        modLabel.text  = modEntry.Name + "\nUMF Mod";
                        break;

                    case ModMenuEntryType.DISABLED_UMF:
                        modSelected.color = new Color(69 / 255f, 34 / 255f, 9 / 255f, 1f);
                        modToggle.GetComponent <Image>().color = new Color(69 / 255f, 34 / 255f, 9 / 255f, 0.25f);
                        modLabel.color = new Color(139 / 255f, 69 / 255f, 19 / 255f, 0.5f);
                        modLabel.text  = modEntry.Name + "\nDisabled UMF Mod";
                        break;

                    case ModMenuEntryType.INCOMPATIBLE_UMF:
                        modSelected.color = new Color(210 / 255f, 105 / 255f, 30 / 255f, 1f);
                        modToggle.GetComponent <Image>().color = new Color(210 / 255f, 105 / 255f, 30 / 255f, 0.25f);
                        modLabel.color = new Color(139 / 255f, 69 / 255f, 19 / 255f, 0.5f);
                        modLabel.text  = modEntry.Name + "\nIncompatible UMF Mod";
                        break;
                    }

                    for (int g = 0; g < gadgetCount; g++)
                    {
                        GadgetInfo    gadget     = modEntry.Gadgets[g];
                        RectTransform gadgetRect = new GameObject("Gadget: " + gadget.Attribute.Name, typeof(RectTransform), typeof(Toggle), typeof(CanvasRenderer), typeof(Image), typeof(Mask)).GetComponent <RectTransform>();
                        gadgetRect.SetParent(modEntryRect);
                        gadgetRect.anchorMin = new Vector2(0f, 1 - ((g + 2) * gadgetHeight));
                        gadgetRect.anchorMax = new Vector2(1f, 1 - ((g + 1) * gadgetHeight));
                        gadgetRect.offsetMin = new Vector2(10f, 10f);
                        gadgetRect.offsetMax = new Vector2(-10f, 10f);
                        Toggle gadgetToggle   = gadgetRect.GetComponent <Toggle>();
                        Image  gadgetSelected = new GameObject("Selected", typeof(RectTransform), typeof(CanvasRenderer), typeof(Image)).GetComponent <Image>();
                        gadgetSelected.rectTransform.SetParent(gadgetRect);
                        gadgetSelected.rectTransform.anchorMin = new Vector2(0f, 0f);
                        gadgetSelected.rectTransform.anchorMax = new Vector2(1f, 1f);
                        gadgetSelected.rectTransform.offsetMin = Vector2.zero;
                        gadgetSelected.rectTransform.offsetMax = Vector2.zero;
                        gadgetSelected.sprite = SceneInjector.BoxSprite;
                        gadgetSelected.type   = Image.Type.Sliced;
                        gadgetSelected.color  = new Color(1f, 1f, 0.5f, 1f);
                        Text gadgetLabel = new GameObject("Label", typeof(RectTransform), typeof(CanvasRenderer), typeof(Text)).GetComponent <Text>();
                        gadgetLabel.rectTransform.SetParent(gadgetRect);
                        gadgetLabel.rectTransform.anchorMin = new Vector2(0f, 0f);
                        gadgetLabel.rectTransform.anchorMax = new Vector2(1f, 1f);
                        gadgetLabel.rectTransform.offsetMin = new Vector2(10, 10);
                        gadgetLabel.rectTransform.offsetMax = new Vector2(-10, -10);
                        gadgetLabel.font                           = SceneInjector.ModConfigMenuText.GetComponent <TextMesh>().font;
                        gadgetLabel.fontSize                       = 12;
                        gadgetLabel.horizontalOverflow             = HorizontalWrapMode.Overflow;
                        gadgetLabel.verticalOverflow               = VerticalWrapMode.Overflow;
                        gadgetLabel.alignment                      = TextAnchor.MiddleLeft;
                        gadgetLabel.text                           = gadget.Attribute.Name + "\n" + (gadget.Gadget.Enabled ? "Enabled" : "Disabled");
                        gadgetLabel.color                          = gadget.Gadget.Enabled ? new Color(1f, 1f, 1f, 1f) : new Color(1f, 1f, 1f, 0.5f);
                        gadgetToggle.GetComponent <Image>().sprite = SceneInjector.BoxSprite;
                        gadgetToggle.GetComponent <Image>().type   = Image.Type.Sliced;
                        gadgetToggle.transition                    = Selectable.Transition.None;
                        gadgetToggle.isOn                          = i == modIndex && g == this.gadgetIndex;
                        gadgetToggle.toggleTransition              = Toggle.ToggleTransition.None;
                        gadgetToggle.graphic                       = gadgetSelected;
                        gadgetToggle.group                         = modList.GetComponent <ToggleGroup>();
                        gadgetToggle.GetComponent <Image>().color  = new Color(1f, 1f, 0.5f, 0.25f);
                        int gadgetIndex = g;
                        gadgetToggle.onValueChanged.AddListener((toggled) => { if (toggled)
                                                                               {
                                                                                   UpdateInfo(modToggle, toggleIndex, gadgetIndex); GadgetCoreAPI.CloseDialog();
                                                                               }
                                                                });
                        bonusOffset++;
                    }
                }
                else
                {
                    RectTransform modEntryRect = new GameObject("Mod Entry: " + modEntry.Name, typeof(RectTransform), typeof(Toggle), typeof(CanvasRenderer), typeof(Image), typeof(Mask)).GetComponent <RectTransform>();
                    modEntryRect.SetParent(modList);
                    modEntryRect.anchorMin = new Vector2(0f, 1 - ((i + bonusOffset + 1) * entryHeight));
                    modEntryRect.anchorMax = new Vector2(1f, 1 - ((i + bonusOffset) * entryHeight));
                    modEntryRect.offsetMin = Vector2.zero;
                    modEntryRect.offsetMax = Vector2.zero;
                    Toggle modToggle   = modEntryRect.GetComponent <Toggle>();
                    Image  modSelected = new GameObject("Selected", typeof(RectTransform), typeof(CanvasRenderer), typeof(Image)).GetComponent <Image>();
                    modSelected.rectTransform.SetParent(modEntryRect);
                    modSelected.rectTransform.anchorMin = new Vector2(0f, 0f);
                    modSelected.rectTransform.anchorMax = new Vector2(1f, 1f);
                    modSelected.rectTransform.offsetMin = Vector2.zero;
                    modSelected.rectTransform.offsetMax = Vector2.zero;
                    modSelected.sprite = SceneInjector.BoxSprite;
                    modSelected.type   = Image.Type.Sliced;
                    Text modLabel = new GameObject("Label", typeof(RectTransform), typeof(CanvasRenderer), typeof(Text)).GetComponent <Text>();
                    modLabel.rectTransform.SetParent(modEntryRect);
                    modLabel.rectTransform.anchorMin = new Vector2(0f, 0f);
                    modLabel.rectTransform.anchorMax = new Vector2(1f, 1f);
                    modLabel.rectTransform.offsetMin = new Vector2(10, 10);
                    modLabel.rectTransform.offsetMax = new Vector2(-10, -10);
                    modLabel.font                           = SceneInjector.ModConfigMenuText.GetComponent <TextMesh>().font;
                    modLabel.fontSize                       = 12;
                    modLabel.horizontalOverflow             = HorizontalWrapMode.Overflow;
                    modLabel.verticalOverflow               = VerticalWrapMode.Overflow;
                    modLabel.alignment                      = TextAnchor.MiddleLeft;
                    modToggle.GetComponent <Image>().sprite = SceneInjector.BoxSprite;
                    modToggle.GetComponent <Image>().type   = Image.Type.Sliced;
                    modToggle.transition                    = Selectable.Transition.None;
                    modToggle.isOn                          = i == modIndex;
                    modToggle.toggleTransition              = Toggle.ToggleTransition.None;
                    modToggle.graphic                       = modSelected;
                    modToggle.group                         = modList.GetComponent <ToggleGroup>();
                    int toggleIndex = i;
                    if (i == modIndex)
                    {
                        selectedToggle = modToggle;
                    }
                    modToggle.onValueChanged.AddListener((toggled) => { if (toggled)
                                                                        {
                                                                            UpdateInfo(modToggle, toggleIndex); GadgetCoreAPI.CloseDialog();
                                                                        }
                                                         });

                    switch (modEntry.Type)
                    {
                    case ModMenuEntryType.EMPTY_GADGET:
                        modSelected.color = new Color(0.5f, 0.5f, 0.125f, 1f);
                        modToggle.GetComponent <Image>().color = new Color(0.5f, 0.5f, 0.125f, 0.25f);
                        modLabel.color = new Color(1f, 1f, 1f, 0.5f);
                        modLabel.text  = modEntry.Name + "\nEmpty Gadget Mod";
                        break;

                    case ModMenuEntryType.INCOMPATIBLE_GADGET:
                        modSelected.color = new Color(1f, 0.5f, 0f, 1f);
                        modToggle.GetComponent <Image>().color = new Color(1f, 0.5f, 0f, 0.25f);
                        modLabel.color = new Color(1f, 1f, 1f, 0.5f);
                        modLabel.text  = modEntry.Name + "\nIncompatible Gadget Mod";
                        break;

                    case ModMenuEntryType.ERRORED_GADGET:
                        modSelected.color = new Color(1f, 0f, 0f, 1f);
                        modToggle.GetComponent <Image>().color = new Color(1f, 0f, 0f, 0.25f);
                        modLabel.color = new Color(1f, 1f, 1f, 0.5f);
                        modLabel.text  = modEntry.Name + "\nErrored Gadget Mod";
                        break;

                    case ModMenuEntryType.UMF:
                        modSelected.color = new Color(139 / 255f, 69 / 255f, 19 / 255f, 1f);
                        modToggle.GetComponent <Image>().color = new Color(139 / 255f, 69 / 255f, 19 / 255f, 0.25f);
                        modLabel.color = new Color(139 / 255f, 69 / 255f, 19 / 255f, 1f);
                        modLabel.text  = modEntry.Name + "\nUMF Mod";
                        break;

                    case ModMenuEntryType.DISABLED_UMF:
                        modSelected.color = new Color(69 / 255f, 34 / 255f, 9 / 255f, 1f);
                        modToggle.GetComponent <Image>().color = new Color(69 / 255f, 34 / 255f, 9 / 255f, 0.25f);
                        modLabel.color = new Color(139 / 255f, 69 / 255f, 19 / 255f, 0.5f);
                        modLabel.text  = modEntry.Name + "\nDisabled UMF Mod";
                        break;

                    case ModMenuEntryType.INCOMPATIBLE_UMF:
                        modSelected.color = new Color(210 / 255f, 105 / 255f, 30 / 255f, 1f);
                        modToggle.GetComponent <Image>().color = new Color(210 / 255f, 105 / 255f, 30 / 255f, 0.25f);
                        modLabel.color = new Color(139 / 255f, 69 / 255f, 19 / 255f, 0.5f);
                        modLabel.text  = modEntry.Name + "\nIncompatible UMF Mod";
                        break;
                    }
                }
            }

            modListScrollView.GetComponent <RectTransform>().localScale = Vector3.one;
            if (scrollPositionCache >= 0)
            {
                modListScrollView.verticalNormalizedPosition = 1 - (scrollPositionCache / (modList.anchorMax.y - modList.anchorMin.y));
            }
            scrollPositionCache = -1;

            if (GadgetModConfigs.GetConfigMenuObject(0) != null)
            {
                GadgetModConfigs.ResetAllConfigMenus();
            }
            else
            {
                GadgetModConfigs.BuildConfigMenus(SceneInjector.ModConfigMenus);
            }

            if (modCount > 0)
            {
                UpdateInfo(selectedToggle, modIndex, gadgetIndex);
            }
        }
Ejemplo n.º 2
0
        private static void BuildPersistantCanvas()
        {
            GadgetCore.CoreLogger.Log("Building Persistant Canvas");
            EventSystem eventSystem = new GameObject("EventSystem", typeof(EventSystem), typeof(StandaloneInputModule)).GetComponent <EventSystem>();

            UnityEngine.Object.DontDestroyOnLoad(eventSystem.gameObject);
            StandaloneInputModule inputModule = eventSystem.GetComponent <StandaloneInputModule>();

            inputModule.horizontalAxis = "Horizontal1";
            inputModule.verticalAxis   = "Vertical1";
            inputModule.submitButton   = "Jump";
            inputModule.cancelButton   = "Cancel";

            PersistantCanvas = new GameObject("Persistant Canvas", typeof(RectTransform), typeof(Canvas), typeof(CanvasScaler), typeof(GraphicRaycaster), typeof(CanvasGroup)).GetComponent <Canvas>();
            UnityEngine.Object.DontDestroyOnLoad(PersistantCanvas.gameObject);
            PersistantCanvas.GetComponent <CanvasGroup>().alpha          = 1;
            PersistantCanvas.GetComponent <CanvasGroup>().interactable   = true;
            PersistantCanvas.GetComponent <CanvasGroup>().blocksRaycasts = true;
            PersistantCanvas.GetComponent <RectTransform>().pivot        = new Vector2(0.5f, 0.5f);
            PersistantCanvas.sortingOrder = 100;
            PersistantCanvas.renderMode   = RenderMode.ScreenSpaceOverlay;
            PersistantCanvas.pixelPerfect = true;
            CanvasScaler scaler = PersistantCanvas.GetComponent <CanvasScaler>();

            scaler.uiScaleMode            = CanvasScaler.ScaleMode.ConstantPixelSize;
            scaler.referencePixelsPerUnit = 100;

            GadgetConsole console = new GameObject("Console", typeof(RectTransform), typeof(GadgetConsole)).GetComponent <GadgetConsole>();

            console.gameObject.SetActive(false);
            console.transform.SetParent(PersistantCanvas.transform);
            console.GetComponent <RectTransform>().anchorMin = new Vector2(0f, 0f);
            console.GetComponent <RectTransform>().anchorMax = new Vector2(1f, 1f);
            console.GetComponent <RectTransform>().offsetMin = new Vector2(10f, 10f);
            console.GetComponent <RectTransform>().offsetMax = new Vector2(-10f, -10f);
            console.InputField = new GameObject("Input Field", typeof(RectTransform), typeof(InputField), typeof(CanvasRenderer), typeof(Image)).GetComponent <InputField>();
            console.InputField.GetComponent <RectTransform>().SetParent(console.transform);
            console.InputField.GetComponent <RectTransform>().anchorMin = new Vector2(0.1f, 0.1f);
            console.InputField.GetComponent <RectTransform>().anchorMax = new Vector2(0.9f, 0.1f);
            console.InputField.GetComponent <RectTransform>().offsetMin = new Vector2(0f, 0f);
            console.InputField.GetComponent <RectTransform>().offsetMax = new Vector2(0f, 50f);
            console.InputField.GetComponent <Image>().sprite            = BoxSprite;
            console.InputField.GetComponent <Image>().type       = Image.Type.Sliced;
            console.InputField.GetComponent <Image>().fillCenter = true;
            Text inputText = new GameObject("Text", typeof(RectTransform), typeof(CanvasRenderer), typeof(Text)).GetComponent <Text>();

            inputText.rectTransform.SetParent(console.InputField.transform);
            inputText.rectTransform.anchorMin = new Vector2(0f, 0f);
            inputText.rectTransform.anchorMax = new Vector2(1f, 1f);
            inputText.rectTransform.offsetMin = new Vector2(10f, 0f);
            inputText.rectTransform.offsetMax = new Vector2(-10f, 0f);
            inputText.alignment          = TextAnchor.MiddleLeft;
            inputText.horizontalOverflow = HorizontalWrapMode.Overflow;
            inputText.font     = ModMenuPanel.descText.font;
            inputText.fontSize = 24;
            console.InputField.textComponent = inputText;

            ScrollRect scrollableChat = new GameObject("Scrollable Chat", typeof(RectTransform), typeof(ScrollRect)).GetComponent <ScrollRect>();

            scrollableChat.GetComponent <RectTransform>().SetParent(console.transform);
            scrollableChat.GetComponent <RectTransform>().anchorMin = new Vector2(0.1f, 0.1f);
            scrollableChat.GetComponent <RectTransform>().anchorMax = new Vector2(0.9f, 1f);
            scrollableChat.GetComponent <RectTransform>().offsetMin = new Vector2(0f, 50f);
            scrollableChat.GetComponent <RectTransform>().offsetMax = new Vector2(0f, 0f);
            scrollableChat.GetComponent <RectTransform>().pivot     = new Vector2(0.5f, 0.5f);
            scrollableChat.inertia                     = false;
            scrollableChat.horizontal                  = false;
            scrollableChat.scrollSensitivity           = 5;
            scrollableChat.verticalScrollbarVisibility = ScrollRect.ScrollbarVisibility.AutoHide;
            scrollableChat.movementType                = ScrollRect.MovementType.Clamped;
            Scrollbar chatScrollBar = new GameObject("Scrollbar", typeof(RectTransform), typeof(CanvasRenderer), typeof(Image), typeof(Scrollbar)).GetComponent <Scrollbar>();

            chatScrollBar.GetComponent <RectTransform>().SetParent(console.transform);
            chatScrollBar.GetComponent <RectTransform>().anchorMin = new Vector2(1f, 0.1f);
            chatScrollBar.GetComponent <RectTransform>().anchorMax = new Vector2(1f, 1f);
            chatScrollBar.GetComponent <RectTransform>().offsetMin = new Vector2(-50f, 0f);
            chatScrollBar.GetComponent <RectTransform>().offsetMax = new Vector2(0f, 0f);
            chatScrollBar.GetComponent <Image>().sprite            = BoxSprite;
            chatScrollBar.GetComponent <Image>().type       = Image.Type.Sliced;
            chatScrollBar.GetComponent <Image>().fillCenter = true;
            RectTransform charScrollBarHandle = new GameObject("Handle", typeof(RectTransform), typeof(CanvasRenderer), typeof(Image)).GetComponent <RectTransform>();

            charScrollBarHandle.SetParent(chatScrollBar.transform);
            charScrollBarHandle.anchorMin = new Vector2(0.05f, 0.05f);
            charScrollBarHandle.anchorMax = new Vector2(0.95f, 0.95f);
            charScrollBarHandle.offsetMin = Vector2.zero;
            charScrollBarHandle.offsetMax = Vector2.zero;
            charScrollBarHandle.GetComponent <Image>().sprite     = BoxSprite;
            charScrollBarHandle.GetComponent <Image>().type       = Image.Type.Sliced;
            charScrollBarHandle.GetComponent <Image>().fillCenter = true;
            chatScrollBar.targetGraphic = charScrollBarHandle.GetComponent <Image>();
            chatScrollBar.handleRect    = charScrollBarHandle;
            chatScrollBar.direction     = Scrollbar.Direction.BottomToTop;
            Mask chatViewport = new GameObject("Viewport", typeof(RectTransform), typeof(Mask), typeof(CanvasRenderer), typeof(Image)).GetComponent <Mask>();

            chatViewport.rectTransform.SetParent(scrollableChat.transform);
            chatViewport.rectTransform.anchorMin           = new Vector2(0f, 0f);
            chatViewport.rectTransform.anchorMax           = new Vector2(1f, 1f);
            chatViewport.rectTransform.offsetMin           = new Vector2(0f, 0f);
            chatViewport.rectTransform.offsetMax           = new Vector2(0f, 0f);
            chatViewport.rectTransform.pivot               = Vector2.zero;
            chatViewport.showMaskGraphic                   = false;
            chatViewport.GetComponent <Image>().sprite     = BoxSprite;
            chatViewport.GetComponent <Image>().type       = Image.Type.Sliced;
            chatViewport.GetComponent <Image>().fillCenter = true;
            console.TextPanel = new GameObject("Content", typeof(RectTransform), typeof(VerticalLayoutGroup), typeof(ContentSizeFitter)).GetComponent <RectTransform>();
            console.TextPanel.SetParent(chatViewport.rectTransform);
            console.TextPanel.anchorMin = new Vector2(0f, 0f);
            console.TextPanel.anchorMax = new Vector2(1f, 1f);
            console.TextPanel.offsetMin = new Vector2(0f, 0f);
            console.TextPanel.offsetMax = new Vector2(0f, 0f);
            console.TextPanel.pivot     = Vector2.zero;
            console.TextPanel.GetComponent <VerticalLayoutGroup>().padding                = new RectOffset(10, 10, 10, 10);
            console.TextPanel.GetComponent <VerticalLayoutGroup>().spacing                = 10;
            console.TextPanel.GetComponent <VerticalLayoutGroup>().childAlignment         = TextAnchor.LowerLeft;
            console.TextPanel.GetComponent <VerticalLayoutGroup>().childControlWidth      = true;
            console.TextPanel.GetComponent <VerticalLayoutGroup>().childControlHeight     = true;
            console.TextPanel.GetComponent <VerticalLayoutGroup>().childForceExpandWidth  = true;
            console.TextPanel.GetComponent <VerticalLayoutGroup>().childForceExpandHeight = true;
            console.TextPanel.GetComponent <ContentSizeFitter>().horizontalFit            = ContentSizeFitter.FitMode.Unconstrained;
            console.TextPanel.GetComponent <ContentSizeFitter>().verticalFit              = ContentSizeFitter.FitMode.PreferredSize;
            scrollableChat.verticalScrollbar = chatScrollBar;
            scrollableChat.viewport          = chatViewport.rectTransform;
            scrollableChat.content           = console.TextPanel;
            console.AlwaysActivePanel        = new GameObject("Always Active Content", typeof(RectTransform), typeof(VerticalLayoutGroup), typeof(ContentSizeFitter)).GetComponent <RectTransform>();
            console.AlwaysActivePanel.SetParent(PersistantCanvas.transform);
            console.AlwaysActivePanel.anchorMin = new Vector2(0.1f, 0.1f);
            console.AlwaysActivePanel.anchorMax = new Vector2(0.9f, 1f);
            console.AlwaysActivePanel.offsetMin = new Vector2(10f, 60f);
            console.AlwaysActivePanel.offsetMax = new Vector2(-10f, -10f);
            console.AlwaysActivePanel.pivot     = Vector2.zero;
            console.AlwaysActivePanel.GetComponent <VerticalLayoutGroup>().padding                = new RectOffset(10, 10, 10, 10);
            console.AlwaysActivePanel.GetComponent <VerticalLayoutGroup>().spacing                = 10;
            console.AlwaysActivePanel.GetComponent <VerticalLayoutGroup>().childAlignment         = TextAnchor.LowerLeft;
            console.AlwaysActivePanel.GetComponent <VerticalLayoutGroup>().childControlWidth      = true;
            console.AlwaysActivePanel.GetComponent <VerticalLayoutGroup>().childControlHeight     = true;
            console.AlwaysActivePanel.GetComponent <VerticalLayoutGroup>().childForceExpandWidth  = true;
            console.AlwaysActivePanel.GetComponent <VerticalLayoutGroup>().childForceExpandHeight = true;
            console.AlwaysActivePanel.GetComponent <ContentSizeFitter>().horizontalFit            = ContentSizeFitter.FitMode.Unconstrained;
            console.AlwaysActivePanel.GetComponent <ContentSizeFitter>().verticalFit              = ContentSizeFitter.FitMode.PreferredSize;

            GadgetConsole.PrintQueuedMessages();

            ConfirmationDialog = new GameObject("Confirmation Dialog", typeof(RectTransform), typeof(CanvasRenderer), typeof(Image));
            ConfirmationDialog.GetComponent <RectTransform>().SetParent(PersistantCanvas.transform);
            ConfirmationDialog.GetComponent <RectTransform>().anchorMin = new Vector2(0.25f, 0.25f);
            ConfirmationDialog.GetComponent <RectTransform>().anchorMax = new Vector2(0.75f, 0.75f);
            ConfirmationDialog.GetComponent <RectTransform>().offsetMin = new Vector2(10, 10);
            ConfirmationDialog.GetComponent <RectTransform>().offsetMax = new Vector2(-10, -10);
            ConfirmationDialog.GetComponent <Image>().sprite            = BoxSprite;
            ConfirmationDialog.GetComponent <Image>().type       = Image.Type.Sliced;
            ConfirmationDialog.GetComponent <Image>().fillCenter = true;
            ConfirmationDialog.SetActive(false);
            Button yesButton = new GameObject("Yes Button", typeof(RectTransform), typeof(CanvasRenderer), typeof(Image), typeof(Button)).GetComponent <Button>();

            yesButton.GetComponent <RectTransform>().SetParent(ConfirmationDialog.transform);
            yesButton.GetComponent <RectTransform>().anchorMin = new Vector2(0f, 0f);
            yesButton.GetComponent <RectTransform>().anchorMax = new Vector2(0.5f, 0.25f);
            yesButton.GetComponent <RectTransform>().offsetMin = new Vector2(10, 10);
            yesButton.GetComponent <RectTransform>().offsetMax = new Vector2(-10, -10);
            yesButton.GetComponent <Image>().sprite            = BoxSprite;
            yesButton.GetComponent <Image>().type       = Image.Type.Sliced;
            yesButton.GetComponent <Image>().fillCenter = true;
            yesButton.targetGraphic = yesButton.GetComponent <Image>();
            ConfirmationYesText     = new GameObject("Text", typeof(RectTransform), typeof(CanvasRenderer), typeof(Text)).GetComponent <Text>();
            ConfirmationYesText.rectTransform.SetParent(yesButton.transform);
            ConfirmationYesText.rectTransform.anchorMin = new Vector2(0f, 0f);
            ConfirmationYesText.rectTransform.anchorMax = new Vector2(1f, 1f);
            ConfirmationYesText.rectTransform.offsetMin = Vector2.zero;
            ConfirmationYesText.rectTransform.offsetMax = Vector2.zero;
            ConfirmationYesText.alignment = TextAnchor.MiddleCenter;
            ConfirmationYesText.font      = ModMenuPanel.descText.font;
            ConfirmationYesText.fontSize  = 24;
            ConfirmationYesText.text      = "Yes";
            Button noButton = new GameObject("No Button", typeof(RectTransform), typeof(CanvasRenderer), typeof(Image), typeof(Button)).GetComponent <Button>();

            noButton.GetComponent <RectTransform>().SetParent(ConfirmationDialog.transform);
            noButton.GetComponent <RectTransform>().anchorMin = new Vector2(0.5f, 0f);
            noButton.GetComponent <RectTransform>().anchorMax = new Vector2(1f, 0.25f);
            noButton.GetComponent <RectTransform>().offsetMin = new Vector2(10, 10);
            noButton.GetComponent <RectTransform>().offsetMax = new Vector2(-10, -10);
            noButton.GetComponent <Image>().sprite            = BoxSprite;
            noButton.GetComponent <Image>().type       = Image.Type.Sliced;
            noButton.GetComponent <Image>().fillCenter = true;
            noButton.targetGraphic = noButton.GetComponent <Image>();
            ConfirmationNoText     = new GameObject("Text", typeof(RectTransform), typeof(CanvasRenderer), typeof(Text)).GetComponent <Text>();
            ConfirmationNoText.rectTransform.SetParent(noButton.transform);
            ConfirmationNoText.rectTransform.anchorMin = new Vector2(0f, 0f);
            ConfirmationNoText.rectTransform.anchorMax = new Vector2(1f, 1f);
            ConfirmationNoText.rectTransform.offsetMin = Vector2.zero;
            ConfirmationNoText.rectTransform.offsetMax = Vector2.zero;
            ConfirmationNoText.alignment = TextAnchor.MiddleCenter;
            ConfirmationNoText.font      = ModMenuPanel.descText.font;
            ConfirmationNoText.fontSize  = 24;
            ConfirmationNoText.text      = "No";
            yesButton.onClick.AddListener(() => {
                GadgetCoreAPI.CloseDialog();
                ConfirmationYesAction?.Invoke();
            });
            noButton.onClick.AddListener(() => {
                GadgetCoreAPI.CloseDialog();
                ConfirmationNoAction?.Invoke();
            });
            ConfirmationText = new GameObject("Text", typeof(RectTransform), typeof(CanvasRenderer), typeof(Text)).GetComponent <Text>();
            ConfirmationText.rectTransform.SetParent(ConfirmationDialog.transform);
            ConfirmationText.rectTransform.anchorMin = new Vector2(0f, 0.25f);
            ConfirmationText.rectTransform.anchorMax = new Vector2(1f, 1f);
            ConfirmationText.rectTransform.offsetMin = Vector2.zero;
            ConfirmationText.rectTransform.offsetMax = Vector2.zero;
            ConfirmationText.alignment = TextAnchor.MiddleCenter;
            ConfirmationText.font      = ModMenuPanel.descText.font;
            ConfirmationText.fontSize  = 24;
            ConfirmationText.text      = "";
        }