Skip to content

Commit e15e589

Browse files
Fix bug where formatting code would replace selected text
1 parent 9ee6155 commit e15e589

7 files changed

+88
-47
lines changed

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ apply plugin: 'net.minecraftforge.gradle'
1313
apply plugin: 'eclipse'
1414
apply plugin: 'maven-publish'
1515

16-
version = '2.3.7'
16+
version = '2.4.2'
1717
group = 'wafflestomper'
1818
archivesBaseName = '1.18.2-Ghostwriter'
1919

src/main/java/wafflestomper/ghostwriter/gui/GhostLayer.java

+31-26
Original file line numberDiff line numberDiff line change
@@ -411,10 +411,15 @@ private Button addPageButton(int x, int y, String label, Button.OnPress action,
411411
return b;
412412
}
413413

414-
415-
private Button addColorFormatButton(int y, String label, String insertChars) {
416-
Button b = this.addPageButton(this.colorFormatButtonX, y, label,
417-
(pressed_button) -> this.parent.insertText(insertChars), true);
414+
415+
private Button addColorFormatButton(int y, String label, String formattingCode) {
416+
Button b = this.addPageButton(
417+
this.colorFormatButtonX,
418+
y,
419+
label,
420+
(pressed_button) -> this.parent.addFormattingCode(formattingCode),
421+
true
422+
);
418423
b.setWidth(SharedConstants.COLOR_FORMAT_BUTTON_WIDTH);
419424
this.colorFormatButtonX += SharedConstants.COLOR_FORMAT_BUTTON_WIDTH;
420425
return b;
@@ -476,31 +481,31 @@ public void init() {
476481
/////////////////////////////////////// Underside buttons ///////////////////////////////////////////
477482
this.colorFormatButtonX = this.screen.width / 2 - (SharedConstants.COLOR_FORMAT_BUTTON_WIDTH * 8);
478483
int colorButY = this.screen.height - 40;
479-
this.addColorFormatButton(colorButY, "\u00a70A", "\u00a70"); // BLACK
480-
this.addColorFormatButton(colorButY, "\u00a71A", "\u00a71"); // DARK_BLUE
481-
this.addColorFormatButton(colorButY, "\u00a72A", "\u00a72"); // DARK_GREEN
482-
this.addColorFormatButton(colorButY, "\u00a73A", "\u00a73"); // DARK_AQUA
483-
this.addColorFormatButton(colorButY, "\u00a74A", "\u00a74"); // DARK_RED
484-
this.addColorFormatButton(colorButY, "\u00a75A", "\u00a75"); // DARK_PURPLE
485-
this.addColorFormatButton(colorButY, "\u00a76A", "\u00a76"); // GOLD
486-
this.addColorFormatButton(colorButY, "\u00a77A", "\u00a77"); // GRAY
487-
this.addColorFormatButton(colorButY, "\u00a78A", "\u00a78"); // DARK_GRAY
488-
this.addColorFormatButton(colorButY, "\u00a79A", "\u00a79"); // BLUE
489-
this.addColorFormatButton(colorButY, "\u00a7aA", "\u00a7a"); // GREEN
490-
this.addColorFormatButton(colorButY, "\u00a7bA", "\u00a7b"); // AQUA
491-
this.addColorFormatButton(colorButY, "\u00a7cA", "\u00a7c"); // RED
492-
this.addColorFormatButton(colorButY, "\u00a7dA", "\u00a7d"); // LIGHT_PURPLE
493-
this.addColorFormatButton(colorButY, "\u00a7eA", "\u00a7e"); // YELLOW
494-
this.addColorFormatButton(colorButY, "\u00a7fA", "\u00a7f"); // WHITE
484+
this.addColorFormatButton(colorButY, "§0A", "§0"); // BLACK
485+
this.addColorFormatButton(colorButY, "§1A", "§1"); // DARK_BLUE
486+
this.addColorFormatButton(colorButY, "§2A", "§2"); // DARK_GREEN
487+
this.addColorFormatButton(colorButY, "§3A", "§3"); // DARK_AQUA
488+
this.addColorFormatButton(colorButY, "§4A", "§4"); // DARK_RED
489+
this.addColorFormatButton(colorButY, "§5A", "§5"); // DARK_PURPLE
490+
this.addColorFormatButton(colorButY, "§6A", "§6"); // GOLD
491+
this.addColorFormatButton(colorButY, "§7A", "§7"); // GRAY
492+
this.addColorFormatButton(colorButY, "§8A", "§8"); // DARK_GRAY
493+
this.addColorFormatButton(colorButY, "§9A", "§9"); // BLUE
494+
this.addColorFormatButton(colorButY, "§aA", "§a"); // GREEN
495+
this.addColorFormatButton(colorButY, "§bA", "§b"); // AQUA
496+
this.addColorFormatButton(colorButY, "§cA", "§c"); // RED
497+
this.addColorFormatButton(colorButY, "§dA", "§d"); // LIGHT_PURPLE
498+
this.addColorFormatButton(colorButY, "§eA", "§e"); // YELLOW
499+
this.addColorFormatButton(colorButY, "§fA", "§f"); // WHITE
495500

496501
this.colorFormatButtonX = this.screen.width / 2 - (SharedConstants.COLOR_FORMAT_BUTTON_WIDTH * 5);
497502
int formatButY = this.screen.height - 20;
498-
this.addColorFormatButton(formatButY, "\u00a7kA", "\u00a7k"); // OBFUSCATED
499-
this.addColorFormatButton(formatButY, "\u00a7lA", "\u00a7l"); // BOLD
500-
this.addColorFormatButton(formatButY, "\u00a7mA", "\u00a7m"); // STRIKETHROUGH
501-
this.addColorFormatButton(formatButY, "\u00a7nA", "\u00a7n"); // UNDERLINE
502-
this.addColorFormatButton(formatButY, "\u00a7oA", "\u00a7o"); // ITALIC
503-
this.addColorFormatButton(formatButY, "Reset Formatting", "\u00a7r").setWidth(100);
503+
this.addColorFormatButton(formatButY, "§kA", "§k"); // OBFUSCATED
504+
this.addColorFormatButton(formatButY, "§lA", "§l"); // BOLD
505+
this.addColorFormatButton(formatButY, "§mA", "§m"); // STRIKETHROUGH
506+
this.addColorFormatButton(formatButY, "§nA", "§n"); // UNDERLINE
507+
this.addColorFormatButton(formatButY, "§oA", "§o"); // ITALIC
508+
this.addColorFormatButton(formatButY, "Reset Formatting", "§r").setWidth(100);
504509

505510
this.buttonsInitialized = true;
506511
this.updateButtons();

src/main/java/wafflestomper/ghostwriter/gui/IGhostBook.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ public interface IGhostBook {
4242
String getPageText(int pageNum);
4343

4444
/**
45-
* Adds text to the open page (or the title if the book is being signed)
45+
* Adds formatting code to the book (or the title if the book is being signed)
4646
* Only used with unsigned books
4747
*/
48-
void insertText(String insertChars);
48+
void addFormattingCode(String formattingCode);
4949

5050
/**
5151
* Adds a new page (if possible) into the book at the given page number, with the given text

src/main/java/wafflestomper/ghostwriter/gui/screen/GhostwriterEditBookScreen.java

+50-15
Original file line numberDiff line numberDiff line change
@@ -183,26 +183,61 @@ public List<String> pagesAsList() {
183183
// any JSON weirdness
184184
return new ArrayList<>(this.pages);
185185
}
186-
187-
188-
@Override // From IGhostBook
189-
public void insertText(String text) {
190-
if (this.isSigning) {
191-
// Put the text into the title
192-
this.titleEdit.insertText(text);
193-
return;
194-
}
195-
// Put the text into the page
196-
this.pageEdit.insertText(text);
197-
}
198-
199-
186+
187+
200188
@Override // From IGhostBook
201189
public void bookChanged(boolean setModifiedFlag) {
202190
if (setModifiedFlag) this.isModified = true;
203191
this.clearDisplayCache(); // TODO: is this all we need to do now?
204192
}
205-
193+
194+
195+
@Override // From IGhostBook
196+
public void addFormattingCode(String formattingCode) {
197+
// Get a reference to the appropriate editor (title or page, depending on whether we're signing)
198+
TextFieldHelper editor = this.isSigning ? this.titleEdit : this.pageEdit;
199+
200+
// If text is selected, we need to temporarily remove the selection so the text isn't replaced by the
201+
// formatting character
202+
int cursorPos = editor.getCursorPos();
203+
int selectionPos = editor.getSelectionPos();
204+
if (cursorPos != selectionPos){
205+
// Note that when text is selected right to left, selectionPos will be lower than cursorPos.
206+
// The second argument to this function controls whether the selectionPos should be preserved. When
207+
// set to false, the selectionPos will be set to the new cursorPos
208+
editor.setCursorPos(Math.min(cursorPos, selectionPos), false);
209+
}
210+
211+
// Next we add the formatting character at the start of the selection (which will also be to the left
212+
// of the cursor in the case that no text is selected)
213+
editor.insertText(formattingCode);
214+
215+
// If text was selected, we need to re-select the text (optionally adding a reset sequence at the end
216+
// of the selection if the character we're inserting isn't a reset char
217+
if (cursorPos != selectionPos){
218+
int extraCharCount = 2;
219+
220+
// Optionally add reset character at the end of selection
221+
if (!formattingCode.equals("§r")){
222+
editor.setCursorPos(Math.max(cursorPos, selectionPos) + extraCharCount, false);
223+
editor.insertText("§r");
224+
extraCharCount = 4;
225+
}
226+
227+
// Re-select the text (note that it's two characters longer and we have to deal with the case where
228+
// the text was selected right-to-left
229+
if (cursorPos < selectionPos){
230+
editor.setSelectionRange(cursorPos, selectionPos + extraCharCount);
231+
}
232+
else{
233+
editor.setSelectionRange(selectionPos, cursorPos + extraCharCount);
234+
}
235+
}
236+
237+
// Mark the book as changed, which will also update the display
238+
this.bookChanged(true);
239+
}
240+
206241

207242
@Override // From IGhostBook
208243
public boolean isBookBeingSigned() {

src/main/java/wafflestomper/ghostwriter/gui/screen/GhostwriterLecternScreen.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public void setPageText(int pageNum, String pageText) {
116116
}
117117

118118
@Override // From IGhostBook
119-
public void insertText(String insertChars) {
119+
public void addFormattingCode(String formattingCode) {
120120
}
121121

122122
@Override // From IGhostBook

src/main/java/wafflestomper/ghostwriter/gui/screen/GhostwriterReadBookScreen.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public void setPageText(int pageNum, String pageText) {
100100
}
101101

102102
@Override // From IGhostBook
103-
public void insertText(String insertChars) {
103+
public void addFormattingCode(String formattingCode) {
104104
}
105105

106106
@Override // From IGhostBook

update.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"homepage": "https://github.com/waffle-stomper/Ghostwriter/releases",
33
"promos":{
4-
"1.18.2-recommended": "2.3.7",
4+
"1.18.2-recommended": "2.4.2",
55
"1.18.1-recommended": "2.3.5",
66
"1.17.1-recommended": "2.3.4",
77
"1.16.5-recommended": "2.2.6",
@@ -20,6 +20,7 @@
2020
"1.10-recommended": "1.8.5"
2121
},
2222
"1.18.2": {
23+
"2.4.2": "Fix bug where formatting characters replace selected text. Thanks to ZeliotL for the bug report",
2324
"2.3.7": "Update to 1.18.2"
2425
},
2526
"1.18.1": {

0 commit comments

Comments
 (0)