From 96fdb8e9e0d139cf55ccac14a2702db0d3462bdb Mon Sep 17 00:00:00 2001 From: Evan Debenham Date: Tue, 25 Jan 2022 13:05:09 -0500 Subject: [PATCH] v1.2.0: Moved ItemButton to its own external class --- .../ui/InventorySlot.java | 116 +++++++++++++ .../shatteredpixeldungeon/ui/ItemSlot.java | 2 +- .../shatteredpixeldungeon/windows/WndBag.java | 154 +++++------------- 3 files changed, 155 insertions(+), 117 deletions(-) create mode 100644 core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ui/InventorySlot.java diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ui/InventorySlot.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ui/InventorySlot.java new file mode 100644 index 000000000..1f80c76fc --- /dev/null +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ui/InventorySlot.java @@ -0,0 +1,116 @@ +/* + * 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.Assets; +import com.shatteredpixel.shatteredpixeldungeon.Dungeon; +import com.shatteredpixel.shatteredpixeldungeon.actors.buffs.LostInventory; +import com.shatteredpixel.shatteredpixeldungeon.items.EquipableItem; +import com.shatteredpixel.shatteredpixeldungeon.items.Gold; +import com.shatteredpixel.shatteredpixeldungeon.items.Item; +import com.shatteredpixel.shatteredpixeldungeon.items.bags.Bag; +import com.shatteredpixel.shatteredpixeldungeon.items.wands.Wand; +import com.shatteredpixel.shatteredpixeldungeon.windows.WndUseItem; +import com.watabou.gltextures.TextureCache; +import com.watabou.noosa.ColorBlock; +import com.watabou.noosa.Game; +import com.watabou.noosa.audio.Sample; + +public class InventorySlot extends ItemSlot { + + private static final int NORMAL = 0x9953564D; + private static final int EQUIPPED = 0x9991938C; + + private ColorBlock bg; + + public InventorySlot( Item item ) { + + super( 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 ); + + bg.visible = !(item instanceof Gold || item instanceof Bag); + + if (item != null) { + + bg.texture( TextureCache.createSolid( item.isEquipped( Dungeon.hero ) ? EQUIPPED : NORMAL ) ); + bg.resetColor(); + 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; + } + } + + if (item.name() == null) { + enable( false ); + } else if (Dungeon.hero.buff(LostInventory.class) != null + && !item.keptThoughLostInvent){ + enable(false); + } + } else { + bg.texture( TextureCache.createSolid( NORMAL ) ); + bg.resetColor(); + } + } + + public Item item(){ + return item; + } + + @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 ); + } + +} diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ui/ItemSlot.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ui/ItemSlot.java index 31dd57435..1220f7a23 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ui/ItemSlot.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ui/ItemSlot.java @@ -134,7 +134,7 @@ public class ItemSlot extends Button { if ((status.width() + extra.width()) > width){ extra.visible = false; - } else { + } else if (item != null) { extra.visible = 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 8e3133ac7..20c129c35 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/windows/WndBag.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/windows/WndBag.java @@ -41,6 +41,7 @@ import com.shatteredpixel.shatteredpixeldungeon.scenes.PixelScene; import com.shatteredpixel.shatteredpixeldungeon.sprites.ItemSprite; import com.shatteredpixel.shatteredpixeldungeon.sprites.ItemSpriteSheet; import com.shatteredpixel.shatteredpixeldungeon.ui.Icons; +import com.shatteredpixel.shatteredpixeldungeon.ui.InventorySlot; import com.shatteredpixel.shatteredpixeldungeon.ui.ItemSlot; import com.shatteredpixel.shatteredpixeldungeon.ui.QuickSlotButton; import com.shatteredpixel.shatteredpixeldungeon.ui.RenderedTextBlock; @@ -270,8 +271,43 @@ public class WndBag extends WndTabbed { int x = col * (slotWidth + SLOT_MARGIN); int y = TITLE_HEIGHT + row * (slotHeight + SLOT_MARGIN); - - add( new ItemButton( item ).setPos( x, y ) ); + + InventorySlot slot = new InventorySlot( item ){ + @Override + protected void onClick() { + if (lastBag != item && !lastBag.contains(item) && !item.isEquipped(Dungeon.hero)){ + + hide(); + + } else if (selector != null) { + + hide(); + selector.onSelect( item ); + + } else { + + Game.scene().addToFront(new WndUseItem( WndBag.this, item ) ); + + } + } + + @Override + protected boolean onLongClick() { + if (selector == null && item.defaultAction != null) { + hide(); + Dungeon.quickslot.setSlot( 0 , item ); + QuickSlotButton.refresh(); + return true; + } else if (selector != null) { + Game.scene().addToFront(new WndInfoItem(item)); + return true; + } else { + return false; + } + } + }; + slot.setRect( x, y, slotWidth, slotHeight ); + add(slot); if (++col >= nCols) { col = 0; @@ -364,120 +400,6 @@ public class WndBag extends WndTabbed { return true; } } - - private class ItemButton extends ItemSlot { - - private static final int NORMAL = 0x9953564D; - private static final int EQUIPPED = 0x9991938C; - - private Item item; - private ColorBlock bg; - - public ItemButton( Item item ) { - - super( item ); - - this.item = item; - if (item instanceof Gold || item instanceof Bag) { - bg.visible = false; - } - - width = slotWidth; - height = slotHeight; - } - - @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; - } - } - - if (item.name() == null) { - enable( false ); - } else if (selector != null && !selector.itemSelectable(item)) { - enable(false); - } else if (Dungeon.hero.buff(LostInventory.class) != null - && !item.keptThoughLostInvent){ - enable(false); - } - } 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() { - if (lastBag != item && !lastBag.contains(item) && !item.isEquipped(Dungeon.hero)){ - - hide(); - - } else if (selector != null) { - - hide(); - selector.onSelect( item ); - - } else { - - Game.scene().addToFront(new WndUseItem( WndBag.this, item ) ); - - } - } - - @Override - protected boolean onLongClick() { - if (selector == null && item.defaultAction != null) { - hide(); - Dungeon.quickslot.setSlot( 0 , item ); - QuickSlotButton.refresh(); - return true; - } else if (selector != null) { - Game.scene().addToFront(new WndInfoItem(item)); - return true; - } else { - return false; - } - } - } public abstract static class ItemSelector { public abstract String textPrompt();