Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from SvenWollinger/SnipScope
Addition of SnipScope classes
- Loading branch information
Showing
3 changed files
with
271 additions
and
0 deletions.
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
src/main/java/io/wollinger/snipsniper/SnipScope/SnipScopeListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package io.wollinger.snipsniper.SnipScope; | ||
|
||
import io.wollinger.snipsniper.utils.InputContainer; | ||
import io.wollinger.snipsniper.utils.Vector2Int; | ||
|
||
import java.awt.*; | ||
import java.awt.event.*; | ||
|
||
public class SnipScopeListener implements KeyListener, MouseListener, MouseMotionListener, MouseWheelListener{ | ||
private final SnipScopeWindow snipScopeWindow; | ||
private final InputContainer input; | ||
private Point lastPoint; | ||
|
||
public SnipScopeListener(SnipScopeWindow snipScopeWindow) { | ||
this.snipScopeWindow = snipScopeWindow; | ||
input = snipScopeWindow.getInputContainer(); | ||
snipScopeWindow.addComponentListener(new ComponentAdapter() { | ||
public void componentResized(ComponentEvent evt) { | ||
snipScopeWindow.resizeTrigger(); | ||
} | ||
}); | ||
} | ||
|
||
//Key listener | ||
|
||
@Override | ||
public void keyTyped(KeyEvent keyEvent) { } | ||
|
||
@Override | ||
public void keyPressed(KeyEvent keyEvent) { | ||
input.setKey(keyEvent.getKeyCode(), true); | ||
if(input.isKeyPressed(KeyEvent.VK_R)) { | ||
snipScopeWindow.resetZoom(); | ||
} | ||
snipScopeWindow.repaint(); | ||
} | ||
|
||
@Override | ||
public void keyReleased(KeyEvent keyEvent) { | ||
input.setKey(keyEvent.getKeyCode(), false); | ||
} | ||
|
||
//Mouse listener | ||
|
||
@Override | ||
public void mouseClicked(MouseEvent mouseEvent) { } | ||
|
||
@Override | ||
public void mousePressed(MouseEvent mouseEvent) { } | ||
|
||
@Override | ||
public void mouseReleased(MouseEvent mouseEvent) { | ||
lastPoint = null; | ||
} | ||
|
||
@Override | ||
public void mouseEntered(MouseEvent mouseEvent) { } | ||
|
||
@Override | ||
public void mouseExited(MouseEvent mouseEvent) { } | ||
|
||
//Mouse motion listener | ||
|
||
@Override | ||
public void mouseDragged(MouseEvent mouseEvent) { | ||
if (input.isKeyPressed(KeyEvent.VK_SPACE)) { | ||
if(lastPoint == null) lastPoint = mouseEvent.getPoint(); | ||
double x = lastPoint.getX() - mouseEvent.getPoint().getX(); | ||
double y = lastPoint.getY() - mouseEvent.getPoint().getY(); | ||
|
||
Vector2Int oldPos = snipScopeWindow.getPosition(); | ||
snipScopeWindow.setPosition(new Vector2Int(oldPos.x + x, oldPos.y +y)); | ||
|
||
lastPoint = mouseEvent.getPoint(); | ||
snipScopeWindow.repaint(); | ||
} | ||
} | ||
|
||
@Override | ||
public void mouseMoved(MouseEvent mouseEvent) { } | ||
|
||
//Mouse wheel listener | ||
|
||
@Override | ||
public void mouseWheelMoved(MouseWheelEvent mouseWheelEvent) { | ||
float oldZoom = snipScopeWindow.getZoom(); | ||
if(mouseWheelEvent.getWheelRotation() == -1) { | ||
snipScopeWindow.setZoom(oldZoom+0.1F); | ||
} else if(mouseWheelEvent.getWheelRotation() == 1) { | ||
if(oldZoom >= 0.2F) | ||
snipScopeWindow.setZoom(oldZoom-0.1F); | ||
} | ||
snipScopeWindow.calculateZoom(); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/io/wollinger/snipsniper/SnipScope/SnipScopeRenderer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package io.wollinger.snipsniper.SnipScope; | ||
|
||
import io.wollinger.snipsniper.utils.Vector2Int; | ||
|
||
import javax.swing.*; | ||
import java.awt.*; | ||
import java.awt.image.BufferedImage; | ||
|
||
public class SnipScopeRenderer extends JPanel { | ||
private SnipScopeWindow snipScopeWindow; | ||
|
||
public SnipScopeRenderer(SnipScopeWindow snipScopeWindow) { | ||
this.snipScopeWindow = snipScopeWindow; | ||
} | ||
|
||
public void paint(Graphics g) { | ||
Dimension optimalDimension = snipScopeWindow.getOptimalImageDimension(); | ||
BufferedImage image = snipScopeWindow.getImage(); | ||
if(image != null && optimalDimension != null) { | ||
int x = getWidth()/2 - (int)(optimalDimension.getWidth()/2); | ||
int y = getHeight()/2 - (int)(optimalDimension.getHeight()/2); | ||
|
||
x -= snipScopeWindow.getZoomOffset().x; | ||
y -= snipScopeWindow.getZoomOffset().y; | ||
|
||
Vector2Int posModifier = snipScopeWindow.getPosition(); | ||
x -= posModifier.x; | ||
y -= posModifier.y; | ||
|
||
float zoom = snipScopeWindow.getZoom(); | ||
g.drawImage(image, x, y, (int)(optimalDimension.getWidth()*zoom), (int)(optimalDimension.getHeight()*zoom), this); | ||
} | ||
} | ||
|
||
} |
141 changes: 141 additions & 0 deletions
141
src/main/java/io/wollinger/snipsniper/SnipScope/SnipScopeWindow.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package io.wollinger.snipsniper.SnipScope; | ||
|
||
import io.wollinger.snipsniper.Config; | ||
import io.wollinger.snipsniper.utils.InputContainer; | ||
import io.wollinger.snipsniper.utils.Utils; | ||
import io.wollinger.snipsniper.utils.Vector2Int; | ||
|
||
import javax.swing.*; | ||
import java.awt.*; | ||
import java.awt.image.BufferedImage; | ||
|
||
public class SnipScopeWindow extends JFrame { | ||
private Config config; | ||
private BufferedImage image; | ||
private String fileLocation; | ||
|
||
private final SnipScopeRenderer renderer; | ||
|
||
private Dimension optimalImageDimension; | ||
private Vector2Int position = new Vector2Int(0,0); | ||
private Vector2Int zoomOffset = new Vector2Int(0, 0); | ||
|
||
private float zoom = 1; | ||
private final InputContainer inputContainer = new InputContainer(); | ||
|
||
public SnipScopeWindow(Config config, BufferedImage image, String fileLocation) { | ||
this.config = config; | ||
this.image = image; | ||
this.fileLocation = fileLocation; | ||
|
||
SnipScopeListener listener = new SnipScopeListener(this); | ||
addKeyListener(listener); | ||
renderer = new SnipScopeRenderer(this); | ||
renderer.addMouseListener(listener); | ||
renderer.addMouseMotionListener(listener); | ||
renderer.addMouseWheelListener(listener); | ||
add(renderer); | ||
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); | ||
setVisible(true); | ||
|
||
Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize(); | ||
if(image.getWidth() >= screenDimension.getWidth() || image.getHeight() > screenDimension.getHeight()) { | ||
Dimension newDimension = Utils.getScaledDimension(image, screenDimension); | ||
setOptimalImageDimension(newDimension); | ||
setSize(newDimension); | ||
} else { | ||
Insets insets = getInsets(); | ||
setSize(insets.left + insets.right + image.getWidth(), insets.bottom + insets.top + image.getHeight()); | ||
setOptimalImageDimension(new Dimension(image.getWidth(), image.getHeight())); | ||
} | ||
|
||
Dimension dimension = getOptimalImageDimension(); | ||
setLocation((int)(screenDimension.getWidth()/2 - dimension.getWidth()/2), (int)(screenDimension.getHeight()/2 - dimension.getHeight()/2)); | ||
} | ||
|
||
public void calculateZoom() { | ||
int dimWidth = (int) getOptimalImageDimension().getWidth(); | ||
int dimHeight = (int) getOptimalImageDimension().getHeight(); | ||
|
||
int offsetX = dimWidth/2; | ||
int offsetY = dimHeight/2; | ||
|
||
int modX = (int)(offsetX * getZoom() - offsetX); | ||
int modY = (int)(offsetY * getZoom() - offsetY); | ||
zoomOffset = new Vector2Int(modX, modY); | ||
|
||
repaint(); | ||
} | ||
|
||
public Vector2Int getPointOnImage(Point point) { | ||
Dimension optimalDimension = getOptimalImageDimension(); | ||
double imageX = (double)renderer.getWidth()/2 - optimalDimension.getWidth()/2; | ||
double imageY = (double)renderer.getHeight()/2 - optimalDimension.getHeight()/2; | ||
|
||
imageX -= zoomOffset.x; | ||
imageY -= zoomOffset.y; | ||
|
||
imageX -= position.x; | ||
imageY -= position.y; | ||
|
||
double width = optimalDimension.getWidth() * zoom; | ||
double height = optimalDimension.getHeight() * zoom; | ||
|
||
double posOnImageX = (point.getX() - imageX) * ((double)image.getWidth()/width); | ||
double posOnImageY = (point.getY() - imageY) * ((double)image.getHeight()/ height); | ||
|
||
return new Vector2Int(posOnImageX, posOnImageY); | ||
} | ||
|
||
public void resetZoom() { | ||
setPosition(new Vector2Int()); | ||
setZoom(1); | ||
setZoomOffset(new Vector2Int()); | ||
} | ||
|
||
public void resizeTrigger() { | ||
setOptimalImageDimension(Utils.getScaledDimension(image, renderer.getSize())); | ||
calculateZoom(); | ||
} | ||
|
||
public Vector2Int getZoomOffset() { | ||
return zoomOffset; | ||
} | ||
|
||
public void setZoomOffset(Vector2Int vec2int) { | ||
zoomOffset = vec2int; | ||
} | ||
|
||
public BufferedImage getImage() { | ||
return image; | ||
} | ||
|
||
public Vector2Int getPosition() { | ||
return position; | ||
} | ||
|
||
public void setPosition(Vector2Int vec2int) { | ||
position = vec2int; | ||
} | ||
|
||
public float getZoom() { | ||
return zoom; | ||
} | ||
|
||
public void setZoom(float zoom) { | ||
this.zoom = zoom; | ||
} | ||
|
||
public Dimension getOptimalImageDimension() { | ||
return optimalImageDimension; | ||
} | ||
|
||
public void setOptimalImageDimension(Dimension dimension) { | ||
optimalImageDimension = dimension; | ||
} | ||
|
||
public InputContainer getInputContainer() { | ||
return inputContainer; | ||
} | ||
|
||
} |