v1.2.0: Moved ItemButton to its own external class

This commit is contained in:
Evan Debenham 2022-01-25 13:05:09 -05:00
parent 6bc0a50409
commit 96fdb8e9e0
3 changed files with 155 additions and 117 deletions

View File

@ -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 <http://www.gnu.org/licenses/>
*/
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 );
}
}

View File

@ -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;
}
}

View File

@ -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();