⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 guimanager.cpp

📁 3D游戏展示程序
💻 CPP
字号:
//--------------------------------------------------
//  Desc: GUI Manager
//  Author: artsylee/2006.11.19
//--------------------------------------------------
#include "../stdafx.h"
#include "GUIManager.h"
#include "../Core/Common.h"
#include "../Core/IniFile.h"
#include "../Core/Log.h"
#include "../Core/Interface.h"

GUIManager::GUIManager()
{
	m_pMainWindow = new CWindow;
	m_WindowsList.push_back(m_pMainWindow);
}

GUIManager::~GUIManager()
{
	RemoveAll();
}

void GUIManager::RemoveAll()
{
	WndItor itor = m_WindowsList.begin();
	while(itor != m_WindowsList.end())
	{
		S_DELETE(*itor);
		itor++;
	}
	m_WindowsList.clear();
	// m_pMainWindow has been delete
	//S_DELETE(m_pMainWindow);
	// 内存释放由m_WindowsList实现
	m_ElementMap.clear();
	m_pMainWindow = NULL;
}

void GUIManager::RemoveElement(CWindow* pWnd, bool bDel)
{
	// 不可使用m_ElementMap删除
	CWindow* pCurWnd = NULL;
	for(WndItor itor=m_WindowsList.begin(); itor!=m_WindowsList.end(); itor++)
	{
		pCurWnd = (*itor);
		if(pCurWnd==pWnd)
		{
			if(bDel)
			{
				S_DELETE(pWnd);
			}
			m_WindowsList.erase(itor);
			return;
		}
		else
		{
			pCurWnd->RemoveWindow(pWnd, bDel);
		}
	}
	return;
}

bool GUIManager::LoadFromFile(char* szFile)
{
	if(szFile == NULL)
	{
		return false;
	}

	CIniFile file(szFile);
	char szDataBuffer[256];
	char szIndex[256];
	CWindow *pWnd = NULL;
	int indexnum = file.GetIndexNum();
	for(int i=0; i<indexnum; i++)
	{
		if(file.GetIndex(i, szIndex))
		{
			if(!file.ReadInt(szIndex, "GUIType"))
			{
				file.ReadString(szIndex, "Container", szDataBuffer);
				DWORD id = file.ReadInt(szDataBuffer, "ID");
				if(!szDataBuffer[0] || !id)
				{
					pWnd = m_pMainWindow;
				}
				else
				{
					pWnd = CreateControl(GUI_WINDOW, NULL);
					if(!pWnd)	return false;
					if(m_ElementMap.find(id)!=m_ElementMap.end())
					{
						WriteLog(INFO_ERROR, "Control ID[%d] has been used in file[%s]index[%s]", id, szFile, szDataBuffer);
						delete pWnd;
						return false;
					}
					pWnd->LoadFromIni(szFile, szDataBuffer);
					AddWindowBack(pWnd);
					m_ElementMap.insert(std::map<DWORD, CWindow*>::value_type(id, pWnd));
				}
				int nControlCount = file.GetContinueData(szIndex);

				CWindow* pElement = NULL;
				for(int i=0; i<nControlCount; i++)
				{
					file.ReadString(szIndex, i, szDataBuffer);
					int  nGUIType = file.ReadInt(szDataBuffer, "GUIType");
					id = file.ReadInt(szDataBuffer, "ID");
					pElement = CreateControl(nGUIType, pWnd);
					if(!nGUIType || !id || pElement==NULL)
					{
						return false;
					}
					if(m_ElementMap.find(id)!=m_ElementMap.end())
					{
						WriteLog(INFO_ERROR, "Control ID[%d] has been used in file[%s]index[%s]", id, szFile, szDataBuffer);
						delete pElement;
						return false;
					}
					m_ElementMap.insert(std::map<DWORD, CWindow*>::value_type(id, pElement));
					pElement->LoadFromIni(szFile, szDataBuffer);
					pWnd->AddWindow(pElement);
					if(nGUIType==GUI_RADIOGROUP)
					{
						int num = ((RadioGroup*)(pElement))->GetElementNum();
						for(int j=0; j<num; j++)
						{
							CCheckBox *pCheck = ((RadioGroup*)(pElement))->GetElement(j);
							if(pCheck)
							{
								if(m_ElementMap.find(pCheck->GetID())!=m_ElementMap.end())
								{
									WriteLog(INFO_ERROR, "Control ID[%d] has been used in file[%s]index[%s]", pCheck->GetID(), szFile, szDataBuffer);
									return false;
								}
								m_ElementMap.insert(std::map<DWORD, CWindow*>::value_type(pCheck->GetID(), pCheck));
							}
						}
					}
					
				}
			}
		}
	}
	return true;
}

CWindow* GUIManager::GetElement(DWORD dwID)
{
	// TODO: 改为使用m_ElementMap获取
	CWindow* pWnd = NULL;
	for(WndItor itor=m_WindowsList.begin(); itor!=m_WindowsList.end(); itor++)
	{
		if((*itor)->GetID()==dwID)
		{
			return (*itor);
		}
		else
		{
			pWnd = (*itor)->GetSubWindow(dwID);
			if(pWnd)
			{
				return pWnd;
			}
		}
	}
	return NULL;
}

void GUIManager::SetAsTopWindow(CWindow* pWnd)
{
	
}

void GUIManager::SetAsTopWindow(DWORD dwID)
{
	if(!FindContainer(dwID))	return;
	CWindow* pWnd = NULL;
	for(WndItor itor=m_WindowsList.begin(); itor!=m_WindowsList.end(); itor++)
	{
		if((*itor)->GetID()==dwID)
		{
			pWnd = (*itor);
			m_WindowsList.erase(itor);
			break;
		}
	}
	if(pWnd)
	{
		m_WindowsList.push_back(pWnd);
	}
	
}

void GUIManager::Render()
{
	for(WndItor itor=m_WindowsList.begin(); itor!=m_WindowsList.end(); itor++)
	{
		(*itor)->Render();
	}
}

bool GUIManager::ProcessEvent()
{
	// 消息处理(反向)
	for(WndRItor itor=m_WindowsList.rbegin(); itor!=m_WindowsList.rend(); itor++)
	{
		if((*itor)->ProcessEvent()!=INVALID_ID)
		{
			return true;
		}
	}
	return false;
}

void GUIManager::AddWindowFront(CWindow* pWnd)
{
	if(pWnd==NULL || FindContainer(pWnd))
	{
		return;
	}
	m_WindowsList.push_front(pWnd);
}

void GUIManager::AddWindowBack(CWindow* pWnd)
{
	if(pWnd==NULL || FindContainer(pWnd))
	{
		return;
	}
	m_WindowsList.push_back(pWnd);
}

bool GUIManager::FindContainer(CWindow* pWnd)
{
	for(WndItor itor=m_WindowsList.begin(); itor!=m_WindowsList.end(); itor++)
	{
		if((*itor)==pWnd)
		{
			return true;
		}
	}
	return false;
}

bool GUIManager::FindContainer(DWORD dwID)
{
	for(WndItor itor=m_WindowsList.begin(); itor!=m_WindowsList.end(); itor++)
	{
		if((*itor)->GetID()==dwID)
		{
			return true;
		}
	}
	return false;
}

CWindow* GUIManager::CreateControl(int GuiType, CWindow* pParent)
{
	switch(GuiType)
	{
	case GUI_WINDOW:		return new CWindow(pParent);
	case GUI_BUTTON:		return new Button(pParent);
	case GUI_COOLBUTTON:	return new CoolButton(pParent);
	case GUI_STATIC:		return new GStatic(pParent);
	case GUI_COOLSTATIC:	return new MLStatic(pParent);
	case GUI_CHECKBOX:		return new CCheckBox(pParent);
	case GUI_RADIOGROUP:	return new RadioGroup(pParent);
	case GUI_SLIDER :		return new CSlider(pParent);
	case GUI_SCROLLBAR:		return new GScrollBar(pParent);
	case GUI_COOLSCROLL:	return new CoolScroll(pParent);
	case GUI_LISTBOX:		return new GListBox(pParent);
	case GUI_COOLLISTBOX:	return new CoolListBox(pParent);
	case GUI_COOLNUMBERINPUT: return new CNumberInputBox(pParent);

	default:				break;
	}
	return NULL;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -