Equinox

EquinioxTrackSmither.java

Mar 9th, 2011
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 65.85 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.metal
  893. .getSmithingID());
  894. return new MenuPopUp(lines, "home");
  895. }
  896. });
  897. paintMenu.add(home);
  898.  
  899. MenuButton tools = new MenuButton("tools", paintMenu, "toolsicon",
  900. new GetPopup() {
  901. MenuPopUp getPopUp() {
  902. String[] lines = new String[5];
  903. lines[0] = "Save proggie";
  904. lines[1] = "Reload data";
  905. lines[2] = "Craft steel";
  906. lines[3] = "Craft iron";
  907. lines[4] = "Craft bronze";
  908. MenuPopUp popUp = new MenuPopUp(lines, "tools");
  909. popUp.addLineListener(new LineListener() {
  910. void fireEvent(MouseEvent e, String line) {
  911. line = line.toLowerCase().trim();
  912. if (e.getID() == MouseEvent.MOUSE_CLICKED) {
  913. if (line.contains("save proggie"))
  914. paintMenu.saveProggie();
  915. else if (line.contains("reload data"))
  916. new DataLoader(smither);
  917. else if (line.contains("craft steel")) {
  918. if (skills
  919. .getCurrentLevel(Skills.SMITHING) < STEEL_LEVEL) {
  920. log.info("You cannot craft steel tracks!");
  921. return;
  922. }
  923. if (!isMember) {
  924. log.info("You must be a member to craft steel tracks!");
  925. return;
  926. }
  927. smither.metalType = 2;
  928. } else if (line.contains("craft iron")) {
  929. if (skills
  930. .getCurrentLevel(Skills.SMITHING) < IRON_LEVEL) {
  931. log.info("You cannot craft iron tracks!");
  932. return;
  933. }
  934. smither.metalType = 1;
  935. } else if (line
  936. .contains("craft bronze")) {
  937. if (skills
  938. .getCurrentLevel(Skills.SMITHING) < BRONZE_LEVEL) {
  939. log.info("You cannot craft bronze tracks!");
  940. return;
  941. }
  942. smither.metalType = 0;
  943. }
  944.  
  945. }
  946. }
  947.  
  948. boolean hasAction(String line) {
  949. line = line.toLowerCase().trim();
  950. String[] lines = new String[5];
  951. lines[0] = "Save proggie";
  952. lines[1] = "Reload data";
  953. lines[2] = "Craft steel";
  954. lines[3] = "Craft iron";
  955. lines[4] = "Craft bronze";
  956. for (String lineC : lines)
  957. if (lineC.toLowerCase().contains(line))
  958. return true;
  959. return false;
  960. }
  961. });
  962. return popUp;
  963. }
  964. });
  965. paintMenu.add(tools);
  966.  
  967. paintMenu.compile();
  968. }
  969.  
  970. /**
  971. * Gets the current Goal for the setSkill, or sets all goal variables to
  972. * -1 if there is no goal
  973. */
  974. public void setUpGoal() {
  975. try {
  976. for (byte b = 0; b < 10; b++)
  977. if (game.getCurrentTab() != Game.TAB_STATS) {
  978. game.openTab(Game.TAB_STATS);
  979. sleep(1000);
  980. } else
  981. break;
  982.  
  983. RSInterface stats = interfaces.get(320);
  984. if (stats != null && stats.isValid()) {
  985. RSComponent mage = stats
  986. .getComponent(SKILL_INTERFACES[SkillID]);
  987. if (mage != null && mage.isValid()
  988. && mage.getArea() != null) {
  989. mouse.move((int) mage.getArea().getCenterX(),
  990. (int) mage.getArea().getCenterY());
  991. sleep(500, 750);
  992. // Now we see what the popup is doing
  993. RSComponent pop = stats.getComponent(202);
  994. if (pop != null && pop.isValid()) {
  995. RSComponent def = pop.getComponent(5);
  996. if (def != null && def.isValid()) {
  997. if (def.getText().trim()
  998. .equalsIgnoreCase("Target XP:")) {
  999. RSComponent goalXP = pop.getComponent(6);
  1000. if (goalXP != null && goalXP.isValid()) {
  1001. goalExp = Integer.valueOf(goalXP
  1002. .getText().replace(",", "")
  1003. .trim());
  1004. goalLevel = -1;
  1005. }
  1006. } else {
  1007. // Proceed
  1008. RSComponent currExp = pop.getComponent(4);
  1009. RSComponent goalRemainder = pop
  1010. .getComponent(12);
  1011. RSComponent goalLvl = pop.getComponent(10);
  1012. if (currExp != null && currExp.isValid()
  1013. && goalRemainder != null
  1014. && goalRemainder.isValid()
  1015. && goalLvl != null
  1016. && goalLvl.isValid()) {
  1017. goalExp = Integer.valueOf(currExp
  1018. .getText().replace(",", "")
  1019. .trim())
  1020. + Integer.valueOf(goalRemainder
  1021. .getText()
  1022. .replace(",", "")
  1023. .trim());
  1024. goalLevel = Integer.valueOf(goalLvl
  1025. .getText().replace(",", "")
  1026. .trim());
  1027. }
  1028. }
  1029. log.info("Target level: "
  1030. + (goalLevel > 0 ? goalLevel : "None"));
  1031. log.info("Target exp: "
  1032. + (goalExp > 0 ? goalExp : "None"));
  1033. return;
  1034. }
  1035. }
  1036. }
  1037. }
  1038. } catch (Exception e) {
  1039. }
  1040. goalExp = -1;
  1041. goalLevel = -1;
  1042. }
  1043.  
  1044. /**
  1045. * Renders the current paint to the given graphics context.
  1046. *
  1047. * @param g
  1048. * as the Graphics2D object to render to
  1049. */
  1050. public void render(Graphics2D g) {
  1051. paintMenu.render(g, mouseLoc);
  1052. int mouseX = mouseLoc.x;
  1053. int mouseY = mouseLoc.y;
  1054. g.setColor(Color.RED);
  1055. g.drawLine(mouseX - 5, mouseY - 5, mouseX + 5, mouseY + 5);
  1056. g.drawLine(mouseX - 5, mouseY + 5, mouseX + 5, mouseY - 5);
  1057. }
  1058.  
  1059. /**
  1060. * Fires a MouseEvent in the paintMenu
  1061. *
  1062. * @param e
  1063. * MouseEvent
  1064. */
  1065. public void fireMouseEvent(MouseEvent e) {
  1066. if (paintMenu.getBoundsFull().contains(e.getPoint()))
  1067. paintMenu.fireMouseEvent(e);
  1068. }
  1069.  
  1070. /**
  1071. * Represents a Menu on a PaintInterface
  1072. *
  1073. * @author Equinox_
  1074. *
  1075. */
  1076. public class Menu {
  1077. final int padding = 1;
  1078. Point location;
  1079. PaintInterface parent;
  1080. PaintStyle style;
  1081. List<MenuItem> components = new ArrayList<MenuItem>();
  1082. final Anchor anchor;
  1083. boolean showAllPopups;
  1084. MenuPopUp visiblePopup = null;
  1085. List<MouseListen> mouseListeners;
  1086. LineListener defaultLineListener;
  1087. private FlowType flowType = FlowType.HORIZONTAL;
  1088. public Dimension bounds;
  1089.  
  1090. /**
  1091. * Constructs a Menu using the provided PaintInterface, Location,
  1092. * Anchor, and the GameScreen as the bounds
  1093. *
  1094. * @param parent
  1095. * PaintInterface
  1096. * @param location
  1097. * Point
  1098. * @param anchor
  1099. * Anchor
  1100. */
  1101. public Menu(PaintInterface parent, Point location, Anchor anchor) {
  1102. this.parent = parent;
  1103. this.style = parent.style;
  1104. this.location = location;
  1105. this.anchor = anchor;
  1106.  
  1107. // Setup dimension
  1108. Rectangle r = interfaces.get(548).getComponent(3).getArea();
  1109. bounds = new Dimension(r.width, r.height);
  1110. }
  1111.  
  1112. /**
  1113. * Sets the FlowType and recompiles the menu
  1114. *
  1115. * @param flowType
  1116. * FlowType
  1117. */
  1118. public void setFlowType(FlowType flowType) {
  1119. this.flowType = flowType;
  1120. this.compile();
  1121. }
  1122.  
  1123. /**
  1124. * Gets the FlowType
  1125. *
  1126. * @return FlowType
  1127. */
  1128. public FlowType getFlowType() {
  1129. return flowType;
  1130. }
  1131.  
  1132. /**
  1133. * Toggles the showAllPopups state
  1134. * */
  1135. public void toggleShowAll() {
  1136. showAllPopups = !showAllPopups;
  1137. }
  1138.  
  1139. /**
  1140. * Constructs a menu using the provided parent and location,
  1141. * assuming Anchor.UP_LEFT
  1142. *
  1143. * @param parent
  1144. * PaintInterface
  1145. * @param location
  1146. * Point
  1147. */
  1148. public Menu(PaintInterface parent, Point location) {
  1149. this(parent, location, Anchor.UP_LEFT);
  1150. }
  1151.  
  1152. /**
  1153. * Adds the specified menu item
  1154. *
  1155. * @param i
  1156. * MenuItem
  1157. */
  1158. public void add(MenuItem i) {
  1159. i.setIndex(components.size());
  1160. components.add(i);
  1161. compile();
  1162. }
  1163.  
  1164. /**
  1165. * Compiles this menu
  1166. */
  1167. public void compile() {
  1168. for (int i = 0; i < components.size(); i++)
  1169. components.get(i).compile();
  1170. }
  1171.  
  1172. /**
  1173. * Gets position x based on the menu's anchor
  1174. *
  1175. * @return int
  1176. */
  1177. public int getAbsX() {
  1178. if (anchor.name().contains("LEFT"))
  1179. return location.x;
  1180. else
  1181. return location.x - getWidth();
  1182. }
  1183.  
  1184. /**
  1185. * Gets position y based on the menu's anchor
  1186. *
  1187. * @return int
  1188. */
  1189. public int getAbsY() {
  1190. if (anchor.name().contains("UP"))
  1191. return location.y;
  1192. else
  1193. return location.y - getHeight();
  1194. }
  1195.  
  1196. /**
  1197. * Gets the maximum width of this menu using the menu's FlowType
  1198. *
  1199. * @return int
  1200. */
  1201. public int getWidth() {
  1202. if (flowType.equals(FlowType.HORIZONTAL)) {
  1203. int width = padding * components.size();
  1204. for (MenuItem m : components)
  1205. if (m.bounds != null)
  1206. width += m.bounds.width;
  1207. return width + padding;
  1208. } else {
  1209. int max = Integer.MIN_VALUE;
  1210. for (MenuItem m : components)
  1211. if (m.bounds != null)
  1212. if (max < m.bounds.width)
  1213. max = (int) m.bounds.width;
  1214. return max + (2 * padding);
  1215. }
  1216. }
  1217.  
  1218. /**
  1219. * Gets the maximum height of this menu based of FlowType
  1220. *
  1221. * @return int
  1222. */
  1223. public int getHeight() {
  1224. if (flowType.equals(FlowType.HORIZONTAL)) {
  1225. int max = Integer.MIN_VALUE;
  1226. for (MenuItem m : components)
  1227. if (m.bounds != null)
  1228. if (max < m.bounds.height)
  1229. max = (int) m.bounds.height;
  1230. return max + (2 * padding);
  1231. } else {
  1232. int height = padding * components.size();
  1233. for (MenuItem m : components)
  1234. if (m.bounds != null)
  1235. height += m.bounds.height;
  1236. return height + padding;
  1237. }
  1238. }
  1239.  
  1240. /**
  1241. * Gets the current bounds of this menu, with rounded corners if
  1242. * specified
  1243. *
  1244. * @param rounded
  1245. * boolean
  1246. * @return RoundRectangle2D.Float
  1247. */
  1248. public RoundRectangle2D.Float getArea(boolean rounded) {
  1249. float round = rounded ? style.roundCorner : 0;
  1250. return new RoundRectangle2D.Float(getAbsX(), getAbsY(),
  1251. getWidth(), getHeight(), round, round);
  1252. }
  1253.  
  1254. public Rectangle getBoundsFull() {
  1255. RoundRectangle2D r1 = getArea(false);
  1256. RoundRectangle2D r2 = visiblePopup != null ? visiblePopup.bkg
  1257. : getArea(false);
  1258. int x = (int) Math.min(r1.getX(), r2.getX());
  1259. int y = (int) Math.min(r1.getY(), r2.getY());
  1260. int width = (int) Math.max(r1.getX() + r1.getWidth(), r2.getX()
  1261. + r2.getWidth())
  1262. - x;
  1263. int height = (int) Math.max(r1.getX() + r1.getHeight(),
  1264. r2.getX() + r2.getHeight())
  1265. - y;
  1266. return new Rectangle(x, y, width, height);
  1267. }
  1268.  
  1269. /**
  1270. * Renders the menu to the specified Graphics object using the
  1271. * provided mouse location for any popups
  1272. *
  1273. * @param g
  1274. * Graphics2D
  1275. * @param mouseLoc
  1276. * Point
  1277. */
  1278. public void render(Graphics2D g, Point mouseLoc) {
  1279. g.setColor(style.bgColor);
  1280. g.fill(getArea(true));
  1281. MenuPopUp overPopup = null;
  1282. for (MenuItem m : components) {
  1283. m.render(g);
  1284. MenuPopUp p = m.getPopUp();
  1285. if (p != null) {
  1286. if (m.bounds.contains(mouseLoc)
  1287. || (m.getPopUp().name
  1288. .equalsIgnoreCase(visiblePopup != null ? visiblePopup.name
  1289. : "") && rectangleContainsApprox(
  1290. visiblePopup.bkg, mouseLoc)))
  1291. overPopup = m.getPopUp();
  1292. }
  1293. }
  1294. if (showAllPopups)
  1295. getAllPopups(true).render(g);
  1296. else if (overPopup != null) {
  1297. overPopup.render(g);
  1298. }
  1299. visiblePopup = overPopup;
  1300. }
  1301.  
  1302. /**
  1303. * Check if the specified point is within sqrt(2) of the Rectangle
  1304. *
  1305. * @param r
  1306. * RoundRectangle2D
  1307. * @param p
  1308. * Point
  1309. * @return boolean contains
  1310. */
  1311. public boolean rectangleContainsApprox(RoundRectangle2D r, Point p) {
  1312. for (int oX = -1; oX <= 1; oX++)
  1313. for (int oY = -1; oY <= 1; oY++)
  1314. if (r.contains(new Point(p.x + oX, p.y + oY)))
  1315. return true;
  1316. return false;
  1317. }
  1318.  
  1319. /**
  1320. * Gets all popups in frontToBack or backToFront order and excluding
  1321. * specified names
  1322. *
  1323. * @param rightToLeft
  1324. * boolean
  1325. * @param excludeItems
  1326. * String...
  1327. * @return MenuPopUp
  1328. */
  1329. public MenuPopUp getAllPopups(boolean backToFront,
  1330. String... excludeItems) {
  1331. List<String> lines = new ArrayList<String>();
  1332. if (components.size() > 0)
  1333. if (backToFront)
  1334. for (int i = components.size() - 1; i >= 0; i--) {
  1335. boolean ignored = false;
  1336. for (String subS : excludeItems)
  1337. if (components.get(i).name
  1338. .equalsIgnoreCase(subS))
  1339. ignored = true;
  1340. if (!ignored) {
  1341. MenuPopUp sub = components.get(i).getPopUp();
  1342. if (sub != null)
  1343. for (String line : sub.lines)
  1344. if (!containsLine(lines, line))
  1345. lines.add(line);
  1346. }
  1347. }
  1348. else
  1349. for (int i = 0; i < components.size(); i++) {
  1350. boolean ignored = false;
  1351. for (String subS : excludeItems)
  1352. if (components.get(i).name
  1353. .equalsIgnoreCase(subS))
  1354. ignored = true;
  1355. if (!ignored) {
  1356. MenuPopUp sub = components.get(i).getPopUp();
  1357. if (sub != null)
  1358. for (String line : sub.lines)
  1359. if (!containsLine(lines, line))
  1360. lines.add(line);
  1361. }
  1362. }
  1363.  
  1364. String[] linesArr = new String[lines.size()];
  1365. for (int i = 0; i < lines.size(); i++)
  1366. linesArr[i] = lines.get(i);
  1367. MenuPopUp p = new MenuPopUp(linesArr, "allpopups");
  1368. p.setParent(components.get(components.size() - 1));
  1369. return p;
  1370. }
  1371.  
  1372. /**
  1373. * Checks if the specified array contains the specified line, just
  1374. * the tag, not the data if possible
  1375. *
  1376. * @param lines
  1377. * List of Strings
  1378. * @param line
  1379. * String
  1380. * @return boolean contains
  1381. */
  1382. public boolean containsLine(List<String> lines, String line) {
  1383. for (String s : lines)
  1384. if (s.contains(":") && line.contains(":")) {
  1385. if (s.split(":")[0]
  1386. .equalsIgnoreCase(line.split(":")[0]))
  1387. return true;
  1388. } else if (s.equalsIgnoreCase(line))
  1389. return true;
  1390. return false;
  1391. }
  1392.  
  1393. /**
  1394. * Gets a proggie as a BufferedImage
  1395. *
  1396. * @return BufferedImage proggie
  1397. */
  1398. public BufferedImage getProggie() {
  1399. String[] lines = getAllPopups(true, "tools").lines;
  1400. Color color = style.FONT_COLOR;
  1401. Font font = style.FONT;
  1402. float line_height = font.getLineMetrics(lines[0], Paint.frc)
  1403. .getHeight();
  1404. int width = 0;
  1405. for (String line : lines)
  1406. if (font.getStringBounds(line, Paint.frc).getWidth() > width)
  1407. width = (int) font.getStringBounds(line, Paint.frc)
  1408. .getWidth();
  1409.  
  1410. int height = (int) (lines.length * line_height);
  1411.  
  1412. width += style.roundCorner + 2;
  1413. height += style.roundCorner + 2;
  1414.  
  1415. RoundRectangle2D.Float bkg = new RoundRectangle2D.Float(0, 0,
  1416. width, height,
  1417. style.roundCorner > 0 ? style.roundCorner + 1 : 0,
  1418. style.roundCorner > 0 ? style.roundCorner + 1 : 0);
  1419. RoundRectangle2D.Float fg = new RoundRectangle2D.Float(
  1420. (float) bkg.getX() + 1, (float) bkg.getY() + 1,
  1421. width - 2, height - 2, style.roundCorner,
  1422. style.roundCorner);
  1423. Rectangle txtBounds = new Rectangle(
  1424. (int) (fg.getX() + (style.roundCorner / 2)),
  1425. (int) (fg.getY() + (style.roundCorner / 2)),
  1426. (int) (width - style.roundCorner - 2), (int) (height
  1427. - style.roundCorner - 2));
  1428. BufferedImage bImg = new BufferedImage(width, height,
  1429. BufferedImage.TYPE_INT_ARGB);
  1430. Graphics2D g = (Graphics2D) bImg.getGraphics();
  1431. g.setColor(style.bgColor);
  1432. g.fill(bkg);
  1433. g.setColor(style.fgColor);
  1434. g.fill(fg);
  1435. g.setFont(font);
  1436. g.setColor(color);
  1437. for (int i = 0; i < lines.length; i++)
  1438. g.drawString(lines[i], txtBounds.x,
  1439. ((i + 1) * (line_height)) + txtBounds.y);
  1440. return bImg;
  1441. }
  1442.  
  1443. /**
  1444. * Saves a progress report to the Screenshots directory
  1445. */
  1446. public void saveProggie() {
  1447. String name = smither.name
  1448. + "_"
  1449. + PaintFormatter.getFormattedTime(
  1450. System.currentTimeMillis() - Paint.startTime,
  1451. "-", true);
  1452. final File dir = new File(
  1453. GlobalConfiguration.Paths.getScreenshotsDirectory());
  1454. if (dir.isDirectory() || dir.mkdirs()) {
  1455. try {
  1456. int ext = 0;
  1457. while (new File(dir, name
  1458. + (ext > 0 ? ("(" + ext + ")") : "") + ".png")
  1459. .exists())
  1460. ext++;
  1461.  
  1462. ImageIO.write(Paint.paintMenu.getProggie(), "png",
  1463. new File(dir, name
  1464. + (ext > 0 ? ("(" + ext + ")") : "")));
  1465. log.info("Saved proggie!");
  1466. log.info(new File(dir, name
  1467. + (ext > 0 ? ("(" + ext + ")") : "") + ".png")
  1468. .getAbsolutePath());
  1469. } catch (IOException e) {
  1470. log.severe("Failed to save proggie!");
  1471. }
  1472. }
  1473. }
  1474.  
  1475. /**
  1476. * Adds the specified mouse listener
  1477. *
  1478. * @param listen
  1479. * MouseListen
  1480. */
  1481. public void addMouseListener(MouseListen listen) {
  1482. if (mouseListeners == null)
  1483. mouseListeners = new ArrayList<MouseListen>();
  1484. mouseListeners.add(listen);
  1485. }
  1486.  
  1487. /**
  1488. * Sets the default line listener
  1489. *
  1490. * @param listen
  1491. * LineListener
  1492. */
  1493. public void setDefaultLineListener(LineListener listen) {
  1494. defaultLineListener = listen;
  1495. }
  1496.  
  1497. /**
  1498. * Fires the MouseEvent in all containing children and this
  1499. *
  1500. * @param e
  1501. * MouseEvent
  1502. */
  1503. public void fireMouseEvent(MouseEvent e) {
  1504. if (getArea(true).contains(e.getPoint()))
  1505. if (mouseListeners != null)
  1506. for (MouseListen m : mouseListeners)
  1507. m.MouseAction(e);
  1508. for (MenuItem m : components)
  1509. if (m.bounds != null)
  1510. if (m.bounds.contains(e.getPoint()))
  1511. m.fireMouseEvent(e);
  1512. if (visiblePopup != null)
  1513. visiblePopup.fireMouseEvent(e);
  1514.  
  1515. }
  1516. }
  1517.  
  1518. /**
  1519. * Represents a class containing the method for getting the popup
  1520. *
  1521. * @author Equinox_
  1522. *
  1523. */
  1524. public abstract class GetPopup {
  1525. abstract MenuPopUp getPopUp();
  1526. }
  1527.  
  1528. /**
  1529. * Represents a modified MouseListener used in MenuEvents
  1530. *
  1531. * @author Equinox_
  1532. *
  1533. */
  1534. public abstract class MouseListen {
  1535. abstract void MouseAction(MouseEvent e);
  1536. }
  1537.  
  1538. /**
  1539. * An abstract class representing MenuItems
  1540. *
  1541. * @author Equinox_
  1542. *
  1543. */
  1544. public abstract class MenuItem {
  1545. final GetPopup getPopup;
  1546. Menu parent;
  1547. RoundRectangle2D.Float bounds;
  1548. int index;
  1549. final int width;
  1550. final int height;
  1551. List<MouseListen> mouseListeners;
  1552. final String name;
  1553.  
  1554. /**
  1555. * Constructs a MenuItem with the given name, parent, getPopup
  1556. * method, width and height
  1557. *
  1558. * @param name
  1559. * String
  1560. * @param parent
  1561. * Menu
  1562. * @param getPopup
  1563. * GetPopup
  1564. * @param width
  1565. * int
  1566. * @param height
  1567. * int
  1568. */
  1569. public MenuItem(String name, Menu parent, GetPopup getPopup,
  1570. int width, int height) {
  1571. this.name = name;
  1572. this.height = height;
  1573. this.width = width;
  1574. this.getPopup = getPopup;
  1575. this.parent = parent;
  1576. this.bounds = new RoundRectangle2D.Float(0, 0, width, height,
  1577. parent.style.roundCorner, parent.style.roundCorner);
  1578. }
  1579.  
  1580. /**
  1581. * Constructs a MenuItem with the given name, parent, width and
  1582. * height
  1583. *
  1584. * @param name
  1585. * String
  1586. * @param parent
  1587. * Menu
  1588. * @param width
  1589. * int
  1590. * @param height
  1591. * int
  1592. */
  1593. public MenuItem(String name, Menu parent, int width, int height) {
  1594. this(name, parent, null, width, height);
  1595. }
  1596.  
  1597. /**
  1598. * sets this item's index
  1599. *
  1600. * @param idx
  1601. * int
  1602. */
  1603. public void setIndex(int idx) {
  1604. this.index = idx;
  1605. }
  1606.  
  1607. /**
  1608. * renders this item to the given Graphics2D
  1609. *
  1610. * @param g
  1611. * Graphics2D
  1612. */
  1613. abstract void render(Graphics2D g);
  1614.  
  1615. /**
  1616. * Compiles this item
  1617. */
  1618. public void compile() {
  1619. this.bounds = new RoundRectangle2D.Float(0, 0, width, height,
  1620. parent.style.roundCorner, parent.style.roundCorner);
  1621. bounds.x = getX();
  1622. bounds.y = getY();
  1623. }
  1624.  
  1625. /**
  1626. * Gets the offset from upper left of parent depending on parent's
  1627. * FlowType
  1628. *
  1629. * @return int offsetY
  1630. */
  1631. public int getOffX() {
  1632. if (parent.getFlowType().equals(FlowType.HORIZONTAL)) {
  1633. int x = 1;
  1634. for (int i = 0; i < index; i++)
  1635. x += 1 + parent.components.get(i).bounds.width;
  1636.  
  1637. return x;
  1638. }
  1639. if (parent.getWidth() > bounds.width)
  1640. return (int) (parent.getWidth() / 2 - bounds.width / 2);
  1641. return 1;
  1642. }
  1643.  
  1644. /**
  1645. * Gets the offset from upper left of parent depending on parent's
  1646. * FlowType
  1647. *
  1648. * @return int offsetX
  1649. */
  1650. public int getOffY() {
  1651. if (parent.getFlowType().equals(FlowType.VERTICAL)) {
  1652. int y = 1;
  1653. for (int i = 0; i < index; i++)
  1654. y += 1 + parent.components.get(i).bounds.height;
  1655.  
  1656. return y;
  1657. }
  1658. return 1;
  1659. }
  1660.  
  1661. /**
  1662. * Gets the absolute x
  1663. *
  1664. * @return int absolute x
  1665. */
  1666. public int getX() {
  1667. int x = getOffX() + parent.getAbsX();
  1668. return x;
  1669. }
  1670.  
  1671. /**
  1672. * Gets the absolute y
  1673. *
  1674. * @return int absolute y
  1675. */
  1676. public int getY() {
  1677. int y = getOffY() + parent.getAbsY();
  1678. return y;
  1679. }
  1680.  
  1681. /**
  1682. * Gets this item's popup
  1683. *
  1684. * @return MenuPopUp
  1685. */
  1686. public MenuPopUp getPopUp() {
  1687. if (getPopup != null) {
  1688. MenuPopUp m = getPopup.getPopUp();
  1689. m.setParent(this);
  1690. if (parent.defaultLineListener != null)
  1691. m.addLineListener(parent.defaultLineListener);
  1692. return m;
  1693. }
  1694. return null;
  1695. }
  1696.  
  1697. /**
  1698. * Adds a mouselistener
  1699. *
  1700. * @param listen
  1701. * MouseListen
  1702. */
  1703. public void addMouseListener(MouseListen listen) {
  1704. if (mouseListeners == null)
  1705. mouseListeners = new ArrayList<MouseListen>();
  1706. mouseListeners.add(listen);
  1707. }
  1708.  
  1709. /**
  1710. * Sends the mouse event to all of this item's listeners
  1711. *
  1712. * @param e
  1713. * MouseEvent
  1714. */
  1715. public void fireMouseEvent(MouseEvent e) {
  1716. if (mouseListeners != null && mouseListeners.size() > 0)
  1717. for (MouseListen m : mouseListeners)
  1718. m.MouseAction(e);
  1719. }
  1720. }
  1721.  
  1722. /**
  1723. * A class representing a mouse over button on the parent menu
  1724. *
  1725. * @author Equinox_
  1726. *
  1727. */
  1728. public class MenuButton extends MenuItem {
  1729. final static int width = 30;
  1730. final static int height = 30;
  1731.  
  1732. String imageIcon;
  1733.  
  1734. /**
  1735. * Constructs a button using the specified name, parent, imageIcon,
  1736. * and getPopup method.
  1737. *
  1738. * @param name
  1739. * String
  1740. * @param parent
  1741. * Menu
  1742. * @param imageIcon
  1743. * String
  1744. * @param getPopUp
  1745. * GetPopup
  1746. */
  1747. public MenuButton(String name, Menu parent, String imageIcon,
  1748. GetPopup getPopUp) {
  1749. super(name, parent, getPopUp, width, height);
  1750. this.imageIcon = imageIcon.toLowerCase();
  1751. }
  1752.  
  1753. /**
  1754. * Constructs a button using the specified name, parent, and
  1755. * imageIcon
  1756. *
  1757. * @param name
  1758. * String
  1759. * @param parent
  1760. * Menu
  1761. * @param imageIcon
  1762. * String
  1763. */
  1764. public MenuButton(String name, Menu parent, String imageIcon) {
  1765. this(name, parent, imageIcon, null);
  1766. }
  1767.  
  1768. /**
  1769. * Constructs a button using the specified name and parent
  1770. *
  1771. * @param name
  1772. * String
  1773. * @param parent
  1774. * Menu
  1775. */
  1776. public MenuButton(String name, Menu parent) {
  1777. this(name, parent, "", null);
  1778. }
  1779.  
  1780. /**
  1781. * Renders this MenuButton to the given Graphics2D context
  1782. */
  1783. public void render(Graphics2D g) {
  1784. // Compiling
  1785. if (parent.parent.icons.containsKey(imageIcon))
  1786. if (parent.parent.icons.get(imageIcon) != null)
  1787. if (!parent.parent.icons.containsKey("Button-"
  1788. + imageIcon)) {
  1789. // Scale the image
  1790. int width = (int) (bounds.width - (parent.style.roundCorner));
  1791. int height = (int) (bounds.height - (parent.style.roundCorner));
  1792. int hints = Image.SCALE_FAST;
  1793. parent.parent.icons.put(
  1794. "Button-" + imageIcon,
  1795. parent.parent.icons.get(imageIcon)
  1796. .getScaledInstance(width, height,
  1797. hints));
  1798. }
  1799.  
  1800. g.setColor(parent.style.fgColor);
  1801. g.fill(bounds);
  1802. if (parent.parent.icons.containsKey("Button-" + imageIcon))
  1803. if (parent.parent.icons.get("Button-" + imageIcon) != null)
  1804. g.drawImage(
  1805. parent.parent.icons.get("Button-" + imageIcon),
  1806. (int) (bounds.x + (parent.style.roundCorner / 2)),
  1807. (int) (bounds.y + (parent.style.roundCorner / 2)),
  1808. null);
  1809. }
  1810. }
  1811.  
  1812. /**
  1813. * A class representing a progress bar in a menu
  1814. *
  1815. * @author Equinox_
  1816. *
  1817. */
  1818. public class MenuProgressBar extends MenuItem {
  1819. final static int width = 100;
  1820. final static int height = 30;
  1821.  
  1822. final GetPercent getPercent;
  1823. RoundRectangle2D.Float percent;
  1824. Point textLoc;
  1825. String prefix;
  1826.  
  1827. /**
  1828. * Constructs a MenuProgress bar using the specified name, parent,
  1829. * getPercent method, text prefix, and getPopup method
  1830. *
  1831. * @param name
  1832. * String
  1833. * @param parent
  1834. * Menu
  1835. * @param getPercent
  1836. * GetPercent
  1837. * @param prefix
  1838. * String
  1839. * @param getPopup
  1840. * GetPopup
  1841. */
  1842. public MenuProgressBar(String name, Menu parent,
  1843. GetPercent getPercent, String prefix, GetPopup getPopup) {
  1844. super(name, parent, getPopup, width, height);
  1845. this.getPercent = getPercent;
  1846. this.prefix = prefix;
  1847. }
  1848.  
  1849. /**
  1850. * Renders this progress bar to the given Graphics context
  1851. */
  1852. public void render(Graphics2D g) {
  1853. percent.width = getPercent.getPercent();
  1854. g.setColor(parent.style.progressBarBG);
  1855. g.fill(bounds);
  1856. g.setColor(parent.style.progressBarFG);
  1857. g.fill(percent);
  1858. String perct = String.valueOf(getPercent.getPercent());
  1859. if (perct.length() < 2)
  1860. perct = "0" + perct;
  1861. g.setFont(style.FONT);
  1862. g.setColor(style.FONT_COLOR);
  1863. g.drawString(prefix + ": " + perct + "%", textLoc.x, textLoc.y);
  1864. }
  1865.  
  1866. /**
  1867. * Compiles this progress bar
  1868. */
  1869. public void compile() {
  1870. super.compile();
  1871. percent = new RoundRectangle2D.Float(bounds.x, bounds.y,
  1872. getPercent.getPercent(), bounds.height,
  1873. bounds.arcwidth, bounds.archeight);
  1874. textLoc = new Point();
  1875. textLoc.x = (int) bounds.getCenterX();
  1876. textLoc.y = (int) bounds.getCenterY();
  1877. textLoc.y += parent.style.FONT.getLineMetrics(prefix + ": 00%",
  1878. parent.parent.frc).getHeight() / 2f;
  1879. textLoc.x -= parent.style.FONT.getStringBounds(
  1880. prefix + ": 00%", parent.parent.frc).getWidth() / 2f;
  1881. }
  1882. }
  1883.  
  1884. /**
  1885. * Class representing the method for getting the current percentile for
  1886. * a progress bar
  1887. *
  1888. * @author Equinox_
  1889. *
  1890. */
  1891. public abstract class GetPercent {
  1892. abstract int getPercent();
  1893. }
  1894.  
  1895. /**
  1896. * Class representing the LineListeners in popups
  1897. *
  1898. * @author Equinox_
  1899. *
  1900. */
  1901. public abstract class LineListener {
  1902. abstract void fireEvent(MouseEvent e, String line);
  1903.  
  1904. abstract boolean hasAction(String line);
  1905. }
  1906.  
  1907. /**
  1908. * A class representing the popups in the paint interface
  1909. *
  1910. * @author Equinox_
  1911. *
  1912. */
  1913. public class MenuPopUp {
  1914. private Font font;
  1915. private Color color;
  1916. private PaintStyle style;
  1917. private RoundRectangle2D bkg;
  1918. public RoundRectangle2D fg;
  1919. public Rectangle txtBounds;
  1920. public MenuItem parent;
  1921. public final String name;
  1922. public String[] lines;
  1923. int width;
  1924. int height;
  1925. List<LineListener> lineListeners;
  1926.  
  1927. /**
  1928. * Creates a popup using the provided array of strings making up the
  1929. * lines and name
  1930. *
  1931. * @param lines
  1932. * as String[]
  1933. * @param name
  1934. * as String
  1935. */
  1936. public MenuPopUp(String[] lines, String name) {
  1937. this.name = name;
  1938. this.lines = lines;
  1939. }
  1940.  
  1941. /**
  1942. * Set this popup's parent and compiles locations
  1943. *
  1944. * @param parent
  1945. * MenuItem
  1946. */
  1947. public void setParent(MenuItem parent) {
  1948. this.parent = parent;
  1949. this.style = parent.parent.style;
  1950. // Calc dimensions
  1951. color = name.equals("tools") ? style.TOOLS_FONT_COLOR
  1952. : style.FONT_COLOR;
  1953. font = name.equals("tools") ? style.TOOLS_FONT : style.FONT;
  1954. float line_height = font.getLineMetrics(lines[0], Paint.frc)
  1955. .getHeight();
  1956. width = 0;
  1957. for (String line : lines)
  1958. if (font.getStringBounds(line, Paint.frc).getWidth() > width)
  1959. width = (int) font.getStringBounds(line, Paint.frc)
  1960. .getWidth();
  1961.  
  1962. height = (int) (lines.length * line_height);
  1963.  
  1964. width += style.roundCorner + 2;
  1965. height += style.roundCorner + 2;
  1966.  
  1967. int halfPadding = (parent.parent.padding / 2);
  1968. if (halfPadding < 1 && halfPadding != 0)
  1969. halfPadding = 1;
  1970. int x = 0;
  1971. int y = 0;
  1972. if (parent.parent.getFlowType().equals(FlowType.HORIZONTAL)) {
  1973. if (parent.bounds.y - height - parent.parent.padding > 0) {
  1974. // Above
  1975. x = (int) (parent.bounds.x - width
  1976. + parent.bounds.width + halfPadding);
  1977. if (x < 0)
  1978. x = (int) (parent.bounds.x - halfPadding);
  1979. y = (int) (parent.parent.getAbsY() - height);
  1980. } else {
  1981. // Below
  1982. x = (int) (parent.bounds.x - halfPadding);
  1983. if (x + width > parent.parent.bounds.width)
  1984. x = (int) (parent.bounds.x - halfPadding - width);
  1985. y = (int) (parent.parent.getAbsY() + parent.parent
  1986. .getHeight());
  1987. }
  1988. } else {
  1989. // Vertical
  1990. if (parent.bounds.x - width - parent.parent.padding > 0) {
  1991. // Left
  1992. x = (int) (parent.parent.getAbsX() - width);
  1993. if (parent.bounds.y - height + halfPadding
  1994. + parent.bounds.height > 0)
  1995. y = (int) (parent.bounds.y - height + halfPadding + parent.bounds.height);
  1996. else
  1997. y = (int) (parent.bounds.y - halfPadding);
  1998. } else {
  1999. // Right
  2000. x = (int) (parent.parent.getAbsX() + parent.parent
  2001. .getWidth());
  2002. if (parent.bounds.y - height + halfPadding
  2003. + parent.bounds.height > 0)
  2004. y = (int) (parent.bounds.y - height + halfPadding + parent.bounds.height);
  2005. else
  2006. y = (int) (parent.bounds.y - halfPadding);
  2007. }
  2008. }
  2009. bkg = new RoundRectangle2D.Float(x, y, width, height,
  2010. style.roundCorner > 0 ? style.roundCorner + 1 : 0,
  2011. style.roundCorner > 0 ? style.roundCorner + 1 : 0);
  2012. fg = new RoundRectangle2D.Float((float) bkg.getX() + 1,
  2013. (float) bkg.getY() + 1, width - 2, height - 2,
  2014. style.roundCorner, style.roundCorner);
  2015. txtBounds = new Rectangle(
  2016. (int) (fg.getX() + (style.roundCorner / 2)),
  2017. (int) (fg.getY() + (style.roundCorner / 2)),
  2018. (int) (width - style.roundCorner - 2), (int) (height
  2019. - style.roundCorner - 2));
  2020. }
  2021.  
  2022. /**
  2023. * Paints this popup to using the provided Graphics2D in relation to
  2024. * it's parent PaintInterface
  2025. *
  2026. * @param g
  2027. * as Graphics2D
  2028. */
  2029. public void render(Graphics2D g) {
  2030. g.setColor(style.bgColor);
  2031. g.fill(bkg);
  2032. g.setColor(style.fgColor);
  2033. g.fill(fg);
  2034.  
  2035. float line_height = (txtBounds.height - 4) / lines.length;
  2036.  
  2037. g.setFont(font);
  2038. g.setColor(color);
  2039. for (int i = 0; i < lines.length; i++) {
  2040. if (new Rectangle(txtBounds.x,
  2041. (int) ((i * (line_height)) + txtBounds.y),
  2042. txtBounds.width, (int) line_height)
  2043. .contains(parent.parent.parent.mouseLoc)
  2044. && hasAction(lines[i])) {
  2045. g.setColor(style.highlightColor);
  2046. g.fill(new Rectangle(txtBounds.x,
  2047. (int) ((i * (line_height)) + 2 + txtBounds.y),
  2048. txtBounds.width, (int) line_height));
  2049. }
  2050. g.setColor(color);
  2051. g.drawString(lines[i], txtBounds.x,
  2052. ((i + 1) * (line_height)) + txtBounds.y);
  2053. }
  2054. }
  2055.  
  2056. boolean hasAction(String line) {
  2057. if (lineListeners != null)
  2058. for (LineListener l : lineListeners)
  2059. if (l.hasAction(line))
  2060. return true;
  2061. return false;
  2062. }
  2063.  
  2064. /**
  2065. * Adds the specified line listener
  2066. *
  2067. * @param l
  2068. * LineListener
  2069. */
  2070. public void addLineListener(LineListener l) {
  2071. if (lineListeners == null)
  2072. lineListeners = new ArrayList<LineListener>();
  2073. this.lineListeners.add(l);
  2074. }
  2075.  
  2076. /**
  2077. * fires the specified mouse event using this popup's line listeners
  2078. *
  2079. * @param e
  2080. * MouseEvent
  2081. */
  2082. public void fireMouseEvent(MouseEvent e) {
  2083. if (!bkg.contains(e.getPoint()) || lineListeners == null
  2084. || lineListeners.size() == 0)
  2085. return;
  2086.  
  2087. int line_height = (txtBounds.height - 4) / lines.length;
  2088.  
  2089. for (int i = 0; i < lines.length; i++) {
  2090. Rectangle stringRect = new Rectangle(txtBounds.x,
  2091. (i * (line_height)) + txtBounds.y, txtBounds.width,
  2092. line_height);
  2093. if (stringRect.contains(e.getPoint())) {
  2094. for (LineListener lineListener : lineListeners)
  2095. lineListener.fireEvent(e, lines[i]);
  2096. return;
  2097. }
  2098. }
  2099. }
  2100. }
  2101.  
  2102. }
  2103.  
  2104. /**
  2105. * Used for specifing the way a menu is layed out.
  2106. *
  2107. * @author Equinox_
  2108. *
  2109. */
  2110. private enum FlowType {
  2111. VERTICAL, HORIZONTAL
  2112. }
  2113.  
  2114. /**
  2115. * Used for anchoring the menu bar
  2116. *
  2117. * @author Equinox_
  2118. *
  2119. */
  2120. private enum Anchor {
  2121. UP_LEFT, UP_RIGHT, DOWN_RIGHT, DOWN_LEFT
  2122. }
  2123.  
  2124. /**
  2125. * Used to style the paint
  2126. *
  2127. * @author Equinox_
  2128. *
  2129. */
  2130. public static class PaintStyle {
  2131. public final Color FONT_COLOR = Color.BLACK;
  2132. public final Font FONT = new Font("Arial", 1, 10)
  2133. .deriveFont(Font.PLAIN);
  2134.  
  2135. public final Font TOOLS_FONT = new Font("Arial", 1, 14)
  2136. .deriveFont(Font.PLAIN);
  2137. public final Color TOOLS_FONT_COLOR = Color.BLACK;
  2138.  
  2139. public final float roundCorner = 10f;
  2140. public final Color progressBarFG = new Color(0f, 1f, 0f, 1f);
  2141. public final Color progressBarBG = new Color(.3f, .3f, .3f, 1f);
  2142. public final Color bgColor = new Color(0f, 0f, 0f, 0.75f);
  2143. final Color fgColor = new Color(.3f, .3f, .3f, 1f);
  2144. final Color highlightColor = new Color(1f, 0f, 0f, 0.1f);
  2145. }
  2146.  
  2147. /**
  2148. * Modified Copy of DrawMouse
  2149. */
  2150. public class ModifiedDrawMouse {
  2151. private MouseTrailing mouseTrailing = new MouseTrailing();
  2152. private ArrayList<Mouses> Click = new ArrayList<Mouses>();
  2153. private long clickLifeTime = 1500;
  2154.  
  2155. public void onRepaint(final Graphics render) {
  2156. if (mouse != null) {
  2157. Point Location = mouse.getLocation();
  2158. if (mouse.isPressed())
  2159. if (Click.size() <= 5)
  2160. Click.add(new Mouses(System.currentTimeMillis(),
  2161. Location));
  2162. mouseTrailing.draw((Graphics2D) render, Color.WHITE);
  2163. render.setColor(Color.GREEN);
  2164. render.drawLine(Location.x - 5, Location.y, Location.x + 5,
  2165. Location.y);
  2166. render.drawLine(Location.x, Location.y - 5, Location.x,
  2167. Location.y + 5);
  2168. if (Click.size() > 1)
  2169. for (Mouses click : Click) {
  2170. if (mouse.isPressed())
  2171. if (Click.size() <= 10)
  2172. Click.add(new Mouses(
  2173. System.currentTimeMillis(), Location));
  2174. float i = ((float) (click.getTime() + clickLifeTime - System
  2175. .currentTimeMillis()) / (float) clickLifeTime) + 0.25f;
  2176. if (i > 0) {
  2177. render.setColor(new Color(1, 0, 0, i > 1 ? 1 : i));
  2178. render.drawLine(click.getLocation().x - 5,
  2179. click.getLocation().y,
  2180. click.getLocation().x + 5,
  2181. click.getLocation().y);
  2182. render.drawLine(click.getLocation().x,
  2183. click.getLocation().y - 5,
  2184. click.getLocation().x,
  2185. click.getLocation().y + 5);
  2186. } else if (System.currentTimeMillis() - click.getTime() >= 1000) {
  2187. Click.remove(click);
  2188. }
  2189. }
  2190.  
  2191. }
  2192. }
  2193.  
  2194. /**
  2195. * @author Baheer (Doout).
  2196. */
  2197. public class MouseTrailing {
  2198. private int LifeTime = 1500;
  2199. private ArrayList<Mouses> Info = new ArrayList<Mouses>();
  2200.  
  2201. private void getLocation() {
  2202. Point m = mouse.getLocation();
  2203. if (Add(m)) {
  2204. Info.add(new Mouses(System.currentTimeMillis(), m));
  2205. }
  2206. }
  2207.  
  2208. public boolean Add(Point p) {
  2209. for (Mouses Trailing : Info)
  2210. if (Trailing.getLocation().equals(p))
  2211. return false;
  2212. return true;
  2213. }
  2214.  
  2215. public void draw(Graphics2D g, Color c) {
  2216. getLocation();
  2217. if (Info.size() > 1)
  2218. for (int t = 1; t < Info.size(); t++) {
  2219. Mouses Trailing1 = Info.get(t);
  2220. Mouses Trailing2 = Info.get(t - 1);
  2221. float i = (float) (Trailing1.getTime() + LifeTime - System
  2222. .currentTimeMillis()) / (float) LifeTime;
  2223. if (i > 0) {
  2224. g.setColor(new Color(c.getRed() / 255f, c
  2225. .getGreen() / 255f, c.getBlue() / 255f, (c
  2226. .getAlpha() / 255f) * i));
  2227. g.drawLine(Trailing1.getLocation().x,
  2228. Trailing1.getLocation().y,
  2229. Trailing2.getLocation().x,
  2230. Trailing2.getLocation().y);
  2231. }
  2232. }
  2233. if (Info.size() > 1) {
  2234. if (System.currentTimeMillis() > Info.get(0).getTime()
  2235. + LifeTime) {
  2236. Info.remove(0);
  2237. }
  2238. }
  2239. }
  2240.  
  2241. }
  2242.  
  2243. private class Mouses {
  2244. private long Time;
  2245. private Point Location;
  2246.  
  2247. public Mouses(long time, Point loc) {
  2248. Time = time;
  2249. Location = loc;
  2250. }
  2251.  
  2252. public long getTime() {
  2253. return Time;
  2254. }
  2255.  
  2256. public Point getLocation() {
  2257. return Location;
  2258. }
  2259. }
  2260. }
  2261.  
  2262. public void mouseDragged(MouseEvent arg0) {
  2263. Paint.mouseLoc = arg0.getPoint();
  2264. }
  2265.  
  2266. public void mouseMoved(MouseEvent arg0) {
  2267. Paint.mouseLoc = arg0.getPoint();
  2268. }
  2269.  
  2270. public void mouseClicked(MouseEvent arg0) {
  2271. Paint.fireMouseEvent(arg0);
  2272. }
  2273.  
  2274. public void mouseEntered(MouseEvent arg0) {
  2275.  
  2276. }
  2277.  
  2278. public void mouseExited(MouseEvent arg0) {
  2279.  
  2280. }
  2281.  
  2282. public void mousePressed(MouseEvent arg0) {
  2283.  
  2284. }
  2285.  
  2286. public void mouseReleased(MouseEvent arg0) {
  2287. }
  2288.  
  2289. /**
  2290. * Loads script data as the actual script runs. Useful for images that are
  2291. * non-essential.
  2292. *
  2293. * @author Equinox_
  2294. *
  2295. */
  2296. public static class DataLoader extends Thread {
  2297. EquinoxTrackSmither smither;
  2298.  
  2299. /**
  2300. * Creates a dataloader using the specified EquinoxTrackSmither instance
  2301. *
  2302. * @param smither
  2303. */
  2304. public DataLoader(EquinoxTrackSmither smither) {
  2305. super("Data Loader");
  2306. this.smither = smither;
  2307. this.start();
  2308. }
  2309.  
  2310. /**
  2311. * Gets the icon for a specified image in the runescape online database.
  2312. *
  2313. * @param id
  2314. * as int
  2315. * @return Image
  2316. * @throws MalformedURLException
  2317. * @throws IOException
  2318. */
  2319. public Image getItemImage(int id) throws MalformedURLException,
  2320. IOException {
  2321. return ImageIO.read(new URL(
  2322. "http://services.runescape.com/m=itemdb_rs/3290_obj_sprite.gif?id="
  2323. + id));
  2324. }
  2325.  
  2326. public void run() {
  2327. try {
  2328. if (JOptionPane.NO_OPTION == JOptionPane.showConfirmDialog(
  2329. null, "Would you like to load some images?",
  2330. "Equinox_ Data Loader", JOptionPane.YES_NO_OPTION))
  2331. throw new Exception();
  2332.  
  2333. long before = System.currentTimeMillis();
  2334. smither.Paint.putImage(
  2335. "homeicon",
  2336. ImageIO.read(new URL(smither.baseURL
  2337. + "/images/houseIcon.png")));
  2338. smither.log("Loaded home image in "
  2339. + PaintFormatter.getFormattedTime(System
  2340. .currentTimeMillis() - before));
  2341. before = System.currentTimeMillis();
  2342. smither.Paint.putImage(
  2343. "toolsicon",
  2344. ImageIO.read(new URL(smither.baseURL
  2345. + "/images/tools.png")));
  2346. smither.log("Loaded tools icon in "
  2347. + PaintFormatter.getFormattedTime(System
  2348. .currentTimeMillis() - before));
  2349. } catch (Exception e) {
  2350. smither.log("Failed to load images");
  2351. }
  2352. }
  2353. }
  2354. }
Advertisement
Add Comment
Please, Sign In to add comment