use of net.runelite.client.ui.overlay.components.BackgroundComponent in project runelite by runelite.
the class PuzzleSolverOverlay method render.
@Override
public Dimension render(Graphics2D graphics) {
if ((!config.displaySolution() && !config.displayRemainingMoves()) || client.getGameState() != GameState.LOGGED_IN) {
return null;
}
ItemContainer container = client.getItemContainer(InventoryID.PUZZLE_BOX);
if (container == null) {
return null;
}
Widget puzzleBox = client.getWidget(WidgetInfo.PUZZLE_BOX);
if (puzzleBox == null) {
return null;
}
net.runelite.api.Point puzzleBoxLocation = puzzleBox.getCanvasLocation();
String infoString = "Solving..";
int[] itemIds = getItemIds(container);
boolean shouldCache = false;
if (solver != null) {
if (solver.hasFailed()) {
infoString = "The puzzle could not be solved";
} else {
if (solver.hasSolution()) {
boolean foundPosition = false;
// Find the current state by looking at the current step and then the next 5 steps
for (int i = 0; i < 6; i++) {
int j = solver.getPosition() + i;
if (j == solver.getStepCount()) {
break;
}
PuzzleState currentState = solver.getStep(j);
// If this is false, player has moved the empty tile
if (currentState != null && currentState.hasPieces(itemIds)) {
foundPosition = true;
solver.setPosition(j);
if (i > 0) {
shouldCache = true;
}
break;
}
}
// see if we can find the current state in the 5 previous steps
if (!foundPosition) {
for (int i = 1; i < 6; i++) {
int j = solver.getPosition() - i;
if (j < 0) {
break;
}
PuzzleState currentState = solver.getStep(j);
if (currentState != null && currentState.hasPieces(itemIds)) {
foundPosition = true;
shouldCache = true;
solver.setPosition(j);
break;
}
}
}
if (foundPosition) {
int stepsLeft = solver.getStepCount() - solver.getPosition() - 1;
if (stepsLeft == 0) {
infoString = "Solved!";
} else if (config.displayRemainingMoves()) {
infoString = "Moves left: " + stepsLeft;
} else {
infoString = null;
}
if (config.displaySolution()) {
if (config.drawDots()) {
graphics.setColor(Color.YELLOW);
// Display the next 4 steps
for (int i = 1; i < 5; i++) {
int j = solver.getPosition() + i;
if (j >= solver.getStepCount()) {
break;
}
PuzzleState futureMove = solver.getStep(j);
if (futureMove == null) {
break;
}
int blankX = futureMove.getEmptyPiece() % DIMENSION;
int blankY = futureMove.getEmptyPiece() / DIMENSION;
int markerSize = DOT_MARKER_SIZE - i * 3;
int x = puzzleBoxLocation.getX() + blankX * PUZZLE_TILE_SIZE + PUZZLE_TILE_SIZE / 2 - markerSize / 2;
int y = puzzleBoxLocation.getY() + blankY * PUZZLE_TILE_SIZE + PUZZLE_TILE_SIZE / 2 - markerSize / 2;
graphics.fillOval(x, y, markerSize, markerSize);
}
} else {
// Find the current blank tile position
PuzzleState currentMove = solver.getStep(solver.getPosition());
int lastBlankX = currentMove.getEmptyPiece() % DIMENSION;
int lastBlankY = currentMove.getEmptyPiece() / DIMENSION;
// Display the next 3 steps
for (int i = 1; i < 4; i++) {
int j = solver.getPosition() + i;
if (j >= solver.getStepCount()) {
break;
}
PuzzleState futureMove = solver.getStep(j);
if (futureMove == null) {
break;
}
int blankX = futureMove.getEmptyPiece() % DIMENSION;
int blankY = futureMove.getEmptyPiece() / DIMENSION;
int xDelta = blankX - lastBlankX;
int yDelta = blankY - lastBlankY;
BufferedImage arrow;
if (xDelta > 0) {
arrow = getRightArrow();
} else if (xDelta < 0) {
arrow = getLeftArrow();
} else if (yDelta > 0) {
arrow = getDownArrow();
} else {
arrow = getUpArrow();
}
int x = puzzleBoxLocation.getX() + blankX * PUZZLE_TILE_SIZE + PUZZLE_TILE_SIZE / 2 - arrow.getWidth() / 2;
int y = puzzleBoxLocation.getY() + blankY * PUZZLE_TILE_SIZE + PUZZLE_TILE_SIZE / 2 - arrow.getHeight() / 2;
OverlayUtil.renderImageLocation(graphics, new net.runelite.api.Point(x, y), arrow);
lastBlankX = blankX;
lastBlankY = blankY;
}
}
}
}
}
}
}
// Draw info box
if (infoString != null) {
int x = puzzleBoxLocation.getX() + puzzleBox.getWidth() / 2 - INFO_BOX_WIDTH / 2;
int y = puzzleBoxLocation.getY() - INFO_BOX_OFFSET_Y;
FontMetrics fm = graphics.getFontMetrics();
int height = INFO_BOX_TOP_BORDER + fm.getHeight() + INFO_BOX_BOTTOM_BORDER;
BackgroundComponent backgroundComponent = new BackgroundComponent();
backgroundComponent.setRectangle(new Rectangle(x, y, INFO_BOX_WIDTH, height));
backgroundComponent.render(graphics);
int textOffsetX = (INFO_BOX_WIDTH - fm.stringWidth(infoString)) / 2;
int textOffsetY = fm.getHeight();
TextComponent textComponent = new TextComponent();
textComponent.setPosition(new Point(x + textOffsetX, y + textOffsetY));
textComponent.setText(infoString);
textComponent.render(graphics);
}
// Solve the puzzle if we don't have an up to date solution
if (solver == null || cachedItems == null || (!shouldCache && !Arrays.equals(cachedItems, itemIds))) {
solve(itemIds);
shouldCache = true;
}
if (shouldCache) {
cacheItems(itemIds);
}
return null;
}
use of net.runelite.client.ui.overlay.components.BackgroundComponent in project runelite by runelite.
the class OpponentInfoOverlay method render.
@Override
public Dimension render(Graphics2D graphics) {
Actor opponent = getOpponent();
// If opponent is null, try to use last opponent
if (opponent == null) {
if (lastOpponent != null && clientNpcs[lastOpponent.getIndex()] != lastOpponent) {
// lastOpponent is no longer valid
lastOpponent = null;
} else {
opponent = lastOpponent;
}
} else {
// Update last opponent
lastOpponent = opponent instanceof NPC ? (NPC) opponent : null;
}
if (opponent != null && opponent.getHealth() > 0) {
lastTime = Instant.now();
lastRatio = (float) opponent.getHealthRatio() / (float) opponent.getHealth();
opponentName = Text.removeTags(opponent.getName());
lastMaxHealth = oppInfoHealth.get(opponentName + "_" + opponent.getCombatLevel());
Actor opponentsOpponent = opponent.getInteracting();
if (opponentsOpponent != null && (opponentsOpponent != client.getLocalPlayer() || client.getSetting(Varbits.MULTICOMBAT_AREA) == 1)) {
opponentsOpponentName = Text.removeTags(opponentsOpponent.getName());
} else {
opponentsOpponentName = null;
}
}
if (Duration.between(Instant.now(), lastTime).abs().compareTo(WAIT) > 0) {
// don't draw anything.
return null;
}
FontMetrics fm = graphics.getFontMetrics();
// opponent name
int height = TOP_BORDER + fm.getHeight();
if (lastRatio >= 0) {
height += BAR_HEIGHT + 6;
}
if (opponentsOpponentName != null) {
height += fm.getHeight() + 3;
}
height += 3;
height += BOTTOM_BORDER;
final BackgroundComponent backgroundComponent = new BackgroundComponent();
backgroundComponent.setRectangle(new Rectangle(0, 0, WIDTH, height));
backgroundComponent.render(graphics);
int y = TOP_BORDER + fm.getHeight();
{
int x = (WIDTH - fm.stringWidth(opponentName)) / 2;
final TextComponent textComponent = new TextComponent();
textComponent.setPosition(new Point(x, y));
textComponent.setText(opponentName);
textComponent.render(graphics);
y += 3;
}
if (lastRatio >= 0) {
int barWidth = (int) (lastRatio * (float) BAR_WIDTH);
y += 3;
graphics.setColor(HP_GREEN);
graphics.fillRect((WIDTH - BAR_WIDTH) / 2, y, barWidth, BAR_HEIGHT);
graphics.setColor(HP_RED);
graphics.fillRect(((WIDTH - BAR_WIDTH) / 2) + barWidth, y, BAR_WIDTH - barWidth, BAR_HEIGHT);
String str;
if (lastMaxHealth != null) {
int currHealth = (int) (lastRatio * lastMaxHealth);
str = currHealth + "/" + lastMaxHealth;
} else {
str = df.format(lastRatio * 100) + "%";
}
y += BAR_HEIGHT;
final TextComponent textComponent1 = new TextComponent();
textComponent1.setText(str);
textComponent1.setPosition(new Point((WIDTH - fm.stringWidth(str)) / 2, y));
textComponent1.render(graphics);
y += 3;
}
if (opponentsOpponentName != null) {
y += fm.getHeight();
int x = (WIDTH - fm.stringWidth(opponentsOpponentName)) / 2;
final TextComponent textComponent = new TextComponent();
textComponent.setPosition(new Point(x, y));
textComponent.setText(opponentsOpponentName);
textComponent.render(graphics);
}
return new Dimension(WIDTH, height);
}
Aggregations