Skip to content

Commit

Permalink
Fixed an issue where the tooltip wouldn't update when scrolling the l…
Browse files Browse the repository at this point in the history
…ist without moving the mouse
  • Loading branch information
tstaples committed Mar 24, 2019
1 parent 88c86db commit 8597cd1
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 25 deletions.
16 changes: 16 additions & 0 deletions GiftTasteHelper/Framework/Controllers/SocialPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ internal class SocialPage
private SVector2 SlotBoundsOffset;
private float SlotHeight;
private Rectangle PageBounds;
private int LastSlotIndex;

/// <summary>Fires when the current slot index changes due to scrolling the list.</summary>
public delegate void SlotIndexChangedDelegate();
public event SlotIndexChangedDelegate OnSlotIndexChanged;


/*********
Expand Down Expand Up @@ -54,6 +59,17 @@ public void OnResize(SDVSocialPage nativePage)
this.SlotBoundsOffset = new SVector2(Game1.tileSize / 4, Game1.tileSize / 8);
this.SlotHeight = this.GetSlotHeight();
this.PageBounds = this.MakePageBounds();
LastSlotIndex = this.GetSlotIndex();
}

public void OnUpdate()
{
int slotIndex = this.GetSlotIndex();
if (slotIndex != this.LastSlotIndex)
{
OnSlotIndexChanged.Invoke();
this.LastSlotIndex = slotIndex;
}
}

public string GetCurrentlyHoveredNpc(SVector2 mousePos)
Expand Down
10 changes: 10 additions & 0 deletions GiftTasteHelper/Framework/Views/GiftHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,16 @@ public virtual void OnDraw()
this.DrawGiftTooltip(this.CurrentGiftDrawData, this.TooltipTitle());
}

public virtual bool WantsUpdateEvent()
{
return false;
}

public virtual void OnPostUpdate(UpdateTickedEventArgs e)
{
// Empty
}

protected bool SetSelectedNPC(string npcName)
{
// TODO: cache these so we only create them once per npc.
Expand Down
8 changes: 8 additions & 0 deletions GiftTasteHelper/Framework/Views/IGiftHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,17 @@ internal interface IGiftHelper
bool OnOpen(IClickableMenu menu);
void OnResize(IClickableMenu menu);
void OnClose();

/// <summary>Should this helper recieve OnDraw events.</summary>
bool CanDraw();
void OnDraw();

/// <summary>Should this helper receive input and tick events. Checked every tick.</summary>
bool CanTick();
void OnCursorMoved(CursorMovedEventArgs e);

/// <summary>Indicates if this helper wants to receive OnPostUpdate events.</summary>
bool WantsUpdateEvent();
void OnPostUpdate(UpdateTickedEventArgs e);
}
}
75 changes: 50 additions & 25 deletions GiftTasteHelper/Framework/Views/SocialPageGiftHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ internal class SocialPageGiftHelper : GiftHelper
** Public methods
*********/
public SocialPageGiftHelper(IGiftDataProvider dataProvider, GiftConfig config, IReflectionHelper reflection, ITranslationHelper translation)
: base(GiftHelperType.SocialPage, dataProvider, config, reflection, translation) { }
: base(GiftHelperType.SocialPage, dataProvider, config, reflection, translation)
{
SocialPage.OnSlotIndexChanged += OnSlotIndexChanged;
}

public override bool OnOpen(IClickableMenu menu)
{
Expand Down Expand Up @@ -56,33 +59,18 @@ public override void OnCursorMoved(CursorMovedEventArgs e)
}

SVector2 mousePos = new SVector2(e.NewPosition.ScreenPixels.X, e.NewPosition.ScreenPixels.Y);
string hoveredNpc = this.SocialPage.GetCurrentlyHoveredNpc(mousePos);
if (hoveredNpc == string.Empty)
{
this.DrawCurrentFrame = false;
return;
}
UpdateHoveredNPC(mousePos);
}

if (hoveredNpc != this.LastHoveredNpc)
{
if (this.GiftDrawDataProvider.HasDataForNpc(hoveredNpc) &&
SetSelectedNPC(hoveredNpc))
{
this.DrawCurrentFrame = true;
this.LastHoveredNpc = hoveredNpc;
}
else
{
this.DrawCurrentFrame = false;
this.LastHoveredNpc = string.Empty;
}
}
else
{
this.LastHoveredNpc = string.Empty;
}
public override bool WantsUpdateEvent()
{
return true;
}

public override void OnPostUpdate(UpdateTickedEventArgs e)
{
this.SocialPage.OnUpdate();
}

/*********
** Protected methods
Expand Down Expand Up @@ -118,5 +106,42 @@ private SDVSocialPage GetNativeSocialPage(IClickableMenu menu)
}
}

private void UpdateHoveredNPC(SVector2 mousePos)
{
string hoveredNpc = this.SocialPage.GetCurrentlyHoveredNpc(mousePos);
if (hoveredNpc == string.Empty)
{
this.DrawCurrentFrame = false;
return;
}

if (hoveredNpc != this.LastHoveredNpc)
{
if (this.GiftDrawDataProvider.HasDataForNpc(hoveredNpc) &&
SetSelectedNPC(hoveredNpc))
{
this.DrawCurrentFrame = true;
this.LastHoveredNpc = hoveredNpc;
}
else
{
this.DrawCurrentFrame = false;
this.LastHoveredNpc = string.Empty;
}
}
else
{
this.LastHoveredNpc = string.Empty;
}
}

private void OnSlotIndexChanged()
{
// We currently only check if the hovered npc changed during mouse move events, so if the user
// scrolls the list without moving the mouse the tooltip won't change and it will be incorrect.
// Listening for when the slot index changes fixes this.
SVector2 mouse = new SVector2(Game1.getMouseX(), Game1.getMouseY());
UpdateHoveredNPC(mouse);
}
}
}
16 changes: 16 additions & 0 deletions GiftTasteHelper/GiftTasteHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,16 +284,32 @@ private void OnRendered(object sender, RenderedEventArgs e)
}
}

private void OnUpdateTicked(object sender, UpdateTickedEventArgs e)
{
Debug.Assert(this.CurrentGiftHelper != null, "OnUpdateTicked listener invoked when currentGiftHelper is null.");

if (this.CurrentGiftHelper.CanTick())
{
this.CurrentGiftHelper.OnPostUpdate(e);
}
}

private void UnsubscribeEvents()
{
Helper.Events.Input.CursorMoved -= OnCursorMoved;
Helper.Events.Display.Rendered -= this.OnRendered;
Helper.Events.GameLoop.UpdateTicked -= OnUpdateTicked;
}

private void SubscribeEvents()
{
Helper.Events.Input.CursorMoved += OnCursorMoved;
Helper.Events.Display.Rendered += this.OnRendered;

if (this.CurrentGiftHelper.WantsUpdateEvent())
{
Helper.Events.GameLoop.UpdateTicked += OnUpdateTicked;
}
}

#region Debug
Expand Down

0 comments on commit 8597cd1

Please sign in to comment.