Skip to content

Commit

Permalink
AngelScript: added supporting API for ImGui drawing.
Browse files Browse the repository at this point in the history
  • Loading branch information
ohlidalp committed Aug 13, 2022
1 parent 0411bbd commit 59cec94
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 25 deletions.
69 changes: 44 additions & 25 deletions doc/angelscript/Script2Game/GameScriptClass.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ namespace Script2Game {

/** \addtogroup ScriptSideAPIs
* @{
*/
*/

/** \addtogroup Script2Game
* @{
*/
*/

/**
* @brief Binding of RoR::GameScript; A general class that will provide you with general functions.
Expand Down Expand Up @@ -63,7 +63,7 @@ class GameScriptClass
/**
* Sends or request information from the master server
*/
int useOnlineAPI(const string apiquery, const dictionary dict, string result);
int useOnlineAPI(const string apiquery, const dictionary dict, string result);

/**
* Gets the Curent frames per second (FPS)
Expand All @@ -75,7 +75,7 @@ class GameScriptClass
* Gets the average frames per second (FPS)
* @return The average FPS
*/
void getAvgFPS();
void getAvgFPS();

/**
* Back to menu
Expand All @@ -85,13 +85,13 @@ class GameScriptClass
/**
* Quits the game
*/
void quitGame();
void quitGame();

/// @}

/// @name GUI
/// @{

/**
* shows a message to the user
* @deprecated Use the game.message function instead.
Expand All @@ -109,7 +109,7 @@ class GameScriptClass
* @param forceVisible Set this to true if you want the message to be forced on the user's screen (~it will show, even when the GUI is hidden).
*/
void message(string txt, string icon, float timeMilliseconds, bool forceVisible);

/**
* OBSOLETE - returns 0.
* @deprecated
Expand All @@ -122,8 +122,8 @@ class GameScriptClass
* @deprecated
* @param size font size in pixels
*/
void setChatFontSize(int size);
void setChatFontSize(int size);

/**
* Shows a message box
*
Expand Down Expand Up @@ -156,7 +156,7 @@ class GameScriptClass
* @param box the name of the box in which the truck will be spawned
*/
void showChooser(string type, string instance, string box);

/**
* set direction arrow
* @param text text to be displayed. "" to hide the text
Expand All @@ -169,13 +169,25 @@ class GameScriptClass
* Hides the direction arrow
* @see UpdateDirectionArrow
*/
void hideDirectionArrow();
void hideDirectionArrow();

/**
* @param world_pos The world position to be converted, in meters.
* @param out_screen_pos The resulting screen position, in pixels.
* @return true if the world position is in front of the camera and the resulting screen position is valid.
*/
bool getScreenPosFromWorldPos(const vector3&in, vector2&out);

/**
* Gets screen size in pixels.
*/
vector2 getDisplaySize();

/// @}

/// @name Script management
/// @{

/**
* registers for a new event to be received by the scripting system
* @param eventValue \see enum scriptEvents
Expand All @@ -186,7 +198,7 @@ class GameScriptClass
* unregisters from receiving event.
* @param eventValue \see enum scriptEvents
*/
void unRegisterEvent(int eventValue);
void unRegisterEvent(int eventValue);

/**
* Adds a global function to the script.
Expand Down Expand Up @@ -216,12 +228,12 @@ class GameScriptClass
* Removes a global variable from the script.
* @param var the declaration of the variable that should be removed, e.g.: "int missionState;"
*/
int deleteScriptVariable(const string var);
int deleteScriptVariable(const string var);

/**
* Clears the event cache
*/
void clearEventCache();
void clearEventCache();

/// @}

Expand All @@ -248,13 +260,13 @@ class GameScriptClass
/**
* Gets the currently loaded terrain instance
*/
TerrainClass@ getTerrain();
TerrainClass@ getTerrain();

/**
* Checks if Caleum is enabled.
* @return true if Caleum is available
*/
bool getCaelumAvailable();
bool getCaelumAvailable();

/**
* gets the time of the day in seconds
Expand All @@ -266,7 +278,7 @@ class GameScriptClass
* sets the time of the day in seconds
* @param value day time in seconds
*/
void setCaelumTime(float value);
void setCaelumTime(float value);

/**
* returns the currently set upo gravity
Expand All @@ -278,7 +290,7 @@ class GameScriptClass
* sets the gravity terrain wide. This is an expensive call, since the masses of all trucks are recalculated.
* @param value new gravity terrain wide (default is -9.81)
*/
void setGravity(float value);
void setGravity(float value);

/**
* returns the current base water level (without waves)
Expand All @@ -297,7 +309,7 @@ class GameScriptClass
* sets the base water height
* @param value base height in meters
*/
void setWaterHeight(float value);
void setWaterHeight(float value);

/**
* This spawns an object
Expand All @@ -323,7 +335,14 @@ class GameScriptClass
* @param instanceName The unique name that you chose when spawning this object
* @see spawnObject
*/
void destroyObject(const string instanceName);
void destroyObject(const string instanceName);

/**
* Calculates mouse cursor position on terrain.
* @param out_pos Calculated position, in meters.
* @return true if mouse points to the terrain and output coordinates are valid.
*/
bool getMousePositionOnTerrain(vector3 &out);

/// @}

Expand All @@ -334,7 +353,7 @@ class GameScriptClass
* Returns the current position of the person
* @return A vector containing the X, Y and Z coordinate of the person or an empty vector if the user is in a truck
*/
vector3 getPersonPosition();
vector3 getPersonPosition();

/**
* sets the character position
Expand Down Expand Up @@ -394,7 +413,7 @@ class GameScriptClass
* @param rot The rotation in which the truck should be spawned
* @return reference to Beam object
*/
BeamClass @spawnTruck(stringtruckName, vector3 pos, vector3 rot);
BeamClass @spawnTruck(stringtruckName, vector3 pos, vector3 rot);

/**
* This method repairs the vehicle in the box
Expand All @@ -409,13 +428,13 @@ class GameScriptClass
/**
* Number of trucks with flag
*/
int getNumTrucksByFlag(int flag);
int getNumTrucksByFlag(int flag);

/**
* Gives the currently used truck a boost in RPM.
* @param factor This factor determines by how much that the RPM of the truck will be increased ( rpm += 2000.0f * factor ).
*/
void boostCurrentTruck(float factor);
void boostCurrentTruck(float factor);

///@}

Expand Down
37 changes: 37 additions & 0 deletions source/main/scripting/GameScript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#include "ScriptEngine.h"
#include "SkyManager.h"
#include "Terrain.h"
#include "TerrainGeometryManager.h"
#include "TerrainObjectManager.h"
#include "Utils.h"
#include "Water.h"
Expand Down Expand Up @@ -454,6 +455,27 @@ void GameScript::hideDirectionArrow()
App::GetGameContext()->GetRaceSystem().UpdateDirectionArrow(0, Vector3::ZERO);
}

bool GameScript::getScreenPosFromWorldPos(Ogre::Vector3 const& world_pos, Ogre::Vector2& out_screen)
{
ImVec2 screen_size = ImGui::GetIO().DisplaySize;
World2ScreenConverter world2screen(
App::GetCameraManager()->GetCamera()->getViewMatrix(true), App::GetCameraManager()->GetCamera()->getProjectionMatrix(), Ogre::Vector2(screen_size.x, screen_size.y));
Ogre::Vector3 pos_xyz = world2screen.Convert(world_pos);
if (pos_xyz.z < 0.f)
{
out_screen.x = pos_xyz.x;
out_screen.y = pos_xyz.y;
return true;
}
return false;
}

Ogre::Vector2 GameScript::getDisplaySize()
{
ImVec2 size = ImGui::GetIO().DisplaySize;
return Vector2(size.x, size.y);
}

int GameScript::setMaterialAmbient(const String& materialName, float red, float green, float blue)
{
try
Expand Down Expand Up @@ -949,6 +971,21 @@ float GameScript::getAvgFPS()
return App::GetAppContext()->GetRenderWindow()->getStatistics().avgFPS;
}

bool GameScript::getMousePositionOnTerrain(Ogre::Vector3& out_pos)
{
if (!HaveSimTerrain(__FUNCTION__))
return false;

Ogre::Vector2 mouse_npos = App::GetInputEngine()->getMouseNormalizedScreenPos();
Ogre::Ray ray = App::GetCameraManager()->GetCamera()->getCameraToViewportRay(mouse_npos.x, mouse_npos.y);
Ogre::TerrainGroup::RayResult ray_result = App::GetSimTerrain()->getGeometryManager()->getTerrainGroup()->rayIntersects(ray);
if (ray_result.hit)
{
out_pos = ray_result.position;
}
return ray_result.hit;
}

bool GameScript::HaveSimTerrain(const char* func_name)
{
if (App::GetSimTerrain() == nullptr)
Expand Down
19 changes: 19 additions & 0 deletions source/main/scripting/GameScript.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,18 @@ class GameScript : public ZeroedMemoryAllocator

void hideDirectionArrow();

/**
* @param world_pos The world position to be converted, in meters.
* @param out_screen_pos The resulting screen position, in pixels.
* @return true if the world position is in front of the camera and the resulting screen position is valid.
*/
bool getScreenPosFromWorldPos(Ogre::Vector3 const& world_pos, Ogre::Vector2& out_screen_pos);

/**
* Gets screen size in pixels.
*/
Ogre::Vector2 getDisplaySize();

/// @}

/// @name Script management
Expand Down Expand Up @@ -247,6 +259,13 @@ class GameScript : public ZeroedMemoryAllocator
*/
void destroyObject(const Ogre::String& instanceName);

/**
* Calculates mouse cursor position on terrain.
* @param out_pos Calculated position, in meters.
* @return true if mouse points to the terrain and output coordinates are valid.
*/
bool getMousePositionOnTerrain(Ogre::Vector3& out_pos);

/// @}

/// @name Character
Expand Down
3 changes: 3 additions & 0 deletions source/main/scripting/bindings/GameScriptAngelscript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ void RoR::RegisterGameScript(AngelScript::asIScriptEngine *engine)
result = engine->RegisterObjectMethod("GameScriptClass", "void showChooser(const string &in, const string &in, const string &in)", AngelScript::asMETHOD(GameScript, showChooser), AngelScript::asCALL_THISCALL); ROR_ASSERT(result >= 0);
result = engine->RegisterObjectMethod("GameScriptClass", "void updateDirectionArrow(const string &in, vector3 &in)", AngelScript::asMETHOD(GameScript, updateDirectionArrow), AngelScript::asCALL_THISCALL); ROR_ASSERT(result >= 0);
result = engine->RegisterObjectMethod("GameScriptClass", "void hideDirectionArrow()", AngelScript::asMETHOD(GameScript, hideDirectionArrow), AngelScript::asCALL_THISCALL); ROR_ASSERT(result >= 0);
result = engine->RegisterObjectMethod("GameScriptClass", "bool getScreenPosFromWorldPos(const vector3&in, vector2&out)", AngelScript::asMETHOD(GameScript, getScreenPosFromWorldPos), AngelScript::asCALL_THISCALL); ROR_ASSERT(result >= 0);
result = engine->RegisterObjectMethod("GameScriptClass", "vector2 getDisplaySize()", AngelScript::asMETHOD(GameScript, getDisplaySize), AngelScript::asCALL_THISCALL); ROR_ASSERT(result >= 0);

// > Script management
result = engine->RegisterObjectMethod("GameScriptClass", "void registerForEvent(int)", AngelScript::asMETHOD(GameScript, registerForEvent), AngelScript::asCALL_THISCALL); ROR_ASSERT(result >= 0);
Expand Down Expand Up @@ -78,6 +80,7 @@ void RoR::RegisterGameScript(AngelScript::asIScriptEngine *engine)
result = engine->RegisterObjectMethod("GameScriptClass", "void spawnObject(const string &in, const string &in, vector3 &in, vector3 &in, const string &in, bool)", AngelScript::asMETHOD(GameScript, spawnObject), AngelScript::asCALL_THISCALL); ROR_ASSERT(result >= 0);
result = engine->RegisterObjectMethod("GameScriptClass", "void moveObjectVisuals(const string &in, vector3 &in)", AngelScript::asMETHOD(GameScript, moveObjectVisuals), AngelScript::asCALL_THISCALL); ROR_ASSERT(result >= 0);
result = engine->RegisterObjectMethod("GameScriptClass", "void destroyObject(const string &in)", AngelScript::asMETHOD(GameScript, destroyObject), AngelScript::asCALL_THISCALL); ROR_ASSERT(result >= 0);
result = engine->RegisterObjectMethod("GameScriptClass", "bool getMousePositionOnTerrain(vector3 &out)", AngelScript::asMETHOD(GameScript, getMousePositionOnTerrain), AngelScript::asCALL_THISCALL); ROR_ASSERT(result >= 0);

// > Character
result = engine->RegisterObjectMethod("GameScriptClass", "vector3 getPersonPosition()", AngelScript::asMETHOD(GameScript, getPersonPosition), AngelScript::asCALL_THISCALL); ROR_ASSERT(result >= 0);
Expand Down
2 changes: 2 additions & 0 deletions source/main/scripting/bindings/OgreAngelscript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ void registerOgreRadian(AngelScript::asIScriptEngine* engine);
void registerOgreDegree(AngelScript::asIScriptEngine* engine);
void registerOgreQuaternion(AngelScript::asIScriptEngine* engine);
void registerOgreTexture(AngelScript::asIScriptEngine* engine);
void registerOgreColourValue(AngelScript::asIScriptEngine* engine);

// main registration method
void RoR::RegisterOgreObjects(AngelScript::asIScriptEngine* engine)
Expand Down Expand Up @@ -261,6 +262,7 @@ void RoR::RegisterOgreObjects(AngelScript::asIScriptEngine* engine)
registerOgreVector2(engine);
registerOgreQuaternion(engine);
registerOgreTexture(engine);
registerOgreColourValue(engine);
}

// register Ogre::Vector3
Expand Down
9 changes: 9 additions & 0 deletions source/main/utils/InputEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1622,6 +1622,15 @@ int InputEngine::getCurrentPovValue(int& joystickNumber, int& pov, int& povdir)
return 0;
}

Ogre::Vector2 InputEngine::getMouseNormalizedScreenPos()
{
OIS::MouseState const& mstate = mMouse->getMouseState();
Ogre::Vector2 res;
res.x = static_cast<float>(mstate.X.abs) / static_cast<float>(mstate.width);
res.y = static_cast<float>(mstate.Y.abs) / static_cast<float>(mstate.height);
return res;
}

event_trigger_t InputEngine::newEvent()
{
event_trigger_t res;
Expand Down
1 change: 1 addition & 0 deletions source/main/utils/InputEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@ class InputEngine : public ZeroedMemoryAllocator
int getCurrentKeyCombo(Ogre::String* combo); //!< Returns number of non-modifier keys pressed (or modifier count as negative number).
int getCurrentJoyButton(int& joystickNumber, int& button);
int getCurrentPovValue(int& joystickNumber, int& pov, int& povdir);
Ogre::Vector2 getMouseNormalizedScreenPos(); //!< Returns XY position in range from 0 (top/left) to 1 (bottom/right)

// Event utils

Expand Down

0 comments on commit 59cec94

Please sign in to comment.