generated from GlennFolker/MindustryModTemplate
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f3fc7e7
commit ec03380
Showing
7 changed files
with
186 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package confictura.input; | ||
|
||
import arc.*; | ||
import arc.input.*; | ||
import arc.input.GestureDetector.*; | ||
import arc.math.geom.*; | ||
import arc.scene.ui.*; | ||
import arc.struct.*; | ||
import confictura.net.*; | ||
import mindustry.game.EventType.*; | ||
import mindustry.gen.*; | ||
|
||
import static arc.Core.*; | ||
import static mindustry.Vars.*; | ||
|
||
public class InputAggregator implements GestureListener{ | ||
protected final Seq<TapHandle> handles = new Seq<>(false); | ||
|
||
protected boolean tapped; | ||
protected final Vec2 tap = new Vec2(); | ||
|
||
public InputAggregator(){ | ||
if(!headless){ | ||
if(mobile) input.addProcessor(new GestureDetector(new GestureListener(){ | ||
@Override | ||
public boolean tap(float x, float y, int count, KeyCode button){ | ||
if(count == 2 && !( | ||
state.isMenu() || | ||
scene.hasMouse(x, y) || | ||
control.input.isPlacing() || | ||
control.input.isBreaking() || | ||
control.input.selectedUnit() != null | ||
)){ | ||
tapped = true; | ||
tap.x = x; | ||
tap.y = y; | ||
|
||
return true; | ||
}else{ | ||
return false; | ||
} | ||
} | ||
})); | ||
|
||
Events.run(Trigger.update, () -> { | ||
if(!mobile){ | ||
//TODO `Keybinds` should have a way to add default bindings. | ||
if(input.keyTap(KeyCode.altLeft) && !(scene.getKeyboardFocus() instanceof TextField)){ | ||
tapped = true; | ||
tap.set(input.mouseWorld()); | ||
} | ||
} | ||
|
||
if(tapped){ | ||
CCall.tap(player, tap.x, tap.y); | ||
tapped = false; | ||
} | ||
}); | ||
} | ||
} | ||
|
||
public TapHandle onTap(TapListener listener){ | ||
var handle = new TapHandle(handles.size, listener); | ||
handles.add(handle); | ||
return handle; | ||
} | ||
|
||
public void tap(Player player, float x, float y){ | ||
handles.each(handle -> handle.listener.tapped(player, x, y)); | ||
} | ||
|
||
public interface TapListener{ | ||
void tapped(Player player, float x, float y); | ||
} | ||
|
||
public class TapHandle{ | ||
protected int index; | ||
protected TapListener listener; | ||
|
||
protected TapHandle(int index, TapListener listener){ | ||
this.index = index; | ||
this.listener = listener; | ||
} | ||
|
||
public void remove(){ | ||
handles.remove(index); | ||
if(handles.size > index) handles.get(index).index = index; | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package confictura.net; | ||
|
||
import mindustry.gen.*; | ||
import mindustry.net.*; | ||
|
||
import static confictura.ConficturaMod.*; | ||
import static mindustry.Vars.*; | ||
|
||
public final class CCall{ | ||
private CCall(){ | ||
throw new AssertionError(); | ||
} | ||
|
||
public static void init(){ | ||
Net.registerPacket(TapPacket::new); | ||
} | ||
|
||
/** Called on clients, handled immediately on said client, and sent to the server to be handled and broadcast to other clients. */ | ||
public static void tap(Player player, float x, float y){ | ||
inputAggregator.tap(player, x, y); | ||
tapSend(player, x, y, false); | ||
} | ||
|
||
static void tapSend(Player player, float x, float y, boolean forwarded){ | ||
if(net.server() || net.client()){ | ||
var packet = new TapPacket(); | ||
if(net.server()) packet.player = player; | ||
packet.x = x; | ||
packet.y = y; | ||
|
||
if(forwarded){ | ||
net.sendExcept(player.con, packet, false); | ||
}else{ | ||
net.send(packet, false); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package confictura.net; | ||
|
||
import arc.util.io.*; | ||
import mindustry.gen.*; | ||
import mindustry.io.*; | ||
import mindustry.net.*; | ||
|
||
import static confictura.ConficturaMod.*; | ||
import static mindustry.Vars.*; | ||
|
||
public class TapPacket extends Packet{ | ||
/** {@code null} when sent from {@linkplain Net#client() clients}, as it is obtained from the sender's connection. */ | ||
public Player player; | ||
public float x, y; | ||
|
||
public TapPacket(){} | ||
|
||
@Override | ||
public void write(Writes write){ | ||
if(net.server()) TypeIO.writeEntity(write, player); | ||
write.f(x); | ||
write.f(y); | ||
} | ||
|
||
@Override | ||
public void read(Reads read){ | ||
if(net.client()) player = TypeIO.readEntity(read); | ||
x = read.f(); | ||
y = read.f(); | ||
} | ||
|
||
@Override | ||
public void handleServer(NetConnection con){ | ||
if(con.player == null || con.kicked) return; | ||
|
||
inputAggregator.tap(con.player, x, y); | ||
CCall.tapSend(con.player, x, y, true); | ||
} | ||
|
||
@Override | ||
public void handleClient(){ | ||
inputAggregator.tap(player, x, y); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters