Skip to content

Commit

Permalink
use script events to setup button
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam- committed Feb 23, 2024
1 parent f642421 commit 42ee75d
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 104 deletions.
3 changes: 3 additions & 0 deletions runelite-api/src/main/java/net/runelite/api/ScriptID.java
Original file line number Diff line number Diff line change
Expand Up @@ -449,4 +449,7 @@ public final class ScriptID

@ScriptArguments(integer = 6, string = 2)
public static final int CHATBOX_KEYINPUT_MATCHED = 2153;

@ScriptArguments(integer = 6)
public static final int EQUIPMENT_SET_STAT_BONUS_SETUP = 3517;
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,20 @@
import net.runelite.api.Item;
import net.runelite.api.ItemContainer;
import net.runelite.api.ItemID;
import net.runelite.api.ScriptID;
import net.runelite.api.Skill;
import net.runelite.api.SpriteID;
import net.runelite.api.VarPlayer;
import net.runelite.api.Varbits;
import net.runelite.api.annotations.Component;
import net.runelite.api.annotations.Interface;
import net.runelite.api.events.WidgetLoaded;
import net.runelite.api.events.ScriptPreFired;
import net.runelite.api.widgets.ComponentID;
import net.runelite.api.widgets.InterfaceID;
import net.runelite.api.widgets.JavaScriptCallback;
import net.runelite.api.widgets.Widget;
import net.runelite.api.widgets.WidgetType;
import net.runelite.api.widgets.WidgetUtil;
import net.runelite.client.callback.ClientThread;
import net.runelite.client.eventbus.EventBus;
import net.runelite.client.eventbus.Subscribe;
Expand Down Expand Up @@ -125,24 +127,33 @@ private WikiDpsManager(
public void startUp()
{
eventBus.register(this);
clientThread.invokeLater(() -> tryAddButton(client, this::launch));
clientThread.invokeLater(() -> tryAddButton(this::launch));
}

public void shutDown()
{
eventBus.unregister(this);
clientThread.invokeLater(() -> removeButton(client));
clientThread.invokeLater(this::removeButton);
}

@Subscribe
public void onWidgetLoaded(WidgetLoaded ev)
public void onScriptPreFired(ScriptPreFired scriptPreFired)
{
for (Screen screen : Screen.values())
if (scriptPreFired.getScriptId() == ScriptID.EQUIPMENT_SET_STAT_BONUS_SETUP)
{
if (ev.getGroupId() == screen.getInterfaceId())
int interfaceId = WidgetUtil.componentToInterface((int) scriptPreFired.getScriptEvent().getArguments()[1]);
boolean setBonus = (int) scriptPreFired.getScriptEvent().getArguments()[4] == 1;

if (!setBonus)
{
addButton(client, screen, this::launch);
break;
if (interfaceId == InterfaceID.BANK_EQUIPMENT)
{
clientThread.invokeLater(() -> addButton(Screen.BANK_EQUIPMENT, this::launch));
}
else if (interfaceId == InterfaceID.EQUIPMENT_BONUSES)
{
addButton(Screen.EQUIPMENT_BONUSES, this::launch);
}
}
}
}
Expand All @@ -151,16 +162,10 @@ public void onWidgetLoaded(WidgetLoaded ev)
@RequiredArgsConstructor
enum Screen
{
EQUIPMENT_BONUSES(InterfaceID.EQUIPMENT_BONUSES, ComponentID.EQUIPMENT_BONUSES_PARENT, ComponentID.EQUIPMENT_BONUSES_SET_BONUS, ComponentID.EQUIPMENT_BONUSES_STAT_BONUS, 55),
BANK_EQUIPMENT(InterfaceID.BANK_EQUIPMENT, ComponentID.BANK_EQUIPMENT_PARENT, ComponentID.BANK_EQUIPMENT_SET_BONUS, ComponentID.BANK_EQUIPMENT_STAT_BONUS, 49),
EQUIPMENT_BONUSES(ComponentID.EQUIPMENT_BONUSES_PARENT, ComponentID.EQUIPMENT_BONUSES_SET_BONUS, ComponentID.EQUIPMENT_BONUSES_STAT_BONUS, 55),
BANK_EQUIPMENT(ComponentID.BANK_EQUIPMENT_PARENT, ComponentID.BANK_EQUIPMENT_SET_BONUS, ComponentID.BANK_EQUIPMENT_STAT_BONUS, 49),
;

/**
* interface containing all the relevant widgets
*/
@Getter(onMethod_ = @Interface)
private final int interfaceId;

/**
* parent widget of the interface, install target
*/
Expand All @@ -186,115 +191,112 @@ enum Screen

}

void tryAddButton(Client client, Runnable onClick)
void tryAddButton(Runnable onClick)
{
for (Screen screen : Screen.values())
{
addButton(client, screen, onClick);
addButton(screen, onClick);
}
}

/**
* Shifts over the Set Bonus / Stat Bonus buttons
* and adds new widgets to make a visually equal button with a different name.
*/
void addButton(Client client, Screen screen, Runnable onClick)
void addButton(Screen screen, Runnable onClick)
{
clientThread.invokeLater(() ->
Widget parent = client.getWidget(screen.getParentId());
Widget setBonus = client.getWidget(screen.getSetBonusId());
Widget statBonus = client.getWidget(screen.getStatBonusId());
Widget[] refComponents;
if (parent == null || setBonus == null || statBonus == null || (refComponents = setBonus.getChildren()) == null)
{
Widget parent = client.getWidget(screen.getParentId());
Widget setBonus = client.getWidget(screen.getSetBonusId());
Widget statBonus = client.getWidget(screen.getStatBonusId());
Widget[] refComponents;
if (parent == null || setBonus == null || statBonus == null || (refComponents = setBonus.getChildren()) == null)
{
return;
}
return;
}

// Since the Set Bonus button uses absolute positioning,
// we must also use absolute for all the children below,
// which means it's necessary to offset the values by simulating corresponding pos/size modes.
int padding = 8;
int w = setBonus.getOriginalWidth();
int h = setBonus.getOriginalHeight();
int x = setBonus.getOriginalX() + (w / 2) + (padding / 2);
int y = setBonus.getOriginalY();
if (screen == Screen.BANK_EQUIPMENT) // uses ABSOLUTE_CENTER
{
y += parent.getHeight() / 2 - setBonus.getHeight() / 2;
}
// Since the Set Bonus button uses absolute positioning,
// we must also use absolute for all the children below,
// which means it's necessary to offset the values by simulating corresponding pos/size modes.
int padding = 8;
int w = setBonus.getOriginalWidth();
int h = setBonus.getOriginalHeight();
int x = setBonus.getOriginalX() + (w / 2) + (padding / 2);
int y = setBonus.getOriginalY();
if (screen == Screen.BANK_EQUIPMENT) // uses ABSOLUTE_CENTER
{
y += parent.getHeight() / 2 - setBonus.getHeight() / 2;
}

// now shift the Set Bonus and Stat Bonus buttons over a bit to make room
setBonus.setOriginalX(setBonus.getOriginalX() - (w / 2) - (padding / 2))
.revalidate();
statBonus.setOriginalX(statBonus.getOriginalX() - (w / 2) - (padding / 2))
.revalidate();

final Widget[] spriteWidgets = new Widget[9];

// the background uses ABSOLUTE_CENTER and MINUS sizing
int bgWidth = w - refComponents[0].getOriginalWidth();
int bgHeight = h - refComponents[0].getOriginalHeight();
int bgX = (x + refComponents[0].getOriginalX()) + (w - bgWidth) / 2;
int bgY = (y + refComponents[0].getOriginalY()) + (h - bgHeight) / 2;
spriteWidgets[0] = parent.createChild(-1, WidgetType.GRAPHIC)
.setSpriteId(refComponents[0].getSpriteId())
.setPos(bgX, bgY)
.setSize(bgWidth, bgHeight);
spriteWidgets[0].revalidate();

// borders and corners all use absolute positioning which is easy
for (int i = 1; i < 9; i++)
{
spriteWidgets[i] = parent.createChild(-1, WidgetType.GRAPHIC)
.setSpriteId(refComponents[i].getSpriteId())
.setPos(x + refComponents[i].getOriginalX(), y + refComponents[i].getOriginalY())
.setSize(refComponents[i].getOriginalWidth(), refComponents[i].getOriginalHeight());
spriteWidgets[i].revalidate();
}
// now shift the Set Bonus and Stat Bonus buttons over a bit to make room
setBonus.setOriginalX(setBonus.getOriginalX() - (w / 2) - (padding / 2))
.revalidate();
statBonus.setOriginalX(statBonus.getOriginalX() - (w / 2) - (padding / 2))
.revalidate();

final Widget[] spriteWidgets = new Widget[9];

// the background uses ABSOLUTE_CENTER and MINUS sizing
int bgWidth = w - refComponents[0].getOriginalWidth();
int bgHeight = h - refComponents[0].getOriginalHeight();
int bgX = (x + refComponents[0].getOriginalX()) + (w - bgWidth) / 2;
int bgY = (y + refComponents[0].getOriginalY()) + (h - bgHeight) / 2;
spriteWidgets[0] = parent.createChild(-1, WidgetType.GRAPHIC)
.setSpriteId(refComponents[0].getSpriteId())
.setPos(bgX, bgY)
.setSize(bgWidth, bgHeight);
spriteWidgets[0].revalidate();

// borders and corners all use absolute positioning which is easy
for (int i = 1; i < 9; i++)
{
spriteWidgets[i] = parent.createChild(-1, WidgetType.GRAPHIC)
.setSpriteId(refComponents[i].getSpriteId())
.setPos(x + refComponents[i].getOriginalX(), y + refComponents[i].getOriginalY())
.setSize(refComponents[i].getOriginalWidth(), refComponents[i].getOriginalHeight());
spriteWidgets[i].revalidate();
}

// text label uses ABSOLUTE_CENTER positioning and MINUS sizing,
// but matches size of parent so effectively no-op
final Widget text = parent.createChild(-1, WidgetType.TEXT)
.setText("View DPS")
.setTextColor(FONT_COLOUR_INACTIVE)
.setFontId(refComponents[9].getFontId())
.setTextShadowed(refComponents[9].getTextShadowed())
.setXTextAlignment(refComponents[9].getXTextAlignment())
.setYTextAlignment(refComponents[9].getYTextAlignment())
.setPos(x, y)
.setSize(w, h);
text.revalidate();

// we'll give the text layer the listeners since it covers the whole area
text.setHasListener(true);
text.setOnMouseOverListener((JavaScriptCallback) ev ->
// text label uses ABSOLUTE_CENTER positioning and MINUS sizing,
// but matches size of parent so effectively no-op
final Widget text = parent.createChild(-1, WidgetType.TEXT)
.setText("View DPS")
.setTextColor(FONT_COLOUR_INACTIVE)
.setFontId(refComponents[9].getFontId())
.setTextShadowed(refComponents[9].getTextShadowed())
.setXTextAlignment(refComponents[9].getXTextAlignment())
.setYTextAlignment(refComponents[9].getYTextAlignment())
.setPos(x, y)
.setSize(w, h);
text.revalidate();

// we'll give the text layer the listeners since it covers the whole area
text.setHasListener(true);
text.setOnMouseOverListener((JavaScriptCallback) ev ->
{
for (int i = 0; i <= 8; i++)
{
for (int i = 0; i <= 8; i++)
{
spriteWidgets[i].setSpriteId(SPRITE_IDS_ACTIVE[i]);
}
text.setTextColor(FONT_COLOUR_ACTIVE);
});
text.setOnMouseLeaveListener((JavaScriptCallback) ev ->
spriteWidgets[i].setSpriteId(SPRITE_IDS_ACTIVE[i]);
}
text.setTextColor(FONT_COLOUR_ACTIVE);
});
text.setOnMouseLeaveListener((JavaScriptCallback) ev ->
{
for (int i = 0; i <= 8; i++)
{
for (int i = 0; i <= 8; i++)
{
spriteWidgets[i].setSpriteId(SPRITE_IDS_INACTIVE[i]);
}
text.setTextColor(FONT_COLOUR_INACTIVE);
});
spriteWidgets[i].setSpriteId(SPRITE_IDS_INACTIVE[i]);
}
text.setTextColor(FONT_COLOUR_INACTIVE);
});

// register a click listener
text.setAction(0, "View DPS on OSRS Wiki");
text.setOnOpListener((JavaScriptCallback) ev -> onClick.run());
// register a click listener
text.setAction(0, "View DPS on OSRS Wiki");
text.setOnOpListener((JavaScriptCallback) ev -> onClick.run());

// recompute locations / sizes on parent
parent.revalidate();
});
// recompute locations / sizes on parent
parent.revalidate();
}

void removeButton(Client client)
void removeButton()
{
for (Screen screen : Screen.values())
{
Expand Down

0 comments on commit 42ee75d

Please sign in to comment.