Skip to content

Commit

Permalink
feat: improve EntryClickedEvent and enable it on index mode categories
Browse files Browse the repository at this point in the history
  • Loading branch information
klikli-dev committed Aug 4, 2024
1 parent b4704c3 commit cedc7b0
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
import com.klikli_dev.modonomicon.client.gui.book.entry.EntryDisplayState;
import net.minecraft.resources.ResourceLocation;

/**
* An event that is fired on the client-side when an entry is clicked in the book.
* If the event is cancelled by a listener, the entry will not be displayed.
*/
public class EntryClickedEvent extends ModonomiconEvent {
protected ResourceLocation bookId;
protected ResourceLocation entryId;
Expand Down Expand Up @@ -36,14 +40,23 @@ public ResourceLocation getEntryId() {
return this.entryId;
}

/**
* For categories in Index mode this is the X coordinate of the button that was clicked, instead of the mouse cursor that clicked it.
*/
public double getMouseX() {
return this.mouseX;
}

/**
* For categories in Index mode this is the Y coordinate of the button that was clicked, instead of the mouse cursor that clicked it.
*/
public double getMouseY() {
return this.mouseY;
}

/**
* For categories in Index mode this is always GLFW_MOUSE_BUTTON_1 (= 0 = left mouse button).
*/
public int getButton() {
return this.button;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// SPDX-FileCopyrightText: 2024 klikli-dev
//
// SPDX-License-Identifier: MIT

package com.klikli_dev.modonomicon.api.events;

import com.klikli_dev.modonomicon.book.BookCommand;
import com.klikli_dev.modonomicon.client.gui.book.entry.EntryDisplayState;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;

/**
* An event that is fired on both the client and server side, when an entry is read for the first time.
* Note that resetting the book and then reading the entry again, will trigger this event again.
* If you are e.g. awarding rewards based on this event you should save somewhere that the player already received the entry.
* See e.g. {@link BookCommand#execute(ServerPlayer)} which stores how many times a command has been executed.
*/
public class EntryFirstReadEvent extends ModonomiconEvent {
protected ResourceLocation bookId;
protected ResourceLocation entryId;

public EntryFirstReadEvent(ResourceLocation bookId, ResourceLocation entryId) {
super(false);

this.bookId = bookId;
this.entryId = entryId;
}

public ResourceLocation getBookId() {
return this.bookId;
}

public ResourceLocation getEntryId() {
return this.entryId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
package com.klikli_dev.modonomicon.client.gui.book;

import com.klikli_dev.modonomicon.book.BookCategory;
import com.klikli_dev.modonomicon.book.entries.BookEntry;
import com.klikli_dev.modonomicon.bookstate.BookUnlockStateManager;
import com.klikli_dev.modonomicon.bookstate.visual.CategoryVisualState;
import com.klikli_dev.modonomicon.client.gui.book.entry.EntryDisplayState;
import net.minecraft.client.Minecraft;

/**
* A screen that represents a book. It usually manages other screens for categories and entries.
Expand All @@ -21,4 +25,31 @@ public interface BookCategoryScreen {
void saveState(CategoryVisualState state);

BookCategory getCategory();

default EntryDisplayState getEntryDisplayState(BookEntry entry) {
var player = Minecraft.getInstance().player;

var isEntryUnlocked = BookUnlockStateManager.get().isUnlockedFor(player, entry);

var anyParentsUnlocked = false;
var allParentsUnlocked = true;
for (var parent : entry.getParents()) {
if (!BookUnlockStateManager.get().isUnlockedFor(player, parent.getEntry())) {
allParentsUnlocked = false;
} else {
anyParentsUnlocked = true;
}
}

if (entry.showWhenAnyParentUnlocked() && !anyParentsUnlocked)
return EntryDisplayState.HIDDEN;

if (!entry.showWhenAnyParentUnlocked() && !allParentsUnlocked)
return EntryDisplayState.HIDDEN;

if (!isEntryUnlocked)
return entry.hideWhileLocked() ? EntryDisplayState.HIDDEN : EntryDisplayState.LOCKED;

return EntryDisplayState.UNLOCKED;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package com.klikli_dev.modonomicon.client.gui.book.index;

import com.klikli_dev.modonomicon.api.ModonomiconConstants.I18n.Gui;
import com.klikli_dev.modonomicon.api.events.EntryClickedEvent;
import com.klikli_dev.modonomicon.book.Book;
import com.klikli_dev.modonomicon.book.BookCategory;
import com.klikli_dev.modonomicon.book.entries.BookEntry;
Expand All @@ -20,7 +21,9 @@
import com.klikli_dev.modonomicon.client.gui.book.BookParentScreen;
import com.klikli_dev.modonomicon.client.gui.book.button.EntryListButton;
import com.klikli_dev.modonomicon.client.gui.book.entry.BookEntryScreen;
import com.klikli_dev.modonomicon.client.gui.book.entry.EntryDisplayState;
import com.klikli_dev.modonomicon.client.render.page.BookPageRenderer;
import com.klikli_dev.modonomicon.events.ModonomiconEvents;
import com.klikli_dev.modonomicon.util.GuiGraphicsExt;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.Button;
Expand Down Expand Up @@ -59,8 +62,17 @@ public BookCategoryIndexScreen(BookParentScreen parentScreen, BookCategory categ
this.category = category;
}

public void handleButtonEntry(Button button) {
public void handleEntryListButton(Button button) {
var entry = ((EntryListButton) button).getEntry();

var displayStyle = this.getEntryDisplayState(entry);
var event = new EntryClickedEvent(this.category.getBook().getId(), entry.getId(), button.getX(), button.getY(), GLFW.GLFW_MOUSE_BUTTON_1, displayStyle);

//if event is canceled -> click was handled and we do not open the entry.
if (ModonomiconEvents.client().entryClicked(event)) {
return;
}

BookGuiManager.get().openEntry(entry.getBook().getId(), entry.getId(), 0);
}

Expand Down Expand Up @@ -318,7 +330,7 @@ public void init() {

void addEntryButtons(int x, int y, int start, int count) {
for (int i = 0; i < count && (i + start) < this.visibleEntries.size(); i++) {
Button button = new EntryListButton(this.visibleEntries.get(start + i), this.bookLeft + x, this.bookTop + y + i * 11, this::handleButtonEntry);
Button button = new EntryListButton(this.visibleEntries.get(start + i), this.bookLeft + x, this.bookTop + y + i * 11, this::handleEntryListButton);
this.addRenderableWidget(button);
this.entryButtons.add(button);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,34 +197,6 @@ public void renderBackgroundParallaxLayer(GuiGraphics guiGraphics, BookCategoryB

}


private EntryDisplayState getEntryDisplayState(BookEntry entry) {
var player = Minecraft.getInstance().player;

var isEntryUnlocked = BookUnlockStateManager.get().isUnlockedFor(player, entry);

var anyParentsUnlocked = false;
var allParentsUnlocked = true;
for (var parent : entry.getParents()) {
if (!BookUnlockStateManager.get().isUnlockedFor(player, parent.getEntry())) {
allParentsUnlocked = false;
} else {
anyParentsUnlocked = true;
}
}

if (entry.showWhenAnyParentUnlocked() && !anyParentsUnlocked)
return EntryDisplayState.HIDDEN;

if (!entry.showWhenAnyParentUnlocked() && !allParentsUnlocked)
return EntryDisplayState.HIDDEN;

if (!isEntryUnlocked)
return entry.hideWhileLocked() ? EntryDisplayState.HIDDEN : EntryDisplayState.LOCKED;

return EntryDisplayState.UNLOCKED;
}

private void renderEntries(GuiGraphics guiGraphics, int mouseX, int mouseY) {
RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F);
RenderSystem.setShader(GameRenderer::getPositionTexShader);
Expand Down

0 comments on commit cedc7b0

Please sign in to comment.