Skip to content

achepta/BlockableInputCallbacks

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

BlockableInputCallbacks

Shared Openplanet plugin that owns native input hooks and dispatches registered callbacks that can block propagation.

Consumer plugins should only register and unregister callbacks.

Add a dependency in your plugin:

[script]
dependencies = ["BlockableInputCallbacks"]

Then register a callback:

int g_CallbackId = -1;
BlockableInputCallbacks::IInputCallback@ g_Callback;

class MyInputCallback : BlockableInputCallbacks::IInputCallback {
    BlockableInputCallbacks::InputDecision OnPadInput(
        BlockableInputCallbacks::PadInput input,
        BlockableInputCallbacks::PadInputEvent event,
        float value
    ) {
        if (input == BlockableInputCallbacks::PadInput::A) {
            if (event == BlockableInputCallbacks::PadInputEvent::Pressed) {
                doStuff();
            }
            return BlockableInputCallbacks::InputDecision::Block;
        }

        return BlockableInputCallbacks::InputDecision::Allow;
    }
}

void Main() {
    @g_Callback = MyInputCallback();
    g_CallbackId = BlockableInputCallbacks::RegisterCallback(
        "MyPlugin",
        g_Callback,
        0.8f
    );
}

void OnDestroyed() {
    if (g_CallbackId >= 0) {
        BlockableInputCallbacks::UnregisterCallback(g_CallbackId);
        g_CallbackId = -1;
    }
    @g_Callback = null;
}

Controller buttons, stick directions, and triggers are dispatched from the XInput conversion hook. Pad callbacks receive Pressed, Held, and Released; value is 1.0 for digital buttons and 0.0..1.0 for stick directions/triggers.

RegisterCallback(name, callback) uses the default activation threshold 0.01. RegisterCallback(name, callback, threshold) lets each callback define its own activation threshold for Pressed, Held, and Released edge detection.

Explicit toggleable blocking can also be applied with overloaded functions:

BlockableInputCallbacks::SetInputBlocked(BlockableInputCallbacks::PadInput::A, true);
BlockableInputCallbacks::SetInputBlocked(BlockableInputCallbacks::KeyboardInput::ArrowLeft, true);

Keyboard input should use Openplanet's normal keyboard callbacks for detection and SetInputBlocked for native propagation blocking:

BlockableInputCallbacks::SetInputBlocked(BlockableInputCallbacks::KeyboardInput::ArrowUp, true);

void OnKeyPress(bool down, VirtualKey key) {
    if (key != VirtualKey::Up) return;
    doStuff();
    // no need to return InputBlocking
}

VirtualKeyToKeyboardInput maps Openplanet key events to the native DirectInput scan-code enum used by the blocking hook. KeyboardInputToVirtualKey provides the reverse mapping.

Use PadInput::None or KeyboardInput::None for disabled settings.

Demo plugin: https://github.com/achepta/BlockableInputCallbacksDemo

About

Shared Openplanet plugin that owns native pad input hooks and dispatches registered callbacks that can block propagation.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages