Skip to content

Instantly share code, notes, and snippets.

@TheBatScripts
Last active December 14, 2015 15:48
Show Gist options
  • Save TheBatScripts/5109981 to your computer and use it in GitHub Desktop.
Save TheBatScripts/5109981 to your computer and use it in GitHub Desktop.
package scripts;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.ScrollPane;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import org.tribot.api.DTMs;
import org.tribot.api.General;
import org.tribot.api.Screen;
import org.tribot.api.types.colour.ColourPoint;
import org.tribot.api.types.colour.DTM;
import org.tribot.api.types.colour.DTMPoint;
import org.tribot.api.types.colour.DTMSubPoint;
import org.tribot.api.types.colour.Tolerance;
import org.tribot.script.Script;
import org.tribot.script.ScriptManifest;
import org.tribot.script.interfaces.Ending;
import org.tribot.script.interfaces.EventBlockingOverride;
import org.tribot.script.interfaces.Painting;
@ScriptManifest (authors = { "TheBat" }, name = "DTM Builder", version = 3.11, category = "Tools")
public class DTMBuilder extends Script implements Painting, EventBlockingOverride, Ending{
private GUI gui = null;
BufferedImage image = General.isEOCClient()?Screen.captureDebugImage():org.tribot.api2007.Screen.getGameImage();
private boolean OPEN = true;
private DTMUtil dtmUtil = new DTMUtil();
private ImportWindow importW = null;
@Override
public void run() {
if(onStart()){
while(!Thread.interrupted() && loop()>=0){}
onStop();
}else println("Script failed to start!");
}
public boolean onStart() {
image = General.isEOCClient()?Screen.captureDebugImage():org.tribot.api2007.Screen.getGameImage();
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
gui = new GUI();
}
});
return true;
}
public long loop() {
if(!OPEN)return -1;
return 5000;
}
public void onStop() {
gui.dispose();
if(importW != null) importW.dispose();
}
@Override
public void onPaint(Graphics g) {
if(gui != null){
BufferedImage image2;
if(!dtmUtil.consume){
ColorModel cm = image.getColorModel();
WritableRaster raster = image.copyData(null);
dtmUtil.cacheImage = new BufferedImage(cm, raster, cm.isAlphaPremultiplied(), null);
cm = dtmUtil.cacheImage.getColorModel();
raster = dtmUtil.cacheImage.copyData(null);
image2 = new BufferedImage(cm, raster, cm.isAlphaPremultiplied(), null);
}else{
ColorModel cm = dtmUtil.cacheImage.getColorModel();
boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
WritableRaster raster = dtmUtil.cacheImage.copyData(null);
image2 = new BufferedImage(cm, raster, isAlphaPremultiplied, null);
}
try{
paintDTM(image2.getGraphics());
gui.img.getGraphics().drawImage(image2, 0,0,image2.getWidth(),image2.getHeight(),null);
}catch(Exception e){
println(e.getMessage());
println(e.getCause());
}
}
g.setColor(Color.GREEN);
for(int i = 0; i < dtmUtil.rects.size(); i++){
g.drawRect(dtmUtil.rects.get(i).x, dtmUtil.rects.get(i).y, dtmUtil.rects.get(i).width, dtmUtil.rects.get(i).height);
}
}
//Paint for the DTM tab
public void paintDTM(Graphics g) {
if(dtmUtil.curBox == null) return;
g.setColor(Color.BLACK);
g.fillRect(10, 350, 200, 45);
g.setColor(Color.WHITE);
g.drawString("x:" + dtmUtil.curBox.loc.x +"y: " + dtmUtil.curBox.loc.y, 20, 370);
g.drawString("r:" + dtmUtil.curBox.rgb.getRed() +" g: " +dtmUtil.curBox.rgb.getGreen() + " b: " + dtmUtil.curBox.rgb.getBlue(), 20, 390);
g.setColor(dtmUtil.curBox.rgb);
g.fillRect(10, 400, 50, 50);
for(int x = 0; x < dtmUtil.colors.length; x++) {
for(int y = 0; y < dtmUtil.colors.length; y++) {
ColorBox c = dtmUtil.colors[x][y];
g.setColor(c.rgb);
g.fillRect(c.box.x, c.box.y, c.box.width, c.box.height);
}
}
g.setColor(Color.RED);
g.drawRect(dtmUtil.colors[0][0].loc.x -1, dtmUtil.colors[0][0].loc.y -1, 14, 14);
g.fillRect(dtmUtil.curBox.box.x, dtmUtil.curBox.box.y, dtmUtil.curBox.box.width, dtmUtil.curBox.box.height);
g.fillRect(dtmUtil.curBox.loc.x, dtmUtil.curBox.loc.y, 1, 1);
if(!gui.colorB.isEmpty()) {
g.setColor(Color.BLUE);
g.drawRect(gui.colorB.get(0).point.x-7, gui.colorB.get(0).point.y-7, 14, 14);
g.fillRect(gui.colorB.get(0).point.x, gui.colorB.get(0).point.y, 1,1);
for(int i = 1; i < gui.colorB.size(); i++) {
g.setColor(new Color(255,255,255,80));
g.drawLine(gui.colorB.get(0).point.x, gui.colorB.get(0).point.y, gui.colorB.get(i).point.x, gui.colorB.get(i).point.y);
g.setColor(Color.GREEN);
g.drawRect(gui.colorB.get(i).point.x-7, gui.colorB.get(i).point.y-7, 14, 14);
g.fillRect(gui.colorB.get(i).point.x, gui.colorB.get(i).point.y, 1,1);
}
}
if(!dtmUtil.DTMloc.isEmpty()) {
dtmUtil.drawDTMSquares(g, Color.GREEN.brighter());
}
}
public class GUI extends JFrame {
private static final long serialVersionUID = 1L;
private final JLabel headerL = new JLabel("TRiBot DTM Builder", JLabel.CENTER);
private final JPanel img = new JPanel();
private final Font normalFont = new Font("Book antiqua", Font.PLAIN, 16);
private boolean movable = false;
private boolean ran = false;
//Lists for button for DTM points
ArrayList<JTextField> pointT = new ArrayList<JTextField>();
ArrayList<JTextField> colorT = new ArrayList<JTextField>();
ArrayList<JButton> selectB = new ArrayList<JButton>();
ArrayList<JButton> removeB = new ArrayList<JButton>();
ArrayList<ColourPoint> colorB = new ArrayList<ColourPoint>();
ArrayList<Tolerance> colorTol = new ArrayList<Tolerance>();
// GUI COMPONENTS
GridBagConstraints c;
GridBagConstraints sC;
Container pane;
Container sPane;
ScrollPane SP;
private int y;
public GUI() {
super("TRiBot DTM Builder");
addBorder(headerL);
setFonts(normalFont, headerL);
pane = new Container();
pane.setLayout(new GridBagLayout());
createC();
SP = new ScrollPane();
sPane = new Container();
sPane.setLayout(new GridBagLayout());
sC = new GridBagConstraints();
sC.fill = GridBagConstraints.HORIZONTAL;
sC.ipady = c.ipadx = 3;
sC.insets = new Insets(5, 5, 5, 5);
makeGUI();
setLocationRelativeTo(getOwner());
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
OPEN = false;
}
});
img.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
if(movable){
movable = false;
ran = false;
}else dtmUtil.click(e);
}
@Override
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
});
img.addMouseMotionListener(new MouseMotionListener(){
ColourPoint[] absPoints = colorB.toArray(new ColourPoint[colorB.size()]);
@Override
public void mouseMoved(MouseEvent e) {
if(movable){
if(!ran){
absPoints = colorB.toArray(new ColourPoint[colorB.size()]);
ran = true;
}
dtmUtil.click(e);
for(int i = 0; i < colorB.size(); i++){
colorB.set(i, new ColourPoint(new Point(absPoints[i].point.x+e.getX(), absPoints[i].point.y+e.getY()), colorB.get(i).colour));
}
}
}
@Override
public void mouseDragged(MouseEvent e) {}
});
buttonsDTM();
}
private void createC() {
c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = c.ipadx = 3;
c.insets = new Insets(5, 5, 5, 5);
}
// Make fonts look nice
void setFonts(Font font, Component... comps) {
for (Component curcomp : comps) {
curcomp.setFont(font);
}
}
// add borders to components
void addBorder(JLabel... lbls) {
final Border border = LineBorder.createGrayLineBorder();
for (JLabel lbl : lbls) {
lbl.setBorder(border);
}
}
// easy placement of panels
void addToGrid(GridBagConstraints c, Component comp, int gridx, int gridy, int gridwidth, double weightx, Container cont) {
c.gridx = gridx;
c.gridy = gridy;
c.gridwidth = gridwidth;
c.weightx = weightx;
cont.add(comp, c);
}
void addToGrid(GridBagConstraints c, Component comp, int gridx, int gridy, int gridwidth, int gridheight, double weightx, Container cont) {
c.gridx = gridx;
c.gridy = gridy;
c.gridheight = gridheight;
c.gridwidth = gridwidth;
c.weightx = weightx;
cont.add(comp, c);
}
void addToGrid(GridBagConstraints c, Component comp, int gridx, int gridy, int gridwidth, int gridheight, double weightx, double weighty, Container cont) {
c.gridx = gridx;
c.gridy = gridy;
c.gridheight = gridheight;
c.gridwidth = gridwidth;
c.weightx = weightx;
c.weighty = weighty;
cont.add(comp, c);
}
public JPanel getPanel(){
return img;
}
//DTM Tab components
private JLabel tolL_DTM = new JLabel("Color Tolerance");
private JLabel nameL_DTM = new JLabel("Name");
private JLabel radL_DTM = new JLabel("Diameter");
private final JButton addB_DTM = new JButton("Add Current Color");
private final JButton clearB_DTM = new JButton("Clear All");
private final JButton copyB_DTM = new JButton("Copy all to clipboard");
private final JButton togB_DTM = new JButton("Freeze");
private final JButton findB_DTM = new JButton("Find DTMS");
private final JButton importB_DTM = new JButton("Import");
private JTextField tolT_DTM = new JTextField("r: 10; g: 10; b: 10;");
private JTextField nameT_DTM = new JTextField("0");
private JTextField radT_DTM = new JTextField("1");
void makeGUI() {
int temp = 0;
y = 0;
pane.removeAll();
SP.removeAll();
sPane.removeAll();
pane.setPreferredSize(new Dimension(Screen.getDimension().width+20, Screen.getDimension().height + 340));
SP.setPreferredSize(new Dimension(Screen.getDimension().width, 175));
addToGrid(c, headerL, 0, y++, 3, 1.0, pane);
double temp1 = c.weighty;
int temp2 = c.fill;
c.weighty = 1.0;
c.fill = GridBagConstraints.BOTH;
addToGrid(c, img, 0, y++, 3, 1.0, pane);
c.fill = temp2;
c.weighty = temp1;
addToGrid(c, addB_DTM, 0, y, 1, 1.0, pane);
addToGrid(c, clearB_DTM, 2, y, 1, 1.0, pane);
addToGrid(c, togB_DTM, 1, y++, 1, 1.0, pane);
addToGrid(c, findB_DTM, 0, y, 1, 1.0, pane);
temp = c.ipadx;
c.ipadx = 0;
c.fill = GridBagConstraints.NONE;
c.anchor = GridBagConstraints.ABOVE_BASELINE_LEADING;
addToGrid(c, tolL_DTM, 1, y, 1, 1.0/2.0, pane);
c.ipadx = 20;
c.anchor = GridBagConstraints.ABOVE_BASELINE_TRAILING;
addToGrid(c, tolT_DTM, 1, y, 1, 1.0/2.0, pane);
c.ipadx = temp;
c.fill = GridBagConstraints.HORIZONTAL;
addToGrid(c, importB_DTM, 2, y++, 1, 1.0, pane);
addToGrid(c, copyB_DTM, 0, y, 1, 1.0, pane);
temp = c.ipadx;
c.ipadx = 0;
c.fill = GridBagConstraints.NONE;
c.anchor = GridBagConstraints.ABOVE_BASELINE_LEADING;
addToGrid(c, radL_DTM, 1, y, 1, 1.0/2.0, pane);
c.ipadx = 20;
c.anchor = GridBagConstraints.ABOVE_BASELINE_TRAILING;
addToGrid(c, radT_DTM, 1, y, 1, 1.0/2.0, pane);
c.ipadx = temp;
c.fill = GridBagConstraints.HORIZONTAL;
temp = c.ipadx;
c.ipadx = 0;
c.fill = GridBagConstraints.NONE;
c.anchor = GridBagConstraints.ABOVE_BASELINE_LEADING;
addToGrid(c, nameL_DTM, 2, y, 1, 1.0/2.0, pane);
c.ipadx = 100;
c.anchor = GridBagConstraints.ABOVE_BASELINE_TRAILING;
addToGrid(c, nameT_DTM, 2, y++, 1, 1.0/2.0, pane);
c.ipadx = temp;
c.fill = GridBagConstraints.HORIZONTAL;
for(int i = 0; i < selectB.size(); i++) {
selectB.get(i).setText("Select Point " + i);
removeB.get(i).setText("Remove Point " + i);
pointT.get(i).setText("Point p" + i + "= new Point(" + colorB.get(i).point.x + ", " + colorB.get(i).point.y + ");");
colorT.get(i).setText("Color c" + i + " = new Color(" + colorB.get(i).colour.getRed() + ", " + colorB.get(i).colour.getGreen() + ", " + colorB.get(i).colour.getBlue() + ");");
addToGrid(sC,selectB.get(i), 0, (4+ (i*2)), 1, 0.5, sPane);
addToGrid(sC,pointT.get(i), 1, (4+ (i*2)), 2, 0.5, sPane);
addToGrid(sC,removeB.get(i), 0, (5+ (i*2)), 1, 0.5, sPane);
addToGrid(sC,colorT.get(i), 1, (5+ (i*2)), 2, 0.5, sPane);
}
SP.add(sPane);
addToGrid(c, SP, 0, y, 3, 2.0/3.0, pane);
getContentPane().add(pane);
pack();
setVisible(true);
}
private void buttonsDTM(){
clearB_DTM.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dtmUtil.DTMloc.clear();
selectB.clear();
removeB.clear();
pointT.clear();
colorT.clear();
colorB.clear();
dtmUtil.rects.clear();
tolT_DTM.setText("r: 10; g: 10; b: 10;");
nameT_DTM.setText(""+dtmUtil.count);
makeGUI();
}
});
togB_DTM.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dtmUtil.consume = !dtmUtil.consume;
if(dtmUtil.consume){
togB_DTM.setText("UnFreeze");
}else{
togB_DTM.setText("Freeze");
}
SP.setSize(new Dimension(750,100));
makeGUI();
}
});
findB_DTM.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
DTMPoint DTM_PT = new DTMPoint(colorB.get(0).colour, dtmUtil.makeTolerance());
DTMSubPoint [] DTM_PTS = dtmUtil.getPoints();
DTM dtm = new DTM(DTM_PT, DTM_PTS);
dtmUtil.drawDTMS(DTMs.find(dtm, 0, 0, Screen.getDimension().width, Screen.getDimension().height),img.getGraphics());
SP.setSize(new Dimension(750,100));
makeGUI();
}
});
copyB_DTM.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
int dia = Integer.parseInt(radT_DTM.getText());
String mainPoint = "DTMPoint DTM_PT_"+ nameT_DTM.getText()+" = new DTMPoint(new Color(" + colorB.get(0).colour.getRed() + ", " + colorB.get(0).colour.getGreen() + ", " + colorB.get(0).colour.getBlue() + "), new Tolerance("+ colorTol.get(0).r + ", " + colorTol.get(0).g + ", " + colorTol.get(0).b+"))";
String subPoints = "DTMSubPoint [] DTM_PTS_"+ nameT_DTM.getText()+" = {";
for(int i = 1; i < pointT.size(); i++) {
subPoints += "new DTMSubPoint(new ColourPoint(new Point(" + (colorB.get(i).point.x-colorB.get(0).point.x) + ", " + (colorB.get(i).point.y-colorB.get(0).point.y) +"), new Color(" + colorB.get(i).colour.getRed() + ", " + colorB.get(i).colour.getGreen() + ", " + colorB.get(i).colour.getBlue() + ")), new Tolerance("+colorTol.get(i).r + ", " + colorTol.get(i).g + ", " + colorTol.get(i).b+"), "+dia+"), ";
}
subPoints = subPoints.substring(0, subPoints.length() - 2);
mainPoint += ";";
subPoints += "};";
String finalS = mainPoint + "\n" + subPoints + "\nDTM dtm"+nameT_DTM.getText()+" = new DTM(DTM_PT_"+ nameT_DTM.getText()+", DTM_PTS_"+ nameT_DTM.getText()+");";
Toolkit toolkit = Toolkit.getDefaultToolkit();
Clipboard clipboard = toolkit.getSystemClipboard();
StringSelection strSel = new StringSelection(finalS);
clipboard.setContents(strSel, null);
dtmUtil.count++;
try{
if(Integer.parseInt(nameT_DTM.getText()) == dtmUtil.count-1)nameT_DTM.setText(""+dtmUtil.count);
}catch(Exception ex){}
}
});
importB_DTM.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
importW = new ImportWindow();
importB_DTM.setEnabled(false);
}
});
}
});
addB_DTM.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try{
colorB.add(new ColourPoint(dtmUtil.curBox.loc,dtmUtil.curBox.rgb));
colorTol.add(dtmUtil.makeTolerance());
selectB.add(new JButton());
removeB.add(new JButton());
pointT.add(new JTextField());
colorT.add(new JTextField());
selectB.get(selectB.size() - 1).addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for(int i = 0; i < selectB.size(); i++) {
if(e.getSource().equals(selectB.get(i))) {
dtmUtil.loc = colorB.get(i).point;
dtmUtil.updateDrawBox();
SP.setSize(new Dimension(750,100));
}
}
}
});
removeB.get(removeB.size() - 1).addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for(int i = 0; i < removeB.size(); i++) {
if(e.getSource().equals(removeB.get(i))) {
colorB.remove(i);
gui.colorTol.remove(i);
selectB.remove(i);
removeB.remove(i);
pointT.remove(i);
colorT.remove(i);
makeGUI();
}
}
}
});
makeGUI();
}catch(Exception ex){}
}
});
}
}
public class ColorBox {
private Color rgb;
private Point loc;
private Rectangle box;
public ColorBox(Color color, Point loc, Rectangle rectangle) {
this.rgb = color;
this.loc = loc;
this.box = rectangle;
}
public boolean checkBox(final Point cursor) {
return box.contains(cursor);
}
}
/**
*
* The extra window for importing DTM data
*
*/
private class ImportWindow extends JFrame{
private static final long serialVersionUID = 6277001333870369606L;
private ScrollPane sp= new ScrollPane();
private JTextArea code = new JTextArea("");
private JButton ok = new JButton("Ok");
private JButton cancel = new JButton("Cancel");
public ImportWindow(){
super("Paste your code here");
this.setLocation(gui.getLocationOnScreen().x+12,gui.getLocationOnScreen().y+407);
JPanel pane = new JPanel();
pane.setLayout(new GridBagLayout());
pane.setBorder(LineBorder.createGrayLineBorder());
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(5, 5, 5, 5);
sp.setPreferredSize(new Dimension(image.getWidth()-262,69));
sp.setMaximumSize(new Dimension(image.getWidth()-262,69));
sp.add(code);
c.ipady = 69;
c.ipadx = 218;
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 2;
c.weightx = 1.0;
pane.add(sp, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = c.ipadx = 3;
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 1;
c.weightx = 1.0;
pane.add(ok, c);
c.gridx = 1;
c.gridy = 1;
c.gridwidth = 1;
c.weightx = 1.0;
pane.add(cancel, c);
add(pane);
pack();
this.setVisible(true);
this.addWindowListener(new WindowListener(){
public void windowActivated(WindowEvent arg0){}
public void windowClosed(WindowEvent arg0){}
public void windowClosing(WindowEvent e){
gui.importB_DTM.setEnabled(true);
}
public void windowDeactivated(WindowEvent arg0){}
public void windowDeiconified(WindowEvent arg0){}
public void windowIconified(WindowEvent arg0){}
public void windowOpened(WindowEvent arg0){}
});
ok.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
try{
if(code.getText().length() > 0){
//Clear stuff before adding the new things
dtmUtil.DTMloc.clear();
gui.selectB.clear();
gui.removeB.clear();
gui.pointT.clear();
gui.colorT.clear();
gui.colorB.clear();
gui.colorTol.clear();
dtmUtil.rects.clear();
gui.makeGUI();
String str = code.getText();
String[] lines = str.split("\\n");
/**
* Adding the root
*/
//pull the color new Color(255, 255, 255)
Pattern pattern = Pattern.compile("(\\Qnew Color(\\E)(\\d+)(\\Q, \\E)(\\d+)(\\Q, \\E)(\\d+)(\\Q)\\E)");
Matcher matcher = pattern.matcher(lines[0]);
matcher.find();
str = matcher.group();
Pattern intPattern = Pattern.compile("([\\-]*)(\\d+)");
Matcher intMatcher = intPattern.matcher(str);
intMatcher.find();
int r = Integer.parseInt(intMatcher.group());
intMatcher.find();
int g = Integer.parseInt(intMatcher.group());
intMatcher.find();
int b = Integer.parseInt(intMatcher.group());
//pull the tolerance
pattern = Pattern.compile("(\\Qnew Tolerance(\\E)(\\d+)(\\Q, \\E)(\\d+)(\\Q, \\E)(\\d+)(\\Q)\\E)");
matcher = pattern.matcher(lines[0]);
matcher.find();
str = matcher.group();
Matcher tolMatcher = intPattern.matcher(str);
tolMatcher.find();
int rT = Integer.parseInt(tolMatcher.group());
tolMatcher.find();
int gT = Integer.parseInt(tolMatcher.group());
tolMatcher.find();
int bT = Integer.parseInt(tolMatcher.group());
addPoint(new Point(0,0), new Color(r,g,b), new Tolerance(rT,gT,bT));
/**
* Adding sub-points
*/
str = lines[1];
pattern = Pattern.compile("(\\Qnew Point(\\E)(\\Q-\\E)*(\\d+)(\\Q, \\E)(\\Q-\\E)*(\\d+)(\\Q), \\E)(\\Qnew Color(\\E)(\\d+)(\\Q, \\E)([\\-]*)(\\d+)(\\Q, \\E)(\\d+)(\\Q)), \\E)(\\Qnew Tolerance(\\E)(\\d+)(\\Q, \\E)(\\d+)(\\Q, \\E)(\\d+)(\\Q)\\E)");
matcher = pattern.matcher(str);
while(matcher.find()){
intMatcher = intPattern.matcher(matcher.group());
//pull the point
intMatcher.find();
int x = Integer.parseInt(intMatcher.group());
intMatcher.find();
int y = Integer.parseInt(intMatcher.group());
//pull the color
intMatcher.find();
r = Integer.parseInt(intMatcher.group());
intMatcher.find();
g = Integer.parseInt(intMatcher.group());
intMatcher.find();
b = Integer.parseInt(intMatcher.group());
//pull the tolerance
tolMatcher.find();
rT = Integer.parseInt(intMatcher.group());
tolMatcher.find();
gT = Integer.parseInt(intMatcher.group());
tolMatcher.find();
bT = Integer.parseInt(intMatcher.group());
addPoint(new Point(x,y), new Color(r,g,b), new Tolerance(rT,gT,bT));
}
/**
* Pulling the name
*/
str = lines[2];
pattern = Pattern.compile("(\\Qdtm\\E)(.*)(\\Q=\\E)");
matcher = pattern.matcher(str);
matcher.find();
gui.nameT_DTM.setText(matcher.group().replace("dtm", "").replace(" =", ""));
gui.tolT_DTM.setText("r: 10; g: 10; b: 10;");
}
gui.importB_DTM.setEnabled(true);
dispose();
gui.movable = true;
}catch(Exception ex){
dispose();
JOptionPane.showMessageDialog(gui, "Failed to read data!", "Error!", JOptionPane.ERROR_MESSAGE);
dtmUtil.DTMloc.clear();
gui.selectB.clear();
gui.removeB.clear();
gui.pointT.clear();
gui.colorT.clear();
gui.colorB.clear();
gui.colorTol.clear();
dtmUtil.rects.clear();
gui.makeGUI();
gui.importB_DTM.setEnabled(true);
}
}
});
cancel.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
gui.importB_DTM.setEnabled(true);
dispose();
}
});
}
private void addPoint(Point pt, Color col, Tolerance tol){
try{
gui.colorB.add(new ColourPoint(pt,col));
gui.colorTol.add(tol);
gui.selectB.add(new JButton());
gui.removeB.add(new JButton());
gui.pointT.add(new JTextField());
gui.colorT.add(new JTextField());
gui.selectB.get(gui.selectB.size() - 1).addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for(int i = 0; i < gui.selectB.size(); i++) {
if(e.getSource().equals(gui.selectB.get(i))) {
dtmUtil.loc = gui.colorB.get(i).point;
dtmUtil.updateDrawBox();
gui.SP.setSize(new Dimension(750,100));
}
}
}
});
gui.removeB.get(gui.removeB.size() - 1).addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for(int i = 0; i < gui.removeB.size(); i++) {
if(e.getSource().equals(gui.removeB.get(i))) {
gui.colorB.remove(i);
gui.colorTol.remove(i);
gui.selectB.remove(i);
gui.removeB.remove(i);
gui.pointT.remove(i);
gui.colorT.remove(i);
gui.makeGUI();
}
}
}
});
gui.makeGUI();
}catch(Exception ex){}
}
}
/**
*
* DTM methods and variables
*
*/
private class DTMUtil{
boolean consume = false;
Point loc = new Point(0, 0);
int count = 0;
final Rectangle drawbox = new Rectangle(280, 330, 130, 130);
ColorBox[][] colors = new ColorBox[13][13];
ColorBox curBox;
Color[][] gameScache = null;
private ArrayList<Rectangle> DTMloc = new ArrayList<Rectangle>();
private ArrayList<Rectangle> rects = new ArrayList<Rectangle>();
private BufferedImage cacheImage;
public void drawDTMSquares(Graphics g, final Color c) {
g.setColor(c);
for(int i = 0; i < DTMloc.size(); i++) {
Rectangle dtm = DTMloc.get(i);
g.drawRect(dtm.x, dtm.y, dtm.width, dtm.height);
g.drawString("dtm" + i, dtm.x + dtm.width - 30, dtm.y + dtm.height + 10);
}
}
private Color[][] to2D(BufferedImage img) {
Color [][] out = new Color [img.getWidth()][img.getHeight()];
for(int i = 0; i < img.getHeight(); i++){
for(int j = 0; j < img.getWidth(); j++){
out [j][i] = new Color(img.getRGB(j, i));
}
}
return out;
}
//13x13
public void updateDrawBox() {
for(int y = 0; y < colors.length; y++) {
for(int x = 0; x < colors.length; x++) {
final Point curpoint = new Point(loc.x + x - 6, loc.y + y - 6);
final Color curcolor = gameScache[curpoint.x][curpoint.y];
final Rectangle currectangle = new Rectangle(280 + (x*10), 330 + (y*10), 10, 10);
colors[x][y] = new ColorBox(curcolor, curpoint, currectangle);
}
}
curBox = colors[6][6];
}
public void drawDTMS(DTM[] DTMS, Graphics g) {
rects = new ArrayList<Rectangle>();
for(int i = 0; i < DTMS.length; i++){
rects.add(buildRect(DTMS[i]));
}
}
public void click(MouseEvent e) {
loc = e.getPoint();
if(drawbox.contains(loc)) {
for(int y = 0; y < colors.length; y++) {
for(int x = 0; x < colors.length; x++) {
if(colors[x][y].checkBox(loc)) {
loc = colors[x][y].loc;
break;
}
}
}
} else {
gameScache = consume?to2D(cacheImage):to2D(image);
}
updateDrawBox();
}
public Rectangle buildRect(DTM dtm) {
Point [] points = new Point [dtm.sub_points.length+1];
points [0] = dtm.main_point.point;
for(int i = 0; i < dtm.sub_points.length; i++){
points [i+1] = dtm.sub_points[i].colourPoint.point;
}
int minX = points[0].x;
for(int i = 0; i < points.length; i++){
if(points[i].x < minX){
minX = points[i].x;
}
}
int maxX = points[0].x;
for(int i = 0; i < points.length; i++){
if(points[i].x > maxX){
maxX = points[i].x;
}
}
int minY = points[0].y;
for(int i = 0; i < points.length; i++){
if(points[i].y < minY){
minY = points[i].y;
}
}
int maxY = points[0].y;
for(int i = 0; i < points.length; i++){
if(points[i].y > maxY){
maxY = points[i].y;
}
}
return new Rectangle(minX,minY,maxX-minX, maxY-minY);
}
public DTMSubPoint[] getPoints() {
try{
ArrayList<DTMSubPoint> points = new ArrayList<DTMSubPoint>();
for(int i = 1; i < gui.colorB.size(); i++){
Point pt = new Point(gui.colorB.get(i).point.x-gui.colorB.get(0).point.x,gui.colorB.get(i).point.y-gui.colorB.get(0).point.y);
points.add(new DTMSubPoint(new ColourPoint(pt, gui.colorB.get(i).colour), makeTolerance(), Integer.parseInt(gui.radT_DTM.getText())));
}
return points.toArray(new DTMSubPoint[points.size()]);
}catch(IndexOutOfBoundsException ex){
return new DTMSubPoint[0];
}
}
public Tolerance makeTolerance() {
String in = gui.tolT_DTM.getText();
String [] rgb = new String [3];
int count = 0;
for(int i = 0; i < in.length(); i++){
char test = in.charAt(i);
if(test == ';'){
int j = i;
for(;j >= 0; j--){
if(in.charAt(j) == ':')break;
}
rgb[count] = in.substring(j+2, i);
count++;
}
}
return new Tolerance(Integer.parseInt(rgb[0]),Integer.parseInt(rgb[1]),Integer.parseInt(rgb[2]));
}
}
@Override
public OVERRIDE_RETURN overrideKeyEvent(KeyEvent e) {
return OVERRIDE_RETURN.PROCESS;
}
@Override
public OVERRIDE_RETURN overrideMouseEvent(MouseEvent e) {
if(e.getID() == MouseEvent.MOUSE_CLICKED && !dtmUtil.consume){
e.consume();
dtmUtil.loc = e.getPoint();
image = General.isEOCClient()?Screen.captureDebugImage():org.tribot.api2007.Screen.getGameImage();
dtmUtil.gameScache = dtmUtil.to2D(image);
dtmUtil.updateDrawBox();
return OVERRIDE_RETURN.DISMISS;
}else if(e.getID() == MouseEvent.MOUSE_PRESSED && !dtmUtil.consume)return OVERRIDE_RETURN.DISMISS;
else return OVERRIDE_RETURN.PROCESS;
}
@Override
public void onEnd() {
onStop();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment