Parcourir la source

isClass/isKindOfClass support in class system, cast and safe_cast functions

Ivan Safrin il y a 13 ans
Parent
commit
d51dbac584

+ 17 - 0
Bindings/Contents/LUA/API/class.lua

@@ -2,6 +2,21 @@ function __areclassesequal(a,b)
 	return a.__ptr == b.__ptr
 end
 
+function __is_kind_of(c,T)
+	local __baseclass = getmetatable(c)
+	while __baseclass do
+		if __baseclass.__classname == T.__classname then
+			return true
+		end
+		__baseclass = __baseclass.__baseclass
+	end
+	return false
+end
+
+function __is_class(c, T)
+	return (c.__classname == T.__classname)
+end
+
 function class(name)
 	local cls = {}
 	cls.__classname = name
@@ -21,6 +36,8 @@ function class(name)
 	end
 
 	cls.__eq = __areclassesequal
+	cls.isKindOfClass = __is_kind_of
+	cls.isClass = __is_class
 
 	cls.__index = function(t,k)
 		local prototype = rawget(t,"__prototype")

+ 30 - 0
Bindings/Contents/LUA/API/defaults.lua

@@ -6,6 +6,36 @@ _G["count"] = function(T)
 	return _count
 end
 
+_G["cast"] = function (c, T)
+	if _G["__ptr_lookup"][T.__classname][c.__ptr] ~= nil then
+		return _G["__ptr_lookup"][T.__classname][c.__ptr]
+	else
+		_G["__ptr_lookup"][T.__classname][c.__ptr] = T("__skip_ptr__")
+		_G["__ptr_lookup"][T.__classname][c.__ptr].__ptr = c.__ptr
+		return _G["__ptr_lookup"][T.__classname][c.__ptr]
+	end
+
+end
+
+function __is_table_kind_of(T,c)
+        local __baseclass = T
+        while __baseclass do
+                if __baseclass.__classname == c.__classname then
+                        return true
+                end
+                __baseclass = __baseclass.__baseclass
+        end
+        return false
+end
+
+_G["safe_cast"] = function(c, T)
+	
+	if c:isKindOfClass(T) or __is_table_kind_of(T,c) then
+		return _G["cast"](c, T)
+	end
+	return nil
+end
+
 _G["print"] = function(msg)
 	_G["debugPrint"](tostring(msg))
 end

+ 2 - 2
Bindings/Scripts/create_lua_library/create_lua_library.py

@@ -386,7 +386,7 @@ def createLUABindings(inputPath, prefix, mainInclude, libSmallName, libName, api
 								else:
 									luafunc = "*(%s*)lua_topointer" % (param["type"].replace("Polygon", "Polycode::Polygon").replace("Rectangle", "Polycode::Rectangle"))
 								lend = ".__ptr"
-								if param["type"] == "int" or param["type"] == "unsigned int":
+								if param["type"] == "int" or param["type"] == "unsigned int" or param["type"] == "short":
 									luafunc = "lua_tointeger"
 									luatype = "LUA_TNUMBER"
 									checkfunc = "lua_isnumber"
@@ -489,7 +489,7 @@ def createLUABindings(inputPath, prefix, mainInclude, libSmallName, libName, api
 									outfunc = "lua_pushstring"
 									basicType = True
 									retFunc = ".c_str()"
-								if pm["rtnType"] == "int" or pm["rtnType"] == "unsigned int" or pm["rtnType"] == "static int" or  pm["rtnType"] == "size_t" or pm["rtnType"] == "static size_t" or pm["rtnType"] == "long" or pm["rtnType"] == "unsigned int" or pm["rtnType"] == "static long":
+								if pm["rtnType"] == "int" or pm["rtnType"] == "unsigned int" or pm["rtnType"] == "static int" or  pm["rtnType"] == "size_t" or pm["rtnType"] == "static size_t" or pm["rtnType"] == "long" or pm["rtnType"] == "unsigned int" or pm["rtnType"] == "static long" or pm["rtnType"] == "short":
 									outfunc = "lua_pushinteger"
 									basicType = True
 								if pm["rtnType"] == "bool" or pm["rtnType"] == "static bool" or pm["rtnType"] == "virtual bool":

+ 7 - 2
IDE/Contents/Source/PolycodeProject.cpp

@@ -96,6 +96,8 @@ bool PolycodeProject::loadProjectFromFile() {
 		for(int i=0; i < configFile.root["modules"]->length; i++) {
 			ObjectEntry *module = (*configFile.root["modules"])[i];
 			data.modules.push_back(module->stringVal);
+			CoreServices::getInstance()->getResourceManager()->addArchive("Standalone/Modules/"+module->stringVal+"/API");
+			
 		}
 	}
 	
@@ -160,10 +162,13 @@ bool PolycodeProject::saveFile() {
 	
 	for(int j=0; j < data.modules.size(); j++) {
 		if(!configFile.root["modules"]) {
-			configFile.root.addChild("modules");	
+			configFile.root.addChild("modules");			
 		}	
-		configFile.root["modules"]->type = ObjectEntry::ARRAY_ENTRY;	
+		configFile.root["modules"]->type = ObjectEntry::ARRAY_ENTRY;
 		configFile.root["modules"]->addChild("module", data.modules[j]);
+		
+		CoreServices::getInstance()->getResourceManager()->addArchive("Standalone/Modules/"+data.modules[j]+"/API");
+		
 	}
 	
 	if(configFile.root["fonts"]) {

+ 4 - 4
Modules/Contents/2DPhysics/Include/PolyPhysicsScreen.h

@@ -175,7 +175,7 @@ public:
 	* @param fixedRotation If this is set to true, the entity will always have a locked rotation.
 	* @return The physics entity wrapper.
 	*/
-	PhysicsScreenEntity *addPhysicsChild(ScreenEntity *newEntity, int entType, bool isStatic, Number friction=0.1, Number density=1, Number restitution = 0, bool isSensor = false, bool fixedRotation = false, short groupIndex = 0);
+	PhysicsScreenEntity *addPhysicsChild(ScreenEntity *newEntity, int entType, bool isStatic, Number friction=0.1, Number density=1, Number restitution = 0, bool isSensor = false, bool fixedRotation = false, int groupIndex = 0);
 
 	/**
 	* Tracks a ScreenEntity as a physics enabled child. 
@@ -189,7 +189,7 @@ public:
 	* @param fixedRotation If this is set to true, the entity will always have a locked rotation.
 	* @return The physics entity wrapper.
 	*/
-	PhysicsScreenEntity *trackPhysicsChild(ScreenEntity *newEntity, int entType, bool isStatic, Number friction=0.1, Number density=1, Number restitution = 0, bool isSensor = false, bool fixedRotation = false, short groupIndex = 0);
+	PhysicsScreenEntity *trackPhysicsChild(ScreenEntity *newEntity, int entType, bool isStatic, Number friction=0.1, Number density=1, Number restitution = 0, bool isSensor = false, bool fixedRotation = false, int groupIndex = 0);
 
 	
 	/**
@@ -208,7 +208,7 @@ public:
 	* @param entType Physics shape of the entity. Possible values are PhysicsScreenEntity::ENTITY_RECT or PhysicsScreenEntity::ENTITY_CIRCLE.
 	* @param entityToRemove Entity to remove from the screen.
 	*/	
-	PhysicsScreenEntity *addCollisionChild(ScreenEntity *newEntity, int entType, short groupIndex = 0);
+	PhysicsScreenEntity *addCollisionChild(ScreenEntity *newEntity, int entType, int groupIndex = 0);
 	
 	/**
 	* Begins tracking collisions for a ScreenEntity.
@@ -216,7 +216,7 @@ public:
 	* @param entType Physics shape of the entity. Possible values are PhysicsScreenEntity::ENTITY_RECT or PhysicsScreenEntity::ENTITY_CIRCLE.
 	* @param entityToRemove Entity to remove from the screen.
 	*/	
-	PhysicsScreenEntity *trackCollisionChild(ScreenEntity *newEntity, int entType, short groupIndex = 0);	
+	PhysicsScreenEntity *trackCollisionChild(ScreenEntity *newEntity, int entType, int groupIndex = 0);	
 	
 	/**
 	* Removes an existing joint.

+ 1 - 1
Modules/Contents/2DPhysics/Include/PolyPhysicsScreenEntity.h

@@ -34,7 +34,7 @@ namespace Polycode {
 	*/	
 	class _PolyExport PhysicsScreenEntity {
 		public:
-			PhysicsScreenEntity(ScreenEntity *entity, b2World *world, Number worldScale, int entType, bool isStatic, Number friction, Number density, Number restitution, bool isSensor, bool fixedRotation, short groupIndex = 0);
+			PhysicsScreenEntity(ScreenEntity *entity, b2World *world, Number worldScale, int entType, bool isStatic, Number friction, Number density, Number restitution, bool isSensor, bool fixedRotation, int groupIndex = 0);
 			~PhysicsScreenEntity();		
 			
 			/**

+ 4 - 4
Modules/Contents/2DPhysics/Source/PolyPhysicsScreen.cpp

@@ -292,14 +292,14 @@ void PhysicsScreen::setVelocityY(ScreenEntity *ent, Number fy) {
 }
 
 
-PhysicsScreenEntity *PhysicsScreen::addCollisionChild(ScreenEntity *newEntity, int entType, short groupIndex) {
+PhysicsScreenEntity *PhysicsScreen::addCollisionChild(ScreenEntity *newEntity, int entType, int groupIndex) {
 	PhysicsScreenEntity *ret;
 	ret = addPhysicsChild(newEntity, entType, false, 0,0.0,0, true, groupIndex);
 	ret->collisionOnly = true; 
 	return ret;
 }
 
-PhysicsScreenEntity *PhysicsScreen::trackCollisionChild(ScreenEntity *newEntity, int entType, short groupIndex) {
+PhysicsScreenEntity *PhysicsScreen::trackCollisionChild(ScreenEntity *newEntity, int entType, int groupIndex) {
 	PhysicsScreenEntity *ret;
 	ret = trackPhysicsChild(newEntity, entType, false, 0,0.0,0, true, groupIndex);
 	ret->collisionOnly = true; 
@@ -417,12 +417,12 @@ void PhysicsScreen::destroyMouseJoint(b2MouseJoint *mJoint) {
 		mJoint = NULL;
 }
 
-PhysicsScreenEntity *PhysicsScreen::addPhysicsChild(ScreenEntity *newEntity, int entType, bool isStatic, Number friction, Number density, Number restitution, bool isSensor, bool fixedRotation, short groupIndex) {
+PhysicsScreenEntity *PhysicsScreen::addPhysicsChild(ScreenEntity *newEntity, int entType, bool isStatic, Number friction, Number density, Number restitution, bool isSensor, bool fixedRotation, int groupIndex) {
 	addChild(newEntity);
 	return trackPhysicsChild(newEntity, entType, isStatic, friction, density, restitution, isSensor, fixedRotation, groupIndex);
 }
 
-PhysicsScreenEntity *PhysicsScreen::trackPhysicsChild(ScreenEntity *newEntity, int entType, bool isStatic, Number friction, Number density, Number restitution, bool isSensor, bool fixedRotation, short groupIndex) {
+PhysicsScreenEntity *PhysicsScreen::trackPhysicsChild(ScreenEntity *newEntity, int entType, bool isStatic, Number friction, Number density, Number restitution, bool isSensor, bool fixedRotation, int groupIndex) {
 	newEntity->setPositionMode(ScreenEntity::POSITION_CENTER);
 	PhysicsScreenEntity *newPhysicsEntity = new PhysicsScreenEntity(newEntity, world, worldScale, entType, isStatic, friction, density, restitution, isSensor,fixedRotation, groupIndex);
 	physicsChildren.push_back(newPhysicsEntity);

+ 1 - 1
Modules/Contents/2DPhysics/Source/PolyPhysicsScreenEntity.cpp

@@ -31,7 +31,7 @@ THE SOFTWARE.
 
 using namespace Polycode;
 
-PhysicsScreenEntity::PhysicsScreenEntity(ScreenEntity *entity, b2World *world, Number worldScale, int entType, bool isStatic, Number friction, Number density, Number restitution, bool isSensor, bool fixedRotation, short groupIndex) {
+PhysicsScreenEntity::PhysicsScreenEntity(ScreenEntity *entity, b2World *world, Number worldScale, int entType, bool isStatic, Number friction, Number density, Number restitution, bool isSensor, bool fixedRotation, int groupIndex) {
 	
 	this->worldScale = worldScale;