Context.cpp 6.3 KB

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