From 9ddead67c29b1c1bd529c1dccbdfc5907c737b88 Mon Sep 17 00:00:00 2001 From: XyperCode Date: Sat, 3 Feb 2024 06:43:02 +0100 Subject: [PATCH] Adding display resolutions! --- .../devices/api/video/CustomResolution.java | 6 + .../ultreon/devices/api/video/VideoInfo.java | 43 +++++ .../java/com/ultreon/devices/core/Laptop.java | 160 ++++++++++++------ .../com/ultreon/devices/core/Resizer.java | 91 ++++++++++ .../com/ultreon/devices/core/TaskBar.java | 12 +- .../java/com/ultreon/devices/core/Window.java | 17 +- .../programs/system/DisplayResolution.java | 27 +++ .../programs/system/PredefinedResolution.java | 56 ++++++ .../devices/programs/system/SettingsApp.java | 16 ++ 9 files changed, 363 insertions(+), 65 deletions(-) create mode 100644 common/src/main/java/com/ultreon/devices/api/video/CustomResolution.java create mode 100644 common/src/main/java/com/ultreon/devices/api/video/VideoInfo.java create mode 100644 common/src/main/java/com/ultreon/devices/core/Resizer.java create mode 100644 common/src/main/java/com/ultreon/devices/programs/system/DisplayResolution.java create mode 100644 common/src/main/java/com/ultreon/devices/programs/system/PredefinedResolution.java diff --git a/common/src/main/java/com/ultreon/devices/api/video/CustomResolution.java b/common/src/main/java/com/ultreon/devices/api/video/CustomResolution.java new file mode 100644 index 00000000..f76407db --- /dev/null +++ b/common/src/main/java/com/ultreon/devices/api/video/CustomResolution.java @@ -0,0 +1,6 @@ +package com.ultreon.devices.api.video; + +import com.ultreon.devices.programs.system.DisplayResolution; + +public record CustomResolution(int width, int height) implements DisplayResolution { +} diff --git a/common/src/main/java/com/ultreon/devices/api/video/VideoInfo.java b/common/src/main/java/com/ultreon/devices/api/video/VideoInfo.java new file mode 100644 index 00000000..60e899a8 --- /dev/null +++ b/common/src/main/java/com/ultreon/devices/api/video/VideoInfo.java @@ -0,0 +1,43 @@ +package com.ultreon.devices.api.video; + +import com.mojang.blaze3d.platform.Window; +import com.ultreon.devices.core.Laptop; +import com.ultreon.devices.programs.system.DisplayResolution; +import com.ultreon.devices.programs.system.PredefinedResolution; +import net.minecraft.client.Minecraft; +import net.minecraft.nbt.CompoundTag; + +import java.util.Arrays; +import java.util.Collection; + +public class VideoInfo { + private DisplayResolution resolution = PredefinedResolution.PREDEFINED_384x216; + + public VideoInfo(CompoundTag videoInfoData) { + if (videoInfoData.contains("resolution")) + resolution = DisplayResolution.load(videoInfoData.getCompound("resolution")); + } + + public Collection getResolutionList() { + Window window = Minecraft.getInstance().getWindow(); + return Arrays.stream(PredefinedResolution.values()) + .filter(r -> r.width() <= window.getGuiScaledWidth() && r.height() <= window.getGuiScaledHeight()) + .toList(); + } + + public void setResolution(DisplayResolution value) { + this.resolution = value; + + Laptop.getInstance().revalidateDisplay(); + } + + public DisplayResolution getResolution() { + return resolution; + } + + public void save(CompoundTag tag) { + if (resolution != null) { + resolution.save(tag); + } + } +} diff --git a/common/src/main/java/com/ultreon/devices/core/Laptop.java b/common/src/main/java/com/ultreon/devices/core/Laptop.java index 9c9c04e5..d2d17035 100644 --- a/common/src/main/java/com/ultreon/devices/core/Laptop.java +++ b/common/src/main/java/com/ultreon/devices/core/Laptop.java @@ -15,10 +15,14 @@ import com.ultreon.devices.api.task.Task; import com.ultreon.devices.api.task.TaskManager; import com.ultreon.devices.api.utils.OnlineRequest; +import com.ultreon.devices.api.video.CustomResolution; +import com.ultreon.devices.api.video.VideoInfo; import com.ultreon.devices.block.entity.ComputerBlockEntity; import com.ultreon.devices.core.task.TaskInstallApp; import com.ultreon.devices.object.AppInfo; import com.ultreon.devices.programs.system.DiagnosticsApp; +import com.ultreon.devices.programs.system.DisplayResolution; +import com.ultreon.devices.programs.system.PredefinedResolution; import com.ultreon.devices.programs.system.SystemApp; import com.ultreon.devices.programs.system.component.FileBrowser; import com.ultreon.devices.programs.system.task.TaskUpdateApplicationData; @@ -30,7 +34,6 @@ import it.unimi.dsi.fastutil.ints.IntArraySet; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.Font; -import net.minecraft.client.gui.Gui; import net.minecraft.client.gui.GuiGraphics; import net.minecraft.client.gui.screens.Screen; import net.minecraft.client.resources.sounds.SimpleSoundInstance; @@ -75,6 +78,7 @@ public class Laptop extends Screen implements System { private static Laptop instance; private Double dragWindowFromX; private Double dragWindowFromY; + private VideoInfo videoInfo; @PlatformOnly("fabric") public static List getApplicationsForFabric() { @@ -88,10 +92,6 @@ public static List getWallpapers() { private static final List WALLPAPERS = new ArrayList<>(); private static final int BORDER = 10; - private static final int DEVICE_WIDTH = 384; - static final int SCREEN_WIDTH = DEVICE_WIDTH - BORDER * 2; - private static final int DEVICE_HEIGHT = 216; - static final int SCREEN_HEIGHT = DEVICE_HEIGHT - BORDER * 2; private static final List tasks = new CopyOnWriteArrayList<>(); private static System system; private static BlockPos pos; @@ -107,7 +107,7 @@ public static List getWallpapers() { private int lastMouseX, lastMouseY; private boolean dragging = false; private final IntArraySet pressed = new IntArraySet(); - private final com.ultreon.devices.api.app.component.Image wallpaper; + private final Image wallpaper; private final Layout wallpaperLayout; private BSOD bsod; @@ -141,6 +141,9 @@ public Laptop(ComputerBlockEntity laptop, boolean worldLess) { this.appData = laptop.getApplicationData(); this.systemData = laptop.getSystemData(); + CompoundTag videoInfoData = this.systemData.getCompound("videoInfo"); + this.videoInfo = new VideoInfo(videoInfoData); + // Windows this.windows = new CopyOnWriteArrayList<>() { @Override @@ -172,8 +175,8 @@ public boolean add(Window window) { if (this.currentWallpaper == null) this.currentWallpaper = new Wallpaper(0); Laptop.system = this; Laptop.pos = laptop.getBlockPos(); - this.wallpaperLayout = new Layout(SCREEN_WIDTH, SCREEN_HEIGHT); - this.wallpaper = new com.ultreon.devices.api.app.component.Image(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT); + this.wallpaperLayout = new Layout(getScreenWidth(), getScreenHeight()); + this.wallpaper = new Image(0, 0, getScreenWidth(), getScreenHeight()); if (currentWallpaper.isBuiltIn()) { wallpaper.setImage(WALLPAPERS.get(currentWallpaper.location)); } else { @@ -190,6 +193,18 @@ public static Laptop getInstance() { return instance; } + public static int getScreenWidth() { + return instance.videoInfo.getResolution().width(); + } + + public static int getScreenHeight() { + return instance.videoInfo.getResolution().height(); + } + + public static DisplayResolution getResolution() { + return instance.videoInfo.getResolution(); + } + public CompoundTag getModSystemTag(Mod mod) { return getModSystemTag(mod.getModId()); } @@ -257,9 +272,9 @@ public static void runLater(Runnable task) { */ @Override public void init() { - int posX = (width - DEVICE_WIDTH) / 2; - int posY = (height - DEVICE_HEIGHT) / 2; - bar.init(posX + BORDER, posY + DEVICE_HEIGHT - 28); + int posX = (width - getDeviceWidth()) / 2; + int posY = (height - getDeviceHeight()) / 2; + bar.init(posX + BORDER, posY + getDeviceHeight() - 28); installedApps.clear(); ListTag list = systemData.getList("InstalledApps", Tag.TAG_STRING); @@ -275,6 +290,14 @@ public void init() { } } + private static int getDeviceWidth() { + return getScreenWidth() + 2 * BORDER; + } + + private static int getDeviceHeight() { + return getScreenHeight() + 2 * BORDER; + } + @Override public void removed() { /* Close all windows and sendTask application data */ @@ -316,6 +339,21 @@ private void updateSystemData() { @Override public void resize(@NotNull Minecraft minecraft, int width, int height) { super.resize(minecraft, width, height); + + if (videoInfo.getResolution().width() > width || videoInfo.getResolution().height() > height) { + videoInfo.setResolution(new CustomResolution(width, height)); + } + + revalidateDisplay(); + } + + public void revalidateDisplay() { + wallpaper.componentWidth = videoInfo.getResolution().width(); + wallpaper.componentHeight = videoInfo.getResolution().height(); + wallpaperLayout.width = videoInfo.getResolution().width(); + wallpaperLayout.height = videoInfo.getResolution().height(); + wallpaperLayout.updateComponents(0, 0); + for (var window : windows) { if (window != null) { window.content.markForLayoutUpdate(); @@ -376,9 +414,9 @@ public void render(final @NotNull GuiGraphics graphics, final int mouseX, final public void renderBsod(final @NotNull GuiGraphics graphics, final int mouseX, final int mouseY, float partialTicks) { renderBezels(graphics, mouseX, mouseY, partialTicks); - int posX = (width - DEVICE_WIDTH) / 2; - int posY = (height - DEVICE_HEIGHT) / 2; - graphics.fill(posX+10, posY+10, posX + DEVICE_WIDTH-10, posY + DEVICE_HEIGHT-10, new Color(0, 0, 255).getRGB()); + int posX = (width - getDeviceWidth()) / 2; + int posY = (height - getDeviceHeight()) / 2; + graphics.fill(posX+10, posY+10, posX + getDeviceWidth()-10, posY + getDeviceHeight()-10, new Color(0, 0, 255).getRGB()); var bo = new ByteArrayOutputStream(); double scale = Minecraft.getInstance().getWindow().getGuiScale(); @@ -386,7 +424,7 @@ public void renderBsod(final @NotNull GuiGraphics graphics, final int mouseX, fi var b = new PrintStream(bo); bsod.throwable.printStackTrace(b); var str = bo.toString(); - drawLines(graphics, Laptop.getFont(), str, posX+10, posY+10+getFont().lineHeight*2, (int) ((DEVICE_WIDTH - 10) * scale), new Color(255, 255, 255).getRGB()); + drawLines(graphics, Laptop.getFont(), str, posX+10, posY+10+getFont().lineHeight*2, (int) ((getDeviceWidth() - 10) * scale), new Color(255, 255, 255).getRGB()); graphics.pose().pushPose(); graphics.pose().scale(2, 2, 0); graphics.pose().translate((posX+10)/2f,(posY+10)/2f,0); @@ -398,7 +436,7 @@ public static void drawLines(GuiGraphics graphics, Font font, String text, int x var lines = new ArrayList(); font.getSplitter().splitLines(FormattedText.of(text.replaceAll("\r\n", "\n").replaceAll("\r", "\n")), width, Style.EMPTY).forEach(b -> lines.add(b.getString())); var totalTextHeight = font.lineHeight*lines.size(); - var textScale = (DEVICE_HEIGHT-20-(getFont().lineHeight*2))/(float)totalTextHeight; + var textScale = (instance.videoInfo.getResolution().height()-20-(getFont().lineHeight*2))/(float)totalTextHeight; textScale = (float) (1f / Minecraft.getInstance().getWindow().getGuiScale()); textScale = Math.max(0.5f, textScale); graphics.pose().pushPose(); @@ -424,23 +462,25 @@ public void renderBezels(final @NotNull GuiGraphics graphics, final int mouseX, //*************************// // Physical Screen // //*************************// - int posX = (width - DEVICE_WIDTH) / 2; - int posY = (height - DEVICE_HEIGHT) / 2; + int deviceWidth = videoInfo.getResolution().width() + BORDER * 2; + int deviceHeight = videoInfo.getResolution().height() + BORDER * 2; + int posX = (width - deviceWidth) / 2; + int posY = (height - deviceHeight) / 2; // Corners graphics.blit(LAPTOP_GUI, posX, posY, 0, 0, BORDER, BORDER); // TOP-LEFT - graphics.blit(LAPTOP_GUI, posX + DEVICE_WIDTH - BORDER, posY, 11, 0, BORDER, BORDER); // TOP-RIGHT - graphics.blit(LAPTOP_GUI, posX + DEVICE_WIDTH - BORDER, posY + DEVICE_HEIGHT - BORDER, 11, 11, BORDER, BORDER); // BOTTOM-RIGHT - graphics.blit(LAPTOP_GUI, posX, posY + DEVICE_HEIGHT - BORDER, 0, 11, BORDER, BORDER); // BOTTOM-LEFT + graphics.blit(LAPTOP_GUI, posX + deviceWidth - BORDER, posY, 11, 0, BORDER, BORDER); // TOP-RIGHT + graphics.blit(LAPTOP_GUI, posX + deviceWidth - BORDER, posY + deviceHeight - BORDER, 11, 11, BORDER, BORDER); // BOTTOM-RIGHT + graphics.blit(LAPTOP_GUI, posX, posY + deviceHeight - BORDER, 0, 11, BORDER, BORDER); // BOTTOM-LEFT // Edges - graphics.blit(LAPTOP_GUI, posX + BORDER, posY, SCREEN_WIDTH, BORDER, 10, 0, 1, BORDER, 256, 256); // TOP - graphics.blit(LAPTOP_GUI, posX + DEVICE_WIDTH - BORDER, posY + BORDER, BORDER, SCREEN_HEIGHT, 11, 10, BORDER, 1, 256, 256); // RIGHT - graphics.blit(LAPTOP_GUI, posX + BORDER, posY + DEVICE_HEIGHT - BORDER, SCREEN_WIDTH, BORDER, 10, 11, 1, BORDER, 256, 256); // BOTTOM - graphics.blit(LAPTOP_GUI, posX, posY + BORDER, BORDER, SCREEN_HEIGHT, 0, 11, BORDER, 1, 256, 256); // LEFT + graphics.blit(LAPTOP_GUI, posX + BORDER, posY, getScreenWidth(), BORDER, 10, 0, 1, BORDER, 256, 256); // TOP + graphics.blit(LAPTOP_GUI, posX + deviceWidth - BORDER, posY + BORDER, BORDER, getScreenHeight(), 11, 10, BORDER, 1, 256, 256); // RIGHT + graphics.blit(LAPTOP_GUI, posX + BORDER, posY + deviceHeight - BORDER, getScreenWidth(), BORDER, 10, 11, 1, BORDER, 256, 256); // BOTTOM + graphics.blit(LAPTOP_GUI, posX, posY + BORDER, BORDER, getScreenHeight(), 0, 11, BORDER, 1, 256, 256); // LEFT // Center - graphics.blit(LAPTOP_GUI, posX + BORDER, posY + BORDER, SCREEN_WIDTH, SCREEN_HEIGHT, 10, 10, 1, 1, 256, 256); + graphics.blit(LAPTOP_GUI, posX + BORDER, posY + BORDER, getScreenWidth(), getScreenHeight(), 10, 10, 1, 1, 256, 256); } @@ -453,14 +493,17 @@ public void renderBezels(final @NotNull GuiGraphics graphics, final int mouseX, * @param partialTicks the rendering partial ticks that forge give use (which is useless here). */ public void renderLaptop(final @NotNull GuiGraphics graphics, final int mouseX, final int mouseY, float partialTicks) { - int posX = (width - DEVICE_WIDTH) / 2; - int posY = (height - DEVICE_HEIGHT) / 2; + int posX = (width - getDeviceWidth()) / 2; + int posY = (height - getDeviceHeight()) / 2; // Fixes the strange partialTicks that Forge decided to give us final float frameTime = Minecraft.getInstance().getFrameTime(); for (Runnable task : tasks) { task.run(); } + renderBezels(graphics, mouseX, mouseY, partialTicks); + + GLHelper.pushScissor(posX, posY, videoInfo.getResolution().width() + BORDER, videoInfo.getResolution().height() + BORDER); //*******************// // Wallpaper // //*******************// @@ -507,21 +550,22 @@ public void renderLaptop(final @NotNull GuiGraphics graphics, final int mouseX, closeApplication(app); } } - graphics.pose().translate(0, 0, 100); + graphics.pose().translate(0, 0, 400); } } } - graphics.pose().popPose(); - - //****************************// - // Render the Application Bar // - //****************************// - bar.render(graphics, this, minecraft, posX + 10, posY + DEVICE_HEIGHT - 28, mouseX, mouseY, frameTime); + bar.render(graphics, this, minecraft, posX + 10, posY + getDeviceHeight() - 28, mouseX, mouseY, frameTime); + graphics.pose().translate(0, 0, 100); if (context != null) { context.render(graphics, this, minecraft, context.xPosition, context.yPosition, mouseX, mouseY, true, frameTime); } + graphics.pose().popPose(); + + //****************************// + // Render the Application Bar // + //****************************// Image.CACHE.entrySet().removeIf(entry -> { Image.CachedImage cachedImage = entry.getValue(); if (cachedImage.isDynamic() && cachedImage.isPendingDeletion()) { @@ -535,6 +579,7 @@ public void renderLaptop(final @NotNull GuiGraphics graphics, final int mouseX, }); super.render(graphics, mouseX, mouseY, frameTime); + GLHelper.popScissor(); GLHelper.clearScissorStack(); } @@ -558,6 +603,21 @@ private void bsod(Throwable e) { this.bsod = new BSOD(e); e.printStackTrace(); } + + public VideoInfo getVideoInfo() { + return videoInfo; + } + + public void setDisplayResolution(PredefinedResolution newValue) { + if (this.videoInfo != null) { + this.videoInfo.setResolution(newValue); + } + } + + public void revalidate() { + + } + private static final class BSOD { private final Throwable throwable; public BSOD(Throwable e) { @@ -569,8 +629,8 @@ public boolean mouseClickedInternal(double mouseX, double mouseY, int mouseButto this.lastMouseX = (int) mouseX; this.lastMouseY = (int) mouseY; - int posX = (width - SCREEN_WIDTH) / 2; - int posY = (height - SCREEN_HEIGHT) / 2; + int posX = (width - getScreenWidth()) / 2; + int posY = (height - getScreenHeight()) / 2; if (this.context != null) { int dropdownX = context.xPosition; @@ -583,7 +643,7 @@ public boolean mouseClickedInternal(double mouseX, double mouseY, int mouseButto } } - this.bar.handleClick(this, posX, posY + SCREEN_HEIGHT - TaskBar.BAR_HEIGHT, (int) mouseX, (int) mouseY, mouseButton); + this.bar.handleClick(this, posX, posY + getScreenHeight() - TaskBar.BAR_HEIGHT, (int) mouseX, (int) mouseY, mouseButton); for (int i = 0; i < windows.size(); i++) { Window window = (Window) windows.get(i); @@ -733,8 +793,8 @@ public boolean keyReleased(int keyCode, int scanCode, int modifiers) { @SuppressWarnings("unchecked") @Override public boolean mouseDragged(double mouseX, double mouseY, int button, double dragX, double dragY) { - int posX = (width - SCREEN_WIDTH) / 2; - int posY = (height - SCREEN_HEIGHT) / 2; + int posX = (width - getScreenWidth()) / 2; + int posY = (height - getScreenHeight()) / 2; try { if (this.context != null) { @@ -850,7 +910,7 @@ private Application openApplication(Application app, CompoundTag intent) { } Window window = new Window<>(app, this); - window.init((width - SCREEN_WIDTH) / 2, (height - SCREEN_HEIGHT) / 2, intent); + window.init((width - getScreenWidth()) / 2, (height - getScreenHeight()) / 2, intent); if (appData.contains(app.getInfo().getFormattedId())) { app.load(appData.getCompound(app.getInfo().getFormattedId())); @@ -966,28 +1026,28 @@ private boolean hasReachedWindowLimit() { } private boolean isMouseOnScreen(int mouseX, int mouseY) { - int posX = (width - SCREEN_WIDTH) / 2; - int posY = (height - SCREEN_HEIGHT) / 2; - return isMouseInside(mouseX, mouseY, posX, posY, posX + SCREEN_WIDTH, posY + SCREEN_HEIGHT); + int posX = (width - getScreenWidth()) / 2; + int posY = (height - getScreenHeight()) / 2; + return isMouseInside(mouseX, mouseY, posX, posY, posX + getScreenWidth(), posY + getScreenHeight()); } private boolean isMouseWithinWindowBar(int mouseX, int mouseY, Window window) { if (window == null) return false; - int posX = (width - SCREEN_WIDTH) / 2; - int posY = (height - SCREEN_HEIGHT) / 2; + int posX = (width - getScreenWidth()) / 2; + int posY = (height - getScreenHeight()) / 2; return isMouseInside(mouseX, mouseY, posX + window.offsetX + 1, posY + window.offsetY + 1, posX + window.offsetX + window.width - 13, posY + window.offsetY + 11); } private boolean isMouseWithinWindow(int mouseX, int mouseY, Window window) { if (window == null) return false; - int posX = (width - SCREEN_WIDTH) / 2; - int posY = (height - SCREEN_HEIGHT) / 2; + int posX = (width - getScreenWidth()) / 2; + int posY = (height - getScreenHeight()) / 2; return isMouseInside(mouseX, mouseY, posX + window.offsetX, posY + window.offsetY, posX + window.offsetX + window.width, posY + window.offsetY + window.height); } public boolean isMouseWithinApp(int mouseX, int mouseY, Window window) { - int posX = (width - SCREEN_WIDTH) / 2; - int posY = (height - SCREEN_HEIGHT) / 2; + int posX = (width - getScreenWidth()) / 2; + int posY = (height - getScreenHeight()) / 2; return isMouseInside(mouseX, mouseY, posX + window.offsetX + 1, posY + window.offsetY + 13, posX + window.offsetX + window.width - 1, posY + window.offsetY + window.height - 1); } diff --git a/common/src/main/java/com/ultreon/devices/core/Resizer.java b/common/src/main/java/com/ultreon/devices/core/Resizer.java new file mode 100644 index 00000000..26cefea7 --- /dev/null +++ b/common/src/main/java/com/ultreon/devices/core/Resizer.java @@ -0,0 +1,91 @@ +package com.ultreon.devices.core; + +import org.joml.Vector2f; + +class Resizer { + private final float ratio; + private final float relativeRatio; + private final Orientation orientation; + private final float sourceWidth; + private final float sourceHeight; + + public Resizer(float srcWidth, float srcHeight) { + this.ratio = srcWidth / srcHeight; + + if (srcWidth > srcHeight) { + this.relativeRatio = srcWidth / srcHeight; + this.orientation = Orientation.LANDSCAPE; + } else if (srcWidth < srcHeight) { + this.relativeRatio = srcHeight / srcWidth; + this.orientation = Orientation.PORTRAIT; + } else { + this.relativeRatio = 1; + this.orientation = Orientation.SQUARE; + } + + this.sourceWidth = srcWidth; + this.sourceHeight = srcHeight; + } + + public Vector2f thumbnail(float maxWidth, float maxHeight) { + float aspectRatio; + float width; + float height; + + if (this.sourceWidth < this.sourceHeight) { + aspectRatio = (float) (this.sourceWidth / (double) this.sourceHeight); + + width = maxWidth; + height = (int) (width / aspectRatio); + + if (height < maxHeight) { + aspectRatio = (float) (this.sourceHeight / (double) this.sourceWidth); + + height = maxHeight; + width = (int) (height / aspectRatio); + } + } else { + aspectRatio = (float) (this.sourceHeight / (double) this.sourceWidth); + + height = maxHeight; + width = (int) (height / aspectRatio); + if (width < maxWidth) { + aspectRatio = (float) (this.sourceWidth / (double) this.sourceHeight); + + width = maxWidth; + height = (int) (width / aspectRatio); + } + } + + return new Vector2f(width, height); + } + + /** + * Aspect ratio orientation. + */ + public enum Orientation { + LANDSCAPE, + SQUARE, + PORTRAIT + } + + public float getRatio() { + return this.ratio; + } + + public float getRelativeRatio() { + return this.relativeRatio; + } + + public Orientation getOrientation() { + return this.orientation; + } + + public float getSourceWidth() { + return this.sourceWidth; + } + + public float getSourceHeight() { + return this.sourceHeight; + } +} diff --git a/common/src/main/java/com/ultreon/devices/core/TaskBar.java b/common/src/main/java/com/ultreon/devices/core/TaskBar.java index 7f198595..00a59c9a 100644 --- a/common/src/main/java/com/ultreon/devices/core/TaskBar.java +++ b/common/src/main/java/com/ultreon/devices/core/TaskBar.java @@ -1,7 +1,6 @@ package com.ultreon.devices.core; import com.mojang.blaze3d.systems.RenderSystem; -import com.mojang.blaze3d.vertex.PoseStack; import com.ultreon.devices.Devices; import com.ultreon.devices.api.TrayItemAdder; import com.ultreon.devices.api.app.Application; @@ -16,7 +15,6 @@ import com.ultreon.devices.programs.system.SystemApp; import com.ultreon.devices.util.Vulnerability; import net.minecraft.client.Minecraft; -import net.minecraft.client.gui.Gui; import net.minecraft.client.gui.GuiGraphics; import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.Tag; @@ -119,8 +117,8 @@ public void render(GuiGraphics graphics, Laptop laptop, Minecraft mc, int x, int int trayItemsWidth = trayItems.size() * 14; graphics.blit(APP_BAR_GUI, x, y, 1, 18, 0, 0, 1, 18, 256, 256); - graphics.blit(APP_BAR_GUI, x + 1, y, Laptop.SCREEN_WIDTH - 36 - trayItemsWidth, 18, 1, 0, 1, 18, 256, 256); - graphics.blit(APP_BAR_GUI, x + Laptop.SCREEN_WIDTH - 35 - trayItemsWidth, y, 35 + trayItemsWidth, 18, 2, 0, 1, 18, 256, 256); + graphics.blit(APP_BAR_GUI, x + 1, y, Laptop.getScreenWidth() - 36 - trayItemsWidth, 18, 1, 0, 1, 18, 256, 256); + graphics.blit(APP_BAR_GUI, x + Laptop.getScreenWidth() - 35 - trayItemsWidth, y, 35 + trayItemsWidth, 18, 2, 0, 1, 18, 256, 256); RenderSystem.disableBlend(); @@ -136,10 +134,10 @@ public void render(GuiGraphics graphics, Laptop laptop, Minecraft mc, int x, int assert mc.level == null || mc.player != null; // assert mc.level != null; //can no longer assume - graphics.drawString(mc.font, timeToString(mc.level != null ? mc.level.getDayTime() : 0), x + 334, y + 5, Color.WHITE.getRGB(), true); + graphics.drawString(mc.font, timeToString(mc.level != null ? mc.level.getDayTime() : 0), x + Laptop.getScreenWidth() - 31, y + 5, Color.WHITE.getRGB(), true); /* Settings App */ - int startX = x + 317; + int startX = x + Laptop.getScreenWidth() - 48; for (int i = 0; i < trayItems.size(); i++) { int posX = startX - (trayItems.size() - 1 - i) * 14; if (isMouseInside(mouseX, mouseY, posX, y + 2, posX + 13, y + 15)) { @@ -172,7 +170,7 @@ public void handleClick(Laptop laptop, int x, int y, int mouseX, int mouseY, int } } - int startX = x + 317; + int startX = x + Laptop.getScreenWidth() - 48; for (int i = 0; i < trayItems.size(); i++) { int posX = startX - (trayItems.size() - 1 - i) * 14; if (isMouseInside(mouseX, mouseY, posX, y + 2, posX + 13, y + 15)) { diff --git a/common/src/main/java/com/ultreon/devices/core/Window.java b/common/src/main/java/com/ultreon/devices/core/Window.java index e9d0d03a..6356820a 100644 --- a/common/src/main/java/com/ultreon/devices/core/Window.java +++ b/common/src/main/java/com/ultreon/devices/core/Window.java @@ -35,8 +35,8 @@ public Window(T wrappable, Laptop laptop) { void setWidth(int width) { this.width = width + 2; - if (this.width > Laptop.SCREEN_WIDTH) { - this.width = Laptop.SCREEN_WIDTH; + if (this.width > Laptop.getScreenWidth()) { + this.width = Laptop.getScreenWidth(); } } @@ -78,8 +78,8 @@ public void render(GuiGraphics graphics, Laptop gui, Minecraft mc, int x, int y, if (content.isPendingLayoutUpdate()) { this.setWidth(content.getWidth()); this.setHeight(content.getHeight()); - this.offsetX = (Laptop.SCREEN_WIDTH - width) / 2; - this.offsetY = (Laptop.SCREEN_HEIGHT - TaskBar.BAR_HEIGHT - height) / 2; + this.offsetX = (Laptop.getScreenWidth() - width) / 2; + this.offsetY = (Laptop.getScreenHeight() - TaskBar.BAR_HEIGHT - height) / 2; updateComponents(x, y); content.clearPendingLayout(); } @@ -122,6 +122,7 @@ public void render(GuiGraphics graphics, Laptop gui, Minecraft mc, int x, int y, content.render(graphics, gui, mc, x + offsetX + 1, y + offsetY + 13, mouseX, mouseY, active && dialogWindow == null, partialTicks); RenderSystem.setShaderColor(1f, 1f, 1f, 1f); + graphics.pose().translate(0, 0, 200); if (dialogWindow != null) { graphics.fill(x + offsetX, y + offsetY, x + offsetX + width, y + offsetY + height, COLOR_WINDOW_DARK); @@ -174,20 +175,20 @@ public void handleKeyReleased(int keyCode, int scanCode, int modifiers) { } public void handleWindowMove(int screenStartX, int screenStartY, int newX, int newY) { - if (newX >= 0 && newX <= Laptop.SCREEN_WIDTH - width) { + if (newX >= 0 && newX <= Laptop.getScreenWidth() - width) { this.offsetX = newX; } else if (newX < 0) { this.offsetX = 0; } else { - this.offsetX = Laptop.SCREEN_WIDTH - width; + this.offsetX = Laptop.getScreenWidth() - width; } - if (newY >= 0 && newY <= Laptop.SCREEN_HEIGHT - TaskBar.BAR_HEIGHT - height) { + if (newY >= 0 && newY <= Laptop.getScreenHeight() - TaskBar.BAR_HEIGHT - height) { this.offsetY = newY; } else if (newY < 0) { this.offsetY = 0; } else { - this.offsetY = Laptop.SCREEN_HEIGHT - TaskBar.BAR_HEIGHT - height; + this.offsetY = Laptop.getScreenHeight() - TaskBar.BAR_HEIGHT - height; } updateComponents(screenStartX, screenStartY); diff --git a/common/src/main/java/com/ultreon/devices/programs/system/DisplayResolution.java b/common/src/main/java/com/ultreon/devices/programs/system/DisplayResolution.java new file mode 100644 index 00000000..9561d490 --- /dev/null +++ b/common/src/main/java/com/ultreon/devices/programs/system/DisplayResolution.java @@ -0,0 +1,27 @@ +package com.ultreon.devices.programs.system; + +import com.ultreon.devices.api.video.CustomResolution; +import net.minecraft.nbt.CompoundTag; + +public interface DisplayResolution { + static DisplayResolution load(CompoundTag resolution) { + var width = resolution.getInt("width"); + var height = resolution.getInt("height"); + + for (PredefinedResolution predefinedResolution : PredefinedResolution.values()) { + if (predefinedResolution.width() == width && predefinedResolution.height() == height) { + return predefinedResolution; + } + } + return new CustomResolution(width, height); + } + + int width(); + + int height(); + + default void save(CompoundTag tag) { + tag.putInt("width", width()); + tag.putInt("height", height()); + } +} diff --git a/common/src/main/java/com/ultreon/devices/programs/system/PredefinedResolution.java b/common/src/main/java/com/ultreon/devices/programs/system/PredefinedResolution.java new file mode 100644 index 00000000..b29c8f69 --- /dev/null +++ b/common/src/main/java/com/ultreon/devices/programs/system/PredefinedResolution.java @@ -0,0 +1,56 @@ +package com.ultreon.devices.programs.system; + +import com.ultreon.devices.core.Laptop; +import net.minecraft.network.chat.Component; + +import java.util.Collection; +import java.util.Collections; + +public enum PredefinedResolution implements DisplayResolution { + PREDEFINED_31360x17280(31360, 17280), + PREDEFINED_15680x8640(15680, 8640), + PREDEFINED_7840x4320(7840, 4320), + PREDEFINED_3840x2160(3840, 2160), + PREDEFINED_2560x1440(2560, 1440), + PREDEFINED_1920x1080(1920, 1080), + PREDEFINED_960x540(960, 540), + PREDEFINED_800x450(800, 450), + PREDEFINED_768x432(768, 432), + PREDEFINED_696x360(696, 360), + PREDEFINED_640x360(640, 360), + PREDEFINED_512x288(512, 288), + PREDEFINED_448x256(448, 256), + PREDEFINED_384x216(384, 216); + + private final int width; + private final int height; + + PredefinedResolution(int width, int height) { + this.width = width; + this.height = height; + } + + @Override + public int width() { + return width; + } + + @Override + public int height() { + return height; + } + + public static PredefinedResolution[] getResolutionList() { + Collection resolutionList = Laptop.getInstance().getVideoInfo().getResolutionList(); + + if (resolutionList == null) { + return new PredefinedResolution[0]; + } + + return resolutionList.toArray(new PredefinedResolution[0]); + } + + public String getDisplayName() { + return width + " × " + height; + } +} diff --git a/common/src/main/java/com/ultreon/devices/programs/system/SettingsApp.java b/common/src/main/java/com/ultreon/devices/programs/system/SettingsApp.java index 5e4c6510..e8ade100 100644 --- a/common/src/main/java/com/ultreon/devices/programs/system/SettingsApp.java +++ b/common/src/main/java/com/ultreon/devices/programs/system/SettingsApp.java @@ -49,6 +49,7 @@ public class SettingsApp extends SystemApp { private Button buttonColorSchemeApply; private final Stack predecessor = new Stack<>(); + private ComboBox.List comboDisplayResolutions; private void resetColorSchemeClick(int mouseX, int mouseY, int mouseButton) { if (mouseButton == 0) { @@ -169,6 +170,21 @@ private Layout createGeneralLayout() { checkBoxShowApps.setClickListener(this::showAllAppsClick); layoutGeneral.addComponent(checkBoxShowApps); + comboDisplayResolutions = new ComboBox.List<>(5, 26 + 20 + 4, PredefinedResolution.getResolutionList()); + comboDisplayResolutions.setListItemRenderer(new ListItemRenderer<>(20) { + @Override + public void render(GuiGraphics graphics, PredefinedResolution resolution, Minecraft mc, int x, int y, int width, int height, boolean selected) { + graphics.drawString(Minecraft.getInstance().font, resolution.getDisplayName(), x + 5, y + 5, 0xFFFFFF); + } + }); + comboDisplayResolutions.setChangeListener((oldValue, newValue) -> { + if (newValue != null) { + getLaptop().setDisplayResolution(newValue); + } + }); + + layoutGeneral.addComponent(comboDisplayResolutions); + return layoutGeneral; }