From 5148b8566687b9366a573e17213df10f76682f51 Mon Sep 17 00:00:00 2001 From: Srendi Date: Sun, 4 Aug 2024 01:21:50 +0200 Subject: [PATCH] [#515] Basic syncing and rendering of our Panel render object for the smart glasses. This is still a prototype and would not support other types of objects (like texts, circles or whatever we plan for the future) --- .../client/ClientRegistry.java | 3 + .../client/screens/SmartGlassesScreen.java | 2 +- .../smartglasses/OverlayModuleOverlay.java | 44 ++++++++ .../smartglasses/OverlayObjectHolder.java | 34 ++++++ .../widgets/SmartGlassesSettingsSwitch.java | 2 +- .../common/network/APNetworking.java | 6 ++ .../toclient/RenderableObjectClearPacket.java | 27 +++++ .../RenderableObjectDeletePacket.java | 29 +++++ .../toclient/RenderableObjectSyncPacket.java | 30 ++++++ .../overlay/OverlayGlassesFunctions.java | 12 ++- .../modules/overlay/OverlayModule.java | 50 ++++++++- .../modules/overlay/OverlayObject.java | 11 +- .../smartglasses/modules/overlay/Panel.java | 25 +---- .../modules/overlay/RenderableObject.java | 100 +++++++++++++++++- .../propertyTypes/FloatingNumberProperty.java | 4 +- .../propertyTypes/FloatingNumberType.java | 8 +- 16 files changed, 345 insertions(+), 42 deletions(-) create mode 100644 src/main/java/de/srendi/advancedperipherals/client/smartglasses/OverlayModuleOverlay.java create mode 100644 src/main/java/de/srendi/advancedperipherals/client/smartglasses/OverlayObjectHolder.java create mode 100644 src/main/java/de/srendi/advancedperipherals/common/network/toclient/RenderableObjectClearPacket.java create mode 100644 src/main/java/de/srendi/advancedperipherals/common/network/toclient/RenderableObjectDeletePacket.java create mode 100644 src/main/java/de/srendi/advancedperipherals/common/network/toclient/RenderableObjectSyncPacket.java diff --git a/src/main/java/de/srendi/advancedperipherals/client/ClientRegistry.java b/src/main/java/de/srendi/advancedperipherals/client/ClientRegistry.java index 9eaf6025f..a6235c32e 100644 --- a/src/main/java/de/srendi/advancedperipherals/client/ClientRegistry.java +++ b/src/main/java/de/srendi/advancedperipherals/client/ClientRegistry.java @@ -7,6 +7,7 @@ import de.srendi.advancedperipherals.client.screens.InventoryManagerScreen; import de.srendi.advancedperipherals.client.screens.SaddleTurtleScreen; import de.srendi.advancedperipherals.client.screens.SmartGlassesScreen; +import de.srendi.advancedperipherals.client.smartglasses.OverlayModuleOverlay; import de.srendi.advancedperipherals.common.setup.APBlockEntityTypes; import de.srendi.advancedperipherals.common.setup.APContainerTypes; import de.srendi.advancedperipherals.common.setup.CCRegistration; @@ -28,6 +29,7 @@ public class ClientRegistry { private static final String[] TURTLE_MODELS = new String[]{"turtle_chat_box_upgrade_left", "turtle_chat_box_upgrade_right", "turtle_environment_upgrade_left", "turtle_environment_upgrade_right", "turtle_player_upgrade_left", "turtle_player_upgrade_right", "turtle_geoscanner_upgrade_left", "turtle_geoscanner_upgrade_right"}; public static final SaddleTurtleScreen SADDLE_TURTLE_OVERLAY = new SaddleTurtleScreen(); + public static final OverlayModuleOverlay OVERLAY_MODULE_OVERLAY = new OverlayModuleOverlay(); @SubscribeEvent public static void registerModels(ModelEvent.RegisterAdditional event) { @@ -71,5 +73,6 @@ public static void registeringRenderers(EntityRenderersEvent.RegisterRenderers e @SubscribeEvent public static void registeringOverlays(RegisterGuiOverlaysEvent event) { event.registerAboveAll(SaddleTurtleScreen.ID, SADDLE_TURTLE_OVERLAY); + event.registerAboveAll(OverlayModuleOverlay.ID, OVERLAY_MODULE_OVERLAY); } } diff --git a/src/main/java/de/srendi/advancedperipherals/client/screens/SmartGlassesScreen.java b/src/main/java/de/srendi/advancedperipherals/client/screens/SmartGlassesScreen.java index 9b4aa2346..c81884b59 100644 --- a/src/main/java/de/srendi/advancedperipherals/client/screens/SmartGlassesScreen.java +++ b/src/main/java/de/srendi/advancedperipherals/client/screens/SmartGlassesScreen.java @@ -67,7 +67,7 @@ protected void renderTooltip(@NotNull PoseStack poseStack, int x, int y) { } @Override - protected void renderLabels(PoseStack poseStack, int x, int y) { + protected void renderLabels(@NotNull PoseStack poseStack, int x, int y) { FormattedCharSequence formattedcharsequence = currentType.getName().getVisualOrderText(); this.font.draw(poseStack, formattedcharsequence, (212 + ComputerSidebar.WIDTH - (float) this.font.width(formattedcharsequence) / 2), 133, 4210752); } diff --git a/src/main/java/de/srendi/advancedperipherals/client/smartglasses/OverlayModuleOverlay.java b/src/main/java/de/srendi/advancedperipherals/client/smartglasses/OverlayModuleOverlay.java new file mode 100644 index 000000000..6b6d74b66 --- /dev/null +++ b/src/main/java/de/srendi/advancedperipherals/client/smartglasses/OverlayModuleOverlay.java @@ -0,0 +1,44 @@ +package de.srendi.advancedperipherals.client.smartglasses; + +import com.mojang.blaze3d.systems.RenderSystem; +import com.mojang.blaze3d.vertex.BufferBuilder; +import com.mojang.blaze3d.vertex.BufferUploader; +import com.mojang.blaze3d.vertex.DefaultVertexFormat; +import com.mojang.blaze3d.vertex.PoseStack; +import com.mojang.blaze3d.vertex.Tesselator; +import com.mojang.blaze3d.vertex.VertexFormat; +import com.mojang.math.Matrix4f; +import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.RenderableObject; +import net.minecraft.client.renderer.GameRenderer; +import net.minecraftforge.client.gui.overlay.ForgeGui; +import net.minecraftforge.client.gui.overlay.IGuiOverlay; + +public class OverlayModuleOverlay implements IGuiOverlay { + public static final String ID = "overlay_module_overlay"; + + @Override + public void render(ForgeGui gui, PoseStack poseStack, float partialTick, int screenWidth, int screenHeight) { + poseStack.pushPose(); + + // Just a simple quads renderer to test the syncing to the client, will evolve while we implement more renderable objects + for (RenderableObject object : OverlayObjectHolder.getObjects()) { + float alpha = object.opacity; + float red = (float) (object.color >> 16 & 255) / 255.0F; + float green = (float) (object.color >> 8 & 255) / 255.0F; + float blue = (float) (object.color & 255) / 255.0F; + + RenderSystem.setShader(GameRenderer::getPositionColorShader); + BufferBuilder bufferbuilder = Tesselator.getInstance().getBuilder(); + Matrix4f matrix = poseStack.last().pose(); + bufferbuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_COLOR); + bufferbuilder.vertex(matrix, (float) object.x, (float) object.maxY, 0f).color(red, green, blue, alpha).endVertex(); + bufferbuilder.vertex(matrix, (float) object.maxX, (float) object.maxY, 0f).color(red, green, blue, alpha).endVertex(); + bufferbuilder.vertex(matrix, (float) object.maxX, (float) object.y, 0f).color(red, green, blue, alpha).endVertex(); + bufferbuilder.vertex(matrix, (float) object.x, (float) object.y, 0f).color(red, green, blue, alpha).endVertex(); + BufferUploader.drawWithShader(bufferbuilder.end()); + } + poseStack.popPose(); + + } + +} diff --git a/src/main/java/de/srendi/advancedperipherals/client/smartglasses/OverlayObjectHolder.java b/src/main/java/de/srendi/advancedperipherals/client/smartglasses/OverlayObjectHolder.java new file mode 100644 index 000000000..85659fb69 --- /dev/null +++ b/src/main/java/de/srendi/advancedperipherals/client/smartglasses/OverlayObjectHolder.java @@ -0,0 +1,34 @@ +package de.srendi.advancedperipherals.client.smartglasses; + +import de.srendi.advancedperipherals.AdvancedPeripherals; +import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.RenderableObject; + +import java.util.ArrayList; +import java.util.List; + +/** + * Static holder for client side renderable objects - will change + */ +public class OverlayObjectHolder { + + public static List objects = new ArrayList<>(); + + public static void addOrUpdateObject(RenderableObject object) { + removeObject(object.getId()); + objects.add(object); + AdvancedPeripherals.debug("Added object to client renderer " + object); + AdvancedPeripherals.debug("Having objects " + objects); + } + + public static List getObjects() { + return objects; + } + + public static void removeObject(String id) { + objects.removeIf(object -> object.getId().equals(id)); + } + + public static void clear() { + objects.clear(); + } +} diff --git a/src/main/java/de/srendi/advancedperipherals/client/widgets/SmartGlassesSettingsSwitch.java b/src/main/java/de/srendi/advancedperipherals/client/widgets/SmartGlassesSettingsSwitch.java index 3ee6e1fb1..a6ab0c0cc 100644 --- a/src/main/java/de/srendi/advancedperipherals/client/widgets/SmartGlassesSettingsSwitch.java +++ b/src/main/java/de/srendi/advancedperipherals/client/widgets/SmartGlassesSettingsSwitch.java @@ -79,7 +79,7 @@ public void renderTooltip(PoseStack poseStack, int x, int y) { } @Override - public void updateNarration(NarrationElementOutput pNarrationElementOutput) { + public void updateNarration(@NotNull NarrationElementOutput pNarrationElementOutput) { } } diff --git a/src/main/java/de/srendi/advancedperipherals/common/network/APNetworking.java b/src/main/java/de/srendi/advancedperipherals/common/network/APNetworking.java index 1a6751ff4..6a11f1fcc 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/network/APNetworking.java +++ b/src/main/java/de/srendi/advancedperipherals/common/network/APNetworking.java @@ -3,6 +3,9 @@ import de.srendi.advancedperipherals.AdvancedPeripherals; import de.srendi.advancedperipherals.common.network.base.IPacket; import de.srendi.advancedperipherals.common.network.toclient.DistanceDetectorSyncPacket; +import de.srendi.advancedperipherals.common.network.toclient.RenderableObjectClearPacket; +import de.srendi.advancedperipherals.common.network.toclient.RenderableObjectDeletePacket; +import de.srendi.advancedperipherals.common.network.toclient.RenderableObjectSyncPacket; import de.srendi.advancedperipherals.common.network.toclient.SaddleTurtleInfoPacket; import de.srendi.advancedperipherals.common.network.toclient.ToastToClientPacket; import de.srendi.advancedperipherals.common.network.toserver.GlassesHotkeyPacket; @@ -38,6 +41,9 @@ public static void init() { registerServerToClient(DistanceDetectorSyncPacket.class, DistanceDetectorSyncPacket::decode); registerServerToClient(SaddleTurtleInfoPacket.class, SaddleTurtleInfoPacket::decode); registerServerToClient(ToastToClientPacket.class, ToastToClientPacket::decode); + registerServerToClient(RenderableObjectSyncPacket.class, RenderableObjectSyncPacket::decode); + registerServerToClient(RenderableObjectDeletePacket.class, RenderableObjectDeletePacket::decode); + registerServerToClient(RenderableObjectClearPacket.class, RenderableObjectClearPacket::decode); registerClientToServer(GlassesHotkeyPacket.class, GlassesHotkeyPacket::decode); registerClientToServer(SaddleTurtleControlPacket.class, SaddleTurtleControlPacket::decode); } diff --git a/src/main/java/de/srendi/advancedperipherals/common/network/toclient/RenderableObjectClearPacket.java b/src/main/java/de/srendi/advancedperipherals/common/network/toclient/RenderableObjectClearPacket.java new file mode 100644 index 000000000..72fd6590a --- /dev/null +++ b/src/main/java/de/srendi/advancedperipherals/common/network/toclient/RenderableObjectClearPacket.java @@ -0,0 +1,27 @@ +package de.srendi.advancedperipherals.common.network.toclient; + +import de.srendi.advancedperipherals.client.smartglasses.OverlayObjectHolder; +import de.srendi.advancedperipherals.common.network.base.IPacket; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraftforge.network.NetworkEvent; + +public class RenderableObjectClearPacket implements IPacket { + + public RenderableObjectClearPacket() { + + } + + @Override + public void handle(NetworkEvent.Context context) { + OverlayObjectHolder.clear(); + } + + @Override + public void encode(FriendlyByteBuf buffer) { + + } + + public static RenderableObjectClearPacket decode(FriendlyByteBuf buffer) { + return new RenderableObjectClearPacket(); + } +} diff --git a/src/main/java/de/srendi/advancedperipherals/common/network/toclient/RenderableObjectDeletePacket.java b/src/main/java/de/srendi/advancedperipherals/common/network/toclient/RenderableObjectDeletePacket.java new file mode 100644 index 000000000..6463b12b1 --- /dev/null +++ b/src/main/java/de/srendi/advancedperipherals/common/network/toclient/RenderableObjectDeletePacket.java @@ -0,0 +1,29 @@ +package de.srendi.advancedperipherals.common.network.toclient; + +import de.srendi.advancedperipherals.client.smartglasses.OverlayObjectHolder; +import de.srendi.advancedperipherals.common.network.base.IPacket; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraftforge.network.NetworkEvent; + +public class RenderableObjectDeletePacket implements IPacket { + + private final String object; + + public RenderableObjectDeletePacket(String object) { + this.object = object; + } + + @Override + public void handle(NetworkEvent.Context context) { + OverlayObjectHolder.removeObject(object); + } + + @Override + public void encode(FriendlyByteBuf buffer) { + buffer.writeUtf(object); + } + + public static RenderableObjectDeletePacket decode(FriendlyByteBuf buffer) { + return new RenderableObjectDeletePacket(buffer.readUtf()); + } +} diff --git a/src/main/java/de/srendi/advancedperipherals/common/network/toclient/RenderableObjectSyncPacket.java b/src/main/java/de/srendi/advancedperipherals/common/network/toclient/RenderableObjectSyncPacket.java new file mode 100644 index 000000000..8feedf25d --- /dev/null +++ b/src/main/java/de/srendi/advancedperipherals/common/network/toclient/RenderableObjectSyncPacket.java @@ -0,0 +1,30 @@ +package de.srendi.advancedperipherals.common.network.toclient; + +import de.srendi.advancedperipherals.client.smartglasses.OverlayObjectHolder; +import de.srendi.advancedperipherals.common.network.base.IPacket; +import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.RenderableObject; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraftforge.network.NetworkEvent; + +public class RenderableObjectSyncPacket implements IPacket { + + private final RenderableObject object; + + public RenderableObjectSyncPacket(RenderableObject object) { + this.object = object; + } + + @Override + public void handle(NetworkEvent.Context context) { + OverlayObjectHolder.addOrUpdateObject(object); + } + + @Override + public void encode(FriendlyByteBuf buffer) { + object.encode(buffer); + } + + public static RenderableObjectSyncPacket decode(FriendlyByteBuf buffer) { + return new RenderableObjectSyncPacket(RenderableObject.decode(buffer)); + } +} diff --git a/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/overlay/OverlayGlassesFunctions.java b/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/overlay/OverlayGlassesFunctions.java index 2ad26b3a8..703e8fcfa 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/overlay/OverlayGlassesFunctions.java +++ b/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/overlay/OverlayGlassesFunctions.java @@ -27,7 +27,7 @@ public final String test() { @LuaFunction public final MethodResult createPanel(String id, IArguments arguments) throws LuaException { Panel panel = new Panel(id, overlayModule, arguments); - Pair success = overlayModule.addObject(panel); + Pair success = overlayModule.addObject(panel); if(!success.getRight()) return MethodResult.of(success.getLeft(), IModule.ErrorConstants.ALREADY_EXISTS); @@ -39,4 +39,14 @@ public final MethodResult getObjects(IArguments arguments) { return MethodResult.of((Object) overlayModule.getObjects().stream().map(OverlayObject::getId).toArray(String[]::new)); } + @LuaFunction + public final MethodResult removeObject(String id) { + return MethodResult.of(overlayModule.removeObject(id)); + } + + @LuaFunction + public final MethodResult clear() { + return MethodResult.of(overlayModule.clear()); + } + } diff --git a/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/overlay/OverlayModule.java b/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/overlay/OverlayModule.java index e7b5c9fe9..2adfb0ebc 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/overlay/OverlayModule.java +++ b/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/overlay/OverlayModule.java @@ -1,11 +1,16 @@ package de.srendi.advancedperipherals.common.smartglasses.modules.overlay; import de.srendi.advancedperipherals.AdvancedPeripherals; +import de.srendi.advancedperipherals.common.network.APNetworking; +import de.srendi.advancedperipherals.common.network.toclient.RenderableObjectClearPacket; +import de.srendi.advancedperipherals.common.network.toclient.RenderableObjectDeletePacket; +import de.srendi.advancedperipherals.common.network.toclient.RenderableObjectSyncPacket; import de.srendi.advancedperipherals.common.smartglasses.SmartGlassesAccess; import de.srendi.advancedperipherals.common.smartglasses.modules.IModule; import de.srendi.advancedperipherals.common.smartglasses.modules.IModuleFunctions; import de.srendi.advancedperipherals.common.util.Pair; import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.level.ServerPlayer; import org.jetbrains.annotations.NotNull; import java.util.concurrent.CopyOnWriteArraySet; @@ -17,7 +22,7 @@ */ public class OverlayModule implements IModule { - public final CopyOnWriteArraySet objects = new CopyOnWriteArraySet<>(); + public final CopyOnWriteArraySet objects = new CopyOnWriteArraySet<>(); public final SmartGlassesAccess access; public OverlayModule(SmartGlassesAccess access) { @@ -47,7 +52,7 @@ public SmartGlassesAccess getAccess() { return access; } - public CopyOnWriteArraySet getObjects() { + public CopyOnWriteArraySet getObjects() { return objects; } @@ -58,12 +63,49 @@ public CopyOnWriteArraySet getObjects() { * @return A pair of the object and a boolean. The boolean is true if the object was added successfully and false if not. * The object is the object which was added or the object which already exists(When not successful). */ - public Pair addObject(OverlayObject object) { - for (OverlayObject overlayObject : objects) { + public Pair addObject(RenderableObject object) { + for (RenderableObject overlayObject : objects) { if (overlayObject.getId().equals(object.getId())) return Pair.of(overlayObject, false); } + APNetworking.sendTo(new RenderableObjectSyncPacket(object), (ServerPlayer) access.getEntity()); objects.add(object); return Pair.of(object, true); } + + /** + * Removes an object from the module if it exists and updates the client. + * + * @param id the object id + * @return true if the object existed and was removed, false if the object was not in the collection + */ + public boolean removeObject(String id) { + boolean removed = objects.removeIf(object -> object.getId().equals(id)); + + if (removed) + APNetworking.sendTo(new RenderableObjectDeletePacket(id), (ServerPlayer) access.getEntity()); + + return removed; + } + + /** + * Removes all objects from the module + * + * @return the amount of objects cleared + */ + public int clear() { + int size = objects.size(); + objects.clear(); + APNetworking.sendTo(new RenderableObjectClearPacket(), (ServerPlayer) access.getEntity()); + return size; + } + + /** + * Just sends a sync package to the client, this method should only be called from the setter lua functions from our objects + * + * @param object the object to sync to the player + */ + public void update(RenderableObject object) { + APNetworking.sendTo(new RenderableObjectSyncPacket(object), (ServerPlayer) access.getEntity()); + } } diff --git a/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/overlay/OverlayObject.java b/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/overlay/OverlayObject.java index ff584021d..ae124e23b 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/overlay/OverlayObject.java +++ b/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/overlay/OverlayObject.java @@ -179,7 +179,7 @@ protected void handle(NetworkEvent.Context context) { } - protected void encode(FriendlyByteBuf buffer) { + public void encode(FriendlyByteBuf buffer) { buffer.writeUtf(id); Entity entity = module.getAccess().getEntity(); if(entity instanceof Player player) { @@ -191,4 +191,13 @@ protected void encode(FriendlyByteBuf buffer) { } } + @Override + public String toString() { + return "OverlayObject{" + + "enabled=" + enabled + + ", id='" + id + '\'' + + ", module=" + module + + ", player=" + player + + '}'; + } } diff --git a/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/overlay/Panel.java b/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/overlay/Panel.java index 562c07f10..122f188df 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/overlay/Panel.java +++ b/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/overlay/Panel.java @@ -2,10 +2,8 @@ import dan200.computercraft.api.lua.IArguments; import dan200.computercraft.api.lua.LuaException; -import de.srendi.advancedperipherals.AdvancedPeripherals; import net.minecraft.network.FriendlyByteBuf; import net.minecraftforge.network.NetworkEvent; -import org.jetbrains.annotations.Nullable; import java.util.UUID; @@ -29,29 +27,8 @@ public Panel(String id, UUID player) { } @Override - protected void encode(FriendlyByteBuf buffer) { + public void encode(FriendlyByteBuf buffer) { super.encode(buffer); - buffer.writeInt(color); - buffer.writeDouble(opacity); - } - - @Nullable - public static Panel decode(FriendlyByteBuf buffer) { - String id = buffer.readUtf(); - boolean hasValidUUID = buffer.readBoolean(); - if (!hasValidUUID) { - AdvancedPeripherals.exception("Tried to decode a buffer for an OverlayObject but without a valid player as target.", new IllegalArgumentException()); - return null; - } - UUID player = buffer.readUUID(); - int color = buffer.readInt(); - double opacity = buffer.readDouble(); - - Panel clientPanel = new Panel(id, player); - clientPanel.color = color; - clientPanel.opacity = opacity; - - return clientPanel; } @Override diff --git a/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/overlay/RenderableObject.java b/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/overlay/RenderableObject.java index 01cad70a2..6e12719e3 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/overlay/RenderableObject.java +++ b/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/overlay/RenderableObject.java @@ -3,27 +3,43 @@ import dan200.computercraft.api.lua.IArguments; import dan200.computercraft.api.lua.LuaException; import dan200.computercraft.api.lua.LuaFunction; +import de.srendi.advancedperipherals.AdvancedPeripherals; import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.propertyTypes.FixedPointNumberProperty; import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.propertyTypes.FloatingNumberProperty; +import net.minecraft.network.FriendlyByteBuf; import java.util.UUID; -public abstract class RenderableObject extends OverlayObject { +// TODO: generate setters/getters lua functions out of our FloatingNumberProperty fields +public class RenderableObject extends OverlayObject { @FloatingNumberProperty(min = 0, max = 1) - public double opacity = 1; + public float opacity = 1; @FixedPointNumberProperty(min = 0, max = 0xFFFFFF) public int color = 0xFFFFFF; + @FixedPointNumberProperty(min = -32767, max = 32767) + public int x = 0; + + @FixedPointNumberProperty(min = -32767, max = 32767) + public int y = 0; + + @FixedPointNumberProperty(min = -32767, max = 32767) + public int maxX = 0; + + @FixedPointNumberProperty(min = -32767, max = 32767) + public int maxY = 0; + @LuaFunction - public double getOpacity() { + public float getOpacity() { return opacity; } @LuaFunction - public void setOpacity(double opacity) { + public void setOpacity(float opacity) { this.opacity = opacity; + getModule().update(this); } @LuaFunction @@ -34,6 +50,31 @@ public int getColor() { @LuaFunction public void setColor(int color) { this.color = color; + getModule().update(this); + } + + @LuaFunction + public void setMaxX(int maxX) { + this.maxX = maxX; + getModule().update(this); + } + + @LuaFunction + public void setMaxY(int maxY) { + this.maxY = maxY; + getModule().update(this); + } + + @LuaFunction + public void setX(int x) { + this.x = x; + getModule().update(this); + } + + @LuaFunction + public void setY(int y) { + this.y = y; + getModule().update(this); } public RenderableObject(String id, OverlayModule module, IArguments arguments) throws LuaException { @@ -44,4 +85,55 @@ public RenderableObject(String id, OverlayModule module, IArguments arguments) t public RenderableObject(String id, UUID player) { super(id, player); } + + @Override + public void encode(FriendlyByteBuf buffer) { + super.encode(buffer); + buffer.writeInt(color); + buffer.writeFloat(opacity); + + buffer.writeInt(x); + buffer.writeInt(y); + buffer.writeInt(maxX); + buffer.writeInt(maxY); + } + + public static RenderableObject decode(FriendlyByteBuf buffer) { + String id = buffer.readUtf(); + boolean hasValidUUID = buffer.readBoolean(); + if (!hasValidUUID) { + AdvancedPeripherals.exception("Tried to decode a buffer for an OverlayObject but without a valid player as target.", new IllegalArgumentException()); + return null; + } + UUID player = buffer.readUUID(); + int color = buffer.readInt(); + float opacity = buffer.readFloat(); + + int x = buffer.readInt(); + int y = buffer.readInt(); + int sizeX = buffer.readInt(); + int sizeY = buffer.readInt(); + + RenderableObject clientObject = new RenderableObject(id, player); + clientObject.color = color; + clientObject.opacity = opacity; + clientObject.x = x; + clientObject.y = y; + clientObject.maxX = sizeX; + clientObject.maxY = sizeY; + + return clientObject; + } + + @Override + public String toString() { + return "RenderableObject{" + + "opacity=" + opacity + + ", color=" + color + + ", x=" + x + + ", y=" + y + + ", sizeX=" + maxX + + ", sizeY=" + maxY + + '}'; + } } diff --git a/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/overlay/propertyTypes/FloatingNumberProperty.java b/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/overlay/propertyTypes/FloatingNumberProperty.java index e377472f3..6199d34a1 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/overlay/propertyTypes/FloatingNumberProperty.java +++ b/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/overlay/propertyTypes/FloatingNumberProperty.java @@ -12,7 +12,7 @@ @ObjectProperty(type = FloatingNumberType.class) public @interface FloatingNumberProperty { - double min() default Double.MIN_VALUE; - double max() default Double.MAX_VALUE; + float min() default Float.MIN_VALUE; + float max() default Float.MAX_VALUE; } diff --git a/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/overlay/propertyTypes/FloatingNumberType.java b/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/overlay/propertyTypes/FloatingNumberType.java index 05d6eb8df..c5089f2ca 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/overlay/propertyTypes/FloatingNumberType.java +++ b/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/overlay/propertyTypes/FloatingNumberType.java @@ -4,8 +4,8 @@ public class FloatingNumberType implements PropertyType { - public double min; - public double max; + public float min; + public float max; @Override public boolean checkIsValid(Object type) { @@ -14,8 +14,8 @@ public boolean checkIsValid(Object type) { @Override public Number mapValue(Object type) { - AdvancedPeripherals.debug("Mapping value " + type + " to " + Math.min(Math.max((double) type, min), max)); - return Math.min(Math.max((double) type, min), max); + AdvancedPeripherals.debug("Mapping value " + type + " to " + Math.min(Math.max((float) type, min), max)); + return Math.min(Math.max((float) type, min), max); } @Override