use of io.anuke.ucore.function.Listenable in project Mindustry by Anuken.
the class WeaponFactory method buildTable.
@Override
public void buildTable(Tile tile, Table table) {
int i = 0;
Table content = new Table();
for (Upgrade upgrade : Upgrade.getAllUpgrades()) {
if (!(upgrade instanceof Weapon))
continue;
Weapon weapon = (Weapon) upgrade;
ItemStack[] requirements = UpgradeRecipes.get(weapon);
Table tiptable = new Table();
Listenable run = () -> {
tiptable.clearChildren();
String description = weapon.description;
tiptable.background("pane");
tiptable.add("[orange]" + weapon.localized(), 0.5f).left().padBottom(2f);
Table reqtable = new Table();
tiptable.row();
tiptable.add(reqtable).left();
if (!control.upgrades().hasWeapon(weapon)) {
for (ItemStack s : requirements) {
int amount = Math.min(state.inventory.getAmount(s.item), s.amount);
reqtable.addImage(s.item.region).padRight(3).size(8 * 2);
reqtable.add((amount >= s.amount ? "" : "[RED]") + amount + " / " + s.amount, 0.5f).left();
reqtable.row();
}
}
tiptable.row();
tiptable.add().size(4);
tiptable.row();
tiptable.add("[gray]" + description).left();
tiptable.row();
if (control.upgrades().hasWeapon(weapon)) {
tiptable.add("$text.purchased").padTop(4).left();
}
tiptable.margin(8f);
};
run.listen();
Tooltip<Table> tip = new Tooltip<>(tiptable, run);
tip.setInstant(true);
ImageButton button = content.addImageButton("white", 8 * 4, () -> {
if (Net.client()) {
NetEvents.handleUpgrade(weapon);
} else {
state.inventory.removeItems(requirements);
control.upgrades().addWeapon(weapon);
ui.hudfrag.updateWeapons();
run.listen();
Effects.sound("purchase");
}
}).size(49f, 54f).padBottom(-5).get();
button.setDisabled(() -> control.upgrades().hasWeapon(weapon) || !state.inventory.hasItems(requirements));
button.getStyle().imageUp = new TextureRegionDrawable(Draw.region(weapon.name));
button.addListener(tip);
if (++i % 3 == 0) {
content.row();
}
}
table.add(content).padTop(140f);
}
Aggregations