Context.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 = (int)luaL_checkinteger(L, 1);
  126. int y = (int)luaL_checkinteger(L, 2);
  127. int flags = (int)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 = (int)luaL_checkinteger(L, 1);
  134. int key_modifier_state = (int)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 = (int)luaL_checkinteger(L, 1);
  141. int key_modifier_state = (int)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 = (int)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 = (int)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. {
  171. Rml::Input::KeyIdentifier key_identifier = (Rml::Input::KeyIdentifier)luaL_checkinteger(L, 1);
  172. int key_modifier_state = (int)luaL_checkinteger(L, 2);
  173. lua_pushboolean(L, obj->ProcessKeyUp(key_identifier, key_modifier_state));
  174. return 1;
  175. }
  176. int ContextProcessTextInput(lua_State* L, Context* obj)
  177. {
  178. const char* text = NULL;
  179. int character = -1;
  180. if (lua_isstring(L, 1))
  181. text = lua_tostring(L, 1);
  182. else
  183. character = (int)luaL_checkinteger(L, 1);
  184. if (character > 0)
  185. lua_pushboolean(L, obj->ProcessTextInput((char)character));
  186. else if (text != NULL)
  187. lua_pushboolean(L, obj->ProcessTextInput(text));
  188. else
  189. Log::Message(Log::LT_WARNING, "Could not process text input on context '%s'.", obj->GetName().c_str());
  190. return 1;
  191. }
  192. //getters
  193. int ContextGetAttrdimensions(lua_State* L)
  194. {
  195. Context* cont = LuaType<Context>::check(L,1);
  196. Vector2i* dim = new Vector2i(cont->GetDimensions());
  197. LuaType<Vector2i>::push(L,dim,true);
  198. return 1;
  199. }
  200. //returns a table of everything
  201. int ContextGetAttrdocuments(lua_State* L)
  202. {
  203. Context* cont = LuaType<Context>::check(L,1);
  204. RMLUI_CHECK_OBJ(cont);
  205. ContextDocumentsProxy* cdp = new ContextDocumentsProxy();
  206. cdp->owner = cont;
  207. LuaType<ContextDocumentsProxy>::push(L,cdp,true); //does get garbage collected (deleted)
  208. return 1;
  209. }
  210. int ContextGetAttrdp_ratio(lua_State* L)
  211. {
  212. Context* cont = LuaType<Context>::check(L,1);
  213. float dp_ratio = cont->GetDensityIndependentPixelRatio();
  214. lua_pushnumber(L, dp_ratio);
  215. return 1;
  216. }
  217. int ContextGetAttrfocus_element(lua_State* L)
  218. {
  219. Context* cont = LuaType<Context>::check(L,1);
  220. RMLUI_CHECK_OBJ(cont);
  221. LuaType<Element>::push(L,cont->GetFocusElement());
  222. return 1;
  223. }
  224. int ContextGetAttrhover_element(lua_State* L)
  225. {
  226. Context* cont = LuaType<Context>::check(L,1);
  227. RMLUI_CHECK_OBJ(cont);
  228. LuaType<Element>::push(L,cont->GetHoverElement());
  229. return 1;
  230. }
  231. int ContextGetAttrname(lua_State* L)
  232. {
  233. Context* cont = LuaType<Context>::check(L,1);
  234. RMLUI_CHECK_OBJ(cont);
  235. lua_pushstring(L,cont->GetName().c_str());
  236. return 1;
  237. }
  238. int ContextGetAttrroot_element(lua_State* L)
  239. {
  240. Context* cont = LuaType<Context>::check(L,1);
  241. RMLUI_CHECK_OBJ(cont);
  242. LuaType<Element>::push(L,cont->GetRootElement());
  243. return 1;
  244. }
  245. //setters
  246. int ContextSetAttrdimensions(lua_State* L)
  247. {
  248. Context* cont = LuaType<Context>::check(L,1);
  249. RMLUI_CHECK_OBJ(cont);
  250. Vector2i* dim = LuaType<Vector2i>::check(L,2);
  251. cont->SetDimensions(*dim);
  252. return 0;
  253. }
  254. int ContextSetAttrdp_ratio(lua_State* L)
  255. {
  256. Context* cont = LuaType<Context>::check(L,1);
  257. RMLUI_CHECK_OBJ(cont);
  258. lua_Number dp_ratio = luaL_checknumber(L,2);
  259. cont->SetDensityIndependentPixelRatio((float)dp_ratio);
  260. return 0;
  261. }
  262. RegType<Context> ContextMethods[] =
  263. {
  264. RMLUI_LUAMETHOD(Context,AddEventListener)
  265. RMLUI_LUAMETHOD(Context,CreateDocument)
  266. RMLUI_LUAMETHOD(Context,LoadDocument)
  267. RMLUI_LUAMETHOD(Context,Render)
  268. RMLUI_LUAMETHOD(Context,UnloadAllDocuments)
  269. RMLUI_LUAMETHOD(Context,UnloadDocument)
  270. RMLUI_LUAMETHOD(Context,Update)
  271. RMLUI_LUAMETHOD(Context,OpenDataModel)
  272. // todo: CloseDataModel
  273. RMLUI_LUAMETHOD(Context,ProcessMouseMove)
  274. RMLUI_LUAMETHOD(Context,ProcessMouseButtonDown)
  275. RMLUI_LUAMETHOD(Context,ProcessMouseButtonUp)
  276. RMLUI_LUAMETHOD(Context,ProcessMouseWheel)
  277. RMLUI_LUAMETHOD(Context,ProcessMouseLeave)
  278. RMLUI_LUAMETHOD(Context,IsMouseInteracting)
  279. RMLUI_LUAMETHOD(Context,ProcessKeyDown)
  280. RMLUI_LUAMETHOD(Context,ProcessKeyUp)
  281. RMLUI_LUAMETHOD(Context,ProcessTextInput)
  282. { nullptr, nullptr },
  283. };
  284. luaL_Reg ContextGetters[] =
  285. {
  286. RMLUI_LUAGETTER(Context,dimensions)
  287. RMLUI_LUAGETTER(Context,documents)
  288. RMLUI_LUAGETTER(Context,dp_ratio)
  289. RMLUI_LUAGETTER(Context,focus_element)
  290. RMLUI_LUAGETTER(Context,hover_element)
  291. RMLUI_LUAGETTER(Context,name)
  292. RMLUI_LUAGETTER(Context,root_element)
  293. { nullptr, nullptr },
  294. };
  295. luaL_Reg ContextSetters[] =
  296. {
  297. RMLUI_LUASETTER(Context,dimensions)
  298. RMLUI_LUASETTER(Context,dp_ratio)
  299. { nullptr, nullptr },
  300. };
  301. RMLUI_LUATYPE_DEFINE(Context)
  302. } // namespace Lua
  303. } // namespace Rml