Context.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. #include "LuaDataModel.h"
  35. namespace Rml {
  36. namespace Lua {
  37. typedef 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 = nullptr;
  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 = RMLUI_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,nullptr), 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),nullptr), 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,false);
  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. return 1;
  92. }
  93. int ContextRender(lua_State* L, Context* obj)
  94. {
  95. lua_pushboolean(L,obj->Render());
  96. return 1;
  97. }
  98. int ContextUnloadAllDocuments(lua_State* /*L*/, Context* obj)
  99. {
  100. obj->UnloadAllDocuments();
  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 ContextUpdate(lua_State* L, Context* obj)
  110. {
  111. lua_pushboolean(L,obj->Update());
  112. return 1;
  113. }
  114. int ContextOpenDataModel(lua_State *L, Context *obj)
  115. {
  116. if (!OpenLuaDataModel(L, obj, 1, 2)) {
  117. // Open fails
  118. lua_pushboolean(L, false);
  119. }
  120. return 1;
  121. }
  122. //input
  123. int ContextProcessMouseMove(lua_State *L, Context *obj)
  124. {
  125. int x = luaL_checkinteger(L, 1);
  126. int y = luaL_checkinteger(L, 2);
  127. int flags = luaL_checkinteger(L, 3);
  128. lua_pushboolean(L, obj->ProcessMouseMove(x, y, flags));
  129. return 1;
  130. }
  131. int ContextProcessMouseButtonDown(lua_State *L, Context *obj)
  132. {
  133. int button_index = luaL_checkinteger(L, 1);
  134. int key_modifier_state = luaL_checkinteger(L, 2);
  135. lua_pushboolean(L, obj->ProcessMouseButtonDown(button_index, key_modifier_state));
  136. return 1;
  137. }
  138. int ContextProcessMouseButtonUp(lua_State *L, Context *obj)
  139. {
  140. int button_index = luaL_checkinteger(L, 1);
  141. int key_modifier_state = luaL_checkinteger(L, 2);
  142. lua_pushboolean(L, obj->ProcessMouseButtonUp(button_index, key_modifier_state));
  143. return 1;
  144. }
  145. int ContextProcessMouseWheel(lua_State *L, Context *obj)
  146. {
  147. float wheel_delta = (float)luaL_checknumber(L, 1);
  148. int key_modifier_state = luaL_checkinteger(L, 2);
  149. lua_pushboolean(L, obj->ProcessMouseWheel(wheel_delta, key_modifier_state));
  150. return 1;
  151. }
  152. int ContextProcessMouseLeave(lua_State *L, Context *obj)
  153. {
  154. lua_pushboolean(L, obj->ProcessMouseLeave());
  155. return 1;
  156. }
  157. int ContextIsMouseInteracting(lua_State *L, Context *obj)
  158. {
  159. lua_pushboolean(L, obj->IsMouseInteracting());
  160. return 1;
  161. }
  162. int ContextProcessKeyDown(lua_State *L, Context *obj)
  163. {
  164. Rml::Input::KeyIdentifier key_identifier = (Rml::Input::KeyIdentifier)luaL_checkinteger(L, 1);
  165. int key_modifier_state = luaL_checkinteger( L, 2 );
  166. lua_pushboolean(L, obj->ProcessKeyDown(key_identifier, key_modifier_state));
  167. return 1;
  168. }
  169. int ContextProcessKeyUp(lua_State *L, Context *obj) {
  170. Rml::Input::KeyIdentifier key_identifier = (Rml::Input::KeyIdentifier)luaL_checkinteger(L, 1);
  171. int key_modifier_state = luaL_checkinteger(L, 2);
  172. lua_pushboolean(L, obj->ProcessKeyUp(key_identifier, key_modifier_state));
  173. return 1;
  174. }
  175. int ContextProcessTextInput(lua_State *L, Context *obj)
  176. {
  177. const char *text = NULL;
  178. int character = -1;
  179. if (lua_isstring(L, 1))
  180. {
  181. text = lua_tostring(L, 1);
  182. } else
  183. {
  184. character = luaL_checkinteger(L, 1);
  185. }
  186. if (character > 0)
  187. {
  188. lua_pushboolean(L, obj->ProcessTextInput((char)character));
  189. }
  190. if (text != NULL)
  191. {
  192. lua_pushboolean(L, obj->ProcessTextInput(text));
  193. }
  194. return 1;
  195. }
  196. //getters
  197. int ContextGetAttrdimensions(lua_State* L)
  198. {
  199. Context* cont = LuaType<Context>::check(L,1);
  200. Vector2i* dim = new Vector2i(cont->GetDimensions());
  201. LuaType<Vector2i>::push(L,dim,true);
  202. return 1;
  203. }
  204. //returns a table of everything
  205. int ContextGetAttrdocuments(lua_State* L)
  206. {
  207. Context* cont = LuaType<Context>::check(L,1);
  208. RMLUI_CHECK_OBJ(cont);
  209. ContextDocumentsProxy* cdp = new ContextDocumentsProxy();
  210. cdp->owner = cont;
  211. LuaType<ContextDocumentsProxy>::push(L,cdp,true); //does get garbage collected (deleted)
  212. return 1;
  213. }
  214. int ContextGetAttrfocus_element(lua_State* L)
  215. {
  216. Context* cont = LuaType<Context>::check(L,1);
  217. RMLUI_CHECK_OBJ(cont);
  218. LuaType<Element>::push(L,cont->GetFocusElement());
  219. return 1;
  220. }
  221. int ContextGetAttrhover_element(lua_State* L)
  222. {
  223. Context* cont = LuaType<Context>::check(L,1);
  224. RMLUI_CHECK_OBJ(cont);
  225. LuaType<Element>::push(L,cont->GetHoverElement());
  226. return 1;
  227. }
  228. int ContextGetAttrname(lua_State* L)
  229. {
  230. Context* cont = LuaType<Context>::check(L,1);
  231. RMLUI_CHECK_OBJ(cont);
  232. lua_pushstring(L,cont->GetName().c_str());
  233. return 1;
  234. }
  235. int ContextGetAttrroot_element(lua_State* L)
  236. {
  237. Context* cont = LuaType<Context>::check(L,1);
  238. RMLUI_CHECK_OBJ(cont);
  239. LuaType<Element>::push(L,cont->GetRootElement());
  240. return 1;
  241. }
  242. //setters
  243. int ContextSetAttrdimensions(lua_State* L)
  244. {
  245. Context* cont = LuaType<Context>::check(L,1);
  246. RMLUI_CHECK_OBJ(cont);
  247. Vector2i* dim = LuaType<Vector2i>::check(L,2);
  248. cont->SetDimensions(*dim);
  249. return 0;
  250. }
  251. RegType<Context> ContextMethods[] =
  252. {
  253. RMLUI_LUAMETHOD(Context,AddEventListener)
  254. RMLUI_LUAMETHOD(Context,CreateDocument)
  255. RMLUI_LUAMETHOD(Context,LoadDocument)
  256. RMLUI_LUAMETHOD(Context,Render)
  257. RMLUI_LUAMETHOD(Context,UnloadAllDocuments)
  258. RMLUI_LUAMETHOD(Context,UnloadDocument)
  259. RMLUI_LUAMETHOD(Context,Update)
  260. RMLUI_LUAMETHOD(Context,OpenDataModel)
  261. // todo: CloseDataModel
  262. RMLUI_LUAMETHOD(Context,ProcessMouseMove)
  263. RMLUI_LUAMETHOD(Context,ProcessMouseButtonDown)
  264. RMLUI_LUAMETHOD(Context,ProcessMouseButtonUp)
  265. RMLUI_LUAMETHOD(Context,ProcessMouseWheel)
  266. RMLUI_LUAMETHOD(Context,ProcessMouseLeave)
  267. RMLUI_LUAMETHOD(Context,IsMouseInteracting)
  268. RMLUI_LUAMETHOD(Context,ProcessKeyDown)
  269. RMLUI_LUAMETHOD(Context,ProcessKeyUp)
  270. RMLUI_LUAMETHOD(Context,ProcessTextInput)
  271. { nullptr, nullptr },
  272. };
  273. luaL_Reg ContextGetters[] =
  274. {
  275. RMLUI_LUAGETTER(Context,dimensions)
  276. RMLUI_LUAGETTER(Context,documents)
  277. RMLUI_LUAGETTER(Context,focus_element)
  278. RMLUI_LUAGETTER(Context,hover_element)
  279. RMLUI_LUAGETTER(Context,name)
  280. RMLUI_LUAGETTER(Context,root_element)
  281. { nullptr, nullptr },
  282. };
  283. luaL_Reg ContextSetters[] =
  284. {
  285. RMLUI_LUASETTER(Context,dimensions)
  286. { nullptr, nullptr },
  287. };
  288. RMLUI_LUATYPE_DEFINE(Context)
  289. } // namespace Lua
  290. } // namespace Rml