Skip to content

Commit

Permalink
👼Script: added setEventSimulatedValue() ⌨️
Browse files Browse the repository at this point in the history
  • Loading branch information
ohlidalp committed Oct 29, 2023
1 parent c0e86ff commit 136fe77
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
6 changes: 6 additions & 0 deletions doc/angelscript/Script2Game/InputEngineClass.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ namespace Script2Game {
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.
*/
Expand Down
5 changes: 5 additions & 0 deletions source/main/scripting/bindings/InputEngineAngelscript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,19 @@ void registerInputEngineObject(asIScriptEngine* engine)
int result = 0;

result = engine->RegisterObjectType("InputEngineClass", sizeof(InputEngine), asOBJ_REF | asOBJ_NOCOUNT); ROR_ASSERT(result>=0);

// > Input processing
result = engine->RegisterObjectMethod("InputEngineClass", "void setEventSimulatedValue(inputEvents ev, float value)", asMETHOD(InputEngine,setEventSimulatedValue), asCALL_THISCALL); ROR_ASSERT(result>=0);

// > Event info
result = engine->RegisterObjectMethod("InputEngineClass", "string getEventCommand(inputEvents ev)", asMETHOD(InputEngine,getEventCommand), asCALL_THISCALL); ROR_ASSERT(result>=0);
result = engine->RegisterObjectMethod("InputEngineClass", "string getEventCommandTrimmed(inputEvents ev)", asMETHOD(InputEngine,getEventCommandTrimmed), asCALL_THISCALL); ROR_ASSERT(result>=0);

// TODO: register OIS::KeyCode or port to SDL2 already (for Apple OSX support)
// result = engine->RegisterObjectMethod("InputEngineClass", "bool isKeyDown(int keycode)", asMETHOD(InputEngine,isKeyDown), asCALL_THISCALL); ROR_ASSERT(result>=0);
// result = engine->RegisterObjectMethod("InputEngineClass", "bool isKeyDownEffective(int keycode)", asMETHOD(InputEngine,isKeyDownEffective), asCALL_THISCALL); ROR_ASSERT(result>=0);

// > Event states
result = engine->RegisterObjectMethod("InputEngineClass", "bool getEventBoolValue(inputEvents ev)", asMETHOD(InputEngine,getEventBoolValue), asCALL_THISCALL); ROR_ASSERT(result>=0);
result = engine->RegisterObjectMethod("InputEngineClass", "bool getEventBoolValueBounce(inputEvents ev, float time = 0.2f)", asMETHOD(InputEngine,getEventBoolValueBounce), asCALL_THISCALL); ROR_ASSERT(result>=0);
}
Expand Down
20 changes: 17 additions & 3 deletions source/main/utils/InputEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,11 @@ void InputEngine::resetKeys()
}
}

void InputEngine::setEventSimulatedValue(RoR::events eventID, float value)
{
event_values_simulated[eventID] = value;
}

bool InputEngine::getEventBoolValue(int eventID)
{
return (getEventValue(eventID) > 0.5f);
Expand Down Expand Up @@ -900,6 +905,10 @@ bool InputEngine::isEventAnalog(int eventID)

float InputEngine::getEventValue(int eventID, bool pure, InputSourceType valueSource /*= InputSourceType::IST_ANY*/)
{
const float simulatedValue = event_values_simulated[eventID];
if (simulatedValue != 0.f)
return simulatedValue;

float returnValue = 0;
std::vector<event_trigger_t> t_vec = events[eventID];
float value = 0;
Expand Down Expand Up @@ -1174,6 +1183,12 @@ const char* InputEngine::getEventTypeName(eventtypes type)
}

void InputEngine::addEvent(int eventID, event_trigger_t& t)
{
this->addEvent(eventID);
events[eventID].push_back(t);
}

void InputEngine::addEvent(int eventID)
{
uniqueCounter++;

Expand All @@ -1183,9 +1198,8 @@ void InputEngine::addEvent(int eventID, event_trigger_t& t)
if (events.find(eventID) == events.end())
{
events[eventID] = std::vector<event_trigger_t>();
events[eventID].clear();
event_values_simulated[eventID] = false;
}
events[eventID].push_back(t);
}

void InputEngine::addEventDefault(int eventID, int deviceID /*= -1*/)
Expand Down Expand Up @@ -1495,7 +1509,7 @@ bool InputEngine::processLine(const char* line, int deviceID)
if (eventID == -1)
return false;
// Insert event with no trigger
events.insert(std::make_pair(eventID, std::vector<event_trigger_t>()));
addEvent(eventID);
return true;
}
case ET_MouseButton:
Expand Down
3 changes: 3 additions & 0 deletions source/main/utils/InputEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,7 @@ class InputEngine
void ProcessKeyRelease(const OIS::KeyEvent& arg);
void ProcessJoystickEvent(const OIS::JoyStickEvent& arg);
void resetKeys();
void setEventSimulatedValue(events eventID, float value);

// Event info

Expand Down Expand Up @@ -513,6 +514,7 @@ class InputEngine
// Event management

void addEvent(int eventID, event_trigger_t& t); //!< Registers new trigger for this event.
void addEvent(int eventID); //!< Registers new event without trigger.
void addEventDefault(int eventID, int deviceID = -1); //!< Adds a new trigger with builtin value for this event.
void updateEvent(int eventID, const event_trigger_t& t);
void eraseEvent(int eventID, const event_trigger_t* t);
Expand Down Expand Up @@ -573,6 +575,7 @@ class InputEngine

// define event aliases
std::map<int, std::vector<event_trigger_t>> events;
std::map<int, float> event_values_simulated;
std::map<int, float> event_times;
std::string m_loaded_configs[MAX_JOYSTICKS];
bool loadMapping(Ogre::String fileName, int deviceID);
Expand Down

0 comments on commit 136fe77

Please sign in to comment.