Skip to content

Commit

Permalink
Codechange: Use tap/release instead of evaluating at every tick
Browse files Browse the repository at this point in the history
Also fixed a crash where selected could be null if mouse is dragged outside of world bounds
  • Loading branch information
zxtej committed Dec 29, 2024
1 parent 0122f4c commit c9ebe03
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 23 deletions.
30 changes: 11 additions & 19 deletions core/src/mindustry/input/DesktopInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ public class DesktopInput extends InputHandler{
/** Previously selected tile. */
public Tile prevSelected;
private long lastShiftZ;
/** Whether the current selected building being dragged from*/
private boolean configDragging = false;

@Override
public void buildUI(Group group){
Expand Down Expand Up @@ -190,7 +188,7 @@ public void drawTop(){
}
}

if (configDragging && config.getSelected() != null) {
if (config.dragging) {
Vec2 mouseCoords = input.mouseWorld();
Building selected = config.getSelected();
Draw.color(Pal.accent);
Expand Down Expand Up @@ -800,22 +798,6 @@ void pollInput(){
int cursorY = tileY(Core.input.mouseY());
int rawCursorX = World.toTile(Core.input.mouseWorld().x), rawCursorY = World.toTile(Core.input.mouseWorld().y);

// Handle drag to config behaviour
if (config.selectedCanDrag) {
if (input.keyDown(Binding.select)) {
if (selected.build == config.getSelected()) configDragging = true;
} else if (configDragging) {
Building hovered = world.build(cursorX, cursorY);
if (hovered != config.getSelected()) {
if (hovered != null) {
config.getSelected().onConfigureBuildTapped(hovered);
}
config.hideConfig();
}
configDragging = false;
}
}

//automatically pause building if the current build queue is empty
if(Core.settings.getBool("buildautopause") && isBuilding && !isBuildingIgnoreNetworking()){
isBuilding = false;
Expand Down Expand Up @@ -931,6 +913,16 @@ else if (Core.input.ctrl()) {
selectUnitsRect();
}

// Handle drag to config behaviour
if(config.dragging && Core.input.keyRelease(Binding.select)){
config.dragging = false;
Building hovered = selected == null ? null : selected.build;
if(hovered != config.getSelected()){
if(hovered != null) config.getSelected().onConfigureBuildTapped(hovered);
config.hideConfig();
}
}

if(Core.input.keyTap(Binding.select) && !Core.scene.hasMouse()){
tappedOne = false;
BuildPlan plan = getPlan(cursorX, cursorY);
Expand Down
8 changes: 4 additions & 4 deletions core/src/mindustry/ui/fragments/BlockConfigFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class BlockConfigFragment{
);
Table table = new Table();
Building selected;
public boolean selectedCanDrag = false;
public boolean dragging = false;

public void build(Group parent){
table.visible = false;
Expand All @@ -36,7 +36,7 @@ public void build(Group parent){
public void forceHide(){
table.visible = false;
selected = null;
selectedCanDrag = false;
dragging = false;
}

public boolean isShown(){
Expand All @@ -51,7 +51,7 @@ public void showConfig(Building tile){
if(selected != null) selected.onConfigureClosed();
if(tile.configTapped()){
selected = tile;
selectedCanDrag = validBuilds.contains(selected.getClass());
dragging = validBuilds.contains(selected.getClass());
table.visible = true;
table.clear();
table.background(null); // clear the background as some blocks set custom ones
Expand Down Expand Up @@ -85,7 +85,7 @@ public boolean hasConfigMouse(){
public void hideConfig(){
if(selected != null) selected.onConfigureClosed();
selected = null;
selectedCanDrag = false;
dragging = false;
table.actions(Actions.scaleTo(0f, 1f, 0.06f, Interp.pow3Out), Actions.visible(false));
}
}

0 comments on commit c9ebe03

Please sign in to comment.