Ui-Info-Suite/SDVModTest/Options/ModOptionsPageHandler.cs

176 lines
10 KiB
C#
Raw Normal View History

2017-07-20 11:51:05 +08:00
using UIInfoSuite.UIElements;
using StardewModdingAPI;
using StardewModdingAPI.Events;
using StardewValley;
using StardewValley.Menus;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
2017-11-28 13:27:52 +08:00
using UIInfoSuite.Extensions;
2017-07-20 11:51:05 +08:00
namespace UIInfoSuite.Options
{
class ModOptionsPageHandler : IDisposable
{
private List<ModOptionsElement> _optionsElements = new List<ModOptionsElement>();
private readonly List<IDisposable> _elementsToDispose;
private readonly IDictionary<string, String> _options;
private ModOptionsPageButton _modOptionsPageButton;
private ModOptionsPage _modOptionsPage;
private readonly IModHelper _helper;
private int _modOptionsTabPageNumber;
private readonly LuckOfDay _luckOfDay;
private readonly ShowBirthdayIcon _showBirthdayIcon = new ShowBirthdayIcon();
private readonly ShowAccurateHearts _showAccurateHearts = new ShowAccurateHearts();
private readonly LocationOfTownsfolk _locationOfTownsfolk;
private readonly ShowWhenAnimalNeedsPet _showWhenAnimalNeedsPet;
private readonly ShowCalendarAndBillboardOnGameMenuButton _showCalendarAndBillboardOnGameMenuButton;
private readonly ShowCropAndBarrelTime _showCropAndBarrelTime;
private readonly ShowItemEffectRanges _showScarecrowAndSprinklerRange;
private readonly ExperienceBar _experienceBar;
private readonly ShowItemHoverInformation _showItemHoverInformation = new ShowItemHoverInformation();
private readonly ShowTravelingMerchant _showTravelingMerchant;
private readonly ShopHarvestPrices _shopHarvestPrices;
private readonly ShowQueenOfSauceIcon _showQueenOfSauceIcon;
2017-11-28 13:27:52 +08:00
private readonly ShowToolUpgradeStatus _showToolUpgradeStatus;
2017-07-20 11:51:05 +08:00
public ModOptionsPageHandler(IModHelper helper, IDictionary<String, String> options)
{
_options = options;
MenuEvents.MenuChanged += AddModOptionsToMenu;
MenuEvents.MenuClosed += RemoveModOptionsFromMenu;
_helper = helper;
ModConfig modConfig = _helper.ReadConfig<ModConfig>();
_luckOfDay = new LuckOfDay(helper);
_locationOfTownsfolk = new LocationOfTownsfolk(_helper, _options);
_showWhenAnimalNeedsPet = new ShowWhenAnimalNeedsPet(_helper);
2017-11-28 13:27:52 +08:00
_showCalendarAndBillboardOnGameMenuButton = new ShowCalendarAndBillboardOnGameMenuButton(helper);
2017-07-20 11:51:05 +08:00
_showScarecrowAndSprinklerRange = new ShowItemEffectRanges(modConfig);
_experienceBar = new ExperienceBar(helper);
_shopHarvestPrices = new ShopHarvestPrices(helper);
_showQueenOfSauceIcon = new ShowQueenOfSauceIcon(helper);
_showTravelingMerchant = new ShowTravelingMerchant(helper);
_showCropAndBarrelTime = new ShowCropAndBarrelTime(helper);
2017-11-28 13:27:52 +08:00
_showToolUpgradeStatus = new ShowToolUpgradeStatus(helper);
2017-07-20 11:51:05 +08:00
_elementsToDispose = new List<IDisposable>()
{
_luckOfDay,
_showBirthdayIcon,
_showAccurateHearts,
_locationOfTownsfolk,
_showWhenAnimalNeedsPet,
_showCalendarAndBillboardOnGameMenuButton,
_showCropAndBarrelTime,
_experienceBar,
_showItemHoverInformation,
_showTravelingMerchant,
_shopHarvestPrices,
2017-11-28 13:27:52 +08:00
_showQueenOfSauceIcon,
_showToolUpgradeStatus
2017-07-20 11:51:05 +08:00
};
int whichOption = 1;
Version thisVersion = Assembly.GetAssembly(this.GetType()).GetName().Version;
_optionsElements.Add(new ModOptionsElement("UI Info Suite v" +
thisVersion.Major + "." + thisVersion.Minor + "." + thisVersion.Build));
2017-11-28 13:27:52 +08:00
_optionsElements.Add(new ModOptionsCheckbox(_helper.SafeGetString(OptionKeys.ShowLuckIcon), whichOption++, _luckOfDay.Toggle, _options, OptionKeys.ShowLuckIcon));
_optionsElements.Add(new ModOptionsCheckbox(_helper.SafeGetString(OptionKeys.ShowLevelUpAnimation), whichOption++, _experienceBar.ToggleLevelUpAnimation, _options, OptionKeys.ShowLevelUpAnimation));
_optionsElements.Add(new ModOptionsCheckbox(_helper.SafeGetString(OptionKeys.ShowExperienceBar), whichOption++, _experienceBar.ToggleShowExperienceBar, _options, OptionKeys.ShowExperienceBar));
_optionsElements.Add(new ModOptionsCheckbox(_helper.SafeGetString(OptionKeys.AllowExperienceBarToFadeOut), whichOption++, _experienceBar.ToggleExperienceBarFade, _options, OptionKeys.AllowExperienceBarToFadeOut));
_optionsElements.Add(new ModOptionsCheckbox(_helper.SafeGetString(OptionKeys.ShowExperienceGain), whichOption++, _experienceBar.ToggleShowExperienceGain, _options, OptionKeys.ShowExperienceGain));
_optionsElements.Add(new ModOptionsCheckbox(_helper.SafeGetString(OptionKeys.ShowLocationOfTownsPeople), whichOption++, _locationOfTownsfolk.ToggleShowNPCLocationsOnMap, _options, OptionKeys.ShowLocationOfTownsPeople));
_optionsElements.Add(new ModOptionsCheckbox(_helper.SafeGetString(OptionKeys.ShowBirthdayIcon), whichOption++, _showBirthdayIcon.ToggleOption, _options, OptionKeys.ShowBirthdayIcon));
_optionsElements.Add(new ModOptionsCheckbox(_helper.SafeGetString(OptionKeys.ShowHeartFills), whichOption++, _showAccurateHearts.ToggleOption, _options, OptionKeys.ShowHeartFills));
_optionsElements.Add(new ModOptionsCheckbox(_helper.SafeGetString(OptionKeys.ShowAnimalsNeedPets), whichOption++, _showWhenAnimalNeedsPet.ToggleOption, _options, OptionKeys.ShowAnimalsNeedPets));
_optionsElements.Add(new ModOptionsCheckbox(_helper.SafeGetString(OptionKeys.DisplayCalendarAndBillboard), whichOption++, _showCalendarAndBillboardOnGameMenuButton.ToggleOption, _options, OptionKeys.DisplayCalendarAndBillboard));
_optionsElements.Add(new ModOptionsCheckbox(_helper.SafeGetString(OptionKeys.ShowCropAndBarrelTooltip), whichOption++, _showCropAndBarrelTime.ToggleOption, _options, OptionKeys.ShowCropAndBarrelTooltip));
_optionsElements.Add(new ModOptionsCheckbox(_helper.SafeGetString(OptionKeys.ShowItemEffectRanges), whichOption++, _showScarecrowAndSprinklerRange.ToggleOption, _options, OptionKeys.ShowItemEffectRanges));
_optionsElements.Add(new ModOptionsCheckbox(_helper.SafeGetString(OptionKeys.ShowExtraItemInformation), whichOption++, _showItemHoverInformation.ToggleOption, _options, OptionKeys.ShowExtraItemInformation));
_optionsElements.Add(new ModOptionsCheckbox(_helper.SafeGetString(OptionKeys.ShowTravelingMerchant), whichOption++, _showTravelingMerchant.ToggleOption, _options, OptionKeys.ShowTravelingMerchant));
_optionsElements.Add(new ModOptionsCheckbox(_helper.SafeGetString(OptionKeys.ShowHarvestPricesInShop), whichOption++, _shopHarvestPrices.ToggleOption, _options, OptionKeys.ShowHarvestPricesInShop));
_optionsElements.Add(new ModOptionsCheckbox(_helper.SafeGetString(OptionKeys.ShowWhenNewRecipesAreAvailable), whichOption++, _showQueenOfSauceIcon.ToggleOption, _options, OptionKeys.ShowWhenNewRecipesAreAvailable));
_optionsElements.Add(new ModOptionsCheckbox(_helper.SafeGetString(OptionKeys.ShowToolUpgradeStatus), whichOption++, _showToolUpgradeStatus.ToggleOption, _options, OptionKeys.ShowToolUpgradeStatus));
2017-07-20 11:51:05 +08:00
}
public void Dispose()
{
foreach (var item in _elementsToDispose)
item.Dispose();
}
private void OnButtonLeftClicked(object sender, EventArgs e)
{
if (Game1.activeClickableMenu is GameMenu)
{
SetActiveClickableMenuToModOptionsPage();
Game1.playSound("smallSelect");
}
}
private void RemoveModOptionsFromMenu(object sender, EventArgsClickableMenuClosed e)
{
2018-05-08 10:26:37 +08:00
GraphicsEvents.OnPostRenderGuiEvent -= DrawButton;
if (_modOptionsPageButton != null)
_modOptionsPageButton.OnLeftClicked -= OnButtonLeftClicked;
2017-07-20 11:51:05 +08:00
if (Game1.activeClickableMenu is GameMenu)
{
2018-05-06 12:21:18 +08:00
List<IClickableMenu> tabPages = _helper.Reflection.GetField<List<IClickableMenu>>(Game1.activeClickableMenu, "pages").GetValue();
2017-07-20 11:51:05 +08:00
tabPages.Remove(_modOptionsPage);
}
}
private void AddModOptionsToMenu(object sender, EventArgsClickableMenuChanged e)
{
if (Game1.activeClickableMenu is GameMenu)
{
if (_modOptionsPageButton == null)
{
_modOptionsPage = new ModOptionsPage(_optionsElements);
_modOptionsPageButton = new ModOptionsPageButton();
}
2017-11-28 13:27:52 +08:00
GraphicsEvents.OnPostRenderGuiEvent += DrawButton;
2017-07-20 11:51:05 +08:00
_modOptionsPageButton.OnLeftClicked += OnButtonLeftClicked;
2018-05-06 12:21:18 +08:00
List<IClickableMenu> tabPages = _helper.Reflection.GetField<List<IClickableMenu>>(Game1.activeClickableMenu, "pages").GetValue();
2017-07-20 11:51:05 +08:00
_modOptionsTabPageNumber = tabPages.Count;
tabPages.Add(_modOptionsPage);
}
}
private void SetActiveClickableMenuToModOptionsPage()
{
GameMenu menu = Game1.activeClickableMenu as GameMenu;
if (menu != null)
menu.currentTab = _modOptionsTabPageNumber;
}
private void DrawButton(object sender, EventArgs e)
{
if (Game1.activeClickableMenu is GameMenu &&
(Game1.activeClickableMenu as GameMenu).currentTab != 3) //don't render when the map is showing
{
if ((Game1.activeClickableMenu as GameMenu).currentTab == _modOptionsTabPageNumber)
{
_modOptionsPageButton.yPositionOnScreen = Game1.activeClickableMenu.yPositionOnScreen + 24;
}
else
{
_modOptionsPageButton.yPositionOnScreen = Game1.activeClickableMenu.yPositionOnScreen + 16;
}
_modOptionsPageButton.draw(Game1.spriteBatch);
//Might need to render hover text here
}
}
}
}