diff --git a/build.gradle b/build.gradle index 17ceb52..c44feee 100644 --- a/build.gradle +++ b/build.gradle @@ -13,7 +13,7 @@ apply plugin: 'net.minecraftforge.gradle' apply plugin: 'eclipse' apply plugin: 'maven-publish' -version = '2.3.7' +version = '2.4.2' group = 'wafflestomper' archivesBaseName = '1.18.2-Ghostwriter' diff --git a/src/main/java/wafflestomper/ghostwriter/gui/GhostLayer.java b/src/main/java/wafflestomper/ghostwriter/gui/GhostLayer.java index c44aca7..d5456d4 100644 --- a/src/main/java/wafflestomper/ghostwriter/gui/GhostLayer.java +++ b/src/main/java/wafflestomper/ghostwriter/gui/GhostLayer.java @@ -411,10 +411,15 @@ private Button addPageButton(int x, int y, String label, Button.OnPress action, return b; } - - private Button addColorFormatButton(int y, String label, String insertChars) { - Button b = this.addPageButton(this.colorFormatButtonX, y, label, - (pressed_button) -> this.parent.insertText(insertChars), true); + + private Button addColorFormatButton(int y, String label, String formattingCode) { + Button b = this.addPageButton( + this.colorFormatButtonX, + y, + label, + (pressed_button) -> this.parent.addFormattingCode(formattingCode), + true + ); b.setWidth(SharedConstants.COLOR_FORMAT_BUTTON_WIDTH); this.colorFormatButtonX += SharedConstants.COLOR_FORMAT_BUTTON_WIDTH; return b; @@ -476,31 +481,31 @@ public void init() { /////////////////////////////////////// Underside buttons /////////////////////////////////////////// this.colorFormatButtonX = this.screen.width / 2 - (SharedConstants.COLOR_FORMAT_BUTTON_WIDTH * 8); int colorButY = this.screen.height - 40; - this.addColorFormatButton(colorButY, "\u00a70A", "\u00a70"); // BLACK - this.addColorFormatButton(colorButY, "\u00a71A", "\u00a71"); // DARK_BLUE - this.addColorFormatButton(colorButY, "\u00a72A", "\u00a72"); // DARK_GREEN - this.addColorFormatButton(colorButY, "\u00a73A", "\u00a73"); // DARK_AQUA - this.addColorFormatButton(colorButY, "\u00a74A", "\u00a74"); // DARK_RED - this.addColorFormatButton(colorButY, "\u00a75A", "\u00a75"); // DARK_PURPLE - this.addColorFormatButton(colorButY, "\u00a76A", "\u00a76"); // GOLD - this.addColorFormatButton(colorButY, "\u00a77A", "\u00a77"); // GRAY - this.addColorFormatButton(colorButY, "\u00a78A", "\u00a78"); // DARK_GRAY - this.addColorFormatButton(colorButY, "\u00a79A", "\u00a79"); // BLUE - this.addColorFormatButton(colorButY, "\u00a7aA", "\u00a7a"); // GREEN - this.addColorFormatButton(colorButY, "\u00a7bA", "\u00a7b"); // AQUA - this.addColorFormatButton(colorButY, "\u00a7cA", "\u00a7c"); // RED - this.addColorFormatButton(colorButY, "\u00a7dA", "\u00a7d"); // LIGHT_PURPLE - this.addColorFormatButton(colorButY, "\u00a7eA", "\u00a7e"); // YELLOW - this.addColorFormatButton(colorButY, "\u00a7fA", "\u00a7f"); // WHITE + this.addColorFormatButton(colorButY, "§0A", "§0"); // BLACK + this.addColorFormatButton(colorButY, "§1A", "§1"); // DARK_BLUE + this.addColorFormatButton(colorButY, "§2A", "§2"); // DARK_GREEN + this.addColorFormatButton(colorButY, "§3A", "§3"); // DARK_AQUA + this.addColorFormatButton(colorButY, "§4A", "§4"); // DARK_RED + this.addColorFormatButton(colorButY, "§5A", "§5"); // DARK_PURPLE + this.addColorFormatButton(colorButY, "§6A", "§6"); // GOLD + this.addColorFormatButton(colorButY, "§7A", "§7"); // GRAY + this.addColorFormatButton(colorButY, "§8A", "§8"); // DARK_GRAY + this.addColorFormatButton(colorButY, "§9A", "§9"); // BLUE + this.addColorFormatButton(colorButY, "§aA", "§a"); // GREEN + this.addColorFormatButton(colorButY, "§bA", "§b"); // AQUA + this.addColorFormatButton(colorButY, "§cA", "§c"); // RED + this.addColorFormatButton(colorButY, "§dA", "§d"); // LIGHT_PURPLE + this.addColorFormatButton(colorButY, "§eA", "§e"); // YELLOW + this.addColorFormatButton(colorButY, "§fA", "§f"); // WHITE this.colorFormatButtonX = this.screen.width / 2 - (SharedConstants.COLOR_FORMAT_BUTTON_WIDTH * 5); int formatButY = this.screen.height - 20; - this.addColorFormatButton(formatButY, "\u00a7kA", "\u00a7k"); // OBFUSCATED - this.addColorFormatButton(formatButY, "\u00a7lA", "\u00a7l"); // BOLD - this.addColorFormatButton(formatButY, "\u00a7mA", "\u00a7m"); // STRIKETHROUGH - this.addColorFormatButton(formatButY, "\u00a7nA", "\u00a7n"); // UNDERLINE - this.addColorFormatButton(formatButY, "\u00a7oA", "\u00a7o"); // ITALIC - this.addColorFormatButton(formatButY, "Reset Formatting", "\u00a7r").setWidth(100); + this.addColorFormatButton(formatButY, "§kA", "§k"); // OBFUSCATED + this.addColorFormatButton(formatButY, "§lA", "§l"); // BOLD + this.addColorFormatButton(formatButY, "§mA", "§m"); // STRIKETHROUGH + this.addColorFormatButton(formatButY, "§nA", "§n"); // UNDERLINE + this.addColorFormatButton(formatButY, "§oA", "§o"); // ITALIC + this.addColorFormatButton(formatButY, "Reset Formatting", "§r").setWidth(100); this.buttonsInitialized = true; this.updateButtons(); diff --git a/src/main/java/wafflestomper/ghostwriter/gui/IGhostBook.java b/src/main/java/wafflestomper/ghostwriter/gui/IGhostBook.java index 89dd290..b6a7535 100644 --- a/src/main/java/wafflestomper/ghostwriter/gui/IGhostBook.java +++ b/src/main/java/wafflestomper/ghostwriter/gui/IGhostBook.java @@ -42,10 +42,10 @@ public interface IGhostBook { String getPageText(int pageNum); /** - * Adds text to the open page (or the title if the book is being signed) + * Adds formatting code to the book (or the title if the book is being signed) * Only used with unsigned books */ - void insertText(String insertChars); + void addFormattingCode(String formattingCode); /** * Adds a new page (if possible) into the book at the given page number, with the given text diff --git a/src/main/java/wafflestomper/ghostwriter/gui/screen/GhostwriterEditBookScreen.java b/src/main/java/wafflestomper/ghostwriter/gui/screen/GhostwriterEditBookScreen.java index 33e64ae..ae9fe2b 100644 --- a/src/main/java/wafflestomper/ghostwriter/gui/screen/GhostwriterEditBookScreen.java +++ b/src/main/java/wafflestomper/ghostwriter/gui/screen/GhostwriterEditBookScreen.java @@ -183,26 +183,61 @@ public List pagesAsList() { // any JSON weirdness return new ArrayList<>(this.pages); } - - - @Override // From IGhostBook - public void insertText(String text) { - if (this.isSigning) { - // Put the text into the title - this.titleEdit.insertText(text); - return; - } - // Put the text into the page - this.pageEdit.insertText(text); - } - - + + @Override // From IGhostBook public void bookChanged(boolean setModifiedFlag) { if (setModifiedFlag) this.isModified = true; this.clearDisplayCache(); // TODO: is this all we need to do now? } - + + + @Override // From IGhostBook + public void addFormattingCode(String formattingCode) { + // Get a reference to the appropriate editor (title or page, depending on whether we're signing) + TextFieldHelper editor = this.isSigning ? this.titleEdit : this.pageEdit; + + // If text is selected, we need to temporarily remove the selection so the text isn't replaced by the + // formatting character + int cursorPos = editor.getCursorPos(); + int selectionPos = editor.getSelectionPos(); + if (cursorPos != selectionPos){ + // Note that when text is selected right to left, selectionPos will be lower than cursorPos. + // The second argument to this function controls whether the selectionPos should be preserved. When + // set to false, the selectionPos will be set to the new cursorPos + editor.setCursorPos(Math.min(cursorPos, selectionPos), false); + } + + // Next we add the formatting character at the start of the selection (which will also be to the left + // of the cursor in the case that no text is selected) + editor.insertText(formattingCode); + + // If text was selected, we need to re-select the text (optionally adding a reset sequence at the end + // of the selection if the character we're inserting isn't a reset char + if (cursorPos != selectionPos){ + int extraCharCount = 2; + + // Optionally add reset character at the end of selection + if (!formattingCode.equals("§r")){ + editor.setCursorPos(Math.max(cursorPos, selectionPos) + extraCharCount, false); + editor.insertText("§r"); + extraCharCount = 4; + } + + // Re-select the text (note that it's two characters longer and we have to deal with the case where + // the text was selected right-to-left + if (cursorPos < selectionPos){ + editor.setSelectionRange(cursorPos, selectionPos + extraCharCount); + } + else{ + editor.setSelectionRange(selectionPos, cursorPos + extraCharCount); + } + } + + // Mark the book as changed, which will also update the display + this.bookChanged(true); + } + @Override // From IGhostBook public boolean isBookBeingSigned() { diff --git a/src/main/java/wafflestomper/ghostwriter/gui/screen/GhostwriterLecternScreen.java b/src/main/java/wafflestomper/ghostwriter/gui/screen/GhostwriterLecternScreen.java index dbe8690..c2bf228 100644 --- a/src/main/java/wafflestomper/ghostwriter/gui/screen/GhostwriterLecternScreen.java +++ b/src/main/java/wafflestomper/ghostwriter/gui/screen/GhostwriterLecternScreen.java @@ -116,7 +116,7 @@ public void setPageText(int pageNum, String pageText) { } @Override // From IGhostBook - public void insertText(String insertChars) { + public void addFormattingCode(String formattingCode) { } @Override // From IGhostBook diff --git a/src/main/java/wafflestomper/ghostwriter/gui/screen/GhostwriterReadBookScreen.java b/src/main/java/wafflestomper/ghostwriter/gui/screen/GhostwriterReadBookScreen.java index de36ad7..3d73174 100644 --- a/src/main/java/wafflestomper/ghostwriter/gui/screen/GhostwriterReadBookScreen.java +++ b/src/main/java/wafflestomper/ghostwriter/gui/screen/GhostwriterReadBookScreen.java @@ -100,7 +100,7 @@ public void setPageText(int pageNum, String pageText) { } @Override // From IGhostBook - public void insertText(String insertChars) { + public void addFormattingCode(String formattingCode) { } @Override // From IGhostBook diff --git a/update.json b/update.json index 1f81fd7..7c7ce44 100644 --- a/update.json +++ b/update.json @@ -1,7 +1,7 @@ { "homepage": "https://github.com/waffle-stomper/Ghostwriter/releases", "promos":{ - "1.18.2-recommended": "2.3.7", + "1.18.2-recommended": "2.4.2", "1.18.1-recommended": "2.3.5", "1.17.1-recommended": "2.3.4", "1.16.5-recommended": "2.2.6", @@ -20,6 +20,7 @@ "1.10-recommended": "1.8.5" }, "1.18.2": { + "2.4.2": "Fix bug where formatting characters replace selected text. Thanks to ZeliotL for the bug report", "2.3.7": "Update to 1.18.2" }, "1.18.1": {