...
 
Commits (2)
......@@ -288,6 +288,34 @@ namespace OB{
*/
virtual void Remove();
/**
* Finds the first ancestor with a given name.
*
* @param name Name of the Instance to search for
*
* @author John M. Harris, Jr.
*/
virtual shared_ptr<Instance> FindFirstAncestor(std::string name);
/**
* Finds the first ancestor with a given class name.
*
* @param className Name of the class to search for
*
* @author John M. Harris, Jr.
*/
virtual shared_ptr<Instance> FindFirstAncestorOfClass(std::string className);
/**
* Finds the first ancestor for which Instance::IsA
* returns true.
*
* @param className Name of the class to search for
*
* @author John M. Harris, Jr.
*/
virtual shared_ptr<Instance> FindFirstAncestorWhichIsA(std::string className);
/**
* Finds the first child with a given name. This has
* an optional argument for searching recursively.
......@@ -310,6 +338,18 @@ namespace OB{
*/
virtual shared_ptr<Instance> FindFirstChildOfClass(std::string className, bool recursive = false);
/**
* Finds the first child for which Instance::IsA
* returns true. This has an optional argument for
* searching recursively.
*
* @param className Name of the class to search for
* @param recursive Whether or not to search recursively.
*
* @author John M. Harris, Jr.
*/
virtual shared_ptr<Instance> FindFirstChildWhichIsA(std::string className, bool recursive = false);
/**
* Returns the children of an object.
*
......@@ -616,7 +656,12 @@ namespace OB{
DECLARE_LUA_METHOD(Clone);
DECLARE_LUA_METHOD(Destroy);
DECLARE_LUA_METHOD(Remove);
DECLARE_LUA_METHOD(FindFirstAncestor);
DECLARE_LUA_METHOD(FindFirstAncestorOfClass);
DECLARE_LUA_METHOD(FindFirstAncestorWhichIsA);
DECLARE_LUA_METHOD(FindFirstChild);
DECLARE_LUA_METHOD(FindFirstChildOfClass);
DECLARE_LUA_METHOD(FindFirstChildWhichIsA);
DECLARE_LUA_METHOD(GetChildren);
DECLARE_LUA_METHOD(GetFullName);
DECLARE_LUA_METHOD(IsA);
......
......@@ -159,6 +159,39 @@ namespace OB{
}
}
shared_ptr<Instance> Instance::FindFirstAncestor(std::string name){
if(Parent){
if(Parent->Name == name){
return Parent;
}else{
return Parent->FindFirstAncestor(name);
}
}
return NULL;
}
shared_ptr<Instance> Instance::FindFirstAncestorOfClass(std::string className){
if(Parent){
if(Parent->ClassName == className){
return Parent;
}else{
return Parent->FindFirstAncestorOfClass(className);
}
}
return NULL;
}
shared_ptr<Instance> Instance::FindFirstChildWhichIsA(std::string className){
if(Parent){
if(Parent->IsA(className)){
return Parent;
}else{
return Parent->FindFirstAncestorWhichIsA(className);
}
}
return NULL;
}
shared_ptr<Instance> Instance::FindFirstChild(std::string name, bool recursive){
for(std::vector<shared_ptr<Instance>>::size_type i = 0; i != children.size(); i++){
shared_ptr<Instance> kid = children[i];
......@@ -205,6 +238,29 @@ namespace OB{
return NULL;
}
shared_ptr<Instance> Instance::FindFirstChildWhichIsA(std::string className, bool recursive){
for(std::vector<shared_ptr<Instance>>::size_type i = 0; i != children.size(); i++){
shared_ptr<Instance> kid = children[i];
if(kid){
if(kid->IsA(className)){
return kid;
}
}
}
if(recursive){
for(std::vector<shared_ptr<Instance>>::size_type i = 0; i != children.size(); i++){
shared_ptr<Instance> kid = children[i];
if(kid){
shared_ptr<Instance> myFind = kid->FindFirstChildWhichIsA(className, recursive);
if(myFind){
return myFind;
}
}
}
}
return NULL;
}
std::vector<shared_ptr<Instance>> Instance::GetChildren(){
return std::vector<shared_ptr<Instance>>(children);
}
......@@ -855,6 +911,8 @@ namespace OB{
{"Destroy", lua_Destroy},
{"Remove", lua_Remove},
{"FindFirstChild", lua_FindFirstChild},
{"FindFirstChildOfClass", lua_FindFirstChildOfClass},
{"FindFirstChildWhichIsA", lua_FindFirstChildWhichIsA},
{"GetChildren", lua_GetChildren},
{"GetFullName", lua_GetFullName},
{"IsA", lua_IsA},
......@@ -1234,6 +1292,54 @@ namespace OB{
return luaL_error(L, COLONERR, "Remove");
}
int Instance::lua_FindFirstAncestor(lua_State* L){
shared_ptr<Instance> inst = checkInstance(L, 1, false);
if(inst){
const char* ancestorName = luaL_checkstring(L, 2);
shared_ptr<Instance> foundStuff = inst->FindFirstAncestor(ancestorName);
if(foundStuff){
return foundStuff->wrap_lua(L);
}
lua_pushnil(L);
return 1;
}
return luaL_error(L, COLONERR, "FindFirstAncestor");
}
int Instance::lua_FindFirstAncestorOfClass(lua_State* L){
shared_ptr<Instance> inst = checkInstance(L, 1, false);
if(inst){
const char* className = luaL_checkstring(L, 2);
shared_ptr<Instance> foundStuff = inst->FindFirstAncestorOfClass(className);
if(foundStuff){
return foundStuff->wrap_lua(L);
}
lua_pushnil(L);
return 1;
}
return luaL_error(L, COLONERR, "FindFirstAncestorOfClass");
}
int Instance::lua_FindFirstAncestorWhichIsA(lua_State* L){
shared_ptr<Instance> inst = checkInstance(L, 1, false);
if(inst){
const char* className = luaL_checkstring(L, 2);
shared_ptr<Instance> foundStuff = inst->FindFirstAncestorWhichIsA(className);
if(foundStuff){
return foundStuff->wrap_lua(L);
}
lua_pushnil(L);
return 1;
}
return luaL_error(L, COLONERR, "FindFirstAncestorWhichIsA");
}
int Instance::lua_FindFirstChild(lua_State* L){
shared_ptr<Instance> inst = checkInstance(L, 1, false);
......@@ -1258,6 +1364,54 @@ namespace OB{
return luaL_error(L, COLONERR, "FindFirstChild");
}
int Instance::lua_FindFirstChildOfClass(lua_State* L){
shared_ptr<Instance> inst = checkInstance(L, 1, false);
if(inst){
const char* kidName = luaL_checkstring(L, 2);
bool recursive = false;
if(!lua_isnoneornil(L, 3)){
if(lua_isboolean(L, 3)){
recursive = lua_toboolean(L, 3);
}else{
luaL_argerror(L, 3, "boolean expected");
}
}
shared_ptr<Instance> foundStuff = inst->FindFirstChildOfClass(kidName, recursive);
if(foundStuff){
return foundStuff->wrap_lua(L);
}
lua_pushnil(L);
return 1;
}
return luaL_error(L, COLONERR, "FindFirstChildOfClass");
}
int Instance::lua_FindFirstChildWhichIsA(lua_State* L){
shared_ptr<Instance> inst = checkInstance(L, 1, false);
if(inst){
const char* kidName = luaL_checkstring(L, 2);
bool recursive = false;
if(!lua_isnoneornil(L, 3)){
if(lua_isboolean(L, 3)){
recursive = lua_toboolean(L, 3);
}else{
luaL_argerror(L, 3, "boolean expected");
}
}
shared_ptr<Instance> foundStuff = inst->FindFirstChildWhichIsA(kidName, recursive);
if(foundStuff){
return foundStuff->wrap_lua(L);
}
lua_pushnil(L);
return 1;
}
return luaL_error(L, COLONERR, "FindFirstChildWhichIsA");
}
int Instance::lua_GetChildren(lua_State* L){
shared_ptr<Instance> inst = checkInstance(L, 1, false);
......