Skip to content

Commit

Permalink
Refactor, convert NotebookData from a record to a class
Browse files Browse the repository at this point in the history
  • Loading branch information
JunePrimavera committed Apr 24, 2024
1 parent 6eaf998 commit 9d5199a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 61 deletions.
3 changes: 2 additions & 1 deletion src/main/java/xyz/sillyjune/notebook/Notebook.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void onInitialize() {
try {
Files.createDirectories(Paths.get(BOOK_FOLDER));
NotebookData data = new NotebookData(new String[0], "default.json");
NotebookData.write(data);
data.write();
} catch (IOException e) {
LOGGER.error(STR."failed to create \{BOOK_FOLDER}");
}
Expand Down Expand Up @@ -86,5 +86,6 @@ public static void openBookKeybindRegister() { // Register keybind
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 final Identifier BOOK_TEXTURE = new Identifier("textures/gui/book.png");
public static String BOOK_FOLDER = "Notebook";
}
21 changes: 10 additions & 11 deletions src/main/java/xyz/sillyjune/notebook/NotebookData.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@
import static xyz.sillyjune.notebook.Notebook.BOOK_FOLDER;
import static xyz.sillyjune.notebook.Notebook.LOGGER;

public record NotebookData(String[] content, String location) {
public String[] content() {
return content;
public class NotebookData {
String[] content;
String location;
NotebookData(String[] content, String location) {
this.content = content;
this.location = location;
}
public String location() {
return location;
}


public static NotebookData read(String location) { // Read the record from a file
File jsondata = new File(BOOK_FOLDER + STR."/\{location}");
Expand All @@ -33,16 +32,16 @@ public static NotebookData read(String location) { // Read the record from a fil
} catch (FileNotFoundException e) { // If it fails, create a new one
LOGGER.error(STR."Failed to read book!\n\{e}");
NotebookData data = new NotebookData(new String[0], STR."\{location}");
write(data); // Write it to file
data.write(); // Write to file
}
String json = d.toString();
return new Gson().fromJson(json, NotebookData.class);
}

public static void write(NotebookData data) {
String json = new Gson().toJson(data);
public void write() {
String json = new Gson().toJson(this);
try { // Write the record to a file
FileWriter writer = new FileWriter(BOOK_FOLDER + STR."/\{data.location}");
FileWriter writer = new FileWriter(BOOK_FOLDER + STR."/\{this.location}");
writer.write(json);
writer.close();
} catch (IOException e) {
Expand Down
74 changes: 25 additions & 49 deletions src/main/java/xyz/sillyjune/notebook/NotebookScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,13 @@
import static xyz.sillyjune.notebook.Notebook.*;

public class NotebookScreen extends Screen {
public static final Identifier BOOK_TEXTURE = new Identifier("textures/gui/book.png");
private static NotebookData DATA;

private static NotebookData DATA;
public static ButtonWidget closeButton;
public static ButtonWidget buttonGo;
public static ButtonWidget buttonNext;
public static ButtonWidget buttonLast;
private int pageIndex;
private int totalPages;
private int cursorIndex;
private List<OrderedText> cachedPage;
private Text pageIndexText;
Expand All @@ -41,7 +39,6 @@ public class NotebookScreen extends Screen {
private PageTurnWidget previousPageButton;
private final boolean pageTurnSound;


public NotebookScreen() {
this(false);
}
Expand All @@ -56,55 +53,49 @@ private NotebookScreen(boolean playPageTurnSound) {

// Returns the amount of pages stored
protected int getPageCount() {
return DATA.content().length;
return DATA.content.length;
}
/// Creates a new blank page
protected void newPage() {
String[] pages = DATA.content();
String[] pages = DATA.content;
String[] new_pages = new String[pages.length+1];
int i = 0;
for (String page : pages) {
new_pages[i] = page;
i++;
}
new_pages[new_pages.length-1] = " ";
DATA = new NotebookData(new_pages, DATA.location());
NotebookData.write(DATA);
totalPages += 1;
DATA.content = new_pages;
DATA.write();
}
// Removes the last page
protected void delPage() {
String[] pages = DATA.content();
String[] pages = DATA.content;
String[] new_pages = new String[pages.length-1];
int i = 0;
for (String _ : new_pages) {
new_pages[i] = pages[i];
i++;
}
DATA = new NotebookData(new_pages, DATA.location());
NotebookData.write(DATA);
totalPages -= 1;
DATA.content = new_pages;
DATA.write();
}
// Reads an existing page from storage
protected String readPage(int pagei) {
if (pagei >= DATA.content().length) {
if (pagei >= DATA.content.length) {
newPage();
}
String content = DATA.content()[pagei];
String content = DATA.content[pagei];
if (content == null) {
return "";
}
return content;
}
// Overwrites an existing page
protected void writePages() {
NotebookData.write(DATA);
}

// 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], DATA.location())) {
if (Objects.equals(Objects.requireNonNull(new File(STR."\{BOOK_FOLDER}/").list())[i], DATA.location)) {
return i;
}
}
Expand All @@ -115,7 +106,6 @@ protected void init() {
DATA = NotebookData.read("default.json");
pageIndex = 0;
this.cursorIndex = readPage(pageIndex).length();
this.totalPages = getPageCount();
// Add done/close button
closeButton = this.addDrawableChild(ButtonWidget.builder(ScreenTexts.DONE, (_) -> {
this.close();
Expand All @@ -135,7 +125,7 @@ protected void init() {
this.goToNextPage();
}, Text.translatable("notebook.button.new")));
this.delPageButton = this.addDrawableChild(new TexturedButtonWidget(i + 99, 155, 20, 20, DEL_PAGE_ICON, (_) -> {
if (totalPages > 1) {
if (DATA.content.length > 1) {
delPage();
this.goToPreviousPage();
}
Expand All @@ -149,28 +139,23 @@ protected void init() {
int bookIndex = getBookIndex();
if (bookIndex < Objects.requireNonNull(new File(STR."\{BOOK_FOLDER}/").list()).length - 1) {
DATA = NotebookData.read(Objects.requireNonNull(new File(STR."\{BOOK_FOLDER}/").list())[bookIndex + 1]);
this.bookNameField.setText(DATA.location().replace(".json", ""));
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) {
DATA = NotebookData.read(Objects.requireNonNull(new File(STR."\{BOOK_FOLDER}/").list())[bookIndex - 1]);
this.bookNameField.setText(DATA.location().replace(".json", ""));
this.bookNameField.setText(DATA.location.replace(".json", ""));
this.pageIndex = 0;
this.totalPages = DATA.content().length;
this.updatePageButtons();
}
}, 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");
DATA = new NotebookData(DATA.content, STR."\{this.bookNameField.getText()}.json");
}, Text.translatable("notebook.button.rename")));



this.updatePageButtons();
}

Expand All @@ -182,12 +167,12 @@ protected void goToPreviousPage() {
this.updatePageButtons();
}
protected void goToNextPage() {
if (this.pageIndex < this.totalPages- 1) { ++this.pageIndex; }
if (this.pageIndex < DATA.content.length - 1) { ++this.pageIndex; }
this.cursorIndex = readPage(pageIndex).length();
this.updatePageButtons();
}
private void updatePageButtons() {
boolean onFinalPage = this.pageIndex == totalPages - 1;
boolean onFinalPage = this.pageIndex == DATA.content.length - 1;
this.nextPageButton.visible = !onFinalPage;
this.newPageButton.visible = onFinalPage;
this.delPageButton.visible = onFinalPage;
Expand All @@ -209,23 +194,17 @@ public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
case 259 -> {
String pageContent = this.readPage(pageIndex);
if (cursorIndex > 0) {
pageContent = pageContent.substring(0, cursorIndex - 1) + pageContent.substring(cursorIndex);
String[] content = DATA.content();
content[pageIndex] = pageContent;
DATA = new NotebookData(content, DATA.location());
NotebookData.write(DATA);
DATA.content[pageIndex] = pageContent.substring(0, cursorIndex - 1) + pageContent.substring(cursorIndex);;
DATA.write();
this.cursorIndex -= 1;
}
return true;
}
case 261 -> {
String pageContent = this.readPage(pageIndex);
if (cursorIndex < pageContent.length()) {
pageContent = pageContent.substring(0, cursorIndex) + pageContent.substring(cursorIndex + 1);
String[] content = DATA.content();
content[pageIndex] = pageContent;
DATA = new NotebookData(content, DATA.location());
NotebookData.write(DATA);
DATA.content[pageIndex] = pageContent.substring(0, cursorIndex) + pageContent.substring(cursorIndex + 1);
DATA.write();
}
return true;
}
Expand Down Expand Up @@ -258,11 +237,8 @@ public boolean charTyped(char chr, int modifiers) {
if (!this.bookNameField.isSelected()) {
String pageContent = this.readPage(pageIndex);
if (cursorIndex > pageContent.length()) { cursorIndex = pageContent.length(); }
pageContent = pageContent.substring(0, cursorIndex) + chr + pageContent.substring(cursorIndex);
String[] content = DATA.content();
content[pageIndex] = pageContent;
DATA = new NotebookData(content, DATA.location());
NotebookData.write(DATA);
DATA.content[pageIndex] = pageContent.substring(0, cursorIndex) + chr + pageContent.substring(cursorIndex);
DATA.write();
this.cursorIndex += 1;
return true;
} else {
Expand All @@ -286,7 +262,7 @@ public void render(DrawContext context, int mouseX, int mouseY, float delta) {
public void renderBackground(DrawContext context, int mouseX, int mouseY, float delta) {
int i = (this.width - 192) / 2;
super.renderBackground(context, mouseX, mouseY, delta);
if (this.totalPages > this.pageIndex) {
if (DATA.content.length > this.pageIndex) {
String pageContent = readPage(pageIndex);
// Cursor rendering
assert this.client != null;
Expand All @@ -296,7 +272,7 @@ public void renderBackground(DrawContext context, int mouseX, int mouseY, float

StringVisitable stringVisitable = StringVisitable.plain(pageContent);
this.cachedPage = this.textRenderer.wrapLines(stringVisitable, 114);
this.pageIndexText = Text.translatable("book.pageIndicator", this.pageIndex + 1, Math.max(this.totalPages, 1));
this.pageIndexText = Text.translatable("book.pageIndicator", this.pageIndex + 1, Math.max(DATA.content.length, 1));
}
int k = this.textRenderer.getWidth(this.pageIndexText);
context.drawTexture(BOOK_TEXTURE, (this.width - 192) / 2, 2, 0, 0, 192, 192);
Expand Down

0 comments on commit 9d5199a

Please sign in to comment.