Skip to content

Commit

Permalink
Release 3.5.9 & Fix display bug for Rect.startDrawing (Disabled ALPHA…
Browse files Browse the repository at this point in the history
…_TEST) & Add more method to GifImage for better control & Add CursorHolder to UI & Add more method to control UI Component styles
  • Loading branch information
trychen committed Aug 31, 2020
1 parent 93e90a8 commit af3e4f7
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 28 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ org.gradle.jvmargs=-Xmx3G

# Mod Properties
mod_id = Pangu
mod_version = 3.5.3
mod_version = 3.5.9
mod_group = cn.mccraft.pangu
mod_core_plugin = cn.mccraft.pangu.core.asm.PanguPlugin

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cn/mccraft/pangu/core/PanguCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
@Mod(
modid = PanguCore.ID,
name = "Pangu Core",
version = "3.0.0",
version = "3.5.8",
useMetadata = true,
acceptedMinecraftVersions = "[1.12.2,1.13)"
)
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/cn/mccraft/pangu/core/client/ui/Component.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@
import net.minecraftforge.fml.relauncher.SideOnly;

import javax.annotation.Nullable;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.*;

@Accessors(chain = true)
@ToString
Expand Down Expand Up @@ -182,6 +179,12 @@ public Set<Style> getStyles() {
return styles;
}

public Component addStyles(Style... s) {
if (styles == null) styles = new HashSet<>();
Collections.addAll(styles, s);
return this;
}

@Override
public int compareTo(Component o) {
return Integer.compare(this.getZLevel(), o.getZLevel());
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/cn/mccraft/pangu/core/client/ui/CursorHolder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package cn.mccraft.pangu.core.client.ui;

import javax.annotation.Nonnull;

public class CursorHolder {
protected String type;
protected Object object;

public boolean isPresent() {
return object != null;
}

public String getType() {
return type;
}

public boolean check(String type) {
return isPresent() && type.equals(this.type);
}

public Object peek() {
return object;
}

public Object pop() {
Object obj = object;
clear();
return obj;
}

public Object push(@Nonnull String type, @Nonnull Object object) {
Object old = this.object;
this.type = type;
this.object = object;
return old;
}

public void clear() {
object = null;
type = null;
}
}
3 changes: 3 additions & 0 deletions src/main/java/cn/mccraft/pangu/core/client/ui/Screen.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ public abstract class Screen extends GuiScreen {
@Setter
protected Component focusedComponent;

@Getter
protected CursorHolder cursor = new CursorHolder();

public Screen() {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.IResource;
import net.minecraft.util.ResourceLocation;
import org.lwjgl.opengl.GL11;

import java.io.FileNotFoundException;
import java.io.IOException;

public class BuiltinGif extends GifImage {
private ResourceLocation path;
protected ResourceLocation path;

public BuiltinGif(ResourceLocation path) {
this.path = path;
Expand Down
26 changes: 24 additions & 2 deletions src/main/java/cn/mccraft/pangu/core/util/image/GifImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import cn.mccraft.pangu.core.util.render.Rect;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.texture.DynamicTexture;
import net.minecraft.util.ResourceLocation;
import org.lwjgl.opengl.GL11;

import java.awt.image.BufferedImage;
import java.io.InputStream;
Expand Down Expand Up @@ -41,6 +43,10 @@ public Thread newThread(Runnable r) {
@Getter
protected int duration;

@Getter
@Setter
protected int frameTimeOffset;

@Getter
protected transient int width, height;

Expand All @@ -66,7 +72,7 @@ public void upload() throws ExecutionException, InterruptedException {
}

public int getFromSequences() {
return sequences[(int) (System.currentTimeMillis() % this.duration)];
return sequences[((int) (System.currentTimeMillis() % this.duration) + frameTimeOffset) % sequences.length];
}

public int getTextureID() {
Expand All @@ -91,7 +97,7 @@ public int getTextureID() {

@Override
public boolean isReady() {
return sequences != null;
return getTextureID() > 0;
}

public abstract void readGifImage(GifDecoder decoder);
Expand All @@ -113,6 +119,22 @@ public List<Frame> decodeFrames() {
return frames;
}

public void restart() {
if (sequences == null) return;
int currentFrame = (int) (System.currentTimeMillis() % this.duration);
setFrameTimeOffset(duration - currentFrame);
}

@Override
public boolean free() {
if (sequences == null) return true;
for (int sequence : sequences) {
GL11.glDeleteTextures(sequence);
}
sequences = null;
return false;
}

@Getter
@AllArgsConstructor
public static class Frame {
Expand Down
66 changes: 49 additions & 17 deletions src/main/java/cn/mccraft/pangu/core/util/image/RemoteImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import lombok.Setter;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.util.ResourceLocation;
import org.lwjgl.opengl.GL11;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
Expand Down Expand Up @@ -75,24 +76,10 @@ public Thread newThread(Runnable r) {
protected RemoteImage(String urlPath) throws URISyntaxException {
this.urlPath = urlPath;
this.url = new URI(urlPath);
this.id = Base64.getEncoder().encodeToString(urlPath.getBytes());
this.id = Base64.getEncoder().encodeToString(urlPath.getBytes()).replace('/', '_').replace('\\', '_');
this.cachedFilePath = createCachedFilePath();
LocalCache.markFileUsed(cachedFilePath.toPath());

FETCHER_EXECUTOR.submit(() -> {
try {
if (!cachedFilePath.exists()) {
PanguCore.getLogger().debug("Start fetching image from " + url.toString());
saveImage();
PanguCore.getLogger().debug("Saved " + urlPath + " to " + cachedFilePath.getAbsolutePath());
} else
PanguCore.getLogger().debug("Loading image " + urlPath + " from local " + cachedFilePath.getAbsolutePath());
} catch (Exception e) {
PanguCore.getLogger().error("Error while fetching or reading image " + url.toString(), e);
exception = true;
}
loaded = true;
});
}

public static RemoteImage deserialize(DataInput out) throws IOException {
Expand Down Expand Up @@ -140,7 +127,7 @@ public int getTextureID() {
width = imageBuffer.get().getWidth();
height = imageBuffer.get().getHeight();
textureID = OpenGL.uploadTexture(imageBuffer.get().getBuffer(), width, height);
imageBuffer = null;
if (!isKeepBufferedImage())imageBuffer = null;
PanguCore.getLogger().debug("Uploaded image for " + url.toString());
} catch (Exception e) {
PanguCore.getLogger().error("Couldn't load remote image from url " + url.toString(), e);
Expand All @@ -152,7 +139,7 @@ public int getTextureID() {

@Override
public boolean isReady() {
return textureID > 0;
return getTextureID() > 0;
}

@Override
Expand All @@ -177,7 +164,9 @@ public ImageBuffer buildBuffer() throws IOException {

BufferedImage image = readImage();
if (image == null) {
exception = true;
PanguCore.getLogger().error("Image file not exists " + url.toString());
return null;
}
return new ImageBuffer(OpenGL.buildARGB(image), image.getWidth(), image.getHeight());
}
Expand All @@ -203,6 +192,49 @@ public File createCachedFilePath() {
return LocalCache.get("images", id).toFile();
}

@Override
public boolean free() {
if (textureID == 0) return true;
GL11.glDeleteTextures(textureID);
textureID = 0;
if (!isKeepBufferedImage()) imageBuffer = null;
PanguCore.getLogger().debug("Free image " + url.toString());
return true;
}

@Override
public void remove() {
cachedFilePath.delete();
free();
exception = false;
loaded = false;
PanguCore.getLogger().debug("Remove image " + url.toString());
}

@Override
public void refresh() {
remove();
fetch();
PanguCore.getLogger().debug("Refresh image " + url.toString());
}

public void fetch() {
FETCHER_EXECUTOR.submit(() -> {
try {
if (!cachedFilePath.exists()) {
PanguCore.getLogger().debug("Start fetching image from " + url.toString());
saveImage();
PanguCore.getLogger().debug("Saved " + urlPath + " to " + cachedFilePath.getAbsolutePath());
} else
PanguCore.getLogger().debug("Loading image " + urlPath + " from local " + cachedFilePath.getAbsolutePath());
} catch (Exception e) {
PanguCore.getLogger().error("Error while fetching or reading image " + url.toString(), e);
exception = true;
}
loaded = true;
});
}

@Data
@AllArgsConstructor
public static class ImageBuffer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,14 @@ default void bind(ResourceLocation loading, ResourceLocation error) {
default TextureAtlasSprite asAtlasSprite() {
return null;
}

default void remove() {
}

default void refresh() {
}

default boolean free() {
return false;
}
}
4 changes: 2 additions & 2 deletions src/main/java/cn/mccraft/pangu/core/util/render/Rect.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ static void startDrawing() {
GlStateManager.enableTexture2D();
GlStateManager.enableBlend();
GlStateManager.tryBlendFuncSeparate(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA, GlStateManager.SourceFactor.ONE, GlStateManager.DestFactor.ZERO);
GlStateManager.enableAlpha();
GlStateManager.disableAlpha();
Rect.color();
}

static void endDrawing() {
GlStateManager.disableBlend();
GlStateManager.disableAlpha();
GlStateManager.enableAlpha();
GlStateManager.disableTexture2D();
}

Expand Down

0 comments on commit af3e4f7

Please sign in to comment.