From 07e3e1f72457d73fe0d2660eee1f495fb08dcede Mon Sep 17 00:00:00 2001 From: Evan Debenham Date: Sun, 20 Apr 2025 13:40:18 -0400 Subject: [PATCH] v3.1.0: implemented a custom note button for WndUseItem --- .../ui/CustomNoteButton.java | 24 +++- .../ui/ItemJournalButton.java | 109 ++++++++++++++++++ .../windows/WndJournal.java | 2 +- .../windows/WndUseItem.java | 35 ++++-- 4 files changed, 151 insertions(+), 19 deletions(-) create mode 100644 core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ui/ItemJournalButton.java diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ui/CustomNoteButton.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ui/CustomNoteButton.java index 1bbcc4cb1..0e9774444 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ui/CustomNoteButton.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ui/CustomNoteButton.java @@ -38,6 +38,7 @@ import com.shatteredpixel.shatteredpixeldungeon.windows.WndJournalItem; import com.shatteredpixel.shatteredpixeldungeon.windows.WndOptions; import com.shatteredpixel.shatteredpixeldungeon.windows.WndTextInput; import com.shatteredpixel.shatteredpixeldungeon.windows.WndTitledMessage; +import com.shatteredpixel.shatteredpixeldungeon.windows.WndUseItem; import com.watabou.utils.Reflection; import java.util.ArrayList; @@ -265,7 +266,7 @@ public class CustomNoteButton extends IconButton { public static class CustomNoteWindow extends WndJournalItem { - public CustomNoteWindow(Notes.CustomRecord rec) { + public CustomNoteWindow(Notes.CustomRecord rec, Window parentWindow) { super(rec.icon(), rec.title(), rec.desc()); RedButton title = new RedButton( Messages.get(CustomNoteWindow.class, "edit_title") ){ @@ -283,7 +284,13 @@ public class CustomNoteButton extends IconButton { if (positive && !text.isEmpty()){ rec.editText(text, rec.desc()); CustomNoteWindow.this.hide(); - ShatteredPixelDungeon.scene().addToFront(new CustomNoteWindow(rec)); + if (parentWindow instanceof WndUseItem){ + WndUseItem newParent = new WndUseItem(((WndUseItem) parentWindow).owner, ((WndUseItem) parentWindow).item); + GameScene.show(newParent); + GameScene.show(new CustomNoteWindow(rec, newParent)); + } else { + GameScene.show(new CustomNoteWindow(rec, parentWindow)); + } } } }); @@ -308,7 +315,7 @@ public class CustomNoteButton extends IconButton { if (positive){ rec.editText(rec.title(), text); CustomNoteWindow.this.hide(); - ShatteredPixelDungeon.scene().addToFront(new CustomNoteWindow(rec)); + GameScene.show(new CustomNoteWindow(rec, parentWindow)); } } }); @@ -330,7 +337,11 @@ public class CustomNoteButton extends IconButton { if (index == 0){ Notes.remove(rec); CustomNoteWindow.this.hide(); - ShatteredPixelDungeon.scene().addToFront(new WndJournal()); + if (parentWindow instanceof WndJournal || parentWindow == null){ + ShatteredPixelDungeon.scene().addToFront(new WndJournal()); + } else if (parentWindow instanceof WndUseItem){ + GameScene.show(new WndUseItem(((WndUseItem) parentWindow).owner, ((WndUseItem) parentWindow).item)); + } } } }); @@ -371,8 +382,9 @@ public class CustomNoteButton extends IconButton { NOTE_SELECT_INSTANCE.onBackPressed(); } hide(); - ShatteredPixelDungeon.scene().addToFront(new WndJournal()); - ShatteredPixelDungeon.scene().addToFront(new CustomNoteWindow(note)); + WndJournal wnd = new WndJournal(); + ShatteredPixelDungeon.scene().addToFront(wnd); + ShatteredPixelDungeon.scene().addToFront(new CustomNoteWindow(note, wnd)); } } }); diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ui/ItemJournalButton.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ui/ItemJournalButton.java new file mode 100644 index 000000000..092d1879d --- /dev/null +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ui/ItemJournalButton.java @@ -0,0 +1,109 @@ +/* + * Pixel Dungeon + * Copyright (C) 2012-2015 Oleg Dolya + * + * Shattered Pixel Dungeon + * Copyright (C) 2014-2025 Evan Debenham + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + */ + +package com.shatteredpixel.shatteredpixeldungeon.ui; + +import com.shatteredpixel.shatteredpixeldungeon.Dungeon; +import com.shatteredpixel.shatteredpixeldungeon.ShatteredPixelDungeon; +import com.shatteredpixel.shatteredpixeldungeon.items.EquipableItem; +import com.shatteredpixel.shatteredpixeldungeon.items.Item; +import com.shatteredpixel.shatteredpixeldungeon.journal.Notes; +import com.shatteredpixel.shatteredpixeldungeon.messages.Messages; +import com.shatteredpixel.shatteredpixeldungeon.scenes.GameScene; +import com.shatteredpixel.shatteredpixeldungeon.scenes.SupporterScene; +import com.shatteredpixel.shatteredpixeldungeon.services.payment.Payment; +import com.shatteredpixel.shatteredpixeldungeon.windows.WndOptions; +import com.shatteredpixel.shatteredpixeldungeon.windows.WndTextInput; +import com.shatteredpixel.shatteredpixeldungeon.windows.WndTitledMessage; +import com.shatteredpixel.shatteredpixeldungeon.windows.WndUseItem; + +public class ItemJournalButton extends IconButton { + + Item item; + Window parentWnd; + + public ItemJournalButton(Item item, Window parentWnd){ + super(Icons.JOURNAL.get()); + this.item = item; + this.parentWnd = parentWnd; + } + + @Override + protected void onClick() { + + customNote(); + + } + + private void customNote(){ + Notes.CustomRecord note = null; + if (item instanceof EquipableItem){ + note = Notes.findCustomRecord(((EquipableItem) item).customNoteID); + } + if (note == null) { + note = Notes.findCustomRecord(item.getClass()); + } + //TODO custom note functionality for rings doesn't really work atm, need to let them have equip-specific notes + if (note == null){ + if (Notes.getRecords(Notes.CustomRecord.class).size() >= Notes.customRecordLimit()){ + GameScene.show(new WndTitledMessage(Icons.INFO.get(), + Messages.get(CustomNoteButton.class, "limit_title"), + Messages.get(CustomNoteButton.class, "limit_text"))); + } else { + note = new Notes.CustomRecord(item, "", ""); + note.assignID(); + if (item instanceof EquipableItem){ + ((EquipableItem) item).customNoteID = note.ID(); + } + addNote(parentWnd, note, Messages.get(CustomNoteButton.class, "new_inv"), + Messages.get(CustomNoteButton.class, "new_item_title", Messages.titleCase(item.name()))); + } + } else { + GameScene.show(new CustomNoteButton.CustomNoteWindow(note, parentWnd)); + } + } + + private static void addNote(Window parentWindow, Notes.CustomRecord note, String promptTitle, String prompttext){ + GameScene.show(new WndTextInput(promptTitle, + prompttext, + "", + 50, + false, + Messages.get(CustomNoteButton.CustomNoteWindow.class, "confirm"), + Messages.get(CustomNoteButton.CustomNoteWindow.class, "cancel")){ + @Override + public void onSelect(boolean positive, String text) { + if (positive && !text.isEmpty()){ + Notes.add(note); + note.editText(text, ""); + if (parentWindow != null) { + parentWindow.hide(); + } + + hide(); + if (parentWindow instanceof WndUseItem){ + GameScene.show(new WndUseItem(((WndUseItem) parentWindow).owner, ((WndUseItem) parentWindow).item)); + } + } + } + }); + } +} diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/windows/WndJournal.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/windows/WndJournal.java index b94549289..73b6ce002 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/windows/WndJournal.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/windows/WndJournal.java @@ -503,7 +503,7 @@ public class WndJournal extends WndTabbed { @Override public boolean onClick(float x, float y) { if (inside(x, y)) { - GameScene.show(new CustomNoteButton.CustomNoteWindow(rec)); + GameScene.show(new CustomNoteButton.CustomNoteWindow(rec, WndJournal.INSTANCE)); return true; } else { return false; diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/windows/WndUseItem.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/windows/WndUseItem.java index 776b959b6..83ef9ce82 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/windows/WndUseItem.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/windows/WndUseItem.java @@ -24,6 +24,7 @@ package com.shatteredpixel.shatteredpixeldungeon.windows; import com.shatteredpixel.shatteredpixeldungeon.Dungeon; import com.shatteredpixel.shatteredpixeldungeon.items.Item; import com.shatteredpixel.shatteredpixeldungeon.ui.InventoryPane; +import com.shatteredpixel.shatteredpixeldungeon.ui.ItemJournalButton; import com.shatteredpixel.shatteredpixeldungeon.ui.RedButton; import com.shatteredpixel.shatteredpixeldungeon.ui.Window; @@ -34,44 +35,54 @@ public class WndUseItem extends WndInfoItem { private static final float BUTTON_HEIGHT = 16; private static final float GAP = 2; - + + public Window owner; + public Item item; + public WndUseItem( final Window owner, final Item item ) { super(item); + this.owner = owner; + this.item = item; + float y = height; if (Dungeon.hero.isAlive() && Dungeon.hero.belongings.contains(item)) { y += GAP; ArrayList buttons = new ArrayList<>(); - for (final String action : item.actions( Dungeon.hero )) { - - RedButton btn = new RedButton( item.actionName(action, Dungeon.hero), 8 ) { + for (final String action : item.actions(Dungeon.hero)) { + + RedButton btn = new RedButton(item.actionName(action, Dungeon.hero), 8) { @Override protected void onClick() { hide(); if (owner != null && owner.parent != null) owner.hide(); - if (Dungeon.hero.isAlive() && Dungeon.hero.belongings.contains(item)){ - item.execute( Dungeon.hero, action ); + if (Dungeon.hero.isAlive() && Dungeon.hero.belongings.contains(item)) { + item.execute(Dungeon.hero, action); } Item.updateQuickslot(); - if (action.equals(item.defaultAction()) && item.usesTargeting && owner == null){ + if (action.equals(item.defaultAction()) && item.usesTargeting && owner == null) { InventoryPane.useTargeting(); } } }; - btn.setSize( btn.reqWidth(), BUTTON_HEIGHT ); + btn.setSize(btn.reqWidth(), BUTTON_HEIGHT); buttons.add(btn); - add( btn ); + add(btn); if (action.equals(item.defaultAction())) { - btn.textColor( TITLE_COLOR ); + btn.textColor(TITLE_COLOR); } - + } y = layoutButtons(buttons, width, y); + + ItemJournalButton btn = new ItemJournalButton(item, this); + btn.setRect(width - 16, 0, 16, 16); + add(btn); } - + resize( width, (int)(y) ); }