Esempio n. 1
0
        public override void OnInitialize()
        {
            base.OnInitialize();

            string text = _mod.DisplayName + " v" + _mod.modFile.version;

            if (_mod.tModLoaderVersion < new Version(0, 10))
            {
                text += $" [c/FF0000:({Language.GetTextValue("tModLoader.ModOldWarning")})]";
            }

            if (_mod.modFile.HasFile("icon.png"))
            {
                try {
                    Texture2D modIconTexture;
                    using (_mod.modFile.Open())
                        using (var s = _mod.modFile.GetStream("icon.png"))
                            modIconTexture = Texture2D.FromStream(Main.instance.GraphicsDevice, s);

                    if (modIconTexture.Width == 80 && modIconTexture.Height == 80)
                    {
                        _modIcon = new UIImage(modIconTexture)
                        {
                            Left = { Percent = 0f },
                            Top  = { Percent = 0f }
                        };
                        Append(_modIcon);
                        _modIconAdjust += 85;
                    }
                }
                catch (Exception e) {
                    Logging.tML.Error("Unknown error", e);
                }
            }

            _modName = new UIText(text)
            {
                Left = new StyleDimension(_modIconAdjust, 0f),
                Top  = { Pixels = 5 }
            };
            Append(_modName);

            _uiModStateText = new UIModStateText(_mod.Enabled)
            {
                Top  = { Pixels = 40 },
                Left = { Pixels = _modIconAdjust }
            };
            _uiModStateText.OnClick += ToggleEnabled;
            Append(_uiModStateText);

            _moreInfoButton = new UIImage(UICommon.ButtonModInfoTexture)
            {
                Width  = { Pixels = 36 },
                Height = { Pixels = 36 },
                Left   = { Pixels = -36, Precent = 1 },
                Top    = { Pixels = 40 }
            };
            _moreInfoButton.OnClick += ShowMoreInfo;
            Append(_moreInfoButton);

            Mod loadedMod = ModLoader.GetMod(_mod.Name);

            if (loadedMod != null && ConfigManager.Configs.ContainsKey(loadedMod))
            {
                _configButton = new UIImage(UICommon.ButtonModConfigTexture)
                {
                    Width  = { Pixels = 36 },
                    Height = { Pixels = 36f },
                    Left   = { Pixels = _moreInfoButton.Left.Pixels - 36 - PADDING, Precent = 1f },
                    Top    = { Pixels = 40f }
                };
                _configButton.OnClick += OpenConfig;
                Append(_configButton);
                if (ConfigManager.ModNeedsReload(loadedMod))
                {
                    _configChangesRequireReload = true;
                }
            }

            _modReferences = _mod.properties.modReferences.Select(x => x.mod).ToArray();
            if (_modReferences.Length > 0 && !_mod.Enabled)
            {
                string refs = string.Join(", ", _mod.properties.modReferences);
                var    icon = UICommon.ButtonExclamationTexture;
                _modReferenceIcon = new UIHoverImage(icon, Language.GetTextValue("tModLoader.ModDependencyClickTooltip", refs))
                {
                    Left = new StyleDimension(_uiModStateText.Left.Pixels + _uiModStateText.Width.Pixels + PADDING, 0f),
                    Top  = { Pixels = 42.5f }
                };
                _modReferenceIcon.OnClick += EnableDependencies;
                Append(_modReferenceIcon);
            }
            if (_mod.modFile.ValidModBrowserSignature)
            {
                _keyImage = new UIHoverImage(Main.itemTexture[ItemID.GoldenKey], Language.GetTextValue("tModLoader.ModsOriginatedFromModBrowser"))
                {
                    Left = { Pixels = -20, Percent = 1f }
                };
                Append(_keyImage);
            }
            if (ModLoader.badUnloaders.Contains(_mod.Name))
            {
                _keyImage = new UIHoverImage(UICommon.ButtonErrorTexture, "This mod did not fully unload during last unload.")
                {
                    Left = { Pixels = _modIconAdjust + PADDING },
                    Top  = { Pixels = 3 }
                };
                Append(_keyImage);
                _modName.Left.Pixels += _keyImage.Width.Pixels + PADDING * 2f;
            }
            if (_mod.properties.beta)
            {
                _keyImage = new UIHoverImage(Main.itemTexture[ItemID.ShadowKey], Language.GetTextValue("tModLoader.BetaModCantPublish"))
                {
                    Left = { Pixels = -10, Percent = 1f }
                };
                Append(_keyImage);
            }

            if (loadedMod != null)
            {
                _loaded = true;
                int[]    values           = { loadedMod.items.Count, loadedMod.npcs.Count, loadedMod.tiles.Count, loadedMod.walls.Count, loadedMod.buffs.Count, loadedMod.mountDatas.Count };
                string[] localizationKeys = { "ModsXItems", "ModsXNPCs", "ModsXTiles", "ModsXWalls", "ModsXBuffs", "ModsXMounts" };
                int      xOffset          = -40;
                for (int i = 0; i < values.Length; i++)
                {
                    if (values[i] > 0)
                    {
                        Texture2D iconTexture = Main.instance.infoIconTexture[i];
                        _keyImage = new UIHoverImage(iconTexture, Language.GetTextValue($"tModLoader.{localizationKeys[i]}", values[i]))
                        {
                            Left = { Pixels = xOffset, Percent = 1f }
                        };
                        Append(_keyImage);
                        xOffset -= 18;
                    }
                }
            }

            OnDoubleClick += (e, el) => {
                // Only trigger if we didn't target the ModStateText, otherwise we trigger this behavior twice
                if (e.Target.GetType() != typeof(UIModStateText))
                {
                    _uiModStateText.Click(e);
                }
            };
        }
Esempio n. 2
0
        public override void OnInitialize()
        {
            base.OnInitialize();

            string text = _mod.DisplayName + " v" + _mod.modFile.Version;

            if (_mod.tModLoaderVersion < new Version(0, 10))
            {
                text += $" [c/FF0000:({Language.GetTextValue("tModLoader.ModOldWarning")})]";
            }

            if (_mod.modFile.HasFile("icon.png"))
            {
                try {
                    Asset <Texture2D> modIconTexture;

                    using (_mod.modFile.Open())
                        using (var s = _mod.modFile.GetStream("icon.png"))
                            modIconTexture = ModLoader.ManifestAssets.CreateUntrackedAsset(
                                $"Terraria.ModLoader.UI.Browser.{_mod.Name}.icon.png",
                                Texture2D.FromStream(Main.instance.GraphicsDevice, s)
                                );

                    if (modIconTexture.Width() == 80 && modIconTexture.Height() == 80)
                    {
                        _modIcon = new UIImage(modIconTexture)
                        {
                            Left = { Percent = 0f },
                            Top  = { Percent = 0f }
                        };
                        Append(_modIcon);
                        _modIconAdjust += 85;
                    }
                }
                catch (Exception e) {
                    Logging.tML.Error("Unknown error", e);
                }
            }

            _modName = new UIText(text)
            {
                Left = new StyleDimension(_modIconAdjust, 0f),
                Top  = { Pixels = 5 }
            };
            Append(_modName);

            _uiModStateText = new UIModStateText(_mod.Enabled)
            {
                Top  = { Pixels = 40 },
                Left = { Pixels = _modIconAdjust }
            };
            _uiModStateText.OnClick += ToggleEnabled;
            Append(_uiModStateText);

            _moreInfoButton = new UIImage(UICommon.ButtonModInfoTexture)
            {
                Width  = { Pixels = 36 },
                Height = { Pixels = 36 },
                Left   = { Pixels = -36, Precent = 1 },
                Top    = { Pixels = 40 }
            };
            _moreInfoButton.OnClick += ShowMoreInfo;
            Append(_moreInfoButton);

            if (ModLoader.TryGetMod(ModName, out var loadedMod) && ConfigManager.Configs.ContainsKey(loadedMod))
            {
                _configButton = new UIImage(UICommon.ButtonModConfigTexture)
                {
                    Width  = { Pixels = 36 },
                    Height = { Pixels = 36f },
                    Left   = { Pixels = _moreInfoButton.Left.Pixels - 36 - PADDING, Precent = 1f },
                    Top    = { Pixels = 40f }
                };
                _configButton.OnClick += OpenConfig;
                Append(_configButton);
                if (ConfigManager.ModNeedsReload(loadedMod))
                {
                    _configChangesRequireReload = true;
                }
            }

            _modReferences = _mod.properties.modReferences.Select(x => x.mod).ToArray();

            if (_modReferences.Length > 0 && !_mod.Enabled)
            {
                string refs = string.Join(", ", _mod.properties.modReferences);
                var    icon = UICommon.ButtonExclamationTexture;
                _modReferenceIcon = new UIHoverImage(icon, Language.GetTextValue("tModLoader.ModDependencyClickTooltip", refs))
                {
                    Left = new StyleDimension(_uiModStateText.Left.Pixels + _uiModStateText.Width.Pixels + PADDING, 0f),
                    Top  = { Pixels = 42.5f }
                };
                _modReferenceIcon.OnClick += EnableDependencies;

                Append(_modReferenceIcon);
            }

            if (_mod.modFile.ValidModBrowserSignature)
            {
                _keyImage = new UIHoverImage(Main.Assets.Request <Texture2D>(TextureAssets.Item[ItemID.GoldenKey].Name), Language.GetTextValue("tModLoader.ModsOriginatedFromModBrowser"))
                {
                    Left = { Pixels = -20, Percent = 1f }
                };

                Append(_keyImage);
            }

            if (ModCompile.DeveloperMode && ModLoader.badUnloaders.Contains(ModName))
            {
                _keyImage = new UIHoverImage(UICommon.ButtonErrorTexture, Language.GetTextValue("tModLoader.ModDidNotFullyUnloadWarning"))
                {
                    Left = { Pixels = _modIconAdjust + PADDING },
                    Top  = { Pixels = 3 }
                };

                Append(_keyImage);

                _modName.Left.Pixels += _keyImage.Width.Pixels + PADDING * 2f;
            }

            if (_mod.properties.beta)
            {
                _keyImage = new UIHoverImage(Main.Assets.Request <Texture2D>(TextureAssets.Item[ItemID.ShadowKey].Name), Language.GetTextValue("tModLoader.BetaModCantPublish"))
                {
                    Left = { Pixels = -10, Percent = 1f }
                };

                Append(_keyImage);
            }

            if (loadedMod != null)
            {
                _loaded = true;
                // TODO: refactor and add nicer icons (and maybe not iterate 6 times)
                int[]    values           = { loadedMod.GetContent <ModItem>().Count(), loadedMod.GetContent <ModNPC>().Count(), loadedMod.GetContent <ModTile>().Count(), loadedMod.GetContent <ModWall>().Count(), loadedMod.GetContent <ModBuff>().Count(), loadedMod.GetContent <ModMount>().Count() };
                string[] localizationKeys = { "ModsXItems", "ModsXNPCs", "ModsXTiles", "ModsXWalls", "ModsXBuffs", "ModsXMounts" };
                int      xOffset          = -40;

                for (int i = 0; i < values.Length; i++)
                {
                    if (values[i] > 0)
                    {
                        _keyImage = new UIHoverImage(Main.Assets.Request <Texture2D>(TextureAssets.InfoIcon[i].Name), Language.GetTextValue($"tModLoader.{localizationKeys[i]}", values[i]))
                        {
                            Left = { Pixels = xOffset, Percent = 1f }
                        };

                        Append(_keyImage);
                        xOffset -= 18;
                    }
                }
            }

            OnDoubleClick += (e, el) => {
                // Only trigger if we didn't target the ModStateText, otherwise we trigger this behavior twice
                if (e.Target.GetType() != typeof(UIModStateText))
                {
                    _uiModStateText.Click(e);
                }
            };

            if (!_loaded)
            {
                _deleteModButton = new UIImage(TextureAssets.Trash)
                {
                    Width  = { Pixels = 36 },
                    Height = { Pixels = 36 },
                    Left   = { Pixels = _moreInfoButton.Left.Pixels - 36 - PADDING, Precent = 1 },
                    Top    = { Pixels = 42.5f }
                };
                _deleteModButton.OnClick += QuickModDelete;
                Append(_deleteModButton);
            }
        }