Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding display resolutions! #97

Merged
merged 3 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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 {
}
43 changes: 43 additions & 0 deletions common/src/main/java/com/ultreon/devices/api/video/VideoInfo.java
Original file line number Diff line number Diff line change
@@ -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<PredefinedResolution> 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);
}
}
}
160 changes: 110 additions & 50 deletions common/src/main/java/com/ultreon/devices/core/Laptop.java

Large diffs are not rendered by default.

91 changes: 91 additions & 0 deletions common/src/main/java/com/ultreon/devices/core/Resizer.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
12 changes: 5 additions & 7 deletions common/src/main/java/com/ultreon/devices/core/TaskBar.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();

Expand All @@ -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)) {
Expand Down Expand Up @@ -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)) {
Expand Down
17 changes: 9 additions & 8 deletions common/src/main/java/com/ultreon/devices/core/Window.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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<PredefinedResolution> resolutionList = Laptop.getInstance().getVideoInfo().getResolutionList();

if (resolutionList == null) {
return new PredefinedResolution[0];
}

return resolutionList.toArray(new PredefinedResolution[0]);
}

public String getDisplayName() {
return width + " × " + height;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class SettingsApp extends SystemApp {
private Button buttonColorSchemeApply;

private final Stack<Layout> predecessor = new Stack<>();
private ComboBox.List<PredefinedResolution> comboDisplayResolutions;

private void resetColorSchemeClick(int mouseX, int mouseY, int mouseButton) {
if (mouseButton == 0) {
Expand Down Expand Up @@ -172,6 +173,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;
}

Expand Down
Loading