2017-07-20 11:51:05 +08:00
using Microsoft.Xna.Framework ;
using Microsoft.Xna.Framework.Graphics ;
using StardewModdingAPI ;
using StardewModdingAPI.Events ;
using StardewValley ;
using StardewValley.Menus ;
using StardewValley.Objects ;
using System ;
using System.Collections.Generic ;
using System.Reflection ;
using UIInfoSuite.Extensions ;
namespace UIInfoSuite.UIElements
{
class ShowQueenOfSauceIcon : IDisposable
{
private Dictionary < String , String > _recipesByDescription = new Dictionary < string , string > ( ) ;
private Dictionary < String , String > _recipes = new Dictionary < String , string > ( ) ;
private String _todaysRecipe ;
private NPC _gus ;
private bool _drawQueenOfSauceIcon = false ;
private bool _drawDishOfDayIcon = false ;
2018-08-20 05:57:45 +08:00
private ClickableTextureComponent _queenOfSauceIcon ;
2017-07-20 11:51:05 +08:00
private readonly IModHelper _helper ;
public void ToggleOption ( bool showQueenOfSauceIcon )
{
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-07-20 11:51:05 +08:00
if ( showQueenOfSauceIcon )
{
LoadRecipes ( ) ;
2018-12-25 12:36:39 +08:00
CheckForNewRecipe ( ) ;
_helper . Events . GameLoop . DayStarted + = OnDayStarted ;
_helper . Events . Display . RenderingHud + = OnRenderingHud ;
_helper . Events . Display . RenderedHud + = OnRenderedHud ;
_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-11-28 13:27:52 +08:00
{
2018-12-25 12:36:39 +08:00
// check if learned recipe
if ( e . IsOneSecond & & _drawQueenOfSauceIcon & & Game1 . player . knowsRecipe ( _todaysRecipe ) )
2017-11-28 13:27:52 +08:00
_drawQueenOfSauceIcon = false ;
}
2017-07-20 11:51:05 +08:00
public ShowQueenOfSauceIcon ( IModHelper helper )
{
_helper = helper ;
}
private void LoadRecipes ( )
{
if ( _recipes . Count = = 0 )
{
_recipes = Game1 . content . Load < Dictionary < String , String > > ( "Data\\TV\\CookingChannel" ) ;
foreach ( var next in _recipes )
{
string [ ] values = next . Value . Split ( '/' ) ;
if ( values . Length > 1 )
{
_recipesByDescription [ values [ 1 ] ] = values [ 0 ] ;
}
}
}
}
private void FindGus ( )
{
foreach ( var location in Game1 . locations )
{
foreach ( var npc in location . characters )
{
2018-05-06 12:21:18 +08:00
if ( npc . Name = = "Gus" )
2017-07-20 11:51:05 +08:00
{
_gus = npc ;
break ;
}
}
if ( _gus ! = null )
break ;
}
}
private string [ ] GetTodaysRecipe ( )
{
String [ ] array1 = new string [ 2 ] ;
int recipeNum = ( int ) ( Game1 . stats . DaysPlayed % 224 / 7 ) ;
//var recipes = Game1.content.Load<Dictionary<String, String>>("Data\\TV\\CookingChannel");
String recipeValue = _recipes . SafeGet ( recipeNum . ToString ( ) ) ;
String [ ] splitValues = null ;
String key = null ;
bool checkCraftingRecipes = true ;
if ( String . IsNullOrEmpty ( recipeValue ) )
{
recipeValue = _recipes [ "1" ] ;
checkCraftingRecipes = false ;
}
splitValues = recipeValue . Split ( '/' ) ;
key = splitValues [ 0 ] ;
///Game code sets this to splitValues[1] to display the language specific
///recipe name. We are skipping a bunch of their steps to just get the
///english name needed to tell if the player knows the recipe or not
array1 [ 0 ] = key ;
if ( checkCraftingRecipes )
{
String craftingRecipesValue = CraftingRecipe . cookingRecipes . SafeGet ( key ) ;
if ( ! String . IsNullOrEmpty ( craftingRecipesValue ) )
splitValues = craftingRecipesValue . Split ( '/' ) ;
}
2018-12-25 09:06:26 +08:00
string languageRecipeName = ( _helper . Content . CurrentLocaleConstant = = LocalizedContentManager . LanguageCode . en ) ?
2017-07-20 11:51:05 +08:00
key : splitValues [ splitValues . Length - 1 ] ;
array1 [ 1 ] = languageRecipeName ;
//String str = null;
//if (!Game1.player.cookingRecipes.ContainsKey(key))
//{
// str = Game1.content.LoadString(@"Strings\StringsFromCSFiles:TV.cs.13153", languageRecipeName);
//}
//else
//{
// str = Game1.content.LoadString(@"Strings\StringsFromCSFiles:TV.cs.13151", languageRecipeName);
//}
//array1[1] = str;
return array1 ;
}
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 icon
2017-07-20 11:51:05 +08:00
if ( ! Game1 . eventUp )
{
if ( _drawQueenOfSauceIcon )
{
Point iconPosition = IconHandler . Handler . GetNewIconPosition ( ) ;
2018-08-20 05:57:45 +08:00
_queenOfSauceIcon = new ClickableTextureComponent (
2017-07-20 11:51:05 +08:00
new Rectangle ( iconPosition . X , iconPosition . Y , 40 , 40 ) ,
Game1 . mouseCursors ,
new Rectangle ( 609 , 361 , 28 , 28 ) ,
1.3f ) ;
2018-08-20 05:57:45 +08:00
_queenOfSauceIcon . draw ( Game1 . spriteBatch ) ;
2017-07-20 11:51:05 +08:00
}
if ( _drawDishOfDayIcon )
{
Point iconLocation = IconHandler . Handler . GetNewIconPosition ( ) ;
float scale = 2.9f ;
Game1 . spriteBatch . Draw (
Game1 . objectSpriteSheet ,
new Vector2 ( iconLocation . X , iconLocation . Y ) ,
new Rectangle ( 306 , 291 , 14 , 14 ) ,
Color . White ,
0 ,
Vector2 . Zero ,
scale ,
SpriteEffects . None ,
1f ) ;
ClickableTextureComponent texture =
new ClickableTextureComponent (
2018-05-06 12:21:18 +08:00
_gus . Name ,
2017-07-20 11:51:05 +08:00
new Rectangle (
iconLocation . X - 7 ,
iconLocation . Y - 2 ,
( int ) ( 16.0 * scale ) ,
( int ) ( 16.0 * scale ) ) ,
null ,
2018-05-06 12:21:18 +08:00
_gus . Name ,
_gus . Sprite . Texture ,
2017-07-20 11:51:05 +08:00
_gus . GetHeadShot ( ) ,
2f ) ;
texture . draw ( Game1 . spriteBatch ) ;
2020-02-20 11:39:13 +08:00
if ( Constants . TargetPlatform = = GamePlatform . Android )
{
if ( texture . containsPoint ( ( int ) ( Game1 . getMouseX ( ) * Game1 . options . zoomLevel ) , ( int ) ( Game1 . getMouseY ( ) * Game1 . options . zoomLevel ) ) )
{
IClickableMenu . drawHoverText (
Game1 . spriteBatch ,
"Gus is selling " + Game1 . dishOfTheDay . DisplayName + " recipe today!" ,
Game1 . dialogueFont ) ;
}
return ;
}
2017-07-20 11:51:05 +08:00
if ( texture . containsPoint ( Game1 . getMouseX ( ) , Game1 . getMouseY ( ) ) )
{
IClickableMenu . drawHoverText (
Game1 . spriteBatch ,
"Gus is selling " + Game1 . dishOfTheDay . DisplayName + " recipe today!" ,
Game1 . dialogueFont ) ;
}
}
}
}
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
2020-02-20 11:39:13 +08:00
if ( Constants . TargetPlatform = = GamePlatform . Android )
{
if ( _drawQueenOfSauceIcon & &
_queenOfSauceIcon . containsPoint ( ( int ) ( Game1 . getMouseX ( ) * Game1 . options . zoomLevel ) , ( int ) ( Game1 . getMouseY ( ) * Game1 . options . zoomLevel ) ) )
{
IClickableMenu . drawHoverText (
Game1 . spriteBatch ,
_helper . SafeGetString (
LanguageKeys . TodaysRecipe ) + _todaysRecipe ,
Game1 . dialogueFont ) ;
}
return ;
}
2018-08-20 05:57:45 +08:00
if ( _drawQueenOfSauceIcon & &
_queenOfSauceIcon . containsPoint ( Game1 . getMouseX ( ) , Game1 . getMouseY ( ) ) )
{
IClickableMenu . drawHoverText (
Game1 . spriteBatch ,
_helper . SafeGetString (
LanguageKeys . TodaysRecipe ) + _todaysRecipe ,
Game1 . dialogueFont ) ;
}
}
2017-07-20 11:51:05 +08:00
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 )
{
this . CheckForNewRecipe ( ) ;
}
private void CheckForNewRecipe ( )
2017-07-20 11:51:05 +08:00
{
TV tv = new TV ( ) ;
2018-05-17 12:13:19 +08:00
int numRecipesKnown = Game1 . player . cookingRecipes . Count ( ) ;
2017-07-20 11:51:05 +08:00
String [ ] recipes = typeof ( TV ) . GetMethod ( "getWeeklyRecipe" , BindingFlags . Instance | BindingFlags . NonPublic ) . Invoke ( tv , null ) as String [ ] ;
//String[] recipe = GetTodaysRecipe();
//_todaysRecipe = recipe[1];
_todaysRecipe = _recipesByDescription . SafeGet ( recipes [ 0 ] ) ;
2018-05-17 12:13:19 +08:00
if ( Game1 . player . cookingRecipes . Count ( ) > numRecipesKnown )
2017-07-20 11:51:05 +08:00
Game1 . player . cookingRecipes . Remove ( _todaysRecipe ) ;
_drawQueenOfSauceIcon = ( Game1 . dayOfMonth % 7 = = 0 | | ( Game1 . dayOfMonth - 3 ) % 7 = = 0 ) & &
Game1 . stats . DaysPlayed > 5 & &
! Game1 . player . knowsRecipe ( _todaysRecipe ) ;
//_drawDishOfDayIcon = !Game1.player.knowsRecipe(Game1.dishOfTheDay.Name);
}
}
}