v1.2.0: added listener functionality to inventory pane

This commit is contained in:
Evan Debenham 2022-02-02 21:48:36 -05:00
parent c80f89133a
commit 58eb2242f4
4 changed files with 117 additions and 50 deletions

View File

@ -104,6 +104,7 @@ public class Shopkeeper extends NPC {
}
public static WndBag sell() {
//FIXME currently doesn't allow for repeated sales on new inventory pane
return GameScene.selectItem( itemSelector );
}

View File

@ -89,6 +89,7 @@ public class Alchemize extends Spell {
@Override
public void onSelect( Item item ) {
if (item != null) {
//FIXME currently doesn't allow for repeated alchemizes on new inventory pane
WndBag parentWnd = GameScene.selectItem( itemSelector );
GameScene.show( new WndAlchemizeItem( item, parentWnd ) );
}
@ -214,8 +215,10 @@ public class Alchemize extends Spell {
Sample.INSTANCE.play(Assets.Sounds.TELEPORT);
if (curItem.quantity() <= 1){
curItem.detachAll(Dungeon.hero.belongings.backpack);
if (owner != null) {
owner.hide();
owner = null;
}
} else {
curItem.detach(Dungeon.hero.belongings.backpack);
}

View File

@ -370,21 +370,6 @@ public class GameScene extends PixelScene {
boss.setPos( 6 + (uiCamera.width - boss.width())/2, 20);
add(boss);
toolbar = new Toolbar();
toolbar.camera = uiCamera;
if (uiSize == 2) {
inventory = new InventoryPane();
inventory.camera = uiCamera;
inventory.setPos(uiCamera.width - inventory.width(), uiCamera.height - inventory.height());
add(inventory);
toolbar.setRect( 0, uiCamera.height - toolbar.height() - inventory.height(), uiCamera.width, toolbar.height() );
} else {
toolbar.setRect( 0, uiCamera.height - toolbar.height(), uiCamera.width, toolbar.height() );
}
add( toolbar );
attack = new AttackIndicator();
attack.camera = uiCamera;
add( attack );
@ -406,6 +391,21 @@ public class GameScene extends PixelScene {
log.newLine();
add( log );
toolbar = new Toolbar();
toolbar.camera = uiCamera;
add( toolbar );
if (uiSize == 2) {
inventory = new InventoryPane();
inventory.camera = uiCamera;
inventory.setPos(uiCamera.width - inventory.width(), uiCamera.height - inventory.height());
add(inventory);
toolbar.setRect( 0, uiCamera.height - toolbar.height() - inventory.height(), uiCamera.width, toolbar.height() );
} else {
toolbar.setRect( 0, uiCamera.height - toolbar.height(), uiCamera.width, toolbar.height() );
}
layoutTags();
switch (InterlevelScene.mode) {
@ -736,7 +736,7 @@ public class GameScene extends PixelScene {
toDestroy.clear();
}
private Point lastOffset = null;
public static Point lastOffset = null;
@Override
public synchronized Gizmo erase (Gizmo g) {
@ -1196,12 +1196,19 @@ public class GameScene extends PixelScene {
public static WndBag selectItem( WndBag.ItemSelector listener ) {
cancelCellSelector();
if (scene != null) {
if (scene.inventory != null && scene.inventory.visible){
scene.inventory.setSelector(listener);
return null;
} else {
WndBag wnd = WndBag.getBag( listener );
if (scene != null) scene.addToFront( wnd );
show(wnd);
return wnd;
}
}
return null;
}
public static boolean cancel() {
if (Dungeon.hero != null && (Dungeon.hero.curAction != null || Dungeon.hero.resting)) {

View File

@ -30,11 +30,13 @@ import com.shatteredpixel.shatteredpixeldungeon.items.bags.MagicalHolster;
import com.shatteredpixel.shatteredpixeldungeon.items.bags.PotionBandolier;
import com.shatteredpixel.shatteredpixeldungeon.items.bags.ScrollHolder;
import com.shatteredpixel.shatteredpixeldungeon.items.bags.VelvetPouch;
import com.shatteredpixel.shatteredpixeldungeon.scenes.GameScene;
import com.shatteredpixel.shatteredpixeldungeon.scenes.PixelScene;
import com.shatteredpixel.shatteredpixeldungeon.sprites.ItemSpriteSheet;
import com.shatteredpixel.shatteredpixeldungeon.windows.WndBag;
import com.shatteredpixel.shatteredpixeldungeon.windows.WndUseItem;
import com.watabou.gltextures.TextureCache;
import com.watabou.input.PointerEvent;
import com.watabou.noosa.BitmapText;
import com.watabou.noosa.ColorBlock;
import com.watabou.noosa.Game;
@ -42,6 +44,7 @@ import com.watabou.noosa.Image;
import com.watabou.noosa.NinePatch;
import com.watabou.noosa.PointerArea;
import com.watabou.noosa.ui.Component;
import com.watabou.utils.Point;
import java.util.ArrayList;
@ -50,6 +53,9 @@ public class InventoryPane extends Component {
private NinePatch bg;
private NinePatch bg2; //2 backgrounds to reduce transparency
//used to prevent clicks through the BG normally, or to cancel selectors if they're enabled
private PointerArea blocker;
private static InventoryPane instance;
private ArrayList<InventorySlot> equipped;
@ -59,6 +65,7 @@ public class InventoryPane extends Component {
private BitmapText goldTxt;
private Image energy;
private BitmapText energyTxt;
private RenderedTextBlock promptTxt;
private ArrayList<BagButton> bags;
@ -68,6 +75,8 @@ public class InventoryPane extends Component {
private static final int SLOT_WIDTH = 17;
private static final int SLOT_HEIGHT = 24;
private WndBag.ItemSelector selector;
public static Bag lastBag;
private boolean lastEnabled = true;
@ -92,22 +101,34 @@ public class InventoryPane extends Component {
bg2 = Chrome.get(Chrome.Type.TOAST_TR);
add(bg2);
//blocks touches from going through BG into game scene
add (new PointerArea(bg));
blocker = new PointerArea(0, 0, PixelScene.uiCamera.width, PixelScene.uiCamera.height){
@Override
protected void onClick(PointerEvent event) {
if (selector != null){
selector = null;
updateInventory();
}
}
};
blocker.target = bg; //targets bg when there is no selector, otherwise targets itself
add (blocker);
equipped = new ArrayList<>();
for (int i = 0; i < 5; i++){
InventorySlot btn = new InventorySlot(null){
@Override
protected void onClick() {
//any windows opened as a consequence of this button should be centered on the inventory
GameScene.lastOffset = new Point((int)InventoryPane.this.centerX() - camera.width/2,
(int)InventoryPane.this.centerY() - camera.height/2);
if (lastBag != item && !lastBag.contains(item) && !item.isEquipped(Dungeon.hero)){
updateInventory();
} else if (selector != null) {
selector.onSelect( item );
selector = null;
updateInventory();
} else {
Window w = new WndUseItem( null, item );
w.offset( (int)InventoryPane.this.centerX() - camera.width/2,
(int)InventoryPane.this.centerY() - camera.height/2 );
w.boundOffsetWithMargin(3);
Game.scene().addToFront(w);
GameScene.show(new WndUseItem( null, item ));
}
}
};
@ -127,28 +148,26 @@ public class InventoryPane extends Component {
energyTxt.hardlight(0x44CCFF);
add(energyTxt);
promptTxt = PixelScene.renderTextBlock(6);
promptTxt.hardlight(Window.TITLE_COLOR);
add(promptTxt);
bagItems = new ArrayList<>();
for (int i = 0; i < 20; i++){
InventorySlot btn = new InventorySlot(null){
@Override
protected void onClick() {
//any windows opened as a consequence of this button should be centered on the inventory
GameScene.lastOffset = new Point((int)InventoryPane.this.centerX() - camera.width/2,
(int)InventoryPane.this.centerY() - camera.height/2);
if (lastBag != item && !lastBag.contains(item) && !item.isEquipped(Dungeon.hero)){
updateInventory();
} else if (selector != null) {
selector.onSelect( item );
selector = null;
updateInventory();
} else {
Window w = new WndUseItem( null, item );
int xOfs, yOfs;
if (w.height > InventoryPane.this.height - 15){
yOfs = (camera.height - w.height)/2 - 9;
} else {
yOfs = (int)(InventoryPane.this.y)/2;
}
if (w.width > InventoryPane.this.width - 15){
xOfs = (camera.width - w.width)/2 - 9;
} else {
xOfs = (int)(InventoryPane.this.x)/2;
}
w.offset(xOfs, yOfs);
Game.scene().addToFront(w);
GameScene.show(new WndUseItem( null, item ));
}
}
};
@ -186,6 +205,13 @@ public class InventoryPane extends Component {
left = i.right()+1;
}
promptTxt.maxWidth((int) (width - (left - x) - bg.marginRight()));
if (promptTxt.height() > 10){
promptTxt.setPos(left, y + 2 + (12 - promptTxt.height()) / 2);
} else {
promptTxt.setPos(left, y + 4 + (10 - promptTxt.height()) / 2);
}
goldTxt.x = left;
goldTxt.y = y+5.5f;
PixelScene.align(goldTxt);
@ -224,6 +250,12 @@ public class InventoryPane extends Component {
}
public void updateInventory(){
if (selector == null){
blocker.target = bg;
} else {
blocker.target = blocker;
}
Belongings stuff = Dungeon.hero.belongings;
if (lastBag == null || !stuff.contains(lastBag)){
@ -256,12 +288,23 @@ public class InventoryPane extends Component {
}
}
if (selector == null) {
promptTxt.visible = false;
goldTxt.text(Integer.toString(Dungeon.gold));
goldTxt.measure();
goldTxt.visible = gold.visible = true;
energyTxt.text(Integer.toString(Dungeon.energy));
energyTxt.measure();
energyTxt.visible = energy.visible = Dungeon.energy > 0;
} else {
promptTxt.text(selector.textPrompt());
promptTxt.visible = true;
goldTxt.visible = gold.visible = false;
energyTxt.visible = energy.visible = false;
}
ArrayList<Bag> inventBags = stuff.getBags();
for (int i = 0; i < bags.size(); i++){
@ -273,10 +316,14 @@ public class InventoryPane extends Component {
}
for (InventorySlot b : equipped){
b.enable(lastEnabled && !(b.item() instanceof WndBag.Placeholder));
b.enable(lastEnabled
&& !(b.item() instanceof WndBag.Placeholder)
&& (selector == null || selector.itemSelectable(b.item())));
}
for (InventorySlot b : bagItems){
b.enable(lastEnabled && b.item() != null);
b.enable(lastEnabled
&& b.item() != null
&& (selector == null || selector.itemSelectable(b.item())));
}
for (BagButton b : bags){
b.enable(lastEnabled);
@ -290,6 +337,15 @@ public class InventoryPane extends Component {
layout();
}
public void setSelector(WndBag.ItemSelector selector){
this.selector = selector;
if (selector.preferredBag() != null) {
Bag preferred = Dungeon.hero.belongings.getItem(selector.preferredBag());
if (preferred != null) lastBag = preferred;
}
updateInventory();
}
@Override
public synchronized void update() {
super.update();