Page 1 of 1

Input, a script to capture key and mouse input

Posted: Sat Mar 12, 2011 5:18 am
by Master Jademus Sreg
I post this for any galaxy scripters here. This script captures all the input from keys, mouse button clicks, and mouse movement, which allows the user to retrieve that information from the playerInput struct array without the use of any additional input catching triggers.

Code: Select all

//=====================================================
// Input, a script to capture key and mouse input
//
// This script will catch and store player input
// from the keyboard and mouse in a struct array.
// Keys and buttons are records as bool states,
// while mouse clicks and movement are kept as
// int and fixed values for the UI and World
// respectively.
//
// To get the state of the X key, you would use:
// playerInput[p].key.state[c_keyX]
// To get the point of the cursor, you could:
// Point(playerInput[p].mouse.x,playerInput[p].mouse.y)
//
// The user may configure the DETECT bool
// constants to set which triggers will be
// created on initialization of the script.
//=====================================================
// CONFIGURABLE CONSTANTS
const bool DETECT_KEYDOWN = true;
const bool DETECT_KEYUP = true;
const bool DETECT_BUTTONDOWN = true;
const bool DETECT_BUTTONUP = true;
const bool DETECT_MOVEMENT = true;
const int INPUT_BUTTONS = 6;
const int INPUT_KEYS = 99;
const int MAX_PLAYERS = 16;
// STRUCTS
struct Button { bool[INPUT_BUTTONS] state; };
struct Key { bool[INPUT_KEYS] state; };
struct Mouse {
    int uix;
    int uiy;
    fixed x;
    fixed y;
    fixed z;
};
struct Input {
    Key key;
    Button button;
    Mouse mouse;
};
// GLOBALS
Input[MAX_PLAYERS] playerInput;
// FUNCTIONS
bool Input_KeyOn (bool testConds, bool runActions) {
    playerInput[EventPlayer()].key.state[EventKeyPressed()] = true;
    return true;
}
bool Input_KeyOff (bool testConds, bool runActions) {
    playerInput[EventPlayer()].key.state[EventKeyPressed()] = false;
    return true;
}
bool Input_ButtonOn (bool testConds, bool runActions) {
    int p = EventPlayer();
    playerInput[p].button.state[EventMouseClickedButton()] = true;
    playerInput[p].mouse.uix = EventMouseClickedPosXUI();
    playerInput[p].mouse.uiy = EventMouseClickedPosYUI();
    playerInput[p].mouse.x = EventMouseClickedPosXWorld();
    playerInput[p].mouse.y = EventMouseClickedPosYWorld();
    playerInput[p].mouse.z = EventMouseClickedPosZWorld();
    return true;
}
bool Input_ButtonOff (bool testConds, bool runActions) {
    int p = EventPlayer();
    playerInput[p].button.state[EventMouseClickedButton()] = false;
    playerInput[p].mouse.uix = EventMouseClickedPosXUI();
    playerInput[p].mouse.uiy = EventMouseClickedPosYUI();
    playerInput[p].mouse.x = EventMouseClickedPosXWorld();
    playerInput[p].mouse.y = EventMouseClickedPosYWorld();
    playerInput[p].mouse.z = EventMouseClickedPosZWorld();
    return true;
}
bool Input_Movement (bool testConds, bool runActions) {
    int p = EventPlayer();
    playerInput[p].mouse.uix = EventMouseMovedPosXUI();
    playerInput[p].mouse.uiy = EventMouseMovedPosYUI();
    playerInput[p].mouse.x = EventMouseMovedPosXWorld();
    playerInput[p].mouse.y = EventMouseMovedPosYWorld();
    playerInput[p].mouse.z = EventMouseMovedPosZWorld();
    return true;
}
void Input_Init () {
    if (DETECT_KEYDOWN) {
        TriggerAddEventKeyPressed(TriggerCreate("Input_KeyOn"),c_playerAny,c_keyNone,true,0,0,0);
    }
    if (DETECT_KEYUP) {
        TriggerAddEventKeyPressed(TriggerCreate("Input_KeyOff"),c_playerAny,c_keyNone,false,0,0,0);
    }
    if (DETECT_BUTTONDOWN) {
        TriggerAddEventMouseClicked(TriggerCreate("Input_ButtonOn"),c_playerAny,c_mouseButtonNone,true);
    }
    if (DETECT_BUTTONUP) {
        TriggerAddEventMouseClicked(TriggerCreate("Input_ButtonOff"),c_playerAny,c_mouseButtonNone,false);
    }
    if (DETECT_MOVEMENT) {
        TriggerAddEventMouseMoved(TriggerCreate("Input_Movement"), c_playerAny);
    }