Skip to content

Commit

Permalink
Implemented book switching/renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
JunePrimavera committed Apr 24, 2024
1 parent ac36eda commit 6eaf998
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 51 deletions.
2 changes: 1 addition & 1 deletion src/main/java/xyz/sillyjune/notebook/Notebook.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;


Expand Down Expand Up @@ -84,6 +83,7 @@ public static void openBookKeybindRegister() { // Register keybind
public static final ButtonTextures NEW_PAGE_ICON = b_id("new_page");
public static final ButtonTextures DEL_PAGE_ICON = b_id("delete_page");
public static final ButtonTextures LAST_BOOK_ICON = b_id("last_book");
public static final ButtonTextures RENAME_BOOK_ICON = b_id("rename_book");
public static final ButtonTextures NEXT_BOOK_ICON = b_id("next_book");
public static NotebookConfig CONFIG;
public static String BOOK_FOLDER = "Notebook";
Expand Down
68 changes: 19 additions & 49 deletions src/main/java/xyz/sillyjune/notebook/NotebookScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public class NotebookScreen extends Screen {
private static NotebookData DATA;

public static ButtonWidget closeButton;
public static ButtonWidget buttonGo;
public static ButtonWidget buttonNext;
public static ButtonWidget buttonLast;
public static String BookName = "Default";
private int pageIndex;
private int totalPages;
private int cursorIndex;
Expand Down Expand Up @@ -104,7 +104,7 @@ protected void writePages() {
// Get index of book in folder
protected int getBookIndex() {
for (int i = 0; i < Objects.requireNonNull(new File(STR."\{BOOK_FOLDER}/").list()).length; i++) {
if (Objects.equals(Objects.requireNonNull(new File(STR."\{BOOK_FOLDER}/").list())[i], BookName)) {
if (Objects.equals(Objects.requireNonNull(new File(STR."\{BOOK_FOLDER}/").list())[i], DATA.location())) {
return i;
}
}
Expand Down Expand Up @@ -144,24 +144,30 @@ protected void init() {
// Top bar buttons
this.bookNameField = this.addDrawableChild(new TextFieldWidget(this.textRenderer, 5, 5, 108, 20, Text.translatable("notebook.text.field")));
this.bookNameField.setEditable(true);
this.bookNameField.setText(BookName);

this.bookNameField.setText("default");
buttonNext = this.addDrawableChild(new TexturedButtonWidget(5, 30, 20, 20, NEXT_BOOK_ICON, (button) -> {
int bookIndex = getBookIndex();
if (bookIndex < Objects.requireNonNull(new File(STR."\{BOOK_FOLDER}/").list()).length - 1) {
BookName = Objects.requireNonNull(new File(STR."\{BOOK_FOLDER}/").list())[bookIndex + 1];
this.bookNameField.setText(BookName);
DATA = NotebookData.read(Objects.requireNonNull(new File(STR."\{BOOK_FOLDER}/").list())[bookIndex + 1]);
this.bookNameField.setText(DATA.location().replace(".json", ""));
this.pageIndex = 0;
this.totalPages = DATA.content().length;
this.updatePageButtons();
}
}, Text.translatable("notebook.button.next")));
buttonLast = this.addDrawableChild(new TexturedButtonWidget(30, 30, 20, 20, LAST_BOOK_ICON, (_) -> {
int bookIndex = getBookIndex();
if (bookIndex > 0) {
BookName = Objects.requireNonNull(new File(BOOK_FOLDER + "/").list())[bookIndex - 1];
this.bookNameField.setText(BookName);
DATA = NotebookData.read(Objects.requireNonNull(new File(STR."\{BOOK_FOLDER}/").list())[bookIndex - 1]);
this.bookNameField.setText(DATA.location().replace(".json", ""));
this.pageIndex = 0;
this.totalPages = DATA.content().length;
this.updatePageButtons();
}
}, Text.translatable("notebook.button.last")));
}, Text.translatable("notebook.button.rename")));
buttonGo = this.addDrawableChild(new TexturedButtonWidget(55, 30, 20, 20, RENAME_BOOK_ICON, (_) -> {
DATA = new NotebookData(DATA.content(), STR."\{this.bookNameField.getText()}.json");
}, Text.translatable("notebook.button.rename")));



Expand All @@ -188,22 +194,7 @@ private void updatePageButtons() {
this.previousPageButton.visible = this.pageIndex > 0;
}
// Deselects all buttons
private void deselButtons() {
if (closeButton != null) {
closeButton.setFocused(false);
}
if (buttonNext != null) {
buttonNext.setFocused(false);
}
if (buttonLast != null) {
buttonLast.setFocused(false);
}
this.bookNameField.setFocused(false);
this.nextPageButton.setFocused(false);
this.previousPageButton.setFocused(false);
this.newPageButton.setFocused(false);

}

// Special keys (delete, backspace, etc)
@Override
Expand Down Expand Up @@ -237,28 +228,24 @@ public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
NotebookData.write(DATA);
}
return true;
}case 262 -> {
}
case 262 -> {
String pageContent = this.readPage( pageIndex);
this.deselButtons();
if (cursorIndex < pageContent.length()) {
cursorIndex += 1;
}
return true;
}
case 263 -> {
this.deselButtons();
if (cursorIndex > 0) {
cursorIndex -= 1;
}
return true;
}
default -> { return false; }

}
} else {
if (keyCode == 257) {
this.bookNameField.setFocused(false);
} else if (keyCode == 259 && !this.bookNameField.getText().isEmpty()) {
if (keyCode == 259 && !this.bookNameField.getText().isEmpty()) {
this.bookNameField.setText(this.bookNameField.getText().substring(0, this.bookNameField.getText().length() - 1));
}
return true;
Expand All @@ -267,6 +254,7 @@ public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
// Normal typing
@Override
public boolean charTyped(char chr, int modifiers) {
System.out.println(STR."\{chr} \{modifiers}");
if (!this.bookNameField.isSelected()) {
String pageContent = this.readPage(pageIndex);
if (cursorIndex > pageContent.length()) { cursorIndex = pageContent.length(); }
Expand All @@ -285,25 +273,7 @@ public boolean charTyped(char chr, int modifiers) {

// The code I am going to avoid like the plague
public void render(DrawContext context, int mouseX, int mouseY, float delta) {
int i = (this.width - 192) / 2;


if (!Objects.equals(this.bookNameField.getText(), BookName) && !Objects.equals(this.bookNameField.getText(), "")) {
boolean bookExists = false;
for (int it = 0; it == Objects.requireNonNull(new File(BOOK_FOLDER).list()).length; it++) {
if (Objects.equals(BookName, Objects.requireNonNull(new File(BOOK_FOLDER).list())[it])) {
bookExists = true;
}
}
if (!bookExists) {
if (!new File(STR."\{BOOK_FOLDER}/\{BookName}").renameTo(new File(STR."\{BOOK_FOLDER}/\{this.bookNameField.getText()}"))) {
System.err.println("Couldn't change book name! Make a bug report.");
} else {
BookName = bookNameField.getText();
}

}
}
renderBackground(context, mouseX, mouseY, delta);
super.render(context, mouseX, mouseY, delta);
context.drawText(this.textRenderer, Text.of("Beta Build - Expect minor bugs or missing features!"), 5, this.height - 22, Colors.RED / 2, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ protected TitleScreenButton(Text title) {
private void addCustomButton(int y, int spacingY, CallbackInfo ci) {
this.addDrawableChild(new TexturedButtonWidget((this.width / 2 + 104), y + spacingY + CONFIG.button_offset(), 20, 20, MAIN_BUTTON_ICON, (button) -> {
assert this.client != null;
NotebookScreen.BookName = "Default";
this.client.setScreen(new NotebookScreen());
}));
}
Expand Down

0 comments on commit 6eaf998

Please sign in to comment.