Skip to content

Commit

Permalink
🧩 Scripting: Added global object 'inputs' (InputEngine) and enum 'inp…
Browse files Browse the repository at this point in the history
…utEvents'.

Enum inputEvents contains all values of enum `events` in InputEngine.h.
Object InputEngineClass contains only a few important functions of InputEngine, see InputEngineAngelscript.cpp for listing.
InputEngine.h: just formatted the enum comments, no code change.

Demo script extended to showcase new abilities.
Added doxygen doc.
  • Loading branch information
ohlidalp committed Jan 13, 2022
1 parent 37e7205 commit 8852766
Show file tree
Hide file tree
Showing 9 changed files with 1,045 additions and 267 deletions.
31 changes: 31 additions & 0 deletions doc/angelscript/InputEngineClass.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@


/**
* @brief Manages input devices, their configuration (input.map ...) and state.
* @note This object is created automatically as global variable `inputs`.
*/
class InputEngineClass
{
public:
/**
* @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);

};
318 changes: 318 additions & 0 deletions doc/angelscript/global_functions.h

Large diffs are not rendered by default.

58 changes: 54 additions & 4 deletions resources/scripts/demo_script.as
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ float g_total_seconds = 0;
CVarClass@ g_app_state = console.cVarFind("app_state"); // 0=bootstrap, 1=main menu, 2=simulation, see AppState in Application.h
CVarClass@ g_sim_state = console.cVarFind("sim_state"); // 0=off, 1=running, 2=paused, 3=terrain editor, see SimState in Application.h
CVarClass@ g_mp_state = console.cVarFind("mp_state"); // 0=disabled, 1=connecting, 2=connected, see MpState in Application.h
CVarClass@ g_io_arcade_controls = console.cVarFind("io_arcade_controls"); // bool

/*
---------------------------------------------------------------------------
Expand All @@ -55,7 +56,7 @@ void main()
void frameStep(float dt)
{
// Open demo window
ImGui::SetNextWindowSize(vector2(300, 150));
ImGui::SetNextWindowSize(vector2(400, 320));
ImGui::Begin("Demo Script", /*open:*/true, /*flags:*/0);

// show some stats
Expand All @@ -64,11 +65,14 @@ void frameStep(float dt)
+ int(g_total_seconds % 60) + "sec");

// Show some game context
if (g_app_state.getInt() == 1)
if (g_app_state.getInt() == 1) // main menu
{
ImGui::Text("Game state: main menu");
ImGui::Text("Pro tip: Press '"
+ inputs.getEventCommandTrimmed(EV_COMMON_CONSOLE_TOGGLE)
+ "' to open console anytime.");
}
else
else if (g_app_state.getInt() == 2) // simulation
{
if (g_mp_state.getInt() == 2)
{
Expand All @@ -90,15 +94,61 @@ void frameStep(float dt)
ImGui::Text("(terrain edit)");
}

ImGui::TextDisabled("Camera controls:");
ImGui::Text("Change camera: " + inputs.getEventCommandTrimmed(EV_CAMERA_CHANGE));
ImGui::Text("Toggle free camera: " + inputs.getEventCommandTrimmed(EV_CAMERA_FREE_MODE));
ImGui::Text("Toggle fixed camera: " + inputs.getEventCommandTrimmed(EV_CAMERA_FREE_MODE_FIX));

BeamClass@ actor = game.getCurrentTruck();
if (actor != null)
{
ImGui::Text("You are driving " + actor.getTruckName());
ImGui::TextDisabled("Vehicle controls:");

ImGui::Text("Accelerate/Brake: "
+ inputs.getEventCommandTrimmed(EV_TRUCK_ACCELERATE) + "/"
+ inputs.getEventCommandTrimmed(EV_TRUCK_BRAKE));
if (g_io_arcade_controls.getBool() == true)
{
ImGui::Text("Arcade controls are enabled (?)");
if (ImGui::IsItemHovered())
{
ImGui::BeginTooltip();
ImGui::Text("'brake' key also accelerates in reverse.");
ImGui::Text("You can change the setting in main menu / settings / gameplay.");
ImGui::EndTooltip();
}
}
else
{
ImGui::Text("Arcade controls are disabled (?)");
if (ImGui::IsItemHovered())
{
ImGui::BeginTooltip();
ImGui::Text("'brake' key only brakes, to accelerate in reverse use 'accelerate' key.");
ImGui::Text("You can change the setting in main menu / settings / gameplay.");
ImGui::EndTooltip();
}
}

ImGui::Text("Steer left/right: "
+ inputs.getEventCommandTrimmed(EV_TRUCK_STEER_LEFT) + "/"
+ inputs.getEventCommandTrimmed(EV_TRUCK_STEER_RIGHT));

}
else
{
ImGui::Text("You are on foot");
}
ImGui::TextDisabled("Character controls:");
ImGui::Text("Forward/Backward: "
+ inputs.getEventCommandTrimmed(EV_CHARACTER_FORWARD) + "/"
+ inputs.getEventCommandTrimmed(EV_CHARACTER_BACKWARDS));
ImGui::Text("Turn left/right: "
+ inputs.getEventCommandTrimmed(EV_CHARACTER_LEFT) + "/"
+ inputs.getEventCommandTrimmed(EV_CHARACTER_RIGHT));
ImGui::Text("Run: " + inputs.getEventCommandTrimmed(EV_CHARACTER_RUN));
}

}

// End window
Expand Down
1 change: 1 addition & 0 deletions source/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ if (USE_ANGELSCRIPT)
scripting/LocalStorage.{h,cpp}
scripting/OgreAngelscript.cpp
scripting/ImGuiAngelscript.cpp
scripting/InputEngineAngelscript.cpp
scripting/OgreScriptBuilder.{h,cpp}
scripting/ScriptEngine.{h,cpp}
)
Expand Down
4 changes: 2 additions & 2 deletions source/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,12 @@ int main(int argc, char *argv[])

App::GetDiscordRpc()->Init();

App::GetAppContext()->SetUpInput();

#ifdef USE_ANGELSCRIPT
App::CreateScriptEngine();
#endif

App::GetAppContext()->SetUpInput();

App::GetGuiManager()->SetUpMenuWallpaper();

// Add "this is obsolete" marker file to old config location
Expand Down
Loading

0 comments on commit 8852766

Please # to comment.