162 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			C++
		
	
	
	
			
		
		
	
	
			162 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			C++
		
	
	
	
| #include "..\enodes.h"
 | ||
| 
 | ||
| e2d::EText::EText()
 | ||
| 	: m_bWordWrapping(false)
 | ||
| 	, m_pFont(nullptr)
 | ||
| 	, m_fWordWrappingWidth(0)
 | ||
| {
 | ||
| 	this->setFont(new EFont());
 | ||
| }
 | ||
| 
 | ||
| e2d::EText::EText(const EString & text)
 | ||
| 	: m_bWordWrapping(false)
 | ||
| 	, m_pFont(nullptr)
 | ||
| 	, m_fWordWrappingWidth(0)
 | ||
| {
 | ||
| 	this->setText(text);
 | ||
| 	this->setFont(new EFont());
 | ||
| }
 | ||
| 
 | ||
| e2d::EText::EText(EFont * font)
 | ||
| 	: m_bWordWrapping(false)
 | ||
| 	, m_pFont(nullptr)
 | ||
| 	, m_fWordWrappingWidth(0)
 | ||
| {
 | ||
| 	this->setFont(font);
 | ||
| }
 | ||
| 
 | ||
| e2d::EText::EText(const EString & text, EFont * font)
 | ||
| 	: m_bWordWrapping(false)
 | ||
| 	, m_pFont(nullptr)
 | ||
| 	, m_fWordWrappingWidth(0)
 | ||
| {
 | ||
| 	this->setText(text);
 | ||
| 	this->setFont(font);
 | ||
| }
 | ||
| 
 | ||
| e2d::EText::EText(const EString & text, EString fontFamily, float fontSize, UINT32 color, UINT32 fontWeight, bool italic)
 | ||
| 	: m_bWordWrapping(false)
 | ||
| 	, m_pFont(nullptr)
 | ||
| 	, m_fWordWrappingWidth(0)
 | ||
| {
 | ||
| 	this->setText(text);
 | ||
| 	this->setFont(new EFont(fontFamily, fontSize, color, fontWeight, italic));
 | ||
| }
 | ||
| 
 | ||
| e2d::EText::~EText()
 | ||
| {
 | ||
| 	SafeRelease(&m_pFont);
 | ||
| }
 | ||
| 
 | ||
| e2d::EString e2d::EText::getText() const
 | ||
| {
 | ||
| 	return m_sText;
 | ||
| }
 | ||
| 
 | ||
| float e2d::EText::getWidth() const
 | ||
| {
 | ||
| 	return m_fWordWrappingWidth * m_fScaleX;
 | ||
| }
 | ||
| 
 | ||
| float e2d::EText::getRealWidth() const
 | ||
| {
 | ||
| 	return m_fWordWrappingWidth;
 | ||
| }
 | ||
| 
 | ||
| e2d::EFont * e2d::EText::getFont() const
 | ||
| {
 | ||
| 	return m_pFont;
 | ||
| }
 | ||
| 
 | ||
| void e2d::EText::setText(const EString & text)
 | ||
| {
 | ||
| 	m_sText = text;
 | ||
| 	_initTextLayout();
 | ||
| }
 | ||
| 
 | ||
| void e2d::EText::setFont(EFont * font)
 | ||
| {
 | ||
| 	if (font)
 | ||
| 	{
 | ||
| 		SafeRelease(&m_pFont);
 | ||
| 		m_pFont = font;
 | ||
| 		font->retain();
 | ||
| 
 | ||
| 		_initTextLayout();
 | ||
| 	}
 | ||
| }
 | ||
| 
 | ||
| void e2d::EText::setWordWrapping(bool value)
 | ||
| {
 | ||
| 	m_bWordWrapping = value;
 | ||
| 	_initTextLayout();
 | ||
| }
 | ||
| 
 | ||
| void e2d::EText::setWordWrappingWidth(float wordWrapWidth)
 | ||
| {
 | ||
| 	m_fWordWrappingWidth = max(wordWrapWidth, 0);
 | ||
| 	_initTextLayout();
 | ||
| }
 | ||
| 
 | ||
| void e2d::EText::onRender()
 | ||
| {
 | ||
| 	ERenderer::getSolidColorBrush()->SetColor(D2D1::ColorF(m_pFont->m_Color, m_fDisplayOpacity));
 | ||
| 	ERenderer::getRenderTarget()->DrawTextW(
 | ||
| 		m_sText,
 | ||
| 		UINT32(m_sText.length()),
 | ||
| 		m_pFont->_getTextFormat(),
 | ||
| 		D2D1::RectF(
 | ||
| 			0,
 | ||
| 			0,
 | ||
| 			m_bWordWrapping ? m_fWordWrappingWidth : m_Size.width,
 | ||
| 			getRealHeight()
 | ||
| 		),
 | ||
| 		ERenderer::getSolidColorBrush()
 | ||
| 	);
 | ||
| }
 | ||
| 
 | ||
| void e2d::EText::_initTextLayout()
 | ||
| {
 | ||
| 	// δ<><CEB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD>Ϊ 0
 | ||
| 	if (!m_pFont || m_sText.isEmpty())
 | ||
| 	{
 | ||
| 		this->_setHeight(0);
 | ||
| 		m_fWordWrappingWidth = 0;
 | ||
| 		return;
 | ||
| 	}
 | ||
| 
 | ||
| 	// δ<><CEB4><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><C4B1>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD> TextFormat <20><><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | ||
| 	if (!m_bWordWrapping)
 | ||
| 	{
 | ||
| 		m_pFont->_getTextFormat()->SetWordWrapping(DWRITE_WORD_WRAPPING_NO_WRAP);
 | ||
| 	}
 | ||
| 	else
 | ||
| 	{
 | ||
| 		m_pFont->_getTextFormat()->SetWordWrapping(DWRITE_WORD_WRAPPING_WRAP);
 | ||
| 	}
 | ||
| 	
 | ||
| 	// <20><>ȡ TextLayout
 | ||
| 	IDWriteTextLayout * pDWriteTextLayout = nullptr;
 | ||
| 
 | ||
| 	HRESULT hr = ERenderer::getIDWriteFactory()->CreateTextLayout(
 | ||
| 		m_sText,
 | ||
| 		UINT32(m_sText.length()),
 | ||
| 		m_pFont->_getTextFormat(),
 | ||
| 		m_bWordWrapping ? m_fWordWrappingWidth : 0,
 | ||
| 		0,
 | ||
| 		&pDWriteTextLayout
 | ||
| 	);
 | ||
| 
 | ||
| 	ASSERT(SUCCEEDED(hr), "Create IDWriteTextFormat Failed!");
 | ||
| 
 | ||
| 	// <20><>ȡ<EFBFBD>ı<EFBFBD><C4B1><EFBFBD><EFBFBD>ֵĿ<D6B5><C4BF>Ⱥ߶<CDB8>
 | ||
| 	DWRITE_TEXT_METRICS metrics;
 | ||
| 	pDWriteTextLayout->GetMetrics(&metrics);
 | ||
| 
 | ||
| 	this->_setSize(metrics.widthIncludingTrailingWhitespace, metrics.height);
 | ||
| 	m_fWordWrappingWidth = metrics.widthIncludingTrailingWhitespace;
 | ||
| 
 | ||
| 	// ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ
 | ||
| 	SafeReleaseInterface(&pDWriteTextLayout);
 | ||
| }
 |