Skip to content

Commit

Permalink
fix: back navigation not working consistently (#254)
Browse files Browse the repository at this point in the history
* chore: remove history reset on close

* fix: book link handler supplies entry id as category id for history

* feat: add per-book separate history stack
  • Loading branch information
klikli-dev authored Sep 29, 2024
1 parent 3b5395e commit 08a111b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ public BookCategory getCategoryToOpen() {

@Override
public void openEntry(BookAddress address) {
//if we jump to a category, we push the current category to history to be able to return
BookGuiManager.get().pushHistory(BookAddress.defaultFor(this.getCategory()));
//we don't have any use for the address here
BookGuiManager.get().openCategoryLinkEntry(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ public BookEntry getEntryToOpen() {

@Override
public void openEntry(BookAddress address) {
//if we jump to an entry, we push the current category to history to be able to return
BookGuiManager.get().pushHistory(BookAddress.defaultFor(this.getCategory()));

BookGuiManager.get().openEntry(this.entryToOpen, BookAddress.defaultFor(this.entryToOpen));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,23 @@
import com.klikli_dev.modonomicon.platform.Services;
import com.mojang.blaze3d.platform.InputConstants;
import com.mojang.datafixers.util.Pair;
import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
import net.minecraft.client.Minecraft;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.Resource;
import net.minecraft.world.entity.player.Player;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;
import org.lwjgl.glfw.GLFW;

import java.util.Map;
import java.util.Stack;

public class BookGuiManager {

private static final BookGuiManager instance = new BookGuiManager();

private final Stack<BookAddress> history = new Stack<>();
private final Map<ResourceLocation, Stack<BookAddress>> history = new Object2ObjectArrayMap<>();

/**
* The currently open screen. Used for unlock state sync to immediately update the open screen.
Expand Down Expand Up @@ -349,27 +352,33 @@ public void openEntry(BookEntry entry, BookAddress address) {
public void pushHistory(ResourceLocation bookId, @Nullable ResourceLocation entryId, int page) {
var book = BookDataManager.get().getBook(bookId);
var entry = book.getEntry(entryId);
this.history.push(BookAddress.of(bookId, entry.getCategoryId(), entryId, page));
this.history.computeIfAbsent(bookId, k -> new Stack<>()).push(BookAddress.of(bookId, entry.getCategoryId(), entryId, page));
}

public void pushHistory(ResourceLocation bookId, @Nullable ResourceLocation categoryId, @Nullable ResourceLocation entryId, int page) {
this.history.push(BookAddress.of(bookId, categoryId, entryId, page));
this.history.computeIfAbsent(bookId, k -> new Stack<>()).push(BookAddress.of(bookId, categoryId, entryId, page));
}

public void pushHistory(BookAddress entry) {
this.history.push(entry);
this.history.computeIfAbsent(entry.bookId(), k -> new Stack<>()).push(entry);
}

public BookAddress popHistory() {
return this.history.pop();
public BookAddress popHistory(ResourceLocation bookId) {
if (!this.history.containsKey(bookId) || this.history.get(bookId).isEmpty())
return null;
return this.history.get(bookId).pop();
}

public BookAddress peekHistory() {
return this.history.peek();
public BookAddress peekHistory(ResourceLocation bookId) {
if (!this.history.containsKey(bookId) || this.history.get(bookId).isEmpty())
return null;
return this.history.get(bookId).peek();
}

public int getHistorySize() {
return this.history.size();
public int getHistorySize(ResourceLocation bookId) {
if (!this.history.containsKey(bookId))
return 0;
return this.history.get(bookId).size();
}

public void resetHistory() {
Expand Down Expand Up @@ -472,19 +481,13 @@ public void closeCategoryScreen(BookCategoryScreen screen) {
Services.NETWORK.sendToServer(new SaveCategoryStateMessage(screen.getCategory(), state));
}

/**
* Call this when you want to close the parent screen naturally, without esc.
* E.g. from the "close"/"x" button.
*/
public void closeParentScreen(BookParentScreen screen) {
Minecraft.getInstance().setScreen(null);
this.openBookParentScreen = null;

var state = BookVisualStateManager.get().getBookStateFor(this.player(), screen.getBook());
screen.saveState(state);
Services.NETWORK.sendToServer(new SaveBookStateMessage(screen.getBook(), state));

this.resetHistory();
}

public void closeScreenStack(BookParentScreen screen) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,16 @@ public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
}

public boolean canSeeBackButton() {
return BookGuiManager.get().getHistorySize() > 0;
return BookGuiManager.get().getHistorySize(this.getBook().getId()) > 0;
}

public void handleBackButton(Button button) {
this.back();
}

public void back() {
if (BookGuiManager.get().getHistorySize() > 0) {
var lastPage = BookGuiManager.get().popHistory();
if (BookGuiManager.get().getHistorySize(this.getBook().getId()) > 0) {
var lastPage = BookGuiManager.get().popHistory(this.getBook().getId());
BookGuiManager.get().openEntry(lastPage.bookId(), lastPage.categoryId(), lastPage.entryId(), lastPage.page());
} else {
this.onClose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public ClickResult handleClick(@NotNull Style pStyle) {

//we push the page we are currently on to the history
var currentPageNumber = this.screen().getCurrentPageNumber();
BookGuiManager.get().pushHistory(this.book().getId(), this.entry().getId(), this.entry().getId(), currentPageNumber);
BookGuiManager.get().pushHistory(this.book().getId(), this.category().getId(), this.entry().getId(), currentPageNumber);
BookGuiManager.get().openEntry(link.bookId, link.entryId, page);
} else if (link.categoryId != null) {
BookGuiManager.get().openEntry(link.bookId, link.categoryId, null, 0);
Expand Down

0 comments on commit 08a111b

Please sign in to comment.