Skip to content

Commit

Permalink
Merge pull request #187 from 7003Mars/v7
Browse files Browse the repository at this point in the history
Allow build plans to be configured
  • Loading branch information
BalaM314 authored Oct 25, 2024
2 parents d1828ff + 6bce78d commit 9680522
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 1 deletion.
1 change: 1 addition & 0 deletions core/src/mindustry/core/Renderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,7 @@ public void showLaunch(CoreBlock coreType){
Vars.ui.hudfrag.showLaunch();
Vars.control.input.config.hideConfig();
Vars.control.input.inv.hide();
Vars.control.input.planConfig.hide();
launchCoreType = coreType;
launching = true;
landCore = player.team().core();
Expand Down
9 changes: 8 additions & 1 deletion core/src/mindustry/input/DesktopInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -1016,7 +1016,14 @@ else if (Core.input.ctrl()) {
if(getPlan(splan.x, splan.y, splan.block.size, splan) != null){
player.unit().plans().remove(splan, true);
}
if(!splanMoved) player.unit().addBuild(splan, false); // Add the plan to the top of the queue
if(!splanMoved && input.ctrl()) {
inv.hide();
config.hideConfig();
planConfig.showConfig(splan);
} else {
planConfig.hide();
if (!splanMoved) player.unit().addBuild(splan, false); // Add the plan to the top of the queue
}
splan = null;
splanMoved = false;
}
Expand Down
5 changes: 5 additions & 0 deletions core/src/mindustry/input/InputHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public abstract class InputHandler implements InputProcessor, GestureListener{

public final BlockInventoryFragment inv;
public final BlockConfigFragment config;
public final PlanConfigFragment planConfig;

private WidgetGroup group = new WidgetGroup();

Expand Down Expand Up @@ -143,6 +144,7 @@ public InputHandler(){
group.touchable = Touchable.childrenOnly;
inv = new BlockInventoryFragment();
config = new BlockConfigFragment();
planConfig = new PlanConfigFragment();

Events.on(UnitDestroyEvent.class, e -> {
if(e.unit != null && e.unit.isPlayer() && e.unit.getPlayer().isLocal() && e.unit.type.weapons.contains(w -> w.bullet.killShooter)){
Expand Down Expand Up @@ -1704,6 +1706,8 @@ boolean checkConfigTap(){

/** Handles tile tap events that are not platform specific. */
boolean tileTapped(@Nullable Building build){
// Should hide plan config regardless of what was tapped
planConfig.hide();
if(build == null){
inv.hide();
config.hideConfig();
Expand Down Expand Up @@ -1998,6 +2002,7 @@ public void add(){

inv.build(group);
config.build(group);
planConfig.build(group);
}
}

Expand Down
108 changes: 108 additions & 0 deletions core/src/mindustry/ui/fragments/PlanConfigFragment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package mindustry.ui.fragments;

import arc.*;
import arc.math.*;
import arc.math.geom.*;
import arc.scene.*;
import arc.scene.actions.*;
import arc.scene.ui.layout.*;
import arc.struct.*;
import arc.util.*;
import mindustry.ctype.*;
import mindustry.entities.units.*;
import mindustry.game.*;
import mindustry.type.*;
import mindustry.world.*;
import mindustry.world.blocks.*;
import mindustry.world.blocks.payloads.Constructor;
import mindustry.world.blocks.payloads.PayloadRouter;
import mindustry.world.blocks.payloads.PayloadSource;
import mindustry.world.blocks.units.UnitFactory;

import static mindustry.Vars.*;

/**
* Displays the configuration UI for build plans before they have been placed.
*/
public class PlanConfigFragment {
Table table = new Table();
BuildPlan selected;

public void build(Group parent) {
table.visible = false;
parent.addChild(table);

Events.on(EventType.ResetEvent.class, e -> forceHide());
}

public void showConfig(BuildPlan plan) {
if (this.selected == plan) {
hide();
return;
}
Block block = plan.block;
if (!block.configurable) return;
selected = plan;
table.clear();

//Only allows configuring content (items, liquids, units, blocks)
//Each block manages its own configs, but that function requires an existing build
//Try to guess it
var options = new Seq<UnlockableContent>();
if (block.configurations.containsKey(Item.class)) {
options.add(content.items());
}
if (block.configurations.containsKey(Liquid.class)) {
options.add(content.liquids());
}
if (block instanceof UnitFactory f) {
options.add(f.plans.map(p -> p.unit).retainAll(u -> !u.isBanned()));
} else if (block.configurations.containsKey(UnitType.class)) {
options.add(content.units().retainAll(u -> !u.isBanned()));
}
if (block instanceof Constructor b) {
options.add(content.blocks().select(b::canProduce));
} else if (block instanceof PayloadSource b) {
options.add(content.blocks().select(b::canProduce));
} else if (block instanceof PayloadRouter b) {
options.add(content.blocks().select(b::canSort));
} else if (block.configurations.containsKey(Block.class)) {
options.add(content.blocks());
}
if (options.isEmpty()) return;

ItemSelection.buildTable(
table, options,
() -> selected != null ? (selected.config instanceof UnlockableContent c ? c : null) : null,
content -> {
selected.config = content;
hide();
},
block.selectionRows, block.selectionColumns
);
table.pack();
table.setTransform(true);
table.visible = true;
table.actions(Actions.scaleTo(0f, 1f), Actions.visible(true),
Actions.scaleTo(1f, 1f, 0.07f, Interp.pow3Out));
table.update(() -> {
table.setOrigin(Align.center);
if (plan.isDone() || !(control.input.selectPlans.contains(plan) || player.unit().plans.contains(plan))) {
this.hide();
return;
}
Vec2 pos = Core.input.mouseScreen(plan.drawx(), plan.drawy() - block.size * tilesize / 2.0F - 1);
table.setPosition(pos.x, pos.y, Align.top);
});
}

public void forceHide() {
table.visible = false;
selected = null;
}

public void hide() {
selected = null;
table.actions(Actions.scaleTo(0f, 1f, 0.06f, Interp.pow3Out), Actions.visible(false));
}
}

0 comments on commit 9680522

Please sign in to comment.