From 40e54245d143263387eba88c154e2307c2e7ec56 Mon Sep 17 00:00:00 2001 From: Evan Debenham Date: Thu, 1 Sep 2022 16:51:18 -0400 Subject: [PATCH] v1.4.0: refactored some code from the journal window --- .../messages/windows/windows.properties | 2 +- .../ui/ScrollingListPane.java | 204 ++++++++++ .../windows/WndJournal.java | 358 +++++------------- 3 files changed, 306 insertions(+), 258 deletions(-) create mode 100644 core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ui/ScrollingListPane.java diff --git a/core/src/main/assets/messages/windows/windows.properties b/core/src/main/assets/messages/windows/windows.properties index 62849f868..ba29c02a1 100644 --- a/core/src/main/assets/messages/windows/windows.properties +++ b/core/src/main/assets/messages/windows/windows.properties @@ -94,7 +94,7 @@ windows.wndjournal.guide=Guide windows.wndjournal.notes=Notes windows.wndjournal.items=Items windows.wndjournal$guidetab.title=Guidebooks -windows.wndjournal$guidetab$guideitem.missing=page missing +windows.wndjournal$guidetab.missing=page missing windows.wndjournal$notestab.keys=Keys windows.wndjournal$notestab.landmarks=Landmarks diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ui/ScrollingListPane.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ui/ScrollingListPane.java new file mode 100644 index 000000000..74cff96b9 --- /dev/null +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ui/ScrollingListPane.java @@ -0,0 +1,204 @@ +/* + * Pixel Dungeon + * Copyright (C) 2012-2015 Oleg Dolya + * + * Shattered Pixel Dungeon + * Copyright (C) 2014-2022 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.scenes.PixelScene; +import com.watabou.noosa.BitmapText; +import com.watabou.noosa.ColorBlock; +import com.watabou.noosa.Image; +import com.watabou.noosa.ui.Component; + +import java.util.ArrayList; + +public class ScrollingListPane extends ScrollPane { + + private ArrayList items = new ArrayList<>(); + + private static final int ITEM_HEIGHT = 18; + + public ScrollingListPane(){ + super(new Component()); + } + + @Override + public void onClick(float x, float y) { + for (Component item : items) { + if ((item instanceof ListItem) && ((ListItem) item).onClick( x, y )) { + break; + } + } + } + + public void addItem( Image icon, String iconText, String text ){ + addItem( new ListItem(icon, iconText, text) ); + } + + public void addItem( ListItem item ){ + content.add(item); + items.add(item); + layout(); + } + + public void addTitle( String text ){ + ListTitle title = new ListTitle(text); + content.add(title); + items.add(title); + layout(); + } + + @Override + public synchronized void clear() { + content.clear(); + items.clear(); + } + + @Override + protected void layout() { + super.layout(); + + float pos = 0; + for (Component item : items){ + item.setRect(0, pos, width, ITEM_HEIGHT); + pos += item.height(); + } + + content.setSize(width, pos); + } + + public static class ListItem extends Component { + + protected Image icon; + protected BitmapText iconLabel; + protected RenderedTextBlock label; + protected ColorBlock line; + + public ListItem( Image icon, String text ) { + this(icon, null, text); + } + + public ListItem( Image icon, String iconText, String text ) { + super(); + + if (icon != null) { + this.icon.copy(icon); + } else { + remove(label); + label = PixelScene.renderTextBlock(9); + add(label); + } + + label.text( text ); + + if (iconText != null) { + iconLabel.text(iconText); + iconLabel.measure(); + } + } + + public boolean onClick( float x, float y ){ + return false; + } + + public void hardlight( int color ){ + iconLabel.hardlight(color); + label.hardlight(color); + } + + public void hardlightIcon( int color ){ + icon.hardlight(color); + } + + @Override + protected void createChildren() { + icon = new Image(); + add( icon ); + + iconLabel = new BitmapText( PixelScene.pixelFont); + add( iconLabel ); + + label = PixelScene.renderTextBlock( 7 ); + add( label ); + + line = new ColorBlock( 1, 1, 0xFF222222); + add(line); + + } + + @Override + protected void layout() { + + icon.y = y + 1 + (height() - 1 - icon.height()) / 2f; + icon.x = x + (16 - icon.width())/2f; + PixelScene.align(icon); + + iconLabel.x = icon.x + (icon.width - iconLabel.width()) / 2f; + iconLabel.y = icon.y + (icon.height - iconLabel.height()) / 2f + 0.5f; + PixelScene.align(iconLabel); + + line.size(width, 1); + line.x = x; + line.y = y; + + label.maxWidth((int)(width - 16 - 1)); + label.setPos(x + 17, y + (height() - label.height()) / 2f); + PixelScene.align(label); + } + } + + public static class ListTitle extends Component { + + protected RenderedTextBlock label; + protected ColorBlock line; + + public ListTitle (String title){ + super(); + label.text(title); + } + + @Override + protected void createChildren() { + label = PixelScene.renderTextBlock( 9 ); + label.hardlight(Window.TITLE_COLOR); + add( label ); + + line = new ColorBlock( 1, 1, 0xFF222222); + add(line); + + } + + @Override + protected void layout() { + + line.size(width, 1); + line.x = x; + line.y = y; + + label.maxWidth((int)(width - 1)); + label.setPos((width-label.width())/2f, + y + (height() - label.height()) / 2f); + PixelScene.align(label); + } + + } + + +} 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 0e33f0978..b6b6a45c0 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/windows/WndJournal.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/windows/WndJournal.java @@ -41,6 +41,7 @@ import com.shatteredpixel.shatteredpixeldungeon.ui.QuickRecipe; import com.shatteredpixel.shatteredpixeldungeon.ui.RedButton; import com.shatteredpixel.shatteredpixeldungeon.ui.RenderedTextBlock; import com.shatteredpixel.shatteredpixeldungeon.ui.ScrollPane; +import com.shatteredpixel.shatteredpixeldungeon.ui.ScrollingListPane; import com.watabou.noosa.BitmapText; import com.watabou.noosa.ColorBlock; import com.watabou.noosa.Image; @@ -143,91 +144,14 @@ public class WndJournal extends WndTabbed { catalogTab.layout(); notesTab.layout(); } - - private static class ListItem extends Component { - - protected RenderedTextBlock label; - protected BitmapText depth; - protected ColorBlock line; - protected Image icon; - - public ListItem( Image icon, String text ) { - this(icon, text, -1); - } - - public ListItem( Image icon, String text, int d ) { - super(); - - this.icon.copy(icon); - - label.text( text ); - - if (d >= 0) { - depth.text(Integer.toString(d)); - depth.measure(); - - if (d == Dungeon.depth) { - label.hardlight(TITLE_COLOR); - depth.hardlight(TITLE_COLOR); - } - } - } - - @Override - protected void createChildren() { - label = PixelScene.renderTextBlock( 7 ); - add( label ); - - icon = new Image(); - add( icon ); - - depth = new BitmapText( PixelScene.pixelFont); - add( depth ); - - line = new ColorBlock( 1, 1, 0xFF222222); - add(line); - - } - - @Override - protected void layout() { - - icon.y = y + 1 + (height() - 1 - icon.height()) / 2f; - icon.x = x + (16 - icon.width())/2f; - PixelScene.align(icon); - - depth.x = icon.x + (icon.width - depth.width()) / 2f; - depth.y = icon.y + (icon.height - depth.height()) / 2f + 1; - PixelScene.align(depth); - - line.size(width, 1); - line.x = 0; - line.y = y; - - label.maxWidth((int)(width - 16 - 1)); - label.setPos(17, y + 1 + (height() - label.height()) / 2f); - PixelScene.align(label); - } - } public static class GuideTab extends Component { - - private ScrollPane list; - private ArrayList pages = new ArrayList<>(); + + private ScrollingListPane list; @Override protected void createChildren() { - list = new ScrollPane( new Component() ){ - @Override - public void onClick( float x, float y ) { - int size = pages.size(); - for (int i=0; i < size; i++) { - if (pages.get( i ).onClick( x, y )) { - break; - } - } - } - }; + list = new ScrollingListPane(); add( list ); } @@ -238,68 +162,36 @@ public class WndJournal extends WndTabbed { } private void updateList(){ - Component content = list.content(); - - float pos = 0; - - ColorBlock line = new ColorBlock( width(), 1, 0xFF222222); - line.y = pos; - content.add(line); - - RenderedTextBlock title = PixelScene.renderTextBlock(Document.ADVENTURERS_GUIDE.title(), 9); - title.hardlight(TITLE_COLOR); - title.maxWidth( (int)width() - 2 ); - title.setPos( (width() - title.width())/2f, pos + 1 + ((ITEM_HEIGHT) - title.height())/2f); - PixelScene.align(title); - content.add(title); - - pos += Math.max(ITEM_HEIGHT, title.height()); - + list.addTitle(Document.ADVENTURERS_GUIDE.title()); + for (String page : Document.ADVENTURERS_GUIDE.pageNames()){ - GuideItem item = new GuideItem( page ); - - item.setRect( 0, pos, width(), ITEM_HEIGHT ); - content.add( item ); - - pos += item.height(); - pages.add(item); - } - - content.setSize( width(), pos ); - list.setSize( list.width(), list.height() ); - } - - private static class GuideItem extends ListItem { - - private boolean found = false; - private String page; - - public GuideItem( String page ){ - super( iconForPage(page), Messages.titleCase(Document.ADVENTURERS_GUIDE.pageTitle(page))); - - this.page = page; - found = Document.ADVENTURERS_GUIDE.isPageFound(page); - - if (!found) { - icon.hardlight( 0.5f, 0.5f, 0.5f); - label.text( Messages.titleCase(Messages.get( this, "missing" ))); - label.hardlight( 0x999999 ); + boolean found = Document.ADVENTURERS_GUIDE.isPageFound(page); + ScrollingListPane.ListItem item = new ScrollingListPane.ListItem( + iconForPage(page), + null, + found ? Messages.titleCase(Document.ADVENTURERS_GUIDE.pageTitle(page)) : Messages.titleCase(Messages.get( this, "missing" )) + ){ + @Override + public boolean onClick(float x, float y) { + if (inside( x, y ) && found) { + GameScene.show( new WndStory( iconForPage(page), + Document.ADVENTURERS_GUIDE.pageTitle(page), + Document.ADVENTURERS_GUIDE.pageBody(page) )); + Document.ADVENTURERS_GUIDE.readPage(page); + return true; + } else { + return false; + } + } + }; + if (!found){ + item.hardlight(0x999999); + item.hardlightIcon(0x999999); } - + list.addItem(item); } - - public boolean onClick( float x, float y ) { - if (inside( x, y ) && found) { - GameScene.show( new WndStory( iconForPage(page), - Document.ADVENTURERS_GUIDE.pageTitle(page), - Document.ADVENTURERS_GUIDE.pageBody(page) )); - Document.ADVENTURERS_GUIDE.readPage(page); - return true; - } else { - return false; - } - } - + + list.setRect(x, y, width, height); } //TODO might just want this to be part of the Document class @@ -346,7 +238,7 @@ public class WndJournal extends WndTabbed { private static final int[] spriteIndexes = {10, 12, 7, 9, 11, 8, 3, 13, 14, 15}; - public static int currentPageIdx = -1; + public static int currentPageIdx = 0; private IconTitle title; private RenderedTextBlock body; @@ -420,6 +312,10 @@ public class WndJournal extends WndTabbed { private void updateList() { + if (!Document.ALCHEMY_GUIDE.isPageFound(currentPageIdx)){ + currentPageIdx = -1; + } + for (int i = 0; i < NUM_BUTTONS; i++) { if (i == currentPageIdx) { pageButtons[i].icon().color(TITLE_COLOR); @@ -514,77 +410,52 @@ public class WndJournal extends WndTabbed { private static class NotesTab extends Component { - private ScrollPane list; + private ScrollingListPane list; @Override protected void createChildren() { - list = new ScrollPane( new Component() ); + list = new ScrollingListPane(); add( list ); } @Override protected void layout() { super.layout(); - list.setRect( 0, 0, width, height); + list.setRect( x, y, width, height); } private void updateList(){ - Component content = list.content(); - - float pos = 0; - //Keys ArrayList keys = Notes.getRecords(Notes.KeyRecord.class); if (!keys.isEmpty()){ - ColorBlock line = new ColorBlock( width(), 1, 0xFF222222); - line.y = pos; - content.add(line); - - RenderedTextBlock title = PixelScene.renderTextBlock(Messages.get(this, "keys"), 9); - title.hardlight(TITLE_COLOR); - title.maxWidth( (int)width() - 2 ); - title.setPos( (width() - title.width())/2f, pos + 1 + ((ITEM_HEIGHT) - title.height())/2f); - PixelScene.align(title); - content.add(title); - - pos += Math.max(ITEM_HEIGHT, title.height()); - } - for(Notes.Record rec : keys){ - ListItem item = new ListItem( Icons.get(Icons.STAIRS), - Messages.titleCase(rec.desc()), rec.depth() ); - item.setRect( 0, pos, width(), ITEM_HEIGHT ); - content.add( item ); - - pos += item.height(); + list.addTitle(Messages.get(this, "keys")); + + for(Notes.Record rec : keys){ + ScrollingListPane.ListItem item = new ScrollingListPane.ListItem( Icons.get(Icons.STAIRS), + Integer.toString(rec.depth()), + Messages.titleCase(rec.desc())); + if (Dungeon.depth == rec.depth()) item.hardlight(TITLE_COLOR); + list.addItem(item); + } } //Landmarks ArrayList landmarks = Notes.getRecords(Notes.LandmarkRecord.class); if (!landmarks.isEmpty()){ - ColorBlock line = new ColorBlock( width(), 1, 0xFF222222); - line.y = pos; - content.add(line); - - RenderedTextBlock title = PixelScene.renderTextBlock(Messages.get(this, "landmarks"), 9); - title.hardlight(TITLE_COLOR); - title.maxWidth( (int)width() - 2 ); - title.setPos( (width() - title.width())/2f, pos + 1 + ((ITEM_HEIGHT) - title.height())/2f); - PixelScene.align(title); - content.add(title); - - pos += Math.max(ITEM_HEIGHT, title.height()); + + list.addTitle(Messages.get(this, "landmarks")); + + for (Notes.Record rec : landmarks) { + ScrollingListPane.ListItem item = new ScrollingListPane.ListItem( Icons.get(Icons.STAIRS), + Integer.toString(rec.depth()), + Messages.titleCase(rec.desc())); + if (Dungeon.depth == rec.depth()) item.hardlight(TITLE_COLOR); + list.addItem(item); + } + } - for (Notes.Record rec : landmarks) { - ListItem item = new ListItem( Icons.get(Icons.STAIRS), - Messages.titleCase(rec.desc()), rec.depth() ); - item.setRect( 0, pos, width(), ITEM_HEIGHT ); - content.add( item ); - - pos += item.height(); - } - - content.setSize( width(), pos ); - list.setSize( list.width(), list.height() ); + + list.setRect(x, y, width, height); } } @@ -606,10 +477,8 @@ public class WndJournal extends WndTabbed { private static final int SCROLL_IDX = 6; private static final int spriteIndexes[] = {1, 2, 4, 5, 6, 9, 11}; - - private ScrollPane list; - - private ArrayList items = new ArrayList<>(); + + private ScrollingListPane list; @Override protected void createChildren() { @@ -626,18 +495,8 @@ public class WndJournal extends WndTabbed { itemButtons[i].icon(new ItemSprite(ItemSpriteSheet.SOMETHING + spriteIndexes[i], null)); add( itemButtons[i] ); } - - list = new ScrollPane( new Component() ) { - @Override - public void onClick( float x, float y ) { - int size = items.size(); - for (int i=0; i < size; i++) { - if (items.get( i ).onClick( x, y )) { - break; - } - } - } - }; + + list = new ScrollingListPane(); add( list ); } @@ -660,7 +519,7 @@ public class WndJournal extends WndTabbed { private void updateList() { - items.clear(); + list.clear(); for (int i = 0; i < NUM_BUTTONS; i++){ if (i == currentItemIdx){ @@ -670,8 +529,6 @@ public class WndJournal extends WndTabbed { } } - Component content = list.content(); - content.clear(); list.scrollTo( 0, 0 ); ArrayList> itemClasses; @@ -716,32 +573,11 @@ public class WndJournal extends WndTabbed { } }); - float pos = 0; for (Class itemClass : itemClasses) { - CatalogItem item = new CatalogItem(Reflection.newInstance(itemClass), known.get(itemClass), Catalog.isSeen(itemClass)); - item.setRect( 0, pos, width, ITEM_HEIGHT ); - content.add( item ); - items.add( item ); - - pos += item.height(); - } - - content.setSize( width, pos ); - list.setSize( list.width(), list.height() ); - } - - private static class CatalogItem extends ListItem { - - private Item item; - private boolean seen; - - public CatalogItem(Item item, boolean IDed, boolean seen ) { - super( new ItemSprite(item), Messages.titleCase(item.trueName())); - - this.item = item; - this.seen = seen; - - if ( seen && !IDed ){ + Item item = Reflection.newInstance(itemClass); + boolean itemIDed = known.get(itemClass); + boolean itemSeen = Catalog.isSeen(itemClass); + if ( itemSeen && !itemIDed ){ if (item instanceof Ring){ ((Ring) item).anonymize(); } else if (item instanceof Potion){ @@ -750,32 +586,40 @@ public class WndJournal extends WndTabbed { ((Scroll) item).anonymize(); } } - - if (!seen) { - icon.copy( new ItemSprite( ItemSpriteSheet.SOMETHING + spriteIndexes[currentItemIdx], null) ); - label.text("???"); - label.hardlight( 0x999999 ); - } else if (!IDed) { - icon.copy( new ItemSprite( ItemSpriteSheet.SOMETHING + spriteIndexes[currentItemIdx], null) ); - label.hardlight( 0xCCCCCC ); - } - - } - - public boolean onClick( float x, float y ) { - if (inside( x, y ) && seen) { - if (item instanceof ClassArmor){ - GameScene.show(new WndTitledMessage(new Image(icon), - Messages.titleCase(item.trueName()), item.desc())); - } else { - GameScene.show(new WndTitledMessage(new Image(icon), - Messages.titleCase(item.trueName()), item.info())); + ScrollingListPane.ListItem listItem = new ScrollingListPane.ListItem( + (itemIDed && itemSeen) ? new ItemSprite(item) : new ItemSprite( ItemSpriteSheet.SOMETHING + spriteIndexes[currentItemIdx]), + null, + itemSeen ? Messages.titleCase(item.trueName()) : "???" + ){ + @Override + public boolean onClick(float x, float y) { + if (inside( x, y ) && itemSeen) { + if (item instanceof ClassArmor){ + GameScene.show(new WndTitledMessage(new Image(icon), + Messages.titleCase(item.trueName()), item.desc())); + } else { + GameScene.show(new WndTitledMessage(new Image(icon), + Messages.titleCase(item.trueName()), item.info())); + } + return true; + } else { + return false; + } } - return true; - } else { - return false; + }; + + if (!itemSeen) { + listItem.hardlight( 0x999999 ); + } else if (!itemIDed) { + listItem.hardlight( 0xCCCCCC ); } + + list.addItem(listItem); + } + + list.setRect(x, itemButtons[NUM_BUTTONS-1].bottom() + 1, width, + height - itemButtons[NUM_BUTTONS-1].bottom() - 1); } }