master
Marcus Gosselin 2015-02-14 14:01:51 -05:00
parent 2678822b15
commit acca920d29
4 changed files with 122 additions and 76 deletions

View File

@ -4,5 +4,6 @@
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry combineaccessrules="false" kind="src" path="/ImgurLibrary"/> <classpathentry combineaccessrules="false" kind="src" path="/ImgurLibrary"/>
<classpathentry combineaccessrules="false" kind="src" path="/MAndEngine"/> <classpathentry combineaccessrules="false" kind="src" path="/MAndEngine"/>
<classpathentry kind="lib" path="lib/jna-4.1.0.jar"/>
<classpathentry kind="output" path="bin"/> <classpathentry kind="output" path="bin"/>
</classpath> </classpath>

BIN
lib/jna-4.1.0.jar 100644

Binary file not shown.

View File

@ -1,3 +1,5 @@
import imgurlibrary.ImgurRequest;
import java.awt.Color; import java.awt.Color;
import java.awt.FontMetrics; import java.awt.FontMetrics;
import java.awt.Graphics2D; import java.awt.Graphics2D;
@ -7,20 +9,25 @@ import java.awt.Graphics2D;
* @author Marcus * @author Marcus
* *
*/ */
public class GraphicalImgurRequest extends ImgurRequest{ public class GraphicalImgurRequest {
// motion, because animation
private Point point = new Point(0, 0); private Point point = new Point(0, 0);
private Point desiredPoint = new Point(0, 0); private Point desiredPoint = new Point(0, 0);
// graphically, because animation
private double progress = 0; private double progress = 0;
private double scanProgress = 0; private double scanProgress = 0;
private ImageListener listener;
public GraphicalImgurRequest(double x1, double y1, double x2, double y2, ImageListener listener) { // wrapped request
super(listener); private final ImgurRequest req;
public GraphicalImgurRequest(double x1, double y1, double x2, double y2, ImgurRequest req) {
point.x = x1; point.x = x1;
point.y = y1; point.y = y1;
desiredPoint.x = x2; desiredPoint.x = x2;
desiredPoint.y = y2; desiredPoint.y = y2;
this.req = req;
} }
public void setDestination(double x, double y) { public void setDestination(double x, double y) {
@ -41,51 +48,58 @@ public class GraphicalImgurRequest extends ImgurRequest{
point.x = x; point.x = x;
point.y = y; point.y = y;
double newProgress = getProgress(); double newProgress = req.getProgress();
if(newProgress == newProgress) if (newProgress == newProgress)
progress -= (progress - newProgress) / 8d; progress -= (progress - newProgress) / 8d;
double newScanProgress = getScanProgress(); double newScanProgress = req.getScanProgress();
if(newScanProgress == newScanProgress) if (newScanProgress == newScanProgress)
scanProgress -= (scanProgress - newScanProgress) / 8d; scanProgress -= (scanProgress - newScanProgress) / 8d;
} }
private class Point { private class Point {
public double x, y; public double x, y;
public Point(double x, double y) { public Point(double x, double y) {
this.x = x; this.x = x;
this.y = y; this.y = y;
} }
} }
public final static int WIDTH = 100; public final static int WIDTH = 100;
public void draw(Graphics2D g) { public void draw(Graphics2D g) {
int XOFF = (int) (point.x);
int XOFF = (int)(point.x); int YOFF = (int) (point.y);
int YOFF = (int)(point.y);
FontMetrics metrics = g.getFontMetrics(); FontMetrics metrics = g.getFontMetrics();
//the bar // the bar
g.setColor(new Color(150, 150, 150)); g.setColor(new Color(150, 150, 150));
g.fillRect(XOFF, YOFF + 13, (int) (WIDTH * scanProgress) + 1, 20); g.fillRect(XOFF, YOFF + 13, (int) (WIDTH * scanProgress) + 1, 20);
g.drawRect(XOFF, YOFF + 13, WIDTH, 20); g.drawRect(XOFF, YOFF + 13, WIDTH, 20);
g.setColor(new Color(70, 70, 70)); g.setColor(new Color(70, 70, 70));
g.fillRect(XOFF, YOFF + 13, (int) (WIDTH * progress) + 1, 20); g.fillRect(XOFF, YOFF + 13, (int) (WIDTH * progress * scanProgress) + 1, 20);
g.drawRect(XOFF, YOFF + 13, WIDTH, 20); g.drawRect(XOFF, YOFF + 13, WIDTH, 20);
//total images // total images
String str = "" + getImagesDiscovered(); String str = "" + req.getImagesDiscovered();
g.drawString(str, XOFF + WIDTH - (metrics.charsWidth(str.toCharArray(), 0, str.length())) / 2, YOFF + 45); g.drawString(str, XOFF + (int) (WIDTH * scanProgress) - (metrics.charsWidth(str.toCharArray(), 0, str.length())) / 2, YOFF + 45);
//downloaded // downloaded
str = "" + getImagesDownloaded(); str = "" + req.getImagesDownloaded();
g.drawString(str, XOFF + (int) (WIDTH * progress) - (metrics.charsWidth(str.toCharArray(), 0, str.length())) / 2, YOFF + 10); g.drawString(str, XOFF + (int) (WIDTH * progress * scanProgress) - (metrics.charsWidth(str.toCharArray(), 0, str.length())) / 2, YOFF + 10);
// name of the thing // name of the thing
str = getTitle(); str = req.getTitle();
g.drawString(str, XOFF + (WIDTH / 2) - (metrics.charsWidth(str.toCharArray(), 0, str.length())) / 2, YOFF); g.drawString(str, XOFF + (WIDTH / 2) - (metrics.charsWidth(str.toCharArray(), 0, str.length())) / 2, YOFF);
} }
// because wrapping
public boolean isBusy() {
return req.isBusy();
}
} }

View File

@ -1,3 +1,7 @@
import imgurlibrary.ImageListener;
import imgurlibrary.ImgurRequest;
import imgurlibrary.SubredditRequest;
import java.awt.Color; import java.awt.Color;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.Font; import java.awt.Font;
@ -17,7 +21,14 @@ import java.util.Scanner;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import com.sun.jna.WString;
import com.sun.jna.ptr.PointerByReference;
import MAndEngine.BasicApp; import MAndEngine.BasicApp;
import MAndEngine.Button;
import MAndEngine.Engine; import MAndEngine.Engine;
/** /**
@ -67,6 +78,11 @@ public class ImgurDownloader implements BasicApp, ImageListener {
*/ */
private final Font font = new Font("Arial", Font.BOLD, 30); private final Font font = new Font("Arial", Font.BOLD, 30);
/**
* settings button
*/
private Button settings;
@Override @Override
public Dimension getResolution() { public Dimension getResolution() {
return new Dimension(WIDTH, HEIGHT); return new Dimension(WIDTH, HEIGHT);
@ -75,13 +91,17 @@ public class ImgurDownloader implements BasicApp, ImageListener {
@Override @Override
public void initialize() { public void initialize() {
settings = new Button(0, new Color(230, 230, 230), WIDTH - 150, 20, 130, 50, "Settings", 10, 10, true);
setCurrentProcessExplicitAppUserModelID("MAndWorks.ImgurDownloader.ImgurDownloader.2.0.0.0");
// System.out.println(getCurrentProcessExplicitAppUserModelID());
requests = new ArrayList<GraphicalImgurRequest>(); requests = new ArrayList<GraphicalImgurRequest>();
try { try {
image = ImageIO.read(this.getClass().getClassLoader() image = ImageIO.read(this.getClass().getClassLoader().getResourceAsStream("start.png"));
.getResourceAsStream("start.png"));
} catch (Exception e) { } catch (Exception e) {
image = null; image = null;
System.out.println("eakljrfgskldhfg");
} }
} }
@ -103,8 +123,7 @@ public class ImgurDownloader implements BasicApp, ImageListener {
int GAP = GraphicalImgurRequest.WIDTH + LEFT_MARGIN; int GAP = GraphicalImgurRequest.WIDTH + LEFT_MARGIN;
for (int i = 0; i < requests.size(); i++) { for (int i = 0; i < requests.size(); i++) {
requests.get(i).setDestination(LEFT_MARGIN + (GAP * i), requests.get(i).setDestination(LEFT_MARGIN + (GAP * i), HEIGHT - BOTTOM_HEIGHT + TOP_MARGIN);
HEIGHT - BOTTOM_HEIGHT + TOP_MARGIN);
requests.get(i).tick(); requests.get(i).tick();
} }
@ -120,6 +139,8 @@ public class ImgurDownloader implements BasicApp, ImageListener {
execute(textBox); execute(textBox);
textBox = ""; textBox = "";
} }
settings.poll();
} }
private void execute(String str) { private void execute(String str) {
@ -127,34 +148,27 @@ public class ImgurDownloader implements BasicApp, ImageListener {
if (parts[0].equalsIgnoreCase("load")) { if (parts[0].equalsIgnoreCase("load")) {
loadScript(parts[1]); loadScript(parts[1]);
} else if (parts[0].equalsIgnoreCase("user")) { } else if (parts[0].equalsIgnoreCase("user")) {
GraphicalImgurRequest request = new GraphicalImgurRequest(
(int) (WIDTH * 1.2d), HEIGHT - BOTTOM_HEIGHT + TOP_MARGIN,
0, 0, this);
int pages = 0;
try {
pages = Integer.parseInt(parts[2]);
} catch (Exception e) {
}
request.saveSubmitted(parts[1], pages == 0 ? 10 : pages);
requests.add(request);
} else { } else {
// assume subreddit // assume subreddit
GraphicalImgurRequest request = new GraphicalImgurRequest( GraphicalImgurRequest greq;
(int) (WIDTH * 1.2d), HEIGHT - BOTTOM_HEIGHT + TOP_MARGIN, ImgurRequest req;
0, 0, this); int pages;
int pages = 0;
try { try {
pages = Integer.parseInt(parts[1]); pages = Integer.parseInt(parts[1]);
} catch (Exception e) { } catch (Exception e) {
pages = 10;
} }
request.saveSubreddit(parts[0], pages == 0 ? 10 : pages, "time");
requests.add(request); req = new SubredditRequest(parts[0], pages, true, this);
// TODO make these positions variables bc vars
greq = new GraphicalImgurRequest((int) (WIDTH * 1.2d), HEIGHT - BOTTOM_HEIGHT + TOP_MARGIN, 0, 0, req);
requests.add(greq);
} }
} }
private void loadScript(String script) { private void loadScript(String script) {
File file = new File(System.getenv("APPDATA") File file = new File(System.getenv("APPDATA") + "\\MAndWorks\\ImgurDownloader\\scripts\\" + script + ".lst");
+ "\\MAndWorks\\ImgurDownloader\\scripts\\" + script + ".lst");
try { try {
Scanner scan = new Scanner(file); Scanner scan = new Scanner(file);
while (scan.hasNextLine()) { while (scan.hasNextLine()) {
@ -205,9 +219,9 @@ public class ImgurDownloader implements BasicApp, ImageListener {
g.setFont(font); g.setFont(font);
g.setColor(new Color(70, 70, 70)); g.setColor(new Color(70, 70, 70));
// TODO filepath variable and stufffffff ya // TODO filepath variable and stufffffff ya
g.drawString("> " + textBox g.drawString("> " + textBox + ((System.currentTimeMillis() / 1000) % 2 == 0 ? "I" : ""), 80, 28);
+ ((System.currentTimeMillis() / 1000) % 2 == 0 ? "I" : ""),
80, 28); settings.render(g);
} }
@ -274,7 +288,7 @@ public class ImgurDownloader implements BasicApp, ImageListener {
} }
@Override @Override
public void newImage(BufferedImage image) { public void sendImage(BufferedImage image) {
try { try {
image = fitImageScale(image, INNER_FRAME_WIDTH, INNER_FRAME_HEIGHT); image = fitImageScale(image, INNER_FRAME_WIDTH, INNER_FRAME_HEIGHT);
@ -285,8 +299,7 @@ public class ImgurDownloader implements BasicApp, ImageListener {
} }
private static BufferedImage fitImageScale(BufferedImage image, int width, private static BufferedImage fitImageScale(BufferedImage image, int width, int height) throws IOException {
int height) throws IOException {
int imageWidth = image.getWidth(); int imageWidth = image.getWidth();
int imageHeight = image.getHeight(); int imageHeight = image.getHeight();
@ -302,33 +315,51 @@ public class ImgurDownloader implements BasicApp, ImageListener {
scaleY = scaleX; scaleY = scaleX;
// give us the transform object thing // give us the transform object thing
AffineTransform scaleTransform = AffineTransform.getScaleInstance( AffineTransform scaleTransform = AffineTransform.getScaleInstance(scaleX, scaleY);
scaleX, scaleY);
// then make the scaling algorithm thing. // then make the scaling algorithm thing.
AffineTransformOp bilinearScaleOp = new AffineTransformOp( AffineTransformOp bilinearScaleOp = new AffineTransformOp(scaleTransform, AffineTransformOp.TYPE_BILINEAR);
scaleTransform, AffineTransformOp.TYPE_BILINEAR);
// out new image that we need to crop onto the buffer with the right // out new image that we need to crop onto the buffer with the right
// dimensions. // dimensions.
BufferedImage newImage = bilinearScaleOp.filter(image, BufferedImage newImage = bilinearScaleOp.filter(image, new BufferedImage((int) (imageWidth * scaleX), (int) (imageHeight * scaleY), image.getType()));
new BufferedImage((int) (imageWidth * scaleX),
(int) (imageHeight * scaleY), image.getType()));
// make the buffer // make the buffer
BufferedImage buffer = new BufferedImage(width, height, BufferedImage buffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
BufferedImage.TYPE_INT_ARGB);
Graphics g = buffer.getGraphics(); Graphics g = buffer.getGraphics();
int newImageWidth = newImage.getWidth(null); int newImageWidth = newImage.getWidth(null);
int newImageHeight = newImage.getHeight(null); int newImageHeight = newImage.getHeight(null);
// do math, shove it on. // do math, shove it on.
g.drawImage(newImage, (width - newImageWidth) / 2, g.drawImage(newImage, (width - newImageWidth) / 2, (height - newImageHeight) / 2, null);
(height - newImageHeight) / 2, null);
// return dat // return dat
return buffer; return buffer;
} }
public static void setCurrentProcessExplicitAppUserModelID(final String appID) {
if (SetCurrentProcessExplicitAppUserModelID(new WString(appID)).longValue() != 0)
throw new RuntimeException("unable to set current process explicit AppUserModelID to: " + appID);
}
public static String getCurrentProcessExplicitAppUserModelID() {
final PointerByReference r = new PointerByReference();
if (GetCurrentProcessExplicitAppUserModelID(r).longValue() == 0) {
final Pointer p = r.getValue();
return p.getString(0, true); // here we leak native memory by
// lazyness
}
return "N/A";
}
private static native NativeLong GetCurrentProcessExplicitAppUserModelID(PointerByReference appID);
private static native NativeLong SetCurrentProcessExplicitAppUserModelID(WString appID);
static {
Native.register("shell32");
}
} }