Skip to content
This repository has been archived by the owner on Aug 6, 2024. It is now read-only.

Commit

Permalink
feat: vnc connection rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
ajh123 committed Feb 10, 2024
1 parent 7eb2a57 commit ed814b3
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 7 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ dependencies {
// modImplementation "net.fabricmc.fabric-api:fabric-api-deprecated:${project.fabric_version}"
modImplementation include("io.github.cottonmc:LibGui:8.1.1+1.20.1")
implementation 'com.shinyhut:vernacular:1.14'
implementation 'org.imgscalr:imgscalr-lib:4.2'
}

processResources {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package uk.minersonline.RealisticComputers.screen;

import com.shinyhut.vernacular.client.VernacularClient;
import com.shinyhut.vernacular.client.VernacularConfig;
import com.shinyhut.vernacular.client.rendering.ColorDepth;
import io.github.cottonmc.cotton.gui.client.LightweightGuiDescription;
import io.github.cottonmc.cotton.gui.widget.WButton;
import io.github.cottonmc.cotton.gui.widget.WGridPanel;
import io.github.cottonmc.cotton.gui.widget.WLabel;
import io.github.cottonmc.cotton.gui.widget.WSprite;
import io.github.cottonmc.cotton.gui.widget.*;
import io.github.cottonmc.cotton.gui.widget.data.Insets;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.tooltip.Tooltip;
Expand All @@ -13,16 +13,29 @@
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;

import java.awt.*;
import java.awt.image.BufferedImage;

public class VNCScreen extends LightweightGuiDescription {
private final VernacularConfig config = new VernacularConfig();
private final VernacularClient client = new VernacularClient(config);

public VNCScreen(Screen parent, BlockPos terminalPosition) {
WGridPanel root = new WGridPanel();
setRootPanel(root);
root.setSize(256, 176);
root.setSize(272, 176);
root.setInsets(Insets.ROOT_PANEL);

WImageWidget image = new WImageWidget();
root.add(image, 0, 0, 16, 12);
root.add(image, 0, 0, 18, 12);

root.validate(this);

config.setColorDepth(ColorDepth.BPP_24_TRUE);
config.setEnableCopyrectEncoding(true);

config.setScreenUpdateListener(image::setImage);

client.start("10.0.0.97", 5900);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,83 @@

import io.github.cottonmc.cotton.gui.client.ScreenDrawing;
import io.github.cottonmc.cotton.gui.widget.WWidget;
import io.github.cottonmc.cotton.gui.widget.data.Texture;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.texture.NativeImage;
import net.minecraft.client.texture.NativeImageBackedTexture;
import net.minecraft.util.Identifier;
import net.minecraft.util.crash.CrashReport;
import net.minecraft.util.math.ColorHelper;
import uk.minersonline.RealisticComputers.RealisticComputers;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import static uk.minersonline.RealisticComputers.RealisticComputers.MOD_ID;

public class WImageWidget extends WWidget {
private BufferedImage image = null;

@Override
public void paint(DrawContext context, int x, int y, int mouseX, int mouseY) {
int color = ColorHelper.Argb.getArgb(255, 0, 0, 0);
ScreenDrawing.coloredRect(context, x, y, width, height, color);
if (image != null) {
Identifier id = makeImage(image);
ScreenDrawing.texturedRect(context, x, y, getWidth(), getHeight(), new Texture(id), 0xFFFFFFFF);
MinecraftClient.getInstance().getTextureManager().destroyTexture(id);
} else {
ScreenDrawing.coloredRect(context, x, y, width, height, color);
}
}

@Override
public boolean canResize() {
return true;
}

public void setImage(Image image) {
if (this.image != null) {
this.image.flush();
}
this.image = (BufferedImage) image;
}

public Identifier makeImage(Image awtImage) {
try {
// Convert AWT image to BufferedImage
BufferedImage bufferedImage = new BufferedImage(awtImage.getWidth(null), awtImage.getHeight(null), BufferedImage.TYPE_INT_ARGB);
bufferedImage.getGraphics().drawImage(awtImage, 0, 0, null);

// Convert BufferedImage to byte array
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "png", byteArrayOutputStream);
byte[] imageData = byteArrayOutputStream.toByteArray();

// Convert byte array to InputStream
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(imageData);

// Read InputStream into NativeImage
NativeImage nativeImage = NativeImage.read(byteArrayInputStream);

// Create a NativeImageBackedTexture
NativeImageBackedTexture texture = new NativeImageBackedTexture(nativeImage);

// Get Minecraft client instance
MinecraftClient client = MinecraftClient.getInstance();

// Return the image.
return client.getTextureManager().registerDynamicTexture(MOD_ID+".image", texture);
} catch (IOException e) {
CrashReport.create(
e,
"An unreachable error occurred whilst rendering screen, in normal circumstances this should not happen."
);
}
return null;
}
}

0 comments on commit ed814b3

Please sign in to comment.