Ejemplo n.º 1
0
Archivo: Gui.cpp Proyecto: macBdog/game
Widget * Gui::CreateWidget(const Widget::WidgetDef & a_def, Widget * a_parent, bool a_startActive)
{
	// Check for a valid parent
	if (a_parent == NULL) 
	{	
		Log::Get().Write(LogLevel::Error, LogCategory::Engine, "Widget creation failed due to an invalid parent.");
		return NULL;
	}

	// Copy properties over to the managed element
	Widget * newWidget = new Widget();
	newWidget->SetName(a_def.m_name);
	newWidget->SetColour(a_def.m_colour);
	newWidget->SetSize(a_def.m_size);
	newWidget->SetOffset(a_def.m_pos);
	newWidget->SetAlignment(a_def.m_pos.GetAlignment());
	newWidget->SetAlignmentAnchor(a_def.m_pos.GetAlignmentAnchor());
	newWidget->SetFontName(a_def.m_fontNameHash);
	newWidget->SetFontSize(a_def.m_fontSize <= 0.5f ? 1.0f : a_def.m_fontSize);
	newWidget->SetSelectFlags(a_def.m_selectFlags);
	newWidget->SetActive(a_startActive);

	a_parent->AddChild(newWidget);
	
	return newWidget;
}
Ejemplo n.º 2
0
Widget * DebugMenuCommand::CreateButton(const char * a_name, Widget * a_parent, Colour a_colour)
{
	// All debug menu elements are created equal
	Widget::WidgetDef curItem;
	curItem.m_size = WidgetVector(0.2f, 0.1f);
	curItem.m_pos = WidgetVector(0.0f, 0.0f);
	curItem.m_pos.SetAlignment(AlignX::Left, AlignY::Top);
	curItem.m_pos.SetAlignmentAnchor(AlignX::Left, AlignY::Top);
	curItem.m_selectFlags = SelectionFlags::Rollover;
	curItem.m_colour = a_colour;
	curItem.m_name = a_name;

	// Check for a loaded debug font
	if (StringHash * debugFont = FontManager::Get().GetDebugFontName())
	{
		curItem.m_fontNameHash = debugFont->GetHash();
	}

	Widget * retWidget = Gui::Get().CreateWidget(curItem, a_parent, false);
	retWidget->SetDebugWidget();
	retWidget->SetActive(false);
	return retWidget;
}
Ejemplo n.º 3
0
Archivo: Gui.cpp Proyecto: 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;
}
Ejemplo n.º 4
0
Archivo: Gui.cpp Proyecto: macBdog/game
bool Gui::Startup(const char * a_guiPath, const DataPack * a_dataPack)
{
	// Cache off the gui path for later use when loading menus
	strncpy(m_guiPath, a_guiPath, strlen(a_guiPath) + 1);

	// Load in the gui config file
	char fileName[StringUtils::s_maxCharsPerLine];
	sprintf(fileName, "%s%s", a_guiPath, "gui.cfg");

	if (a_dataPack != NULL && a_dataPack->IsLoaded())
	{
		if (DataPackEntry * guiConfig = a_dataPack->GetEntry(fileName))
		{
			m_configFile.Load(guiConfig);
		}
	}
	else
	{
		m_configFile.Load(fileName);
	}
	
	// Setup the debug menu root element
	m_debugRoot.SetName("DebugMenu");
	m_debugRoot.SetActive(false);
	m_debugRoot.SetDebugWidget();

	// Load menus from data
	LoadMenus(a_guiPath, a_dataPack);

	// There are no menus loaded
	if (m_menus.GetLength() == 0)
	{
		Log::Get().Write(LogLevel::Warning, LogCategory::Engine, "No menus loaded, a default menu has been created.");

		// Create one and add it to the list
		Widget * defaultMenu = new Widget();
		defaultMenu->SetName("New Menu");
		sprintf(fileName, "%s%s", a_guiPath, "newMenu.mnu");
		defaultMenu->SetFilePath(fileName);
		defaultMenu->SetActive(false);

		MenuListNode * newMenuNode = new MenuListNode();
		newMenuNode->SetData(defaultMenu);
		
		m_menus.Insert(newMenuNode);
	}

	// Default the active menu
	if (m_startupMenu == NULL)
	{
		Log::Get().WriteGameErrorNoParams("No menu found with beginLoaded set to true, defaulting to the first menu loaded!");
		m_startupMenu = m_menus.GetHead()->GetData();
	}
	m_activeMenu = m_startupMenu;
	m_activeMenu->SetActive();

	// Setup the mouse cursor element if present in config file
	m_cursor.SetActive(false);
	if (GameFile::Object * configObj = m_configFile.FindObject("config"))
	{
		if (GameFile::Property * mouseCursorProp = configObj->FindProperty("mouseCursorTexture"))
		{
			m_cursor.SetTexture(TextureManager::Get().GetTexture(mouseCursorProp->GetString(), TextureCategory::Gui));
			m_cursor.SetAlignment(AlignX::Middle, AlignY::Centre);
			m_cursor.SetOffset(Vector2(0.0f, 0.0f));
			m_cursor.SetSize(Vector2(0.16f / RenderManager::Get().GetViewAspect(), 0.16f));
			m_cursor.SetActive(true);
		}
	}
	
	// Setup input callbacks for handling events, left mouse buttons activate gui elements
	InputManager & inMan = InputManager::Get();
	inMan.RegisterMouseCallback(this, &Gui::MouseInputHandler, MouseButton::Left);
	inMan.RegisterMouseCallback(this, &Gui::MouseInputHandler, MouseButton::Right);

	return true;
}