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

255 lines
11 KiB
C#
Raw Permalink Normal View History

2017-07-20 11:51:05 +08:00
using Microsoft.Xna.Framework;
using UIInfoSuite.Extensions;
using StardewModdingAPI.Events;
using StardewValley;
using StardewValley.Menus;
using StardewValley.TerrainFeatures;
using System;
using System.Collections.Generic;
using System.Text;
using StardewValley.Objects;
using StardewModdingAPI;
2017-11-28 13:27:52 +08:00
using StardewValley.Locations;
using StardewValley.Buildings;
2017-07-20 11:51:05 +08:00
namespace UIInfoSuite.UIElements
{
class ShowCropAndBarrelTime : IDisposable
{
2018-12-25 12:36:39 +08:00
private readonly Dictionary<int, String> _indexOfCropNames = new Dictionary<int, string>();
2017-07-20 11:51:05 +08:00
private StardewValley.Object _currentTile;
private TerrainFeature _terrain;
2017-11-28 13:27:52 +08:00
private Building _currentTileBuilding = null;
2017-07-20 11:51:05 +08:00
private readonly IModHelper _helper;
public ShowCropAndBarrelTime(IModHelper helper)
{
_helper = helper;
}
public void ToggleOption(bool showCropAndBarrelTimes)
{
2018-12-25 12:36:39 +08:00
_helper.Events.Display.RenderingHud -= OnRenderingHud;
_helper.Events.GameLoop.UpdateTicked -= OnUpdateTicked;
2017-07-20 11:51:05 +08:00
if (showCropAndBarrelTimes)
{
2018-12-25 12:36:39 +08:00
_helper.Events.Display.RenderingHud += OnRenderingHud;
_helper.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
if (!e.IsMultipleOf(4))
return;
2018-05-06 12:21:18 +08:00
2018-12-25 12:36:39 +08:00
// get tile under cursor
_currentTileBuilding = Game1.currentLocation is BuildableGameLocation buildableLocation
? buildableLocation.getBuildingAt(Game1.currentCursorTile)
: null;
2018-05-06 12:21:18 +08:00
if (Game1.currentLocation != null)
{
if (Game1.currentLocation.Objects == null ||
!Game1.currentLocation.Objects.TryGetValue(Game1.currentCursorTile, out _currentTile))
{
_currentTile = null;
}
if (Game1.currentLocation.terrainFeatures == null ||
!Game1.currentLocation.terrainFeatures.TryGetValue(Game1.currentCursorTile, out _terrain))
{
if (_currentTile is IndoorPot pot &&
pot.hoeDirt.Value != null)
{
_terrain = pot.hoeDirt.Value;
}
else
{
_terrain = null;
}
2018-05-06 12:21:18 +08:00
}
}
else
{
_currentTile = null;
_terrain = null;
}
2017-07-20 11:51:05 +08:00
}
public void Dispose()
{
ToggleOption(false);
}
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 hover tooltip
2017-11-28 13:27:52 +08:00
if (_currentTileBuilding != null)
{
if (_currentTileBuilding is Mill millBuilding)
{
2018-05-06 12:21:18 +08:00
if (millBuilding.input.Value != null)
2017-11-28 13:27:52 +08:00
{
2018-05-06 12:21:18 +08:00
if (!millBuilding.input.Value.isEmpty())
2017-11-28 13:27:52 +08:00
{
2018-05-06 12:21:18 +08:00
int wheatCount = 0;
int beetCount = 0;
foreach (var item in millBuilding.input.Value.items)
2017-11-28 13:27:52 +08:00
{
2018-05-06 12:21:18 +08:00
if (item != null &&
!String.IsNullOrEmpty(item.Name))
{
switch (item.Name)
{
case "Wheat": wheatCount = item.Stack; break;
case "Beet": beetCount = item.Stack; break;
}
}
2017-11-28 13:27:52 +08:00
}
2018-05-06 12:21:18 +08:00
StringBuilder builder = new StringBuilder();
2017-11-28 13:27:52 +08:00
if (wheatCount > 0)
2018-05-06 12:21:18 +08:00
builder.Append(wheatCount + " wheat");
2017-11-28 13:27:52 +08:00
2018-05-06 12:21:18 +08:00
if (beetCount > 0)
{
if (wheatCount > 0)
builder.Append(Environment.NewLine);
builder.Append(beetCount + " beets");
}
if (builder.Length > 0)
{
IClickableMenu.drawHoverText(
Game1.spriteBatch,
builder.ToString(),
Game1.smallFont);
}
2017-11-28 13:27:52 +08:00
}
}
}
}
else if (_currentTile != null &&
2018-05-06 12:21:18 +08:00
(!_currentTile.bigCraftable.Value ||
_currentTile.MinutesUntilReady > 0))
2017-07-20 11:51:05 +08:00
{
2018-05-06 12:21:18 +08:00
if (_currentTile.bigCraftable.Value &&
_currentTile.MinutesUntilReady > 0 &&
_currentTile.heldObject.Value != null &&
2017-07-20 11:51:05 +08:00
_currentTile.Name != "Heater")
{
StringBuilder hoverText = new StringBuilder();
hoverText.AppendLine(_currentTile.heldObject.Value.DisplayName);
2017-07-20 11:51:05 +08:00
if (_currentTile is Cask)
{
Cask currentCask = _currentTile as Cask;
2018-05-06 12:21:18 +08:00
hoverText.Append((int)(currentCask.daysToMature.Value / currentCask.agingRate.Value))
2017-07-20 11:51:05 +08:00
.Append(" " + _helper.SafeGetString(
LanguageKeys.DaysToMature));
}
else
{
2018-05-06 12:21:18 +08:00
int hours = _currentTile.MinutesUntilReady / 60;
int minutes = _currentTile.MinutesUntilReady % 60;
2017-07-20 11:51:05 +08:00
if (hours > 0)
hoverText.Append(hours).Append(" ")
.Append(_helper.SafeGetString(
LanguageKeys.Hours))
.Append(", ");
hoverText.Append(minutes).Append(" ")
.Append(_helper.SafeGetString(
LanguageKeys.Minutes));
}
IClickableMenu.drawHoverText(
Game1.spriteBatch,
hoverText.ToString(),
Game1.smallFont);
}
}
else if (_terrain != null)
{
if (_terrain is HoeDirt)
{
HoeDirt hoeDirt = _terrain as HoeDirt;
if (hoeDirt.crop != null &&
2018-05-06 12:21:18 +08:00
!hoeDirt.crop.dead.Value)
2017-07-20 11:51:05 +08:00
{
int num = 0;
2018-05-06 12:21:18 +08:00
if (hoeDirt.crop.fullyGrown.Value &&
hoeDirt.crop.dayOfCurrentPhase.Value > 0)
2017-07-20 11:51:05 +08:00
{
2018-05-06 12:21:18 +08:00
num = hoeDirt.crop.dayOfCurrentPhase.Value;
2017-07-20 11:51:05 +08:00
}
else
{
for (int i = 0; i < hoeDirt.crop.phaseDays.Count - 1; ++i)
{
2018-05-06 12:21:18 +08:00
if (hoeDirt.crop.currentPhase.Value == i)
num -= hoeDirt.crop.dayOfCurrentPhase.Value;
2017-07-20 11:51:05 +08:00
2018-05-06 12:21:18 +08:00
if (hoeDirt.crop.currentPhase.Value <= i)
2017-07-20 11:51:05 +08:00
num += hoeDirt.crop.phaseDays[i];
}
}
2018-05-06 12:21:18 +08:00
if (hoeDirt.crop.indexOfHarvest.Value > 0)
2017-07-20 11:51:05 +08:00
{
2018-05-06 12:21:18 +08:00
String hoverText = _indexOfCropNames.SafeGet(hoeDirt.crop.indexOfHarvest.Value);
2017-07-20 11:51:05 +08:00
if (String.IsNullOrEmpty(hoverText))
{
2018-05-06 12:21:18 +08:00
hoverText = new StardewValley.Object(new Debris(hoeDirt.crop.indexOfHarvest.Value, Vector2.Zero, Vector2.Zero).chunkType.Value, 1).DisplayName;
_indexOfCropNames.Add(hoeDirt.crop.indexOfHarvest.Value, hoverText);
2017-07-20 11:51:05 +08:00
}
StringBuilder finalHoverText = new StringBuilder();
finalHoverText.Append(hoverText).Append(": ");
if (num > 0)
{
finalHoverText.Append(num).Append(" ")
.Append(_helper.SafeGetString(
LanguageKeys.Days));
}
else
{
finalHoverText.Append(_helper.SafeGetString(
LanguageKeys.ReadyToHarvest));
}
IClickableMenu.drawHoverText(
Game1.spriteBatch,
finalHoverText.ToString(),
Game1.smallFont);
}
}
}
else if (_terrain is FruitTree)
{
FruitTree tree = _terrain as FruitTree;
var text = new StardewValley.Object(new Debris(tree.indexOfFruit.Value, Vector2.Zero, Vector2.Zero).chunkType.Value, 1).DisplayName;
2018-05-06 12:21:18 +08:00
if (tree.daysUntilMature.Value > 0)
2017-07-20 11:51:05 +08:00
{
text += Environment.NewLine + tree.daysUntilMature.Value + " " +
2017-07-20 11:51:05 +08:00
_helper.SafeGetString(
LanguageKeys.DaysToMature);
2017-07-20 11:51:05 +08:00
}
IClickableMenu.drawHoverText(
Game1.spriteBatch,
text,
Game1.smallFont);
2017-07-20 11:51:05 +08:00
}
}
}
}
}