Magic_Ling_Pixel_Dungeon/src/com/shatteredpixel/shatteredpixeldungeon/ui/Toolbar.java

430 lines
10 KiB
Java
Raw Normal View History

2014-07-27 13:39:07 +00:00
/*
* Pixel Dungeon
2015-06-12 20:44:04 +00:00
* Copyright (C) 2012-2015 Oleg Dolya
*
* Shattered Pixel Dungeon
* Copyright (C) 2014-2015 Evan Debenham
2014-07-27 13:39:07 +00:00
*
* 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;
2014-07-27 13:39:07 +00:00
import com.watabou.noosa.Game;
import com.watabou.noosa.Gizmo;
import com.watabou.noosa.Image;
import com.watabou.noosa.ui.Button;
import com.watabou.noosa.ui.Component;
import com.shatteredpixel.shatteredpixeldungeon.ShatteredPixelDungeon;
import com.shatteredpixel.shatteredpixeldungeon.Assets;
import com.shatteredpixel.shatteredpixeldungeon.Dungeon;
import com.shatteredpixel.shatteredpixeldungeon.DungeonTilemap;
import com.shatteredpixel.shatteredpixeldungeon.items.Item;
import com.shatteredpixel.shatteredpixeldungeon.scenes.CellSelector;
import com.shatteredpixel.shatteredpixeldungeon.scenes.GameScene;
import com.shatteredpixel.shatteredpixeldungeon.sprites.ItemSprite;
import com.shatteredpixel.shatteredpixeldungeon.windows.WndBag;
import com.shatteredpixel.shatteredpixeldungeon.windows.WndCatalogus;
2014-07-27 13:39:07 +00:00
public class Toolbar extends Component {
private Tool btnWait;
private Tool btnSearch;
private Tool btnInventory;
private QuickslotTool[] btnQuick;
2014-07-27 13:39:07 +00:00
private PickedUpItem pickedUp;
private boolean lastEnabled = true;
2015-07-15 03:07:51 +00:00
private boolean examining = false;
private static Toolbar instance;
public enum Mode {
SPLIT,
GROUP,
CENTER
}
2014-07-27 13:39:07 +00:00
public Toolbar() {
super();
2015-07-15 03:07:51 +00:00
instance = this;
2014-07-27 13:39:07 +00:00
height = btnInventory.height();
}
@Override
protected void createChildren() {
add(btnWait = new Tool(24, 0, 20, 26) {
2014-07-27 13:39:07 +00:00
@Override
protected void onClick() {
2015-07-15 03:07:51 +00:00
examining = false;
Dungeon.hero.rest(false);
}
2014-07-27 13:39:07 +00:00
protected boolean onLongClick() {
2015-07-15 03:07:51 +00:00
examining = false;
Dungeon.hero.rest(true);
2014-07-27 13:39:07 +00:00
return true;
}
;
});
2014-07-27 13:39:07 +00:00
add(btnSearch = new Tool(44, 0, 20, 26) {
2014-07-27 13:39:07 +00:00
@Override
protected void onClick() {
if (!examining) {
GameScene.selectCell(informer);
examining = true;
} else {
informer.onSelect(null);
Dungeon.hero.search(true);
}
2014-07-27 13:39:07 +00:00
}
2015-07-15 04:39:20 +00:00
@Override
protected boolean onLongClick() {
Dungeon.hero.search(true);
return true;
}
});
btnQuick = new QuickslotTool[4];
add( btnQuick[3] = new QuickslotTool( 64, 0, 22, 24, 3) );
add( btnQuick[2] = new QuickslotTool( 64, 0, 22, 24, 2) );
add(btnQuick[1] = new QuickslotTool(64, 0, 22, 24, 1));
add(btnQuick[0] = new QuickslotTool(64, 0, 22, 24, 0));
2014-07-27 13:39:07 +00:00
add(btnInventory = new Tool(0, 0, 24, 26) {
2014-07-27 13:39:07 +00:00
private GoldIndicator gold;
2014-07-27 13:39:07 +00:00
@Override
protected void onClick() {
GameScene.show(new WndBag(Dungeon.hero.belongings.backpack, null, WndBag.Mode.ALL, null));
2014-07-27 13:39:07 +00:00
}
2014-07-27 13:39:07 +00:00
protected boolean onLongClick() {
GameScene.show(new WndCatalogus());
2014-07-27 13:39:07 +00:00
return true;
}
;
2014-07-27 13:39:07 +00:00
@Override
protected void createChildren() {
super.createChildren();
gold = new GoldIndicator();
add(gold);
}
;
2014-07-27 13:39:07 +00:00
@Override
protected void layout() {
super.layout();
gold.fill(this);
}
;
});
add(pickedUp = new PickedUpItem());
2014-07-27 13:39:07 +00:00
}
@Override
protected void layout() {
int[] visible = new int[4];
2015-07-15 03:07:51 +00:00
int slots = ShatteredPixelDungeon.quickSlots();
for(int i = 0; i <= 3; i++)
visible[i] = (int)((slots > i) ? y+2 : y+25);
2015-07-15 03:07:51 +00:00
for(int i = 0; i <= 3; i++) {
btnQuick[i].visible = btnQuick[i].active = slots > i;
//decides on quickslot layout, depending on available screen size.
if (slots == 4 && width < 150){
if (width < 139){
if ((ShatteredPixelDungeon.flipToolbar() && i == 3) ||
(!ShatteredPixelDungeon.flipToolbar() && i == 0)) {
btnQuick[i].border(0, 0);
btnQuick[i].frame(88, 0, 17, 24);
} else {
btnQuick[i].border(0, 1);
btnQuick[i].frame(88, 0, 18, 24);
}
} else {
if (i == 0 && !ShatteredPixelDungeon.flipToolbar() ||
i == 3 && ShatteredPixelDungeon.flipToolbar()){
btnQuick[i].border(0, 2);
btnQuick[i].frame(106, 0, 19, 24);
} else if (i == 0 && ShatteredPixelDungeon.flipToolbar() ||
i == 3 && !ShatteredPixelDungeon.flipToolbar()){
btnQuick[i].border(2, 1);
btnQuick[i].frame(86, 0, 20, 24);
} else {
btnQuick[i].border(0, 1);
btnQuick[i].frame(88, 0, 18, 24);
}
}
} else {
btnQuick[i].border(2, 2);
btnQuick[i].frame(64, 0, 22, 24);
}
2015-07-15 03:07:51 +00:00
}
2015-07-15 03:07:51 +00:00
float right = width;
switch(Mode.valueOf(ShatteredPixelDungeon.toolbarMode())){
case SPLIT:
btnWait.setPos(x, y);
btnSearch.setPos(btnWait.right(), y);
btnInventory.setPos(right - btnInventory.width(), y);
btnQuick[0].setPos(btnInventory.left() - btnQuick[0].width(), visible[0]);
btnQuick[1].setPos(btnQuick[0].left() - btnQuick[1].width(), visible[1]);
btnQuick[2].setPos(btnQuick[1].left() - btnQuick[2].width(), visible[2]);
btnQuick[3].setPos(btnQuick[2].left() - btnQuick[3].width(), visible[3]);
2015-07-15 03:07:51 +00:00
break;
//center = group but.. well.. centered, so all we need to do is pre-emptively set the right side further in.
case CENTER:
right = width - (width - btnWait.width() - btnSearch.width() - btnInventory.width() -
btnQuick[0].width() - btnQuick[1].width() - btnQuick[2].width() - btnQuick[3].width())/2;
2015-07-15 03:07:51 +00:00
case GROUP:
btnWait.setPos(right - btnWait.width(), y);
btnSearch.setPos(btnWait.left() - btnSearch.width(), y);
btnInventory.setPos(btnSearch.left() - btnInventory.width(), y);
btnQuick[0].setPos(btnInventory.left() - btnQuick[0].width(), visible[0]);
btnQuick[1].setPos(btnQuick[0].left() - btnQuick[1].width(), visible[1]);
btnQuick[2].setPos(btnQuick[1].left() - btnQuick[2].width(), visible[2]);
btnQuick[3].setPos(btnQuick[2].left() - btnQuick[3].width(), visible[3]);
2015-07-15 03:07:51 +00:00
break;
}
right = width;
if (ShatteredPixelDungeon.flipToolbar()) {
2015-07-15 03:07:51 +00:00
btnWait.setPos( (right - btnWait.right()), y);
btnSearch.setPos( (right - btnSearch.right()), y);
btnInventory.setPos( (right - btnInventory.right()), y);
2015-07-15 03:07:51 +00:00
for(int i = 0; i <= 3; i++) {
btnQuick[i].setPos( right - btnQuick[i].right(), visible[i]);
}
}
2015-07-15 03:07:51 +00:00
}
2015-07-15 03:07:51 +00:00
public static void updateLayout(){
if (instance != null) instance.layout();
2014-07-27 13:39:07 +00:00
}
@Override
public void update() {
super.update();
if (lastEnabled != Dungeon.hero.ready) {
lastEnabled = Dungeon.hero.ready;
for (Gizmo tool : members) {
if (tool instanceof Tool) {
((Tool)tool).enable( lastEnabled );
}
}
}
if (!Dungeon.hero.isAlive()) {
btnInventory.enable(true);
2014-07-27 13:39:07 +00:00
}
}
public void pickup( Item item ) {
pickedUp.reset( item,
btnInventory.centerX(),
2014-07-27 13:39:07 +00:00
btnInventory.centerY() );
}
private static CellSelector.Listener informer = new CellSelector.Listener() {
@Override
public void onSelect( Integer cell ) {
2015-07-15 03:07:51 +00:00
instance.examining = false;
GameScene.examineCell( cell );
}
2014-07-27 13:39:07 +00:00
@Override
public String prompt() {
return "Press again to search\nPress a cell for info";
2014-07-27 13:39:07 +00:00
}
};
private static class Tool extends Button {
private static final int BGCOLOR = 0x7B8073;
private Image base;
public Tool( int x, int y, int width, int height ) {
super();
frame(x, y, width, height);
}
public void frame( int x, int y, int width, int height) {
2014-07-27 13:39:07 +00:00
base.frame( x, y, width, height );
2014-07-27 13:39:07 +00:00
this.width = width;
this.height = height;
}
@Override
protected void createChildren() {
super.createChildren();
base = new Image( Assets.TOOLBAR );
add( base );
}
@Override
protected void layout() {
super.layout();
base.x = x;
base.y = y;
}
@Override
protected void onTouchDown() {
base.brightness( 1.4f );
}
@Override
protected void onTouchUp() {
if (active) {
base.resetColor();
} else {
base.tint( BGCOLOR, 0.7f );
}
}
public void enable( boolean value ) {
if (value != active) {
if (value) {
base.resetColor();
} else {
base.tint( BGCOLOR, 0.7f );
}
active = value;
}
}
}
private static class QuickslotTool extends Tool {
private QuickSlotButton slot;
private int borderLeft = 2;
private int borderRight = 2;
2014-07-27 13:39:07 +00:00
public QuickslotTool( int x, int y, int width, int height, int slotNum ) {
2014-07-27 13:39:07 +00:00
super( x, y, width, height );
slot = new QuickSlotButton( slotNum );
2014-07-27 13:39:07 +00:00
add( slot );
}
public void border( int left, int right ){
borderLeft = left;
borderRight = right;
layout();
}
2014-07-27 13:39:07 +00:00
@Override
protected void layout() {
super.layout();
slot.setRect( x + borderLeft, y + 2, width - borderLeft-borderRight, height - 4 );
2014-07-27 13:39:07 +00:00
}
@Override
public void enable( boolean value ) {
super.enable( value );
2014-07-27 13:39:07 +00:00
slot.enable( value );
}
}
private static class PickedUpItem extends ItemSprite {
private static final float DISTANCE = DungeonTilemap.SIZE;
private static final float DURATION = 0.2f;
private float dstX;
private float dstY;
private float left;
public PickedUpItem() {
super();
originToCenter();
active =
visible =
2014-07-27 13:39:07 +00:00
false;
}
public void reset( Item item, float dstX, float dstY ) {
view( item );
2014-07-27 13:39:07 +00:00
active =
visible =
2014-07-27 13:39:07 +00:00
true;
this.dstX = dstX - ItemSprite.SIZE / 2;
this.dstY = dstY - ItemSprite.SIZE / 2;
left = DURATION;
x = this.dstX - DISTANCE;
y = this.dstY - DISTANCE;
alpha( 1 );
}
@Override
public void update() {
super.update();
if ((left -= Game.elapsed) <= 0) {
visible =
active =
2014-07-27 13:39:07 +00:00
false;
if (emitter != null) emitter.on = false;
2014-07-27 13:39:07 +00:00
} else {
float p = left / DURATION;
2014-07-27 13:39:07 +00:00
scale.set( (float)Math.sqrt( p ) );
float offset = DISTANCE * p;
x = dstX - offset;
y = dstY - offset;
}
}
}
}