Massive input refactoring

This commit is contained in:
Anuken
2018-05-29 00:15:24 -04:00
parent 2bf7d06a91
commit 4655bd5502
19 changed files with 599 additions and 696 deletions

View File

@@ -1,11 +1,292 @@
package io.anuke.mindustry.ui.fragments;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Interpolation;
import com.badlogic.gdx.utils.Array;
import io.anuke.mindustry.core.GameState.State;
import io.anuke.mindustry.entities.Player;
import io.anuke.mindustry.entities.TileEntity;
import io.anuke.mindustry.input.InputHandler;
import io.anuke.mindustry.type.ItemStack;
import io.anuke.mindustry.type.Recipe;
import io.anuke.mindustry.type.Section;
import io.anuke.ucore.scene.Element;
import io.anuke.ucore.scene.Group;
import io.anuke.ucore.scene.actions.Actions;
import io.anuke.ucore.scene.builders.table;
import io.anuke.ucore.scene.event.ClickListener;
import io.anuke.ucore.scene.event.InputEvent;
import io.anuke.ucore.scene.event.Touchable;
import io.anuke.ucore.scene.ui.ButtonGroup;
import io.anuke.ucore.scene.ui.Image;
import io.anuke.ucore.scene.ui.ImageButton;
import io.anuke.ucore.scene.ui.Label;
import io.anuke.ucore.scene.ui.layout.Stack;
import io.anuke.ucore.scene.ui.layout.Table;
import io.anuke.ucore.util.Bundles;
import io.anuke.ucore.util.Mathf;
import static io.anuke.mindustry.Vars.*;
public class BlocksFragment implements Fragment{
private Table desctable, blocks;
private Stack stack = new Stack();
private boolean shown = true;
private Recipe hoveredDescriptionRecipe;
@Override
public void build(Group parent){
InputHandler input = control.input(0);
new table(){{
abottom();
aright();
visible(() -> !state.is(State.menu) && shown);
blocks = new table(){{
desctable = new Table("button");
desctable.setVisible(() -> hoveredDescriptionRecipe != null || input.recipe != null);
desctable.update(() -> {
// note: This is required because there is no direct connection between
// input.recipe and the description ui. If input.recipe gets set to null
// a proper cleanup of the ui elements is required.
boolean anyRecipeShown = input.recipe != null || hoveredDescriptionRecipe != null;
boolean descriptionTableClean = desctable.getChildren().size == 0;
boolean cleanupRequired = !anyRecipeShown && !descriptionTableClean;
if(cleanupRequired){
desctable.clear();
}
});
stack.add(desctable);
add(stack).fillX().uniformX();
row();
new table("pane") {{
touchable(Touchable.enabled);
int rows = 4;
int maxcol = 0;
float size = 48;
Stack stack = new Stack();
ButtonGroup<ImageButton> group = new ButtonGroup<>();
Array<Recipe> recipes = new Array<>();
for (Section sec : Section.values()) {
recipes.clear();
Recipe.getBySection(sec, recipes);
maxcol = Math.max((int) ((float) recipes.size / rows + 1), maxcol);
}
for (Section sec : Section.values()) {
int secrows = 4;
recipes.clear();
Recipe.getBySection(sec, recipes);
Table table = new Table();
ImageButton button = new ImageButton("icon-" + sec.name(), "toggle");
button.clicked(() -> {
if (!table.isVisible() && input.recipe != null) {
input.recipe = null;
}
});
button.setName("sectionbutton" + sec.name());
add(button).growX().height(54).padLeft(-1).padTop(sec.ordinal() <= secrows-1 ? -10 : -5);
button.getImageCell().size(40).padBottom(4).padTop(2);
group.add(button);
if (sec.ordinal() % secrows == secrows-1) {
row();
}
table.margin(4);
table.top().left();
int i = 0;
for (Recipe r : recipes) {
ImageButton image = new ImageButton(new TextureRegion(), "select");
TextureRegion[] regions = r.result.getCompactIcon();
Stack istack = new Stack();
for(TextureRegion region : regions){
istack.add(new Image(region));
}
image.getImageCell().setActor(istack).size(size);
image.addChild(istack);
image.getImage().remove();
image.addListener(new ClickListener(){
@Override
public void enter(InputEvent event, float x, float y, int pointer, Element fromActor) {
super.enter(event, x, y, pointer, fromActor);
if (hoveredDescriptionRecipe != r) {
hoveredDescriptionRecipe = r;
updateRecipe(r);
}
}
@Override
public void exit(InputEvent event, float x, float y, int pointer, Element toActor) {
super.exit(event, x, y, pointer, toActor);
hoveredDescriptionRecipe = null;
updateRecipe(input.recipe);
}
});
image.addListener(new ClickListener(){
@Override
public void clicked(InputEvent event, float x, float y){
// note: input.recipe only gets set here during a click.
// during a hover only the visual description will be updated.
InputHandler handler = mobile ? input : control.input(event.getPointer());
boolean nothingSelectedYet = handler.recipe == null;
boolean selectedSomethingElse = !nothingSelectedYet && handler.recipe != r;
boolean shouldMakeSelection = nothingSelectedYet || selectedSomethingElse;
if (shouldMakeSelection) {
handler.recipe = r;
hoveredDescriptionRecipe = r;
updateRecipe(r);
} else {
handler.recipe = null;
hoveredDescriptionRecipe = null;
updateRecipe(null);
}
}
});
table.add(image).size(size + 8);
image.update(() -> {
image.setTouchable(Touchable.enabled);
for(Element e : istack.getChildren()){
e.setColor(Color.WHITE);
}
for(Player player : players){
if(control.input(player.playerIndex).recipe == r){
image.setChecked(true);
return;
}
}
image.setChecked(false);
});
if (i % rows == rows - 1) {
table.row();
}
i++;
}
table.setVisible(button::isChecked);
stack.add(table);
}
row();
add(stack).colspan(Section.values().length);
margin(10f);
marginLeft(1f);
marginRight(1f);
end();
}}.right().bottom().uniformX();
visible(() -> !state.is(State.menu) && shown);
}}.end().get();
}}.end();
}
}
public void toggle(boolean show, float t, Interpolation ip){
if(!show){
blocks.actions(Actions.translateBy(0, -blocks.getHeight() - stack.getHeight(), t, ip), Actions.call(() -> shown = false));
}else{
shown = true;
blocks.actions(Actions.translateBy(0, -blocks.getTranslation().y, t, ip));
}
}
void updateRecipe(Recipe recipe){
if (recipe == null) {
desctable.clear();
return;
}
desctable.clear();
desctable.setTouchable(Touchable.enabled);
desctable.defaults().left();
desctable.left();
desctable.margin(12);
Table header = new Table();
desctable.add(header).left();
desctable.row();
TextureRegion[] regions = recipe.result.getCompactIcon();
Stack istack = new Stack();
for(TextureRegion region : regions) istack.add(new Image(region));
header.add(istack).size(8*5).padTop(4);
Label nameLabel = new Label(recipe.result.formalName);
nameLabel.setWrap(true);
header.add(nameLabel).padLeft(2).width(120f);
desctable.add().pad(2);
Table requirements = new Table();
desctable.row();
desctable.add(requirements);
desctable.left();
for(ItemStack stack : recipe.requirements){
requirements.addImage(stack.item.region).size(8*3);
Label reqlabel = new Label("");
reqlabel.update(() -> {
int current = stack.amount;
String text = Mathf.clamp(current, 0, stack.amount) + "/" + stack.amount;
reqlabel.setText(text);
});
requirements.add(reqlabel).left();
requirements.row();
}
desctable.row();
Label label = new Label("[health]"+ Bundles.get("text.health")+": " + recipe.result.health);
label.setWrap(true);
desctable.add(label).width(200).padTop(4).padBottom(2);
}
private void checkUnlockableBlocks(){
TileEntity entity = players[0].getClosestCore();
if(entity == null) return;
for(Recipe recipe : Recipe.all()){
if(entity.items.hasAtLeastOneOfItems(recipe.requirements)){
control.database().unlockContent(recipe);
}
}
}
}

View File

@@ -48,7 +48,7 @@ public class DebugFragment implements Fragment {
new table(){{
visible(() -> debug);
abottom().aleft();
atop().aright();
new table("pane"){{
defaults().fillX().width(100f);

View File

@@ -9,8 +9,11 @@ public class OverlayFragment implements Fragment{
public final BlockConfigFragment config;
private Group group = new Group();
private InputHandler input;
public OverlayFragment(InputHandler input){
this.input = input;
tool = new ToolFragment(input);
inv = new BlockInventoryFragment(input);
config = new BlockConfigFragment();
@@ -24,6 +27,8 @@ public class OverlayFragment implements Fragment{
inv.build(group);
tool.build(group);
config.build(group);
input.buildUI(group);
}
public void remove(){

View File

@@ -1,237 +1,12 @@
package io.anuke.mindustry.ui.fragments;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.Interpolation;
import com.badlogic.gdx.utils.Align;
import io.anuke.mindustry.core.GameState.State;
import io.anuke.mindustry.input.AndroidInput;
import io.anuke.mindustry.input.InputHandler;
import io.anuke.mindustry.input.PlaceMode;
import io.anuke.ucore.core.Core;
import io.anuke.ucore.scene.Group;
import io.anuke.ucore.scene.actions.Actions;
import io.anuke.ucore.scene.builders.imagebutton;
import io.anuke.ucore.scene.builders.label;
import io.anuke.ucore.scene.builders.table;
import io.anuke.ucore.scene.event.Touchable;
import io.anuke.ucore.scene.ui.ButtonGroup;
import io.anuke.ucore.scene.ui.ImageButton;
import io.anuke.ucore.scene.ui.Label;
import io.anuke.ucore.scene.ui.layout.Table;
import io.anuke.ucore.scene.ui.layout.Unit;
import io.anuke.ucore.util.Bundles;
import io.anuke.ucore.util.Mathf;
import static io.anuke.mindustry.Vars.*;
public class PlacementFragment implements Fragment{
boolean shown = false, placing = false;
Table breaktable, next, container;
Label modelabel;
@Override
public void build(Group parent){
if(!mobile) return;
InputHandler input = control.input(0);
float s = 50f;
float translation = Unit.dp.scl(58f);
new table(){{
visible(() -> !state.is(State.menu));
abottom();
aleft();
ButtonGroup<ImageButton> placeGroup = new ButtonGroup<>();
ButtonGroup<ImageButton> breakGroup = new ButtonGroup<>();
update(t -> {
if((input.recipe == null) == placing){
float i = 0.1f;
Interpolation n = Interpolation.pow3Out;
if(input.recipe == null){
placing = false;
container.clearActions();
container.actions(Actions.translateBy(0, -(container.getTranslation().y + translation), i, n));
if (!input.lastBreakMode.both) input.placeMode = input.lastBreakMode;
}else{
placing = true;
container.clearActions();
container.actions(Actions.translateBy(0, -(container.getTranslation().y), i, n));
input.placeMode = input.lastPlaceMode;
}
}
if(!input.placeMode.delete){
placeGroup.setMinCheckCount(1);
for(ImageButton button : placeGroup.getButtons()){
if(button.getName().equals(input.placeMode.name())){
button.setChecked(true);
break;
}
}
}else{
placeGroup.setMinCheckCount(0);
for(ImageButton button : placeGroup.getButtons())
button.setChecked(false);
}
if(input.placeMode.delete || input.breakMode.both){
PlaceMode mode = input.breakMode;
breakGroup.setMinCheckCount(1);
for(ImageButton button : breakGroup.getButtons()){
if(button.getName().equals(mode.name())){
button.setChecked(true);
break;
}
}
}else{
breakGroup.setMinCheckCount(0);
for(ImageButton button : breakGroup.getButtons())
button.setChecked(false);
}
});
container = new table(){{
modelabel = new label("").get();
row();
//break menu
new table() {{
abottom();
aleft();
height(s + 5 + 4);
next = new table("pane") {{
margin(5f);
defaults().padBottom(-5.5f);
new imagebutton("icon-arrow-right", 10 * 3, () -> {
toggle(!shown);
}).update(l -> l.getStyle().imageUp = Core.skin.getDrawable(shown ? "icon-arrow-left" : "icon-" + input.breakMode.name())).size(s, s + 4);
}}.end().get();
breaktable = new table("pane") {{
visible(() -> shown);
margin(5f);
marginLeft(-(Unit.dp.scl(1f) - 1f) * 2.5f);
touchable(Touchable.enabled);
aleft();
defaults().size(s, s + 4);
for (PlaceMode mode : PlaceMode.values()) {
if (!mode.shown || !mode.delete) continue;
defaults().padBottom(-5.5f);
ImageButton button = new imagebutton("icon-" + mode.name(), "toggle", 10 * 3, () -> {
input.resetCursor();
input.breakMode = mode;
input.lastBreakMode = mode;
if (!mode.both){
input.placeMode = mode;
}else{
input.placeMode = input.lastPlaceMode;
}
modeText(Bundles.format("text.mode.break", mode.toString()));
}).group(breakGroup).get();
button.setName(mode.name());
button.released(() -> {
//TODO hack
if(mode == PlaceMode.areaDelete){
((AndroidInput)input).placing = false;
}
});
}
}}.end().get();
breaktable.getParent().swapActor(breaktable, next);
breaktable.getTranslation().set(-breaktable.getPrefWidth(), 0);
}}.end().get();
row();
//place menu
new table() {{
touchable(Touchable.enabled);
aleft();
new table("pane") {{
margin(5f);
aleft();
defaults().size(s, s + 4).padBottom(-5.5f);
Color color = Color.GRAY;
new imagebutton("icon-cancel", 14 * 3, () -> {
input.recipe = null;
}).imageColor(color)
.visible(() -> input.recipe != null);
for (PlaceMode mode : PlaceMode.values()) {
if (!mode.shown || mode.delete) continue;
new imagebutton("icon-" + mode.name(), "toggle", 10 * 3, () -> {
input.resetCursor();
input.placeMode = mode;
input.lastPlaceMode = mode;
modeText(Bundles.format("text.mode.place", mode.toString()));
}).group(placeGroup).get().setName(mode.name());
}
new imagebutton("icon-arrow", 14 * 3, () -> {
input.rotation = Mathf.mod(input.rotation + 1, 4);
}).imageColor(color).visible(() -> input.recipe != null).update(image -> {
image.getImage().setRotation(input.rotation * 90);
image.getImage().setOrigin(Align.center);
});
}}.left().end();
}}.left().end();
}}.end().get();
container.setTranslation(0, -translation);
}}.end();
}
private void modeText(String text){
modelabel.setText(text);
modelabel.clearActions();
modelabel.setColor(Color.WHITE);
modelabel.actions(Actions.fadeOut(5f, Interpolation.fade));
}
private void toggle(boolean show){
InputHandler input = control.input(0);
float dur = 0.3f;
Interpolation in = Interpolation.pow3Out;
if(breaktable.getActions().size != 0 || shown == show) return;
breaktable.getParent().swapActor(breaktable, next);
if(!show){
input.breakMode = PlaceMode.none;
if(input.placeMode.delete) input.placeMode = PlaceMode.none;
breaktable.actions(Actions.translateBy(-breaktable.getWidth() - 5, 0, dur, in), Actions.call(() -> shown = false));
}else{
shown = true;
breaktable.actions(Actions.translateBy(-breaktable.getTranslation().x - 5, 0, dur, in));
}
}
}

View File

@@ -1,24 +1,10 @@
package io.anuke.mindustry.ui.fragments;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Align;
import io.anuke.mindustry.core.GameState.State;
import io.anuke.mindustry.input.InputHandler;
import io.anuke.mindustry.input.PlaceMode;
import io.anuke.ucore.core.Core;
import io.anuke.ucore.core.Graphics;
import io.anuke.ucore.scene.Group;
import io.anuke.ucore.scene.ui.layout.Table;
import static io.anuke.mindustry.Vars.*;
public class ToolFragment implements Fragment{
private Table tools;
private InputHandler input;
public int px, py, px2, py2;
public boolean confirming;
private final InputHandler input;
public ToolFragment(InputHandler input){
this.input = input;
@@ -26,53 +12,6 @@ public class ToolFragment implements Fragment{
@Override
public void build(Group parent){
float isize = 14*3;
tools = new Table();
tools.addImageButton("icon-cancel", isize, () -> {
if(input.placeMode == PlaceMode.areaDelete && confirming){
confirming = false;
}else{
input.recipe = null;
}
});
tools.addImageButton("icon-rotate", isize, () -> {
input.rotation++;
input.rotation %= 4;
});
tools.addImageButton("icon-check", isize, () -> {
if(input.placeMode == PlaceMode.areaDelete && confirming){
input.placeMode.released(input, px, py, px2, py2);
confirming = false;
}else{
input.placeMode.tapped(input, input.getBlockX(), input.getBlockY());
}
});
tools.setVisible(() ->
!state.is(State.menu) && mobile && ((input.recipe != null &&
input.placeMode == PlaceMode.cursor) || confirming)
);
tools.update(() -> {
if(confirming){
Vector2 v = Graphics.screen((px + px2)/2f * tilesize, Math.min(py, py2) * tilesize - tilesize*1.5f);
tools.setPosition(v.x, v.y, Align.top);
}else{
tools.setPosition(input.getCursorX(),
Gdx.graphics.getHeight() - input.getCursorY() - 15*Core.cameraScale, Align.top);
}
if(input.placeMode != PlaceMode.areaDelete){
confirming = false;
}
});
parent.addChild(tools);
}
}