00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "asRect.h"
00018
00019 #ifdef __COMPILE_WITH_ANGELSCRIPT__
00020
00021 #include "asIrrHelper.h"
00022 #include "asDimension2d.h"
00023 #include "asVector2d.h"
00024
00025
00026
00030 template<typename T>
00031 void constructor(void *memory)
00032 {
00033 new (memory) rect<T>();
00034 }
00035
00039 template<typename T>
00040 void constructor(const position2d<T> &pos, const dimension2d<T> &size, void *memory)
00041 {
00042 new (memory) rect<T>(pos, size);
00043 }
00044
00048 template<typename T>
00049 void constructor(const position2d<T> &upperLeft, const position2d<T> &lowerRight, void *memory)
00050 {
00051 new (memory) rect<T>(upperLeft, lowerRight);
00052 }
00053
00057 template<typename T>
00058 void deconstructor(void *memory)
00059 {
00060 ((rect<T>*)memory)->~rect<T>();
00061 }
00062
00065 template<typename T>
00066 int compare(const rect<T> &a, const rect<T> &b)
00067 {
00068 if( a < b ) return -1;
00069 if( b < a ) return 1;
00070 return 0;
00071 }
00072
00073
00078 template<typename T>
00079 void bindRectT(asIScriptEngine *engine, const char *typeName, const char *asType)
00080 {
00081
00082 int r;
00083 std::stringstream ss;
00084
00085
00086 r = engine->RegisterObjectType(typeName, sizeof(rect<T>), asOBJ_VALUE | asOBJ_APP_CLASS_CDA); assert(r >= 0);
00087
00088
00089
00090 r = engine->RegisterObjectBehaviour(typeName, asBEHAVE_CONSTRUCT, "void f()", asFUNCTIONPR(constructor<T>,
00091 (void*), void), asCALL_CDECL_OBJLAST); assert(r >= 0);
00092
00093 ss.str("");
00094 ss << "void f(const " << getVector2dName(asType) << " &in, const " << getDimension2dName(asType) << " &in)";
00095 r = engine->RegisterObjectBehaviour(typeName, asBEHAVE_CONSTRUCT, ss.str().c_str(), asFUNCTIONPR(constructor<T>,
00096 (const position2d<T>&, const dimension2d<T>&, void*), void),
00097 asCALL_CDECL_OBJLAST); assert(r >= 0);
00098
00099 ss.str("");
00100 ss << "void f(const " << getVector2dName(asType) << " &in, const " << getVector2dName(asType) << " &in)";
00101 r = engine->RegisterObjectBehaviour(typeName, asBEHAVE_CONSTRUCT, ss.str().c_str(), asFUNCTIONPR(constructor<T>,
00102 (const position2d<T>&, const position2d<T>&, void*), void),
00103 asCALL_CDECL_OBJLAST); assert(r >= 0);
00104
00105 r = engine->RegisterObjectBehaviour(typeName, asBEHAVE_DESTRUCT, "void f()", asFUNCTION(deconstructor<T>),
00106 asCALL_CDECL_OBJLAST); assert(r >= 0);
00107
00108
00109
00110 ss.str("");
00111 ss << typeName << " opAdd(const " << getVector2dName(asType) << "&in)";
00112 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00113 asFUNCTIONPR((add< rect<T>, rect<T>, position2d<T> >),
00114 (const rect<T>&, const position2d<T>&), rect<T>),
00115 asCALL_CDECL_OBJFIRST); assert(r >= 0);
00116
00117
00118 ss.str("");
00119 ss << typeName << "& opAddAssign(const " << getVector2dName(asType) << "&in)";
00120 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00121 asMETHODPR(rect<T>, operator+=, (const position2d<T>&),
00122 rect<T>&), asCALL_THISCALL); assert(r >= 0);
00123
00124
00125 ss.str("");
00126 ss << typeName << " opSub(const " << getVector2dName(asType) << "&in)";
00127 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00128 asFUNCTIONPR((substract< rect<T>, rect<T>, position2d<T> >),
00129 (const rect<T>&, const position2d<T>&), rect<T>),
00130 asCALL_CDECL_OBJFIRST); assert(r >= 0);
00131
00132
00133 ss.str("");
00134 ss << typeName << "& opSubAssign(const " << getVector2dName(asType) << "&in)";
00135 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00136 asMETHODPR(rect<T>, operator-=, (const position2d<T>&),
00137 rect<T>&), asCALL_THISCALL); assert(r >= 0);
00138
00139
00140 ss.str("");
00141 ss << "bool opEquals(const " << typeName << " &in)";
00142 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(), asFUNCTIONPR(equal< rect<T> >,
00143 (const rect<T>&, const rect<T>&), bool), asCALL_CDECL_OBJFIRST); assert(r >= 0);
00144
00145 ss.str("");
00146 ss << "bool opCmp(const " << typeName << " &in)";
00147 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(), asFUNCTIONPR(compare< T >,
00148 (const rect<T>&, const rect<T>&), int), asCALL_CDECL_OBJFIRST); assert(r >= 0);
00149
00150
00151 ss.str("");
00152 ss << "void addInternalPoint(const " << getVector2dName(asType) << " &in)";
00153 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00154 asMETHODPR(rect<T>, addInternalPoint, (const position2d<T>&), void),
00155 asCALL_THISCALL); assert(r >= 0);
00156
00157 ss.str("");
00158 ss << "void clipAgainst(const " << typeName << " &in)";
00159 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00160 asMETHOD(rect<T>, clipAgainst), asCALL_THISCALL); assert(r >= 0);
00161
00162 ss.str("");
00163 ss << "void constrainTo(const " << typeName << " &in)";
00164 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00165 asMETHOD(rect<T>, constrainTo), asCALL_THISCALL); assert(r >= 0);
00166
00167 ss.str("");
00168 ss << asType << " getArea()";
00169 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00170 asMETHOD(rect<T>, getArea), asCALL_THISCALL); assert(r >= 0);
00171
00172 ss.str("");
00173 ss << getVector2dName(asType) << " getCenter()";
00174 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00175 asMETHOD(rect<T>, getCenter), asCALL_THISCALL); assert(r >= 0);
00176
00177 ss.str("");
00178 ss << asType << " getHeight()";
00179 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00180 asMETHOD(rect<T>, getHeight), asCALL_THISCALL); assert(r >= 0);
00181
00182 ss.str("");
00183 ss << getDimension2dName(asType) << " getSize()";
00184 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00185 asMETHOD(rect<T>, getSize), asCALL_THISCALL); assert(r >= 0);
00186
00187 ss.str("");
00188 ss << asType << " getWidth()";
00189 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00190 asMETHOD(rect<T>, getWidth), asCALL_THISCALL); assert(r >= 0);
00191
00192 ss.str("");
00193 ss << "bool isPointInside(const " << getVector2dName(asType) << " &in)";
00194 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00195 asMETHOD(rect<T>, isPointInside), asCALL_THISCALL); assert(r >= 0);
00196
00197 ss.str("");
00198 ss << "bool isRectCollided(const " << typeName << " &in)";
00199 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00200 asMETHOD(rect<T>, isRectCollided), asCALL_THISCALL); assert(r >= 0);
00201
00202 r = engine->RegisterObjectMethod(typeName, "bool isValid()",
00203 asMETHOD(rect<T>, isValid), asCALL_THISCALL); assert(r >= 0);
00204
00205 r = engine->RegisterObjectMethod(typeName, "void repair()",
00206 asMETHOD(rect<T>, repair), asCALL_THISCALL); assert(r >= 0);
00207
00208
00209 ss.str("");
00210 ss << getVector2dName(asType) << " LowerRightCorner";
00211 r = engine->RegisterObjectProperty(typeName, ss.str().c_str(), offsetof(rect<T>, LowerRightCorner)); assert(r >= 0);
00212
00213 ss.str("");
00214 ss << getVector2dName(asType) << " UpperLeftCorner";
00215 r = engine->RegisterObjectProperty(typeName, ss.str().c_str(), offsetof(rect<T>, UpperLeftCorner)); assert(r >= 0);
00216 }
00217
00220 void bindRect(asIScriptEngine *engine)
00221 {
00222 bindRectT<f32>(engine, "rectf", "f32");
00223 bindRectT<s32>(engine, "recti", "s32");
00224 }
00225
00228 std::string getRectName(const std::string &asType)
00229 {
00230 if(asType == "f32") return std::string("rectf");
00231 if(asType == "s32") return std::string("recti");
00232 else return std::string("");
00233 }
00234
00235 #endif // __COMPILE_WITH_ANGELSCRIPT__
00236
00237