v1.4.0: refactored some code from the journal window

This commit is contained in:
Evan Debenham
2022-09-01 16:51:18 -04:00
parent c6cc0d78e1
commit 40e54245d1
3 changed files with 306 additions and 258 deletions
@@ -94,7 +94,7 @@ windows.wndjournal.guide=Guide
windows.wndjournal.notes=Notes windows.wndjournal.notes=Notes
windows.wndjournal.items=Items windows.wndjournal.items=Items
windows.wndjournal$guidetab.title=Guidebooks 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.keys=Keys
windows.wndjournal$notestab.landmarks=Landmarks windows.wndjournal$notestab.landmarks=Landmarks
@@ -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 <http://www.gnu.org/licenses/>
*/
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<Component> 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);
}
}
}
@@ -41,6 +41,7 @@ import com.shatteredpixel.shatteredpixeldungeon.ui.QuickRecipe;
import com.shatteredpixel.shatteredpixeldungeon.ui.RedButton; import com.shatteredpixel.shatteredpixeldungeon.ui.RedButton;
import com.shatteredpixel.shatteredpixeldungeon.ui.RenderedTextBlock; import com.shatteredpixel.shatteredpixeldungeon.ui.RenderedTextBlock;
import com.shatteredpixel.shatteredpixeldungeon.ui.ScrollPane; import com.shatteredpixel.shatteredpixeldungeon.ui.ScrollPane;
import com.shatteredpixel.shatteredpixeldungeon.ui.ScrollingListPane;
import com.watabou.noosa.BitmapText; import com.watabou.noosa.BitmapText;
import com.watabou.noosa.ColorBlock; import com.watabou.noosa.ColorBlock;
import com.watabou.noosa.Image; import com.watabou.noosa.Image;
@@ -144,90 +145,13 @@ public class WndJournal extends WndTabbed {
notesTab.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 { public static class GuideTab extends Component {
private ScrollPane list; private ScrollingListPane list;
private ArrayList<GuideItem> pages = new ArrayList<>();
@Override @Override
protected void createChildren() { protected void createChildren() {
list = new ScrollPane( new Component() ){ list = new ScrollingListPane();
@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;
}
}
}
};
add( list ); add( list );
} }
@@ -238,68 +162,36 @@ public class WndJournal extends WndTabbed {
} }
private void updateList(){ private void updateList(){
Component content = list.content(); list.addTitle(Document.ADVENTURERS_GUIDE.title());
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());
for (String page : Document.ADVENTURERS_GUIDE.pageNames()){ for (String page : Document.ADVENTURERS_GUIDE.pageNames()){
GuideItem item = new GuideItem( page ); boolean found = Document.ADVENTURERS_GUIDE.isPageFound(page);
ScrollingListPane.ListItem item = new ScrollingListPane.ListItem(
item.setRect( 0, pos, width(), ITEM_HEIGHT ); iconForPage(page),
content.add( item ); null,
found ? Messages.titleCase(Document.ADVENTURERS_GUIDE.pageTitle(page)) : Messages.titleCase(Messages.get( this, "missing" ))
pos += item.height(); ){
pages.add(item); @Override
} public boolean onClick(float x, float y) {
if (inside( x, y ) && found) {
content.setSize( width(), pos ); GameScene.show( new WndStory( iconForPage(page),
list.setSize( list.width(), list.height() ); Document.ADVENTURERS_GUIDE.pageTitle(page),
} Document.ADVENTURERS_GUIDE.pageBody(page) ));
Document.ADVENTURERS_GUIDE.readPage(page);
private static class GuideItem extends ListItem { return true;
} else {
private boolean found = false; return false;
private String page; }
}
public GuideItem( String page ){ };
super( iconForPage(page), Messages.titleCase(Document.ADVENTURERS_GUIDE.pageTitle(page))); if (!found){
item.hardlight(0x999999);
this.page = page; item.hardlightIcon(0x999999);
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 );
}
}
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.addItem(item);
} }
list.setRect(x, y, width, height);
} }
//TODO might just want this to be part of the Document class //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}; 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 IconTitle title;
private RenderedTextBlock body; private RenderedTextBlock body;
@@ -420,6 +312,10 @@ public class WndJournal extends WndTabbed {
private void updateList() { private void updateList() {
if (!Document.ALCHEMY_GUIDE.isPageFound(currentPageIdx)){
currentPageIdx = -1;
}
for (int i = 0; i < NUM_BUTTONS; i++) { for (int i = 0; i < NUM_BUTTONS; i++) {
if (i == currentPageIdx) { if (i == currentPageIdx) {
pageButtons[i].icon().color(TITLE_COLOR); pageButtons[i].icon().color(TITLE_COLOR);
@@ -514,77 +410,52 @@ public class WndJournal extends WndTabbed {
private static class NotesTab extends Component { private static class NotesTab extends Component {
private ScrollPane list; private ScrollingListPane list;
@Override @Override
protected void createChildren() { protected void createChildren() {
list = new ScrollPane( new Component() ); list = new ScrollingListPane();
add( list ); add( list );
} }
@Override @Override
protected void layout() { protected void layout() {
super.layout(); super.layout();
list.setRect( 0, 0, width, height); list.setRect( x, y, width, height);
} }
private void updateList(){ private void updateList(){
Component content = list.content();
float pos = 0;
//Keys //Keys
ArrayList<Notes.KeyRecord> keys = Notes.getRecords(Notes.KeyRecord.class); ArrayList<Notes.KeyRecord> keys = Notes.getRecords(Notes.KeyRecord.class);
if (!keys.isEmpty()){ if (!keys.isEmpty()){
ColorBlock line = new ColorBlock( width(), 1, 0xFF222222); list.addTitle(Messages.get(this, "keys"));
line.y = pos;
content.add(line);
RenderedTextBlock title = PixelScene.renderTextBlock(Messages.get(this, "keys"), 9); for(Notes.Record rec : keys){
title.hardlight(TITLE_COLOR); ScrollingListPane.ListItem item = new ScrollingListPane.ListItem( Icons.get(Icons.STAIRS),
title.maxWidth( (int)width() - 2 ); Integer.toString(rec.depth()),
title.setPos( (width() - title.width())/2f, pos + 1 + ((ITEM_HEIGHT) - title.height())/2f); Messages.titleCase(rec.desc()));
PixelScene.align(title); if (Dungeon.depth == rec.depth()) item.hardlight(TITLE_COLOR);
content.add(title); list.addItem(item);
}
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();
} }
//Landmarks //Landmarks
ArrayList<Notes.LandmarkRecord> landmarks = Notes.getRecords(Notes.LandmarkRecord.class); ArrayList<Notes.LandmarkRecord> landmarks = Notes.getRecords(Notes.LandmarkRecord.class);
if (!landmarks.isEmpty()){ 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); list.addTitle(Messages.get(this, "landmarks"));
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 : landmarks) {
} ScrollingListPane.ListItem item = new ScrollingListPane.ListItem( Icons.get(Icons.STAIRS),
for (Notes.Record rec : landmarks) { Integer.toString(rec.depth()),
ListItem item = new ListItem( Icons.get(Icons.STAIRS), Messages.titleCase(rec.desc()));
Messages.titleCase(rec.desc()), rec.depth() ); if (Dungeon.depth == rec.depth()) item.hardlight(TITLE_COLOR);
item.setRect( 0, pos, width(), ITEM_HEIGHT ); list.addItem(item);
content.add( item ); }
pos += item.height();
} }
content.setSize( width(), pos ); list.setRect(x, y, width, height);
list.setSize( list.width(), list.height() );
} }
} }
@@ -607,9 +478,7 @@ public class WndJournal extends WndTabbed {
private static final int spriteIndexes[] = {1, 2, 4, 5, 6, 9, 11}; private static final int spriteIndexes[] = {1, 2, 4, 5, 6, 9, 11};
private ScrollPane list; private ScrollingListPane list;
private ArrayList<CatalogItem> items = new ArrayList<>();
@Override @Override
protected void createChildren() { protected void createChildren() {
@@ -627,17 +496,7 @@ public class WndJournal extends WndTabbed {
add( itemButtons[i] ); add( itemButtons[i] );
} }
list = new ScrollPane( new Component() ) { list = new ScrollingListPane();
@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;
}
}
}
};
add( list ); add( list );
} }
@@ -660,7 +519,7 @@ public class WndJournal extends WndTabbed {
private void updateList() { private void updateList() {
items.clear(); list.clear();
for (int i = 0; i < NUM_BUTTONS; i++){ for (int i = 0; i < NUM_BUTTONS; i++){
if (i == currentItemIdx){ if (i == currentItemIdx){
@@ -670,8 +529,6 @@ public class WndJournal extends WndTabbed {
} }
} }
Component content = list.content();
content.clear();
list.scrollTo( 0, 0 ); list.scrollTo( 0, 0 );
ArrayList<Class<? extends Item>> itemClasses; ArrayList<Class<? extends Item>> itemClasses;
@@ -716,32 +573,11 @@ public class WndJournal extends WndTabbed {
} }
}); });
float pos = 0;
for (Class<? extends Item> itemClass : itemClasses) { for (Class<? extends Item> itemClass : itemClasses) {
CatalogItem item = new CatalogItem(Reflection.newInstance(itemClass), known.get(itemClass), Catalog.isSeen(itemClass)); Item item = Reflection.newInstance(itemClass);
item.setRect( 0, pos, width, ITEM_HEIGHT ); boolean itemIDed = known.get(itemClass);
content.add( item ); boolean itemSeen = Catalog.isSeen(itemClass);
items.add( item ); if ( itemSeen && !itemIDed ){
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 ){
if (item instanceof Ring){ if (item instanceof Ring){
((Ring) item).anonymize(); ((Ring) item).anonymize();
} else if (item instanceof Potion){ } else if (item instanceof Potion){
@@ -750,32 +586,40 @@ public class WndJournal extends WndTabbed {
((Scroll) item).anonymize(); ((Scroll) item).anonymize();
} }
} }
ScrollingListPane.ListItem listItem = new ScrollingListPane.ListItem(
if (!seen) { (itemIDed && itemSeen) ? new ItemSprite(item) : new ItemSprite( ItemSpriteSheet.SOMETHING + spriteIndexes[currentItemIdx]),
icon.copy( new ItemSprite( ItemSpriteSheet.SOMETHING + spriteIndexes[currentItemIdx], null) ); null,
label.text("???"); itemSeen ? Messages.titleCase(item.trueName()) : "???"
label.hardlight( 0x999999 ); ){
} else if (!IDed) { @Override
icon.copy( new ItemSprite( ItemSpriteSheet.SOMETHING + spriteIndexes[currentItemIdx], null) ); public boolean onClick(float x, float y) {
label.hardlight( 0xCCCCCC ); if (inside( x, y ) && itemSeen) {
} if (item instanceof ClassArmor){
GameScene.show(new WndTitledMessage(new Image(icon),
} Messages.titleCase(item.trueName()), item.desc()));
} else {
public boolean onClick( float x, float y ) { GameScene.show(new WndTitledMessage(new Image(icon),
if (inside( x, y ) && seen) { Messages.titleCase(item.trueName()), item.info()));
if (item instanceof ClassArmor){ }
GameScene.show(new WndTitledMessage(new Image(icon), return true;
Messages.titleCase(item.trueName()), item.desc())); } else {
} else { return false;
GameScene.show(new WndTitledMessage(new Image(icon), }
Messages.titleCase(item.trueName()), item.info()));
} }
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);
} }
} }