Ui-Info-Suite/SDVModTest/UIElements/ShowToolUpgradeStatus.cs

144 lines
5.1 KiB
C#
Raw Normal View History

2017-11-28 13:27:52 +08:00
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
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 UIInfoSuite.Extensions;
namespace UIInfoSuite.UIElements
{
class ShowToolUpgradeStatus : IDisposable
{
private readonly IModHelper _helper;
private Rectangle _toolTexturePosition;
private String _hoverText;
private Tool _toolBeingUpgraded;
private ClickableTextureComponent _toolUpgradeIcon;
2017-11-28 13:27:52 +08:00
public ShowToolUpgradeStatus(IModHelper helper)
{
_helper = helper;
}
public void ToggleOption(bool showToolUpgradeStatus)
{
GraphicsEvents.OnPreRenderHudEvent -= DrawToolUpgradeStatus;
GraphicsEvents.OnPostRenderHudEvent -= DrawHoverText;
2017-11-28 13:27:52 +08:00
TimeEvents.AfterDayStarted -= DayChanged;
GameEvents.OneSecondTick -= CheckForMidDayChanges;
if (showToolUpgradeStatus)
{
DayChanged(null, new EventArgsIntChanged(0, Game1.dayOfMonth));
GraphicsEvents.OnPreRenderHudEvent += DrawToolUpgradeStatus;
GraphicsEvents.OnPostRenderHudEvent += DrawHoverText;
2017-11-28 13:27:52 +08:00
TimeEvents.AfterDayStarted += DayChanged;
GameEvents.OneSecondTick += CheckForMidDayChanges;
}
}
private void CheckForMidDayChanges(object sender, EventArgs e)
{
if (_toolBeingUpgraded != Game1.player.toolBeingUpgraded.Value)
2017-11-28 13:27:52 +08:00
DayChanged(null, null);
}
private void DayChanged(object sender, EventArgs e)
{
if (Game1.player.toolBeingUpgraded.Value != null)
2017-11-28 13:27:52 +08:00
{
_toolBeingUpgraded = Game1.player.toolBeingUpgraded.Value;
2017-11-28 13:27:52 +08:00
_toolTexturePosition = new Rectangle();
if (_toolBeingUpgraded is StardewValley.Tools.WateringCan)
{
_toolTexturePosition.X = 32;
_toolTexturePosition.Y = 228;
_toolTexturePosition.Width = 16;
_toolTexturePosition.Height = 11;
}
else
{
_toolTexturePosition.Width = 16;
_toolTexturePosition.Height = 16;
_toolTexturePosition.X = 81;
_toolTexturePosition.Y = 31;
if (!(_toolBeingUpgraded is StardewValley.Tools.Hoe))
{
_toolTexturePosition.Y += 64;
if (!(_toolBeingUpgraded is StardewValley.Tools.Pickaxe))
{
_toolTexturePosition.Y += 64;
}
}
}
_toolTexturePosition.X += (111 * _toolBeingUpgraded.UpgradeLevel);
if (_toolTexturePosition.X > Game1.toolSpriteSheet.Width)
{
_toolTexturePosition.Y += 32;
_toolTexturePosition.X -= 333;
}
if (Game1.player.daysLeftForToolUpgrade.Value > 0)
2017-11-28 13:27:52 +08:00
{
_hoverText = String.Format(_helper.SafeGetString(LanguageKeys.DaysUntilToolIsUpgraded),
Game1.player.daysLeftForToolUpgrade.Value, _toolBeingUpgraded.DisplayName);
2017-11-28 13:27:52 +08:00
}
else
{
_hoverText = String.Format(_helper.SafeGetString(LanguageKeys.ToolIsFinishedBeingUpgraded),
_toolBeingUpgraded.DisplayName);
}
}
else
{
_toolBeingUpgraded = null;
}
}
private void DrawToolUpgradeStatus(object sender, EventArgs e)
{
if (!Game1.eventUp &&
_toolBeingUpgraded != null)
{
Point iconPosition = IconHandler.Handler.GetNewIconPosition();
_toolUpgradeIcon =
2017-11-28 13:27:52 +08:00
new ClickableTextureComponent(
new Rectangle(iconPosition.X, iconPosition.Y, 40, 40),
Game1.toolSpriteSheet,
_toolTexturePosition,
2.5f);
_toolUpgradeIcon.draw(Game1.spriteBatch);
}
}
2017-11-28 13:27:52 +08:00
private void DrawHoverText(object sender, EventArgs e)
{
if (_toolBeingUpgraded != null &&
_toolUpgradeIcon.containsPoint(Game1.getMouseX(), Game1.getMouseY()))
{
IClickableMenu.drawHoverText(
2017-11-28 13:27:52 +08:00
Game1.spriteBatch,
_hoverText, Game1.dialogueFont);
}
}
public void Dispose()
{
ToggleOption(false);
_toolBeingUpgraded = null;
}
}
}