Implement Drag and drop overlay
This commit is contained in:
parent
e0af6118bd
commit
cb4174f7e5
Binary file not shown.
BIN
DivaBot/finish.png
Normal file
BIN
DivaBot/finish.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 35 KiB |
112
DivaBot/src/sig/Calibrator.java
Normal file
112
DivaBot/src/sig/Calibrator.java
Normal file
@ -0,0 +1,112 @@
|
||||
package sig;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Rectangle;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.swing.JPanel;
|
||||
|
||||
public class Calibrator{
|
||||
Calibrator(JPanel p) throws IOException, InterruptedException {
|
||||
boolean failed=false;
|
||||
int x = Math.min(MyRobot.STARTDRAG.x,MyRobot.ENDDRAG.x);
|
||||
int y = Math.min(MyRobot.STARTDRAG.y,MyRobot.ENDDRAG.y);
|
||||
int width = (Math.max(MyRobot.STARTDRAG.x,MyRobot.ENDDRAG.x)-Math.min(MyRobot.STARTDRAG.x,MyRobot.ENDDRAG.x));
|
||||
int height = (Math.max(MyRobot.STARTDRAG.y,MyRobot.ENDDRAG.y)-Math.min(MyRobot.STARTDRAG.y,MyRobot.ENDDRAG.y));
|
||||
|
||||
if (MyRobot.STARTDRAG.x>MyRobot.ENDDRAG.x) {
|
||||
int xTemp=MyRobot.STARTDRAG.x;
|
||||
MyRobot.STARTDRAG.x=MyRobot.ENDDRAG.x;
|
||||
MyRobot.ENDDRAG.x=xTemp;
|
||||
}
|
||||
if (MyRobot.STARTDRAG.y>MyRobot.ENDDRAG.y) {
|
||||
int yTemp=MyRobot.STARTDRAG.y;
|
||||
MyRobot.STARTDRAG.y=MyRobot.ENDDRAG.y;
|
||||
MyRobot.ENDDRAG.y=yTemp;
|
||||
}
|
||||
|
||||
/*Rectangle currentPointer = new Rectangle(
|
||||
x,y,
|
||||
(int)Math.floor(x+width*0.03984375d),
|
||||
(int)Math.floor(y+height*0.30833333333333333333333333333333d));
|
||||
calibrationline=currentPointer;*/
|
||||
failed=CalibrationStage1(p);
|
||||
if (failed) {return;}
|
||||
failed=CalibrationStage2(p);
|
||||
if (failed) {return;}
|
||||
p.setVisible(true);
|
||||
MyRobot.CALIBRATIONSTATUS="First calibration set done: X"+(x-MyRobot.STARTDRAG.x)+" Y"+(y-MyRobot.STARTDRAG.y);
|
||||
// failed=CalibrationStage3(p);
|
||||
// if (failed) {return;}
|
||||
// failed=CalibrationStage4(p);
|
||||
// if (failed) {return;}
|
||||
//MyRobot.CALIBRATIONSTATUS="First calibration set done: X"+(x-MyRobot.STARTDRAG.x)+" Y"+(y-MyRobot.STARTDRAG.y);
|
||||
}
|
||||
|
||||
private boolean CalibrationStage1(JPanel p) throws IOException, InterruptedException {
|
||||
boolean calibrated=false;
|
||||
int MAXTRIES=10000;
|
||||
MyRobot.CALIBRATIONSTATUS="Calibration Stage 1...";
|
||||
p.repaint();
|
||||
//ImageIO.write(MYROBOT.getSizedCapture(new Rectangle(x,y,width,height)),"png",new File("capture.png"));
|
||||
BufferedImage currentScreen = MyRobot.MYROBOT.getSizedCapture(new Rectangle(0,0,MyRobot.screenSize.width,MyRobot.screenSize.height));
|
||||
ImageIO.write(currentScreen.getSubimage(MyRobot.STARTDRAG.x,MyRobot.STARTDRAG.y,MyRobot.ENDDRAG.x-MyRobot.STARTDRAG.x,MyRobot.ENDDRAG.y-MyRobot.STARTDRAG.y),"png",new File("capture.png"));
|
||||
while (!calibrated&&MAXTRIES>0) {
|
||||
//Try moving left until the difference is too high or the colors are not right anymore.
|
||||
MyRobot.STARTDRAG.x-=1;
|
||||
//BufferedImage pixel = MYROBOT.getSizedCapture(new Rectangle(MyRobot.STARTDRAG.x,MyRobot.STARTDRAG.y,1,1));
|
||||
currentScreen = currentScreen.getSubimage(MyRobot.STARTDRAG.x,MyRobot.STARTDRAG.y,1,1);
|
||||
ImageIO.write(currentScreen,"png",new File("capture_"+System.nanoTime()+".png"));
|
||||
Color col = new Color(currentScreen.getRGB(0, 0));
|
||||
System.out.println("Checking "+col);
|
||||
if (!(col.getRed()>=5&&col.getRed()<=40&&
|
||||
col.getGreen()>=170&&col.getGreen()<=210&&
|
||||
col.getBlue()>=205&&col.getBlue()<=250)) {
|
||||
//This is the max X. Calibration on this side good.
|
||||
MyRobot.STARTDRAG.x++;
|
||||
System.out.println("End at "+MyRobot.STARTDRAG.x);
|
||||
return false;
|
||||
}
|
||||
MAXTRIES--;
|
||||
}
|
||||
MyRobot.CALIBRATIONSTATUS="Calibration failed! Try making the capture region larger and defining it more accurately.";
|
||||
p.repaint();
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean CalibrationStage2(JPanel p) throws IOException {
|
||||
boolean calibrated=false;
|
||||
int MAXTRIES=10000;
|
||||
MyRobot.CALIBRATIONSTATUS="Calibration Stage 2...";
|
||||
p.repaint();
|
||||
//ImageIO.write(MYROBOT.getSizedCapture(new Rectangle(x,y,width,height)),"png",new File("capture.png"));
|
||||
//BufferedImage currentScreen = MYROBOT.getSizedCapture(new Rectangle(MyRobot.STARTDRAG.x,MyRobot.STARTDRAG.y,MyRobot.ENDDRAG,height));
|
||||
BufferedImage currentScreen = MyRobot.MYROBOT.getSizedCapture(new Rectangle(0,0,MyRobot.screenSize.width,MyRobot.screenSize.height));
|
||||
ImageIO.write(currentScreen.getSubimage(MyRobot.STARTDRAG.x,MyRobot.STARTDRAG.y,MyRobot.ENDDRAG.x-MyRobot.STARTDRAG.x,MyRobot.ENDDRAG.y-MyRobot.STARTDRAG.y),"png",new File("capture_2.png"));
|
||||
while (!calibrated&&MAXTRIES>0) {
|
||||
//Try moving left until the difference is too high or the colors are not right anymore.
|
||||
MyRobot.STARTDRAG.y-=1;
|
||||
p.repaint();
|
||||
//currentScreen.setRGB(MyRobot.STARTDRAG.x,MyRobot.STARTDRAG.y, new Color(0,0,0).getRGB());
|
||||
currentScreen = currentScreen.getSubimage(MyRobot.STARTDRAG.x,MyRobot.STARTDRAG.y,1,1);
|
||||
ImageIO.write(currentScreen,"png",new File("capture_2_"+System.nanoTime()+".png"));
|
||||
Color col = new Color(currentScreen.getRGB(0, 0));
|
||||
System.out.println("Checking "+col);
|
||||
if (!(col.getRed()>=5&&col.getRed()<=40&&
|
||||
col.getGreen()>=170&&col.getGreen()<=210&&
|
||||
col.getBlue()>=205&&col.getBlue()<=250)) {
|
||||
//This is the max Y. Calibration on this side good.
|
||||
MyRobot.STARTDRAG.y++;
|
||||
System.out.println("End at "+MyRobot.STARTDRAG.y);
|
||||
return false;
|
||||
}
|
||||
MAXTRIES--;
|
||||
}
|
||||
MyRobot.CALIBRATIONSTATUS="Calibration failed! Try making the capture region larger and defining it more accurately.";
|
||||
p.repaint();
|
||||
return true;
|
||||
}
|
||||
}
|
@ -25,6 +25,10 @@ public class CustomRobot extends Robot{
|
||||
super(screen);
|
||||
}
|
||||
|
||||
public synchronized BufferedImage getSizedCapture(Rectangle r) {
|
||||
return super.createScreenCapture(r);
|
||||
}
|
||||
|
||||
public void refreshScreen() {
|
||||
currentScreen = super.createScreenCapture(new Rectangle(418+18,204+83,912-18,586-83));
|
||||
}
|
||||
|
@ -1,7 +1,11 @@
|
||||
package sig;
|
||||
import java.awt.Color;
|
||||
import java.awt.Cursor;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Font;
|
||||
import java.awt.FontFormatException;
|
||||
import java.awt.FontMetrics;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.GraphicsDevice;
|
||||
import java.awt.GraphicsEnvironment;
|
||||
import java.awt.Image;
|
||||
@ -9,6 +13,7 @@ import java.awt.Point;
|
||||
import java.awt.Rectangle;
|
||||
import java.awt.Robot;
|
||||
import java.awt.Toolkit;
|
||||
import java.awt.Window;
|
||||
import java.awt.datatransfer.Clipboard;
|
||||
import java.awt.datatransfer.StringSelection;
|
||||
import java.awt.event.ActionEvent;
|
||||
@ -16,6 +21,7 @@ import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.event.MouseListener;
|
||||
import java.awt.event.MouseMotionListener;
|
||||
import java.awt.font.TextAttribute;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.BufferedReader;
|
||||
@ -41,6 +47,9 @@ import java.util.List;
|
||||
import java.util.Scanner;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import javax.imageio.IIOImage;
|
||||
import javax.imageio.ImageIO;
|
||||
@ -58,6 +67,7 @@ import javax.swing.JOptionPane;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JTextField;
|
||||
import javax.swing.KeyStroke;
|
||||
import javax.swing.SwingUtilities;
|
||||
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpResponse;
|
||||
@ -111,6 +121,9 @@ public class MyRobot{
|
||||
static SongData selectedSong = null;
|
||||
static String difficulty = "H"; //H=Hard EX=Extreme EXEX=Extra Extreme
|
||||
static boolean recordedResults=false;
|
||||
static boolean CALIBRATION = true;
|
||||
static Point STARTDRAG = null;
|
||||
static Point ENDDRAG = null;
|
||||
|
||||
int lastcool,lastfine,lastsafe,lastsad,lastworst,lastcombo,lastscore;
|
||||
float lastpercent;
|
||||
@ -128,6 +141,16 @@ public class MyRobot{
|
||||
|
||||
boolean overlayHidden=false;
|
||||
static boolean onSongSelect=false;
|
||||
static BufferedImage finishbutton = null;
|
||||
static Dimension screenSize = new Dimension(0,0);
|
||||
static boolean dragging = false;
|
||||
static String CALIBRATIONSTATUS = "";
|
||||
static boolean calibrating=true;
|
||||
static Rectangle calibrationline = null;
|
||||
static boolean repaintCalled = false;
|
||||
public static Overlay OVERLAY;
|
||||
|
||||
public static ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
|
||||
|
||||
|
||||
public static void main(String[] args) throws JSONException, IOException, FontFormatException {
|
||||
@ -136,6 +159,7 @@ public class MyRobot{
|
||||
for (String key : JSONObject.getNames(obj)) {
|
||||
SONGNAMES[Integer.parseInt(key)-1] = new SongInfo(obj.getJSONObject(key));
|
||||
}
|
||||
finishbutton = ImageIO.read(new File("finish.png"));
|
||||
new MyRobot().go();
|
||||
}
|
||||
|
||||
@ -442,15 +466,27 @@ public class MyRobot{
|
||||
//System.out.println(title.getText());
|
||||
}
|
||||
});
|
||||
|
||||
RunTests();
|
||||
f.setVisible(true);
|
||||
f.setSize(1362, 1036);
|
||||
f.add(p);
|
||||
JFrame.setDefaultLookAndFeelDecorated(true);
|
||||
f.setUndecorated(true);
|
||||
OVERLAY = new Overlay();
|
||||
OVERLAY.setBounds(f.getGraphicsConfiguration().getBounds());
|
||||
OVERLAY.setOpaque(false);
|
||||
f.addMouseListener(OVERLAY);
|
||||
f.addMouseMotionListener(OVERLAY);
|
||||
screenSize=new Dimension(f.getGraphicsConfiguration().getBounds().width,f.getGraphicsConfiguration().getBounds().height);
|
||||
f.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
|
||||
//f.add(p);
|
||||
//System.out.println(f.getGraphicsConfiguration().getBounds().width+"/"+f.getGraphicsConfiguration().getBounds().height);
|
||||
f.setSize(f.getGraphicsConfiguration().getBounds().width,f.getGraphicsConfiguration().getBounds().height);
|
||||
f.add(OVERLAY);
|
||||
f.setBackground(new Color(0,0,0,0));
|
||||
f.setVisible(true);
|
||||
f.setTitle("DivaBot");
|
||||
title = new JTextField();
|
||||
title.setSize(200,100);
|
||||
title.setText((currentSong>=SONGNAMES.length)?"DONE!":SONGNAMES[currentSong].name);
|
||||
|
||||
|
||||
SongData s = SongData.getByTitle(SONGNAMES[currentSong].name);
|
||||
|
||||
BotMain();
|
||||
|
83
DivaBot/src/sig/Overlay.java
Normal file
83
DivaBot/src/sig/Overlay.java
Normal file
@ -0,0 +1,83 @@
|
||||
package sig;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.event.MouseListener;
|
||||
import java.awt.event.MouseMotionListener;
|
||||
|
||||
import javax.swing.JPanel;
|
||||
|
||||
public class Overlay extends JPanel implements MouseMotionListener,MouseListener{
|
||||
|
||||
Overlay() {
|
||||
Thread t = new Thread() {
|
||||
public void run() {
|
||||
while (true) {
|
||||
repaint();
|
||||
try {
|
||||
Thread.sleep(1000/60);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
t.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
g.setColor(new Color(0,0,0,1));
|
||||
g.fillRect(0, 0, MyRobot.screenSize.width, MyRobot.screenSize.height);
|
||||
g.setColor(new Color(0,255,255,96));
|
||||
if (MyRobot.STARTDRAG!=null&&MyRobot.ENDDRAG!=null) {
|
||||
g.fillRect(0,0,Math.min(MyRobot.STARTDRAG.x,MyRobot.ENDDRAG.x),MyRobot.screenSize.height);
|
||||
g.fillRect(Math.min(MyRobot.STARTDRAG.x,MyRobot.ENDDRAG.x),0,MyRobot.screenSize.width,Math.min(MyRobot.STARTDRAG.y,MyRobot.ENDDRAG.y));
|
||||
g.fillRect(Math.min(MyRobot.STARTDRAG.x,MyRobot.ENDDRAG.x), Math.max(MyRobot.STARTDRAG.y,MyRobot.ENDDRAG.y), Math.max(MyRobot.STARTDRAG.x,MyRobot.ENDDRAG.x)-Math.min(MyRobot.STARTDRAG.x,MyRobot.ENDDRAG.x), MyRobot.screenSize.height-Math.max(MyRobot.STARTDRAG.y,MyRobot.ENDDRAG.y));
|
||||
g.fillRect(Math.max(MyRobot.STARTDRAG.x,MyRobot.ENDDRAG.x), Math.min(MyRobot.STARTDRAG.y,MyRobot.ENDDRAG.y), MyRobot.screenSize.width-Math.max(MyRobot.STARTDRAG.x,MyRobot.ENDDRAG.x), MyRobot.screenSize.height-Math.min(MyRobot.STARTDRAG.y,MyRobot.ENDDRAG.y));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e) {
|
||||
MyRobot.STARTDRAG=e.getLocationOnScreen();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseReleased(MouseEvent e) {
|
||||
MyRobot.ENDDRAG=e.getLocationOnScreen();
|
||||
if (MyRobot.STARTDRAG.x>MyRobot.ENDDRAG.x) {
|
||||
var xTemp = MyRobot.STARTDRAG.x;
|
||||
MyRobot.STARTDRAG.x=MyRobot.ENDDRAG.x;
|
||||
MyRobot.ENDDRAG.x=xTemp;
|
||||
}
|
||||
if (MyRobot.STARTDRAG.y>MyRobot.ENDDRAG.y) {
|
||||
var xTemp = MyRobot.STARTDRAG.y;
|
||||
MyRobot.STARTDRAG.y=MyRobot.ENDDRAG.y;
|
||||
MyRobot.ENDDRAG.y=xTemp;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseEntered(MouseEvent e) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseExited(MouseEvent e) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseDragged(MouseEvent e) {
|
||||
MyRobot.ENDDRAG=e.getLocationOnScreen();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseMoved(MouseEvent e) {
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user