Skip to content

Commit

Permalink
[#515] Basic syncing and rendering of our Panel render object for the…
Browse files Browse the repository at this point in the history
… 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)
  • Loading branch information
SirEndii committed Aug 3, 2024
1 parent 3d9689c commit 5148b85
Show file tree
Hide file tree
Showing 16 changed files with 345 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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();

}

}
Original file line number Diff line number Diff line change
@@ -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<RenderableObject> 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<RenderableObject> getObjects() {
return objects;
}

public static void removeObject(String id) {
objects.removeIf(object -> object.getId().equals(id));
}

public static void clear() {
objects.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<OverlayObject, Boolean> success = overlayModule.addObject(panel);
Pair<RenderableObject, Boolean> success = overlayModule.addObject(panel);
if(!success.getRight())
return MethodResult.of(success.getLeft(), IModule.ErrorConstants.ALREADY_EXISTS);

Expand All @@ -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());
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,7 +22,7 @@
*/
public class OverlayModule implements IModule {

public final CopyOnWriteArraySet<OverlayObject> objects = new CopyOnWriteArraySet<>();
public final CopyOnWriteArraySet<RenderableObject> objects = new CopyOnWriteArraySet<>();
public final SmartGlassesAccess access;

public OverlayModule(SmartGlassesAccess access) {
Expand Down Expand Up @@ -47,7 +52,7 @@ public SmartGlassesAccess getAccess() {
return access;
}

public CopyOnWriteArraySet<OverlayObject> getObjects() {
public CopyOnWriteArraySet<RenderableObject> getObjects() {
return objects;
}

Expand All @@ -58,12 +63,49 @@ public CopyOnWriteArraySet<OverlayObject> 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<OverlayObject, Boolean> addObject(OverlayObject object) {
for (OverlayObject overlayObject : objects) {
public Pair<RenderableObject, Boolean> 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -191,4 +191,13 @@ protected void encode(FriendlyByteBuf buffer) {
}
}

@Override
public String toString() {
return "OverlayObject{" +
"enabled=" + enabled +
", id='" + id + '\'' +
", module=" + module +
", player=" + player +
'}';
}
}
Loading

0 comments on commit 5148b85

Please sign in to comment.