📄 window.cpp
字号:
//--------------------------------------------------
// Desc: Base Window UI
// Author: artsylee/2006.11.15
//--------------------------------------------------
#include "../stdafx.h"
#include "Window.h"
#include "GUIManager.h"
#include "../Core/Common.h"
#include "../Core/Input.h"
#include "../Core/IniFile.h"
#include "../Core/Graphics.h"
#include "../Core/TextureManager.h"
#include "../Core/Interface.h"
CWindow::CWindow(CWindow* pParent)
{
m_dwAttrib = GUI_VISIBLE | GUI_ENABLE;
m_dwGUIType = GUI_WINDOW;
m_dwColor = 0xffffffff;
m_hTexture = INVALID_HANDLE;
m_hBackground = INVALID_HANDLE;
m_hHeader = INVALID_HANDLE;
m_dwID = INVALID_ID;
m_pParent = pParent;
m_DragState = DRAG_NONE;
m_bFastDraw = true;
m_ComplexDraw = false;
m_pRgnInfo = false;
m_bUseRgn = false;
m_WindowBorder = 0;
m_WindowCull = 0;
m_szUIPath[0] = 0;
}
CWindow::~CWindow()
{
Release();
}
void CWindow::Release()
{
if(g_pTextureManager)
{
// Release this window
::ReleaseTexture(m_hTexture);
::ReleaseTexture(m_hBackground);
::ReleaseTexture(m_hHeader);
}
// Release SubWindows
WndItor itor = m_SubWindows.begin();
while(itor != m_SubWindows.end())
{
S_DELETE(*itor);
itor++;
}
m_SubWindows.clear();
// RGN info
S_DELETE_ARRAY(m_pRgnInfo);
}
//--------------------------------------------------
// 注意: WOW方式无不规则RGN, 不区分是否快速绘图
// 有效的背景图片句柄即为WOW方式
//--------------------------------------------------
void CWindow::Render()
{
if(m_dwAttrib & GUI_VISIBLE)
{
if(m_ComplexDraw)
{
g_pGraphics->BeginSprite();
if(m_hBackground)
{
RECT rcWnd = { m_ptPos.x, m_ptPos.y, m_ptPos.x+m_Width, m_ptPos.y+m_Height };
GRect rc;
rc.SetRectWH(rcWnd.left+m_WindowCull, rcWnd.top+m_WindowCull, (rcWnd.right-rcWnd.left)-m_WindowCull*2, (rcWnd.bottom-rcWnd.top)-m_WindowCull*2);
g_pGraphics->RenderSprite(m_hBackground, &rc, NULL, m_dwColor);
RenderNewStyle(m_hTexture, rcWnd, m_dwColor, m_WindowBorder);
g_pGraphics->RenderSprite(m_hHeader, m_ptPos.x+m_Width/2-128, m_ptPos.y-12);
DrawSingleLine(m_ptPos.x + m_Width/2, m_ptPos.y+8, 0xffffffff, true, m_szCaption);
}
else
{
if(m_bFastDraw) RenderExpandEx();
else RenderExpand();
// Caption
DrawSingleLine(m_ptPos.x + m_Width/2, m_ptPos.y+18, 0xffffffff, true, m_szCaption);
}
g_pGraphics->EndSprite();
}
else
{
g_pGraphics->RenderSprite(m_hTexture, m_ptPos.x, m_ptPos.y, &m_rcSrc, m_dwColor);
}
// Render SubWindows
WndItor itor = m_SubWindows.begin();
while(itor != m_SubWindows.end())
{
(*itor)->Render();
itor++;
}
}
}
bool CWindow::LoadFromIni(char* pfilename, char* pIndex)
{
CIniFile ui(pfilename);
m_dwID = ui.ReadDWORD(pIndex, "ID");
m_ptPos = ui.ReadPoint(pIndex, "Position");
m_rcSrc = ui.ReadRect(pIndex, "SrcRect");
m_dwColor = ui.ReadDWORD(pIndex, "Color", 0xffffffff);
m_dwAttrib = ui.ReadDWORD(pIndex, "Attrib", 10);
ui.ReadString(pIndex, "Caption", m_szCaption);
ui.ReadString(pIndex, "File", m_szUIPath);
DWORD index = ui.ReadDWORD(pIndex, "Picture");
if(ui.IsReadSucceed())
{
m_hTexture = LoadAMFTexture(m_szUIPath, index);
GRect zeroRc(0, 0, 0, 0);
if(m_rcSrc==zeroRc)
{
CTexture * ptex = g_pTextureManager->GetTexture(m_hTexture);
if(ptex)
{
m_rcSrc.SetRect(0, 0, ptex->GetWidth(), ptex->GetHeight());
}
}
}
CTexture *pTexture = g_pTextureManager->GetTexture(m_hTexture);
if(pTexture)
{
m_WindowBorder = pTexture->GetHeight();
if(m_WindowBorder==8) m_WindowCull = 3;
if(m_WindowBorder==16) m_WindowCull = 5;
if(m_WindowBorder==32) m_WindowCull = 10;
}
index = ui.ReadDWORD(pIndex, "Background");
if(ui.IsReadSucceed())
{
m_hBackground = LoadAMFTexture(m_szUIPath, index);
}
index = ui.ReadDWORD(pIndex, "Header");
if(ui.IsReadSucceed())
{
m_hHeader = LoadAMFTexture(m_szUIPath, index);
}
m_Width = ui.ReadInt(pIndex, "Width", m_rcSrc.Width());
m_Height = ui.ReadInt(pIndex, "Height", m_rcSrc.Height());
if(m_Width == m_rcSrc.Width() && m_Height == m_rcSrc.Height())
{
m_ComplexDraw = false;
}
else
{
if(m_Width<=m_rcSrc.Width() && m_Height<=m_rcSrc.Height())
{
m_ComplexDraw = false;
m_Width = m_rcSrc.Width();
m_Height = m_rcSrc.Height();
}
else
{
m_ComplexDraw = true;
}
}
if(m_ComplexDraw)
{
m_bUseRgn = true;
}
m_bUseRgn = ui.ReadBOOL(pIndex, "UseRGN", m_bUseRgn);
if(m_hTexture && m_bUseRgn && !m_hBackground)
{
CreateRGN(&m_rcSrc);
}
if(m_pParent)
{
OffSet(m_pParent->GetRect().left, m_pParent->GetRect().top);
}
return true;
}
DWORD CWindow::ProcessEvent()
{
if(!(m_dwAttrib & GUI_VISIBLE) || !(m_dwAttrib & GUI_ENABLE))
{
return INVALID_ID;
}
GRect rcDst(m_ptPos.x, m_ptPos.y, m_ptPos.x + m_Width, m_ptPos.y + m_Height);
DWORD hRes = INVALID_ID;
WndItor itor = m_SubWindows.begin();
while(itor != m_SubWindows.end())
{
DWORD hTemp = (*itor)->ProcessEvent();
if(hTemp != INVALID_ID && hRes == INVALID_ID)
{
hRes = hTemp;
}
itor++;
}
if(hRes != INVALID_ID) return hRes;
switch(g_stInputInfo.MouseValue)
{
case LB_DOWN:
{
if(CheckInRGN(rcDst) && (m_dwAttrib & GUI_DRAGABLE))
{
m_DragState = DRAG_START;
g_pGUIManager->SetAsTopWindow(m_dwID);
m_ptDragStart.x = g_stInputInfo.point.x;
m_ptDragStart.y = g_stInputInfo.point.y;
return m_dwID;
}
else
{
m_DragState = DRAG_NONE;
}
}
break;
case LB_UP:
{
m_DragState = DRAG_NONE;
}
break;
}
if(g_stInputInfo.MouseMove && (m_dwAttrib & GUI_DRAGABLE) && (m_DragState == DRAG_START))
{
int offX = g_stInputInfo.point.x - m_ptDragStart.x;
int offY = g_stInputInfo.point.y - m_ptDragStart.y;
OffSet(offX, offY);
m_ptDragStart.x += offX;
m_ptDragStart.y += offY;
return m_dwID;
}
if(CheckInRGN(rcDst))
return m_dwID;
return INVALID_ID;
}
bool CWindow::CheckInRGN(GRect rc, POINT *pt)
{
POINT tempPoint;
if(pt!=NULL)
{
tempPoint = *pt;
}
else
{
tempPoint = g_stInputInfo.point;
}
if(rc.PtInRect(tempPoint))
{
if(m_bUseRgn)
return m_pRgnInfo[tempPoint.x-m_ptPos.x+(tempPoint.y-m_ptPos.y)*m_Width];
else
return true;
}
else
return false;
}
POINT CWindow::GetRGNCenter(void)
{
POINT pt = { 0, 0 };
if(m_pRgnInfo == NULL)
return pt;
int minX = m_Width;
int maxX = 0;
int minY = m_Height;
int maxY = 0;
for(int j=0; j<m_Height; j++)
{
for(int i=0; i<m_Width; i++)
{
if(m_pRgnInfo[j*m_Height+i])
{
if(i>maxX) maxX = i;
if(i<minX) minX = i;
if(j>maxY) maxY = j;
if(j<minY) minY = j;
}
}
}
pt.x = (maxX+minX)/2;
pt.y = (maxY+minY)/2;
return pt;
}
void CWindow::MoveTo(int x, int y)
{
OffSet(x-m_ptPos.x, y-m_ptPos.y);
}
void CWindow::OffSet(int x, int y)
{
m_ptPos.Offset(x, y);
WndItor itor = m_SubWindows.begin();
while(itor != m_SubWindows.end())
{
(*itor)->OffSet(x, y);
itor++;
}
}
GRect CWindow::GetRect()
{
GRect rc;
rc.SetRectWH(m_ptPos.x, m_ptPos.y, m_Width, m_Height);
return rc;
}
void CWindow::Show(bool c)
{
if(c)
{
if(!(m_dwAttrib & GUI_VISIBLE)) // 每个界面弹出时,默认是在最顶层
{
g_pGUIManager->SetAsTopWindow(m_dwID);
m_dwAttrib |= GUI_VISIBLE;
}
}
else
{
m_dwAttrib &= ~GUI_VISIBLE;
}
}
void CWindow::Enable()
{
m_dwAttrib |= GUI_ENABLE;
WndItor itor = m_SubWindows.begin();
while(itor != m_SubWindows.end())
{
(*itor)->Enable();
itor++;
}
}
void CWindow::Disable()
{
m_dwAttrib &= ~GUI_ENABLE;
WndItor itor = m_SubWindows.begin();
while(itor != m_SubWindows.end())
{
(*itor)->Disable();
itor++;
}
}
void CWindow::SetDragable(bool c)
{
if(c)
{
m_dwAttrib |= GUI_DRAGABLE;
}
else
{
m_dwAttrib &= ~GUI_DRAGABLE;
}
}
void CWindow::SetActive(bool c)
{
if(c)
{
m_dwAttrib |= GUI_ACTIVE;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -