Skip to content

Commit 89b8237

Browse files
committed
serializable drawable implements IJsonSerializable
1 parent f9a0e5c commit 89b8237

File tree

11 files changed

+58
-38
lines changed

11 files changed

+58
-38
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.cleanroommc.modularui.api;
2+
3+
import com.google.gson.JsonObject;
4+
5+
public interface IJsonSerializable {
6+
7+
/**
8+
* Reads extra json data after this drawable is created.
9+
*
10+
* @param json json to read from
11+
*/
12+
default void loadFromJson(JsonObject json) {}
13+
14+
/**
15+
* Writes all json data necessary so that deserializing it results in the same drawable.
16+
*
17+
* @param json json to write to
18+
* @return if the drawable was serialized
19+
*/
20+
default boolean saveToJson(JsonObject json) {
21+
return false;
22+
}
23+
}

src/main/java/com/cleanroommc/modularui/api/drawable/IDrawable.java

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -137,23 +137,6 @@ default Icon asIcon() {
137137
return new Icon(this);
138138
}
139139

140-
/**
141-
* Reads extra json data after this drawable is created.
142-
*
143-
* @param json json to read from
144-
*/
145-
default void loadFromJson(JsonObject json) {}
146-
147-
/**
148-
* Writes all json data necessary so that deserializing it results in the same drawable.
149-
*
150-
* @param json json to write to
151-
* @return if the drawable was serialized
152-
*/
153-
default boolean saveToJson(JsonObject json) {
154-
return false;
155-
}
156-
157140
/**
158141
* An empty drawable. Does nothing.
159142
*/

src/main/java/com/cleanroommc/modularui/api/drawable/IKey.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.cleanroommc.modularui.api.drawable;
22

3+
import com.cleanroommc.modularui.api.IJsonSerializable;
34
import com.cleanroommc.modularui.drawable.Icon;
45
import com.cleanroommc.modularui.drawable.text.*;
56
import com.cleanroommc.modularui.screen.viewport.GuiContext;
@@ -21,7 +22,7 @@
2122
/**
2223
* This represents a piece of text in a GUI.
2324
*/
24-
public interface IKey extends IDrawable {
25+
public interface IKey extends IDrawable, IJsonSerializable {
2526

2627
int TEXT_COLOR = 0xFF404040;
2728

src/main/java/com/cleanroommc/modularui/drawable/Circle.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.cleanroommc.modularui.drawable;
22

3+
import com.cleanroommc.modularui.api.IJsonSerializable;
34
import com.cleanroommc.modularui.api.drawable.IDrawable;
45
import com.cleanroommc.modularui.screen.viewport.GuiContext;
56
import com.cleanroommc.modularui.theme.WidgetTheme;
@@ -12,7 +13,7 @@
1213
import com.google.gson.JsonObject;
1314
import org.jetbrains.annotations.Contract;
1415

15-
public class Circle implements IDrawable {
16+
public class Circle implements IDrawable, IJsonSerializable {
1617

1718
private int colorInner, colorOuter, segments;
1819

src/main/java/com/cleanroommc/modularui/drawable/DrawableArray.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.cleanroommc.modularui.drawable;
22

3+
import com.cleanroommc.modularui.api.IJsonSerializable;
34
import com.cleanroommc.modularui.api.drawable.IDrawable;
45
import com.cleanroommc.modularui.screen.viewport.GuiContext;
56
import com.cleanroommc.modularui.theme.WidgetTheme;
@@ -9,7 +10,7 @@
910
import net.minecraftforge.fml.relauncher.Side;
1011
import net.minecraftforge.fml.relauncher.SideOnly;
1112

12-
public class DrawableArray implements IDrawable {
13+
public class DrawableArray implements IDrawable, IJsonSerializable {
1314

1415
public static final IDrawable[] EMPTY_BACKGROUND = {};
1516

src/main/java/com/cleanroommc/modularui/drawable/DrawableSerialization.java

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.cleanroommc.modularui.drawable;
22

33
import com.cleanroommc.modularui.ModularUI;
4+
import com.cleanroommc.modularui.api.IJsonSerializable;
45
import com.cleanroommc.modularui.api.drawable.IDrawable;
56
import com.cleanroommc.modularui.api.drawable.IKey;
67
import com.cleanroommc.modularui.utils.JsonHelper;
@@ -37,7 +38,7 @@ public static String getTextureId(UITexture texture) {
3738
return REVERSE_TEXTURES.get(texture);
3839
}
3940

40-
public static <T extends IDrawable> void registerDrawableType(String id, Class<T> type, Function<@NotNull JsonObject, @NotNull T> creator) {
41+
public static <T extends IDrawable & IJsonSerializable> void registerDrawableType(String id, Class<T> type, Function<@NotNull JsonObject, @NotNull T> creator) {
4142
if (DRAWABLE_TYPES.containsKey(id)) {
4243
throw new IllegalArgumentException("Drawable type '" + id + "' already exists!");
4344
}
@@ -49,14 +50,11 @@ public static <T extends IDrawable> void registerDrawableType(String id, Class<T
4950

5051
@ApiStatus.Internal
5152
public static void init() {
52-
registerDrawableType("empty", IDrawable.class, json -> IDrawable.EMPTY);
53-
registerDrawableType("null", null, json -> IDrawable.EMPTY);
54-
registerDrawableType("none", null, json -> IDrawable.NONE);
53+
// empty, none and text are special cases
5554
registerDrawableType("texture", UITexture.class, UITexture::parseFromJson);
56-
registerDrawableType("color", null, json -> new Rectangle());
55+
registerDrawableType("color", Rectangle.class, json -> new Rectangle());
5756
registerDrawableType("rectangle", Rectangle.class, json -> new Rectangle());
5857
registerDrawableType("ellipse", Circle.class, json -> new Circle());
59-
registerDrawableType("text", IKey.class, DrawableSerialization::parseText);
6058
registerDrawableType("item", ItemDrawable.class, ItemDrawable::ofJson);
6159
registerDrawableType("icon", Icon.class, Icon::ofJson);
6260
}
@@ -107,12 +105,17 @@ public IDrawable deserialize(JsonElement element, Type typeOfT, JsonDeserializat
107105
return IDrawable.EMPTY;
108106
}
109107
String type = JsonHelper.getString(json, "empty", "type");
108+
if ("text".equals(type)) {
109+
IKey key = parseText(json);
110+
key.loadFromJson(json);
111+
return key;
112+
}
110113
if (!DRAWABLE_TYPES.containsKey(type)) {
111-
ModularUI.LOGGER.throwing(new JsonParseException("Drawable type '" + type + "' is either not specified or invalid!"));
114+
ModularUI.LOGGER.throwing(new JsonParseException("Drawable type '" + type + "' is either not json serializable!"));
112115
return IDrawable.EMPTY;
113116
}
114117
IDrawable drawable = DRAWABLE_TYPES.get(type).apply(json);
115-
drawable.loadFromJson(json);
118+
((IJsonSerializable) drawable).loadFromJson(json);
116119
return drawable;
117120
}
118121

@@ -128,8 +131,12 @@ public JsonElement serialize(IDrawable src, Type typeOfSrc, JsonSerializationCon
128131
return jsonArray;
129132
}
130133
JsonObject json = new JsonObject();
131-
if (src instanceof IKey) {
134+
if (src instanceof IKey key) {
132135
json.addProperty("type", "text");
136+
// TODO serialize text properly
137+
json.addProperty("text", key.getFormatted());
138+
} else if (!(src instanceof IJsonSerializable)) {
139+
throw new IllegalArgumentException("Can't serialize IDrawable which doesn't implement IJsonSerializable!");
133140
} else {
134141
Class<?> type = src.getClass();
135142
String key = REVERSE_DRAWABLE_TYPES.get(type);
@@ -142,10 +149,9 @@ public JsonElement serialize(IDrawable src, Type typeOfSrc, JsonSerializationCon
142149
return JsonNull.INSTANCE;
143150
}
144151
json.addProperty("type", key);
145-
}
146-
147-
if (!src.saveToJson(json)) {
148-
ModularUI.LOGGER.error("Serialization of drawable {} failed!", src.getClass().getSimpleName());
152+
if (!((IJsonSerializable) src).saveToJson(json)) {
153+
ModularUI.LOGGER.error("Serialization of drawable {} failed!", src.getClass().getSimpleName());
154+
}
149155
}
150156
return json;
151157
}

src/main/java/com/cleanroommc/modularui/drawable/Icon.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.cleanroommc.modularui.drawable;
22

3+
import com.cleanroommc.modularui.api.IJsonSerializable;
34
import com.cleanroommc.modularui.api.drawable.IDrawable;
45
import com.cleanroommc.modularui.api.drawable.IIcon;
56
import com.cleanroommc.modularui.screen.viewport.GuiContext;
@@ -16,7 +17,7 @@
1617
/**
1718
* A {@link IDrawable} wrapper with a fixed size and an alignment.
1819
*/
19-
public class Icon implements IIcon {
20+
public class Icon implements IIcon, IJsonSerializable {
2021

2122
private final IDrawable drawable;
2223
private int width = 0, height = 0;

src/main/java/com/cleanroommc/modularui/drawable/IngredientDrawable.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.cleanroommc.modularui.drawable;
22

3+
import com.cleanroommc.modularui.api.IJsonSerializable;
34
import com.cleanroommc.modularui.api.drawable.IDrawable;
45
import com.cleanroommc.modularui.screen.viewport.GuiContext;
56
import com.cleanroommc.modularui.theme.WidgetTheme;
@@ -10,7 +11,7 @@
1011
import net.minecraftforge.fml.relauncher.Side;
1112
import net.minecraftforge.fml.relauncher.SideOnly;
1213

13-
public class IngredientDrawable implements IDrawable {
14+
public class IngredientDrawable implements IDrawable, IJsonSerializable {
1415

1516
private ItemStack[] items;
1617

src/main/java/com/cleanroommc/modularui/drawable/ItemDrawable.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.cleanroommc.modularui.drawable;
22

3+
import com.cleanroommc.modularui.api.IJsonSerializable;
34
import com.cleanroommc.modularui.api.drawable.IDrawable;
45
import com.cleanroommc.modularui.screen.viewport.GuiContext;
56
import com.cleanroommc.modularui.theme.WidgetTheme;
@@ -24,7 +25,7 @@
2425

2526
import java.util.NoSuchElementException;
2627

27-
public class ItemDrawable implements IDrawable {
28+
public class ItemDrawable implements IDrawable, IJsonSerializable {
2829

2930
private ItemStack item = ItemStack.EMPTY;
3031

src/main/java/com/cleanroommc/modularui/drawable/Rectangle.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.cleanroommc.modularui.drawable;
22

3+
import com.cleanroommc.modularui.api.IJsonSerializable;
34
import com.cleanroommc.modularui.api.drawable.IDrawable;
45
import com.cleanroommc.modularui.screen.viewport.GuiContext;
56
import com.cleanroommc.modularui.theme.WidgetTheme;
@@ -14,7 +15,7 @@
1415

1516
import java.util.function.IntConsumer;
1617

17-
public class Rectangle implements IDrawable {
18+
public class Rectangle implements IDrawable, IJsonSerializable {
1819

1920
public static final double PI_2 = Math.PI / 2;
2021

0 commit comments

Comments
 (0)