Skip to content

Commit

Permalink
Render active script in terminal
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Oct 28, 2023
1 parent 44a915d commit e3ff3cc
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import javax.annotation.Nullable;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Expand All @@ -37,6 +38,10 @@ public class ContainerScreenTerminalScripting extends ContainerScreenExtended<Co
public static int PATHS_HEIGHT = 230;
public static int PATHS_ROW_HEIGHT = 5;
public static int PATHS_MAX_ROWS = PATHS_HEIGHT / PATHS_ROW_HEIGHT;
public static int SCRIPT_X = 88;
public static int SCRIPT_Y = 18;
public static int SCRIPT_WIDTH = 160;
public static int SCRIPT_HEIGHT = 131;

private final Player player;

Expand Down Expand Up @@ -104,46 +109,76 @@ protected void renderBg(PoseStack matrixStack, float partialTicks, int mouseX, i
scrollBar.drawGuiContainerBackgroundLayer(matrixStack, partialTicks, mouseX, mouseY);

if (!this.getMenu().getAvailableDisks().isEmpty()) {
this.renderScripts(matrixStack, partialTicks, mouseX, mouseY);
this.renderScriptPaths(matrixStack, partialTicks, mouseX, mouseY);
} else {
// Gray-out editor and file list
RenderSystem.setShaderColor(0.3F, 0.3F, 0.3F, 0.3F);
fill(matrixStack, leftPos + 88, topPos + 18, leftPos + 88 + 160, topPos + 18 + 131, Helpers.RGBAToInt(50, 50, 50, 100));
fill(matrixStack, leftPos + PATHS_X, topPos + PATHS_Y, leftPos + PATHS_X + PATHS_WIDTH, topPos + PATHS_Y + PATHS_HEIGHT, Helpers.RGBAToInt(50, 50, 50, 100));
RenderSystem.setShaderColor(1, 1, 1, 1);
}

if (this.getMenu().getActiveScript() != null) {
this.renderActiveScript(matrixStack, partialTicks, mouseX, mouseY, this.getMenu().getActiveScript());
} else {
// Gray-out editor and file list
RenderSystem.setShaderColor(0.3F, 0.3F, 0.3F, 0.3F);
fill(matrixStack, leftPos + SCRIPT_X, topPos + SCRIPT_Y, leftPos + SCRIPT_X + SCRIPT_WIDTH, topPos + SCRIPT_Y + SCRIPT_HEIGHT, Helpers.RGBAToInt(50, 50, 50, 100));
RenderSystem.setShaderColor(1, 1, 1, 1);
}
}

@Nullable
protected Map<Path, String> getActiveScripts() {
return this.container.getLastScripts().get(this.container.getActiveDisk());
}

protected void renderScripts(PoseStack poseStack, float partialTicks, int mouseX, int mouseY) {
protected List<Path> getVisibleScriptPaths() {
Map<Path, String> scripts = getActiveScripts();
if (scripts != null) {
List<Path> paths = scripts.keySet().stream().sorted().collect(Collectors.toList());
if (!paths.isEmpty()) {
List<Path> pathsSubList = paths.subList(
return paths.subList(
Math.max(0, this.firstRow),
Math.max(0, this.firstRow) + Math.min(paths.size(), scrollBar.getVisibleRows())
);
int i = 0;
for (Path path : pathsSubList) {
boolean hovering = isHovering(PATHS_X, PATHS_Y + i * PATHS_ROW_HEIGHT, PATHS_WIDTH, PATHS_ROW_HEIGHT, mouseX, mouseY);
RenderHelpers.drawScaledString(
poseStack,
font,
StringUtil.truncateStringIfNecessary(path.toString(), 50, true),
this.leftPos + PATHS_X + 1,
this.topPos + PATHS_Y + i * PATHS_ROW_HEIGHT + 1,
0.5f,
hovering ? Helpers.RGBToInt(50, 50, 250) : Helpers.RGBToInt(0, 0, 0)
);
i++;
}
}
}
return Collections.emptyList();
}

protected void renderScriptPaths(PoseStack poseStack, float partialTicks, int mouseX, int mouseY) {
List<Path> paths = getVisibleScriptPaths();
int i = 0;
for (Path path : paths) {
boolean hovering = isHovering(PATHS_X, PATHS_Y + i * PATHS_ROW_HEIGHT, PATHS_WIDTH, PATHS_ROW_HEIGHT, mouseX, mouseY);
RenderHelpers.drawScaledString(
poseStack,
font,
StringUtil.truncateStringIfNecessary(path.toString(), 50, true),
this.leftPos + PATHS_X + 1,
this.topPos + PATHS_Y + i * PATHS_ROW_HEIGHT + 1,
0.5f,
hovering ? Helpers.RGBToInt(50, 50, 250) : Helpers.RGBToInt(0, 0, 0)
);
i++;
}
}

@Nullable
protected Path getHoveredScriptPath(double mouseX, double mouseY) {
List<Path> paths = getVisibleScriptPaths();
int i = 0;
for (Path path : paths) {
if (isHovering(PATHS_X, PATHS_Y + i * PATHS_ROW_HEIGHT, PATHS_WIDTH, PATHS_ROW_HEIGHT, mouseX, mouseY)) {
return path;
}
i++;
}
return null;
}

protected void renderActiveScript(PoseStack poseStack, float partialTicks, int mouseX, int mouseY, String script) {
font.draw(poseStack, script, this.leftPos + SCRIPT_X + 1, this.topPos + SCRIPT_Y + 1, Helpers.RGBToInt(0, 0, 0));
}

@Override
Expand All @@ -156,6 +191,12 @@ protected void renderLabels(PoseStack poseStack, int mouseX, int mouseY) {

@Override
public boolean mouseClicked(double mouseX, double mouseY, int mouseButton) {
Path hoveredScriptPath = getHoveredScriptPath(mouseX, mouseY);
if (hoveredScriptPath != null) {
getMenu().setActiveScriptPath(hoveredScriptPath);
return true;
}

// Update channel when changing channel field
if (this.fieldDisk.mouseClicked(mouseX, mouseY, mouseButton)) {
int disk;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class ContainerTerminalScripting extends InventoryContainer {
private final Int2ObjectMap<Map<Path, String>> lastScripts = new Int2ObjectAVLTreeMap<>();
private IntList availableDisks;
private int activeDisk;
private Path activeScriptPath;

public ContainerTerminalScripting(int id, Inventory playerInventory, FriendlyByteBuf packetBuffer) {
this(id, playerInventory, PartHelpers.readPartTarget(packetBuffer), Optional.empty(),
Expand All @@ -68,6 +69,7 @@ public ContainerTerminalScripting(int id, Inventory playerInventory,

this.availableDisks = initData.getAvailableDisks();
this.activeDisk = this.availableDisks.isEmpty() ? -1 : this.availableDisks.getInt(0);
this.activeScriptPath = null;
}

public Level getLevel() {
Expand Down Expand Up @@ -197,6 +199,28 @@ public void setServerScript(int disk, Path path, @Nullable String script) {
ScriptingNetworkHelpers.getScriptingData().setScript(disk, path, script, IScriptingData.ChangeLocation.MEMORY);
}

@Nullable
public Path getActiveScriptPath() {
return activeScriptPath;
}

public void setActiveScriptPath(Path activeScriptPath) {
this.activeScriptPath = activeScriptPath;
}

@Nullable
public String getActiveScript() {
Path path = getActiveScriptPath();
int disk = getActiveDisk();
if (path != null && disk >= 0) {
Map<Path, String> diskScripts = getLastScripts().get(disk);
if (diskScripts != null) {
return diskScripts.get(path);
}
}
return null;
}

public static class InitData {

private final IntList availableDisks;
Expand Down

0 comments on commit e3ff3cc

Please sign in to comment.