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

93 lines
3.6 KiB
C#
Raw Normal View History

2017-07-20 11:51:05 +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 ShowTravelingMerchant : IDisposable
{
private bool _travelingMerchantIsHere = false;
private ClickableTextureComponent _travelingMerchantIcon;
2017-07-20 11:51:05 +08:00
private readonly IModHelper _helper;
public void ToggleOption(bool showTravelingMerchant)
{
2018-12-25 12:36:39 +08:00
_helper.Events.Display.RenderingHud -= OnRenderingHud;
_helper.Events.Display.RenderedHud -= OnRenderedHud;
_helper.Events.GameLoop.DayStarted -= OnDayStarted;
2017-07-20 11:51:05 +08:00
if (showTravelingMerchant)
{
2018-12-25 12:36:39 +08:00
UpdateTravelingMerchant();
_helper.Events.Display.RenderingHud += OnRenderingHud;
_helper.Events.Display.RenderedHud += OnRenderedHud;
_helper.Events.GameLoop.DayStarted += OnDayStarted;
2017-07-20 11:51:05 +08:00
}
}
2017-07-20 11:51:05 +08:00
public ShowTravelingMerchant(IModHelper helper)
{
_helper = helper;
}
public void Dispose()
{
ToggleOption(false);
}
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, EventArgs e)
2017-07-20 11:51:05 +08:00
{
2018-12-25 12:36:39 +08:00
UpdateTravelingMerchant();
}
2017-07-20 11:51:05 +08:00
2018-12-25 12:36:39 +08:00
private void UpdateTravelingMerchant()
{
2017-07-20 11:51:05 +08:00
int dayOfWeek = Game1.dayOfMonth % 7;
_travelingMerchantIsHere = dayOfWeek == 0 || dayOfWeek == 5;
}
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).</summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event arguments.</param>
private void OnRenderingHud(object sender, RenderingHudEventArgs e)
2017-07-20 11:51:05 +08:00
{
2018-12-25 12:36:39 +08:00
// draw traveling merchant
if (!Game1.eventUp && _travelingMerchantIsHere)
2017-07-20 11:51:05 +08:00
{
Point iconPosition = IconHandler.Handler.GetNewIconPosition();
_travelingMerchantIcon =
2017-07-20 11:51:05 +08:00
new ClickableTextureComponent(
new Rectangle(iconPosition.X, iconPosition.Y, 40, 40),
2017-11-28 13:27:52 +08:00
Game1.mouseCursors,
2017-07-20 11:51:05 +08:00
new Rectangle(192, 1411, 20, 20),
2f);
_travelingMerchantIcon.draw(Game1.spriteBatch);
}
}
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-12-25 12:36:39 +08:00
// draw hover text
2019-11-27 14:12:29 +08:00
if (_travelingMerchantIsHere && (_travelingMerchantIcon?.containsPoint(Game1.getMouseX(), Game1.getMouseY()) ?? false))
{
string hoverText = _helper.SafeGetString(
LanguageKeys.TravelingMerchantIsInTown);
IClickableMenu.drawHoverText(
Game1.spriteBatch,
hoverText, Game1.dialogueFont);
2017-07-20 11:51:05 +08:00
}
}
}
}