Context.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #include "precompiled.h"
  2. #include "Context.h"
  3. #include <Rocket/Core/Context.h>
  4. #include <Rocket/Core/ElementDocument.h>
  5. #include <Rocket/Core/Factory.h>
  6. #include "LuaEventListener.h"
  7. namespace Rocket {
  8. namespace Core {
  9. namespace Lua {
  10. typedef Rocket::Core::ElementDocument Document;
  11. //methods
  12. int ContextAddEventListener(lua_State* L, Context* obj)
  13. {
  14. //need to make an EventListener for Lua before I can do anything else
  15. LUACHECKOBJ(obj);
  16. const char* evt = luaL_checkstring(L,1); //event
  17. Element* element = NULL;
  18. bool capturephase = false;
  19. //get the rest of the stuff needed to construct the listener
  20. if(lua_gettop(L) > 2)
  21. {
  22. if(!lua_isnoneornil(L,3))
  23. element = LuaType<Element>::check(L,3);
  24. if(!lua_isnoneornil(L,4))
  25. capturephase = CHECK_BOOL(L,4);
  26. }
  27. int type = lua_type(L,2);
  28. if(type == LUA_TFUNCTION)
  29. {
  30. if(element)
  31. element->AddEventListener(evt, new LuaEventListener(L,2,element), capturephase);
  32. else
  33. obj->AddEventListener(evt, new LuaEventListener(L,2,NULL), capturephase);
  34. }
  35. else if(type == LUA_TSTRING)
  36. {
  37. if(element)
  38. element->AddEventListener(evt, new LuaEventListener(luaL_checkstring(L,2),element), capturephase);
  39. else
  40. obj->AddEventListener(evt, new LuaEventListener(luaL_checkstring(L,2),NULL), capturephase);
  41. }
  42. else
  43. {
  44. Log::Message(Log::LT_WARNING, "Lua Context:AddEventLisener's 2nd argument can only be a Lua function or a string, you passed in a %s", lua_typename(L,type));
  45. }
  46. return 0;
  47. }
  48. int ContextAddMouseCursor(lua_State* L, Context* obj)
  49. {
  50. Document* cursor_doc = LuaType<Document>::check(L,1);
  51. obj->AddMouseCursor(cursor_doc);
  52. return 0;
  53. }
  54. int ContextCreateDocument(lua_State* L, Context* obj)
  55. {
  56. const char* tag;
  57. if(lua_gettop(L) < 1)
  58. tag = "body";
  59. else
  60. tag = luaL_checkstring(L,1);
  61. Document* doc = obj->CreateDocument(tag);
  62. LuaType<Document>::push(L,doc,true);
  63. //for debugging
  64. int count = doc->GetReferenceCount();
  65. return 1;
  66. }
  67. int ContextLoadDocument(lua_State* L, Context* obj)
  68. {
  69. const char* path = luaL_checkstring(L,1);
  70. Document* doc = obj->LoadDocument(path);
  71. LuaType<Document>::push(L,doc,false);
  72. doc->RemoveReference();
  73. return 1;
  74. }
  75. int ContextLoadMouseCursor(lua_State* L, Context* obj)
  76. {
  77. const char* path = luaL_checkstring(L,1);
  78. Document* doc = obj->LoadMouseCursor(path);
  79. LuaType<Document>::push(L,doc);
  80. return 1;
  81. }
  82. int ContextRender(lua_State* L, Context* obj)
  83. {
  84. lua_pushboolean(L,obj->Render());
  85. return 1;
  86. }
  87. int ContextShowMouseCursor(lua_State* L, Context* obj)
  88. {
  89. bool show = CHECK_BOOL(L,1);
  90. obj->ShowMouseCursor(show);
  91. return 0;
  92. }
  93. int ContextUnloadAllDocuments(lua_State* L, Context* obj)
  94. {
  95. obj->UnloadAllDocuments();
  96. return 0;
  97. }
  98. int ContextUnloadAllMouseCursors(lua_State* L, Context* obj)
  99. {
  100. obj->UnloadAllMouseCursors();
  101. return 0;
  102. }
  103. int ContextUnloadDocument(lua_State* L, Context* obj)
  104. {
  105. Document* doc = LuaType<Document>::check(L,1);
  106. obj->UnloadDocument(doc);
  107. return 0;
  108. }
  109. int ContextUnloadMouseCursor(lua_State* L, Context* obj)
  110. {
  111. const char* name = luaL_checkstring(L,1);
  112. obj->UnloadMouseCursor(name);
  113. return 0;
  114. }
  115. int ContextUpdate(lua_State* L, Context* obj)
  116. {
  117. lua_pushboolean(L,obj->Update());
  118. return 1;
  119. }
  120. //getters
  121. int ContextGetAttrdimensions(lua_State* L)
  122. {
  123. Context* cont = LuaType<Context>::check(L,1);
  124. const Vector2i* dim = &cont->GetDimensions();
  125. //const_cast-ing so that the user can do dimensions.x = 3 and it will actually change the dimensions
  126. //of the context
  127. LuaType<Vector2i>::push(L,const_cast<Vector2i*>(dim));
  128. return 1;
  129. }
  130. //returns a table of everything
  131. int ContextGetAttrdocuments(lua_State* L)
  132. {
  133. Context* cont = LuaType<Context>::check(L,1);
  134. Element* root = cont->GetRootElement();
  135. lua_newtable(L);
  136. int tableindex = lua_gettop(L);
  137. for(int i = 0; i < root->GetNumChildren(); i++)
  138. {
  139. Document* doc = root->GetChild(i)->GetOwnerDocument();
  140. if(doc == NULL)
  141. continue;
  142. LuaType<Document>::push(L,doc);
  143. lua_setfield(L, tableindex,doc->GetId().CString());
  144. /* //is this a bad idea?
  145. lua_pushinteger(L,i);
  146. lua_setfield(L,tableindex,doc->GetId().CString());
  147. */
  148. }
  149. return 1;
  150. }
  151. int ContextGetAttrfocus_element(lua_State* L)
  152. {
  153. Context* cont = LuaType<Context>::check(L,1);
  154. LuaType<Element>::push(L,cont->GetFocusElement());
  155. return 1;
  156. }
  157. int ContextGetAttrhover_element(lua_State* L)
  158. {
  159. Context* cont = LuaType<Context>::check(L,1);
  160. LuaType<Element>::push(L,cont->GetHoverElement());
  161. return 1;
  162. }
  163. int ContextGetAttrname(lua_State* L)
  164. {
  165. Context* cont = LuaType<Context>::check(L,1);
  166. lua_pushstring(L,cont->GetName().CString());
  167. return 1;
  168. }
  169. int ContextGetAttrroot_element(lua_State* L)
  170. {
  171. Context* cont = LuaType<Context>::check(L,1);
  172. LuaType<Element>::push(L,cont->GetRootElement());
  173. return 1;
  174. }
  175. //setters
  176. int ContextSetAttrdimensions(lua_State* L)
  177. {
  178. Context* cont = LuaType<Context>::check(L,1);
  179. Vector2i* dim = LuaType<Vector2i>::check(L,2);
  180. cont->SetDimensions(*dim);
  181. return 0;
  182. }
  183. RegType<Context> ContextMethods[] =
  184. {
  185. LUAMETHOD(Context,AddEventListener)
  186. LUAMETHOD(Context,AddMouseCursor)
  187. LUAMETHOD(Context,CreateDocument)
  188. LUAMETHOD(Context,LoadDocument)
  189. LUAMETHOD(Context,LoadMouseCursor)
  190. LUAMETHOD(Context,Render)
  191. LUAMETHOD(Context,ShowMouseCursor)
  192. LUAMETHOD(Context,UnloadAllDocuments)
  193. LUAMETHOD(Context,UnloadAllMouseCursors)
  194. LUAMETHOD(Context,UnloadDocument)
  195. LUAMETHOD(Context,UnloadMouseCursor)
  196. LUAMETHOD(Context,Update)
  197. { NULL, NULL },
  198. };
  199. luaL_reg ContextGetters[] =
  200. {
  201. LUAGETTER(Context,dimensions)
  202. LUAGETTER(Context,documents)
  203. LUAGETTER(Context,focus_element)
  204. LUAGETTER(Context,hover_element)
  205. LUAGETTER(Context,name)
  206. LUAGETTER(Context,root_element)
  207. { NULL, NULL },
  208. };
  209. luaL_reg ContextSetters[] =
  210. {
  211. LUASETTER(Context,dimensions)
  212. { NULL, NULL },
  213. };
  214. /*
  215. template<> const char* GetTClassName<Context>() { return "Context"; }
  216. template<> RegType<Context>* GetMethodTable<Context>() { return ContextMethods; }
  217. template<> luaL_reg* GetAttrTable<Context>() { return ContextGetters; }
  218. template<> luaL_reg* SetAttrTable<Context>() { return ContextSetters; }
  219. */
  220. }
  221. }
  222. }