bitetti

LRocket.cpp - self LibRocket & Lua integration

Oct 26th, 2011
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.86 KB | None | 0 0
  1. /*
  2.  * LRocket.cpp
  3.  *
  4.  *  Created on: 19/10/2011
  5.  *      Author: bitetti
  6.  */
  7.  
  8. #include "LRocket.h"
  9.  
  10. namespace WWP {
  11.  
  12. /**
  13.  * Registros de Lua
  14.  */
  15. const luaL_reg LRocket::meta_methods[] = {
  16.     /*{"__gc", image_destroy },*/
  17.     { 0, 0 }
  18. };
  19.  
  20. const luaL_reg LRocket::class_methods[] = {
  21.     {"initLibRocket", initLibRocket },
  22.     {"showCursor", showCursor},
  23.     {"loadDocument", loadDocument},
  24.     {"unloadDocument", unloadDocument},
  25.     {"setInnerHTML", setInnerHTML},
  26.     {"getInnerHTML", getInnerHTML},
  27.     {"showDocument", showDocument},
  28.     {"hideDocument", hideDocument},
  29.     {"getFormElementValue", getFormElementValue},
  30.     { 0, 0 }
  31. };
  32.  
  33. LRocket* LRocket::main = 0;
  34.  
  35. lua_State* LRocket::Lua = 0;
  36.  
  37. Rocket::Core::ElementDocument* LRocket::cursor = 0;
  38.  
  39. std::list<Rocket_Document*> LRocket::documentos;
  40.  
  41. LRocket::LRocket() :
  42.     m_rkContext(0),
  43.     m_rkFileInterface(0),
  44.     m_rkOgreRenderer(0),
  45.     m_rkRenderListener(0),
  46.     m_rkEventListener(0),
  47.     m_document(0)
  48. {
  49.     // TODO Auto-generated constructor stub
  50.  
  51. }
  52.  
  53. LRocket::~LRocket() {
  54.  
  55.     std::list<Rocket_Document*>::iterator it = documentos.begin();
  56.     for(; it != documentos.end(); it++)
  57.         delete ((Rocket_Document*) *it)->document;
  58.  
  59.  
  60.     documentos.clear();
  61.  
  62.     if (m_document) m_document->RemoveReference();
  63.         m_document = 0;
  64.  
  65.     // Shutdown Rocket.
  66.     if (m_rkContext) m_rkContext->RemoveReference();
  67.     m_rkContext = 0;
  68.     Rocket::Core::Shutdown();
  69.  
  70.     if (m_rkOgreSystem) { delete m_rkOgreSystem; m_rkOgreSystem = 0; }
  71.     if (m_rkOgreRenderer) { delete m_rkOgreRenderer; m_rkOgreRenderer = 0; }
  72.     if (m_rkFileInterface) { delete m_rkFileInterface; m_rkFileInterface = 0; }
  73.  
  74.     if (m_rkEventListener) { delete m_rkEventListener; m_rkEventListener = 0; }
  75.     if (m_rkRenderListener) { delete m_rkRenderListener; m_rkRenderListener = 0; }
  76. }
  77.  
  78.  
  79. void LRocket::registrarFuncoes(lua_State* L)
  80. {
  81.     int metatable, methods;
  82.  
  83.     lua_pushliteral(L, LROCKET_CLASS );                 //Nome da classe em Lua
  84.     methods   = newtable(L);                            //Tabela de metodos
  85.     metatable = newtable(L);                            //SQLite class
  86.     lua_pushliteral(L, "__index");                      //add index event to metatable
  87.     lua_pushvalue(L, methods);                          //
  88.     lua_settable(L, metatable);                         //metatable.__index = methods
  89.     lua_pushliteral(L, "__metatable");                  //esconde metatable
  90.     lua_pushvalue(L, methods);
  91.     lua_settable(L, metatable);                         //metatable.__metatable = methods
  92.     luaL_openlib(L, 0, meta_methods,  0);               //preenche metatable
  93.     luaL_openlib(L, 0, class_methods, 1);
  94.  
  95.     //lua_pushliteral(L,"wwp_net_version");
  96.     //lua_pushstring(L, "1.0" );
  97.     //lua_settable(L,-3);
  98.  
  99.     lua_settable(L, LUA_GLOBALSINDEX);
  100. }
  101.  
  102. void LRocket::finaliza()
  103. {
  104.     if (main) delete main;
  105. }
  106.  
  107. void LRocket::ProcessEvent(Rocket::Core::Event& event)
  108. {
  109.     gkString type = event.GetType().CString();
  110.     std::cout<<type<<"\n";
  111.     //GK_ASSERT(m_document);
  112.     Rocket::Core::Element* elm = event.GetTargetElement();//  m_document->GetElementById(DEMO_PAGE_INPUT_ID);
  113.     if (elm)
  114.     {
  115.         const char* base = static_cast<Rocket::Controls::ElementFormControl*>(elm)->GetValue().CString();
  116.         int len = std::strlen(base);
  117.         if (len > 11 )
  118.         {
  119.             char str[11];
  120.             str[0] = base[0];
  121.             str[1] = base[1];
  122.             str[2] = base[2];
  123.             str[3] = base[3];
  124.             str[4] = base[4];
  125.             str[5] = base[5];
  126.             str[6] = base[6];
  127.             str[7] = base[7];
  128.             str[8] = base[8];
  129.             str[9] = base[9];
  130.             str[10] = 0;
  131.             base += 10;
  132.  
  133.             int p=10;
  134.             gkString function = "";
  135.             while( p<len && base[0]!=',')
  136.             {
  137.                 p++;
  138.                 function.append(1,base[0]);
  139.                 base ++;
  140.             }
  141.             base++;
  142.  
  143.             //std::cout<<function.c_str()<<": "<<base<<"\n";
  144.             if (std::strlen(base)<len)
  145.             {
  146.                 lua_getfield( LRocket::Lua , LUA_GLOBALSINDEX, function.c_str());
  147.                 lua_pushstring(LRocket::Lua, base);
  148.                 lua_call(LRocket::Lua, 1, 0);
  149.             }
  150.         }
  151.  
  152.         //printf("button clicked: %s\n", full.c_str());
  153.     }
  154. }
  155.  
  156. int LRocket::initLibRocket( lua_State* L)
  157. {
  158.  
  159.     main = new LRocket();
  160.  
  161.     Lua = L;
  162.  
  163.     //prepara resource
  164.     Ogre::ResourceGroupManager::getSingleton().createResourceGroup("Rocket",true);
  165.     //gkBlendLoader::getSingleton().loadFile(gkUtils::getFile("./media/biblioteca_principal.blend"), "Scene", "Rocket");
  166.  
  167.     main->window = gkEngine::getSingleton().getActiveScene()->getDisplayWindow();
  168.     main->m_rkOgreRenderer = new RenderInterfaceOgre3D(main->window->getWidth(), main->window->getHeight());
  169.     Rocket::Core::SetRenderInterface(main->m_rkOgreRenderer);
  170.  
  171.     main->m_rkOgreSystem = new SystemInterfaceOgre3D();
  172.     Rocket::Core::SetSystemInterface(main->m_rkOgreSystem);
  173.  
  174.     Rocket::Core::Initialise();
  175.     Rocket::Controls::Initialise();
  176.  
  177.     installRocketFonts();
  178.  
  179.     main->m_rkContext = Rocket::Core::CreateContext(
  180.             "main",
  181.             Rocket::Core::Vector2i(main->window->getWidth(), main->window->getHeight())
  182.         );
  183.     Rocket::Debugger::Initialise(main->m_rkContext);
  184.  
  185.     main->m_rkFileInterface = new FileInterfaceOgre3D();
  186.     Rocket::Core::SetFileInterface(main->m_rkFileInterface);
  187.  
  188.  
  189.     // Load the mouse cursor and release the caller's reference.
  190.     cursor = main->m_rkContext->LoadMouseCursor("cursor.rml");
  191.     if (cursor)
  192.         cursor->RemoveReference();
  193.  
  194.     main->m_rkEventListener  = new RocketEventListener(main->window, main->m_rkContext);
  195.     main->m_rkRenderListener = new RocketRenderListener(main->window->getRenderWindow(), main->window->getRenderScene()->getManager(), main->m_rkContext);
  196.  
  197.     return 0;
  198. }
  199.  
  200. int LRocket::showCursor( lua_State* L)
  201. {
  202.     int stack = lua_gettop(L);
  203.     if(stack>0)
  204.     {
  205.         if (lua_isboolean(L,1))
  206.             if (cursor)
  207.                 if (lua_toboolean(L,1))
  208.                     cursor->Show(0);
  209.                 else
  210.                     cursor->Hide();
  211.     }
  212.     return 0;
  213. }
  214.  
  215. int  LRocket::loadDocument(lua_State* L)
  216. {
  217.     int stack = lua_gettop(L);
  218.     if (stack>0 && lua_isstring(L,1))
  219.     {
  220.         Rocket::Core::ElementDocument* document;
  221.         document = main->m_rkContext->LoadDocument(lua_tostring(L,1));
  222.  
  223.         if (document)
  224.         {
  225.             gkString nome = lua_tostring(L,1);
  226.             gkString call = "";
  227.             if (stack>1)
  228.                 call = lua_tostring(L,2);
  229.  
  230.             Rocket::Core::ElementList lista;
  231.             document->GetElementsByTagName(lista,"input");
  232.             Rocket::Core::ElementList::iterator it;
  233.  
  234.             if (lista.size()>0)
  235.                 for( it=lista.begin(); it != lista.end(); it++)
  236.                 {
  237.                     Rocket::Core::Element* element =(Rocket::Core::Element*) *it.base();
  238.                     element->AddEventListener("click", main);
  239.                 }
  240.  
  241.             document->Show();
  242.  
  243.             documentos.push_back( new Rocket_Document( nome.c_str(), call.c_str(), document ) );
  244.             main->m_document = document; // current document
  245.             lua_pushlightuserdata(L, (void*) document);
  246.         }
  247.         else
  248.             lua_pushnil(L);
  249.     }
  250.     return 1;
  251. }
  252.  
  253. int  LRocket::unloadDocument(lua_State* L)
  254. {
  255.     int stack = lua_gettop(L);
  256.     if (stack>0 && lua_isstring(L,1))
  257.     {
  258.         Rocket::Core::ElementDocument* document;
  259.         //document = main->m_rkContext->LoadDocument(lua_tostring(L,1));
  260.  
  261.  
  262.         //std::cout<<"boleano "<<(documentos.begin() == it)<<"\n";
  263.         for(std::list<Rocket_Document*>::iterator it = documentos.begin(); it != documentos.end(); it++)
  264.         {
  265.             if(((Rocket_Document*) *it)->name.compare(lua_tostring(L,1))==0)
  266.             {
  267.                 document = ((Rocket_Document*) *it)->document;
  268.                 if (document)
  269.                 {
  270.                     Rocket::Core::ElementList lista;
  271.                     document->GetElementsByTagName(lista,"input");
  272.                     Rocket::Core::ElementList::iterator it2;
  273.  
  274.                     if (lista.size()>0)
  275.                         for( it2=lista.begin(); it2 != lista.end(); it2++)
  276.                         {
  277.                             Rocket::Core::Element* element =(Rocket::Core::Element*) *it2.base();
  278.                             element->RemoveEventListener("click", main);
  279.                         }
  280.  
  281.                     document->Hide();
  282.                     document->RemoveReference();
  283.                     document->Close();
  284.                     delete document;
  285.                     //((Rocket_Document*) *it)->document = 0;
  286.                     std::cout<<"deletia "<<((Rocket_Document*) *it)->name.c_str()<<"\n";
  287.                     documentos.remove(*it);
  288.                 }
  289.             } }
  290.     }
  291.     return 0;
  292. }
  293.  
  294. int  LRocket::setInnerHTML(lua_State* L)
  295. {
  296.     int stack = lua_gettop(L);
  297.     if (stack>1)
  298.     {
  299.         if (lua_isstring(L,1) && lua_isstring(L,2))
  300.         {
  301.             Rocket::Core::Element* element = main->m_document->GetElementById(lua_tostring(L,1));
  302.             if (element)
  303.             {
  304.                 element->SetInnerRML( lua_tostring(L,2) );
  305.             }
  306.         }
  307.     }
  308.     return 0;
  309. }
  310.  
  311. int  LRocket::getInnerHTML(lua_State* L)
  312. {
  313.     int stack = lua_gettop(L);
  314.     if (stack>0 && lua_isstring(L,1))
  315.     {
  316.         Rocket::Core::Element* element = main->m_document->GetElementById(lua_tostring(L,1));
  317.         if (element)
  318.         {
  319.             //element->GetInnerRML(lua_tostring(L,1));
  320.         }
  321.     }
  322.     return 0;
  323. }
  324.  
  325. int  LRocket::getFormElementValue(lua_State* L)
  326. {
  327.     int stack = lua_gettop(L);
  328.     if (stack>0)
  329.     {
  330.         Rocket::Core::Element* element;
  331.         if (stack>1)
  332.         {
  333.             // modelo "formName", "fieldName"
  334.             Rocket::Core::Element* form = main->m_document->GetElementById(lua_tostring(L,1));
  335.             element = form->GetElementById(lua_tostring(L,2));
  336.         } else {
  337.             // modelo "fieldName"
  338.             element = main->m_document->GetElementById(lua_tostring(L,1));
  339.         }
  340.         if (element)
  341.         {
  342.             //element->GetInnerRML(lua_tostring(L,1));
  343.             lua_pushstring(L, static_cast<Rocket::Controls::ElementFormControl*>(element)->GetValue().CString() );
  344.  
  345.         } else {
  346.             lua_pushnil(L);
  347.         }
  348.     }
  349.     return 1;
  350. }
  351.  
  352. int  LRocket::showDocument(lua_State* L)
  353. {
  354.     int stack = lua_gettop(L);
  355.     if (stack>0 && lua_isstring(L,1))
  356.     {
  357.         Rocket::Core::ElementDocument* document = main->m_document;
  358.         for(std::list<Rocket_Document*>::iterator it = documentos.begin(); it != documentos.end(); it++)
  359.             if(((Rocket_Document*) *it)->name.compare(lua_tostring(L,1))==0)
  360.                 document = ((Rocket_Document*) *it)->document;
  361.         if (document)
  362.         {
  363.             document->Show();
  364.             main->m_document = document;
  365.         }
  366.     }
  367.     return 0;
  368. }
  369.  
  370. int  LRocket::hideDocument(lua_State* L)
  371. {
  372.     int stack = lua_gettop(L);
  373.     if (stack>0 && lua_isstring(L,1))
  374.     {
  375.         Rocket::Core::ElementDocument* document = main->m_document;
  376.         for(std::list<Rocket_Document*>::iterator it = documentos.begin(); it != documentos.end(); it++)
  377.             if(((Rocket_Document*) *it)->name.compare(lua_tostring(L,1))==0)
  378.                 document = ((Rocket_Document*) *it)->document;
  379.         if (document)
  380.         {
  381.             document->Hide();
  382.         }
  383.     }
  384.     return 0;
  385. }
  386.  
  387.  
  388. }
  389.  
  390.  
Advertisement
Add Comment
Please, Sign In to add comment