Context.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "precompiled.h"
  28. #include "Context.h"
  29. #include <Rocket/Core/Context.h>
  30. #include <Rocket/Core/ElementDocument.h>
  31. #include <Rocket/Core/Factory.h>
  32. #include "LuaEventListener.h"
  33. #include "ContextDocumentsProxy.h"
  34. namespace Rocket {
  35. namespace Core {
  36. namespace Lua {
  37. typedef Rocket::Core::ElementDocument Document;
  38. template<> void ExtraInit<Context>(lua_State* L, int metatable_index) { return; }
  39. //methods
  40. int ContextAddEventListener(lua_State* L, Context* obj)
  41. {
  42. //need to make an EventListener for Lua before I can do anything else
  43. const char* evt = luaL_checkstring(L,1); //event
  44. Element* element = NULL;
  45. bool capturephase = false;
  46. //get the rest of the stuff needed to construct the listener
  47. if(lua_gettop(L) > 2)
  48. {
  49. if(!lua_isnoneornil(L,3))
  50. element = LuaType<Element>::check(L,3);
  51. if(!lua_isnoneornil(L,4))
  52. capturephase = CHECK_BOOL(L,4);
  53. }
  54. int type = lua_type(L,2);
  55. if(type == LUA_TFUNCTION)
  56. {
  57. if(element)
  58. element->AddEventListener(evt, new LuaEventListener(L,2,element), capturephase);
  59. else
  60. obj->AddEventListener(evt, new LuaEventListener(L,2,NULL), capturephase);
  61. }
  62. else if(type == LUA_TSTRING)
  63. {
  64. if(element)
  65. element->AddEventListener(evt, new LuaEventListener(luaL_checkstring(L,2),element), capturephase);
  66. else
  67. obj->AddEventListener(evt, new LuaEventListener(luaL_checkstring(L,2),NULL), capturephase);
  68. }
  69. else
  70. {
  71. 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));
  72. }
  73. return 0;
  74. }
  75. int ContextCreateDocument(lua_State* L, Context* obj)
  76. {
  77. const char* tag;
  78. if(lua_gettop(L) < 1)
  79. tag = "body";
  80. else
  81. tag = luaL_checkstring(L,1);
  82. Document* doc = obj->CreateDocument(tag);
  83. LuaType<Document>::push(L,doc,true);
  84. return 1;
  85. }
  86. int ContextLoadDocument(lua_State* L, Context* obj)
  87. {
  88. const char* path = luaL_checkstring(L,1);
  89. Document* doc = obj->LoadDocument(path);
  90. LuaType<Document>::push(L,doc,false);
  91. doc->RemoveReference();
  92. return 1;
  93. }
  94. int ContextRender(lua_State* L, Context* obj)
  95. {
  96. lua_pushboolean(L,obj->Render());
  97. return 1;
  98. }
  99. int ContextUnloadAllDocuments(lua_State* L, Context* obj)
  100. {
  101. obj->UnloadAllDocuments();
  102. return 0;
  103. }
  104. int ContextUnloadDocument(lua_State* L, Context* obj)
  105. {
  106. Document* doc = LuaType<Document>::check(L,1);
  107. obj->UnloadDocument(doc);
  108. return 0;
  109. }
  110. int ContextUpdate(lua_State* L, Context* obj)
  111. {
  112. lua_pushboolean(L,obj->Update());
  113. return 1;
  114. }
  115. //getters
  116. int ContextGetAttrdimensions(lua_State* L)
  117. {
  118. Context* cont = LuaType<Context>::check(L,1);
  119. const Vector2i* dim = &cont->GetDimensions();
  120. //const_cast-ing so that the user can do dimensions.x = 3 and it will actually change the dimensions
  121. //of the context
  122. LuaType<Vector2i>::push(L,const_cast<Vector2i*>(dim));
  123. return 1;
  124. }
  125. //returns a table of everything
  126. int ContextGetAttrdocuments(lua_State* L)
  127. {
  128. Context* cont = LuaType<Context>::check(L,1);
  129. LUACHECKOBJ(cont);
  130. ContextDocumentsProxy* cdp = new ContextDocumentsProxy();
  131. cdp->owner = cont;
  132. LuaType<ContextDocumentsProxy>::push(L,cdp,true); //does get garbage collected (deleted)
  133. return 1;
  134. }
  135. int ContextGetAttrfocus_element(lua_State* L)
  136. {
  137. Context* cont = LuaType<Context>::check(L,1);
  138. LUACHECKOBJ(cont);
  139. LuaType<Element>::push(L,cont->GetFocusElement());
  140. return 1;
  141. }
  142. int ContextGetAttrhover_element(lua_State* L)
  143. {
  144. Context* cont = LuaType<Context>::check(L,1);
  145. LUACHECKOBJ(cont);
  146. LuaType<Element>::push(L,cont->GetHoverElement());
  147. return 1;
  148. }
  149. int ContextGetAttrname(lua_State* L)
  150. {
  151. Context* cont = LuaType<Context>::check(L,1);
  152. LUACHECKOBJ(cont);
  153. lua_pushstring(L,cont->GetName().CString());
  154. return 1;
  155. }
  156. int ContextGetAttrroot_element(lua_State* L)
  157. {
  158. Context* cont = LuaType<Context>::check(L,1);
  159. LUACHECKOBJ(cont);
  160. LuaType<Element>::push(L,cont->GetRootElement());
  161. return 1;
  162. }
  163. //setters
  164. int ContextSetAttrdimensions(lua_State* L)
  165. {
  166. Context* cont = LuaType<Context>::check(L,1);
  167. LUACHECKOBJ(cont);
  168. Vector2i* dim = LuaType<Vector2i>::check(L,2);
  169. cont->SetDimensions(*dim);
  170. return 0;
  171. }
  172. RegType<Context> ContextMethods[] =
  173. {
  174. LUAMETHOD(Context,AddEventListener)
  175. LUAMETHOD(Context,CreateDocument)
  176. LUAMETHOD(Context,LoadDocument)
  177. LUAMETHOD(Context,Render)
  178. LUAMETHOD(Context,UnloadAllDocuments)
  179. LUAMETHOD(Context,UnloadDocument)
  180. LUAMETHOD(Context,Update)
  181. { NULL, NULL },
  182. };
  183. luaL_Reg ContextGetters[] =
  184. {
  185. LUAGETTER(Context,dimensions)
  186. LUAGETTER(Context,documents)
  187. LUAGETTER(Context,focus_element)
  188. LUAGETTER(Context,hover_element)
  189. LUAGETTER(Context,name)
  190. LUAGETTER(Context,root_element)
  191. { NULL, NULL },
  192. };
  193. luaL_Reg ContextSetters[] =
  194. {
  195. LUASETTER(Context,dimensions)
  196. { NULL, NULL },
  197. };
  198. LUACORETYPEDEFINE(Context,true)
  199. }
  200. }
  201. }