Skip to content

Commit

Permalink
Honestly quite incredible
Browse files Browse the repository at this point in the history
  • Loading branch information
enjarai committed Nov 23, 2023
1 parent baf5e45 commit 0ee58fd
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class ModCanvasUtils {
public static final CanvasImage LOCK_ICON_UNLOCKED = loadImage("lock_icon_unlocked.png");
public static final CanvasImage ENERGY_CONDUITS_ACTIVE = loadImage("energy_conduits_active.png");
public static final CanvasImage ENERGY_CONDUITS_INACTIVE = loadImage("energy_conduits_inactive.png");
public static final CanvasImage HISTORY_CURRENT_OUTLINE = loadImage("history_current_outline.png");
public static final CanvasImage SCANNER_APP = loadImage("app/scanner.png");
public static final CanvasImage GPS_APP = loadImage("app/gps.png");
public static final CanvasImage BAD_APPLE_APP = loadImage("app/bad_apple.png");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.mojang.serialization.Codec;
import dev.enjarai.minitardis.MiniTardis;
import dev.enjarai.minitardis.ModSounds;
import dev.enjarai.minitardis.component.HistoryEntry;
import dev.enjarai.minitardis.component.Tardis;
import net.minecraft.sound.SoundCategory;
import net.minecraft.util.Identifier;
Expand All @@ -29,6 +30,7 @@ public void init(Tardis tardis) {

@Override
public void complete(Tardis tardis) {
tardis.getCurrentLocation().ifPresent(location -> tardis.addHistoryEntry(new HistoryEntry(location)));
tardis.setCurrentLocation(Optional.empty());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@ public <T extends AppElement> T addElement(T element) {
return element;
}

public Iterable<AppElement> children(TardisControl controls) {
return children;
}

@Override
public void draw(TardisControl controls, ConsoleScreenBlockEntity blockEntity, DrawableCanvas canvas) {
children.forEach(el -> el.draw(controls, canvas));
children(controls).forEach(el -> el.draw(controls, canvas));
}

@Override
public boolean onClick(TardisControl controls, ConsoleScreenBlockEntity blockEntity, ServerPlayerEntity player, ClickType type, int x, int y) {
for (var element : children) {
for (var element : children(controls)) {
if (element.onClick(controls, blockEntity, player, type, x, y)) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,73 @@
package dev.enjarai.minitardis.component.screen.app;

import com.google.common.collect.Iterables;
import com.mojang.serialization.Codec;
import dev.enjarai.minitardis.MiniTardis;
import dev.enjarai.minitardis.block.console.ConsoleScreenBlockEntity;
import dev.enjarai.minitardis.canvas.ModCanvasUtils;
import dev.enjarai.minitardis.component.TardisControl;
import dev.enjarai.minitardis.component.TardisLocation;
import dev.enjarai.minitardis.component.screen.element.AppElement;
import dev.enjarai.minitardis.component.screen.element.SmallButtonElement;
import eu.pb4.mapcanvas.api.core.CanvasColor;
import eu.pb4.mapcanvas.api.core.DrawableCanvas;
import eu.pb4.mapcanvas.api.font.DefaultFonts;
import eu.pb4.mapcanvas.api.utils.CanvasUtils;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.ClickType;
import net.minecraft.util.Identifier;

import java.util.List;
import java.util.Optional;

public class HistoryApp extends ElementHoldingApp {
public static final Codec<HistoryApp> CODEC = Codec.unit(HistoryApp::new);
public static final Codec<HistoryApp> CODEC = Codec.INT.xmap(HistoryApp::new, a -> a.currentPage).fieldOf("current_page").codec();
public static final Identifier ID = MiniTardis.id("history");
private static final int ENTRIES_PER_PAGE = 3;

private int currentPage;

private final List<SmallButtonElement> shownEntryButtons = List.of(
new SmallButtonElement(98, 28, "Set", controls -> controls.getTardis()
.setDestination(controls.getTardis().getHistory().get(currentPage * ENTRIES_PER_PAGE).location(), false)),
new SmallButtonElement(98, 54, "Set", controls -> controls.getTardis()
.setDestination(controls.getTardis().getHistory().get(currentPage * ENTRIES_PER_PAGE + 1).location(), false)),
new SmallButtonElement(98, 80, "Set", controls -> controls.getTardis()
.setDestination(controls.getTardis().getHistory().get(currentPage * ENTRIES_PER_PAGE + 2).location(), false))
);

private HistoryApp(int currentPage) {
this.currentPage = currentPage;

addElement(new SmallButtonElement(2, 2, "Prev", controls -> this.currentPage = Math.max(this.currentPage - 1, 0)));
addElement(new SmallButtonElement(30, 2, "Next", controls -> this.currentPage = Math.min(this.currentPage + 1, (controls.getTardis().getHistory().size() - 1) / ENTRIES_PER_PAGE)));
}

public HistoryApp() {
this(0);
}

@Override
public Iterable<AppElement> children(TardisControl controls) {
return Iterables.concat(super.children(controls), shownEntryButtons.subList(0, Math.min(controls.getTardis().getHistory().size() - currentPage * ENTRIES_PER_PAGE, 3)));
}

@Override
public void draw(TardisControl controls, ConsoleScreenBlockEntity blockEntity, DrawableCanvas canvas) {
var current = controls.getTardis().getCurrentLocation();
DefaultFonts.VANILLA.drawText(canvas, "Current Location", 3, 4, 8, CanvasColor.WHITE_HIGH);
drawLocation(current, canvas, 3, 4 + 20);

var destination = controls.getTardis().getDestination();
DefaultFonts.VANILLA.drawText(canvas, "Destination", 3, 4 + 41, 8, CanvasColor.WHITE_HIGH);
drawLocation(destination, canvas, 3, 4 + 61);

var isLocked = controls.isDestinationLocked();
var color = isLocked ? CanvasColor.LIME_HIGH : CanvasColor.RED_HIGH;
// CanvasUtils.fill(canvas, 2, 84, 126, 94, color);
var lockedText = isLocked ? ">> Locked <<" : "|| Unlocked ||";
var lockedWidth = DefaultFonts.VANILLA.getTextWidth(lockedText, 8);
DefaultFonts.VANILLA.drawText(canvas, lockedText, 64 - lockedWidth / 2, 86, 8, color);
var history = controls.getTardis().getHistory();

DefaultFonts.VANILLA.drawText(canvas, this.currentPage + 1 + "/" + ((history.size() - 1) / ENTRIES_PER_PAGE + 1), 62, 7, 8, CanvasColor.WHITE_HIGH);

for (int i = currentPage * ENTRIES_PER_PAGE; i < Math.min((currentPage * ENTRIES_PER_PAGE) + ENTRIES_PER_PAGE, history.size()); i++) {
var relativeIndex = i - currentPage * ENTRIES_PER_PAGE;
var entry = history.get(i);

if (entry.location().equals(controls.getTardis().getDestination().orElse(null))) {
CanvasUtils.draw(canvas, 2, 18 + 26 * relativeIndex, ModCanvasUtils.HISTORY_CURRENT_OUTLINE);
}

var numText = (i + 1) + ".";
var numWidth = DefaultFonts.VANILLA.getTextWidth(numText, 8);
DefaultFonts.VANILLA.drawText(canvas, numText, 4, 20 + 26 * relativeIndex, 8, CanvasColor.WHITE_HIGH);
GpsApp.drawLocation(Optional.of(entry.location()), canvas, 4 + numWidth + 4, 20 + 26 * relativeIndex);
}

super.draw(controls, blockEntity, canvas);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import dev.enjarai.minitardis.canvas.ModCanvasUtils;
import dev.enjarai.minitardis.component.DestinationScanner;
import dev.enjarai.minitardis.component.TardisControl;
import dev.enjarai.minitardis.component.screen.element.SideButtonElement;
import dev.enjarai.minitardis.component.screen.element.SmallButtonElement;
import eu.pb4.mapcanvas.api.core.CanvasColor;
import eu.pb4.mapcanvas.api.core.DrawableCanvas;
import eu.pb4.mapcanvas.api.utils.CanvasUtils;
Expand All @@ -18,8 +18,8 @@ public class ScannerApp extends ElementHoldingApp {
public static final Identifier ID = MiniTardis.id("scanner");

public ScannerApp() {
addElement(new SideButtonElement(96 + 2, 2 + 14, "XAxis", controls -> controls.getTardis().getDestinationScanner().useXAxis()));
addElement(new SideButtonElement(96 + 2, 2 + 14 + 14, "ZAxis", controls -> controls.getTardis().getDestinationScanner().useZAxis()));
addElement(new SmallButtonElement(96 + 2, 2 + 14, "XAxis", controls -> controls.getTardis().getDestinationScanner().useXAxis()));
addElement(new SmallButtonElement(96 + 2, 2 + 14 + 14, "ZAxis", controls -> controls.getTardis().getDestinationScanner().useZAxis()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ public interface ScreenApp {
ScannerApp.ID, ScannerApp.CODEC,
GpsApp.ID, GpsApp.CODEC,
BadAppleApp.ID, BadAppleApp.CODEC,
StatusApp.ID, StatusApp.CODEC
StatusApp.ID, StatusApp.CODEC,
HistoryApp.ID, HistoryApp.CODEC
);
Map<Identifier, Supplier<? extends ScreenApp>> CONSTRUCTORS = Map.of(
ScannerApp.ID, ScannerApp::new,
GpsApp.ID, GpsApp::new,
BadAppleApp.ID, BadAppleApp::new,
StatusApp.ID, StatusApp::new
StatusApp.ID, StatusApp::new,
HistoryApp.ID, HistoryApp::new
);
Codec<ScreenApp> CODEC = Identifier.CODEC.dispatch(ScreenApp::id, ALL::get);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@

import java.util.function.Consumer;

public class SideButtonElement extends PlacedElement {
public class SmallButtonElement extends PlacedElement {
private String text;
private Consumer<TardisControl> clickCallback;
private int pressedFrames;

public SideButtonElement(int x, int y, String text, Consumer<TardisControl> clickCallback) {
public SmallButtonElement(int x, int y, String text, Consumer<TardisControl> clickCallback) {
super(x, y, 28, 14);
this.text = text;
this.clickCallback = clickCallback;
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0ee58fd

Please sign in to comment.