Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Font;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Image;
- import java.awt.Point;
- import java.awt.Rectangle;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.awt.event.MouseMotionListener;
- import java.awt.font.FontRenderContext;
- import java.awt.geom.AffineTransform;
- import java.awt.geom.RoundRectangle2D;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.text.DecimalFormat;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.logging.Logger;
- import javax.imageio.ImageIO;
- import javax.swing.JOptionPane;
- import org.rsbot.event.listeners.PaintListener;
- import org.rsbot.script.Script;
- import org.rsbot.script.ScriptManifest;
- import org.rsbot.script.methods.Equipment;
- import org.rsbot.script.methods.Game;
- import org.rsbot.script.methods.Skills;
- import org.rsbot.script.wrappers.RSComponent;
- import org.rsbot.script.wrappers.RSInterface;
- import org.rsbot.script.wrappers.RSObject;
- import org.rsbot.util.GlobalConfiguration;
- @ScriptManifest(authors = { "Equinox_" }, name = "Equinox Track Smither", version = 1.0, description = "Smiths Steel, Iron, and Bronze tracks in the Artisians Workshop. Start by the anvils downstairs.")
- public class EquinoxTrackSmither extends Script implements PaintListener,
- MouseMotionListener, MouseListener {
- PaintInterface Paint;
- ModifiedDrawMouse drawMouse = new ModifiedDrawMouse();
- final String baseURL = "http://equinox-scripts.web44.net/scripts/natcrafter";
- final double VERSION = getClass().getAnnotation(ScriptManifest.class)
- .version();
- final String author = PaintFormatter.formatList(getClass().getAnnotation(
- ScriptManifest.class).authors());
- final String name = PaintFormatter.properCaseAll(getClass().getAnnotation(
- ScriptManifest.class).name());
- // Levels
- static final int STEEL_LEVEL = 60;
- static final int IRON_LEVEL = 35;
- static final int BRONZE_LEVEL = 12;
- // Items TODO Add gold hammer ID
- static final int HAMMER_ID = 2347;
- static final int GOLD_HAMMER_ID = 0;
- // Objects
- static final int[] TUNNEL_ID = { 24826, 24825 };
- static final int ANVIL_ID = 24820;
- // Interfaces
- static final int NUMBER_SELECTION_INTERFACE = 916;
- static final int NUMBER_SELECTION_ALL = 8;
- static final int NUMBER_SELECTION_ALL_TEXT = 17;
- static final int NUMBER_SELECTION_SCROLL_OUT = 22;
- static final int NUMBER_SELECTION_BARRIER = 9;
- static final int NUMBER_SELECTION_ITEM_INTERFACE = 905;
- static final int NUMBER_SELECTION_INGOT_SELECT = 14;
- public Metal metal = new Metal();
- public int TrackCount = 13;
- public State state = State.Wait;
- public int currentSmith = 0;
- public int metalType = -1;
- public boolean isMember = false;
- class Metal {
- String def = "";
- int INGOT_ID;
- int TROUGH_ID;
- // Track
- int TRACK_20; // Same as rails
- int TRACK_40;
- int TRACK_60;
- int TRACK_80;
- int TRACK_100;
- // Track pieces
- int Rails;
- int Base_Plate;
- int Spikes;
- int Joint;
- int Tie;
- int[] getAll() {
- return new int[] { INGOT_ID, TRACK_20, TRACK_40, TRACK_60,
- TRACK_80, TRACK_100, Rails, Base_Plate, Spikes, Joint, Tie };
- }
- String getName(int id) {
- if (id == Rails)
- return "Rails";
- if (id == Base_Plate)
- return "Base Plate";
- if (id == Spikes)
- return "Spikes";
- if (id == Joint)
- return "Joint";
- if (id == Tie)
- return "Tie";
- if (id == TRACK_40)
- return "Track 40%";
- if (id == TRACK_60)
- return "Track 60%";
- if (id == TRACK_80)
- return "Track 80%";
- if (id == TRACK_100)
- return "Track 100%";
- return "";
- }
- int getUnfTrackCount() {
- return inventory.getCount(TRACK_20, TRACK_40, TRACK_60, TRACK_80);
- }
- int getComponent(int id) {
- if (id == TRACK_40 || id == TRACK_60 || id == TRACK_80
- || id == TRACK_100)
- return 14;
- if (id >= Rails && id <= Tie)
- return id - Rails + 14;
- return -1;
- }
- int getSmithingID() {
- if (inventory.getCount(INGOT_ID) > 0) {
- if (inventory.getCount(TRACK_40, TRACK_60, TRACK_80) == 0) {
- if (inventory.getCount(TRACK_20) < TrackCount)
- return Rails;
- if (inventory.getCount(Base_Plate) < TrackCount)
- return Base_Plate;
- }
- if (inventory.contains(TRACK_40))
- if (inventory.getCount(Spikes) < TrackCount)
- return Spikes;
- if (inventory.contains(TRACK_60))
- if (inventory.getCount(Joint) < TrackCount)
- return Joint;
- if (inventory.contains(TRACK_80))
- if (inventory.getCount(Tie) < TrackCount)
- return Tie;
- }
- // Proceed to Track-Material upgrade:
- for (int i = 0; i < 4; i++) {
- if (inventory.getCount(i == 0 ? TRACK_20 : (TRACK_40 + i - 1)) >= TrackCount
- && inventory.getCount(Base_Plate + i) >= TrackCount)
- return TRACK_40 + i;
- }
- return -1;
- }
- int[] getNeededIDs() {
- if (inventory.getCount(INGOT_ID) > 0) {
- if (inventory.getCount(TRACK_40, TRACK_60, TRACK_80) == 0) {
- if (inventory.getCount(TRACK_20) < TrackCount)
- return new int[] { INGOT_ID };
- if (inventory.getCount(Base_Plate) < TrackCount)
- return new int[] { INGOT_ID };
- }
- if (inventory.contains(TRACK_40))
- if (inventory.getCount(Spikes) < TrackCount)
- return new int[] { INGOT_ID };
- if (inventory.contains(TRACK_60))
- if (inventory.getCount(Joint) < TrackCount)
- return new int[] { INGOT_ID };
- if (inventory.contains(TRACK_80))
- if (inventory.getCount(Tie) < TrackCount)
- return new int[] { INGOT_ID };
- }
- // Proceed to Track-Material upgrade:
- for (int i = 0; i < 4; i++) {
- if (inventory.getCount(i == 0 ? TRACK_20 : (TRACK_40 + i - 1)) >= TrackCount
- && inventory.getCount(Base_Plate + i) >= TrackCount)
- return new int[] { i == 0 ? TRACK_20 : (TRACK_40 + i - 1),
- Base_Plate + i };
- }
- return new int[] { -1 };
- }
- }
- Metal getSteel() {
- Metal metal = new Metal();
- metal.def = "steel";
- metal.INGOT_ID = 20504;
- metal.TROUGH_ID = 24823;
- // Track
- metal.TRACK_20 = 20520;
- metal.TRACK_40 = 20529;
- metal.TRACK_60 = 20530;
- metal.TRACK_80 = 20531;
- metal.TRACK_100 = 20532;
- // Track pieces
- metal.Rails = 20520;
- metal.Base_Plate = 20521;
- metal.Spikes = 20522;
- metal.Joint = 20523;
- metal.Tie = 20524;
- return metal;
- }
- Metal getIron() {
- Metal metal = new Metal();
- metal.def = "iron";
- metal.INGOT_ID = 20503;
- metal.TROUGH_ID = 24822;
- // Track
- metal.TRACK_20 = 20515;
- metal.TRACK_40 = 20525;
- metal.TRACK_60 = 20526;
- metal.TRACK_80 = 20527;
- metal.TRACK_100 = 20528;
- // Track pieces
- metal.Rails = 20515;
- metal.Base_Plate = 20516;
- metal.Spikes = 20517;
- metal.Joint = 20518;
- metal.Tie = 20519;
- return metal;
- }
- Metal getBronze() {
- Metal metal = new Metal();
- metal.def = "bronze";
- metal.INGOT_ID = 20502;
- metal.TROUGH_ID = 24821;
- // Track
- metal.TRACK_20 = 20506;
- metal.TRACK_40 = 20511;
- metal.TRACK_60 = 20512;
- metal.TRACK_80 = 20513;
- metal.TRACK_100 = 20514;
- // Track pieces
- metal.Rails = 20506;
- metal.Base_Plate = 20507;
- metal.Spikes = 20508;
- metal.Joint = 20509;
- metal.Tie = 20510;
- return metal;
- }
- void setSteel() {
- metal = getSteel();
- dropAllOf(getBronze().getAll());
- dropAllOf(getIron().getAll());
- log.info("Set track type to steel!");
- }
- void setIron() {
- metal = getIron();
- dropAllOf(getBronze().getAll());
- dropAllOf(getSteel().getAll());
- log.info("Set track type to iron!");
- }
- void setBronze() {
- metal = getBronze();
- dropAllOf(getSteel().getAll());
- dropAllOf(getIron().getAll());
- log.info("Set track type to bronze!");
- }
- enum State {
- Wait, NumberSelection_SELECT_ALL, NumberSelection_SCROLL_OUT, NumberSelection_CLICK, GetIngots, Smith, LayTrack
- }
- public boolean onStart() {
- if (!(inventory.contains(HAMMER_ID) || equipment
- .containsOneOf(GOLD_HAMMER_ID))) {
- log.severe("You must start with a hammer in your inventory!");
- return false;
- }
- int sLevel = skills.getCurrentLevel(Skills.SMITHING);
- if ((isMember = (JOptionPane.YES_OPTION == JOptionPane
- .showConfirmDialog(null, "Are you a member?",
- "Equinox_ Track Smither", JOptionPane.YES_NO_OPTION)))
- && sLevel >= STEEL_LEVEL) {
- setSteel();
- } else if (sLevel >= IRON_LEVEL) {
- setIron();
- } else if (sLevel >= BRONZE_LEVEL) {
- setBronze();
- } else {
- // No high enough level
- log.severe("You don't have the required smithing level! (Level 12)");
- return false;
- }
- int cE = inventory.getCountExcept(metal.getAll());
- if (cE == 0)
- TrackCount = 14;
- if (cE > 2) {
- // Extra items
- log.severe("You have move than 2 non-required items in your inventory. Please dispose of them.");
- return false;
- }
- if (cE == 1) {
- log.info("Attempting to fill an inventory slot.");
- if (equipment.getCount() > 0)
- for (int i = 0; i < Equipment.ITEM_SLOTS; i++)
- if (equipment.getItem(i) != null
- && equipment.getItem(i).getID() != GOLD_HAMMER_ID)
- if (equipment.getItem(i).doClick(true))
- break;
- }
- Paint = new PaintInterface(this, log, Skills.SMITHING);
- new DataLoader(this);
- return true;
- }
- void dropAllOf(int... items) {
- for (int c = 0; c < 4; c++) {
- for (int r = 0; r < 7; r++) {
- boolean found = false;
- for (int i = 0; i < items.length && !found; ++i)
- found = items[i] == inventory.getItems()[c + r * 4].getID();
- if (found)
- inventory.dropItem(c, r);
- }
- }
- }
- State getState() {
- RSInterface numSelect = interfaces.get(NUMBER_SELECTION_INTERFACE);
- if (numSelect != null && numSelect.isValid()) {
- // Check if we can "take all"
- RSComponent isAll = numSelect
- .getComponent(NUMBER_SELECTION_ALL_TEXT);
- boolean isTrack = false;
- try {
- RSComponent comp = interfaces.get(
- NUMBER_SELECTION_ITEM_INTERFACE).getComponent(14);
- if (comp.getComponents() != null)
- for (RSComponent child : comp.getComponents())
- if (child.getText().toLowerCase()
- .contains(metal.def + " track")) {
- isTrack = true;
- break;
- }
- } catch (Exception e) {
- }
- if (isAll != null && isAll.isValid() && isAll.getText() != null
- && !isAll.getText().equalsIgnoreCase("all") && !isTrack) {
- RSComponent barrier = numSelect
- .getComponent(NUMBER_SELECTION_BARRIER);
- RSComponent all = numSelect.getComponent(NUMBER_SELECTION_ALL);
- if (all != null && barrier != null && barrier.isValid()
- && all.isValid()
- && all.getAbsoluteX() < barrier.getAbsoluteX())
- return State.NumberSelection_SELECT_ALL;
- else
- return State.NumberSelection_SCROLL_OUT;
- } else
- return State.NumberSelection_CLICK;
- }
- if (inventory.getCount(metal.TRACK_100) >= TrackCount)
- return State.LayTrack;
- if (metal.getSmithingID() == -1 && !inventory.contains(metal.INGOT_ID)
- && !inventory.isFull())
- return State.GetIngots;
- if (inventory.containsAll(metal.getNeededIDs())
- && metal.getSmithingID() > -1)
- return State.Smith;
- return State.Wait;
- }
- public void smith(int id, int count) {
- currentSmith = id;
- int[] invNeed = metal.getNeededIDs();
- RSObject obj = objects.getNearest(ANVIL_ID);
- if (obj != null)
- obj.doAction("Smith");
- sleep(750);
- RSInterface itemCont = interfaces.get(NUMBER_SELECTION_ITEM_INTERFACE);
- for (byte b = 0; b < 100; b++)
- if ((itemCont = interfaces.get(NUMBER_SELECTION_ITEM_INTERFACE))
- .isValid())
- break;
- else
- sleep(50);
- if (!(itemCont == null || itemCont.isValid()))
- return;
- RSComponent item = itemCont.getComponent(metal.getComponent(id));
- if (!(item == null || item.isValid()))
- return;
- item.doClick();
- // Give 1 minute to smith them all
- for (byte b1 = 0; b1 < 600; b1++) {
- if (inventory.getCount(id) >= count
- || !inventory.containsAll(invNeed) || !isRunning())
- break;
- else
- sleep(100);
- }
- currentSmith = 0;
- }
- public int loop() {
- if (metalType == 0 && !metal.def.equalsIgnoreCase("bronze"))
- setBronze();
- if (metalType == 1 && !metal.def.equalsIgnoreCase("iron"))
- setIron();
- if (metalType == 2 && !metal.def.equalsIgnoreCase("steel") && isMember)
- setSteel();
- mouse.setSpeed(random(7, 9));
- try {
- state = getState();
- switch (state) {
- case NumberSelection_SELECT_ALL:
- RSInterface numSelect = interfaces
- .get(NUMBER_SELECTION_INTERFACE);
- if (numSelect != null && numSelect.isValid()) {
- RSComponent all = numSelect
- .getComponent(NUMBER_SELECTION_ALL);
- if (all != null && all.isValid())
- all.doClick();
- }
- break;
- case NumberSelection_SCROLL_OUT:
- numSelect = interfaces.get(NUMBER_SELECTION_INTERFACE);
- if (numSelect != null && numSelect.isValid()) {
- RSComponent out = numSelect
- .getComponent(NUMBER_SELECTION_SCROLL_OUT);
- if (out != null && out.isValid())
- out.doClick();
- }
- break;
- case NumberSelection_CLICK:
- numSelect = interfaces.get(NUMBER_SELECTION_ITEM_INTERFACE);
- if (numSelect != null && numSelect.isValid()) {
- RSComponent out = numSelect.getComponent(14);
- if (out != null && out.isValid())
- out.doClick();
- }
- break;
- case GetIngots:
- RSObject obj = objects.getNearest(metal.TROUGH_ID);
- if (obj == null)
- break;
- for (byte b = 0; b < 300; b++)
- if (!obj.isOnScreen())
- if (!getMyPlayer().isMoving())
- walking.walkTileMM(obj.getLocation());
- else
- sleep(100);
- else
- break;
- obj.doAction("Take-ingots");
- sleep(750);
- break;
- case LayTrack:
- obj = objects.getNearest(TUNNEL_ID);
- if (obj == null)
- break;
- for (byte b = 0; b < 300; b++)
- if (!obj.isOnScreen())
- if (!getMyPlayer().isMoving())
- walking.walkTileMM(obj.getLocation());
- else
- sleep(100);
- else
- break;
- obj.doAction("Lay-tracks");
- while (getMyPlayer().isMoving())
- sleep(100);
- for (byte b = 0; b < 300; b++)
- if (getMyPlayer().isMoving())
- sleep(100);
- else
- break;
- sleep(750);
- break;
- case Smith:
- smith(metal.getSmithingID(), TrackCount);
- break;
- }
- } catch (Exception e) {
- }
- return 1000;
- }
- public void onRepaint(Graphics render) {
- Graphics2D g = (Graphics2D) render;
- drawMouse.onRepaint(render);
- if (Paint != null)
- Paint.render(g);
- }
- /**
- * Used to format data into more conventional formats
- *
- * @author Equinox_
- *
- */
- public static class PaintFormatter {
- /**
- * Converts the first letter of every word to uppercase, and the rest
- * lowercase.
- *
- * @param s
- * The input String
- * @return the properly formatted string
- */
- public static String properCaseAll(String s) {
- String[] words = s.split(" ");
- String result = "";
- for (int i = 0; i < words.length; i++) {
- String f = words[i].substring(0, 1).toUpperCase();
- words[i] = f + words[i].substring(1).toLowerCase();
- result += " " + words[i];
- }
- return result.substring(1);
- }
- /**
- * Capitalizes the first letter of every sentence, with every other
- * letter lowercase.
- *
- * @param s
- * The input string
- * @return The properly formatted String
- */
- public static String properCaseFirst(String s) {
- String[] sentences = new String[] { s };
- if (s.contains("."))
- sentences = s.split(".");
- String result = "";
- for (int i = 0; i < sentences.length; i++) {
- String f = sentences[i].substring(0, 1).toUpperCase();
- sentences[i] = f + sentences[i].substring(1).toLowerCase();
- result += " " + sentences[i];
- }
- return result.substring(1);
- }
- /**
- * Formats time to DD:HH:MM:SS:MSMS with days and millis optional
- *
- * @param timeMillis
- * long milliseconds of time
- * @param sep
- * String seperation character
- * @param millisInc
- * boolean include milliseconds
- * @return
- */
- public static String getFormattedTime(final long timeMillis,
- String sep, boolean millisInc) {
- long millis = timeMillis;
- final long days = millis / (24 * 1000 * 60 * 60);
- millis -= days * (24 * 1000 * 60 * 60);
- final long hours = millis / (1000 * 60 * 60);
- millis -= hours * 1000 * 60 * 60;
- final long minutes = millis / (1000 * 60);
- millis -= minutes * 1000 * 60;
- final long _seconds = millis / 1000;
- millis -= _seconds * 1000;
- String dayString = String.valueOf(days);
- String hoursString = String.valueOf(hours);
- String minutesString = String.valueOf(minutes);
- String secondsString = String.valueOf(_seconds);
- if (hours < 10) {
- hoursString = 0 + hoursString;
- }
- if (minutes < 10) {
- minutesString = 0 + minutesString;
- }
- if (_seconds < 10) {
- secondsString = 0 + secondsString;
- }
- String s = "";
- if (days > 0)
- s = dayString + sep;
- s += hoursString + sep + minutesString + sep + secondsString;
- if (millisInc)
- s += sep + String.valueOf(millis);
- return s;
- }
- /**
- * Converts the milliseconds to DD:HH:MM:SS with the days optional.
- *
- * @param timeMillis
- * the number of milliseconds
- * @return the properly formatted time as a String
- */
- public static String getFormattedTime(final long timeMillis) {
- return getFormattedTime(timeMillis, ":", false);
- }
- /**
- * Formats a specified amount into thousands, millions or billions, then
- * adds the proper suffix
- *
- * @param money
- * the amount of money
- * @return the formatted string
- */
- public static String getFormattedMoney(long money) {
- /*
- * String suffix = ""; int t = 1; if (money >= 10000) { t *= 1000;
- * suffix = "K"; } if (money / t > 10000) { t *= 1000; suffix = "M";
- * } if (money / t > 10000) { t *= 1000; suffix = "B"; } return
- * String.valueOf((money / t)) + suffix;
- */
- return new DecimalFormat("###,###,###,###,###,###").format(money);
- }
- /**
- * Converts an array of strings into one string, using commas and 'and' <br>
- * <br>
- * Examples: <br>
- * {"option1"} returns "option1"<br>
- * {"option1","option2"} returns "option1 and option2"<br>
- * {"op1","op2","op3"} returns "op1, op2, and op3"<br>
- * <br>
- *
- * @param list
- * The array of strings making up the list.
- * @return The properly formatted string
- */
- public static String formatList(String[] list) {
- String s = "";
- if (list != null)
- if (list.length == 2) {
- s = list[0] + " and " + list[1];
- } else if (list.length > 2) {
- for (int i = 0; i < list.length - 1; i++)
- s += list[i] + ", ";
- s += "and " + list[list.length - 1];
- } else if (list.length == 1)
- s = list[0];
- return s;
- }
- }
- /**
- * A class used to represent the paint interface for this script
- *
- * @author Equinox_
- *
- */
- public class PaintInterface {
- // Constant
- public final int[] SKILL_INTERFACES = { Skills.INTERFACE_ATTACK,
- Skills.INTERFACE_DEFENSE, Skills.INTERFACE_STRENGTH,
- Skills.INTERFACE_CONSTITUTION, Skills.INTERFACE_RANGE,
- Skills.INTERFACE_PRAYER, Skills.INTERFACE_MAGIC,
- Skills.INTERFACE_COOKING, Skills.INTERFACE_WOODCUTTING,
- Skills.INTERFACE_FLETCHING, Skills.INTERFACE_FISHING,
- Skills.INTERFACE_FIREMAKING, Skills.INTERFACE_CRAFTING,
- Skills.INTERFACE_SMITHING, Skills.INTERFACE_MINING,
- Skills.INTERFACE_HERBLORE, Skills.INTERFACE_AGILITY,
- Skills.INTERFACE_THIEVING, Skills.INTERFACE_SLAYER,
- Skills.INTERFACE_FARMING, Skills.INTERFACE_RUNECRAFTING,
- Skills.INTERFACE_HUNTER, Skills.INTERFACE_CONSTRUCTION,
- Skills.INTERFACE_SUMMONING, Skills.INTERFACE_DUNGEONEERING };
- // Paint data
- public long startTime = 0;
- public int startLevel;
- public int startExp;
- // Goal
- public int goalExp;
- public int goalLevel;
- // Images
- public Map<String, Image> icons = new HashMap<String, Image>();
- public void putImage(String name, Image img) {
- icons.put(name.toLowerCase(), img);
- }
- public Image getImage(String name) {
- if (icons.containsKey(name.toLowerCase()))
- return icons.get(name.toLowerCase());
- return null;
- }
- public FontRenderContext frc = new FontRenderContext(
- new AffineTransform(), false, false);
- public PaintStyle style = new PaintStyle();
- public Point mouseLoc = new Point(0, 0);
- final EquinoxTrackSmither smither;
- final Logger log;
- int SkillID = -1;
- public Menu paintMenu;
- /**
- * Constructs a PaintInterface using the provided parent and logger
- * object and no skill progress bars
- *
- * @param smither
- * the parent NatCrafter
- * @param log
- * the Logger object
- */
- public PaintInterface(EquinoxTrackSmither smither, Logger log) {
- this(smither, log, -1);
- }
- /**
- * Constructs a PaintInterface with the provided EquinoxTrackSmither
- * instance, Logger instance, and SkillID
- *
- * @param smither
- * EquinoxTrackSmither
- * @param log
- * Logger
- * @param SkillID
- * int
- */
- public PaintInterface(EquinoxTrackSmither smither, Logger log,
- int SkillID) {
- this.smither = smither;
- this.log = log;
- this.SkillID = SkillID;
- if (SkillID >= 0 && SkillID < Skills.SKILL_NAMES.length) {
- startExp = skills.getCurrentExp(SkillID);
- startTime = System.currentTimeMillis();
- startLevel = skills.getCurrentLevel(SkillID);
- setUpGoal();
- }
- init();
- }
- /**
- * Initializes paintMenu
- */
- public void init() {
- RSComponent screen = interfaces.get(548).getComponent(3);
- Point bottomRight = new Point(screen.getAbsoluteX()
- + screen.getWidth(), screen.getAbsoluteY()
- + screen.getHeight());
- paintMenu = new Menu(this, bottomRight, Anchor.DOWN_RIGHT);
- paintMenu.setFlowType(FlowType.HORIZONTAL);
- paintMenu.addMouseListener(new MouseListen() {
- void MouseAction(MouseEvent e) {
- if (e.getID() == MouseEvent.MOUSE_CLICKED)
- paintMenu.toggleShowAll();
- }
- });
- if (SkillID >= 0 && SkillID < Skills.SKILL_NAMES.length) {
- MenuProgressBar progressBar = new MenuProgressBar("nextlvl",
- paintMenu, new GetPercent() {
- int getPercent() {
- return skills.getPercentToNextLevel(SkillID);
- }
- }, "Next Level", new GetPopup() {
- MenuPopUp getPopUp() {
- long millis = Math.abs(System
- .currentTimeMillis() - startTime);
- if (millis <= 0)
- millis = 1;
- long expGained = skills.getCurrentExp(SkillID)
- - startExp;
- long expPerHour = (expGained * 3600000l)
- / millis;
- String TTL = "";
- int TTLunf = (int) ((expPerHour > 0) ? (((float) skills
- .getExpToNextLevel(SkillID))
- / ((float) expPerHour) * 3600000f) : 0);
- if (TTLunf != 0)
- TTL = PaintFormatter
- .getFormattedTime(TTLunf);
- else
- TTL = "No exp gained";
- String[] lines = new String[4];
- lines[0] = "Exp gained: "
- + PaintFormatter
- .getFormattedMoney(expGained);
- lines[1] = "Exp per Hour: "
- + PaintFormatter
- .getFormattedMoney(expPerHour);
- lines[2] = "Exp 'till level: "
- + PaintFormatter.getFormattedMoney(skills
- .getExpToNextLevel(SkillID));
- lines[3] = "Time 'till level: " + TTL;
- return new MenuPopUp(lines, "nextlevel");
- }
- });
- paintMenu.add(progressBar);
- if (goalExp > skills.getCurrentExp(SkillID)) {
- MenuProgressBar goalBar = new MenuProgressBar("goal",
- paintMenu, new GetPercent() {
- int getPercent() {
- float expTillGoalStart = goalExp - startExp;
- double expGained = skills
- .getCurrentExp(SkillID) - startExp;
- double result = expGained
- / expTillGoalStart;
- if (result > 1)
- result = 1;
- if (result < 0)
- result = 0;
- return (int) (result * 100);
- }
- }, "Goal", new GetPopup() {
- MenuPopUp getPopUp() {
- long millis = Math.abs(System
- .currentTimeMillis() - startTime);
- if (millis <= 0)
- millis = 1;
- long expGained = skills
- .getCurrentExp(SkillID) - startExp;
- long expPerHour = (expGained * 3600000l)
- / millis;
- int expTillGoal = goalExp
- - skills.getCurrentExp(SkillID);
- String TTG = "";
- int TTGunf = (int) ((expPerHour > 0) ? (((float) expTillGoal)
- / ((float) expPerHour) * 3600000f)
- : 0);
- if (TTGunf != 0)
- TTG = PaintFormatter
- .getFormattedTime(TTGunf);
- else
- TTG = "No exp gained";
- String[] lines = new String[goalLevel > 0 ? 6
- : 5];
- int off = goalLevel > 0 ? 1 : 0;
- if (goalLevel > 0)
- lines[0] = "Goal Level: " + goalLevel;
- lines[0 + off] = "Goal Exp: "
- + PaintFormatter
- .getFormattedMoney(goalExp);
- lines[1 + off] = "Exp gained: "
- + PaintFormatter
- .getFormattedMoney(expGained);
- lines[2 + off] = "Exp per Hour: "
- + PaintFormatter
- .getFormattedMoney(expPerHour);
- lines[3 + off] = "Exp 'till goal: "
- + PaintFormatter
- .getFormattedMoney(expTillGoal);
- lines[4 + off] = "Time 'till goal: " + TTG;
- return new MenuPopUp(lines, "goalbar");
- }
- });
- paintMenu.add(goalBar);
- }
- }
- // Actual buttons:
- MenuButton home = new MenuButton("home", paintMenu, "homeicon",
- new GetPopup() {
- MenuPopUp getPopUp() {
- long millis = Math.abs(System.currentTimeMillis()
- - startTime);
- if (millis <= 0)
- millis = 1;
- String[] lines = new String[5];
- lines[0] = smither.name;
- lines[1] = "By: " + smither.author;
- lines[2] = "Version " + smither.VERSION;
- lines[3] = "Runtime: "
- + PaintFormatter.getFormattedTime(millis);
- lines[4] = PaintFormatter
- .properCaseAll(smither.state.name()
- .replace("_", " "));
- if (smither.state.equals(State.Smith))
- lines[4] += " - "
- + smither.metal.getName(smither.currentSmith);
- return new MenuPopUp(lines, "home");
- }
- });
- paintMenu.add(home);
- MenuButton tools = new MenuButton("tools", paintMenu, "toolsicon",
- new GetPopup() {
- MenuPopUp getPopUp() {
- String[] lines = new String[5];
- lines[0] = "Save proggie";
- lines[1] = "Reload data";
- lines[2] = "Craft steel";
- lines[3] = "Craft iron";
- lines[4] = "Craft bronze";
- MenuPopUp popUp = new MenuPopUp(lines, "tools");
- popUp.addLineListener(new LineListener() {
- void fireEvent(MouseEvent e, String line) {
- line = line.toLowerCase().trim();
- if (e.getID() == MouseEvent.MOUSE_CLICKED) {
- if (line.contains("save proggie"))
- paintMenu.saveProggie();
- else if (line.contains("reload data"))
- new DataLoader(smither);
- else if (line.contains("craft steel")) {
- if (skills
- .getCurrentLevel(Skills.SMITHING) < STEEL_LEVEL) {
- log.info("You cannot craft steel tracks!");
- return;
- }
- if (!isMember) {
- log.info("You must be a member to craft steel tracks!");
- return;
- }
- smither.metalType = 2;
- } else if (line.contains("craft iron")) {
- if (skills
- .getCurrentLevel(Skills.SMITHING) < IRON_LEVEL) {
- log.info("You cannot craft iron tracks!");
- return;
- }
- smither.metalType = 1;
- } else if (line
- .contains("craft bronze")) {
- if (skills
- .getCurrentLevel(Skills.SMITHING) < BRONZE_LEVEL) {
- log.info("You cannot craft bronze tracks!");
- return;
- }
- smither.metalType = 0;
- }
- }
- }
- boolean hasAction(String line) {
- line = line.toLowerCase().trim();
- String[] lines = new String[5];
- lines[0] = "Save proggie";
- lines[1] = "Reload data";
- lines[2] = "Craft steel";
- lines[3] = "Craft iron";
- lines[4] = "Craft bronze";
- for (String lineC : lines)
- if (lineC.toLowerCase().contains(line))
- return true;
- return false;
- }
- });
- return popUp;
- }
- });
- paintMenu.add(tools);
- paintMenu.compile();
- }
- /**
- * Gets the current Goal for the setSkill, or sets all goal variables to
- * -1 if there is no goal
- */
- public void setUpGoal() {
- try {
- for (byte b = 0; b < 10; b++)
- if (game.getCurrentTab() != Game.TAB_STATS) {
- game.openTab(Game.TAB_STATS);
- sleep(1000);
- } else
- break;
- RSInterface stats = interfaces.get(320);
- if (stats != null && stats.isValid()) {
- RSComponent mage = stats
- .getComponent(SKILL_INTERFACES[SkillID]);
- if (mage != null && mage.isValid()
- && mage.getArea() != null) {
- mouse.move((int) mage.getArea().getCenterX(),
- (int) mage.getArea().getCenterY());
- sleep(500, 750);
- // Now we see what the popup is doing
- RSComponent pop = stats.getComponent(202);
- if (pop != null && pop.isValid()) {
- RSComponent def = pop.getComponent(5);
- if (def != null && def.isValid()) {
- if (def.getText().trim()
- .equalsIgnoreCase("Target XP:")) {
- RSComponent goalXP = pop.getComponent(6);
- if (goalXP != null && goalXP.isValid()) {
- goalExp = Integer.valueOf(goalXP
- .getText().replace(",", "")
- .trim());
- goalLevel = -1;
- }
- } else {
- // Proceed
- RSComponent currExp = pop.getComponent(4);
- RSComponent goalRemainder = pop
- .getComponent(12);
- RSComponent goalLvl = pop.getComponent(10);
- if (currExp != null && currExp.isValid()
- && goalRemainder != null
- && goalRemainder.isValid()
- && goalLvl != null
- && goalLvl.isValid()) {
- goalExp = Integer.valueOf(currExp
- .getText().replace(",", "")
- .trim())
- + Integer.valueOf(goalRemainder
- .getText()
- .replace(",", "")
- .trim());
- goalLevel = Integer.valueOf(goalLvl
- .getText().replace(",", "")
- .trim());
- }
- }
- log.info("Target level: "
- + (goalLevel > 0 ? goalLevel : "None"));
- log.info("Target exp: "
- + (goalExp > 0 ? goalExp : "None"));
- return;
- }
- }
- }
- }
- } catch (Exception e) {
- }
- goalExp = -1;
- goalLevel = -1;
- }
- /**
- * Renders the current paint to the given graphics context.
- *
- * @param g
- * as the Graphics2D object to render to
- */
- public void render(Graphics2D g) {
- paintMenu.render(g, mouseLoc);
- int mouseX = mouseLoc.x;
- int mouseY = mouseLoc.y;
- g.setColor(Color.RED);
- g.drawLine(mouseX - 5, mouseY - 5, mouseX + 5, mouseY + 5);
- g.drawLine(mouseX - 5, mouseY + 5, mouseX + 5, mouseY - 5);
- }
- /**
- * Fires a MouseEvent in the paintMenu
- *
- * @param e
- * MouseEvent
- */
- public void fireMouseEvent(MouseEvent e) {
- if (paintMenu.getBoundsFull().contains(e.getPoint()))
- paintMenu.fireMouseEvent(e);
- }
- /**
- * Represents a Menu on a PaintInterface
- *
- * @author Equinox_
- *
- */
- public class Menu {
- final int padding = 1;
- Point location;
- PaintInterface parent;
- PaintStyle style;
- List<MenuItem> components = new ArrayList<MenuItem>();
- final Anchor anchor;
- boolean showAllPopups;
- MenuPopUp visiblePopup = null;
- List<MouseListen> mouseListeners;
- LineListener defaultLineListener;
- private FlowType flowType = FlowType.HORIZONTAL;
- public Dimension bounds;
- /**
- * Constructs a Menu using the provided PaintInterface, Location,
- * Anchor, and the GameScreen as the bounds
- *
- * @param parent
- * PaintInterface
- * @param location
- * Point
- * @param anchor
- * Anchor
- */
- public Menu(PaintInterface parent, Point location, Anchor anchor) {
- this.parent = parent;
- this.style = parent.style;
- this.location = location;
- this.anchor = anchor;
- // Setup dimension
- Rectangle r = interfaces.get(548).getComponent(3).getArea();
- bounds = new Dimension(r.width, r.height);
- }
- /**
- * Sets the FlowType and recompiles the menu
- *
- * @param flowType
- * FlowType
- */
- public void setFlowType(FlowType flowType) {
- this.flowType = flowType;
- this.compile();
- }
- /**
- * Gets the FlowType
- *
- * @return FlowType
- */
- public FlowType getFlowType() {
- return flowType;
- }
- /**
- * Toggles the showAllPopups state
- * */
- public void toggleShowAll() {
- showAllPopups = !showAllPopups;
- }
- /**
- * Constructs a menu using the provided parent and location,
- * assuming Anchor.UP_LEFT
- *
- * @param parent
- * PaintInterface
- * @param location
- * Point
- */
- public Menu(PaintInterface parent, Point location) {
- this(parent, location, Anchor.UP_LEFT);
- }
- /**
- * Adds the specified menu item
- *
- * @param i
- * MenuItem
- */
- public void add(MenuItem i) {
- i.setIndex(components.size());
- components.add(i);
- compile();
- }
- /**
- * Compiles this menu
- */
- public void compile() {
- for (int i = 0; i < components.size(); i++)
- components.get(i).compile();
- }
- /**
- * Gets position x based on the menu's anchor
- *
- * @return int
- */
- public int getAbsX() {
- if (anchor.name().contains("LEFT"))
- return location.x;
- else
- return location.x - getWidth();
- }
- /**
- * Gets position y based on the menu's anchor
- *
- * @return int
- */
- public int getAbsY() {
- if (anchor.name().contains("UP"))
- return location.y;
- else
- return location.y - getHeight();
- }
- /**
- * Gets the maximum width of this menu using the menu's FlowType
- *
- * @return int
- */
- public int getWidth() {
- if (flowType.equals(FlowType.HORIZONTAL)) {
- int width = padding * components.size();
- for (MenuItem m : components)
- if (m.bounds != null)
- width += m.bounds.width;
- return width + padding;
- } else {
- int max = Integer.MIN_VALUE;
- for (MenuItem m : components)
- if (m.bounds != null)
- if (max < m.bounds.width)
- max = (int) m.bounds.width;
- return max + (2 * padding);
- }
- }
- /**
- * Gets the maximum height of this menu based of FlowType
- *
- * @return int
- */
- public int getHeight() {
- if (flowType.equals(FlowType.HORIZONTAL)) {
- int max = Integer.MIN_VALUE;
- for (MenuItem m : components)
- if (m.bounds != null)
- if (max < m.bounds.height)
- max = (int) m.bounds.height;
- return max + (2 * padding);
- } else {
- int height = padding * components.size();
- for (MenuItem m : components)
- if (m.bounds != null)
- height += m.bounds.height;
- return height + padding;
- }
- }
- /**
- * Gets the current bounds of this menu, with rounded corners if
- * specified
- *
- * @param rounded
- * boolean
- * @return RoundRectangle2D.Float
- */
- public RoundRectangle2D.Float getArea(boolean rounded) {
- float round = rounded ? style.roundCorner : 0;
- return new RoundRectangle2D.Float(getAbsX(), getAbsY(),
- getWidth(), getHeight(), round, round);
- }
- public Rectangle getBoundsFull() {
- RoundRectangle2D r1 = getArea(false);
- RoundRectangle2D r2 = visiblePopup != null ? visiblePopup.bkg
- : getArea(false);
- int x = (int) Math.min(r1.getX(), r2.getX());
- int y = (int) Math.min(r1.getY(), r2.getY());
- int width = (int) Math.max(r1.getX() + r1.getWidth(), r2.getX()
- + r2.getWidth())
- - x;
- int height = (int) Math.max(r1.getX() + r1.getHeight(),
- r2.getX() + r2.getHeight())
- - y;
- return new Rectangle(x, y, width, height);
- }
- /**
- * Renders the menu to the specified Graphics object using the
- * provided mouse location for any popups
- *
- * @param g
- * Graphics2D
- * @param mouseLoc
- * Point
- */
- public void render(Graphics2D g, Point mouseLoc) {
- g.setColor(style.bgColor);
- g.fill(getArea(true));
- MenuPopUp overPopup = null;
- for (MenuItem m : components) {
- m.render(g);
- MenuPopUp p = m.getPopUp();
- if (p != null) {
- if (m.bounds.contains(mouseLoc)
- || (m.getPopUp().name
- .equalsIgnoreCase(visiblePopup != null ? visiblePopup.name
- : "") && rectangleContainsApprox(
- visiblePopup.bkg, mouseLoc)))
- overPopup = m.getPopUp();
- }
- }
- if (showAllPopups)
- getAllPopups(true).render(g);
- else if (overPopup != null) {
- overPopup.render(g);
- }
- visiblePopup = overPopup;
- }
- /**
- * Check if the specified point is within sqrt(2) of the Rectangle
- *
- * @param r
- * RoundRectangle2D
- * @param p
- * Point
- * @return boolean contains
- */
- public boolean rectangleContainsApprox(RoundRectangle2D r, Point p) {
- for (int oX = -1; oX <= 1; oX++)
- for (int oY = -1; oY <= 1; oY++)
- if (r.contains(new Point(p.x + oX, p.y + oY)))
- return true;
- return false;
- }
- /**
- * Gets all popups in frontToBack or backToFront order and excluding
- * specified names
- *
- * @param rightToLeft
- * boolean
- * @param excludeItems
- * String...
- * @return MenuPopUp
- */
- public MenuPopUp getAllPopups(boolean backToFront,
- String... excludeItems) {
- List<String> lines = new ArrayList<String>();
- if (components.size() > 0)
- if (backToFront)
- for (int i = components.size() - 1; i >= 0; i--) {
- boolean ignored = false;
- for (String subS : excludeItems)
- if (components.get(i).name
- .equalsIgnoreCase(subS))
- ignored = true;
- if (!ignored) {
- MenuPopUp sub = components.get(i).getPopUp();
- if (sub != null)
- for (String line : sub.lines)
- if (!containsLine(lines, line))
- lines.add(line);
- }
- }
- else
- for (int i = 0; i < components.size(); i++) {
- boolean ignored = false;
- for (String subS : excludeItems)
- if (components.get(i).name
- .equalsIgnoreCase(subS))
- ignored = true;
- if (!ignored) {
- MenuPopUp sub = components.get(i).getPopUp();
- if (sub != null)
- for (String line : sub.lines)
- if (!containsLine(lines, line))
- lines.add(line);
- }
- }
- String[] linesArr = new String[lines.size()];
- for (int i = 0; i < lines.size(); i++)
- linesArr[i] = lines.get(i);
- MenuPopUp p = new MenuPopUp(linesArr, "allpopups");
- p.setParent(components.get(components.size() - 1));
- return p;
- }
- /**
- * Checks if the specified array contains the specified line, just
- * the tag, not the data if possible
- *
- * @param lines
- * List of Strings
- * @param line
- * String
- * @return boolean contains
- */
- public boolean containsLine(List<String> lines, String line) {
- for (String s : lines)
- if (s.contains(":") && line.contains(":")) {
- if (s.split(":")[0]
- .equalsIgnoreCase(line.split(":")[0]))
- return true;
- } else if (s.equalsIgnoreCase(line))
- return true;
- return false;
- }
- /**
- * Gets a proggie as a BufferedImage
- *
- * @return BufferedImage proggie
- */
- public BufferedImage getProggie() {
- String[] lines = getAllPopups(true, "tools").lines;
- Color color = style.FONT_COLOR;
- Font font = style.FONT;
- float line_height = font.getLineMetrics(lines[0], Paint.frc)
- .getHeight();
- int width = 0;
- for (String line : lines)
- if (font.getStringBounds(line, Paint.frc).getWidth() > width)
- width = (int) font.getStringBounds(line, Paint.frc)
- .getWidth();
- int height = (int) (lines.length * line_height);
- width += style.roundCorner + 2;
- height += style.roundCorner + 2;
- RoundRectangle2D.Float bkg = new RoundRectangle2D.Float(0, 0,
- width, height,
- style.roundCorner > 0 ? style.roundCorner + 1 : 0,
- style.roundCorner > 0 ? style.roundCorner + 1 : 0);
- RoundRectangle2D.Float fg = new RoundRectangle2D.Float(
- (float) bkg.getX() + 1, (float) bkg.getY() + 1,
- width - 2, height - 2, style.roundCorner,
- style.roundCorner);
- Rectangle txtBounds = new Rectangle(
- (int) (fg.getX() + (style.roundCorner / 2)),
- (int) (fg.getY() + (style.roundCorner / 2)),
- (int) (width - style.roundCorner - 2), (int) (height
- - style.roundCorner - 2));
- BufferedImage bImg = new BufferedImage(width, height,
- BufferedImage.TYPE_INT_ARGB);
- Graphics2D g = (Graphics2D) bImg.getGraphics();
- g.setColor(style.bgColor);
- g.fill(bkg);
- g.setColor(style.fgColor);
- g.fill(fg);
- g.setFont(font);
- g.setColor(color);
- for (int i = 0; i < lines.length; i++)
- g.drawString(lines[i], txtBounds.x,
- ((i + 1) * (line_height)) + txtBounds.y);
- return bImg;
- }
- /**
- * Saves a progress report to the Screenshots directory
- */
- public void saveProggie() {
- String name = smither.name
- + "_"
- + PaintFormatter.getFormattedTime(
- System.currentTimeMillis() - Paint.startTime,
- "-", true);
- final File dir = new File(
- GlobalConfiguration.Paths.getScreenshotsDirectory());
- if (dir.isDirectory() || dir.mkdirs()) {
- try {
- int ext = 0;
- while (new File(dir, name
- + (ext > 0 ? ("(" + ext + ")") : "") + ".png")
- .exists())
- ext++;
- ImageIO.write(Paint.paintMenu.getProggie(), "png",
- new File(dir, name
- + (ext > 0 ? ("(" + ext + ")") : "")));
- log.info("Saved proggie!");
- log.info(new File(dir, name
- + (ext > 0 ? ("(" + ext + ")") : "") + ".png")
- .getAbsolutePath());
- } catch (IOException e) {
- log.severe("Failed to save proggie!");
- }
- }
- }
- /**
- * Adds the specified mouse listener
- *
- * @param listen
- * MouseListen
- */
- public void addMouseListener(MouseListen listen) {
- if (mouseListeners == null)
- mouseListeners = new ArrayList<MouseListen>();
- mouseListeners.add(listen);
- }
- /**
- * Sets the default line listener
- *
- * @param listen
- * LineListener
- */
- public void setDefaultLineListener(LineListener listen) {
- defaultLineListener = listen;
- }
- /**
- * Fires the MouseEvent in all containing children and this
- *
- * @param e
- * MouseEvent
- */
- public void fireMouseEvent(MouseEvent e) {
- if (getArea(true).contains(e.getPoint()))
- if (mouseListeners != null)
- for (MouseListen m : mouseListeners)
- m.MouseAction(e);
- for (MenuItem m : components)
- if (m.bounds != null)
- if (m.bounds.contains(e.getPoint()))
- m.fireMouseEvent(e);
- if (visiblePopup != null)
- visiblePopup.fireMouseEvent(e);
- }
- }
- /**
- * Represents a class containing the method for getting the popup
- *
- * @author Equinox_
- *
- */
- public abstract class GetPopup {
- abstract MenuPopUp getPopUp();
- }
- /**
- * Represents a modified MouseListener used in MenuEvents
- *
- * @author Equinox_
- *
- */
- public abstract class MouseListen {
- abstract void MouseAction(MouseEvent e);
- }
- /**
- * An abstract class representing MenuItems
- *
- * @author Equinox_
- *
- */
- public abstract class MenuItem {
- final GetPopup getPopup;
- Menu parent;
- RoundRectangle2D.Float bounds;
- int index;
- final int width;
- final int height;
- List<MouseListen> mouseListeners;
- final String name;
- /**
- * Constructs a MenuItem with the given name, parent, getPopup
- * method, width and height
- *
- * @param name
- * String
- * @param parent
- * Menu
- * @param getPopup
- * GetPopup
- * @param width
- * int
- * @param height
- * int
- */
- public MenuItem(String name, Menu parent, GetPopup getPopup,
- int width, int height) {
- this.name = name;
- this.height = height;
- this.width = width;
- this.getPopup = getPopup;
- this.parent = parent;
- this.bounds = new RoundRectangle2D.Float(0, 0, width, height,
- parent.style.roundCorner, parent.style.roundCorner);
- }
- /**
- * Constructs a MenuItem with the given name, parent, width and
- * height
- *
- * @param name
- * String
- * @param parent
- * Menu
- * @param width
- * int
- * @param height
- * int
- */
- public MenuItem(String name, Menu parent, int width, int height) {
- this(name, parent, null, width, height);
- }
- /**
- * sets this item's index
- *
- * @param idx
- * int
- */
- public void setIndex(int idx) {
- this.index = idx;
- }
- /**
- * renders this item to the given Graphics2D
- *
- * @param g
- * Graphics2D
- */
- abstract void render(Graphics2D g);
- /**
- * Compiles this item
- */
- public void compile() {
- this.bounds = new RoundRectangle2D.Float(0, 0, width, height,
- parent.style.roundCorner, parent.style.roundCorner);
- bounds.x = getX();
- bounds.y = getY();
- }
- /**
- * Gets the offset from upper left of parent depending on parent's
- * FlowType
- *
- * @return int offsetY
- */
- public int getOffX() {
- if (parent.getFlowType().equals(FlowType.HORIZONTAL)) {
- int x = 1;
- for (int i = 0; i < index; i++)
- x += 1 + parent.components.get(i).bounds.width;
- return x;
- }
- if (parent.getWidth() > bounds.width)
- return (int) (parent.getWidth() / 2 - bounds.width / 2);
- return 1;
- }
- /**
- * Gets the offset from upper left of parent depending on parent's
- * FlowType
- *
- * @return int offsetX
- */
- public int getOffY() {
- if (parent.getFlowType().equals(FlowType.VERTICAL)) {
- int y = 1;
- for (int i = 0; i < index; i++)
- y += 1 + parent.components.get(i).bounds.height;
- return y;
- }
- return 1;
- }
- /**
- * Gets the absolute x
- *
- * @return int absolute x
- */
- public int getX() {
- int x = getOffX() + parent.getAbsX();
- return x;
- }
- /**
- * Gets the absolute y
- *
- * @return int absolute y
- */
- public int getY() {
- int y = getOffY() + parent.getAbsY();
- return y;
- }
- /**
- * Gets this item's popup
- *
- * @return MenuPopUp
- */
- public MenuPopUp getPopUp() {
- if (getPopup != null) {
- MenuPopUp m = getPopup.getPopUp();
- m.setParent(this);
- if (parent.defaultLineListener != null)
- m.addLineListener(parent.defaultLineListener);
- return m;
- }
- return null;
- }
- /**
- * Adds a mouselistener
- *
- * @param listen
- * MouseListen
- */
- public void addMouseListener(MouseListen listen) {
- if (mouseListeners == null)
- mouseListeners = new ArrayList<MouseListen>();
- mouseListeners.add(listen);
- }
- /**
- * Sends the mouse event to all of this item's listeners
- *
- * @param e
- * MouseEvent
- */
- public void fireMouseEvent(MouseEvent e) {
- if (mouseListeners != null && mouseListeners.size() > 0)
- for (MouseListen m : mouseListeners)
- m.MouseAction(e);
- }
- }
- /**
- * A class representing a mouse over button on the parent menu
- *
- * @author Equinox_
- *
- */
- public class MenuButton extends MenuItem {
- final static int width = 30;
- final static int height = 30;
- String imageIcon;
- /**
- * Constructs a button using the specified name, parent, imageIcon,
- * and getPopup method.
- *
- * @param name
- * String
- * @param parent
- * Menu
- * @param imageIcon
- * String
- * @param getPopUp
- * GetPopup
- */
- public MenuButton(String name, Menu parent, String imageIcon,
- GetPopup getPopUp) {
- super(name, parent, getPopUp, width, height);
- this.imageIcon = imageIcon.toLowerCase();
- }
- /**
- * Constructs a button using the specified name, parent, and
- * imageIcon
- *
- * @param name
- * String
- * @param parent
- * Menu
- * @param imageIcon
- * String
- */
- public MenuButton(String name, Menu parent, String imageIcon) {
- this(name, parent, imageIcon, null);
- }
- /**
- * Constructs a button using the specified name and parent
- *
- * @param name
- * String
- * @param parent
- * Menu
- */
- public MenuButton(String name, Menu parent) {
- this(name, parent, "", null);
- }
- /**
- * Renders this MenuButton to the given Graphics2D context
- */
- public void render(Graphics2D g) {
- // Compiling
- if (parent.parent.icons.containsKey(imageIcon))
- if (parent.parent.icons.get(imageIcon) != null)
- if (!parent.parent.icons.containsKey("Button-"
- + imageIcon)) {
- // Scale the image
- int width = (int) (bounds.width - (parent.style.roundCorner));
- int height = (int) (bounds.height - (parent.style.roundCorner));
- int hints = Image.SCALE_FAST;
- parent.parent.icons.put(
- "Button-" + imageIcon,
- parent.parent.icons.get(imageIcon)
- .getScaledInstance(width, height,
- hints));
- }
- g.setColor(parent.style.fgColor);
- g.fill(bounds);
- if (parent.parent.icons.containsKey("Button-" + imageIcon))
- if (parent.parent.icons.get("Button-" + imageIcon) != null)
- g.drawImage(
- parent.parent.icons.get("Button-" + imageIcon),
- (int) (bounds.x + (parent.style.roundCorner / 2)),
- (int) (bounds.y + (parent.style.roundCorner / 2)),
- null);
- }
- }
- /**
- * A class representing a progress bar in a menu
- *
- * @author Equinox_
- *
- */
- public class MenuProgressBar extends MenuItem {
- final static int width = 100;
- final static int height = 30;
- final GetPercent getPercent;
- RoundRectangle2D.Float percent;
- Point textLoc;
- String prefix;
- /**
- * Constructs a MenuProgress bar using the specified name, parent,
- * getPercent method, text prefix, and getPopup method
- *
- * @param name
- * String
- * @param parent
- * Menu
- * @param getPercent
- * GetPercent
- * @param prefix
- * String
- * @param getPopup
- * GetPopup
- */
- public MenuProgressBar(String name, Menu parent,
- GetPercent getPercent, String prefix, GetPopup getPopup) {
- super(name, parent, getPopup, width, height);
- this.getPercent = getPercent;
- this.prefix = prefix;
- }
- /**
- * Renders this progress bar to the given Graphics context
- */
- public void render(Graphics2D g) {
- percent.width = getPercent.getPercent();
- g.setColor(parent.style.progressBarBG);
- g.fill(bounds);
- g.setColor(parent.style.progressBarFG);
- g.fill(percent);
- String perct = String.valueOf(getPercent.getPercent());
- if (perct.length() < 2)
- perct = "0" + perct;
- g.setFont(style.FONT);
- g.setColor(style.FONT_COLOR);
- g.drawString(prefix + ": " + perct + "%", textLoc.x, textLoc.y);
- }
- /**
- * Compiles this progress bar
- */
- public void compile() {
- super.compile();
- percent = new RoundRectangle2D.Float(bounds.x, bounds.y,
- getPercent.getPercent(), bounds.height,
- bounds.arcwidth, bounds.archeight);
- textLoc = new Point();
- textLoc.x = (int) bounds.getCenterX();
- textLoc.y = (int) bounds.getCenterY();
- textLoc.y += parent.style.FONT.getLineMetrics(prefix + ": 00%",
- parent.parent.frc).getHeight() / 2f;
- textLoc.x -= parent.style.FONT.getStringBounds(
- prefix + ": 00%", parent.parent.frc).getWidth() / 2f;
- }
- }
- /**
- * Class representing the method for getting the current percentile for
- * a progress bar
- *
- * @author Equinox_
- *
- */
- public abstract class GetPercent {
- abstract int getPercent();
- }
- /**
- * Class representing the LineListeners in popups
- *
- * @author Equinox_
- *
- */
- public abstract class LineListener {
- abstract void fireEvent(MouseEvent e, String line);
- abstract boolean hasAction(String line);
- }
- /**
- * A class representing the popups in the paint interface
- *
- * @author Equinox_
- *
- */
- public class MenuPopUp {
- private Font font;
- private Color color;
- private PaintStyle style;
- private RoundRectangle2D bkg;
- public RoundRectangle2D fg;
- public Rectangle txtBounds;
- public MenuItem parent;
- public final String name;
- public String[] lines;
- int width;
- int height;
- List<LineListener> lineListeners;
- /**
- * Creates a popup using the provided array of strings making up the
- * lines and name
- *
- * @param lines
- * as String[]
- * @param name
- * as String
- */
- public MenuPopUp(String[] lines, String name) {
- this.name = name;
- this.lines = lines;
- }
- /**
- * Set this popup's parent and compiles locations
- *
- * @param parent
- * MenuItem
- */
- public void setParent(MenuItem parent) {
- this.parent = parent;
- this.style = parent.parent.style;
- // Calc dimensions
- color = name.equals("tools") ? style.TOOLS_FONT_COLOR
- : style.FONT_COLOR;
- font = name.equals("tools") ? style.TOOLS_FONT : style.FONT;
- float line_height = font.getLineMetrics(lines[0], Paint.frc)
- .getHeight();
- width = 0;
- for (String line : lines)
- if (font.getStringBounds(line, Paint.frc).getWidth() > width)
- width = (int) font.getStringBounds(line, Paint.frc)
- .getWidth();
- height = (int) (lines.length * line_height);
- width += style.roundCorner + 2;
- height += style.roundCorner + 2;
- int halfPadding = (parent.parent.padding / 2);
- if (halfPadding < 1 && halfPadding != 0)
- halfPadding = 1;
- int x = 0;
- int y = 0;
- if (parent.parent.getFlowType().equals(FlowType.HORIZONTAL)) {
- if (parent.bounds.y - height - parent.parent.padding > 0) {
- // Above
- x = (int) (parent.bounds.x - width
- + parent.bounds.width + halfPadding);
- if (x < 0)
- x = (int) (parent.bounds.x - halfPadding);
- y = (int) (parent.parent.getAbsY() - height);
- } else {
- // Below
- x = (int) (parent.bounds.x - halfPadding);
- if (x + width > parent.parent.bounds.width)
- x = (int) (parent.bounds.x - halfPadding - width);
- y = (int) (parent.parent.getAbsY() + parent.parent
- .getHeight());
- }
- } else {
- // Vertical
- if (parent.bounds.x - width - parent.parent.padding > 0) {
- // Left
- x = (int) (parent.parent.getAbsX() - width);
- if (parent.bounds.y - height + halfPadding
- + parent.bounds.height > 0)
- y = (int) (parent.bounds.y - height + halfPadding + parent.bounds.height);
- else
- y = (int) (parent.bounds.y - halfPadding);
- } else {
- // Right
- x = (int) (parent.parent.getAbsX() + parent.parent
- .getWidth());
- if (parent.bounds.y - height + halfPadding
- + parent.bounds.height > 0)
- y = (int) (parent.bounds.y - height + halfPadding + parent.bounds.height);
- else
- y = (int) (parent.bounds.y - halfPadding);
- }
- }
- bkg = new RoundRectangle2D.Float(x, y, width, height,
- style.roundCorner > 0 ? style.roundCorner + 1 : 0,
- style.roundCorner > 0 ? style.roundCorner + 1 : 0);
- fg = new RoundRectangle2D.Float((float) bkg.getX() + 1,
- (float) bkg.getY() + 1, width - 2, height - 2,
- style.roundCorner, style.roundCorner);
- txtBounds = new Rectangle(
- (int) (fg.getX() + (style.roundCorner / 2)),
- (int) (fg.getY() + (style.roundCorner / 2)),
- (int) (width - style.roundCorner - 2), (int) (height
- - style.roundCorner - 2));
- }
- /**
- * Paints this popup to using the provided Graphics2D in relation to
- * it's parent PaintInterface
- *
- * @param g
- * as Graphics2D
- */
- public void render(Graphics2D g) {
- g.setColor(style.bgColor);
- g.fill(bkg);
- g.setColor(style.fgColor);
- g.fill(fg);
- float line_height = (txtBounds.height - 4) / lines.length;
- g.setFont(font);
- g.setColor(color);
- for (int i = 0; i < lines.length; i++) {
- if (new Rectangle(txtBounds.x,
- (int) ((i * (line_height)) + txtBounds.y),
- txtBounds.width, (int) line_height)
- .contains(parent.parent.parent.mouseLoc)
- && hasAction(lines[i])) {
- g.setColor(style.highlightColor);
- g.fill(new Rectangle(txtBounds.x,
- (int) ((i * (line_height)) + 2 + txtBounds.y),
- txtBounds.width, (int) line_height));
- }
- g.setColor(color);
- g.drawString(lines[i], txtBounds.x,
- ((i + 1) * (line_height)) + txtBounds.y);
- }
- }
- boolean hasAction(String line) {
- if (lineListeners != null)
- for (LineListener l : lineListeners)
- if (l.hasAction(line))
- return true;
- return false;
- }
- /**
- * Adds the specified line listener
- *
- * @param l
- * LineListener
- */
- public void addLineListener(LineListener l) {
- if (lineListeners == null)
- lineListeners = new ArrayList<LineListener>();
- this.lineListeners.add(l);
- }
- /**
- * fires the specified mouse event using this popup's line listeners
- *
- * @param e
- * MouseEvent
- */
- public void fireMouseEvent(MouseEvent e) {
- if (!bkg.contains(e.getPoint()) || lineListeners == null
- || lineListeners.size() == 0)
- return;
- int line_height = (txtBounds.height - 4) / lines.length;
- for (int i = 0; i < lines.length; i++) {
- Rectangle stringRect = new Rectangle(txtBounds.x,
- (i * (line_height)) + txtBounds.y, txtBounds.width,
- line_height);
- if (stringRect.contains(e.getPoint())) {
- for (LineListener lineListener : lineListeners)
- lineListener.fireEvent(e, lines[i]);
- return;
- }
- }
- }
- }
- }
- /**
- * Used for specifing the way a menu is layed out.
- *
- * @author Equinox_
- *
- */
- private enum FlowType {
- VERTICAL, HORIZONTAL
- }
- /**
- * Used for anchoring the menu bar
- *
- * @author Equinox_
- *
- */
- private enum Anchor {
- UP_LEFT, UP_RIGHT, DOWN_RIGHT, DOWN_LEFT
- }
- /**
- * Used to style the paint
- *
- * @author Equinox_
- *
- */
- public static class PaintStyle {
- public final Color FONT_COLOR = Color.BLACK;
- public final Font FONT = new Font("Arial", 1, 10)
- .deriveFont(Font.PLAIN);
- public final Font TOOLS_FONT = new Font("Arial", 1, 14)
- .deriveFont(Font.PLAIN);
- public final Color TOOLS_FONT_COLOR = Color.BLACK;
- public final float roundCorner = 10f;
- public final Color progressBarFG = new Color(0f, 1f, 0f, 1f);
- public final Color progressBarBG = new Color(.3f, .3f, .3f, 1f);
- public final Color bgColor = new Color(0f, 0f, 0f, 0.75f);
- final Color fgColor = new Color(.3f, .3f, .3f, 1f);
- final Color highlightColor = new Color(1f, 0f, 0f, 0.1f);
- }
- /**
- * Modified Copy of DrawMouse
- */
- public class ModifiedDrawMouse {
- private MouseTrailing mouseTrailing = new MouseTrailing();
- private ArrayList<Mouses> Click = new ArrayList<Mouses>();
- private long clickLifeTime = 1500;
- public void onRepaint(final Graphics render) {
- if (mouse != null) {
- Point Location = mouse.getLocation();
- if (mouse.isPressed())
- if (Click.size() <= 5)
- Click.add(new Mouses(System.currentTimeMillis(),
- Location));
- mouseTrailing.draw((Graphics2D) render, Color.WHITE);
- render.setColor(Color.GREEN);
- render.drawLine(Location.x - 5, Location.y, Location.x + 5,
- Location.y);
- render.drawLine(Location.x, Location.y - 5, Location.x,
- Location.y + 5);
- if (Click.size() > 1)
- for (Mouses click : Click) {
- if (mouse.isPressed())
- if (Click.size() <= 10)
- Click.add(new Mouses(
- System.currentTimeMillis(), Location));
- float i = ((float) (click.getTime() + clickLifeTime - System
- .currentTimeMillis()) / (float) clickLifeTime) + 0.25f;
- if (i > 0) {
- render.setColor(new Color(1, 0, 0, i > 1 ? 1 : i));
- render.drawLine(click.getLocation().x - 5,
- click.getLocation().y,
- click.getLocation().x + 5,
- click.getLocation().y);
- render.drawLine(click.getLocation().x,
- click.getLocation().y - 5,
- click.getLocation().x,
- click.getLocation().y + 5);
- } else if (System.currentTimeMillis() - click.getTime() >= 1000) {
- Click.remove(click);
- }
- }
- }
- }
- /**
- * @author Baheer (Doout).
- */
- public class MouseTrailing {
- private int LifeTime = 1500;
- private ArrayList<Mouses> Info = new ArrayList<Mouses>();
- private void getLocation() {
- Point m = mouse.getLocation();
- if (Add(m)) {
- Info.add(new Mouses(System.currentTimeMillis(), m));
- }
- }
- public boolean Add(Point p) {
- for (Mouses Trailing : Info)
- if (Trailing.getLocation().equals(p))
- return false;
- return true;
- }
- public void draw(Graphics2D g, Color c) {
- getLocation();
- if (Info.size() > 1)
- for (int t = 1; t < Info.size(); t++) {
- Mouses Trailing1 = Info.get(t);
- Mouses Trailing2 = Info.get(t - 1);
- float i = (float) (Trailing1.getTime() + LifeTime - System
- .currentTimeMillis()) / (float) LifeTime;
- if (i > 0) {
- g.setColor(new Color(c.getRed() / 255f, c
- .getGreen() / 255f, c.getBlue() / 255f, (c
- .getAlpha() / 255f) * i));
- g.drawLine(Trailing1.getLocation().x,
- Trailing1.getLocation().y,
- Trailing2.getLocation().x,
- Trailing2.getLocation().y);
- }
- }
- if (Info.size() > 1) {
- if (System.currentTimeMillis() > Info.get(0).getTime()
- + LifeTime) {
- Info.remove(0);
- }
- }
- }
- }
- private class Mouses {
- private long Time;
- private Point Location;
- public Mouses(long time, Point loc) {
- Time = time;
- Location = loc;
- }
- public long getTime() {
- return Time;
- }
- public Point getLocation() {
- return Location;
- }
- }
- }
- public void mouseDragged(MouseEvent arg0) {
- Paint.mouseLoc = arg0.getPoint();
- }
- public void mouseMoved(MouseEvent arg0) {
- Paint.mouseLoc = arg0.getPoint();
- }
- public void mouseClicked(MouseEvent arg0) {
- Paint.fireMouseEvent(arg0);
- }
- public void mouseEntered(MouseEvent arg0) {
- }
- public void mouseExited(MouseEvent arg0) {
- }
- public void mousePressed(MouseEvent arg0) {
- }
- public void mouseReleased(MouseEvent arg0) {
- }
- /**
- * Loads script data as the actual script runs. Useful for images that are
- * non-essential.
- *
- * @author Equinox_
- *
- */
- public static class DataLoader extends Thread {
- EquinoxTrackSmither smither;
- /**
- * Creates a dataloader using the specified EquinoxTrackSmither instance
- *
- * @param smither
- */
- public DataLoader(EquinoxTrackSmither smither) {
- super("Data Loader");
- this.smither = smither;
- this.start();
- }
- /**
- * Gets the icon for a specified image in the runescape online database.
- *
- * @param id
- * as int
- * @return Image
- * @throws MalformedURLException
- * @throws IOException
- */
- public Image getItemImage(int id) throws MalformedURLException,
- IOException {
- return ImageIO.read(new URL(
- "http://services.runescape.com/m=itemdb_rs/3290_obj_sprite.gif?id="
- + id));
- }
- public void run() {
- try {
- if (JOptionPane.NO_OPTION == JOptionPane.showConfirmDialog(
- null, "Would you like to load some images?",
- "Equinox_ Data Loader", JOptionPane.YES_NO_OPTION))
- throw new Exception();
- long before = System.currentTimeMillis();
- smither.Paint.putImage(
- "homeicon",
- ImageIO.read(new URL(smither.baseURL
- + "/images/houseIcon.png")));
- smither.log("Loaded home image in "
- + PaintFormatter.getFormattedTime(System
- .currentTimeMillis() - before));
- before = System.currentTimeMillis();
- smither.Paint.putImage(
- "toolsicon",
- ImageIO.read(new URL(smither.baseURL
- + "/images/tools.png")));
- smither.log("Loaded tools icon in "
- + PaintFormatter.getFormattedTime(System
- .currentTimeMillis() - before));
- } catch (Exception e) {
- smither.log("Failed to load images");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment