Skip to content

Commit

Permalink
Rename "Return" to "Close", add new CHM controls
Browse files Browse the repository at this point in the history
Some of these might come to other mods in the future, not sure
  • Loading branch information
Eiim committed Jan 9, 2024
1 parent 7eed5fe commit 205cf52
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 30 deletions.
100 changes: 76 additions & 24 deletions src/main/java/chokistream/ChirunoModClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,19 @@ public class ChirunoModClient implements StreamingInterface {
private InputStream in = null;
private OutputStream out = null;
private ColorMode colorMode;
public int quality;
private BufferedImage lastTopImage;
private BufferedImage lastBottomImage;

private int topFrames;
private int bottomFrames;

// Remember settings for future use
private int quality;
private int cpuLimit;
private boolean tga;
private boolean interlace;
private DSScreenBoth screen;

private static final int FORMAT_MASK = 0b00000111;
private static final int TGA_MASK = 0b00001000;
private static final int SCREEN_MASK = 0b00010000;
Expand All @@ -57,14 +63,19 @@ public class ChirunoModClient implements StreamingInterface {
*/
public ChirunoModClient(String host, int quality, boolean reqTGA, boolean interlace, int capCPU, ColorMode receivedColorMode,
int port, DSScreenBoth reqScreen) throws UnknownHostException, IOException {
this.quality = quality;
this.cpuLimit = capCPU;
this.tga = reqTGA;
this.screen = reqScreen;
this.interlace = interlace;

// Connect to TCP port and set up client
client = new Socket(host, port);
client.setTcpNoDelay(true);
in = client.getInputStream();
out = client.getOutputStream();

this.colorMode = receivedColorMode;
this.quality = quality;

lastTopImage = new BufferedImage(400, 240, BufferedImage.TYPE_INT_RGB);
lastBottomImage = new BufferedImage(320, 240, BufferedImage.TYPE_INT_RGB);
Expand Down Expand Up @@ -93,28 +104,6 @@ public void sendQuality(int quality) throws IOException {
out.write((new Packet((byte)0x04, (byte)0x01, new byte[] {(byte)quality})).pack);
}

// Increase quality by a certain amount, up to 100
public void increaseQuality(int delta) throws IOException {
if(quality + delta < 100) {
quality = quality + delta;
sendQuality(quality);
} else if(quality < 100) {
quality = 100;
sendQuality(100);
}
}

// Decrease quality by a certain amount, down to 0
public void decreaseQuality(int delta) throws IOException {
if(quality - delta > 0) {
quality = quality - delta;
sendQuality(quality);
} else if(quality > 0) {
quality = 0;
sendQuality(0);
}
}

public void sendScreen(DSScreenBoth screen) throws IOException {
logger.log("Sending screen packet of "+screen.getLongName(), LogLevel.VERBOSE);
byte scr = switch(screen) {
Expand Down Expand Up @@ -152,6 +141,69 @@ public void sendDebug(byte[] debugData) throws IOException {
logger.log("Sending debug packet", LogLevel.VERBOSE);
out.write((new Packet((byte)0xFF, (byte)0x00, debugData)).pack);
}

// Increase quality by a certain amount, up to 100
public void increaseQuality(int delta) throws IOException {
if(quality + delta < 100) {
quality = quality + delta;
sendQuality(quality);
} else if(quality < 100) {
quality = 100;
sendQuality(100);
}
}

// Decrease quality by a certain amount, down to 1
public void decreaseQuality(int delta) throws IOException {
if(quality - delta > 1) {
quality = quality - delta;
sendQuality(quality);
} else if(quality > 1) {
quality = 1;
sendQuality(1);
}
}

// Increase CPU cap by a certain amount, up to 100
public void increaseCPU(int delta) throws IOException {
if(cpuLimit + delta < 100) {
cpuLimit = cpuLimit + delta;
sendLimitCPU(cpuLimit);
} else if(cpuLimit < 100) {
cpuLimit = 100;
sendLimitCPU(100);
}
}

// Decrease CPU cap by a certain amount, down to 0
public void decreaseCPU(int delta) throws IOException {
if(cpuLimit - delta > 0) {
cpuLimit = cpuLimit - delta;
sendLimitCPU(cpuLimit);
} else if(cpuLimit > 0) {
cpuLimit = 0;
sendLimitCPU(0);
}
}

public void toggleTGA() throws IOException {
tga = !tga;
sendImageType(tga);
}

public void toggleInterlacing() throws IOException {
interlace = !interlace;
sendInterlace(interlace);
}

public void switchScreen() throws IOException {
screen = switch(screen) {
case TOP -> DSScreenBoth.BOTTOM;
case BOTTOM -> DSScreenBoth.BOTH;
case BOTH -> DSScreenBoth.TOP;
};
sendScreen(screen);
}

@Override
public void close() throws IOException {
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/chokistream/KeypressHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void keyPressed(KeyEvent e) {
// Generic commands
if(ck.get(Controls.SCREENSHOT).matches(e)) {
output.screenshot();
} else if(ck.get(Controls.RETURN).matches(e)) {
} else if(ck.get(Controls.CLOSE).matches(e)) {
output.kill();
}

Expand All @@ -44,6 +44,16 @@ public void keyPressed(KeyEvent e) {
c.increaseQuality(1);
} else if(ck.get(Controls.QUALITY_DOWN).matches(e)) {
c.decreaseQuality(1);
} else if(ck.get(Controls.CPU_UP).matches(e)) {
c.increaseCPU(1);
} else if(ck.get(Controls.CPU_DOWN).matches(e)) {
c.decreaseCPU(1);
} else if(ck.get(Controls.TGA).matches(e)) {
c.toggleTGA();
} else if(ck.get(Controls.REQ_SCREEN).matches(e)) {
c.switchScreen();
} else if(ck.get(Controls.INTERLACE).matches(e)) {
c.toggleInterlacing();
}
}
} catch(IOException e1) {
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/chokistream/SwingGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -599,18 +599,24 @@ public void createControls() {
add(header, p, c, 0, 0, 2, 1);

add(new JLabel(Controls.SCREENSHOT.getLongName()), p, c, 0, 1);
add(new JLabel(Controls.RETURN.getLongName()), p, c, 0, 2);
add(new JLabel(Controls.CLOSE.getLongName()), p, c, 0, 2);
add(new JLabel(Controls.QUALITY_UP.getLongName()), p, c, 0, 3);
add(new JLabel(Controls.QUALITY_DOWN.getLongName()), p, c, 0, 4);
add(new JLabel(Controls.REQ_SCREEN.getLongName()), p, c, 0, 5);
add(new JLabel(Controls.TGA.getLongName()), p, c, 0, 6);
add(new JLabel(Controls.INTERLACE.getLongName()), p, c, 0, 7);

// Temporary, for layout
controlsFields.put(Controls.SCREENSHOT, add(new JTextField(), p, c, 1, 1));
controlsFields.put(Controls.RETURN, add(new JTextField(), p, c, 1, 2));
controlsFields.put(Controls.CLOSE, add(new JTextField(), p, c, 1, 2));
controlsFields.put(Controls.QUALITY_UP, add(new JTextField(), p, c, 1, 3));
controlsFields.put(Controls.QUALITY_DOWN, add(new JTextField(), p, c, 1, 4));
controlsFields.put(Controls.REQ_SCREEN, add(new JTextField(), p, c, 1, 5));
controlsFields.put(Controls.TGA, add(new JTextField(), p, c, 1, 6));
controlsFields.put(Controls.INTERLACE, add(new JTextField(), p, c, 1, 7));

JButton apply = new JButton("Apply");
add(apply, p, c, 0, 5, 2, 1);
add(apply, p, c, 0, 8, 2, 1);
apply.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/chokistream/props/Controls.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@
public enum Controls {

SCREENSHOT("screenshot", "Screenshot", new Input(KeyEvent.VK_S)),
RETURN("return", "Return", new Input(KeyEvent.VK_BACK_SPACE)),
CLOSE("close", "Close", new Input(KeyEvent.VK_BACK_SPACE)),
QUALITY_UP("quality_up", "Increase Quality", new Input(KeyEvent.VK_UP)),
QUALITY_DOWN("quality_down", "Decrease Quality", new Input(KeyEvent.VK_DOWN));
QUALITY_DOWN("quality_down", "Decrease Quality", new Input(KeyEvent.VK_DOWN)),
CPU_UP("cpu_up", "Increase CPU Cap", new Input(KeyEvent.VK_BRACERIGHT)),
CPU_DOWN("cpu_down", "Decrease CPU Cap", new Input(KeyEvent.VK_BRACELEFT)),
REQ_SCREEN("req_screen","Switch requested screen", new Input(KeyEvent.VK_R)),
INTERLACE("interlace","Toggle interlacing", new Input(KeyEvent.VK_I)),
TGA("tga","Toggle TGA", new Input(KeyEvent.VK_T));

private String shortName;
private String longName;
Expand Down

0 comments on commit 205cf52

Please sign in to comment.