Skip to content

Commit

Permalink
add stuff from docs
Browse files Browse the repository at this point in the history
(cherry picked from commit 943b4de)
  • Loading branch information
brachy84 authored and miozune committed Nov 5, 2023
1 parent 1342f3d commit 0dfc2a4
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

import java.lang.reflect.Type;
import java.util.ArrayList;
Expand All @@ -25,7 +26,7 @@ public class DrawableSerialization implements JsonSerializer<IDrawable>, JsonDes

private static final Map<String, Function<JsonObject, IDrawable>> DRAWABLE_TYPES = new HashMap<>();

public static void registerDrawableType(String id, Function<JsonObject, IDrawable> creator) {
public static void registerDrawableType(String id, Function<@NotNull JsonObject, @NotNull IDrawable> creator) {
if (DRAWABLE_TYPES.containsKey(id)) {
throw new IllegalArgumentException("Drawable type '" + id + "' already exists!");
}
Expand All @@ -41,6 +42,8 @@ public static void init() {
registerDrawableType("rectangle", json -> new Rectangle());
registerDrawableType("ellipse", json -> new Circle());
registerDrawableType("text", DrawableSerialization::parseText);
registerDrawableType("item", ItemDrawable::ofJson);
registerDrawableType("icon", Icon::ofJson);
}

@Override
Expand Down
36 changes: 33 additions & 3 deletions src/main/java/com/cleanroommc/modularui/drawable/Icon.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import com.cleanroommc.modularui.api.drawable.IIcon;
import com.cleanroommc.modularui.screen.viewport.GuiContext;
import com.cleanroommc.modularui.utils.Alignment;
import com.cleanroommc.modularui.utils.JsonHelper;
import com.cleanroommc.modularui.widget.sizer.Box;
import com.google.gson.JsonObject;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

Expand All @@ -14,7 +16,7 @@
public class Icon implements IIcon {

private final IDrawable drawable;
private int width = 18, height = 18;
private int width = 0, height = 0;
private Alignment alignment = Alignment.Center;
private final Box margin = new Box();

Expand Down Expand Up @@ -60,12 +62,12 @@ public Alignment getAlignment() {
}

public Icon width(int width) {
this.width = width;
this.width = Math.max(0, width);
return this;
}

public Icon height(int height) {
this.height = height;
this.height = Math.max(0, height);
return this;
}

Expand Down Expand Up @@ -116,4 +118,32 @@ public Icon marginBottom(int val) {
this.margin.bottom(val);
return this;
}

@Override
public void loadFromJson(JsonObject json) {
this.width = (json.has("autoWidth") || json.has("autoSize")) &&
JsonHelper.getBoolean(json, true, "autoWidth", "autoSize") ? 0 :
JsonHelper.getInt(json, 0, "width", "w", "size");
this.height = (json.has("autoHeight") || json.has("autoSize")) &&
JsonHelper.getBoolean(json, true, "autoHeight", "autoSize") ? 0 :
JsonHelper.getInt(json, 0, "height", "h", "size");
this.alignment = JsonHelper.deserialize(json, Alignment.class, Alignment.Center, "alignment", "align");
this.margin.all(JsonHelper.getInt(json, 0, "margin"));
if (json.has("marginHorizontal")) {
this.margin.left = json.get("marginHorizontal").getAsInt();
this.margin.right = this.margin.left;
}
if (json.has("marginVertical")) {
this.margin.top = json.get("marginVertical").getAsInt();
this.margin.bottom = this.margin.top;
}
this.margin.top = JsonHelper.getInt(json, this.margin.top, "marginTop");
this.margin.bottom = JsonHelper.getInt(json, this.margin.bottom, "marginBottom");
this.margin.left = JsonHelper.getInt(json, this.margin.left, "marginLeft");
this.margin.right = JsonHelper.getInt(json, this.margin.right, "marginRight");
}

public static Icon ofJson(JsonObject json) {
return JsonHelper.deserialize(json, IDrawable.class, IDrawable.EMPTY, "drawable", "icon").asIcon();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,24 @@
import com.cleanroommc.modularui.api.drawable.IDrawable;
import com.cleanroommc.modularui.screen.GuiScreenWrapper;
import com.cleanroommc.modularui.screen.viewport.GuiContext;
import com.cleanroommc.modularui.utils.GameObjectHelper;
import com.cleanroommc.modularui.utils.JsonHelper;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.RenderHelper;
import net.minecraft.client.renderer.entity.RenderItem;
import net.minecraft.item.ItemStack;
import org.jetbrains.annotations.Nullable;
import net.minecraft.nbt.JsonToNBT;
import net.minecraft.nbt.NBTException;
import net.minecraft.nbt.NBTTagCompound;
import org.lwjgl.opengl.GL11;

import javax.annotation.Nullable;
import java.util.NoSuchElementException;

public class ItemDrawable implements IDrawable {

private ItemStack item = null;
Expand Down Expand Up @@ -51,4 +60,35 @@ public ItemDrawable setItem(@Nullable ItemStack item) {
this.item = item;
return this;
}

public static ItemDrawable ofJson(JsonObject json) {
String itemS = JsonHelper.getString(json, null, "item");
if (itemS == null) throw new JsonParseException("Item property not found!");
String[] parts = itemS.split(":");
if (parts.length < 2)
throw new JsonParseException("Item property must have be in the format 'mod:item_name:meta'");
int meta = 0;
if (parts.length > 2) {
try {
meta = Integer.parseInt(parts[2]);
} catch (NumberFormatException e) {
throw new JsonParseException(e);
}
}
ItemStack item;
try {
item = GameObjectHelper.getItemStack(parts[0], parts[1], meta);
} catch (NoSuchElementException e) {
throw new JsonParseException(e);
}
if (json.has("nbt")) {
try {
NBTTagCompound nbt = (NBTTagCompound) JsonToNBT.func_150315_a(JsonHelper.getObject(json, new JsonObject(), o -> o, "nbt").toString());
item.setTagCompound(nbt);
} catch (NBTException e) {
throw new JsonParseException(e);
}
}
return new ItemDrawable(item);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.cleanroommc.modularui.utils;

import cpw.mods.fml.common.registry.GameRegistry;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;

import java.util.NoSuchElementException;

public class GameObjectHelper {

public static ItemStack getItemStack(String mod, String path) {
return getItemStack(mod, path, 0);
}

public static ItemStack getItemStack(String mod, String path, int meta) {
ItemStack item = GameRegistry.findItemStack(mod, path, 1);
if (item == null) throw new NoSuchElementException("Item '" + mod + ":" + path + "' was not found!");
Items.feather.setDamage(item, meta);
return item;
}
}

0 comments on commit 0dfc2a4

Please sign in to comment.