示例#1
0
文件: Gui.cpp 项目: macBdog/game
bool Gui::LoadMenu(const GameFile & a_menuFile, const DataPack * a_dataPack)
{
	// Load the menu file
	Widget * createdMenu = NULL;
	if (a_menuFile.IsLoaded())
	{
		// Create a new widget and copy properties from file
		if (GameFile::Object * menuObject = a_menuFile.FindObject("menu"))
		{
			if (GameFile::Property * nameProp = menuObject->FindProperty("name"))
			{
				createdMenu = new Widget();
				createdMenu->SetName(a_menuFile.GetString("menu", "name"));
				if (a_dataPack == NULL || !a_dataPack->IsLoaded())
				{
					char menuFilePath[StringUtils::s_maxCharsPerLine];
					sprintf(menuFilePath, "%s%s.mnu", m_guiPath, createdMenu->GetName());
					createdMenu->SetFilePath(menuFilePath);
				}
				createdMenu->SetActive(false);

				// TODO Support for non fullscreen menus
				createdMenu->SetSize(Vector2(2.0f, 2.0f));
				createdMenu->SetDrawPos(Vector2(-1.0, 1.0));

				// Load child elements of the menu
				LinkedListNode<GameFile::Object> * childWidget = menuObject->GetChildren();
				while (childWidget != NULL)
				{
					GameFile::Object * curObject = childWidget->GetData();
					Widget * newChild = CreateWidget(curObject, createdMenu);
					// If not specified in the file, align child widgets to the parent menu
					if (!newChild->HasAlignTo())
					{
						newChild->SetAlignTo(createdMenu);
					}
					childWidget = childWidget->GetNext();
				}

				// Add to list of menus
				m_menus.InsertNew(createdMenu);
			}
			else // No properties present
			{
				Log::Get().Write(LogLevel::Error, LogCategory::Engine, "Error loading menu file %s, menu does not have a name property.", a_menuFile);
			}

			// Set the active menu to the last menu with the begin loaded property
			if (createdMenu != NULL && menuObject->FindProperty("beginLoaded"))
			{
				if (menuObject->FindProperty("beginLoaded")->GetBool())
				{
					m_startupMenu = createdMenu;
				}
			}
		}
		else // Unexpected file format, no root element
		{
			Log::Get().Write(LogLevel::Error, LogCategory::Engine, "Error loading menu file %s, no valid menu parent element.", a_menuFile);
		}

		return true;
	}
	return false;
}
示例#2
0
	//compiles listener/commander connections inside widgets
	void GuiMgrParser::CompileWidget( WidgetInfo& info )
	{
		if(!info.m_widget) return;

		const LInfo& linfo = info.m_listenerInfo;

		//add widget to gui
		m_widgetInfos.pop();
		if(m_widgetInfos.size()) {
			Widget* parent = m_widgetInfos.top().m_widget;
			if(!parent) {
				error_log("Couldn't add widget \"%s\"to non-existing parent!",info.m_widget->GetName().c_str());
			} else {
				if(!parent->AddWidget(info.m_widget)) {
					error_log("Couldn't add widget \"%s\" to his parent \"%s\". Maybe duplicate exists?",info.m_widget->GetName().c_str(),parent->GetName().c_str());
				}
			}
		} else {
			
			if(!m_gui->AddWidget(info.m_widget)) {
				error_log("Couldn't add widget \"%s\" to his GUI!. Maybe duplicate exists?",info.m_widget->GetName().c_str());
			}
			
		}

		if(info.m_widget)
			info.m_widget->SetLoading(false);

		if(!linfo.size()) return;

		if(info.m_widget)
			info.m_widget->SetLoading(true);

		//compile listener connections
		for(LInfo::const_iterator itr = linfo.begin(); itr != linfo.end(); itr++) 
		{
			for(ListenerInfo::const_iterator it = itr->second.begin(); it != itr->second.end(); it++) 
			{		
				const std::vector<std::string>& paths = it->second;
				for(uint32 i=0; i<paths.size(); i++) 
				{
					std::string path = paths[i];
					uint32 pos = path.find("this.");

					//build real path
					if(pos != std::string::npos) {
						path.replace(pos,4,info.m_widget->GetWidgetPath());
					} 
					std::stack<Widget*> hierarchy = m_gui->QueryWidgetPath(path);
					Widget* temp = NULL;
					while(hierarchy.size()) {
						temp = hierarchy.top(); 
						hierarchy.pop();
					}
					if(!temp) { 
						error_log("Error compiling widget \"%s\"", info.m_widget->GetWidgetPath());
						return;
					}	
					info.m_widget->m_mediator.Connect(temp,itr->first,it->first);
				}
			}
		}

		if(info.m_widget)
			info.m_widget->SetLoading(false);
	}