Context.cpp 6.2 KB

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