GUIEngine初始化、菜单刷新流程以及核心函数介绍
一 流程图
二 流程图中涉及到的核心函数介绍
1.new TextDestGuiEngine
初始换创建一个TextDestGuiEngine类实例,用于后期点击界面上文字位置时通过guiFormSpecMenu获取到相应的文字,然后能够通过函数知道点击了MainMenu中的哪个Buttons或者Event。
主要涉及到以及两个函数:
minetest中的guiEngine.cpp
void TextDestGuiEngine::gotText(std::map<std::string,std::string> fields)
{
m_engine->getScriptIface()->handleMainMenuButtons(fields);
}
/******************************************************************************/
void TextDestGuiEngine::gotText(std::wstring text)
{
m_engine->getScriptIface()->handleMainMenuEvent(wide_to_narrow(text));
}
2.create texture source
m_texture_source = newMenuTextureSource(m_device->getVideoDriver());
3.create formspecsource
m_formspecgui = newFormspecFormSource("");
这个实例中主要一个作用存scriptapi的lua文件中读取的值,譬如菜单界面显示的那些文字,以及那些按钮、tab的选中在状态等。lua文件中的数据能通过*/MineT.app/Contents/Resources/share/builtin/mainmenu/*.lua文件读取出来的,具体读取在loadMainMenuScript中。
主要 涉及的函数
mintest->script->lua_api->l_mainmenu.cpp
int ModApiMainMenu::l_update_formspec(lua_State *L)
{
…
//read formspec
std::string formspec(luaL_checkstring(L,1));
engine->m_formspecgui->setForm(formspec);
...
}
4.create menu
minetest->guiFormSpecMenu.h minetest->modalMenu.h
m_menu = newGUIFormSpecMenu(m_device,m_parent, -1,m_menumanager, NULL,NULL,m_texture_source,m_formspecgui,m_buttonhandler,NULL);
GUIFormSpecMenu::GUIModalMenu::IGUIElement
创建这个对象,主要作用是创建出Menu菜单中一系列界面元素。
控件的层次关系是gui环境的最顶层的staticText(在mymain中加入的,即这里的m_parent);然后是m_menu这个IGUIElement控件;最后是通过m_formspecgui解析出来的所有控件(button、tab、checkbox、staticText等等),这些控件是m_menu的子控件。
主要涉及函数
void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
{
…
std::vector<std::string> elements =split(m_formspec_string,’]’);//界面所有元素
for (unsignedint i=0;i< elements.size();i++)
{
parseElement(&mydata,elements[i]);
}
…
}
void GUIFormSpecMenu::parseElement(parserData* data,std::string element);解析所有子控件的。
5.Initialize scripting
minetest->script->scripting_mainmenu.h minetest->script->cpp_api->s_mainmenu.h
m_script = newMainMenuScripting(this);
作用:初始化一些lua相关的模块接口等(初始化lua_State *L、注册lua_State *L等)
6.loadMainMenuScript
加载主菜单的脚本,读取*/MineT.app/Contents/Resources/share/builtin/mainmenu/*.lua文件,存在m_formspecgui中。
主要涉及函数
minetest->script->cpp_api->s_base.cpp
bool ScriptApiBase::loadScript(conststd::string &scriptpath)
int ret = luaL_loadfile(L, scriptpath.c_str()) ||lua_pcall(L, 0,0, m_errorhandler);
mintest->script->lua_api->l_mainmenu.cpp->l_update_formspec
int ModApiMainMenu::l_update_formspec(lua_State *L)
{
…
//read formspec
std::string formspec(luaL_checkstring(L,1));
engine->m_formspecgui->setForm(formspec);
...
}
7.run
主要的菜单界面刷新就是这个函数。这个函数中主要是一个循环,使用了鬼火引擎渲染绘制方式。
device->run()这个控制的循环,同时界面响应也是从这里为入口的。
场景主要绘制:云
GUI环境绘制:菜单界面上的控件
这个函数一直循环,直到界面上点击了start game之后m_startgame =false之后推出循环。
mintest->script->lua_api->l_mainmenu.cpp->l_start
CIrrDevice::run()->CIrrDevice::postEventFromUser()->CGUIEnvironment::postEventFromUser()->CGUITabControl:onEvent()->GUIFormSpecMenu::OnEvent()->acceptInput()->TextDestGuiEngine::gotText->ScriptApiMainMenu::handleMainMenuButtons()->ModApiMainMenu::l_start(lua_State *L):engine->m_startgame =true;