Context.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 ContextAddMouseCursor(lua_State* L, Context* obj)
  76. {
  77. Document* cursor_doc = LuaType<Document>::check(L,1);
  78. obj->AddMouseCursor(cursor_doc);
  79. return 0;
  80. }
  81. int ContextCreateDocument(lua_State* L, Context* obj)
  82. {
  83. const char* tag;
  84. if(lua_gettop(L) < 1)
  85. tag = "body";
  86. else
  87. tag = luaL_checkstring(L,1);
  88. Document* doc = obj->CreateDocument(tag);
  89. LuaType<Document>::push(L,doc,true);
  90. return 1;
  91. }
  92. int ContextLoadDocument(lua_State* L, Context* obj)
  93. {
  94. const char* path = luaL_checkstring(L,1);
  95. Document* doc = obj->LoadDocument(path);
  96. LuaType<Document>::push(L,doc,false);
  97. doc->RemoveReference();
  98. return 1;
  99. }
  100. int ContextLoadMouseCursor(lua_State* L, Context* obj)
  101. {
  102. const char* path = luaL_checkstring(L,1);
  103. Document* doc = obj->LoadMouseCursor(path);
  104. LuaType<Document>::push(L,doc);
  105. return 1;
  106. }
  107. int ContextRender(lua_State* L, Context* obj)
  108. {
  109. lua_pushboolean(L,obj->Render());
  110. return 1;
  111. }
  112. int ContextShowMouseCursor(lua_State* L, Context* obj)
  113. {
  114. bool show = CHECK_BOOL(L,1);
  115. obj->ShowMouseCursor(show);
  116. return 0;
  117. }
  118. int ContextUnloadAllDocuments(lua_State* L, Context* obj)
  119. {
  120. obj->UnloadAllDocuments();
  121. return 0;
  122. }
  123. int ContextUnloadAllMouseCursors(lua_State* L, Context* obj)
  124. {
  125. obj->UnloadAllMouseCursors();
  126. return 0;
  127. }
  128. int ContextUnloadDocument(lua_State* L, Context* obj)
  129. {
  130. Document* doc = LuaType<Document>::check(L,1);
  131. obj->UnloadDocument(doc);
  132. return 0;
  133. }
  134. int ContextUnloadMouseCursor(lua_State* L, Context* obj)
  135. {
  136. const char* name = luaL_checkstring(L,1);
  137. obj->UnloadMouseCursor(name);
  138. return 0;
  139. }
  140. int ContextUpdate(lua_State* L, Context* obj)
  141. {
  142. lua_pushboolean(L,obj->Update());
  143. return 1;
  144. }
  145. //getters
  146. int ContextGetAttrdimensions(lua_State* L)
  147. {
  148. Context* cont = LuaType<Context>::check(L,1);
  149. const Vector2i* dim = &cont->GetDimensions();
  150. //const_cast-ing so that the user can do dimensions.x = 3 and it will actually change the dimensions
  151. //of the context
  152. LuaType<Vector2i>::push(L,const_cast<Vector2i*>(dim));
  153. return 1;
  154. }
  155. //returns a table of everything
  156. int ContextGetAttrdocuments(lua_State* L)
  157. {
  158. Context* cont = LuaType<Context>::check(L,1);
  159. LUACHECKOBJ(cont);
  160. ContextDocumentsProxy* cdp = new ContextDocumentsProxy();
  161. cdp->owner = cont;
  162. LuaType<ContextDocumentsProxy>::push(L,cdp,true); //does get garbage collected (deleted)
  163. return 1;
  164. }
  165. int ContextGetAttrfocus_element(lua_State* L)
  166. {
  167. Context* cont = LuaType<Context>::check(L,1);
  168. LUACHECKOBJ(cont);
  169. LuaType<Element>::push(L,cont->GetFocusElement());
  170. return 1;
  171. }
  172. int ContextGetAttrhover_element(lua_State* L)
  173. {
  174. Context* cont = LuaType<Context>::check(L,1);
  175. LUACHECKOBJ(cont);
  176. LuaType<Element>::push(L,cont->GetHoverElement());
  177. return 1;
  178. }
  179. int ContextGetAttrname(lua_State* L)
  180. {
  181. Context* cont = LuaType<Context>::check(L,1);
  182. LUACHECKOBJ(cont);
  183. lua_pushstring(L,cont->GetName().CString());
  184. return 1;
  185. }
  186. int ContextGetAttrroot_element(lua_State* L)
  187. {
  188. Context* cont = LuaType<Context>::check(L,1);
  189. LUACHECKOBJ(cont);
  190. LuaType<Element>::push(L,cont->GetRootElement());
  191. return 1;
  192. }
  193. //setters
  194. int ContextSetAttrdimensions(lua_State* L)
  195. {
  196. Context* cont = LuaType<Context>::check(L,1);
  197. LUACHECKOBJ(cont);
  198. Vector2i* dim = LuaType<Vector2i>::check(L,2);
  199. cont->SetDimensions(*dim);
  200. return 0;
  201. }
  202. RegType<Context> ContextMethods[] =
  203. {
  204. LUAMETHOD(Context,AddEventListener)
  205. LUAMETHOD(Context,AddMouseCursor)
  206. LUAMETHOD(Context,CreateDocument)
  207. LUAMETHOD(Context,LoadDocument)
  208. LUAMETHOD(Context,LoadMouseCursor)
  209. LUAMETHOD(Context,Render)
  210. LUAMETHOD(Context,ShowMouseCursor)
  211. LUAMETHOD(Context,UnloadAllDocuments)
  212. LUAMETHOD(Context,UnloadAllMouseCursors)
  213. LUAMETHOD(Context,UnloadDocument)
  214. LUAMETHOD(Context,UnloadMouseCursor)
  215. LUAMETHOD(Context,Update)
  216. { NULL, NULL },
  217. };
  218. luaL_Reg ContextGetters[] =
  219. {
  220. LUAGETTER(Context,dimensions)
  221. LUAGETTER(Context,documents)
  222. LUAGETTER(Context,focus_element)
  223. LUAGETTER(Context,hover_element)
  224. LUAGETTER(Context,name)
  225. LUAGETTER(Context,root_element)
  226. { NULL, NULL },
  227. };
  228. luaL_Reg ContextSetters[] =
  229. {
  230. LUASETTER(Context,dimensions)
  231. { NULL, NULL },
  232. };
  233. LUACORETYPEDEFINE(Context,true)
  234. }
  235. }
  236. }