Skip to content

Commit

Permalink
Tap listener
Browse files Browse the repository at this point in the history
  • Loading branch information
GlennFolker committed Apr 9, 2024
1 parent f3fc7e7 commit ec03380
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 9 deletions.
12 changes: 11 additions & 1 deletion src/confictura/ConficturaMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import confictura.gen.*;
import confictura.graphics.*;
import confictura.graphics.g3d.*;
import confictura.input.*;
import confictura.net.*;
import confictura.ui.*;
import confictura.ui.dialogs.*;
import confictura.util.*;
Expand All @@ -34,26 +36,32 @@
* Main entry point of the mod. Handles startup things like content loading, entity registering, and utility bindings.
* @author GlennFolker
*/
@SuppressWarnings("unchecked")
public class ConficturaMod extends Mod{
// <--- Meta information. --->
public static DevBuild dev;

public static Seq<String> packages = Seq.with("java.lang", "java.util");
public static Seq<Class<?>> classes = new Seq<>();
public static ObjectIntMap<String> blockColors = new ObjectIntMap<>();

// <--- Modules present in both servers and clients. --->
public static Cinematic cinematic;
public static InputAggregator inputAggregator;

// <--- Modules only present in clients, typically rendering or auxiliary input utilities. --->
public static RenderContext renderContext;
public static ModelPropDrawer modelPropDrawer;

// <--- Map-editor extension modules. --->
public static CinematicEditor cinematicEditor;

// <--- UI extension modules. --->
public static CinematicEditorDialog cinematicDialog;
public static ScriptDialog scriptDialog;

protected static LoadedMod mod;

@SuppressWarnings("unchecked")
public ConficturaMod(){
try{
var devImpl = (Class<? extends DevBuild>)Class.forName("confictura.DevBuildImpl", true, mods.mainLoader());
Expand Down Expand Up @@ -172,6 +180,7 @@ public String getName(){
app.post(() -> {
CShaders.load();

inputAggregator = new InputAggregator();
renderContext = new RenderContext();
modelPropDrawer = new ModelPropDrawer(CShaders.modelProp, 8192, 16384);
});
Expand All @@ -198,6 +207,7 @@ public void init(){

@Override
public void loadContent(){
CCall.init();
EntityRegistry.register();

ScriptUtils.init();
Expand Down
4 changes: 3 additions & 1 deletion src/confictura/content/CUnitTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ private CUnitTypes(){

/** Instantiates all contents. Called in the main thread in {@link ConficturaMod#loadContent()}. */
public static void load(){
parrier = new CUnitType("parrier", SkillMechUnit.class);
parrier = new CUnitType("parrier", SkillMechUnit.class){{
canBoost = true;
}};
}
}
90 changes: 90 additions & 0 deletions src/confictura/input/InputAggregator.java
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;
}
}
}
5 changes: 0 additions & 5 deletions src/confictura/input/ParryInput.java

This file was deleted.

38 changes: 38 additions & 0 deletions src/confictura/net/CCall.java
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);
}
}
}
}
44 changes: 44 additions & 0 deletions src/confictura/net/TapPacket.java
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);
}
}
2 changes: 0 additions & 2 deletions src/confictura/util/StructUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,10 @@ public static <T> boolean arrayEq(T[] first, T[] second, Boolf2<T, T> eq){
return true;
}

@FunctionalInterface
public interface Reducei<T>{
int get(T item, int accum);
}

@FunctionalInterface
public interface Reducef<T>{
float get(T item, float accum);
}
Expand Down

0 comments on commit ec03380

Please sign in to comment.