From 85e5e1a12e7d6f63f294610ba4a1ac3e73d2d0a3 Mon Sep 17 00:00:00 2001 From: Evan Debenham Date: Wed, 16 Jun 2021 23:05:20 -0400 Subject: [PATCH] v0.9.3b: added a quick-use inventory window --- .../shatteredpixeldungeon/items/bags/Bag.java | 4 +- .../shatteredpixeldungeon/ui/Toolbar.java | 5 +- .../shatteredpixeldungeon/windows/WndBag.java | 5 +- .../windows/WndQuickBag.java | 224 ++++++++++++++++++ .../windows/WndUseItem.java | 3 +- 5 files changed, 233 insertions(+), 8 deletions(-) create mode 100644 core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/windows/WndQuickBag.java diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/bags/Bag.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/bags/Bag.java index 56818db7f..844221344 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/bags/Bag.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/bags/Bag.java @@ -27,7 +27,7 @@ import com.shatteredpixel.shatteredpixeldungeon.actors.Char; import com.shatteredpixel.shatteredpixeldungeon.actors.hero.Hero; import com.shatteredpixel.shatteredpixeldungeon.items.Item; import com.shatteredpixel.shatteredpixeldungeon.scenes.GameScene; -import com.shatteredpixel.shatteredpixeldungeon.windows.WndBag; +import com.shatteredpixel.shatteredpixeldungeon.windows.WndQuickBag; import com.watabou.utils.Bundlable; import com.watabou.utils.Bundle; @@ -61,7 +61,7 @@ public class Bag extends Item implements Iterable { if (action.equals( AC_OPEN )) { - GameScene.show( new WndBag( this, null, WndBag.Mode.ALL, null ) ); + GameScene.show( new WndQuickBag( this ) ); } } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ui/Toolbar.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ui/Toolbar.java index 41d2c3529..1bf27ea14 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ui/Toolbar.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ui/Toolbar.java @@ -32,7 +32,7 @@ import com.shatteredpixel.shatteredpixeldungeon.scenes.GameScene; import com.shatteredpixel.shatteredpixeldungeon.sprites.ItemSprite; import com.shatteredpixel.shatteredpixeldungeon.tiles.DungeonTerrainTilemap; import com.shatteredpixel.shatteredpixeldungeon.windows.WndBag; -import com.shatteredpixel.shatteredpixeldungeon.windows.WndJournal; +import com.shatteredpixel.shatteredpixeldungeon.windows.WndQuickBag; import com.watabou.input.GameAction; import com.watabou.noosa.Camera; import com.watabou.noosa.Game; @@ -153,8 +153,7 @@ public class Toolbar extends Component { @Override protected boolean onLongClick() { - WndJournal.last_index = 3; //catalog page - GameScene.show(new WndJournal()); + GameScene.show(new WndQuickBag(null)); return true; } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/windows/WndBag.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/windows/WndBag.java index e9575326c..875c87707 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/windows/WndBag.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/windows/WndBag.java @@ -57,6 +57,7 @@ import com.shatteredpixel.shatteredpixeldungeon.ui.Icons; import com.shatteredpixel.shatteredpixeldungeon.ui.ItemSlot; import com.shatteredpixel.shatteredpixeldungeon.ui.QuickSlotButton; import com.shatteredpixel.shatteredpixeldungeon.ui.RenderedTextBlock; +import com.shatteredpixel.shatteredpixeldungeon.ui.Window; import com.watabou.gltextures.TextureCache; import com.watabou.input.KeyBindings; import com.watabou.input.KeyEvent; @@ -68,8 +69,8 @@ import com.watabou.noosa.audio.Sample; public class WndBag extends WndTabbed { - //only one wnditem can appear at a time - private static WndBag INSTANCE; + //only one bag window can appear at a time + public static Window INSTANCE; //FIXME this is getting cumbersome, there should be a better way to manage this public static enum Mode { diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/windows/WndQuickBag.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/windows/WndQuickBag.java new file mode 100644 index 000000000..ac11e3da6 --- /dev/null +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/windows/WndQuickBag.java @@ -0,0 +1,224 @@ +package com.shatteredpixel.shatteredpixeldungeon.windows; + +import com.shatteredpixel.shatteredpixeldungeon.Assets; +import com.shatteredpixel.shatteredpixeldungeon.Chrome; +import com.shatteredpixel.shatteredpixeldungeon.Dungeon; +import com.shatteredpixel.shatteredpixeldungeon.actors.hero.Talent; +import com.shatteredpixel.shatteredpixeldungeon.items.EquipableItem; +import com.shatteredpixel.shatteredpixeldungeon.items.Generator; +import com.shatteredpixel.shatteredpixeldungeon.items.Item; +import com.shatteredpixel.shatteredpixeldungeon.items.artifacts.Artifact; +import com.shatteredpixel.shatteredpixeldungeon.items.artifacts.CloakOfShadows; +import com.shatteredpixel.shatteredpixeldungeon.items.bags.Bag; +import com.shatteredpixel.shatteredpixeldungeon.items.wands.Wand; +import com.shatteredpixel.shatteredpixeldungeon.messages.Languages; +import com.shatteredpixel.shatteredpixeldungeon.messages.Messages; +import com.shatteredpixel.shatteredpixeldungeon.scenes.GameScene; +import com.shatteredpixel.shatteredpixeldungeon.scenes.PixelScene; +import com.shatteredpixel.shatteredpixeldungeon.ui.ItemSlot; +import com.shatteredpixel.shatteredpixeldungeon.ui.RenderedTextBlock; +import com.shatteredpixel.shatteredpixeldungeon.ui.Window; +import com.watabou.gltextures.TextureCache; +import com.watabou.noosa.ColorBlock; +import com.watabou.noosa.Game; +import com.watabou.noosa.audio.Sample; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; + +public class WndQuickBag extends Window { + + public WndQuickBag(Bag bag){ + super(0, 0, 0, Chrome.get(Chrome.Type.TOAST_TR)); + + if( WndBag.INSTANCE != null ){ + WndBag.INSTANCE.hide(); + } + WndBag.INSTANCE = this; + + float width = 0, height = 0; + int maxWidth = PixelScene.landscape() ? 240 : 135; + int left = 0; + int top = 10; + + ArrayList items = new ArrayList<>(); + + for (Item i : bag == null ? Dungeon.hero.belongings : bag){ + if (i.defaultAction == null){ + continue; + } + if (i instanceof Bag) { + continue; + } + if (i instanceof Artifact + && !i.isEquipped(Dungeon.hero) + && (!(i instanceof CloakOfShadows) || !Dungeon.hero.hasTalent(Talent.LIGHT_CLOAK))){ + continue; + } + items.add(i); + } + + Collections.sort(items, quickBagComparator); + + if (items.isEmpty()){ + hide(); + return; + } + + int btnWidth = 16; + int btnHeight = 20; + + //height of the toolbar and status pane, plus a little extra + int targetHeight = PixelScene.uiCamera.height - 100; + int rows = (int)Math.ceil(items.size() / (float)((maxWidth+1) / (btnWidth+1))); + int expectedHeight = rows * btnHeight + (rows-1); + while (expectedHeight > targetHeight && btnHeight > 16){ + btnHeight--; + expectedHeight -= rows; + } + + for (Item i : items){ + QuickItemButton btn = new QuickItemButton(i); + btn.setRect(left, top, btnWidth, btnHeight); + add(btn); + + if (width < btn.right()) width = btn.right(); + if (height < btn.bottom()) height = btn.bottom(); + + left += btnWidth+1; + + if (left + btnWidth > maxWidth){ + left = 0; + top += btnHeight+1; + } + } + + //TODO translate this! + RenderedTextBlock txtTitle; + if ( Messages.lang() == Languages.ENGLISH){ + txtTitle = PixelScene.renderTextBlock( "Quick-use an Item", 8 ); + } else { + txtTitle = PixelScene.renderTextBlock( Messages.titleCase(bag != null ? bag.name() : Dungeon.hero.belongings.backpack.name()), 8 ); + } + txtTitle.hardlight( TITLE_COLOR ); + if (txtTitle.width() > width) width = txtTitle.width(); + + txtTitle.setPos( + (width - txtTitle.width())/2f, + (10 - txtTitle.height()) / 2f - 1); + PixelScene.align(txtTitle); + add( txtTitle ); + + resize((int)width, (int)height); + + int bottom = GameScene.uiCamera.height; + + //offset to be above the toolbar + offset((int) (bottom/2 - 30 - height/2)); + + } + + public static final Comparator quickBagComparator = new Comparator() { + @Override + public int compare( Item lhs, Item rhs ) { + if (lhs.isEquipped(Dungeon.hero) && !rhs.isEquipped(Dungeon.hero)){ + return -1; + } else if (!lhs.isEquipped(Dungeon.hero) && rhs.isEquipped(Dungeon.hero)){ + return 1; + } else { + return Generator.Category.order(lhs) - Generator.Category.order(rhs); + } + } + }; + + @Override + public void hide() { + super.hide(); + if (WndBag.INSTANCE == this){ + WndBag.INSTANCE = null; + } + } + + private class QuickItemButton extends ItemSlot { + + private static final int NORMAL = 0x9953564D; + private static final int EQUIPPED = 0x9991938C; + private Item item; + private ColorBlock bg; + + public QuickItemButton(Item item) { + + super(item); + showExtraInfo(false); + + this.item = item; + + } + + @Override + protected void createChildren() { + bg = new ColorBlock(1, 1, NORMAL); + add(bg); + + super.createChildren(); + } + + @Override + protected void layout() { + bg.size(width, height); + bg.x = x; + bg.y = y; + + super.layout(); + } + + @Override + public void item(Item item) { + + super.item(item); + if (item != null) { + + bg.texture( TextureCache.createSolid( item.isEquipped( Dungeon.hero ) ? EQUIPPED : NORMAL ) ); + if (item.cursed && item.cursedKnown) { + bg.ra = +0.3f; + bg.ga = -0.15f; + } else if (!item.isIdentified()) { + if ((item instanceof EquipableItem || item instanceof Wand) && item.cursedKnown) { + bg.ba = 0.3f; + } else { + bg.ra = 0.3f; + bg.ba = 0.3f; + } + } + + } else { + bg.color(NORMAL); + } + } + + @Override + protected void onPointerDown() { + bg.brightness(1.5f); + Sample.INSTANCE.play(Assets.Sounds.CLICK, 0.7f, 0.7f, 1.2f); + } + + protected void onPointerUp() { + bg.brightness(1.0f); + } + + @Override + protected void onClick() { + hide(); + item.execute(Dungeon.hero); //TODO targeting? + } + + @Override + protected boolean onLongClick() { + Game.scene().addToFront(new WndUseItem(WndQuickBag.this, item)); + return true; + } + + } + +} 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 1570f0e1a..ee4a89358 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.RedButton; +import com.shatteredpixel.shatteredpixeldungeon.ui.Window; import java.util.ArrayList; @@ -36,7 +37,7 @@ public class WndUseItem extends WndInfoItem { private static final float GAP = 2; - public WndUseItem(final WndBag owner, final Item item ) { + public WndUseItem( final Window owner, final Item item ) { super(item);