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

152 lines
5.7 KiB
C#
Raw Normal View History

2017-07-20 11:51:05 +08:00
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using UIInfoSuite.Extensions;
using StardewModdingAPI.Events;
using StardewValley;
using StardewValley.Menus;
using System;
namespace UIInfoSuite.UIElements
{
class ShowBirthdayIcon : IDisposable
{
private NPC _birthdayNPC;
private ClickableTextureComponent _birthdayIcon;
2018-12-25 12:36:39 +08:00
private readonly IModEvents _events;
public ShowBirthdayIcon(IModEvents events)
{
_events = events;
}
2017-07-20 11:51:05 +08:00
public void ToggleOption(bool showBirthdayIcon)
{
2018-12-25 12:36:39 +08:00
_events.GameLoop.DayStarted -= OnDayStarted;
_events.Display.RenderingHud -= OnRenderingHud;
_events.Display.RenderedHud -= OnRenderedHud;
_events.GameLoop.UpdateTicked -= OnUpdateTicked;
2017-07-20 11:51:05 +08:00
if (showBirthdayIcon)
{
2018-12-25 12:36:39 +08:00
CheckForBirthday();
_events.GameLoop.DayStarted += OnDayStarted;
_events.Display.RenderingHud += OnRenderingHud;
_events.Display.RenderedHud += OnRenderedHud;
_events.GameLoop.UpdateTicked += OnUpdateTicked;
2017-07-20 11:51:05 +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-07-20 11:51:05 +08:00
{
2018-12-25 12:36:39 +08:00
// check if gift has been given
if (e.IsOneSecond && _birthdayNPC != null && Game1.player?.friendshipData != null)
2017-07-20 11:51:05 +08:00
{
Game1.player.friendshipData.FieldDict.TryGetValue(_birthdayNPC.Name, out var netRef);
2018-05-06 12:21:18 +08:00
//var birthdayNPCDetails = Game1.player.friendshipData.SafeGet(_birthdayNPC.name);
Friendship birthdayNPCDetails = netRef;
2017-07-20 11:51:05 +08:00
if (birthdayNPCDetails != null)
{
2018-05-06 12:21:18 +08:00
if (birthdayNPCDetails.GiftsToday == 1)
2017-07-20 11:51:05 +08:00
_birthdayNPC = null;
}
}
}
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, DayStartedEventArgs e)
{
CheckForBirthday();
}
private void CheckForBirthday()
2017-07-20 11:51:05 +08:00
{
_birthdayNPC = null;
foreach (var location in Game1.locations)
{
foreach (var character in location.characters)
{
if (character.isBirthday(Game1.currentSeason, Game1.dayOfMonth))
{
_birthdayNPC = character;
break;
}
}
if (_birthdayNPC != null)
break;
}
}
2018-12-25 12:36:39 +08:00
/// <summary>Raised before drawing the HUD (item toolbar, clock, etc) to the screen.</summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event arguments.</param>
private void OnRenderingHud(object sender, EventArgs e)
2017-07-20 11:51:05 +08:00
{
2018-12-25 12:36:39 +08:00
// draw birthday icon
2017-07-20 11:51:05 +08:00
if (!Game1.eventUp)
{
if (_birthdayNPC != null)
{
Rectangle headShot = _birthdayNPC.GetHeadShot();
Point iconPosition = IconHandler.Handler.GetNewIconPosition();
float scale = 2.9f;
Game1.spriteBatch.Draw(
Game1.mouseCursors,
new Vector2(iconPosition.X, iconPosition.Y),
new Rectangle(228, 409, 16, 16),
Color.White,
0.0f,
Vector2.Zero,
scale,
SpriteEffects.None,
1f);
_birthdayIcon =
2017-07-20 11:51:05 +08:00
new ClickableTextureComponent(
2018-05-06 12:21:18 +08:00
_birthdayNPC.Name,
2017-07-20 11:51:05 +08:00
new Rectangle(
iconPosition.X - 7,
iconPosition.Y - 2,
(int)(16.0 * scale),
(int)(16.0 * scale)),
null,
2018-05-06 12:21:18 +08:00
_birthdayNPC.Name,
_birthdayNPC.Sprite.Texture,
2017-07-20 11:51:05 +08:00
headShot,
2f);
_birthdayIcon.draw(Game1.spriteBatch);
2017-07-20 11:51:05 +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-12-25 12:36:39 +08:00
// draw hover text
if (_birthdayNPC != null &&
(_birthdayIcon?.containsPoint(Game1.getMouseX(), Game1.getMouseY()) ?? false))
{
String hoverText = String.Format("{0}'s Birthday", _birthdayNPC.Name);
IClickableMenu.drawHoverText(
Game1.spriteBatch,
hoverText,
Game1.dialogueFont);
}
}
2017-07-20 11:51:05 +08:00
}
}