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

📄 guicellarrayctrl.cc

📁 五行MMORPG引擎系统V1.0
💻 CC
📖 第 1 页 / 共 2 页
字号:
void GuiCellArrayControl::inspectPostApply()
{
	Parent::inspectPostApply();

	if(m_nCellAmount != m_arCellBtns.size())
	{
		destroyCells();
		createCells(/*m_arCellBtns.size()*/);
	}
	else
	{	
		updateCells();
	}

	setUpdate();
   //resize(getPosition(), getExtent());
}

//void GuiCellArrayControl::onNameChange(const char *name)
//{
//}

//void GuiCellArrayControl::onStaticModified(const char* slotName)
//{
//	if(GuiControl::smDesignTime)
//	{
//		if(slotName == StringTable->lookup("cellAmount"))
//		{
//			//m_bNeedCreateCells
//		}
//	}
//
//}



void GuiCellArrayControl::refreshCell(GuiCellButtonCtrl* pCell)
{
	U32 nIndex = pCell->m_nCellIndex;
	StringTableEntry pName;


	pName = m_arCellNames[nIndex];

	pCell->setGameIcon(pName,true);

	if(nIndex < m_arCellAccels.size() && m_arCellAccels[nIndex][0])
		pCell->mAcceleratorKey = m_arCellAccels[nIndex];


	if(nIndex < m_arCellCursors.size() && m_arCellCursors[nIndex][0])
		pCell->m_pCursorName = m_arCellCursors[nIndex];


	//Cell Align设置
	if(m_uCellAlign != ALIGN_NONE)
		updateCell(pCell);

	//Profile设置
	if(m_pCellProfile == NULL)
		pCell->mProfile		= mProfile;
	else
		pCell->mProfile		= m_pCellProfile;
	pCell->mTooltipProfile	= mTooltipProfile;
}

void GuiCellArrayControl::destroyCells()
{
	U32	n=0;
	for(n = 0; n < m_arCellBtns.size(); n++)
	{	
		removeObject(m_arCellBtns[n]);
		m_arCellBtns[n]->deleteObject();
	}
	m_arCellBtns.setSize(0);
}



static void checkCellArrayVector(Vector<StringTableEntry>& arDat,S32 nAmount)
{

	if(arDat.size() < nAmount)
	{
		U32 nSize = arDat.size();
		arDat.setSize(nAmount);
		for(S32 col = nSize; col < nAmount; col++)
			arDat[col] = StringTable->getBlank();
	}
	else if(arDat.size() > nAmount)
		arDat.setSize(nAmount);

}


void GuiCellArrayControl::initCells(U32 uAmount)
{
	destroyCells();

	//clearCell
	m_nCellAmount = uAmount;
	checkCellArrayVector(m_arCellNames,m_nCellAmount);

}


void GuiCellArrayControl::createCells(U32 uBase)
{
	//removeObject();
	//if(m_nCellAmount uBase 

	AssertWarn(m_arCellBtns.size() == 0,"请先调用destroyCells()");

	U32		col,row,nIndex;
	S32		nCenter,nTitleOffset;
	Point2I	stepSize;
	Point2I	boundSize(mBounds.extent.x - mColSpacing, mBounds.extent.y - mRowSpacing);
	Point2I	cellPos;
	GuiCellButtonCtrl* pCell;

	StringTableEntry pBlank = StringTable->getBlank();
	StringTableEntry pName;


	//计算cell区域
	calcMaxStrWidth();


	stepSize.set(m_cellAreaSize.x + mColSpacing, m_cellAreaSize.y + mRowSpacing);

	if(m_nCols <= 0)
		m_nCols = m_nCellAmount;
		//m_nCols = boundSize.x / stepSize.x;
	m_nRows	= m_nCellAmount / m_nCols;

	if(m_nCellAmount % m_nCols)
		m_nRows++;

	if(m_pText[0])
		nTitleOffset = mProfile->mFont->getHeight() + mRowSpacing;
	else
		nTitleOffset = mRowSpacing;


	m_cellNumBak.x = m_nCols;
	m_cellNumBak.y	= m_nRows;


	//填充Names后面的空白
	checkCellArrayVector(m_arCellNames,m_nCellAmount);

	//填充Blanks后面的空白
	checkCellArrayVector(m_arCellBlanks,m_nCellAmount);

	checkCellArrayVector(m_arCellCursors,m_nCellAmount);
	checkCellArrayVector(m_arCellAccels,m_nCellAmount);





	nIndex = 0;
	for(row = 0; row < m_nRows; row++)
	{
		cellPos.y = nTitleOffset +  row * stepSize.y;
		for(col = 0; col < m_nCols; col++,nIndex++)
		{
			if(nIndex < uBase)
				continue;

			if(nIndex >= m_nCellAmount)
				goto laEnd;

			cellPos.x = mColSpacing + col * stepSize.x;

			pCell = new GuiCellButtonCtrl(this);
			pCell->m_nCellIndex	= nIndex;

			pCell->resize(cellPos,m_cellAreaSize);

			refreshCell(pCell);

			pCell->registerObject();
			addObject(pCell);
			m_arCellBtns.push_back(pCell);

			//pCell->awaken();
		}
	}

laEnd:
	setExtent(Point2I(0,0));
	m_bNeedCreateCells = false;
	return;
}


bool GuiCellArrayControl::setCellName(U32 nIndex, StringTableEntry pCellName)
{
	if(pCellName == NULL || pCellName[0] == 0)
		return false;
	if(nIndex >= m_nCellAmount)
		return false;
	

	if(!m_arCellBtns.size())
	{
		createCells();
	}
	if(m_arCellBtns[nIndex] == NULL)
		return false;

	m_arCellNames[nIndex] = pCellName;
	updateGameIcon(nIndex);
		//GuiCellButtonCtrl*	pCell;
		//Point2I					cellPos;

		//cellPos.x	= (nIndex % m_nCols) * m_cellAreaSize.x;
		//cellPos.y	= (nIndex / m_nCols) * m_cellAreaSize.y;
		//
		//pCell = new GuiCellButtonCtrl(this);
		//pCell->m_nCellIndex	= nIndex;
		//
		//pCell->resize(cellPos,m_cellAreaSize);

		//refreshCell(pCell);

		//pCell->registerObject();
		//addObject(pCell);
		//m_arCellBtns[nIndex] = pCell;

	return true;
}



void GuiCellArrayControl::updateCell(GuiCellButtonCtrl* pCell)
{
	//Point2I	cellSize(m_cellSize);
	S32	nCenter,
			nIconCenter;
	S32	nOffset	= (m_bCellBorder)?0 : 1;
	StringTableEntry pName;

	if(bool(m_iconTextureHandle))
		pName = m_arCellNames[pCell->m_nCellIndex];
	else
		pName = pCell->m_pGameIcon->pName;


	if(m_uCellAlign & 1)
	{	
		nCenter		= ((S32)(m_cellAreaSize.y - mProfile->mFont->getHeight()) / 2);
		if(m_uCellAlign == ALIGN_LEFT)
		{
			pCell->m_iconOffset	= Point2I(m_cellAreaSize.x - (m_cellSize.x+nOffset), nOffset);
			pCell->m_textOffset	= Point2I(nOffset,nCenter);
		}
		else
		{
			pCell->m_iconOffset	= Point2I(nOffset,nOffset);
			pCell->m_textOffset = Point2I(m_cellSize.x + mColSpacing ,  nCenter );
		}

	}
	else
	{
		nIconCenter	= (m_cellAreaSize.x - m_cellSize.x)/2;
		nCenter		= ((S32)(m_cellAreaSize.x - mProfile->mFont->getStrWidth(pName)) / 2);
		if(m_uCellAlign == ALIGN_TOP)
		{
			pCell->m_iconOffset	= Point2I(nIconCenter, m_cellAreaSize.y - (m_cellSize.y + nOffset));
			pCell->m_textOffset = Point2I(nCenter,nOffset);
		}
		else
		{
			pCell->m_iconOffset	= Point2I(nIconCenter,nOffset);
			pCell->m_textOffset = Point2I(nCenter, m_cellSize.y + mRowSpacing + nOffset );
		}
	}

}


void GuiCellArrayControl::updateCells()
{
	if(m_arCellBtns.size() == 0)
		return;


	U32		col,row,nIndex;
	S32		nCenter,nTitleOffset;
	Point2I	stepSize;
	Point2I	boundSize(mBounds.extent.x - mColSpacing, mBounds.extent.y - mRowSpacing);
	Point2I	cellPos;	
	GuiCellButtonCtrl* pCell;


	///////////////////////////////////
	//计算cell区域
	calcMaxStrWidth();

	stepSize.set(m_cellAreaSize.x + mColSpacing, m_cellAreaSize.y + mRowSpacing);

	if(m_nCols <= 0)
		m_nCols = boundSize.x / stepSize.x;
	m_nRows	= m_nCellAmount / m_nCols;

	if(m_nCellAmount % m_nCols)
		m_nRows++;



	if(m_pText[0])
		nTitleOffset = mProfile->mFont->getHeight() + mRowSpacing;
	else
		nTitleOffset = mRowSpacing;


	nIndex = 0;
	for(row = 0; row < m_nRows; row++)
	{
		cellPos.y = nTitleOffset + row * stepSize.y;
		for(col = 0; col < m_nCols; col++,nIndex++)
		{
			if(nIndex >= m_nCellAmount)
				goto laEnd;
			pCell = m_arCellBtns[nIndex];
			if(pCell == NULL)
				continue;

			cellPos.x = mColSpacing + col * stepSize.x;

			pCell->resize(cellPos,m_cellAreaSize);

			refreshCell(pCell);


			////Cell Align设置
			//if(m_uCellAlign != ALIGN_NONE)
			//	updateCell(pCell);
		}
	}

laEnd:
	setExtent(Point2I(0,0));
	return;
}


void GuiCellArrayControl::calcMaxStrWidth()
{
	AssertWarn(mProfile->mFont,"需要在Profile之后,方调用");
	U32 n;
	U32 w;
	m_uMaxStrWidth = 0;

	//非align处理
	if(m_uCellAlign == ALIGN_NONE)
	{
		m_uMaxStrWidth = 0;
		m_cellAreaSize.x	= m_cellSize.x;
		m_cellAreaSize.y	= m_cellSize.y;

	}

	//左右上下各align类型处理
	else
	{

		//获取字符最大长度
		StringTableEntry				pName;

		for(n = 0; n < m_arCellNames.size(); n++)
		{
			if(bool(m_iconTextureHandle))
			{
				pName = m_arCellNames[n];
			}
			else
			{
				RPG::GameIconDictionary::Entry* pEntry;
				pEntry = RPG::g_pGameIconMan->GetGameIcon(m_arCellNames[n]);
				if(!pEntry)
					continue;
				pName = pEntry->pName;
			}
			w = mProfile->mFont->getStrWidth(pName);

			if(w > m_uMaxStrWidth)
				m_uMaxStrWidth = w;
		}

		//生成cell区域
		if(m_uCellAlign & 1)
		{	
			m_cellAreaSize.x	= m_cellSize.x + mColSpacing*2 + m_uMaxStrWidth;
			m_cellAreaSize.y	= m_cellSize.y;
		}
		else
		{
			m_cellAreaSize.x	= getMax(m_cellSize.x,(S32)m_uMaxStrWidth);//m_cellSize.x;
			m_cellAreaSize.y	= m_cellSize.y + mRowSpacing*2 + mProfile->mFont->getHeight();
		}
	}


	//如果有边界,区域w,h加2
	if(m_bCellBorder)
		m_cellAreaSize += Point2I(2,2);


}



bool GuiCellArrayControl::processGoodsInfos(RPG::GGoodsArray::GGoodsInfo goodsInfo[],U32 nNum)
{
	GuiCellArrayControl*		pCellArrayUI;
	StringTableEntry			pUIName;
	ERPGGoodsArrayTypes		type;
	U32							n;

	for(n=0; n<nNum; n++)
	{
		if(goodsInfo[n].nState == GGoodsArray::GAS_FAILED)
			continue;
		if(goodsInfo[n].pOwner == NULL)
			continue;

		if(goodsInfo[n].pOwner == m_pDataSource && goodsInfo[n].nState == GGoodsArray::GAS_UPDATE)
		{
			updateGameIcon(goodsInfo[n].nIndex - m_uBaseIndex);
			continue;
		}

		type		= goodsInfo[n].pOwner->GetArrayType(goodsInfo[n].nIndex);
		pUIName	= RPG::g_pRPGSetting->GetGoodsArTypeUIName(type);
		if(	pUIName && pUIName[0] && Sim::findObject(pUIName, pCellArrayUI))
		{
			if(goodsInfo[n].nState == GGoodsArray::GAS_UPDATE)
				pCellArrayUI->updateGameIcon(goodsInfo[n].nIndex - pCellArrayUI->m_uBaseIndex);
		}
	}//for

	return nNum > 0;
}



void GuiCellArrayControl::clearCell(GuiCellButtonCtrl* pCell, bool bDiscard)
{

	if(pCell == NULL)
		return;

	BOOL bRet;

	if(StringTable->isBlank(m_arCellNames[pCell->m_nCellIndex]))
		return;

	if(m_pDataSource)
	{
		if(bDiscard)
		{	
			U32							nNum;
			GGoodsArray::GGoodsInfo	goodsInfos[3];
			nNum = m_pDataSource->DiscardGoods(pCell->m_nCellIndex + m_uBaseIndex,goodsInfos);
			processGoodsInfos(goodsInfos, nNum);
			return;
		}
		else
			bRet = m_pDataSource->RemoveGoods(pCell->m_nCellIndex + m_uBaseIndex);

		if(!bRet)
			return;
	}

	m_arCellNames[pCell->m_nCellIndex] = StringTable->getBlank();
	pCell->setGameIcon(NULL,true);
	savePrefData();
}


U32 GuiCellArrayControl::putCellTo(GuiCellButtonCtrl* pCell, GuiCellButtonCtrl* pDest)
{
	if(pCell == NULL || pCell == pDest)
		return GPS_FAILED;

	GuiCellArrayControl	*pOwnerSrc,
								*pOwnerDest;

	pOwnerSrc	= pCell->m_pOwnerCtrl;
	pOwnerDest	= pDest->m_pOwnerCtrl;

	if(pOwnerSrc == NULL || NULL == pOwnerDest)
		return GPS_FAILED;

	if(bool(pOwnerSrc->m_iconTextureHandle) || bool(pOwnerDest->m_iconTextureHandle))
		return GPS_FAILED;


	U32			nRet;
	StringTableEntry	sSrc;
	U32 uIndex	= pCell->m_nCellIndex;
	U32 uDest	= pDest->m_nCellIndex;

	if(pOwnerSrc->m_pDataSource && pOwnerDest->m_pDataSource)
	{
		nRet = pOwnerSrc->m_pDataSource->PutGoodsTo(uIndex + m_uBaseIndex,
													pOwnerDest->m_pDataSource, 
													pOwnerDest->m_uBaseIndex + uDest);
		if(nRet == GPS_FAILED)
			return GPS_FAILED;

	}
	else
	{	
		nRet = GPS_OK;
		pOwnerDest->m_arCellNames[uDest]		= pOwnerSrc->m_arCellNames[uIndex];
		pOwnerDest->m_arCellCursors[uDest]	= pOwnerSrc->m_arCellCursors[uIndex];
		pOwnerDest->savePrefData();
	}

	//更新图标
	if(nRet == GPS_SWAP)
		updateGameIcon(uIndex);
	pOwnerDest->updateGameIcon(uDest);
	//if(nRet == GPS_SWAP)
	//	pCell->setGameIcon(pOwnerSrc->m_arCellNames[uIndex],true);
	//pDest->setGameIcon(pOwnerDest->m_arCellNames[uDest],true);
	return nRet;
}


void GuiCellArrayControl::swapCellTo(GuiCellButtonCtrl* pCell, GuiCellButtonCtrl* pDest)
{
	if(bool(m_iconTextureHandle))
		return;

	if(pCell == NULL || pCell == pDest)
		return;

	StringTableEntry	sSrc;
	U32 uIndex	= pCell->m_nCellIndex;
	U32 uDest	= pDest->m_nCellIndex;

	if(StringTable->isBlank(m_arCellNames[uIndex]))
		return;

	if(m_pDataSource)
	{
		if(m_pDataSource->SwapGoods(uIndex + m_uBaseIndex,uDest + m_uBaseIndex) == GPS_FAILED)
			return;

		m_arCellNames[uIndex]	= m_pDataSource->GetIconLabel(uIndex + m_uBaseIndex);
		m_arCellNames[uDest]		= m_pDataSource->GetIconLabel(uDest + m_uBaseIndex);
	}
	else
	{
		//交换指令位置
		sSrc							= m_arCellNames[uIndex];
		m_arCellNames[uIndex]	= m_arCellNames[uDest];
		m_arCellNames[uDest]		= sSrc;

		//交换光标位置
		sSrc							= m_arCellCursors[uIndex];
		m_arCellCursors[uIndex]	= m_arCellCursors[uDest];
		m_arCellCursors[uDest]	= sSrc;
		savePrefData();
	}


	//更新图标
	pCell->setGameIcon(m_arCellNames[uIndex],true);
	pDest->setGameIcon(m_arCellNames[uDest],true);

}



void GuiCellArrayControl::onPreRender()
{
	updateHilightCell();
}


//-------------------------------------
void GuiCellArrayControl::onRender(Point2I offset, const RectI& updateRect)
{
	if(mProfile->mBorder)
	{
		RectI rect(offset, mBounds.extent);
		dglClearBitmapModulation();

		dglDrawRect(rect,mProfile->mBorderColor);
		dglDrawRectFill(rect,mProfile->mFillColor);
	}

	if(m_pText[0])
	{
		dglSetBitmapModulation(mProfile->mFontColor);
		dglDrawText(mProfile->mFont,offset + Point2I(mColSpacing,mRowSpacing), m_pText);
	}

	Parent::renderChildControls(offset,updateRect);
}



void GuiCellArrayControl::resize(const Point2I &newPosition, const Point2I &newExtent)
{
	Point2I extent(	m_nCols * (m_cellAreaSize.x + mColSpacing) + mColSpacing,
							m_nRows  * (m_cellAreaSize.y + mRowSpacing) + mRowSpacing);
	if(m_pText[0])
		extent.y += mProfile->mFont->getHeight() + mRowSpacing;

	Parent::resize(newPosition,extent);
}

///////////////////////////////////////////////////////////////////////
//
ConsoleMethod(GuiCellArrayControl,updateGameIcon,void,3,4,"object.updateGameIcon(index,name)")
{
	S32 nIndex = dAtoi(argv[2]);

	if(argc < 4)
	{
		object->updateGameIcon(nIndex);
	}
	else
	{
		StringTableEntry pCellName = StringTable->insert(argv[3]);
		object->setCellName(nIndex,pCellName);
	}
}

⌨️ 快捷键说明

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