Equinox

EquinioxTrackSmither.java

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