use of io.anuke.mindustry.input.InputHandler in project Mindustry by Anuken.
the class ToolFragment method build.
public void build() {
InputHandler input = control.input();
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(px, py, px2, py2);
confirming = false;
} else {
input.placeMode.tapped(control.input().getBlockX(), control.input().getBlockY());
}
});
Core.scene.add(tools);
tools.setVisible(() -> !state.is(State.menu) && android && ((input.recipe != null && state.inventory.hasItems(input.recipe.requirements) && 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(control.input().getCursorX(), Gdx.graphics.getHeight() - control.input().getCursorY() - 15 * Core.cameraScale, Align.top);
}
if (input.placeMode != PlaceMode.areaDelete) {
confirming = false;
}
});
}
use of io.anuke.mindustry.input.InputHandler in project Mindustry by Anuken.
the class Renderer method drawOverlay.
void drawOverlay() {
// draw tutorial placement point
if (world.getMap().name.equals("tutorial") && control.tutorial().showBlock()) {
int x = world.getCore().x + control.tutorial().getPlacePoint().x;
int y = world.getCore().y + control.tutorial().getPlacePoint().y;
int rot = control.tutorial().getPlaceRotation();
Lines.stroke(1f);
Draw.color(Color.YELLOW);
Lines.square(x * tilesize, y * tilesize, tilesize / 2f + Mathf.sin(Timers.time(), 4f, 1f));
Draw.color(Color.ORANGE);
Lines.stroke(2f);
if (rot != -1) {
Lines.lineAngle(x * tilesize, y * tilesize, rot * 90, 6);
}
Draw.reset();
}
// draw config selected block
if (ui.configfrag.isShown()) {
Tile tile = ui.configfrag.getSelectedTile();
Draw.color(Colors.get("accent"));
Lines.stroke(1f);
Lines.square(tile.drawx(), tile.drawy(), tile.block().width * tilesize / 2f + 1f);
Draw.reset();
}
int tilex = control.input().getBlockX();
int tiley = control.input().getBlockY();
if (android) {
Vector2 vec = Graphics.world(Gdx.input.getX(0), Gdx.input.getY(0));
tilex = Mathf.scl2(vec.x, tilesize);
tiley = Mathf.scl2(vec.y, tilesize);
}
InputHandler input = control.input();
// draw placement box
if ((input.recipe != null && state.inventory.hasItems(input.recipe.requirements) && (!ui.hasMouse() || android) && control.input().drawPlace())) {
input.placeMode.draw(control.input().getBlockX(), control.input().getBlockY(), control.input().getBlockEndX(), control.input().getBlockEndY());
Lines.stroke(1f);
Draw.color(Color.SCARLET);
for (SpawnPoint spawn : world.getSpawns()) {
Lines.dashCircle(spawn.start.worldx(), spawn.start.worldy(), enemyspawnspace);
}
if (world.getCore() != null) {
Draw.color(Color.LIME);
Lines.poly(world.getSpawnX(), world.getSpawnY(), 4, 6f, Timers.time() * 2f);
}
if (input.breakMode == PlaceMode.holdDelete)
input.breakMode.draw(tilex, tiley, 0, 0);
} else if (input.breakMode.delete && control.input().drawPlace() && (input.recipe == null || !state.inventory.hasItems(input.recipe.requirements)) && (input.placeMode.delete || input.breakMode.both || !android)) {
if (input.breakMode == PlaceMode.holdDelete)
input.breakMode.draw(tilex, tiley, 0, 0);
else
input.breakMode.draw(control.input().getBlockX(), control.input().getBlockY(), control.input().getBlockEndX(), control.input().getBlockEndY());
}
if (ui.toolfrag.confirming) {
ToolFragment t = ui.toolfrag;
PlaceMode.areaDelete.draw(t.px, t.py, t.px2, t.py2);
}
Draw.reset();
// draw selected block bars and info
if (input.recipe == null && !ui.hasMouse()) {
Tile tile = world.tileWorld(Graphics.mouseWorld().x, Graphics.mouseWorld().y);
if (tile != null && tile.block() != Blocks.air) {
Tile target = tile;
if (tile.isLinked())
target = tile.getLinked();
if (showBlockDebug && target.entity != null) {
Draw.color(Color.RED);
Lines.crect(target.drawx(), target.drawy(), target.block().width * tilesize, target.block().height * tilesize);
Vector2 v = new Vector2();
Draw.tcolor(Color.YELLOW);
Draw.tscl(0.25f);
Array<Object> arr = target.block().getDebugInfo(target);
StringBuilder result = new StringBuilder();
for (int i = 0; i < arr.size / 2; i++) {
result.append(arr.get(i * 2));
result.append(": ");
result.append(arr.get(i * 2 + 1));
result.append("\n");
}
Draw.textc(result.toString(), target.drawx(), target.drawy(), v);
Draw.color(0f, 0f, 0f, 0.5f);
Fill.rect(target.drawx(), target.drawy(), v.x, v.y);
Draw.textc(result.toString(), target.drawx(), target.drawy(), v);
Draw.tscl(fontscale);
Draw.reset();
}
if (Inputs.keyDown("block_info") && target.block().fullDescription != null) {
Draw.color(Colors.get("accent"));
Lines.crect(target.drawx(), target.drawy(), target.block().width * tilesize, target.block().height * tilesize);
Draw.color();
}
if (target.entity != null) {
int bot = 0, top = 0;
for (BlockBar bar : target.block().bars) {
float offset = Mathf.sign(bar.top) * (target.block().height / 2f * tilesize + 3f + 4f * ((bar.top ? top : bot))) + (bar.top ? -1f : 0f);
float value = bar.value.get(target);
if (MathUtils.isEqual(value, -1f))
continue;
drawBar(bar.color, target.drawx(), target.drawy() + offset, value);
if (bar.top)
top++;
else
bot++;
}
}
target.block().drawSelect(target);
}
}
if ((!debug || showUI) && Settings.getBool("healthbars")) {
// draw entity health bars
for (Enemy entity : enemyGroup.all()) {
drawHealth(entity);
}
for (Player player : playerGroup.all()) {
if (!player.isDead() && !player.isAndroid)
drawHealth(player);
}
}
}
use of io.anuke.mindustry.input.InputHandler in project Mindustry by Anuken.
the class BlocksFragment method build.
public void build() {
InputHandler input = control.input();
new table() {
{
abottom();
aright();
visible(() -> !state.is(State.menu) && shown);
blocks = new table() {
{
itemtable = new Table("button");
itemtable.setVisible(() -> input.recipe == null && !state.mode.infiniteResources);
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(itemtable);
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();
Recipes.getBy(sec, recipes);
maxcol = Math.max((int) ((float) recipes.size / rows + 1), maxcol);
}
for (Section sec : Section.values()) {
recipes.clear();
Recipes.getBy(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() <= 2 ? -10 : -5);
button.getImageCell().size(40).padBottom(4).padTop(2);
group.add(button);
if (sec.ordinal() % 3 == 2 && sec.ordinal() > 0) {
row();
}
table.margin(4);
table.top().left();
int i = 0;
for (Recipe r : recipes) {
TextureRegion region = Draw.hasRegion(r.result.name() + "-icon") ? Draw.region(r.result.name() + "-icon") : Draw.region(r.result.name());
ImageButton image = new ImageButton(region, "select");
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.clicked(() -> {
// note: input.recipe only gets set here during a click.
// during a hover only the visual description will be updated.
boolean nothingSelectedYet = input.recipe == null;
boolean selectedSomethingElse = !nothingSelectedYet && input.recipe != r;
boolean shouldMakeSelection = nothingSelectedYet || selectedSomethingElse;
if (shouldMakeSelection) {
input.recipe = r;
hoveredDescriptionRecipe = r;
updateRecipe(r);
} else {
input.recipe = null;
hoveredDescriptionRecipe = null;
updateRecipe(null);
}
});
table.add(image).size(size + 8);
image.getImageCell().size(size);
image.update(() -> {
boolean canPlace = !control.tutorial().active() || control.tutorial().canPlace();
boolean has = (state.inventory.hasItems(r.requirements)) && canPlace;
image.setChecked(input.recipe == r);
image.setTouchable(canPlace ? Touchable.enabled : Touchable.disabled);
image.getImage().setColor(has ? Color.WHITE : Hue.lightness(0.33f));
});
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();
row();
if (!android) {
weapons = new table("button").margin(0).fillX().end().get();
}
visible(() -> !state.is(State.menu) && shown);
}
}.end().get();
}
}.end();
updateWeapons();
}
use of io.anuke.mindustry.input.InputHandler in project Mindustry by Anuken.
the class PlacementFragment method build.
public void build() {
if (!android)
return;
InputHandler input = control.input();
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, () -> {
control.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, () -> {
control.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();
}
Aggregations