forked from Clones/Controlify
separate API from impl
This commit is contained in:
27
src/main/java/dev/isxander/controlify/api/ControlifyApi.java
Normal file
27
src/main/java/dev/isxander/controlify/api/ControlifyApi.java
Normal file
@ -0,0 +1,27 @@
|
||||
package dev.isxander.controlify.api;
|
||||
|
||||
import dev.isxander.controlify.Controlify;
|
||||
import dev.isxander.controlify.InputMode;
|
||||
import dev.isxander.controlify.api.bind.ControlifyBindingsApi;
|
||||
import dev.isxander.controlify.controller.Controller;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* Interface with Controlify in a manner where you don't need to worry about updates
|
||||
* breaking! This is the recommended way to interact with Controlify.
|
||||
*/
|
||||
public interface ControlifyApi {
|
||||
/**
|
||||
* @return the controller currently in use. If disabled, this will return {@link Controller#DUMMY}
|
||||
*/
|
||||
@NotNull Controller<?, ?> currentController();
|
||||
|
||||
@NotNull InputMode currentInputMode();
|
||||
void setInputMode(@NotNull InputMode mode);
|
||||
|
||||
@NotNull ControlifyBindingsApi bindingsApi();
|
||||
|
||||
static ControlifyApi get() {
|
||||
return Controlify.instance();
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package dev.isxander.controlify.api.bind;
|
||||
|
||||
import dev.isxander.controlify.bindings.BindingSupplier;
|
||||
import dev.isxander.controlify.bindings.GamepadBinds;
|
||||
import net.minecraft.client.KeyMapping;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
|
||||
import java.util.function.BooleanSupplier;
|
||||
|
||||
public interface ControlifyBindingsApi {
|
||||
/**
|
||||
* Registers a custom binding for all available controllers.
|
||||
* If the controller is not a gamepad, the binding with be empty by default.
|
||||
*
|
||||
* @param bind the default gamepad bind
|
||||
* @param id the identifier for the binding, the namespace should be your modid.
|
||||
* @return the binding supplier to fetch the binding for a specific controller.
|
||||
*/
|
||||
BindingSupplier registerBind(GamepadBinds bind, ResourceLocation id);
|
||||
|
||||
/**
|
||||
* Registers a custom binding for all available controllers.
|
||||
* If the controller is not a gamepad, the binding with be empty by default.
|
||||
*
|
||||
* @param bind the default gamepad bind
|
||||
* @param id the identifier for the binding, the namespace should be your modid.
|
||||
* @param override the minecraft keybind to imitate.
|
||||
* @param toggleOverride a supplier that returns true if the vanilla keybind should be treated as a {@link net.minecraft.client.ToggleKeyMapping}
|
||||
* @return the binding supplier to fetch the binding for a specific controller.
|
||||
*/
|
||||
BindingSupplier registerBind(GamepadBinds bind, ResourceLocation id, KeyMapping override, BooleanSupplier toggleOverride);
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package dev.isxander.controlify.api.buttonguide;
|
||||
|
||||
/**
|
||||
* Whether the action should be on the left or right list.
|
||||
*/
|
||||
public enum ActionLocation {
|
||||
LEFT, RIGHT
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package dev.isxander.controlify.api.buttonguide;
|
||||
|
||||
/**
|
||||
* Defines how the action is sorted in the list. All default Controlify actions are {@link #NORMAL}.
|
||||
*/
|
||||
public enum ActionPriority {
|
||||
LOWEST,
|
||||
LOW,
|
||||
NORMAL,
|
||||
HIGH,
|
||||
HIGHEST
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package dev.isxander.controlify.api.buttonguide;
|
||||
|
||||
import dev.isxander.controlify.bindings.ControllerBinding;
|
||||
|
||||
/**
|
||||
* Allows you to register your own actions to the button guide.
|
||||
* This should be called through {@link dev.isxander.controlify.api.event.ControlifyEvents#BUTTON_GUIDE_REGISTRY} as
|
||||
* these should be called every time the guide is initialised.
|
||||
*/
|
||||
public interface ButtonGuideRegistry {
|
||||
/**
|
||||
* Registers a new action to the button guide.
|
||||
*
|
||||
* @param binding the binding for the action, if unbound, the action is hidden.
|
||||
* @param location the location of the action, left or right.
|
||||
* @param priority the priority of the action, used to sort the list.
|
||||
* @param supplier the supplier for the name of the action. can be empty to hide the action.
|
||||
*/
|
||||
void registerGuideAction(ControllerBinding<?> binding, ActionLocation location, ActionPriority priority, GuideActionNameSupplier supplier);
|
||||
|
||||
/**
|
||||
* Registers a new action to the button guide.
|
||||
*
|
||||
* @param binding the binding for the action, if unbound, the action is hidden.
|
||||
* @param location the location of the action, left or right.
|
||||
* @param supplier the supplier for the name of the action. can be empty to hide the action.
|
||||
*/
|
||||
void registerGuideAction(ControllerBinding<?> binding, ActionLocation location, GuideActionNameSupplier supplier);
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package dev.isxander.controlify.api.buttonguide;
|
||||
|
||||
import dev.isxander.controlify.controller.Controller;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.multiplayer.ClientLevel;
|
||||
import net.minecraft.client.player.LocalPlayer;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.world.phys.HitResult;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Supplies the text to display for a guide action based on the current context.
|
||||
* If return is empty, the action will not be displayed.
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface GuideActionNameSupplier {
|
||||
Optional<Component> supply(
|
||||
Minecraft client,
|
||||
LocalPlayer player,
|
||||
ClientLevel level,
|
||||
HitResult hitResult,
|
||||
Controller<?, ?> controller
|
||||
);
|
||||
}
|
@ -0,0 +1,66 @@
|
||||
package dev.isxander.controlify.api.event;
|
||||
|
||||
import dev.isxander.controlify.InputMode;
|
||||
import dev.isxander.controlify.bindings.ControllerBindings;
|
||||
import dev.isxander.controlify.controller.Controller;
|
||||
import dev.isxander.controlify.api.buttonguide.ButtonGuideRegistry;
|
||||
import net.fabricmc.fabric.api.event.Event;
|
||||
import net.fabricmc.fabric.api.event.EventFactory;
|
||||
|
||||
public final class ControlifyEvents {
|
||||
/**
|
||||
* Triggers when the input mode is changed from keyboard to controller or vice versa.
|
||||
*/
|
||||
public static final Event<InputModeChanged> INPUT_MODE_CHANGED = EventFactory.createArrayBacked(InputModeChanged.class, callbacks -> mode -> {
|
||||
for (InputModeChanged callback : callbacks) {
|
||||
callback.onInputModeChanged(mode);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Triggers every tick when the current controller state has been updated.
|
||||
*/
|
||||
public static final Event<ControllerStateUpdate> CONTROLLER_STATE_UPDATED = EventFactory.createArrayBacked(ControllerStateUpdate.class, callbacks -> controller -> {
|
||||
for (ControllerStateUpdate callback : callbacks) {
|
||||
callback.onControllerStateUpdate(controller);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Triggers when the button guide entries are being populated, so you can add more of your own.
|
||||
*/
|
||||
public static final Event<ButtonGuideRegistryEvent> BUTTON_GUIDE_REGISTRY = EventFactory.createArrayBacked(ButtonGuideRegistryEvent.class, callbacks -> (bindings, registry) -> {
|
||||
for (ButtonGuideRegistryEvent callback : callbacks) {
|
||||
callback.onRegisterButtonGuide(bindings, registry);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Triggers in a GUI when the virtual mouse is toggled on or off.
|
||||
*/
|
||||
public static final Event<VirtualMouseToggled> VIRTUAL_MOUSE_TOGGLED = EventFactory.createArrayBacked(VirtualMouseToggled.class, callbacks -> enabled -> {
|
||||
for (VirtualMouseToggled callback : callbacks) {
|
||||
callback.onVirtualMouseToggled(enabled);
|
||||
}
|
||||
});
|
||||
|
||||
@FunctionalInterface
|
||||
public interface InputModeChanged {
|
||||
void onInputModeChanged(InputMode mode);
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
public interface ControllerStateUpdate {
|
||||
void onControllerStateUpdate(Controller<?, ?> controller);
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
public interface ButtonGuideRegistryEvent {
|
||||
void onRegisterButtonGuide(ControllerBindings<?> bindings, ButtonGuideRegistry registry);
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
public interface VirtualMouseToggled {
|
||||
void onVirtualMouseToggled(boolean enabled);
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package dev.isxander.controlify.api.vmousesnapping;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* An interface to implement by gui components to define snap points for virtual mouse snapping.
|
||||
*/
|
||||
public interface ISnapBehaviour {
|
||||
Set<SnapPoint> getSnapPoints();
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package dev.isxander.controlify.api.vmousesnapping;
|
||||
|
||||
import org.joml.Vector2i;
|
||||
import org.joml.Vector2ic;
|
||||
|
||||
/**
|
||||
* Defines a point on the screen that the virtual mouse can snap to.
|
||||
*
|
||||
* @param position the position on the screen where the cursor will snap to
|
||||
* @param range how far away from the snap point the cursor can be and still snap to it
|
||||
*/
|
||||
public record SnapPoint(Vector2ic position, int range) {
|
||||
public SnapPoint(int x, int y, int range) {
|
||||
this(new Vector2i(x, y), range);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user