Context.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. #include "Context.h"
  2. #include "ContextDocumentsProxy.h"
  3. #include "LuaDataModel.h"
  4. #include "LuaEventListener.h"
  5. #include <RmlUi/Core/Context.h>
  6. #include <RmlUi/Core/ElementDocument.h>
  7. #include <RmlUi/Core/Factory.h>
  8. namespace Rml {
  9. namespace Lua {
  10. typedef ElementDocument Document;
  11. template <>
  12. void ExtraInit<Context>(lua_State* /*L*/, int /*metatable_index*/)
  13. {
  14. return;
  15. }
  16. // methods
  17. int ContextAddEventListener(lua_State* L, Context* obj)
  18. {
  19. // need to make an EventListener for Lua before I can do anything else
  20. const char* evt = luaL_checkstring(L, 1); // event
  21. Element* element = nullptr;
  22. bool capturephase = false;
  23. // get the rest of the stuff needed to construct the listener
  24. if (lua_gettop(L) > 2)
  25. {
  26. if (!lua_isnoneornil(L, 3))
  27. element = LuaType<Element>::check(L, 3);
  28. if (!lua_isnoneornil(L, 4))
  29. capturephase = RMLUI_CHECK_BOOL(L, 4);
  30. }
  31. int type = lua_type(L, 2);
  32. if (type == LUA_TFUNCTION)
  33. {
  34. if (element)
  35. element->AddEventListener(evt, new LuaEventListener(L, 2, element), capturephase);
  36. else
  37. obj->AddEventListener(evt, new LuaEventListener(L, 2, nullptr), capturephase);
  38. }
  39. else if (type == LUA_TSTRING)
  40. {
  41. if (element)
  42. element->AddEventListener(evt, new LuaEventListener(luaL_checkstring(L, 2), element), capturephase);
  43. else
  44. obj->AddEventListener(evt, new LuaEventListener(luaL_checkstring(L, 2), nullptr), capturephase);
  45. }
  46. else
  47. {
  48. 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",
  49. lua_typename(L, type));
  50. }
  51. return 0;
  52. }
  53. int ContextCreateDocument(lua_State* L, Context* obj)
  54. {
  55. const char* tag;
  56. if (lua_gettop(L) < 1)
  57. tag = "body";
  58. else
  59. tag = luaL_checkstring(L, 1);
  60. Document* doc = obj->CreateDocument(tag);
  61. LuaType<Document>::push(L, doc, false);
  62. return 1;
  63. }
  64. int ContextLoadDocument(lua_State* L, Context* obj)
  65. {
  66. const char* path = luaL_checkstring(L, 1);
  67. Document* doc = obj->LoadDocument(path);
  68. LuaType<Document>::push(L, doc, false);
  69. return 1;
  70. }
  71. int ContextRender(lua_State* L, Context* obj)
  72. {
  73. lua_pushboolean(L, obj->Render());
  74. return 1;
  75. }
  76. int ContextUnloadAllDocuments(lua_State* /*L*/, Context* obj)
  77. {
  78. obj->UnloadAllDocuments();
  79. return 0;
  80. }
  81. int ContextUnloadDocument(lua_State* L, Context* obj)
  82. {
  83. Document* doc = LuaType<Document>::check(L, 1);
  84. obj->UnloadDocument(doc);
  85. return 0;
  86. }
  87. int ContextUpdate(lua_State* L, Context* obj)
  88. {
  89. lua_pushboolean(L, obj->Update());
  90. return 1;
  91. }
  92. int ContextOpenDataModel(lua_State* L, Context* obj)
  93. {
  94. if (!OpenLuaDataModel(L, obj, 1, 2))
  95. {
  96. // Open fails
  97. lua_pushboolean(L, false);
  98. }
  99. return 1;
  100. }
  101. // input
  102. int ContextProcessMouseMove(lua_State* L, Context* obj)
  103. {
  104. int x = (int)luaL_checkinteger(L, 1);
  105. int y = (int)luaL_checkinteger(L, 2);
  106. int flags = (int)luaL_checkinteger(L, 3);
  107. lua_pushboolean(L, obj->ProcessMouseMove(x, y, flags));
  108. return 1;
  109. }
  110. int ContextProcessMouseButtonDown(lua_State* L, Context* obj)
  111. {
  112. int button_index = (int)luaL_checkinteger(L, 1);
  113. int key_modifier_state = (int)luaL_checkinteger(L, 2);
  114. lua_pushboolean(L, obj->ProcessMouseButtonDown(button_index, key_modifier_state));
  115. return 1;
  116. }
  117. int ContextProcessMouseButtonUp(lua_State* L, Context* obj)
  118. {
  119. int button_index = (int)luaL_checkinteger(L, 1);
  120. int key_modifier_state = (int)luaL_checkinteger(L, 2);
  121. lua_pushboolean(L, obj->ProcessMouseButtonUp(button_index, key_modifier_state));
  122. return 1;
  123. }
  124. int ContextProcessMouseWheel(lua_State* L, Context* obj)
  125. {
  126. float wheel_delta = (float)luaL_checknumber(L, 1);
  127. int key_modifier_state = (int)luaL_checkinteger(L, 2);
  128. lua_pushboolean(L, obj->ProcessMouseWheel(wheel_delta, key_modifier_state));
  129. return 1;
  130. }
  131. int ContextProcessMouseLeave(lua_State* L, Context* obj)
  132. {
  133. lua_pushboolean(L, obj->ProcessMouseLeave());
  134. return 1;
  135. }
  136. int ContextIsMouseInteracting(lua_State* L, Context* obj)
  137. {
  138. lua_pushboolean(L, obj->IsMouseInteracting());
  139. return 1;
  140. }
  141. int ContextProcessKeyDown(lua_State* L, Context* obj)
  142. {
  143. Rml::Input::KeyIdentifier key_identifier = (Rml::Input::KeyIdentifier)luaL_checkinteger(L, 1);
  144. int key_modifier_state = (int)luaL_checkinteger(L, 2);
  145. lua_pushboolean(L, obj->ProcessKeyDown(key_identifier, key_modifier_state));
  146. return 1;
  147. }
  148. int ContextProcessKeyUp(lua_State* L, Context* obj)
  149. {
  150. Rml::Input::KeyIdentifier key_identifier = (Rml::Input::KeyIdentifier)luaL_checkinteger(L, 1);
  151. int key_modifier_state = (int)luaL_checkinteger(L, 2);
  152. lua_pushboolean(L, obj->ProcessKeyUp(key_identifier, key_modifier_state));
  153. return 1;
  154. }
  155. int ContextProcessTextInput(lua_State* L, Context* obj)
  156. {
  157. const char* text = NULL;
  158. int character = -1;
  159. if (lua_isstring(L, 1))
  160. text = lua_tostring(L, 1);
  161. else
  162. character = (int)luaL_checkinteger(L, 1);
  163. if (character > 0)
  164. lua_pushboolean(L, obj->ProcessTextInput((char)character));
  165. else if (text != NULL)
  166. lua_pushboolean(L, obj->ProcessTextInput(text));
  167. else
  168. Log::Message(Log::LT_WARNING, "Could not process text input on context '%s'.", obj->GetName().c_str());
  169. return 1;
  170. }
  171. // getters
  172. int ContextGetAttrdimensions(lua_State* L)
  173. {
  174. Context* cont = LuaType<Context>::check(L, 1);
  175. Vector2i* dim = new Vector2i(cont->GetDimensions());
  176. LuaType<Vector2i>::push(L, dim, true);
  177. return 1;
  178. }
  179. // returns a table of everything
  180. int ContextGetAttrdocuments(lua_State* L)
  181. {
  182. Context* cont = LuaType<Context>::check(L, 1);
  183. RMLUI_CHECK_OBJ(cont);
  184. ContextDocumentsProxy* cdp = new ContextDocumentsProxy();
  185. cdp->owner = cont;
  186. LuaType<ContextDocumentsProxy>::push(L, cdp, true); // does get garbage collected (deleted)
  187. return 1;
  188. }
  189. int ContextGetAttrdp_ratio(lua_State* L)
  190. {
  191. Context* cont = LuaType<Context>::check(L, 1);
  192. float dp_ratio = cont->GetDensityIndependentPixelRatio();
  193. lua_pushnumber(L, dp_ratio);
  194. return 1;
  195. }
  196. int ContextGetAttrfocus_element(lua_State* L)
  197. {
  198. Context* cont = LuaType<Context>::check(L, 1);
  199. RMLUI_CHECK_OBJ(cont);
  200. LuaType<Element>::push(L, cont->GetFocusElement());
  201. return 1;
  202. }
  203. int ContextGetAttrhover_element(lua_State* L)
  204. {
  205. Context* cont = LuaType<Context>::check(L, 1);
  206. RMLUI_CHECK_OBJ(cont);
  207. LuaType<Element>::push(L, cont->GetHoverElement());
  208. return 1;
  209. }
  210. int ContextGetAttrname(lua_State* L)
  211. {
  212. Context* cont = LuaType<Context>::check(L, 1);
  213. RMLUI_CHECK_OBJ(cont);
  214. lua_pushstring(L, cont->GetName().c_str());
  215. return 1;
  216. }
  217. int ContextGetAttrroot_element(lua_State* L)
  218. {
  219. Context* cont = LuaType<Context>::check(L, 1);
  220. RMLUI_CHECK_OBJ(cont);
  221. LuaType<Element>::push(L, cont->GetRootElement());
  222. return 1;
  223. }
  224. // setters
  225. int ContextSetAttrdimensions(lua_State* L)
  226. {
  227. Context* cont = LuaType<Context>::check(L, 1);
  228. RMLUI_CHECK_OBJ(cont);
  229. Vector2i* dim = LuaType<Vector2i>::check(L, 2);
  230. cont->SetDimensions(*dim);
  231. return 0;
  232. }
  233. int ContextSetAttrdp_ratio(lua_State* L)
  234. {
  235. Context* cont = LuaType<Context>::check(L, 1);
  236. RMLUI_CHECK_OBJ(cont);
  237. lua_Number dp_ratio = luaL_checknumber(L, 2);
  238. cont->SetDensityIndependentPixelRatio((float)dp_ratio);
  239. return 0;
  240. }
  241. RegType<Context> ContextMethods[] = {
  242. RMLUI_LUAMETHOD(Context, AddEventListener),
  243. RMLUI_LUAMETHOD(Context, CreateDocument),
  244. RMLUI_LUAMETHOD(Context, LoadDocument),
  245. RMLUI_LUAMETHOD(Context, Render),
  246. RMLUI_LUAMETHOD(Context, UnloadAllDocuments),
  247. RMLUI_LUAMETHOD(Context, UnloadDocument),
  248. RMLUI_LUAMETHOD(Context, Update),
  249. RMLUI_LUAMETHOD(Context, OpenDataModel),
  250. // todo: CloseDataModel
  251. RMLUI_LUAMETHOD(Context, ProcessMouseMove),
  252. RMLUI_LUAMETHOD(Context, ProcessMouseButtonDown),
  253. RMLUI_LUAMETHOD(Context, ProcessMouseButtonUp),
  254. RMLUI_LUAMETHOD(Context, ProcessMouseWheel),
  255. RMLUI_LUAMETHOD(Context, ProcessMouseLeave),
  256. RMLUI_LUAMETHOD(Context, IsMouseInteracting),
  257. RMLUI_LUAMETHOD(Context, ProcessKeyDown),
  258. RMLUI_LUAMETHOD(Context, ProcessKeyUp),
  259. RMLUI_LUAMETHOD(Context, ProcessTextInput),
  260. {nullptr, nullptr},
  261. };
  262. luaL_Reg ContextGetters[] = {
  263. RMLUI_LUAGETTER(Context, dimensions),
  264. RMLUI_LUAGETTER(Context, documents),
  265. RMLUI_LUAGETTER(Context, dp_ratio),
  266. RMLUI_LUAGETTER(Context, focus_element),
  267. RMLUI_LUAGETTER(Context, hover_element),
  268. RMLUI_LUAGETTER(Context, name),
  269. RMLUI_LUAGETTER(Context, root_element),
  270. {nullptr, nullptr},
  271. };
  272. luaL_Reg ContextSetters[] = {
  273. RMLUI_LUASETTER(Context, dimensions),
  274. RMLUI_LUASETTER(Context, dp_ratio),
  275. {nullptr, nullptr},
  276. };
  277. RMLUI_LUATYPE_DEFINE(Context)
  278. } // namespace Lua
  279. } // namespace Rml