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

📄 asl_guiobj.cpp

📁 泡泡堂单机版(含ASL游戏引擎源码 泡泡堂单机版(含ASL游戏引擎源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//-----------------------------------------------------------------------------
//
//    ____ Azure Star Game Engine 蓝星游戏引擎 ____
//
//    Copyright (c) 2006, 蓝星工作室
//    All rights reserved.
//
//    文件名称: asl_guiobj.cpp
//    摘    要: 各种GUI控件类实现
//
//    当前版本: 1.0
//    作    者: 汤  祺
//    创建日期: 2006-8-12
//
//-----------------------------------------------------------------------------

#include "asl_guiobj.h"
#include "asl_gui.h"

using namespace std;

namespace ASL
{

//-----------------------------------------------------------------------------
// ASLGuiObj类实现
//-----------------------------------------------------------------------------
ASLControl::ASLControl(void)
: m_pParent(NULL)
, m_nLeft(0)
, m_nTop(0)
, m_nWidth(0)
, m_nHeight(0)
, m_bEnabled(true)
, m_bVisible(true)
, m_bFocused(false)
, m_pFont(NULL)
, m_clFontColor(clWhite)
, Tag(0)
{	
}

ASLControl::~ASLControl(void)
{
	if (m_pParent != NULL)
	{
		m_pParent->DelChild(this);
	}
	list<ASLControl*>::iterator it;
	for (it = m_lChildren.begin(); it != m_lChildren.end(); ++it)
	{
		(*it)->m_pParent = NULL;
		GUI.DelCtrl(*it);
	}
	GUI.DelCtrl(this);
}

void ASLControl::Create(void)
{
	GUI.AddCtrl(this);
}

void ASLControl::SetParent(ASLControl *pParent)
{
	if (m_pParent == pParent)
	{
		return;
	}

	if (m_pParent != NULL)
	{
		m_pParent->DelChild(this);
	}

	m_pParent = pParent;
	if (m_pParent != NULL)
	{
		m_pParent->AddChild(this);
		m_pFont = m_pParent->GetFont();
		m_clFontColor = m_pParent->GetFontColor();
	}
}

void ASLControl::AddChild(ASLControl* pChild)
{
	ASSERT(pChild != NULL);

	list<ASLControl*>::iterator it;
	for (it = m_lChildren.begin(); it != m_lChildren.end(); ++it)
	{
		if ((*it) == pChild)
		{
			return;
		}
	}

	m_lChildren.push_back(pChild);
}

void ASLControl::DelChild(ASLControl* pChild)
{
	ASSERT(pChild != NULL);

	list<ASLControl*>::iterator it;
	for (it = m_lChildren.begin(); it != m_lChildren.end(); ++it)
	{
		if ((*it) == pChild)
		{
			m_lChildren.erase(it);
			return;
		}
	}
}

void ASLControl::Enable(void)
{
	m_bEnabled = true;
	list<ASLControl*>::iterator it;
	for (it = m_lChildren.begin(); it != m_lChildren.end(); ++it)
	{
		(*it)->Enable();
	}
}

void ASLControl::Disable(void)
{
	m_bEnabled = false;
	list<ASLControl*>::iterator it;
	for (it = m_lChildren.begin(); it != m_lChildren.end(); ++it)
	{
		(*it)->Disable();
	}	
}

void ASLControl::Show(void)
{
	m_bVisible = true;
	list<ASLControl*>::iterator it;
	for (it = m_lChildren.begin(); it != m_lChildren.end(); ++it)
	{
		(*it)->Show();
	}
}

void ASLControl::Hide(void)
{
	m_bVisible = false;
	list<ASLControl*>::iterator it;
	for (it = m_lChildren.begin(); it != m_lChildren.end(); ++it)
	{
		(*it)->Hide();
	}
}

void ASLControl::ClientToScreen(POINT &pt) const
{
	if (m_pParent != NULL)
	{
		m_pParent->ClientToScreen(pt);
	}
	pt.x += m_nLeft;
	pt.y += m_nTop;
}

void ASLControl::ScreenToClient(POINT &pt) const
{
	if (m_pParent != NULL)
	{
		m_pParent->ScreenToClient(pt);
	}
	pt.x -= m_nLeft;
	pt.y -= m_nTop;
}

bool ASLControl::IsPointIn(POINT pt) const
{
	ScreenToClient(pt);
	if (pt.x >= 0 && pt.x < m_nWidth && pt.y >= 0 && pt.y < m_nHeight)
	{
		return true;
	}
	else
	{
		return false;
	}
}

void ASLControl::SetFocus(void)
{
	if (CanHaveFocus())
	{
		GUI.SetFocus(this);
	}
}

void ASLControl::MouseEnter(void)
{
	if (OnMouseEnter.function != NULL)
	{
		(OnMouseEnter.invoker->*OnMouseEnter.function)();
	}
}

void ASLControl::MouseLeave(void)
{
	if (OnMouseLeave.function != NULL)
	{
		(OnMouseLeave.invoker->*OnMouseLeave.function)();
	}
}

void ASLControl::MouseMove(POINT pt)
{
	if (OnMouseMove.function != NULL)
	{
		ScreenToClient(pt);
		(OnMouseMove.invoker->*OnMouseMove.function)(pt);
	}
}

void ASLControl::MouseDown(POINT pt, MouseButton mb)
{
	if (OnMouseDown.function != NULL)
	{
		ScreenToClient(pt);
		(OnMouseDown.invoker->*OnMouseDown.function)(pt, mb);
	}
}

void ASLControl::MouseUp(POINT pt, MouseButton mb)
{
	if (OnMouseUp.function != NULL)
	{
		ScreenToClient(pt);
		(OnMouseUp.invoker->*OnMouseUp.function)(pt, mb);
	}
}

void ASLControl::KeyDown(DWORD dwKey, ShiftState ss)
{
	if (OnKeyDown.function != NULL)
	{
		(OnKeyDown.invoker->*OnKeyDown.function)(dwKey, ss);
	}
}

void ASLControl::KeyUp(DWORD dwKey, ShiftState ss)
{
	if (OnKeyUp.function != NULL)
	{
		(OnKeyUp.invoker->*OnKeyUp.function)(dwKey, ss);
	}
}

void ASLControl::KeyPress(char cKey)
{
	if (OnKeyPress.function != NULL)
	{
		(OnKeyPress.invoker->*OnKeyPress.function)(cKey);
	}
}

void ASLControl::Enter(void)
{
	m_bFocused = true;
	if (OnEnter.function != NULL)
	{
		(OnEnter.invoker->*OnEnter.function)();
	}
}
	
void ASLControl::Leave(void)
{
	m_bFocused = false;
	if (OnLeave.function != NULL)
	{
		(OnLeave.invoker->*OnLeave.function)();
	}
}

//-----------------------------------------------------------------------------
// ASLWinCtrl类实现
//-----------------------------------------------------------------------------
ASLWinControl::ASLWinControl(void)
: m_pbmBackground(NULL)
{
}

ASLWinControl::~ASLWinControl(void)
{
}

void ASLWinControl::Draw(void) const
{
	if (!m_bVisible || m_pbmBackground == NULL)
	{
		return;
	}

	if (m_pbmBackground->IsAlphaChannel())
	{
		m_pbmBackground->DrawAlphaChannel(GUI.GetCanvas(), m_nLeft, m_nTop);
	}
	else
	{
		m_pbmBackground->Draw(GUI.GetCanvas(), m_nLeft, m_nTop);
	}
}

void ASLWinControl::SetBG(ASLBitmap &bmBackground)
{
	m_pbmBackground = &bmBackground;
}

//-----------------------------------------------------------------------------
// ASLPanel类实现
//-----------------------------------------------------------------------------
void ASLPanel::Create(ASLControl *pParent, int nLeft, int nTop, int nWidth,
					  int nHeight)
{
	ASLControl::Create();
	SetParent(pParent);
	SetLeft(nLeft);
	SetTop(nTop);
	SetWidth(nWidth);
	SetHeight(nHeight);
}

void ASLPanel::Create(ASLControl *pParent, int nLeft, int nTop, 
					  ASLBitmap &bmBackground)
{
	ASLControl::Create();
	SetParent(pParent);
	SetLeft(nLeft);
	SetTop(nTop);
	SetBG(bmBackground);
	SetWidth(bmBackground.GetWidth());
	SetHeight(bmBackground.GetHeight());
}

//-----------------------------------------------------------------------------
// ASLForm类实现
//-----------------------------------------------------------------------------
ASLForm::ASLForm(void) 
: m_bHolding(false)
, m_bMovable(false)
{
}

void ASLForm::Create(ASLControl *pParent, int nLeft, int nTop, int nWidth,
					 int nHeight, ASLFont &fnt, COLOR cl)
{
	ASLControl::Create();
	SetParent(pParent);
	SetLeft(nLeft);
	SetTop(nTop);
	SetWidth(nWidth);
	SetHeight(nHeight);
	SetFont(fnt);
	SetFontColor(cl);
}

void ASLForm::Create(ASLControl *pParent, int nLeft, int nTop,
					 ASLBitmap &bmBackground, ASLFont &fnt, COLOR cl)
{
	ASLControl::Create();
	SetParent(pParent);
	SetLeft(nLeft);
	SetTop(nTop);
	SetBG(bmBackground);
	SetWidth(bmBackground.GetWidth());
	SetHeight(bmBackground.GetHeight());
	SetFont(fnt);
	SetFontColor(cl);
}

void ASLForm::MouseDown(POINT pt, MouseButton mb)
{
	if (!m_bMovable)
	{
		return;
	}

	RECT rcReal;
	rcReal.left = m_rcHot.left + m_nLeft;
	rcReal.top = m_rcHot.top + m_nTop;
	rcReal.right = rcReal.left + m_rcHot.right - m_rcHot.left;
	rcReal.bottom = rcReal.top + m_rcHot.bottom - m_rcHot.top;

	if (mb == mbLeft && PtInRect(&rcReal, pt))
	{
		m_bHolding = true;
		m_ptOffset.x = pt.x - rcReal.left;
		m_ptOffset.y = pt.y - rcReal.top;
	}

	ASLControl::MouseDown(pt, mb);
}

void ASLForm::MouseUp(POINT pt, MouseButton mb)
{
	if (!m_bMovable)
	{
		return;
	}

	if (mb == mbLeft)
	{
		m_bHolding = false;
	}

	ASLControl::MouseUp(pt, mb);
}

void ASLForm::MouseMove(POINT pt)
{
	if (!m_bMovable)
	{
		return;
	}

	if (m_bHolding)
	{
		SetLeft(pt.x - m_ptOffset.x);
		SetTop(pt.y - m_ptOffset.y);
	}

	ASLControl::MouseMove(pt);
}

//-----------------------------------------------------------------------------
// ASLLabel类实现
//-----------------------------------------------------------------------------
void ASLLabel::Create(ASLControl *pParent, int nLeft, int nTop, LPCSTR szCaption)
{
	ASLControl::Create();
	SetParent(pParent);
	SetLeft(nLeft);
	SetTop(nTop);
	SetCaption(szCaption);
}

void ASLLabel::Draw(void) const
{
	if (!m_bVisible || m_pFont == NULL)
	{
		return;
	}

	POINT pt = { 0, 0 };
	ClientToScreen(pt);		
	m_pFont->DrawText(GUI.GetCanvas(), pt.x, pt.y, m_strCaption.c_str(), m_clFontColor);
}

//-----------------------------------------------------------------------------
// ASLImage类实现
//-----------------------------------------------------------------------------
ASLImage::ASLImage(void) 
: m_pbmImage(NULL)
{
}

void ASLImage::Create(ASLControl *pParent, int nLeft, int nTop, ASLBitmap &bmImage)
{
	ASLControl::Create();
	SetParent(pParent);
	SetLeft(nLeft);
	SetTop(nTop);
	SetImage(bmImage);
}

void ASLImage::Draw(void) const
{
	if (m_pbmImage == NULL)
	{
		return;
	}

	POINT pt = { 0, 0 };
	ClientToScreen(pt);

	if (m_pbmImage->IsAlphaChannel())
	{
		m_pbmImage->DrawAlphaChannel(GUI.GetCanvas(), pt.x, pt.y);
	}
	else
	{
		m_pbmImage->Draw(GUI.GetCanvas(), pt.x, pt.y);
	}
}

void ASLImage::SetImage(ASLBitmap &bmImage)
{
	m_pbmImage = &bmImage;
	m_nWidth = m_pbmImage->GetWidth();
	m_nHeight = m_pbmImage->GetHeight();
}

//-----------------------------------------------------------------------------
// ASLPicButton类实现
//-----------------------------------------------------------------------------
ASLPicButton::ASLPicButton(void) 
: m_pbmGlyph(NULL)
, m_bsState(bsUp)
, m_bHolding(false)
, m_bHovering(false)
{
	m_ptDownOffset.x = 0;
	m_ptDownOffset.y = 0;
}

void ASLPicButton::Draw(void) const
{
	if (!m_bVisible || m_pbmGlyph == NULL)
	{
		return;
	}

	POINT pt = { 0, 0 };
	if (m_bsState == bsDown)
	{
		pt.x += m_ptDownOffset.x;
		pt.y += m_ptDownOffset.y;
	}
	ClientToScreen(pt);

	ButtonState bsSeq = m_bsState;
	if (!m_bEnabled)
	{
		bsSeq = bsDisabled;
	}

	if (m_pbmGlyph->IsAlphaChannel())
	{
		m_pbmGlyph->DrawAlphaChannel(GUI.GetCanvas(), pt.x, pt.y, bsSeq);
	}
	else
	{
		m_pbmGlyph->Draw(GUI.GetCanvas(), pt.x, pt.y, bsSeq);
	}
}

void ASLPicButton::SetGlyph(ASLBitmap &bmGlyph)
{
	m_pbmGlyph = &bmGlyph;
	m_nWidth = m_pbmGlyph->GetWidth() / 4;
	m_nHeight = m_pbmGlyph->GetHeight();
	m_pbmGlyph->SetBlock(4, 1);
}

void ASLPicButton::SetDownOffset(int x, int y)
{
	m_ptDownOffset.x = x;
	m_ptDownOffset.y = y;
}

void ASLPicButton::MouseEnter(void)
{
	m_bHovering = true;
	if (m_bHolding)
	{
		m_bsState = bsDown;
	}
	else
	{
		m_bsState = bsHot;
	}

	ASLControl::MouseEnter();
}

⌨️ 快捷键说明

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