BookViewScreen .java

net.minecraft.client.gui.screens.inventory.BookViewScreen (class): members: createPageControlButtons, cachedPageComponents, PAGE_TEXT_X_OFFSET, BookAccess.

net.minecraft.client.gui.screens.inventoryclass284 linesclientextends Screen9.5 KB

API Surface (20)

void setBookAccess(final BookViewScreen.BookAccess bookAccess)
boolean setPage(final int page)
boolean forcePage(final int page)
void init()
Component getNarrationMessage()
void createMenuControls()
void createPageControlButtons()
void pageBack()
void pageForward()
boolean keyPressed(final KeyEvent event)
void extractRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a)
void extractBackground(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a)
int menuControlsTop()
boolean mouseClicked(final MouseButtonEvent event, final boolean doubleClick)
boolean handleClickEvent(final @Nullable ClickEvent event)
void closeContainerOnServer()
boolean isInGameUi()
record BookAccess(List<Component> pages)
int getPageCount()
Component getPage(final int page)

Fields & Constants (25)

PAGE_INDICATOR_TEXT_Y_OFFSET
PAGE_TEXT_X_OFFSET
PAGE_TEXT_Y_OFFSET
BACKGROUND_TEXTURE_WIDTH
BACKGROUND_TEXTURE_HEIGHT
TITLE
PAGE_TEXT_STYLE
EMPTY_ACCESS
BOOK_LOCATION
TEXT_WIDTH
TEXT_HEIGHT
IMAGE_WIDTH
PAGE_INDICATOR_X_OFFSET
IMAGE_HEIGHT
PAGE_BUTTON_Y
PAGE_BACK_BUTTON_X
PAGE_FORWARD_BUTTON_X
bookAccess
currentPage
cachedPageComponents
cachedPage
pageMsg
forwardButton
backButton
playTurnSound

String literals (4)

"book.view.title"
"textures/gui/book.png"
"book.pageIndicator"
"Player not available"

Full Source

BookViewScreen.java284 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.gui.screens.inventory;
2
3
import java.util.Collections;
4
import java.util.List;
5
import java.util.Objects;
6
import net.fabricmc.api.EnvType;
7
import net.fabricmc.api.Environment;
8
import net.minecraft.client.Minecraft;
9
import net.minecraft.client.gui.ActiveTextCollector;
10
import net.minecraft.client.gui.GuiGraphicsExtractor;
11
import net.minecraft.client.gui.TextAlignment;
12
import net.minecraft.client.gui.components.Button;
13
import net.minecraft.client.gui.screens.Screen;
14
import net.minecraft.client.input.KeyEvent;
15
import net.minecraft.client.input.MouseButtonEvent;
16
import net.minecraft.client.player.LocalPlayer;
17
import net.minecraft.client.renderer.RenderPipelines;
18
import net.minecraft.core.component.DataComponents;
19
import net.minecraft.network.chat.ClickEvent;
20
import net.minecraft.network.chat.CommonComponents;
21
import net.minecraft.network.chat.Component;
22
import net.minecraft.network.chat.ComponentUtils;
23
import net.minecraft.network.chat.FormattedText;
24
import net.minecraft.network.chat.Style;
25
import net.minecraft.network.chat.ClickEvent.ChangePage;
26
import net.minecraft.network.chat.ClickEvent.RunCommand;
27
import net.minecraft.resources.Identifier;
28
import net.minecraft.util.FormattedCharSequence;
29
import net.minecraft.util.Mth;
30
import net.minecraft.world.item.ItemStack;
31
import net.minecraft.world.item.component.WritableBookContent;
32
import net.minecraft.world.item.component.WrittenBookContent;
33
import org.jspecify.annotations.Nullable;
34
35
@Environment(EnvType.CLIENT)
36
public class BookViewScreen extends Screen {
37
	public static final int PAGE_INDICATOR_TEXT_Y_OFFSET = 16;
38
	public static final int PAGE_TEXT_X_OFFSET = 36;
39
	public static final int PAGE_TEXT_Y_OFFSET = 30;
40
	private static final int BACKGROUND_TEXTURE_WIDTH = 256;
41
	private static final int BACKGROUND_TEXTURE_HEIGHT = 256;
42
	private static final Component TITLE = Component.translatable("book.view.title");
43
	private static final Style PAGE_TEXT_STYLE = Style.EMPTY.withoutShadow().withColor(-16777216);
44
	public static final BookViewScreen.BookAccess EMPTY_ACCESS = new BookViewScreen.BookAccess(List.of());
45
	public static final Identifier BOOK_LOCATION = Identifier.withDefaultNamespace("textures/gui/book.png");
46
	protected static final int TEXT_WIDTH = 114;
47
	protected static final int TEXT_HEIGHT = 128;
48
	protected static final int IMAGE_WIDTH = 192;
49
	private static final int PAGE_INDICATOR_X_OFFSET = 148;
50
	protected static final int IMAGE_HEIGHT = 192;
51
	private static final int PAGE_BUTTON_Y = 157;
52
	private static final int PAGE_BACK_BUTTON_X = 43;
53
	private static final int PAGE_FORWARD_BUTTON_X = 116;
54
	private BookViewScreen.BookAccess bookAccess;
55
	private int currentPage;
56
	private List<FormattedCharSequence> cachedPageComponents = Collections.emptyList();
57
	private int cachedPage = -1;
58
	private Component pageMsg = CommonComponents.EMPTY;
59
	private PageButton forwardButton;
60
	private PageButton backButton;
61
	private final boolean playTurnSound;
62
63
	public BookViewScreen(final BookViewScreen.BookAccess bookAccess) {
64
		this(bookAccess, true);
65
	}
66
67
	public BookViewScreen() {
68
		this(EMPTY_ACCESS, false);
69
	}
70
71
	private BookViewScreen(final BookViewScreen.BookAccess bookAccess, final boolean playTurnSound) {
72
		super(TITLE);
73
		this.bookAccess = bookAccess;
74
		this.playTurnSound = playTurnSound;
75
	}
76
77
	public void setBookAccess(final BookViewScreen.BookAccess bookAccess) {
78
		this.bookAccess = bookAccess;
79
		this.currentPage = Mth.clamp(this.currentPage, 0, bookAccess.getPageCount());
80
		this.updateButtonVisibility();
81
		this.cachedPage = -1;
82
	}
83
84
	public boolean setPage(final int page) {
85
		int clampedPage = Mth.clamp(page, 0, this.bookAccess.getPageCount() - 1);
86
		if (clampedPage != this.currentPage) {
87
			this.currentPage = clampedPage;
88
			this.updateButtonVisibility();
89
			this.cachedPage = -1;
90
			return true;
91
		} else {
92
			return false;
93
		}
94
	}
95
96
	protected boolean forcePage(final int page) {
97
		return this.setPage(page);
98
	}
99
100
	@Override
101
	protected void init() {
102
		this.createMenuControls();
103
		this.createPageControlButtons();
104
	}
105
106
	@Override
107
	public Component getNarrationMessage() {
108
		return CommonComponents.joinLines(super.getNarrationMessage(), this.getPageNumberMessage(), this.bookAccess.getPage(this.currentPage));
109
	}
110
111
	private Component getPageNumberMessage() {
112
		return Component.translatable("book.pageIndicator", this.currentPage + 1, Math.max(this.getNumPages(), 1)).withStyle(PAGE_TEXT_STYLE);
113
	}
114
115
	protected void createMenuControls() {
116
		this.addRenderableWidget(
117
			Button.builder(CommonComponents.GUI_DONE, button -> this.onClose()).pos((this.width - 200) / 2, this.menuControlsTop()).width(200).build()
118
		);
119
	}
120
121
	protected void createPageControlButtons() {
122
		int left = this.backgroundLeft();
123
		int top = this.backgroundTop();
124
		this.forwardButton = this.addRenderableWidget(new PageButton(left + 116, top + 157, true, button -> this.pageForward(), this.playTurnSound));
125
		this.backButton = this.addRenderableWidget(new PageButton(left + 43, top + 157, false, button -> this.pageBack(), this.playTurnSound));
126
		this.updateButtonVisibility();
127
	}
128
129
	private int getNumPages() {
130
		return this.bookAccess.getPageCount();
131
	}
132
133
	protected void pageBack() {
134
		if (this.currentPage > 0) {
135
			this.currentPage--;
136
		}
137
138
		this.updateButtonVisibility();
139
	}
140
141
	protected void pageForward() {
142
		if (this.currentPage < this.getNumPages() - 1) {
143
			this.currentPage++;
144
		}
145
146
		this.updateButtonVisibility();
147
	}
148
149
	private void updateButtonVisibility() {
150
		this.forwardButton.visible = this.currentPage < this.getNumPages() - 1;
151
		this.backButton.visible = this.currentPage > 0;
152
	}
153
154
	@Override
155
	public boolean keyPressed(final KeyEvent event) {
156
		if (super.keyPressed(event)) {
157
			return true;
158
		}
159
160
		return switch (event.shortcutKey()) {
161
			case 1073741899 -> {
162
				this.backButton.onPress(event);
163
				yield true;
164
			}
165
			case 1073741902 -> {
166
				this.forwardButton.onPress(event);
167
				yield true;
168
			}
169
			default -> false;
170
		};
171
	}
172
173
	@Override
174
	public void extractRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
175
		super.extractRenderState(graphics, mouseX, mouseY, a);
176
		this.visitText(graphics.textRenderer(GuiGraphicsExtractor.HoveredTextEffects.TOOLTIP_AND_CURSOR), false);
177
	}
178
179
	private void visitText(final ActiveTextCollector collector, final boolean clickableOnly) {
180
		if (this.cachedPage != this.currentPage) {
181
			FormattedText pageText = ComponentUtils.mergeStyles(this.bookAccess.getPage(this.currentPage), PAGE_TEXT_STYLE);
182
			this.cachedPageComponents = this.font.split(pageText, 114);
183
			this.pageMsg = this.getPageNumberMessage();
184
			this.cachedPage = this.currentPage;
185
		}
186
187
		int left = this.backgroundLeft();
188
		int top = this.backgroundTop();
189
		if (!clickableOnly) {
190
			collector.accept(TextAlignment.RIGHT, left + 148, top + 16, this.pageMsg);
191
		}
192
193
		int shownLines = Math.min(128 / 9, this.cachedPageComponents.size());
194
195
		for (int i = 0; i < shownLines; i++) {
196
			FormattedCharSequence component = this.cachedPageComponents.get(i);
197
			collector.accept(left + 36, top + 30 + i * 9, component);
198
		}
199
	}
200
201
	@Override
202
	public void extractBackground(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
203
		super.extractBackground(graphics, mouseX, mouseY, a);
204
		graphics.blit(RenderPipelines.GUI_TEXTURED, BOOK_LOCATION, this.backgroundLeft(), this.backgroundTop(), 0.0F, 0.0F, 192, 192, 256, 256);
205
	}
206
207
	private int backgroundLeft() {
208
		return (this.width - 192) / 2;
209
	}
210
211
	private int backgroundTop() {
212
		return 2;
213
	}
214
215
	protected int menuControlsTop() {
216
		return this.backgroundTop() + 192 + 2;
217
	}
218
219
	@Override
220
	public boolean mouseClicked(final MouseButtonEvent event, final boolean doubleClick) {
221
		if (event.button() == 1) {
222
			ActiveTextCollector.ClickableStyleFinder finder = new ActiveTextCollector.ClickableStyleFinder(this.font, (int)event.x(), (int)event.y());
223
			this.visitText(finder, true);
224
			Style clickedStyle = finder.result();
225
			if (clickedStyle != null && this.handleClickEvent(clickedStyle.getClickEvent())) {
226
				return true;
227
			}
228
		}
229
230
		return super.mouseClicked(event, doubleClick);
231
	}
232
233
	protected boolean handleClickEvent(final @Nullable ClickEvent event) {
234
		if (event == null) {
235
			return false;
236
		}
237
238
		LocalPlayer player = Objects.requireNonNull(this.minecraft.player, "Player not available");
239
		switch (event) {
240
			case ChangePage(int page):
241
				this.forcePage(page - 1);
242
				break;
243
			case RunCommand(String command):
244
				this.closeContainerOnServer();
245
				clickCommandAction(player, command, null);
246
				break;
247
			default:
248
				defaultHandleGameClickEvent(event, this.minecraft, this);
249
		}
250
251
		return true;
252
	}
253
254
	protected void closeContainerOnServer() {
255
	}
256
257
	@Override
258
	public boolean isInGameUi() {
259
		return true;
260
	}
261
262
	@Environment(EnvType.CLIENT)
263
	public record BookAccess(List<Component> pages) {
264
		public int getPageCount() {
265
			return this.pages.size();
266
		}
267
268
		public Component getPage(final int page) {
269
			return page >= 0 && page < this.getPageCount() ? this.pages.get(page) : CommonComponents.EMPTY;
270
		}
271
272
		public static BookViewScreen.@Nullable BookAccess fromItem(final ItemStack itemStack) {
273
			boolean filterEnabled = Minecraft.getInstance().isTextFilteringEnabled();
274
			WrittenBookContent writtenContent = itemStack.get(DataComponents.WRITTEN_BOOK_CONTENT);
275
			if (writtenContent != null) {
276
				return new BookViewScreen.BookAccess(writtenContent.getPages(filterEnabled));
277
			}
278
279
			WritableBookContent writableContent = itemStack.get(DataComponents.WRITABLE_BOOK_CONTENT);
280
			return writableContent != null ? new BookViewScreen.BookAccess(writableContent.getPages(filterEnabled).map(Component::literal).toList()) : null;
281
		}
282
	}
283
}
284

Decompiled Minecraft 26.3 source of BookViewScreen (net/minecraft/client/gui/screens/inventory/BookViewScreen.java) with Mojang official mappings, stored verbatim on the site.