1
0
mirror of https://github.com/davpapp/PowerMiner synced 2024-11-14 21:45:14 -05:00
PowerMiner/src/Inventory.java

110 lines
3.8 KiB
Java
Raw Normal View History

import java.awt.AWTException;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
2018-02-02 05:25:00 -05:00
import java.io.File;
import java.io.IOException;
2018-01-31 08:12:02 -05:00
2018-02-02 05:25:00 -05:00
import javax.imageio.ImageIO;
2018-01-31 08:12:02 -05:00
public class Inventory {
Robot robot;
Rectangle inventoryRectangleToCapture;
2018-01-31 08:12:02 -05:00
InventorySlot[][] inventorySlots;
InventoryItems items;
2018-01-31 08:12:02 -05:00
public Inventory() throws AWTException, IOException {
initializeInventoryRectangle();
initializeInventorySlots();
initializeItems();
robot = new Robot();
}
private void initializeInventoryRectangle() {
inventoryRectangleToCapture = new Rectangle(Constants.INVENTORY_WINDOW_OFFSET_X, Constants.INVENTORY_WINDOW_OFFSET_Y, Constants.INVENTORY_WINDOW_WIDTH, Constants.INVENTORY_WINDOW_HEIGHT);
}
private void initializeInventorySlots() {
2018-01-31 08:12:02 -05:00
inventorySlots = new InventorySlot[4][7];
for (int row = 0; row < Constants.INVENTORY_NUM_ROWS; row++) {
for (int column = 0; column < Constants.INVENTORY_NUM_COLUMNS; column++) {
inventorySlots[row][column] = new InventorySlot(row, column);
2018-01-31 08:12:02 -05:00
}
}
}
private void initializeItems() throws IOException {
items = new InventoryItems(Paths.INVENTORY_ITEMS_DIRECTORY_PATH);
}
public void update() throws IOException {
BufferedImage image = robot.createScreenCapture(this.inventoryRectangleToCapture);
updateAllInventorySlots(image);
}
public void updateLastSlot() throws IOException {
BufferedImage image = robot.createScreenCapture(this.inventoryRectangleToCapture);
updateLastInventorySlot(image);
}
private void updateAllInventorySlots(BufferedImage image) throws IOException {
for (int row = 0; row < Constants.INVENTORY_NUM_ROWS; row++) {
for (int column = 0; column < Constants.INVENTORY_NUM_COLUMNS; column++) {
2018-02-01 22:39:57 -05:00
inventorySlots[row][column].updateInventorySlot(image);
}
}
}
private void updateLastInventorySlot(BufferedImage image) throws IOException {
inventorySlots[Constants.INVENTORY_NUM_ROWS - 1][Constants.INVENTORY_NUM_COLUMNS - 1].updateInventorySlot(image);
}
2018-02-22 11:58:54 -05:00
public void updateAndWriteAllInventoryImages() throws IOException {
BufferedImage image = robot.createScreenCapture(this.inventoryRectangleToCapture);
2018-02-22 11:58:54 -05:00
ImageIO.write(image, "png", new File("/home/dpapp/Desktop/RunescapeAI/Tests/Inventory/inventory_TO_RENAME.png"));
writeAllInventorySlotImages(image);
}
2018-02-22 11:58:54 -05:00
private void writeAllInventorySlotImages(BufferedImage image) throws IOException {
for (int row = 0; row < Constants.INVENTORY_NUM_ROWS; row++) {
for (int column = 0; column < Constants.INVENTORY_NUM_COLUMNS; column++) {
inventorySlots[row][column].writeInventorySlotImage(image, row, column);
}
}
2018-01-31 08:12:02 -05:00
}
public String getItemNameInInventorySlot(int row, int column) {
return inventorySlots[row][column].getItemNameInInventorySlot(items);
}
public boolean isLastSlotInInventoryFull() {
return !inventorySlots[Constants.INVENTORY_NUM_ROWS - 1][Constants.INVENTORY_NUM_COLUMNS - 1].isInventorySlotEmpty(items);
}
public boolean isInventoryFull() {
// TODO: this will fail if some unexpected item shows up
for (int row = 0; row < Constants.INVENTORY_NUM_ROWS; row++) {
for (int column = 0; column < Constants.INVENTORY_NUM_COLUMNS; column++) {
if (inventorySlots[row][column].isInventorySlotEmpty(items)) {
return false;
}
}
}
return true;
}
public Point getClickCoordinatesForInventorySlot(int row, int column) {
Point centerOfInventorySlot = inventorySlots[row][column].getClickablePointWithinItemSlot();
int x = Constants.INVENTORY_WINDOW_OFFSET_X + row * Constants.INVENTORY_SLOT_WIDTH + centerOfInventorySlot.x;
int y = Constants.INVENTORY_WINDOW_OFFSET_Y + column * Constants.INVENTORY_SLOT_HEIGHT + centerOfInventorySlot.y;
return new Point(x, y);
}
// For testing only
public void updateWithFakeImageForTests(BufferedImage testImage) throws IOException {
updateAllInventorySlots(testImage);
}
2018-01-31 08:12:02 -05:00
}