Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
👼Script: Added enum keyCodes and isKeyDown*() functions.
Browse files Browse the repository at this point in the history
2 new example scripts.
also new script func `setEventStatusSupressed(keyCodes keycode, bool supressed)`
Updated and formatted docs.
Fixed group formatting in InputEngine.h
ohlidalp committed Oct 29, 2023
1 parent 136fe77 commit a680797
Showing 7 changed files with 670 additions and 352 deletions.
82 changes: 57 additions & 25 deletions doc/angelscript/Script2Game/InputEngineClass.h
Original file line number Diff line number Diff line change
@@ -17,31 +17,63 @@ class InputEngineClass
{
public:

/**
* Set a permanent (you must also clear it!) override for an input event; Value can be between -1 and 1; Don't forget to reset back to 0!
*/
void setEventSimulatedValue(inputEvents ev, float val);

/**
* @return full configuration string for given command, including the EXPL modifier.
*/
string getEventCommand(inputEvents ev);

/**
* @return configuration string for given command, omitting the EXPL modifier.
*/
string getEventCommandTrimmed(inputEvents ev);

/**
* @return true if the event input is active (keyboard key pressed etc.).
*/
bool getEventBoolValue(inputEvents ev);

/**
* @return true if the event input is active (keyboard key pressed etc.)
* and the bouncing on/off cycle is in 'on' state.
*/
bool getEventBoolValueBounce(inputEvents ev, float time=0.2);
/// @name Input processing
/// @{

/**
* Set a permanent (you must also clear it!) override for an input event; Value can be between -1 and 1; Don't forget to reset back to 0!
*/
void setEventSimulatedValue(inputEvents ev, float val);

/// @}

/// @name Event info
/// @{

/**
* @return full configuration string for given command, including the EXPL modifier.
*/
string getEventCommand(inputEvents ev);

/**
* @return configuration string for given command, omitting the EXPL modifier.
*/
string getEventCommandTrimmed(inputEvents ev);

/// @}

/// @name Event states
/// @{

/**
* @return true if the event input is active (keyboard key pressed etc.).
*/
bool getEventBoolValue(inputEvents ev);

/**
* @return true if the event input is active (keyboard key pressed etc.)
* and the bouncing on/off cycle is in 'on' state.
*/
bool getEventBoolValueBounce(inputEvents ev, float time=0.2);

/**
* Tells if the game recognizes the key as pressed (not blocked by game state or obscured by GUI).
*/
bool isKeyDownEffective(keyCodes keycode);

/**
* Get the safe "was key pressed?" value, optionally specifying the repeat ('bounce') interval.
*/
bool isKeyDownValueBounce(keyCodes keycode, float time = 0.2f);

/// @}

/// @name Direct input device states
/// @{

bool isKeyDown(keyCodes keycode);

/// @}

};

92 changes: 92 additions & 0 deletions doc/angelscript/Script2Game/globals.h
Original file line number Diff line number Diff line change
@@ -413,6 +413,98 @@ enum inputEvents
EV_TRUCKEDIT_RELOAD,
}

enum keyCodes
{
// PLEASE maintain the same order as in 'InputEngine.h' and 'InputEngineAngelscript.cpp'

// Numpad
KC_NUMPAD1,
KC_NUMPAD2,
KC_NUMPAD3,
KC_NUMPAD4,
KC_NUMPAD5,
KC_NUMPAD6,
KC_NUMPAD7,
KC_NUMPAD8,
KC_NUMPAD9,
KC_NUMPAD0,

// Number keys (not the numpad)
KC_1,
KC_2,
KC_3,
KC_4,
KC_5,
KC_6,
KC_7,
KC_8,
KC_9,
KC_0,

// Function keys
KC_F1 ,
KC_F2 ,
KC_F3 ,
KC_F4 ,
KC_F5 ,
KC_F6 ,
KC_F7 ,
KC_F8 ,
KC_F9 ,
KC_F10,
KC_F11,
KC_F12,

// Edit keys
KC_INSERT,
KC_DELETE,
KC_BACKSPACE,
KC_CAPSLOCK,
KC_NUMLOCK,
KC_SCROLLLOCK,
KC_TAB,

// Navigation keys
KC_ESCAPE,
KC_RETURN,
KC_LEFT ,
KC_RIGHT ,
KC_HOME ,
KC_UP ,
KC_PGUP ,
KC_END ,
KC_DOWN ,
KC_PGDOWN,
KC_PAUSE ,

// Modifiers
KC_LCTRL ,
KC_RCTRL ,
KC_LSHIFT,
KC_RSHIFT,
KC_LALT ,
KC_RALT ,
KC_LWIN ,
KC_RWIN ,

// Special characters
KC_MINUS ,
KC_EQUALS ,
KC_LBRACKET ,
KC_RBRACKET ,
KC_SEMICOLON ,
KC_APOSTROPHE,
KC_GRAVE ,
KC_BACKSLASH ,
KC_COMMA ,
KC_PERIOD ,
KC_SLASH ,
KC_MULTIPLY ,
KC_SPACE ,
KC_SUBTRACT ,
KC_ADD ,
}

/**
* Binding of RoR::ActorType
*/
34 changes: 34 additions & 0 deletions resources/scripts/example_keyCodes.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// EXAMPLE - querying key states and events
// ===================================================

string keyEventLog; // keys pressed history
string keyEventSeparator;
int HISTORY_LEN = 100;

void showKeyState(keyCodes kc, string desc)
{
// Draw the immediate state
ImGui::Text(desc); ImGui::SameLine(); ImGui::Text(""+inputs.isKeyDownEffective(kc));

// remember the keypress event
if (inputs.isKeyDownValueBounce(kc)) {
keyEventLog = desc + keyEventSeparator + keyEventLog;
keyEventSeparator = ", ";
// History len cap
if (keyEventLog.length() > HISTORY_LEN) { keyEventLog.resize(HISTORY_LEN); }
}
}

void frameStep(float dt)
{
ImGui::TextDisabled("===[ K E Y S T A T E S : ]===");

showKeyState(KC_F1, "F1"); ImGui::SameLine(); ImGui::Dummy(vector2(10, 1));
showKeyState(KC_F2, "F2"); ImGui::SameLine(); ImGui::Dummy(vector2(10, 1));
showKeyState(KC_F3, "F3"); ImGui::SameLine(); ImGui::Dummy(vector2(10, 1));
showKeyState(KC_F4, "F4");

ImGui::TextDisabled("===[ K E Y H I S T O R Y : ]===");

ImGui::Text(keyEventLog);
}
34 changes: 34 additions & 0 deletions resources/scripts/example_setEventSimulatedValue.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// EXAMPLE - how to use
// ===================================================

void drawInputBtn(inputEvents ev, string caption)
{
ImGui::Button(caption);
if (ImGui::IsItemActive()) // Is button currently pressed?
{ inputs.setEventSimulatedValue(ev, 1.f); }
else
{ inputs.setEventSimulatedValue(ev, 0.f); } // IMPORTANT! The value is persistent, we must also reset it.
}

// `frameStep()` runs every frame; `dt` is delta time in seconds.
void frameStep(float dt)
{
if (@game.getCurrentTruck() == null)
{
ImGui::TextDisabled("Character controls:");
drawInputBtn(EV_CHARACTER_FORWARD, " ^ ");
drawInputBtn(EV_CHARACTER_LEFT, "<");
ImGui::SameLine();
drawInputBtn(EV_CHARACTER_RIGHT, ">");
drawInputBtn(EV_CHARACTER_BACKWARDS, " v ");
}
else
{
ImGui::TextDisabled("Vehicle controls:");
drawInputBtn(EV_TRUCK_ACCELERATE, " ^ ");
drawInputBtn(EV_TRUCK_STEER_LEFT, "<");
ImGui::SameLine();
drawInputBtn(EV_TRUCK_STEER_RIGHT, ">");
drawInputBtn(EV_TRUCK_BRAKE, " v ");
}
}
Loading

0 comments on commit a680797

Please sign in to comment.