Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Added DearIMGUI bindings to AngelScript #2786

Merged
merged 4 commits into from
Aug 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions doc/angelscript/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ class game
* @param eventValue \see enum scriptEvents
*/
void registerForEvent(int eventValue);

/**
* unregisters from receiving event.
* @param eventValue \see enum scriptEvents
*/
void unRegisterEvent(int eventValue);

/**
* shows a message to the user
Expand Down
3 changes: 2 additions & 1 deletion source/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ if (ROR_USE_ANGELSCRIPT)
list(APPEND SOURCE_FILES
scripting/GameScript.{h,cpp}
scripting/LocalStorage.{h,cpp}
scripting/OgreAngelscript.{h,cpp}
scripting/OgreAngelscript.cpp
scripting/ImGuiAngelscript.cpp
scripting/OgreScriptBuilder.{h,cpp}
scripting/ScriptEngine.{h,cpp}
)
Expand Down
11 changes: 9 additions & 2 deletions source/main/scripting/GameScript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,15 @@ void GameScript::registerForEvent(int eventValue)
{
if (App::GetScriptEngine())
{
App::GetScriptEngine()->eventMask = -1;
App::GetScriptEngine()->eventMask += eventValue;
App::GetScriptEngine()->eventMask |= eventValue;
}
}

void GameScript::unRegisterEvent(int eventValue)
{
if (App::GetScriptEngine())
{
App::GetScriptEngine()->eventMask &= ~eventValue;
}
}

Expand Down
6 changes: 6 additions & 0 deletions source/main/scripting/GameScript.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@ class GameScript : public ZeroedMemoryAllocator
*/
void registerForEvent(int eventValue);

/**
* unregisters from receiving event.
* @param eventValue \see enum scriptEvents
*/
void unRegisterEvent(int eventValue);

/**
* DEPRECATED: use message
* shows a message to the user
Expand Down
447 changes: 447 additions & 0 deletions source/main/scripting/ImGuiAngelscript.cpp

Large diffs are not rendered by default.

249 changes: 245 additions & 4 deletions source/main/scripting/OgreAngelscript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@
/// @author Thomas Fischer
/// @date 31th of July 2009

#include "OgreAngelscript.h"

#include "Application.h"
#include "ScriptEngine.h"

using namespace Ogre;
using namespace AngelScript;
Expand Down Expand Up @@ -53,6 +52,27 @@ static void Vector3InitConstructorScaler(float s, Vector3* self)
new(self) Vector3(s, s, s);
}

/***VECTOR2***/
static void Vector2DefaultConstructor(Vector2* self)
{
new(self) Vector2();
}

static void Vector2CopyConstructor(const Vector2& other, Vector2* self)
{
new(self) Vector2(other);
}

static void Vector2InitConstructor(float x, float y, Vector2* self)
{
new(self) Vector2(x, y);
}

static void Vector2InitConstructorScaler(float s, Vector2* self)
{
new(self) Vector2(s, s);
}

// not used
static int Vector3Cmp(const Vector3& a, const Vector3& b)
{
Expand Down Expand Up @@ -159,8 +179,53 @@ static void QuaternionInitConstructorScaler(float s, Quaternion* self)
new(self) Quaternion(s, s, s, s);
}

/***COLOURVALUE***/
static void ColourValueDefaultConstructor(ColourValue* self)
{
new(self) ColourValue();
}

static void ColourValueInitConstructor(float r, float g, float b, float a, ColourValue* self)
{
new(self) ColourValue(r,g,b,a);
}

static void ColourValueCopyConstructor(const ColourValue& other, ColourValue* self)
{
new(self) ColourValue(other.r, other.g, other.b, other.a);
}

/***TEXTURE***/
static void TexturePtrDefaultConstructor(TexturePtr* self)
{
new (self) TexturePtr();
}

static void TexturePtrCopyConstructor(const TexturePtr& other, TexturePtr* self)
{
new (self) TexturePtr(other);
}

static void TexturePtrDestructor(TexturePtr* self)
{
(self)->~TexturePtr();
}

static void TexturePtrAssignOperator(const TexturePtr& other, TexturePtr* self)
{
(self)->operator=(other);
}

// forward declarations, defined below
void registerOgreVector3(AngelScript::asIScriptEngine* engine);
void registerOgreVector2(AngelScript::asIScriptEngine* engine);
void registerOgreRadian(AngelScript::asIScriptEngine* engine);
void registerOgreDegree(AngelScript::asIScriptEngine* engine);
void registerOgreQuaternion(AngelScript::asIScriptEngine* engine);
void registerOgreTexture(AngelScript::asIScriptEngine* engine);

// main registration method
void registerOgreObjects(AngelScript::asIScriptEngine* engine)
void RoR::RegisterOgreObjects(AngelScript::asIScriptEngine* engine)
{
int r;

Expand All @@ -174,18 +239,28 @@ void registerOgreObjects(AngelScript::asIScriptEngine* engine)
r = engine->RegisterObjectType("radian", sizeof(Radian), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_CLASS_CA | asOBJ_APP_CLASS_ALLFLOATS);
ROR_ASSERT( r >= 0 );

// Ogre::Vector2
r = engine->RegisterObjectType("vector2", sizeof(Vector2), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_CLASS_CA | asOBJ_APP_CLASS_ALLFLOATS);
ROR_ASSERT( r >= 0 );

// Ogre::Vector3
r = engine->RegisterObjectType("vector3", sizeof(Ogre::Vector3), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_CLASS_CA | asOBJ_APP_CLASS_ALLFLOATS);
r = engine->RegisterObjectType("vector3", sizeof(Vector3), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_CLASS_CA | asOBJ_APP_CLASS_ALLFLOATS);
ROR_ASSERT( r >= 0 );

// Ogre::Quaternion
r = engine->RegisterObjectType("quaternion", sizeof(Quaternion), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_CLASS_CA | asOBJ_APP_CLASS_ALLFLOATS);
ROR_ASSERT( r >= 0 );

// Ogre::ColourValue
r = engine->RegisterObjectType("color", sizeof(ColourValue), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_CLASS_CA | asOBJ_APP_CLASS_ALLFLOATS);
ROR_ASSERT( r >= 0 );

registerOgreRadian(engine);
registerOgreDegree(engine);
registerOgreVector3(engine);
registerOgreVector2(engine);
registerOgreQuaternion(engine);
registerOgreTexture(engine);
}

// register Ogre::Vector3
Expand Down Expand Up @@ -318,6 +393,120 @@ void registerOgreVector3(AngelScript::asIScriptEngine* engine)
ROR_ASSERT( r >= 0 );
}


// register Ogre::Vector2
void registerOgreVector2(AngelScript::asIScriptEngine* engine)
{
int r;

// Register the object properties
r = engine->RegisterObjectProperty("vector2", "float x", offsetof(Ogre::Vector2, x));
ROR_ASSERT( r >= 0 );
r = engine->RegisterObjectProperty("vector2", "float y", offsetof(Ogre::Vector2, y));
ROR_ASSERT( r >= 0 );

// Register the object constructors
r = engine->RegisterObjectBehaviour("vector2", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(Vector2DefaultConstructor), asCALL_CDECL_OBJLAST);
ROR_ASSERT( r >= 0 );
r = engine->RegisterObjectBehaviour("vector2", asBEHAVE_CONSTRUCT, "void f(float, float)", asFUNCTION(Vector2InitConstructor), asCALL_CDECL_OBJLAST);
ROR_ASSERT( r >= 0 );
r = engine->RegisterObjectBehaviour("vector2", asBEHAVE_CONSTRUCT, "void f(const vector2 &in)", asFUNCTION(Vector2CopyConstructor), asCALL_CDECL_OBJLAST);
ROR_ASSERT( r >= 0 );
r = engine->RegisterObjectBehaviour("vector2", asBEHAVE_CONSTRUCT, "void f(float)", asFUNCTION(Vector2InitConstructorScaler), asCALL_CDECL_OBJLAST);
ROR_ASSERT( r >= 0 );

// Register the object operators
r = engine->RegisterObjectMethod("vector2", "float opIndex(int) const", asMETHODPR(Vector2, operator[], (size_t) const, float), asCALL_THISCALL);
ROR_ASSERT( r >= 0 );
r = engine->RegisterObjectMethod("vector2", "vector2 &f(const vector2 &in)", asMETHODPR(Vector2, operator =, (const Vector2 &), Vector2&), asCALL_THISCALL);
ROR_ASSERT( r >= 0 );
r = engine->RegisterObjectMethod("vector2", "bool opEquals(const vector2 &in) const", asMETHODPR(Vector2, operator==,(const Vector2&) const, bool), asCALL_THISCALL);
ROR_ASSERT( r >= 0 );

r = engine->RegisterObjectMethod("vector2", "vector2 opAdd(const vector2 &in) const", asMETHODPR(Vector2, operator+,(const Vector2&) const, Vector2), asCALL_THISCALL);
ROR_ASSERT( r >= 0 );
r = engine->RegisterObjectMethod("vector2", "vector2 opSub(const vector2 &in) const", asMETHODPR(Vector2, operator-,(const Vector2&) const, Vector2), asCALL_THISCALL);
ROR_ASSERT( r >= 0 );

r = engine->RegisterObjectMethod("vector2", "vector2 opMul(float) const", asMETHODPR(Vector2, operator*,(const float) const, Vector2), asCALL_THISCALL);
ROR_ASSERT( r >= 0 );
r = engine->RegisterObjectMethod("vector2", "vector2 opMul(const vector2 &in) const", asMETHODPR(Vector2, operator*,(const Vector2&) const, Vector2), asCALL_THISCALL);
ROR_ASSERT( r >= 0 );
r = engine->RegisterObjectMethod("vector2", "vector2 opDiv(float) const", asMETHODPR(Vector2, operator/,(const float) const, Vector2), asCALL_THISCALL);
ROR_ASSERT( r >= 0 );
r = engine->RegisterObjectMethod("vector2", "vector2 opDiv(const vector2 &in) const", asMETHODPR(Vector2, operator/,(const Vector2&) const, Vector2), asCALL_THISCALL);
ROR_ASSERT( r >= 0 );

r = engine->RegisterObjectMethod("vector2", "vector2 opAdd() const", asMETHODPR(Vector2, operator+,() const, const Vector2&), asCALL_THISCALL);
ROR_ASSERT( r >= 0 );
r = engine->RegisterObjectMethod("vector2", "vector2 opSub() const", asMETHODPR(Vector2, operator-,() const, Vector2), asCALL_THISCALL);
ROR_ASSERT( r >= 0 );

r = engine->RegisterObjectMethod("vector2", "vector2 &opAddAssign(const vector2 &in)", asMETHODPR(Vector2,operator+=,(const Vector2 &),Vector2&), asCALL_THISCALL);
ROR_ASSERT( r >= 0 );
r = engine->RegisterObjectMethod("vector2", "vector2 &opAddAssign(float)", asMETHODPR(Vector2,operator+=,(const float),Vector2&), asCALL_THISCALL);
ROR_ASSERT( r >= 0 );

r = engine->RegisterObjectMethod("vector2", "vector2 &opSubAssign(const vector2 &in)", asMETHODPR(Vector2,operator-=,(const Vector2 &),Vector2&), asCALL_THISCALL);
ROR_ASSERT( r >= 0 );
r = engine->RegisterObjectMethod("vector2", "vector2 &opSubAssign(float)", asMETHODPR(Vector2,operator-=,(const float),Vector2&), asCALL_THISCALL);
ROR_ASSERT( r >= 0 );

r = engine->RegisterObjectMethod("vector2", "vector2 &opMulAssign(const vector2 &in)", asMETHODPR(Vector2,operator*=,(const Vector2 &),Vector2&), asCALL_THISCALL);
ROR_ASSERT( r >= 0 );
r = engine->RegisterObjectMethod("vector2", "vector2 &opMulAssign(float)", asMETHODPR(Vector2,operator*=,(const float),Vector2&), asCALL_THISCALL);
ROR_ASSERT( r >= 0 );

r = engine->RegisterObjectMethod("vector2", "vector2 &opDivAssign(const vector2 &in)", asMETHODPR(Vector2,operator/=,(const Vector2 &),Vector2&), asCALL_THISCALL);
ROR_ASSERT( r >= 0 );
r = engine->RegisterObjectMethod("vector2", "vector2 &opDivAssign(float)", asMETHODPR(Vector2,operator/=,(const float),Vector2&), asCALL_THISCALL);
ROR_ASSERT( r >= 0 );

// Register the object methods

r = engine->RegisterObjectMethod("vector2", "float length() const", asMETHOD(Vector2,length), asCALL_THISCALL);
ROR_ASSERT( r >= 0 );
r = engine->RegisterObjectMethod("vector2", "float squaredLength() const", asMETHOD(Vector2,squaredLength), asCALL_THISCALL);
ROR_ASSERT( r >= 0 );

r = engine->RegisterObjectMethod("vector2", "float distance(const vector2 &in) const", asMETHOD(Vector2,distance), asCALL_THISCALL);
ROR_ASSERT( r >= 0 );
r = engine->RegisterObjectMethod("vector2", "float squaredDistance(const vector2 &in) const", asMETHOD(Vector2,squaredDistance), asCALL_THISCALL);
ROR_ASSERT( r >= 0 );

r = engine->RegisterObjectMethod("vector2", "float dotProduct(const vector2 &in) const", asMETHOD(Vector2,dotProduct), asCALL_THISCALL);
ROR_ASSERT( r >= 0 );

r = engine->RegisterObjectMethod("vector2", "float normalise()", asMETHOD(Vector2,normalise), asCALL_THISCALL);
ROR_ASSERT( r >= 0 );
r = engine->RegisterObjectMethod("vector2", "float crossProduct(const vector2 &in) const", asMETHOD(Vector2,crossProduct), asCALL_THISCALL);
ROR_ASSERT( r >= 0 );
r = engine->RegisterObjectMethod("vector2", "vector2 midPoint(const vector2 &in) const", asMETHOD(Vector2,midPoint), asCALL_THISCALL);
ROR_ASSERT( r >= 0 );
r = engine->RegisterObjectMethod("vector2", "void makeFloor(const vector2 &in)", asMETHOD(Vector2,makeFloor), asCALL_THISCALL);
ROR_ASSERT( r >= 0 );
r = engine->RegisterObjectMethod("vector2", "void makeCeil(const vector2 &in)", asMETHOD(Vector2,makeCeil), asCALL_THISCALL);
ROR_ASSERT( r >= 0 );
r = engine->RegisterObjectMethod("vector2", "vector2 perpendicular() const", asMETHOD(Vector2,perpendicular), asCALL_THISCALL);
ROR_ASSERT( r >= 0 );
r = engine->RegisterObjectMethod("vector2", "vector2 randomDeviant(const radian &in, const vector2 &in) const", asMETHOD(Vector2,randomDeviant), asCALL_THISCALL);
ROR_ASSERT( r >= 0 );
r = engine->RegisterObjectMethod("vector2", "radian angleBetween(const vector2 &in)", asMETHOD(Vector2,angleBetween), asCALL_THISCALL);
ROR_ASSERT( r >= 0 );
r = engine->RegisterObjectMethod("vector2", "bool isZeroLength() const", asMETHOD(Vector2,isZeroLength), asCALL_THISCALL);
ROR_ASSERT( r >= 0 );
r = engine->RegisterObjectMethod("vector2", "vector2 normalisedCopy() const", asMETHOD(Vector2,normalisedCopy), asCALL_THISCALL);
ROR_ASSERT( r >= 0 );
r = engine->RegisterObjectMethod("vector2", "vector2 reflect(const vector2 &in) const", asMETHOD(Vector2,reflect), asCALL_THISCALL);
ROR_ASSERT( r >= 0 );

r = engine->RegisterObjectMethod("vector2", "bool positionEquals(const vector2 &in, float) const", asMETHOD(Vector2,positionEquals), asCALL_THISCALL);
ROR_ASSERT( r >= 0 );

r = engine->RegisterObjectMethod("vector2", "bool isNaN() const", asMETHOD(Vector2,isNaN), asCALL_THISCALL);
ROR_ASSERT( r >= 0 );
}

void registerOgreRadian(AngelScript::asIScriptEngine* engine)
{
int r;
Expand Down Expand Up @@ -569,3 +758,55 @@ void registerOgreQuaternion(AngelScript::asIScriptEngine* engine)
r = engine->RegisterGlobalFunction("quaternion nlerp(float, const quaternion &in, const quaternion &in, bool &in)", asFUNCTION(Quaternion::nlerp), asCALL_CDECL);
ROR_ASSERT( r >= 0 );
}

void registerOgreColourValue(AngelScript::asIScriptEngine* engine)
{
int r;

// Register the object properties
r = engine->RegisterObjectProperty("color", "float r", offsetof(ColourValue, r));
ROR_ASSERT( r >= 0 );
r = engine->RegisterObjectProperty("color", "float g", offsetof(ColourValue, g));
ROR_ASSERT( r >= 0 );
r = engine->RegisterObjectProperty("color", "float b", offsetof(ColourValue, b));
ROR_ASSERT( r >= 0 );
r = engine->RegisterObjectProperty("color", "float a", offsetof(ColourValue, a));
ROR_ASSERT( r >= 0 );

// Register the object constructors
r = engine->RegisterObjectBehaviour("color", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(ColourValueDefaultConstructor), asCALL_CDECL_OBJLAST);
ROR_ASSERT( r >= 0 );
r = engine->RegisterObjectBehaviour("color", asBEHAVE_CONSTRUCT, "void f(float r, float g, float b, float a)", asFUNCTION(ColourValueInitConstructor), asCALL_CDECL_OBJLAST);
ROR_ASSERT( r >= 0 );
r = engine->RegisterObjectBehaviour("color", asBEHAVE_CONSTRUCT, "void f(const color &other)", asFUNCTION(QuaternionCopyConstructor), asCALL_CDECL_OBJLAST);
}

void registerOgreTexture(AngelScript::asIScriptEngine* engine)
{
engine->SetDefaultNamespace("Ogre");
engine->RegisterObjectType("TexturePtr", sizeof(TexturePtr), asOBJ_VALUE | asGetTypeTraits<TexturePtr>());
engine->RegisterObjectBehaviour("TexturePtr", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(TexturePtrDefaultConstructor), asCALL_CDECL_OBJLAST);
engine->RegisterObjectBehaviour("TexturePtr", asBEHAVE_CONSTRUCT, "void f(const TexturePtr&in)", asFUNCTION(TexturePtrCopyConstructor), asCALL_CDECL_OBJLAST);
engine->RegisterObjectBehaviour("TexturePtr", asBEHAVE_DESTRUCT, "void f()", asFUNCTION(TexturePtrDestructor), asCALL_CDECL_OBJLAST);
engine->RegisterObjectMethod("TexturePtr", "TexturePtr& opAssign(const TexturePtr&in)", asFUNCTION(TexturePtrAssignOperator), asCALL_CDECL_OBJLAST);

// Wrappers are inevitable, see https://www.gamedev.net/forums/topic/540419-custom-smartpointers-and-angelscript-/
engine->RegisterObjectMethod("TexturePtr", "uint getWidth()", asFUNCTIONPR([](TexturePtr const& self){
return (Ogre::uint32)self->getWidth();
}, (TexturePtr const&), Ogre::uint32), asCALL_CDECL_OBJFIRST);
engine->RegisterObjectMethod("TexturePtr", "uint getHeight()", asFUNCTIONPR([](TexturePtr const& self){
return (Ogre::uint32)self->getHeight();
}, (TexturePtr const&), Ogre::uint32), asCALL_CDECL_OBJFIRST);

engine->RegisterObjectType("TextureManager", sizeof(TextureManager), asOBJ_REF | asOBJ_NOCOUNT);
// Convenience wrapper to omit optional parameters
engine->RegisterObjectMethod("TextureManager", "TexturePtr load(string file, string rg)", asFUNCTIONPR([](TextureManager& mgr, std::string const& file, std::string const& rg){
return mgr.load(file, rg);
}, (TextureManager& mgr, std::string const& file, std::string const& rg), TexturePtr), asCALL_CDECL_OBJFIRST);


engine->SetDefaultNamespace("Ogre::TextureManager");
engine->RegisterGlobalFunction("TextureManager& getSingleton()", asFUNCTION(TextureManager::getSingleton), asCALL_CDECL);

engine->SetDefaultNamespace("");
}
Loading