From 9b4c9e48002d29b662fb11b2ba58177f04268ba5 Mon Sep 17 00:00:00 2001 From: Juuxel <6596629+Juuxel@users.noreply.github.com> Date: Sat, 27 Feb 2021 16:18:48 +0200 Subject: Move all implementation classes into the impl package --- .../github/cottonmc/cotton/gui/client/LibGui.java | 2 + .../cottonmc/cotton/gui/client/LibGuiClient.java | 79 -------------- .../cottonmc/cotton/gui/client/LibGuiConfig.java | 8 -- .../cottonmc/cotton/gui/client/NinePatch.java | 103 ++---------------- .../cotton/gui/client/modmenu/ConfigGui.java | 62 ----------- .../cotton/gui/client/modmenu/ModMenuSupport.java | 18 ---- .../cotton/gui/client/modmenu/WKirbSprite.java | 117 --------------------- .../cotton/gui/client/modmenu/package-info.java | 4 - .../cotton/gui/impl/client/LibGuiClient.java | 75 +++++++++++++ .../cotton/gui/impl/client/LibGuiConfig.java | 8 ++ .../cotton/gui/impl/client/NinePatchInternals.java | 104 ++++++++++++++++++ .../cotton/gui/impl/client/package-info.java | 7 ++ .../cotton/gui/impl/modmenu/ConfigGui.java | 62 +++++++++++ .../cotton/gui/impl/modmenu/ModMenuSupport.java | 18 ++++ .../cotton/gui/impl/modmenu/WKirbSprite.java | 117 +++++++++++++++++++++ .../cotton/gui/impl/modmenu/package-info.java | 7 ++ .../github/cottonmc/cotton/gui/widget/WLabel.java | 4 +- 17 files changed, 413 insertions(+), 382 deletions(-) delete mode 100644 src/main/java/io/github/cottonmc/cotton/gui/client/LibGuiClient.java delete mode 100644 src/main/java/io/github/cottonmc/cotton/gui/client/LibGuiConfig.java delete mode 100644 src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/ConfigGui.java delete mode 100644 src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/ModMenuSupport.java delete mode 100644 src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/WKirbSprite.java delete mode 100644 src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/package-info.java create mode 100644 src/main/java/io/github/cottonmc/cotton/gui/impl/client/LibGuiClient.java create mode 100644 src/main/java/io/github/cottonmc/cotton/gui/impl/client/LibGuiConfig.java create mode 100644 src/main/java/io/github/cottonmc/cotton/gui/impl/client/NinePatchInternals.java create mode 100644 src/main/java/io/github/cottonmc/cotton/gui/impl/client/package-info.java create mode 100644 src/main/java/io/github/cottonmc/cotton/gui/impl/modmenu/ConfigGui.java create mode 100644 src/main/java/io/github/cottonmc/cotton/gui/impl/modmenu/ModMenuSupport.java create mode 100644 src/main/java/io/github/cottonmc/cotton/gui/impl/modmenu/WKirbSprite.java create mode 100644 src/main/java/io/github/cottonmc/cotton/gui/impl/modmenu/package-info.java (limited to 'src/main/java') diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/LibGui.java b/src/main/java/io/github/cottonmc/cotton/gui/client/LibGui.java index 90bb985..f781f81 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/LibGui.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/client/LibGui.java @@ -1,5 +1,7 @@ package io.github.cottonmc.cotton.gui.client; +import io.github.cottonmc.cotton.gui.impl.client.LibGuiClient; + /** * This class provides access to LibGui configuration and other global data. * diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/LibGuiClient.java b/src/main/java/io/github/cottonmc/cotton/gui/client/LibGuiClient.java deleted file mode 100644 index 3c1f72f..0000000 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/LibGuiClient.java +++ /dev/null @@ -1,79 +0,0 @@ -package io.github.cottonmc.cotton.gui.client; - -import net.fabricmc.api.ClientModInitializer; -import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; -import net.fabricmc.fabric.api.resource.ResourceManagerHelper; -import net.fabricmc.loader.api.FabricLoader; -import net.minecraft.resource.ResourceType; - -import blue.endless.jankson.Jankson; -import blue.endless.jankson.JsonElement; -import blue.endless.jankson.JsonObject; -import io.github.cottonmc.cotton.gui.impl.ScreenNetworkingImpl; -import io.github.cottonmc.jankson.JanksonFactory; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; -import org.jetbrains.annotations.ApiStatus; - -import java.io.File; -import java.io.FileOutputStream; -import java.nio.charset.StandardCharsets; - -@ApiStatus.Internal -public class LibGuiClient implements ClientModInitializer { - public static final Logger logger = LogManager.getLogger(); - public static final String MODID = "libgui"; - /** @deprecated Replaced with {@link LibGui} */ - @Deprecated - public static volatile LibGuiConfig config; - - public static final Jankson jankson = JanksonFactory.createJankson(); - - @Override - public void onInitializeClient() { - config = loadConfig(); - - ResourceManagerHelper.get(ResourceType.CLIENT_RESOURCES).registerReloadListener(NinePatch.MetadataLoader.INSTANCE); - - ClientPlayNetworking.registerGlobalReceiver(ScreenNetworkingImpl.SCREEN_MESSAGE_S2C, (client, networkHandler, buf, responseSender) -> { - ScreenNetworkingImpl.handle(client, client.player, buf); - }); - } - - public static LibGuiConfig loadConfig() { - try { - File file = new File(FabricLoader.getInstance().getConfigDirectory(),"libgui.json5"); - - if (!file.exists()) saveConfig(new LibGuiConfig()); - - JsonObject json = jankson.load(file); - config = jankson.fromJson(json, LibGuiConfig.class); - - /* - JsonElement jsonElementNew = jankson.toJson(new LibGuiConfig()); - if(jsonElementNew instanceof JsonObject) { - JsonObject jsonNew = (JsonObject) jsonElementNew; - if(json.getDelta(jsonNew).size()>= 0) { //TODO: Insert new keys as defaults into `json` IR object instead of writing the config out, so comments are preserved - saveConfig(config); - } - }*/ - } catch (Exception e) { - logger.error("[LibGui] Error loading config: {}", e.getMessage()); - } - return config; - } - - public static void saveConfig(LibGuiConfig config) { - try { - File file = new File(FabricLoader.getInstance().getConfigDirectory(),"libgui.json5"); - - JsonElement json = jankson.toJson(config); - String result = json.toJson(true, true); - try (FileOutputStream out = new FileOutputStream(file, false)) { - out.write(result.getBytes(StandardCharsets.UTF_8)); - } - } catch (Exception e) { - logger.error("[LibGui] Error saving config: {}", e.getMessage()); - } - } -} diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/LibGuiConfig.java b/src/main/java/io/github/cottonmc/cotton/gui/client/LibGuiConfig.java deleted file mode 100644 index b971e31..0000000 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/LibGuiConfig.java +++ /dev/null @@ -1,8 +0,0 @@ -package io.github.cottonmc.cotton.gui.client; - -import blue.endless.jankson.Comment; - -public class LibGuiConfig { - @Comment("Whether dark mode should be enabled. Will only affect Vanilla-styled GUIs.") - public boolean darkMode = false; -} diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/NinePatch.java b/src/main/java/io/github/cottonmc/cotton/gui/client/NinePatch.java index 0375d8d..29b2bea 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/NinePatch.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/client/NinePatch.java @@ -2,25 +2,14 @@ package io.github.cottonmc.cotton.gui.client; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; -import net.fabricmc.fabric.api.resource.IdentifiableResourceReloadListener; import net.minecraft.client.util.math.MatrixStack; -import net.minecraft.resource.Resource; -import net.minecraft.resource.ResourceManager; -import net.minecraft.resource.SinglePreparationResourceReloadListener; import net.minecraft.util.Identifier; import net.minecraft.util.math.MathHelper; -import net.minecraft.util.profiler.Profiler; +import io.github.cottonmc.cotton.gui.impl.client.NinePatchInternals; import io.github.cottonmc.cotton.gui.widget.WWidget; import org.jetbrains.annotations.Nullable; -import java.io.InputStream; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; -import java.util.Properties; - /** * The nine-patch background painter paints rectangles using a nine-patch texture. * @@ -179,7 +168,7 @@ public class NinePatch implements BackgroundPainter { int y2 = top + height - cornerSize; float uv1 = cornerUv; float uv2 = 1.0f - cornerUv; - Mode mode = this.mode != null ? this.mode : MetadataLoader.INSTANCE.getProperties(texture).getMode(); + Mode mode = this.mode != null ? this.mode : NinePatchInternals.MetadataLoader.INSTANCE.getProperties(texture).getMode(); ScreenDrawing.texturedRect(matrices, left, top, cornerSize, cornerSize, texture, 0, 0, uv1, uv1, 0xFF_FFFFFF); ScreenDrawing.texturedRect(matrices, x2, top, cornerSize, cornerSize, texture, uv2, 0, 1, uv1, 0xFF_FFFFFF); @@ -240,8 +229,15 @@ public class NinePatch implements BackgroundPainter { */ TILING; + /** + * Deserializes a nine-patch mode from a string. + * + * @param str the mode string + * @return the mode, or null if the string is invalid + * @since 4.0.0 + */ @Nullable - static Mode fromString(String str) { + public static Mode fromString(String str) { if (str == null) return null; if (str.equalsIgnoreCase("stretching")) return STRETCHING; @@ -251,83 +247,4 @@ public class NinePatch implements BackgroundPainter { } } - public static class TextureProperties { - public static final TextureProperties DEFAULT = new TextureProperties(Mode.STRETCHING); - - private final Mode mode; - - public TextureProperties(Mode mode) { - this.mode = mode; - } - - public Mode getMode() { - return mode; - } - } - - public static class MetadataLoader extends SinglePreparationResourceReloadListener> implements IdentifiableResourceReloadListener { - public static final MetadataLoader INSTANCE = new MetadataLoader(); - - private static final Identifier ID = new Identifier("libgui", "9patch_metadata"); - private static final String SUFFIX = ".9patch"; - - private Map properties = Collections.emptyMap(); - - public TextureProperties getProperties(Identifier texture) { - return properties.getOrDefault(texture, TextureProperties.DEFAULT); - } - - @Override - public Identifier getFabricId() { - return ID; - } - - @Override - protected Map prepare(ResourceManager manager, Profiler profiler) { - Collection ids = manager.findResources("textures", s -> s.endsWith(SUFFIX)); - Map result = new HashMap<>(); - - for (Identifier input : ids) { - try (Resource resource = manager.getResource(input); - InputStream stream = resource.getInputStream()) { - Properties props = new Properties(); - props.load(stream); - Identifier textureId = new Identifier(input.getNamespace(), input.getPath().substring(0, input.getPath().length() - SUFFIX.length())); - result.put(textureId, props); - } catch (Exception e) { - LibGuiClient.logger.error("Error while loading metadata file {}, skipping...", input, e); - } - } - - return result; - } - - @Override - protected void apply(Map meta, ResourceManager manager, Profiler profiler) { - properties = new HashMap<>(); - for (Map.Entry entry : meta.entrySet()) { - Identifier id = entry.getKey(); - Properties props = entry.getValue(); - - Mode mode = TextureProperties.DEFAULT.getMode(); -// float cornerUv = TextureProperties.DEFAULT.getCornerUv(); - - if (props.containsKey("mode")) { - String modeStr = props.getProperty("mode"); - mode = Mode.fromString(modeStr); - if (mode == null) { - LibGuiClient.logger.error("Invalid mode '{}' in nine-patch metadata file for texture {}", modeStr, id); - continue; - } - } - -// if (props.containsKey("cornerUv")) { -// cornerUv = Float.parseFloat(props.getProperty("cornerUv")); -// } - - TextureProperties texProperties = new TextureProperties(mode); - properties.put(id, texProperties); - } - } - } } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/ConfigGui.java b/src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/ConfigGui.java deleted file mode 100644 index cd3fe0a..0000000 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/ConfigGui.java +++ /dev/null @@ -1,62 +0,0 @@ -package io.github.cottonmc.cotton.gui.client.modmenu; - -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.gui.screen.Screen; -import net.minecraft.text.TranslatableText; - -import io.github.cottonmc.cotton.gui.client.BackgroundPainter; -import io.github.cottonmc.cotton.gui.client.LibGuiClient; -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.WTextField; -import io.github.cottonmc.cotton.gui.widget.WToggleButton; - -public class ConfigGui extends LightweightGuiDescription { - - public ConfigGui(Screen previous) { - WGridPanel root = new WGridPanel(); - setRootPanel(root); - - WToggleButton darkmodeButton = new WToggleButton(new TranslatableText("option.libgui.darkmode")) { - @Override - public void onToggle(boolean on) { - LibGuiClient.config.darkMode = on; - LibGuiClient.saveConfig(LibGuiClient.config); - } - }; - darkmodeButton.setToggle(LibGuiClient.config.darkMode); - root.add(darkmodeButton, 0, 2, 6, 1); - - WTextField testField = new WTextField(); - testField.setSuggestion("test"); - root.add(testField, 0, 3, 4, 1); - - /* - WSlider verticalSlider = new WSlider(-100, 100, Axis.VERTICAL); - verticalSlider.setDraggingFinishedListener(() -> System.out.println("Mouse released")); - verticalSlider.setValueChangeListener(System.out::println); - - WLabeledSlider horizontalSlider = new WLabeledSlider(0, 500); - horizontalSlider.setLabelUpdater(value -> new LiteralText(value + "!")); - horizontalSlider.setDraggingFinishedListener(() -> System.out.println("Mouse released")); - horizontalSlider.setValue(250); - - root.add(verticalSlider, 6, 0, 1, 3); - root.add(horizontalSlider, 1, 4, 4, 1); - */ - - root.add(new WKirbSprite(), 5, 4); - - WButton doneButton = new WButton(new TranslatableText("gui.done")); - doneButton.setOnClick(()->{ - MinecraftClient.getInstance().openScreen(previous); - }); - root.add(doneButton, 0, 5, 3, 1); - - root.setBackgroundPainter(BackgroundPainter.VANILLA); - root.setSize(7*18, 6*18); - - root.validate(this); - } -} diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/ModMenuSupport.java b/src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/ModMenuSupport.java deleted file mode 100644 index ccdff93..0000000 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/ModMenuSupport.java +++ /dev/null @@ -1,18 +0,0 @@ -package io.github.cottonmc.cotton.gui.client.modmenu; - -import net.minecraft.text.TranslatableText; - -import io.github.cottonmc.cotton.gui.client.CottonClientScreen; -import io.github.prospector.modmenu.api.ConfigScreenFactory; -import io.github.prospector.modmenu.api.ModMenuApi; - -public class ModMenuSupport implements ModMenuApi { - @Override - public ConfigScreenFactory getModConfigScreenFactory() { - return screen -> new CottonClientScreen(new TranslatableText("options.libgui.libgui_settings"), new ConfigGui(screen)) { - public void onClose() { - this.client.openScreen(screen); - } - }; - } -} diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/WKirbSprite.java b/src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/WKirbSprite.java deleted file mode 100644 index 2f7832d..0000000 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/WKirbSprite.java +++ /dev/null @@ -1,117 +0,0 @@ -package io.github.cottonmc.cotton.gui.client.modmenu; - -import net.fabricmc.api.EnvType; -import net.fabricmc.api.Environment; -import net.minecraft.client.util.math.MatrixStack; -import net.minecraft.util.Identifier; - -import io.github.cottonmc.cotton.gui.client.LibGui; -import io.github.cottonmc.cotton.gui.client.ScreenDrawing; -import io.github.cottonmc.cotton.gui.widget.WWidget; - -import java.util.ArrayList; - -public class WKirbSprite extends WWidget { - private static final Identifier KIRB = new Identifier("libgui:textures/widget/kirb.png"); - - private static final float PX = 1f/416f; - private static final float KIRB_WIDTH = 32*PX; - - private int currentFrame= 0; - private long currentFrameTime = 0; - private int[] toSleep = { 0, 0, 0, 1, 2, 1, 2, 0, 0, 0, 1, 2, 3 }; - private int[] asleep = { 4, 4, 4, 4, 5, 6, 7, 6, 5 }; - private int[] toAwake = { 3, 3, 8, 8, 8, 8, 8, 8, 8 }; - private int[] awake = { 9, 9, 9, 10, 11, 12 }; - private State state = State.ASLEEP; - private ArrayList pendingFrames = new ArrayList<>(); - - private int frameTime = 300; - private long lastFrame; - - public WKirbSprite() { - state = (LibGui.isDarkMode()) ? State.ASLEEP : State.AWAKE; - } - - public void schedule(int[] frames) { - for(int i : frames) pendingFrames.add(i); - } - - @Override - public boolean canResize() { - return false; - } - - @Override - public int getWidth() { - return 32; - } - - @Override - public int getHeight() { - return 32; - } - - @Environment(EnvType.CLIENT) - @Override - public void paint(MatrixStack matrices, int x, int y, int mouseX, int mouseY) { - long now = System.nanoTime() / 1_000_000L; - - - if (pendingFrames.isEmpty()) { - - if (LibGui.isDarkMode()) { - switch(state) { - case AWAKE: - state = State.FALLING_ASLEEP; - break; - case FALLING_ASLEEP: - state = State.ASLEEP; - break; - default: - //zzzz - state = State.ASLEEP; - break; - } - } else { - switch(state) { - case ASLEEP: - state = State.WAKING_UP; - break; - case WAKING_UP: - state = State.AWAKE; - break; - default: - state = State.AWAKE; - break; - } - } - - switch (state) { - case ASLEEP: schedule(asleep); break; - case WAKING_UP: schedule(toAwake); break; - case AWAKE: schedule(awake); break; - case FALLING_ASLEEP: schedule(toSleep); break; - } - } - - float offset = KIRB_WIDTH * currentFrame; - ScreenDrawing.texturedRect(matrices, x, y+8, 32, 32, KIRB, offset, 0, offset+KIRB_WIDTH, 1, 0xFFFFFFFF); - - long elapsed = now - lastFrame; - currentFrameTime += elapsed; - if (currentFrameTime >= frameTime) { - if (!pendingFrames.isEmpty()) currentFrame = pendingFrames.remove(0); - currentFrameTime = 0; - } - - this.lastFrame = now; - } - - public static enum State { - AWAKE, - FALLING_ASLEEP, - ASLEEP, - WAKING_UP; - } -} diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/package-info.java b/src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/package-info.java deleted file mode 100644 index ad040bf..0000000 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/package-info.java +++ /dev/null @@ -1,4 +0,0 @@ -/** - * Mod Menu support for LibGui. - */ -package io.github.cottonmc.cotton.gui.client.modmenu; diff --git a/src/main/java/io/github/cottonmc/cotton/gui/impl/client/LibGuiClient.java b/src/main/java/io/github/cottonmc/cotton/gui/impl/client/LibGuiClient.java new file mode 100644 index 0000000..97109c2 --- /dev/null +++ b/src/main/java/io/github/cottonmc/cotton/gui/impl/client/LibGuiClient.java @@ -0,0 +1,75 @@ +package io.github.cottonmc.cotton.gui.impl.client; + +import net.fabricmc.api.ClientModInitializer; +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; +import net.fabricmc.fabric.api.resource.ResourceManagerHelper; +import net.fabricmc.loader.api.FabricLoader; +import net.minecraft.resource.ResourceType; + +import blue.endless.jankson.Jankson; +import blue.endless.jankson.JsonElement; +import blue.endless.jankson.JsonObject; +import io.github.cottonmc.cotton.gui.impl.ScreenNetworkingImpl; +import io.github.cottonmc.jankson.JanksonFactory; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.io.File; +import java.io.FileOutputStream; +import java.nio.charset.StandardCharsets; + +public class LibGuiClient implements ClientModInitializer { + public static final Logger logger = LogManager.getLogger(); + public static final String MODID = "libgui"; + public static volatile LibGuiConfig config; + + public static final Jankson jankson = JanksonFactory.createJankson(); + + @Override + public void onInitializeClient() { + config = loadConfig(); + + ResourceManagerHelper.get(ResourceType.CLIENT_RESOURCES).registerReloadListener(NinePatchInternals.MetadataLoader.INSTANCE); + + ClientPlayNetworking.registerGlobalReceiver(ScreenNetworkingImpl.SCREEN_MESSAGE_S2C, (client, networkHandler, buf, responseSender) -> { + ScreenNetworkingImpl.handle(client, client.player, buf); + }); + } + + public static LibGuiConfig loadConfig() { + try { + File file = new File(FabricLoader.getInstance().getConfigDirectory(),"libgui.json5"); + + if (!file.exists()) saveConfig(new LibGuiConfig()); + + JsonObject json = jankson.load(file); + config = jankson.fromJson(json, LibGuiConfig.class); + + /* + JsonElement jsonElementNew = jankson.toJson(new LibGuiConfig()); + if(jsonElementNew instanceof JsonObject) { + JsonObject jsonNew = (JsonObject) jsonElementNew; + if(json.getDelta(jsonNew).size()>= 0) { //TODO: Insert new keys as defaults into `json` IR object instead of writing the config out, so comments are preserved + saveConfig(config); + } + }*/ + } catch (Exception e) { + logger.error("[LibGui] Error loading config: {}", e.getMessage()); + } + return config; + } + + public static void saveConfig(LibGuiConfig config) { + try { + File file = new File(FabricLoader.getInstance().getConfigDirectory(),"libgui.json5"); + + JsonElement json = jankson.toJson(config); + String result = json.toJson(true, true); + try (FileOutputStream out = new FileOutputStream(file, false)) { + out.write(result.getBytes(StandardCharsets.UTF_8)); + } + } catch (Exception e) { + logger.error("[LibGui] Error saving config: {}", e.getMessage()); + } + } +} diff --git a/src/main/java/io/github/cottonmc/cotton/gui/impl/client/LibGuiConfig.java b/src/main/java/io/github/cottonmc/cotton/gui/impl/client/LibGuiConfig.java new file mode 100644 index 0000000..b1985af --- /dev/null +++ b/src/main/java/io/github/cottonmc/cotton/gui/impl/client/LibGuiConfig.java @@ -0,0 +1,8 @@ +package io.github.cottonmc.cotton.gui.impl.client; + +import blue.endless.jankson.Comment; + +public class LibGuiConfig { + @Comment("Whether dark mode should be enabled. Will only affect Vanilla-styled GUIs.") + public boolean darkMode = false; +} diff --git a/src/main/java/io/github/cottonmc/cotton/gui/impl/client/NinePatchInternals.java b/src/main/java/io/github/cottonmc/cotton/gui/impl/client/NinePatchInternals.java new file mode 100644 index 0000000..7a18413 --- /dev/null +++ b/src/main/java/io/github/cottonmc/cotton/gui/impl/client/NinePatchInternals.java @@ -0,0 +1,104 @@ +package io.github.cottonmc.cotton.gui.impl.client; + +import io.github.cottonmc.cotton.gui.client.NinePatch; + +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.fabricmc.fabric.api.resource.IdentifiableResourceReloadListener; +import net.minecraft.resource.Resource; +import net.minecraft.resource.ResourceManager; +import net.minecraft.resource.SinglePreparationResourceReloadListener; +import net.minecraft.util.Identifier; +import net.minecraft.util.profiler.Profiler; + +import java.io.InputStream; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +@Environment(EnvType.CLIENT) +public final class NinePatchInternals { + @Environment(EnvType.CLIENT) + public static class TextureProperties { + public static final TextureProperties DEFAULT = new TextureProperties(NinePatch.Mode.STRETCHING); + + private final NinePatch.Mode mode; + + public TextureProperties(NinePatch.Mode mode) { + this.mode = mode; + } + + public NinePatch.Mode getMode() { + return mode; + } + } + + @Environment(EnvType.CLIENT) + public static class MetadataLoader extends SinglePreparationResourceReloadListener> implements IdentifiableResourceReloadListener { + public static final MetadataLoader INSTANCE = new MetadataLoader(); + + private static final Identifier ID = new Identifier("libgui", "9patch_metadata"); + private static final String SUFFIX = ".9patch"; + + private Map properties = Collections.emptyMap(); + + public TextureProperties getProperties(Identifier texture) { + return properties.getOrDefault(texture, TextureProperties.DEFAULT); + } + + @Override + public Identifier getFabricId() { + return ID; + } + + @Override + protected Map prepare(ResourceManager manager, Profiler profiler) { + Collection ids = manager.findResources("textures", s -> s.endsWith(SUFFIX)); + Map result = new HashMap<>(); + + for (Identifier input : ids) { + try (Resource resource = manager.getResource(input); + InputStream stream = resource.getInputStream()) { + Properties props = new Properties(); + props.load(stream); + Identifier textureId = new Identifier(input.getNamespace(), input.getPath().substring(0, input.getPath().length() - SUFFIX.length())); + result.put(textureId, props); + } catch (Exception e) { + LibGuiClient.logger.error("Error while loading metadata file {}, skipping...", input, e); + } + } + + return result; + } + + @Override + protected void apply(Map meta, ResourceManager manager, Profiler profiler) { + properties = new HashMap<>(); + for (Map.Entry entry : meta.entrySet()) { + Identifier id = entry.getKey(); + Properties props = entry.getValue(); + + NinePatch.Mode mode = TextureProperties.DEFAULT.getMode(); +// float cornerUv = TextureProperties.DEFAULT.getCornerUv(); + + if (props.containsKey("mode")) { + String modeStr = props.getProperty("mode"); + mode = NinePatch.Mode.fromString(modeStr); + if (mode == null) { + LibGuiClient.logger.error("Invalid mode '{}' in nine-patch metadata file for texture {}", modeStr, id); + continue; + } + } + +// if (props.containsKey("cornerUv")) { +// cornerUv = Float.parseFloat(props.getProperty("cornerUv")); +// } + + TextureProperties texProperties = new TextureProperties(mode); + properties.put(id, texProperties); + } + } + } +} diff --git a/src/main/java/io/github/cottonmc/cotton/gui/impl/client/package-info.java b/src/main/java/io/github/cottonmc/cotton/gui/impl/client/package-info.java new file mode 100644 index 0000000..16c1859 --- /dev/null +++ b/src/main/java/io/github/cottonmc/cotton/gui/impl/client/package-info.java @@ -0,0 +1,7 @@ +/** + * Internal implementation classes. + */ +@ApiStatus.Internal +package io.github.cottonmc.cotton.gui.impl.client; + +import org.jetbrains.annotations.ApiStatus; diff --git a/src/main/java/io/github/cottonmc/cotton/gui/impl/modmenu/ConfigGui.java b/src/main/java/io/github/cottonmc/cotton/gui/impl/modmenu/ConfigGui.java new file mode 100644 index 0000000..625b67c --- /dev/null +++ b/src/main/java/io/github/cottonmc/cotton/gui/impl/modmenu/ConfigGui.java @@ -0,0 +1,62 @@ +package io.github.cottonmc.cotton.gui.impl.modmenu; + +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.screen.Screen; +import net.minecraft.text.TranslatableText; + +import io.github.cottonmc.cotton.gui.client.BackgroundPainter; +import io.github.cottonmc.cotton.gui.impl.client.LibGuiClient; +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.WTextField; +import io.github.cottonmc.cotton.gui.widget.WToggleButton; + +public class ConfigGui extends LightweightGuiDescription { + + public ConfigGui(Screen previous) { + WGridPanel root = new WGridPanel(); + setRootPanel(root); + + WToggleButton darkmodeButton = new WToggleButton(new TranslatableText("option.libgui.darkmode")) { + @Override + public void onToggle(boolean on) { + LibGuiClient.config.darkMode = on; + LibGuiClient.saveConfig(LibGuiClient.config); + } + }; + darkmodeButton.setToggle(LibGuiClient.config.darkMode); + root.add(darkmodeButton, 0, 2, 6, 1); + + WTextField testField = new WTextField(); + testField.setSuggestion("test"); + root.add(testField, 0, 3, 4, 1); + + /* + WSlider verticalSlider = new WSlider(-100, 100, Axis.VERTICAL); + verticalSlider.setDraggingFinishedListener(() -> System.out.println("Mouse released")); + verticalSlider.setValueChangeListener(System.out::println); + + WLabeledSlider horizontalSlider = new WLabeledSlider(0, 500); + horizontalSlider.setLabelUpdater(value -> new LiteralText(value + "!")); + horizontalSlider.setDraggingFinishedListener(() -> System.out.println("Mouse released")); + horizontalSlider.setValue(250); + + root.add(verticalSlider, 6, 0, 1, 3); + root.add(horizontalSlider, 1, 4, 4, 1); + */ + + root.add(new WKirbSprite(), 5, 4); + + WButton doneButton = new WButton(new TranslatableText("gui.done")); + doneButton.setOnClick(()->{ + MinecraftClient.getInstance().openScreen(previous); + }); + root.add(doneButton, 0, 5, 3, 1); + + root.setBackgroundPainter(BackgroundPainter.VANILLA); + root.setSize(7*18, 6*18); + + root.validate(this); + } +} diff --git a/src/main/java/io/github/cottonmc/cotton/gui/impl/modmenu/ModMenuSupport.java b/src/main/java/io/github/cottonmc/cotton/gui/impl/modmenu/ModMenuSupport.java new file mode 100644 index 0000000..80b57bf --- /dev/null +++ b/src/main/java/io/github/cottonmc/cotton/gui/impl/modmenu/ModMenuSupport.java @@ -0,0 +1,18 @@ +package io.github.cottonmc.cotton.gui.impl.modmenu; + +import net.minecraft.text.TranslatableText; + +import io.github.cottonmc.cotton.gui.client.CottonClientScreen; +import io.github.prospector.modmenu.api.ConfigScreenFactory; +import io.github.prospector.modmenu.api.ModMenuApi; + +public class ModMenuSupport implements ModMenuApi { + @Override + public ConfigScreenFactory getModConfigScreenFactory() { + return screen -> new CottonClientScreen(new TranslatableText("options.libgui.libgui_settings"), new ConfigGui(screen)) { + public void onClose() { + this.client.openScreen(screen); + } + }; + } +} diff --git a/src/main/java/io/github/cottonmc/cotton/gui/impl/modmenu/WKirbSprite.java b/src/main/java/io/github/cottonmc/cotton/gui/impl/modmenu/WKirbSprite.java new file mode 100644 index 0000000..4ea5324 --- /dev/null +++ b/src/main/java/io/github/cottonmc/cotton/gui/impl/modmenu/WKirbSprite.java @@ -0,0 +1,117 @@ +package io.github.cottonmc.cotton.gui.impl.modmenu; + +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.util.Identifier; + +import io.github.cottonmc.cotton.gui.client.LibGui; +import io.github.cottonmc.cotton.gui.client.ScreenDrawing; +import io.github.cottonmc.cotton.gui.widget.WWidget; + +import java.util.ArrayList; + +public class WKirbSprite extends WWidget { + private static final Identifier KIRB = new Identifier("libgui:textures/widget/kirb.png"); + + private static final float PX = 1f/416f; + private static final float KIRB_WIDTH = 32*PX; + + private int currentFrame= 0; + private long currentFrameTime = 0; + private int[] toSleep = { 0, 0, 0, 1, 2, 1, 2, 0, 0, 0, 1, 2, 3 }; + private int[] asleep = { 4, 4, 4, 4, 5, 6, 7, 6, 5 }; + private int[] toAwake = { 3, 3, 8, 8, 8, 8, 8, 8, 8 }; + private int[] awake = { 9, 9, 9, 10, 11, 12 }; + private State state = State.ASLEEP; + private ArrayList pendingFrames = new ArrayList<>(); + + private int frameTime = 300; + private long lastFrame; + + public WKirbSprite() { + state = (LibGui.isDarkMode()) ? State.ASLEEP : State.AWAKE; + } + + public void schedule(int[] frames) { + for(int i : frames) pendingFrames.add(i); + } + + @Override + public boolean canResize() { + return false; + } + + @Override + public int getWidth() { + return 32; + } + + @Override + public int getHeight() { + return 32; + } + + @Environment(EnvType.CLIENT) + @Override + public void paint(MatrixStack matrices, int x, int y, int mouseX, int mouseY) { + long now = System.nanoTime() / 1_000_000L; + + + if (pendingFrames.isEmpty()) { + + if (LibGui.isDarkMode()) { + switch(state) { + case AWAKE: + state = State.FALLING_ASLEEP; + break; + case FALLING_ASLEEP: + state = State.ASLEEP; + break; + default: + //zzzz + state = State.ASLEEP; + break; + } + } else { + switch(state) { + case ASLEEP: + state = State.WAKING_UP; + break; + case WAKING_UP: + state = State.AWAKE; + break; + default: + state = State.AWAKE; + break; + } + } + + switch (state) { + case ASLEEP: schedule(asleep); break; + case WAKING_UP: schedule(toAwake); break; + case AWAKE: schedule(awake); break; + case FALLING_ASLEEP: schedule(toSleep); break; + } + } + + float offset = KIRB_WIDTH * currentFrame; + ScreenDrawing.texturedRect(matrices, x, y+8, 32, 32, KIRB, offset, 0, offset+KIRB_WIDTH, 1, 0xFFFFFFFF); + + long elapsed = now - lastFrame; + currentFrameTime += elapsed; + if (currentFrameTime >= frameTime) { + if (!pendingFrames.isEmpty()) currentFrame = pendingFrames.remove(0); + currentFrameTime = 0; + } + + this.lastFrame = now; + } + + public static enum State { + AWAKE, + FALLING_ASLEEP, + ASLEEP, + WAKING_UP; + } +} diff --git a/src/main/java/io/github/cottonmc/cotton/gui/impl/modmenu/package-info.java b/src/main/java/io/github/cottonmc/cotton/gui/impl/modmenu/package-info.java new file mode 100644 index 0000000..68b85be --- /dev/null +++ b/src/main/java/io/github/cottonmc/cotton/gui/impl/modmenu/package-info.java @@ -0,0 +1,7 @@ +/** + * Mod Menu support for LibGui. + */ +@ApiStatus.Internal +package io.github.cottonmc.cotton.gui.impl.modmenu; + +import org.jetbrains.annotations.ApiStatus; diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java index 1fc6018..78133b5 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java @@ -1,5 +1,7 @@ package io.github.cottonmc.cotton.gui.widget; +import io.github.cottonmc.cotton.gui.impl.client.LibGuiConfig; + import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.MinecraftClient; @@ -33,7 +35,7 @@ public class WLabel extends WWidget { public static final int DEFAULT_TEXT_COLOR = 0x404040; /** - * The default text color for {@linkplain io.github.cottonmc.cotton.gui.client.LibGuiConfig#darkMode dark mode} labels. + * The default text color for {@linkplain LibGuiConfig#darkMode dark mode} labels. */ public static final int DEFAULT_DARKMODE_TEXT_COLOR = 0xbcbcbc; -- cgit