forked from MrCrayfish/MrCrayfishDeviceMod
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
363 additions
and
65 deletions.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
common/src/main/java/com/ultreon/devices/api/video/CustomResolution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
43
common/src/main/java/com/ultreon/devices/api/video/VideoInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
160
common/src/main/java/com/ultreon/devices/core/Laptop.java
Large diffs are not rendered by default.
Oops, something went wrong.
91 changes: 91 additions & 0 deletions
91
common/src/main/java/com/ultreon/devices/core/Resizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
common/src/main/java/com/ultreon/devices/programs/system/DisplayResolution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
common/src/main/java/com/ultreon/devices/programs/system/PredefinedResolution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters