Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * LRocket.cpp
- *
- * Created on: 19/10/2011
- * Author: bitetti
- */
- #include "LRocket.h"
- namespace WWP {
- /**
- * Registros de Lua
- */
- const luaL_reg LRocket::meta_methods[] = {
- /*{"__gc", image_destroy },*/
- { 0, 0 }
- };
- const luaL_reg LRocket::class_methods[] = {
- {"initLibRocket", initLibRocket },
- {"showCursor", showCursor},
- {"loadDocument", loadDocument},
- {"unloadDocument", unloadDocument},
- {"setInnerHTML", setInnerHTML},
- {"getInnerHTML", getInnerHTML},
- {"showDocument", showDocument},
- {"hideDocument", hideDocument},
- {"getFormElementValue", getFormElementValue},
- { 0, 0 }
- };
- LRocket* LRocket::main = 0;
- lua_State* LRocket::Lua = 0;
- Rocket::Core::ElementDocument* LRocket::cursor = 0;
- std::list<Rocket_Document*> LRocket::documentos;
- LRocket::LRocket() :
- m_rkContext(0),
- m_rkFileInterface(0),
- m_rkOgreRenderer(0),
- m_rkRenderListener(0),
- m_rkEventListener(0),
- m_document(0)
- {
- // TODO Auto-generated constructor stub
- }
- LRocket::~LRocket() {
- std::list<Rocket_Document*>::iterator it = documentos.begin();
- for(; it != documentos.end(); it++)
- delete ((Rocket_Document*) *it)->document;
- documentos.clear();
- if (m_document) m_document->RemoveReference();
- m_document = 0;
- // Shutdown Rocket.
- if (m_rkContext) m_rkContext->RemoveReference();
- m_rkContext = 0;
- Rocket::Core::Shutdown();
- if (m_rkOgreSystem) { delete m_rkOgreSystem; m_rkOgreSystem = 0; }
- if (m_rkOgreRenderer) { delete m_rkOgreRenderer; m_rkOgreRenderer = 0; }
- if (m_rkFileInterface) { delete m_rkFileInterface; m_rkFileInterface = 0; }
- if (m_rkEventListener) { delete m_rkEventListener; m_rkEventListener = 0; }
- if (m_rkRenderListener) { delete m_rkRenderListener; m_rkRenderListener = 0; }
- }
- void LRocket::registrarFuncoes(lua_State* L)
- {
- int metatable, methods;
- lua_pushliteral(L, LROCKET_CLASS ); //Nome da classe em Lua
- methods = newtable(L); //Tabela de metodos
- metatable = newtable(L); //SQLite class
- lua_pushliteral(L, "__index"); //add index event to metatable
- lua_pushvalue(L, methods); //
- lua_settable(L, metatable); //metatable.__index = methods
- lua_pushliteral(L, "__metatable"); //esconde metatable
- lua_pushvalue(L, methods);
- lua_settable(L, metatable); //metatable.__metatable = methods
- luaL_openlib(L, 0, meta_methods, 0); //preenche metatable
- luaL_openlib(L, 0, class_methods, 1);
- //lua_pushliteral(L,"wwp_net_version");
- //lua_pushstring(L, "1.0" );
- //lua_settable(L,-3);
- lua_settable(L, LUA_GLOBALSINDEX);
- }
- void LRocket::finaliza()
- {
- if (main) delete main;
- }
- void LRocket::ProcessEvent(Rocket::Core::Event& event)
- {
- gkString type = event.GetType().CString();
- std::cout<<type<<"\n";
- //GK_ASSERT(m_document);
- Rocket::Core::Element* elm = event.GetTargetElement();// m_document->GetElementById(DEMO_PAGE_INPUT_ID);
- if (elm)
- {
- const char* base = static_cast<Rocket::Controls::ElementFormControl*>(elm)->GetValue().CString();
- int len = std::strlen(base);
- if (len > 11 )
- {
- char str[11];
- str[0] = base[0];
- str[1] = base[1];
- str[2] = base[2];
- str[3] = base[3];
- str[4] = base[4];
- str[5] = base[5];
- str[6] = base[6];
- str[7] = base[7];
- str[8] = base[8];
- str[9] = base[9];
- str[10] = 0;
- base += 10;
- int p=10;
- gkString function = "";
- while( p<len && base[0]!=',')
- {
- p++;
- function.append(1,base[0]);
- base ++;
- }
- base++;
- //std::cout<<function.c_str()<<": "<<base<<"\n";
- if (std::strlen(base)<len)
- {
- lua_getfield( LRocket::Lua , LUA_GLOBALSINDEX, function.c_str());
- lua_pushstring(LRocket::Lua, base);
- lua_call(LRocket::Lua, 1, 0);
- }
- }
- //printf("button clicked: %s\n", full.c_str());
- }
- }
- int LRocket::initLibRocket( lua_State* L)
- {
- main = new LRocket();
- Lua = L;
- //prepara resource
- Ogre::ResourceGroupManager::getSingleton().createResourceGroup("Rocket",true);
- //gkBlendLoader::getSingleton().loadFile(gkUtils::getFile("./media/biblioteca_principal.blend"), "Scene", "Rocket");
- main->window = gkEngine::getSingleton().getActiveScene()->getDisplayWindow();
- main->m_rkOgreRenderer = new RenderInterfaceOgre3D(main->window->getWidth(), main->window->getHeight());
- Rocket::Core::SetRenderInterface(main->m_rkOgreRenderer);
- main->m_rkOgreSystem = new SystemInterfaceOgre3D();
- Rocket::Core::SetSystemInterface(main->m_rkOgreSystem);
- Rocket::Core::Initialise();
- Rocket::Controls::Initialise();
- installRocketFonts();
- main->m_rkContext = Rocket::Core::CreateContext(
- "main",
- Rocket::Core::Vector2i(main->window->getWidth(), main->window->getHeight())
- );
- Rocket::Debugger::Initialise(main->m_rkContext);
- main->m_rkFileInterface = new FileInterfaceOgre3D();
- Rocket::Core::SetFileInterface(main->m_rkFileInterface);
- // Load the mouse cursor and release the caller's reference.
- cursor = main->m_rkContext->LoadMouseCursor("cursor.rml");
- if (cursor)
- cursor->RemoveReference();
- main->m_rkEventListener = new RocketEventListener(main->window, main->m_rkContext);
- main->m_rkRenderListener = new RocketRenderListener(main->window->getRenderWindow(), main->window->getRenderScene()->getManager(), main->m_rkContext);
- return 0;
- }
- int LRocket::showCursor( lua_State* L)
- {
- int stack = lua_gettop(L);
- if(stack>0)
- {
- if (lua_isboolean(L,1))
- if (cursor)
- if (lua_toboolean(L,1))
- cursor->Show(0);
- else
- cursor->Hide();
- }
- return 0;
- }
- int LRocket::loadDocument(lua_State* L)
- {
- int stack = lua_gettop(L);
- if (stack>0 && lua_isstring(L,1))
- {
- Rocket::Core::ElementDocument* document;
- document = main->m_rkContext->LoadDocument(lua_tostring(L,1));
- if (document)
- {
- gkString nome = lua_tostring(L,1);
- gkString call = "";
- if (stack>1)
- call = lua_tostring(L,2);
- Rocket::Core::ElementList lista;
- document->GetElementsByTagName(lista,"input");
- Rocket::Core::ElementList::iterator it;
- if (lista.size()>0)
- for( it=lista.begin(); it != lista.end(); it++)
- {
- Rocket::Core::Element* element =(Rocket::Core::Element*) *it.base();
- element->AddEventListener("click", main);
- }
- document->Show();
- documentos.push_back( new Rocket_Document( nome.c_str(), call.c_str(), document ) );
- main->m_document = document; // current document
- lua_pushlightuserdata(L, (void*) document);
- }
- else
- lua_pushnil(L);
- }
- return 1;
- }
- int LRocket::unloadDocument(lua_State* L)
- {
- int stack = lua_gettop(L);
- if (stack>0 && lua_isstring(L,1))
- {
- Rocket::Core::ElementDocument* document;
- //document = main->m_rkContext->LoadDocument(lua_tostring(L,1));
- //std::cout<<"boleano "<<(documentos.begin() == it)<<"\n";
- for(std::list<Rocket_Document*>::iterator it = documentos.begin(); it != documentos.end(); it++)
- {
- if(((Rocket_Document*) *it)->name.compare(lua_tostring(L,1))==0)
- {
- document = ((Rocket_Document*) *it)->document;
- if (document)
- {
- Rocket::Core::ElementList lista;
- document->GetElementsByTagName(lista,"input");
- Rocket::Core::ElementList::iterator it2;
- if (lista.size()>0)
- for( it2=lista.begin(); it2 != lista.end(); it2++)
- {
- Rocket::Core::Element* element =(Rocket::Core::Element*) *it2.base();
- element->RemoveEventListener("click", main);
- }
- document->Hide();
- document->RemoveReference();
- document->Close();
- delete document;
- //((Rocket_Document*) *it)->document = 0;
- std::cout<<"deletia "<<((Rocket_Document*) *it)->name.c_str()<<"\n";
- documentos.remove(*it);
- }
- } }
- }
- return 0;
- }
- int LRocket::setInnerHTML(lua_State* L)
- {
- int stack = lua_gettop(L);
- if (stack>1)
- {
- if (lua_isstring(L,1) && lua_isstring(L,2))
- {
- Rocket::Core::Element* element = main->m_document->GetElementById(lua_tostring(L,1));
- if (element)
- {
- element->SetInnerRML( lua_tostring(L,2) );
- }
- }
- }
- return 0;
- }
- int LRocket::getInnerHTML(lua_State* L)
- {
- int stack = lua_gettop(L);
- if (stack>0 && lua_isstring(L,1))
- {
- Rocket::Core::Element* element = main->m_document->GetElementById(lua_tostring(L,1));
- if (element)
- {
- //element->GetInnerRML(lua_tostring(L,1));
- }
- }
- return 0;
- }
- int LRocket::getFormElementValue(lua_State* L)
- {
- int stack = lua_gettop(L);
- if (stack>0)
- {
- Rocket::Core::Element* element;
- if (stack>1)
- {
- // modelo "formName", "fieldName"
- Rocket::Core::Element* form = main->m_document->GetElementById(lua_tostring(L,1));
- element = form->GetElementById(lua_tostring(L,2));
- } else {
- // modelo "fieldName"
- element = main->m_document->GetElementById(lua_tostring(L,1));
- }
- if (element)
- {
- //element->GetInnerRML(lua_tostring(L,1));
- lua_pushstring(L, static_cast<Rocket::Controls::ElementFormControl*>(element)->GetValue().CString() );
- } else {
- lua_pushnil(L);
- }
- }
- return 1;
- }
- int LRocket::showDocument(lua_State* L)
- {
- int stack = lua_gettop(L);
- if (stack>0 && lua_isstring(L,1))
- {
- Rocket::Core::ElementDocument* document = main->m_document;
- for(std::list<Rocket_Document*>::iterator it = documentos.begin(); it != documentos.end(); it++)
- if(((Rocket_Document*) *it)->name.compare(lua_tostring(L,1))==0)
- document = ((Rocket_Document*) *it)->document;
- if (document)
- {
- document->Show();
- main->m_document = document;
- }
- }
- return 0;
- }
- int LRocket::hideDocument(lua_State* L)
- {
- int stack = lua_gettop(L);
- if (stack>0 && lua_isstring(L,1))
- {
- Rocket::Core::ElementDocument* document = main->m_document;
- for(std::list<Rocket_Document*>::iterator it = documentos.begin(); it != documentos.end(); it++)
- if(((Rocket_Document*) *it)->name.compare(lua_tostring(L,1))==0)
- document = ((Rocket_Document*) *it)->document;
- if (document)
- {
- document->Hide();
- }
- }
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment