Skip to content
This repository has been archived by the owner on Apr 8, 2023. It is now read-only.

client: Improve menumanager #2026

Merged
merged 5 commits into from
Nov 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions runelite-api/src/main/java/net/runelite/api/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -1905,4 +1905,10 @@ public interface Client extends GameShell
* the "You have been disconnected." message anymore.
*/
void setHideDisconnect(boolean dontShow);

/**
* Sets the fields in the temporary menu entry that's saved in the client
* when a inventory item is clicked and dragged.
*/
void setTempMenuEntry(MenuEntry entry);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
package net.runelite.api.events;

import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import net.runelite.api.MenuEntry;
Expand All @@ -42,7 +41,6 @@
* it seems that this event still triggers with the "Cancel" action.
*/
@Getter
@EqualsAndHashCode(callSuper = true)
public class MenuOptionClicked extends MenuEntry implements Event
{
public MenuOptionClicked(String option, String target, int identifier, int opcode, int param0, int param1, boolean forceLeftClick)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import static net.runelite.api.MenuOpcode.MENU_ACTION_DEPRIORITIZE_OFFSET;
import net.runelite.api.NPCDefinition;
import net.runelite.api.events.BeforeRender;
import net.runelite.api.events.ClientTick;
import net.runelite.api.events.MenuEntryAdded;
import net.runelite.api.events.MenuOpened;
import net.runelite.api.events.MenuOptionClicked;
Expand Down Expand Up @@ -89,7 +90,6 @@ public class MenuManager
private final Map<AbstractComparableEntry, AbstractComparableEntry> swaps = new HashMap<>();

private MenuEntry leftClickEntry = null;
private MenuEntry firstEntry = null;

private int playerAttackIdx = -1;

Expand All @@ -102,11 +102,15 @@ private MenuManager(Client client, EventBus eventBus)

eventBus.subscribe(MenuOpened.class, this, this::onMenuOpened);
eventBus.subscribe(MenuEntryAdded.class, this, this::onMenuEntryAdded);
eventBus.subscribe(BeforeRender.class, this, this::onBeforeRender);
eventBus.subscribe(PlayerMenuOptionsChanged.class, this, this::onPlayerMenuOptionsChanged);
eventBus.subscribe(NpcActionChanged.class, this, this::onNpcActionChanged);
eventBus.subscribe(WidgetPressed.class, this, this::onWidgetPressed);
eventBus.subscribe(MenuOptionClicked.class, this, this::onMenuOptionClicked);

// Make sure last tick's entry gets cleared
eventBus.subscribe(ClientTick.class, this, tick -> leftClickEntry = null);
// Rebuild left click menu for top left entry
eventBus.subscribe(BeforeRender.class, this, br -> rebuildLeftClickMenu());
}

/**
Expand Down Expand Up @@ -153,7 +157,7 @@ private void onMenuOpened(MenuOpened event)
// Need to reorder the list to normal, then rebuild with swaps
MenuEntry[] oldEntries = event.getMenuEntries();

firstEntry = null;
leftClickEntry = null;

List<MenuEntry> newEntries = Lists.newArrayList(oldEntries);

Expand Down Expand Up @@ -281,25 +285,20 @@ private void onMenuEntryAdded(MenuEntryAdded event)
}
}

private void onBeforeRender(BeforeRender event)
{
rebuildLeftClickMenu();
}

private MenuEntry rebuildLeftClickMenu()
private void rebuildLeftClickMenu()
{
leftClickEntry = null;
if (client.isMenuOpen())
{
return null;
return;
}

int menuOptionCount = client.getMenuOptionCount();
if (menuOptionCount <= 2)
{
return null;
return;
}

firstEntry = null;
MenuEntry[] entries = new MenuEntry[menuOptionCount + priorityEntries.size()];
System.arraycopy(client.getMenuEntries(), 0, entries, 0, menuOptionCount);

Expand All @@ -308,21 +307,19 @@ private MenuEntry rebuildLeftClickMenu()
indexPriorityEntries(entries, menuOptionCount);
}

if (firstEntry == null && !swaps.isEmpty())
if (leftClickEntry == null && !swaps.isEmpty())
{
indexSwapEntries(entries, menuOptionCount);
}


if (firstEntry == null)
if (leftClickEntry == null)
{
// stop being null smh
firstEntry = entries[menuOptionCount - 1];
leftClickEntry = entries[menuOptionCount - 1];
}

client.setMenuEntries(entries);

return firstEntry;
}

public void addPlayerMenuItem(String menuText)
Expand Down Expand Up @@ -426,16 +423,18 @@ private void removeNpcOption(NPCDefinition composition, String npcOption)

private void onWidgetPressed(WidgetPressed event)
{
leftClickEntry = rebuildLeftClickMenu();
rebuildLeftClickMenu();
client.setTempMenuEntry(leftClickEntry);
}

private void onMenuOptionClicked(MenuOptionClicked event)
{
if (!client.isMenuOpen() && event.isAuthentic())
// option and target will be the same if this one came from "tempMenuAction"
if (!client.isMenuOpen() && !event.getOption().equals(event.getTarget()) && event.isAuthentic())
{
if (event.getMouseButton() != 0)
if (!event.equals(leftClickEntry))
{
leftClickEntry = rebuildLeftClickMenu();
rebuildLeftClickMenu();
}

if (leftClickEntry != null)
Expand Down Expand Up @@ -879,7 +878,7 @@ private void indexPriorityEntries(MenuEntry[] entries, int menuOptionCount)
entries[menuOptionCount + i] = prios[i].entry;
}

firstEntry = entries[menuOptionCount + i - 1];
leftClickEntry = entries[menuOptionCount + i - 1];

}

Expand Down Expand Up @@ -922,7 +921,7 @@ private void indexSwapEntries(MenuEntry[] entries, int menuOptionCount)

entries[i] = first;
entries[menuOptionCount - 1] = entry;
firstEntry = entry;
leftClickEntry = entry;
return;
}
}
Expand Down
34 changes: 34 additions & 0 deletions runelite-mixins/src/main/java/net/runelite/mixins/MenuMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@
package net.runelite.mixins;

import net.runelite.api.MenuEntry;
import net.runelite.api.events.WidgetPressed;
import net.runelite.api.mixins.FieldHook;
import net.runelite.api.mixins.Inject;
import net.runelite.api.mixins.Mixin;
import net.runelite.api.mixins.Shadow;
import net.runelite.rs.api.RSClient;
import net.runelite.rs.api.RSFont;
import net.runelite.rs.api.RSMenuAction;

@Mixin(RSClient.class)
public abstract class MenuMixin implements RSClient
Expand All @@ -41,6 +45,12 @@ public abstract class MenuMixin implements RSClient
private static final int MENU_HEADER_GRADIENT_TOP_2010 = 0x322E22;
private static final int MENU_HEADER_GRADIENT_BOTTOM_2010 = 0x090A04;

@Shadow("client")
private static RSClient client;

@Shadow("tempMenuAction")
private static RSMenuAction tempMenuAction;

@Inject
@Override
public void draw2010Menu()
Expand Down Expand Up @@ -140,4 +150,28 @@ public void setLeftClickMenuEntry(final MenuEntry entry)
getMenuArguments2()[i] = entry.getParam1();
getMenuForceLeftClick()[i] = entry.isForceLeftClick();
}

@Inject
@FieldHook("tempMenuAction")
public static void onTempMenuActionChanged(int idx)
{
if (tempMenuAction != null)
{
client.getCallbacks().post(WidgetPressed.class, WidgetPressed.INSTANCE);
}
}

@Inject
@Override
public void setTempMenuEntry(MenuEntry entry)
{
if (entry == null || tempMenuAction == null)
return;

tempMenuAction.setOption(entry.getOption());
tempMenuAction.setOpcode(entry.getOpcode());
tempMenuAction.setIdentifier(entry.getIdentifier());
tempMenuAction.setParam0(entry.getParam0());
tempMenuAction.setParam1(entry.getParam1());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@
import net.runelite.api.events.VarbitChanged;
import net.runelite.api.events.VolumeChanged;
import net.runelite.api.events.WidgetLoaded;
import net.runelite.api.events.WidgetPressed;
import net.runelite.api.hooks.Callbacks;
import net.runelite.api.hooks.DrawCallbacks;
import net.runelite.api.mixins.Copy;
Expand Down Expand Up @@ -1403,16 +1402,6 @@ public void invokeMenuAction(int actionParam, int widgetId, int menuAction, int
client.sendMenuAction(actionParam, widgetId, menuAction, id, menuOption, "!AUTHENTIC" + menuTarget, var6, var7);
}

@Inject
@FieldHook("tempMenuAction")
public static void onTempMenuActionChanged(int idx)
{
if (client.getTempMenuAction() != null)
{
client.getCallbacks().post(WidgetPressed.class, WidgetPressed.INSTANCE);
}
}

@FieldHook("Login_username")
@Inject
public static void onUsernameChanged(int idx)
Expand Down
Loading