Context.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #include "precompiled.h"
  2. #include "Context.h"
  3. #include <Rocket/Core/Context.h>
  4. #include <Rocket/Core/ElementDocument.h>
  5. namespace Rocket {
  6. namespace Core {
  7. namespace Lua {
  8. typedef Rocket::Core::ElementDocument Document;
  9. //methods
  10. int ContextAddEventListener(lua_State* L, Context* obj)
  11. {
  12. //need to make an EventListener for Lua before I can do anything else
  13. return 0;
  14. }
  15. int ContextAddMouseCursor(lua_State* L, Context* obj)
  16. {
  17. Document* cursor_doc = LuaType<Document>::check(L,1);
  18. obj->AddMouseCursor(cursor_doc);
  19. return 0;
  20. }
  21. int ContextCreateDocument(lua_State* L, Context* obj)
  22. {
  23. const char* tag;
  24. if(lua_gettop(L) < 1)
  25. tag = "body";
  26. else
  27. tag = luaL_checkstring(L,1);
  28. Document* doc = obj->CreateDocument(tag);
  29. LuaType<Document>::push(L,doc,true);
  30. return 1;
  31. }
  32. int ContextLoadDocument(lua_State* L, Context* obj)
  33. {
  34. const char* path = luaL_checkstring(L,1);
  35. Document* doc = obj->LoadDocument(path);
  36. LuaType<Document>::push(L,doc,true);
  37. return 1;
  38. }
  39. int ContextLoadMouseCursor(lua_State* L, Context* obj)
  40. {
  41. const char* path = luaL_checkstring(L,1);
  42. Document* doc = obj->LoadMouseCursor(path);
  43. LuaType<Document>::push(L,doc);
  44. return 1;
  45. }
  46. int ContextRender(lua_State* L, Context* obj)
  47. {
  48. lua_pushboolean(L,obj->Render());
  49. return 1;
  50. }
  51. int ContextShowMouseCursor(lua_State* L, Context* obj)
  52. {
  53. bool show = CHECK_BOOL(L,1);
  54. obj->ShowMouseCursor(show);
  55. return 0;
  56. }
  57. int ContextUnloadAllDocuments(lua_State* L, Context* obj)
  58. {
  59. obj->UnloadAllDocuments();
  60. return 0;
  61. }
  62. int ContextUnloadAllMouseCursors(lua_State* L, Context* obj)
  63. {
  64. obj->UnloadAllMouseCursors();
  65. return 0;
  66. }
  67. int ContextUnloadDocument(lua_State* L, Context* obj)
  68. {
  69. Document* doc = LuaType<Document>::check(L,1);
  70. obj->UnloadDocument(doc);
  71. return 0;
  72. }
  73. int ContextUnloadMouseCursor(lua_State* L, Context* obj)
  74. {
  75. const char* name = luaL_checkstring(L,1);
  76. obj->UnloadMouseCursor(name);
  77. return 0;
  78. }
  79. int ContextUpdate(lua_State* L, Context* obj)
  80. {
  81. lua_pushboolean(L,obj->Update());
  82. return 1;
  83. }
  84. //getters
  85. int ContextGetAttrdimensions(lua_State* L)
  86. {
  87. Context* cont = LuaType<Context>::check(L,1);
  88. const Vector2i* dim = &cont->GetDimensions();
  89. //const_cast-ing so that the user can do dimensions.x = 3 and it will actually change the dimensions
  90. //of the context
  91. LuaType<Vector2i>::push(L,const_cast<Vector2i*>(dim));
  92. return 1;
  93. }
  94. //returns a table of everything
  95. int ContextGetAttrdocuments(lua_State* L)
  96. {
  97. Context* cont = LuaType<Context>::check(L,1);
  98. Element* root = cont->GetRootElement();
  99. lua_newtable(L);
  100. int tableindex = lua_gettop(L);
  101. for(int i = 0; i < root->GetNumChildren(); i++)
  102. {
  103. Document* doc = root->GetChild(i)->GetOwnerDocument();
  104. if(doc == NULL)
  105. continue;
  106. LuaType<Document>::push(L,doc);
  107. lua_setfield(L, tableindex,doc->GetId().CString());
  108. /* //is this a bad idea?
  109. lua_pushinteger(L,i);
  110. lua_setfield(L,tableindex,doc->GetId().CString());
  111. */
  112. }
  113. return 1;
  114. }
  115. int ContextGetAttrfocus_element(lua_State* L)
  116. {
  117. Context* cont = LuaType<Context>::check(L,1);
  118. LuaType<Element>::push(L,cont->GetFocusElement());
  119. return 1;
  120. }
  121. int ContextGetAttrhover_element(lua_State* L)
  122. {
  123. Context* cont = LuaType<Context>::check(L,1);
  124. LuaType<Element>::push(L,cont->GetHoverElement());
  125. return 1;
  126. }
  127. int ContextGetAttrname(lua_State* L)
  128. {
  129. Context* cont = LuaType<Context>::check(L,1);
  130. lua_pushstring(L,cont->GetName().CString());
  131. return 1;
  132. }
  133. int ContextGetAttrroot_element(lua_State* L)
  134. {
  135. Context* cont = LuaType<Context>::check(L,1);
  136. LuaType<Element>::push(L,cont->GetRootElement());
  137. return 1;
  138. }
  139. //setters
  140. int ContextSetAttrdimensions(lua_State* L)
  141. {
  142. Context* cont = LuaType<Context>::check(L,1);
  143. Vector2i* dim = LuaType<Vector2i>::check(L,2);
  144. cont->SetDimensions(*dim);
  145. return 0;
  146. }
  147. RegType<Context> ContextMethods[] =
  148. {
  149. LUAMETHOD(Context,AddEventListener)
  150. LUAMETHOD(Context,AddMouseCursor)
  151. LUAMETHOD(Context,CreateDocument)
  152. LUAMETHOD(Context,LoadDocument)
  153. LUAMETHOD(Context,LoadMouseCursor)
  154. LUAMETHOD(Context,Render)
  155. LUAMETHOD(Context,ShowMouseCursor)
  156. LUAMETHOD(Context,UnloadAllDocuments)
  157. LUAMETHOD(Context,UnloadAllMouseCursors)
  158. LUAMETHOD(Context,UnloadDocument)
  159. LUAMETHOD(Context,UnloadMouseCursor)
  160. LUAMETHOD(Context,Update)
  161. { NULL, NULL },
  162. };
  163. luaL_reg ContextGetters[] =
  164. {
  165. LUAGETTER(Context,dimensions)
  166. LUAGETTER(Context,documents)
  167. LUAGETTER(Context,focus_element)
  168. LUAGETTER(Context,hover_element)
  169. LUAGETTER(Context,name)
  170. LUAGETTER(Context,root_element)
  171. { NULL, NULL },
  172. };
  173. luaL_reg ContextSetters[] =
  174. {
  175. LUASETTER(Context,dimensions)
  176. { NULL, NULL },
  177. };
  178. /*
  179. template<> const char* GetTClassName<Context>() { return "Context"; }
  180. template<> RegType<Context>* GetMethodTable<Context>() { return ContextMethods; }
  181. template<> luaL_reg* GetAttrTable<Context>() { return ContextGetters; }
  182. template<> luaL_reg* SetAttrTable<Context>() { return ContextSetters; }
  183. */
  184. }
  185. }
  186. }