2017-11-28 13:27:52 +08:00
using Microsoft.Xna.Framework ;
using StardewModdingAPI ;
using StardewModdingAPI.Events ;
using StardewValley ;
using StardewValley.Menus ;
using System ;
using UIInfoSuite.Extensions ;
namespace UIInfoSuite.UIElements
{
class ShowToolUpgradeStatus : IDisposable
{
private readonly IModHelper _helper ;
private Rectangle _toolTexturePosition ;
private String _hoverText ;
private Tool _toolBeingUpgraded ;
2018-08-20 05:57:45 +08:00
private ClickableTextureComponent _toolUpgradeIcon ;
2017-11-28 13:27:52 +08:00
public ShowToolUpgradeStatus ( IModHelper helper )
{
_helper = helper ;
}
public void ToggleOption ( bool showToolUpgradeStatus )
{
2018-12-25 12:36:39 +08:00
_helper . Events . Display . RenderingHud - = OnRenderingHud ;
_helper . Events . Display . RenderedHud - = OnRenderedHud ;
_helper . Events . GameLoop . DayStarted - = OnDayStarted ;
_helper . Events . GameLoop . UpdateTicked - = OnUpdateTicked ;
2017-11-28 13:27:52 +08:00
if ( showToolUpgradeStatus )
{
2018-12-25 12:36:39 +08:00
UpdateToolInfo ( ) ;
_helper . Events . Display . RenderingHud + = OnRenderingHud ;
_helper . Events . Display . RenderedHud + = OnRenderedHud ;
_helper . Events . GameLoop . DayStarted + = OnDayStarted ;
_helper . Events . GameLoop . UpdateTicked + = OnUpdateTicked ;
2017-11-28 13:27:52 +08:00
}
}
2018-12-25 12:36:39 +08:00
/// <summary>Raised after the game state is updated (≈60 times per second).</summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event arguments.</param>
private void OnUpdateTicked ( object sender , UpdateTickedEventArgs e )
2017-11-28 13:27:52 +08:00
{
2018-12-25 12:36:39 +08:00
if ( e . IsOneSecond & & _toolBeingUpgraded ! = Game1 . player . toolBeingUpgraded . Value )
UpdateToolInfo ( ) ;
2017-11-28 13:27:52 +08:00
}
2018-12-25 12:36:39 +08:00
/// <summary>Raised after the game begins a new day (including when the player loads a save).</summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event arguments.</param>
private void OnDayStarted ( object sender , DayStartedEventArgs e )
2017-11-28 13:27:52 +08:00
{
2018-12-25 12:36:39 +08:00
this . UpdateToolInfo ( ) ;
}
private void UpdateToolInfo ( )
{
//
2018-07-16 00:50:29 +08:00
if ( Game1 . player . toolBeingUpgraded . Value ! = null )
2017-11-28 13:27:52 +08:00
{
2018-07-16 00:50:29 +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 ;
}
2018-07-16 00:50:29 +08:00
if ( Game1 . player . daysLeftForToolUpgrade . Value > 0 )
2017-11-28 13:27:52 +08:00
{
_hoverText = String . Format ( _helper . SafeGetString ( LanguageKeys . DaysUntilToolIsUpgraded ) ,
2018-07-16 00:50:29 +08:00
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 ;
}
}
2018-12-25 12:36:39 +08:00
/// <summary>Raised before drawing the HUD (item toolbar, clock, etc) to the screen. The vanilla HUD may be hidden at this point (e.g. because a menu is open). Content drawn to the sprite batch at this point will appear under the HUD.</summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event arguments.</param>
private void OnRenderingHud ( object sender , RenderingHudEventArgs e )
2017-11-28 13:27:52 +08:00
{
2018-12-25 12:36:39 +08:00
// draw tool upgrade status
if ( ! Game1 . eventUp & & _toolBeingUpgraded ! = null )
2017-11-28 13:27:52 +08:00
{
Point iconPosition = IconHandler . Handler . GetNewIconPosition ( ) ;
2018-08-20 05:57:45 +08:00
_toolUpgradeIcon =
2017-11-28 13:27:52 +08:00
new ClickableTextureComponent (
new Rectangle ( iconPosition . X , iconPosition . Y , 40 , 40 ) ,
Game1 . toolSpriteSheet ,
_toolTexturePosition ,
2.5f ) ;
2018-08-20 05:57:45 +08:00
_toolUpgradeIcon . draw ( Game1 . spriteBatch ) ;
}
}
2017-11-28 13:27:52 +08:00
2018-12-25 12:36:39 +08:00
/// <summary>Raised after drawing the HUD (item toolbar, clock, etc) to the sprite batch, but before it's rendered to the screen.</summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event arguments.</param>
private void OnRenderedHud ( object sender , RenderedHudEventArgs e )
2018-08-20 05:57:45 +08:00
{
2018-12-25 12:36:39 +08:00
// draw hover text
2019-11-27 14:12:29 +08:00
if ( _toolBeingUpgraded ! = null & &
( _toolUpgradeIcon ? . containsPoint ( Game1 . getMouseX ( ) , Game1 . getMouseY ( ) ) ? ? false ) )
2018-08-20 05:57:45 +08:00
{
IClickableMenu . drawHoverText (
2017-11-28 13:27:52 +08:00
Game1 . spriteBatch ,
_hoverText , Game1 . dialogueFont ) ;
}
}
public void Dispose ( )
{
ToggleOption ( false ) ;
_toolBeingUpgraded = null ;
}
}
}