1
0
forked from Clones/Controlify

rumble source

This commit is contained in:
isXander
2023-04-05 12:27:47 +01:00
parent f7fe7d8ec0
commit cf3e67fff4
16 changed files with 209 additions and 31 deletions

View File

@ -4,11 +4,14 @@ import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import dev.isxander.controlify.Controlify;
import dev.isxander.controlify.InputMode;
import dev.isxander.controlify.api.ControlifyApi;
import dev.isxander.controlify.bindings.ControllerBindings;
import dev.isxander.controlify.controller.hid.ControllerHIDService;
import dev.isxander.controlify.controller.sdl2.SDL2NativesManager;
import dev.isxander.controlify.rumble.RumbleCapable;
import dev.isxander.controlify.rumble.RumbleManager;
import dev.isxander.controlify.rumble.RumbleSource;
import org.libsdl.SDL;
import org.lwjgl.glfw.GLFW;
@ -107,7 +110,14 @@ public abstract class AbstractController<S extends ControllerState, C extends Co
@Override
public void setConfig(Gson gson, JsonElement json) {
C newConfig = gson.fromJson(json, new TypeToken<C>(getClass()){}.getType());
C newConfig;
try {
newConfig = gson.fromJson(json, new TypeToken<C>(getClass()){}.getType());
} catch (Exception e) {
Controlify.LOGGER.error("Could not set config for controller " + name() + " (" + uid() + ")! Using default config instead. Printing json: " + json.toString(), e);
return;
}
if (newConfig != null) {
this.config = newConfig;
} else {
@ -117,9 +127,16 @@ public abstract class AbstractController<S extends ControllerState, C extends Co
}
@Override
public boolean setRumble(float strongMagnitude, float weakMagnitude) {
public boolean setRumble(float strongMagnitude, float weakMagnitude, RumbleSource source) {
if (!canRumble()) return false;
var strengthMod = config().getRumbleStrength(source);
if (source != RumbleSource.MASTER)
strengthMod *= config().getRumbleStrength(RumbleSource.MASTER);
strongMagnitude *= strengthMod;
weakMagnitude *= strengthMod;
// the duration doesn't matter because we are not updating the joystick state,
// so there is never any SDL check to stop the rumble after the desired time.
if (!SDL.SDL_JoystickRumble(ptrJoystick, (int)(strongMagnitude * 65535.0F), (int)(weakMagnitude * 65535.0F), 1)) {
@ -131,7 +148,9 @@ public abstract class AbstractController<S extends ControllerState, C extends Co
@Override
public boolean canRumble() {
return SDL2NativesManager.isLoaded() && config().allowVibrations;
return SDL2NativesManager.isLoaded()
&& config().allowVibrations
&& ControlifyApi.get().currentInputMode() == InputMode.CONTROLLER;
}
@Override

View File

@ -7,7 +7,9 @@ import dev.isxander.controlify.controller.gamepad.GamepadController;
import dev.isxander.controlify.controller.hid.ControllerHIDService;
import dev.isxander.controlify.controller.joystick.SingleJoystickController;
import dev.isxander.controlify.debug.DebugProperties;
import dev.isxander.controlify.rumble.RumbleCapable;
import dev.isxander.controlify.rumble.RumbleManager;
import dev.isxander.controlify.rumble.RumbleSource;
import org.lwjgl.glfw.GLFW;
import java.util.HashMap;
@ -71,6 +73,17 @@ public interface Controller<S extends ControllerState, C extends ControllerConfi
Controller<?, ?> DUMMY = new Controller<>() {
private final ControllerBindings<ControllerState> bindings = new ControllerBindings<>(this);
private final RumbleManager rumbleManager = new RumbleManager(new RumbleCapable() {
@Override
public boolean setRumble(float strongMagnitude, float weakMagnitude, RumbleSource source) {
return false;
}
@Override
public boolean canRumble() {
return false;
}
});
private final ControllerConfig config = new ControllerConfig() {
@Override
public void setDeadzone(int axis, float deadzone) {
@ -150,7 +163,7 @@ public interface Controller<S extends ControllerState, C extends ControllerConfi
@Override
public RumbleManager rumbleManager() {
return null;
return rumbleManager;
}
};
}

View File

@ -1,5 +1,12 @@
package dev.isxander.controlify.controller;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import dev.isxander.controlify.rumble.RumbleSource;
import net.minecraft.resources.ResourceLocation;
import java.util.Map;
public abstract class ControllerConfig {
public float horizontalLookSensitivity = 1f;
public float verticalLookSensitivity = 0.9f;
@ -24,9 +31,18 @@ public abstract class ControllerConfig {
public boolean reduceAimingSensitivity = true;
public boolean allowVibrations = true;
public JsonObject vibrationStrengths = RumbleSource.getDefaultJson();
public boolean calibrated = false;
public abstract void setDeadzone(int axis, float deadzone);
public abstract float getDeadzone(int axis);
public float getRumbleStrength(RumbleSource source) {
return vibrationStrengths.asMap().getOrDefault(source.id().toString(), new JsonPrimitive(1f)).getAsFloat();
}
public void setRumbleStrength(RumbleSource source, float strength) {
vibrationStrengths.addProperty(source.id().toString(), strength);
}
}

View File

@ -10,6 +10,7 @@ import dev.isxander.controlify.controller.joystick.mapping.JoystickMapping;
import dev.isxander.controlify.controller.joystick.mapping.RPJoystickMapping;
import dev.isxander.controlify.rumble.RumbleCapable;
import dev.isxander.controlify.rumble.RumbleManager;
import dev.isxander.controlify.rumble.RumbleSource;
import org.lwjgl.glfw.GLFW;
import java.util.List;
@ -140,7 +141,7 @@ public class CompoundJoystickController implements JoystickController<JoystickCo
}
@Override
public boolean setRumble(float strongMagnitude, float weakMagnitude) {
public boolean setRumble(float strongMagnitude, float weakMagnitude, RumbleSource source) {
return false;
}