Equinox

EquinioxTrackSmither.java

Mar 10th, 2011
1,190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 65.79 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Dimension;
  3. import java.awt.Font;
  4. import java.awt.Graphics;
  5. import java.awt.Graphics2D;
  6. import java.awt.Image;
  7. import java.awt.Point;
  8. import java.awt.Rectangle;
  9. import java.awt.event.MouseEvent;
  10. import java.awt.event.MouseListener;
  11. import java.awt.event.MouseMotionListener;
  12. import java.awt.font.FontRenderContext;
  13. import java.awt.geom.AffineTransform;
  14. import java.awt.geom.RoundRectangle2D;
  15. import java.awt.image.BufferedImage;
  16. import java.io.File;
  17. import java.io.IOException;
  18. import java.net.MalformedURLException;
  19. import java.net.URL;
  20. import java.text.DecimalFormat;
  21. import java.util.ArrayList;
  22. import java.util.HashMap;
  23. import java.util.List;
  24. import java.util.Map;
  25. import java.util.logging.Logger;
  26.  
  27. import javax.imageio.ImageIO;
  28. import javax.swing.JOptionPane;
  29.  
  30. import org.rsbot.event.listeners.PaintListener;
  31. import org.rsbot.script.Script;
  32. import org.rsbot.script.ScriptManifest;
  33. import org.rsbot.script.methods.Equipment;
  34. import org.rsbot.script.methods.Game;
  35. import org.rsbot.script.methods.Skills;
  36. import org.rsbot.script.wrappers.RSComponent;
  37. import org.rsbot.script.wrappers.RSInterface;
  38. import org.rsbot.script.wrappers.RSObject;
  39. import org.rsbot.util.GlobalConfiguration;
  40.  
  41. @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.")
  42. public class EquinoxTrackSmither extends Script implements PaintListener,
  43.         MouseMotionListener, MouseListener {
  44.     PaintInterface Paint;
  45.     ModifiedDrawMouse drawMouse = new ModifiedDrawMouse();
  46.  
  47.     final String baseURL = "http://equinox-scripts.web44.net/scripts/natcrafter";
  48.     final double VERSION = getClass().getAnnotation(ScriptManifest.class)
  49.             .version();
  50.     final String author = PaintFormatter.formatList(getClass().getAnnotation(
  51.             ScriptManifest.class).authors());
  52.     final String name = PaintFormatter.properCaseAll(getClass().getAnnotation(
  53.             ScriptManifest.class).name());
  54.  
  55.     // Levels
  56.     static final int STEEL_LEVEL = 60;
  57.     static final int IRON_LEVEL = 35;
  58.     static final int BRONZE_LEVEL = 12;
  59.  
  60.     static final int HAMMER_ID = 2347;
  61.     static final int GOLD_HAMMER_ID = 20084;
  62.  
  63.     // Objects
  64.     static final int[] TUNNEL_ID = { 24826, 24825 };
  65.     static final int ANVIL_ID = 24820;
  66.  
  67.     // Interfaces
  68.     static final int NUMBER_SELECTION_INTERFACE = 916;
  69.     static final int NUMBER_SELECTION_ALL = 8;
  70.     static final int NUMBER_SELECTION_ALL_TEXT = 17;
  71.     static final int NUMBER_SELECTION_SCROLL_OUT = 22;
  72.     static final int NUMBER_SELECTION_BARRIER = 9;
  73.     static final int NUMBER_SELECTION_ITEM_INTERFACE = 905;
  74.     static final int NUMBER_SELECTION_INGOT_SELECT = 14;
  75.  
  76.     public Metal metal = new Metal();
  77.     public int TrackCount = 13;
  78.     public State state = State.Wait;
  79.     public int currentSmith = 0;
  80.     public int metalType = -1;
  81.     public boolean isMember = false;
  82.  
  83.     class Metal {
  84.         String def = "";
  85.  
  86.         int INGOT_ID;
  87.         int TROUGH_ID;
  88.         // Track
  89.         int TRACK_20; // Same as rails
  90.         int TRACK_40;
  91.         int TRACK_60;
  92.         int TRACK_80;
  93.         int TRACK_100;
  94.         // Track pieces
  95.         int Rails;
  96.         int Base_Plate;
  97.         int Spikes;
  98.         int Joint;
  99.         int Tie;
  100.  
  101.         int[] getAll() {
  102.             return new int[] { INGOT_ID, TRACK_20, TRACK_40, TRACK_60,
  103.                     TRACK_80, TRACK_100, Rails, Base_Plate, Spikes, Joint, Tie };
  104.         }
  105.  
  106.         String getName(int id) {
  107.             if (id == Rails)
  108.                 return "Rails";
  109.             if (id == Base_Plate)
  110.                 return "Base Plate";
  111.             if (id == Spikes)
  112.                 return "Spikes";
  113.             if (id == Joint)
  114.                 return "Joint";
  115.             if (id == Tie)
  116.                 return "Tie";
  117.             if (id == TRACK_40)
  118.                 return "Track 40%";
  119.             if (id == TRACK_60)
  120.                 return "Track 60%";
  121.             if (id == TRACK_80)
  122.                 return "Track 80%";
  123.             if (id == TRACK_100)
  124.                 return "Track 100%";
  125.             return "";
  126.         }
  127.  
  128.         int getUnfTrackCount() {
  129.             return inventory.getCount(TRACK_20, TRACK_40, TRACK_60, TRACK_80);
  130.         }
  131.  
  132.         int getComponent(int id) {
  133.             if (id == TRACK_40 || id == TRACK_60 || id == TRACK_80
  134.                     || id == TRACK_100)
  135.                 return 14;
  136.             if (id >= Rails && id <= Tie)
  137.                 return id - Rails + 14;
  138.             return -1;
  139.         }
  140.  
  141.         int getSmithingID() {
  142.             if (inventory.getCount(INGOT_ID) > 0) {
  143.                 if (inventory.getCount(TRACK_40, TRACK_60, TRACK_80) == 0) {
  144.                     if (inventory.getCount(TRACK_20) < TrackCount)
  145.                         return Rails;
  146.                     if (inventory.getCount(Base_Plate) < TrackCount)
  147.                         return Base_Plate;
  148.                 }
  149.                 if (inventory.contains(TRACK_40))
  150.                     if (inventory.getCount(Spikes) < TrackCount)
  151.                         return Spikes;
  152.                 if (inventory.contains(TRACK_60))
  153.                     if (inventory.getCount(Joint) < TrackCount)
  154.                         return Joint;
  155.                 if (inventory.contains(TRACK_80))
  156.                     if (inventory.getCount(Tie) < TrackCount)
  157.                         return Tie;
  158.             }
  159.  
  160.             // Proceed to Track-Material upgrade:
  161.             for (int i = 0; i < 4; i++) {
  162.                 if (inventory.getCount(i == 0 ? TRACK_20 : (TRACK_40 + i - 1)) >= TrackCount
  163.                         && inventory.getCount(Base_Plate + i) >= TrackCount)
  164.                     return TRACK_40 + i;
  165.             }
  166.             return -1;
  167.         }
  168.  
  169.         int[] getNeededIDs() {
  170.             if (inventory.getCount(INGOT_ID) > 0) {
  171.                 if (inventory.getCount(TRACK_40, TRACK_60, TRACK_80) == 0) {
  172.                     if (inventory.getCount(TRACK_20) < TrackCount)
  173.                         return new int[] { INGOT_ID };
  174.                     if (inventory.getCount(Base_Plate) < TrackCount)
  175.                         return new int[] { INGOT_ID };
  176.                 }
  177.                 if (inventory.contains(TRACK_40))
  178.                     if (inventory.getCount(Spikes) < TrackCount)
  179.                         return new int[] { INGOT_ID };
  180.                 if (inventory.contains(TRACK_60))
  181.                     if (inventory.getCount(Joint) < TrackCount)
  182.                         return new int[] { INGOT_ID };
  183.                 if (inventory.contains(TRACK_80))
  184.                     if (inventory.getCount(Tie) < TrackCount)
  185.                         return new int[] { INGOT_ID };
  186.             }
  187.  
  188.             // Proceed to Track-Material upgrade:
  189.             for (int i = 0; i < 4; i++) {
  190.                 if (inventory.getCount(i == 0 ? TRACK_20 : (TRACK_40 + i - 1)) >= TrackCount
  191.                         && inventory.getCount(Base_Plate + i) >= TrackCount)
  192.                     return new int[] { i == 0 ? TRACK_20 : (TRACK_40 + i - 1),
  193.                             Base_Plate + i };
  194.             }
  195.             return new int[] { -1 };
  196.         }
  197.  
  198.     }
  199.  
  200.     Metal getSteel() {
  201.         Metal metal = new Metal();
  202.         metal.def = "steel";
  203.         metal.INGOT_ID = 20504;
  204.         metal.TROUGH_ID = 24823;
  205.         // Track
  206.         metal.TRACK_20 = 20520;
  207.         metal.TRACK_40 = 20529;
  208.         metal.TRACK_60 = 20530;
  209.         metal.TRACK_80 = 20531;
  210.         metal.TRACK_100 = 20532;
  211.         // Track pieces
  212.         metal.Rails = 20520;
  213.         metal.Base_Plate = 20521;
  214.         metal.Spikes = 20522;
  215.         metal.Joint = 20523;
  216.         metal.Tie = 20524;
  217.         return metal;
  218.     }
  219.  
  220.     Metal getIron() {
  221.         Metal metal = new Metal();
  222.         metal.def = "iron";
  223.         metal.INGOT_ID = 20503;
  224.         metal.TROUGH_ID = 24822;
  225.         // Track
  226.         metal.TRACK_20 = 20515;
  227.         metal.TRACK_40 = 20525;
  228.         metal.TRACK_60 = 20526;
  229.         metal.TRACK_80 = 20527;
  230.         metal.TRACK_100 = 20528;
  231.         // Track pieces
  232.         metal.Rails = 20515;
  233.         metal.Base_Plate = 20516;
  234.         metal.Spikes = 20517;
  235.         metal.Joint = 20518;
  236.         metal.Tie = 20519;
  237.         return metal;
  238.     }
  239.  
  240.     Metal getBronze() {
  241.         Metal metal = new Metal();
  242.         metal.def = "bronze";
  243.         metal.INGOT_ID = 20502;
  244.         metal.TROUGH_ID = 24821;
  245.         // Track
  246.         metal.TRACK_20 = 20506;
  247.         metal.TRACK_40 = 20511;
  248.         metal.TRACK_60 = 20512;
  249.         metal.TRACK_80 = 20513;
  250.         metal.TRACK_100 = 20514;
  251.         // Track pieces
  252.         metal.Rails = 20506;
  253.         metal.Base_Plate = 20507;
  254.         metal.Spikes = 20508;
  255.         metal.Joint = 20509;
  256.         metal.Tie = 20510;
  257.         return metal;
  258.     }
  259.  
  260.     void setSteel() {
  261.         metal = getSteel();
  262.         dropAllOf(getBronze().getAll());
  263.         dropAllOf(getIron().getAll());
  264.         log.info("Set track type to steel!");
  265.     }
  266.  
  267.     void setIron() {
  268.         metal = getIron();
  269.         dropAllOf(getBronze().getAll());
  270.         dropAllOf(getSteel().getAll());
  271.         log.info("Set track type to iron!");
  272.     }
  273.  
  274.     void setBronze() {
  275.         metal = getBronze();
  276.         dropAllOf(getSteel().getAll());
  277.         dropAllOf(getIron().getAll());
  278.         log.info("Set track type to bronze!");
  279.     }
  280.  
  281.     enum State {
  282.         Wait, NumberSelection_SELECT_ALL, NumberSelection_SCROLL_OUT, NumberSelection_CLICK, GetIngots, Smith, LayTrack
  283.     }
  284.  
  285.     public boolean onStart() {
  286.         if (!(inventory.contains(HAMMER_ID) || equipment
  287.                 .containsOneOf(GOLD_HAMMER_ID))) {
  288.             log.severe("You must start with a hammer in your inventory!");
  289.             return false;
  290.         }
  291.         int sLevel = skills.getCurrentLevel(Skills.SMITHING);
  292.         if ((isMember = (JOptionPane.YES_OPTION == JOptionPane
  293.                 .showConfirmDialog(null, "Are you a member?",
  294.                         "Equinox_ Track Smither", JOptionPane.YES_NO_OPTION)))
  295.                 && sLevel >= STEEL_LEVEL) {
  296.             setSteel();
  297.         } else if (sLevel >= IRON_LEVEL) {
  298.             setIron();
  299.         } else if (sLevel >= BRONZE_LEVEL) {
  300.             setBronze();
  301.         } else {
  302.             // No high enough level
  303.             log.severe("You don't have the required smithing level!  (Level 12)");
  304.             return false;
  305.         }
  306.  
  307.         int cE = inventory.getCountExcept(metal.getAll());
  308.         if (cE == 0)
  309.             TrackCount = 14;
  310.  
  311.         if (cE > 2) {
  312.             // Extra items
  313.             log.severe("You have move than 2 non-required items in your inventory.  Please dispose of them.");
  314.             return false;
  315.         }
  316.         if (cE == 1) {
  317.             log.info("Attempting to fill an inventory slot.");
  318.             if (equipment.getCount() > 0)
  319.                 for (int i = 0; i < Equipment.ITEM_SLOTS; i++)
  320.                     if (equipment.getItem(i) != null
  321.                             && equipment.getItem(i).getID() != GOLD_HAMMER_ID)
  322.                         if (equipment.getItem(i).doClick(true))
  323.                             break;
  324.         }
  325.         Paint = new PaintInterface(this, log, Skills.SMITHING);
  326.         new DataLoader(this);
  327.         return true;
  328.     }
  329.  
  330.     void dropAllOf(int... items) {
  331.         for (int c = 0; c < 4; c++) {
  332.             for (int r = 0; r < 7; r++) {
  333.                 boolean found = false;
  334.                 for (int i = 0; i < items.length && !found; ++i)
  335.                     found = items[i] == inventory.getItems()[c + r * 4].getID();
  336.                 if (found)
  337.                     inventory.dropItem(c, r);
  338.             }
  339.         }
  340.     }
  341.  
  342.     State getState() {
  343.         RSInterface numSelect = interfaces.get(NUMBER_SELECTION_INTERFACE);
  344.         if (numSelect != null && numSelect.isValid()) {
  345.             // Check if we can "take all"
  346.             RSComponent isAll = numSelect
  347.                     .getComponent(NUMBER_SELECTION_ALL_TEXT);
  348.             boolean isTrack = false;
  349.             try {
  350.                 RSComponent comp = interfaces.get(
  351.                         NUMBER_SELECTION_ITEM_INTERFACE).getComponent(14);
  352.                 if (comp.getComponents() != null)
  353.                     for (RSComponent child : comp.getComponents())
  354.                         if (child.getText().toLowerCase()
  355.                                 .contains(metal.def + " track")) {
  356.                             isTrack = true;
  357.                             break;
  358.                         }
  359.             } catch (Exception e) {
  360.             }
  361.  
  362.             if (isAll != null && isAll.isValid() && isAll.getText() != null
  363.                     && !isAll.getText().equalsIgnoreCase("all") && !isTrack) {
  364.                 RSComponent barrier = numSelect
  365.                         .getComponent(NUMBER_SELECTION_BARRIER);
  366.                 RSComponent all = numSelect.getComponent(NUMBER_SELECTION_ALL);
  367.                 if (all != null && barrier != null && barrier.isValid()
  368.                         && all.isValid()
  369.                         && all.getAbsoluteX() < barrier.getAbsoluteX())
  370.                     return State.NumberSelection_SELECT_ALL;
  371.                 else
  372.                     return State.NumberSelection_SCROLL_OUT;
  373.             } else
  374.                 return State.NumberSelection_CLICK;
  375.         }
  376.         if (inventory.getCount(metal.TRACK_100) >= TrackCount)
  377.             return State.LayTrack;
  378.         if (metal.getSmithingID() == -1 && !inventory.contains(metal.INGOT_ID)
  379.                 && !inventory.isFull())
  380.             return State.GetIngots;
  381.         if (inventory.containsAll(metal.getNeededIDs())
  382.                 && metal.getSmithingID() > -1)
  383.             return State.Smith;
  384.         return State.Wait;
  385.     }
  386.  
  387.     public void smith(int id, int count) {
  388.         currentSmith = id;
  389.         int[] invNeed = metal.getNeededIDs();
  390.         RSObject obj = objects.getNearest(ANVIL_ID);
  391.         if (obj != null)
  392.             obj.doAction("Smith");
  393.         sleep(750);
  394.         RSInterface itemCont = interfaces.get(NUMBER_SELECTION_ITEM_INTERFACE);
  395.         for (byte b = 0; b < 100; b++)
  396.             if ((itemCont = interfaces.get(NUMBER_SELECTION_ITEM_INTERFACE))
  397.                     .isValid())
  398.                 break;
  399.             else
  400.                 sleep(50);
  401.         if (!(itemCont == null || itemCont.isValid()))
  402.             return;
  403.         RSComponent item = itemCont.getComponent(metal.getComponent(id));
  404.         if (!(item == null || item.isValid()))
  405.             return;
  406.         item.doClick();
  407.         // Give 1 minute to smith them all
  408.         for (byte b1 = 0; b1 < 600; b1++) {
  409.             if (inventory.getCount(id) >= count
  410.                     || !inventory.containsAll(invNeed) || !isRunning())
  411.                 break;
  412.             else
  413.                 sleep(100);
  414.         }
  415.         currentSmith = 0;
  416.     }
  417.  
  418.     public int loop() {
  419.         if (metalType == 0 && !metal.def.equalsIgnoreCase("bronze"))
  420.             setBronze();
  421.         if (metalType == 1 && !metal.def.equalsIgnoreCase("iron"))
  422.             setIron();
  423.         if (metalType == 2 && !metal.def.equalsIgnoreCase("steel") && isMember)
  424.             setSteel();
  425.  
  426.         mouse.setSpeed(random(7, 9));
  427.         try {
  428.             state = getState();
  429.             switch (state) {
  430.             case NumberSelection_SELECT_ALL:
  431.                 RSInterface numSelect = interfaces
  432.                         .get(NUMBER_SELECTION_INTERFACE);
  433.                 if (numSelect != null && numSelect.isValid()) {
  434.                     RSComponent all = numSelect
  435.                             .getComponent(NUMBER_SELECTION_ALL);
  436.                     if (all != null && all.isValid())
  437.                         all.doClick();
  438.                 }
  439.                 break;
  440.             case NumberSelection_SCROLL_OUT:
  441.                 numSelect = interfaces.get(NUMBER_SELECTION_INTERFACE);
  442.                 if (numSelect != null && numSelect.isValid()) {
  443.                     RSComponent out = numSelect
  444.                             .getComponent(NUMBER_SELECTION_SCROLL_OUT);
  445.                     if (out != null && out.isValid())
  446.                         out.doClick();
  447.                 }
  448.                 break;
  449.             case NumberSelection_CLICK:
  450.                 numSelect = interfaces.get(NUMBER_SELECTION_ITEM_INTERFACE);
  451.                 if (numSelect != null && numSelect.isValid()) {
  452.                     RSComponent out = numSelect.getComponent(14);
  453.                     if (out != null && out.isValid())
  454.                         out.doClick();
  455.                 }
  456.                 break;
  457.             case GetIngots:
  458.                 RSObject obj = objects.getNearest(metal.TROUGH_ID);
  459.                 if (obj == null)
  460.                     break;
  461.                 for (byte b = 0; b < 300; b++)
  462.                     if (!obj.isOnScreen())
  463.                         if (!getMyPlayer().isMoving())
  464.                             walking.walkTileMM(obj.getLocation());
  465.                         else
  466.                             sleep(100);
  467.                     else
  468.                         break;
  469.                 obj.doAction("Take-ingots");
  470.                 sleep(750);
  471.                 break;
  472.             case LayTrack:
  473.                 obj = objects.getNearest(TUNNEL_ID);
  474.                 if (obj == null)
  475.                     break;
  476.                 for (byte b = 0; b < 300; b++)
  477.                     if (!obj.isOnScreen())
  478.                         if (!getMyPlayer().isMoving())
  479.                             walking.walkTileMM(obj.getLocation());
  480.                         else
  481.                             sleep(100);
  482.                     else
  483.                         break;
  484.                 obj.doAction("Lay-tracks");
  485.                 while (getMyPlayer().isMoving())
  486.                     sleep(100);
  487.                 for (byte b = 0; b < 300; b++)
  488.                     if (getMyPlayer().isMoving())
  489.                         sleep(100);
  490.                     else
  491.                         break;
  492.  
  493.                 sleep(750);
  494.                 break;
  495.             case Smith:
  496.                 smith(metal.getSmithingID(), TrackCount);
  497.                 break;
  498.             }
  499.         } catch (Exception e) {
  500.         }
  501.         return 1000;
  502.     }
  503.  
  504.     public void onRepaint(Graphics render) {
  505.         Graphics2D g = (Graphics2D) render;
  506.         drawMouse.onRepaint(render);
  507.         if (Paint != null)
  508.             Paint.render(g);
  509.     }
  510.  
  511.     /**
  512.      * Used to format data into more conventional formats
  513.      *
  514.      * @author Equinox_
  515.      *
  516.      */
  517.     public static class PaintFormatter {
  518.         /**
  519.          * Converts the first letter of every word to uppercase, and the rest
  520.          * lowercase.
  521.          *
  522.          * @param s
  523.          *            The input String
  524.          * @return the properly formatted string
  525.          */
  526.         public static String properCaseAll(String s) {
  527.             String[] words = s.split(" ");
  528.             String result = "";
  529.             for (int i = 0; i < words.length; i++) {
  530.                 String f = words[i].substring(0, 1).toUpperCase();
  531.                 words[i] = f + words[i].substring(1).toLowerCase();
  532.                 result += " " + words[i];
  533.             }
  534.             return result.substring(1);
  535.         }
  536.  
  537.         /**
  538.          * Capitalizes the first letter of every sentence, with every other
  539.          * letter lowercase.
  540.          *
  541.          * @param s
  542.          *            The input string
  543.          * @return The properly formatted String
  544.          */
  545.         public static String properCaseFirst(String s) {
  546.             String[] sentences = new String[] { s };
  547.             if (s.contains("."))
  548.                 sentences = s.split(".");
  549.             String result = "";
  550.             for (int i = 0; i < sentences.length; i++) {
  551.                 String f = sentences[i].substring(0, 1).toUpperCase();
  552.                 sentences[i] = f + sentences[i].substring(1).toLowerCase();
  553.                 result += " " + sentences[i];
  554.             }
  555.             return result.substring(1);
  556.         }
  557.  
  558.         /**
  559.          * Formats time to DD:HH:MM:SS:MSMS with days and millis optional
  560.          *
  561.          * @param timeMillis
  562.          *            long milliseconds of time
  563.          * @param sep
  564.          *            String seperation character
  565.          * @param millisInc
  566.          *            boolean include milliseconds
  567.          * @return
  568.          */
  569.         public static String getFormattedTime(final long timeMillis,
  570.                 String sep, boolean millisInc) {
  571.             long millis = timeMillis;
  572.             final long days = millis / (24 * 1000 * 60 * 60);
  573.             millis -= days * (24 * 1000 * 60 * 60);
  574.             final long hours = millis / (1000 * 60 * 60);
  575.             millis -= hours * 1000 * 60 * 60;
  576.             final long minutes = millis / (1000 * 60);
  577.             millis -= minutes * 1000 * 60;
  578.             final long _seconds = millis / 1000;
  579.             millis -= _seconds * 1000;
  580.             String dayString = String.valueOf(days);
  581.             String hoursString = String.valueOf(hours);
  582.             String minutesString = String.valueOf(minutes);
  583.             String secondsString = String.valueOf(_seconds);
  584.             if (hours < 10) {
  585.                 hoursString = 0 + hoursString;
  586.             }
  587.             if (minutes < 10) {
  588.                 minutesString = 0 + minutesString;
  589.             }
  590.             if (_seconds < 10) {
  591.                 secondsString = 0 + secondsString;
  592.             }
  593.             String s = "";
  594.             if (days > 0)
  595.                 s = dayString + sep;
  596.             s += hoursString + sep + minutesString + sep + secondsString;
  597.             if (millisInc)
  598.                 s += sep + String.valueOf(millis);
  599.             return s;
  600.         }
  601.  
  602.         /**
  603.          * Converts the milliseconds to DD:HH:MM:SS with the days optional.
  604.          *
  605.          * @param timeMillis
  606.          *            the number of milliseconds
  607.          * @return the properly formatted time as a String
  608.          */
  609.         public static String getFormattedTime(final long timeMillis) {
  610.             return getFormattedTime(timeMillis, ":", false);
  611.         }
  612.  
  613.         /**
  614.          * Formats a specified amount into thousands, millions or billions, then
  615.          * adds the proper suffix
  616.          *
  617.          * @param money
  618.          *            the amount of money
  619.          * @return the formatted string
  620.          */
  621.         public static String getFormattedMoney(long money) {
  622.             /*
  623.              * String suffix = ""; int t = 1; if (money >= 10000) { t *= 1000;
  624.              * suffix = "K"; } if (money / t > 10000) { t *= 1000; suffix = "M";
  625.              * } if (money / t > 10000) { t *= 1000; suffix = "B"; } return
  626.              * String.valueOf((money / t)) + suffix;
  627.              */
  628.             return new DecimalFormat("###,###,###,###,###,###").format(money);
  629.         }
  630.  
  631.         /**
  632.          * Converts an array of strings into one string, using commas and 'and' <br>
  633.          * <br>
  634.          * Examples: <br>
  635.          * {"option1"} returns "option1"<br>
  636.          * {"option1","option2"} returns "option1 and option2"<br>
  637.          * {"op1","op2","op3"} returns "op1, op2, and op3"<br>
  638.          * <br>
  639.          *
  640.          * @param list
  641.          *            The array of strings making up the list.
  642.          * @return The properly formatted string
  643.          */
  644.         public static String formatList(String[] list) {
  645.             String s = "";
  646.             if (list != null)
  647.                 if (list.length == 2) {
  648.                     s = list[0] + " and " + list[1];
  649.                 } else if (list.length > 2) {
  650.                     for (int i = 0; i < list.length - 1; i++)
  651.                         s += list[i] + ", ";
  652.                     s += "and " + list[list.length - 1];
  653.                 } else if (list.length == 1)
  654.                     s = list[0];
  655.             return s;
  656.         }
  657.     }
  658.  
  659.     /**
  660.      * A class used to represent the paint interface for this script
  661.      *
  662.      * @author Equinox_
  663.      *
  664.      */
  665.     public class PaintInterface {
  666.         // Constant
  667.         public final int[] SKILL_INTERFACES = { Skills.INTERFACE_ATTACK,
  668.                 Skills.INTERFACE_DEFENSE, Skills.INTERFACE_STRENGTH,
  669.                 Skills.INTERFACE_CONSTITUTION, Skills.INTERFACE_RANGE,
  670.                 Skills.INTERFACE_PRAYER, Skills.INTERFACE_MAGIC,
  671.                 Skills.INTERFACE_COOKING, Skills.INTERFACE_WOODCUTTING,
  672.                 Skills.INTERFACE_FLETCHING, Skills.INTERFACE_FISHING,
  673.                 Skills.INTERFACE_FIREMAKING, Skills.INTERFACE_CRAFTING,
  674.                 Skills.INTERFACE_SMITHING, Skills.INTERFACE_MINING,
  675.                 Skills.INTERFACE_HERBLORE, Skills.INTERFACE_AGILITY,
  676.                 Skills.INTERFACE_THIEVING, Skills.INTERFACE_SLAYER,
  677.                 Skills.INTERFACE_FARMING, Skills.INTERFACE_RUNECRAFTING,
  678.                 Skills.INTERFACE_HUNTER, Skills.INTERFACE_CONSTRUCTION,
  679.                 Skills.INTERFACE_SUMMONING, Skills.INTERFACE_DUNGEONEERING };
  680.  
  681.         // Paint data
  682.         public long startTime = 0;
  683.         public int startLevel;
  684.         public int startExp;
  685.  
  686.         // Goal
  687.         public int goalExp;
  688.         public int goalLevel;
  689.  
  690.         // Images
  691.         public Map<String, Image> icons = new HashMap<String, Image>();
  692.  
  693.         public void putImage(String name, Image img) {
  694.             icons.put(name.toLowerCase(), img);
  695.         }
  696.  
  697.         public Image getImage(String name) {
  698.             if (icons.containsKey(name.toLowerCase()))
  699.                 return icons.get(name.toLowerCase());
  700.             return null;
  701.         }
  702.  
  703.         public FontRenderContext frc = new FontRenderContext(
  704.                 new AffineTransform(), false, false);
  705.  
  706.         public PaintStyle style = new PaintStyle();
  707.         public Point mouseLoc = new Point(0, 0);
  708.  
  709.         final EquinoxTrackSmither smither;
  710.         final Logger log;
  711.         int SkillID = -1;
  712.         public Menu paintMenu;
  713.  
  714.         /**
  715.          * Constructs a PaintInterface using the provided parent and logger
  716.          * object and no skill progress bars
  717.          *
  718.          * @param smither
  719.          *            the parent NatCrafter
  720.          * @param log
  721.          *            the Logger object
  722.          */
  723.         public PaintInterface(EquinoxTrackSmither smither, Logger log) {
  724.             this(smither, log, -1);
  725.         }
  726.  
  727.         /**
  728.          * Constructs a PaintInterface with the provided EquinoxTrackSmither
  729.          * instance, Logger instance, and SkillID
  730.          *
  731.          * @param smither
  732.          *            EquinoxTrackSmither
  733.          * @param log
  734.          *            Logger
  735.          * @param SkillID
  736.          *            int
  737.          */
  738.         public PaintInterface(EquinoxTrackSmither smither, Logger log,
  739.                 int SkillID) {
  740.             this.smither = smither;
  741.             this.log = log;
  742.             this.SkillID = SkillID;
  743.             if (SkillID >= 0 && SkillID < Skills.SKILL_NAMES.length) {
  744.                 startExp = skills.getCurrentExp(SkillID);
  745.                 startTime = System.currentTimeMillis();
  746.                 startLevel = skills.getCurrentLevel(SkillID);
  747.                 setUpGoal();
  748.             }
  749.             init();
  750.         }
  751.  
  752.         /**
  753.          * Initializes paintMenu
  754.          */
  755.         public void init() {
  756.             RSComponent screen = interfaces.get(548).getComponent(3);
  757.             Point bottomRight = new Point(screen.getAbsoluteX()
  758.                     + screen.getWidth(), screen.getAbsoluteY()
  759.                     + screen.getHeight());
  760.             paintMenu = new Menu(this, bottomRight, Anchor.DOWN_RIGHT);
  761.             paintMenu.setFlowType(FlowType.HORIZONTAL);
  762.             paintMenu.addMouseListener(new MouseListen() {
  763.                 void MouseAction(MouseEvent e) {
  764.                     if (e.getID() == MouseEvent.MOUSE_CLICKED)
  765.                         paintMenu.toggleShowAll();
  766.                 }
  767.             });
  768.             if (SkillID >= 0 && SkillID < Skills.SKILL_NAMES.length) {
  769.                 MenuProgressBar progressBar = new MenuProgressBar("nextlvl",
  770.                         paintMenu, new GetPercent() {
  771.                             int getPercent() {
  772.                                 return skills.getPercentToNextLevel(SkillID);
  773.                             }
  774.                         }, "Next Level", new GetPopup() {
  775.                             MenuPopUp getPopUp() {
  776.                                 long millis = Math.abs(System
  777.                                         .currentTimeMillis() - startTime);
  778.                                 if (millis <= 0)
  779.                                     millis = 1;
  780.                                 long expGained = skills.getCurrentExp(SkillID)
  781.                                         - startExp;
  782.                                 long expPerHour = (expGained * 3600000l)
  783.                                         / millis;
  784.                                 String TTL = "";
  785.                                 int TTLunf = (int) ((expPerHour > 0) ? (((float) skills
  786.                                         .getExpToNextLevel(SkillID))
  787.                                         / ((float) expPerHour) * 3600000f) : 0);
  788.                                 if (TTLunf != 0)
  789.                                     TTL = PaintFormatter
  790.                                             .getFormattedTime(TTLunf);
  791.                                 else
  792.                                     TTL = "No exp gained";
  793.                                 String[] lines = new String[4];
  794.                                 lines[0] = "Exp gained: "
  795.                                         + PaintFormatter
  796.                                                 .getFormattedMoney(expGained);
  797.                                 lines[1] = "Exp per Hour: "
  798.                                         + PaintFormatter
  799.                                                 .getFormattedMoney(expPerHour);
  800.                                 lines[2] = "Exp 'till level: "
  801.                                         + PaintFormatter.getFormattedMoney(skills
  802.                                                 .getExpToNextLevel(SkillID));
  803.                                 lines[3] = "Time 'till level: " + TTL;
  804.                                 return new MenuPopUp(lines, "nextlevel");
  805.                             }
  806.  
  807.                         });
  808.                 paintMenu.add(progressBar);
  809.  
  810.                 if (goalExp > skills.getCurrentExp(SkillID)) {
  811.                     MenuProgressBar goalBar = new MenuProgressBar("goal",
  812.                             paintMenu, new GetPercent() {
  813.                                 int getPercent() {
  814.                                     float expTillGoalStart = goalExp - startExp;
  815.                                     double expGained = skills
  816.                                             .getCurrentExp(SkillID) - startExp;
  817.                                     double result = expGained
  818.                                             / expTillGoalStart;
  819.                                     if (result > 1)
  820.                                         result = 1;
  821.                                     if (result < 0)
  822.                                         result = 0;
  823.                                     return (int) (result * 100);
  824.                                 }
  825.                             }, "Goal", new GetPopup() {
  826.                                 MenuPopUp getPopUp() {
  827.                                     long millis = Math.abs(System
  828.                                             .currentTimeMillis() - startTime);
  829.                                     if (millis <= 0)
  830.                                         millis = 1;
  831.                                     long expGained = skills
  832.                                             .getCurrentExp(SkillID) - startExp;
  833.                                     long expPerHour = (expGained * 3600000l)
  834.                                             / millis;
  835.                                     int expTillGoal = goalExp
  836.                                             - skills.getCurrentExp(SkillID);
  837.                                     String TTG = "";
  838.                                     int TTGunf = (int) ((expPerHour > 0) ? (((float) expTillGoal)
  839.                                             / ((float) expPerHour) * 3600000f)
  840.                                             : 0);
  841.                                     if (TTGunf != 0)
  842.                                         TTG = PaintFormatter
  843.                                                 .getFormattedTime(TTGunf);
  844.                                     else
  845.                                         TTG = "No exp gained";
  846.                                     String[] lines = new String[goalLevel > 0 ? 6
  847.                                             : 5];
  848.                                     int off = goalLevel > 0 ? 1 : 0;
  849.                                     if (goalLevel > 0)
  850.                                         lines[0] = "Goal Level: " + goalLevel;
  851.                                     lines[0 + off] = "Goal Exp: "
  852.                                             + PaintFormatter
  853.                                                     .getFormattedMoney(goalExp);
  854.                                     lines[1 + off] = "Exp gained: "
  855.                                             + PaintFormatter
  856.                                                     .getFormattedMoney(expGained);
  857.                                     lines[2 + off] = "Exp per Hour: "
  858.                                             + PaintFormatter
  859.                                                     .getFormattedMoney(expPerHour);
  860.                                     lines[3 + off] = "Exp 'till goal: "
  861.                                             + PaintFormatter
  862.                                                     .getFormattedMoney(expTillGoal);
  863.                                     lines[4 + off] = "Time 'till goal: " + TTG;
  864.                                     return new MenuPopUp(lines, "goalbar");
  865.                                 }
  866.  
  867.                             });
  868.                     paintMenu.add(goalBar);
  869.                 }
  870.             }
  871.  
  872.             // Actual buttons:
  873.             MenuButton home = new MenuButton("home", paintMenu, "homeicon",
  874.                     new GetPopup() {
  875.                         MenuPopUp getPopUp() {
  876.                             long millis = Math.abs(System.currentTimeMillis()
  877.                                     - startTime);
  878.                             if (millis <= 0)
  879.                                 millis = 1;
  880.                             String[] lines = new String[5];
  881.                             lines[0] = smither.name;
  882.                             lines[1] = "By: " + smither.author;
  883.                             lines[2] = "Version " + smither.VERSION;
  884.                             lines[3] = "Runtime: "
  885.                                     + PaintFormatter.getFormattedTime(millis);
  886.                             lines[4] = PaintFormatter
  887.                                     .properCaseAll(smither.state.name()
  888.                                             .replace("_", " "));
  889.                             if (smither.state.equals(State.Smith))
  890.                                 lines[4] += " - "
  891.                                         + smither.metal.getName(smither.currentSmith);
  892.                             return new MenuPopUp(lines, "home");
  893.                         }
  894.                     });
  895.             paintMenu.add(home);
  896.  
  897.             MenuButton tools = new MenuButton("tools", paintMenu, "toolsicon",
  898.                     new GetPopup() {
  899.                         MenuPopUp getPopUp() {
  900.                             String[] lines = new String[5];
  901.                             lines[0] = "Save proggie";
  902.                             lines[1] = "Reload data";
  903.                             lines[2] = "Craft steel";
  904.                             lines[3] = "Craft iron";
  905.                             lines[4] = "Craft bronze";
  906.                             MenuPopUp popUp = new MenuPopUp(lines, "tools");
  907.                             popUp.addLineListener(new LineListener() {
  908.                                 void fireEvent(MouseEvent e, String line) {
  909.                                     line = line.toLowerCase().trim();
  910.                                     if (e.getID() == MouseEvent.MOUSE_CLICKED) {
  911.                                         if (line.contains("save proggie"))
  912.                                             paintMenu.saveProggie();
  913.                                         else if (line.contains("reload data"))
  914.                                             new DataLoader(smither);
  915.                                         else if (line.contains("craft steel")) {
  916.                                             if (skills
  917.                                                     .getCurrentLevel(Skills.SMITHING) < STEEL_LEVEL) {
  918.                                                 log.info("You cannot craft steel tracks!");
  919.                                                 return;
  920.                                             }
  921.                                             if (!isMember) {
  922.                                                 log.info("You must be a member to craft steel tracks!");
  923.                                                 return;
  924.                                             }
  925.                                             smither.metalType = 2;
  926.                                         } else if (line.contains("craft iron")) {
  927.                                             if (skills
  928.                                                     .getCurrentLevel(Skills.SMITHING) < IRON_LEVEL) {
  929.                                                 log.info("You cannot craft iron tracks!");
  930.                                                 return;
  931.                                             }
  932.                                             smither.metalType = 1;
  933.                                         } else if (line
  934.                                                 .contains("craft bronze")) {
  935.                                             if (skills
  936.                                                     .getCurrentLevel(Skills.SMITHING) < BRONZE_LEVEL) {
  937.                                                 log.info("You cannot craft bronze tracks!");
  938.                                                 return;
  939.                                             }
  940.                                             smither.metalType = 0;
  941.                                         }
  942.  
  943.                                     }
  944.                                 }
  945.  
  946.                                 boolean hasAction(String line) {
  947.                                     line = line.toLowerCase().trim();
  948.                                     String[] lines = new String[5];
  949.                                     lines[0] = "Save proggie";
  950.                                     lines[1] = "Reload data";
  951.                                     lines[2] = "Craft steel";
  952.                                     lines[3] = "Craft iron";
  953.                                     lines[4] = "Craft bronze";
  954.                                     for (String lineC : lines)
  955.                                         if (lineC.toLowerCase().contains(line))
  956.                                             return true;
  957.                                     return false;
  958.                                 }
  959.                             });
  960.                             return popUp;
  961.                         }
  962.                     });
  963.             paintMenu.add(tools);
  964.  
  965.             paintMenu.compile();
  966.         }
  967.  
  968.         /**
  969.          * Gets the current Goal for the setSkill, or sets all goal variables to
  970.          * -1 if there is no goal
  971.          */
  972.         public void setUpGoal() {
  973.             try {
  974.                 for (byte b = 0; b < 10; b++)
  975.                     if (game.getCurrentTab() != Game.TAB_STATS) {
  976.                         game.openTab(Game.TAB_STATS);
  977.                         sleep(1000);
  978.                     } else
  979.                         break;
  980.  
  981.                 RSInterface stats = interfaces.get(320);
  982.                 if (stats != null && stats.isValid()) {
  983.                     RSComponent mage = stats
  984.                             .getComponent(SKILL_INTERFACES[SkillID]);
  985.                     if (mage != null && mage.isValid()
  986.                             && mage.getArea() != null) {
  987.                         mouse.move((int) mage.getArea().getCenterX(),
  988.                                 (int) mage.getArea().getCenterY());
  989.                         sleep(500, 750);
  990.                         // Now we see what the popup is doing
  991.                         RSComponent pop = stats.getComponent(202);
  992.                         if (pop != null && pop.isValid()) {
  993.                             RSComponent def = pop.getComponent(5);
  994.                             if (def != null && def.isValid()) {
  995.                                 if (def.getText().trim()
  996.                                         .equalsIgnoreCase("Target XP:")) {
  997.                                     RSComponent goalXP = pop.getComponent(6);
  998.                                     if (goalXP != null && goalXP.isValid()) {
  999.                                         goalExp = Integer.valueOf(goalXP
  1000.                                                 .getText().replace(",", "")
  1001.                                                 .trim());
  1002.                                         goalLevel = -1;
  1003.                                     }
  1004.                                 } else {
  1005.                                     // Proceed
  1006.                                     RSComponent currExp = pop.getComponent(4);
  1007.                                     RSComponent goalRemainder = pop
  1008.                                             .getComponent(12);
  1009.                                     RSComponent goalLvl = pop.getComponent(10);
  1010.                                     if (currExp != null && currExp.isValid()
  1011.                                             && goalRemainder != null
  1012.                                             && goalRemainder.isValid()
  1013.                                             && goalLvl != null
  1014.                                             && goalLvl.isValid()) {
  1015.                                         goalExp = Integer.valueOf(currExp
  1016.                                                 .getText().replace(",", "")
  1017.                                                 .trim())
  1018.                                                 + Integer.valueOf(goalRemainder
  1019.                                                         .getText()
  1020.                                                         .replace(",", "")
  1021.                                                         .trim());
  1022.                                         goalLevel = Integer.valueOf(goalLvl
  1023.                                                 .getText().replace(",", "")
  1024.                                                 .trim());
  1025.                                     }
  1026.                                 }
  1027.                                 log.info("Target level: "
  1028.                                         + (goalLevel > 0 ? goalLevel : "None"));
  1029.                                 log.info("Target exp: "
  1030.                                         + (goalExp > 0 ? goalExp : "None"));
  1031.                                 return;
  1032.                             }
  1033.                         }
  1034.                     }
  1035.                 }
  1036.             } catch (Exception e) {
  1037.             }
  1038.             goalExp = -1;
  1039.             goalLevel = -1;
  1040.         }
  1041.  
  1042.         /**
  1043.          * Renders the current paint to the given graphics context.
  1044.          *
  1045.          * @param g
  1046.          *            as the Graphics2D object to render to
  1047.          */
  1048.         public void render(Graphics2D g) {
  1049.             paintMenu.render(g, mouseLoc);
  1050.             int mouseX = mouseLoc.x;
  1051.             int mouseY = mouseLoc.y;
  1052.             g.setColor(Color.RED);
  1053.             g.drawLine(mouseX - 5, mouseY - 5, mouseX + 5, mouseY + 5);
  1054.             g.drawLine(mouseX - 5, mouseY + 5, mouseX + 5, mouseY - 5);
  1055.         }
  1056.  
  1057.         /**
  1058.          * Fires a MouseEvent in the paintMenu
  1059.          *
  1060.          * @param e
  1061.          *            MouseEvent
  1062.          */
  1063.         public void fireMouseEvent(MouseEvent e) {
  1064.             if (paintMenu.getBoundsFull().contains(e.getPoint()))
  1065.                 paintMenu.fireMouseEvent(e);
  1066.         }
  1067.  
  1068.         /**
  1069.          * Represents a Menu on a PaintInterface
  1070.          *
  1071.          * @author Equinox_
  1072.          *
  1073.          */
  1074.         public class Menu {
  1075.             final int padding = 1;
  1076.             Point location;
  1077.             PaintInterface parent;
  1078.             PaintStyle style;
  1079.             List<MenuItem> components = new ArrayList<MenuItem>();
  1080.             final Anchor anchor;
  1081.             boolean showAllPopups;
  1082.             MenuPopUp visiblePopup = null;
  1083.             List<MouseListen> mouseListeners;
  1084.             LineListener defaultLineListener;
  1085.             private FlowType flowType = FlowType.HORIZONTAL;
  1086.             public Dimension bounds;
  1087.  
  1088.             /**
  1089.              * Constructs a Menu using the provided PaintInterface, Location,
  1090.              * Anchor, and the GameScreen as the bounds
  1091.              *
  1092.              * @param parent
  1093.              *            PaintInterface
  1094.              * @param location
  1095.              *            Point
  1096.              * @param anchor
  1097.              *            Anchor
  1098.              */
  1099.             public Menu(PaintInterface parent, Point location, Anchor anchor) {
  1100.                 this.parent = parent;
  1101.                 this.style = parent.style;
  1102.                 this.location = location;
  1103.                 this.anchor = anchor;
  1104.  
  1105.                 // Setup dimension
  1106.                 Rectangle r = interfaces.get(548).getComponent(3).getArea();
  1107.                 bounds = new Dimension(r.width, r.height);
  1108.             }
  1109.  
  1110.             /**
  1111.              * Sets the FlowType and recompiles the menu
  1112.              *
  1113.              * @param flowType
  1114.              *            FlowType
  1115.              */
  1116.             public void setFlowType(FlowType flowType) {
  1117.                 this.flowType = flowType;
  1118.                 this.compile();
  1119.             }
  1120.  
  1121.             /**
  1122.              * Gets the FlowType
  1123.              *
  1124.              * @return FlowType
  1125.              */
  1126.             public FlowType getFlowType() {
  1127.                 return flowType;
  1128.             }
  1129.  
  1130.             /**
  1131.              * Toggles the showAllPopups state
  1132.              * */
  1133.             public void toggleShowAll() {
  1134.                 showAllPopups = !showAllPopups;
  1135.             }
  1136.  
  1137.             /**
  1138.              * Constructs a menu using the provided parent and location,
  1139.              * assuming Anchor.UP_LEFT
  1140.              *
  1141.              * @param parent
  1142.              *            PaintInterface
  1143.              * @param location
  1144.              *            Point
  1145.              */
  1146.             public Menu(PaintInterface parent, Point location) {
  1147.                 this(parent, location, Anchor.UP_LEFT);
  1148.             }
  1149.  
  1150.             /**
  1151.              * Adds the specified menu item
  1152.              *
  1153.              * @param i
  1154.              *            MenuItem
  1155.              */
  1156.             public void add(MenuItem i) {
  1157.                 i.setIndex(components.size());
  1158.                 components.add(i);
  1159.                 compile();
  1160.             }
  1161.  
  1162.             /**
  1163.              * Compiles this menu
  1164.              */
  1165.             public void compile() {
  1166.                 for (int i = 0; i < components.size(); i++)
  1167.                     components.get(i).compile();
  1168.             }
  1169.  
  1170.             /**
  1171.              * Gets position x based on the menu's anchor
  1172.              *
  1173.              * @return int
  1174.              */
  1175.             public int getAbsX() {
  1176.                 if (anchor.name().contains("LEFT"))
  1177.                     return location.x;
  1178.                 else
  1179.                     return location.x - getWidth();
  1180.             }
  1181.  
  1182.             /**
  1183.              * Gets position y based on the menu's anchor
  1184.              *
  1185.              * @return int
  1186.              */
  1187.             public int getAbsY() {
  1188.                 if (anchor.name().contains("UP"))
  1189.                     return location.y;
  1190.                 else
  1191.                     return location.y - getHeight();
  1192.             }
  1193.  
  1194.             /**
  1195.              * Gets the maximum width of this menu using the menu's FlowType
  1196.              *
  1197.              * @return int
  1198.              */
  1199.             public int getWidth() {
  1200.                 if (flowType.equals(FlowType.HORIZONTAL)) {
  1201.                     int width = padding * components.size();
  1202.                     for (MenuItem m : components)
  1203.                         if (m.bounds != null)
  1204.                             width += m.bounds.width;
  1205.                     return width + padding;
  1206.                 } else {
  1207.                     int max = Integer.MIN_VALUE;
  1208.                     for (MenuItem m : components)
  1209.                         if (m.bounds != null)
  1210.                             if (max < m.bounds.width)
  1211.                                 max = (int) m.bounds.width;
  1212.                     return max + (2 * padding);
  1213.                 }
  1214.             }
  1215.  
  1216.             /**
  1217.              * Gets the maximum height of this menu based of FlowType
  1218.              *
  1219.              * @return int
  1220.              */
  1221.             public int getHeight() {
  1222.                 if (flowType.equals(FlowType.HORIZONTAL)) {
  1223.                     int max = Integer.MIN_VALUE;
  1224.                     for (MenuItem m : components)
  1225.                         if (m.bounds != null)
  1226.                             if (max < m.bounds.height)
  1227.                                 max = (int) m.bounds.height;
  1228.                     return max + (2 * padding);
  1229.                 } else {
  1230.                     int height = padding * components.size();
  1231.                     for (MenuItem m : components)
  1232.                         if (m.bounds != null)
  1233.                             height += m.bounds.height;
  1234.                     return height + padding;
  1235.                 }
  1236.             }
  1237.  
  1238.             /**
  1239.              * Gets the current bounds of this menu, with rounded corners if
  1240.              * specified
  1241.              *
  1242.              * @param rounded
  1243.              *            boolean
  1244.              * @return RoundRectangle2D.Float
  1245.              */
  1246.             public RoundRectangle2D.Float getArea(boolean rounded) {
  1247.                 float round = rounded ? style.roundCorner : 0;
  1248.                 return new RoundRectangle2D.Float(getAbsX(), getAbsY(),
  1249.                         getWidth(), getHeight(), round, round);
  1250.             }
  1251.  
  1252.             public Rectangle getBoundsFull() {
  1253.                 RoundRectangle2D r1 = getArea(false);
  1254.                 RoundRectangle2D r2 = visiblePopup != null ? visiblePopup.bkg
  1255.                         : getArea(false);
  1256.                 int x = (int) Math.min(r1.getX(), r2.getX());
  1257.                 int y = (int) Math.min(r1.getY(), r2.getY());
  1258.                 int width = (int) Math.max(r1.getX() + r1.getWidth(), r2.getX()
  1259.                         + r2.getWidth())
  1260.                         - x;
  1261.                 int height = (int) Math.max(r1.getX() + r1.getHeight(),
  1262.                         r2.getX() + r2.getHeight())
  1263.                         - y;
  1264.                 return new Rectangle(x, y, width, height);
  1265.             }
  1266.  
  1267.             /**
  1268.              * Renders the menu to the specified Graphics object using the
  1269.              * provided mouse location for any popups
  1270.              *
  1271.              * @param g
  1272.              *            Graphics2D
  1273.              * @param mouseLoc
  1274.              *            Point
  1275.              */
  1276.             public void render(Graphics2D g, Point mouseLoc) {
  1277.                 g.setColor(style.bgColor);
  1278.                 g.fill(getArea(true));
  1279.                 MenuPopUp overPopup = null;
  1280.                 for (MenuItem m : components) {
  1281.                     m.render(g);
  1282.                     MenuPopUp p = m.getPopUp();
  1283.                     if (p != null) {
  1284.                         if (m.bounds.contains(mouseLoc)
  1285.                                 || (m.getPopUp().name
  1286.                                         .equalsIgnoreCase(visiblePopup != null ? visiblePopup.name
  1287.                                                 : "") && rectangleContainsApprox(
  1288.                                         visiblePopup.bkg, mouseLoc)))
  1289.                             overPopup = m.getPopUp();
  1290.                     }
  1291.                 }
  1292.                 if (showAllPopups)
  1293.                     getAllPopups(true).render(g);
  1294.                 else if (overPopup != null) {
  1295.                     overPopup.render(g);
  1296.                 }
  1297.                 visiblePopup = overPopup;
  1298.             }
  1299.  
  1300.             /**
  1301.              * Check if the specified point is within sqrt(2) of the Rectangle
  1302.              *
  1303.              * @param r
  1304.              *            RoundRectangle2D
  1305.              * @param p
  1306.              *            Point
  1307.              * @return boolean contains
  1308.              */
  1309.             public boolean rectangleContainsApprox(RoundRectangle2D r, Point p) {
  1310.                 for (int oX = -1; oX <= 1; oX++)
  1311.                     for (int oY = -1; oY <= 1; oY++)
  1312.                         if (r.contains(new Point(p.x + oX, p.y + oY)))
  1313.                             return true;
  1314.                 return false;
  1315.             }
  1316.  
  1317.             /**
  1318.              * Gets all popups in frontToBack or backToFront order and excluding
  1319.              * specified names
  1320.              *
  1321.              * @param rightToLeft
  1322.              *            boolean
  1323.              * @param excludeItems
  1324.              *            String...
  1325.              * @return MenuPopUp
  1326.              */
  1327.             public MenuPopUp getAllPopups(boolean backToFront,
  1328.                     String... excludeItems) {
  1329.                 List<String> lines = new ArrayList<String>();
  1330.                 if (components.size() > 0)
  1331.                     if (backToFront)
  1332.                         for (int i = components.size() - 1; i >= 0; i--) {
  1333.                             boolean ignored = false;
  1334.                             for (String subS : excludeItems)
  1335.                                 if (components.get(i).name
  1336.                                         .equalsIgnoreCase(subS))
  1337.                                     ignored = true;
  1338.                             if (!ignored) {
  1339.                                 MenuPopUp sub = components.get(i).getPopUp();
  1340.                                 if (sub != null)
  1341.                                     for (String line : sub.lines)
  1342.                                         if (!containsLine(lines, line))
  1343.                                             lines.add(line);
  1344.                             }
  1345.                         }
  1346.                     else
  1347.                         for (int i = 0; i < components.size(); i++) {
  1348.                             boolean ignored = false;
  1349.                             for (String subS : excludeItems)
  1350.                                 if (components.get(i).name
  1351.                                         .equalsIgnoreCase(subS))
  1352.                                     ignored = true;
  1353.                             if (!ignored) {
  1354.                                 MenuPopUp sub = components.get(i).getPopUp();
  1355.                                 if (sub != null)
  1356.                                     for (String line : sub.lines)
  1357.                                         if (!containsLine(lines, line))
  1358.                                             lines.add(line);
  1359.                             }
  1360.                         }
  1361.  
  1362.                 String[] linesArr = new String[lines.size()];
  1363.                 for (int i = 0; i < lines.size(); i++)
  1364.                     linesArr[i] = lines.get(i);
  1365.                 MenuPopUp p = new MenuPopUp(linesArr, "allpopups");
  1366.                 p.setParent(components.get(components.size() - 1));
  1367.                 return p;
  1368.             }
  1369.  
  1370.             /**
  1371.              * Checks if the specified array contains the specified line, just
  1372.              * the tag, not the data if possible
  1373.              *
  1374.              * @param lines
  1375.              *            List of Strings
  1376.              * @param line
  1377.              *            String
  1378.              * @return boolean contains
  1379.              */
  1380.             public boolean containsLine(List<String> lines, String line) {
  1381.                 for (String s : lines)
  1382.                     if (s.contains(":") && line.contains(":")) {
  1383.                         if (s.split(":")[0]
  1384.                                 .equalsIgnoreCase(line.split(":")[0]))
  1385.                             return true;
  1386.                     } else if (s.equalsIgnoreCase(line))
  1387.                         return true;
  1388.                 return false;
  1389.             }
  1390.  
  1391.             /**
  1392.              * Gets a proggie as a BufferedImage
  1393.              *
  1394.              * @return BufferedImage proggie
  1395.              */
  1396.             public BufferedImage getProggie() {
  1397.                 String[] lines = getAllPopups(true, "tools").lines;
  1398.                 Color color = style.FONT_COLOR;
  1399.                 Font font = style.FONT;
  1400.                 float line_height = font.getLineMetrics(lines[0], Paint.frc)
  1401.                         .getHeight();
  1402.                 int width = 0;
  1403.                 for (String line : lines)
  1404.                     if (font.getStringBounds(line, Paint.frc).getWidth() > width)
  1405.                         width = (int) font.getStringBounds(line, Paint.frc)
  1406.                                 .getWidth();
  1407.  
  1408.                 int height = (int) (lines.length * line_height);
  1409.  
  1410.                 width += style.roundCorner + 2;
  1411.                 height += style.roundCorner + 2;
  1412.  
  1413.                 RoundRectangle2D.Float bkg = new RoundRectangle2D.Float(0, 0,
  1414.                         width, height,
  1415.                         style.roundCorner > 0 ? style.roundCorner + 1 : 0,
  1416.                         style.roundCorner > 0 ? style.roundCorner + 1 : 0);
  1417.                 RoundRectangle2D.Float fg = new RoundRectangle2D.Float(
  1418.                         (float) bkg.getX() + 1, (float) bkg.getY() + 1,
  1419.                         width - 2, height - 2, style.roundCorner,
  1420.                         style.roundCorner);
  1421.                 Rectangle txtBounds = new Rectangle(
  1422.                         (int) (fg.getX() + (style.roundCorner / 2)),
  1423.                         (int) (fg.getY() + (style.roundCorner / 2)),
  1424.                         (int) (width - style.roundCorner - 2), (int) (height
  1425.                                 - style.roundCorner - 2));
  1426.                 BufferedImage bImg = new BufferedImage(width, height,
  1427.                         BufferedImage.TYPE_INT_ARGB);
  1428.                 Graphics2D g = (Graphics2D) bImg.getGraphics();
  1429.                 g.setColor(style.bgColor);
  1430.                 g.fill(bkg);
  1431.                 g.setColor(style.fgColor);
  1432.                 g.fill(fg);
  1433.                 g.setFont(font);
  1434.                 g.setColor(color);
  1435.                 for (int i = 0; i < lines.length; i++)
  1436.                     g.drawString(lines[i], txtBounds.x,
  1437.                             ((i + 1) * (line_height)) + txtBounds.y);
  1438.                 return bImg;
  1439.             }
  1440.  
  1441.             /**
  1442.              * Saves a progress report to the Screenshots directory
  1443.              */
  1444.             public void saveProggie() {
  1445.                 String name = smither.name
  1446.                         + "_"
  1447.                         + PaintFormatter.getFormattedTime(
  1448.                                 System.currentTimeMillis() - Paint.startTime,
  1449.                                 "-", true);
  1450.                 final File dir = new File(
  1451.                         GlobalConfiguration.Paths.getScreenshotsDirectory());
  1452.                 if (dir.isDirectory() || dir.mkdirs()) {
  1453.                     try {
  1454.                         int ext = 0;
  1455.                         while (new File(dir, name
  1456.                                 + (ext > 0 ? ("(" + ext + ")") : "") + ".png")
  1457.                                 .exists())
  1458.                             ext++;
  1459.  
  1460.                         ImageIO.write(Paint.paintMenu.getProggie(), "png",
  1461.                                 new File(dir, name
  1462.                                         + (ext > 0 ? ("(" + ext + ")") : "")));
  1463.                         log.info("Saved proggie!");
  1464.                         log.info(new File(dir, name
  1465.                                 + (ext > 0 ? ("(" + ext + ")") : "") + ".png")
  1466.                                 .getAbsolutePath());
  1467.                     } catch (IOException e) {
  1468.                         log.severe("Failed to save proggie!");
  1469.                     }
  1470.                 }
  1471.             }
  1472.  
  1473.             /**
  1474.              * Adds the specified mouse listener
  1475.              *
  1476.              * @param listen
  1477.              *            MouseListen
  1478.              */
  1479.             public void addMouseListener(MouseListen listen) {
  1480.                 if (mouseListeners == null)
  1481.                     mouseListeners = new ArrayList<MouseListen>();
  1482.                 mouseListeners.add(listen);
  1483.             }
  1484.  
  1485.             /**
  1486.              * Sets the default line listener
  1487.              *
  1488.              * @param listen
  1489.              *            LineListener
  1490.              */
  1491.             public void setDefaultLineListener(LineListener listen) {
  1492.                 defaultLineListener = listen;
  1493.             }
  1494.  
  1495.             /**
  1496.              * Fires the MouseEvent in all containing children and this
  1497.              *
  1498.              * @param e
  1499.              *            MouseEvent
  1500.              */
  1501.             public void fireMouseEvent(MouseEvent e) {
  1502.                 if (getArea(true).contains(e.getPoint()))
  1503.                     if (mouseListeners != null)
  1504.                         for (MouseListen m : mouseListeners)
  1505.                             m.MouseAction(e);
  1506.                 for (MenuItem m : components)
  1507.                     if (m.bounds != null)
  1508.                         if (m.bounds.contains(e.getPoint()))
  1509.                             m.fireMouseEvent(e);
  1510.                 if (visiblePopup != null)
  1511.                     visiblePopup.fireMouseEvent(e);
  1512.  
  1513.             }
  1514.         }
  1515.  
  1516.         /**
  1517.          * Represents a class containing the method for getting the popup
  1518.          *
  1519.          * @author Equinox_
  1520.          *
  1521.          */
  1522.         public abstract class GetPopup {
  1523.             abstract MenuPopUp getPopUp();
  1524.         }
  1525.  
  1526.         /**
  1527.          * Represents a modified MouseListener used in MenuEvents
  1528.          *
  1529.          * @author Equinox_
  1530.          *
  1531.          */
  1532.         public abstract class MouseListen {
  1533.             abstract void MouseAction(MouseEvent e);
  1534.         }
  1535.  
  1536.         /**
  1537.          * An abstract class representing MenuItems
  1538.          *
  1539.          * @author Equinox_
  1540.          *
  1541.          */
  1542.         public abstract class MenuItem {
  1543.             final GetPopup getPopup;
  1544.             Menu parent;
  1545.             RoundRectangle2D.Float bounds;
  1546.             int index;
  1547.             final int width;
  1548.             final int height;
  1549.             List<MouseListen> mouseListeners;
  1550.             final String name;
  1551.  
  1552.             /**
  1553.              * Constructs a MenuItem with the given name, parent, getPopup
  1554.              * method, width and height
  1555.              *
  1556.              * @param name
  1557.              *            String
  1558.              * @param parent
  1559.              *            Menu
  1560.              * @param getPopup
  1561.              *            GetPopup
  1562.              * @param width
  1563.              *            int
  1564.              * @param height
  1565.              *            int
  1566.              */
  1567.             public MenuItem(String name, Menu parent, GetPopup getPopup,
  1568.                     int width, int height) {
  1569.                 this.name = name;
  1570.                 this.height = height;
  1571.                 this.width = width;
  1572.                 this.getPopup = getPopup;
  1573.                 this.parent = parent;
  1574.                 this.bounds = new RoundRectangle2D.Float(0, 0, width, height,
  1575.                         parent.style.roundCorner, parent.style.roundCorner);
  1576.             }
  1577.  
  1578.             /**
  1579.              * Constructs a MenuItem with the given name, parent, width and
  1580.              * height
  1581.              *
  1582.              * @param name
  1583.              *            String
  1584.              * @param parent
  1585.              *            Menu
  1586.              * @param width
  1587.              *            int
  1588.              * @param height
  1589.              *            int
  1590.              */
  1591.             public MenuItem(String name, Menu parent, int width, int height) {
  1592.                 this(name, parent, null, width, height);
  1593.             }
  1594.  
  1595.             /**
  1596.              * sets this item's index
  1597.              *
  1598.              * @param idx
  1599.              *            int
  1600.              */
  1601.             public void setIndex(int idx) {
  1602.                 this.index = idx;
  1603.             }
  1604.  
  1605.             /**
  1606.              * renders this item to the given Graphics2D
  1607.              *
  1608.              * @param g
  1609.              *            Graphics2D
  1610.              */
  1611.             abstract void render(Graphics2D g);
  1612.  
  1613.             /**
  1614.              * Compiles this item
  1615.              */
  1616.             public void compile() {
  1617.                 this.bounds = new RoundRectangle2D.Float(0, 0, width, height,
  1618.                         parent.style.roundCorner, parent.style.roundCorner);
  1619.                 bounds.x = getX();
  1620.                 bounds.y = getY();
  1621.             }
  1622.  
  1623.             /**
  1624.              * Gets the offset from upper left of parent depending on parent's
  1625.              * FlowType
  1626.              *
  1627.              * @return int offsetY
  1628.              */
  1629.             public int getOffX() {
  1630.                 if (parent.getFlowType().equals(FlowType.HORIZONTAL)) {
  1631.                     int x = 1;
  1632.                     for (int i = 0; i < index; i++)
  1633.                         x += 1 + parent.components.get(i).bounds.width;
  1634.  
  1635.                     return x;
  1636.                 }
  1637.                 if (parent.getWidth() > bounds.width)
  1638.                     return (int) (parent.getWidth() / 2 - bounds.width / 2);
  1639.                 return 1;
  1640.             }
  1641.  
  1642.             /**
  1643.              * Gets the offset from upper left of parent depending on parent's
  1644.              * FlowType
  1645.              *
  1646.              * @return int offsetX
  1647.              */
  1648.             public int getOffY() {
  1649.                 if (parent.getFlowType().equals(FlowType.VERTICAL)) {
  1650.                     int y = 1;
  1651.                     for (int i = 0; i < index; i++)
  1652.                         y += 1 + parent.components.get(i).bounds.height;
  1653.  
  1654.                     return y;
  1655.                 }
  1656.                 return 1;
  1657.             }
  1658.  
  1659.             /**
  1660.              * Gets the absolute x
  1661.              *
  1662.              * @return int absolute x
  1663.              */
  1664.             public int getX() {
  1665.                 int x = getOffX() + parent.getAbsX();
  1666.                 return x;
  1667.             }
  1668.  
  1669.             /**
  1670.              * Gets the absolute y
  1671.              *
  1672.              * @return int absolute y
  1673.              */
  1674.             public int getY() {
  1675.                 int y = getOffY() + parent.getAbsY();
  1676.                 return y;
  1677.             }
  1678.  
  1679.             /**
  1680.              * Gets this item's popup
  1681.              *
  1682.              * @return MenuPopUp
  1683.              */
  1684.             public MenuPopUp getPopUp() {
  1685.                 if (getPopup != null) {
  1686.                     MenuPopUp m = getPopup.getPopUp();
  1687.                     m.setParent(this);
  1688.                     if (parent.defaultLineListener != null)
  1689.                         m.addLineListener(parent.defaultLineListener);
  1690.                     return m;
  1691.                 }
  1692.                 return null;
  1693.             }
  1694.  
  1695.             /**
  1696.              * Adds a mouselistener
  1697.              *
  1698.              * @param listen
  1699.              *            MouseListen
  1700.              */
  1701.             public void addMouseListener(MouseListen listen) {
  1702.                 if (mouseListeners == null)
  1703.                     mouseListeners = new ArrayList<MouseListen>();
  1704.                 mouseListeners.add(listen);
  1705.             }
  1706.  
  1707.             /**
  1708.              * Sends the mouse event to all of this item's listeners
  1709.              *
  1710.              * @param e
  1711.              *            MouseEvent
  1712.              */
  1713.             public void fireMouseEvent(MouseEvent e) {
  1714.                 if (mouseListeners != null && mouseListeners.size() > 0)
  1715.                     for (MouseListen m : mouseListeners)
  1716.                         m.MouseAction(e);
  1717.             }
  1718.         }
  1719.  
  1720.         /**
  1721.          * A class representing a mouse over button on the parent menu
  1722.          *
  1723.          * @author Equinox_
  1724.          *
  1725.          */
  1726.         public class MenuButton extends MenuItem {
  1727.             final static int width = 30;
  1728.             final static int height = 30;
  1729.  
  1730.             String imageIcon;
  1731.  
  1732.             /**
  1733.              * Constructs a button using the specified name, parent, imageIcon,
  1734.              * and getPopup method.
  1735.              *
  1736.              * @param name
  1737.              *            String
  1738.              * @param parent
  1739.              *            Menu
  1740.              * @param imageIcon
  1741.              *            String
  1742.              * @param getPopUp
  1743.              *            GetPopup
  1744.              */
  1745.             public MenuButton(String name, Menu parent, String imageIcon,
  1746.                     GetPopup getPopUp) {
  1747.                 super(name, parent, getPopUp, width, height);
  1748.                 this.imageIcon = imageIcon.toLowerCase();
  1749.             }
  1750.  
  1751.             /**
  1752.              * Constructs a button using the specified name, parent, and
  1753.              * imageIcon
  1754.              *
  1755.              * @param name
  1756.              *            String
  1757.              * @param parent
  1758.              *            Menu
  1759.              * @param imageIcon
  1760.              *            String
  1761.              */
  1762.             public MenuButton(String name, Menu parent, String imageIcon) {
  1763.                 this(name, parent, imageIcon, null);
  1764.             }
  1765.  
  1766.             /**
  1767.              * Constructs a button using the specified name and parent
  1768.              *
  1769.              * @param name
  1770.              *            String
  1771.              * @param parent
  1772.              *            Menu
  1773.              */
  1774.             public MenuButton(String name, Menu parent) {
  1775.                 this(name, parent, "", null);
  1776.             }
  1777.  
  1778.             /**
  1779.              * Renders this MenuButton to the given Graphics2D context
  1780.              */
  1781.             public void render(Graphics2D g) {
  1782.                 // Compiling
  1783.                 if (parent.parent.icons.containsKey(imageIcon))
  1784.                     if (parent.parent.icons.get(imageIcon) != null)
  1785.                         if (!parent.parent.icons.containsKey("Button-"
  1786.                                 + imageIcon)) {
  1787.                             // Scale the image
  1788.                             int width = (int) (bounds.width - (parent.style.roundCorner));
  1789.                             int height = (int) (bounds.height - (parent.style.roundCorner));
  1790.                             int hints = Image.SCALE_FAST;
  1791.                             parent.parent.icons.put(
  1792.                                     "Button-" + imageIcon,
  1793.                                     parent.parent.icons.get(imageIcon)
  1794.                                             .getScaledInstance(width, height,
  1795.                                                     hints));
  1796.                         }
  1797.  
  1798.                 g.setColor(parent.style.fgColor);
  1799.                 g.fill(bounds);
  1800.                 if (parent.parent.icons.containsKey("Button-" + imageIcon))
  1801.                     if (parent.parent.icons.get("Button-" + imageIcon) != null)
  1802.                         g.drawImage(
  1803.                                 parent.parent.icons.get("Button-" + imageIcon),
  1804.                                 (int) (bounds.x + (parent.style.roundCorner / 2)),
  1805.                                 (int) (bounds.y + (parent.style.roundCorner / 2)),
  1806.                                 null);
  1807.             }
  1808.         }
  1809.  
  1810.         /**
  1811.          * A class representing a progress bar in a menu
  1812.          *
  1813.          * @author Equinox_
  1814.          *
  1815.          */
  1816.         public class MenuProgressBar extends MenuItem {
  1817.             final static int width = 100;
  1818.             final static int height = 30;
  1819.  
  1820.             final GetPercent getPercent;
  1821.             RoundRectangle2D.Float percent;
  1822.             Point textLoc;
  1823.             String prefix;
  1824.  
  1825.             /**
  1826.              * Constructs a MenuProgress bar using the specified name, parent,
  1827.              * getPercent method, text prefix, and getPopup method
  1828.              *
  1829.              * @param name
  1830.              *            String
  1831.              * @param parent
  1832.              *            Menu
  1833.              * @param getPercent
  1834.              *            GetPercent
  1835.              * @param prefix
  1836.              *            String
  1837.              * @param getPopup
  1838.              *            GetPopup
  1839.              */
  1840.             public MenuProgressBar(String name, Menu parent,
  1841.                     GetPercent getPercent, String prefix, GetPopup getPopup) {
  1842.                 super(name, parent, getPopup, width, height);
  1843.                 this.getPercent = getPercent;
  1844.                 this.prefix = prefix;
  1845.             }
  1846.  
  1847.             /**
  1848.              * Renders this progress bar to the given Graphics context
  1849.              */
  1850.             public void render(Graphics2D g) {
  1851.                 percent.width = getPercent.getPercent();
  1852.                 g.setColor(parent.style.progressBarBG);
  1853.                 g.fill(bounds);
  1854.                 g.setColor(parent.style.progressBarFG);
  1855.                 g.fill(percent);
  1856.                 String perct = String.valueOf(getPercent.getPercent());
  1857.                 if (perct.length() < 2)
  1858.                     perct = "0" + perct;
  1859.                 g.setFont(style.FONT);
  1860.                 g.setColor(style.FONT_COLOR);
  1861.                 g.drawString(prefix + ": " + perct + "%", textLoc.x, textLoc.y);
  1862.             }
  1863.  
  1864.             /**
  1865.              * Compiles this progress bar
  1866.              */
  1867.             public void compile() {
  1868.                 super.compile();
  1869.                 percent = new RoundRectangle2D.Float(bounds.x, bounds.y,
  1870.                         getPercent.getPercent(), bounds.height,
  1871.                         bounds.arcwidth, bounds.archeight);
  1872.                 textLoc = new Point();
  1873.                 textLoc.x = (int) bounds.getCenterX();
  1874.                 textLoc.y = (int) bounds.getCenterY();
  1875.                 textLoc.y += parent.style.FONT.getLineMetrics(prefix + ": 00%",
  1876.                         parent.parent.frc).getHeight() / 2f;
  1877.                 textLoc.x -= parent.style.FONT.getStringBounds(
  1878.                         prefix + ": 00%", parent.parent.frc).getWidth() / 2f;
  1879.             }
  1880.         }
  1881.  
  1882.         /**
  1883.          * Class representing the method for getting the current percentile for
  1884.          * a progress bar
  1885.          *
  1886.          * @author Equinox_
  1887.          *
  1888.          */
  1889.         public abstract class GetPercent {
  1890.             abstract int getPercent();
  1891.         }
  1892.  
  1893.         /**
  1894.          * Class representing the LineListeners in popups
  1895.          *
  1896.          * @author Equinox_
  1897.          *
  1898.          */
  1899.         public abstract class LineListener {
  1900.             abstract void fireEvent(MouseEvent e, String line);
  1901.  
  1902.             abstract boolean hasAction(String line);
  1903.         }
  1904.  
  1905.         /**
  1906.          * A class representing the popups in the paint interface
  1907.          *
  1908.          * @author Equinox_
  1909.          *
  1910.          */
  1911.         public class MenuPopUp {
  1912.             private Font font;
  1913.             private Color color;
  1914.             private PaintStyle style;
  1915.             private RoundRectangle2D bkg;
  1916.             public RoundRectangle2D fg;
  1917.             public Rectangle txtBounds;
  1918.             public MenuItem parent;
  1919.             public final String name;
  1920.             public String[] lines;
  1921.             int width;
  1922.             int height;
  1923.             List<LineListener> lineListeners;
  1924.  
  1925.             /**
  1926.              * Creates a popup using the provided array of strings making up the
  1927.              * lines and name
  1928.              *
  1929.              * @param lines
  1930.              *            as String[]
  1931.              * @param name
  1932.              *            as String
  1933.              */
  1934.             public MenuPopUp(String[] lines, String name) {
  1935.                 this.name = name;
  1936.                 this.lines = lines;
  1937.             }
  1938.  
  1939.             /**
  1940.              * Set this popup's parent and compiles locations
  1941.              *
  1942.              * @param parent
  1943.              *            MenuItem
  1944.              */
  1945.             public void setParent(MenuItem parent) {
  1946.                 this.parent = parent;
  1947.                 this.style = parent.parent.style;
  1948.                 // Calc dimensions
  1949.                 color = name.equals("tools") ? style.TOOLS_FONT_COLOR
  1950.                         : style.FONT_COLOR;
  1951.                 font = name.equals("tools") ? style.TOOLS_FONT : style.FONT;
  1952.                 float line_height = font.getLineMetrics(lines[0], Paint.frc)
  1953.                         .getHeight();
  1954.                 width = 0;
  1955.                 for (String line : lines)
  1956.                     if (font.getStringBounds(line, Paint.frc).getWidth() > width)
  1957.                         width = (int) font.getStringBounds(line, Paint.frc)
  1958.                                 .getWidth();
  1959.  
  1960.                 height = (int) (lines.length * line_height);
  1961.  
  1962.                 width += style.roundCorner + 2;
  1963.                 height += style.roundCorner + 2;
  1964.  
  1965.                 int halfPadding = (parent.parent.padding / 2);
  1966.                 if (halfPadding < 1 && halfPadding != 0)
  1967.                     halfPadding = 1;
  1968.                 int x = 0;
  1969.                 int y = 0;
  1970.                 if (parent.parent.getFlowType().equals(FlowType.HORIZONTAL)) {
  1971.                     if (parent.bounds.y - height - parent.parent.padding > 0) {
  1972.                         // Above
  1973.                         x = (int) (parent.bounds.x - width
  1974.                                 + parent.bounds.width + halfPadding);
  1975.                         if (x < 0)
  1976.                             x = (int) (parent.bounds.x - halfPadding);
  1977.                         y = (int) (parent.parent.getAbsY() - height);
  1978.                     } else {
  1979.                         // Below
  1980.                         x = (int) (parent.bounds.x - halfPadding);
  1981.                         if (x + width > parent.parent.bounds.width)
  1982.                             x = (int) (parent.bounds.x - halfPadding - width);
  1983.                         y = (int) (parent.parent.getAbsY() + parent.parent
  1984.                                 .getHeight());
  1985.                     }
  1986.                 } else {
  1987.                     // Vertical
  1988.                     if (parent.bounds.x - width - parent.parent.padding > 0) {
  1989.                         // Left
  1990.                         x = (int) (parent.parent.getAbsX() - width);
  1991.                         if (parent.bounds.y - height + halfPadding
  1992.                                 + parent.bounds.height > 0)
  1993.                             y = (int) (parent.bounds.y - height + halfPadding + parent.bounds.height);
  1994.                         else
  1995.                             y = (int) (parent.bounds.y - halfPadding);
  1996.                     } else {
  1997.                         // Right
  1998.                         x = (int) (parent.parent.getAbsX() + parent.parent
  1999.                                 .getWidth());
  2000.                         if (parent.bounds.y - height + halfPadding
  2001.                                 + parent.bounds.height > 0)
  2002.                             y = (int) (parent.bounds.y - height + halfPadding + parent.bounds.height);
  2003.                         else
  2004.                             y = (int) (parent.bounds.y - halfPadding);
  2005.                     }
  2006.                 }
  2007.                 bkg = new RoundRectangle2D.Float(x, y, width, height,
  2008.                         style.roundCorner > 0 ? style.roundCorner + 1 : 0,
  2009.                         style.roundCorner > 0 ? style.roundCorner + 1 : 0);
  2010.                 fg = new RoundRectangle2D.Float((float) bkg.getX() + 1,
  2011.                         (float) bkg.getY() + 1, width - 2, height - 2,
  2012.                         style.roundCorner, style.roundCorner);
  2013.                 txtBounds = new Rectangle(
  2014.                         (int) (fg.getX() + (style.roundCorner / 2)),
  2015.                         (int) (fg.getY() + (style.roundCorner / 2)),
  2016.                         (int) (width - style.roundCorner - 2), (int) (height
  2017.                                 - style.roundCorner - 2));
  2018.             }
  2019.  
  2020.             /**
  2021.              * Paints this popup to using the provided Graphics2D in relation to
  2022.              * it's parent PaintInterface
  2023.              *
  2024.              * @param g
  2025.              *            as Graphics2D
  2026.              */
  2027.             public void render(Graphics2D g) {
  2028.                 g.setColor(style.bgColor);
  2029.                 g.fill(bkg);
  2030.                 g.setColor(style.fgColor);
  2031.                 g.fill(fg);
  2032.  
  2033.                 float line_height = (txtBounds.height - 4) / lines.length;
  2034.  
  2035.                 g.setFont(font);
  2036.                 g.setColor(color);
  2037.                 for (int i = 0; i < lines.length; i++) {
  2038.                     if (new Rectangle(txtBounds.x,
  2039.                             (int) ((i * (line_height)) + txtBounds.y),
  2040.                             txtBounds.width, (int) line_height)
  2041.                             .contains(parent.parent.parent.mouseLoc)
  2042.                             && hasAction(lines[i])) {
  2043.                         g.setColor(style.highlightColor);
  2044.                         g.fill(new Rectangle(txtBounds.x,
  2045.                                 (int) ((i * (line_height)) + 2 + txtBounds.y),
  2046.                                 txtBounds.width, (int) line_height));
  2047.                     }
  2048.                     g.setColor(color);
  2049.                     g.drawString(lines[i], txtBounds.x,
  2050.                             ((i + 1) * (line_height)) + txtBounds.y);
  2051.                 }
  2052.             }
  2053.  
  2054.             boolean hasAction(String line) {
  2055.                 if (lineListeners != null)
  2056.                     for (LineListener l : lineListeners)
  2057.                         if (l.hasAction(line))
  2058.                             return true;
  2059.                 return false;
  2060.             }
  2061.  
  2062.             /**
  2063.              * Adds the specified line listener
  2064.              *
  2065.              * @param l
  2066.              *            LineListener
  2067.              */
  2068.             public void addLineListener(LineListener l) {
  2069.                 if (lineListeners == null)
  2070.                     lineListeners = new ArrayList<LineListener>();
  2071.                 this.lineListeners.add(l);
  2072.             }
  2073.  
  2074.             /**
  2075.              * fires the specified mouse event using this popup's line listeners
  2076.              *
  2077.              * @param e
  2078.              *            MouseEvent
  2079.              */
  2080.             public void fireMouseEvent(MouseEvent e) {
  2081.                 if (!bkg.contains(e.getPoint()) || lineListeners == null
  2082.                         || lineListeners.size() == 0)
  2083.                     return;
  2084.  
  2085.                 int line_height = (txtBounds.height - 4) / lines.length;
  2086.  
  2087.                 for (int i = 0; i < lines.length; i++) {
  2088.                     Rectangle stringRect = new Rectangle(txtBounds.x,
  2089.                             (i * (line_height)) + txtBounds.y, txtBounds.width,
  2090.                             line_height);
  2091.                     if (stringRect.contains(e.getPoint())) {
  2092.                         for (LineListener lineListener : lineListeners)
  2093.                             lineListener.fireEvent(e, lines[i]);
  2094.                         return;
  2095.                     }
  2096.                 }
  2097.             }
  2098.         }
  2099.  
  2100.     }
  2101.  
  2102.     /**
  2103.      * Used for specifing the way a menu is layed out.
  2104.      *
  2105.      * @author Equinox_
  2106.      *
  2107.      */
  2108.     private enum FlowType {
  2109.         VERTICAL, HORIZONTAL
  2110.     }
  2111.  
  2112.     /**
  2113.      * Used for anchoring the menu bar
  2114.      *
  2115.      * @author Equinox_
  2116.      *
  2117.      */
  2118.     private enum Anchor {
  2119.         UP_LEFT, UP_RIGHT, DOWN_RIGHT, DOWN_LEFT
  2120.     }
  2121.  
  2122.     /**
  2123.      * Used to style the paint
  2124.      *
  2125.      * @author Equinox_
  2126.      *
  2127.      */
  2128.     public static class PaintStyle {
  2129.         public final Color FONT_COLOR = Color.BLACK;
  2130.         public final Font FONT = new Font("Arial", 1, 10)
  2131.                 .deriveFont(Font.PLAIN);
  2132.  
  2133.         public final Font TOOLS_FONT = new Font("Arial", 1, 14)
  2134.                 .deriveFont(Font.PLAIN);
  2135.         public final Color TOOLS_FONT_COLOR = Color.BLACK;
  2136.  
  2137.         public final float roundCorner = 10f;
  2138.         public final Color progressBarFG = new Color(0f, 1f, 0f, 1f);
  2139.         public final Color progressBarBG = new Color(.3f, .3f, .3f, 1f);
  2140.         public final Color bgColor = new Color(0f, 0f, 0f, 0.75f);
  2141.         final Color fgColor = new Color(.3f, .3f, .3f, 1f);
  2142.         final Color highlightColor = new Color(1f, 0f, 0f, 0.1f);
  2143.     }
  2144.  
  2145.     /**
  2146.      * Modified Copy of DrawMouse
  2147.      */
  2148.     public class ModifiedDrawMouse {
  2149.         private MouseTrailing mouseTrailing = new MouseTrailing();
  2150.         private ArrayList<Mouses> Click = new ArrayList<Mouses>();
  2151.         private long clickLifeTime = 1500;
  2152.  
  2153.         public void onRepaint(final Graphics render) {
  2154.             if (mouse != null) {
  2155.                 Point Location = mouse.getLocation();
  2156.                 if (mouse.isPressed())
  2157.                     if (Click.size() <= 5)
  2158.                         Click.add(new Mouses(System.currentTimeMillis(),
  2159.                                 Location));
  2160.                 mouseTrailing.draw((Graphics2D) render, Color.WHITE);
  2161.                 render.setColor(Color.GREEN);
  2162.                 render.drawLine(Location.x - 5, Location.y, Location.x + 5,
  2163.                         Location.y);
  2164.                 render.drawLine(Location.x, Location.y - 5, Location.x,
  2165.                         Location.y + 5);
  2166.                 if (Click.size() > 1)
  2167.                     for (Mouses click : Click) {
  2168.                         if (mouse.isPressed())
  2169.                             if (Click.size() <= 10)
  2170.                                 Click.add(new Mouses(
  2171.                                         System.currentTimeMillis(), Location));
  2172.                         float i = ((float) (click.getTime() + clickLifeTime - System
  2173.                                 .currentTimeMillis()) / (float) clickLifeTime) + 0.25f;
  2174.                         if (i > 0) {
  2175.                             render.setColor(new Color(1, 0, 0, i > 1 ? 1 : i));
  2176.                             render.drawLine(click.getLocation().x - 5,
  2177.                                     click.getLocation().y,
  2178.                                     click.getLocation().x + 5,
  2179.                                     click.getLocation().y);
  2180.                             render.drawLine(click.getLocation().x,
  2181.                                     click.getLocation().y - 5,
  2182.                                     click.getLocation().x,
  2183.                                     click.getLocation().y + 5);
  2184.                         } else if (System.currentTimeMillis() - click.getTime() >= 1000) {
  2185.                             Click.remove(click);
  2186.                         }
  2187.                     }
  2188.  
  2189.             }
  2190.         }
  2191.  
  2192.         /**
  2193.          * @author Baheer (Doout).
  2194.          */
  2195.         public class MouseTrailing {
  2196.             private int LifeTime = 1500;
  2197.             private ArrayList<Mouses> Info = new ArrayList<Mouses>();
  2198.  
  2199.             private void getLocation() {
  2200.                 Point m = mouse.getLocation();
  2201.                 if (Add(m)) {
  2202.                     Info.add(new Mouses(System.currentTimeMillis(), m));
  2203.                 }
  2204.             }
  2205.  
  2206.             public boolean Add(Point p) {
  2207.                 for (Mouses Trailing : Info)
  2208.                     if (Trailing.getLocation().equals(p))
  2209.                         return false;
  2210.                 return true;
  2211.             }
  2212.  
  2213.             public void draw(Graphics2D g, Color c) {
  2214.                 getLocation();
  2215.                 if (Info.size() > 1)
  2216.                     for (int t = 1; t < Info.size(); t++) {
  2217.                         Mouses Trailing1 = Info.get(t);
  2218.                         Mouses Trailing2 = Info.get(t - 1);
  2219.                         float i = (float) (Trailing1.getTime() + LifeTime - System
  2220.                                 .currentTimeMillis()) / (float) LifeTime;
  2221.                         if (i > 0) {
  2222.                             g.setColor(new Color(c.getRed() / 255f, c
  2223.                                     .getGreen() / 255f, c.getBlue() / 255f, (c
  2224.                                     .getAlpha() / 255f) * i));
  2225.                             g.drawLine(Trailing1.getLocation().x,
  2226.                                     Trailing1.getLocation().y,
  2227.                                     Trailing2.getLocation().x,
  2228.                                     Trailing2.getLocation().y);
  2229.                         }
  2230.                     }
  2231.                 if (Info.size() > 1) {
  2232.                     if (System.currentTimeMillis() > Info.get(0).getTime()
  2233.                             + LifeTime) {
  2234.                         Info.remove(0);
  2235.                     }
  2236.                 }
  2237.             }
  2238.  
  2239.         }
  2240.  
  2241.         private class Mouses {
  2242.             private long Time;
  2243.             private Point Location;
  2244.  
  2245.             public Mouses(long time, Point loc) {
  2246.                 Time = time;
  2247.                 Location = loc;
  2248.             }
  2249.  
  2250.             public long getTime() {
  2251.                 return Time;
  2252.             }
  2253.  
  2254.             public Point getLocation() {
  2255.                 return Location;
  2256.             }
  2257.         }
  2258.     }
  2259.  
  2260.     public void mouseDragged(MouseEvent arg0) {
  2261.         Paint.mouseLoc = arg0.getPoint();
  2262.     }
  2263.  
  2264.     public void mouseMoved(MouseEvent arg0) {
  2265.         Paint.mouseLoc = arg0.getPoint();
  2266.     }
  2267.  
  2268.     public void mouseClicked(MouseEvent arg0) {
  2269.         Paint.fireMouseEvent(arg0);
  2270.     }
  2271.  
  2272.     public void mouseEntered(MouseEvent arg0) {
  2273.  
  2274.     }
  2275.  
  2276.     public void mouseExited(MouseEvent arg0) {
  2277.  
  2278.     }
  2279.  
  2280.     public void mousePressed(MouseEvent arg0) {
  2281.  
  2282.     }
  2283.  
  2284.     public void mouseReleased(MouseEvent arg0) {
  2285.     }
  2286.  
  2287.     /**
  2288.      * Loads script data as the actual script runs. Useful for images that are
  2289.      * non-essential.
  2290.      *
  2291.      * @author Equinox_
  2292.      *
  2293.      */
  2294.     public static class DataLoader extends Thread {
  2295.         EquinoxTrackSmither smither;
  2296.  
  2297.         /**
  2298.          * Creates a dataloader using the specified EquinoxTrackSmither instance
  2299.          *
  2300.          * @param smither
  2301.          */
  2302.         public DataLoader(EquinoxTrackSmither smither) {
  2303.             super("Data Loader");
  2304.             this.smither = smither;
  2305.             this.start();
  2306.         }
  2307.  
  2308.         /**
  2309.          * Gets the icon for a specified image in the runescape online database.
  2310.          *
  2311.          * @param id
  2312.          *            as int
  2313.          * @return Image
  2314.          * @throws MalformedURLException
  2315.          * @throws IOException
  2316.          */
  2317.         public Image getItemImage(int id) throws MalformedURLException,
  2318.                 IOException {
  2319.             return ImageIO.read(new URL(
  2320.                     "http://services.runescape.com/m=itemdb_rs/3290_obj_sprite.gif?id="
  2321.                             + id));
  2322.         }
  2323.  
  2324.         public void run() {
  2325.             try {
  2326.                 if (JOptionPane.NO_OPTION == JOptionPane.showConfirmDialog(
  2327.                         null, "Would you like to load some images?",
  2328.                         "Equinox_ Data Loader", JOptionPane.YES_NO_OPTION))
  2329.                     throw new Exception();
  2330.  
  2331.                 long before = System.currentTimeMillis();
  2332.                 smither.Paint.putImage(
  2333.                         "homeicon",
  2334.                         ImageIO.read(new URL(smither.baseURL
  2335.                                 + "/images/houseIcon.png")));
  2336.                 smither.log("Loaded home image in "
  2337.                         + PaintFormatter.getFormattedTime(System
  2338.                                 .currentTimeMillis() - before));
  2339.                 before = System.currentTimeMillis();
  2340.                 smither.Paint.putImage(
  2341.                         "toolsicon",
  2342.                         ImageIO.read(new URL(smither.baseURL
  2343.                                 + "/images/tools.png")));
  2344.                 smither.log("Loaded tools icon in "
  2345.                         + PaintFormatter.getFormattedTime(System
  2346.                                 .currentTimeMillis() - before));
  2347.             } catch (Exception e) {
  2348.                 smither.log("Failed to load images");
  2349.             }
  2350.         }
  2351.     }
  2352. }
Advertisement
Add Comment
Please, Sign In to add comment