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

📄 asl_guiobj.cpp

📁 泡泡堂单机版(含ASL游戏引擎源码 泡泡堂单机版(含ASL游戏引擎源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:

void ASLPicButton::MouseLeave(void)
{
	m_bHovering = false;
	m_bsState = bsUp;

	ASLControl::MouseLeave();
}

void ASLPicButton::MouseDown(POINT pt, MouseButton mb)
{
	if (mb == mbLeft)
	{
		m_bsState = bsDown;
		m_bHolding = true;
	}

	ASLControl::MouseDown(pt, mb);
}

void ASLPicButton::MouseUp(POINT pt, MouseButton mb)
{
	if (mb == mbLeft)
	{
		if (m_bsState == bsDown && OnClick.function != NULL)
		{
			(OnClick.invoker->*OnClick.function)();
		}

		if (m_bHovering)
		{
			m_bsState = bsHot;
		}
		else
		{
			m_bsState = bsUp;
		}
		m_bHolding = false;
	}

	ASLControl::MouseUp(pt, mb);		
}

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

void ASLButton::Draw(void) const
{
	ASLPicButton::Draw();

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

	COLOR clFontColor = m_clFontColor;
	if (!m_bEnabled)
	{
		clFontColor = RGB16(150, 150, 150);
	}

	if (m_pFont != NULL)
	{
		m_pFont->DrawText(GUI.GetCanvas(), pt.x + m_ptTextPos.x, pt.y + m_ptTextPos.y, 
			m_strCaption.c_str(), clFontColor);
	}
}

void ASLButton::SetCaption(LPCSTR szCaption)
{
	ASSERT(m_pFont != NULL);

	ASLControl::SetCaption(szCaption);
	int len = (int)m_strCaption.length();
	m_ptTextPos.x = (m_nWidth - len * m_pFont->GetCharWidth()) / 2;
	m_ptTextPos.y = (m_nHeight - m_pFont->GetCharHeight()) / 2;
}

//-----------------------------------------------------------------------------
// ASLCheckBox类实现
//-----------------------------------------------------------------------------
ASLCheckBox::ASLCheckBox(void) 
: m_bChecked(false)
{
}

void ASLCheckBox::Create(ASLControl *pParent, int nLeft, int nTop, 
						 ASLBitmap &bmGlyph, LPCSTR szCaption)
{
	ASLControl::Create();
	SetParent(pParent);
	SetLeft(nLeft);
	SetTop(nTop);
	SetGlyph(bmGlyph);
	SetCaption(szCaption);
}

void ASLCheckBox::Draw(void) const
{
	ASLPicButton::Draw();

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

	if (m_bChecked && m_pbmGlyph != NULL)
	{
		int nCheckSeq = m_bEnabled ? 4 : 5;
		if (m_pbmGlyph->IsAlphaChannel())
		{
			m_pbmGlyph->DrawAlphaChannel(GUI.GetCanvas(), pt.x, pt.y, nCheckSeq);
		}
		else
		{
			m_pbmGlyph->Draw(GUI.GetCanvas(), pt.x, pt.y, nCheckSeq);
		}
	}

	if (m_pFont != NULL)
	{
		int nHeight = m_pFont->GetCharHeight();
		int nOffsetY = (m_nHeight - nHeight) / 2;
		m_pFont->DrawText(GUI.GetCanvas(), pt.x + m_nWidth + 10, pt.y + nOffsetY, 
			m_strCaption.c_str(), m_clFontColor);
	}
}

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

void ASLCheckBox::MouseUp(POINT pt, MouseButton mb)
{
	if (mb == mbLeft && m_bsState == bsDown)
	{
		m_bChecked = !m_bChecked;
	}
	ASLPicButton::MouseUp(pt, mb);
}

//-----------------------------------------------------------------------------
// ASLRadioButton类实现
//-----------------------------------------------------------------------------
ASLRadioButton::ASLRadioButton(void) 
: m_nGroup(0)
{	
}

ASLRadioButton::~ASLRadioButton(void)
{
	GUI.DelRadio(this);
}

void ASLRadioButton::Create(ASLControl *pParent, int nLeft, int nTop, 
							ASLBitmap &bmGlyph, LPCSTR szCaption)
{
	ASLControl::Create();
	SetParent(pParent);
	SetLeft(nLeft);
	SetTop(nTop);
	SetGlyph(bmGlyph);
	SetCaption(szCaption);
	GUI.AddRadio(this);
}

void ASLRadioButton::Check(void)
{
	GUI.CheckRadio(this);
	m_bChecked = true;
}

void ASLRadioButton::MouseUp(POINT pt, MouseButton mb)
{
	if (mb == mbLeft && m_bsState == bsDown)
	{
		m_bChecked = true;
	}
	
	if (m_bChecked)
	{
		GUI.CheckRadio(this);
	}
	
	ASLPicButton::MouseUp(pt, mb);
}

//-----------------------------------------------------------------------------
// ASLScrollBar类实现
//-----------------------------------------------------------------------------
ASLScrollBar::ASLScrollBar(void) 
: m_sbKind(sbHorizontal)
, m_pbmSlider(NULL)
, m_nOffset(0)
, m_nSliderPos(0)
, m_bHolding(false)
, m_nMin(0)
, m_nMax(100)
{
}

void ASLScrollBar::Create(ASLControl *pParent, int nLeft, int nTop, 
						  ScrollBarKind sbKind, ASLBitmap &bmSlider, int nLength)
{
	ASLControl::Create();
	SetParent(pParent);
	SetLeft(nLeft);
	SetTop(nTop);
	SetKind(sbKind);
	SetSlider(bmSlider);
	SetLength(nLength);
}

void ASLScrollBar::Draw(void) const
{
	if (m_pbmSlider == NULL)
	{
		return;
	}

	POINT pt = { 0, 0 };
	if (m_sbKind == sbHorizontal)
	{
		pt.x = m_nSliderPos;
	}
	else
	{
		pt.y = m_nSliderPos;
	}
	ClientToScreen(pt);

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

void ASLScrollBar::SetSlider(ASLBitmap &bmSlider)
{
	m_pbmSlider = &bmSlider;
	if (m_sbKind == sbHorizontal)
	{
		SetHeight(bmSlider.GetHeight());
	}
	else
	{
		SetWidth(bmSlider.GetWidth());
	}
}

void ASLScrollBar::SetLength(int nLength)
{
	if (m_sbKind == sbHorizontal)
	{
		SetWidth(nLength);
	}
	else
	{
		SetHeight(nLength);
	}	
}

int ASLScrollBar::GetPosition(void) const
{
	int nLen;

	if (m_sbKind == sbHorizontal)
	{
		nLen = m_nWidth - m_pbmSlider->GetWidth();
	}
	else
	{
		nLen = m_nHeight - m_pbmSlider->GetHeight();
	}

	return m_nSliderPos * (m_nMax - m_nMin) / nLen + m_nMin;
}

void ASLScrollBar::SetPosition(int nPos)
{
	int nLen;

	if (m_sbKind == sbHorizontal)
	{
		nLen = m_nWidth - m_pbmSlider->GetWidth();
	}
	else
	{
		nLen = m_nHeight - m_pbmSlider->GetHeight();
	}

	m_nSliderPos = (nPos - m_nMin) * nLen / (m_nMax - m_nMin);
}

void ASLScrollBar::MouseDown(POINT pt, MouseButton mb)
{
	if (mb == mbLeft)
	{
		ScreenToClient(pt);
		if (m_sbKind == sbHorizontal)
		{
			if (pt.x >= m_nSliderPos && pt.x < m_nSliderPos + m_pbmSlider->GetWidth())
			{
				m_bHolding = true;
				m_nOffset = pt.x - m_nSliderPos;
			}
		}
		else
		{
			if (pt.y >= m_nSliderPos && pt.y < m_nSliderPos + m_pbmSlider->GetHeight())
			{
				m_bHolding = true;
				m_nOffset = pt.y - m_nSliderPos;
			}
		}
	}

	ASLControl::MouseDown(pt, mb);
}

void ASLScrollBar::MouseUp(POINT pt, MouseButton mb)
{
	if (mb == mbLeft && m_bHolding)
	{
		m_bHolding = false;

		if (OnChange.function != NULL)
		{
			(OnChange.invoker->*OnChange.function)();
		}
	}

	ASLControl::MouseUp(pt, mb);
}

void ASLScrollBar::MouseMove(POINT pt)
{
	if (m_bHolding)
	{
		ScreenToClient(pt);

		if (m_sbKind == sbHorizontal)
		{
			m_nSliderPos = pt.x - m_nOffset;
			if (m_nSliderPos < 0)
			{
				m_nSliderPos = 0;
			}
			else if (m_nSliderPos > m_nWidth - m_pbmSlider->GetWidth())
			{
				m_nSliderPos = m_nWidth - m_pbmSlider->GetWidth();
			}
		}
		else
		{
			m_nSliderPos = pt.y - m_nOffset;
			if (m_nSliderPos < 0)
			{
				m_nSliderPos = 0;
			}
			else if (m_nSliderPos > m_nHeight - m_pbmSlider->GetHeight())
			{
				m_nSliderPos = m_nHeight - m_pbmSlider->GetHeight();
			}
		}

		if (OnScroll.function != NULL)
		{
			(OnScroll.invoker->*OnScroll.function)();
		}
	}

	ASLControl::MouseMove(pt);
}

//-----------------------------------------------------------------------------
// ASLEdit类实现
//-----------------------------------------------------------------------------
ASLEdit::ASLEdit(void) 
: m_strText("")
, m_fTime(0.0f)
, m_nCaretPos(0)
, m_bShowCaret(true)
, m_nCharWidth(0)
, m_csStyle(csVertical)
{	
}

void ASLEdit::Create(ASLControl *pParent, int nLeft, int nTop, int nWidth)
{
	ASLControl::Create();
	SetParent(pParent);
	SetLeft(nLeft);
	SetTop(nTop);
	SetWidth(nWidth);
}

void ASLEdit::Draw(void) const
{
	POINT pt = { 0, 0 };
	ClientToScreen(pt);

	if (m_strText != "")
	{
		if (m_bEnabled)
		{
			m_pFont->DrawText(GUI.GetCanvas(), pt.x, pt.y, m_clFontColor, m_strText.c_str());
		}
		else
		{
			m_pFont->DrawText(GUI.GetCanvas(), pt.x, pt.y, RGB16(150, 150, 150), m_strText.c_str());
		}
	}
	if (m_bFocused && m_bShowCaret && m_bEnabled)
	{
		pt.x += m_nCharWidth * m_nCaretPos;
		if (m_csStyle == csVertical)
		{
			GUI.GetCanvas().Line(m_clCaretColor, pt.x, pt.y, pt.x, pt.y + m_nHeight);
		}
		else
		{
			pt.y += m_nHeight;
			GUI.GetCanvas().Line(m_clCaretColor, pt.x, pt.y, pt.x + m_nCharWidth, pt.y);
		}
	}
}

void ASLEdit::Update(float fDelta)
{
	m_fTime += fDelta;
	if (m_fTime >= 0.5)
	{
		m_fTime -= 0.5;
		m_bShowCaret = !m_bShowCaret;
	}
}

void ASLEdit::SetParent(ASLControl *pParent)
{
	ASLControl::SetParent(pParent);
	m_clCaretColor = m_clFontColor;
	if (m_pFont != NULL)
	{
		m_nHeight = m_pFont->GetCharHeight();
		m_nCharWidth = m_pFont->GetCharWidth();		
	}	
}

void ASLEdit::SetFont(ASLFont &fnt)
{
	ASLControl::SetFont(fnt);
	if (m_pFont != NULL)
	{
		m_nHeight = m_pFont->GetCharHeight();
		m_nCharWidth = m_pFont->GetCharWidth();
	}
}

void ASLEdit::MouseDown(POINT pt, MouseButton mb)
{
	if (mb != mbLeft)
	{
		return;
	}

	ScreenToClient(pt);
	m_nCaretPos = (pt.x + m_nCharWidth/2) / m_nCharWidth;
	if (m_nCaretPos > (int)m_strText.size())
	{
		m_nCaretPos = (int)m_strText.size();
	}
	m_fTime = 0;
	m_bShowCaret = true;

	ASLControl::MouseDown(pt, mb);
}

void ASLEdit::KeyPress(char cKey)
{
	if (cKey < 33 || ((int)m_strText.size()+1) * m_nCharWidth > m_nWidth)
	{
		return;
	}

	std::string former = m_strText.substr(0, m_nCaretPos);
	std::string latter = m_strText.substr(m_nCaretPos, m_strText.size() - m_nCaretPos);
	m_strText = former + cKey + latter;
	MoveCaret(1);

	ASLControl::KeyPress(cKey);
}

void ASLEdit::KeyDown(DWORD dwKey, ShiftState ss)
{
	if (dwKey == VK_RIGHT)
	{
		MoveCaret(1);
		if (m_nCaretPos > (int)m_strText.size())
		{
			m_nCaretPos = (int)m_strText.size();
		}
	}
	else if (dwKey == VK_LEFT)
	{
		MoveCaret(-1);
		if (m_nCaretPos < 0)
		{
			m_nCaretPos = 0;
		}
	}
	else if (dwKey == VK_DELETE && m_nCaretPos < (int)m_strText.size())
	{
		std::string former = m_strText.substr(0, m_nCaretPos);
		std::string latter = m_strText.substr(m_nCaretPos+1, m_strText.size() - m_nCaretPos);
		m_strText = former + latter;
	}
	else if (dwKey == VK_BACK && m_nCaretPos > 0)
	{
		std::string former = m_strText.substr(0, m_nCaretPos-1);
		std::string latter = m_strText.substr(m_nCaretPos, m_strText.size() - m_nCaretPos);
		m_strText = former + latter;
		MoveCaret(-1);
	}

	ASLControl::KeyDown(dwKey, ss);
}

void ASLEdit::MoveCaret(int nOffset)
{
	// 当前位置加上偏移值
	m_nCaretPos += nOffset;

	// 调整位置使之合法
	if (m_nCaretPos > (int)m_strText.size())
	{
		m_nCaretPos = (int)m_strText.size();
	}
	else if (m_nCaretPos < 0)
	{
		m_nCaretPos = 0;
	}
	
	// 设光标为显示状态
	m_bShowCaret = true;
	m_fTime = 0;
}

} // namespace ASL

⌨️ 快捷键说明

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