Placement toggle support

This commit is contained in:
Anuken
2018-11-19 12:27:42 -05:00
parent ba01ed0153
commit c75cdc6d28

View File

@@ -16,6 +16,7 @@ import io.anuke.mindustry.world.Tile;
import io.anuke.mindustry.world.blocks.OreBlock;
import io.anuke.ucore.core.Graphics;
import io.anuke.ucore.scene.Group;
import io.anuke.ucore.scene.actions.Actions;
import io.anuke.ucore.scene.event.Touchable;
import io.anuke.ucore.scene.ui.ButtonGroup;
import io.anuke.ucore.scene.ui.Image;
@@ -30,149 +31,151 @@ public class PlacementFragment extends Fragment{
Array<Recipe> returned = new Array<>();
Category currentCategory = Category.turret;
Block hovered;
Block lastDisplay;
Block hovered, lastDisplay;
Tile hoverTile;
Table blockTable;
Table blockTable, toggler;
boolean shown = true;
@Override
public void build(Group parent){
parent.fill(frame -> {
InputHandler input = control.input(0);
parent.fill(full -> {
toggler = full;
full.bottom().right().visible(() -> !state.is(State.menu));
//rebuilds the category table with the correct recipes
Runnable rebuildCategory = () -> {
blockTable.clear();
blockTable.top().margin(5);
full.table(frame -> {
InputHandler input = control.input(0);
//rebuilds the category table with the correct recipes
Runnable rebuildCategory = () -> {
blockTable.clear();
blockTable.top().margin(5);
int index = 0;
int index = 0;
ButtonGroup<ImageButton> group = new ButtonGroup<>();
group.setMinCheckCount(0);
ButtonGroup<ImageButton> group = new ButtonGroup<>();
group.setMinCheckCount(0);
for(Recipe recipe : recipes(currentCategory)){
for(Recipe recipe : recipes(currentCategory)){
if(index++ % rowWidth == 0){
blockTable.row();
}
boolean[] unlocked = {false};
ImageButton button = blockTable.addImageButton("icon-locked", "select", 8*4, () -> {
if(control.unlocks.isUnlocked(recipe)){
input.recipe = input.recipe == recipe ? null : recipe;
if(index++ % rowWidth == 0){
blockTable.row();
}
}).size(46f).group(group).get();
button.update(() -> { //color unplacable things gray
boolean ulock = control.unlocks.isUnlocked(recipe);
TileEntity core = players[0].getClosestCore();
Color color = core != null && core.items.has(recipe.requirements) ? Color.WHITE : ulock ? Color.GRAY : Color.WHITE;
button.forEach(elem -> elem.setColor(color));
button.setChecked(input.recipe == recipe);
boolean[] unlocked = {false};
if(ulock == unlocked[0]) return;
unlocked[0] = ulock;
ImageButton button = blockTable.addImageButton("icon-locked", "select", 8*4, () -> {
if(control.unlocks.isUnlocked(recipe)){
input.recipe = input.recipe == recipe ? null : recipe;
}
}).size(46f).group(group).get();
if(!ulock){
button.replaceImage(new Image("icon-locked"));
}else{
button.replaceImage(new ImageStack(recipe.result.getCompactIcon()));
}
});
button.update(() -> { //color unplacable things gray
boolean ulock = control.unlocks.isUnlocked(recipe);
TileEntity core = players[0].getClosestCore();
Color color = core != null && core.items.has(recipe.requirements) ? Color.WHITE : ulock ? Color.GRAY : Color.WHITE;
button.forEach(elem -> elem.setColor(color));
button.setChecked(input.recipe == recipe);
if(!mobile){
button.hovered(() -> hovered = recipe.result);
button.exited(() -> {
if(hovered == recipe.result){
hovered = null;
if(ulock == unlocked[0]) return;
unlocked[0] = ulock;
if(!ulock){
button.replaceImage(new Image("icon-locked"));
}else{
button.replaceImage(new ImageStack(recipe.result.getCompactIcon()));
}
});
if(!mobile){
button.hovered(() -> hovered = recipe.result);
button.exited(() -> {
if(hovered == recipe.result){
hovered = null;
}
});
}
}
}
blockTable.act(0f);
};
blockTable.act(0f);
};
frame.bottom().right().visible(() -> !state.is(State.menu));
frame.table("clear", top -> {
top.add(new Table()).growX().update(topTable -> {
if((tileDisplayBlock() == null && lastDisplay == getSelected()) ||
(tileDisplayBlock() != null && lastDisplay == tileDisplayBlock())) return;
frame.table("clear", top -> {
top.add(new Table()).growX().update(topTable -> {
if((tileDisplayBlock() == null && lastDisplay == getSelected()) ||
(tileDisplayBlock() != null && lastDisplay == tileDisplayBlock())) return;
topTable.clear();
topTable.top().left().margin(5);
topTable.clear();
topTable.top().left().margin(5);
lastDisplay = getSelected();
lastDisplay = getSelected();
if(lastDisplay != null){ //show selected recipe
topTable.table(header -> {
header.left();
header.add(new ImageStack(lastDisplay.getCompactIcon())).size(8*4);
header.labelWrap(() ->
if(lastDisplay != null){ //show selected recipe
topTable.table(header -> {
header.left();
header.add(new ImageStack(lastDisplay.getCompactIcon())).size(8*4);
header.labelWrap(() ->
!control.unlocks.isUnlocked(Recipe.getByResult(lastDisplay)) ? Bundles.get("text.blocks.unknown") : lastDisplay.formalName)
.left().width(190f).padLeft(5);
header.add().growX();
if(control.unlocks.isUnlocked(Recipe.getByResult(lastDisplay))){
header.addButton("?", "clear-partial", () -> ui.content.show(Recipe.getByResult(lastDisplay)))
.size(8 * 5).padTop(-5).padRight(-5).right().grow();
}
}).growX().left();
topTable.row();
//add requirement table
topTable.table(req -> {
req.top().left();
header.add().growX();
if(control.unlocks.isUnlocked(Recipe.getByResult(lastDisplay))){
header.addButton("?", "clear-partial", () -> ui.content.show(Recipe.getByResult(lastDisplay)))
.size(8 * 5).padTop(-5).padRight(-5).right().grow();
}
}).growX().left();
topTable.row();
//add requirement table
topTable.table(req -> {
req.top().left();
for(ItemStack stack : Recipe.getByResult(lastDisplay).requirements){
req.table(line -> {
line.left();
line.addImage(stack.item.region).size(8*2);
line.add(stack.item.localizedName()).color(Color.LIGHT_GRAY).padLeft(2).left();
line.labelWrap(() -> {
TileEntity core = players[0].getClosestCore();
if(core == null || state.mode.infiniteResources) return "*/*";
for(ItemStack stack : Recipe.getByResult(lastDisplay).requirements){
req.table(line -> {
line.left();
line.addImage(stack.item.region).size(8*2);
line.add(stack.item.localizedName()).color(Color.LIGHT_GRAY).padLeft(2).left();
line.labelWrap(() -> {
TileEntity core = players[0].getClosestCore();
if(core == null || state.mode.infiniteResources) return "*/*";
int amount = core.items.get(stack.item);
String color = (amount < stack.amount / 2f ? "[red]" : amount < stack.amount ? "[accent]" : "[white]");
int amount = core.items.get(stack.item);
String color = (amount < stack.amount / 2f ? "[red]" : amount < stack.amount ? "[accent]" : "[white]");
return color + ui.formatAmount(amount) + "[white]/" + stack.amount;
}).padLeft(5);
}).left();
req.row();
}
}).growX().left().margin(3);
return color + ui.formatAmount(amount) + "[white]/" + stack.amount;
}).padLeft(5);
}).left();
req.row();
}
}).growX().left().margin(3);
}else if(tileDisplayBlock() != null){ //show selected tile
lastDisplay = tileDisplayBlock();
topTable.add(new ImageStack(lastDisplay.getDisplayIcon(hoverTile))).size(8*4);
topTable.labelWrap(lastDisplay.getDisplayName(hoverTile)).left().width(190f).padLeft(5);
}else if(tileDisplayBlock() != null){ //show selected tile
lastDisplay = tileDisplayBlock();
topTable.add(new ImageStack(lastDisplay.getDisplayIcon(hoverTile))).size(8*4);
topTable.labelWrap(lastDisplay.getDisplayName(hoverTile)).left().width(190f).padLeft(5);
}
});
top.row();
top.addImage("blank").growX().color(Palette.accent).height(3f);
}).colspan(3).fillX().visible(() -> getSelected() != null || tileDisplayBlock() != null).touchable(Touchable.enabled);
frame.row();
frame.table("clear", blocks -> blockTable = blocks).fillY().bottom().touchable(Touchable.enabled);
frame.addImage("blank").width(3f).fillY().color(Palette.accent);
frame.table(categories -> {
categories.defaults().size(48f);
ButtonGroup<ImageButton> group = new ButtonGroup<>();
for(Category cat : Category.values()){
if(recipes(cat).isEmpty()) continue;
categories.addImageButton("icon-" + cat.name(), "clear-toggle", 16*2, () -> {
currentCategory = cat;
rebuildCategory.run();
}).group(group);
if(cat.ordinal() %2 == 1) categories.row();
}
});
top.row();
top.addImage("blank").growX().color(Palette.accent).height(3f);
}).colspan(3).fillX().visible(() -> getSelected() != null || tileDisplayBlock() != null).touchable(Touchable.enabled);
frame.row();
frame.table("clear", blocks -> blockTable = blocks).fillY().bottom().touchable(Touchable.enabled);
frame.addImage("blank").width(3f).fillY().color(Palette.accent);
frame.table(categories -> {
categories.defaults().size(48f);
}).touchable(Touchable.enabled);
ButtonGroup<ImageButton> group = new ButtonGroup<>();
for(Category cat : Category.values()){
if(recipes(cat).isEmpty()) continue;
categories.addImageButton("icon-" + cat.name(), "clear-toggle", 16*2, () -> {
currentCategory = cat;
rebuildCategory.run();
}).group(group);
if(cat.ordinal() %2 == 1) categories.row();
}
}).touchable(Touchable.enabled);
rebuildCategory.run();
rebuildCategory.run();
});
});
}
@@ -220,6 +223,12 @@ public class PlacementFragment extends Fragment{
}
void toggle(float t, Interpolation ip){
if(shown){
shown = false;
toggler.actions(Actions.translateBy(toggler.getTranslation().x + toggler.getWidth(), 0, t, ip));
}else{
shown = true;
toggler.actions(Actions.translateBy(-toggler.getTranslation().x, 0, t, ip));
}
}
}