Context.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. #include "ContextDocumentsProxy.h"
  8. namespace Rocket {
  9. namespace Core {
  10. namespace Lua {
  11. typedef Rocket::Core::ElementDocument Document;
  12. //methods
  13. int ContextAddEventListener(lua_State* L, Context* obj)
  14. {
  15. //need to make an EventListener for Lua before I can do anything else
  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. LUACHECKOBJ(cont);
  135. ContextDocumentsProxy* cdp = new ContextDocumentsProxy();
  136. cdp->owner = cont;
  137. LuaType<ContextDocumentsProxy>::push(L,cdp,true); //does get garbage collected (deleted)
  138. return 1;
  139. }
  140. int ContextGetAttrfocus_element(lua_State* L)
  141. {
  142. Context* cont = LuaType<Context>::check(L,1);
  143. LUACHECKOBJ(cont);
  144. LuaType<Element>::push(L,cont->GetFocusElement());
  145. return 1;
  146. }
  147. int ContextGetAttrhover_element(lua_State* L)
  148. {
  149. Context* cont = LuaType<Context>::check(L,1);
  150. LUACHECKOBJ(cont);
  151. LuaType<Element>::push(L,cont->GetHoverElement());
  152. return 1;
  153. }
  154. int ContextGetAttrname(lua_State* L)
  155. {
  156. Context* cont = LuaType<Context>::check(L,1);
  157. LUACHECKOBJ(cont);
  158. lua_pushstring(L,cont->GetName().CString());
  159. return 1;
  160. }
  161. int ContextGetAttrroot_element(lua_State* L)
  162. {
  163. Context* cont = LuaType<Context>::check(L,1);
  164. LUACHECKOBJ(cont);
  165. LuaType<Element>::push(L,cont->GetRootElement());
  166. return 1;
  167. }
  168. //setters
  169. int ContextSetAttrdimensions(lua_State* L)
  170. {
  171. Context* cont = LuaType<Context>::check(L,1);
  172. LUACHECKOBJ(cont);
  173. Vector2i* dim = LuaType<Vector2i>::check(L,2);
  174. cont->SetDimensions(*dim);
  175. return 0;
  176. }
  177. RegType<Context> ContextMethods[] =
  178. {
  179. LUAMETHOD(Context,AddEventListener)
  180. LUAMETHOD(Context,AddMouseCursor)
  181. LUAMETHOD(Context,CreateDocument)
  182. LUAMETHOD(Context,LoadDocument)
  183. LUAMETHOD(Context,LoadMouseCursor)
  184. LUAMETHOD(Context,Render)
  185. LUAMETHOD(Context,ShowMouseCursor)
  186. LUAMETHOD(Context,UnloadAllDocuments)
  187. LUAMETHOD(Context,UnloadAllMouseCursors)
  188. LUAMETHOD(Context,UnloadDocument)
  189. LUAMETHOD(Context,UnloadMouseCursor)
  190. LUAMETHOD(Context,Update)
  191. { NULL, NULL },
  192. };
  193. luaL_reg ContextGetters[] =
  194. {
  195. LUAGETTER(Context,dimensions)
  196. LUAGETTER(Context,documents)
  197. LUAGETTER(Context,focus_element)
  198. LUAGETTER(Context,hover_element)
  199. LUAGETTER(Context,name)
  200. LUAGETTER(Context,root_element)
  201. { NULL, NULL },
  202. };
  203. luaL_reg ContextSetters[] =
  204. {
  205. LUASETTER(Context,dimensions)
  206. { NULL, NULL },
  207. };
  208. /*
  209. template<> const char* GetTClassName<Context>() { return "Context"; }
  210. template<> RegType<Context>* GetMethodTable<Context>() { return ContextMethods; }
  211. template<> luaL_reg* GetAttrTable<Context>() { return ContextGetters; }
  212. template<> luaL_reg* SetAttrTable<Context>() { return ContextSetters; }
  213. */
  214. }
  215. }
  216. }